api.c 1.9 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947369483694936950369513695236953369543695536956369573695836959369603696136962369633696436965369663696736968369693697036971369723697336974369753697636977369783697936980369813698236983369843698536986369873698836989369903699136992369933699436995369963699736998369993700037001370023700337004370053700637007370083700937010370113701237013370143701537016370173701837019370203702137022370233702437025370263702737028370293703037031370323703337034370353703637037370383703937040370413704237043370443704537046370473704837049370503705137052370533705437055370563705737058370593706037061370623706337064370653706637067370683706937070370713707237073370743707537076370773707837079370803708137082370833708437085370863708737088370893709037091370923709337094370953709637097370983709937100371013710237103371043710537106371073710837109371103711137112371133711437115371163711737118371193712037121371223712337124371253712637127371283712937130371313713237133371343713537136371373713837139371403714137142371433714437145371463714737148371493715037151371523715337154371553715637157371583715937160371613716237163371643716537166371673716837169371703717137172371733717437175371763717737178371793718037181371823718337184371853718637187371883718937190371913719237193371943719537196371973719837199372003720137202372033720437205372063720737208372093721037211372123721337214372153721637217372183721937220372213722237223372243722537226372273722837229372303723137232372333723437235372363723737238372393724037241372423724337244372453724637247372483724937250372513725237253372543725537256372573725837259372603726137262372633726437265372663726737268372693727037271372723727337274372753727637277372783727937280372813728237283372843728537286372873728837289372903729137292372933729437295372963729737298372993730037301373023730337304373053730637307373083730937310373113731237313373143731537316373173731837319373203732137322373233732437325373263732737328373293733037331373323733337334373353733637337373383733937340373413734237343373443734537346373473734837349373503735137352373533735437355373563735737358373593736037361373623736337364373653736637367373683736937370373713737237373373743737537376373773737837379373803738137382373833738437385373863738737388373893739037391373923739337394373953739637397373983739937400374013740237403374043740537406374073740837409374103741137412374133741437415374163741737418374193742037421374223742337424374253742637427374283742937430374313743237433374343743537436374373743837439374403744137442374433744437445374463744737448374493745037451374523745337454374553745637457374583745937460374613746237463374643746537466374673746837469374703747137472374733747437475374763747737478374793748037481374823748337484374853748637487374883748937490374913749237493374943749537496374973749837499375003750137502375033750437505375063750737508375093751037511375123751337514375153751637517375183751937520375213752237523375243752537526375273752837529375303753137532375333753437535375363753737538375393754037541375423754337544375453754637547375483754937550375513755237553375543755537556375573755837559375603756137562375633756437565375663756737568375693757037571375723757337574375753757637577375783757937580375813758237583375843758537586375873758837589375903759137592375933759437595375963759737598375993760037601376023760337604376053760637607376083760937610376113761237613376143761537616376173761837619376203762137622376233762437625376263762737628376293763037631376323763337634376353763637637376383763937640376413764237643376443764537646376473764837649376503765137652376533765437655376563765737658376593766037661376623766337664376653766637667376683766937670376713767237673376743767537676376773767837679376803768137682376833768437685376863768737688376893769037691376923769337694376953769637697376983769937700377013770237703377043770537706377073770837709377103771137712377133771437715377163771737718377193772037721377223772337724377253772637727377283772937730377313773237733377343773537736377373773837739377403774137742377433774437745377463774737748377493775037751377523775337754377553775637757377583775937760377613776237763377643776537766377673776837769377703777137772377733777437775377763777737778377793778037781377823778337784377853778637787377883778937790377913779237793377943779537796377973779837799378003780137802378033780437805378063780737808378093781037811378123781337814378153781637817378183781937820378213782237823378243782537826378273782837829378303783137832378333783437835378363783737838378393784037841378423784337844378453784637847378483784937850378513785237853378543785537856378573785837859378603786137862378633786437865378663786737868378693787037871378723787337874378753787637877378783787937880378813788237883378843788537886378873788837889378903789137892378933789437895378963789737898378993790037901379023790337904379053790637907379083790937910379113791237913379143791537916379173791837919379203792137922379233792437925379263792737928379293793037931379323793337934379353793637937379383793937940379413794237943379443794537946379473794837949379503795137952379533795437955379563795737958379593796037961379623796337964379653796637967379683796937970379713797237973379743797537976379773797837979379803798137982379833798437985379863798737988379893799037991379923799337994379953799637997379983799938000380013800238003380043800538006380073800838009380103801138012380133801438015380163801738018380193802038021380223802338024380253802638027380283802938030380313803238033380343803538036380373803838039380403804138042380433804438045380463804738048380493805038051380523805338054380553805638057380583805938060380613806238063380643806538066380673806838069380703807138072380733807438075380763807738078380793808038081380823808338084380853808638087380883808938090380913809238093380943809538096380973809838099381003810138102381033810438105381063810738108381093811038111381123811338114381153811638117381183811938120381213812238123381243812538126381273812838129381303813138132381333813438135381363813738138381393814038141381423814338144381453814638147381483814938150381513815238153381543815538156381573815838159381603816138162381633816438165381663816738168381693817038171381723817338174381753817638177381783817938180381813818238183381843818538186381873818838189381903819138192381933819438195381963819738198381993820038201382023820338204382053820638207382083820938210382113821238213382143821538216382173821838219382203822138222382233822438225382263822738228382293823038231382323823338234382353823638237382383823938240382413824238243382443824538246382473824838249382503825138252382533825438255382563825738258382593826038261382623826338264382653826638267382683826938270382713827238273382743827538276382773827838279382803828138282382833828438285382863828738288382893829038291382923829338294382953829638297382983829938300383013830238303383043830538306383073830838309383103831138312383133831438315383163831738318383193832038321383223832338324383253832638327383283832938330383313833238333383343833538336383373833838339383403834138342383433834438345383463834738348383493835038351383523835338354383553835638357383583835938360383613836238363383643836538366383673836838369383703837138372383733837438375383763837738378383793838038381383823838338384383853838638387383883838938390383913839238393383943839538396383973839838399384003840138402384033840438405384063840738408384093841038411384123841338414384153841638417384183841938420384213842238423384243842538426384273842838429384303843138432384333843438435384363843738438384393844038441384423844338444384453844638447384483844938450384513845238453384543845538456384573845838459384603846138462384633846438465384663846738468384693847038471384723847338474384753847638477384783847938480384813848238483384843848538486384873848838489384903849138492384933849438495384963849738498384993850038501385023850338504385053850638507385083850938510385113851238513385143851538516385173851838519385203852138522385233852438525385263852738528385293853038531385323853338534385353853638537385383853938540385413854238543385443854538546385473854838549385503855138552385533855438555385563855738558385593856038561385623856338564385653856638567385683856938570385713857238573385743857538576385773857838579385803858138582385833858438585385863858738588385893859038591385923859338594385953859638597385983859938600386013860238603386043860538606386073860838609386103861138612386133861438615386163861738618386193862038621386223862338624386253862638627386283862938630386313863238633386343863538636386373863838639386403864138642386433864438645386463864738648386493865038651386523865338654386553865638657386583865938660386613866238663386643866538666386673866838669386703867138672386733867438675386763867738678386793868038681386823868338684386853868638687386883868938690386913869238693386943869538696386973869838699387003870138702387033870438705387063870738708387093871038711387123871338714387153871638717387183871938720387213872238723387243872538726387273872838729387303873138732387333873438735387363873738738387393874038741387423874338744387453874638747387483874938750387513875238753387543875538756387573875838759387603876138762387633876438765387663876738768387693877038771387723877338774387753877638777387783877938780387813878238783387843878538786387873878838789387903879138792387933879438795387963879738798387993880038801388023880338804388053880638807388083880938810388113881238813388143881538816388173881838819388203882138822388233882438825388263882738828388293883038831388323883338834388353883638837388383883938840388413884238843388443884538846388473884838849388503885138852388533885438855388563885738858388593886038861388623886338864388653886638867388683886938870388713887238873388743887538876388773887838879388803888138882388833888438885388863888738888388893889038891388923889338894388953889638897388983889938900389013890238903389043890538906389073890838909389103891138912389133891438915389163891738918389193892038921389223892338924389253892638927389283892938930389313893238933389343893538936389373893838939389403894138942389433894438945389463894738948389493895038951389523895338954389553895638957389583895938960389613896238963389643896538966389673896838969389703897138972389733897438975389763897738978389793898038981389823898338984389853898638987389883898938990389913899238993389943899538996389973899838999390003900139002390033900439005390063900739008390093901039011390123901339014390153901639017390183901939020390213902239023390243902539026390273902839029390303903139032390333903439035390363903739038390393904039041390423904339044390453904639047390483904939050390513905239053390543905539056390573905839059390603906139062390633906439065390663906739068390693907039071390723907339074390753907639077390783907939080390813908239083390843908539086390873908839089390903909139092390933909439095390963909739098390993910039101391023910339104391053910639107391083910939110391113911239113391143911539116391173911839119391203912139122391233912439125391263912739128391293913039131391323913339134391353913639137391383913939140391413914239143391443914539146391473914839149391503915139152391533915439155391563915739158391593916039161391623916339164391653916639167391683916939170391713917239173391743917539176391773917839179391803918139182391833918439185391863918739188391893919039191391923919339194391953919639197391983919939200392013920239203392043920539206392073920839209392103921139212392133921439215392163921739218392193922039221392223922339224392253922639227392283922939230392313923239233392343923539236392373923839239392403924139242392433924439245392463924739248392493925039251392523925339254392553925639257392583925939260392613926239263392643926539266392673926839269392703927139272392733927439275392763927739278392793928039281392823928339284392853928639287392883928939290392913929239293392943929539296392973929839299393003930139302393033930439305393063930739308393093931039311393123931339314393153931639317393183931939320393213932239323393243932539326393273932839329393303933139332393333933439335393363933739338393393934039341393423934339344393453934639347393483934939350393513935239353393543935539356393573935839359393603936139362393633936439365393663936739368393693937039371393723937339374393753937639377393783937939380393813938239383393843938539386393873938839389393903939139392393933939439395393963939739398393993940039401394023940339404394053940639407394083940939410394113941239413394143941539416394173941839419394203942139422394233942439425394263942739428394293943039431394323943339434394353943639437394383943939440394413944239443394443944539446394473944839449394503945139452394533945439455394563945739458394593946039461394623946339464394653946639467394683946939470394713947239473394743947539476394773947839479394803948139482394833948439485394863948739488394893949039491394923949339494394953949639497394983949939500395013950239503395043950539506395073950839509395103951139512395133951439515395163951739518395193952039521395223952339524395253952639527395283952939530395313953239533395343953539536395373953839539395403954139542395433954439545395463954739548395493955039551395523955339554395553955639557395583955939560395613956239563395643956539566395673956839569395703957139572395733957439575395763957739578395793958039581395823958339584395853958639587395883958939590395913959239593395943959539596395973959839599396003960139602396033960439605396063960739608396093961039611396123961339614396153961639617396183961939620396213962239623396243962539626396273962839629396303963139632396333963439635396363963739638396393964039641396423964339644396453964639647396483964939650396513965239653396543965539656396573965839659396603966139662396633966439665396663966739668396693967039671396723967339674396753967639677396783967939680396813968239683396843968539686396873968839689396903969139692396933969439695396963969739698396993970039701397023970339704397053970639707397083970939710397113971239713397143971539716397173971839719397203972139722397233972439725397263972739728397293973039731397323973339734397353973639737397383973939740397413974239743397443974539746397473974839749397503975139752397533975439755397563975739758397593976039761397623976339764397653976639767397683976939770397713977239773397743977539776397773977839779397803978139782397833978439785397863978739788397893979039791397923979339794397953979639797397983979939800398013980239803398043980539806398073980839809398103981139812398133981439815398163981739818398193982039821398223982339824398253982639827398283982939830398313983239833398343983539836398373983839839398403984139842398433984439845398463984739848398493985039851398523985339854398553985639857398583985939860398613986239863398643986539866398673986839869398703987139872398733987439875398763987739878398793988039881398823988339884398853988639887398883988939890398913989239893398943989539896398973989839899399003990139902399033990439905399063990739908399093991039911399123991339914399153991639917399183991939920399213992239923399243992539926399273992839929399303993139932399333993439935399363993739938399393994039941399423994339944399453994639947399483994939950399513995239953399543995539956399573995839959399603996139962399633996439965399663996739968399693997039971399723997339974399753997639977399783997939980399813998239983399843998539986399873998839989399903999139992399933999439995399963999739998399994000040001400024000340004400054000640007400084000940010400114001240013400144001540016400174001840019400204002140022400234002440025400264002740028400294003040031400324003340034400354003640037400384003940040400414004240043400444004540046400474004840049400504005140052400534005440055400564005740058400594006040061400624006340064400654006640067400684006940070400714007240073400744007540076400774007840079400804008140082400834008440085400864008740088400894009040091400924009340094400954009640097400984009940100401014010240103401044010540106401074010840109401104011140112401134011440115401164011740118401194012040121401224012340124401254012640127401284012940130401314013240133401344013540136401374013840139401404014140142401434014440145401464014740148401494015040151401524015340154401554015640157401584015940160401614016240163401644016540166401674016840169401704017140172401734017440175401764017740178401794018040181401824018340184401854018640187401884018940190401914019240193401944019540196401974019840199402004020140202402034020440205402064020740208402094021040211402124021340214402154021640217402184021940220402214022240223402244022540226402274022840229402304023140232402334023440235402364023740238402394024040241402424024340244402454024640247402484024940250402514025240253402544025540256402574025840259402604026140262402634026440265402664026740268402694027040271402724027340274402754027640277402784027940280402814028240283402844028540286402874028840289402904029140292402934029440295402964029740298402994030040301403024030340304403054030640307403084030940310403114031240313403144031540316403174031840319403204032140322403234032440325403264032740328403294033040331403324033340334403354033640337403384033940340403414034240343403444034540346403474034840349403504035140352403534035440355403564035740358403594036040361403624036340364403654036640367403684036940370403714037240373403744037540376403774037840379403804038140382403834038440385403864038740388403894039040391403924039340394403954039640397403984039940400404014040240403404044040540406404074040840409404104041140412404134041440415404164041740418404194042040421404224042340424404254042640427404284042940430404314043240433404344043540436404374043840439404404044140442404434044440445404464044740448404494045040451404524045340454404554045640457404584045940460404614046240463404644046540466404674046840469404704047140472404734047440475404764047740478404794048040481404824048340484404854048640487404884048940490404914049240493404944049540496404974049840499405004050140502405034050440505405064050740508405094051040511405124051340514405154051640517405184051940520405214052240523405244052540526405274052840529405304053140532405334053440535405364053740538405394054040541405424054340544405454054640547405484054940550405514055240553405544055540556405574055840559405604056140562405634056440565405664056740568405694057040571405724057340574405754057640577405784057940580405814058240583405844058540586405874058840589405904059140592405934059440595405964059740598405994060040601406024060340604406054060640607406084060940610406114061240613406144061540616406174061840619406204062140622406234062440625406264062740628406294063040631406324063340634406354063640637406384063940640406414064240643406444064540646406474064840649406504065140652406534065440655406564065740658406594066040661406624066340664406654066640667406684066940670406714067240673406744067540676406774067840679406804068140682406834068440685406864068740688406894069040691406924069340694406954069640697406984069940700407014070240703407044070540706407074070840709407104071140712407134071440715407164071740718407194072040721407224072340724407254072640727407284072940730407314073240733407344073540736407374073840739407404074140742407434074440745407464074740748407494075040751407524075340754407554075640757407584075940760407614076240763407644076540766407674076840769407704077140772407734077440775407764077740778407794078040781407824078340784407854078640787407884078940790407914079240793407944079540796407974079840799408004080140802408034080440805408064080740808408094081040811408124081340814408154081640817408184081940820408214082240823408244082540826408274082840829408304083140832408334083440835408364083740838408394084040841408424084340844408454084640847408484084940850408514085240853408544085540856408574085840859408604086140862408634086440865408664086740868408694087040871408724087340874408754087640877408784087940880408814088240883408844088540886408874088840889408904089140892408934089440895408964089740898408994090040901409024090340904409054090640907409084090940910409114091240913409144091540916409174091840919409204092140922409234092440925409264092740928409294093040931409324093340934409354093640937409384093940940409414094240943409444094540946409474094840949409504095140952409534095440955409564095740958409594096040961409624096340964409654096640967409684096940970409714097240973409744097540976409774097840979409804098140982409834098440985409864098740988409894099040991409924099340994409954099640997409984099941000410014100241003410044100541006410074100841009410104101141012410134101441015410164101741018410194102041021410224102341024410254102641027410284102941030410314103241033410344103541036410374103841039410404104141042410434104441045410464104741048410494105041051410524105341054410554105641057410584105941060410614106241063410644106541066410674106841069410704107141072410734107441075410764107741078410794108041081410824108341084410854108641087410884108941090410914109241093410944109541096410974109841099411004110141102411034110441105411064110741108411094111041111411124111341114411154111641117411184111941120411214112241123411244112541126411274112841129411304113141132411334113441135411364113741138411394114041141411424114341144411454114641147411484114941150411514115241153411544115541156411574115841159411604116141162411634116441165411664116741168411694117041171411724117341174411754117641177411784117941180411814118241183411844118541186411874118841189411904119141192411934119441195411964119741198411994120041201412024120341204412054120641207412084120941210412114121241213412144121541216412174121841219412204122141222412234122441225412264122741228412294123041231412324123341234412354123641237412384123941240412414124241243412444124541246412474124841249412504125141252412534125441255412564125741258412594126041261412624126341264412654126641267412684126941270412714127241273412744127541276412774127841279412804128141282412834128441285412864128741288412894129041291412924129341294412954129641297412984129941300413014130241303413044130541306413074130841309413104131141312413134131441315413164131741318413194132041321413224132341324413254132641327413284132941330413314133241333413344133541336413374133841339413404134141342413434134441345413464134741348413494135041351413524135341354413554135641357413584135941360413614136241363413644136541366413674136841369413704137141372413734137441375413764137741378413794138041381413824138341384413854138641387413884138941390413914139241393413944139541396413974139841399414004140141402414034140441405414064140741408414094141041411414124141341414414154141641417414184141941420414214142241423414244142541426414274142841429414304143141432414334143441435414364143741438414394144041441414424144341444414454144641447414484144941450414514145241453414544145541456414574145841459414604146141462414634146441465414664146741468414694147041471414724147341474414754147641477414784147941480414814148241483414844148541486414874148841489414904149141492414934149441495414964149741498414994150041501415024150341504415054150641507415084150941510415114151241513415144151541516415174151841519415204152141522415234152441525415264152741528415294153041531415324153341534415354153641537415384153941540415414154241543415444154541546415474154841549415504155141552415534155441555415564155741558415594156041561415624156341564415654156641567415684156941570415714157241573415744157541576415774157841579415804158141582415834158441585415864158741588415894159041591415924159341594415954159641597415984159941600416014160241603416044160541606416074160841609416104161141612416134161441615416164161741618416194162041621416224162341624416254162641627416284162941630416314163241633416344163541636416374163841639416404164141642416434164441645416464164741648416494165041651416524165341654416554165641657416584165941660416614166241663416644166541666416674166841669416704167141672416734167441675416764167741678416794168041681416824168341684416854168641687416884168941690416914169241693416944169541696416974169841699417004170141702417034170441705417064170741708417094171041711417124171341714417154171641717417184171941720417214172241723417244172541726417274172841729417304173141732417334173441735417364173741738417394174041741417424174341744417454174641747417484174941750417514175241753417544175541756417574175841759417604176141762417634176441765417664176741768417694177041771417724177341774417754177641777417784177941780417814178241783417844178541786417874178841789417904179141792417934179441795417964179741798417994180041801418024180341804418054180641807418084180941810418114181241813418144181541816418174181841819418204182141822418234182441825418264182741828418294183041831418324183341834418354183641837418384183941840418414184241843418444184541846418474184841849418504185141852418534185441855418564185741858418594186041861418624186341864418654186641867418684186941870418714187241873418744187541876418774187841879418804188141882418834188441885418864188741888418894189041891418924189341894418954189641897418984189941900419014190241903419044190541906419074190841909419104191141912419134191441915419164191741918419194192041921419224192341924419254192641927419284192941930419314193241933419344193541936419374193841939419404194141942419434194441945419464194741948419494195041951419524195341954419554195641957419584195941960419614196241963419644196541966419674196841969419704197141972419734197441975419764197741978419794198041981419824198341984419854198641987419884198941990419914199241993419944199541996419974199841999420004200142002420034200442005420064200742008420094201042011420124201342014420154201642017420184201942020420214202242023420244202542026420274202842029420304203142032420334203442035420364203742038420394204042041420424204342044420454204642047420484204942050420514205242053420544205542056420574205842059420604206142062420634206442065420664206742068420694207042071420724207342074420754207642077420784207942080420814208242083420844208542086420874208842089420904209142092420934209442095420964209742098420994210042101421024210342104421054210642107421084210942110421114211242113421144211542116421174211842119421204212142122421234212442125421264212742128421294213042131421324213342134421354213642137421384213942140421414214242143421444214542146421474214842149421504215142152421534215442155421564215742158421594216042161421624216342164421654216642167421684216942170421714217242173421744217542176421774217842179421804218142182421834218442185421864218742188421894219042191421924219342194421954219642197421984219942200422014220242203422044220542206422074220842209422104221142212422134221442215422164221742218422194222042221422224222342224422254222642227422284222942230422314223242233422344223542236422374223842239422404224142242422434224442245422464224742248422494225042251422524225342254422554225642257422584225942260422614226242263422644226542266422674226842269422704227142272422734227442275422764227742278422794228042281422824228342284422854228642287422884228942290422914229242293422944229542296422974229842299423004230142302423034230442305423064230742308423094231042311423124231342314423154231642317423184231942320423214232242323423244232542326423274232842329423304233142332423334233442335423364233742338423394234042341423424234342344423454234642347423484234942350423514235242353423544235542356423574235842359423604236142362423634236442365423664236742368423694237042371423724237342374423754237642377423784237942380423814238242383423844238542386423874238842389423904239142392423934239442395423964239742398423994240042401424024240342404424054240642407424084240942410424114241242413424144241542416424174241842419424204242142422424234242442425424264242742428424294243042431424324243342434424354243642437424384243942440424414244242443424444244542446424474244842449424504245142452424534245442455424564245742458424594246042461424624246342464424654246642467424684246942470424714247242473424744247542476424774247842479424804248142482424834248442485424864248742488424894249042491424924249342494424954249642497424984249942500425014250242503425044250542506425074250842509425104251142512425134251442515425164251742518425194252042521425224252342524425254252642527425284252942530425314253242533425344253542536425374253842539425404254142542425434254442545425464254742548425494255042551425524255342554425554255642557425584255942560425614256242563425644256542566425674256842569425704257142572425734257442575425764257742578425794258042581425824258342584425854258642587425884258942590425914259242593425944259542596425974259842599426004260142602426034260442605426064260742608426094261042611426124261342614426154261642617426184261942620426214262242623426244262542626426274262842629426304263142632426334263442635426364263742638426394264042641426424264342644426454264642647426484264942650426514265242653426544265542656426574265842659426604266142662426634266442665426664266742668426694267042671426724267342674426754267642677426784267942680426814268242683426844268542686426874268842689426904269142692426934269442695426964269742698426994270042701427024270342704427054270642707427084270942710427114271242713427144271542716427174271842719427204272142722427234272442725427264272742728427294273042731427324273342734427354273642737427384273942740427414274242743427444274542746427474274842749427504275142752427534275442755427564275742758427594276042761427624276342764427654276642767427684276942770427714277242773427744277542776427774277842779427804278142782427834278442785427864278742788427894279042791427924279342794427954279642797427984279942800428014280242803428044280542806428074280842809428104281142812428134281442815428164281742818428194282042821428224282342824428254282642827428284282942830428314283242833428344283542836428374283842839428404284142842428434284442845428464284742848428494285042851428524285342854428554285642857428584285942860428614286242863428644286542866428674286842869428704287142872428734287442875428764287742878428794288042881428824288342884428854288642887428884288942890428914289242893428944289542896428974289842899429004290142902429034290442905429064290742908429094291042911429124291342914429154291642917429184291942920429214292242923429244292542926429274292842929429304293142932429334293442935429364293742938429394294042941429424294342944429454294642947429484294942950429514295242953429544295542956429574295842959429604296142962429634296442965429664296742968429694297042971429724297342974429754297642977429784297942980429814298242983429844298542986429874298842989429904299142992429934299442995429964299742998429994300043001430024300343004430054300643007430084300943010430114301243013430144301543016430174301843019430204302143022430234302443025430264302743028430294303043031430324303343034430354303643037430384303943040430414304243043430444304543046430474304843049430504305143052430534305443055430564305743058430594306043061430624306343064430654306643067430684306943070430714307243073430744307543076430774307843079430804308143082430834308443085430864308743088430894309043091430924309343094430954309643097430984309943100431014310243103431044310543106431074310843109431104311143112431134311443115431164311743118431194312043121431224312343124431254312643127431284312943130431314313243133431344313543136431374313843139431404314143142431434314443145431464314743148431494315043151431524315343154431554315643157431584315943160431614316243163431644316543166431674316843169431704317143172431734317443175431764317743178431794318043181431824318343184431854318643187431884318943190431914319243193431944319543196431974319843199432004320143202432034320443205432064320743208432094321043211432124321343214432154321643217432184321943220432214322243223432244322543226432274322843229432304323143232432334323443235432364323743238432394324043241432424324343244432454324643247432484324943250432514325243253432544325543256432574325843259432604326143262432634326443265432664326743268432694327043271432724327343274432754327643277432784327943280432814328243283432844328543286432874328843289432904329143292432934329443295432964329743298432994330043301433024330343304433054330643307433084330943310433114331243313433144331543316433174331843319433204332143322433234332443325433264332743328433294333043331433324333343334433354333643337433384333943340433414334243343433444334543346433474334843349433504335143352433534335443355433564335743358433594336043361433624336343364433654336643367433684336943370433714337243373433744337543376433774337843379433804338143382433834338443385433864338743388433894339043391433924339343394433954339643397433984339943400434014340243403434044340543406434074340843409434104341143412434134341443415434164341743418434194342043421434224342343424434254342643427434284342943430434314343243433434344343543436434374343843439434404344143442434434344443445434464344743448434494345043451434524345343454434554345643457434584345943460434614346243463434644346543466434674346843469434704347143472434734347443475434764347743478434794348043481434824348343484434854348643487434884348943490434914349243493434944349543496434974349843499435004350143502435034350443505435064350743508435094351043511435124351343514435154351643517435184351943520435214352243523435244352543526435274352843529435304353143532435334353443535435364353743538435394354043541435424354343544435454354643547435484354943550435514355243553435544355543556435574355843559435604356143562435634356443565435664356743568435694357043571435724357343574435754357643577435784357943580435814358243583435844358543586435874358843589435904359143592435934359443595435964359743598435994360043601436024360343604436054360643607436084360943610436114361243613436144361543616436174361843619436204362143622436234362443625436264362743628436294363043631436324363343634436354363643637436384363943640436414364243643436444364543646436474364843649436504365143652436534365443655436564365743658436594366043661436624366343664436654366643667436684366943670436714367243673436744367543676436774367843679436804368143682436834368443685436864368743688436894369043691436924369343694436954369643697436984369943700437014370243703437044370543706437074370843709437104371143712437134371443715437164371743718437194372043721437224372343724437254372643727437284372943730437314373243733437344373543736437374373843739437404374143742437434374443745437464374743748437494375043751437524375343754437554375643757437584375943760437614376243763437644376543766437674376843769437704377143772437734377443775437764377743778437794378043781437824378343784437854378643787437884378943790437914379243793437944379543796437974379843799438004380143802438034380443805438064380743808438094381043811438124381343814438154381643817438184381943820438214382243823438244382543826438274382843829438304383143832438334383443835438364383743838438394384043841438424384343844438454384643847438484384943850438514385243853438544385543856438574385843859438604386143862438634386443865438664386743868438694387043871438724387343874438754387643877438784387943880438814388243883438844388543886438874388843889438904389143892438934389443895438964389743898438994390043901439024390343904439054390643907439084390943910439114391243913439144391543916439174391843919439204392143922439234392443925439264392743928439294393043931439324393343934439354393643937439384393943940439414394243943439444394543946439474394843949439504395143952439534395443955439564395743958439594396043961439624396343964439654396643967439684396943970439714397243973439744397543976439774397843979439804398143982439834398443985439864398743988439894399043991439924399343994439954399643997439984399944000440014400244003440044400544006440074400844009440104401144012440134401444015440164401744018440194402044021440224402344024440254402644027440284402944030440314403244033440344403544036440374403844039440404404144042440434404444045440464404744048440494405044051440524405344054440554405644057440584405944060440614406244063440644406544066440674406844069440704407144072440734407444075440764407744078440794408044081440824408344084440854408644087440884408944090440914409244093440944409544096440974409844099441004410144102441034410444105441064410744108441094411044111441124411344114441154411644117441184411944120441214412244123441244412544126441274412844129441304413144132441334413444135441364413744138441394414044141441424414344144441454414644147441484414944150441514415244153441544415544156441574415844159441604416144162441634416444165441664416744168441694417044171441724417344174441754417644177441784417944180441814418244183441844418544186441874418844189441904419144192441934419444195441964419744198441994420044201442024420344204442054420644207442084420944210442114421244213442144421544216442174421844219442204422144222442234422444225442264422744228442294423044231442324423344234442354423644237442384423944240442414424244243442444424544246442474424844249442504425144252442534425444255442564425744258442594426044261442624426344264442654426644267442684426944270442714427244273442744427544276442774427844279442804428144282442834428444285442864428744288442894429044291442924429344294442954429644297442984429944300443014430244303443044430544306443074430844309443104431144312443134431444315443164431744318443194432044321443224432344324443254432644327443284432944330443314433244333443344433544336443374433844339443404434144342443434434444345443464434744348443494435044351443524435344354443554435644357443584435944360443614436244363443644436544366443674436844369443704437144372443734437444375443764437744378443794438044381443824438344384443854438644387443884438944390443914439244393443944439544396443974439844399444004440144402444034440444405444064440744408444094441044411444124441344414444154441644417444184441944420444214442244423444244442544426444274442844429444304443144432444334443444435444364443744438444394444044441444424444344444444454444644447444484444944450444514445244453444544445544456444574445844459444604446144462444634446444465444664446744468444694447044471444724447344474444754447644477444784447944480444814448244483444844448544486444874448844489444904449144492444934449444495444964449744498444994450044501445024450344504445054450644507445084450944510445114451244513445144451544516445174451844519445204452144522445234452444525445264452744528445294453044531445324453344534445354453644537445384453944540445414454244543445444454544546445474454844549445504455144552445534455444555445564455744558445594456044561445624456344564445654456644567445684456944570445714457244573445744457544576445774457844579445804458144582445834458444585445864458744588445894459044591445924459344594445954459644597445984459944600446014460244603446044460544606446074460844609446104461144612446134461444615446164461744618446194462044621446224462344624446254462644627446284462944630446314463244633446344463544636446374463844639446404464144642446434464444645446464464744648446494465044651446524465344654446554465644657446584465944660446614466244663446644466544666446674466844669446704467144672446734467444675446764467744678446794468044681446824468344684446854468644687446884468944690446914469244693446944469544696446974469844699447004470144702447034470444705447064470744708447094471044711447124471344714447154471644717447184471944720447214472244723447244472544726447274472844729447304473144732447334473444735447364473744738447394474044741447424474344744447454474644747447484474944750447514475244753447544475544756447574475844759447604476144762447634476444765447664476744768447694477044771447724477344774447754477644777447784477944780447814478244783447844478544786447874478844789447904479144792447934479444795447964479744798447994480044801448024480344804448054480644807448084480944810448114481244813448144481544816448174481844819448204482144822448234482444825448264482744828448294483044831448324483344834448354483644837448384483944840448414484244843448444484544846448474484844849448504485144852448534485444855448564485744858448594486044861448624486344864448654486644867448684486944870448714487244873448744487544876448774487844879448804488144882448834488444885448864488744888448894489044891448924489344894448954489644897448984489944900449014490244903449044490544906449074490844909449104491144912449134491444915449164491744918449194492044921449224492344924449254492644927449284492944930449314493244933449344493544936449374493844939449404494144942449434494444945449464494744948449494495044951449524495344954449554495644957449584495944960449614496244963449644496544966449674496844969449704497144972449734497444975449764497744978449794498044981449824498344984449854498644987449884498944990449914499244993449944499544996449974499844999450004500145002450034500445005450064500745008450094501045011450124501345014450154501645017450184501945020450214502245023450244502545026450274502845029450304503145032450334503445035450364503745038450394504045041450424504345044450454504645047450484504945050450514505245053450544505545056450574505845059450604506145062450634506445065450664506745068450694507045071450724507345074450754507645077450784507945080450814508245083450844508545086450874508845089450904509145092450934509445095450964509745098450994510045101451024510345104451054510645107451084510945110451114511245113451144511545116451174511845119451204512145122451234512445125451264512745128451294513045131451324513345134451354513645137451384513945140451414514245143451444514545146451474514845149451504515145152451534515445155451564515745158451594516045161451624516345164451654516645167451684516945170451714517245173451744517545176451774517845179451804518145182451834518445185451864518745188451894519045191451924519345194451954519645197451984519945200452014520245203452044520545206452074520845209452104521145212452134521445215452164521745218452194522045221452224522345224452254522645227452284522945230452314523245233452344523545236452374523845239452404524145242452434524445245452464524745248452494525045251452524525345254452554525645257452584525945260452614526245263452644526545266452674526845269452704527145272452734527445275452764527745278452794528045281452824528345284452854528645287452884528945290452914529245293452944529545296452974529845299453004530145302453034530445305453064530745308453094531045311453124531345314453154531645317453184531945320453214532245323453244532545326453274532845329453304533145332453334533445335453364533745338453394534045341453424534345344453454534645347453484534945350453514535245353453544535545356453574535845359453604536145362453634536445365453664536745368453694537045371453724537345374453754537645377453784537945380453814538245383453844538545386453874538845389453904539145392453934539445395453964539745398453994540045401454024540345404454054540645407454084540945410454114541245413454144541545416454174541845419454204542145422454234542445425454264542745428454294543045431454324543345434454354543645437454384543945440454414544245443454444544545446454474544845449454504545145452454534545445455454564545745458454594546045461454624546345464454654546645467454684546945470454714547245473454744547545476454774547845479454804548145482454834548445485454864548745488454894549045491454924549345494454954549645497454984549945500455014550245503455044550545506455074550845509455104551145512455134551445515455164551745518455194552045521455224552345524455254552645527455284552945530455314553245533455344553545536455374553845539455404554145542455434554445545455464554745548455494555045551455524555345554455554555645557455584555945560455614556245563455644556545566455674556845569455704557145572455734557445575455764557745578455794558045581455824558345584455854558645587455884558945590455914559245593455944559545596455974559845599456004560145602456034560445605456064560745608456094561045611456124561345614456154561645617456184561945620456214562245623456244562545626456274562845629456304563145632456334563445635456364563745638456394564045641456424564345644456454564645647456484564945650456514565245653456544565545656456574565845659456604566145662456634566445665456664566745668456694567045671456724567345674456754567645677456784567945680456814568245683456844568545686456874568845689456904569145692456934569445695456964569745698456994570045701457024570345704457054570645707457084570945710457114571245713457144571545716457174571845719457204572145722457234572445725457264572745728457294573045731457324573345734457354573645737457384573945740457414574245743457444574545746457474574845749457504575145752457534575445755457564575745758457594576045761457624576345764457654576645767457684576945770457714577245773457744577545776457774577845779457804578145782457834578445785457864578745788457894579045791457924579345794457954579645797457984579945800458014580245803458044580545806458074580845809458104581145812458134581445815458164581745818458194582045821458224582345824458254582645827458284582945830458314583245833458344583545836458374583845839458404584145842458434584445845458464584745848458494585045851458524585345854458554585645857458584585945860458614586245863458644586545866458674586845869458704587145872458734587445875458764587745878458794588045881458824588345884458854588645887458884588945890458914589245893458944589545896458974589845899459004590145902459034590445905459064590745908459094591045911459124591345914459154591645917459184591945920459214592245923459244592545926459274592845929459304593145932459334593445935459364593745938459394594045941459424594345944459454594645947459484594945950459514595245953459544595545956459574595845959459604596145962459634596445965459664596745968459694597045971459724597345974459754597645977459784597945980459814598245983459844598545986459874598845989459904599145992459934599445995459964599745998459994600046001460024600346004460054600646007460084600946010460114601246013460144601546016460174601846019460204602146022460234602446025460264602746028460294603046031460324603346034460354603646037460384603946040460414604246043460444604546046460474604846049460504605146052460534605446055460564605746058460594606046061460624606346064460654606646067460684606946070460714607246073460744607546076460774607846079460804608146082460834608446085460864608746088460894609046091460924609346094460954609646097460984609946100461014610246103461044610546106461074610846109461104611146112461134611446115461164611746118461194612046121461224612346124461254612646127461284612946130461314613246133461344613546136461374613846139461404614146142461434614446145461464614746148461494615046151461524615346154461554615646157461584615946160461614616246163461644616546166461674616846169461704617146172461734617446175461764617746178461794618046181461824618346184461854618646187461884618946190461914619246193461944619546196461974619846199462004620146202462034620446205462064620746208462094621046211462124621346214462154621646217462184621946220462214622246223462244622546226462274622846229462304623146232462334623446235462364623746238462394624046241462424624346244462454624646247462484624946250462514625246253462544625546256462574625846259462604626146262462634626446265462664626746268462694627046271462724627346274462754627646277462784627946280462814628246283462844628546286462874628846289462904629146292462934629446295462964629746298462994630046301463024630346304463054630646307463084630946310463114631246313463144631546316463174631846319463204632146322463234632446325463264632746328463294633046331463324633346334463354633646337463384633946340463414634246343463444634546346463474634846349463504635146352463534635446355463564635746358463594636046361463624636346364463654636646367463684636946370463714637246373463744637546376463774637846379463804638146382463834638446385463864638746388463894639046391463924639346394463954639646397463984639946400464014640246403464044640546406464074640846409464104641146412464134641446415464164641746418464194642046421464224642346424464254642646427464284642946430464314643246433464344643546436464374643846439464404644146442464434644446445464464644746448464494645046451464524645346454464554645646457464584645946460464614646246463464644646546466464674646846469464704647146472464734647446475464764647746478464794648046481464824648346484464854648646487464884648946490464914649246493464944649546496464974649846499465004650146502465034650446505465064650746508465094651046511465124651346514465154651646517465184651946520465214652246523465244652546526465274652846529465304653146532465334653446535465364653746538465394654046541465424654346544465454654646547465484654946550465514655246553465544655546556465574655846559465604656146562465634656446565465664656746568465694657046571465724657346574465754657646577465784657946580465814658246583465844658546586465874658846589465904659146592465934659446595465964659746598465994660046601466024660346604466054660646607466084660946610466114661246613466144661546616466174661846619466204662146622466234662446625466264662746628466294663046631466324663346634466354663646637466384663946640466414664246643466444664546646466474664846649466504665146652466534665446655466564665746658466594666046661466624666346664466654666646667466684666946670466714667246673466744667546676466774667846679466804668146682466834668446685466864668746688466894669046691466924669346694466954669646697466984669946700467014670246703467044670546706467074670846709467104671146712467134671446715467164671746718467194672046721467224672346724467254672646727467284672946730467314673246733467344673546736467374673846739467404674146742467434674446745467464674746748467494675046751467524675346754467554675646757467584675946760467614676246763467644676546766467674676846769467704677146772467734677446775467764677746778467794678046781467824678346784467854678646787467884678946790467914679246793467944679546796467974679846799468004680146802468034680446805468064680746808468094681046811468124681346814468154681646817468184681946820468214682246823468244682546826468274682846829468304683146832468334683446835468364683746838468394684046841468424684346844468454684646847468484684946850468514685246853468544685546856468574685846859468604686146862468634686446865468664686746868468694687046871468724687346874468754687646877468784687946880468814688246883468844688546886468874688846889468904689146892468934689446895468964689746898468994690046901469024690346904469054690646907469084690946910469114691246913469144691546916469174691846919469204692146922469234692446925469264692746928469294693046931469324693346934469354693646937469384693946940469414694246943469444694546946469474694846949469504695146952469534695446955469564695746958469594696046961469624696346964469654696646967469684696946970469714697246973469744697546976469774697846979469804698146982469834698446985469864698746988469894699046991469924699346994469954699646997469984699947000470014700247003470044700547006470074700847009470104701147012470134701447015470164701747018470194702047021470224702347024470254702647027470284702947030470314703247033470344703547036470374703847039470404704147042470434704447045470464704747048470494705047051470524705347054470554705647057470584705947060470614706247063470644706547066470674706847069470704707147072470734707447075470764707747078470794708047081470824708347084470854708647087470884708947090470914709247093470944709547096470974709847099471004710147102471034710447105471064710747108471094711047111471124711347114471154711647117471184711947120471214712247123471244712547126471274712847129471304713147132471334713447135471364713747138471394714047141471424714347144471454714647147471484714947150471514715247153471544715547156471574715847159471604716147162471634716447165471664716747168471694717047171471724717347174471754717647177471784717947180471814718247183471844718547186471874718847189471904719147192471934719447195471964719747198471994720047201472024720347204472054720647207472084720947210472114721247213472144721547216472174721847219472204722147222472234722447225472264722747228472294723047231472324723347234472354723647237472384723947240472414724247243472444724547246472474724847249472504725147252472534725447255472564725747258472594726047261472624726347264472654726647267472684726947270472714727247273472744727547276472774727847279472804728147282472834728447285472864728747288472894729047291472924729347294472954729647297472984729947300473014730247303473044730547306473074730847309473104731147312473134731447315473164731747318473194732047321473224732347324473254732647327473284732947330473314733247333473344733547336473374733847339473404734147342473434734447345473464734747348473494735047351473524735347354473554735647357473584735947360473614736247363473644736547366473674736847369473704737147372473734737447375473764737747378473794738047381473824738347384473854738647387473884738947390473914739247393473944739547396473974739847399474004740147402474034740447405474064740747408474094741047411474124741347414474154741647417474184741947420474214742247423474244742547426474274742847429474304743147432474334743447435474364743747438474394744047441474424744347444474454744647447474484744947450474514745247453474544745547456474574745847459474604746147462474634746447465474664746747468474694747047471474724747347474474754747647477474784747947480474814748247483474844748547486474874748847489474904749147492474934749447495474964749747498474994750047501475024750347504475054750647507475084750947510475114751247513475144751547516475174751847519475204752147522475234752447525475264752747528475294753047531475324753347534475354753647537475384753947540475414754247543475444754547546475474754847549475504755147552475534755447555475564755747558475594756047561475624756347564475654756647567475684756947570475714757247573475744757547576475774757847579475804758147582475834758447585475864758747588475894759047591475924759347594475954759647597475984759947600476014760247603476044760547606476074760847609476104761147612476134761447615476164761747618476194762047621476224762347624476254762647627476284762947630476314763247633476344763547636476374763847639476404764147642476434764447645476464764747648476494765047651476524765347654476554765647657476584765947660476614766247663476644766547666476674766847669476704767147672476734767447675476764767747678476794768047681476824768347684476854768647687476884768947690476914769247693476944769547696476974769847699477004770147702477034770447705477064770747708477094771047711477124771347714477154771647717477184771947720477214772247723477244772547726477274772847729477304773147732477334773447735477364773747738477394774047741477424774347744477454774647747477484774947750477514775247753477544775547756477574775847759477604776147762477634776447765477664776747768477694777047771477724777347774477754777647777477784777947780477814778247783477844778547786477874778847789477904779147792477934779447795477964779747798477994780047801478024780347804478054780647807478084780947810478114781247813478144781547816478174781847819478204782147822478234782447825478264782747828478294783047831478324783347834478354783647837478384783947840478414784247843478444784547846478474784847849478504785147852478534785447855478564785747858478594786047861478624786347864478654786647867478684786947870478714787247873478744787547876478774787847879478804788147882478834788447885478864788747888478894789047891478924789347894478954789647897478984789947900479014790247903479044790547906479074790847909479104791147912479134791447915479164791747918479194792047921479224792347924479254792647927479284792947930479314793247933479344793547936479374793847939479404794147942479434794447945479464794747948479494795047951479524795347954479554795647957479584795947960479614796247963479644796547966479674796847969479704797147972479734797447975479764797747978479794798047981479824798347984479854798647987479884798947990479914799247993479944799547996479974799847999480004800148002480034800448005480064800748008480094801048011480124801348014480154801648017480184801948020480214802248023480244802548026480274802848029480304803148032480334803448035480364803748038480394804048041480424804348044480454804648047480484804948050480514805248053480544805548056480574805848059480604806148062480634806448065480664806748068480694807048071480724807348074480754807648077480784807948080480814808248083480844808548086480874808848089480904809148092480934809448095480964809748098480994810048101481024810348104481054810648107481084810948110481114811248113481144811548116481174811848119481204812148122481234812448125481264812748128481294813048131481324813348134481354813648137481384813948140481414814248143481444814548146481474814848149481504815148152481534815448155481564815748158481594816048161481624816348164481654816648167481684816948170481714817248173481744817548176481774817848179481804818148182481834818448185481864818748188481894819048191481924819348194481954819648197481984819948200482014820248203482044820548206482074820848209482104821148212482134821448215482164821748218482194822048221482224822348224482254822648227482284822948230482314823248233482344823548236482374823848239482404824148242482434824448245482464824748248482494825048251482524825348254482554825648257482584825948260482614826248263482644826548266482674826848269482704827148272482734827448275482764827748278482794828048281482824828348284482854828648287482884828948290482914829248293482944829548296482974829848299483004830148302483034830448305483064830748308483094831048311483124831348314483154831648317483184831948320483214832248323483244832548326483274832848329483304833148332483334833448335483364833748338483394834048341483424834348344483454834648347483484834948350483514835248353483544835548356483574835848359483604836148362483634836448365483664836748368483694837048371483724837348374483754837648377483784837948380483814838248383483844838548386483874838848389483904839148392483934839448395483964839748398483994840048401484024840348404484054840648407484084840948410484114841248413484144841548416484174841848419484204842148422484234842448425484264842748428484294843048431484324843348434484354843648437484384843948440484414844248443484444844548446484474844848449484504845148452484534845448455484564845748458484594846048461484624846348464484654846648467484684846948470484714847248473484744847548476484774847848479484804848148482484834848448485484864848748488484894849048491484924849348494484954849648497484984849948500485014850248503485044850548506485074850848509485104851148512485134851448515485164851748518485194852048521485224852348524485254852648527485284852948530485314853248533485344853548536485374853848539485404854148542485434854448545485464854748548485494855048551485524855348554485554855648557485584855948560485614856248563485644856548566485674856848569485704857148572485734857448575485764857748578485794858048581485824858348584485854858648587485884858948590485914859248593485944859548596485974859848599486004860148602486034860448605486064860748608486094861048611486124861348614486154861648617486184861948620486214862248623486244862548626486274862848629486304863148632486334863448635486364863748638486394864048641486424864348644486454864648647486484864948650486514865248653486544865548656486574865848659486604866148662486634866448665486664866748668486694867048671486724867348674486754867648677486784867948680486814868248683486844868548686486874868848689486904869148692486934869448695486964869748698486994870048701487024870348704487054870648707487084870948710487114871248713487144871548716487174871848719487204872148722487234872448725487264872748728487294873048731487324873348734487354873648737487384873948740487414874248743487444874548746487474874848749487504875148752487534875448755487564875748758487594876048761487624876348764487654876648767487684876948770487714877248773487744877548776487774877848779487804878148782487834878448785487864878748788487894879048791487924879348794487954879648797487984879948800488014880248803488044880548806488074880848809488104881148812488134881448815488164881748818488194882048821488224882348824488254882648827488284882948830488314883248833488344883548836488374883848839488404884148842488434884448845488464884748848488494885048851488524885348854488554885648857488584885948860488614886248863488644886548866488674886848869488704887148872488734887448875488764887748878488794888048881488824888348884488854888648887488884888948890488914889248893488944889548896488974889848899489004890148902489034890448905489064890748908489094891048911489124891348914489154891648917489184891948920489214892248923489244892548926489274892848929489304893148932489334893448935489364893748938489394894048941489424894348944489454894648947489484894948950489514895248953489544895548956489574895848959489604896148962489634896448965489664896748968489694897048971489724897348974489754897648977489784897948980489814898248983489844898548986489874898848989489904899148992489934899448995489964899748998489994900049001490024900349004490054900649007490084900949010490114901249013490144901549016490174901849019490204902149022490234902449025490264902749028490294903049031490324903349034490354903649037490384903949040490414904249043490444904549046490474904849049490504905149052490534905449055490564905749058490594906049061490624906349064490654906649067490684906949070490714907249073490744907549076490774907849079490804908149082490834908449085490864908749088490894909049091490924909349094490954909649097490984909949100491014910249103491044910549106491074910849109491104911149112491134911449115491164911749118491194912049121491224912349124491254912649127491284912949130491314913249133491344913549136491374913849139491404914149142491434914449145491464914749148491494915049151491524915349154491554915649157491584915949160491614916249163491644916549166491674916849169491704917149172491734917449175491764917749178491794918049181491824918349184491854918649187491884918949190491914919249193491944919549196491974919849199492004920149202492034920449205492064920749208492094921049211492124921349214492154921649217492184921949220492214922249223492244922549226492274922849229492304923149232492334923449235492364923749238492394924049241492424924349244492454924649247492484924949250492514925249253492544925549256492574925849259492604926149262492634926449265492664926749268492694927049271492724927349274492754927649277492784927949280492814928249283492844928549286492874928849289492904929149292492934929449295492964929749298492994930049301493024930349304493054930649307493084930949310493114931249313493144931549316493174931849319493204932149322493234932449325493264932749328493294933049331493324933349334493354933649337493384933949340493414934249343493444934549346493474934849349493504935149352493534935449355493564935749358493594936049361493624936349364493654936649367493684936949370493714937249373493744937549376493774937849379493804938149382493834938449385493864938749388493894939049391493924939349394493954939649397493984939949400494014940249403494044940549406494074940849409494104941149412494134941449415494164941749418494194942049421494224942349424494254942649427494284942949430494314943249433494344943549436494374943849439494404944149442494434944449445494464944749448494494945049451494524945349454494554945649457494584945949460494614946249463494644946549466494674946849469494704947149472494734947449475494764947749478494794948049481494824948349484494854948649487494884948949490494914949249493494944949549496494974949849499495004950149502495034950449505495064950749508495094951049511495124951349514495154951649517495184951949520495214952249523495244952549526495274952849529495304953149532495334953449535495364953749538495394954049541495424954349544495454954649547495484954949550495514955249553495544955549556495574955849559495604956149562495634956449565495664956749568495694957049571495724957349574495754957649577495784957949580495814958249583495844958549586495874958849589495904959149592495934959449595495964959749598495994960049601496024960349604496054960649607496084960949610496114961249613496144961549616496174961849619496204962149622496234962449625496264962749628496294963049631496324963349634496354963649637496384963949640496414964249643496444964549646496474964849649496504965149652496534965449655496564965749658496594966049661496624966349664496654966649667496684966949670496714967249673496744967549676496774967849679496804968149682496834968449685496864968749688496894969049691496924969349694496954969649697496984969949700497014970249703497044970549706497074970849709497104971149712497134971449715497164971749718497194972049721497224972349724497254972649727497284972949730497314973249733497344973549736497374973849739497404974149742497434974449745497464974749748497494975049751497524975349754497554975649757497584975949760497614976249763497644976549766497674976849769497704977149772497734977449775497764977749778497794978049781497824978349784497854978649787497884978949790497914979249793497944979549796497974979849799498004980149802498034980449805498064980749808498094981049811498124981349814498154981649817498184981949820498214982249823498244982549826498274982849829498304983149832498334983449835498364983749838498394984049841498424984349844498454984649847498484984949850498514985249853498544985549856498574985849859498604986149862498634986449865498664986749868498694987049871498724987349874498754987649877498784987949880498814988249883498844988549886498874988849889498904989149892498934989449895498964989749898498994990049901499024990349904499054990649907499084990949910499114991249913499144991549916499174991849919499204992149922499234992449925499264992749928499294993049931499324993349934499354993649937499384993949940499414994249943499444994549946499474994849949499504995149952499534995449955499564995749958499594996049961499624996349964499654996649967499684996949970499714997249973499744997549976499774997849979499804998149982499834998449985499864998749988499894999049991499924999349994499954999649997499984999950000500015000250003500045000550006500075000850009500105001150012500135001450015500165001750018500195002050021500225002350024500255002650027500285002950030500315003250033500345003550036500375003850039500405004150042500435004450045500465004750048500495005050051500525005350054500555005650057500585005950060500615006250063500645006550066500675006850069500705007150072500735007450075500765007750078500795008050081500825008350084500855008650087500885008950090500915009250093500945009550096500975009850099501005010150102501035010450105501065010750108501095011050111501125011350114501155011650117501185011950120501215012250123501245012550126501275012850129501305013150132501335013450135501365013750138501395014050141501425014350144501455014650147501485014950150501515015250153501545015550156501575015850159501605016150162501635016450165501665016750168501695017050171501725017350174501755017650177501785017950180501815018250183501845018550186501875018850189501905019150192501935019450195501965019750198501995020050201502025020350204502055020650207502085020950210502115021250213502145021550216502175021850219502205022150222502235022450225502265022750228502295023050231502325023350234502355023650237502385023950240502415024250243502445024550246502475024850249502505025150252502535025450255502565025750258502595026050261502625026350264502655026650267502685026950270502715027250273502745027550276502775027850279502805028150282502835028450285502865028750288502895029050291502925029350294502955029650297502985029950300503015030250303503045030550306503075030850309503105031150312503135031450315503165031750318503195032050321503225032350324503255032650327503285032950330503315033250333503345033550336503375033850339503405034150342503435034450345503465034750348503495035050351503525035350354503555035650357503585035950360503615036250363503645036550366503675036850369503705037150372503735037450375503765037750378503795038050381503825038350384503855038650387503885038950390503915039250393503945039550396503975039850399504005040150402504035040450405504065040750408504095041050411504125041350414504155041650417504185041950420504215042250423504245042550426504275042850429504305043150432504335043450435504365043750438504395044050441504425044350444504455044650447504485044950450504515045250453504545045550456504575045850459504605046150462504635046450465504665046750468504695047050471504725047350474504755047650477504785047950480504815048250483504845048550486504875048850489504905049150492504935049450495504965049750498504995050050501505025050350504505055050650507505085050950510505115051250513505145051550516505175051850519505205052150522505235052450525505265052750528505295053050531505325053350534505355053650537505385053950540505415054250543505445054550546505475054850549505505055150552505535055450555505565055750558505595056050561505625056350564505655056650567505685056950570505715057250573505745057550576505775057850579505805058150582505835058450585505865058750588505895059050591505925059350594505955059650597505985059950600506015060250603506045060550606506075060850609506105061150612506135061450615506165061750618506195062050621506225062350624506255062650627506285062950630506315063250633506345063550636506375063850639506405064150642506435064450645506465064750648506495065050651506525065350654506555065650657506585065950660506615066250663506645066550666506675066850669506705067150672506735067450675506765067750678506795068050681506825068350684506855068650687506885068950690506915069250693506945069550696506975069850699507005070150702507035070450705507065070750708507095071050711507125071350714507155071650717507185071950720507215072250723507245072550726507275072850729507305073150732507335073450735507365073750738507395074050741507425074350744507455074650747507485074950750507515075250753507545075550756507575075850759507605076150762507635076450765507665076750768507695077050771507725077350774507755077650777507785077950780507815078250783507845078550786507875078850789507905079150792507935079450795507965079750798507995080050801508025080350804508055080650807508085080950810508115081250813508145081550816508175081850819508205082150822508235082450825508265082750828508295083050831508325083350834508355083650837508385083950840508415084250843508445084550846508475084850849508505085150852508535085450855508565085750858508595086050861508625086350864508655086650867508685086950870508715087250873508745087550876508775087850879508805088150882508835088450885508865088750888508895089050891508925089350894508955089650897508985089950900509015090250903509045090550906509075090850909509105091150912509135091450915509165091750918509195092050921509225092350924509255092650927509285092950930509315093250933509345093550936509375093850939509405094150942509435094450945509465094750948509495095050951509525095350954509555095650957509585095950960509615096250963509645096550966509675096850969509705097150972509735097450975509765097750978509795098050981509825098350984509855098650987509885098950990509915099250993509945099550996509975099850999510005100151002510035100451005510065100751008510095101051011510125101351014510155101651017510185101951020510215102251023510245102551026510275102851029510305103151032510335103451035510365103751038510395104051041510425104351044510455104651047510485104951050510515105251053510545105551056510575105851059510605106151062510635106451065510665106751068510695107051071510725107351074510755107651077510785107951080510815108251083510845108551086510875108851089510905109151092510935109451095510965109751098510995110051101511025110351104511055110651107511085110951110511115111251113511145111551116511175111851119511205112151122511235112451125511265112751128511295113051131511325113351134511355113651137511385113951140511415114251143511445114551146511475114851149511505115151152511535115451155511565115751158511595116051161511625116351164511655116651167511685116951170511715117251173511745117551176511775117851179511805118151182511835118451185511865118751188511895119051191511925119351194511955119651197511985119951200512015120251203512045120551206512075120851209512105121151212512135121451215512165121751218512195122051221512225122351224512255122651227512285122951230512315123251233512345123551236512375123851239512405124151242512435124451245512465124751248512495125051251512525125351254512555125651257512585125951260512615126251263512645126551266512675126851269512705127151272512735127451275512765127751278512795128051281512825128351284512855128651287512885128951290512915129251293512945129551296512975129851299513005130151302513035130451305513065130751308513095131051311513125131351314513155131651317513185131951320513215132251323513245132551326513275132851329513305133151332513335133451335513365133751338513395134051341513425134351344513455134651347513485134951350513515135251353513545135551356513575135851359513605136151362513635136451365513665136751368513695137051371513725137351374513755137651377513785137951380513815138251383513845138551386513875138851389513905139151392513935139451395513965139751398513995140051401514025140351404514055140651407514085140951410514115141251413514145141551416514175141851419514205142151422514235142451425514265142751428514295143051431514325143351434514355143651437514385143951440514415144251443514445144551446514475144851449514505145151452514535145451455514565145751458514595146051461514625146351464514655146651467514685146951470514715147251473514745147551476514775147851479514805148151482514835148451485514865148751488514895149051491514925149351494514955149651497514985149951500515015150251503515045150551506515075150851509515105151151512515135151451515515165151751518515195152051521515225152351524515255152651527515285152951530515315153251533515345153551536515375153851539515405154151542515435154451545515465154751548515495155051551515525155351554515555155651557515585155951560515615156251563515645156551566515675156851569515705157151572515735157451575515765157751578515795158051581515825158351584515855158651587515885158951590515915159251593515945159551596515975159851599516005160151602516035160451605516065160751608516095161051611516125161351614516155161651617516185161951620516215162251623516245162551626516275162851629516305163151632516335163451635516365163751638516395164051641516425164351644516455164651647516485164951650516515165251653516545165551656516575165851659516605166151662516635166451665516665166751668516695167051671516725167351674516755167651677516785167951680516815168251683516845168551686516875168851689516905169151692516935169451695516965169751698516995170051701517025170351704517055170651707517085170951710517115171251713517145171551716517175171851719517205172151722517235172451725517265172751728517295173051731517325173351734517355173651737517385173951740517415174251743517445174551746517475174851749517505175151752517535175451755517565175751758517595176051761517625176351764517655176651767517685176951770517715177251773517745177551776517775177851779517805178151782517835178451785517865178751788517895179051791517925179351794517955179651797517985179951800518015180251803518045180551806518075180851809518105181151812518135181451815518165181751818518195182051821518225182351824518255182651827518285182951830518315183251833518345183551836518375183851839518405184151842518435184451845518465184751848518495185051851518525185351854518555185651857518585185951860518615186251863518645186551866518675186851869518705187151872518735187451875518765187751878518795188051881518825188351884518855188651887518885188951890518915189251893518945189551896518975189851899519005190151902519035190451905519065190751908519095191051911519125191351914519155191651917519185191951920519215192251923519245192551926519275192851929519305193151932519335193451935519365193751938519395194051941519425194351944519455194651947519485194951950519515195251953519545195551956519575195851959519605196151962519635196451965519665196751968519695197051971519725197351974519755197651977519785197951980519815198251983519845198551986519875198851989519905199151992519935199451995519965199751998519995200052001520025200352004520055200652007520085200952010520115201252013520145201552016520175201852019520205202152022520235202452025520265202752028520295203052031520325203352034520355203652037520385203952040520415204252043520445204552046520475204852049520505205152052520535205452055520565205752058520595206052061520625206352064520655206652067520685206952070520715207252073520745207552076520775207852079520805208152082520835208452085520865208752088520895209052091520925209352094520955209652097520985209952100521015210252103521045210552106521075210852109521105211152112521135211452115521165211752118521195212052121521225212352124521255212652127521285212952130521315213252133521345213552136521375213852139521405214152142521435214452145521465214752148521495215052151521525215352154521555215652157521585215952160521615216252163521645216552166521675216852169521705217152172521735217452175521765217752178521795218052181521825218352184521855218652187521885218952190521915219252193521945219552196521975219852199522005220152202522035220452205522065220752208522095221052211522125221352214522155221652217522185221952220522215222252223522245222552226522275222852229522305223152232522335223452235522365223752238522395224052241522425224352244522455224652247522485224952250522515225252253522545225552256522575225852259522605226152262522635226452265522665226752268522695227052271522725227352274522755227652277522785227952280522815228252283522845228552286522875228852289522905229152292522935229452295522965229752298522995230052301523025230352304523055230652307523085230952310523115231252313523145231552316523175231852319523205232152322523235232452325523265232752328523295233052331523325233352334523355233652337523385233952340523415234252343523445234552346523475234852349523505235152352523535235452355523565235752358523595236052361523625236352364523655236652367523685236952370523715237252373523745237552376523775237852379523805238152382523835238452385523865238752388523895239052391523925239352394523955239652397523985239952400524015240252403524045240552406524075240852409524105241152412524135241452415524165241752418524195242052421524225242352424524255242652427524285242952430524315243252433524345243552436524375243852439524405244152442524435244452445524465244752448524495245052451524525245352454524555245652457524585245952460524615246252463524645246552466524675246852469524705247152472524735247452475524765247752478524795248052481524825248352484524855248652487524885248952490524915249252493524945249552496524975249852499525005250152502525035250452505525065250752508525095251052511525125251352514525155251652517525185251952520525215252252523525245252552526525275252852529525305253152532525335253452535525365253752538525395254052541525425254352544525455254652547525485254952550525515255252553525545255552556525575255852559525605256152562525635256452565525665256752568525695257052571525725257352574525755257652577525785257952580525815258252583525845258552586525875258852589525905259152592525935259452595525965259752598525995260052601526025260352604526055260652607526085260952610526115261252613526145261552616526175261852619526205262152622526235262452625526265262752628526295263052631526325263352634526355263652637526385263952640526415264252643526445264552646526475264852649526505265152652526535265452655526565265752658526595266052661526625266352664526655266652667526685266952670526715267252673526745267552676526775267852679526805268152682526835268452685526865268752688526895269052691526925269352694526955269652697526985269952700527015270252703527045270552706527075270852709527105271152712527135271452715527165271752718527195272052721527225272352724527255272652727527285272952730527315273252733527345273552736527375273852739527405274152742527435274452745527465274752748527495275052751527525275352754527555275652757527585275952760527615276252763527645276552766527675276852769527705277152772527735277452775527765277752778527795278052781527825278352784527855278652787527885278952790527915279252793527945279552796527975279852799528005280152802528035280452805528065280752808528095281052811528125281352814528155281652817528185281952820528215282252823528245282552826528275282852829528305283152832528335283452835528365283752838528395284052841528425284352844528455284652847528485284952850528515285252853528545285552856528575285852859528605286152862528635286452865528665286752868528695287052871528725287352874528755287652877528785287952880528815288252883528845288552886528875288852889528905289152892528935289452895528965289752898528995290052901529025290352904529055290652907529085290952910529115291252913529145291552916529175291852919529205292152922529235292452925529265292752928529295293052931529325293352934529355293652937529385293952940529415294252943529445294552946529475294852949529505295152952529535295452955529565295752958529595296052961529625296352964529655296652967529685296952970529715297252973529745297552976529775297852979529805298152982529835298452985529865298752988529895299052991529925299352994529955299652997529985299953000530015300253003530045300553006530075300853009530105301153012530135301453015530165301753018530195302053021530225302353024530255302653027530285302953030530315303253033530345303553036530375303853039530405304153042530435304453045530465304753048530495305053051530525305353054530555305653057530585305953060530615306253063530645306553066530675306853069530705307153072530735307453075530765307753078530795308053081530825308353084530855308653087530885308953090530915309253093530945309553096530975309853099531005310153102531035310453105531065310753108531095311053111531125311353114531155311653117531185311953120531215312253123531245312553126531275312853129531305313153132531335313453135531365313753138531395314053141531425314353144531455314653147531485314953150531515315253153531545315553156531575315853159531605316153162531635316453165531665316753168531695317053171531725317353174531755317653177531785317953180531815318253183531845318553186531875318853189531905319153192531935319453195531965319753198531995320053201532025320353204532055320653207532085320953210532115321253213532145321553216532175321853219532205322153222532235322453225532265322753228532295323053231532325323353234532355323653237532385323953240532415324253243532445324553246532475324853249532505325153252532535325453255532565325753258532595326053261532625326353264532655326653267532685326953270532715327253273532745327553276532775327853279532805328153282532835328453285532865328753288532895329053291532925329353294532955329653297532985329953300533015330253303533045330553306533075330853309533105331153312533135331453315533165331753318533195332053321533225332353324533255332653327533285332953330533315333253333533345333553336533375333853339533405334153342533435334453345533465334753348533495335053351533525335353354533555335653357533585335953360533615336253363533645336553366533675336853369533705337153372533735337453375533765337753378533795338053381533825338353384533855338653387533885338953390533915339253393533945339553396533975339853399534005340153402534035340453405534065340753408534095341053411534125341353414534155341653417534185341953420534215342253423534245342553426534275342853429534305343153432534335343453435534365343753438534395344053441534425344353444534455344653447534485344953450534515345253453534545345553456534575345853459534605346153462534635346453465534665346753468534695347053471534725347353474534755347653477534785347953480534815348253483534845348553486534875348853489534905349153492534935349453495534965349753498534995350053501535025350353504535055350653507535085350953510535115351253513535145351553516535175351853519535205352153522535235352453525535265352753528535295353053531535325353353534535355353653537535385353953540535415354253543535445354553546535475354853549535505355153552535535355453555535565355753558535595356053561535625356353564535655356653567535685356953570535715357253573535745357553576535775357853579535805358153582535835358453585535865358753588535895359053591535925359353594535955359653597535985359953600536015360253603536045360553606536075360853609536105361153612536135361453615536165361753618536195362053621536225362353624536255362653627536285362953630536315363253633536345363553636536375363853639536405364153642536435364453645536465364753648536495365053651536525365353654536555365653657536585365953660536615366253663536645366553666536675366853669536705367153672536735367453675536765367753678536795368053681536825368353684536855368653687536885368953690536915369253693536945369553696536975369853699537005370153702537035370453705537065370753708537095371053711537125371353714537155371653717537185371953720537215372253723537245372553726537275372853729537305373153732537335373453735537365373753738537395374053741537425374353744537455374653747537485374953750537515375253753537545375553756537575375853759537605376153762537635376453765537665376753768537695377053771537725377353774537755377653777537785377953780537815378253783537845378553786537875378853789537905379153792537935379453795537965379753798537995380053801538025380353804538055380653807538085380953810538115381253813538145381553816538175381853819538205382153822538235382453825538265382753828538295383053831538325383353834538355383653837538385383953840538415384253843538445384553846538475384853849538505385153852538535385453855538565385753858538595386053861538625386353864538655386653867538685386953870538715387253873538745387553876538775387853879538805388153882538835388453885538865388753888538895389053891538925389353894538955389653897538985389953900539015390253903539045390553906539075390853909539105391153912539135391453915539165391753918539195392053921539225392353924539255392653927539285392953930539315393253933539345393553936539375393853939539405394153942539435394453945539465394753948539495395053951539525395353954539555395653957539585395953960539615396253963539645396553966539675396853969539705397153972539735397453975539765397753978539795398053981539825398353984539855398653987539885398953990539915399253993539945399553996539975399853999540005400154002540035400454005540065400754008540095401054011540125401354014540155401654017540185401954020540215402254023540245402554026540275402854029540305403154032540335403454035540365403754038540395404054041540425404354044540455404654047540485404954050540515405254053540545405554056540575405854059540605406154062540635406454065540665406754068540695407054071540725407354074540755407654077540785407954080540815408254083540845408554086540875408854089540905409154092540935409454095540965409754098540995410054101541025410354104541055410654107541085410954110541115411254113541145411554116541175411854119541205412154122541235412454125541265412754128541295413054131541325413354134541355413654137541385413954140541415414254143541445414554146541475414854149541505415154152541535415454155541565415754158541595416054161541625416354164541655416654167541685416954170541715417254173541745417554176541775417854179541805418154182541835418454185541865418754188541895419054191541925419354194541955419654197541985419954200542015420254203542045420554206542075420854209542105421154212542135421454215542165421754218542195422054221542225422354224542255422654227542285422954230542315423254233542345423554236542375423854239542405424154242542435424454245542465424754248542495425054251542525425354254542555425654257542585425954260542615426254263542645426554266542675426854269542705427154272542735427454275542765427754278542795428054281542825428354284542855428654287542885428954290542915429254293542945429554296542975429854299543005430154302543035430454305543065430754308543095431054311543125431354314543155431654317543185431954320543215432254323543245432554326543275432854329543305433154332543335433454335543365433754338543395434054341543425434354344543455434654347543485434954350543515435254353543545435554356543575435854359543605436154362543635436454365543665436754368543695437054371543725437354374543755437654377543785437954380543815438254383543845438554386543875438854389543905439154392543935439454395543965439754398543995440054401544025440354404544055440654407544085440954410544115441254413544145441554416544175441854419544205442154422544235442454425544265442754428544295443054431544325443354434544355443654437544385443954440544415444254443544445444554446544475444854449544505445154452544535445454455544565445754458544595446054461544625446354464544655446654467544685446954470544715447254473544745447554476544775447854479544805448154482544835448454485544865448754488544895449054491544925449354494544955449654497544985449954500545015450254503545045450554506545075450854509545105451154512545135451454515545165451754518545195452054521545225452354524545255452654527545285452954530545315453254533545345453554536545375453854539545405454154542545435454454545545465454754548545495455054551545525455354554545555455654557545585455954560545615456254563545645456554566545675456854569545705457154572545735457454575545765457754578545795458054581545825458354584545855458654587545885458954590545915459254593545945459554596545975459854599546005460154602546035460454605546065460754608546095461054611546125461354614546155461654617546185461954620546215462254623546245462554626546275462854629546305463154632546335463454635546365463754638546395464054641546425464354644546455464654647546485464954650546515465254653546545465554656546575465854659546605466154662546635466454665546665466754668546695467054671546725467354674546755467654677546785467954680546815468254683546845468554686546875468854689546905469154692546935469454695546965469754698546995470054701547025470354704547055470654707547085470954710547115471254713547145471554716547175471854719547205472154722547235472454725547265472754728547295473054731547325473354734547355473654737547385473954740547415474254743547445474554746547475474854749547505475154752547535475454755547565475754758547595476054761547625476354764547655476654767547685476954770547715477254773547745477554776547775477854779547805478154782547835478454785547865478754788547895479054791547925479354794547955479654797547985479954800548015480254803548045480554806548075480854809548105481154812548135481454815548165481754818548195482054821548225482354824548255482654827548285482954830548315483254833548345483554836548375483854839548405484154842548435484454845548465484754848548495485054851548525485354854548555485654857548585485954860548615486254863548645486554866548675486854869548705487154872548735487454875548765487754878548795488054881548825488354884548855488654887548885488954890548915489254893548945489554896548975489854899549005490154902549035490454905549065490754908549095491054911549125491354914549155491654917549185491954920549215492254923549245492554926549275492854929549305493154932549335493454935549365493754938549395494054941549425494354944549455494654947549485494954950549515495254953549545495554956549575495854959549605496154962549635496454965549665496754968549695497054971549725497354974549755497654977549785497954980549815498254983549845498554986549875498854989549905499154992549935499454995549965499754998549995500055001550025500355004550055500655007550085500955010550115501255013550145501555016550175501855019550205502155022550235502455025550265502755028550295503055031550325503355034550355503655037550385503955040550415504255043550445504555046550475504855049550505505155052550535505455055550565505755058550595506055061550625506355064550655506655067550685506955070550715507255073550745507555076550775507855079550805508155082550835508455085550865508755088550895509055091550925509355094550955509655097550985509955100551015510255103551045510555106551075510855109551105511155112551135511455115551165511755118551195512055121551225512355124551255512655127551285512955130551315513255133551345513555136551375513855139551405514155142551435514455145551465514755148551495515055151551525515355154551555515655157551585515955160551615516255163551645516555166551675516855169551705517155172551735517455175551765517755178551795518055181551825518355184551855518655187551885518955190551915519255193551945519555196551975519855199552005520155202552035520455205552065520755208552095521055211552125521355214552155521655217552185521955220552215522255223552245522555226552275522855229552305523155232552335523455235552365523755238552395524055241552425524355244552455524655247552485524955250552515525255253552545525555256552575525855259552605526155262552635526455265552665526755268552695527055271552725527355274552755527655277552785527955280552815528255283552845528555286552875528855289552905529155292552935529455295552965529755298552995530055301553025530355304553055530655307553085530955310553115531255313553145531555316553175531855319553205532155322553235532455325553265532755328553295533055331553325533355334553355533655337553385533955340553415534255343553445534555346553475534855349553505535155352553535535455355553565535755358553595536055361553625536355364553655536655367553685536955370553715537255373553745537555376553775537855379553805538155382553835538455385553865538755388553895539055391553925539355394553955539655397553985539955400554015540255403554045540555406554075540855409554105541155412554135541455415554165541755418554195542055421554225542355424554255542655427554285542955430554315543255433554345543555436554375543855439554405544155442554435544455445554465544755448554495545055451554525545355454554555545655457554585545955460554615546255463554645546555466554675546855469554705547155472554735547455475554765547755478554795548055481554825548355484554855548655487554885548955490554915549255493554945549555496554975549855499555005550155502555035550455505555065550755508555095551055511555125551355514555155551655517555185551955520555215552255523555245552555526555275552855529555305553155532555335553455535555365553755538555395554055541555425554355544555455554655547555485554955550555515555255553555545555555556555575555855559555605556155562555635556455565555665556755568555695557055571555725557355574555755557655577555785557955580555815558255583555845558555586555875558855589555905559155592555935559455595555965559755598555995560055601556025560355604556055560655607556085560955610556115561255613556145561555616556175561855619556205562155622556235562455625556265562755628556295563055631556325563355634556355563655637556385563955640556415564255643556445564555646556475564855649556505565155652556535565455655556565565755658556595566055661556625566355664556655566655667556685566955670556715567255673556745567555676556775567855679556805568155682556835568455685556865568755688556895569055691556925569355694556955569655697556985569955700557015570255703557045570555706557075570855709557105571155712557135571455715557165571755718557195572055721557225572355724557255572655727557285572955730557315573255733557345573555736557375573855739557405574155742557435574455745557465574755748557495575055751557525575355754557555575655757557585575955760557615576255763557645576555766557675576855769557705577155772557735577455775557765577755778557795578055781557825578355784557855578655787557885578955790557915579255793557945579555796557975579855799558005580155802558035580455805558065580755808558095581055811558125581355814558155581655817558185581955820558215582255823558245582555826558275582855829558305583155832558335583455835558365583755838558395584055841558425584355844558455584655847558485584955850558515585255853558545585555856558575585855859558605586155862558635586455865558665586755868558695587055871558725587355874558755587655877558785587955880558815588255883558845588555886558875588855889558905589155892558935589455895558965589755898558995590055901559025590355904559055590655907559085590955910559115591255913559145591555916559175591855919559205592155922559235592455925559265592755928559295593055931559325593355934559355593655937559385593955940559415594255943559445594555946559475594855949559505595155952559535595455955559565595755958559595596055961559625596355964559655596655967559685596955970559715597255973559745597555976559775597855979559805598155982559835598455985559865598755988559895599055991559925599355994559955599655997559985599956000560015600256003560045600556006560075600856009560105601156012560135601456015560165601756018560195602056021560225602356024560255602656027560285602956030560315603256033560345603556036560375603856039560405604156042560435604456045560465604756048560495605056051560525605356054560555605656057560585605956060560615606256063560645606556066560675606856069560705607156072560735607456075560765607756078560795608056081560825608356084560855608656087560885608956090560915609256093560945609556096560975609856099561005610156102561035610456105561065610756108561095611056111561125611356114561155611656117561185611956120561215612256123561245612556126561275612856129561305613156132561335613456135561365613756138561395614056141561425614356144561455614656147561485614956150561515615256153561545615556156561575615856159561605616156162561635616456165561665616756168561695617056171561725617356174561755617656177561785617956180561815618256183561845618556186561875618856189561905619156192561935619456195561965619756198561995620056201562025620356204562055620656207562085620956210562115621256213562145621556216562175621856219562205622156222562235622456225562265622756228562295623056231562325623356234562355623656237562385623956240562415624256243562445624556246562475624856249562505625156252562535625456255562565625756258562595626056261562625626356264562655626656267562685626956270562715627256273562745627556276562775627856279562805628156282562835628456285562865628756288562895629056291562925629356294562955629656297562985629956300563015630256303563045630556306563075630856309563105631156312563135631456315563165631756318563195632056321563225632356324563255632656327563285632956330563315633256333563345633556336563375633856339563405634156342563435634456345563465634756348563495635056351563525635356354563555635656357563585635956360563615636256363563645636556366563675636856369563705637156372563735637456375563765637756378563795638056381563825638356384563855638656387563885638956390563915639256393563945639556396563975639856399564005640156402564035640456405564065640756408564095641056411564125641356414564155641656417564185641956420564215642256423564245642556426564275642856429564305643156432564335643456435564365643756438564395644056441564425644356444564455644656447564485644956450564515645256453564545645556456564575645856459564605646156462564635646456465564665646756468564695647056471564725647356474564755647656477564785647956480564815648256483564845648556486564875648856489564905649156492564935649456495564965649756498564995650056501565025650356504565055650656507565085650956510565115651256513565145651556516565175651856519565205652156522565235652456525565265652756528565295653056531565325653356534565355653656537565385653956540565415654256543565445654556546565475654856549565505655156552565535655456555565565655756558565595656056561565625656356564565655656656567565685656956570565715657256573565745657556576565775657856579565805658156582565835658456585565865658756588565895659056591565925659356594565955659656597565985659956600566015660256603566045660556606566075660856609566105661156612566135661456615566165661756618566195662056621566225662356624566255662656627566285662956630566315663256633566345663556636566375663856639566405664156642566435664456645566465664756648566495665056651566525665356654566555665656657566585665956660566615666256663566645666556666566675666856669566705667156672566735667456675566765667756678566795668056681566825668356684566855668656687566885668956690566915669256693566945669556696566975669856699567005670156702567035670456705567065670756708567095671056711567125671356714567155671656717567185671956720567215672256723567245672556726567275672856729567305673156732567335673456735567365673756738567395674056741567425674356744567455674656747567485674956750567515675256753567545675556756567575675856759567605676156762567635676456765567665676756768567695677056771567725677356774567755677656777567785677956780567815678256783567845678556786567875678856789567905679156792567935679456795567965679756798567995680056801568025680356804568055680656807568085680956810568115681256813568145681556816568175681856819568205682156822568235682456825568265682756828568295683056831568325683356834568355683656837568385683956840568415684256843568445684556846568475684856849568505685156852568535685456855568565685756858568595686056861568625686356864568655686656867568685686956870568715687256873568745687556876568775687856879568805688156882568835688456885568865688756888568895689056891568925689356894568955689656897568985689956900569015690256903569045690556906569075690856909569105691156912569135691456915569165691756918569195692056921569225692356924569255692656927569285692956930569315693256933569345693556936569375693856939569405694156942569435694456945569465694756948569495695056951569525695356954569555695656957569585695956960569615696256963569645696556966569675696856969569705697156972569735697456975569765697756978569795698056981569825698356984569855698656987569885698956990569915699256993569945699556996569975699856999570005700157002570035700457005570065700757008570095701057011570125701357014570155701657017570185701957020570215702257023570245702557026570275702857029570305703157032570335703457035570365703757038570395704057041570425704357044570455704657047570485704957050570515705257053570545705557056570575705857059570605706157062570635706457065570665706757068570695707057071570725707357074570755707657077570785707957080570815708257083570845708557086570875708857089570905709157092570935709457095570965709757098570995710057101571025710357104571055710657107571085710957110571115711257113571145711557116571175711857119571205712157122571235712457125571265712757128571295713057131571325713357134571355713657137571385713957140571415714257143571445714557146571475714857149571505715157152571535715457155571565715757158571595716057161571625716357164571655716657167571685716957170571715717257173571745717557176571775717857179571805718157182571835718457185571865718757188571895719057191571925719357194571955719657197571985719957200572015720257203572045720557206572075720857209572105721157212572135721457215572165721757218572195722057221572225722357224572255722657227572285722957230572315723257233572345723557236572375723857239572405724157242572435724457245572465724757248572495725057251572525725357254572555725657257572585725957260572615726257263572645726557266572675726857269572705727157272572735727457275572765727757278572795728057281572825728357284572855728657287572885728957290572915729257293572945729557296572975729857299573005730157302573035730457305573065730757308573095731057311573125731357314573155731657317573185731957320573215732257323573245732557326573275732857329573305733157332573335733457335573365733757338573395734057341573425734357344573455734657347573485734957350573515735257353573545735557356573575735857359573605736157362573635736457365573665736757368573695737057371573725737357374573755737657377573785737957380573815738257383573845738557386573875738857389573905739157392573935739457395573965739757398573995740057401574025740357404574055740657407574085740957410574115741257413574145741557416574175741857419574205742157422574235742457425574265742757428574295743057431574325743357434574355743657437574385743957440574415744257443574445744557446574475744857449574505745157452574535745457455574565745757458574595746057461574625746357464574655746657467574685746957470574715747257473574745747557476574775747857479574805748157482574835748457485574865748757488574895749057491574925749357494574955749657497574985749957500575015750257503575045750557506575075750857509575105751157512575135751457515575165751757518575195752057521575225752357524575255752657527575285752957530575315753257533575345753557536575375753857539575405754157542575435754457545575465754757548575495755057551575525755357554575555755657557575585755957560575615756257563575645756557566575675756857569575705757157572575735757457575575765757757578575795758057581575825758357584575855758657587575885758957590575915759257593575945759557596575975759857599576005760157602576035760457605576065760757608576095761057611576125761357614576155761657617576185761957620576215762257623576245762557626576275762857629576305763157632576335763457635576365763757638576395764057641576425764357644576455764657647576485764957650576515765257653576545765557656576575765857659576605766157662576635766457665576665766757668576695767057671576725767357674576755767657677576785767957680576815768257683576845768557686576875768857689576905769157692576935769457695576965769757698576995770057701577025770357704577055770657707577085770957710577115771257713577145771557716577175771857719577205772157722577235772457725577265772757728577295773057731577325773357734577355773657737577385773957740577415774257743577445774557746577475774857749577505775157752577535775457755577565775757758577595776057761577625776357764577655776657767577685776957770577715777257773577745777557776577775777857779577805778157782577835778457785577865778757788577895779057791577925779357794577955779657797577985779957800578015780257803578045780557806578075780857809578105781157812578135781457815578165781757818578195782057821578225782357824578255782657827578285782957830578315783257833578345783557836578375783857839578405784157842578435784457845578465784757848578495785057851578525785357854578555785657857578585785957860578615786257863578645786557866578675786857869578705787157872578735787457875578765787757878578795788057881578825788357884578855788657887578885788957890578915789257893578945789557896578975789857899579005790157902579035790457905579065790757908579095791057911579125791357914579155791657917579185791957920579215792257923579245792557926579275792857929579305793157932579335793457935579365793757938579395794057941579425794357944579455794657947579485794957950579515795257953579545795557956579575795857959579605796157962579635796457965579665796757968579695797057971579725797357974579755797657977579785797957980579815798257983579845798557986579875798857989579905799157992579935799457995579965799757998579995800058001580025800358004580055800658007580085800958010580115801258013580145801558016580175801858019580205802158022580235802458025580265802758028580295803058031580325803358034580355803658037580385803958040580415804258043580445804558046580475804858049580505805158052580535805458055580565805758058580595806058061580625806358064580655806658067580685806958070580715807258073580745807558076580775807858079580805808158082580835808458085580865808758088580895809058091580925809358094580955809658097580985809958100581015810258103581045810558106581075810858109581105811158112581135811458115581165811758118581195812058121581225812358124581255812658127581285812958130581315813258133581345813558136581375813858139581405814158142581435814458145581465814758148581495815058151581525815358154581555815658157581585815958160581615816258163581645816558166581675816858169581705817158172581735817458175581765817758178581795818058181581825818358184581855818658187581885818958190581915819258193581945819558196581975819858199582005820158202582035820458205582065820758208582095821058211582125821358214582155821658217582185821958220582215822258223582245822558226582275822858229582305823158232582335823458235582365823758238582395824058241582425824358244582455824658247582485824958250582515825258253582545825558256582575825858259582605826158262582635826458265582665826758268582695827058271582725827358274582755827658277582785827958280582815828258283582845828558286582875828858289582905829158292582935829458295582965829758298582995830058301583025830358304583055830658307583085830958310583115831258313583145831558316583175831858319583205832158322583235832458325583265832758328583295833058331583325833358334583355833658337583385833958340583415834258343583445834558346583475834858349583505835158352583535835458355583565835758358583595836058361583625836358364583655836658367583685836958370583715837258373583745837558376583775837858379583805838158382583835838458385583865838758388583895839058391583925839358394583955839658397583985839958400584015840258403584045840558406584075840858409584105841158412584135841458415584165841758418584195842058421584225842358424584255842658427584285842958430584315843258433584345843558436584375843858439584405844158442584435844458445584465844758448584495845058451584525845358454584555845658457584585845958460584615846258463584645846558466584675846858469584705847158472584735847458475584765847758478584795848058481584825848358484584855848658487584885848958490584915849258493584945849558496584975849858499585005850158502585035850458505585065850758508585095851058511585125851358514585155851658517585185851958520585215852258523585245852558526585275852858529585305853158532585335853458535585365853758538585395854058541585425854358544585455854658547585485854958550585515855258553585545855558556585575855858559585605856158562585635856458565585665856758568585695857058571585725857358574585755857658577585785857958580585815858258583585845858558586585875858858589585905859158592585935859458595585965859758598585995860058601586025860358604586055860658607586085860958610586115861258613586145861558616586175861858619586205862158622586235862458625586265862758628586295863058631586325863358634586355863658637586385863958640586415864258643586445864558646586475864858649586505865158652586535865458655586565865758658586595866058661586625866358664586655866658667586685866958670586715867258673586745867558676586775867858679586805868158682586835868458685586865868758688586895869058691586925869358694586955869658697586985869958700587015870258703587045870558706587075870858709587105871158712587135871458715587165871758718587195872058721587225872358724587255872658727587285872958730587315873258733587345873558736587375873858739587405874158742587435874458745587465874758748587495875058751587525875358754587555875658757587585875958760587615876258763587645876558766587675876858769587705877158772587735877458775587765877758778587795878058781587825878358784587855878658787587885878958790587915879258793587945879558796587975879858799588005880158802588035880458805588065880758808588095881058811588125881358814588155881658817588185881958820588215882258823588245882558826588275882858829588305883158832588335883458835588365883758838588395884058841588425884358844588455884658847588485884958850588515885258853588545885558856588575885858859588605886158862588635886458865588665886758868588695887058871588725887358874588755887658877588785887958880588815888258883588845888558886588875888858889588905889158892588935889458895588965889758898588995890058901589025890358904589055890658907589085890958910589115891258913589145891558916589175891858919589205892158922589235892458925589265892758928589295893058931589325893358934589355893658937589385893958940589415894258943589445894558946589475894858949589505895158952589535895458955589565895758958589595896058961589625896358964589655896658967589685896958970589715897258973589745897558976589775897858979589805898158982589835898458985589865898758988589895899058991589925899358994589955899658997589985899959000590015900259003590045900559006590075900859009590105901159012590135901459015590165901759018590195902059021590225902359024590255902659027590285902959030590315903259033590345903559036590375903859039590405904159042590435904459045590465904759048590495905059051590525905359054590555905659057590585905959060590615906259063590645906559066590675906859069590705907159072590735907459075590765907759078590795908059081590825908359084590855908659087590885908959090590915909259093590945909559096590975909859099591005910159102591035910459105591065910759108591095911059111591125911359114591155911659117591185911959120591215912259123591245912559126591275912859129591305913159132591335913459135591365913759138591395914059141591425914359144591455914659147591485914959150591515915259153591545915559156591575915859159591605916159162591635916459165591665916759168591695917059171591725917359174591755917659177591785917959180591815918259183591845918559186591875918859189591905919159192591935919459195591965919759198591995920059201592025920359204592055920659207592085920959210592115921259213592145921559216592175921859219592205922159222592235922459225592265922759228592295923059231592325923359234592355923659237592385923959240592415924259243592445924559246592475924859249592505925159252592535925459255592565925759258592595926059261592625926359264592655926659267592685926959270592715927259273592745927559276592775927859279592805928159282592835928459285592865928759288592895929059291592925929359294592955929659297592985929959300593015930259303593045930559306593075930859309593105931159312593135931459315593165931759318593195932059321593225932359324593255932659327593285932959330593315933259333593345933559336593375933859339593405934159342593435934459345593465934759348593495935059351593525935359354593555935659357593585935959360593615936259363593645936559366593675936859369593705937159372593735937459375593765937759378593795938059381593825938359384593855938659387593885938959390593915939259393593945939559396593975939859399594005940159402594035940459405594065940759408594095941059411594125941359414594155941659417594185941959420594215942259423594245942559426594275942859429594305943159432594335943459435594365943759438594395944059441594425944359444594455944659447594485944959450594515945259453594545945559456594575945859459594605946159462594635946459465594665946759468594695947059471594725947359474594755947659477594785947959480594815948259483594845948559486594875948859489594905949159492594935949459495594965949759498594995950059501595025950359504595055950659507595085950959510595115951259513595145951559516595175951859519595205952159522595235952459525595265952759528595295953059531595325953359534595355953659537595385953959540595415954259543595445954559546595475954859549595505955159552595535955459555595565955759558595595956059561595625956359564595655956659567595685956959570595715957259573595745957559576595775957859579595805958159582595835958459585595865958759588595895959059591595925959359594595955959659597595985959959600596015960259603596045960559606596075960859609596105961159612596135961459615596165961759618596195962059621596225962359624596255962659627596285962959630596315963259633596345963559636596375963859639596405964159642596435964459645596465964759648596495965059651596525965359654596555965659657596585965959660596615966259663596645966559666596675966859669596705967159672596735967459675596765967759678596795968059681596825968359684596855968659687596885968959690596915969259693596945969559696596975969859699597005970159702597035970459705597065970759708597095971059711597125971359714597155971659717597185971959720597215972259723597245972559726597275972859729597305973159732597335973459735597365973759738597395974059741597425974359744597455974659747597485974959750597515975259753597545975559756597575975859759597605976159762597635976459765597665976759768597695977059771597725977359774597755977659777597785977959780597815978259783597845978559786597875978859789597905979159792597935979459795597965979759798597995980059801598025980359804598055980659807598085980959810598115981259813598145981559816598175981859819598205982159822598235982459825598265982759828598295983059831598325983359834598355983659837598385983959840598415984259843598445984559846598475984859849598505985159852598535985459855598565985759858598595986059861598625986359864598655986659867598685986959870598715987259873598745987559876598775987859879598805988159882598835988459885598865988759888598895989059891598925989359894598955989659897598985989959900599015990259903599045990559906599075990859909599105991159912599135991459915599165991759918599195992059921599225992359924599255992659927599285992959930599315993259933599345993559936599375993859939599405994159942599435994459945599465994759948599495995059951599525995359954599555995659957599585995959960599615996259963599645996559966599675996859969599705997159972599735997459975599765997759978599795998059981599825998359984599855998659987599885998959990599915999259993599945999559996599975999859999600006000160002600036000460005600066000760008600096001060011600126001360014600156001660017600186001960020600216002260023600246002560026600276002860029600306003160032600336003460035600366003760038600396004060041600426004360044600456004660047600486004960050600516005260053600546005560056600576005860059600606006160062600636006460065600666006760068600696007060071600726007360074600756007660077600786007960080600816008260083600846008560086600876008860089600906009160092600936009460095600966009760098600996010060101601026010360104601056010660107601086010960110601116011260113601146011560116601176011860119601206012160122601236012460125601266012760128601296013060131601326013360134601356013660137601386013960140601416014260143601446014560146601476014860149601506015160152601536015460155601566015760158601596016060161601626016360164601656016660167601686016960170601716017260173601746017560176601776017860179601806018160182601836018460185601866018760188601896019060191601926019360194601956019660197601986019960200602016020260203602046020560206602076020860209602106021160212602136021460215602166021760218602196022060221602226022360224602256022660227602286022960230602316023260233602346023560236602376023860239602406024160242602436024460245602466024760248602496025060251602526025360254602556025660257602586025960260602616026260263602646026560266602676026860269602706027160272602736027460275602766027760278602796028060281602826028360284602856028660287602886028960290602916029260293602946029560296602976029860299603006030160302603036030460305603066030760308603096031060311603126031360314603156031660317603186031960320603216032260323603246032560326603276032860329603306033160332603336033460335603366033760338603396034060341603426034360344603456034660347603486034960350603516035260353603546035560356603576035860359603606036160362603636036460365603666036760368603696037060371603726037360374603756037660377603786037960380603816038260383603846038560386603876038860389603906039160392603936039460395603966039760398603996040060401604026040360404604056040660407604086040960410604116041260413604146041560416604176041860419604206042160422604236042460425604266042760428604296043060431604326043360434604356043660437604386043960440604416044260443604446044560446604476044860449604506045160452604536045460455604566045760458604596046060461604626046360464604656046660467604686046960470604716047260473604746047560476604776047860479604806048160482604836048460485604866048760488604896049060491604926049360494604956049660497604986049960500605016050260503605046050560506605076050860509605106051160512605136051460515605166051760518605196052060521605226052360524605256052660527605286052960530605316053260533605346053560536605376053860539605406054160542605436054460545605466054760548605496055060551605526055360554605556055660557605586055960560605616056260563605646056560566605676056860569605706057160572605736057460575605766057760578605796058060581605826058360584605856058660587605886058960590
  1. /* api.c API unit tests
  2. *
  3. * Copyright (C) 2006-2022 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/engine.h>
  273. #include <wolfssl/openssl/hmac.h>
  274. #include <wolfssl/openssl/objects.h>
  275. #include <wolfssl/openssl/rand.h>
  276. #include <wolfssl/openssl/modes.h>
  277. #include <wolfssl/openssl/fips_rand.h>
  278. #include <wolfssl/openssl/kdf.h>
  279. #ifdef OPENSSL_ALL
  280. #include <wolfssl/openssl/txt_db.h>
  281. #include <wolfssl/openssl/lhash.h>
  282. #endif
  283. #ifndef NO_AES
  284. #include <wolfssl/openssl/aes.h>
  285. #endif
  286. #ifndef NO_DES3
  287. #include <wolfssl/openssl/des.h>
  288. #endif
  289. #ifdef HAVE_ECC
  290. #include <wolfssl/openssl/ecdsa.h>
  291. #endif
  292. #ifdef HAVE_PKCS7
  293. #include <wolfssl/openssl/pkcs7.h>
  294. #endif
  295. #ifdef HAVE_ED25519
  296. #include <wolfssl/openssl/ed25519.h>
  297. #endif
  298. #ifdef HAVE_ED448
  299. #include <wolfssl/openssl/ed448.h>
  300. #endif
  301. #endif /* OPENSSL_EXTRA */
  302. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  303. && !defined(NO_SHA256) && !defined(RC_NO_RNG)
  304. #include <wolfssl/wolfcrypt/srp.h>
  305. #endif
  306. #if (defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)) || \
  307. defined(HAVE_SESSION_TICKET) || (defined(OPENSSL_EXTRA) && \
  308. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)) || \
  309. defined(WOLFSSL_TEST_STATIC_BUILD) || defined(WOLFSSL_DTLS)
  310. /* for testing SSL_get_peer_cert_chain, or SESSION_TICKET_HINT_DEFAULT,
  311. * for setting authKeyIdSrc in WOLFSSL_X509, or testing DTLS sequence
  312. * number tracking */
  313. #include "wolfssl/internal.h"
  314. #endif
  315. /* force enable test buffers */
  316. #ifndef USE_CERT_BUFFERS_2048
  317. #define USE_CERT_BUFFERS_2048
  318. #endif
  319. #ifndef USE_CERT_BUFFERS_256
  320. #define USE_CERT_BUFFERS_256
  321. #endif
  322. #include <wolfssl/certs_test.h>
  323. typedef struct testVector {
  324. const char* input;
  325. const char* output;
  326. size_t inLen;
  327. size_t outLen;
  328. } testVector;
  329. #if defined(HAVE_PKCS7)
  330. typedef struct {
  331. const byte* content;
  332. word32 contentSz;
  333. int contentOID;
  334. int encryptOID;
  335. int keyWrapOID;
  336. int keyAgreeOID;
  337. byte* cert;
  338. size_t certSz;
  339. byte* privateKey;
  340. word32 privateKeySz;
  341. } pkcs7EnvelopedVector;
  342. #ifndef NO_PKCS7_ENCRYPTED_DATA
  343. typedef struct {
  344. const byte* content;
  345. word32 contentSz;
  346. int contentOID;
  347. int encryptOID;
  348. byte* encryptionKey;
  349. word32 encryptionKeySz;
  350. } pkcs7EncryptedVector;
  351. #endif
  352. #endif /* HAVE_PKCS7 */
  353. /*----------------------------------------------------------------------------*
  354. | Constants
  355. *----------------------------------------------------------------------------*/
  356. /* Test result constants and macros. */
  357. /* Test succeeded. */
  358. #define TEST_SUCCESS (1)
  359. /* Test failed. */
  360. #define TEST_FAIL (0)
  361. /* Test skipped - not run. */
  362. #define TEST_SKIPPED (-1)
  363. /* Returns the result based on whether check is true.
  364. *
  365. * @param [in] check Condition for success.
  366. * @return When condition is true: TEST_SUCCESS.
  367. * @return When condition is false: TEST_FAIL.
  368. */
  369. #ifdef DEBUG_WOLFSSL_VERBOSE
  370. #define XSTRINGIFY(s) STRINGIFY(s)
  371. #define STRINGIFY(s) #s
  372. #define TEST_RES_CHECK(check) ({ \
  373. int _ret = (check) ? TEST_SUCCESS : TEST_FAIL; \
  374. if (_ret == TEST_FAIL) { \
  375. fprintf(stderr, " check \"%s\" at %d ", \
  376. XSTRINGIFY(check), __LINE__); \
  377. } \
  378. _ret; })
  379. #else
  380. #define TEST_RES_CHECK(check) \
  381. ((check) ? TEST_SUCCESS : TEST_FAIL)
  382. #endif /* DEBUG_WOLFSSL_VERBOSE */
  383. #define TEST_STRING "Everyone gets Friday off."
  384. #define TEST_STRING_SZ 25
  385. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  386. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  387. #define TEST_RSA_BITS 1024
  388. #else
  389. #define TEST_RSA_BITS 2048
  390. #endif
  391. #define TEST_RSA_BYTES (TEST_RSA_BITS/8)
  392. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  393. (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT))
  394. static const char* bogusFile =
  395. #ifdef _WIN32
  396. "NUL"
  397. #else
  398. "/dev/null"
  399. #endif
  400. ;
  401. #endif /* !NO_FILESYSTEM && !NO_CERTS && (!NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT) */
  402. enum {
  403. TESTING_RSA = 1,
  404. TESTING_ECC = 2
  405. };
  406. #ifdef WOLFSSL_QNX_CAAM
  407. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  408. static int testDevId = WOLFSSL_CAAM_DEVID;
  409. #else
  410. static int testDevId = INVALID_DEVID;
  411. #endif
  412. /*----------------------------------------------------------------------------*
  413. | Setup
  414. *----------------------------------------------------------------------------*/
  415. static int test_wolfSSL_Init(void)
  416. {
  417. int result;
  418. result = wolfSSL_Init();
  419. result = TEST_RES_CHECK(result == WOLFSSL_SUCCESS);
  420. return result;
  421. }
  422. static int test_wolfSSL_Cleanup(void)
  423. {
  424. int result;
  425. result = wolfSSL_Cleanup();
  426. result = TEST_RES_CHECK(result == WOLFSSL_SUCCESS);
  427. return result;
  428. }
  429. /* Initialize the wolfCrypt state.
  430. * POST: 0 success.
  431. */
  432. static int test_wolfCrypt_Init(void)
  433. {
  434. int result;
  435. result = wolfCrypt_Init();
  436. result = TEST_RES_CHECK(result == 0);
  437. return result;
  438. } /* END test_wolfCrypt_Init */
  439. static int test_wolfCrypt_Cleanup(void)
  440. {
  441. int result;
  442. result = wolfCrypt_Cleanup();
  443. result = TEST_RES_CHECK(result == 0);
  444. return result;
  445. }
  446. /*----------------------------------------------------------------------------*
  447. | Platform dependent function test
  448. *----------------------------------------------------------------------------*/
  449. static int test_fileAccess(void)
  450. {
  451. int res = TEST_SKIPPED;
  452. #if defined(WOLFSSL_TEST_PLATFORMDEPEND) && !defined(NO_FILESYSTEM)
  453. const char *fname[] = {
  454. svrCertFile, svrKeyFile, caCertFile,
  455. eccCertFile, eccKeyFile, eccRsaCertFile,
  456. cliCertFile, cliCertDerFile, cliKeyFile,
  457. dhParamFile,
  458. cliEccKeyFile, cliEccCertFile, caEccCertFile, edCertFile, edKeyFile,
  459. cliEdCertFile, cliEdKeyFile, caEdCertFile,
  460. NULL
  461. };
  462. const char derfile[] = "./certs/server-cert.der";
  463. XFILE f;
  464. size_t sz;
  465. byte *buff;
  466. int i;
  467. AssertTrue(XFOPEN("badfilename", "rb") == XBADFILE);
  468. for (i=0; fname[i] != NULL ; i++) {
  469. AssertTrue((f = XFOPEN(fname[i], "rb")) != XBADFILE);
  470. XFCLOSE(f);
  471. }
  472. AssertTrue((f = XFOPEN(derfile, "rb")) != XBADFILE);
  473. AssertTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  474. sz = (size_t) XFTELL(f);
  475. XREWIND(f);
  476. AssertTrue(sz == sizeof_server_cert_der_2048);
  477. AssertTrue((buff = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE)) != NULL) ;
  478. AssertTrue(XFREAD(buff, 1, sz, f) == sz);
  479. XMEMCMP(server_cert_der_2048, buff, sz);
  480. res = TEST_RES_CHECK(1);
  481. #endif
  482. return res;
  483. }
  484. /*----------------------------------------------------------------------------*
  485. | Method Allocators
  486. *----------------------------------------------------------------------------*/
  487. static int test_wolfSSL_Method_Allocators(void)
  488. {
  489. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  490. do { \
  491. WOLFSSL_METHOD *method; \
  492. condition(method = allocator()); \
  493. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  494. } while(0)
  495. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  496. TEST_METHOD_ALLOCATOR(a, AssertNotNull)
  497. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  498. TEST_METHOD_ALLOCATOR(a, AssertNull)
  499. #ifndef NO_OLD_TLS
  500. #ifdef WOLFSSL_ALLOW_SSLV3
  501. #ifndef NO_WOLFSSL_SERVER
  502. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  503. #endif
  504. #ifndef NO_WOLFSSL_CLIENT
  505. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  506. #endif
  507. #endif
  508. #ifdef WOLFSSL_ALLOW_TLSV10
  509. #ifndef NO_WOLFSSL_SERVER
  510. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  511. #endif
  512. #ifndef NO_WOLFSSL_CLIENT
  513. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  514. #endif
  515. #endif
  516. #ifndef NO_WOLFSSL_SERVER
  517. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  518. #endif
  519. #ifndef NO_WOLFSSL_CLIENT
  520. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  521. #endif
  522. #endif /* !NO_OLD_TLS */
  523. #ifndef WOLFSSL_NO_TLS12
  524. #ifndef NO_WOLFSSL_SERVER
  525. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  526. #endif
  527. #ifndef NO_WOLFSSL_CLIENT
  528. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  529. #endif
  530. #endif /* !WOLFSSL_NO_TLS12 */
  531. #ifdef WOLFSSL_TLS13
  532. #ifndef NO_WOLFSSL_SERVER
  533. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
  534. #endif
  535. #ifndef NO_WOLFSSL_CLIENT
  536. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
  537. #endif
  538. #endif /* WOLFSSL_TLS13 */
  539. #ifndef NO_WOLFSSL_SERVER
  540. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
  541. #endif
  542. #ifndef NO_WOLFSSL_CLIENT
  543. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  544. #endif
  545. #ifdef WOLFSSL_DTLS
  546. #ifndef NO_OLD_TLS
  547. #ifndef NO_WOLFSSL_SERVER
  548. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  549. #endif
  550. #ifndef NO_WOLFSSL_CLIENT
  551. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  552. #endif
  553. #endif
  554. #ifndef WOLFSSL_NO_TLS12
  555. #ifndef NO_WOLFSSL_SERVER
  556. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  557. #endif
  558. #ifndef NO_WOLFSSL_CLIENT
  559. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  560. #endif
  561. #endif
  562. #endif /* WOLFSSL_DTLS */
  563. #if !defined(NO_OLD_TLS) && defined(OPENSSL_EXTRA)
  564. /* Stubs */
  565. #ifndef NO_WOLFSSL_SERVER
  566. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  567. #endif
  568. #ifndef NO_WOLFSSL_CLIENT
  569. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  570. #endif
  571. #endif
  572. /* Test Either Method (client or server) */
  573. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  574. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_method);
  575. #ifndef NO_OLD_TLS
  576. #ifdef WOLFSSL_ALLOW_TLSV10
  577. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_method);
  578. #endif
  579. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_method);
  580. #endif /* !NO_OLD_TLS */
  581. #ifndef WOLFSSL_NO_TLS12
  582. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_method);
  583. #endif /* !WOLFSSL_NO_TLS12 */
  584. #ifdef WOLFSSL_TLS13
  585. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_method);
  586. #endif /* WOLFSSL_TLS13 */
  587. #ifdef WOLFSSL_DTLS
  588. TEST_VALID_METHOD_ALLOCATOR(wolfDTLS_method);
  589. #ifndef NO_OLD_TLS
  590. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_method);
  591. #endif /* !NO_OLD_TLS */
  592. #ifndef WOLFSSL_NO_TLS12
  593. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_method);
  594. #endif /* !WOLFSSL_NO_TLS12 */
  595. #endif /* WOLFSSL_DTLS */
  596. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  597. return TEST_SUCCESS;
  598. }
  599. /*----------------------------------------------------------------------------*
  600. | Context
  601. *----------------------------------------------------------------------------*/
  602. #ifndef NO_WOLFSSL_SERVER
  603. static int test_wolfSSL_CTX_new(void)
  604. {
  605. WOLFSSL_CTX *ctx;
  606. WOLFSSL_METHOD* method;
  607. AssertNull(ctx = wolfSSL_CTX_new(NULL));
  608. AssertNotNull(method = wolfSSLv23_server_method());
  609. AssertNotNull(ctx = wolfSSL_CTX_new(method));
  610. wolfSSL_CTX_free(ctx);
  611. return TEST_RES_CHECK(1);
  612. }
  613. #endif
  614. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  615. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  616. static int test_for_double_Free(void)
  617. {
  618. WOLFSSL_CTX* ctx;
  619. WOLFSSL* ssl;
  620. int skipTest = 0;
  621. const char* testCertFile;
  622. const char* testKeyFile;
  623. char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
  624. ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
  625. "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
  626. "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
  627. "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
  628. "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
  629. "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
  630. "NULL-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
  631. "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
  632. "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
  633. "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
  634. ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
  635. "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
  636. ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
  637. "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
  638. "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
  639. "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
  640. "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
  641. "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
  642. "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
  643. "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
  644. "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
  645. "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
  646. "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
  647. "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
  648. "LY1305-OLD:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
  649. "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
  650. "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
  651. "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
  652. "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
  653. /* OpenVPN uses a "blacklist" method to specify which ciphers NOT to use */
  654. #ifdef OPENSSL_EXTRA
  655. char openvpnCiphers[] = "DEFAULT:!EXP:!LOW:!MEDIUM:!kDH:!kECDH:!DSS:!PSK:"
  656. "!SRP:!kRSA:!aNULL:!eNULL";
  657. #endif
  658. #ifndef NO_RSA
  659. testCertFile = svrCertFile;
  660. testKeyFile = svrKeyFile;
  661. #elif defined(HAVE_ECC)
  662. testCertFile = eccCertFile;
  663. testKeyFile = eccKeyFile;
  664. #else
  665. skipTest = 1;
  666. #endif
  667. if (skipTest != 1) {
  668. #ifndef NO_WOLFSSL_SERVER
  669. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  670. AssertNotNull(ctx);
  671. #else
  672. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  673. AssertNotNull(ctx);
  674. #endif
  675. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  676. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  677. ssl = wolfSSL_new(ctx);
  678. AssertNotNull(ssl);
  679. /* First test freeing SSL, then CTX */
  680. wolfSSL_free(ssl);
  681. wolfSSL_CTX_free(ctx);
  682. #ifndef NO_WOLFSSL_CLIENT
  683. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  684. AssertNotNull(ctx);
  685. #else
  686. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  687. AssertNotNull(ctx);
  688. #endif
  689. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  690. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  691. ssl = wolfSSL_new(ctx);
  692. AssertNotNull(ssl);
  693. /* Next test freeing CTX then SSL */
  694. wolfSSL_CTX_free(ctx);
  695. wolfSSL_free(ssl);
  696. #ifndef NO_WOLFSSL_SERVER
  697. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  698. AssertNotNull(ctx);
  699. #else
  700. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  701. AssertNotNull(ctx);
  702. #endif
  703. /* Test setting ciphers at ctx level */
  704. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  705. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  706. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
  707. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  708. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  709. /* only update TLSv13 suites */
  710. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384"));
  711. #endif
  712. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  713. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  714. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  715. /* only update pre-TLSv13 suites */
  716. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-GCM-SHA256"));
  717. #endif
  718. #ifdef OPENSSL_EXTRA
  719. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, openvpnCiphers));
  720. #endif
  721. AssertNotNull(ssl = wolfSSL_new(ctx));
  722. wolfSSL_CTX_free(ctx);
  723. wolfSSL_free(ssl);
  724. #ifndef NO_WOLFSSL_CLIENT
  725. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  726. AssertNotNull(ctx);
  727. #else
  728. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  729. AssertNotNull(ctx);
  730. #endif
  731. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  732. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  733. ssl = wolfSSL_new(ctx);
  734. AssertNotNull(ssl);
  735. /* test setting ciphers at SSL level */
  736. AssertTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
  737. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  738. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  739. /* only update TLSv13 suites */
  740. AssertTrue(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"));
  741. #endif
  742. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  743. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  744. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  745. /* only update pre-TLSv13 suites */
  746. AssertTrue(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"));
  747. #endif
  748. wolfSSL_CTX_free(ctx);
  749. wolfSSL_free(ssl);
  750. }
  751. return TEST_RES_CHECK(1);
  752. }
  753. #endif
  754. static int test_wolfSSL_CTX_set_cipher_list_bytes(void)
  755. {
  756. int res = TEST_SKIPPED;
  757. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)) && \
  758. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  759. (!defined(NO_RSA) || defined(HAVE_ECC))
  760. const char* testCertFile;
  761. const char* testKeyFile;
  762. WOLFSSL_CTX* ctx;
  763. WOLFSSL* ssl;
  764. const byte cipherList[] =
  765. {
  766. /* TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x16,
  767. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x39,
  768. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x33,
  769. /* TLS_DH_anon_WITH_AES_128_CBC_SHA */ 0xC0, 0x34,
  770. /* TLS_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x35,
  771. /* TLS_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x2F,
  772. /* TLS_RSA_WITH_NULL_MD5 */ 0xC0, 0x01,
  773. /* TLS_RSA_WITH_NULL_SHA */ 0xC0, 0x02,
  774. /* TLS_PSK_WITH_AES_256_CBC_SHA */ 0xC0, 0x8d,
  775. /* TLS_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0xae,
  776. /* TLS_PSK_WITH_AES_256_CBC_SHA384 */ 0xC0, 0xaf,
  777. /* TLS_PSK_WITH_AES_128_CBC_SHA */ 0xC0, 0x8c,
  778. /* TLS_PSK_WITH_NULL_SHA256 */ 0xC0, 0xb0,
  779. /* TLS_PSK_WITH_NULL_SHA384 */ 0xC0, 0xb1,
  780. /* TLS_PSK_WITH_NULL_SHA */ 0xC0, 0x2c,
  781. /* SSL_RSA_WITH_RC4_128_SHA */ 0xC0, 0x05,
  782. /* SSL_RSA_WITH_RC4_128_MD5 */ 0xC0, 0x04,
  783. /* SSL_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0A,
  784. /* ECC suites, first byte is 0xC0 (ECC_BYTE) */
  785. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x14,
  786. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x13,
  787. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0A,
  788. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x09,
  789. /* TLS_ECDHE_RSA_WITH_RC4_128_SHA */ 0xC0, 0x11,
  790. /* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x07,
  791. /* TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x12,
  792. /* TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x08,
  793. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x27,
  794. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256*/ 0xC0, 0x23,
  795. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x28,
  796. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384*/ 0xC0, 0x24,
  797. /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */ 0xC0, 0x06,
  798. /* TLS_ECDHE_PSK_WITH_NULL_SHA256 */ 0xC0, 0x3a,
  799. /* TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x37,
  800. /* static ECDH, first byte is 0xC0 (ECC_BYTE) */
  801. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0F,
  802. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x0E,
  803. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x05,
  804. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x04,
  805. /* TLS_ECDH_RSA_WITH_RC4_128_SHA */ 0xC0, 0x0C,
  806. /* TLS_ECDH_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x02,
  807. /* TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0D,
  808. /* TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x03,
  809. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x29,
  810. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x25,
  811. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x2A,
  812. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x26,
  813. /* WDM_WITH_NULL_SHA256 */ 0x00, 0xFE, /* wolfSSL DTLS Multicast */
  814. /* SHA256 */
  815. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x6b,
  816. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x67,
  817. /* TLS_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x3d,
  818. /* TLS_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x3c,
  819. /* TLS_RSA_WITH_NULL_SHA256 */ 0x00, 0x3b,
  820. /* TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 */ 0x00, 0xb2,
  821. /* TLS_DHE_PSK_WITH_NULL_SHA256 */ 0x00, 0xb4,
  822. /* SHA384 */
  823. /* TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 */ 0x00, 0xb3,
  824. /* TLS_DHE_PSK_WITH_NULL_SHA384 */ 0x00, 0xb5,
  825. /* AES-GCM */
  826. /* TLS_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9c,
  827. /* TLS_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9d,
  828. /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9e,
  829. /* TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9f,
  830. /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa7,
  831. /* TLS_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xa8,
  832. /* TLS_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa9,
  833. /* TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xaa,
  834. /* TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xab,
  835. /* ECC AES-GCM, first byte is 0xC0 (ECC_BYTE) */
  836. /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2b,
  837. /* TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2c,
  838. /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2d,
  839. /* TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2e,
  840. /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2f,
  841. /* TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x30,
  842. /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x31,
  843. /* TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x32,
  844. /* AES-CCM, first byte is 0xC0 but isn't ECC,
  845. * also, in some of the other AES-CCM suites
  846. * there will be second byte number conflicts
  847. * with non-ECC AES-GCM */
  848. /* TLS_RSA_WITH_AES_128_CCM_8 */ 0xC0, 0xa0,
  849. /* TLS_RSA_WITH_AES_256_CCM_8 */ 0xC0, 0xa1,
  850. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM */ 0xC0, 0xac,
  851. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 */ 0xC0, 0xae,
  852. /* TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 */ 0xC0, 0xaf,
  853. /* TLS_PSK_WITH_AES_128_CCM */ 0xC0, 0xa4,
  854. /* TLS_PSK_WITH_AES_256_CCM */ 0xC0, 0xa5,
  855. /* TLS_PSK_WITH_AES_128_CCM_8 */ 0xC0, 0xa8,
  856. /* TLS_PSK_WITH_AES_256_CCM_8 */ 0xC0, 0xa9,
  857. /* TLS_DHE_PSK_WITH_AES_128_CCM */ 0xC0, 0xa6,
  858. /* TLS_DHE_PSK_WITH_AES_256_CCM */ 0xC0, 0xa7,
  859. /* Camellia */
  860. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x41,
  861. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x84,
  862. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xba,
  863. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc0,
  864. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x45,
  865. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x88,
  866. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xbe,
  867. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc4,
  868. /* chacha20-poly1305 suites first byte is 0xCC (CHACHA_BYTE) */
  869. /* TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa8,
  870. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa9,
  871. /* TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xaa,
  872. /* TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xac,
  873. /* TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xab,
  874. /* TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xad,
  875. /* chacha20-poly1305 earlier version of nonce and padding (CHACHA_BYTE) */
  876. /* TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x13,
  877. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x14,
  878. /* TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x15,
  879. /* ECDHE_PSK RFC8442, first byte is 0xD0 (ECDHE_PSK_BYTE) */
  880. /* TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 */ 0xD0, 0x01,
  881. /* TLS v1.3 cipher suites */
  882. /* TLS_AES_128_GCM_SHA256 */ 0x13, 0x01,
  883. /* TLS_AES_256_GCM_SHA384 */ 0x13, 0x02,
  884. /* TLS_CHACHA20_POLY1305_SHA256 */ 0x13, 0x03,
  885. /* TLS_AES_128_CCM_SHA256 */ 0x13, 0x04,
  886. /* TLS_AES_128_CCM_8_SHA256 */ 0x13, 0x05,
  887. /* TLS v1.3 Integrity only cipher suites - 0xC0 (ECC) first byte */
  888. /* TLS_SHA256_SHA256 */ 0xC0, 0xB4,
  889. /* TLS_SHA384_SHA384 */ 0xC0, 0xB5
  890. };
  891. #ifndef NO_RSA
  892. testCertFile = svrCertFile;
  893. testKeyFile = svrKeyFile;
  894. #elif defined(HAVE_ECC)
  895. testCertFile = eccCertFile;
  896. testKeyFile = eccKeyFile;
  897. #endif
  898. #ifndef NO_WOLFSSL_SERVER
  899. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  900. AssertNotNull(ctx);
  901. #else
  902. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  903. AssertNotNull(ctx);
  904. #endif
  905. AssertTrue(wolfSSL_CTX_set_cipher_list_bytes(ctx, &cipherList[0U],
  906. sizeof(cipherList)));
  907. wolfSSL_CTX_free(ctx);
  908. #ifndef NO_WOLFSSL_SERVER
  909. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  910. AssertNotNull(ctx);
  911. #else
  912. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  913. AssertNotNull(ctx);
  914. #endif
  915. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  916. WOLFSSL_FILETYPE_PEM));
  917. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  918. WOLFSSL_FILETYPE_PEM));
  919. ssl = wolfSSL_new(ctx);
  920. AssertNotNull(ssl);
  921. AssertTrue(wolfSSL_set_cipher_list_bytes(ssl, &cipherList[0U],
  922. sizeof(cipherList)));
  923. wolfSSL_free(ssl);
  924. wolfSSL_CTX_free(ctx);
  925. res = TEST_RES_CHECK(1);
  926. #endif /* (OPENSSL_EXTRA || WOLFSSL_SET_CIPHER_BYTES) &&
  927. (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && (!NO_RSA || HAVE_ECC) */
  928. return res;
  929. }
  930. static int test_wolfSSL_CTX_use_certificate_file(void)
  931. {
  932. int res = TEST_SKIPPED;
  933. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  934. WOLFSSL_CTX *ctx;
  935. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  936. /* invalid context */
  937. AssertFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  938. WOLFSSL_FILETYPE_PEM));
  939. /* invalid cert file */
  940. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  941. WOLFSSL_FILETYPE_PEM));
  942. /* invalid cert type */
  943. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  944. #ifdef NO_RSA
  945. /* rsa needed */
  946. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,WOLFSSL_FILETYPE_PEM));
  947. #else
  948. /* success */
  949. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  950. #endif
  951. wolfSSL_CTX_free(ctx);
  952. res = TEST_RES_CHECK(1);
  953. #endif
  954. return res;
  955. }
  956. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  957. static int test_wolfSSL_CTX_use_certificate_ASN1(void)
  958. {
  959. int res = TEST_SKIPPED;
  960. #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
  961. WOLFSSL_CTX* ctx;
  962. int ret;
  963. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  964. ret = SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
  965. server_cert_der_2048);
  966. wolfSSL_CTX_free(ctx);
  967. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  968. #endif
  969. return res;
  970. }
  971. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  972. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  973. * context using buffer.
  974. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  975. * --enable-testcert flag.
  976. */
  977. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  978. {
  979. int res = TEST_SKIPPED;
  980. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  981. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  982. WOLFSSL_CTX* ctx;
  983. int ret;
  984. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  985. ret = wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  986. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1);
  987. wolfSSL_CTX_free(ctx);
  988. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  989. #endif
  990. return res;
  991. } /*END test_wolfSSL_CTX_use_certificate_buffer*/
  992. static int test_wolfSSL_CTX_use_PrivateKey_file(void)
  993. {
  994. int res = TEST_SKIPPED;
  995. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  996. WOLFSSL_CTX *ctx;
  997. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  998. /* invalid context */
  999. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  1000. WOLFSSL_FILETYPE_PEM));
  1001. /* invalid key file */
  1002. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  1003. WOLFSSL_FILETYPE_PEM));
  1004. /* invalid key type */
  1005. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  1006. /* success */
  1007. #ifdef NO_RSA
  1008. /* rsa needed */
  1009. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  1010. #else
  1011. /* success */
  1012. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  1013. #endif
  1014. wolfSSL_CTX_free(ctx);
  1015. res = TEST_RES_CHECK(1);
  1016. #endif
  1017. return res;
  1018. }
  1019. /* test both file and buffer versions along with unloading trusted peer certs */
  1020. static int test_wolfSSL_CTX_trust_peer_cert(void)
  1021. {
  1022. int res = TEST_SKIPPED;
  1023. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  1024. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_RSA)
  1025. WOLFSSL_CTX *ctx;
  1026. WOLFSSL* ssl;
  1027. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1028. AssertNotNull(ssl = wolfSSL_new(ctx));
  1029. #if !defined(NO_FILESYSTEM)
  1030. /* invalid file */
  1031. AssertIntNE(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  1032. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1033. AssertIntNE(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  1034. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1035. AssertIntNE(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1036. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1037. /* success */
  1038. AssertIntEQ(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1039. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1040. /* unload cert */
  1041. AssertIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1042. AssertIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1043. /* invalid file */
  1044. AssertIntNE(wolfSSL_trust_peer_cert(ssl, NULL,
  1045. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1046. AssertIntNE(wolfSSL_trust_peer_cert(ssl, bogusFile,
  1047. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1048. AssertIntNE(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1049. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1050. /* success */
  1051. AssertIntEQ(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1052. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1053. #ifdef WOLFSSL_LOCAL_X509_STORE
  1054. /* unload cert */
  1055. AssertIntNE(wolfSSL_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1056. AssertIntEQ(wolfSSL_Unload_trust_peers(ssl), WOLFSSL_SUCCESS);
  1057. #endif
  1058. #endif
  1059. /* Test of loading certs from buffers */
  1060. /* invalid buffer */
  1061. AssertIntNE(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  1062. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1063. /* success */
  1064. #ifdef USE_CERT_BUFFERS_1024
  1065. AssertIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  1066. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1067. #endif
  1068. #ifdef USE_CERT_BUFFERS_2048
  1069. AssertIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  1070. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1071. #endif
  1072. /* unload cert */
  1073. AssertIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1074. AssertIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1075. wolfSSL_free(ssl);
  1076. wolfSSL_CTX_free(ctx);
  1077. res = TEST_RES_CHECK(1);
  1078. #endif
  1079. return res;
  1080. }
  1081. static int test_wolfSSL_CTX_load_verify_locations(void)
  1082. {
  1083. int res = TEST_SKIPPED;
  1084. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  1085. WOLFSSL_CTX *ctx;
  1086. #ifndef NO_RSA
  1087. WOLFSSL_CERT_MANAGER* cm;
  1088. #ifdef PERSIST_CERT_CACHE
  1089. int cacheSz;
  1090. #endif
  1091. #endif
  1092. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1093. const char* load_certs_path = "./certs/external";
  1094. const char* load_no_certs_path = "./examples";
  1095. const char* load_expired_path = "./certs/test/expired";
  1096. #endif
  1097. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1098. /* invalid arguments */
  1099. AssertIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL), WOLFSSL_FAILURE);
  1100. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL), WOLFSSL_FAILURE);
  1101. /* invalid ca file */
  1102. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
  1103. WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
  1104. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
  1105. (defined(WOLFSSL_QT) && \
  1106. !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
  1107. /* invalid path */
  1108. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
  1109. WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
  1110. #endif
  1111. /* load ca cert */
  1112. #ifdef NO_RSA
  1113. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1114. WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
  1115. #else /* Skip the following test without RSA certs. */
  1116. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL), WOLFSSL_SUCCESS);
  1117. #ifdef PERSIST_CERT_CACHE
  1118. /* Get cert cache size */
  1119. cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx);
  1120. #endif
  1121. /* Test unloading CA's */
  1122. AssertIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
  1123. #ifdef PERSIST_CERT_CACHE
  1124. /* Verify no certs (result is less than cacheSz) */
  1125. AssertIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1126. #endif
  1127. /* load ca cert again */
  1128. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL), WOLFSSL_SUCCESS);
  1129. /* Test getting CERT_MANAGER */
  1130. AssertNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  1131. /* Test unloading CA's using CM */
  1132. AssertIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  1133. #ifdef PERSIST_CERT_CACHE
  1134. /* Verify no certs (result is less than cacheSz) */
  1135. AssertIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1136. #endif
  1137. #endif
  1138. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1139. /* Test loading CA certificates using a path */
  1140. #ifdef NO_RSA
  1141. /* failure here okay since certs in external directory are RSA */
  1142. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1143. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1144. #else
  1145. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1146. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1147. #endif
  1148. /* Test loading path with no files */
  1149. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_no_certs_path,
  1150. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
  1151. /* Test loading expired CA certificates */
  1152. #ifdef NO_RSA
  1153. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_expired_path,
  1154. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1155. WOLFSSL_SUCCESS);
  1156. #else
  1157. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_expired_path,
  1158. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1159. WOLFSSL_SUCCESS);
  1160. #endif
  1161. /* Test loading CA certificates and ignoring all errors */
  1162. #ifdef NO_RSA
  1163. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1164. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
  1165. #else
  1166. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1167. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
  1168. #endif
  1169. #endif
  1170. wolfSSL_CTX_free(ctx);
  1171. res = TEST_RES_CHECK(1);
  1172. #endif
  1173. return res;
  1174. }
  1175. static int test_wolfSSL_CTX_load_system_CA_certs(void)
  1176. {
  1177. int res = TEST_SKIPPED;
  1178. #if defined(WOLFSSL_SYS_CA_CERTS) && !defined(NO_WOLFSSL_CLIENT) && \
  1179. (!defined(NO_RSA) || defined(HAVE_ECC))
  1180. WOLFSSL_CTX* ctx;
  1181. byte dirValid = 0;
  1182. int ret = 0;
  1183. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1184. if (ctx == NULL) {
  1185. fprintf(stderr, "wolfSSL_CTX_new failed.\n");
  1186. ret = -1;
  1187. }
  1188. if (ret == 0) {
  1189. #if defined(USE_WINDOWS_API) || defined(__APPLE__)
  1190. dirValid = 1;
  1191. #else
  1192. word32 numDirs;
  1193. const char** caDirs = wolfSSL_get_system_CA_dirs(&numDirs);
  1194. if (caDirs == NULL || numDirs == 0) {
  1195. fprintf(stderr, "wolfSSL_get_system_CA_dirs failed.\n");
  1196. ret = -1;
  1197. }
  1198. else {
  1199. ReadDirCtx dirCtx;
  1200. word32 i;
  1201. for (i = 0; i < numDirs; ++i) {
  1202. if (wc_ReadDirFirst(&dirCtx, caDirs[i], NULL) == 0) {
  1203. /* Directory isn't empty. */
  1204. dirValid = 1;
  1205. wc_ReadDirClose(&dirCtx);
  1206. break;
  1207. }
  1208. }
  1209. }
  1210. #endif
  1211. }
  1212. /*
  1213. * If the directory isn't empty, we should be able to load CA
  1214. * certs from it. On Windows/Mac, we assume the CA cert stores are
  1215. * usable.
  1216. */
  1217. if (ret == 0 && dirValid && wolfSSL_CTX_load_system_CA_certs(ctx) !=
  1218. WOLFSSL_SUCCESS) {
  1219. fprintf(stderr, "wolfSSL_CTX_load_system_CA_certs failed.\n");
  1220. ret = -1;
  1221. }
  1222. #ifdef OPENSSL_EXTRA
  1223. if (ret == 0 &&
  1224. wolfSSL_CTX_set_default_verify_paths(ctx) != WOLFSSL_SUCCESS) {
  1225. fprintf(stderr, "wolfSSL_CTX_set_default_verify_paths failed.\n");
  1226. ret = -1;
  1227. }
  1228. #endif /* OPENSSL_EXTRA */
  1229. wolfSSL_CTX_free(ctx);
  1230. res = TEST_RES_CHECK(ret == 0);
  1231. #endif /* WOLFSSL_SYS_CA_CERTS && !NO_WOLFSSL_CLIENT */
  1232. return res;
  1233. }
  1234. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1235. static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz, int file_type)
  1236. {
  1237. int ret;
  1238. WOLFSSL_CERT_MANAGER* cm;
  1239. cm = wolfSSL_CertManagerNew();
  1240. if (cm == NULL) {
  1241. fprintf(stderr, "test_cm_load_ca failed\n");
  1242. return -1;
  1243. }
  1244. ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
  1245. wolfSSL_CertManagerFree(cm);
  1246. return ret;
  1247. }
  1248. static int test_cm_load_ca_file(const char* ca_cert_file)
  1249. {
  1250. int ret = 0;
  1251. byte* cert_buf = NULL;
  1252. size_t cert_sz = 0;
  1253. #if defined(WOLFSSL_PEM_TO_DER)
  1254. DerBuffer* pDer = NULL;
  1255. #endif
  1256. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1257. if (ret == 0) {
  1258. /* normal test */
  1259. ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
  1260. if (ret == WOLFSSL_SUCCESS) {
  1261. /* test including null terminator in length */
  1262. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1263. if (tmp == NULL) {
  1264. ret = MEMORY_E;
  1265. }
  1266. else {
  1267. cert_buf = tmp;
  1268. cert_buf[cert_sz] = '\0';
  1269. ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
  1270. WOLFSSL_FILETYPE_PEM);
  1271. }
  1272. }
  1273. #if defined(WOLFSSL_PEM_TO_DER)
  1274. if (ret == WOLFSSL_SUCCESS) {
  1275. /* test loading DER */
  1276. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1277. if (ret == 0 && pDer != NULL) {
  1278. ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
  1279. WOLFSSL_FILETYPE_ASN1);
  1280. wc_FreeDer(&pDer);
  1281. }
  1282. }
  1283. #endif
  1284. }
  1285. free(cert_buf);
  1286. return ret;
  1287. }
  1288. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  1289. static int test_wolfSSL_CertManagerCheckOCSPResponse(void)
  1290. {
  1291. int res = TEST_SKIPPED;
  1292. #if defined(HAVE_OCSP) && !defined(NO_RSA)
  1293. /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
  1294. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  1295. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
  1296. defined(HAVE_LIGHTY)
  1297. WOLFSSL_CERT_MANAGER* cm = NULL;
  1298. /* Raw OCSP response bytes captured using the following setup:
  1299. * - Run responder with
  1300. * openssl ocsp -port 9999 -ndays 9999
  1301. * -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
  1302. * -rsigner certs/ocsp/ocsp-responder-cert.pem
  1303. * -rkey certs/ocsp/ocsp-responder-key.pem
  1304. * -CA certs/ocsp/intermediate1-ca-cert.pem
  1305. * - Run client with
  1306. * openssl ocsp -host 127.0.0.1:9999 -respout resp.out
  1307. * -issuer certs/ocsp/intermediate1-ca-cert.pem
  1308. * -cert certs/ocsp/server1-cert.pem
  1309. * -CAfile certs/ocsp/root-ca-cert.pem -noverify
  1310. * - Copy raw response from Wireshark.
  1311. */
  1312. byte response[] = {
  1313. 0x30, 0x82, 0x07, 0x40, 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, 0x82, 0x07, 0x35, 0x06,
  1314. 0x09, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, 0x07, 0x26, 0x30, 0x82,
  1315. 0x07, 0x22, 0x30, 0x82, 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09,
  1316. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
  1317. 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10,
  1318. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65,
  1319. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53,
  1320. 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67,
  1321. 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04,
  1322. 0x03, 0x0c, 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20,
  1323. 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a,
  1324. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77,
  1325. 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x31,
  1326. 0x30, 0x35, 0x30, 0x33, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30, 0x5a, 0x30, 0x64, 0x30, 0x62, 0x30,
  1327. 0x3a, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0x71, 0x4d,
  1328. 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18,
  1329. 0xda, 0x04, 0x04, 0x14, 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2,
  1330. 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, 0x00, 0x18, 0x0f, 0x32,
  1331. 0x30, 0x32, 0x31, 0x30, 0x35, 0x30, 0x33, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30, 0x5a, 0xa0, 0x11,
  1332. 0x18, 0x0f, 0x32, 0x30, 0x34, 0x38, 0x30, 0x39, 0x31, 0x37, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30,
  1333. 0x5a, 0xa1, 0x23, 0x30, 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30,
  1334. 0x01, 0x02, 0x04, 0x12, 0x04, 0x10, 0x38, 0x31, 0x60, 0x99, 0xc8, 0x05, 0x09, 0x68, 0x1c, 0x33,
  1335. 0x49, 0xea, 0x45, 0x26, 0x2f, 0x6d, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  1336. 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x4d, 0x58, 0xcc, 0x69, 0x42, 0xe2,
  1337. 0x9e, 0x64, 0xf6, 0x57, 0xce, 0xcb, 0x5f, 0x14, 0xaf, 0x08, 0x6c, 0xc1, 0x52, 0x7a, 0x40, 0x0a,
  1338. 0xfd, 0xb6, 0xce, 0xbb, 0x40, 0xf4, 0xb9, 0xa5, 0x88, 0xc7, 0xf3, 0x42, 0x9f, 0xa9, 0x94, 0xbe,
  1339. 0x6e, 0x7e, 0x09, 0x30, 0x9d, 0x0e, 0x10, 0x6f, 0x9c, 0xd9, 0x4c, 0x71, 0x81, 0x41, 0x64, 0x95,
  1340. 0xf5, 0x85, 0x77, 0x94, 0x81, 0x61, 0x88, 0xc8, 0x0b, 0x50, 0xbb, 0x37, 0xc8, 0x86, 0x76, 0xd8,
  1341. 0xa2, 0xed, 0x66, 0x34, 0xfb, 0xe4, 0xe7, 0x09, 0x8c, 0xf5, 0xb5, 0x85, 0xd0, 0x4b, 0xb5, 0xe6,
  1342. 0x23, 0x62, 0xc3, 0xd0, 0xef, 0xf7, 0x42, 0x89, 0x02, 0x80, 0x64, 0xc9, 0xed, 0xdd, 0x7c, 0x8f,
  1343. 0x0d, 0xe7, 0x43, 0x9b, 0x88, 0x1f, 0xb0, 0xfd, 0x24, 0x01, 0xc7, 0x55, 0xc3, 0x73, 0x12, 0x84,
  1344. 0x09, 0x7c, 0x57, 0xa8, 0x5d, 0xab, 0x75, 0x29, 0x5c, 0x36, 0x97, 0x64, 0x40, 0x0b, 0x55, 0x34,
  1345. 0x0a, 0x5d, 0xb1, 0x1b, 0x61, 0x1b, 0xdc, 0xe5, 0x89, 0xdd, 0x92, 0x62, 0x57, 0xa7, 0x52, 0xb4,
  1346. 0x38, 0x9a, 0x48, 0xc8, 0x3a, 0x14, 0xde, 0x69, 0x42, 0xe9, 0x37, 0xa4, 0xe7, 0x2d, 0x00, 0xa7,
  1347. 0x0b, 0x29, 0x18, 0xd5, 0xce, 0xd9, 0x0d, 0xdd, 0xfe, 0xae, 0x86, 0xb3, 0x32, 0x1c, 0xc9, 0x33,
  1348. 0xb0, 0x2b, 0xb7, 0x3c, 0x0d, 0x43, 0xd8, 0x6c, 0xf2, 0xb7, 0xcd, 0x7b, 0xd5, 0x7d, 0xf0, 0xde,
  1349. 0x34, 0x9f, 0x6d, 0x83, 0xb9, 0xd5, 0xed, 0xe3, 0xda, 0x96, 0x40, 0x9e, 0xd6, 0xa6, 0xfd, 0x70,
  1350. 0x80, 0x70, 0x87, 0x61, 0x0f, 0xc5, 0x9f, 0x75, 0xfe, 0x11, 0x78, 0x34, 0xc9, 0x42, 0x16, 0x73,
  1351. 0x46, 0x7b, 0x05, 0x53, 0x28, 0x43, 0xbe, 0xee, 0x88, 0x67, 0x1d, 0xcc, 0x74, 0xa7, 0xb6, 0x58,
  1352. 0x7b, 0x29, 0x68, 0x40, 0xcf, 0xce, 0x7b, 0x19, 0x33, 0x68, 0xa0, 0x82, 0x04, 0xc6, 0x30, 0x82,
  1353. 0x04, 0xc2, 0x30, 0x82, 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02,
  1354. 0x01, 0x04, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  1355. 0x00, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  1356. 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68,
  1357. 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c,
  1358. 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04,
  1359. 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03,
  1360. 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67,
  1361. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, 0x53,
  1362. 0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09,
  1363. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40,
  1364. 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32,
  1365. 0x31, 0x30, 0x32, 0x31, 0x30, 0x31, 0x39, 0x34, 0x39, 0x35, 0x34, 0x5a, 0x17, 0x0d, 0x32, 0x33,
  1366. 0x31, 0x31, 0x30, 0x37, 0x31, 0x39, 0x34, 0x39, 0x35, 0x34, 0x5a, 0x30, 0x81, 0x9e, 0x31, 0x0b,
  1367. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06,
  1368. 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e,
  1369. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74,
  1370. 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c,
  1371. 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
  1372. 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03,
  1373. 0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, 0x43, 0x53,
  1374. 0x50, 0x20, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, 0x1d, 0x06,
  1375. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f,
  1376. 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x82, 0x01, 0x22,
  1377. 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03,
  1378. 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xb8, 0xba, 0x23,
  1379. 0xb4, 0xf6, 0xc3, 0x7b, 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, 0x1e, 0x63, 0xb9, 0x85,
  1380. 0x23, 0x34, 0x50, 0x6d, 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, 0x5c, 0x2d, 0xf7, 0x63,
  1381. 0x88, 0xd1, 0x07, 0x7a, 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, 0x22, 0xb4, 0x94, 0x41,
  1382. 0x38, 0xe2, 0x9d, 0x74, 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, 0xca, 0x3f, 0x46, 0x2b,
  1383. 0xfe, 0xe5, 0x5a, 0x3f, 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, 0xc3, 0xee, 0x42, 0xf8,
  1384. 0x8d, 0xeb, 0x92, 0x95, 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, 0x16, 0x80, 0x90, 0xce,
  1385. 0x24, 0x35, 0x21, 0xc4, 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, 0x0a, 0x5a, 0x4f, 0x4a,
  1386. 0x73, 0x31, 0x50, 0xee, 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, 0x87, 0xb1, 0x99, 0xe2,
  1387. 0x10, 0xa7, 0x06, 0x72, 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, 0x76, 0xf8, 0xe0, 0x4a,
  1388. 0xec, 0xbc, 0x93, 0xf4, 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, 0xb4, 0x90, 0x30, 0xbb,
  1389. 0x17, 0xb0, 0xfe, 0x97, 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, 0x19, 0x12, 0x3c, 0xab,
  1390. 0x82, 0x71, 0x78, 0xff, 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, 0x8c, 0x27, 0xac, 0x11,
  1391. 0xb8, 0xd8, 0x43, 0x49, 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, 0x24, 0x87, 0x17, 0x3b,
  1392. 0xd8, 0x04, 0x65, 0x6c, 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, 0x73, 0x68, 0x26, 0x14,
  1393. 0x87, 0x95, 0xc3, 0x5f, 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, 0x0a, 0x8b, 0x98, 0xf3,
  1394. 0xe3, 0xff, 0x4e, 0x44, 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, 0x39, 0x02, 0x03, 0x01,
  1395. 0x00, 0x01, 0xa3, 0x82, 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, 0x06, 0x03, 0x55, 0x1d,
  1396. 0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14,
  1397. 0x32, 0x67, 0xe1, 0xb1, 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, 0x40, 0x50, 0xb5, 0x46,
  1398. 0x56, 0xb8, 0x30, 0x36, 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x81, 0xbc, 0x30,
  1399. 0x81, 0xb9, 0x80, 0x14, 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, 0x47, 0xa5, 0x38, 0xd7,
  1400. 0xb0, 0x04, 0x82, 0x3a, 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, 0x81, 0x9a, 0x30, 0x81,
  1401. 0x97, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13,
  1402. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67,
  1403. 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65,
  1404. 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07,
  1405. 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
  1406. 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
  1407. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
  1408. 0x72, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  1409. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
  1410. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, 0x13, 0x06, 0x03, 0x55,
  1411. 0x1d, 0x25, 0x04, 0x0c, 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x09,
  1412. 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03,
  1413. 0x82, 0x01, 0x01, 0x00, 0x07, 0xca, 0xa6, 0xa1, 0x9f, 0xbf, 0xaf, 0x92, 0x41, 0x35, 0x66, 0x51,
  1414. 0xac, 0xbc, 0x2c, 0xec, 0xe7, 0x8d, 0x65, 0x7e, 0xe9, 0x40, 0xfe, 0x5a, 0xab, 0x8a, 0x1d, 0x3d,
  1415. 0x13, 0xdb, 0xb4, 0x43, 0x2c, 0x9a, 0x36, 0x98, 0x21, 0xa5, 0xe8, 0xca, 0xa9, 0x4d, 0xfc, 0xe3,
  1416. 0xf7, 0x45, 0x88, 0xcd, 0x33, 0xbf, 0x8a, 0x62, 0x10, 0x2f, 0xb2, 0xb7, 0x04, 0xef, 0x26, 0x43,
  1417. 0x51, 0x1d, 0x43, 0x62, 0x7d, 0x1e, 0x50, 0xc8, 0xd5, 0x98, 0x94, 0x71, 0x8f, 0x3b, 0x23, 0x26,
  1418. 0xf1, 0x71, 0x8e, 0x1e, 0x3d, 0x3f, 0x21, 0xfd, 0xb7, 0x2d, 0x65, 0xe4, 0x07, 0x65, 0xac, 0x3c,
  1419. 0xfc, 0xc0, 0x47, 0xa9, 0x32, 0xf6, 0xda, 0x26, 0x93, 0x10, 0xb2, 0xd1, 0x6d, 0xc8, 0x81, 0x31,
  1420. 0x7c, 0xb0, 0x6b, 0xc5, 0x22, 0x8d, 0xb3, 0xfa, 0xbe, 0x82, 0xea, 0x41, 0x42, 0xc4, 0xc0, 0xef,
  1421. 0xe3, 0x84, 0x0f, 0x6f, 0x9a, 0x03, 0x63, 0xb3, 0x30, 0xe0, 0x31, 0x81, 0x2a, 0x16, 0xb3, 0x47,
  1422. 0xd9, 0x5b, 0x38, 0x93, 0x07, 0xd0, 0x6e, 0x79, 0x52, 0x2c, 0xe5, 0x50, 0x84, 0x79, 0x10, 0xe7,
  1423. 0xf6, 0x31, 0x7a, 0x3e, 0x48, 0xa2, 0x38, 0x21, 0x90, 0x7a, 0xf2, 0x5f, 0x48, 0xa4, 0x46, 0x93,
  1424. 0x87, 0xdd, 0x5c, 0x83, 0x64, 0xea, 0xb5, 0x99, 0xa2, 0xe9, 0x01, 0x40, 0xfe, 0xf0, 0x48, 0x66,
  1425. 0x4f, 0x96, 0xf7, 0x83, 0x52, 0xf8, 0x6d, 0xf8, 0x5f, 0xed, 0x0c, 0xbb, 0xbe, 0xd0, 0x69, 0x10,
  1426. 0x4b, 0x99, 0x8f, 0xf8, 0x61, 0x53, 0x9d, 0x12, 0xca, 0x86, 0xaa, 0xb1, 0x80, 0xb4, 0xa6, 0xc1,
  1427. 0xcb, 0xb7, 0x48, 0xf7, 0x9f, 0x55, 0xb4, 0x6e, 0xab, 0xd3, 0xa1, 0xaa, 0x4b, 0xa7, 0x21, 0x6e,
  1428. 0x16, 0x7f, 0xad, 0xbb, 0xea, 0x0f, 0x41, 0x80, 0x9b, 0x7f, 0xd6, 0x46, 0xa2, 0xc0, 0x61, 0x72,
  1429. 0x59, 0x59, 0xa0, 0x07
  1430. };
  1431. OcspEntry entry[1];
  1432. CertStatus status[1];
  1433. OcspRequest* request;
  1434. byte serial[] = {0x05};
  1435. byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
  1436. byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
  1437. XMEMSET(entry, 0, sizeof(OcspEntry));
  1438. XMEMSET(status, 0, sizeof(CertStatus));
  1439. AssertNotNull(request = wolfSSL_OCSP_REQUEST_new());
  1440. request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  1441. DYNAMIC_TYPE_OCSP_REQUEST);
  1442. AssertNotNull(request->serial);
  1443. request->serialSz = sizeof(serial);
  1444. XMEMCPY(request->serial, serial, sizeof(serial));
  1445. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  1446. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  1447. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1448. AssertIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  1449. AssertIntEQ(wolfSSL_CertManagerLoadCA(cm,
  1450. "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
  1451. /* Response should be valid. */
  1452. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, response,
  1453. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  1454. /* Flip a byte in the request serial number, response should be invalid
  1455. * now. */
  1456. request->serial[0] ^= request->serial[0];
  1457. AssertIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, response,
  1458. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  1459. wolfSSL_OCSP_REQUEST_free(request);
  1460. wolfSSL_CertManagerFree(cm);
  1461. res = TEST_RES_CHECK(1);
  1462. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  1463. * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
  1464. #endif /* HAVE_OCSP */
  1465. return res;
  1466. }
  1467. static int test_wolfSSL_CheckOCSPResponse(void)
  1468. {
  1469. int result = TEST_SKIPPED;
  1470. #if defined(HAVE_OCSP) && !defined(NO_RSA) && defined(OPENSSL_ALL)
  1471. const char* responseFile = "./certs/ocsp/test-response.der";
  1472. const char* responseMultiFile = "./certs/ocsp/test-multi-response.der";
  1473. const char* responseNoInternFile = "./certs/ocsp/test-response-nointern.der";
  1474. const char* caFile = "./certs/ocsp/root-ca-cert.pem";
  1475. OcspResponse* res = NULL;
  1476. byte data[4096];
  1477. const unsigned char* pt;
  1478. int dataSz;
  1479. XFILE f;
  1480. WOLFSSL_OCSP_BASICRESP* bs;
  1481. WOLFSSL_X509_STORE* st;
  1482. WOLFSSL_X509* issuer;
  1483. f = XFOPEN(responseFile, "rb");
  1484. AssertTrue(f != XBADFILE);
  1485. dataSz = (word32)XFREAD(data, 1, sizeof(data), f);
  1486. AssertIntGT(dataSz, 0);
  1487. XFCLOSE(f);
  1488. pt = data;
  1489. res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz);
  1490. AssertNotNull(res);
  1491. issuer = wolfSSL_X509_load_certificate_file(caFile, SSL_FILETYPE_PEM);
  1492. AssertNotNull(issuer);
  1493. st = wolfSSL_X509_STORE_new();
  1494. AssertNotNull(st);
  1495. AssertIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  1496. bs = wolfSSL_OCSP_response_get1_basic(res);
  1497. AssertNotNull(bs);
  1498. AssertIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  1499. wolfSSL_OCSP_BASICRESP_free(bs);
  1500. wolfSSL_OCSP_RESPONSE_free(res);
  1501. wolfSSL_X509_STORE_free(st);
  1502. wolfSSL_X509_free(issuer);
  1503. /* check loading a response with optional certs */
  1504. f = XFOPEN(responseNoInternFile, "rb");
  1505. AssertTrue(f != XBADFILE);
  1506. dataSz = (word32)XFREAD(data, 1, sizeof(data), f);
  1507. AssertIntGT(dataSz, 0);
  1508. XFCLOSE(f);
  1509. pt = data;
  1510. res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz);
  1511. AssertNotNull(res);
  1512. wolfSSL_OCSP_RESPONSE_free(res);
  1513. /* check loading a response with multiple certs */
  1514. {
  1515. WOLFSSL_CERT_MANAGER* cm = NULL;
  1516. OcspEntry *entry;
  1517. CertStatus* status;
  1518. OcspRequest* request;
  1519. byte serial1[] = {0x01};
  1520. byte serial[] = {0x02};
  1521. byte issuerHash[] = {
  1522. 0x44, 0xA8, 0xDB, 0xD1, 0xBC, 0x97, 0x0A, 0x83,
  1523. 0x3B, 0x5B, 0x31, 0x9A, 0x4C, 0xB8, 0xD2, 0x52,
  1524. 0x37, 0x15, 0x8A, 0x88
  1525. };
  1526. byte issuerKeyHash[] = {
  1527. 0x73, 0xB0, 0x1C, 0xA4, 0x2F, 0x82, 0xCB, 0xCF,
  1528. 0x47, 0xA5, 0x38, 0xD7, 0xB0, 0x04, 0x82, 0x3A,
  1529. 0x7E, 0x72, 0x15, 0x21
  1530. };
  1531. entry = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  1532. DYNAMIC_TYPE_OPENSSL);
  1533. AssertNotNull(entry);
  1534. status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  1535. DYNAMIC_TYPE_OPENSSL);
  1536. AssertNotNull(status);
  1537. XMEMSET(entry, 0, sizeof(OcspEntry));
  1538. XMEMSET(status, 0, sizeof(CertStatus));
  1539. AssertNotNull(request = wolfSSL_OCSP_REQUEST_new());
  1540. request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  1541. DYNAMIC_TYPE_OCSP_REQUEST);
  1542. AssertNotNull(request->serial);
  1543. request->serialSz = sizeof(serial);
  1544. XMEMCPY(request->serial, serial, sizeof(serial));
  1545. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  1546. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  1547. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1548. AssertIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  1549. AssertIntEQ(wolfSSL_CertManagerLoadCA(cm, caFile, NULL),
  1550. WOLFSSL_SUCCESS);
  1551. f = XFOPEN(responseMultiFile, "rb");
  1552. AssertTrue(f != XBADFILE);
  1553. dataSz = (word32)XFREAD(data, 1, sizeof(data), f);
  1554. AssertIntGT(dataSz, 0);
  1555. XFCLOSE(f);
  1556. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  1557. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  1558. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  1559. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  1560. AssertNotNull(entry->status);
  1561. XMEMCPY(request->serial, serial1, sizeof(serial1));
  1562. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  1563. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  1564. /* store both status's in the entry to check that "next" is not
  1565. * overwritten */
  1566. status->next = entry->status;
  1567. entry->status = status;
  1568. XMEMCPY(request->serial, serial, sizeof(serial));
  1569. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  1570. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  1571. AssertNotNull(entry->status->next);
  1572. /* compare the status found */
  1573. AssertIntEQ(status->serialSz, entry->status->serialSz);
  1574. AssertIntEQ(XMEMCMP(status->serial, entry->status->serial,
  1575. status->serialSz), 0);
  1576. wolfSSL_OCSP_CERTID_free(entry);
  1577. wolfSSL_OCSP_REQUEST_free(request);
  1578. wolfSSL_CertManagerFree(cm);
  1579. }
  1580. #if defined(WC_RSA_PSS)
  1581. {
  1582. const char* responsePssFile = "./certs/ocsp/test-response-rsapss.der";
  1583. /* check loading a response with RSA-PSS signature */
  1584. f = XFOPEN(responsePssFile, "rb");
  1585. AssertTrue(f != XBADFILE);
  1586. dataSz = (word32)XFREAD(data, 1, sizeof(data), f);
  1587. AssertIntGT(dataSz, 0);
  1588. XFCLOSE(f);
  1589. pt = data;
  1590. res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz);
  1591. AssertNotNull(res);
  1592. /* try to verify the response */
  1593. issuer = wolfSSL_X509_load_certificate_file(caFile, SSL_FILETYPE_PEM);
  1594. AssertNotNull(issuer);
  1595. st = wolfSSL_X509_STORE_new();
  1596. AssertNotNull(st);
  1597. AssertIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  1598. bs = wolfSSL_OCSP_response_get1_basic(res);
  1599. AssertNotNull(bs);
  1600. AssertIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  1601. wolfSSL_OCSP_BASICRESP_free(bs);
  1602. wolfSSL_OCSP_RESPONSE_free(res);
  1603. wolfSSL_X509_STORE_free(st);
  1604. wolfSSL_X509_free(issuer);
  1605. }
  1606. #endif
  1607. result = TEST_RES_CHECK(1);
  1608. #endif /* HAVE_OCSP */
  1609. return result;
  1610. }
  1611. static int test_wolfSSL_CertManagerLoadCABuffer(void)
  1612. {
  1613. int res = TEST_SKIPPED;
  1614. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1615. const char* ca_cert = "./certs/ca-cert.pem";
  1616. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1617. int ret;
  1618. ret = test_cm_load_ca_file(ca_cert);
  1619. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1620. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1621. #elif defined(NO_RSA)
  1622. AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
  1623. #else
  1624. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1625. #endif
  1626. ret = test_cm_load_ca_file(ca_expired_cert);
  1627. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1628. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1629. res = TEST_RES_CHECK(ret == WOLFSSL_FATAL_ERROR);
  1630. #elif defined(NO_RSA)
  1631. AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
  1632. res = TEST_RES_CHECK(ret == ASN_UNKNOWN_OID_E);
  1633. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  1634. !defined(OPENSSL_COMPATIBLE_DEFAULTS)
  1635. AssertIntEQ(ret, ASN_AFTER_DATE_E);
  1636. res = TEST_RES_CHECK(ret == ASN_AFTER_DATE_E);
  1637. #else
  1638. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1639. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  1640. #endif
  1641. #endif
  1642. return res;
  1643. }
  1644. static int test_wolfSSL_CertManagerGetCerts(void)
  1645. {
  1646. int res = TEST_SKIPPED;
  1647. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1648. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1649. defined(WOLFSSL_SIGNER_DER_CERT)
  1650. WOLFSSL_CERT_MANAGER* cm = NULL;
  1651. WOLFSSL_STACK* sk = NULL;
  1652. X509* x509 = NULL;
  1653. X509* cert1 = NULL;
  1654. FILE* file1 = NULL;
  1655. #ifdef DEBUG_WOLFSSL_VERBOSE
  1656. WOLFSSL_BIO* bio = NULL;
  1657. #endif
  1658. int i = 0;
  1659. int ret = 0;
  1660. const byte* der;
  1661. int derSz = 0;
  1662. AssertNotNull(file1=fopen("./certs/ca-cert.pem", "rb"));
  1663. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  1664. fclose(file1);
  1665. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1666. AssertNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1667. AssertNotNull(der = wolfSSL_X509_get_der(cert1, &derSz));
  1668. ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz, WOLFSSL_FILETYPE_ASN1);
  1669. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  1670. /* Check that ASN_SELF_SIGNED_E is returned for a self-signed cert for QT
  1671. * and full OpenSSL compatibility */
  1672. AssertIntEQ(ret, ASN_SELF_SIGNED_E);
  1673. #else
  1674. AssertIntEQ(ret, ASN_NO_SIGNER_E);
  1675. #endif
  1676. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  1677. "./certs/ca-cert.pem", NULL));
  1678. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1679. for (i = 0; i < sk_X509_num(sk); i++) {
  1680. x509 = sk_X509_value(sk, i);
  1681. AssertIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  1682. #ifdef DEBUG_WOLFSSL_VERBOSE
  1683. bio = BIO_new(wolfSSL_BIO_s_file());
  1684. if (bio != NULL) {
  1685. BIO_set_fp(bio, stderr, BIO_NOCLOSE);
  1686. X509_print(bio, x509);
  1687. BIO_free(bio);
  1688. }
  1689. #endif /* DEBUG_WOLFSSL_VERBOSE */
  1690. }
  1691. wolfSSL_X509_free(cert1);
  1692. sk_X509_pop_free(sk, NULL);
  1693. wolfSSL_CertManagerFree(cm);
  1694. res = TEST_RES_CHECK(1);
  1695. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1696. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1697. defined(WOLFSSL_SIGNER_DER_CERT) */
  1698. return res;
  1699. }
  1700. static int test_wolfSSL_CertManagerSetVerify(void)
  1701. {
  1702. int res = TEST_SKIPPED;
  1703. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1704. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1705. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  1706. int ret = 0;
  1707. WOLFSSL_CERT_MANAGER* cm;
  1708. int tmp = myVerifyAction;
  1709. const char* ca_cert = "./certs/ca-cert.pem";
  1710. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  1711. cm = wolfSSL_CertManagerNew();
  1712. AssertNotNull(cm);
  1713. wolfSSL_CertManagerSetVerify(cm, myVerify);
  1714. ret = wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL);
  1715. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1716. AssertIntEQ(ret, -1);
  1717. #else
  1718. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1719. #endif
  1720. /* Use the test CB that always accepts certs */
  1721. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  1722. ret = wolfSSL_CertManagerVerify(cm, expiredCert, WOLFSSL_FILETYPE_PEM);
  1723. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1724. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  1725. {
  1726. const char* verifyCert = "./certs/server-cert.pem";
  1727. /* Use the test CB that always fails certs */
  1728. myVerifyAction = VERIFY_FORCE_FAIL;
  1729. ret = wolfSSL_CertManagerVerify(cm, verifyCert, WOLFSSL_FILETYPE_PEM);
  1730. AssertIntEQ(ret, VERIFY_CERT_ERROR);
  1731. }
  1732. #endif
  1733. wolfSSL_CertManagerFree(cm);
  1734. myVerifyAction = tmp;
  1735. res = TEST_RES_CHECK(1);
  1736. #endif
  1737. return res;
  1738. }
  1739. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  1740. defined(DEBUG_UNIT_TEST_CERTS)
  1741. /* Used when debugging name constraint tests. Not static to allow use in
  1742. * multiple locations with complex define guards. */
  1743. void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
  1744. {
  1745. BIO* out = BIO_new_file(fileName, "wb");
  1746. if (out != NULL) {
  1747. PEM_write_bio_X509(out, x509);
  1748. BIO_free(out);
  1749. }
  1750. }
  1751. void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName)
  1752. {
  1753. BIO* out = BIO_new_file(fileName, "wb");
  1754. if (out != NULL) {
  1755. BIO_write(out, der, derSz);
  1756. BIO_free(out);
  1757. }
  1758. }
  1759. #else
  1760. #define DEBUG_WRITE_CERT_X509(x509, fileName)
  1761. #define DEBUG_WRITE_DER(der, derSz, fileName)
  1762. #endif
  1763. static int test_wolfSSL_CertManagerNameConstraint(void)
  1764. {
  1765. int res = TEST_SKIPPED;
  1766. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1767. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1768. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1769. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  1770. !defined(NO_SHA256)
  1771. WOLFSSL_CERT_MANAGER* cm;
  1772. WOLFSSL_EVP_PKEY *priv;
  1773. WOLFSSL_X509_NAME* name;
  1774. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  1775. const char* server_cert = "./certs/test/server-goodcn.pem";
  1776. int i = 0;
  1777. static const byte extNameConsOid[] = {85, 29, 30};
  1778. RsaKey key;
  1779. WC_RNG rng;
  1780. byte *der;
  1781. int derSz;
  1782. word32 idx = 0;
  1783. byte *pt;
  1784. WOLFSSL_X509 *x509, *ca;
  1785. wc_InitRng(&rng);
  1786. /* load in CA private key for signing */
  1787. AssertIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, testDevId), 0);
  1788. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  1789. sizeof_server_key_der_2048), 0);
  1790. /* get ca certificate then alter it */
  1791. AssertNotNull(der =
  1792. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  1793. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  1794. WOLFSSL_FILETYPE_ASN1));
  1795. AssertNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  1796. XMEMCPY(der, pt, derSz);
  1797. /* find the name constraint extension and alter it */
  1798. pt = der;
  1799. for (i = 0; i < derSz - 3; i++) {
  1800. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  1801. pt += 3;
  1802. break;
  1803. }
  1804. pt++;
  1805. }
  1806. AssertIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  1807. /* go to the length value and set it to 0 */
  1808. while (i < derSz && *pt != 0x81) {
  1809. pt++;
  1810. i++;
  1811. }
  1812. AssertIntNE(i, derSz); /* did not place to alter */
  1813. pt++;
  1814. *pt = 0x00;
  1815. /* resign the altered certificate */
  1816. AssertIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  1817. FOURK_BUF, &key, NULL, &rng)), 0);
  1818. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1819. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1820. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  1821. wolfSSL_CertManagerFree(cm);
  1822. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1823. wolfSSL_X509_free(x509);
  1824. wc_FreeRsaKey(&key);
  1825. wc_FreeRng(&rng);
  1826. /* add email alt name to satisfy constraint */
  1827. pt = (byte*)server_key_der_2048;
  1828. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  1829. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  1830. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1831. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1832. WOLFSSL_FILETYPE_ASN1));
  1833. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  1834. DEBUG_WRITE_DER(der, derSz, "ca.der");
  1835. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1836. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1837. /* Good cert test with proper alt email name */
  1838. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1839. WOLFSSL_FILETYPE_PEM));
  1840. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1841. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1842. AssertNotNull(name = X509_NAME_new());
  1843. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1844. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1845. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1846. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1847. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1848. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  1849. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1850. X509_NAME_free(name);
  1851. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  1852. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1853. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  1854. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1855. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1856. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1857. wolfSSL_X509_free(x509);
  1858. /* Cert with bad alt name list */
  1859. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1860. WOLFSSL_FILETYPE_PEM));
  1861. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1862. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1863. AssertNotNull(name = X509_NAME_new());
  1864. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1865. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1866. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1867. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1868. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1869. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  1870. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1871. X509_NAME_free(name);
  1872. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  1873. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  1874. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1875. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  1876. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1877. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1878. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1879. wolfSSL_CertManagerFree(cm);
  1880. wolfSSL_X509_free(x509);
  1881. wolfSSL_X509_free(ca);
  1882. wolfSSL_EVP_PKEY_free(priv);
  1883. res = TEST_RES_CHECK(1);
  1884. #endif
  1885. return res;
  1886. }
  1887. static int test_wolfSSL_CertManagerNameConstraint2(void)
  1888. {
  1889. int res = TEST_SKIPPED;
  1890. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1891. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1892. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1893. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  1894. const char* ca_cert = "./certs/test/cert-ext-ndir.der";
  1895. const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
  1896. const char* server_cert = "./certs/server-cert.pem";
  1897. WOLFSSL_CERT_MANAGER* cm;
  1898. WOLFSSL_X509 *x509, *ca;
  1899. const unsigned char *der;
  1900. const unsigned char *pt;
  1901. WOLFSSL_EVP_PKEY *priv;
  1902. WOLFSSL_X509_NAME* name;
  1903. int derSz;
  1904. /* C=US*/
  1905. char altName[] = {
  1906. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  1907. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
  1908. };
  1909. /* C=ID */
  1910. char altNameFail[] = {
  1911. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  1912. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
  1913. };
  1914. /* C=US ST=California*/
  1915. char altNameExc[] = {
  1916. 0x30, 0x22,
  1917. 0x31, 0x0B,
  1918. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  1919. 0x31, 0x13,
  1920. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
  1921. 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
  1922. };
  1923. /* load in CA private key for signing */
  1924. pt = ca_key_der_2048;
  1925. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  1926. sizeof_ca_key_der_2048));
  1927. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1928. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1929. WOLFSSL_FILETYPE_ASN1));
  1930. AssertNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  1931. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1932. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1933. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1934. WOLFSSL_FILETYPE_PEM));
  1935. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1936. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1937. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1938. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1939. #else
  1940. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1941. #endif
  1942. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1943. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1944. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1945. /* add in matching DIR alt name and resign */
  1946. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  1947. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1948. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1949. #else
  1950. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1951. #endif
  1952. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1953. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1954. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1955. wolfSSL_X509_free(x509);
  1956. /* check verify fail */
  1957. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1958. WOLFSSL_FILETYPE_PEM));
  1959. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1960. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1961. /* add in miss matching DIR alt name and resign */
  1962. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  1963. ASN_DIR_TYPE);
  1964. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1965. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1966. #else
  1967. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1968. #endif
  1969. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1970. #ifndef WOLFSSL_NO_ASN_STRICT
  1971. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1972. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1973. #else
  1974. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1975. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1976. #endif
  1977. /* check that it still fails if one bad altname and one good altname is in
  1978. * the certificate */
  1979. wolfSSL_X509_free(x509);
  1980. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1981. WOLFSSL_FILETYPE_PEM));
  1982. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1983. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1984. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  1985. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  1986. ASN_DIR_TYPE);
  1987. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1988. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1989. #else
  1990. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1991. #endif
  1992. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1993. #ifndef WOLFSSL_NO_ASN_STRICT
  1994. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1995. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1996. #else
  1997. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1998. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1999. #endif
  2000. /* check it fails with switching position of bad altname */
  2001. wolfSSL_X509_free(x509);
  2002. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2003. WOLFSSL_FILETYPE_PEM));
  2004. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2005. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2006. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2007. ASN_DIR_TYPE);
  2008. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2009. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2010. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2011. #else
  2012. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2013. #endif
  2014. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2015. #ifndef WOLFSSL_NO_ASN_STRICT
  2016. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2017. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2018. #else
  2019. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2020. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2021. #endif
  2022. wolfSSL_CertManagerFree(cm);
  2023. wolfSSL_X509_free(x509);
  2024. wolfSSL_X509_free(ca);
  2025. /* now test with excluded name constraint */
  2026. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2027. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
  2028. WOLFSSL_FILETYPE_ASN1));
  2029. AssertNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2030. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2031. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2032. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2033. WOLFSSL_FILETYPE_PEM));
  2034. wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
  2035. ASN_DIR_TYPE);
  2036. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2037. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2038. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2039. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2040. #else
  2041. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2042. #endif
  2043. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2044. #ifndef WOLFSSL_NO_ASN_STRICT
  2045. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2046. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2047. #else
  2048. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2049. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2050. #endif
  2051. wolfSSL_CertManagerFree(cm);
  2052. wolfSSL_X509_free(x509);
  2053. wolfSSL_X509_free(ca);
  2054. wolfSSL_EVP_PKEY_free(priv);
  2055. res = TEST_RES_CHECK(1);
  2056. #endif
  2057. return res;
  2058. }
  2059. static int test_wolfSSL_CertManagerNameConstraint3(void)
  2060. {
  2061. int res = TEST_SKIPPED;
  2062. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2063. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2064. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2065. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2066. !defined(NO_SHA256)
  2067. WOLFSSL_CERT_MANAGER* cm;
  2068. WOLFSSL_EVP_PKEY *priv;
  2069. WOLFSSL_X509_NAME* name;
  2070. const char* ca_cert = "./certs/test/cert-ext-mnc.der";
  2071. const char* server_cert = "./certs/test/server-goodcn.pem";
  2072. byte *der;
  2073. int derSz;
  2074. byte *pt;
  2075. WOLFSSL_X509 *x509, *ca;
  2076. pt = (byte*)server_key_der_2048;
  2077. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2078. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2079. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2080. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2081. WOLFSSL_FILETYPE_ASN1));
  2082. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2083. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2084. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2085. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2086. /* check satisfying .wolfssl.com constraint passes */
  2087. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2088. WOLFSSL_FILETYPE_PEM));
  2089. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2090. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2091. AssertNotNull(name = X509_NAME_new());
  2092. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2093. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2094. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2095. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2096. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2097. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2098. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2099. X509_NAME_free(name);
  2100. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2101. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2102. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2103. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2104. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2105. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2106. wolfSSL_X509_free(x509);
  2107. /* check satisfying .random.com constraint passes */
  2108. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2109. WOLFSSL_FILETYPE_PEM));
  2110. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2111. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2112. AssertNotNull(name = X509_NAME_new());
  2113. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2114. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2115. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2116. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2117. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2118. (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
  2119. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2120. X509_NAME_free(name);
  2121. wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
  2122. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2123. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2124. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2125. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2126. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2127. wolfSSL_X509_free(x509);
  2128. /* check fail case when neither constraint is matched */
  2129. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2130. WOLFSSL_FILETYPE_PEM));
  2131. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2132. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2133. AssertNotNull(name = X509_NAME_new());
  2134. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2135. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2136. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2137. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2138. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2139. (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
  2140. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2141. X509_NAME_free(name);
  2142. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2143. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2144. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2145. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2146. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2147. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2148. wolfSSL_CertManagerFree(cm);
  2149. wolfSSL_X509_free(x509);
  2150. wolfSSL_X509_free(ca);
  2151. wolfSSL_EVP_PKEY_free(priv);
  2152. res = TEST_RES_CHECK(1);
  2153. #endif
  2154. return res;
  2155. }
  2156. static int test_wolfSSL_CertManagerNameConstraint4(void)
  2157. {
  2158. int res = TEST_SKIPPED;
  2159. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2160. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2161. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2162. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2163. !defined(NO_SHA256)
  2164. WOLFSSL_CERT_MANAGER* cm;
  2165. WOLFSSL_EVP_PKEY *priv;
  2166. WOLFSSL_X509_NAME* name;
  2167. const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
  2168. const char* server_cert = "./certs/test/server-goodcn.pem";
  2169. byte *der;
  2170. int derSz;
  2171. byte *pt;
  2172. WOLFSSL_X509 *x509, *ca;
  2173. pt = (byte*)server_key_der_2048;
  2174. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2175. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2176. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2177. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2178. WOLFSSL_FILETYPE_ASN1));
  2179. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2180. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2181. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2182. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2183. /* check satisfying wolfssl.com constraint passes */
  2184. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2185. WOLFSSL_FILETYPE_PEM));
  2186. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2187. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2188. AssertNotNull(name = X509_NAME_new());
  2189. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2190. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2191. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2192. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2193. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2194. X509_NAME_free(name);
  2195. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2196. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2197. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2198. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2199. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2200. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2201. wolfSSL_X509_free(x509);
  2202. /* check satisfying example.com constraint passes */
  2203. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2204. WOLFSSL_FILETYPE_PEM));
  2205. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2206. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2207. AssertNotNull(name = X509_NAME_new());
  2208. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2209. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2210. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2211. (byte*)"example.com", 11, -1, 0), SSL_SUCCESS);
  2212. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2213. X509_NAME_free(name);
  2214. wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
  2215. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2216. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2217. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2218. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2219. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2220. wolfSSL_X509_free(x509);
  2221. /* check satisfying wolfssl.com constraint passes with list of DNS's */
  2222. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2223. WOLFSSL_FILETYPE_PEM));
  2224. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2225. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2226. AssertNotNull(name = X509_NAME_new());
  2227. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2228. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2229. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2230. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2231. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2232. X509_NAME_free(name);
  2233. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2234. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2235. wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
  2236. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2237. DEBUG_WRITE_CERT_X509(x509, "good-multiple-constraint-cert.pem");
  2238. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2239. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2240. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2241. wolfSSL_X509_free(x509);
  2242. /* check fail when one DNS in the list is bad */
  2243. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2244. WOLFSSL_FILETYPE_PEM));
  2245. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2246. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2247. AssertNotNull(name = X509_NAME_new());
  2248. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2249. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2250. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2251. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2252. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2253. X509_NAME_free(name);
  2254. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2255. wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
  2256. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2257. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2258. DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
  2259. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2260. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2261. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2262. wolfSSL_X509_free(x509);
  2263. /* check fail case when neither constraint is matched */
  2264. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2265. WOLFSSL_FILETYPE_PEM));
  2266. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2267. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2268. AssertNotNull(name = X509_NAME_new());
  2269. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2270. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2271. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2272. (byte*)"common", 6, -1, 0), SSL_SUCCESS);
  2273. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2274. X509_NAME_free(name);
  2275. wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
  2276. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2277. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2278. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2279. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2280. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2281. wolfSSL_CertManagerFree(cm);
  2282. wolfSSL_X509_free(x509);
  2283. wolfSSL_X509_free(ca);
  2284. wolfSSL_EVP_PKEY_free(priv);
  2285. res = TEST_RES_CHECK(1);
  2286. #endif
  2287. return res;
  2288. }
  2289. static int test_wolfSSL_CertManagerNameConstraint5(void)
  2290. {
  2291. int res = TEST_SKIPPED;
  2292. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2293. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2294. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2295. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2296. !defined(NO_SHA256)
  2297. WOLFSSL_CERT_MANAGER* cm;
  2298. WOLFSSL_EVP_PKEY *priv;
  2299. WOLFSSL_X509_NAME* name;
  2300. const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
  2301. const char* server_cert = "./certs/test/server-goodcn.pem";
  2302. byte *der;
  2303. int derSz;
  2304. byte *pt;
  2305. WOLFSSL_X509 *x509, *ca;
  2306. pt = (byte*)server_key_der_2048;
  2307. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2308. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2309. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2310. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2311. WOLFSSL_FILETYPE_ASN1));
  2312. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2313. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2314. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2315. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2316. /* check satisfying wolfssl.com constraint passes */
  2317. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2318. WOLFSSL_FILETYPE_PEM));
  2319. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2320. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2321. AssertNotNull(name = X509_NAME_new());
  2322. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2323. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2324. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2325. (byte*)"example", 7, -1, 0), SSL_SUCCESS);
  2326. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2327. X509_NAME_free(name);
  2328. wolfSSL_X509_add_altname(x509, "good.example", ASN_DNS_TYPE);
  2329. wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
  2330. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2331. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2332. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2333. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2334. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2335. wolfSSL_X509_free(x509);
  2336. /* fail with DNS check because of common name */
  2337. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2338. WOLFSSL_FILETYPE_PEM));
  2339. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2340. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2341. AssertNotNull(name = X509_NAME_new());
  2342. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2343. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2344. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2345. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2346. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2347. X509_NAME_free(name);
  2348. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2349. wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
  2350. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2351. DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
  2352. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2353. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2354. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2355. wolfSSL_X509_free(x509);
  2356. /* fail on permitted DNS name constraint */
  2357. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2358. WOLFSSL_FILETYPE_PEM));
  2359. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2360. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2361. AssertNotNull(name = X509_NAME_new());
  2362. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2363. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2364. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2365. X509_NAME_free(name);
  2366. wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
  2367. wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
  2368. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2369. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2370. DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
  2371. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2372. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2373. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2374. wolfSSL_X509_free(x509);
  2375. /* fail on permitted email name constraint */
  2376. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2377. WOLFSSL_FILETYPE_PEM));
  2378. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2379. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2380. AssertNotNull(name = X509_NAME_new());
  2381. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2382. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2383. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2384. X509_NAME_free(name);
  2385. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2386. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2387. wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
  2388. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2389. DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
  2390. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2391. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2392. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2393. wolfSSL_X509_free(x509);
  2394. /* success with empty email name */
  2395. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2396. WOLFSSL_FILETYPE_PEM));
  2397. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2398. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2399. AssertNotNull(name = X509_NAME_new());
  2400. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2401. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2402. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2403. X509_NAME_free(name);
  2404. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2405. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2406. DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
  2407. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2408. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2409. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2410. wolfSSL_X509_free(x509);
  2411. wolfSSL_CertManagerFree(cm);
  2412. wolfSSL_X509_free(ca);
  2413. wolfSSL_EVP_PKEY_free(priv);
  2414. res = TEST_RES_CHECK(1);
  2415. #endif
  2416. return res;
  2417. }
  2418. static int test_wolfSSL_FPKI(void)
  2419. {
  2420. int res = TEST_SKIPPED;
  2421. #if defined(WOLFSSL_FPKI) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  2422. XFILE f;
  2423. const char* fpkiCert = "./certs/fpki-cert.der";
  2424. DecodedCert cert;
  2425. byte buf[4096];
  2426. byte* uuid;
  2427. byte* fascn;
  2428. word32 fascnSz;
  2429. word32 uuidSz;
  2430. int bytes;
  2431. f = XFOPEN(fpkiCert, "rb");
  2432. AssertTrue((f != XBADFILE));
  2433. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2434. XFCLOSE(f);
  2435. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2436. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  2437. AssertIntEQ(wc_GetFASCNFromCert(&cert, NULL, &fascnSz), LENGTH_ONLY_E) ;
  2438. fascn = (byte*)XMALLOC(fascnSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2439. AssertNotNull(fascn);
  2440. AssertIntEQ(wc_GetFASCNFromCert(&cert, fascn, &fascnSz), 0);
  2441. XFREE(fascn, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2442. AssertIntEQ(wc_GetUUIDFromCert(&cert, NULL, &uuidSz), LENGTH_ONLY_E);
  2443. uuid = (byte*)XMALLOC(uuidSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2444. AssertNotNull(uuid);
  2445. AssertIntEQ(wc_GetUUIDFromCert(&cert, uuid, &uuidSz), 0);
  2446. XFREE(uuid, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2447. wc_FreeDecodedCert(&cert);
  2448. res = TEST_RES_CHECK(1);
  2449. #endif
  2450. return res;
  2451. }
  2452. /* use RID in confuncture with other names to test parsing of unknown other
  2453. * names */
  2454. static int test_wolfSSL_OtherName(void)
  2455. {
  2456. int res = TEST_SKIPPED;
  2457. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  2458. XFILE f;
  2459. const char* ridCert = "./certs/rid-cert.der";
  2460. DecodedCert cert;
  2461. byte buf[4096];
  2462. int bytes;
  2463. f = XFOPEN(ridCert, "rb");
  2464. AssertTrue((f != XBADFILE));
  2465. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2466. XFCLOSE(f);
  2467. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2468. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  2469. wc_FreeDecodedCert(&cert);
  2470. res = TEST_RES_CHECK(1);
  2471. #endif
  2472. return res;
  2473. }
  2474. static int test_wolfSSL_CertRsaPss(void)
  2475. {
  2476. int res = TEST_SKIPPED;
  2477. /* FIPS v2 and below don't support long salts. */
  2478. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM) && \
  2479. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  2480. (HAVE_FIPS_VERSION > 2))) && (!defined(HAVE_SELFTEST) || \
  2481. (defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION > 2)))
  2482. XFILE f;
  2483. const char* rsaPssSha256Cert = "./certs/rsapss/ca-rsapss.der";
  2484. const char* rsaPssRootSha256Cert = "./certs/rsapss/root-rsapss.pem";
  2485. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  2486. const char* rsaPssSha384Cert = "./certs/rsapss/ca-3072-rsapss.der";
  2487. const char* rsaPssRootSha384Cert = "./certs/rsapss/root-3072-rsapss.pem";
  2488. #endif
  2489. DecodedCert cert;
  2490. byte buf[4096];
  2491. int bytes;
  2492. WOLFSSL_CERT_MANAGER* cm;
  2493. cm = wolfSSL_CertManagerNew();
  2494. AssertNotNull(cm);
  2495. AssertIntEQ(WOLFSSL_SUCCESS,
  2496. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha256Cert, NULL));
  2497. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  2498. AssertIntEQ(WOLFSSL_SUCCESS,
  2499. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha384Cert, NULL));
  2500. #endif
  2501. f = XFOPEN(rsaPssSha256Cert, "rb");
  2502. AssertTrue((f != XBADFILE));
  2503. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2504. XFCLOSE(f);
  2505. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2506. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  2507. wc_FreeDecodedCert(&cert);
  2508. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  2509. RSA_MAX_SIZE >= 3072
  2510. f = XFOPEN(rsaPssSha384Cert, "rb");
  2511. AssertTrue((f != XBADFILE));
  2512. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2513. XFCLOSE(f);
  2514. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2515. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  2516. wc_FreeDecodedCert(&cert);
  2517. #endif
  2518. wolfSSL_CertManagerFree(cm);
  2519. res = TEST_RES_CHECK(1);
  2520. #endif
  2521. return res;
  2522. }
  2523. static int test_wolfSSL_CertManagerCRL(void)
  2524. {
  2525. int res = TEST_SKIPPED;
  2526. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  2527. !defined(NO_RSA)
  2528. const char* ca_cert = "./certs/ca-cert.pem";
  2529. const char* crl1 = "./certs/crl/crl.pem";
  2530. const char* crl2 = "./certs/crl/crl2.pem";
  2531. WOLFSSL_CERT_MANAGER* cm = NULL;
  2532. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2533. AssertIntEQ(WOLFSSL_SUCCESS,
  2534. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2535. AssertIntEQ(WOLFSSL_SUCCESS,
  2536. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  2537. AssertIntEQ(WOLFSSL_SUCCESS,
  2538. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  2539. wolfSSL_CertManagerFreeCRL(cm);
  2540. AssertIntEQ(WOLFSSL_SUCCESS,
  2541. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  2542. AssertIntEQ(WOLFSSL_SUCCESS,
  2543. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2544. wolfSSL_CertManagerFree(cm);
  2545. res = TEST_RES_CHECK(1);
  2546. #endif
  2547. return res;
  2548. }
  2549. static int test_wolfSSL_CTX_load_verify_locations_ex(void)
  2550. {
  2551. int res = TEST_SKIPPED;
  2552. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2553. !defined(NO_WOLFSSL_CLIENT)
  2554. WOLFSSL_CTX* ctx;
  2555. const char* ca_cert = "./certs/ca-cert.pem";
  2556. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2557. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2558. AssertNotNull(ctx);
  2559. /* test good CA */
  2560. AssertTrue(WOLFSSL_SUCCESS ==
  2561. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  2562. WOLFSSL_LOAD_FLAG_NONE));
  2563. /* test expired CA */
  2564. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  2565. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  2566. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2567. #else
  2568. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  2569. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2570. #endif
  2571. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  2572. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  2573. wolfSSL_CTX_free(ctx);
  2574. res = TEST_RES_CHECK(1);
  2575. #endif
  2576. return res;
  2577. }
  2578. static int test_wolfSSL_CTX_load_verify_buffer_ex(void)
  2579. {
  2580. int res = TEST_SKIPPED;
  2581. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2582. defined(USE_CERT_BUFFERS_2048)
  2583. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  2584. WOLFSSL_CTX* ctx;
  2585. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  2586. byte ca_expired_cert[TWOK_BUF];
  2587. word32 sizeof_ca_expired_cert;
  2588. XFILE fp;
  2589. #ifndef NO_WOLFSSL_CLIENT
  2590. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2591. #else
  2592. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2593. #endif
  2594. AssertNotNull(ctx);
  2595. /* test good CA */
  2596. AssertTrue(WOLFSSL_SUCCESS ==
  2597. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  2598. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  2599. WOLFSSL_LOAD_FLAG_NONE));
  2600. /* load expired CA */
  2601. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  2602. fp = XFOPEN(ca_expired_cert_file, "rb");
  2603. AssertTrue(fp != XBADFILE);
  2604. sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  2605. sizeof(ca_expired_cert), fp);
  2606. XFCLOSE(fp);
  2607. /* test expired CA failure */
  2608. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  2609. AssertIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2610. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2611. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2612. #else
  2613. AssertIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2614. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2615. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2616. #endif
  2617. /* test expired CA success */
  2618. AssertIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2619. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2620. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  2621. wolfSSL_CTX_free(ctx);
  2622. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  2623. res = TEST_RES_CHECK(1);
  2624. #endif
  2625. return res;
  2626. }
  2627. static int test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  2628. {
  2629. int res = TEST_SKIPPED;
  2630. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  2631. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048) && \
  2632. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2633. WOLFSSL_CTX* ctx;
  2634. #ifndef NO_WOLFSSL_CLIENT
  2635. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2636. #else
  2637. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2638. #endif
  2639. AssertTrue(WOLFSSL_SUCCESS == wolfSSL_CTX_load_verify_chain_buffer_format(
  2640. ctx, ca_cert_chain_der, sizeof_ca_cert_chain_der,
  2641. WOLFSSL_FILETYPE_ASN1));
  2642. wolfSSL_CTX_free(ctx);
  2643. res = TEST_RES_CHECK(1);
  2644. #endif
  2645. return res;
  2646. }
  2647. static int test_wolfSSL_CTX_add1_chain_cert(void)
  2648. {
  2649. int res = TEST_SKIPPED;
  2650. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
  2651. defined(KEEP_OUR_CERT) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  2652. WOLFSSL_CTX* ctx;
  2653. WOLFSSL* ssl;
  2654. const char *certChain[] = {
  2655. "./certs/intermediate/client-int-cert.pem",
  2656. "./certs/intermediate/ca-int2-cert.pem",
  2657. "./certs/intermediate/ca-int-cert.pem",
  2658. "./certs/ca-cert.pem",
  2659. NULL
  2660. };
  2661. const char** cert;
  2662. WOLFSSL_X509* x509;
  2663. WOLF_STACK_OF(X509)* chain = NULL;
  2664. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2665. AssertNotNull(ssl = wolfSSL_new(ctx));
  2666. for (cert = certChain; *cert != NULL; cert++) {
  2667. x509 = wolfSSL_X509_load_certificate_file(*cert, WOLFSSL_FILETYPE_PEM);
  2668. AssertNotNull(x509);
  2669. AssertIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
  2670. X509_free(x509);
  2671. }
  2672. for (cert = certChain; *cert != NULL; cert++) {
  2673. x509 = wolfSSL_X509_load_certificate_file(*cert, WOLFSSL_FILETYPE_PEM);
  2674. AssertNotNull(x509);
  2675. AssertIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
  2676. X509_free(x509);
  2677. }
  2678. AssertIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
  2679. AssertIntEQ(sk_X509_num(chain), 3);
  2680. AssertIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
  2681. AssertIntEQ(sk_X509_num(chain), 3);
  2682. SSL_free(ssl);
  2683. SSL_CTX_free(ctx);
  2684. res = TEST_RES_CHECK(1);
  2685. #endif
  2686. return res;
  2687. }
  2688. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  2689. {
  2690. int res = TEST_SKIPPED;
  2691. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2692. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2693. const char* server_chain_der = "./certs/server-cert-chain.der";
  2694. const char* client_single_pem = "./certs/client-cert.pem";
  2695. WOLFSSL_CTX* ctx;
  2696. int ret = 0;
  2697. (void)server_chain_der;
  2698. (void)client_single_pem;
  2699. (void)ctx;
  2700. #ifndef NO_WOLFSSL_CLIENT
  2701. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2702. AssertNotNull(ctx);
  2703. #else
  2704. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2705. AssertNotNull(ctx);
  2706. #endif
  2707. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  2708. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2709. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  2710. client_single_pem, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  2711. wolfSSL_CTX_free(ctx);
  2712. res = TEST_RES_CHECK(ret == 0);
  2713. #endif
  2714. return res;
  2715. }
  2716. static int test_wolfSSL_CTX_SetTmpDH_file(void)
  2717. {
  2718. int res = TEST_SKIPPED;
  2719. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  2720. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2721. WOLFSSL_CTX *ctx;
  2722. (void)ctx;
  2723. #ifndef NO_WOLFSSL_CLIENT
  2724. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2725. #else
  2726. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2727. #endif
  2728. /* invalid context */
  2729. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  2730. dhParamFile, WOLFSSL_FILETYPE_PEM));
  2731. /* invalid dhParamFile file */
  2732. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  2733. NULL, WOLFSSL_FILETYPE_PEM));
  2734. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  2735. bogusFile, WOLFSSL_FILETYPE_PEM));
  2736. /* success */
  2737. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  2738. WOLFSSL_FILETYPE_PEM));
  2739. wolfSSL_CTX_free(ctx);
  2740. res = TEST_RES_CHECK(1);
  2741. #endif
  2742. return res;
  2743. }
  2744. static int test_wolfSSL_CTX_SetTmpDH_buffer(void)
  2745. {
  2746. int res = TEST_SKIPPED;
  2747. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  2748. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2749. WOLFSSL_CTX *ctx;
  2750. (void)ctx;
  2751. #ifndef NO_WOLFSSL_CLIENT
  2752. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2753. #else
  2754. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2755. #endif
  2756. /* invalid context */
  2757. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, dh_key_der_2048,
  2758. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2759. /* invalid dhParamFile file */
  2760. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  2761. 0, WOLFSSL_FILETYPE_ASN1));
  2762. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dsa_key_der_2048,
  2763. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2764. /* success */
  2765. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2766. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2767. wolfSSL_CTX_free(ctx);
  2768. res = TEST_RES_CHECK(1);
  2769. #endif
  2770. return res;
  2771. }
  2772. static int test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  2773. {
  2774. int res = TEST_SKIPPED;
  2775. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  2776. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2777. WOLFSSL_CTX *ctx;
  2778. (void)ctx;
  2779. #ifndef NO_WOLFSSL_CLIENT
  2780. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2781. AssertNotNull(ctx);
  2782. #else
  2783. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2784. AssertNotNull(ctx);
  2785. #endif
  2786. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  2787. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2788. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2789. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  2790. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2791. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2792. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  2793. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2794. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2795. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  2796. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2797. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2798. wolfSSL_CTX_free(ctx);
  2799. res = TEST_RES_CHECK(1);
  2800. #endif
  2801. return res;
  2802. }
  2803. static int test_wolfSSL_CTX_der_load_verify_locations(void)
  2804. {
  2805. int res = TEST_SKIPPED;
  2806. #if defined(WOLFSSL_DER_LOAD) && \
  2807. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2808. WOLFSSL_CTX* ctx = NULL;
  2809. const char* derCert = "./certs/server-cert.der";
  2810. const char* nullPath = NULL;
  2811. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  2812. const char* emptyPath = "";
  2813. /* der load Case 1 ctx NULL */
  2814. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2815. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2816. #ifndef NO_WOLFSSL_CLIENT
  2817. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2818. #else
  2819. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2820. #endif
  2821. /* Case 2 filePath NULL */
  2822. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  2823. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2824. /* Case 3 invalid format */
  2825. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2826. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  2827. /* Case 4 filePath not valid */
  2828. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  2829. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2830. /* Case 5 filePath empty */
  2831. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  2832. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2833. #ifndef NO_RSA
  2834. /* Case 6 success case */
  2835. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2836. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2837. #endif
  2838. wolfSSL_CTX_free(ctx);
  2839. res = TEST_RES_CHECK(1);
  2840. #endif
  2841. return res;
  2842. }
  2843. static int test_wolfSSL_CTX_enable_disable(void)
  2844. {
  2845. int res = TEST_SKIPPED;
  2846. #ifndef NO_CERTS
  2847. WOLFSSL_CTX* ctx = NULL;
  2848. #ifdef HAVE_CRL
  2849. AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  2850. AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  2851. #endif
  2852. #ifdef HAVE_OCSP
  2853. AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  2854. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  2855. #endif
  2856. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2857. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2858. AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  2859. AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  2860. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  2861. AssertIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  2862. #endif
  2863. #ifndef NO_WOLFSSL_CLIENT
  2864. #ifdef HAVE_EXTENDED_MASTER
  2865. AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  2866. #endif
  2867. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2868. AssertNotNull(ctx);
  2869. #ifdef HAVE_EXTENDED_MASTER
  2870. AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  2871. #endif
  2872. #elif !defined(NO_WOLFSSL_SERVER)
  2873. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2874. #else
  2875. return TEST_SUCCESS;
  2876. #endif
  2877. #ifdef HAVE_CRL
  2878. AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  2879. AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  2880. #endif
  2881. #ifdef HAVE_OCSP
  2882. AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  2883. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  2884. WOLFSSL_SUCCESS);
  2885. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  2886. WOLFSSL_SUCCESS);
  2887. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  2888. WOLFSSL_SUCCESS);
  2889. #endif
  2890. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2891. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2892. AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  2893. AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  2894. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  2895. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  2896. #endif
  2897. wolfSSL_CTX_free(ctx);
  2898. res = TEST_RES_CHECK(1);
  2899. #endif /* NO_CERTS */
  2900. return res;
  2901. }
  2902. static int test_wolfSSL_CTX_ticket_API(void)
  2903. {
  2904. int res = TEST_SKIPPED;
  2905. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  2906. WOLFSSL_CTX* ctx = NULL;
  2907. void *userCtx = (void*)"this is my ctx";
  2908. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2909. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
  2910. AssertTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
  2911. wolfSSL_CTX_free(ctx);
  2912. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
  2913. AssertNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
  2914. res = TEST_RES_CHECK(1);
  2915. #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
  2916. return res;
  2917. }
  2918. static int test_wolfSSL_set_minmax_proto_version(void)
  2919. {
  2920. int res = TEST_SKIPPED;
  2921. #ifdef OPENSSL_EXTRA
  2922. WOLFSSL_CTX *ctx;
  2923. WOLFSSL *ssl;
  2924. int ret;
  2925. (void)ret;
  2926. (void)ssl;
  2927. #ifndef NO_WOLFSSL_CLIENT
  2928. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2929. AssertNotNull(ssl = wolfSSL_new(ctx));
  2930. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  2931. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  2932. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  2933. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  2934. AssertIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
  2935. AssertIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
  2936. AssertIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
  2937. AssertIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
  2938. wolfSSL_free(ssl);
  2939. wolfSSL_CTX_free(ctx);
  2940. #endif
  2941. #ifndef NO_WOLFSSL_SERVER
  2942. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2943. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  2944. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  2945. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  2946. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  2947. wolfSSL_CTX_free(ctx);
  2948. #endif
  2949. res = TEST_RES_CHECK(1);
  2950. #endif
  2951. return res;
  2952. }
  2953. /*----------------------------------------------------------------------------*
  2954. | SSL
  2955. *----------------------------------------------------------------------------*/
  2956. static int test_server_wolfSSL_new(void)
  2957. {
  2958. int res = TEST_SKIPPED;
  2959. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2960. !defined(NO_WOLFSSL_SERVER)
  2961. WOLFSSL_CTX *ctx;
  2962. WOLFSSL_CTX *ctx_nocert;
  2963. WOLFSSL *ssl;
  2964. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2965. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2966. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  2967. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  2968. /* invalid context */
  2969. AssertNull(ssl = wolfSSL_new(NULL));
  2970. #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && !defined(OPENSSL_EXTRA)
  2971. AssertNull(ssl = wolfSSL_new(ctx_nocert));
  2972. #endif
  2973. /* success */
  2974. AssertNotNull(ssl = wolfSSL_new(ctx));
  2975. wolfSSL_free(ssl);
  2976. wolfSSL_CTX_free(ctx);
  2977. wolfSSL_CTX_free(ctx_nocert);
  2978. res = TEST_RES_CHECK(1);
  2979. #endif
  2980. return res;
  2981. }
  2982. static int test_client_wolfSSL_new(void)
  2983. {
  2984. int res = TEST_SKIPPED;
  2985. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2986. !defined(NO_WOLFSSL_CLIENT)
  2987. WOLFSSL_CTX *ctx;
  2988. WOLFSSL_CTX *ctx_nocert;
  2989. WOLFSSL *ssl;
  2990. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2991. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2992. AssertTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  2993. /* invalid context */
  2994. AssertNull(ssl = wolfSSL_new(NULL));
  2995. /* success */
  2996. AssertNotNull(ssl = wolfSSL_new(ctx_nocert));
  2997. wolfSSL_free(ssl);
  2998. /* success */
  2999. AssertNotNull(ssl = wolfSSL_new(ctx));
  3000. wolfSSL_free(ssl);
  3001. wolfSSL_CTX_free(ctx);
  3002. wolfSSL_CTX_free(ctx_nocert);
  3003. res = TEST_RES_CHECK(1);
  3004. #endif
  3005. return res;
  3006. }
  3007. static int test_wolfSSL_SetTmpDH_file(void)
  3008. {
  3009. int res = TEST_SKIPPED;
  3010. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  3011. !defined(NO_WOLFSSL_SERVER)
  3012. WOLFSSL_CTX *ctx;
  3013. WOLFSSL *ssl;
  3014. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3015. #ifndef NO_RSA
  3016. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  3017. WOLFSSL_FILETYPE_PEM));
  3018. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  3019. WOLFSSL_FILETYPE_PEM));
  3020. #elif defined(HAVE_ECC)
  3021. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  3022. WOLFSSL_FILETYPE_PEM));
  3023. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  3024. WOLFSSL_FILETYPE_PEM));
  3025. #elif defined(HAVE_ED25519)
  3026. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  3027. WOLFSSL_FILETYPE_PEM));
  3028. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  3029. WOLFSSL_FILETYPE_PEM));
  3030. #elif defined(HAVE_ED448)
  3031. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  3032. WOLFSSL_FILETYPE_PEM));
  3033. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  3034. WOLFSSL_FILETYPE_PEM));
  3035. #endif
  3036. AssertNotNull(ssl = wolfSSL_new(ctx));
  3037. /* invalid ssl */
  3038. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  3039. dhParamFile, WOLFSSL_FILETYPE_PEM));
  3040. /* invalid dhParamFile file */
  3041. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  3042. NULL, WOLFSSL_FILETYPE_PEM));
  3043. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  3044. bogusFile, WOLFSSL_FILETYPE_PEM));
  3045. /* success */
  3046. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  3047. WOLFSSL_FILETYPE_PEM));
  3048. wolfSSL_free(ssl);
  3049. wolfSSL_CTX_free(ctx);
  3050. res = TEST_RES_CHECK(1);
  3051. #endif
  3052. return res;
  3053. }
  3054. static int test_wolfSSL_SetTmpDH_buffer(void)
  3055. {
  3056. int res = TEST_SKIPPED;
  3057. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  3058. WOLFSSL_CTX *ctx;
  3059. WOLFSSL *ssl;
  3060. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3061. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  3062. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3063. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  3064. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3065. AssertNotNull(ssl = wolfSSL_new(ctx));
  3066. /* invalid ssl */
  3067. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  3068. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3069. /* invalid dhParamFile file */
  3070. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  3071. 0, WOLFSSL_FILETYPE_ASN1));
  3072. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  3073. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3074. /* success */
  3075. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3076. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3077. wolfSSL_free(ssl);
  3078. wolfSSL_CTX_free(ctx);
  3079. res = TEST_RES_CHECK(1);
  3080. #endif
  3081. return res;
  3082. }
  3083. static int test_wolfSSL_SetMinMaxDhKey_Sz(void)
  3084. {
  3085. int res = TEST_SKIPPED;
  3086. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  3087. WOLFSSL_CTX *ctx, *ctx2;
  3088. WOLFSSL *ssl, *ssl2;
  3089. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3090. AssertNotNull(ctx);
  3091. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  3092. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3093. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  3094. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3095. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  3096. ssl = wolfSSL_new(ctx);
  3097. AssertNotNull(ssl);
  3098. ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3099. AssertNotNull(ctx2);
  3100. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  3101. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3102. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  3103. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3104. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  3105. ssl2 = wolfSSL_new(ctx2);
  3106. AssertNotNull(ssl2);
  3107. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3108. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3109. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  3110. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3111. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3112. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  3113. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3114. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3115. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  3116. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3117. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  3118. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  3119. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3120. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  3121. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3122. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3123. wolfSSL_free(ssl2);
  3124. wolfSSL_CTX_free(ctx2);
  3125. wolfSSL_free(ssl);
  3126. wolfSSL_CTX_free(ctx);
  3127. res = TEST_RES_CHECK(1);
  3128. #endif
  3129. return res;
  3130. }
  3131. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  3132. * allowed.
  3133. * POST: return 1 on success.
  3134. */
  3135. static int test_wolfSSL_SetMinVersion(void)
  3136. {
  3137. int res = TEST_SKIPPED;
  3138. #ifndef NO_WOLFSSL_CLIENT
  3139. int failFlag = WOLFSSL_SUCCESS;
  3140. WOLFSSL_CTX* ctx;
  3141. WOLFSSL* ssl;
  3142. int itr;
  3143. #ifndef NO_OLD_TLS
  3144. const int versions[] = {
  3145. #ifdef WOLFSSL_ALLOW_TLSV10
  3146. WOLFSSL_TLSV1,
  3147. #endif
  3148. WOLFSSL_TLSV1_1,
  3149. WOLFSSL_TLSV1_2};
  3150. #elif !defined(WOLFSSL_NO_TLS12)
  3151. const int versions[] = { WOLFSSL_TLSV1_2 };
  3152. #else
  3153. const int versions[] = { WOLFSSL_TLSV1_3 };
  3154. #endif
  3155. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3156. ssl = wolfSSL_new(ctx);
  3157. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  3158. if (wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS) {
  3159. failFlag = WOLFSSL_FAILURE;
  3160. }
  3161. }
  3162. wolfSSL_free(ssl);
  3163. wolfSSL_CTX_free(ctx);
  3164. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  3165. #endif
  3166. return res;
  3167. } /* END test_wolfSSL_SetMinVersion */
  3168. /*----------------------------------------------------------------------------*
  3169. | EC
  3170. *----------------------------------------------------------------------------*/
  3171. /* Test function for EC_POINT_new, EC_POINT_mul, EC_POINT_free,
  3172. EC_GROUP_new_by_curve_name, EC_GROUP_order_bits
  3173. */
  3174. #ifdef OPENSSL_EXTRA
  3175. static int test_wolfSSL_EC(void)
  3176. {
  3177. int res = TEST_SKIPPED;
  3178. #if !defined(WOLFSSL_SP_MATH) && \
  3179. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  3180. #if defined(HAVE_ECC)
  3181. BN_CTX *ctx;
  3182. EC_GROUP *group;
  3183. EC_GROUP *group2;
  3184. EC_POINT *Gxy, *new_point, *set_point;
  3185. BIGNUM *k = NULL, *Gx = NULL, *Gy = NULL, *Gz = NULL;
  3186. BIGNUM *X, *Y;
  3187. BIGNUM *set_point_bn;
  3188. char* hexStr;
  3189. int group_bits;
  3190. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD17AF381913FF7A96314EA47055EA0FD0";
  3191. /* NISTP256R1 Gx/Gy */
  3192. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
  3193. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5";
  3194. #ifndef HAVE_SELFTEST
  3195. EC_POINT *tmp;
  3196. size_t bin_len;
  3197. unsigned char* buf = NULL;
  3198. const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C2964FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5";
  3199. const unsigned char binUncompG[] = {
  3200. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  3201. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  3202. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  3203. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  3204. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  3205. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  3206. };
  3207. #ifdef HAVE_COMP_KEY
  3208. const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
  3209. const unsigned char binCompG[] = {
  3210. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  3211. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  3212. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  3213. };
  3214. #endif
  3215. #endif
  3216. AssertNotNull(ctx = BN_CTX_new());
  3217. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  3218. AssertNotNull(group2 = EC_GROUP_dup(group));
  3219. AssertIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  3220. AssertNotNull(Gxy = EC_POINT_new(group));
  3221. AssertNotNull(new_point = EC_POINT_new(group));
  3222. AssertNotNull(set_point = EC_POINT_new(group));
  3223. AssertNotNull(X = BN_new());
  3224. AssertNotNull(Y = BN_new());
  3225. AssertNotNull(set_point_bn = BN_new());
  3226. /* load test values */
  3227. AssertIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  3228. AssertIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  3229. AssertIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  3230. AssertIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  3231. /* populate coordinates for input point */
  3232. Gxy->X = Gx;
  3233. Gxy->Y = Gy;
  3234. Gxy->Z = Gz;
  3235. #ifndef HAVE_SELFTEST
  3236. /* perform point multiplication */
  3237. AssertIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), WOLFSSL_SUCCESS);
  3238. AssertIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), WOLFSSL_SUCCESS);
  3239. AssertIntEQ(BN_is_zero(new_point->X), 0);
  3240. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  3241. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  3242. AssertIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), WOLFSSL_SUCCESS);
  3243. AssertIntEQ(BN_is_zero(new_point->X), 0);
  3244. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  3245. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  3246. AssertIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), WOLFSSL_SUCCESS);
  3247. AssertIntEQ(BN_is_zero(new_point->X), 0);
  3248. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  3249. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  3250. #else
  3251. AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy, ctx), WOLFSSL_SUCCESS);
  3252. AssertIntEQ(BN_is_zero(new_point->X), 0);
  3253. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  3254. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  3255. #endif
  3256. /* check if point X coordinate is zero */
  3257. AssertIntEQ(BN_is_zero(new_point->X), 0);
  3258. #ifdef USE_ECC_B_PARAM
  3259. AssertIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  3260. #endif /* USE_ECC_B_PARAM */
  3261. /* Force non-affine coordinates */
  3262. AssertIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  3263. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  3264. new_point->inSet = 0;
  3265. /* extract the coordinates from point */
  3266. AssertIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y, ctx), WOLFSSL_SUCCESS);
  3267. /* check if point X coordinate is zero */
  3268. AssertIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  3269. /* set the same X and Y points in another object */
  3270. AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y, ctx), WOLFSSL_SUCCESS);
  3271. /* compare points as they should be the same */
  3272. AssertIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  3273. /* Test copying */
  3274. AssertIntEQ(EC_POINT_copy(new_point, set_point), 1);
  3275. /* Test inverting */
  3276. AssertIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  3277. AssertPtrEq(EC_POINT_point2bn(group, set_point, POINT_CONVERSION_UNCOMPRESSED,
  3278. set_point_bn, ctx), set_point_bn);
  3279. /* check bn2hex */
  3280. hexStr = BN_bn2hex(k);
  3281. AssertStrEQ(hexStr, kTest);
  3282. #ifndef NO_FILESYSTEM
  3283. BN_print_fp(stderr, k);
  3284. fprintf(stderr, "\n");
  3285. #endif
  3286. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  3287. hexStr = BN_bn2hex(Gx);
  3288. AssertStrEQ(hexStr, kGx);
  3289. #ifndef NO_FILESYSTEM
  3290. BN_print_fp(stderr, Gx);
  3291. fprintf(stderr, "\n");
  3292. #endif
  3293. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  3294. hexStr = BN_bn2hex(Gy);
  3295. AssertStrEQ(hexStr, kGy);
  3296. #ifndef NO_FILESYSTEM
  3297. BN_print_fp(stderr, Gy);
  3298. fprintf(stderr, "\n");
  3299. #endif
  3300. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  3301. #ifndef HAVE_SELFTEST
  3302. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  3303. AssertStrEQ(hexStr, uncompG);
  3304. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  3305. #ifdef HAVE_COMP_KEY
  3306. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  3307. AssertStrEQ(hexStr, compG);
  3308. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  3309. #endif
  3310. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx);
  3311. AssertIntEQ(bin_len, sizeof(binUncompG));
  3312. AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL, DYNAMIC_TYPE_ECC));
  3313. AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, buf,
  3314. bin_len, ctx), bin_len);
  3315. AssertIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  3316. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  3317. #ifdef HAVE_COMP_KEY
  3318. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL, 0, ctx);
  3319. AssertIntEQ(bin_len, sizeof(binCompG));
  3320. AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL, DYNAMIC_TYPE_ECC));
  3321. AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  3322. bin_len, ctx), bin_len);
  3323. AssertIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  3324. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  3325. #endif
  3326. AssertNotNull(tmp = EC_POINT_new(group));
  3327. AssertIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG), ctx), 1);
  3328. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  3329. EC_POINT_free(tmp);
  3330. #ifdef HAVE_COMP_KEY
  3331. AssertNotNull(tmp = EC_POINT_new(group));
  3332. AssertIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx), 1);
  3333. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  3334. EC_POINT_free(tmp);
  3335. #endif
  3336. #endif
  3337. /* test BN_mod_add */
  3338. AssertIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  3339. (WOLFSSL_BIGNUM*)BN_value_one(),
  3340. (WOLFSSL_BIGNUM*)BN_value_one(), NULL), 1);
  3341. AssertIntEQ(BN_is_zero(new_point->Z), 1);
  3342. /* cleanup */
  3343. BN_free(X);
  3344. BN_free(Y);
  3345. BN_free(k);
  3346. BN_free(set_point_bn);
  3347. EC_POINT_free(new_point);
  3348. EC_POINT_free(set_point);
  3349. EC_POINT_free(Gxy);
  3350. EC_GROUP_free(group);
  3351. EC_GROUP_free(group2);
  3352. BN_CTX_free(ctx);
  3353. res = TEST_RES_CHECK(1);
  3354. #endif /* HAVE_ECC */
  3355. #endif /* OPENSSL_EXTRA && !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  3356. return res;
  3357. }
  3358. #endif /* OPENSSL_EXTRA */
  3359. #ifndef NO_BIO
  3360. static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  3361. {
  3362. int res = TEST_SKIPPED;
  3363. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA)
  3364. EC_GROUP *group;
  3365. BIO* bio;
  3366. AssertNotNull(bio = BIO_new(BIO_s_file()));
  3367. AssertIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  3368. AssertNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  3369. AssertIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  3370. EC_GROUP_free(group);
  3371. BIO_free(bio);
  3372. res = TEST_RES_CHECK(1);
  3373. #endif /* HAVE_ECC */
  3374. return res;
  3375. }
  3376. #endif /* !NO_BIO */
  3377. # if defined(OPENSSL_EXTRA)
  3378. static int test_wolfSSL_ECDSA_SIG(void)
  3379. {
  3380. int res = TEST_SKIPPED;
  3381. #ifdef HAVE_ECC
  3382. WOLFSSL_ECDSA_SIG* sig = NULL;
  3383. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  3384. const unsigned char* cp;
  3385. unsigned char* p;
  3386. unsigned char outSig[8];
  3387. unsigned char sigData[8] =
  3388. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  3389. sig = wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData));
  3390. AssertNull(sig);
  3391. cp = sigData;
  3392. AssertNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  3393. AssertIntEQ((cp == sigData + 8), 1);
  3394. cp = sigData;
  3395. AssertNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  3396. AssertNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  3397. AssertIntEQ((sig == sig2), 1);
  3398. cp = outSig;
  3399. p = outSig;
  3400. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  3401. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  3402. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  3403. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  3404. AssertIntEQ((p == outSig + 8), 1);
  3405. AssertIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  3406. wolfSSL_ECDSA_SIG_free(sig);
  3407. res = TEST_RES_CHECK(1);
  3408. #endif /* HAVE_ECC */
  3409. return res;
  3410. }
  3411. static int test_EC_i2d(void)
  3412. {
  3413. int res = TEST_SKIPPED;
  3414. #if defined(HAVE_ECC) && !defined(HAVE_FIPS)
  3415. EC_KEY *key;
  3416. EC_KEY *copy;
  3417. int len;
  3418. unsigned char *buf = NULL;
  3419. const unsigned char *tmp = NULL;
  3420. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  3421. AssertIntEQ(EC_KEY_generate_key(key), 1);
  3422. AssertIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  3423. AssertIntEQ(i2d_EC_PUBKEY(key, &buf), len);
  3424. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3425. buf = NULL;
  3426. AssertIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  3427. AssertIntEQ(i2d_ECPrivateKey(key, &buf), len);
  3428. tmp = buf;
  3429. AssertNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  3430. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3431. buf = NULL;
  3432. AssertIntGT((len = i2o_ECPublicKey(key, &buf)), 0);
  3433. tmp = buf;
  3434. AssertNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  3435. AssertIntEQ(EC_KEY_check_key(key), 1);
  3436. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  3437. EC_KEY_free(key);
  3438. EC_KEY_free(copy);
  3439. res = TEST_RES_CHECK(1);
  3440. #endif /* HAVE_ECC */
  3441. return res;
  3442. }
  3443. static int test_ECDSA_size_sign(void)
  3444. {
  3445. int res = TEST_SKIPPED;
  3446. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  3447. EC_KEY *key;
  3448. int id;
  3449. byte hash[WC_MAX_DIGEST_SIZE];
  3450. byte sig[ECC_MAX_SIG_SIZE];
  3451. unsigned int sigSz = sizeof(sig);
  3452. XMEMSET(hash, 123, sizeof(hash));
  3453. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  3454. AssertIntEQ(id, ECC_SECP256R1);
  3455. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  3456. AssertIntEQ(EC_KEY_generate_key(key), 1);
  3457. AssertIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  3458. AssertIntGE(ECDSA_size(key), sigSz);
  3459. AssertIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, key), 1);
  3460. EC_KEY_free(key);
  3461. res = TEST_RES_CHECK(1);
  3462. #endif /* HAVE_ECC && !NO_ECC256 && !NO_ECC_SECP */
  3463. return res;
  3464. }
  3465. static int test_ED25519(void)
  3466. {
  3467. int res = TEST_SKIPPED;
  3468. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  3469. defined(WOLFSSL_KEY_GEN)
  3470. byte priv[ED25519_PRV_KEY_SIZE];
  3471. unsigned int privSz = (unsigned int)sizeof(priv);
  3472. byte pub[ED25519_PUB_KEY_SIZE];
  3473. unsigned int pubSz = (unsigned int)sizeof(pub);
  3474. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  3475. const char* msg = TEST_STRING;
  3476. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  3477. byte sig[ED25519_SIG_SIZE];
  3478. unsigned int sigSz = (unsigned int)sizeof(sig);
  3479. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  3480. AssertIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  3481. WOLFSSL_SUCCESS);
  3482. AssertIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  3483. AssertIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  3484. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  3485. AssertIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  3486. &sigSz), WOLFSSL_SUCCESS);
  3487. AssertIntEQ(sigSz, ED25519_SIG_SIZE);
  3488. #ifdef HAVE_ED25519_VERIFY
  3489. AssertIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  3490. sigSz), WOLFSSL_SUCCESS);
  3491. #endif /* HAVE_ED25519_VERIFY */
  3492. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  3493. res = TEST_RES_CHECK(1);
  3494. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
  3495. return res;
  3496. }
  3497. static int test_ED448(void)
  3498. {
  3499. int res = TEST_SKIPPED;
  3500. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  3501. defined(WOLFSSL_KEY_GEN)
  3502. byte priv[ED448_PRV_KEY_SIZE];
  3503. unsigned int privSz = (unsigned int)sizeof(priv);
  3504. byte pub[ED448_PUB_KEY_SIZE];
  3505. unsigned int pubSz = (unsigned int)sizeof(pub);
  3506. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  3507. const char* msg = TEST_STRING;
  3508. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  3509. byte sig[ED448_SIG_SIZE];
  3510. unsigned int sigSz = (unsigned int)sizeof(sig);
  3511. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  3512. AssertIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz),
  3513. WOLFSSL_SUCCESS);
  3514. AssertIntEQ(privSz, ED448_PRV_KEY_SIZE);
  3515. AssertIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  3516. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  3517. AssertIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  3518. &sigSz), WOLFSSL_SUCCESS);
  3519. AssertIntEQ(sigSz, ED448_SIG_SIZE);
  3520. #ifdef HAVE_ED448_VERIFY
  3521. AssertIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  3522. sigSz), WOLFSSL_SUCCESS);
  3523. #endif /* HAVE_ED448_VERIFY */
  3524. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  3525. res = TEST_RES_CHECK(1);
  3526. #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
  3527. return res;
  3528. }
  3529. #endif /* OPENSSL_EXTRA */
  3530. #include <wolfssl/openssl/pem.h>
  3531. /*----------------------------------------------------------------------------*
  3532. | EVP
  3533. *----------------------------------------------------------------------------*/
  3534. static int test_wolfSSL_EVP_PKEY_print_public(void)
  3535. {
  3536. int res = TEST_SKIPPED;
  3537. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  3538. WOLFSSL_BIO* rbio = NULL;
  3539. WOLFSSL_BIO* wbio = NULL;
  3540. WOLFSSL_EVP_PKEY* pkey = NULL;
  3541. char line[256] = { 0 };
  3542. char line1[256] = { 0 };
  3543. int i;
  3544. /* test error cases */
  3545. AssertIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
  3546. /*
  3547. * test RSA public key print
  3548. * in this test, pass '3' for indent
  3549. */
  3550. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
  3551. rbio = BIO_new_mem_buf( client_keypub_der_1024,
  3552. sizeof_client_keypub_der_1024);
  3553. AssertNotNull(rbio);
  3554. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3555. AssertNotNull(pkey);
  3556. wbio = BIO_new(BIO_s_mem());
  3557. AssertNotNull(wbio);
  3558. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
  3559. BIO_gets(wbio, line, sizeof(line));
  3560. strcpy(line1, " RSA Public-Key: (1024 bit)\n");
  3561. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3562. BIO_gets(wbio, line, sizeof(line));
  3563. strcpy(line1, " Modulus:\n");
  3564. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3565. BIO_gets(wbio, line, sizeof(line));
  3566. strcpy(line1, " 00:bc:73:0e:a8:49:f3:74:a2:a9:ef:18:a5:da:55:\n");
  3567. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3568. /* skip to the end of modulus element*/
  3569. for (i = 0; i < 8 ;i++) {
  3570. BIO_gets(wbio, line, sizeof(line));
  3571. }
  3572. BIO_gets(wbio, line, sizeof(line));
  3573. strcpy(line1, " Exponent: 65537 (0x010001)\n");
  3574. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3575. /* should reach EOF */
  3576. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3577. EVP_PKEY_free(pkey);
  3578. pkey = NULL;
  3579. BIO_free(rbio);
  3580. BIO_free(wbio);
  3581. rbio = NULL;
  3582. wbio = NULL;
  3583. #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
  3584. /*
  3585. * test DSA public key print
  3586. */
  3587. #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
  3588. rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
  3589. sizeof_dsa_pub_key_der_2048);
  3590. AssertNotNull(rbio);
  3591. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3592. AssertNotNull(pkey);
  3593. wbio = BIO_new(BIO_s_mem());
  3594. AssertNotNull(wbio);
  3595. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3596. BIO_gets(wbio, line, sizeof(line));
  3597. strcpy(line1, "DSA Public-Key: (2048 bit)\n");
  3598. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3599. BIO_gets(wbio, line, sizeof(line));
  3600. strcpy(line1, "pub:\n");
  3601. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3602. BIO_gets(wbio, line, sizeof(line));
  3603. strcpy(line1,
  3604. " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
  3605. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3606. /* skip to the end of pub element*/
  3607. for (i = 0; i < 17 ;i++) {
  3608. BIO_gets(wbio, line, sizeof(line));
  3609. }
  3610. BIO_gets(wbio, line, sizeof(line));
  3611. strcpy(line1, "P:\n");
  3612. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3613. /* skip to the end of P element*/
  3614. for (i = 0; i < 18 ;i++) {
  3615. BIO_gets(wbio, line, sizeof(line));
  3616. }
  3617. BIO_gets(wbio, line, sizeof(line));
  3618. strcpy(line1, "Q:\n");
  3619. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3620. /* skip to the end of Q element*/
  3621. for (i = 0; i < 3 ;i++) {
  3622. BIO_gets(wbio, line, sizeof(line));
  3623. }
  3624. BIO_gets(wbio, line, sizeof(line));
  3625. strcpy(line1, "G:\n");
  3626. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3627. /* skip to the end of G element*/
  3628. for (i = 0; i < 18 ;i++) {
  3629. BIO_gets(wbio, line, sizeof(line));
  3630. }
  3631. /* should reach EOF */
  3632. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3633. EVP_PKEY_free(pkey);
  3634. pkey = NULL;
  3635. BIO_free(rbio);
  3636. BIO_free(wbio);
  3637. rbio = NULL;
  3638. wbio = NULL;
  3639. #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
  3640. /*
  3641. * test ECC public key print
  3642. */
  3643. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  3644. rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
  3645. sizeof_ecc_clikeypub_der_256);
  3646. AssertNotNull(rbio);
  3647. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3648. AssertNotNull(pkey);
  3649. wbio = BIO_new(BIO_s_mem());
  3650. AssertNotNull(wbio);
  3651. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3652. BIO_gets(wbio, line, sizeof(line));
  3653. strcpy(line1, "Public-Key: (256 bit)\n");
  3654. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3655. BIO_gets(wbio, line, sizeof(line));
  3656. strcpy(line1, "pub:\n");
  3657. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3658. BIO_gets(wbio, line, sizeof(line));
  3659. strcpy(line1,
  3660. " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
  3661. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3662. /* skip to the end of pub element*/
  3663. for (i = 0; i < 4 ;i++) {
  3664. BIO_gets(wbio, line, sizeof(line));
  3665. }
  3666. BIO_gets(wbio, line, sizeof(line));
  3667. strcpy(line1, "ASN1 OID: prime256v1\n");
  3668. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3669. BIO_gets(wbio, line, sizeof(line));
  3670. strcpy(line1, "NIST CURVE: P-256\n");
  3671. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3672. /* should reach EOF */
  3673. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3674. EVP_PKEY_free(pkey);
  3675. pkey = NULL;
  3676. BIO_free(rbio);
  3677. BIO_free(wbio);
  3678. rbio = NULL;
  3679. wbio = NULL;
  3680. #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
  3681. /*
  3682. * test DH public key print
  3683. */
  3684. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  3685. rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
  3686. sizeof_dh_pub_key_der_2048);
  3687. AssertNotNull(rbio);
  3688. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3689. AssertNotNull(pkey);
  3690. wbio = BIO_new(BIO_s_mem());
  3691. AssertNotNull(wbio);
  3692. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3693. BIO_gets(wbio, line, sizeof(line));
  3694. strcpy(line1, "DH Public-Key: (2048 bit)\n");
  3695. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3696. BIO_gets(wbio, line, sizeof(line));
  3697. strcpy(line1, "public-key:\n");
  3698. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3699. BIO_gets(wbio, line, sizeof(line));
  3700. strcpy(line1,
  3701. " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
  3702. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3703. /* skip to the end of public-key element*/
  3704. for (i = 0; i < 17 ;i++) {
  3705. BIO_gets(wbio, line, sizeof(line));
  3706. }
  3707. BIO_gets(wbio, line, sizeof(line));
  3708. strcpy(line1, "prime:\n");
  3709. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3710. BIO_gets(wbio, line, sizeof(line));
  3711. strcpy(line1,
  3712. " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
  3713. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3714. /* skip to the end of prime element*/
  3715. for (i = 0; i < 17 ;i++) {
  3716. BIO_gets(wbio, line, sizeof(line));
  3717. }
  3718. BIO_gets(wbio, line, sizeof(line));
  3719. strcpy(line1, "generator: 2 (0x02)\n");
  3720. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3721. /* should reach EOF */
  3722. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3723. EVP_PKEY_free(pkey);
  3724. pkey = NULL;
  3725. BIO_free(rbio);
  3726. BIO_free(wbio);
  3727. rbio = NULL;
  3728. wbio = NULL;
  3729. #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
  3730. /* to prevent "unused variable" warning */
  3731. (void)pkey;
  3732. (void)wbio;
  3733. (void)rbio;
  3734. (void)line;
  3735. (void)line1;
  3736. (void)i;
  3737. res = TEST_RES_CHECK(1);
  3738. #endif /* OPENSSL_EXTRA */
  3739. return res;
  3740. }
  3741. /* Test functions for base64 encode/decode */
  3742. static int test_wolfSSL_EVP_ENCODE_CTX_new(void)
  3743. {
  3744. int res = TEST_SKIPPED;
  3745. #if defined(OPENSSL_EXTRA) && \
  3746. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  3747. EVP_ENCODE_CTX* ctx = NULL;
  3748. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3749. AssertIntEQ( ctx->remaining,0);
  3750. AssertIntEQ( ctx->data[0],0);
  3751. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3752. EVP_ENCODE_CTX_free(ctx);
  3753. res = TEST_RES_CHECK(1);
  3754. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
  3755. return res;
  3756. }
  3757. static int test_wolfSSL_EVP_ENCODE_CTX_free(void)
  3758. {
  3759. int res = TEST_SKIPPED;
  3760. #if defined(OPENSSL_EXTRA) && \
  3761. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  3762. EVP_ENCODE_CTX* ctx = NULL;
  3763. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3764. EVP_ENCODE_CTX_free(ctx);
  3765. res = TEST_RES_CHECK(1);
  3766. #endif /*OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
  3767. return res;
  3768. }
  3769. static int test_wolfSSL_EVP_EncodeInit(void)
  3770. {
  3771. int res = TEST_SKIPPED;
  3772. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3773. EVP_ENCODE_CTX* ctx = NULL;
  3774. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3775. AssertIntEQ( ctx->remaining,0);
  3776. AssertIntEQ( ctx->data[0],0);
  3777. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3778. /* make ctx dirty */
  3779. ctx->remaining = 10;
  3780. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  3781. EVP_EncodeInit(ctx);
  3782. AssertIntEQ( ctx->remaining,0);
  3783. AssertIntEQ( ctx->data[0],0);
  3784. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3785. EVP_ENCODE_CTX_free(ctx);
  3786. res = TEST_RES_CHECK(1);
  3787. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3788. return res;
  3789. }
  3790. static int test_wolfSSL_EVP_EncodeUpdate(void)
  3791. {
  3792. int res = TEST_SKIPPED;
  3793. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3794. int outl;
  3795. int total;
  3796. const unsigned char plain0[] = {"Th"};
  3797. const unsigned char plain1[] = {"This is a base64 encodeing test."};
  3798. const unsigned char plain2[] = {"This is additional data."};
  3799. const unsigned char enc0[] = {"VGg=\n"};
  3800. /* expected encoded result for the first output 64 chars plus trailing LF*/
  3801. const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
  3802. const unsigned char enc2[] =
  3803. {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
  3804. unsigned char encOutBuff[300];
  3805. EVP_ENCODE_CTX* ctx = NULL;
  3806. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3807. EVP_EncodeInit(ctx);
  3808. /* illegal parameter test */
  3809. AssertIntEQ(
  3810. EVP_EncodeUpdate(
  3811. NULL, /* pass NULL as ctx */
  3812. encOutBuff,
  3813. &outl,
  3814. plain1,
  3815. sizeof(plain1)-1),
  3816. 0 /* expected result code 0: fail */
  3817. );
  3818. AssertIntEQ(
  3819. EVP_EncodeUpdate(
  3820. ctx,
  3821. NULL, /* pass NULL as out buff */
  3822. &outl,
  3823. plain1,
  3824. sizeof(plain1)-1),
  3825. 0 /* expected result code 0: fail */
  3826. );
  3827. AssertIntEQ(
  3828. EVP_EncodeUpdate(
  3829. ctx,
  3830. encOutBuff,
  3831. NULL, /* pass NULL as outl */
  3832. plain1,
  3833. sizeof(plain1)-1),
  3834. 0 /* expected result code 0: fail */
  3835. );
  3836. AssertIntEQ(
  3837. EVP_EncodeUpdate(
  3838. ctx,
  3839. encOutBuff,
  3840. &outl,
  3841. NULL, /* pass NULL as in */
  3842. sizeof(plain1)-1),
  3843. 0 /* expected result code 0: fail */
  3844. );
  3845. AssertIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
  3846. /* meaningless parameter test */
  3847. AssertIntEQ(
  3848. EVP_EncodeUpdate(
  3849. ctx,
  3850. encOutBuff,
  3851. &outl,
  3852. plain1,
  3853. 0), /* pass zero input */
  3854. 1 /* expected result code 1: success */
  3855. );
  3856. /* very small data encoding test */
  3857. EVP_EncodeInit(ctx);
  3858. AssertIntEQ(
  3859. EVP_EncodeUpdate(
  3860. ctx,
  3861. encOutBuff,
  3862. &outl,
  3863. plain0,
  3864. sizeof(plain0)-1),
  3865. 1 /* expected result code 1: success */
  3866. );
  3867. AssertIntEQ(outl,0);
  3868. EVP_EncodeFinal(
  3869. ctx,
  3870. encOutBuff + outl,
  3871. &outl);
  3872. AssertIntEQ( outl, sizeof(enc0)-1);
  3873. AssertIntEQ(
  3874. XSTRNCMP(
  3875. (const char*)encOutBuff,
  3876. (const char*)enc0,sizeof(enc0) ),
  3877. 0);
  3878. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  3879. AssertIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
  3880. sizeof(enc0)-1);
  3881. AssertIntEQ(
  3882. XSTRNCMP(
  3883. (const char*)encOutBuff,
  3884. (const char*)enc0,sizeof(enc0) ),
  3885. 0);
  3886. /* pass small size( < 48bytes ) input, then make sure they are not
  3887. * encoded and just stored in ctx
  3888. */
  3889. EVP_EncodeInit(ctx);
  3890. total = 0;
  3891. outl = 0;
  3892. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  3893. AssertIntEQ(
  3894. EVP_EncodeUpdate(
  3895. ctx,
  3896. encOutBuff, /* buffer for output */
  3897. &outl, /* size of output */
  3898. plain1, /* input */
  3899. sizeof(plain1)-1), /* size of input */
  3900. 1); /* expected result code 1:success */
  3901. total += outl;
  3902. AssertIntEQ(outl, 0); /* no output expected */
  3903. AssertIntEQ(ctx->remaining, sizeof(plain1) -1);
  3904. AssertTrue(
  3905. XSTRNCMP((const char*)(ctx->data),
  3906. (const char*)plain1,
  3907. ctx->remaining) ==0 );
  3908. AssertTrue(encOutBuff[0] == 0);
  3909. /* call wolfSSL_EVP_EncodeUpdate again to make it encode
  3910. * the stored data and the new input together
  3911. */
  3912. AssertIntEQ(
  3913. EVP_EncodeUpdate(
  3914. ctx,
  3915. encOutBuff + outl, /* buffer for output */
  3916. &outl, /* size of output */
  3917. plain2, /* additional input */
  3918. sizeof(plain2) -1), /* size of additional input */
  3919. 1); /* expected result code 1:success */
  3920. total += outl;
  3921. AssertIntNE(outl, 0); /* some output is expected this time*/
  3922. AssertIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
  3923. AssertIntEQ(
  3924. XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
  3925. /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
  3926. EVP_EncodeFinal(
  3927. ctx,
  3928. encOutBuff + outl,
  3929. &outl);
  3930. total += outl;
  3931. AssertIntNE(total,0);
  3932. AssertIntNE(outl,0);
  3933. AssertIntEQ(XSTRNCMP(
  3934. (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
  3935. /* test with illeagal parameters */
  3936. outl = 1;
  3937. EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
  3938. AssertIntEQ(outl, 0);
  3939. outl = 1;
  3940. EVP_EncodeFinal(ctx, NULL, &outl);
  3941. AssertIntEQ(outl, 0);
  3942. EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
  3943. EVP_EncodeFinal(NULL, NULL, NULL);
  3944. EVP_ENCODE_CTX_free(ctx);
  3945. res = TEST_RES_CHECK(1);
  3946. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3947. return res;
  3948. }
  3949. static int test_wolfSSL_EVP_EncodeFinal(void)
  3950. {
  3951. int res = TEST_SKIPPED;
  3952. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3953. /* tests for wolfSSL_EVP_EncodeFinal are included in
  3954. * test_wolfSSL_EVP_EncodeUpdate
  3955. */
  3956. res = TEST_RES_CHECK(1);
  3957. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3958. return res;
  3959. }
  3960. static int test_wolfSSL_EVP_DecodeInit(void)
  3961. {
  3962. int res = TEST_SKIPPED;
  3963. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  3964. EVP_ENCODE_CTX* ctx = NULL;
  3965. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3966. AssertIntEQ( ctx->remaining,0);
  3967. AssertIntEQ( ctx->data[0],0);
  3968. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3969. /* make ctx dirty */
  3970. ctx->remaining = 10;
  3971. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  3972. EVP_DecodeInit(ctx);
  3973. AssertIntEQ( ctx->remaining,0);
  3974. AssertIntEQ( ctx->data[0],0);
  3975. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3976. EVP_ENCODE_CTX_free(ctx);
  3977. res = TEST_RES_CHECK(1);
  3978. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  3979. return res;
  3980. }
  3981. static int test_wolfSSL_EVP_DecodeUpdate(void)
  3982. {
  3983. int res = TEST_SKIPPED;
  3984. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  3985. int outl;
  3986. unsigned char decOutBuff[300];
  3987. EVP_ENCODE_CTX* ctx;
  3988. static const unsigned char enc1[] =
  3989. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  3990. /* const unsigned char plain1[] =
  3991. {"This is a base64 decoding test."} */
  3992. ctx = EVP_ENCODE_CTX_new();
  3993. EVP_DecodeInit(ctx);
  3994. /* illegal parameter tests */
  3995. /* pass NULL as ctx */
  3996. AssertIntEQ(
  3997. EVP_DecodeUpdate(
  3998. NULL, /* pass NULL as ctx */
  3999. decOutBuff,
  4000. &outl,
  4001. enc1,
  4002. sizeof(enc1)-1),
  4003. -1 /* expected result code -1: fail */
  4004. );
  4005. AssertIntEQ( outl, 0);
  4006. /* pass NULL as output */
  4007. AssertIntEQ(
  4008. EVP_DecodeUpdate(
  4009. ctx,
  4010. NULL, /* pass NULL as out buff */
  4011. &outl,
  4012. enc1,
  4013. sizeof(enc1)-1),
  4014. -1 /* expected result code -1: fail */
  4015. );
  4016. AssertIntEQ( outl, 0);
  4017. /* pass NULL as outl */
  4018. AssertIntEQ(
  4019. EVP_DecodeUpdate(
  4020. ctx,
  4021. decOutBuff,
  4022. NULL, /* pass NULL as outl */
  4023. enc1,
  4024. sizeof(enc1)-1),
  4025. -1 /* expected result code -1: fail */
  4026. );
  4027. /* pass NULL as input */
  4028. AssertIntEQ(
  4029. EVP_DecodeUpdate(
  4030. ctx,
  4031. decOutBuff,
  4032. &outl,
  4033. NULL, /* pass NULL as in */
  4034. sizeof(enc1)-1),
  4035. -1 /* expected result code -1: fail */
  4036. );
  4037. AssertIntEQ( outl, 0);
  4038. AssertIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
  4039. /* pass zero length input */
  4040. AssertIntEQ(
  4041. EVP_DecodeUpdate(
  4042. ctx,
  4043. decOutBuff,
  4044. &outl,
  4045. enc1,
  4046. 0), /* pass zero as input len */
  4047. 1 /* expected result code 1: success */
  4048. );
  4049. /* decode correct base64 string */
  4050. {
  4051. static const unsigned char enc2[] =
  4052. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  4053. static const unsigned char plain2[] =
  4054. {"This is a base64 decoding test."};
  4055. EVP_EncodeInit(ctx);
  4056. AssertIntEQ(
  4057. EVP_DecodeUpdate(
  4058. ctx,
  4059. decOutBuff,
  4060. &outl,
  4061. enc2,
  4062. sizeof(enc2)-1),
  4063. 0 /* expected result code 0: success */
  4064. );
  4065. AssertIntEQ(outl,sizeof(plain2) -1);
  4066. AssertIntEQ(
  4067. EVP_DecodeFinal(
  4068. ctx,
  4069. decOutBuff + outl,
  4070. &outl),
  4071. 1 /* expected result code 1: success */
  4072. );
  4073. AssertIntEQ(outl, 0); /* expected DecodeFinal outout no data */
  4074. AssertIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  4075. sizeof(plain2) -1 ),0);
  4076. AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
  4077. sizeof(plain2)-1);
  4078. AssertIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  4079. sizeof(plain2) -1 ),0);
  4080. }
  4081. /* decode correct base64 string which does not have '\n' in its last*/
  4082. {
  4083. static const unsigned char enc3[] =
  4084. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
  4085. static const unsigned char plain3[] =
  4086. {"This is a base64 decoding test."}; /* 31 chars */
  4087. EVP_EncodeInit(ctx);
  4088. AssertIntEQ(
  4089. EVP_DecodeUpdate(
  4090. ctx,
  4091. decOutBuff,
  4092. &outl,
  4093. enc3,
  4094. sizeof(enc3)-1),
  4095. 0 /* expected result code 0: success */
  4096. );
  4097. AssertIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
  4098. AssertIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  4099. sizeof(plain3) -1 ),0);
  4100. AssertIntEQ(
  4101. EVP_DecodeFinal(
  4102. ctx,
  4103. decOutBuff + outl,
  4104. &outl),
  4105. 1 /* expected result code 1: success */
  4106. );
  4107. AssertIntEQ(outl,0 );
  4108. AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
  4109. sizeof(plain3)-1);
  4110. AssertIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  4111. sizeof(plain3) -1 ),0);
  4112. }
  4113. /* decode string which has a padding char ('=') in the illegal position*/
  4114. {
  4115. static const unsigned char enc4[] =
  4116. {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
  4117. EVP_EncodeInit(ctx);
  4118. AssertIntEQ(
  4119. EVP_DecodeUpdate(
  4120. ctx,
  4121. decOutBuff,
  4122. &outl,
  4123. enc4,
  4124. sizeof(enc4)-1),
  4125. -1 /* expected result code -1: error */
  4126. );
  4127. AssertIntEQ(outl,0);
  4128. AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
  4129. }
  4130. /* small data decode test */
  4131. {
  4132. static const unsigned char enc00[] = {"VG"};
  4133. static const unsigned char enc01[] = {"g=\n"};
  4134. static const unsigned char plain4[] = {"Th"};
  4135. EVP_EncodeInit(ctx);
  4136. AssertIntEQ(
  4137. EVP_DecodeUpdate(
  4138. ctx,
  4139. decOutBuff,
  4140. &outl,
  4141. enc00,
  4142. sizeof(enc00)-1),
  4143. 1 /* expected result code 1: success */
  4144. );
  4145. AssertIntEQ(outl,0);
  4146. AssertIntEQ(
  4147. EVP_DecodeUpdate(
  4148. ctx,
  4149. decOutBuff + outl,
  4150. &outl,
  4151. enc01,
  4152. sizeof(enc01)-1),
  4153. 0 /* expected result code 0: success */
  4154. );
  4155. AssertIntEQ(outl,sizeof(plain4)-1);
  4156. /* test with illegal parameters */
  4157. AssertIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
  4158. AssertIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
  4159. AssertIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
  4160. AssertIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
  4161. EVP_DecodeFinal(
  4162. ctx,
  4163. decOutBuff + outl,
  4164. &outl);
  4165. AssertIntEQ( outl, 0);
  4166. AssertIntEQ(
  4167. XSTRNCMP(
  4168. (const char*)decOutBuff,
  4169. (const char*)plain4,sizeof(plain4)-1 ),
  4170. 0);
  4171. }
  4172. EVP_ENCODE_CTX_free(ctx);
  4173. res = TEST_RES_CHECK(1);
  4174. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4175. return res;
  4176. }
  4177. static int test_wolfSSL_EVP_DecodeFinal(void)
  4178. {
  4179. int res = TEST_SKIPPED;
  4180. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  4181. /* tests for wolfSSL_EVP_DecodeFinal are included in
  4182. * test_wolfSSL_EVP_DecodeUpdate
  4183. */
  4184. res = TEST_RES_CHECK(1);
  4185. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4186. return res;
  4187. }
  4188. /* Test function for wolfSSL_EVP_get_cipherbynid.
  4189. */
  4190. #ifdef OPENSSL_EXTRA
  4191. static int test_wolfSSL_EVP_get_cipherbynid(void)
  4192. {
  4193. #ifndef NO_AES
  4194. const WOLFSSL_EVP_CIPHER* c;
  4195. c = wolfSSL_EVP_get_cipherbynid(419);
  4196. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4197. defined(WOLFSSL_AES_128)
  4198. AssertNotNull(c);
  4199. AssertNotNull(strcmp("EVP_AES_128_CBC", c));
  4200. #else
  4201. AssertNull(c);
  4202. #endif
  4203. c = wolfSSL_EVP_get_cipherbynid(423);
  4204. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4205. defined(WOLFSSL_AES_192)
  4206. AssertNotNull(c);
  4207. AssertNotNull(strcmp("EVP_AES_192_CBC", c));
  4208. #else
  4209. AssertNull(c);
  4210. #endif
  4211. c = wolfSSL_EVP_get_cipherbynid(427);
  4212. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4213. defined(WOLFSSL_AES_256)
  4214. AssertNotNull(c);
  4215. AssertNotNull(strcmp("EVP_AES_256_CBC", c));
  4216. #else
  4217. AssertNull(c);
  4218. #endif
  4219. c = wolfSSL_EVP_get_cipherbynid(904);
  4220. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  4221. AssertNotNull(c);
  4222. AssertNotNull(strcmp("EVP_AES_128_CTR", c));
  4223. #else
  4224. AssertNull(c);
  4225. #endif
  4226. c = wolfSSL_EVP_get_cipherbynid(905);
  4227. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  4228. AssertNotNull(c);
  4229. AssertNotNull(strcmp("EVP_AES_192_CTR", c));
  4230. #else
  4231. AssertNull(c);
  4232. #endif
  4233. c = wolfSSL_EVP_get_cipherbynid(906);
  4234. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  4235. AssertNotNull(c);
  4236. AssertNotNull(strcmp("EVP_AES_256_CTR", c));
  4237. #else
  4238. AssertNull(c);
  4239. #endif
  4240. c = wolfSSL_EVP_get_cipherbynid(418);
  4241. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  4242. AssertNotNull(c);
  4243. AssertNotNull(strcmp("EVP_AES_128_ECB", c));
  4244. #else
  4245. AssertNull(c);
  4246. #endif
  4247. c = wolfSSL_EVP_get_cipherbynid(422);
  4248. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  4249. AssertNotNull(c);
  4250. AssertNotNull(strcmp("EVP_AES_192_ECB", c));
  4251. #else
  4252. AssertNull(c);
  4253. #endif
  4254. c = wolfSSL_EVP_get_cipherbynid(426);
  4255. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  4256. AssertNotNull(c);
  4257. AssertNotNull(strcmp("EVP_AES_256_ECB", c));
  4258. #else
  4259. AssertNull(c);
  4260. #endif
  4261. #endif /* !NO_AES */
  4262. #ifndef NO_DES3
  4263. AssertNotNull(strcmp("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  4264. #ifdef WOLFSSL_DES_ECB
  4265. AssertNotNull(strcmp("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  4266. #endif
  4267. AssertNotNull(strcmp("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  4268. #ifdef WOLFSSL_DES_ECB
  4269. AssertNotNull(strcmp("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  4270. #endif
  4271. #endif /* !NO_DES3 */
  4272. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  4273. AssertNotNull(strcmp("EVP_CHACHA20_POLY13O5", EVP_get_cipherbynid(1018)));
  4274. #endif
  4275. /* test for nid is out of range */
  4276. AssertNull(wolfSSL_EVP_get_cipherbynid(1));
  4277. return TEST_RES_CHECK(1);
  4278. }
  4279. static int test_wolfSSL_EVP_CIPHER_CTX(void)
  4280. {
  4281. int res = TEST_SKIPPED;
  4282. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  4283. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  4284. const EVP_CIPHER *init = EVP_aes_128_cbc();
  4285. const EVP_CIPHER *test;
  4286. byte key[AES_BLOCK_SIZE] = {0};
  4287. byte iv[AES_BLOCK_SIZE] = {0};
  4288. AssertNotNull(ctx);
  4289. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  4290. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  4291. test = EVP_CIPHER_CTX_cipher(ctx);
  4292. AssertTrue(init == test);
  4293. AssertIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  4294. AssertIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  4295. AssertIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  4296. EVP_CIPHER_CTX_free(ctx);
  4297. /* test EVP_CIPHER_CTX_cleanup with NULL */
  4298. AssertIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
  4299. res = TEST_RES_CHECK(1);
  4300. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  4301. return res;
  4302. }
  4303. #endif /* OPENSSL_EXTRA */
  4304. /*----------------------------------------------------------------------------*
  4305. | IO
  4306. *----------------------------------------------------------------------------*/
  4307. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  4308. !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
  4309. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  4310. #define HAVE_IO_TESTS_DEPENDENCIES
  4311. #endif
  4312. /* helper functions */
  4313. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  4314. #ifdef WOLFSSL_SESSION_EXPORT
  4315. #ifdef WOLFSSL_DTLS
  4316. /* set up function for sending session information */
  4317. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  4318. {
  4319. WOLFSSL_CTX* ctx = NULL;
  4320. WOLFSSL* ssl = NULL;
  4321. AssertNotNull(inSsl);
  4322. AssertNotNull(buf);
  4323. AssertIntNE(0, sz);
  4324. /* Set ctx to DTLS 1.2 */
  4325. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  4326. AssertNotNull(ctx);
  4327. ssl = wolfSSL_new(ctx);
  4328. AssertNotNull(ssl);
  4329. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  4330. wolfSSL_free(ssl);
  4331. wolfSSL_CTX_free(ctx);
  4332. (void)userCtx;
  4333. return 0;
  4334. }
  4335. #endif
  4336. /* returns negative value on fail and positive (including 0) on success */
  4337. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  4338. {
  4339. int ret, err, loop_count, count, timeout = 10;
  4340. char msg[] = "I hear you fa shizzle!";
  4341. char input[1024];
  4342. loop_count = ((func_args*)args)->argc;
  4343. #ifdef WOLFSSL_ASYNC_CRYPT
  4344. err = 0; /* Reset error */
  4345. #endif
  4346. do {
  4347. #ifdef WOLFSSL_ASYNC_CRYPT
  4348. if (err == WC_PENDING_E) {
  4349. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4350. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4351. }
  4352. #endif
  4353. ret = wolfSSL_accept(ssl);
  4354. err = wolfSSL_get_error(ssl, 0);
  4355. if (err == WOLFSSL_ERROR_WANT_READ ||
  4356. err == WOLFSSL_ERROR_WANT_WRITE) {
  4357. int select_ret;
  4358. err = WC_PENDING_E;
  4359. select_ret = tcp_select(*sockfd, timeout);
  4360. if (select_ret == TEST_TIMEOUT) {
  4361. return WOLFSSL_FATAL_ERROR;
  4362. }
  4363. }
  4364. } while (err == WC_PENDING_E);
  4365. if (ret != WOLFSSL_SUCCESS) {
  4366. char buff[WOLFSSL_MAX_ERROR_SZ];
  4367. fprintf(stderr, "error = %d, %s\n", err,
  4368. wolfSSL_ERR_error_string(err, buff));
  4369. return ret;
  4370. }
  4371. for (count = 0; count < loop_count; count++) {
  4372. int select_ret;
  4373. select_ret = tcp_select(*sockfd, timeout);
  4374. if (select_ret == TEST_TIMEOUT) {
  4375. ret = WOLFSSL_FATAL_ERROR;
  4376. break;
  4377. }
  4378. do {
  4379. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  4380. if (ret > 0) {
  4381. input[ret] = '\0';
  4382. fprintf(stderr, "Client message: %s\n", input);
  4383. }
  4384. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  4385. do {
  4386. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  4387. return WOLFSSL_FATAL_ERROR;
  4388. }
  4389. err = wolfSSL_get_error(ssl, ret);
  4390. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  4391. }
  4392. return ret;
  4393. }
  4394. #endif /* WOLFSSL_SESSION_EXPORT */
  4395. /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
  4396. #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  4397. defined(HAVE_AES_CBC)
  4398. typedef struct openssl_key_ctx {
  4399. byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
  4400. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  4401. byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
  4402. byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
  4403. } openssl_key_ctx;
  4404. static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
  4405. static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
  4406. static WC_INLINE int OpenSSLTicketInit(void)
  4407. {
  4408. int ret = wc_InitRng(&myOpenSSLKey_rng);
  4409. if (ret != 0) return ret;
  4410. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
  4411. sizeof(myOpenSSLKey_ctx.name));
  4412. if (ret != 0) return ret;
  4413. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
  4414. sizeof(myOpenSSLKey_ctx.key));
  4415. if (ret != 0) return ret;
  4416. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
  4417. sizeof(myOpenSSLKey_ctx.hmacKey));
  4418. if (ret != 0) return ret;
  4419. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
  4420. sizeof(myOpenSSLKey_ctx.iv));
  4421. if (ret != 0) return ret;
  4422. return 0;
  4423. }
  4424. static WC_INLINE int myTicketEncCbOpenSSL(WOLFSSL* ssl,
  4425. byte name[WOLFSSL_TICKET_NAME_SZ],
  4426. byte iv[WOLFSSL_TICKET_IV_SZ],
  4427. WOLFSSL_EVP_CIPHER_CTX *ectx,
  4428. WOLFSSL_HMAC_CTX *hctx, int enc) {
  4429. (void)ssl;
  4430. if (enc) {
  4431. XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
  4432. XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
  4433. }
  4434. else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
  4435. sizeof(myOpenSSLKey_ctx.name)) != 0 ||
  4436. XMEMCMP(iv, myOpenSSLKey_ctx.iv,
  4437. sizeof(myOpenSSLKey_ctx.iv)) != 0) {
  4438. return 0;
  4439. }
  4440. HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
  4441. if (enc)
  4442. EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  4443. else
  4444. EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  4445. return 1;
  4446. }
  4447. static WC_INLINE void OpenSSLTicketCleanup(void)
  4448. {
  4449. wc_FreeRng(&myOpenSSLKey_rng);
  4450. }
  4451. #endif
  4452. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4453. #ifdef WC_SHA512_DIGEST_SIZE
  4454. #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
  4455. #else
  4456. #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
  4457. #endif
  4458. byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
  4459. byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
  4460. byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
  4461. byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
  4462. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  4463. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  4464. {
  4465. SOCKET_T sockfd = 0;
  4466. SOCKET_T clientfd = 0;
  4467. word16 port;
  4468. callback_functions* cbf;
  4469. WOLFSSL_CTX* ctx = 0;
  4470. WOLFSSL* ssl = 0;
  4471. func_args* opts = (func_args*)args;
  4472. char msg[] = "I hear you fa shizzle!";
  4473. char input[1024];
  4474. int idx;
  4475. int ret, err = 0;
  4476. int sharedCtx = 0;
  4477. int doUdp = 0;
  4478. SOCKADDR_IN_T cliAddr;
  4479. socklen_t cliLen;
  4480. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4481. size_t msg_len = 0;
  4482. #endif
  4483. #ifdef WOLFSSL_TIRTOS
  4484. fdOpenSession(Task_self());
  4485. #endif
  4486. opts->return_code = TEST_FAIL;
  4487. cbf = opts->callbacks;
  4488. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4489. if (cbf != NULL && cbf->ctx) {
  4490. ctx = cbf->ctx;
  4491. sharedCtx = 1;
  4492. }
  4493. else
  4494. #endif
  4495. {
  4496. WOLFSSL_METHOD* method = NULL;
  4497. if (cbf != NULL && cbf->method != NULL) {
  4498. method = cbf->method();
  4499. }
  4500. else {
  4501. method = wolfSSLv23_server_method();
  4502. }
  4503. ctx = wolfSSL_CTX_new(method);
  4504. }
  4505. if (ctx == NULL) {
  4506. goto done;
  4507. }
  4508. if (cbf == NULL || !cbf->ticNoInit) {
  4509. #if defined(HAVE_SESSION_TICKET) && \
  4510. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  4511. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  4512. OpenSSLTicketInit();
  4513. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  4514. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  4515. TicketInit();
  4516. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  4517. #endif
  4518. #endif
  4519. }
  4520. #if defined(USE_WINDOWS_API)
  4521. port = opts->signal->port;
  4522. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  4523. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  4524. /* Let tcp_listen assign port */
  4525. port = 0;
  4526. #else
  4527. /* Use default port */
  4528. port = wolfSSLPort;
  4529. #endif
  4530. if (cbf != NULL)
  4531. doUdp = cbf->doUdp;
  4532. /* do it here to detect failure */
  4533. tcp_accept(
  4534. &sockfd, &clientfd, opts, port, 0, doUdp, 0, 0, 1, 0, 0);
  4535. if (doUdp) {
  4536. cliLen = sizeof(cliAddr);
  4537. idx = (int)recvfrom(sockfd, input, sizeof(input), MSG_PEEK,
  4538. (struct sockaddr*)&cliAddr, &cliLen);
  4539. AssertIntGT(idx, 0);
  4540. }
  4541. else {
  4542. CloseSocket(sockfd);
  4543. }
  4544. wolfSSL_CTX_set_verify(ctx,
  4545. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  4546. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4547. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4548. #endif
  4549. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  4550. != WOLFSSL_SUCCESS) {
  4551. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4552. goto done;
  4553. }
  4554. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4555. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4556. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4557. #else
  4558. if (wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4559. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4560. #endif
  4561. /*err_sys("can't load server cert chain file, "
  4562. "Please run from wolfSSL home dir");*/
  4563. goto done;
  4564. }
  4565. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4566. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4567. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4568. #else
  4569. if (wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4570. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4571. #endif
  4572. /*err_sys("can't load server key file, "
  4573. "Please run from wolfSSL home dir");*/
  4574. goto done;
  4575. }
  4576. /* call ctx setup callback */
  4577. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4578. cbf->ctx_ready(ctx);
  4579. }
  4580. ssl = wolfSSL_new(ctx);
  4581. if (ssl == NULL) {
  4582. goto done;
  4583. }
  4584. if (doUdp) {
  4585. err = wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  4586. if (err != WOLFSSL_SUCCESS)
  4587. goto done;
  4588. }
  4589. #ifdef WOLFSSL_SESSION_EXPORT
  4590. /* only add in more complex nonblocking case with session export tests */
  4591. if (args && opts->argc > 0) {
  4592. /* set as nonblock and time out for waiting on read/write */
  4593. tcp_set_nonblocking(&clientfd);
  4594. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  4595. }
  4596. #endif
  4597. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4598. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  4599. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4600. #else
  4601. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  4602. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4603. #endif
  4604. /*err_sys("can't load server cert chain file, "
  4605. "Please run from wolfSSL home dir");*/
  4606. goto done;
  4607. }
  4608. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4609. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  4610. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4611. #else
  4612. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  4613. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4614. #endif
  4615. /*err_sys("can't load server key file, "
  4616. "Please run from wolfSSL home dir");*/
  4617. goto done;
  4618. }
  4619. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  4620. /*err_sys("SSL_set_fd failed");*/
  4621. goto done;
  4622. }
  4623. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  4624. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  4625. #elif !defined(NO_DH)
  4626. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  4627. #endif
  4628. /* call ssl setup callback */
  4629. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4630. cbf->ssl_ready(ssl);
  4631. }
  4632. #ifdef WOLFSSL_SESSION_EXPORT
  4633. /* only add in more complex nonblocking case with session export tests */
  4634. if (opts->argc > 0) {
  4635. ret = nonblocking_accept_read(args, ssl, &clientfd);
  4636. if (ret >= 0) {
  4637. opts->return_code = TEST_SUCCESS;
  4638. }
  4639. #ifdef WOLFSSL_TIRTOS
  4640. Task_yield();
  4641. #endif
  4642. goto done;
  4643. }
  4644. #endif
  4645. #ifdef WOLFSSL_ASYNC_CRYPT
  4646. err = 0; /* Reset error */
  4647. #endif
  4648. do {
  4649. #ifdef WOLFSSL_ASYNC_CRYPT
  4650. if (err == WC_PENDING_E) {
  4651. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4652. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4653. }
  4654. #endif
  4655. ret = wolfSSL_accept(ssl);
  4656. err = wolfSSL_get_error(ssl, 0);
  4657. } while (err == WC_PENDING_E);
  4658. if (ret != WOLFSSL_SUCCESS) {
  4659. char buff[WOLFSSL_MAX_ERROR_SZ];
  4660. fprintf(stderr, "error = %d, %s\n", err,
  4661. wolfSSL_ERR_error_string(err, buff));
  4662. /*err_sys("SSL_accept failed");*/
  4663. goto done;
  4664. }
  4665. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4666. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  4667. msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
  4668. AssertIntGE(msg_len, 0);
  4669. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  4670. msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
  4671. AssertIntGE(msg_len, 0);
  4672. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  4673. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  4674. if (idx > 0) {
  4675. input[idx] = '\0';
  4676. fprintf(stderr, "Client message: %s\n", input);
  4677. }
  4678. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  4679. /*err_sys("SSL_write failed");*/
  4680. #ifdef WOLFSSL_TIRTOS
  4681. return;
  4682. #else
  4683. return 0;
  4684. #endif
  4685. }
  4686. if (cbf != NULL && cbf->on_result != NULL)
  4687. cbf->on_result(ssl);
  4688. #ifdef WOLFSSL_TIRTOS
  4689. Task_yield();
  4690. #endif
  4691. opts->return_code = TEST_SUCCESS;
  4692. done:
  4693. if (cbf != NULL)
  4694. cbf->last_err = err;
  4695. wolfSSL_shutdown(ssl);
  4696. wolfSSL_free(ssl);
  4697. if (!sharedCtx)
  4698. wolfSSL_CTX_free(ctx);
  4699. CloseSocket(clientfd);
  4700. #ifdef WOLFSSL_TIRTOS
  4701. fdCloseSession(Task_self());
  4702. #endif
  4703. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4704. && defined(HAVE_THREAD_LS)
  4705. wc_ecc_fp_free(); /* free per thread cache */
  4706. #endif
  4707. if (cbf == NULL || !cbf->ticNoInit) {
  4708. #if defined(HAVE_SESSION_TICKET) && \
  4709. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  4710. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  4711. OpenSSLTicketCleanup();
  4712. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  4713. TicketCleanup();
  4714. #endif
  4715. #endif
  4716. }
  4717. #ifndef WOLFSSL_TIRTOS
  4718. return 0;
  4719. #endif
  4720. }
  4721. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  4722. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  4723. {
  4724. SOCKET_T sockfd = 0;
  4725. SOCKET_T clientfd = 0;
  4726. word16 port;
  4727. callback_functions* cbf;
  4728. WOLFSSL_CTX* ctx = 0;
  4729. WOLFSSL* ssl = 0;
  4730. char msg[] = "I hear you fa shizzle!";
  4731. char input[1024];
  4732. int idx;
  4733. int ret, err = 0;
  4734. int sharedCtx = 0;
  4735. int loop_count = ((func_args*)args)->argc;
  4736. int count = 0;
  4737. #ifdef WOLFSSL_TIRTOS
  4738. fdOpenSession(Task_self());
  4739. #endif
  4740. ((func_args*)args)->return_code = TEST_FAIL;
  4741. cbf = ((func_args*)args)->callbacks;
  4742. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4743. if (cbf != NULL && cbf->ctx) {
  4744. ctx = cbf->ctx;
  4745. sharedCtx = 1;
  4746. }
  4747. else
  4748. #endif
  4749. {
  4750. WOLFSSL_METHOD* method = NULL;
  4751. if (cbf != NULL && cbf->method != NULL) {
  4752. method = cbf->method();
  4753. }
  4754. else {
  4755. method = wolfSSLv23_server_method();
  4756. }
  4757. ctx = wolfSSL_CTX_new(method);
  4758. }
  4759. #if defined(USE_WINDOWS_API)
  4760. port = ((func_args*)args)->signal->port;
  4761. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  4762. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  4763. /* Let tcp_listen assign port */
  4764. port = 0;
  4765. #else
  4766. /* Use default port */
  4767. port = wolfSSLPort;
  4768. #endif
  4769. wolfSSL_CTX_set_verify(ctx,
  4770. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  4771. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4772. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4773. #endif
  4774. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  4775. != WOLFSSL_SUCCESS) {
  4776. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4777. goto done;
  4778. }
  4779. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4780. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4781. /*err_sys("can't load server cert chain file, "
  4782. "Please run from wolfSSL home dir");*/
  4783. goto done;
  4784. }
  4785. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4786. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4787. /*err_sys("can't load server key file, "
  4788. "Please run from wolfSSL home dir");*/
  4789. goto done;
  4790. }
  4791. /* call ctx setup callback */
  4792. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4793. cbf->ctx_ready(ctx);
  4794. }
  4795. while (count != loop_count) {
  4796. ssl = wolfSSL_new(ctx);
  4797. if (ssl == NULL) {
  4798. goto done;
  4799. }
  4800. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  4801. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4802. /*err_sys("can't load server cert chain file, "
  4803. "Please run from wolfSSL home dir");*/
  4804. goto done;
  4805. }
  4806. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  4807. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4808. /*err_sys("can't load server key file, "
  4809. "Please run from wolfSSL home dir");*/
  4810. goto done;
  4811. }
  4812. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  4813. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  4814. #elif !defined(NO_DH)
  4815. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  4816. #endif
  4817. /* call ssl setup callback */
  4818. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4819. cbf->ssl_ready(ssl);
  4820. }
  4821. /* do it here to detect failure */
  4822. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  4823. CloseSocket(sockfd);
  4824. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  4825. /*err_sys("SSL_set_fd failed");*/
  4826. goto done;
  4827. }
  4828. #ifdef WOLFSSL_ASYNC_CRYPT
  4829. err = 0; /* Reset error */
  4830. #endif
  4831. do {
  4832. #ifdef WOLFSSL_ASYNC_CRYPT
  4833. if (err == WC_PENDING_E) {
  4834. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4835. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4836. }
  4837. #endif
  4838. ret = wolfSSL_accept(ssl);
  4839. err = wolfSSL_get_error(ssl, 0);
  4840. } while (err == WC_PENDING_E);
  4841. if (ret != WOLFSSL_SUCCESS) {
  4842. char buff[WOLFSSL_MAX_ERROR_SZ];
  4843. fprintf(stderr, "error = %d, %s\n", err,
  4844. wolfSSL_ERR_error_string(err, buff));
  4845. /*err_sys("SSL_accept failed");*/
  4846. goto done;
  4847. }
  4848. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  4849. if (idx > 0) {
  4850. input[idx] = '\0';
  4851. fprintf(stderr, "Client message: %s\n", input);
  4852. }
  4853. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  4854. /*err_sys("SSL_write failed");*/
  4855. #ifdef WOLFSSL_TIRTOS
  4856. return;
  4857. #else
  4858. return 0;
  4859. #endif
  4860. }
  4861. /* free ssl for this connection */
  4862. wolfSSL_shutdown(ssl);
  4863. wolfSSL_free(ssl); ssl = NULL;
  4864. CloseSocket(clientfd);
  4865. count++;
  4866. }
  4867. #ifdef WOLFSSL_TIRTOS
  4868. Task_yield();
  4869. #endif
  4870. ((func_args*)args)->return_code = TEST_SUCCESS;
  4871. done:
  4872. if (ssl != NULL) {
  4873. wolfSSL_shutdown(ssl);
  4874. wolfSSL_free(ssl);
  4875. }
  4876. if (!sharedCtx)
  4877. wolfSSL_CTX_free(ctx);
  4878. CloseSocket(clientfd);
  4879. #ifdef WOLFSSL_TIRTOS
  4880. fdCloseSession(Task_self());
  4881. #endif
  4882. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4883. && defined(HAVE_THREAD_LS)
  4884. wc_ecc_fp_free(); /* free per thread cache */
  4885. #endif
  4886. #ifndef WOLFSSL_TIRTOS
  4887. return 0;
  4888. #endif
  4889. }
  4890. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  4891. typedef int (*cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  4892. static int test_client_nofail(void* args, cbType cb)
  4893. {
  4894. #if !defined(NO_WOLFSSL_CLIENT)
  4895. SOCKET_T sockfd = 0;
  4896. callback_functions* cbf;
  4897. WOLFSSL_CTX* ctx = 0;
  4898. WOLFSSL* ssl = 0;
  4899. WOLFSSL_CIPHER* cipher;
  4900. char msg[64] = "hello wolfssl!";
  4901. char reply[1024];
  4902. int input;
  4903. int msgSz = (int)XSTRLEN(msg);
  4904. int ret, err = 0;
  4905. int cipherSuite;
  4906. int sharedCtx = 0;
  4907. int doUdp = 0;
  4908. const char* cipherName1, *cipherName2;
  4909. #ifdef WOLFSSL_TIRTOS
  4910. fdOpenSession(Task_self());
  4911. #endif
  4912. ((func_args*)args)->return_code = TEST_FAIL;
  4913. cbf = ((func_args*)args)->callbacks;
  4914. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4915. if (cbf != NULL && cbf->ctx) {
  4916. ctx = cbf->ctx;
  4917. sharedCtx = cbf->isSharedCtx;
  4918. }
  4919. else
  4920. #endif
  4921. {
  4922. WOLFSSL_METHOD* method = NULL;
  4923. if (cbf != NULL && cbf->method != NULL) {
  4924. method = cbf->method();
  4925. }
  4926. else {
  4927. method = wolfSSLv23_client_method();
  4928. }
  4929. ctx = wolfSSL_CTX_new(method);
  4930. }
  4931. if (cbf != NULL)
  4932. doUdp = cbf->doUdp;
  4933. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4934. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4935. #endif
  4936. /* Do connect here so server detects failures */
  4937. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  4938. doUdp, 0, NULL);
  4939. /* Connect the socket so that we don't have to set the peer later on */
  4940. if (doUdp)
  4941. udp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port);
  4942. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  4943. {
  4944. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4945. goto done;
  4946. }
  4947. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4948. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  4949. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4950. #else
  4951. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  4952. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4953. #endif
  4954. /*err_sys("can't load client cert file, "
  4955. "Please run from wolfSSL home dir");*/
  4956. goto done;
  4957. }
  4958. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4959. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  4960. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4961. #else
  4962. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  4963. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4964. #endif
  4965. /*err_sys("can't load client key file, "
  4966. "Please run from wolfSSL home dir");*/
  4967. goto done;
  4968. }
  4969. /* call ctx setup callback */
  4970. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4971. cbf->ctx_ready(ctx);
  4972. }
  4973. ssl = wolfSSL_new(ctx);
  4974. if (ssl == NULL) {
  4975. goto done;
  4976. }
  4977. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4978. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  4979. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4980. #else
  4981. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  4982. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4983. #endif
  4984. /*err_sys("can't load client cert file, "
  4985. "Please run from wolfSSL home dir");*/
  4986. goto done;
  4987. }
  4988. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4989. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  4990. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4991. #else
  4992. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  4993. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4994. #endif
  4995. /*err_sys("can't load client key file, "
  4996. "Please run from wolfSSL home dir");*/
  4997. goto done;
  4998. }
  4999. if (!doUdp) {
  5000. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5001. /*err_sys("SSL_set_fd failed");*/
  5002. goto done;
  5003. }
  5004. }
  5005. else {
  5006. #ifdef WOLFSSL_DTLS
  5007. if (wolfSSL_set_dtls_fd_connected(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5008. /*err_sys("SSL_set_fd failed");*/
  5009. goto done;
  5010. }
  5011. #else
  5012. goto done;
  5013. #endif
  5014. }
  5015. /* call ssl setup callback */
  5016. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5017. cbf->ssl_ready(ssl);
  5018. }
  5019. #ifdef WOLFSSL_ASYNC_CRYPT
  5020. err = 0; /* Reset error */
  5021. #endif
  5022. do {
  5023. #ifdef WOLFSSL_ASYNC_CRYPT
  5024. if (err == WC_PENDING_E) {
  5025. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5026. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5027. }
  5028. #endif
  5029. ret = wolfSSL_connect(ssl);
  5030. err = wolfSSL_get_error(ssl, 0);
  5031. } while (err == WC_PENDING_E);
  5032. if (ret != WOLFSSL_SUCCESS) {
  5033. char buff[WOLFSSL_MAX_ERROR_SZ];
  5034. fprintf(stderr, "error = %d, %s\n", err,
  5035. wolfSSL_ERR_error_string(err, buff));
  5036. /*err_sys("SSL_connect failed");*/
  5037. goto done;
  5038. }
  5039. /* test the various get cipher methods */
  5040. /* Internal cipher suite names */
  5041. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  5042. cipherName1 = wolfSSL_get_cipher_name(ssl);
  5043. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  5044. (cipherSuite >> 8), cipherSuite & 0xFF);
  5045. AssertStrEQ(cipherName1, cipherName2);
  5046. /* IANA Cipher Suites Names */
  5047. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  5048. then it's the internal cipher suite name */
  5049. cipher = wolfSSL_get_current_cipher(ssl);
  5050. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  5051. cipherName2 = wolfSSL_get_cipher(ssl);
  5052. AssertStrEQ(cipherName1, cipherName2);
  5053. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  5054. !defined(WOLFSSL_QT)
  5055. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  5056. (cipherSuite >> 8), cipherSuite & 0xFF);
  5057. AssertStrEQ(cipherName1, cipherName2);
  5058. #endif
  5059. if (cb != NULL)
  5060. (cb)(ctx, ssl);
  5061. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  5062. /*err_sys("SSL_write failed");*/
  5063. goto done;
  5064. }
  5065. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5066. if (input > 0) {
  5067. reply[input] = '\0';
  5068. fprintf(stderr, "Server response: %s\n", reply);
  5069. }
  5070. if (cbf != NULL && cbf->on_result != NULL)
  5071. cbf->on_result(ssl);
  5072. ((func_args*)args)->return_code = TEST_SUCCESS;
  5073. done:
  5074. if (cbf != NULL)
  5075. cbf->last_err = err;
  5076. wolfSSL_free(ssl);
  5077. if (!sharedCtx)
  5078. wolfSSL_CTX_free(ctx);
  5079. CloseSocket(sockfd);
  5080. #ifdef WOLFSSL_TIRTOS
  5081. fdCloseSession(Task_self());
  5082. #endif
  5083. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5084. && defined(HAVE_THREAD_LS)
  5085. wc_ecc_fp_free(); /* free per thread cache */
  5086. #endif
  5087. #else
  5088. (void)args;
  5089. (void)cb;
  5090. #endif /* !NO_WOLFSSL_CLIENT */
  5091. return 0;
  5092. }
  5093. void test_wolfSSL_client_server_nofail(callback_functions* client_cb,
  5094. callback_functions* server_cb)
  5095. {
  5096. func_args client_args;
  5097. func_args server_args;
  5098. tcp_ready ready;
  5099. THREAD_TYPE serverThread;
  5100. XMEMSET(&client_args, 0, sizeof(func_args));
  5101. XMEMSET(&server_args, 0, sizeof(func_args));
  5102. #ifdef WOLFSSL_TIRTOS
  5103. fdOpenSession(Task_self());
  5104. #endif
  5105. StartTCP();
  5106. InitTcpReady(&ready);
  5107. #if defined(USE_WINDOWS_API)
  5108. /* use RNG to get random port if using windows */
  5109. ready.port = GetRandomPort();
  5110. #endif
  5111. server_args.signal = &ready;
  5112. server_args.callbacks = server_cb;
  5113. client_args.signal = &ready;
  5114. client_args.callbacks = client_cb;
  5115. start_thread(test_server_nofail, &server_args, &serverThread);
  5116. wait_tcp_ready(&server_args);
  5117. test_client_nofail(&client_args, NULL);
  5118. join_thread(serverThread);
  5119. client_cb->return_code = client_args.return_code;
  5120. server_cb->return_code = server_args.return_code;
  5121. FreeTcpReady(&ready);
  5122. #ifdef WOLFSSL_TIRTOS
  5123. fdOpenSession(Task_self());
  5124. #endif
  5125. }
  5126. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  5127. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT)
  5128. static void test_client_reuse_WOLFSSLobj(void* args, void *cb, void* server_args)
  5129. {
  5130. SOCKET_T sockfd = 0;
  5131. callback_functions* cbf;
  5132. WOLFSSL_CTX* ctx = 0;
  5133. WOLFSSL* ssl = 0;
  5134. WOLFSSL_SESSION* session = NULL;
  5135. char msg[64] = "hello wolfssl!";
  5136. char reply[1024];
  5137. int input;
  5138. int msgSz = (int)XSTRLEN(msg);
  5139. int ret, err = 0;
  5140. int sharedCtx = 0;
  5141. #ifdef WOLFSSL_TIRTOS
  5142. fdOpenSession(Task_self());
  5143. #endif
  5144. ((func_args*)args)->return_code = TEST_FAIL;
  5145. cbf = ((func_args*)args)->callbacks;
  5146. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5147. if (cbf != NULL && cbf->ctx) {
  5148. ctx = cbf->ctx;
  5149. sharedCtx = 1;
  5150. }
  5151. else
  5152. #endif
  5153. {
  5154. WOLFSSL_METHOD* method = NULL;
  5155. if (cbf != NULL && cbf->method != NULL) {
  5156. method = cbf->method();
  5157. }
  5158. else {
  5159. method = wolfSSLv23_client_method();
  5160. }
  5161. ctx = wolfSSL_CTX_new(method);
  5162. }
  5163. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5164. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5165. #endif
  5166. /* Do connect here so server detects failures */
  5167. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5168. 0, 0, NULL);
  5169. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  5170. {
  5171. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5172. goto done;
  5173. }
  5174. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5175. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5176. /*err_sys("can't load client cert file, "
  5177. "Please run from wolfSSL home dir");*/
  5178. goto done;
  5179. }
  5180. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5181. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5182. /*err_sys("can't load client key file, "
  5183. "Please run from wolfSSL home dir");*/
  5184. goto done;
  5185. }
  5186. /* call ctx setup callback */
  5187. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5188. cbf->ctx_ready(ctx);
  5189. }
  5190. ssl = wolfSSL_new(ctx);
  5191. if (ssl == NULL) {
  5192. goto done;
  5193. }
  5194. /* keep handshakre resources for re-using WOLFSSL obj */
  5195. wolfSSL_KeepArrays(ssl);
  5196. if (wolfSSL_KeepHandshakeResources(ssl)) {
  5197. /* err_sys("SSL_KeepHandshakeResources failed"); */
  5198. goto done;
  5199. }
  5200. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  5201. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5202. /*err_sys("can't load client cert file, "
  5203. "Please run from wolfSSL home dir");*/
  5204. goto done;
  5205. }
  5206. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  5207. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5208. /*err_sys("can't load client key file, "
  5209. "Please run from wolfSSL home dir");*/
  5210. goto done;
  5211. }
  5212. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5213. /*err_sys("SSL_set_fd failed");*/
  5214. goto done;
  5215. }
  5216. /* call ssl setup callback */
  5217. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5218. cbf->ssl_ready(ssl);
  5219. }
  5220. #ifdef WOLFSSL_ASYNC_CRYPT
  5221. err = 0; /* Reset error */
  5222. #endif
  5223. do {
  5224. #ifdef WOLFSSL_ASYNC_CRYPT
  5225. if (err == WC_PENDING_E) {
  5226. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5227. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5228. }
  5229. #endif
  5230. ret = wolfSSL_connect(ssl);
  5231. err = wolfSSL_get_error(ssl, 0);
  5232. } while (err == WC_PENDING_E);
  5233. if (ret != WOLFSSL_SUCCESS) {
  5234. char buff[WOLFSSL_MAX_ERROR_SZ];
  5235. fprintf(stderr, "error = %d, %s\n", err,
  5236. wolfSSL_ERR_error_string(err, buff));
  5237. /*err_sys("SSL_connect failed");*/
  5238. goto done;
  5239. }
  5240. /* Build first session */
  5241. if (cb != NULL)
  5242. ((cbType)cb)(ctx, ssl);
  5243. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  5244. /*err_sys("SSL_write failed");*/
  5245. goto done;
  5246. }
  5247. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5248. if (input > 0) {
  5249. reply[input] = '\0';
  5250. fprintf(stderr, "Server response: %s\n", reply);
  5251. }
  5252. /* Session Resumption by re-using WOLFSSL object */
  5253. wolfSSL_set_quiet_shutdown(ssl, 1);
  5254. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  5255. /* err_sys ("SSL shutdown failed"); */
  5256. goto done;
  5257. }
  5258. session = wolfSSL_get1_session(ssl);
  5259. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  5260. /* err_sys ("SSL_clear failed"); */
  5261. goto done;
  5262. }
  5263. wolfSSL_set_session(ssl, session);
  5264. wolfSSL_SESSION_free(session);
  5265. session = NULL;
  5266. /* close socket once */
  5267. CloseSocket(sockfd);
  5268. sockfd = 0;
  5269. /* wait until server ready */
  5270. wait_tcp_ready((func_args*)server_args);
  5271. fprintf(stderr, "session resumption\n");
  5272. /* Do re-connect */
  5273. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5274. 0, 0, NULL);
  5275. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5276. /*err_sys("SSL_set_fd failed");*/
  5277. goto done;
  5278. }
  5279. #ifdef WOLFSSL_ASYNC_CRYPT
  5280. err = 0; /* Reset error */
  5281. #endif
  5282. do {
  5283. #ifdef WOLFSSL_ASYNC_CRYPT
  5284. if (err == WC_PENDING_E) {
  5285. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5286. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5287. }
  5288. #endif
  5289. ret = wolfSSL_connect(ssl);
  5290. err = wolfSSL_get_error(ssl, 0);
  5291. } while (err == WC_PENDING_E);
  5292. if (ret != WOLFSSL_SUCCESS) {
  5293. char buff[WOLFSSL_MAX_ERROR_SZ];
  5294. fprintf(stderr, "error = %d, %s\n", err,
  5295. wolfSSL_ERR_error_string(err, buff));
  5296. /*err_sys("SSL_connect failed");*/
  5297. goto done;
  5298. }
  5299. /* Build first session */
  5300. if (cb != NULL)
  5301. ((cbType)cb)(ctx, ssl);
  5302. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  5303. /*err_sys("SSL_write failed");*/
  5304. goto done;
  5305. }
  5306. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5307. if (input > 0) {
  5308. reply[input] = '\0';
  5309. fprintf(stderr, "Server response: %s\n", reply);
  5310. }
  5311. ((func_args*)args)->return_code = TEST_SUCCESS;
  5312. done:
  5313. wolfSSL_free(ssl);
  5314. if (!sharedCtx)
  5315. wolfSSL_CTX_free(ctx);
  5316. CloseSocket(sockfd);
  5317. #ifdef WOLFSSL_TIRTOS
  5318. fdCloseSession(Task_self());
  5319. #endif
  5320. return;
  5321. }
  5322. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  5323. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT) */
  5324. static int test_client_verifyDepth(void* args)
  5325. {
  5326. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT)
  5327. SOCKET_T sockfd = 0;
  5328. callback_functions* cbf;
  5329. WOLFSSL_CTX* ctx = 0;
  5330. WOLFSSL* ssl = 0;
  5331. char msg[64] = "hello wolfssl!";
  5332. char reply[1024];
  5333. int input;
  5334. int msgSz = (int)XSTRLEN(msg);
  5335. int ret, err = 0;
  5336. int verify_depth = ((func_args*)args)->argc;
  5337. ((func_args*)args)->return_code = TEST_FAIL;
  5338. cbf = ((func_args*)args)->callbacks;
  5339. {
  5340. WOLFSSL_METHOD* method = NULL;
  5341. if (cbf != NULL && cbf->method != NULL) {
  5342. method = cbf->method();
  5343. }
  5344. else {
  5345. method = wolfSSLv23_client_method();
  5346. }
  5347. ctx = wolfSSL_CTX_new(method);
  5348. }
  5349. /* Do connect here so server detects failures */
  5350. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5351. 0, 0, NULL);
  5352. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0)
  5353. != WOLFSSL_SUCCESS)
  5354. {
  5355. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5356. goto done;
  5357. }
  5358. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5359. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5360. /*err_sys("can't load client cert file, "
  5361. "Please run from wolfSSL home dir");*/
  5362. goto done;
  5363. }
  5364. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5365. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5366. /*err_sys("can't load client key file, "
  5367. "Please run from wolfSSL home dir");*/
  5368. goto done;
  5369. }
  5370. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  5371. /* set verify depth */
  5372. if (verify_depth == 0) {
  5373. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  5374. SSL_CTX_set_verify_depth(ctx, verify_depth);
  5375. }
  5376. else if (verify_depth == -1) {
  5377. myVerifyAction = VERIFY_USE_PREVERFIY;
  5378. SSL_CTX_set_verify_depth(ctx, 0);
  5379. }
  5380. else if (verify_depth > 0) {
  5381. myVerifyAction = VERIFY_USE_PREVERFIY;
  5382. SSL_CTX_set_verify_depth(ctx, verify_depth);
  5383. }
  5384. ssl = wolfSSL_new(ctx);
  5385. if (ssl == NULL) {
  5386. goto done;
  5387. }
  5388. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5389. /*err_sys("SSL_set_fd failed");*/
  5390. goto done;
  5391. }
  5392. #ifdef WOLFSSL_ASYNC_CRYPT
  5393. err = 0; /* Reset error */
  5394. #endif
  5395. do {
  5396. #ifdef WOLFSSL_ASYNC_CRYPT
  5397. if (err == WC_PENDING_E) {
  5398. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5399. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5400. }
  5401. #endif
  5402. ret = wolfSSL_connect(ssl);
  5403. err = wolfSSL_get_error(ssl, 0);
  5404. } while (err == WC_PENDING_E);
  5405. if (ret != WOLFSSL_SUCCESS) {
  5406. char buff[WOLFSSL_MAX_ERROR_SZ];
  5407. fprintf(stderr, "error = %d, %s\n", err,
  5408. wolfSSL_ERR_error_string(err, buff));
  5409. goto done;
  5410. }
  5411. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  5412. goto done;
  5413. }
  5414. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5415. if (input > 0) {
  5416. reply[input] = '\0';
  5417. fprintf(stderr, "Server response: %s\n", reply);
  5418. }
  5419. ((func_args*)args)->return_code = TEST_SUCCESS;
  5420. done:
  5421. wolfSSL_free(ssl);
  5422. wolfSSL_CTX_free(ctx);
  5423. CloseSocket(sockfd);
  5424. #else
  5425. (void)args;
  5426. #endif /* defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT) */
  5427. return 0;
  5428. }
  5429. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  5430. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
  5431. defined(HAVE_ALPN) && defined(HAVE_SNI) && \
  5432. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
  5433. #define HAVE_ALPN_PROTOS_SUPPORT
  5434. #endif
  5435. /* Generic TLS client / server with callbacks for API unit tests
  5436. * Used by SNI / ALPN / crypto callback helper functions */
  5437. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  5438. (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
  5439. defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
  5440. #define ENABLE_TLS_CALLBACK_TEST
  5441. #endif
  5442. #if defined(ENABLE_TLS_CALLBACK_TEST) || \
  5443. (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
  5444. /* TLS server for API unit testing - generic */
  5445. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  5446. {
  5447. callback_functions* callbacks = ((func_args*)args)->callbacks;
  5448. WOLFSSL_CTX* ctx = NULL;
  5449. WOLFSSL* ssl = NULL;
  5450. SOCKET_T sfd = 0;
  5451. SOCKET_T cfd = 0;
  5452. word16 port;
  5453. char msg[] = "I hear you fa shizzle!";
  5454. int len = (int) XSTRLEN(msg);
  5455. char input[1024];
  5456. int idx;
  5457. int ret, err = 0;
  5458. ((func_args*)args)->return_code = TEST_FAIL;
  5459. #ifdef WOLFSSL_STATIC_MEMORY
  5460. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  5461. callbacks->memSz > 0) {
  5462. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  5463. callbacks->mem, callbacks->memSz, 0, 1);
  5464. if (ret != WOLFSSL_SUCCESS) {
  5465. fprintf(stderr, "CTX static new failed %d\n", ret);
  5466. return 0;
  5467. }
  5468. }
  5469. #else
  5470. if (ctx == NULL) {
  5471. ctx = wolfSSL_CTX_new(callbacks->method());
  5472. }
  5473. if (ctx == NULL) {
  5474. fprintf(stderr, "CTX new failed\n");
  5475. return 0;
  5476. }
  5477. #endif
  5478. /* set defaults */
  5479. if (callbacks->caPemFile == NULL)
  5480. callbacks->caPemFile = cliCertFile;
  5481. if (callbacks->certPemFile == NULL)
  5482. callbacks->certPemFile = svrCertFile;
  5483. if (callbacks->keyPemFile == NULL)
  5484. callbacks->keyPemFile = svrKeyFile;
  5485. #ifdef WOLFSSL_TIRTOS
  5486. fdOpenSession(Task_self());
  5487. #endif
  5488. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  5489. #if defined(USE_WINDOWS_API)
  5490. port = ((func_args*)args)->signal->port;
  5491. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  5492. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  5493. /* Let tcp_listen assign port */
  5494. port = 0;
  5495. #else
  5496. /* Use default port */
  5497. port = wolfSSLPort;
  5498. #endif
  5499. wolfSSL_CTX_set_verify(ctx,
  5500. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5501. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5502. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5503. #endif
  5504. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  5505. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_dtls_set_export(ctx, test_export));
  5506. #endif
  5507. AssertIntEQ(WOLFSSL_SUCCESS,
  5508. wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0));
  5509. AssertIntEQ(WOLFSSL_SUCCESS,
  5510. wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  5511. WOLFSSL_FILETYPE_PEM));
  5512. AssertIntEQ(WOLFSSL_SUCCESS,
  5513. wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  5514. WOLFSSL_FILETYPE_PEM));
  5515. if (callbacks->ctx_ready)
  5516. callbacks->ctx_ready(ctx);
  5517. ssl = wolfSSL_new(ctx);
  5518. if (ssl == NULL) {
  5519. fprintf(stderr, "SSL new failed\n");
  5520. wolfSSL_CTX_free(ctx);
  5521. return 0;
  5522. }
  5523. if (wolfSSL_dtls(ssl)) {
  5524. SOCKADDR_IN_T cliAddr;
  5525. socklen_t cliLen;
  5526. cliLen = sizeof(cliAddr);
  5527. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  5528. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  5529. (struct sockaddr*)&cliAddr, &cliLen);
  5530. AssertIntGT(idx, 0);
  5531. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  5532. }
  5533. else {
  5534. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  5535. CloseSocket(sfd);
  5536. }
  5537. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  5538. if (callbacks->loadToSSL) {
  5539. wolfSSL_SetDevId(ssl, callbacks->devId);
  5540. AssertIntEQ(WOLFSSL_SUCCESS,
  5541. wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  5542. WOLFSSL_FILETYPE_PEM));
  5543. AssertIntEQ(WOLFSSL_SUCCESS,
  5544. wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  5545. WOLFSSL_FILETYPE_PEM));
  5546. }
  5547. #ifdef NO_PSK
  5548. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5549. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5550. #elif !defined(NO_DH)
  5551. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  5552. #endif
  5553. #endif
  5554. if (callbacks->ssl_ready)
  5555. callbacks->ssl_ready(ssl);
  5556. #ifdef WOLFSSL_ASYNC_CRYPT
  5557. err = 0; /* Reset error */
  5558. #endif
  5559. do {
  5560. #ifdef WOLFSSL_ASYNC_CRYPT
  5561. if (err == WC_PENDING_E) {
  5562. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5563. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5564. }
  5565. #endif
  5566. ret = wolfSSL_accept(ssl);
  5567. err = wolfSSL_get_error(ssl, ret);
  5568. } while (err == WC_PENDING_E);
  5569. if (ret != WOLFSSL_SUCCESS) {
  5570. char buff[WOLFSSL_MAX_ERROR_SZ];
  5571. fprintf(stderr, "accept error = %d, %s\n", err,
  5572. wolfSSL_ERR_error_string(err, buff));
  5573. /*err_sys("SSL_accept failed");*/
  5574. }
  5575. else {
  5576. #ifdef WOLFSSL_ASYNC_CRYPT
  5577. err = 0; /* Reset error */
  5578. #endif
  5579. do {
  5580. #ifdef WOLFSSL_ASYNC_CRYPT
  5581. if (err == WC_PENDING_E) {
  5582. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5583. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5584. }
  5585. #endif
  5586. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  5587. err = wolfSSL_get_error(ssl, idx);
  5588. } while (err == WC_PENDING_E);
  5589. if (idx > 0) {
  5590. input[idx] = 0;
  5591. fprintf(stderr, "Client message: %s\n", input);
  5592. }
  5593. #ifdef WOLFSSL_ASYNC_CRYPT
  5594. err = 0; /* Reset error */
  5595. #endif
  5596. do {
  5597. #ifdef WOLFSSL_ASYNC_CRYPT
  5598. if (err == WC_PENDING_E) {
  5599. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5600. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5601. }
  5602. #endif
  5603. ret = wolfSSL_write(ssl, msg, len);
  5604. err = wolfSSL_get_error(ssl, ret);
  5605. } while (err == WC_PENDING_E);
  5606. AssertIntEQ(len, ret);
  5607. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
  5608. defined(WOLFSSL_DTLS)
  5609. if (wolfSSL_dtls(ssl)) {
  5610. byte* import;
  5611. word32 sz;
  5612. wolfSSL_dtls_export(ssl, NULL, &sz);
  5613. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  5614. AssertNotNull(import);
  5615. idx = wolfSSL_dtls_export(ssl, import, &sz);
  5616. AssertIntGE(idx, 0);
  5617. AssertIntGE(wolfSSL_dtls_import(ssl, import, idx), 0);
  5618. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  5619. }
  5620. #endif
  5621. #ifdef WOLFSSL_TIRTOS
  5622. Task_yield();
  5623. #endif
  5624. ((func_args*)args)->return_code = TEST_SUCCESS;
  5625. }
  5626. if (callbacks->on_result)
  5627. callbacks->on_result(ssl);
  5628. wolfSSL_shutdown(ssl);
  5629. wolfSSL_free(ssl);
  5630. wolfSSL_CTX_free(ctx);
  5631. CloseSocket(cfd);
  5632. #ifdef WOLFSSL_TIRTOS
  5633. fdCloseSession(Task_self());
  5634. #endif
  5635. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5636. && defined(HAVE_THREAD_LS)
  5637. wc_ecc_fp_free(); /* free per thread cache */
  5638. #endif
  5639. #ifndef WOLFSSL_TIRTOS
  5640. return 0;
  5641. #endif
  5642. }
  5643. /* TLS Client for API unit testing - generic */
  5644. static void run_wolfssl_client(void* args)
  5645. {
  5646. callback_functions* callbacks = ((func_args*)args)->callbacks;
  5647. WOLFSSL_CTX* ctx = NULL;
  5648. WOLFSSL* ssl = NULL;
  5649. SOCKET_T sfd = 0;
  5650. char msg[] = "hello wolfssl server!";
  5651. int len = (int) XSTRLEN(msg);
  5652. char input[1024];
  5653. int ret, err = 0;
  5654. ((func_args*)args)->return_code = TEST_FAIL;
  5655. /* set defaults */
  5656. if (callbacks->caPemFile == NULL)
  5657. callbacks->caPemFile = caCertFile;
  5658. if (callbacks->certPemFile == NULL)
  5659. callbacks->certPemFile = cliCertFile;
  5660. if (callbacks->keyPemFile == NULL)
  5661. callbacks->keyPemFile = cliKeyFile;
  5662. #ifdef WOLFSSL_STATIC_MEMORY
  5663. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  5664. callbacks->memSz > 0) {
  5665. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  5666. callbacks->mem, callbacks->memSz, 0, 1);
  5667. if (ret != WOLFSSL_SUCCESS) {
  5668. fprintf(stderr, "CTX static new failed %d\n", ret);
  5669. return;
  5670. }
  5671. }
  5672. #else
  5673. if (ctx == NULL) {
  5674. ctx = wolfSSL_CTX_new(callbacks->method());
  5675. }
  5676. if (ctx == NULL) {
  5677. fprintf(stderr, "CTX new failed\n");
  5678. return;
  5679. }
  5680. #endif
  5681. #ifdef WOLFSSL_TIRTOS
  5682. fdOpenSession(Task_self());
  5683. #endif
  5684. if (!callbacks->loadToSSL) {
  5685. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  5686. }
  5687. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5688. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5689. #endif
  5690. AssertIntEQ(WOLFSSL_SUCCESS,
  5691. wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0));
  5692. if (!callbacks->loadToSSL) {
  5693. AssertIntEQ(WOLFSSL_SUCCESS,
  5694. wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  5695. WOLFSSL_FILETYPE_PEM));
  5696. AssertIntEQ(WOLFSSL_SUCCESS,
  5697. wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  5698. WOLFSSL_FILETYPE_PEM));
  5699. }
  5700. if (callbacks->ctx_ready)
  5701. callbacks->ctx_ready(ctx);
  5702. ssl = wolfSSL_new(ctx);
  5703. if (wolfSSL_dtls(ssl)) {
  5704. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5705. 1, 0, ssl);
  5706. }
  5707. else {
  5708. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5709. 0, 0, ssl);
  5710. }
  5711. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  5712. if (callbacks->loadToSSL) {
  5713. wolfSSL_SetDevId(ssl, callbacks->devId);
  5714. AssertIntEQ(WOLFSSL_SUCCESS,
  5715. wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  5716. WOLFSSL_FILETYPE_PEM));
  5717. AssertIntEQ(WOLFSSL_SUCCESS,
  5718. wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  5719. WOLFSSL_FILETYPE_PEM));
  5720. }
  5721. if (callbacks->ssl_ready)
  5722. callbacks->ssl_ready(ssl);
  5723. #ifdef WOLFSSL_ASYNC_CRYPT
  5724. err = 0; /* Reset error */
  5725. #endif
  5726. do {
  5727. #ifdef WOLFSSL_ASYNC_CRYPT
  5728. if (err == WC_PENDING_E) {
  5729. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5730. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5731. }
  5732. #endif
  5733. ret = wolfSSL_connect(ssl);
  5734. err = wolfSSL_get_error(ssl, ret);
  5735. } while (err == WC_PENDING_E);
  5736. if (ret != WOLFSSL_SUCCESS) {
  5737. char buff[WOLFSSL_MAX_ERROR_SZ];
  5738. fprintf(stderr, "error = %d, %s\n", err,
  5739. wolfSSL_ERR_error_string(err, buff));
  5740. /*err_sys("SSL_connect failed");*/
  5741. }
  5742. else {
  5743. #ifdef WOLFSSL_ASYNC_CRYPT
  5744. err = 0; /* Reset error */
  5745. #endif
  5746. do {
  5747. #ifdef WOLFSSL_ASYNC_CRYPT
  5748. if (err == WC_PENDING_E) {
  5749. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5750. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5751. }
  5752. #endif
  5753. ret = wolfSSL_write(ssl, msg, len);
  5754. err = wolfSSL_get_error(ssl, ret);
  5755. } while (err == WC_PENDING_E);
  5756. AssertIntEQ(len, ret);
  5757. #ifdef WOLFSSL_ASYNC_CRYPT
  5758. err = 0; /* Reset error */
  5759. #endif
  5760. do {
  5761. #ifdef WOLFSSL_ASYNC_CRYPT
  5762. if (err == WC_PENDING_E) {
  5763. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5764. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5765. }
  5766. #endif
  5767. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  5768. err = wolfSSL_get_error(ssl, ret);
  5769. } while (err == WC_PENDING_E);
  5770. if (ret > 0) {
  5771. input[ret] = '\0'; /* null term */
  5772. fprintf(stderr, "Server response: %s\n", input);
  5773. }
  5774. ((func_args*)args)->return_code = TEST_SUCCESS;
  5775. }
  5776. if (callbacks->on_result)
  5777. callbacks->on_result(ssl);
  5778. wolfSSL_free(ssl);
  5779. wolfSSL_CTX_free(ctx);
  5780. CloseSocket(sfd);
  5781. #ifdef WOLFSSL_TIRTOS
  5782. fdCloseSession(Task_self());
  5783. #endif
  5784. }
  5785. #endif /* ENABLE_TLS_CALLBACK_TEST */
  5786. static int test_wolfSSL_read_write(void)
  5787. {
  5788. /* The unit testing for read and write shall happen simultaneously, since
  5789. * one can't do anything with one without the other. (Except for a failure
  5790. * test case.) This function will call all the others that will set up,
  5791. * execute, and report their test findings.
  5792. *
  5793. * Set up the success case first. This function will become the template
  5794. * for the other tests. This should eventually be renamed
  5795. *
  5796. * The success case isn't interesting, how can this fail?
  5797. * - Do not give the client context a CA certificate. The connect should
  5798. * fail. Do not need server for this?
  5799. * - Using NULL for the ssl object on server. Do not need client for this.
  5800. * - Using NULL for the ssl object on client. Do not need server for this.
  5801. * - Good ssl objects for client and server. Client write() without server
  5802. * read().
  5803. * - Good ssl objects for client and server. Server write() without client
  5804. * read().
  5805. * - Forgetting the password callback?
  5806. */
  5807. tcp_ready ready;
  5808. func_args client_args;
  5809. func_args server_args;
  5810. THREAD_TYPE serverThread;
  5811. XMEMSET(&client_args, 0, sizeof(func_args));
  5812. XMEMSET(&server_args, 0, sizeof(func_args));
  5813. #ifdef WOLFSSL_TIRTOS
  5814. fdOpenSession(Task_self());
  5815. #endif
  5816. StartTCP();
  5817. InitTcpReady(&ready);
  5818. #if defined(USE_WINDOWS_API)
  5819. /* use RNG to get random port if using windows */
  5820. ready.port = GetRandomPort();
  5821. #endif
  5822. server_args.signal = &ready;
  5823. client_args.signal = &ready;
  5824. start_thread(test_server_nofail, &server_args, &serverThread);
  5825. wait_tcp_ready(&server_args);
  5826. test_client_nofail(&client_args, NULL);
  5827. join_thread(serverThread);
  5828. AssertTrue(client_args.return_code);
  5829. AssertTrue(server_args.return_code);
  5830. FreeTcpReady(&ready);
  5831. #ifdef WOLFSSL_TIRTOS
  5832. fdOpenSession(Task_self());
  5833. #endif
  5834. return TEST_RES_CHECK(1);
  5835. }
  5836. static int test_wolfSSL_reuse_WOLFSSLobj(void)
  5837. {
  5838. int res = TEST_SKIPPED;
  5839. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  5840. !defined(WOLFSSL_TLS13)
  5841. /* The unit test for session resumption by re-using WOLFSSL object.
  5842. * WOLFSSL object is not cleared after first session. It re-use the obeject
  5843. * for second connection.
  5844. */
  5845. tcp_ready ready;
  5846. func_args client_args;
  5847. func_args server_args;
  5848. THREAD_TYPE serverThread;
  5849. XMEMSET(&client_args, 0, sizeof(func_args));
  5850. XMEMSET(&server_args, 0, sizeof(func_args));
  5851. #ifdef WOLFSSL_TIRTOS
  5852. fdOpenSession(Task_self());
  5853. #endif
  5854. StartTCP();
  5855. InitTcpReady(&ready);
  5856. #if defined(USE_WINDOWS_API)
  5857. /* use RNG to get random port if using windows */
  5858. ready.port = GetRandomPort();
  5859. #endif
  5860. server_args.signal = &ready;
  5861. client_args.signal = &ready;
  5862. /* the var is used for loop number */
  5863. server_args.argc = 2;
  5864. start_thread(test_server_loop, &server_args, &serverThread);
  5865. wait_tcp_ready(&server_args);
  5866. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  5867. join_thread(serverThread);
  5868. AssertTrue(client_args.return_code);
  5869. AssertTrue(server_args.return_code);
  5870. FreeTcpReady(&ready);
  5871. #ifdef WOLFSSL_TIRTOS
  5872. fdOpenSession(Task_self());
  5873. #endif
  5874. res = TEST_RES_CHECK(1);
  5875. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  5876. return res;
  5877. }
  5878. static int test_wolfSSL_CTX_verifyDepth_ServerClient(void)
  5879. {
  5880. int res = TEST_SKIPPED;
  5881. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT)
  5882. /* This unit test is to check set verify Depth */
  5883. tcp_ready ready;
  5884. func_args client_args;
  5885. func_args server_args;
  5886. THREAD_TYPE serverThread;
  5887. callback_functions client_cbf;
  5888. XMEMSET(&client_args, 0, sizeof(func_args));
  5889. XMEMSET(&server_args, 0, sizeof(func_args));
  5890. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  5891. #ifdef WOLFSSL_TLS13
  5892. client_cbf.method = wolfTLSv1_3_client_method;
  5893. #endif /* WOLFSSL_TLS13 */
  5894. client_args.callbacks = &client_cbf;
  5895. StartTCP();
  5896. InitTcpReady(&ready);
  5897. #if defined(USE_WINDOWS_API)
  5898. /* use RNG to get random port if using windows */
  5899. ready.port = GetRandomPort();
  5900. #endif
  5901. server_args.signal = &ready;
  5902. client_args.signal = &ready;
  5903. /* the var is used for loop number */
  5904. server_args.argc = 1;
  5905. /* test case 1 verify depth is equal to peer chain */
  5906. {
  5907. start_thread(test_server_nofail, &server_args, &serverThread);
  5908. wait_tcp_ready(&server_args);
  5909. /* the var is used for verify depth */
  5910. client_args.argc = 2;
  5911. test_client_verifyDepth(&client_args);
  5912. join_thread(serverThread);
  5913. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5914. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5915. }
  5916. /* test case 2
  5917. * verify depth is zero, number of peer's chain is 2.
  5918. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  5919. * callback.
  5920. */
  5921. /* the var is used for verify depth 0 and VERIFY_OVERRIDE_ERROR */
  5922. {
  5923. start_thread(test_server_nofail, &server_args, &serverThread);
  5924. wait_tcp_ready(&server_args);
  5925. client_args.argc = 0;
  5926. test_client_verifyDepth(&client_args);
  5927. join_thread(serverThread);
  5928. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5929. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5930. }
  5931. /* test case 3
  5932. * verify depth is zero, number of peer's chain is 2
  5933. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  5934. * therefore, handshake becomes failure.
  5935. */
  5936. /* the var is used for verify depth 0 and VERIFY_USE_PREVERFIY */
  5937. {
  5938. start_thread(test_server_nofail, &server_args, &serverThread);
  5939. wait_tcp_ready(&server_args);
  5940. client_args.argc = -1;
  5941. test_client_verifyDepth(&client_args);
  5942. join_thread(serverThread);
  5943. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5944. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5945. }
  5946. FreeTcpReady(&ready);
  5947. res = TEST_RES_CHECK(1);
  5948. #else
  5949. (void)test_client_verifyDepth;
  5950. #endif /* (OPENSSL_EXTRA) && !(WOLFSSL_TIRTOS) && (NO_WOLFSSL_CLIENT) */
  5951. return res;
  5952. }
  5953. static int test_wolfSSL_CTX_set_cipher_list(void)
  5954. {
  5955. int res = TEST_SKIPPED;
  5956. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  5957. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  5958. && !defined(NO_SHA256)
  5959. WOLFSSL_CTX* ctx;
  5960. WOLFSSL_CTX* ctxClient;
  5961. tcp_ready ready;
  5962. func_args client_args;
  5963. func_args server_args;
  5964. callback_functions client_cb;
  5965. callback_functions server_cb;
  5966. THREAD_TYPE serverThread;
  5967. XMEMSET(&client_args, 0, sizeof(func_args));
  5968. XMEMSET(&server_args, 0, sizeof(func_args));
  5969. StartTCP();
  5970. InitTcpReady(&ready);
  5971. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  5972. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  5973. AssertNotNull((ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method())));
  5974. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, "DEFAULT:!NULL"));
  5975. AssertIntEQ(WOLFSSL_SUCCESS,
  5976. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  5977. AssertIntEQ(WOLFSSL_SUCCESS,
  5978. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  5979. AssertIntEQ(WOLFSSL_SUCCESS,
  5980. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  5981. AssertNotNull((ctxClient = wolfSSL_CTX_new(wolfTLSv1_2_client_method())));
  5982. AssertTrue(wolfSSL_CTX_set_cipher_list(ctxClient, "ECDHE-RSA-AES128-SHA256"));
  5983. client_cb.ctx = ctxClient;
  5984. server_cb.ctx = ctx;
  5985. /* we are responsible for free'ing WOLFSSL_CTX */
  5986. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  5987. server_args.signal = &ready;
  5988. server_args.callbacks = &server_cb;
  5989. client_args.signal = &ready;
  5990. client_args.callbacks = &client_cb;
  5991. client_args.return_code = TEST_FAIL;
  5992. start_thread(test_server_nofail, &server_args, &serverThread);
  5993. wait_tcp_ready(&server_args);
  5994. test_client_nofail(&client_args, NULL);
  5995. join_thread(serverThread);
  5996. wolfSSL_CTX_free(client_cb.ctx);
  5997. wolfSSL_CTX_free(server_cb.ctx);
  5998. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5999. FreeTcpReady(&ready);
  6000. res = TEST_RES_CHECK(1);
  6001. #endif
  6002. return res;
  6003. }
  6004. static int test_client_get_finished(void* args, cbType cb)
  6005. {
  6006. #if defined(WOLFSSL_HAVE_TLS_UNIQUE) && !defined(NO_WOLFSSL_CLIENT)
  6007. SOCKET_T sockfd = 0;
  6008. callback_functions* cbf;
  6009. WOLFSSL_CTX* ctx = 0;
  6010. WOLFSSL* ssl = 0;
  6011. char msg[64] = "hello wolfssl!";
  6012. char reply[1024];
  6013. int msgSz = (int)XSTRLEN(msg);
  6014. int ret, err = 0;
  6015. WOLFSSL_METHOD* method = NULL;
  6016. size_t msg_len = 0;
  6017. (void) args;
  6018. (void) cb;
  6019. ((func_args*)args)->return_code = TEST_FAIL;
  6020. cbf = ((func_args*)args)->callbacks;
  6021. if (cbf != NULL && cbf->method != NULL) {
  6022. method = cbf->method();
  6023. }
  6024. else {
  6025. method = wolfSSLv23_client_method();
  6026. }
  6027. ctx = wolfSSL_CTX_new(method);
  6028. /* Do connect here so server detects failures */
  6029. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6030. 0, 0, NULL);
  6031. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  6032. {
  6033. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6034. goto done;
  6035. }
  6036. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6037. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6038. goto done;
  6039. }
  6040. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6041. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6042. goto done;
  6043. }
  6044. /* call ctx setup callback */
  6045. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6046. cbf->ctx_ready(ctx);
  6047. }
  6048. ssl = wolfSSL_new(ctx);
  6049. if (ssl == NULL) {
  6050. goto done;
  6051. }
  6052. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6053. goto done;
  6054. }
  6055. /* call ssl setup callback */
  6056. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6057. cbf->ssl_ready(ssl);
  6058. }
  6059. #ifdef WOLFSSL_ASYNC_CRYPT
  6060. err = 0; /* Reset error */
  6061. #endif
  6062. do {
  6063. #ifdef WOLFSSL_ASYNC_CRYPT
  6064. if (err == WC_PENDING_E) {
  6065. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6066. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6067. }
  6068. #endif
  6069. ret = wolfSSL_connect(ssl);
  6070. err = wolfSSL_get_error(ssl, 0);
  6071. } while (err == WC_PENDING_E);
  6072. if (ret != WOLFSSL_SUCCESS) {
  6073. char buff[WOLFSSL_MAX_ERROR_SZ];
  6074. fprintf(stderr, "error = %d, %s\n", err,
  6075. wolfSSL_ERR_error_string(err, buff));
  6076. goto done;
  6077. }
  6078. /* get_finished test */
  6079. /* 1. get own sent message */
  6080. XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
  6081. msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
  6082. AssertIntGE(msg_len, 0);
  6083. /* 2. get peer message */
  6084. XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
  6085. msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
  6086. AssertIntGE(msg_len, 0);
  6087. if (cb != NULL)
  6088. (cb)(ctx, ssl);
  6089. #ifdef WOLFSSL_ASYNC_CRYPT
  6090. err = 0; /* Reset error */
  6091. #endif
  6092. do {
  6093. #ifdef WOLFSSL_ASYNC_CRYPT
  6094. if (err == WC_PENDING_E) {
  6095. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6096. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6097. }
  6098. #endif
  6099. ret = wolfSSL_write(ssl, msg, msgSz);
  6100. err = wolfSSL_get_error(ssl, 0);
  6101. } while (err == WC_PENDING_E);
  6102. if (ret != msgSz) {
  6103. /*err_sys("SSL_write failed");*/
  6104. goto done;
  6105. }
  6106. #ifdef WOLFSSL_ASYNC_CRYPT
  6107. err = 0; /* Reset error */
  6108. #endif
  6109. do {
  6110. #ifdef WOLFSSL_ASYNC_CRYPT
  6111. if (err == WC_PENDING_E) {
  6112. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6113. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6114. }
  6115. #endif
  6116. ret = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6117. err = wolfSSL_get_error(ssl, 0);
  6118. } while (err == WC_PENDING_E);
  6119. if (ret > 0) {
  6120. reply[ret] = '\0';
  6121. fprintf(stderr, "Server response: %s\n", reply);
  6122. }
  6123. ((func_args*)args)->return_code = TEST_SUCCESS;
  6124. done:
  6125. wolfSSL_free(ssl);
  6126. wolfSSL_CTX_free(ctx);
  6127. CloseSocket(sockfd);
  6128. #else
  6129. (void)args;
  6130. (void)cb;
  6131. #endif /* WOLFSSL_HAVE_TLS_UNIQUE && !NO_WOLFSSL_CLIENT */
  6132. return 0;
  6133. }
  6134. static int test_wolfSSL_get_finished(void)
  6135. {
  6136. int res = TEST_SKIPPED;
  6137. #if !defined(NO_RSA) && defined(WOLFSSL_HAVE_TLS_UNIQUE)
  6138. tcp_ready ready;
  6139. func_args client_args;
  6140. func_args server_args;
  6141. THREAD_TYPE serverThread;
  6142. XMEMSET(&client_args, 0, sizeof(func_args));
  6143. XMEMSET(&server_args, 0, sizeof(func_args));
  6144. StartTCP();
  6145. InitTcpReady(&ready);
  6146. #if defined(USE_WINDOWS_API)
  6147. /* use RNG to get random port if using windows */
  6148. ready.port = GetRandomPort();
  6149. #endif
  6150. server_args.signal = &ready;
  6151. client_args.signal = &ready;
  6152. start_thread(test_server_nofail, &server_args, &serverThread);
  6153. wait_tcp_ready(&server_args);
  6154. test_client_get_finished(&client_args, NULL);
  6155. join_thread(serverThread);
  6156. AssertTrue(client_args.return_code);
  6157. AssertTrue(server_args.return_code);
  6158. /* test received msg vs sent msg */
  6159. AssertIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
  6160. AssertIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
  6161. FreeTcpReady(&ready);
  6162. res = TEST_RES_CHECK(1);
  6163. #else
  6164. (void)test_client_get_finished;
  6165. #endif /* !NO_RSA && WOLFSSL_HAVE_TLS_UNIQUE */
  6166. return res;
  6167. }
  6168. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  6169. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  6170. !defined(NO_SESSION_CACHE)
  6171. /* Sessions to restore/store */
  6172. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_client_sess;
  6173. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_server_sess;
  6174. static WOLFSSL_CTX* test_wolfSSL_CTX_add_session_server_ctx;
  6175. static void test_wolfSSL_CTX_add_session_ctx_ready(WOLFSSL_CTX* ctx)
  6176. {
  6177. /* Don't store sessions. Lookup is still enabled. */
  6178. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  6179. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  6180. AssertIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  6181. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  6182. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  6183. /* Require both peers to provide certs */
  6184. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  6185. }
  6186. static void test_wolfSSL_CTX_add_session_on_result(WOLFSSL* ssl)
  6187. {
  6188. WOLFSSL_SESSION** sess;
  6189. if (wolfSSL_is_server(ssl))
  6190. sess = &test_wolfSSL_CTX_add_session_server_sess;
  6191. else
  6192. sess = &test_wolfSSL_CTX_add_session_client_sess;
  6193. if (*sess == NULL) {
  6194. #ifdef NO_SESSION_CACHE_REF
  6195. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  6196. #else
  6197. /* Test for backwards compatibility */
  6198. if (wolfSSL_is_server(ssl)) {
  6199. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  6200. }
  6201. else {
  6202. AssertNotNull(*sess = wolfSSL_get_session(ssl));
  6203. }
  6204. #endif
  6205. /* Now save the session in the internal store to make it available
  6206. * for lookup. For TLS 1.3, we can't save the session without
  6207. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  6208. * session from cache. */
  6209. if (wolfSSL_is_server(ssl)
  6210. #ifndef WOLFSSL_TICKET_HAVE_ID
  6211. && wolfSSL_version(ssl) != TLS1_3_VERSION
  6212. #endif
  6213. )
  6214. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  6215. *sess), WOLFSSL_SUCCESS);
  6216. }
  6217. else {
  6218. /* If we have a session retrieved then remaining connections should be
  6219. * resuming on that session */
  6220. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  6221. }
  6222. /* Save CTX to be able to decrypt tickets */
  6223. if (wolfSSL_is_server(ssl) &&
  6224. test_wolfSSL_CTX_add_session_server_ctx == NULL) {
  6225. AssertNotNull(test_wolfSSL_CTX_add_session_server_ctx
  6226. = wolfSSL_get_SSL_CTX(ssl));
  6227. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  6228. WOLFSSL_SUCCESS);
  6229. }
  6230. #ifdef SESSION_CERTS
  6231. #ifndef WOLFSSL_TICKET_HAVE_ID
  6232. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  6233. wolfSSL_session_reused(ssl))
  6234. #endif
  6235. {
  6236. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  6237. * for all connections. TLS 1.3 only has tickets so if we don't
  6238. * include the session id in the ticket then the certificates
  6239. * will not be available on resumption. */
  6240. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  6241. AssertNotNull(peer);
  6242. wolfSSL_X509_free(peer);
  6243. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  6244. AssertNotNull(wolfSSL_SESSION_get0_peer(*sess));
  6245. }
  6246. #endif
  6247. }
  6248. static void test_wolfSSL_CTX_add_session_ssl_ready(WOLFSSL* ssl)
  6249. {
  6250. /* Set the session to reuse for the client */
  6251. AssertIntEQ(wolfSSL_set_session(ssl,
  6252. test_wolfSSL_CTX_add_session_client_sess), WOLFSSL_SUCCESS);
  6253. }
  6254. #endif
  6255. static int test_wolfSSL_CTX_add_session(void)
  6256. {
  6257. int res = TEST_SKIPPED;
  6258. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  6259. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  6260. !defined(NO_SESSION_CACHE)
  6261. tcp_ready ready;
  6262. func_args client_args;
  6263. func_args server_args;
  6264. THREAD_TYPE serverThread;
  6265. callback_functions client_cb;
  6266. callback_functions server_cb;
  6267. method_provider methods[][2] = {
  6268. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  6269. !defined(NO_DES3))
  6270. /* Without AES there are almost no ciphersuites available. This leads
  6271. * to no ciphersuites being available and an error. */
  6272. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method },
  6273. #endif
  6274. #ifndef WOLFSSL_NO_TLS12
  6275. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method },
  6276. #endif
  6277. /* Needs the default ticket callback since it is tied to the
  6278. * connection context and this makes it easy to carry over the ticket
  6279. * crypto context between connections */
  6280. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  6281. defined(HAVE_SESSION_TICKET)
  6282. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method },
  6283. #endif
  6284. };
  6285. const size_t methodsLen = sizeof(methods)/sizeof(*methods);
  6286. size_t i, j;
  6287. for (i = 0; i < methodsLen; i++) {
  6288. /* First run creates a connection while the second+ run will attempt
  6289. * to resume the connection. The trick is that the internal cache
  6290. * is turned off. wolfSSL_CTX_add_session should put the session in
  6291. * the cache anyway. */
  6292. test_wolfSSL_CTX_add_session_client_sess = NULL;
  6293. test_wolfSSL_CTX_add_session_server_sess = NULL;
  6294. test_wolfSSL_CTX_add_session_server_ctx = NULL;
  6295. for (j = 0; j < 5; j++) {
  6296. #ifdef WOLFSSL_TIRTOS
  6297. fdOpenSession(Task_self());
  6298. #endif
  6299. StartTCP();
  6300. InitTcpReady(&ready);
  6301. XMEMSET(&client_args, 0, sizeof(func_args));
  6302. XMEMSET(&server_args, 0, sizeof(func_args));
  6303. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  6304. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  6305. client_cb.method = methods[i][0];
  6306. server_cb.method = methods[i][1];
  6307. server_args.signal = &ready;
  6308. server_args.callbacks = &server_cb;
  6309. client_args.signal = &ready;
  6310. client_args.callbacks = &client_cb;
  6311. if (test_wolfSSL_CTX_add_session_server_ctx != NULL) {
  6312. server_cb.ctx = test_wolfSSL_CTX_add_session_server_ctx;
  6313. server_cb.isSharedCtx = 1;
  6314. }
  6315. server_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  6316. client_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  6317. if (j != 0)
  6318. client_cb.ssl_ready = test_wolfSSL_CTX_add_session_ssl_ready;
  6319. server_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  6320. client_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  6321. server_cb.ticNoInit = 1; /* Use default builtin */
  6322. start_thread(test_server_nofail, &server_args, &serverThread);
  6323. wait_tcp_ready(&server_args);
  6324. test_client_nofail(&client_args, NULL);
  6325. join_thread(serverThread);
  6326. AssertTrue(client_args.return_code);
  6327. AssertTrue(server_args.return_code);
  6328. FreeTcpReady(&ready);
  6329. }
  6330. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_client_sess);
  6331. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_server_sess);
  6332. wolfSSL_CTX_free(test_wolfSSL_CTX_add_session_server_ctx);
  6333. }
  6334. res = TEST_RES_CHECK(1);
  6335. #endif
  6336. return res;
  6337. }
  6338. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  6339. /* canned export of a session using older version 3 */
  6340. static unsigned char version_3[] = {
  6341. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  6342. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  6343. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  6344. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  6345. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  6346. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6347. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  6348. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  6349. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  6350. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6351. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  6352. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  6353. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6354. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  6355. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  6356. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  6357. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  6358. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  6359. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  6360. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  6361. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6362. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6363. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6364. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6365. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6366. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6367. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6368. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6369. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6370. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6371. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6372. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
  6373. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  6374. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  6375. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  6376. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  6377. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  6378. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  6379. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  6380. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  6381. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  6382. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  6383. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  6384. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  6385. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6386. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  6387. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  6388. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  6389. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  6390. 0x2E, 0x31, 0xED, 0x4F
  6391. };
  6392. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  6393. static int test_wolfSSL_dtls_export(void)
  6394. {
  6395. int res = TEST_SKIPPED;
  6396. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  6397. tcp_ready ready;
  6398. func_args client_args;
  6399. func_args server_args;
  6400. THREAD_TYPE serverThread;
  6401. callback_functions server_cbf;
  6402. callback_functions client_cbf;
  6403. #ifdef WOLFSSL_TIRTOS
  6404. fdOpenSession(Task_self());
  6405. #endif
  6406. InitTcpReady(&ready);
  6407. #if defined(USE_WINDOWS_API)
  6408. /* use RNG to get random port if using windows */
  6409. ready.port = GetRandomPort();
  6410. #endif
  6411. /* set using dtls */
  6412. XMEMSET(&client_args, 0, sizeof(func_args));
  6413. XMEMSET(&server_args, 0, sizeof(func_args));
  6414. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6415. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  6416. server_cbf.method = wolfDTLSv1_2_server_method;
  6417. client_cbf.method = wolfDTLSv1_2_client_method;
  6418. server_args.callbacks = &server_cbf;
  6419. client_args.callbacks = &client_cbf;
  6420. server_args.signal = &ready;
  6421. client_args.signal = &ready;
  6422. start_thread(run_wolfssl_server, &server_args, &serverThread);
  6423. wait_tcp_ready(&server_args);
  6424. run_wolfssl_client(&client_args);
  6425. join_thread(serverThread);
  6426. AssertTrue(client_args.return_code);
  6427. AssertTrue(server_args.return_code);
  6428. FreeTcpReady(&ready);
  6429. #ifdef WOLFSSL_TIRTOS
  6430. fdOpenSession(Task_self());
  6431. #endif
  6432. {
  6433. SOCKET_T sockfd = 0;
  6434. WOLFSSL_CTX* ctx;
  6435. WOLFSSL* ssl;
  6436. char msg[64] = "hello wolfssl!";
  6437. char reply[1024];
  6438. int msgSz = (int)XSTRLEN(msg);
  6439. byte *session, *window;
  6440. unsigned int sessionSz, windowSz;
  6441. #ifndef TEST_IPV6
  6442. struct sockaddr_in peerAddr;
  6443. #else
  6444. struct sockaddr_in6 peerAddr;
  6445. #endif /* TEST_IPV6 */
  6446. int i;
  6447. /* Set ctx to DTLS 1.2 */
  6448. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  6449. AssertNotNull(ssl = wolfSSL_new(ctx));
  6450. /* test importing version 3 */
  6451. AssertIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  6452. /* test importing bad length and bad version */
  6453. version_3[2] += 1;
  6454. AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  6455. version_3[2] -= 1; version_3[1] = 0XA0;
  6456. AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  6457. wolfSSL_free(ssl);
  6458. wolfSSL_CTX_free(ctx);
  6459. /* check storing client state after connection and storing window only */
  6460. #ifdef WOLFSSL_TIRTOS
  6461. fdOpenSession(Task_self());
  6462. #endif
  6463. InitTcpReady(&ready);
  6464. #if defined(USE_WINDOWS_API)
  6465. /* use RNG to get random port if using windows */
  6466. ready.port = GetRandomPort();
  6467. #endif
  6468. /* set using dtls */
  6469. XMEMSET(&server_args, 0, sizeof(func_args));
  6470. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6471. server_cbf.method = wolfDTLSv1_2_server_method;
  6472. server_cbf.doUdp = 1;
  6473. server_args.callbacks = &server_cbf;
  6474. server_args.argc = 3; /* set loop_count to 3 */
  6475. server_args.signal = &ready;
  6476. start_thread(test_server_nofail, &server_args, &serverThread);
  6477. wait_tcp_ready(&server_args);
  6478. /* create and connect with client */
  6479. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  6480. AssertIntEQ(WOLFSSL_SUCCESS,
  6481. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  6482. AssertIntEQ(WOLFSSL_SUCCESS,
  6483. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  6484. AssertIntEQ(WOLFSSL_SUCCESS,
  6485. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  6486. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 1, 0, NULL);
  6487. AssertNotNull(ssl = wolfSSL_new(ctx));
  6488. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  6489. /* store server information connected too */
  6490. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  6491. #ifndef TEST_IPV6
  6492. peerAddr.sin_family = AF_INET;
  6493. AssertIntEQ(XINET_PTON(AF_INET, wolfSSLIP, &peerAddr.sin_addr),1);
  6494. peerAddr.sin_port = XHTONS(server_args.signal->port);
  6495. #else
  6496. peerAddr.sin6_family = AF_INET6;
  6497. AssertIntEQ(
  6498. XINET_PTON(AF_INET6, wolfSSLIP, &peerAddr.sin6_addr),1);
  6499. peerAddr.sin6_port = XHTONS(server_args.signal->port);
  6500. #endif
  6501. AssertIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  6502. WOLFSSL_SUCCESS);
  6503. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  6504. AssertIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  6505. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6506. AssertIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  6507. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  6508. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  6509. AssertIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  6510. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6511. AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  6512. wolfSSL_free(ssl);
  6513. for (i = 1; i < server_args.argc; i++) {
  6514. /* restore state */
  6515. AssertNotNull(ssl = wolfSSL_new(ctx));
  6516. AssertIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  6517. AssertIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  6518. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  6519. AssertIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  6520. WOLFSSL_SUCCESS);
  6521. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  6522. AssertIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  6523. AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  6524. wolfSSL_free(ssl);
  6525. }
  6526. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6527. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6528. wolfSSL_CTX_free(ctx);
  6529. fprintf(stderr, "done and waiting for server\n");
  6530. join_thread(serverThread);
  6531. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  6532. FreeTcpReady(&ready);
  6533. #ifdef WOLFSSL_TIRTOS
  6534. fdOpenSession(Task_self());
  6535. #endif
  6536. }
  6537. res = TEST_RES_CHECK(1);
  6538. #endif
  6539. return res;
  6540. }
  6541. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  6542. #ifdef WOLFSSL_TLS13
  6543. static const byte canned_client_tls13_session[] = {
  6544. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
  6545. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  6546. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
  6547. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  6548. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  6549. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  6550. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  6551. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
  6552. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  6553. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6554. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6555. 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  6556. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  6557. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  6558. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  6559. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  6560. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  6561. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  6562. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  6563. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  6564. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  6565. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  6566. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  6567. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  6568. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  6569. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  6570. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  6571. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  6572. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  6573. 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
  6574. 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
  6575. 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
  6576. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  6577. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  6578. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  6579. 0x00, 0x03
  6580. };
  6581. static const byte canned_server_tls13_session[] = {
  6582. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
  6583. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  6584. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
  6585. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  6586. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6587. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  6588. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  6589. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
  6590. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  6591. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6592. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6593. 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  6594. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  6595. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  6596. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  6597. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  6598. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  6599. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  6600. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  6601. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  6602. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  6603. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  6604. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  6605. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  6606. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  6607. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  6608. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  6609. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  6610. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  6611. 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
  6612. 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
  6613. 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
  6614. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  6615. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  6616. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  6617. 0x00, 0x04
  6618. };
  6619. #endif /* WOLFSSL_TLS13 */
  6620. static const byte canned_client_session[] = {
  6621. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  6622. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  6623. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
  6624. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  6625. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  6626. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6627. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  6628. 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
  6629. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  6630. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6631. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6632. 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  6633. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  6634. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  6635. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  6636. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  6637. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  6638. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  6639. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  6640. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  6641. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  6642. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  6643. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  6644. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  6645. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  6646. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  6647. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  6648. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  6649. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  6650. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  6651. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6652. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6653. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6654. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  6655. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  6656. 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  6657. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  6658. 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  6659. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  6660. 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  6661. 0x00, 0x03
  6662. };
  6663. static const byte canned_server_session[] = {
  6664. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  6665. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  6666. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  6667. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  6668. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  6669. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6670. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  6671. 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
  6672. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  6673. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6674. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6675. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  6676. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  6677. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  6678. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  6679. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  6680. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  6681. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  6682. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  6683. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  6684. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  6685. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  6686. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  6687. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  6688. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  6689. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  6690. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  6691. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  6692. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  6693. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  6694. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6695. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6696. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6697. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  6698. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  6699. 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  6700. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  6701. 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  6702. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  6703. 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  6704. 0x00, 0x04
  6705. };
  6706. static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
  6707. {
  6708. SOCKET_T sockfd = 0;
  6709. SOCKET_T clientfd = 0;
  6710. word16 port;
  6711. callback_functions* cbf;
  6712. WOLFSSL_CTX* ctx = 0;
  6713. WOLFSSL* ssl = 0;
  6714. char msg[] = "I hear you fa shizzle!";
  6715. char input[1024];
  6716. int idx;
  6717. #ifdef WOLFSSL_TIRTOS
  6718. fdOpenSession(Task_self());
  6719. #endif
  6720. ((func_args*)args)->return_code = TEST_FAIL;
  6721. cbf = ((func_args*)args)->callbacks;
  6722. {
  6723. WOLFSSL_METHOD* method = NULL;
  6724. if (cbf != NULL && cbf->method != NULL) {
  6725. method = cbf->method();
  6726. }
  6727. else {
  6728. method = wolfTLSv1_2_server_method();
  6729. }
  6730. ctx = wolfSSL_CTX_new(method);
  6731. }
  6732. if (ctx == NULL) {
  6733. goto done;
  6734. }
  6735. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  6736. #if defined(USE_WINDOWS_API)
  6737. port = ((func_args*)args)->signal->port;
  6738. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6739. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6740. /* Let tcp_listen assign port */
  6741. port = 0;
  6742. #else
  6743. /* Use default port */
  6744. port = wolfSSLPort;
  6745. #endif
  6746. /* do it here to detect failure */
  6747. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  6748. CloseSocket(sockfd);
  6749. /* call ctx setup callback */
  6750. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6751. cbf->ctx_ready(ctx);
  6752. }
  6753. ssl = wolfSSL_new(ctx);
  6754. if (ssl == NULL) {
  6755. goto done;
  6756. }
  6757. wolfSSL_set_fd(ssl, clientfd);
  6758. /* call ssl setup callback */
  6759. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6760. cbf->ssl_ready(ssl);
  6761. }
  6762. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6763. if (idx > 0) {
  6764. input[idx] = '\0';
  6765. fprintf(stderr, "Client message export/import: %s\n", input);
  6766. }
  6767. else {
  6768. fprintf(stderr, "ret = %d error = %d\n", idx,
  6769. wolfSSL_get_error(ssl, idx));
  6770. goto done;
  6771. }
  6772. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  6773. /*err_sys("SSL_write failed");*/
  6774. #ifdef WOLFSSL_TIRTOS
  6775. return;
  6776. #else
  6777. return 0;
  6778. #endif
  6779. }
  6780. #ifdef WOLFSSL_TIRTOS
  6781. Task_yield();
  6782. #endif
  6783. ((func_args*)args)->return_code = TEST_SUCCESS;
  6784. done:
  6785. wolfSSL_shutdown(ssl);
  6786. wolfSSL_free(ssl);
  6787. wolfSSL_CTX_free(ctx);
  6788. CloseSocket(clientfd);
  6789. #ifdef WOLFSSL_TIRTOS
  6790. fdCloseSession(Task_self());
  6791. #endif
  6792. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6793. && defined(HAVE_THREAD_LS)
  6794. wc_ecc_fp_free(); /* free per thread cache */
  6795. #endif
  6796. #if defined(HAVE_SESSION_TICKET) && \
  6797. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6798. #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
  6799. OpenSSLTicketCleanup();
  6800. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6801. TicketCleanup();
  6802. #endif
  6803. #endif
  6804. #ifndef WOLFSSL_TIRTOS
  6805. return 0;
  6806. #endif
  6807. }
  6808. static void load_tls12_canned_server(WOLFSSL* ssl)
  6809. {
  6810. int clientfd = wolfSSL_get_fd(ssl);
  6811. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
  6812. sizeof(canned_server_session)), sizeof(canned_server_session));
  6813. wolfSSL_set_fd(ssl, clientfd);
  6814. }
  6815. #ifdef WOLFSSL_TLS13
  6816. static void load_tls13_canned_server(WOLFSSL* ssl)
  6817. {
  6818. int clientfd = wolfSSL_get_fd(ssl);
  6819. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
  6820. sizeof(canned_server_tls13_session)),
  6821. sizeof(canned_server_tls13_session));
  6822. wolfSSL_set_fd(ssl, clientfd);
  6823. }
  6824. #endif
  6825. /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  6826. static int test_wolfSSL_tls_export_run(int v)
  6827. {
  6828. SOCKET_T sockfd = 0;
  6829. WOLFSSL_CTX* ctx = 0;
  6830. WOLFSSL* ssl = 0;
  6831. char msg[64] = "hello wolfssl!";
  6832. char reply[1024];
  6833. word32 replySz;
  6834. int msgSz = (int)XSTRLEN(msg);
  6835. const byte* clientSession = NULL;
  6836. int clientSessionSz = 0;
  6837. tcp_ready ready;
  6838. func_args server_args;
  6839. THREAD_TYPE serverThread;
  6840. callback_functions server_cbf;
  6841. #ifdef WOLFSSL_TIRTOS
  6842. fdOpenSession(Task_self());
  6843. #endif
  6844. InitTcpReady(&ready);
  6845. #if defined(USE_WINDOWS_API)
  6846. /* use RNG to get random port if using windows */
  6847. ready.port = GetRandomPort();
  6848. #endif
  6849. XMEMSET(&server_args, 0, sizeof(func_args));
  6850. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6851. switch (v) {
  6852. case WOLFSSL_TLSV1_2:
  6853. server_cbf.method = wolfTLSv1_2_server_method;
  6854. server_cbf.ssl_ready = load_tls12_canned_server;
  6855. /* setup the client side */
  6856. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  6857. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  6858. clientSession = canned_client_session;
  6859. clientSessionSz = sizeof(canned_client_session);
  6860. break;
  6861. #ifdef WOLFSSL_TLS13
  6862. case WOLFSSL_TLSV1_3:
  6863. server_cbf.method = wolfTLSv1_3_server_method;
  6864. server_cbf.ssl_ready = load_tls13_canned_server;
  6865. /* setup the client side */
  6866. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  6867. clientSession = canned_client_tls13_session;
  6868. clientSessionSz = sizeof(canned_client_tls13_session);
  6869. break;
  6870. #endif
  6871. }
  6872. server_args.callbacks = &server_cbf;
  6873. server_args.signal = &ready;
  6874. start_thread(tls_export_server, &server_args, &serverThread);
  6875. wait_tcp_ready(&server_args);
  6876. #ifdef WOLFSSL_TIRTOS
  6877. fdOpenSession(Task_self());
  6878. #endif
  6879. AssertNotNull(ssl = wolfSSL_new(ctx));
  6880. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  6881. AssertIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
  6882. clientSessionSz);
  6883. replySz = sizeof(reply);
  6884. AssertIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
  6885. #if !defined(NO_PSK) && defined(HAVE_ANON)
  6886. /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
  6887. AssertIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
  6888. #endif
  6889. wolfSSL_set_fd(ssl, sockfd);
  6890. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  6891. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
  6892. wolfSSL_free(ssl);
  6893. wolfSSL_CTX_free(ctx);
  6894. CloseSocket(sockfd);
  6895. #ifdef WOLFSSL_TIRTOS
  6896. fdCloseSession(Task_self());
  6897. #endif
  6898. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6899. && defined(HAVE_THREAD_LS)
  6900. wc_ecc_fp_free(); /* free per thread cache */
  6901. #endif
  6902. join_thread(serverThread);
  6903. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  6904. FreeTcpReady(&ready);
  6905. #ifdef WOLFSSL_TIRTOS
  6906. fdOpenSession(Task_self());
  6907. #endif
  6908. return TEST_RES_CHECK(1);
  6909. }
  6910. #endif
  6911. static int test_wolfSSL_tls_export(void)
  6912. {
  6913. int res = TEST_SKIPPED;
  6914. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  6915. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
  6916. #ifdef WOLFSSL_TLS13
  6917. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
  6918. #endif
  6919. res = TEST_RES_CHECK(1);
  6920. #endif
  6921. return res;
  6922. }
  6923. /*----------------------------------------------------------------------------*
  6924. | TLS extensions tests
  6925. *----------------------------------------------------------------------------*/
  6926. #ifdef ENABLE_TLS_CALLBACK_TEST
  6927. /* Connection test runner - generic */
  6928. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  6929. callback_functions* server_callbacks)
  6930. {
  6931. tcp_ready ready;
  6932. func_args client_args;
  6933. func_args server_args;
  6934. THREAD_TYPE serverThread;
  6935. XMEMSET(&client_args, 0, sizeof(func_args));
  6936. XMEMSET(&server_args, 0, sizeof(func_args));
  6937. StartTCP();
  6938. client_args.callbacks = client_callbacks;
  6939. server_args.callbacks = server_callbacks;
  6940. #ifdef WOLFSSL_TIRTOS
  6941. fdOpenSession(Task_self());
  6942. #endif
  6943. /* RUN Server side */
  6944. InitTcpReady(&ready);
  6945. #if defined(USE_WINDOWS_API)
  6946. /* use RNG to get random port if using windows */
  6947. ready.port = GetRandomPort();
  6948. #endif
  6949. server_args.signal = &ready;
  6950. client_args.signal = &ready;
  6951. start_thread(run_wolfssl_server, &server_args, &serverThread);
  6952. wait_tcp_ready(&server_args);
  6953. /* RUN Client side */
  6954. run_wolfssl_client(&client_args);
  6955. join_thread(serverThread);
  6956. FreeTcpReady(&ready);
  6957. #ifdef WOLFSSL_TIRTOS
  6958. fdCloseSession(Task_self());
  6959. #endif
  6960. client_callbacks->return_code = client_args.return_code;
  6961. server_callbacks->return_code = server_args.return_code;
  6962. }
  6963. #endif /* ENABLE_TLS_CALLBACK_TEST */
  6964. #ifdef HAVE_SNI
  6965. static int test_wolfSSL_UseSNI_params(void)
  6966. {
  6967. int res = TEST_SKIPPED;
  6968. #if !defined(NO_WOLFSSL_CLIENT)
  6969. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  6970. WOLFSSL *ssl = wolfSSL_new(ctx);
  6971. AssertNotNull(ctx);
  6972. AssertNotNull(ssl);
  6973. /* invalid [ctx|ssl] */
  6974. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  6975. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  6976. /* invalid type */
  6977. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  6978. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  6979. /* invalid data */
  6980. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  6981. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  6982. /* success case */
  6983. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  6984. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  6985. wolfSSL_free(ssl);
  6986. wolfSSL_CTX_free(ctx);
  6987. res = TEST_RES_CHECK(1);
  6988. #endif /* !NO_WOLFSSL_CLIENT */
  6989. return res;
  6990. }
  6991. /* BEGIN of connection tests callbacks */
  6992. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  6993. {
  6994. AssertIntEQ(WOLFSSL_SUCCESS,
  6995. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  6996. }
  6997. static void use_SNI_at_ssl(WOLFSSL* ssl)
  6998. {
  6999. AssertIntEQ(WOLFSSL_SUCCESS,
  7000. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  7001. }
  7002. static void different_SNI_at_ssl(WOLFSSL* ssl)
  7003. {
  7004. AssertIntEQ(WOLFSSL_SUCCESS,
  7005. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  7006. }
  7007. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  7008. {
  7009. use_SNI_at_ssl(ssl);
  7010. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  7011. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  7012. }
  7013. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  7014. {
  7015. use_SNI_at_ssl(ssl);
  7016. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  7017. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  7018. }
  7019. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  7020. {
  7021. use_SNI_at_ctx(ctx);
  7022. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  7023. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  7024. }
  7025. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  7026. {
  7027. use_SNI_at_ssl(ssl);
  7028. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  7029. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  7030. }
  7031. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  7032. {
  7033. use_SNI_at_ctx(ctx);
  7034. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  7035. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  7036. }
  7037. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  7038. {
  7039. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  7040. }
  7041. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  7042. {
  7043. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  7044. }
  7045. static void verify_SNI_no_matching(WOLFSSL* ssl)
  7046. {
  7047. byte type = WOLFSSL_SNI_HOST_NAME;
  7048. void* request = (void*) &type; /* to be overwritten */
  7049. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  7050. AssertNotNull(request);
  7051. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, &request));
  7052. AssertNull(request);
  7053. }
  7054. static void verify_SNI_real_matching(WOLFSSL* ssl)
  7055. {
  7056. byte type = WOLFSSL_SNI_HOST_NAME;
  7057. void* request = NULL;
  7058. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  7059. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  7060. AssertNotNull(request);
  7061. AssertStrEQ("www.wolfssl.com", (char*)request);
  7062. }
  7063. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  7064. {
  7065. byte type = WOLFSSL_SNI_HOST_NAME;
  7066. void* request = NULL;
  7067. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  7068. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  7069. AssertNotNull(request);
  7070. AssertStrEQ("ww2.wolfssl.com", (char*)request);
  7071. }
  7072. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  7073. {
  7074. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  7075. }
  7076. /* END of connection tests callbacks */
  7077. static int test_wolfSSL_UseSNI_connection(void)
  7078. {
  7079. int res = TEST_SKIPPED;
  7080. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  7081. callback_functions client_cb;
  7082. callback_functions server_cb;
  7083. size_t i;
  7084. struct {
  7085. method_provider client_meth;
  7086. method_provider server_meth;
  7087. } methods[] = {
  7088. #if defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_TLS13)
  7089. {wolfSSLv23_client_method, wolfSSLv23_server_method},
  7090. #endif
  7091. #ifndef WOLFSSL_NO_TLS12
  7092. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method},
  7093. #endif
  7094. #ifdef WOLFSSL_TLS13
  7095. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method},
  7096. #endif
  7097. };
  7098. for (i = 0; i < (sizeof(methods)/sizeof(*methods)); i++) {
  7099. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7100. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7101. client_cb.method = methods[i].client_meth;
  7102. server_cb.method = methods[i].server_meth;
  7103. client_cb.devId = testDevId;
  7104. server_cb.devId = testDevId;
  7105. /* success case at ctx */
  7106. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  7107. server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  7108. test_wolfSSL_client_server(&client_cb, &server_cb);
  7109. /* success case at ssl */
  7110. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
  7111. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
  7112. test_wolfSSL_client_server(&client_cb, &server_cb);
  7113. /* default mismatch behavior */
  7114. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
  7115. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
  7116. test_wolfSSL_client_server(&client_cb, &server_cb);
  7117. /* continue on mismatch */
  7118. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  7119. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  7120. test_wolfSSL_client_server(&client_cb, &server_cb);
  7121. /* fake answer on mismatch */
  7122. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  7123. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
  7124. test_wolfSSL_client_server(&client_cb, &server_cb);
  7125. /* sni abort - success */
  7126. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  7127. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  7128. test_wolfSSL_client_server(&client_cb, &server_cb);
  7129. /* sni abort - abort when absent (ctx) */
  7130. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  7131. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
  7132. test_wolfSSL_client_server(&client_cb, &server_cb);
  7133. /* sni abort - abort when absent (ssl) */
  7134. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  7135. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
  7136. test_wolfSSL_client_server(&client_cb, &server_cb);
  7137. /* sni abort - success when overwritten */
  7138. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  7139. 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;
  7140. test_wolfSSL_client_server(&client_cb, &server_cb);
  7141. /* sni abort - success when allowing mismatches */
  7142. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  7143. server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
  7144. test_wolfSSL_client_server(&client_cb, &server_cb);
  7145. }
  7146. res = TEST_RES_CHECK(1);
  7147. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  7148. return res;
  7149. }
  7150. static int test_wolfSSL_SNI_GetFromBuffer(void)
  7151. {
  7152. byte buff[] = { /* www.paypal.com */
  7153. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  7154. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  7155. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  7156. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  7157. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  7158. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  7159. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  7160. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  7161. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  7162. };
  7163. byte buff2[] = { /* api.textmate.org */
  7164. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  7165. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  7166. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  7167. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  7168. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  7169. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  7170. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  7171. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  7172. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  7173. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  7174. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  7175. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  7176. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  7177. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  7178. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  7179. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  7180. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  7181. };
  7182. byte buff3[] = { /* no sni extension */
  7183. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  7184. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  7185. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  7186. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  7187. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  7188. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  7189. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  7190. };
  7191. byte buff4[] = { /* last extension has zero size */
  7192. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  7193. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  7194. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  7195. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  7196. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  7197. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  7198. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  7199. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  7200. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  7201. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  7202. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  7203. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  7204. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  7205. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  7206. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  7207. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  7208. 0x12, 0x00, 0x00
  7209. };
  7210. byte buff5[] = { /* SSL v2.0 client hello */
  7211. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  7212. /* dummy bytes bellow, just to pass size check */
  7213. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  7214. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  7215. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  7216. };
  7217. byte result[32] = {0};
  7218. word32 length = 32;
  7219. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  7220. 0, result, &length));
  7221. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  7222. 0, result, &length));
  7223. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  7224. 1, result, &length));
  7225. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  7226. 0, result, &length));
  7227. buff[0] = 0x16;
  7228. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  7229. 0, result, &length));
  7230. buff[1] = 0x03;
  7231. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  7232. sizeof(buff), 0, result, &length));
  7233. buff[2] = 0x03;
  7234. AssertIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  7235. sizeof(buff), 0, result, &length));
  7236. buff[4] = 0x64;
  7237. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  7238. 0, result, &length));
  7239. result[length] = 0;
  7240. AssertStrEQ("www.paypal.com", (const char*) result);
  7241. length = 32;
  7242. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  7243. 0, result, &length));
  7244. result[length] = 0;
  7245. AssertStrEQ("api.textmate.org", (const char*) result);
  7246. /* SSL v2.0 tests */
  7247. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  7248. sizeof(buff5), 0, result, &length));
  7249. buff5[2] = 0x02;
  7250. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  7251. sizeof(buff5), 0, result, &length));
  7252. buff5[2] = 0x01; buff5[6] = 0x08;
  7253. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  7254. sizeof(buff5), 0, result, &length));
  7255. buff5[6] = 0x09; buff5[8] = 0x01;
  7256. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  7257. sizeof(buff5), 0, result, &length));
  7258. return TEST_RES_CHECK(1);
  7259. }
  7260. #endif /* HAVE_SNI */
  7261. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  7262. static int test_wolfSSL_UseTrustedCA(void)
  7263. {
  7264. int res = TEST_SKIPPED;
  7265. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  7266. && !defined(NO_RSA)
  7267. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7268. WOLFSSL_CTX *ctx;
  7269. WOLFSSL *ssl;
  7270. byte id[20];
  7271. #ifndef NO_WOLFSSL_SERVER
  7272. AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  7273. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  7274. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  7275. #else
  7276. AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  7277. #endif
  7278. AssertNotNull((ssl = wolfSSL_new(ctx)));
  7279. XMEMSET(id, 0, sizeof(id));
  7280. /* error cases */
  7281. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  7282. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7283. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  7284. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7285. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  7286. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7287. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  7288. #ifdef NO_SHA
  7289. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7290. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  7291. #endif
  7292. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7293. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  7294. /* success cases */
  7295. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7296. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  7297. #ifndef NO_SHA
  7298. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7299. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  7300. #endif
  7301. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7302. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  7303. wolfSSL_free(ssl);
  7304. wolfSSL_CTX_free(ctx);
  7305. res = TEST_RES_CHECK(1);
  7306. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7307. #endif /* HAVE_TRUSTED_CA */
  7308. return res;
  7309. }
  7310. static int test_wolfSSL_UseMaxFragment(void)
  7311. {
  7312. int res = TEST_SKIPPED;
  7313. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  7314. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  7315. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7316. #ifndef NO_WOLFSSL_SERVER
  7317. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  7318. #else
  7319. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7320. #endif
  7321. WOLFSSL *ssl;
  7322. #ifdef OPENSSL_EXTRA
  7323. int (*UseMaxFragment)(SSL *s, uint8_t mode);
  7324. int (*CTX_UseMaxFragment)(SSL_CTX *c, uint8_t mode);
  7325. #else
  7326. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  7327. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  7328. #endif
  7329. #ifndef NO_WOLFSSL_SERVER
  7330. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  7331. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  7332. #endif
  7333. AssertNotNull(ctx);
  7334. ssl = wolfSSL_new(ctx);
  7335. AssertNotNull(ssl);
  7336. #ifdef OPENSSL_EXTRA
  7337. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  7338. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  7339. #else
  7340. UseMaxFragment = wolfSSL_UseMaxFragment;
  7341. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  7342. #endif
  7343. /* error cases */
  7344. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  7345. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  7346. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  7347. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  7348. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  7349. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  7350. /* success case */
  7351. #ifdef OPENSSL_EXTRA
  7352. AssertIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  7353. #else
  7354. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  7355. #endif
  7356. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  7357. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  7358. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  7359. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  7360. #ifdef OPENSSL_EXTRA
  7361. AssertIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  7362. AssertIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  7363. #else
  7364. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  7365. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  7366. #endif
  7367. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  7368. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  7369. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  7370. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  7371. #ifdef OPENSSL_EXTRA
  7372. AssertIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  7373. #else
  7374. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  7375. #endif
  7376. wolfSSL_free(ssl);
  7377. wolfSSL_CTX_free(ctx);
  7378. res = TEST_RES_CHECK(1);
  7379. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7380. #endif
  7381. return res;
  7382. }
  7383. static int test_wolfSSL_UseTruncatedHMAC(void)
  7384. {
  7385. int res = TEST_SKIPPED;
  7386. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  7387. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  7388. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7389. #ifndef NO_WOLFSSL_SERVER
  7390. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  7391. #else
  7392. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7393. #endif
  7394. WOLFSSL *ssl;
  7395. AssertNotNull(ctx);
  7396. #ifndef NO_WOLFSSL_SERVER
  7397. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  7398. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  7399. #endif
  7400. ssl = wolfSSL_new(ctx);
  7401. AssertNotNull(ssl);
  7402. /* error cases */
  7403. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  7404. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  7405. /* success case */
  7406. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  7407. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  7408. wolfSSL_free(ssl);
  7409. wolfSSL_CTX_free(ctx);
  7410. res = TEST_RES_CHECK(1);
  7411. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7412. #endif
  7413. return res;
  7414. }
  7415. static int test_wolfSSL_UseSupportedCurve(void)
  7416. {
  7417. int res = TEST_SKIPPED;
  7418. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && !defined(NO_TLS)
  7419. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7420. WOLFSSL *ssl = wolfSSL_new(ctx);
  7421. AssertNotNull(ctx);
  7422. AssertNotNull(ssl);
  7423. /* error cases */
  7424. AssertIntNE(WOLFSSL_SUCCESS,
  7425. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  7426. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  7427. AssertIntNE(WOLFSSL_SUCCESS,
  7428. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  7429. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  7430. /* success case */
  7431. AssertIntEQ(WOLFSSL_SUCCESS,
  7432. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  7433. AssertIntEQ(WOLFSSL_SUCCESS,
  7434. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  7435. wolfSSL_free(ssl);
  7436. wolfSSL_CTX_free(ctx);
  7437. res = TEST_RES_CHECK(1);
  7438. #endif
  7439. return res;
  7440. }
  7441. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  7442. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  7443. {
  7444. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  7445. }
  7446. static void use_ALPN_all(WOLFSSL* ssl)
  7447. {
  7448. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  7449. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  7450. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  7451. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  7452. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7453. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  7454. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7455. }
  7456. static void use_ALPN_all_continue(WOLFSSL* ssl)
  7457. {
  7458. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  7459. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  7460. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  7461. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  7462. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7463. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  7464. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  7465. }
  7466. static void use_ALPN_one(WOLFSSL* ssl)
  7467. {
  7468. /* spdy/2 */
  7469. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  7470. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  7471. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7472. }
  7473. static void use_ALPN_unknown(WOLFSSL* ssl)
  7474. {
  7475. /* http/2.0 */
  7476. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  7477. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  7478. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7479. }
  7480. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  7481. {
  7482. /* http/2.0 */
  7483. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  7484. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  7485. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  7486. }
  7487. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  7488. {
  7489. /* spdy/3 */
  7490. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7491. char *proto = NULL;
  7492. word16 protoSz = 0;
  7493. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  7494. /* check value */
  7495. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  7496. if (proto) {
  7497. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  7498. }
  7499. }
  7500. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  7501. {
  7502. char *proto = NULL;
  7503. word16 protoSz = 0;
  7504. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  7505. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  7506. /* check value */
  7507. AssertIntEQ(1, (0 == protoSz));
  7508. AssertIntEQ(1, (NULL == proto));
  7509. }
  7510. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  7511. {
  7512. /* http/1.1 */
  7513. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  7514. char *proto;
  7515. word16 protoSz = 0;
  7516. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  7517. /* check value */
  7518. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  7519. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  7520. }
  7521. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  7522. {
  7523. /* spdy/2 */
  7524. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  7525. char *proto;
  7526. word16 protoSz = 0;
  7527. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  7528. /* check value */
  7529. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  7530. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  7531. }
  7532. static void verify_ALPN_client_list(WOLFSSL* ssl)
  7533. {
  7534. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  7535. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  7536. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  7537. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  7538. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7539. char *clist = NULL;
  7540. word16 clistSz = 0;
  7541. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  7542. &clistSz));
  7543. /* check value */
  7544. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  7545. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  7546. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  7547. }
  7548. static int test_wolfSSL_UseALPN_connection(void)
  7549. {
  7550. int res = TEST_SKIPPED;
  7551. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  7552. callback_functions client_cb;
  7553. callback_functions server_cb;
  7554. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7555. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7556. client_cb.method = wolfSSLv23_client_method;
  7557. server_cb.method = wolfSSLv23_server_method;
  7558. client_cb.devId = testDevId;
  7559. server_cb.devId = testDevId;
  7560. /* success case same list */
  7561. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  7562. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
  7563. test_wolfSSL_client_server(&client_cb, &server_cb);
  7564. /* success case only one for server */
  7565. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  7566. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
  7567. test_wolfSSL_client_server(&client_cb, &server_cb);
  7568. /* success case only one for client */
  7569. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
  7570. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
  7571. test_wolfSSL_client_server(&client_cb, &server_cb);
  7572. /* success case none for client */
  7573. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  7574. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
  7575. test_wolfSSL_client_server(&client_cb, &server_cb);
  7576. /* success case mismatch behavior but option 'continue' set */
  7577. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
  7578. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
  7579. test_wolfSSL_client_server(&client_cb, &server_cb);
  7580. /* success case read protocol send by client */
  7581. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  7582. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
  7583. test_wolfSSL_client_server(&client_cb, &server_cb);
  7584. /* mismatch behavior with same list
  7585. * the first and only this one must be taken */
  7586. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  7587. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
  7588. test_wolfSSL_client_server(&client_cb, &server_cb);
  7589. /* default mismatch behavior */
  7590. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  7591. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  7592. test_wolfSSL_client_server(&client_cb, &server_cb);
  7593. res = TEST_RES_CHECK(1);
  7594. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  7595. return res;
  7596. }
  7597. static int test_wolfSSL_UseALPN_params(void)
  7598. {
  7599. int res = TEST_SKIPPED;
  7600. #ifndef NO_WOLFSSL_CLIENT
  7601. /* "http/1.1" */
  7602. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  7603. /* "spdy/1" */
  7604. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  7605. /* "spdy/2" */
  7606. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  7607. /* "spdy/3" */
  7608. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7609. char buff[256];
  7610. word32 idx;
  7611. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7612. WOLFSSL *ssl = wolfSSL_new(ctx);
  7613. AssertNotNull(ctx);
  7614. AssertNotNull(ssl);
  7615. /* error cases */
  7616. AssertIntNE(WOLFSSL_SUCCESS,
  7617. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  7618. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7619. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  7620. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7621. /* success case */
  7622. /* http1 only */
  7623. AssertIntEQ(WOLFSSL_SUCCESS,
  7624. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  7625. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7626. /* http1, spdy1 */
  7627. XMEMCPY(buff, http1, sizeof(http1));
  7628. idx = sizeof(http1);
  7629. buff[idx++] = ',';
  7630. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  7631. idx += sizeof(spdy1);
  7632. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  7633. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7634. /* http1, spdy2, spdy1 */
  7635. XMEMCPY(buff, http1, sizeof(http1));
  7636. idx = sizeof(http1);
  7637. buff[idx++] = ',';
  7638. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  7639. idx += sizeof(spdy2);
  7640. buff[idx++] = ',';
  7641. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  7642. idx += sizeof(spdy1);
  7643. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  7644. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7645. /* spdy3, http1, spdy2, spdy1 */
  7646. XMEMCPY(buff, spdy3, sizeof(spdy3));
  7647. idx = sizeof(spdy3);
  7648. buff[idx++] = ',';
  7649. XMEMCPY(buff+idx, http1, sizeof(http1));
  7650. idx += sizeof(http1);
  7651. buff[idx++] = ',';
  7652. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  7653. idx += sizeof(spdy2);
  7654. buff[idx++] = ',';
  7655. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  7656. idx += sizeof(spdy1);
  7657. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  7658. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  7659. wolfSSL_free(ssl);
  7660. wolfSSL_CTX_free(ctx);
  7661. res = TEST_RES_CHECK(1);
  7662. #endif
  7663. return res;
  7664. }
  7665. #endif /* HAVE_ALPN */
  7666. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  7667. static void CTX_set_alpn_protos(SSL_CTX *ctx)
  7668. {
  7669. unsigned char p[] = {
  7670. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  7671. 6, 's', 'p', 'd', 'y', '/', '2',
  7672. 6, 's', 'p', 'd', 'y', '/', '1',
  7673. };
  7674. unsigned char p_len = sizeof(p);
  7675. int ret;
  7676. ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
  7677. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  7678. AssertIntEQ(ret, 0);
  7679. #else
  7680. AssertIntEQ(ret, SSL_SUCCESS);
  7681. #endif
  7682. }
  7683. static void set_alpn_protos(SSL* ssl)
  7684. {
  7685. unsigned char p[] = {
  7686. 6, 's', 'p', 'd', 'y', '/', '3',
  7687. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  7688. 6, 's', 'p', 'd', 'y', '/', '2',
  7689. 6, 's', 'p', 'd', 'y', '/', '1',
  7690. };
  7691. unsigned char p_len = sizeof(p);
  7692. int ret;
  7693. ret = SSL_set_alpn_protos(ssl, p, p_len);
  7694. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  7695. AssertIntEQ(ret, 0);
  7696. #else
  7697. AssertIntEQ(ret, SSL_SUCCESS);
  7698. #endif
  7699. }
  7700. static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
  7701. {
  7702. /* "spdy/3" */
  7703. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7704. const unsigned char *proto;
  7705. unsigned int protoSz = 0;
  7706. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  7707. /* check value */
  7708. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  7709. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  7710. }
  7711. static void verify_alpn_matching_http1(WOLFSSL* ssl)
  7712. {
  7713. /* "http/1.1" */
  7714. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  7715. const unsigned char *proto;
  7716. unsigned int protoSz = 0;
  7717. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  7718. /* check value */
  7719. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  7720. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  7721. }
  7722. static int test_wolfSSL_set_alpn_protos(void)
  7723. {
  7724. int res = TEST_SKIPPED;
  7725. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  7726. callback_functions client_cb;
  7727. callback_functions server_cb;
  7728. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7729. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7730. client_cb.method = wolfSSLv23_client_method;
  7731. server_cb.method = wolfSSLv23_server_method;
  7732. client_cb.devId = testDevId;
  7733. server_cb.devId = testDevId;
  7734. /* use CTX_alpn_protos */
  7735. client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  7736. server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
  7737. test_wolfSSL_client_server(&client_cb, &server_cb);
  7738. /* use set_alpn_protos */
  7739. client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
  7740. server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
  7741. test_wolfSSL_client_server(&client_cb, &server_cb);
  7742. res = TEST_RES_CHECK(1);
  7743. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  7744. return res;
  7745. }
  7746. #endif /* HAVE_ALPN_PROTOS_SUPPORT */
  7747. static int test_wolfSSL_DisableExtendedMasterSecret(void)
  7748. {
  7749. int res = TEST_SKIPPED;
  7750. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  7751. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7752. WOLFSSL *ssl = wolfSSL_new(ctx);
  7753. AssertNotNull(ctx);
  7754. AssertNotNull(ssl);
  7755. /* error cases */
  7756. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  7757. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  7758. /* success cases */
  7759. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  7760. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  7761. wolfSSL_free(ssl);
  7762. wolfSSL_CTX_free(ctx);
  7763. res = TEST_RES_CHECK(1);
  7764. #endif
  7765. return res;
  7766. }
  7767. static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  7768. {
  7769. int res = TEST_SKIPPED;
  7770. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  7771. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7772. WOLFSSL *ssl = wolfSSL_new(ctx);
  7773. AssertNotNull(ctx);
  7774. AssertNotNull(ssl);
  7775. /* error cases */
  7776. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  7777. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  7778. /* success cases */
  7779. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  7780. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  7781. wolfSSL_free(ssl);
  7782. wolfSSL_CTX_free(ctx);
  7783. res = TEST_RES_CHECK(1);
  7784. #endif
  7785. return res;
  7786. }
  7787. #if !defined(NO_WOLFSSL_SERVER) && (!defined(NO_RSA) || defined(HAVE_ECC))
  7788. /* Called when writing. */
  7789. static int DummySend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  7790. {
  7791. (void)ssl;
  7792. (void)buf;
  7793. (void)sz;
  7794. (void)ctx;
  7795. /* Force error return from wolfSSL_accept_TLSv13(). */
  7796. return WANT_WRITE;
  7797. }
  7798. /* Called when reading. */
  7799. static int BufferInfoRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  7800. {
  7801. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  7802. int len = (int)msg->length;
  7803. (void)ssl;
  7804. (void)sz;
  7805. /* Pass back as much of message as will fit in buffer. */
  7806. if (len > sz)
  7807. len = sz;
  7808. XMEMCPY(buf, msg->buffer, len);
  7809. /* Move over returned data. */
  7810. msg->buffer += len;
  7811. msg->length -= len;
  7812. /* Amount actually copied. */
  7813. return len;
  7814. }
  7815. #endif
  7816. /* Test the detection of duplicate known TLS extensions.
  7817. * Specifically in a ClientHello.
  7818. */
  7819. static int test_tls_ext_duplicate(void)
  7820. {
  7821. int res = TEST_SKIPPED;
  7822. #if !defined(NO_WOLFSSL_SERVER) && (!defined(NO_RSA) || defined(HAVE_ECC))
  7823. const unsigned char clientHelloDupTlsExt[] = {
  7824. 0x16, 0x03, 0x03, 0x00, 0x6a, 0x01, 0x00, 0x00,
  7825. 0x66, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  7826. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  7827. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  7828. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  7829. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  7830. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  7831. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  7832. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7833. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x13, 0x01,
  7834. 0x00, 0x9e, 0x01, 0x00,
  7835. /* Extensions - duplicate signature algorithms. */
  7836. 0x00, 0x19, 0x00, 0x0d,
  7837. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01, 0x00, 0x0d,
  7838. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01,
  7839. /* Supported Versions extension for TLS 1.3. */
  7840. 0x00, 0x2b,
  7841. 0x00, 0x05, 0x04, 0x03, 0x04, 0x03, 0x03
  7842. };
  7843. WOLFSSL_BUFFER_INFO msg;
  7844. const char* testCertFile;
  7845. const char* testKeyFile;
  7846. WOLFSSL_CTX *ctx;
  7847. WOLFSSL *ssl;
  7848. #ifndef NO_RSA
  7849. testCertFile = svrCertFile;
  7850. testKeyFile = svrKeyFile;
  7851. #elif defined(HAVE_ECC)
  7852. testCertFile = eccCertFile;
  7853. testKeyFile = eccKeyFile;
  7854. #endif
  7855. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  7856. AssertNotNull(ctx);
  7857. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  7858. WOLFSSL_FILETYPE_PEM));
  7859. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  7860. WOLFSSL_FILETYPE_PEM));
  7861. /* Read from 'msg'. */
  7862. wolfSSL_SetIORecv(ctx, BufferInfoRecv);
  7863. /* No where to send to - dummy sender. */
  7864. wolfSSL_SetIOSend(ctx, DummySend);
  7865. ssl = wolfSSL_new(ctx);
  7866. AssertNotNull(ssl);
  7867. msg.buffer = (unsigned char*)clientHelloDupTlsExt;
  7868. msg.length = (unsigned int)sizeof(clientHelloDupTlsExt);
  7869. wolfSSL_SetIOReadCtx(ssl, &msg);
  7870. AssertIntNE(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  7871. AssertIntEQ(wolfSSL_get_error(ssl, 0), DUPLICATE_TLS_EXT_E);
  7872. wolfSSL_free(ssl);
  7873. wolfSSL_CTX_free(ctx);
  7874. res = TEST_RES_CHECK(1);
  7875. #endif
  7876. return res;
  7877. }
  7878. /*----------------------------------------------------------------------------*
  7879. | X509 Tests
  7880. *----------------------------------------------------------------------------*/
  7881. static int test_wolfSSL_X509_NAME_get_entry(void)
  7882. {
  7883. int res = TEST_SKIPPED;
  7884. #if !defined(NO_CERTS) && !defined(NO_RSA)
  7885. #if defined(OPENSSL_ALL) || \
  7886. (defined(OPENSSL_EXTRA) && \
  7887. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  7888. /* use openssl like name to test mapping */
  7889. X509_NAME_ENTRY* ne;
  7890. X509_NAME* name;
  7891. X509* x509;
  7892. #ifndef NO_FILESYSTEM
  7893. ASN1_STRING* asn;
  7894. char* subCN = NULL;
  7895. #endif
  7896. int idx;
  7897. ASN1_OBJECT *object = NULL;
  7898. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  7899. defined(WOLFSSL_NGINX)
  7900. #ifndef NO_BIO
  7901. BIO* bio;
  7902. #endif
  7903. #endif
  7904. #ifndef NO_FILESYSTEM
  7905. x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  7906. WOLFSSL_FILETYPE_PEM);
  7907. AssertNotNull(x509);
  7908. name = X509_get_subject_name(x509);
  7909. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  7910. AssertIntGE(idx, 0);
  7911. ne = X509_NAME_get_entry(name, idx);
  7912. AssertNotNull(ne);
  7913. asn = X509_NAME_ENTRY_get_data(ne);
  7914. AssertNotNull(asn);
  7915. subCN = (char*)ASN1_STRING_data(asn);
  7916. AssertNotNull(subCN);
  7917. wolfSSL_FreeX509(x509);
  7918. #endif
  7919. x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  7920. WOLFSSL_FILETYPE_PEM);
  7921. AssertNotNull(x509);
  7922. name = X509_get_subject_name(x509);
  7923. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  7924. AssertIntGE(idx, 0);
  7925. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  7926. defined(WOLFSSL_NGINX)
  7927. #ifndef NO_BIO
  7928. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  7929. AssertIntEQ(X509_NAME_print_ex(bio, name, 4,
  7930. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  7931. AssertIntEQ(X509_NAME_print_ex_fp(stderr, name, 4,
  7932. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  7933. BIO_free(bio);
  7934. #endif
  7935. #endif
  7936. ne = X509_NAME_get_entry(name, idx);
  7937. AssertNotNull(ne);
  7938. AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
  7939. wolfSSL_FreeX509(x509);
  7940. res = TEST_RES_CHECK(1);
  7941. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  7942. #endif /* !NO_CERTS && !NO_RSA */
  7943. return res;
  7944. }
  7945. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  7946. static int test_wolfSSL_PKCS12(void)
  7947. {
  7948. int res = TEST_SKIPPED;
  7949. /* .p12 file is encrypted with DES3 */
  7950. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  7951. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  7952. * Password Key
  7953. */
  7954. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  7955. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  7956. !defined(NO_SHA) && defined(HAVE_PKCS12) && !defined(NO_BIO)
  7957. byte buf[6000];
  7958. char file[] = "./certs/test-servercert.p12";
  7959. char order[] = "./certs/ecc-rsa-server.p12";
  7960. #ifdef WC_RC2
  7961. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  7962. #endif
  7963. char pass[] = "a password";
  7964. const char goodPsw[] = "wolfSSL test";
  7965. const char badPsw[] = "bad";
  7966. #ifdef HAVE_ECC
  7967. WOLFSSL_X509_NAME* subject;
  7968. WOLFSSL_X509 *x509;
  7969. #endif
  7970. XFILE f;
  7971. int bytes, ret, goodPswLen, badPswLen;
  7972. WOLFSSL_BIO *bio;
  7973. WOLFSSL_EVP_PKEY *pkey;
  7974. WC_PKCS12 *pkcs12;
  7975. WC_PKCS12 *pkcs12_2;
  7976. WOLFSSL_X509 *cert;
  7977. WOLFSSL_X509 *tmp;
  7978. WOLF_STACK_OF(WOLFSSL_X509) *ca;
  7979. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  7980. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  7981. WOLFSSL_CTX *ctx;
  7982. WOLFSSL *ssl;
  7983. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  7984. #endif
  7985. f = XFOPEN(file, "rb");
  7986. AssertTrue((f != XBADFILE));
  7987. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  7988. XFCLOSE(f);
  7989. goodPswLen = (int)XSTRLEN(goodPsw);
  7990. badPswLen = (int)XSTRLEN(badPsw);
  7991. bio = BIO_new_mem_buf((void*)buf, bytes);
  7992. AssertNotNull(bio);
  7993. pkcs12 = d2i_PKCS12_bio(bio, NULL);
  7994. AssertNotNull(pkcs12);
  7995. PKCS12_free(pkcs12);
  7996. AssertIntEQ(BIO_write(bio, buf, bytes), bytes); /* d2i consumes BIO */
  7997. d2i_PKCS12_bio(bio, &pkcs12);
  7998. AssertNotNull(pkcs12);
  7999. BIO_free(bio);
  8000. /* check verify MAC directly */
  8001. ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen);
  8002. AssertIntEQ(ret, 1);
  8003. /* check verify MAC fail case directly */
  8004. ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen);
  8005. AssertIntEQ(ret, 0);
  8006. /* check verify MAC fail case */
  8007. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  8008. AssertIntEQ(ret, 0);
  8009. AssertNull(pkey);
  8010. AssertNull(cert);
  8011. /* check parse with no extra certs kept */
  8012. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  8013. AssertIntEQ(ret, 1);
  8014. AssertNotNull(pkey);
  8015. AssertNotNull(cert);
  8016. wolfSSL_EVP_PKEY_free(pkey);
  8017. wolfSSL_X509_free(cert);
  8018. /* check parse with extra certs kept */
  8019. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  8020. AssertIntEQ(ret, 1);
  8021. AssertNotNull(pkey);
  8022. AssertNotNull(cert);
  8023. AssertNotNull(ca);
  8024. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  8025. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  8026. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  8027. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8028. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  8029. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  8030. #else
  8031. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  8032. #endif
  8033. /* Copy stack structure */
  8034. AssertNotNull(tmp_ca = X509_chain_up_ref(ca));
  8035. AssertIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  8036. /* CTX now owns the tmp_ca stack structure */
  8037. tmp_ca = NULL;
  8038. AssertIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  8039. AssertNotNull(tmp_ca);
  8040. AssertIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  8041. /* Check that the main cert is also set */
  8042. AssertNotNull(SSL_CTX_get0_certificate(ctx));
  8043. AssertNotNull(ssl = SSL_new(ctx));
  8044. AssertNotNull(SSL_get_certificate(ssl));
  8045. SSL_free(ssl);
  8046. SSL_CTX_free(ctx);
  8047. #endif
  8048. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8049. /* should be 2 other certs on stack */
  8050. tmp = sk_X509_pop(ca);
  8051. AssertNotNull(tmp);
  8052. X509_free(tmp);
  8053. tmp = sk_X509_pop(ca);
  8054. AssertNotNull(tmp);
  8055. X509_free(tmp);
  8056. AssertNull(sk_X509_pop(ca));
  8057. EVP_PKEY_free(pkey);
  8058. X509_free(cert);
  8059. sk_X509_pop_free(ca, X509_free);
  8060. /* check PKCS12_create */
  8061. AssertNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  8062. AssertIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  8063. SSL_SUCCESS);
  8064. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  8065. -1, -1, 100, -1, 0)));
  8066. EVP_PKEY_free(pkey);
  8067. X509_free(cert);
  8068. sk_X509_pop_free(ca, NULL);
  8069. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  8070. SSL_SUCCESS);
  8071. PKCS12_free(pkcs12_2);
  8072. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  8073. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  8074. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  8075. 2000, 1, 0)));
  8076. EVP_PKEY_free(pkey);
  8077. X509_free(cert);
  8078. sk_X509_pop_free(ca, NULL);
  8079. /* convert to DER then back and parse */
  8080. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  8081. AssertIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  8082. PKCS12_free(pkcs12_2);
  8083. AssertNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  8084. BIO_free(bio);
  8085. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  8086. SSL_SUCCESS);
  8087. /* should be 2 other certs on stack */
  8088. tmp = sk_X509_pop(ca);
  8089. AssertNotNull(tmp);
  8090. X509_free(tmp);
  8091. tmp = sk_X509_pop(ca);
  8092. AssertNotNull(tmp);
  8093. X509_free(tmp);
  8094. AssertNull(sk_X509_pop(ca));
  8095. #ifndef NO_RC4
  8096. PKCS12_free(pkcs12_2);
  8097. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  8098. NID_pbe_WithSHA1And128BitRC4,
  8099. NID_pbe_WithSHA1And128BitRC4,
  8100. 2000, 1, 0)));
  8101. EVP_PKEY_free(pkey);
  8102. X509_free(cert);
  8103. sk_X509_pop_free(ca, NULL);
  8104. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  8105. SSL_SUCCESS);
  8106. #endif /* NO_RC4 */
  8107. EVP_PKEY_free(pkey);
  8108. X509_free(cert);
  8109. PKCS12_free(pkcs12);
  8110. PKCS12_free(pkcs12_2);
  8111. sk_X509_pop_free(ca, NULL);
  8112. #ifdef HAVE_ECC
  8113. /* test order of parsing */
  8114. f = XFOPEN(order, "rb");
  8115. AssertTrue(f != XBADFILE);
  8116. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  8117. XFCLOSE(f);
  8118. AssertNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  8119. AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  8120. AssertIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  8121. WOLFSSL_SUCCESS);
  8122. /* check use of pkey after parse */
  8123. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  8124. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  8125. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8126. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  8127. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  8128. #else
  8129. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  8130. #endif
  8131. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  8132. SSL_CTX_free(ctx);
  8133. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8134. #endif
  8135. AssertNotNull(pkey);
  8136. AssertNotNull(cert);
  8137. AssertNotNull(ca);
  8138. /* compare subject lines of certificates */
  8139. AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  8140. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  8141. SSL_FILETYPE_PEM));
  8142. AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  8143. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  8144. X509_free(x509);
  8145. /* test expected fail case */
  8146. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  8147. SSL_FILETYPE_PEM));
  8148. AssertIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  8149. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  8150. X509_free(x509);
  8151. X509_free(cert);
  8152. /* get subject line from ca stack */
  8153. AssertNotNull(cert = sk_X509_pop(ca));
  8154. AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  8155. /* compare subject from certificate in ca to expected */
  8156. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  8157. SSL_FILETYPE_PEM));
  8158. AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  8159. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  8160. EVP_PKEY_free(pkey);
  8161. X509_free(x509);
  8162. X509_free(cert);
  8163. BIO_free(bio);
  8164. PKCS12_free(pkcs12);
  8165. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  8166. /* test order of parsing */
  8167. f = XFOPEN(file, "rb");
  8168. AssertTrue(f != XBADFILE);
  8169. AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  8170. XFCLOSE(f);
  8171. /* check verify MAC fail case */
  8172. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  8173. AssertIntEQ(ret, 0);
  8174. AssertNull(pkey);
  8175. AssertNull(cert);
  8176. /* check parse with no extra certs kept */
  8177. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  8178. AssertIntEQ(ret, 1);
  8179. AssertNotNull(pkey);
  8180. AssertNotNull(cert);
  8181. wolfSSL_EVP_PKEY_free(pkey);
  8182. wolfSSL_X509_free(cert);
  8183. /* check parse with extra certs kept */
  8184. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  8185. AssertIntEQ(ret, 1);
  8186. AssertNotNull(pkey);
  8187. AssertNotNull(cert);
  8188. AssertNotNull(ca);
  8189. wolfSSL_EVP_PKEY_free(pkey);
  8190. wolfSSL_X509_free(cert);
  8191. sk_X509_pop_free(ca, NULL);
  8192. PKCS12_free(pkcs12);
  8193. #endif /* HAVE_ECC */
  8194. #ifdef WC_RC2
  8195. /* test PKCS#12 with RC2 encryption */
  8196. f = XFOPEN(rc2p12, "rb");
  8197. AssertTrue(f != XBADFILE);
  8198. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  8199. XFCLOSE(f);
  8200. AssertNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  8201. AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  8202. /* check verify MAC fail case */
  8203. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  8204. AssertIntEQ(ret, 0);
  8205. AssertNull(pkey);
  8206. AssertNull(cert);
  8207. /* check parse iwth not extra certs kept */
  8208. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  8209. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  8210. AssertNotNull(pkey);
  8211. AssertNotNull(cert);
  8212. /* check parse with extra certs kept */
  8213. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  8214. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  8215. AssertNotNull(pkey);
  8216. AssertNotNull(cert);
  8217. AssertNotNull(ca);
  8218. wolfSSL_EVP_PKEY_free(pkey);
  8219. wolfSSL_X509_free(cert);
  8220. sk_X509_pop_free(ca, NULL);
  8221. BIO_free(bio);
  8222. PKCS12_free(pkcs12);
  8223. #endif /* WC_RC2 */
  8224. /* Test i2d_PKCS12_bio */
  8225. f = XFOPEN(file, "rb");
  8226. AssertTrue((f != XBADFILE));
  8227. AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  8228. XFCLOSE(f);
  8229. bio = BIO_new(BIO_s_mem());
  8230. AssertNotNull(bio);
  8231. ret = i2d_PKCS12_bio(bio, pkcs12);
  8232. AssertIntEQ(ret, 1);
  8233. ret = i2d_PKCS12_bio(NULL, pkcs12);
  8234. AssertIntEQ(ret, 0);
  8235. ret = i2d_PKCS12_bio(bio, NULL);
  8236. AssertIntEQ(ret, 0);
  8237. PKCS12_free(pkcs12);
  8238. BIO_free(bio);
  8239. (void)order;
  8240. res = TEST_RES_CHECK(1);
  8241. #endif /* OPENSSL_EXTRA */
  8242. #endif /* HAVE_FIPS */
  8243. return res;
  8244. }
  8245. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  8246. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  8247. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  8248. #define TEST_PKCS8_ENC
  8249. #endif
  8250. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  8251. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  8252. /* used to keep track if FailTestCallback was called */
  8253. static int failTestCallbackCalled = 0;
  8254. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  8255. {
  8256. (void)passwd;
  8257. (void)sz;
  8258. (void)rw;
  8259. (void)userdata;
  8260. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  8261. failTestCallbackCalled = 1;
  8262. return -1;
  8263. }
  8264. #endif
  8265. static int test_wolfSSL_no_password_cb(void)
  8266. {
  8267. int res = TEST_SKIPPED;
  8268. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  8269. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  8270. WOLFSSL_CTX* ctx;
  8271. byte buff[FOURK_BUF];
  8272. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  8273. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  8274. XFILE f;
  8275. int bytes;
  8276. #ifndef NO_WOLFSSL_CLIENT
  8277. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  8278. #else
  8279. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  8280. #endif
  8281. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  8282. AssertTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  8283. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8284. XFCLOSE(f);
  8285. AssertIntLE(bytes, sizeof(buff));
  8286. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8287. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8288. AssertTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  8289. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8290. XFCLOSE(f);
  8291. AssertIntLE(bytes, sizeof(buff));
  8292. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8293. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8294. wolfSSL_CTX_free(ctx);
  8295. if (failTestCallbackCalled != 0) {
  8296. Fail(("Password callback should not be called by default"),
  8297. ("Password callback was called without attempting "
  8298. "to first decipher private key without password."));
  8299. }
  8300. res = TEST_RES_CHECK(1);
  8301. #endif
  8302. return res;
  8303. }
  8304. #ifdef TEST_PKCS8_ENC
  8305. /* for PKCS8 test case */
  8306. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  8307. {
  8308. int flag = 0;
  8309. (void)rw;
  8310. if (userdata != NULL) {
  8311. flag = *((int*)userdata); /* user set data */
  8312. }
  8313. switch (flag) {
  8314. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  8315. * can be anything the user wishes to be passed to the callback
  8316. * associated with the WOLFSSL_CTX */
  8317. XSTRNCPY(passwd, "yassl123", sz);
  8318. return 8;
  8319. default:
  8320. return BAD_FUNC_ARG;
  8321. }
  8322. }
  8323. #endif /* TEST_PKCS8_ENC */
  8324. /* Testing functions dealing with PKCS8 */
  8325. static int test_wolfSSL_PKCS8(void)
  8326. {
  8327. int res = TEST_SKIPPED;
  8328. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8)
  8329. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8330. byte buff[FOURK_BUF];
  8331. byte der[FOURK_BUF];
  8332. #ifndef NO_RSA
  8333. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  8334. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  8335. #endif
  8336. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  8337. #ifdef HAVE_ECC
  8338. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  8339. #endif
  8340. XFILE f;
  8341. int bytes;
  8342. WOLFSSL_CTX* ctx;
  8343. #if defined(HAVE_ECC) && !defined(NO_CODING)
  8344. int ret;
  8345. ecc_key key;
  8346. word32 x = 0;
  8347. #endif
  8348. #ifdef TEST_PKCS8_ENC
  8349. #if !defined(NO_RSA) && !defined(NO_SHA)
  8350. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  8351. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  8352. #endif
  8353. #if defined(HAVE_ECC) && !defined(NO_SHA)
  8354. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  8355. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  8356. #endif
  8357. int flag;
  8358. #endif
  8359. (void)der;
  8360. #ifndef NO_WOLFSSL_CLIENT
  8361. #ifndef WOLFSSL_NO_TLS12
  8362. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  8363. #else
  8364. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  8365. #endif
  8366. #else
  8367. #ifndef WOLFSSL_NO_TLS12
  8368. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  8369. #else
  8370. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  8371. #endif
  8372. #endif
  8373. #ifdef TEST_PKCS8_ENC
  8374. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  8375. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  8376. flag = 1; /* used by password callback as return code */
  8377. #if !defined(NO_RSA) && !defined(NO_SHA)
  8378. /* test loading PEM PKCS8 encrypted file */
  8379. f = XFOPEN(serverKeyPkcs8EncPemFile, "rb");
  8380. AssertTrue((f != XBADFILE));
  8381. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8382. XFCLOSE(f);
  8383. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8384. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8385. /* this next case should fail because of password callback return code */
  8386. flag = 0; /* used by password callback as return code */
  8387. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8388. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8389. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  8390. AssertIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  8391. "yassl123"), 0);
  8392. /* test that error value is returned with a bad password */
  8393. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  8394. "bad"), 0);
  8395. /* test loading PEM PKCS8 encrypted file */
  8396. f = XFOPEN(serverKeyPkcs8EncDerFile, "rb");
  8397. AssertTrue((f != XBADFILE));
  8398. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8399. XFCLOSE(f);
  8400. flag = 1; /* used by password callback as return code */
  8401. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8402. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8403. /* this next case should fail because of password callback return code */
  8404. flag = 0; /* used by password callback as return code */
  8405. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8406. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8407. #endif /* !NO_RSA && !NO_SHA */
  8408. #if defined(HAVE_ECC) && !defined(NO_SHA)
  8409. /* test loading PEM PKCS8 encrypted ECC Key file */
  8410. f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb");
  8411. AssertTrue((f != XBADFILE));
  8412. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8413. XFCLOSE(f);
  8414. flag = 1; /* used by password callback as return code */
  8415. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8416. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8417. /* this next case should fail because of password callback return code */
  8418. flag = 0; /* used by password callback as return code */
  8419. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8420. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8421. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  8422. AssertIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  8423. "yassl123"), 0);
  8424. /* test that error value is returned with a bad password */
  8425. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  8426. "bad"), 0);
  8427. /* test loading DER PKCS8 encrypted ECC Key file */
  8428. f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb");
  8429. AssertTrue((f != XBADFILE));
  8430. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8431. XFCLOSE(f);
  8432. flag = 1; /* used by password callback as return code */
  8433. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8434. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8435. /* this next case should fail because of password callback return code */
  8436. flag = 0; /* used by password callback as return code */
  8437. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8438. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8439. /* leave flag as "okay" */
  8440. flag = 1;
  8441. #endif /* HAVE_ECC && !NO_SHA */
  8442. #endif /* TEST_PKCS8_ENC */
  8443. #ifndef NO_RSA
  8444. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  8445. f = XFOPEN(serverKeyPkcs8DerFile, "rb");
  8446. AssertTrue((f != XBADFILE));
  8447. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8448. XFCLOSE(f);
  8449. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8450. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8451. /* test loading PEM PKCS8 private key file (not encrypted) */
  8452. f = XFOPEN(serverKeyPkcs8PemFile, "rb");
  8453. AssertTrue((f != XBADFILE));
  8454. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8455. XFCLOSE(f);
  8456. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8457. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8458. #endif /* !NO_RSA */
  8459. /* Test PKCS8 PEM ECC key no crypt */
  8460. f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb");
  8461. AssertTrue((f != XBADFILE));
  8462. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8463. XFCLOSE(f);
  8464. #ifdef HAVE_ECC
  8465. /* Test PKCS8 PEM ECC key no crypt */
  8466. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8467. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8468. #ifndef NO_CODING
  8469. /* decrypt PKCS8 PEM to key in DER format */
  8470. AssertIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  8471. (word32)sizeof(der), NULL)), 0);
  8472. ret = wc_ecc_init(&key);
  8473. if (ret == 0) {
  8474. ret = wc_EccPrivateKeyDecode(der, &x, &key, bytes);
  8475. wc_ecc_free(&key);
  8476. }
  8477. AssertIntEQ(ret, 0);
  8478. #endif
  8479. /* Test PKCS8 DER ECC key no crypt */
  8480. f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb");
  8481. AssertTrue((f != XBADFILE));
  8482. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8483. XFCLOSE(f);
  8484. /* Test using a PKCS8 ECC PEM */
  8485. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8486. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8487. #else
  8488. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  8489. AssertIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  8490. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  8491. #endif /* HAVE_ECC */
  8492. wolfSSL_CTX_free(ctx);
  8493. res = TEST_RES_CHECK(1);
  8494. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8495. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  8496. return res;
  8497. }
  8498. static int test_wolfSSL_PKCS8_ED25519(void)
  8499. {
  8500. int res = TEST_SKIPPED;
  8501. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  8502. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  8503. defined(HAVE_ED25519_KEY_IMPORT)
  8504. const byte encPrivKey[] = \
  8505. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  8506. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  8507. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  8508. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  8509. "2L9W4v7swXkV+X+a1ww=\n"
  8510. "-----END ENCRYPTED PRIVATE KEY-----\n";
  8511. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  8512. byte der[FOURK_BUF];
  8513. WOLFSSL_CTX* ctx;
  8514. int bytes;
  8515. XMEMSET(der, 0, sizeof(der));
  8516. AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  8517. (word32)sizeof(der), password)), 0);
  8518. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8519. #ifndef NO_WOLFSSL_SERVER
  8520. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  8521. #else
  8522. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  8523. #endif
  8524. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  8525. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8526. wolfSSL_CTX_free(ctx);
  8527. res = TEST_RES_CHECK(1);
  8528. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8529. #endif
  8530. return res;
  8531. }
  8532. static int test_wolfSSL_PKCS8_ED448(void)
  8533. {
  8534. int res = TEST_SKIPPED;
  8535. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  8536. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  8537. defined(HAVE_ED448_KEY_IMPORT)
  8538. const byte encPrivKey[] = \
  8539. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  8540. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  8541. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  8542. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  8543. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  8544. "-----END ENCRYPTED PRIVATE KEY-----\n";
  8545. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  8546. byte der[FOURK_BUF];
  8547. WOLFSSL_CTX* ctx;
  8548. int bytes;
  8549. XMEMSET(der, 0, sizeof(der));
  8550. AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  8551. (word32)sizeof(der), password)), 0);
  8552. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8553. #ifndef NO_WOLFSSL_SERVER
  8554. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  8555. #else
  8556. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  8557. #endif
  8558. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  8559. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8560. wolfSSL_CTX_free(ctx);
  8561. res = TEST_RES_CHECK(1);
  8562. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8563. #endif
  8564. return res;
  8565. }
  8566. /* Testing functions dealing with PKCS5 */
  8567. static int test_wolfSSL_PKCS5(void)
  8568. {
  8569. int res = TEST_SKIPPED;
  8570. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  8571. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  8572. const char* passwd = "myfipsPa$$W0rd";
  8573. #else
  8574. const char *passwd = "pass1234";
  8575. #endif
  8576. const unsigned char *salt = (unsigned char *)"salt1234";
  8577. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  8578. DYNAMIC_TYPE_TMP_BUFFER);
  8579. int ret = 0;
  8580. AssertNotNull(out);
  8581. ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  8582. (int)XSTRLEN((const char *) salt), 10,
  8583. WC_SHA_DIGEST_SIZE,out);
  8584. AssertIntEQ(ret, SSL_SUCCESS);
  8585. #ifdef WOLFSSL_SHA512
  8586. ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  8587. (int)XSTRLEN((const char *) salt), 10,
  8588. wolfSSL_EVP_sha512(), WC_SHA_DIGEST_SIZE, out);
  8589. AssertIntEQ(ret, SSL_SUCCESS);
  8590. #endif
  8591. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8592. res = TEST_RES_CHECK(1);
  8593. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  8594. return res;
  8595. }
  8596. /* test parsing URI from certificate */
  8597. static int test_wolfSSL_URI(void)
  8598. {
  8599. int res = TEST_SKIPPED;
  8600. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  8601. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  8602. defined(OPENSSL_EXTRA))
  8603. WOLFSSL_X509* x509;
  8604. const char uri[] = "./certs/client-uri-cert.pem";
  8605. const char badUri[] = "./certs/client-relative-uri.pem";
  8606. x509 = wolfSSL_X509_load_certificate_file(uri, WOLFSSL_FILETYPE_PEM);
  8607. AssertNotNull(x509);
  8608. wolfSSL_FreeX509(x509);
  8609. x509 = wolfSSL_X509_load_certificate_file(badUri, WOLFSSL_FILETYPE_PEM);
  8610. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT) \
  8611. && !defined(WOLFSSL_FPKI)
  8612. AssertNull(x509);
  8613. #else
  8614. AssertNotNull(x509);
  8615. wolfSSL_FreeX509(x509);
  8616. #endif
  8617. res = TEST_RES_CHECK(1);
  8618. #endif
  8619. return res;
  8620. }
  8621. static int test_wolfSSL_TBS(void)
  8622. {
  8623. int res = TEST_SKIPPED;
  8624. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  8625. && defined(OPENSSL_EXTRA)
  8626. WOLFSSL_X509* x509;
  8627. const unsigned char* tbs;
  8628. int tbsSz;
  8629. AssertNotNull(x509 =
  8630. wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
  8631. AssertNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  8632. AssertNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  8633. AssertNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  8634. AssertIntEQ(tbsSz, 1003);
  8635. wolfSSL_FreeX509(x509);
  8636. res = TEST_RES_CHECK(1);
  8637. #endif
  8638. return res;
  8639. }
  8640. static int test_wolfSSL_X509_verify(void)
  8641. {
  8642. int res = TEST_SKIPPED;
  8643. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  8644. && defined(OPENSSL_EXTRA)
  8645. WOLFSSL_X509* ca;
  8646. WOLFSSL_X509* serv;
  8647. WOLFSSL_EVP_PKEY* pkey;
  8648. unsigned char buf[2048];
  8649. const unsigned char* pt = NULL;
  8650. int bufSz;
  8651. AssertNotNull(ca =
  8652. wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
  8653. AssertIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  8654. WOLFSSL_SUCCESS);
  8655. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  8656. WOLFSSL_SUCCESS);
  8657. AssertIntEQ(bufSz, 294);
  8658. bufSz = 2048;
  8659. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  8660. WOLFSSL_SUCCESS);
  8661. AssertIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  8662. AssertIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  8663. AssertNotNull(serv =
  8664. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM));
  8665. /* success case */
  8666. pt = buf;
  8667. AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  8668. AssertIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  8669. AssertIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  8670. wolfSSL_EVP_PKEY_free(pkey);
  8671. /* fail case */
  8672. bufSz = 2048;
  8673. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  8674. WOLFSSL_SUCCESS);
  8675. pt = buf;
  8676. AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  8677. AssertIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  8678. AssertIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  8679. AssertIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  8680. wolfSSL_EVP_PKEY_free(pkey);
  8681. wolfSSL_FreeX509(ca);
  8682. wolfSSL_FreeX509(serv);
  8683. res = TEST_RES_CHECK(1);
  8684. #endif
  8685. return res;
  8686. }
  8687. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  8688. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  8689. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  8690. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  8691. /* create certificate with version 2 */
  8692. static void test_set_x509_badversion(WOLFSSL_CTX* ctx)
  8693. {
  8694. WOLFSSL_X509 *x509, *x509v2;
  8695. WOLFSSL_EVP_PKEY *priv, *pub;
  8696. unsigned char *der = NULL, *key = NULL, *pt;
  8697. char *header, *name;
  8698. int derSz;
  8699. long keySz;
  8700. XFILE fp;
  8701. WOLFSSL_ASN1_TIME *notBefore, *notAfter;
  8702. time_t t;
  8703. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  8704. WOLFSSL_FILETYPE_PEM));
  8705. fp = XFOPEN(cliKeyFile, "rb");
  8706. AssertIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  8707. WOLFSSL_SUCCESS);
  8708. XFCLOSE(fp);
  8709. pt = key;
  8710. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  8711. (const unsigned char**)&pt, keySz));
  8712. /* create the version 2 certificate */
  8713. AssertNotNull(x509v2 = X509_new());
  8714. AssertIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  8715. AssertIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  8716. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  8717. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  8718. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  8719. AssertNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  8720. AssertIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  8721. t = time(NULL);
  8722. AssertNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  8723. AssertNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  8724. AssertTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  8725. AssertTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  8726. AssertIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  8727. derSz = wolfSSL_i2d_X509(x509v2, &der);
  8728. AssertIntGT(derSz, 0);
  8729. AssertIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  8730. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8731. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL); /* TODO: Replace with API call */
  8732. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8733. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8734. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8735. wolfSSL_X509_free(x509);
  8736. wolfSSL_X509_free(x509v2);
  8737. wolfSSL_EVP_PKEY_free(priv);
  8738. wolfSSL_EVP_PKEY_free(pub);
  8739. wolfSSL_ASN1_TIME_free(notBefore);
  8740. wolfSSL_ASN1_TIME_free(notAfter);
  8741. }
  8742. /* override certificate version error */
  8743. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  8744. {
  8745. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  8746. AssertIntEQ(store->error, ASN_VERSION_E);
  8747. #else
  8748. AssertIntEQ(store->error, 0);
  8749. #endif
  8750. AssertIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  8751. (void)preverify;
  8752. return 1;
  8753. }
  8754. /* set verify callback that will override bad certificate version */
  8755. static void test_set_override_x509(WOLFSSL_CTX* ctx)
  8756. {
  8757. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  8758. }
  8759. #endif
  8760. static int test_wolfSSL_X509_TLS_version(void)
  8761. {
  8762. int res = TEST_SKIPPED;
  8763. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  8764. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  8765. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  8766. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  8767. tcp_ready ready;
  8768. func_args server_args;
  8769. func_args client_args;
  8770. THREAD_TYPE serverThread;
  8771. callback_functions func_cb_client;
  8772. callback_functions func_cb_server;
  8773. /* test server rejects a client certificate that is not version 3 */
  8774. #ifdef WOLFSSL_TIRTOS
  8775. fdOpenSession(Task_self());
  8776. #endif
  8777. XMEMSET(&server_args, 0, sizeof(func_args));
  8778. XMEMSET(&client_args, 0, sizeof(func_args));
  8779. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  8780. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  8781. StartTCP();
  8782. InitTcpReady(&ready);
  8783. #if defined(USE_WINDOWS_API)
  8784. /* use RNG to get random port if using windows */
  8785. ready.port = GetRandomPort();
  8786. #endif
  8787. server_args.signal = &ready;
  8788. client_args.signal = &ready;
  8789. server_args.return_code = TEST_FAIL;
  8790. client_args.return_code = TEST_FAIL;
  8791. func_cb_client.ctx_ready = &test_set_x509_badversion;
  8792. #ifndef WOLFSSL_NO_TLS12
  8793. func_cb_client.method = wolfTLSv1_2_client_method;
  8794. #else
  8795. func_cb_client.method = wolfTLSv1_3_client_method;
  8796. #endif
  8797. client_args.callbacks = &func_cb_client;
  8798. #ifndef WOLFSSL_NO_TLS12
  8799. func_cb_server.method = wolfTLSv1_2_server_method;
  8800. #else
  8801. func_cb_server.method = wolfTLSv1_3_server_method;
  8802. #endif
  8803. server_args.callbacks = &func_cb_server;
  8804. start_thread(test_server_nofail, &server_args, &serverThread);
  8805. wait_tcp_ready(&server_args);
  8806. test_client_nofail(&client_args, NULL);
  8807. join_thread(serverThread);
  8808. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  8809. AssertIntEQ(client_args.return_code, TEST_FAIL);
  8810. AssertIntEQ(server_args.return_code, TEST_FAIL);
  8811. #else
  8812. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  8813. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  8814. #endif
  8815. FreeTcpReady(&ready);
  8816. #ifdef WOLFSSL_TIRTOS
  8817. fdCloseSession(Task_self());
  8818. #endif
  8819. /* Now re run but override the bad X509 version */
  8820. #ifdef WOLFSSL_TIRTOS
  8821. fdOpenSession(Task_self());
  8822. #endif
  8823. XMEMSET(&server_args, 0, sizeof(func_args));
  8824. XMEMSET(&client_args, 0, sizeof(func_args));
  8825. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  8826. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  8827. StartTCP();
  8828. InitTcpReady(&ready);
  8829. #if defined(USE_WINDOWS_API)
  8830. /* use RNG to get random port if using windows */
  8831. ready.port = GetRandomPort();
  8832. #endif
  8833. server_args.signal = &ready;
  8834. client_args.signal = &ready;
  8835. server_args.return_code = TEST_FAIL;
  8836. client_args.return_code = TEST_FAIL;
  8837. func_cb_client.ctx_ready = &test_set_x509_badversion;
  8838. func_cb_server.ctx_ready = &test_set_override_x509;
  8839. #ifndef WOLFSSL_NO_TLS12
  8840. func_cb_client.method = wolfTLSv1_2_client_method;
  8841. #else
  8842. func_cb_client.method = wolfTLSv1_3_client_method;
  8843. #endif
  8844. client_args.callbacks = &func_cb_client;
  8845. #ifndef WOLFSSL_NO_TLS12
  8846. func_cb_server.method = wolfTLSv1_2_server_method;
  8847. #else
  8848. func_cb_server.method = wolfTLSv1_3_server_method;
  8849. #endif
  8850. server_args.callbacks = &func_cb_server;
  8851. start_thread(test_server_nofail, &server_args, &serverThread);
  8852. wait_tcp_ready(&server_args);
  8853. test_client_nofail(&client_args, NULL);
  8854. join_thread(serverThread);
  8855. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  8856. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  8857. FreeTcpReady(&ready);
  8858. #ifdef WOLFSSL_TIRTOS
  8859. fdCloseSession(Task_self());
  8860. #endif
  8861. res = TEST_RES_CHECK(1);
  8862. #endif
  8863. return res;
  8864. }
  8865. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  8866. * version allowed.
  8867. * POST: 1 on success.
  8868. */
  8869. static int test_wolfSSL_CTX_SetMinVersion(void)
  8870. {
  8871. int res = TEST_SKIPPED;
  8872. #ifndef NO_WOLFSSL_CLIENT
  8873. int failFlag = WOLFSSL_SUCCESS;
  8874. WOLFSSL_CTX* ctx;
  8875. int itr;
  8876. #ifndef NO_OLD_TLS
  8877. const int versions[] = {
  8878. #ifdef WOLFSSL_ALLOW_TLSV10
  8879. WOLFSSL_TLSV1,
  8880. #endif
  8881. WOLFSSL_TLSV1_1,
  8882. WOLFSSL_TLSV1_2 };
  8883. #elif !defined(WOLFSSL_NO_TLS12)
  8884. const int versions[] = { WOLFSSL_TLSV1_2 };
  8885. #elif defined(WOLFSSL_TLS13)
  8886. const int versions[] = { WOLFSSL_TLSV1_3 };
  8887. #else
  8888. const int versions[0];
  8889. #endif
  8890. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8891. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  8892. if (wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr))
  8893. != WOLFSSL_SUCCESS) {
  8894. failFlag = WOLFSSL_FAILURE;
  8895. }
  8896. }
  8897. wolfSSL_CTX_free(ctx);
  8898. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  8899. #endif
  8900. return res;
  8901. } /* END test_wolfSSL_CTX_SetMinVersion */
  8902. /*----------------------------------------------------------------------------*
  8903. | OCSP Stapling
  8904. *----------------------------------------------------------------------------*/
  8905. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  8906. * need to contact the CA, lowering the cost of cert revocation checking.
  8907. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  8908. * POST: 1 returned for success.
  8909. */
  8910. static int test_wolfSSL_UseOCSPStapling(void)
  8911. {
  8912. int res = TEST_SKIPPED;
  8913. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  8914. !defined(NO_WOLFSSL_CLIENT)
  8915. int ret;
  8916. WOLFSSL_CTX* ctx;
  8917. WOLFSSL* ssl;
  8918. #ifndef NO_WOLFSSL_CLIENT
  8919. #ifndef WOLFSSL_NO_TLS12
  8920. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  8921. #else
  8922. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  8923. #endif
  8924. #else
  8925. #ifndef WOLFSSL_NO_TLS12
  8926. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  8927. #else
  8928. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  8929. #endif
  8930. #endif
  8931. ssl = wolfSSL_new(ctx);
  8932. ret = wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  8933. WOLFSSL_CSR2_OCSP_USE_NONCE);
  8934. wolfSSL_free(ssl);
  8935. wolfSSL_CTX_free(ctx);
  8936. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  8937. #endif
  8938. return res;
  8939. } /*END test_wolfSSL_UseOCSPStapling */
  8940. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  8941. * stapling eliminates the need to contact the CA and lowers cert revocation
  8942. * check.
  8943. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  8944. */
  8945. static int test_wolfSSL_UseOCSPStaplingV2(void)
  8946. {
  8947. int res = TEST_SKIPPED;
  8948. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  8949. !defined(NO_WOLFSSL_CLIENT)
  8950. int ret;
  8951. WOLFSSL_CTX* ctx;
  8952. WOLFSSL* ssl;
  8953. #ifndef NO_WOLFSSL_CLIENT
  8954. #ifndef WOLFSSL_NO_TLS12
  8955. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  8956. #else
  8957. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  8958. #endif
  8959. #else
  8960. #ifndef WOLFSSL_NO_TLS12
  8961. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  8962. #else
  8963. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  8964. #endif
  8965. #endif
  8966. ssl = wolfSSL_new(ctx);
  8967. ret = wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  8968. WOLFSSL_CSR2_OCSP_USE_NONCE );
  8969. wolfSSL_free(ssl);
  8970. wolfSSL_CTX_free(ctx);
  8971. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  8972. #endif
  8973. return res;
  8974. } /*END test_wolfSSL_UseOCSPStaplingV2*/
  8975. /*----------------------------------------------------------------------------*
  8976. | Multicast Tests
  8977. *----------------------------------------------------------------------------*/
  8978. static int test_wolfSSL_mcast(void)
  8979. {
  8980. int res = TEST_SKIPPED;
  8981. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  8982. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  8983. WOLFSSL_CTX* ctx;
  8984. WOLFSSL* ssl;
  8985. int result;
  8986. byte preMasterSecret[512];
  8987. byte clientRandom[32];
  8988. byte serverRandom[32];
  8989. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  8990. byte buf[256];
  8991. word16 newId;
  8992. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method());
  8993. AssertNotNull(ctx);
  8994. result = wolfSSL_CTX_mcast_set_member_id(ctx, 0);
  8995. AssertIntEQ(result, WOLFSSL_SUCCESS);
  8996. ssl = wolfSSL_new(ctx);
  8997. AssertNotNull(ssl);
  8998. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  8999. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  9000. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  9001. result = wolfSSL_set_secret(ssl, 23,
  9002. preMasterSecret, sizeof(preMasterSecret),
  9003. clientRandom, serverRandom, suite);
  9004. AssertIntEQ(result, WOLFSSL_SUCCESS);
  9005. result = wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf));
  9006. AssertIntLE(result, 0);
  9007. AssertIntLE(newId, 100);
  9008. wolfSSL_free(ssl);
  9009. wolfSSL_CTX_free(ctx);
  9010. res = TEST_RES_CHECK(1);
  9011. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 ||
  9012. * WOLFSSL_SNIFFER) */
  9013. return res;
  9014. }
  9015. /*----------------------------------------------------------------------------*
  9016. | Wolfcrypt
  9017. *----------------------------------------------------------------------------*/
  9018. /*
  9019. * Unit test for the wc_InitBlake2b()
  9020. */
  9021. static int test_wc_InitBlake2b(void)
  9022. {
  9023. int res = TEST_SKIPPED;
  9024. #ifdef HAVE_BLAKE2
  9025. Blake2b blake;
  9026. int ret = 0;
  9027. /* Test good arg. */
  9028. ret = wc_InitBlake2b(&blake, 64);
  9029. if (ret != 0) {
  9030. ret = WOLFSSL_FATAL_ERROR;
  9031. }
  9032. /* Test bad arg. */
  9033. if (!ret) {
  9034. ret = wc_InitBlake2b(NULL, 64);
  9035. if (ret == 0) {
  9036. ret = WOLFSSL_FATAL_ERROR;
  9037. }
  9038. else {
  9039. ret = 0;
  9040. }
  9041. }
  9042. if (!ret) {
  9043. ret = wc_InitBlake2b(NULL, 128);
  9044. if (ret == 0) {
  9045. ret = WOLFSSL_FATAL_ERROR;
  9046. }
  9047. else {
  9048. ret = 0;
  9049. }
  9050. }
  9051. if (!ret) {
  9052. ret = wc_InitBlake2b(&blake, 128);
  9053. if (ret == 0) {
  9054. ret = WOLFSSL_FATAL_ERROR;
  9055. }
  9056. else {
  9057. ret = 0;
  9058. }
  9059. }
  9060. if (!ret) {
  9061. ret = wc_InitBlake2b(NULL, 0);
  9062. if (ret == 0) {
  9063. ret = WOLFSSL_FATAL_ERROR;
  9064. }
  9065. else {
  9066. ret = 0;
  9067. }
  9068. }
  9069. if (!ret) {
  9070. ret = wc_InitBlake2b(&blake, 0);
  9071. if (ret == 0) {
  9072. ret = WOLFSSL_FATAL_ERROR;
  9073. }
  9074. else {
  9075. ret = 0;
  9076. }
  9077. }
  9078. res = TEST_RES_CHECK(ret == 0);
  9079. #endif
  9080. return res;
  9081. } /*END test_wc_InitBlake2b*/
  9082. /*
  9083. * Unit test for the wc_InitBlake2b_WithKey()
  9084. */
  9085. static int test_wc_InitBlake2b_WithKey(void)
  9086. {
  9087. int res = TEST_SKIPPED;
  9088. #ifdef HAVE_BLAKE2
  9089. Blake2b blake;
  9090. word32 digestSz = BLAKE2B_KEYBYTES;
  9091. byte key[BLAKE2B_KEYBYTES];
  9092. word32 keylen = BLAKE2B_KEYBYTES;
  9093. int ret = 0;
  9094. XMEMSET(key, 0, sizeof(key));
  9095. /* Test good arg. */
  9096. ret = wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen);
  9097. if (ret != 0) {
  9098. ret = WOLFSSL_FATAL_ERROR;
  9099. }
  9100. /* Test bad args. */
  9101. if (ret == 0) {
  9102. ret = wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen);
  9103. if (ret == BAD_FUNC_ARG) {
  9104. ret = 0;
  9105. }
  9106. }
  9107. if (ret == 0) {
  9108. ret = wc_InitBlake2b_WithKey(&blake, digestSz, key, 256);
  9109. if (ret == BAD_FUNC_ARG) {
  9110. ret = 0;
  9111. }
  9112. }
  9113. if (ret == 0) {
  9114. ret = wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen);
  9115. }
  9116. res = TEST_RES_CHECK(ret == 0);
  9117. #endif
  9118. return res;
  9119. } /*END wc_InitBlake2b_WithKey*/
  9120. /*
  9121. * Unit test for the wc_InitBlake2s_WithKey()
  9122. */
  9123. static int test_wc_InitBlake2s_WithKey(void)
  9124. {
  9125. int res = TEST_SKIPPED;
  9126. #ifdef HAVE_BLAKE2S
  9127. Blake2s blake;
  9128. word32 digestSz = BLAKE2S_KEYBYTES;
  9129. byte *key = (byte*)"01234567890123456789012345678901";
  9130. word32 keylen = BLAKE2S_KEYBYTES;
  9131. int ret = 0;
  9132. /* Test good arg. */
  9133. ret = wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen);
  9134. if (ret != 0) {
  9135. ret = WOLFSSL_FATAL_ERROR;
  9136. }
  9137. /* Test bad args. */
  9138. if (ret == 0) {
  9139. ret = wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen);
  9140. if (ret == BAD_FUNC_ARG) {
  9141. ret = 0;
  9142. }
  9143. }
  9144. if (ret == 0) {
  9145. ret = wc_InitBlake2s_WithKey(&blake, digestSz, key, 256);
  9146. if (ret == BAD_FUNC_ARG) {
  9147. ret = 0;
  9148. }
  9149. }
  9150. if (ret == 0) {
  9151. ret = wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen);
  9152. }
  9153. res = TEST_RES_CHECK(ret == 0);
  9154. #endif
  9155. return res;
  9156. } /*END wc_InitBlake2s_WithKey*/
  9157. /*
  9158. * Unit test for the wc_InitMd5()
  9159. */
  9160. static int test_wc_InitMd5(void)
  9161. {
  9162. int res = TEST_SKIPPED;
  9163. #ifndef NO_MD5
  9164. wc_Md5 md5;
  9165. int ret;
  9166. int flag = 0;
  9167. /* Test good arg. */
  9168. ret = wc_InitMd5(&md5);
  9169. if (ret != 0) {
  9170. flag = WOLFSSL_FATAL_ERROR;
  9171. }
  9172. /* Test bad arg. */
  9173. if (!flag) {
  9174. ret = wc_InitMd5(NULL);
  9175. if (ret != BAD_FUNC_ARG) {
  9176. flag = WOLFSSL_FATAL_ERROR;
  9177. }
  9178. }
  9179. wc_Md5Free(&md5);
  9180. res = TEST_RES_CHECK(flag == 0);
  9181. #endif
  9182. return res;
  9183. } /* END test_wc_InitMd5 */
  9184. /*
  9185. * Testing wc_UpdateMd5()
  9186. */
  9187. static int test_wc_Md5Update(void)
  9188. {
  9189. int res = TEST_SKIPPED;
  9190. #ifndef NO_MD5
  9191. wc_Md5 md5;
  9192. byte hash[WC_MD5_DIGEST_SIZE];
  9193. testVector a, b, c;
  9194. int ret;
  9195. int flag = 0;
  9196. ret = wc_InitMd5(&md5);
  9197. if (ret != 0) {
  9198. flag = ret;
  9199. }
  9200. /* Input */
  9201. if (!flag) {
  9202. a.input = "a";
  9203. a.inLen = XSTRLEN(a.input);
  9204. ret = wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen);
  9205. if (ret != 0) {
  9206. flag = ret;
  9207. }
  9208. }
  9209. if (!flag) {
  9210. ret = wc_Md5Final(&md5, hash);
  9211. if (ret != 0) {
  9212. flag = ret;
  9213. }
  9214. }
  9215. /* Update input. */
  9216. if (!flag) {
  9217. a.input = "abc";
  9218. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  9219. "\x72";
  9220. a.inLen = XSTRLEN(a.input);
  9221. a.outLen = XSTRLEN(a.output);
  9222. ret = wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen);
  9223. if (ret != 0) {
  9224. flag = ret;
  9225. }
  9226. }
  9227. if (!flag) {
  9228. ret = wc_Md5Final(&md5, hash);
  9229. if (ret != 0) {
  9230. flag = ret;
  9231. }
  9232. }
  9233. if (!flag) {
  9234. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  9235. flag = WOLFSSL_FATAL_ERROR;
  9236. }
  9237. }
  9238. /*Pass in bad values. */
  9239. if (!flag) {
  9240. b.input = NULL;
  9241. b.inLen = 0;
  9242. ret = wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen);
  9243. if (ret != 0) {
  9244. flag = ret;
  9245. }
  9246. }
  9247. if (!flag) {
  9248. c.input = NULL;
  9249. c.inLen = WC_MD5_DIGEST_SIZE;
  9250. ret = wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen);
  9251. if (ret != BAD_FUNC_ARG) {
  9252. flag = WOLFSSL_FATAL_ERROR;
  9253. }
  9254. }
  9255. if (!flag) {
  9256. ret = wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen);
  9257. if (ret != BAD_FUNC_ARG) {
  9258. flag = WOLFSSL_FATAL_ERROR;
  9259. }
  9260. }
  9261. wc_Md5Free(&md5);
  9262. res = TEST_RES_CHECK(flag == 0);
  9263. #endif
  9264. return res;
  9265. } /* END test_wc_Md5Update() */
  9266. /*
  9267. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  9268. */
  9269. static int test_wc_Md5Final(void)
  9270. {
  9271. int res = TEST_SKIPPED;
  9272. #ifndef NO_MD5
  9273. /* Instantiate */
  9274. wc_Md5 md5;
  9275. byte* hash_test[3];
  9276. byte hash1[WC_MD5_DIGEST_SIZE];
  9277. byte hash2[2*WC_MD5_DIGEST_SIZE];
  9278. byte hash3[5*WC_MD5_DIGEST_SIZE];
  9279. int times, i, ret;
  9280. int flag = 0;
  9281. /* Initialize */
  9282. ret = wc_InitMd5(&md5);
  9283. if (ret != 0) {
  9284. flag = ret;
  9285. }
  9286. if (!flag) {
  9287. hash_test[0] = hash1;
  9288. hash_test[1] = hash2;
  9289. hash_test[2] = hash3;
  9290. }
  9291. times = sizeof(hash_test)/sizeof(byte*);
  9292. for (i = 0; i < times; i++) {
  9293. if (!flag) {
  9294. ret = wc_Md5Final(&md5, hash_test[i]);
  9295. if (ret != 0) {
  9296. flag = WOLFSSL_FATAL_ERROR;
  9297. }
  9298. }
  9299. }
  9300. /* Test bad args. */
  9301. if (!flag) {
  9302. ret = wc_Md5Final(NULL, NULL);
  9303. if (ret != BAD_FUNC_ARG) {
  9304. flag = WOLFSSL_FATAL_ERROR;
  9305. }
  9306. }
  9307. if (!flag) {
  9308. ret = wc_Md5Final(NULL, hash1);
  9309. if (ret != BAD_FUNC_ARG) {
  9310. flag = WOLFSSL_FATAL_ERROR;
  9311. }
  9312. }
  9313. if (!flag) {
  9314. ret = wc_Md5Final(&md5, NULL);
  9315. if (ret != BAD_FUNC_ARG) {
  9316. flag = WOLFSSL_FATAL_ERROR;
  9317. }
  9318. }
  9319. wc_Md5Free(&md5);
  9320. res = TEST_RES_CHECK(flag == 0);
  9321. #endif
  9322. return res;
  9323. }
  9324. /*
  9325. * Unit test for the wc_InitSha()
  9326. */
  9327. static int test_wc_InitSha(void)
  9328. {
  9329. int res = TEST_SKIPPED;
  9330. #ifndef NO_SHA
  9331. wc_Sha sha;
  9332. int ret;
  9333. int flag = 0;
  9334. /* Test good arg. */
  9335. ret = wc_InitSha(&sha);
  9336. if (ret != 0) {
  9337. flag = WOLFSSL_FATAL_ERROR;
  9338. }
  9339. /* Test bad arg. */
  9340. if (!flag) {
  9341. ret = wc_InitSha(NULL);
  9342. if (ret != BAD_FUNC_ARG) {
  9343. flag = WOLFSSL_FATAL_ERROR;
  9344. }
  9345. }
  9346. wc_ShaFree(&sha);
  9347. res = TEST_RES_CHECK(flag == 0);
  9348. #endif
  9349. return res;
  9350. } /* END test_wc_InitSha */
  9351. /*
  9352. * Tesing wc_ShaUpdate()
  9353. */
  9354. static int test_wc_ShaUpdate(void)
  9355. {
  9356. int res = TEST_SKIPPED;
  9357. #ifndef NO_SHA
  9358. wc_Sha sha;
  9359. byte hash[WC_SHA_DIGEST_SIZE];
  9360. testVector a, b, c;
  9361. int flag = 0;
  9362. int ret;
  9363. ret = wc_InitSha(&sha);
  9364. if (ret != 0) {
  9365. flag = ret;
  9366. }
  9367. /* Input. */
  9368. if (!flag) {
  9369. a.input = "a";
  9370. a.inLen = XSTRLEN(a.input);
  9371. ret = wc_ShaUpdate(&sha, NULL, 0);
  9372. if (ret != 0) {
  9373. flag = ret;
  9374. }
  9375. ret = wc_ShaUpdate(&sha, (byte*)a.input, 0);
  9376. if (ret != 0) {
  9377. flag = ret;
  9378. }
  9379. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  9380. if (ret != 0) {
  9381. flag = ret;
  9382. }
  9383. }
  9384. if (!flag) {
  9385. ret = wc_ShaFinal(&sha, hash);
  9386. if (ret != 0) {
  9387. flag = ret;
  9388. }
  9389. }
  9390. /* Update input. */
  9391. if (!flag) {
  9392. a.input = "abc";
  9393. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  9394. "\x6C\x9C\xD0\xD8\x9D";
  9395. a.inLen = XSTRLEN(a.input);
  9396. a.outLen = XSTRLEN(a.output);
  9397. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  9398. if (ret != 0) {
  9399. flag = ret;
  9400. }
  9401. }
  9402. if (!flag) {
  9403. ret = wc_ShaFinal(&sha, hash);
  9404. if (ret !=0) {
  9405. flag = ret;
  9406. }
  9407. }
  9408. if (!flag) {
  9409. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  9410. flag = WOLFSSL_FATAL_ERROR;
  9411. }
  9412. }
  9413. /* Try passing in bad values. */
  9414. if (!flag) {
  9415. b.input = NULL;
  9416. b.inLen = 0;
  9417. ret = wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen);
  9418. if (ret != 0) {
  9419. flag = ret;
  9420. }
  9421. }
  9422. if (!flag) {
  9423. c.input = NULL;
  9424. c.inLen = WC_SHA_DIGEST_SIZE;
  9425. ret = wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen);
  9426. if (ret != BAD_FUNC_ARG) {
  9427. flag = WOLFSSL_FATAL_ERROR;
  9428. }
  9429. }
  9430. if (!flag) {
  9431. ret = wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  9432. if (ret != BAD_FUNC_ARG) {
  9433. flag = WOLFSSL_FATAL_ERROR;
  9434. }
  9435. }
  9436. wc_ShaFree(&sha);
  9437. res = TEST_RES_CHECK(flag == 0);
  9438. #endif
  9439. return res;
  9440. } /* END test_wc_ShaUpdate() */
  9441. /*
  9442. * Unit test on wc_ShaFinal
  9443. */
  9444. static int test_wc_ShaFinal(void)
  9445. {
  9446. int res = TEST_SKIPPED;
  9447. #ifndef NO_SHA
  9448. wc_Sha sha;
  9449. byte* hash_test[3];
  9450. byte hash1[WC_SHA_DIGEST_SIZE];
  9451. byte hash2[2*WC_SHA_DIGEST_SIZE];
  9452. byte hash3[5*WC_SHA_DIGEST_SIZE];
  9453. int times, i, ret;
  9454. int flag = 0;
  9455. /*Initialize*/
  9456. ret = wc_InitSha(&sha);
  9457. if (ret) {
  9458. flag = ret;
  9459. }
  9460. if (!flag) {
  9461. hash_test[0] = hash1;
  9462. hash_test[1] = hash2;
  9463. hash_test[2] = hash3;
  9464. }
  9465. times = sizeof(hash_test)/sizeof(byte*);
  9466. for (i = 0; i < times; i++) {
  9467. if (!flag) {
  9468. ret = wc_ShaFinal(&sha, hash_test[i]);
  9469. if (ret != 0) {
  9470. flag = WOLFSSL_FATAL_ERROR;
  9471. }
  9472. }
  9473. }
  9474. /* Test bad args. */
  9475. if (!flag) {
  9476. ret = wc_ShaFinal(NULL, NULL);
  9477. if (ret != BAD_FUNC_ARG) {
  9478. flag = WOLFSSL_FATAL_ERROR;
  9479. }
  9480. }
  9481. if (!flag) {
  9482. ret = wc_ShaFinal(NULL, hash1);
  9483. if (ret != BAD_FUNC_ARG) {
  9484. flag = WOLFSSL_FATAL_ERROR;
  9485. }
  9486. }
  9487. if (!flag) {
  9488. ret = wc_ShaFinal(&sha, NULL);
  9489. if (ret != BAD_FUNC_ARG) {
  9490. flag = WOLFSSL_FATAL_ERROR;
  9491. }
  9492. }
  9493. wc_ShaFree(&sha);
  9494. res = TEST_RES_CHECK(flag == 0);
  9495. #endif
  9496. return res;
  9497. } /* END test_wc_ShaFinal */
  9498. /*
  9499. * Unit test for wc_InitSha256()
  9500. */
  9501. static int test_wc_InitSha256(void)
  9502. {
  9503. int res = TEST_SKIPPED;
  9504. #ifndef NO_SHA256
  9505. wc_Sha256 sha256;
  9506. int ret;
  9507. int flag = 0;
  9508. /* Test good arg. */
  9509. ret = wc_InitSha256(&sha256);
  9510. if (ret != 0) {
  9511. flag = WOLFSSL_FATAL_ERROR;
  9512. }
  9513. /* Test bad arg. */
  9514. if (!flag) {
  9515. ret = wc_InitSha256(NULL);
  9516. if (ret != BAD_FUNC_ARG) {
  9517. flag = WOLFSSL_FATAL_ERROR;
  9518. }
  9519. }
  9520. wc_Sha256Free(&sha256);
  9521. res = TEST_RES_CHECK(flag == 0);
  9522. #endif
  9523. return res;
  9524. } /* END test_wc_InitSha256 */
  9525. /*
  9526. * Unit test for wc_Sha256Update()
  9527. */
  9528. static int test_wc_Sha256Update(void)
  9529. {
  9530. int res = TEST_SKIPPED;
  9531. #ifndef NO_SHA256
  9532. wc_Sha256 sha256;
  9533. byte hash[WC_SHA256_DIGEST_SIZE];
  9534. testVector a, b, c;
  9535. int ret;
  9536. int flag = 0;
  9537. ret = wc_InitSha256(&sha256);
  9538. if (ret != 0) {
  9539. flag = ret;
  9540. }
  9541. /* Input. */
  9542. if (!flag) {
  9543. a.input = "a";
  9544. a.inLen = XSTRLEN(a.input);
  9545. ret = wc_Sha256Update(&sha256, NULL, 0);
  9546. if (ret != 0) {
  9547. flag = ret;
  9548. }
  9549. ret = wc_Sha256Update(&sha256, (byte*)a.input, 0);
  9550. if (ret != 0) {
  9551. flag = ret;
  9552. }
  9553. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  9554. if (ret != 0) {
  9555. flag = ret;
  9556. }
  9557. }
  9558. if (!flag) {
  9559. ret = wc_Sha256Final(&sha256, hash);
  9560. if (ret != 0) {
  9561. flag = ret;
  9562. }
  9563. }
  9564. /* Update input. */
  9565. if (!flag) {
  9566. a.input = "abc";
  9567. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  9568. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  9569. "\x15\xAD";
  9570. a.inLen = XSTRLEN(a.input);
  9571. a.outLen = XSTRLEN(a.output);
  9572. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  9573. if (ret != 0) {
  9574. flag = ret;
  9575. }
  9576. }
  9577. if (!flag) {
  9578. ret = wc_Sha256Final(&sha256, hash);
  9579. if (ret != 0) {
  9580. flag = ret;
  9581. }
  9582. }
  9583. if (!flag) {
  9584. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  9585. flag = WOLFSSL_FATAL_ERROR;
  9586. }
  9587. }
  9588. /* Try passing in bad values */
  9589. if (!flag) {
  9590. b.input = NULL;
  9591. b.inLen = 0;
  9592. ret = wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen);
  9593. if (ret != 0) {
  9594. flag = ret;
  9595. }
  9596. }
  9597. if (!flag) {
  9598. c.input = NULL;
  9599. c.inLen = WC_SHA256_DIGEST_SIZE;
  9600. ret = wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen);
  9601. if (ret != BAD_FUNC_ARG) {
  9602. flag = WOLFSSL_FATAL_ERROR;
  9603. }
  9604. }
  9605. if (!flag) {
  9606. ret = wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen);
  9607. if (ret != BAD_FUNC_ARG) {
  9608. flag = WOLFSSL_FATAL_ERROR;
  9609. }
  9610. }
  9611. wc_Sha256Free(&sha256);
  9612. res = TEST_RES_CHECK(flag == 0);
  9613. #endif
  9614. return res;
  9615. } /* END test_wc_Sha256Update */
  9616. /*
  9617. * Unit test function for wc_Sha256Final()
  9618. */
  9619. static int test_wc_Sha256Final(void)
  9620. {
  9621. int res = TEST_SKIPPED;
  9622. #ifndef NO_SHA256
  9623. wc_Sha256 sha256;
  9624. byte* hash_test[3];
  9625. byte hash1[WC_SHA256_DIGEST_SIZE];
  9626. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  9627. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  9628. int times, i, ret;
  9629. int flag = 0;
  9630. /* Initialize */
  9631. ret = wc_InitSha256(&sha256);
  9632. if (ret != 0) {
  9633. flag = ret;
  9634. }
  9635. if (!flag) {
  9636. hash_test[0] = hash1;
  9637. hash_test[1] = hash2;
  9638. hash_test[2] = hash3;
  9639. }
  9640. times = sizeof(hash_test) / sizeof(byte*);
  9641. for (i = 0; i < times; i++) {
  9642. if (!flag) {
  9643. ret = wc_Sha256Final(&sha256, hash_test[i]);
  9644. if (ret != 0) {
  9645. flag = WOLFSSL_FATAL_ERROR;
  9646. }
  9647. }
  9648. }
  9649. /* Test bad args. */
  9650. if (!flag ) {
  9651. ret = wc_Sha256Final(NULL, NULL);
  9652. if (ret != BAD_FUNC_ARG) {
  9653. flag = WOLFSSL_FATAL_ERROR;
  9654. }
  9655. }
  9656. if (!flag) {
  9657. ret = wc_Sha256Final(NULL, hash1);
  9658. if (ret != BAD_FUNC_ARG) {
  9659. flag = WOLFSSL_FATAL_ERROR;
  9660. }
  9661. }
  9662. if (!flag) {
  9663. ret = wc_Sha256Final(&sha256, NULL);
  9664. if (ret != BAD_FUNC_ARG) {
  9665. flag = WOLFSSL_FATAL_ERROR;
  9666. }
  9667. }
  9668. wc_Sha256Free(&sha256);
  9669. res = TEST_RES_CHECK(flag == 0);
  9670. #endif
  9671. return res;
  9672. } /* END test_wc_Sha256Final */
  9673. /*
  9674. * Unit test function for wc_Sha256FinalRaw()
  9675. */
  9676. static int test_wc_Sha256FinalRaw(void)
  9677. {
  9678. int res = TEST_SKIPPED;
  9679. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  9680. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  9681. !defined(WOLFSSL_NO_HASH_RAW)
  9682. wc_Sha256 sha256;
  9683. byte* hash_test[3];
  9684. byte hash1[WC_SHA256_DIGEST_SIZE];
  9685. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  9686. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  9687. int times, i, ret;
  9688. int flag = 0;
  9689. /* Initialize */
  9690. ret = wc_InitSha256(&sha256);
  9691. if (ret != 0) {
  9692. flag = ret;
  9693. }
  9694. if (!flag) {
  9695. hash_test[0] = hash1;
  9696. hash_test[1] = hash2;
  9697. hash_test[2] = hash3;
  9698. }
  9699. times = sizeof(hash_test) / sizeof(byte*);
  9700. for (i = 0; i < times; i++) {
  9701. if (!flag) {
  9702. ret = wc_Sha256FinalRaw(&sha256, hash_test[i]);
  9703. if (ret != 0) {
  9704. flag = WOLFSSL_FATAL_ERROR;
  9705. }
  9706. }
  9707. }
  9708. /* Test bad args. */
  9709. if (!flag) {
  9710. ret = wc_Sha256FinalRaw(NULL, NULL);
  9711. if (ret != BAD_FUNC_ARG) {
  9712. flag = WOLFSSL_FATAL_ERROR;
  9713. }
  9714. }
  9715. if (!flag) {
  9716. ret = wc_Sha256FinalRaw(NULL, hash1);
  9717. if (ret != BAD_FUNC_ARG) {
  9718. flag = WOLFSSL_FATAL_ERROR;
  9719. }
  9720. }
  9721. if (!flag) {
  9722. ret = wc_Sha256FinalRaw(&sha256, NULL);
  9723. if (ret != BAD_FUNC_ARG) {
  9724. flag = WOLFSSL_FATAL_ERROR;
  9725. }
  9726. }
  9727. wc_Sha256Free(&sha256);
  9728. res = TEST_RES_CHECK(flag == 0);
  9729. #endif
  9730. return res;
  9731. } /* END test_wc_Sha256FinalRaw */
  9732. /*
  9733. * Unit test function for wc_Sha256GetFlags()
  9734. */
  9735. static int test_wc_Sha256GetFlags(void)
  9736. {
  9737. int res = TEST_SKIPPED;
  9738. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  9739. wc_Sha256 sha256;
  9740. word32 flags = 0;
  9741. int flag = 0;
  9742. /* Initialize */
  9743. flag = wc_InitSha256(&sha256);
  9744. if (flag == 0) {
  9745. flag = wc_Sha256GetFlags(&sha256, &flags);
  9746. }
  9747. if (flag == 0) {
  9748. if (flags & WC_HASH_FLAG_ISCOPY) {
  9749. flag = 0;
  9750. }
  9751. }
  9752. wc_Sha256Free(&sha256);
  9753. res = TEST_RES_CHECK(flag == 0);
  9754. #endif
  9755. return res;
  9756. } /* END test_wc_Sha256GetFlags */
  9757. /*
  9758. * Unit test function for wc_Sha256Free()
  9759. */
  9760. static int test_wc_Sha256Free(void)
  9761. {
  9762. int res = TEST_SKIPPED;
  9763. #ifndef NO_SHA256
  9764. wc_Sha256Free(NULL);
  9765. res = TEST_RES_CHECK(1);
  9766. #endif
  9767. return res;
  9768. } /* END test_wc_Sha256Free */
  9769. /*
  9770. * Unit test function for wc_Sha256GetHash()
  9771. */
  9772. static int test_wc_Sha256GetHash(void)
  9773. {
  9774. int res = TEST_SKIPPED;
  9775. #ifndef NO_SHA256
  9776. wc_Sha256 sha256;
  9777. byte hash1[WC_SHA256_DIGEST_SIZE];
  9778. int flag = 0;
  9779. /* Initialize */
  9780. flag = wc_InitSha256(&sha256);
  9781. if (flag == 0) {
  9782. flag = wc_Sha256GetHash(&sha256, hash1);
  9783. }
  9784. /*test bad arguments*/
  9785. if (flag == 0) {
  9786. flag = wc_Sha256GetHash(NULL, NULL);
  9787. if (flag == BAD_FUNC_ARG) {
  9788. flag = 0;
  9789. }
  9790. }
  9791. if (flag == 0) {
  9792. flag = wc_Sha256GetHash(NULL, hash1);
  9793. if (flag == BAD_FUNC_ARG) {
  9794. flag = 0;
  9795. }
  9796. }
  9797. if (flag == 0) {
  9798. flag = wc_Sha256GetHash(&sha256, NULL);
  9799. if (flag == BAD_FUNC_ARG) {
  9800. flag = 0;
  9801. }
  9802. }
  9803. wc_Sha256Free(&sha256);
  9804. res = TEST_RES_CHECK(flag == 0);
  9805. #endif
  9806. return res;
  9807. } /* END test_wc_Sha256GetHash */
  9808. /*
  9809. * Unit test function for wc_Sha256Copy()
  9810. */
  9811. static int test_wc_Sha256Copy(void)
  9812. {
  9813. int res = TEST_SKIPPED;
  9814. #ifndef NO_SHA256
  9815. wc_Sha256 sha256;
  9816. wc_Sha256 temp;
  9817. int flag = 0;
  9818. /* Initialize */
  9819. flag = wc_InitSha256(&sha256);
  9820. if (flag == 0) {
  9821. flag = wc_InitSha256(&temp);
  9822. }
  9823. if (flag == 0) {
  9824. flag = wc_Sha256Copy(&sha256, &temp);
  9825. }
  9826. /*test bad arguments*/
  9827. if (flag == 0) {
  9828. flag = wc_Sha256Copy(NULL, NULL);
  9829. if (flag == BAD_FUNC_ARG) {
  9830. flag = 0;
  9831. }
  9832. }
  9833. if (flag == 0) {
  9834. flag = wc_Sha256Copy(NULL, &temp);
  9835. if (flag == BAD_FUNC_ARG) {
  9836. flag = 0;
  9837. }
  9838. }
  9839. if (flag == 0) {
  9840. flag = wc_Sha256Copy(&sha256, NULL);
  9841. if (flag == BAD_FUNC_ARG) {
  9842. flag = 0;
  9843. }
  9844. }
  9845. wc_Sha256Free(&sha256);
  9846. wc_Sha256Free(&temp);
  9847. res = TEST_RES_CHECK(flag == 0);
  9848. #endif
  9849. return res;
  9850. } /* END test_wc_Sha256Copy */
  9851. /*
  9852. * Testing wc_InitSha512()
  9853. */
  9854. static int test_wc_InitSha512(void)
  9855. {
  9856. int res = TEST_SKIPPED;
  9857. #ifdef WOLFSSL_SHA512
  9858. wc_Sha512 sha512;
  9859. int ret;
  9860. int flag = 0;
  9861. /* Test good arg. */
  9862. ret = wc_InitSha512(&sha512);
  9863. if (ret != 0) {
  9864. flag = WOLFSSL_FATAL_ERROR;
  9865. }
  9866. /* Test bad arg. */
  9867. if (!flag) {
  9868. ret = wc_InitSha512(NULL);
  9869. if (ret != BAD_FUNC_ARG) {
  9870. flag = WOLFSSL_FATAL_ERROR;
  9871. }
  9872. }
  9873. wc_Sha512Free(&sha512);
  9874. res = TEST_RES_CHECK(flag == 0);
  9875. #endif
  9876. return res;
  9877. } /* END test_wc_InitSha512 */
  9878. /*
  9879. * wc_Sha512Update() test.
  9880. */
  9881. static int test_wc_Sha512Update(void)
  9882. {
  9883. int res = TEST_SKIPPED;
  9884. #ifdef WOLFSSL_SHA512
  9885. wc_Sha512 sha512;
  9886. byte hash[WC_SHA512_DIGEST_SIZE];
  9887. testVector a, b, c;
  9888. int ret;
  9889. int flag = 0;
  9890. ret = wc_InitSha512(&sha512);
  9891. if (ret != 0) {
  9892. flag = ret;
  9893. }
  9894. /* Input. */
  9895. if (!flag) {
  9896. a.input = "a";
  9897. a.inLen = XSTRLEN(a.input);
  9898. ret = wc_Sha512Update(&sha512, NULL, 0);
  9899. if (ret != 0) {
  9900. flag = ret;
  9901. }
  9902. ret = wc_Sha512Update(&sha512,(byte*)a.input, 0);
  9903. if (ret != 0) {
  9904. flag = ret;
  9905. }
  9906. ret = wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen);
  9907. if (ret != 0) {
  9908. flag = ret;
  9909. }
  9910. ret = wc_Sha512Final(&sha512, hash);
  9911. if (ret != 0) {
  9912. flag = ret;
  9913. }
  9914. }
  9915. /* Update input. */
  9916. if (!flag) {
  9917. a.input = "abc";
  9918. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  9919. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  9920. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  9921. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  9922. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  9923. a.inLen = XSTRLEN(a.input);
  9924. a.outLen = XSTRLEN(a.output);
  9925. ret = wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen);
  9926. if (ret != 0) {
  9927. flag = ret;
  9928. }
  9929. }
  9930. if (!flag) {
  9931. ret = wc_Sha512Final(&sha512, hash);
  9932. if (ret != 0) {
  9933. flag = ret;
  9934. }
  9935. }
  9936. if (!flag) {
  9937. if (XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE) != 0) {
  9938. flag = WOLFSSL_FATAL_ERROR;
  9939. }
  9940. }
  9941. /* Try passing in bad values */
  9942. if (!flag) {
  9943. b.input = NULL;
  9944. b.inLen = 0;
  9945. ret = wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen);
  9946. if (ret != 0) {
  9947. flag = ret;
  9948. }
  9949. }
  9950. if (!flag) {
  9951. c.input = NULL;
  9952. c.inLen = WC_SHA512_DIGEST_SIZE;
  9953. ret = wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen);
  9954. if (ret != BAD_FUNC_ARG) {
  9955. flag = WOLFSSL_FATAL_ERROR;
  9956. }
  9957. }
  9958. if (!flag) {
  9959. ret = wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen);
  9960. if (ret != BAD_FUNC_ARG) {
  9961. flag = WOLFSSL_FATAL_ERROR;
  9962. }
  9963. }
  9964. wc_Sha512Free(&sha512);
  9965. res = TEST_RES_CHECK(flag == 0);
  9966. #endif
  9967. return res;
  9968. } /* END test_wc_Sha512Update */
  9969. #ifdef WOLFSSL_SHA512
  9970. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  9971. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  9972. /* Perfoms test for
  9973. * - wc_Sha512Final/wc_Sha512FinalRaw
  9974. * - wc_Sha512_224Final/wc_Sha512_224Final
  9975. * - wc_Sha512_256Final/wc_Sha512_256Final
  9976. * parameter:
  9977. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  9978. * WC_HASH_TYPE_SHA512_256
  9979. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  9980. *return 0 on success
  9981. */
  9982. static int test_Sha512_Family_Final(int type, int isRaw)
  9983. {
  9984. wc_Sha512 sha512;
  9985. byte* hash_test[3];
  9986. byte hash1[WC_SHA512_DIGEST_SIZE];
  9987. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  9988. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  9989. int times, i, ret;
  9990. int(*initFp)(wc_Sha512*);
  9991. int(*finalFp)(wc_Sha512*, byte*);
  9992. void(*freeFp)(wc_Sha512*);
  9993. if (type == WC_HASH_TYPE_SHA512) {
  9994. initFp = wc_InitSha512;
  9995. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  9996. !defined(WOLFSSL_NO_HASH_RAW)
  9997. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  9998. #else
  9999. finalFp = (isRaw)? NULL : wc_Sha512Final;
  10000. #endif
  10001. freeFp = wc_Sha512Free;
  10002. }
  10003. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10004. #if !defined(WOLFSSL_NOSHA512_224)
  10005. else if (type == WC_HASH_TYPE_SHA512_224) {
  10006. initFp = wc_InitSha512_224;
  10007. #if !defined(WOLFSSL_NO_HASH_RAW)
  10008. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  10009. #else
  10010. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  10011. #endif
  10012. freeFp = wc_Sha512_224Free;
  10013. }
  10014. #endif
  10015. #if !defined(WOLFSSL_NOSHA512_256)
  10016. else if (type == WC_HASH_TYPE_SHA512_256) {
  10017. initFp = wc_InitSha512_256;
  10018. #if !defined(WOLFSSL_NO_HASH_RAW)
  10019. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  10020. #else
  10021. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  10022. #endif
  10023. freeFp = wc_Sha512_256Free;
  10024. }
  10025. #endif
  10026. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10027. else
  10028. return TEST_FAIL;
  10029. /* Initialize */
  10030. ret = initFp(&sha512);
  10031. if (!ret) {
  10032. hash_test[0] = hash1;
  10033. hash_test[1] = hash2;
  10034. hash_test[2] = hash3;
  10035. }
  10036. times = sizeof(hash_test) / sizeof(byte *);
  10037. /* Good test args. */
  10038. for (i = 0; i < times && ret == 0; i++) {
  10039. ret = finalFp(&sha512, hash_test[i]);
  10040. }
  10041. /* Test bad args. */
  10042. if (!ret) {
  10043. if (finalFp(NULL, NULL) != BAD_FUNC_ARG) {
  10044. ret = WOLFSSL_FATAL_ERROR;
  10045. }
  10046. }
  10047. if (!ret) {
  10048. if (finalFp(NULL, hash1) != BAD_FUNC_ARG) {
  10049. ret = WOLFSSL_FATAL_ERROR;
  10050. }
  10051. }
  10052. if (!ret) {
  10053. if (finalFp(&sha512, NULL) != BAD_FUNC_ARG) {
  10054. ret = WOLFSSL_FATAL_ERROR;
  10055. }
  10056. }
  10057. freeFp(&sha512);
  10058. return ret;
  10059. }
  10060. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  10061. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  10062. #endif /* WOLFSSL_SHA512 */
  10063. /*
  10064. * Unit test function for wc_Sha512Final()
  10065. */
  10066. static int test_wc_Sha512Final(void)
  10067. {
  10068. int res = TEST_SKIPPED;
  10069. #ifdef WOLFSSL_SHA512
  10070. wc_Sha512 sha512;
  10071. byte* hash_test[3];
  10072. byte hash1[WC_SHA512_DIGEST_SIZE];
  10073. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  10074. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  10075. int times, i, ret;
  10076. int flag = 0;
  10077. /* Initialize */
  10078. ret = wc_InitSha512(&sha512);
  10079. if (ret != 0) {
  10080. flag = ret;
  10081. }
  10082. if (!flag) {
  10083. hash_test[0] = hash1;
  10084. hash_test[1] = hash2;
  10085. hash_test[2] = hash3;
  10086. }
  10087. times = sizeof(hash_test) / sizeof(byte *);
  10088. for (i = 0; i < times; i++) {
  10089. if (!flag) {
  10090. ret = wc_Sha512Final(&sha512, hash_test[i]);
  10091. if (ret != 0) {
  10092. flag = WOLFSSL_FATAL_ERROR;
  10093. }
  10094. }
  10095. }
  10096. /* Test bad args. */
  10097. if (!flag) {
  10098. ret = wc_Sha512Final(NULL, NULL);
  10099. if (ret != BAD_FUNC_ARG) {
  10100. flag = WOLFSSL_FATAL_ERROR;
  10101. }
  10102. }
  10103. if (!flag) {
  10104. ret = wc_Sha512Final(NULL, hash1);
  10105. if (ret != BAD_FUNC_ARG) {
  10106. flag = WOLFSSL_FATAL_ERROR;
  10107. }
  10108. }
  10109. if (!flag) {
  10110. ret = wc_Sha512Final(&sha512, NULL);
  10111. if (ret != BAD_FUNC_ARG) {
  10112. flag = WOLFSSL_FATAL_ERROR;
  10113. }
  10114. }
  10115. wc_Sha512Free(&sha512);
  10116. res = TEST_RES_CHECK(flag == 0);
  10117. #endif
  10118. return res;
  10119. } /* END test_wc_Sha512Final */
  10120. /*
  10121. * Unit test function for wc_Sha512GetFlags()
  10122. */
  10123. static int test_wc_Sha512GetFlags(void)
  10124. {
  10125. int res = TEST_SKIPPED;
  10126. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  10127. wc_Sha512 sha512;
  10128. word32 flags = 0;
  10129. int flag = 0;
  10130. /* Initialize */
  10131. flag = wc_InitSha512(&sha512);
  10132. if (flag == 0) {
  10133. flag = wc_Sha512GetFlags(&sha512, &flags);
  10134. }
  10135. if (flag == 0) {
  10136. if (flags & WC_HASH_FLAG_ISCOPY) {
  10137. flag = 0;
  10138. }
  10139. }
  10140. wc_Sha512Free(&sha512);
  10141. res = TEST_RES_CHECK(flag == 0);
  10142. #endif
  10143. return res;
  10144. } /* END test_wc_Sha512GetFlags */
  10145. /*
  10146. * Unit test function for wc_Sha512FinalRaw()
  10147. */
  10148. static int test_wc_Sha512FinalRaw(void)
  10149. {
  10150. int res = TEST_SKIPPED;
  10151. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  10152. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  10153. !defined(WOLFSSL_NO_HASH_RAW)
  10154. wc_Sha512 sha512;
  10155. byte* hash_test[3];
  10156. byte hash1[WC_SHA512_DIGEST_SIZE];
  10157. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  10158. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  10159. int times, i, ret;
  10160. int flag = 0;
  10161. /* Initialize */
  10162. ret = wc_InitSha512(&sha512);
  10163. if (ret != 0) {
  10164. flag = ret;
  10165. }
  10166. if (!flag) {
  10167. hash_test[0] = hash1;
  10168. hash_test[1] = hash2;
  10169. hash_test[2] = hash3;
  10170. }
  10171. times = sizeof(hash_test) / sizeof(byte*);
  10172. /* Good test args. */
  10173. for (i = 0; i < times; i++) {
  10174. if (!flag) {
  10175. ret = wc_Sha512FinalRaw(&sha512, hash_test[i]);
  10176. if (ret != 0) {
  10177. flag = WOLFSSL_FATAL_ERROR;
  10178. }
  10179. }
  10180. }
  10181. /* Test bad args. */
  10182. if (!flag ) {
  10183. ret = wc_Sha512FinalRaw(NULL, NULL);
  10184. if (ret != BAD_FUNC_ARG) {
  10185. flag = WOLFSSL_FATAL_ERROR;
  10186. }
  10187. }
  10188. if (!flag) {
  10189. ret = wc_Sha512FinalRaw(NULL, hash1);
  10190. if (ret != BAD_FUNC_ARG) {
  10191. flag = WOLFSSL_FATAL_ERROR;
  10192. }
  10193. }
  10194. if (!flag) {
  10195. ret = wc_Sha512FinalRaw(&sha512, NULL);
  10196. if (ret != BAD_FUNC_ARG) {
  10197. flag = WOLFSSL_FATAL_ERROR;
  10198. }
  10199. }
  10200. wc_Sha512Free(&sha512);
  10201. res = TEST_RES_CHECK(flag == 0);
  10202. #endif
  10203. return res;
  10204. } /* END test_wc_Sha512FinalRaw */
  10205. /*
  10206. * Unit test function for wc_Sha512Free()
  10207. */
  10208. static int test_wc_Sha512Free(void)
  10209. {
  10210. int res = TEST_SKIPPED;
  10211. #ifdef WOLFSSL_SHA512
  10212. wc_Sha512Free(NULL);
  10213. res = TEST_RES_CHECK(1);
  10214. #endif
  10215. return res;
  10216. } /* END test_wc_Sha512Free */
  10217. #ifdef WOLFSSL_SHA512
  10218. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  10219. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  10220. static int test_Sha512_Family_GetHash(int type )
  10221. {
  10222. int flag = 0;
  10223. int(*initFp)(wc_Sha512*);
  10224. int(*ghashFp)(wc_Sha512*, byte*);
  10225. wc_Sha512 sha512;
  10226. byte hash1[WC_SHA512_DIGEST_SIZE];
  10227. if (type == WC_HASH_TYPE_SHA512) {
  10228. initFp = wc_InitSha512;
  10229. ghashFp = wc_Sha512GetHash;
  10230. }
  10231. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10232. #if !defined(WOLFSSL_NOSHA512_224)
  10233. else if (type == WC_HASH_TYPE_SHA512_224) {
  10234. initFp = wc_InitSha512_224;
  10235. ghashFp = wc_Sha512_224GetHash;
  10236. }
  10237. #endif
  10238. #if !defined(WOLFSSL_NOSHA512_256)
  10239. else if (type == WC_HASH_TYPE_SHA512_256) {
  10240. initFp = wc_InitSha512_256;
  10241. ghashFp = wc_Sha512_256GetHash;
  10242. }
  10243. #endif
  10244. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10245. else {
  10246. initFp = NULL;
  10247. ghashFp = NULL;
  10248. }
  10249. if (initFp == NULL || ghashFp == NULL)
  10250. return TEST_FAIL;
  10251. if (!flag) {
  10252. flag = initFp(&sha512);
  10253. }
  10254. if (!flag) {
  10255. flag = ghashFp(&sha512, hash1);
  10256. }
  10257. /*test bad arguments*/
  10258. if (!flag) {
  10259. if (ghashFp(NULL, NULL) != BAD_FUNC_ARG )
  10260. flag = WOLFSSL_FATAL_ERROR;
  10261. }
  10262. if (!flag) {
  10263. if (ghashFp(NULL, hash1) != BAD_FUNC_ARG )
  10264. flag = WOLFSSL_FATAL_ERROR;
  10265. }
  10266. if (!flag) {
  10267. if (ghashFp(&sha512, NULL) != BAD_FUNC_ARG )
  10268. flag = WOLFSSL_FATAL_ERROR;
  10269. }
  10270. wc_Sha512Free(&sha512);
  10271. return flag;
  10272. }
  10273. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  10274. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  10275. #endif /* WOLFSSL_SHA512 */
  10276. /*
  10277. * Unit test function for wc_Sha512GetHash()
  10278. */
  10279. static int test_wc_Sha512GetHash(void)
  10280. {
  10281. int res = TEST_SKIPPED;
  10282. #ifdef WOLFSSL_SHA512
  10283. wc_Sha512 sha512;
  10284. byte hash1[WC_SHA512_DIGEST_SIZE];
  10285. int flag = 0;
  10286. /* Initialize */
  10287. flag = wc_InitSha512(&sha512);
  10288. if (flag == 0) {
  10289. flag = wc_Sha512GetHash(&sha512, hash1);
  10290. }
  10291. /*test bad arguments*/
  10292. if (flag == 0) {
  10293. flag = wc_Sha512GetHash(NULL, NULL);
  10294. if (flag == BAD_FUNC_ARG) {
  10295. flag = 0;
  10296. }
  10297. }
  10298. if (flag == 0) {
  10299. flag = wc_Sha512GetHash(NULL, hash1);
  10300. if (flag == BAD_FUNC_ARG) {
  10301. flag = 0;
  10302. }
  10303. }
  10304. if (flag == 0) {
  10305. flag = wc_Sha512GetHash(&sha512, NULL);
  10306. if (flag == BAD_FUNC_ARG) {
  10307. flag = 0;
  10308. }
  10309. }
  10310. wc_Sha512Free(&sha512);
  10311. res = TEST_RES_CHECK(flag == 0);
  10312. #endif
  10313. return res;
  10314. } /* END test_wc_Sha512GetHash */
  10315. /*
  10316. * Unit test function for wc_Sha512Copy()
  10317. */
  10318. static int test_wc_Sha512Copy(void)
  10319. {
  10320. int res = TEST_SKIPPED;
  10321. #ifdef WOLFSSL_SHA512
  10322. wc_Sha512 sha512;
  10323. wc_Sha512 temp;
  10324. int flag;
  10325. /* Initialize */
  10326. flag = wc_InitSha512(&sha512);
  10327. if (flag == 0) {
  10328. flag = wc_InitSha512(&temp);
  10329. }
  10330. if (flag == 0) {
  10331. flag = wc_Sha512Copy(&sha512, &temp);
  10332. }
  10333. /*test bad arguments*/
  10334. if (flag == 0) {
  10335. flag = wc_Sha512Copy(NULL, NULL);
  10336. if (flag == BAD_FUNC_ARG) {
  10337. flag = 0;
  10338. }
  10339. }
  10340. if (flag == 0) {
  10341. flag = wc_Sha512Copy(NULL, &temp);
  10342. if (flag == BAD_FUNC_ARG) {
  10343. flag = 0;
  10344. }
  10345. }
  10346. if (flag == 0) {
  10347. flag = wc_Sha512Copy(&sha512, NULL);
  10348. if (flag == BAD_FUNC_ARG) {
  10349. flag = 0;
  10350. }
  10351. }
  10352. wc_Sha512Free(&sha512);
  10353. wc_Sha512Free(&temp);
  10354. res = TEST_RES_CHECK(flag == 0);
  10355. #endif
  10356. return res;
  10357. } /* END test_wc_Sha512Copy */
  10358. static int test_wc_InitSha512_224(void)
  10359. {
  10360. int res = TEST_SKIPPED;
  10361. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10362. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  10363. wc_Sha512 sha512;
  10364. int ret;
  10365. int flag = 0;
  10366. /* Test good arg. */
  10367. ret = wc_InitSha512_224(&sha512);
  10368. if (ret != 0) {
  10369. flag = WOLFSSL_FATAL_ERROR;
  10370. }
  10371. /* Test bad arg. */
  10372. if (!flag) {
  10373. ret = wc_InitSha512_224(NULL);
  10374. if (ret != BAD_FUNC_ARG) {
  10375. flag = WOLFSSL_FATAL_ERROR;
  10376. }
  10377. }
  10378. wc_Sha512_224Free(&sha512);
  10379. res = TEST_RES_CHECK(flag == 0);
  10380. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  10381. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10382. return res;
  10383. }
  10384. static int test_wc_Sha512_224Update(void)
  10385. {
  10386. int res = TEST_SKIPPED;
  10387. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10388. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  10389. wc_Sha512 sha512;
  10390. byte hash[WC_SHA512_DIGEST_SIZE];
  10391. testVector a, c;
  10392. int ret;
  10393. int flag = 0;
  10394. ret = wc_InitSha512_224(&sha512);
  10395. if (ret != 0) {
  10396. flag = ret;
  10397. }
  10398. /* Input. */
  10399. if (!flag) {
  10400. a.input = "a";
  10401. a.inLen = XSTRLEN(a.input);
  10402. ret = wc_Sha512_224Update(&sha512, NULL, 0);
  10403. if (ret != 0) {
  10404. flag = ret;
  10405. }
  10406. ret = wc_Sha512_224Update(&sha512,(byte*)a.input, 0);
  10407. if (ret != 0) {
  10408. flag = ret;
  10409. }
  10410. ret = wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen);
  10411. if (ret != 0) {
  10412. flag = ret;
  10413. }
  10414. ret = wc_Sha512_224Final(&sha512, hash);
  10415. if (ret != 0) {
  10416. flag = ret;
  10417. }
  10418. }
  10419. /* Update input. */
  10420. if (!flag) {
  10421. a.input = "abc";
  10422. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  10423. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  10424. a.inLen = XSTRLEN(a.input);
  10425. a.outLen = XSTRLEN(a.output);
  10426. ret = wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen);
  10427. if (ret != 0) {
  10428. flag = ret;
  10429. }
  10430. }
  10431. if (!flag) {
  10432. ret = wc_Sha512_224Final(&sha512, hash);
  10433. if (ret != 0) {
  10434. flag = ret;
  10435. }
  10436. }
  10437. if (!flag) {
  10438. if (XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE) != 0) {
  10439. flag = WOLFSSL_FATAL_ERROR;
  10440. }
  10441. }
  10442. if (!flag) {
  10443. c.input = NULL;
  10444. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  10445. ret = wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen);
  10446. if (ret != BAD_FUNC_ARG) {
  10447. flag = WOLFSSL_FATAL_ERROR;
  10448. }
  10449. }
  10450. if (!flag) {
  10451. ret = wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen);
  10452. if (ret != BAD_FUNC_ARG) {
  10453. flag = WOLFSSL_FATAL_ERROR;
  10454. }
  10455. }
  10456. wc_Sha512_224Free(&sha512);
  10457. res = TEST_RES_CHECK(flag == 0);
  10458. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  10459. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10460. return res;
  10461. }
  10462. static int test_wc_Sha512_224Final(void)
  10463. {
  10464. int res = TEST_SKIPPED;
  10465. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10466. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  10467. int ret = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0);
  10468. res = TEST_RES_CHECK(ret == 0);
  10469. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  10470. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10471. return res;
  10472. }
  10473. static int test_wc_Sha512_224GetFlags(void)
  10474. {
  10475. int res = TEST_SKIPPED;
  10476. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10477. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  10478. wc_Sha512 sha512, copy;
  10479. word32 flags = 0;
  10480. int flag = 0;
  10481. /* Initialize */
  10482. flag = wc_InitSha512_224(&sha512);
  10483. if (!flag) {
  10484. flag = wc_InitSha512_224(&copy);
  10485. }
  10486. if (!flag) {
  10487. flag = wc_Sha512_224Copy(&sha512, &copy);
  10488. }
  10489. if (!flag) {
  10490. flag = wc_Sha512_224GetFlags(&copy, &flags);
  10491. }
  10492. if (!flag) {
  10493. if (flags & WC_HASH_FLAG_ISCOPY)
  10494. flag = 0;
  10495. else
  10496. flag = WOLFSSL_FATAL_ERROR;
  10497. }
  10498. wc_Sha512_224Free(&copy);
  10499. wc_Sha512_224Free(&sha512);
  10500. res = TEST_RES_CHECK(flag == 0);
  10501. #endif
  10502. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10503. return res;
  10504. }
  10505. static int test_wc_Sha512_224FinalRaw(void)
  10506. {
  10507. int res = TEST_SKIPPED;
  10508. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  10509. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  10510. !defined(WOLFSSL_NO_HASH_RAW)
  10511. int ret = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1);
  10512. res = TEST_RES_CHECK(ret == 0);
  10513. #endif
  10514. return res;
  10515. }
  10516. static int test_wc_Sha512_224Free(void)
  10517. {
  10518. int res = TEST_SKIPPED;
  10519. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10520. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  10521. wc_Sha512_224Free(NULL);
  10522. res = TEST_RES_CHECK(1);
  10523. #endif
  10524. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10525. return res;
  10526. }
  10527. static int test_wc_Sha512_224GetHash(void)
  10528. {
  10529. int res = TEST_SKIPPED;
  10530. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10531. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  10532. int ret = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224);
  10533. res = TEST_RES_CHECK(ret == 0);
  10534. #endif
  10535. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10536. return res;
  10537. }
  10538. static int test_wc_Sha512_224Copy(void)
  10539. {
  10540. int res = TEST_SKIPPED;
  10541. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10542. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  10543. wc_Sha512 sha512;
  10544. wc_Sha512 temp;
  10545. int flag = 0;
  10546. /* Initialize */
  10547. flag = wc_InitSha512_224(&sha512);
  10548. if (flag == 0) {
  10549. flag = wc_InitSha512_224(&temp);
  10550. }
  10551. if (flag == 0) {
  10552. flag = wc_Sha512_224Copy(&sha512, &temp);
  10553. }
  10554. /*test bad arguments*/
  10555. if (flag == 0) {
  10556. if (wc_Sha512_224Copy(NULL, NULL) != BAD_FUNC_ARG)
  10557. flag = WOLFSSL_FATAL_ERROR;
  10558. }
  10559. if (flag == 0) {
  10560. if (wc_Sha512_224Copy(NULL, &temp) != BAD_FUNC_ARG)
  10561. flag = WOLFSSL_FATAL_ERROR;
  10562. }
  10563. if (flag == 0) {
  10564. if (wc_Sha512_224Copy(&sha512, NULL) != BAD_FUNC_ARG)
  10565. flag = WOLFSSL_FATAL_ERROR;
  10566. }
  10567. wc_Sha512_224Free(&sha512);
  10568. wc_Sha512_224Free(&temp);
  10569. res = TEST_RES_CHECK(flag == 0);
  10570. #endif
  10571. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10572. return res;
  10573. }
  10574. static int test_wc_InitSha512_256(void)
  10575. {
  10576. int res = TEST_SKIPPED;
  10577. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10578. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10579. wc_Sha512 sha512;
  10580. int ret;
  10581. int flag = 0;
  10582. /* Test good arg. */
  10583. ret = wc_InitSha512_256(&sha512);
  10584. if (ret != 0) {
  10585. flag = WOLFSSL_FATAL_ERROR;
  10586. }
  10587. /* Test bad arg. */
  10588. if (!flag) {
  10589. ret = wc_InitSha512_256(NULL);
  10590. if (ret != BAD_FUNC_ARG) {
  10591. flag = WOLFSSL_FATAL_ERROR;
  10592. }
  10593. }
  10594. wc_Sha512_256Free(&sha512);
  10595. res = TEST_RES_CHECK(flag == 0);
  10596. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  10597. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10598. return res;
  10599. }
  10600. static int test_wc_Sha512_256Update(void)
  10601. {
  10602. int res = TEST_SKIPPED;
  10603. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10604. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10605. wc_Sha512 sha512;
  10606. byte hash[WC_SHA512_DIGEST_SIZE];
  10607. testVector a, c;
  10608. int ret;
  10609. int flag = 0;
  10610. ret = wc_InitSha512_256(&sha512);
  10611. if (ret != 0) {
  10612. flag = ret;
  10613. }
  10614. /* Input. */
  10615. if (!flag) {
  10616. a.input = "a";
  10617. a.inLen = XSTRLEN(a.input);
  10618. ret = wc_Sha512_256Update(&sha512, NULL, 0);
  10619. if (ret != 0) {
  10620. flag = ret;
  10621. }
  10622. ret = wc_Sha512_256Update(&sha512,(byte*)a.input, 0);
  10623. if (ret != 0) {
  10624. flag = ret;
  10625. }
  10626. ret = wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen);
  10627. if (ret != 0) {
  10628. flag = ret;
  10629. }
  10630. ret = wc_Sha512_256Final(&sha512, hash);
  10631. if (ret != 0) {
  10632. flag = ret;
  10633. }
  10634. }
  10635. /* Update input. */
  10636. if (!flag) {
  10637. a.input = "abc";
  10638. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  10639. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  10640. "\x07\xe7\xaf\x23";
  10641. a.inLen = XSTRLEN(a.input);
  10642. a.outLen = XSTRLEN(a.output);
  10643. ret = wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen);
  10644. if (ret != 0) {
  10645. flag = ret;
  10646. }
  10647. }
  10648. if (!flag) {
  10649. ret = wc_Sha512_256Final(&sha512, hash);
  10650. if (ret != 0) {
  10651. flag = ret;
  10652. }
  10653. }
  10654. if (!flag) {
  10655. if (XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE) != 0) {
  10656. flag = WOLFSSL_FATAL_ERROR;
  10657. }
  10658. }
  10659. if (!flag) {
  10660. c.input = NULL;
  10661. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  10662. ret = wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen);
  10663. if (ret != BAD_FUNC_ARG) {
  10664. flag = WOLFSSL_FATAL_ERROR;
  10665. }
  10666. }
  10667. if (!flag) {
  10668. ret = wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen);
  10669. if (ret != BAD_FUNC_ARG) {
  10670. flag = WOLFSSL_FATAL_ERROR;
  10671. }
  10672. }
  10673. wc_Sha512_256Free(&sha512);
  10674. res = TEST_RES_CHECK(flag == 0);
  10675. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  10676. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10677. return res;
  10678. }
  10679. static int test_wc_Sha512_256Final(void)
  10680. {
  10681. int res = TEST_SKIPPED;
  10682. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10683. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10684. int ret = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0);
  10685. res = TEST_RES_CHECK(ret == 0);
  10686. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  10687. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10688. return res;
  10689. }
  10690. static int test_wc_Sha512_256GetFlags(void)
  10691. {
  10692. int res = TEST_SKIPPED;
  10693. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10694. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  10695. wc_Sha512 sha512, copy;
  10696. word32 flags = 0;
  10697. int flag = 0;
  10698. /* Initialize */
  10699. flag = wc_InitSha512_256(&sha512);
  10700. if (!flag ) {
  10701. flag = wc_InitSha512_256(&copy);
  10702. }
  10703. if (!flag ) {
  10704. flag = wc_Sha512_256Copy(&sha512, &copy);
  10705. }
  10706. if (!flag ) {
  10707. flag = wc_Sha512_256GetFlags(&copy, &flags);
  10708. }
  10709. if (!flag) {
  10710. if (flags & WC_HASH_FLAG_ISCOPY)
  10711. flag = 0;
  10712. else
  10713. flag = WOLFSSL_FATAL_ERROR;
  10714. }
  10715. wc_Sha512_256Free(&sha512);
  10716. res = TEST_RES_CHECK(flag == 0);
  10717. #endif
  10718. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10719. return res;
  10720. }
  10721. static int test_wc_Sha512_256FinalRaw(void)
  10722. {
  10723. int res = TEST_SKIPPED;
  10724. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  10725. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  10726. !defined(WOLFSSL_NO_HASH_RAW)
  10727. int ret = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1);
  10728. res = TEST_RES_CHECK(ret == 0);
  10729. #endif
  10730. return res;
  10731. }
  10732. static int test_wc_Sha512_256Free(void)
  10733. {
  10734. int res = TEST_SKIPPED;
  10735. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10736. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10737. wc_Sha512_256Free(NULL);
  10738. res = TEST_RES_CHECK(1);
  10739. #endif
  10740. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10741. return res;
  10742. }
  10743. static int test_wc_Sha512_256GetHash(void)
  10744. {
  10745. int res = TEST_SKIPPED;
  10746. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10747. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10748. int ret = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256);
  10749. res = TEST_RES_CHECK(ret == 0);
  10750. #endif
  10751. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10752. return res;
  10753. }
  10754. static int test_wc_Sha512_256Copy(void)
  10755. {
  10756. int res = TEST_SKIPPED;
  10757. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10758. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10759. wc_Sha512 sha512;
  10760. wc_Sha512 temp;
  10761. int flag = 0;
  10762. /* Initialize */
  10763. flag = wc_InitSha512_256(&sha512);
  10764. if (flag == 0) {
  10765. flag = wc_InitSha512_256(&temp);
  10766. }
  10767. if (flag == 0) {
  10768. flag = wc_Sha512_256Copy(&sha512, &temp);
  10769. }
  10770. /*test bad arguments*/
  10771. if (flag == 0) {
  10772. if (wc_Sha512_256Copy(NULL, NULL) != BAD_FUNC_ARG)
  10773. flag = WOLFSSL_FATAL_ERROR;
  10774. }
  10775. if (flag == 0) {
  10776. if (wc_Sha512_256Copy(NULL, &temp) != BAD_FUNC_ARG)
  10777. flag = WOLFSSL_FATAL_ERROR;
  10778. }
  10779. if (flag == 0) {
  10780. if (wc_Sha512_256Copy(&sha512, NULL) != BAD_FUNC_ARG)
  10781. flag = WOLFSSL_FATAL_ERROR;
  10782. }
  10783. wc_Sha512_256Free(&sha512);
  10784. wc_Sha512_256Free(&temp);
  10785. res = TEST_RES_CHECK(flag == 0);
  10786. #endif
  10787. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10788. return res;
  10789. }
  10790. /*
  10791. * Testing wc_InitSha384()
  10792. */
  10793. static int test_wc_InitSha384(void)
  10794. {
  10795. int res = TEST_SKIPPED;
  10796. #ifdef WOLFSSL_SHA384
  10797. wc_Sha384 sha384;
  10798. int ret;
  10799. int flag = 0;
  10800. /* Test good arg. */
  10801. ret = wc_InitSha384(&sha384);
  10802. if (ret != 0) {
  10803. flag = WOLFSSL_FATAL_ERROR;
  10804. }
  10805. /* Test bad arg. */
  10806. if (!flag) {
  10807. ret = wc_InitSha384(NULL);
  10808. if (ret != BAD_FUNC_ARG) {
  10809. flag = WOLFSSL_FATAL_ERROR;
  10810. }
  10811. }
  10812. wc_Sha384Free(&sha384);
  10813. res = TEST_RES_CHECK(flag == 0);
  10814. #endif
  10815. return res;
  10816. } /* END test_wc_InitSha384 */
  10817. /*
  10818. * test wc_Sha384Update()
  10819. */
  10820. static int test_wc_Sha384Update(void)
  10821. {
  10822. int res = TEST_SKIPPED;
  10823. #ifdef WOLFSSL_SHA384
  10824. wc_Sha384 sha384;
  10825. byte hash[WC_SHA384_DIGEST_SIZE];
  10826. testVector a, b, c;
  10827. int ret;
  10828. int flag = 0;
  10829. ret = wc_InitSha384(&sha384);
  10830. if (ret != 0) {
  10831. flag = ret;
  10832. }
  10833. /* Input */
  10834. if (!flag) {
  10835. a.input = "a";
  10836. a.inLen = XSTRLEN(a.input);
  10837. ret = wc_Sha384Update(&sha384, NULL, 0);
  10838. if (ret != 0) {
  10839. flag = ret;
  10840. }
  10841. ret = wc_Sha384Update(&sha384, (byte*)a.input, 0);
  10842. if (ret != 0) {
  10843. flag = ret;
  10844. }
  10845. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  10846. if (ret != 0) {
  10847. flag = ret;
  10848. }
  10849. }
  10850. if (!flag) {
  10851. ret = wc_Sha384Final(&sha384, hash);
  10852. if (ret != 0) {
  10853. flag = ret;
  10854. }
  10855. }
  10856. /* Update input. */
  10857. if (!flag) {
  10858. a.input = "abc";
  10859. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  10860. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  10861. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  10862. "\xc8\x25\xa7";
  10863. a.inLen = XSTRLEN(a.input);
  10864. a.outLen = XSTRLEN(a.output);
  10865. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  10866. if (ret != 0) {
  10867. flag = ret;
  10868. }
  10869. }
  10870. if (!flag) {
  10871. ret = wc_Sha384Final(&sha384, hash);
  10872. if (ret != 0) {
  10873. flag = ret;
  10874. }
  10875. }
  10876. if (!flag) {
  10877. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  10878. flag = WOLFSSL_FATAL_ERROR;
  10879. }
  10880. }
  10881. /* Pass in bad values. */
  10882. if (!flag) {
  10883. b.input = NULL;
  10884. b.inLen = 0;
  10885. ret = wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen);
  10886. if (ret != 0) {
  10887. flag = ret;
  10888. }
  10889. }
  10890. if (!flag) {
  10891. c.input = NULL;
  10892. c.inLen = WC_SHA384_DIGEST_SIZE;
  10893. ret = wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen);
  10894. if (ret != BAD_FUNC_ARG) {
  10895. flag = WOLFSSL_FATAL_ERROR;
  10896. }
  10897. }
  10898. if (!flag) {
  10899. ret = wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen);
  10900. if (ret != BAD_FUNC_ARG) {
  10901. flag = WOLFSSL_FATAL_ERROR;
  10902. }
  10903. }
  10904. wc_Sha384Free(&sha384);
  10905. res = TEST_RES_CHECK(flag == 0);
  10906. #endif
  10907. return res;
  10908. } /* END test_wc_Sha384Update */
  10909. /*
  10910. * Unit test function for wc_Sha384Final();
  10911. */
  10912. static int test_wc_Sha384Final(void)
  10913. {
  10914. int res = TEST_SKIPPED;
  10915. #ifdef WOLFSSL_SHA384
  10916. wc_Sha384 sha384;
  10917. byte* hash_test[3];
  10918. byte hash1[WC_SHA384_DIGEST_SIZE];
  10919. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  10920. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  10921. int times, i, ret;
  10922. int flag = 0;
  10923. /* Initialize */
  10924. ret = wc_InitSha384(&sha384);
  10925. if (ret) {
  10926. flag = ret;
  10927. }
  10928. if (!flag) {
  10929. hash_test[0] = hash1;
  10930. hash_test[1] = hash2;
  10931. hash_test[2] = hash3;
  10932. }
  10933. times = sizeof(hash_test) / sizeof(byte*);
  10934. /* Good test args. */
  10935. for (i = 0; i < times; i++) {
  10936. if (!flag) {
  10937. ret = wc_Sha384Final(&sha384, hash_test[i]);
  10938. if (ret != 0) {
  10939. flag = WOLFSSL_FATAL_ERROR;
  10940. }
  10941. }
  10942. }
  10943. /* Test bad args. */
  10944. if (!flag) {
  10945. ret = wc_Sha384Final(NULL, NULL);
  10946. if (ret != BAD_FUNC_ARG) {
  10947. flag = WOLFSSL_FATAL_ERROR;
  10948. }
  10949. }
  10950. if (!flag) {
  10951. ret = wc_Sha384Final(NULL, hash1);
  10952. if (ret != BAD_FUNC_ARG) {
  10953. flag = WOLFSSL_FATAL_ERROR;
  10954. }
  10955. }
  10956. if (!flag) {
  10957. ret = wc_Sha384Final(&sha384, NULL);
  10958. if (ret != BAD_FUNC_ARG) {
  10959. flag = WOLFSSL_FATAL_ERROR;
  10960. }
  10961. }
  10962. wc_Sha384Free(&sha384);
  10963. res = TEST_RES_CHECK(flag == 0);
  10964. #endif
  10965. return res;
  10966. } /* END test_wc_Sha384Final */
  10967. /*
  10968. * Unit test function for wc_Sha384GetFlags()
  10969. */
  10970. static int test_wc_Sha384GetFlags(void)
  10971. {
  10972. int res = TEST_SKIPPED;
  10973. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  10974. wc_Sha384 sha384;
  10975. word32 flags = 0;
  10976. int flag = 0;
  10977. /* Initialize */
  10978. flag = wc_InitSha384(&sha384);
  10979. if (flag == 0) {
  10980. flag = wc_Sha384GetFlags(&sha384, &flags);
  10981. }
  10982. if (flag == 0) {
  10983. if (flags & WC_HASH_FLAG_ISCOPY) {
  10984. flag = 0;
  10985. }
  10986. }
  10987. wc_Sha384Free(&sha384);
  10988. res = TEST_RES_CHECK(flag == 0);
  10989. #endif
  10990. return res;
  10991. } /* END test_wc_Sha384GetFlags */
  10992. /*
  10993. * Unit test function for wc_Sha384FinalRaw()
  10994. */
  10995. static int test_wc_Sha384FinalRaw(void)
  10996. {
  10997. int res = TEST_SKIPPED;
  10998. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  10999. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  11000. !defined(WOLFSSL_NO_HASH_RAW)
  11001. wc_Sha384 sha384;
  11002. byte* hash_test[3];
  11003. byte hash1[WC_SHA384_DIGEST_SIZE];
  11004. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  11005. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  11006. int times, i, ret;
  11007. int flag = 0;
  11008. /* Initialize */
  11009. ret = wc_InitSha384(&sha384);
  11010. if (ret != 0) {
  11011. flag = ret;
  11012. }
  11013. if (!flag) {
  11014. hash_test[0] = hash1;
  11015. hash_test[1] = hash2;
  11016. hash_test[2] = hash3;
  11017. }
  11018. times = sizeof(hash_test) / sizeof(byte*);
  11019. /* Good test args. */
  11020. for (i = 0; i < times; i++) {
  11021. if (!flag) {
  11022. ret = wc_Sha384FinalRaw(&sha384, hash_test[i]);
  11023. if (ret != 0) {
  11024. flag = WOLFSSL_FATAL_ERROR;
  11025. }
  11026. }
  11027. }
  11028. /* Test bad args. */
  11029. if (!flag ) {
  11030. ret = wc_Sha384FinalRaw(NULL, NULL);
  11031. if (ret != BAD_FUNC_ARG) {
  11032. flag = WOLFSSL_FATAL_ERROR;
  11033. }
  11034. }
  11035. if (!flag) {
  11036. ret = wc_Sha384FinalRaw(NULL, hash1);
  11037. if (ret != BAD_FUNC_ARG) {
  11038. flag = WOLFSSL_FATAL_ERROR;
  11039. }
  11040. }
  11041. if (!flag) {
  11042. ret = wc_Sha384FinalRaw(&sha384, NULL);
  11043. if (ret != BAD_FUNC_ARG) {
  11044. flag = WOLFSSL_FATAL_ERROR;
  11045. }
  11046. }
  11047. wc_Sha384Free(&sha384);
  11048. res = TEST_RES_CHECK(flag == 0);
  11049. #endif
  11050. return res;
  11051. } /* END test_wc_Sha384FinalRaw */
  11052. /*
  11053. * Unit test function for wc_Sha384Free()
  11054. */
  11055. static int test_wc_Sha384Free(void)
  11056. {
  11057. int res = TEST_SKIPPED;
  11058. #ifdef WOLFSSL_SHA384
  11059. wc_Sha384Free(NULL);
  11060. res = TEST_RES_CHECK(1);
  11061. #endif
  11062. return res;
  11063. } /* END test_wc_Sha384Free */
  11064. /*
  11065. * Unit test function for wc_Sha384GetHash()
  11066. */
  11067. static int test_wc_Sha384GetHash(void)
  11068. {
  11069. int res = TEST_SKIPPED;
  11070. #ifdef WOLFSSL_SHA384
  11071. wc_Sha384 sha384;
  11072. byte hash1[WC_SHA384_DIGEST_SIZE];
  11073. int flag = 0;
  11074. /* Initialize */
  11075. flag = wc_InitSha384(&sha384);
  11076. if (flag == 0) {
  11077. flag = wc_Sha384GetHash(&sha384, hash1);
  11078. }
  11079. /*test bad arguments*/
  11080. if (flag == 0) {
  11081. flag = wc_Sha384GetHash(NULL, NULL);
  11082. if (flag == BAD_FUNC_ARG) {
  11083. flag = 0;
  11084. }
  11085. }
  11086. if (flag == 0) {
  11087. flag = wc_Sha384GetHash(NULL, hash1);
  11088. if (flag == BAD_FUNC_ARG) {
  11089. flag = 0;
  11090. }
  11091. }
  11092. if (flag == 0) {
  11093. flag = wc_Sha384GetHash(&sha384, NULL);
  11094. if (flag == BAD_FUNC_ARG) {
  11095. flag = 0;
  11096. }
  11097. }
  11098. wc_Sha384Free(&sha384);
  11099. res = TEST_RES_CHECK(flag == 0);
  11100. #endif
  11101. return res;
  11102. } /* END test_wc_Sha384GetHash */
  11103. /*
  11104. * Unit test function for wc_Sha384Copy()
  11105. */
  11106. static int test_wc_Sha384Copy(void)
  11107. {
  11108. int res = TEST_SKIPPED;
  11109. #ifdef WOLFSSL_SHA384
  11110. wc_Sha384 sha384;
  11111. wc_Sha384 temp;
  11112. int flag = 0;
  11113. /* Initialize */
  11114. flag = wc_InitSha384(&sha384);
  11115. if (flag == 0) {
  11116. flag = wc_InitSha384(&temp);
  11117. }
  11118. if (flag == 0) {
  11119. flag = wc_Sha384Copy(&sha384, &temp);
  11120. }
  11121. /*test bad arguments*/
  11122. if (flag == 0) {
  11123. flag = wc_Sha384Copy(NULL, NULL);
  11124. if (flag == BAD_FUNC_ARG) {
  11125. flag = 0;
  11126. }
  11127. }
  11128. if (flag == 0) {
  11129. flag = wc_Sha384Copy(NULL, &temp);
  11130. if (flag == BAD_FUNC_ARG) {
  11131. flag = 0;
  11132. }
  11133. }
  11134. if (flag == 0) {
  11135. flag = wc_Sha384Copy(&sha384, NULL);
  11136. if (flag == BAD_FUNC_ARG) {
  11137. flag = 0;
  11138. }
  11139. }
  11140. wc_Sha384Free(&sha384);
  11141. wc_Sha384Free(&temp);
  11142. res = TEST_RES_CHECK(flag == 0);
  11143. #endif
  11144. return res;
  11145. } /* END test_wc_Sha384Copy */
  11146. /*
  11147. * Testing wc_InitSha224();
  11148. */
  11149. static int test_wc_InitSha224(void)
  11150. {
  11151. int res = TEST_SKIPPED;
  11152. #ifdef WOLFSSL_SHA224
  11153. wc_Sha224 sha224;
  11154. int ret;
  11155. int flag = 0;
  11156. /* Test good arg. */
  11157. ret = wc_InitSha224(&sha224);
  11158. if (ret != 0) {
  11159. flag = WOLFSSL_FATAL_ERROR;
  11160. }
  11161. /* Test bad arg. */
  11162. if (!flag) {
  11163. ret = wc_InitSha224(NULL);
  11164. if (ret != BAD_FUNC_ARG) {
  11165. flag = WOLFSSL_FATAL_ERROR;
  11166. }
  11167. }
  11168. wc_Sha224Free(&sha224);
  11169. res = TEST_RES_CHECK(flag == 0);
  11170. #endif
  11171. return res;
  11172. } /* END test_wc_InitSha224 */
  11173. /*
  11174. * Unit test on wc_Sha224Update
  11175. */
  11176. static int test_wc_Sha224Update(void)
  11177. {
  11178. int res = TEST_SKIPPED;
  11179. #ifdef WOLFSSL_SHA224
  11180. wc_Sha224 sha224;
  11181. byte hash[WC_SHA224_DIGEST_SIZE];
  11182. testVector a, b, c;
  11183. int ret;
  11184. int flag = 0;
  11185. ret = wc_InitSha224(&sha224);
  11186. if (ret != 0) {
  11187. flag = ret;
  11188. }
  11189. /* Input. */
  11190. if (!flag) {
  11191. a.input = "a";
  11192. a.inLen = XSTRLEN(a.input);
  11193. ret = wc_Sha224Update(&sha224, NULL, 0);
  11194. if (ret != 0) {
  11195. flag = ret;
  11196. }
  11197. ret = wc_Sha224Update(&sha224, (byte*)a.input, 0);
  11198. if (ret != 0) {
  11199. flag = ret;
  11200. }
  11201. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  11202. if (ret != 0) {
  11203. flag = ret;
  11204. }
  11205. }
  11206. if (!flag) {
  11207. ret = wc_Sha224Final(&sha224, hash);
  11208. if (ret != 0) {
  11209. flag = ret;
  11210. }
  11211. }
  11212. /* Update input. */
  11213. if (!flag) {
  11214. a.input = "abc";
  11215. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  11216. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  11217. a.inLen = XSTRLEN(a.input);
  11218. a.outLen = XSTRLEN(a.output);
  11219. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  11220. if (ret != 0) {
  11221. flag = ret;
  11222. }
  11223. }
  11224. if (!flag) {
  11225. ret = wc_Sha224Final(&sha224, hash);
  11226. if (ret != 0) {
  11227. flag = ret;
  11228. }
  11229. }
  11230. if (!flag) {
  11231. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  11232. flag = WOLFSSL_FATAL_ERROR;
  11233. }
  11234. }
  11235. /* Pass in bad values. */
  11236. if (!flag) {
  11237. b.input = NULL;
  11238. b.inLen = 0;
  11239. ret = wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen);
  11240. if (ret != 0) {
  11241. flag = ret;
  11242. }
  11243. }
  11244. if (!flag) {
  11245. c.input = NULL;
  11246. c.inLen = WC_SHA224_DIGEST_SIZE;
  11247. ret = wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen);
  11248. if (ret != BAD_FUNC_ARG) {
  11249. flag = WOLFSSL_FATAL_ERROR;
  11250. }
  11251. }
  11252. if (!flag) {
  11253. ret = wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen);
  11254. if (ret != BAD_FUNC_ARG) {
  11255. flag = WOLFSSL_FATAL_ERROR;
  11256. }
  11257. }
  11258. wc_Sha224Free(&sha224);
  11259. res = TEST_RES_CHECK(flag == 0);
  11260. #endif
  11261. return res;
  11262. } /* END test_wc_Sha224Update */
  11263. /*
  11264. * Unit test for wc_Sha224Final();
  11265. */
  11266. static int test_wc_Sha224Final(void)
  11267. {
  11268. int res = TEST_SKIPPED;
  11269. #ifdef WOLFSSL_SHA224
  11270. wc_Sha224 sha224;
  11271. byte* hash_test[3];
  11272. byte hash1[WC_SHA224_DIGEST_SIZE];
  11273. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  11274. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  11275. int times, i, ret;
  11276. int flag = 0;
  11277. /* Initialize */
  11278. ret = wc_InitSha224(&sha224);
  11279. if (ret) {
  11280. flag = ret;
  11281. }
  11282. if (!flag) {
  11283. hash_test[0] = hash1;
  11284. hash_test[1] = hash2;
  11285. hash_test[2] = hash3;
  11286. }
  11287. times = sizeof(hash_test) / sizeof(byte*);
  11288. /* Good test args. */
  11289. /* Testing oversized buffers. */
  11290. for (i = 0; i < times; i++) {
  11291. if (!flag) {
  11292. ret = wc_Sha224Final(&sha224, hash_test[i]);
  11293. if (ret != 0) {
  11294. flag = WOLFSSL_FATAL_ERROR;
  11295. }
  11296. }
  11297. }
  11298. /* Test bad args. */
  11299. if (!flag) {
  11300. ret = wc_Sha224Final(NULL, NULL);
  11301. if (ret != BAD_FUNC_ARG) {
  11302. flag = WOLFSSL_FATAL_ERROR;
  11303. }
  11304. }
  11305. if (!flag) {
  11306. ret = wc_Sha224Final(NULL, hash1);
  11307. if (ret != BAD_FUNC_ARG) {
  11308. flag = WOLFSSL_FATAL_ERROR;
  11309. }
  11310. }
  11311. if (!flag) {
  11312. ret = wc_Sha224Final(&sha224, NULL);
  11313. if (ret != BAD_FUNC_ARG) {
  11314. flag = WOLFSSL_FATAL_ERROR;
  11315. }
  11316. }
  11317. wc_Sha224Free(&sha224);
  11318. res = TEST_RES_CHECK(flag == 0);
  11319. #endif
  11320. return res;
  11321. } /* END test_wc_Sha224Final */
  11322. /*
  11323. * Unit test function for wc_Sha224SetFlags()
  11324. */
  11325. static int test_wc_Sha224SetFlags(void)
  11326. {
  11327. int res = TEST_SKIPPED;
  11328. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  11329. wc_Sha224 sha224;
  11330. word32 flags = 0;
  11331. int flag = 0;
  11332. /* Initialize */
  11333. flag = wc_InitSha224(&sha224);
  11334. if (flag == 0) {
  11335. flag = wc_Sha224SetFlags(&sha224, flags);
  11336. }
  11337. if (flag == 0) {
  11338. if (flags & WC_HASH_FLAG_ISCOPY) {
  11339. flag = 0;
  11340. }
  11341. }
  11342. wc_Sha224Free(&sha224);
  11343. res = TEST_RES_CHECK(flag == 0);
  11344. #endif
  11345. return res;
  11346. } /* END test_wc_Sha224SetFlags */
  11347. /*
  11348. * Unit test function for wc_Sha224GetFlags()
  11349. */
  11350. static int test_wc_Sha224GetFlags(void)
  11351. {
  11352. int res = TEST_SKIPPED;
  11353. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  11354. wc_Sha224 sha224;
  11355. word32 flags = 0;
  11356. int flag = 0;
  11357. /* Initialize */
  11358. flag = wc_InitSha224(&sha224);
  11359. if (flag == 0) {
  11360. flag = wc_Sha224GetFlags(&sha224, &flags);
  11361. }
  11362. if (flag == 0) {
  11363. if (flags & WC_HASH_FLAG_ISCOPY) {
  11364. flag = 0;
  11365. }
  11366. }
  11367. wc_Sha224Free(&sha224);
  11368. res = TEST_RES_CHECK(flag == 0);
  11369. #endif
  11370. return res;
  11371. } /* END test_wc_Sha224GetFlags */
  11372. /*
  11373. * Unit test function for wc_Sha224Free()
  11374. */
  11375. static int test_wc_Sha224Free(void)
  11376. {
  11377. int res = TEST_SKIPPED;
  11378. #ifdef WOLFSSL_SHA224
  11379. wc_Sha224Free(NULL);
  11380. res = TEST_RES_CHECK(1);
  11381. #endif
  11382. return res;
  11383. } /* END test_wc_Sha224Free */
  11384. /*
  11385. * Unit test function for wc_Sha224GetHash()
  11386. */
  11387. static int test_wc_Sha224GetHash(void)
  11388. {
  11389. int res = TEST_SKIPPED;
  11390. #ifdef WOLFSSL_SHA224
  11391. wc_Sha224 sha224;
  11392. byte hash1[WC_SHA224_DIGEST_SIZE];
  11393. int flag = 0;
  11394. /* Initialize */
  11395. flag = wc_InitSha224(&sha224);
  11396. if (flag == 0) {
  11397. flag = wc_Sha224GetHash(&sha224, hash1);
  11398. }
  11399. /*test bad arguments*/
  11400. if (flag == 0) {
  11401. flag = wc_Sha224GetHash(NULL, NULL);
  11402. if (flag == BAD_FUNC_ARG) {
  11403. flag = 0;
  11404. }
  11405. }
  11406. if (flag == 0) {
  11407. flag = wc_Sha224GetHash(NULL, hash1);
  11408. if (flag == BAD_FUNC_ARG) {
  11409. flag = 0;
  11410. }
  11411. }
  11412. if (flag == 0) {
  11413. flag = wc_Sha224GetHash(&sha224, NULL);
  11414. if (flag == BAD_FUNC_ARG) {
  11415. flag = 0;
  11416. }
  11417. }
  11418. wc_Sha224Free(&sha224);
  11419. res = TEST_RES_CHECK(flag == 0);
  11420. #endif
  11421. return res;
  11422. } /* END test_wc_Sha224GetHash */
  11423. /*
  11424. * Unit test function for wc_Sha224Copy()
  11425. */
  11426. static int test_wc_Sha224Copy(void)
  11427. {
  11428. int res = TEST_SKIPPED;
  11429. #ifdef WOLFSSL_SHA224
  11430. wc_Sha224 sha224;
  11431. wc_Sha224 temp;
  11432. int flag = 0;
  11433. /* Initialize */
  11434. flag = wc_InitSha224(&sha224);
  11435. if (flag == 0) {
  11436. flag = wc_InitSha224(&temp);
  11437. }
  11438. if (flag == 0) {
  11439. flag = wc_Sha224Copy(&sha224, &temp);
  11440. }
  11441. /*test bad arguments*/
  11442. if (flag == 0) {
  11443. flag = wc_Sha224Copy(NULL, NULL);
  11444. if (flag == BAD_FUNC_ARG) {
  11445. flag = 0;
  11446. }
  11447. }
  11448. if (flag == 0) {
  11449. flag = wc_Sha224Copy(NULL, &temp);
  11450. if (flag == BAD_FUNC_ARG) {
  11451. flag = 0;
  11452. }
  11453. }
  11454. if (flag == 0) {
  11455. flag = wc_Sha224Copy(&sha224, NULL);
  11456. if (flag == BAD_FUNC_ARG) {
  11457. flag = 0;
  11458. }
  11459. }
  11460. wc_Sha224Free(&sha224);
  11461. wc_Sha224Free(&temp);
  11462. res = TEST_RES_CHECK(flag == 0);
  11463. #endif
  11464. return res;
  11465. } /* END test_wc_Sha224Copy */
  11466. /*
  11467. * Testing wc_InitRipeMd()
  11468. */
  11469. static int test_wc_InitRipeMd(void)
  11470. {
  11471. int res = TEST_SKIPPED;
  11472. #ifdef WOLFSSL_RIPEMD
  11473. RipeMd ripemd;
  11474. int ret;
  11475. int flag = 0;
  11476. /* Test good arg. */
  11477. ret = wc_InitRipeMd(&ripemd);
  11478. if (ret != 0) {
  11479. flag = WOLFSSL_FATAL_ERROR;
  11480. }
  11481. /* Test bad arg. */
  11482. if (!flag) {
  11483. ret = wc_InitRipeMd(NULL);
  11484. if (ret != BAD_FUNC_ARG) {
  11485. flag = WOLFSSL_FATAL_ERROR;
  11486. }
  11487. }
  11488. res = TEST_RES_CHECK(flag == 0);
  11489. #endif
  11490. return res;
  11491. } /* END test_wc_InitRipeMd */
  11492. /*
  11493. * Testing wc_RipeMdUpdate()
  11494. */
  11495. static int test_wc_RipeMdUpdate(void)
  11496. {
  11497. int res = TEST_SKIPPED;
  11498. #ifdef WOLFSSL_RIPEMD
  11499. RipeMd ripemd;
  11500. byte hash[RIPEMD_DIGEST_SIZE];
  11501. testVector a, b, c;
  11502. int ret;
  11503. int flag = 0;
  11504. ret = wc_InitRipeMd(&ripemd);
  11505. if (ret != 0) {
  11506. flag = ret;
  11507. }
  11508. /* Input */
  11509. if (!flag) {
  11510. a.input = "a";
  11511. a.inLen = XSTRLEN(a.input);
  11512. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  11513. if (ret != 0) {
  11514. flag = ret;
  11515. }
  11516. }
  11517. if (!flag) {
  11518. ret = wc_RipeMdFinal(&ripemd, hash);
  11519. if (ret != 0) {
  11520. flag = ret;
  11521. }
  11522. }
  11523. /* Update input. */
  11524. if (!flag) {
  11525. a.input = "abc";
  11526. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  11527. "\xb0\x87\xf1\x5a\x0b\xfc";
  11528. a.inLen = XSTRLEN(a.input);
  11529. a.outLen = XSTRLEN(a.output);
  11530. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  11531. if (ret != 0) {
  11532. flag = ret;
  11533. }
  11534. }
  11535. if (!flag) {
  11536. ret = wc_RipeMdFinal(&ripemd, hash);
  11537. if (ret != 0) {
  11538. flag = ret;
  11539. }
  11540. }
  11541. if (!flag) {
  11542. if (XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE) != 0) {
  11543. flag = WOLFSSL_FATAL_ERROR;
  11544. }
  11545. }
  11546. /* Pass in bad values. */
  11547. if (!flag) {
  11548. b.input = NULL;
  11549. b.inLen = 0;
  11550. ret = wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen);
  11551. if (ret != 0) {
  11552. flag = ret;
  11553. }
  11554. }
  11555. if (!flag) {
  11556. c.input = NULL;
  11557. c.inLen = RIPEMD_DIGEST_SIZE;
  11558. ret = wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen);
  11559. if (ret != BAD_FUNC_ARG) {
  11560. flag = WOLFSSL_FATAL_ERROR;
  11561. }
  11562. }
  11563. if (!flag) {
  11564. ret = wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  11565. if (ret != BAD_FUNC_ARG) {
  11566. flag = WOLFSSL_FATAL_ERROR;
  11567. }
  11568. }
  11569. res = TEST_RES_CHECK(flag == 0);
  11570. #endif
  11571. return res;
  11572. } /* END test_wc_RipeMdUdpate */
  11573. /*
  11574. * Unit test function for wc_RipeMdFinal()
  11575. */
  11576. static int test_wc_RipeMdFinal(void)
  11577. {
  11578. int res = TEST_SKIPPED;
  11579. #ifdef WOLFSSL_RIPEMD
  11580. RipeMd ripemd;
  11581. byte* hash_test[3];
  11582. byte hash1[RIPEMD_DIGEST_SIZE];
  11583. byte hash2[2*RIPEMD_DIGEST_SIZE];
  11584. byte hash3[5*RIPEMD_DIGEST_SIZE];
  11585. int times, i, ret;
  11586. int flag = 0;
  11587. /* Initialize */
  11588. ret = wc_InitRipeMd(&ripemd);
  11589. if (ret != 0) {
  11590. flag = ret;
  11591. }
  11592. if (!flag) {
  11593. hash_test[0] = hash1;
  11594. hash_test[1] = hash2;
  11595. hash_test[2] = hash3;
  11596. }
  11597. times = sizeof(hash_test) / sizeof(byte*);
  11598. /* Testing oversized buffers. */
  11599. for (i = 0; i < times; i++) {
  11600. if (!flag) {
  11601. ret = wc_RipeMdFinal(&ripemd, hash_test[i]);
  11602. if (ret != 0) {
  11603. flag = WOLFSSL_FATAL_ERROR;
  11604. }
  11605. }
  11606. }
  11607. /* Test bad args. */
  11608. if (!flag) {
  11609. ret = wc_RipeMdFinal(NULL, NULL);
  11610. if (ret != BAD_FUNC_ARG) {
  11611. flag = WOLFSSL_FATAL_ERROR;
  11612. }
  11613. }
  11614. if (!flag) {
  11615. ret = wc_RipeMdFinal(NULL, hash1);
  11616. if (ret != BAD_FUNC_ARG) {
  11617. flag = WOLFSSL_FATAL_ERROR;
  11618. }
  11619. }
  11620. if (!flag) {
  11621. ret = wc_RipeMdFinal(&ripemd, NULL);
  11622. if (ret != BAD_FUNC_ARG) {
  11623. flag = WOLFSSL_FATAL_ERROR;
  11624. }
  11625. }
  11626. res = TEST_RES_CHECK(flag == 0);
  11627. #endif
  11628. return res;
  11629. } /* END test_wc_RipeMdFinal */
  11630. /*
  11631. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  11632. * wc_InitSha3_512
  11633. */
  11634. static int test_wc_InitSha3(void)
  11635. {
  11636. int res = TEST_SKIPPED;
  11637. #if defined(WOLFSSL_SHA3)
  11638. wc_Sha3 sha3;
  11639. int ret = 0;
  11640. (void)sha3;
  11641. #if !defined(WOLFSSL_NOSHA3_224)
  11642. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  11643. /* Test bad args. */
  11644. if (ret == 0) {
  11645. ret = wc_InitSha3_224(NULL, HEAP_HINT, testDevId);
  11646. if (ret == BAD_FUNC_ARG) {
  11647. ret = 0;
  11648. }
  11649. else if (ret == 0) {
  11650. ret = WOLFSSL_FATAL_ERROR;
  11651. }
  11652. }
  11653. wc_Sha3_224_Free(&sha3);
  11654. #endif /* NOSHA3_224 */
  11655. #if !defined(WOLFSSL_NOSHA3_256)
  11656. if (ret == 0) {
  11657. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  11658. /* Test bad args. */
  11659. if (ret == 0) {
  11660. ret = wc_InitSha3_256(NULL, HEAP_HINT, testDevId);
  11661. if (ret == BAD_FUNC_ARG) {
  11662. ret = 0;
  11663. }
  11664. else if (ret == 0) {
  11665. ret = WOLFSSL_FATAL_ERROR;
  11666. }
  11667. }
  11668. wc_Sha3_256_Free(&sha3);
  11669. } /* END sha3_256 */
  11670. #endif /* NOSHA3_256 */
  11671. #if !defined(WOLFSSL_NOSHA3_384)
  11672. if (ret == 0) {
  11673. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  11674. /* Test bad args. */
  11675. if (ret == 0) {
  11676. ret = wc_InitSha3_384(NULL, HEAP_HINT, testDevId);
  11677. if (ret == BAD_FUNC_ARG) {
  11678. ret = 0;
  11679. }
  11680. else if (ret == 0) {
  11681. ret = WOLFSSL_FATAL_ERROR;
  11682. }
  11683. }
  11684. wc_Sha3_384_Free(&sha3);
  11685. } /* END sha3_384 */
  11686. #endif /* NOSHA3_384 */
  11687. #if !defined(WOLFSSL_NOSHA3_512)
  11688. if (ret == 0) {
  11689. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  11690. /* Test bad args. */
  11691. if (ret == 0) {
  11692. ret = wc_InitSha3_512(NULL, HEAP_HINT, testDevId);
  11693. if (ret == BAD_FUNC_ARG) {
  11694. ret = 0;
  11695. }
  11696. else if (ret == 0) {
  11697. ret = WOLFSSL_FATAL_ERROR;
  11698. }
  11699. }
  11700. wc_Sha3_512_Free(&sha3);
  11701. } /* END sha3_512 */
  11702. #endif /* NOSHA3_512 */
  11703. res = TEST_RES_CHECK(ret == 0);
  11704. #endif
  11705. return res;
  11706. } /* END test_wc_InitSha3 */
  11707. /*
  11708. * Testing wc_Sha3_Update()
  11709. */
  11710. static int testing_wc_Sha3_Update(void)
  11711. {
  11712. int res = TEST_SKIPPED;
  11713. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  11714. !defined(WOLFSSL_AFALG_XILINX)
  11715. wc_Sha3 sha3;
  11716. byte msg[] = "Everybody's working for the weekend.";
  11717. byte msg2[] = "Everybody gets Friday off.";
  11718. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  11719. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  11720. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  11721. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  11722. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  11723. word32 msglen = sizeof(msg) - 1;
  11724. word32 msg2len = sizeof(msg2);
  11725. word32 msgCmplen = sizeof(msgCmp);
  11726. int ret = 0;
  11727. #if !defined(WOLFSSL_NOSHA3_224)
  11728. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  11729. if (ret != 0) {
  11730. return TEST_FAIL;
  11731. }
  11732. ret = wc_Sha3_224_Update(&sha3, msg, msglen);
  11733. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  11734. ret = WOLFSSL_FATAL_ERROR;
  11735. }
  11736. if (ret == 0) {
  11737. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  11738. if (ret == 0 && XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  11739. ret = WOLFSSL_FATAL_ERROR;
  11740. }
  11741. }
  11742. /* Pass bad args. */
  11743. if (ret == 0) {
  11744. ret = wc_Sha3_224_Update(NULL, msg2, msg2len);
  11745. if (ret == BAD_FUNC_ARG) {
  11746. ret = wc_Sha3_224_Update(&sha3, NULL, 5);
  11747. }
  11748. if (ret == BAD_FUNC_ARG) {
  11749. wc_Sha3_224_Free(&sha3);
  11750. if (wc_InitSha3_224(&sha3, HEAP_HINT, testDevId)) {
  11751. return TEST_FAIL;
  11752. }
  11753. ret = wc_Sha3_224_Update(&sha3, NULL, 0);
  11754. if (ret == 0) {
  11755. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  11756. }
  11757. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  11758. ret = WOLFSSL_FATAL_ERROR;
  11759. }
  11760. }
  11761. }
  11762. wc_Sha3_224_Free(&sha3);
  11763. #endif /* SHA3_224 */
  11764. #if !defined(WOLFSSL_NOSHA3_256)
  11765. if (ret == 0) {
  11766. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  11767. if (ret != 0) {
  11768. return TEST_FAIL;
  11769. }
  11770. ret = wc_Sha3_256_Update(&sha3, msg, msglen);
  11771. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  11772. ret = WOLFSSL_FATAL_ERROR;
  11773. }
  11774. if (ret == 0) {
  11775. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  11776. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  11777. ret = WOLFSSL_FATAL_ERROR;
  11778. }
  11779. }
  11780. /* Pass bad args. */
  11781. if (ret == 0) {
  11782. ret = wc_Sha3_256_Update(NULL, msg2, msg2len);
  11783. if (ret == BAD_FUNC_ARG) {
  11784. ret = wc_Sha3_256_Update(&sha3, NULL, 5);
  11785. }
  11786. if (ret == BAD_FUNC_ARG) {
  11787. wc_Sha3_256_Free(&sha3);
  11788. if (wc_InitSha3_256(&sha3, HEAP_HINT, testDevId)) {
  11789. return TEST_FAIL;
  11790. }
  11791. ret = wc_Sha3_256_Update(&sha3, NULL, 0);
  11792. if (ret == 0) {
  11793. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  11794. }
  11795. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  11796. ret = WOLFSSL_FATAL_ERROR;
  11797. }
  11798. }
  11799. }
  11800. wc_Sha3_256_Free(&sha3);
  11801. }
  11802. #endif /* SHA3_256 */
  11803. #if !defined(WOLFSSL_NOSHA3_384)
  11804. if (ret == 0) {
  11805. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  11806. if (ret != 0) {
  11807. return TEST_FAIL;
  11808. }
  11809. ret = wc_Sha3_384_Update(&sha3, msg, msglen);
  11810. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  11811. ret = WOLFSSL_FATAL_ERROR;
  11812. }
  11813. if (ret == 0) {
  11814. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  11815. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  11816. ret = WOLFSSL_FATAL_ERROR;
  11817. }
  11818. }
  11819. /* Pass bad args. */
  11820. if (ret == 0) {
  11821. ret = wc_Sha3_384_Update(NULL, msg2, msg2len);
  11822. if (ret == BAD_FUNC_ARG) {
  11823. ret = wc_Sha3_384_Update(&sha3, NULL, 5);
  11824. }
  11825. if (ret == BAD_FUNC_ARG) {
  11826. wc_Sha3_384_Free(&sha3);
  11827. if (wc_InitSha3_384(&sha3, HEAP_HINT, testDevId)) {
  11828. return TEST_FAIL;
  11829. }
  11830. ret = wc_Sha3_384_Update(&sha3, NULL, 0);
  11831. if (ret == 0) {
  11832. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  11833. }
  11834. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  11835. ret = WOLFSSL_FATAL_ERROR;
  11836. }
  11837. }
  11838. }
  11839. wc_Sha3_384_Free(&sha3);
  11840. }
  11841. #endif /* SHA3_384 */
  11842. #if !defined(WOLFSSL_NOSHA3_512)
  11843. if (ret == 0) {
  11844. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  11845. if (ret != 0) {
  11846. return TEST_FAIL;
  11847. }
  11848. ret = wc_Sha3_512_Update(&sha3, msg, msglen);
  11849. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  11850. ret = WOLFSSL_FATAL_ERROR;
  11851. }
  11852. if (ret == 0) {
  11853. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  11854. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  11855. ret = WOLFSSL_FATAL_ERROR;
  11856. }
  11857. }
  11858. /* Pass bad args. */
  11859. if (ret == 0) {
  11860. ret = wc_Sha3_512_Update(NULL, msg2, msg2len);
  11861. if (ret == BAD_FUNC_ARG) {
  11862. ret = wc_Sha3_512_Update(&sha3, NULL, 5);
  11863. }
  11864. if (ret == BAD_FUNC_ARG) {
  11865. wc_Sha3_512_Free(&sha3);
  11866. if (wc_InitSha3_512(&sha3, HEAP_HINT, testDevId)) {
  11867. return TEST_FAIL;
  11868. }
  11869. ret = wc_Sha3_512_Update(&sha3, NULL, 0);
  11870. if (ret == 0) {
  11871. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  11872. }
  11873. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  11874. ret = WOLFSSL_FATAL_ERROR;
  11875. }
  11876. }
  11877. }
  11878. wc_Sha3_512_Free(&sha3);
  11879. }
  11880. #endif /* SHA3_512 */
  11881. res = TEST_RES_CHECK(ret == 0);
  11882. #endif /* WOLFSSL_SHA3 */
  11883. return res;
  11884. } /* END testing_wc_Sha3_Update */
  11885. /*
  11886. * Testing wc_Sha3_224_Final()
  11887. */
  11888. static int test_wc_Sha3_224_Final(void)
  11889. {
  11890. int res = TEST_SKIPPED;
  11891. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  11892. wc_Sha3 sha3;
  11893. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  11894. "nopnopq";
  11895. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  11896. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  11897. "\x64\xea\xd0\xfc\xce\x33";
  11898. byte hash[WC_SHA3_224_DIGEST_SIZE];
  11899. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  11900. int ret = 0;
  11901. /* Init stack variables. */
  11902. XMEMSET(hash, 0, sizeof(hash));
  11903. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  11904. if (ret != 0) {
  11905. return TEST_FAIL;
  11906. }
  11907. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11908. if (ret == 0) {
  11909. ret = wc_Sha3_224_Final(&sha3, hash);
  11910. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE) != 0) {
  11911. ret = WOLFSSL_FATAL_ERROR;
  11912. }
  11913. }
  11914. /* Test bad args. */
  11915. if (ret == 0) {
  11916. ret = wc_Sha3_224_Final(NULL, hash);
  11917. if (ret == 0) {
  11918. ret = wc_Sha3_224_Final(&sha3, NULL);
  11919. }
  11920. if (ret == BAD_FUNC_ARG) {
  11921. ret = 0;
  11922. }
  11923. else if (ret == 0) {
  11924. ret = WOLFSSL_FATAL_ERROR;
  11925. }
  11926. }
  11927. wc_Sha3_224_Free(&sha3);
  11928. if (ret == 0) {
  11929. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  11930. if (ret != 0) {
  11931. return TEST_FAIL;
  11932. }
  11933. /* Init stack variables. */
  11934. XMEMSET(hash, 0, sizeof(hash));
  11935. XMEMSET(hashRet, 0, sizeof(hashRet));
  11936. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11937. if (ret == 0) {
  11938. ret = wc_Sha3_224_GetHash(&sha3, hashRet);
  11939. }
  11940. if (ret == 0) {
  11941. ret = wc_Sha3_224_Final(&sha3, hash);
  11942. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE) != 0) {
  11943. ret = WOLFSSL_FATAL_ERROR;
  11944. }
  11945. }
  11946. if (ret == 0) {
  11947. /* Test bad args. */
  11948. ret = wc_Sha3_224_GetHash(NULL, hashRet);
  11949. if (ret == BAD_FUNC_ARG) {
  11950. ret = wc_Sha3_224_GetHash(&sha3, NULL);
  11951. }
  11952. if (ret == BAD_FUNC_ARG) {
  11953. ret = 0;
  11954. }
  11955. else if (ret == 0) {
  11956. ret = WOLFSSL_FATAL_ERROR;
  11957. }
  11958. }
  11959. }
  11960. wc_Sha3_224_Free(&sha3);
  11961. res = TEST_RES_CHECK(ret == 0);
  11962. #endif
  11963. return res;
  11964. } /* END test_wc_Sha3_224_Final */
  11965. /*
  11966. * Testing wc_Sha3_256_Final()
  11967. */
  11968. static int test_wc_Sha3_256_Final(void)
  11969. {
  11970. int res = TEST_SKIPPED;
  11971. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  11972. wc_Sha3 sha3;
  11973. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  11974. "nopnopq";
  11975. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  11976. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  11977. "\xdd\x97\x49\x6d\x33\x76";
  11978. byte hash[WC_SHA3_256_DIGEST_SIZE];
  11979. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  11980. int ret = 0;
  11981. /* Init stack variables. */
  11982. XMEMSET(hash, 0, sizeof(hash));
  11983. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  11984. if (ret != 0) {
  11985. return TEST_FAIL;
  11986. }
  11987. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11988. if (ret == 0) {
  11989. ret = wc_Sha3_256_Final(&sha3, hash);
  11990. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE) != 0) {
  11991. ret = WOLFSSL_FATAL_ERROR;
  11992. }
  11993. }
  11994. /* Test bad args. */
  11995. if (ret == 0) {
  11996. ret = wc_Sha3_256_Final(NULL, hash);
  11997. if (ret == 0) {
  11998. ret = wc_Sha3_256_Final(&sha3, NULL);
  11999. }
  12000. if (ret == BAD_FUNC_ARG) {
  12001. ret = 0;
  12002. }
  12003. else if (ret == 0) {
  12004. ret = WOLFSSL_FATAL_ERROR;
  12005. }
  12006. }
  12007. wc_Sha3_256_Free(&sha3);
  12008. if (ret == 0) {
  12009. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  12010. if (ret != 0) {
  12011. return TEST_FAIL;
  12012. }
  12013. /* Init stack variables. */
  12014. XMEMSET(hash, 0, sizeof(hash));
  12015. XMEMSET(hashRet, 0, sizeof(hashRet));
  12016. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12017. if (ret == 0) {
  12018. ret = wc_Sha3_256_GetHash(&sha3, hashRet);
  12019. }
  12020. if (ret == 0) {
  12021. ret = wc_Sha3_256_Final(&sha3, hash);
  12022. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE) != 0) {
  12023. ret = WOLFSSL_FATAL_ERROR;
  12024. }
  12025. }
  12026. if (ret == 0) {
  12027. /* Test bad args. */
  12028. ret = wc_Sha3_256_GetHash(NULL, hashRet);
  12029. if (ret == BAD_FUNC_ARG) {
  12030. ret = wc_Sha3_256_GetHash(&sha3, NULL);
  12031. }
  12032. if (ret == BAD_FUNC_ARG) {
  12033. ret = 0;
  12034. }
  12035. else if (ret == 0) {
  12036. ret = WOLFSSL_FATAL_ERROR;
  12037. }
  12038. }
  12039. }
  12040. wc_Sha3_256_Free(&sha3);
  12041. res = TEST_RES_CHECK(ret == 0);
  12042. #endif
  12043. return res;
  12044. } /* END test_wc_Sha3_256_Final */
  12045. /*
  12046. * Testing wc_Sha3_384_Final()
  12047. */
  12048. static int test_wc_Sha3_384_Final(void)
  12049. {
  12050. int res = TEST_SKIPPED;
  12051. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  12052. wc_Sha3 sha3;
  12053. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12054. "nopnopq";
  12055. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  12056. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  12057. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  12058. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  12059. byte hash[WC_SHA3_384_DIGEST_SIZE];
  12060. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  12061. int ret = 0;
  12062. /* Init stack variables. */
  12063. XMEMSET(hash, 0, sizeof(hash));
  12064. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  12065. if (ret != 0) {
  12066. return TEST_FAIL;
  12067. }
  12068. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12069. if (ret == 0) {
  12070. ret = wc_Sha3_384_Final(&sha3, hash);
  12071. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE) != 0) {
  12072. ret = WOLFSSL_FATAL_ERROR;
  12073. }
  12074. }
  12075. /* Test bad args. */
  12076. if (ret == 0) {
  12077. ret = wc_Sha3_384_Final(NULL, hash);
  12078. if (ret == 0) {
  12079. ret = wc_Sha3_384_Final(&sha3, NULL);
  12080. }
  12081. if (ret == BAD_FUNC_ARG) {
  12082. ret = 0;
  12083. }
  12084. else if (ret == 0) {
  12085. ret = WOLFSSL_FATAL_ERROR;
  12086. }
  12087. }
  12088. wc_Sha3_384_Free(&sha3);
  12089. if (ret == 0) {
  12090. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  12091. if (ret != 0) {
  12092. return TEST_FAIL;
  12093. }
  12094. /* Init stack variables. */
  12095. XMEMSET(hash, 0, sizeof(hash));
  12096. XMEMSET(hashRet, 0, sizeof(hashRet));
  12097. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12098. if (ret == 0) {
  12099. ret = wc_Sha3_384_GetHash(&sha3, hashRet);
  12100. }
  12101. if (ret == 0) {
  12102. ret = wc_Sha3_384_Final(&sha3, hash);
  12103. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE) != 0) {
  12104. ret = WOLFSSL_FATAL_ERROR;
  12105. }
  12106. }
  12107. if (ret == 0) {
  12108. /* Test bad args. */
  12109. ret = wc_Sha3_384_GetHash(NULL, hashRet);
  12110. if (ret == BAD_FUNC_ARG) {
  12111. ret = wc_Sha3_384_GetHash(&sha3, NULL);
  12112. }
  12113. if (ret == BAD_FUNC_ARG) {
  12114. ret = 0;
  12115. }
  12116. else if (ret == 0) {
  12117. ret = WOLFSSL_FATAL_ERROR;
  12118. }
  12119. }
  12120. }
  12121. wc_Sha3_384_Free(&sha3);
  12122. res = TEST_RES_CHECK(ret == 0);
  12123. #endif
  12124. return res;
  12125. } /* END test_wc_Sha3_384_Final */
  12126. /*
  12127. * Testing wc_Sha3_512_Final()
  12128. */
  12129. static int test_wc_Sha3_512_Final(void)
  12130. {
  12131. int res = TEST_SKIPPED;
  12132. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  12133. !defined(WOLFSSL_NOSHA3_384)
  12134. wc_Sha3 sha3;
  12135. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12136. "nopnopq";
  12137. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  12138. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  12139. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  12140. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  12141. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  12142. byte hash[WC_SHA3_512_DIGEST_SIZE];
  12143. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  12144. int ret = 0;
  12145. /* Init stack variables. */
  12146. XMEMSET(hash, 0, sizeof(hash));
  12147. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  12148. if (ret != 0) {
  12149. return TEST_FAIL;
  12150. }
  12151. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12152. if (ret == 0) {
  12153. ret = wc_Sha3_512_Final(&sha3, hash);
  12154. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE) != 0) {
  12155. ret = WOLFSSL_FATAL_ERROR;
  12156. }
  12157. }
  12158. /* Test bad args. */
  12159. if (ret == 0) {
  12160. ret = wc_Sha3_512_Final(NULL, hash);
  12161. if (ret == 0) {
  12162. ret = wc_Sha3_384_Final(&sha3, NULL);
  12163. }
  12164. if (ret == BAD_FUNC_ARG) {
  12165. ret = 0;
  12166. }
  12167. else if (ret == 0) {
  12168. ret = WOLFSSL_FATAL_ERROR;
  12169. }
  12170. }
  12171. wc_Sha3_512_Free(&sha3);
  12172. if (ret == 0) {
  12173. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  12174. if (ret != 0) {
  12175. return TEST_FAIL;
  12176. }
  12177. /* Init stack variables. */
  12178. XMEMSET(hash, 0, sizeof(hash));
  12179. XMEMSET(hashRet, 0, sizeof(hashRet));
  12180. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12181. if (ret == 0) {
  12182. ret = wc_Sha3_512_GetHash(&sha3, hashRet);
  12183. }
  12184. if (ret == 0) {
  12185. ret = wc_Sha3_512_Final(&sha3, hash);
  12186. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE) != 0) {
  12187. ret = WOLFSSL_FATAL_ERROR;
  12188. }
  12189. }
  12190. if (ret == 0) {
  12191. /* Test bad args. */
  12192. ret = wc_Sha3_512_GetHash(NULL, hashRet);
  12193. if (ret == BAD_FUNC_ARG) {
  12194. ret = wc_Sha3_512_GetHash(&sha3, NULL);
  12195. }
  12196. if (ret == BAD_FUNC_ARG) {
  12197. ret = 0;
  12198. }
  12199. else if (ret == 0) {
  12200. ret = WOLFSSL_FATAL_ERROR;
  12201. }
  12202. }
  12203. }
  12204. wc_Sha3_512_Free(&sha3);
  12205. res = TEST_RES_CHECK(ret == 0);
  12206. #endif
  12207. return res;
  12208. } /* END test_wc_Sha3_512_Final */
  12209. /*
  12210. * Testing wc_Sha3_224_Copy()
  12211. */
  12212. static int test_wc_Sha3_224_Copy(void)
  12213. {
  12214. int res = TEST_SKIPPED;
  12215. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  12216. wc_Sha3 sha3, sha3Cpy;
  12217. const char* msg = TEST_STRING;
  12218. word32 msglen = (word32)TEST_STRING_SZ;
  12219. byte hash[WC_SHA3_224_DIGEST_SIZE];
  12220. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  12221. int ret = 0;
  12222. XMEMSET(hash, 0, sizeof(hash));
  12223. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12224. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  12225. if (ret != 0) {
  12226. return TEST_FAIL;
  12227. }
  12228. ret = wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId);
  12229. if (ret != 0) {
  12230. wc_Sha3_224_Free(&sha3);
  12231. return TEST_FAIL;
  12232. }
  12233. ret = wc_Sha3_224_Update(&sha3, (byte*)msg, msglen);
  12234. if (ret == 0) {
  12235. ret = wc_Sha3_224_Copy(&sha3Cpy, &sha3);
  12236. if (ret == 0) {
  12237. ret = wc_Sha3_224_Final(&sha3, hash);
  12238. if (ret == 0) {
  12239. ret = wc_Sha3_224_Final(&sha3Cpy, hashCpy);
  12240. }
  12241. }
  12242. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  12243. ret = WOLFSSL_FATAL_ERROR;
  12244. }
  12245. }
  12246. /* Test bad args. */
  12247. if (ret == 0) {
  12248. ret = wc_Sha3_224_Copy(NULL, &sha3);
  12249. if (ret == BAD_FUNC_ARG) {
  12250. ret = wc_Sha3_224_Copy(&sha3Cpy, NULL);
  12251. }
  12252. if (ret == BAD_FUNC_ARG) {
  12253. ret = 0;
  12254. }
  12255. else if (ret == 0) {
  12256. ret = WOLFSSL_FATAL_ERROR;
  12257. }
  12258. }
  12259. res = TEST_RES_CHECK(ret == 0);
  12260. #endif
  12261. return res;
  12262. } /* END test_wc_Sha3_224_Copy */
  12263. /*
  12264. * Testing wc_Sha3_256_Copy()
  12265. */
  12266. static int test_wc_Sha3_256_Copy(void)
  12267. {
  12268. int res = TEST_SKIPPED;
  12269. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  12270. wc_Sha3 sha3, sha3Cpy;
  12271. const char* msg = TEST_STRING;
  12272. word32 msglen = (word32)TEST_STRING_SZ;
  12273. byte hash[WC_SHA3_256_DIGEST_SIZE];
  12274. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  12275. int ret = 0;
  12276. XMEMSET(hash, 0, sizeof(hash));
  12277. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12278. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  12279. if (ret != 0) {
  12280. return TEST_FAIL;
  12281. }
  12282. ret = wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId);
  12283. if (ret != 0) {
  12284. wc_Sha3_256_Free(&sha3);
  12285. return TEST_FAIL;
  12286. }
  12287. ret = wc_Sha3_256_Update(&sha3, (byte*)msg, msglen);
  12288. if (ret == 0) {
  12289. ret = wc_Sha3_256_Copy(&sha3Cpy, &sha3);
  12290. if (ret == 0) {
  12291. ret = wc_Sha3_256_Final(&sha3, hash);
  12292. if (ret == 0) {
  12293. ret = wc_Sha3_256_Final(&sha3Cpy, hashCpy);
  12294. }
  12295. }
  12296. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  12297. ret = WOLFSSL_FATAL_ERROR;
  12298. }
  12299. }
  12300. /* Test bad args. */
  12301. if (ret == 0) {
  12302. ret = wc_Sha3_256_Copy(NULL, &sha3);
  12303. if (ret == BAD_FUNC_ARG) {
  12304. ret = wc_Sha3_256_Copy(&sha3Cpy, NULL);
  12305. }
  12306. if (ret == BAD_FUNC_ARG) {
  12307. ret = 0;
  12308. }
  12309. else if (ret == 0) {
  12310. ret = WOLFSSL_FATAL_ERROR;
  12311. }
  12312. }
  12313. res = TEST_RES_CHECK(ret == 0);
  12314. #endif
  12315. return res;
  12316. } /* END test_wc_Sha3_256_Copy */
  12317. /*
  12318. * Testing wc_Sha3_384_Copy()
  12319. */
  12320. static int test_wc_Sha3_384_Copy(void)
  12321. {
  12322. int res = TEST_SKIPPED;
  12323. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  12324. wc_Sha3 sha3, sha3Cpy;
  12325. const char* msg = TEST_STRING;
  12326. word32 msglen = (word32)TEST_STRING_SZ;
  12327. byte hash[WC_SHA3_384_DIGEST_SIZE];
  12328. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  12329. int ret = 0;
  12330. XMEMSET(hash, 0, sizeof(hash));
  12331. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12332. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  12333. if (ret != 0) {
  12334. return TEST_FAIL;
  12335. }
  12336. ret = wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId);
  12337. if (ret != 0) {
  12338. wc_Sha3_384_Free(&sha3);
  12339. return TEST_FAIL;
  12340. }
  12341. ret = wc_Sha3_384_Update(&sha3, (byte*)msg, msglen);
  12342. if (ret == 0) {
  12343. ret = wc_Sha3_384_Copy(&sha3Cpy, &sha3);
  12344. if (ret == 0) {
  12345. ret = wc_Sha3_384_Final(&sha3, hash);
  12346. if (ret == 0) {
  12347. ret = wc_Sha3_384_Final(&sha3Cpy, hashCpy);
  12348. }
  12349. }
  12350. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  12351. ret = WOLFSSL_FATAL_ERROR;
  12352. }
  12353. }
  12354. /* Test bad args. */
  12355. if (ret == 0) {
  12356. ret = wc_Sha3_384_Copy(NULL, &sha3);
  12357. if (ret == BAD_FUNC_ARG) {
  12358. ret = wc_Sha3_384_Copy(&sha3Cpy, NULL);
  12359. }
  12360. if (ret == BAD_FUNC_ARG) {
  12361. ret = 0;
  12362. }
  12363. else if (ret == 0) {
  12364. ret = WOLFSSL_FATAL_ERROR;
  12365. }
  12366. }
  12367. res = TEST_RES_CHECK(ret == 0);
  12368. #endif
  12369. return res;
  12370. } /* END test_wc_Sha3_384_Copy */
  12371. /*
  12372. * Testing wc_Sha3_512_Copy()
  12373. */
  12374. static int test_wc_Sha3_512_Copy(void)
  12375. {
  12376. int res = TEST_SKIPPED;
  12377. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  12378. wc_Sha3 sha3, sha3Cpy;
  12379. const char* msg = TEST_STRING;
  12380. word32 msglen = (word32)TEST_STRING_SZ;
  12381. byte hash[WC_SHA3_512_DIGEST_SIZE];
  12382. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  12383. int ret = 0;
  12384. XMEMSET(hash, 0, sizeof(hash));
  12385. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12386. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  12387. if (ret != 0) {
  12388. return TEST_FAIL;
  12389. }
  12390. ret = wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId);
  12391. if (ret != 0) {
  12392. wc_Sha3_512_Free(&sha3);
  12393. return TEST_FAIL;
  12394. }
  12395. ret = wc_Sha3_512_Update(&sha3, (byte*)msg, msglen);
  12396. if (ret == 0) {
  12397. ret = wc_Sha3_512_Copy(&sha3Cpy, &sha3);
  12398. if (ret == 0) {
  12399. ret = wc_Sha3_512_Final(&sha3, hash);
  12400. if (ret == 0) {
  12401. ret = wc_Sha3_512_Final(&sha3Cpy, hashCpy);
  12402. }
  12403. }
  12404. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  12405. ret = WOLFSSL_FATAL_ERROR;
  12406. }
  12407. }
  12408. /* Test bad args. */
  12409. if (ret == 0) {
  12410. ret = wc_Sha3_512_Copy(NULL, &sha3);
  12411. if (ret == BAD_FUNC_ARG) {
  12412. ret = wc_Sha3_512_Copy(&sha3Cpy, NULL);
  12413. }
  12414. if (ret == BAD_FUNC_ARG) {
  12415. ret = 0;
  12416. }
  12417. else if (ret == 0) {
  12418. ret = WOLFSSL_FATAL_ERROR;
  12419. }
  12420. }
  12421. res = TEST_RES_CHECK(ret == 0);
  12422. #endif
  12423. return res;
  12424. } /* END test_wc_Sha3_512_Copy */
  12425. /*
  12426. * Unit test function for wc_Sha3_GetFlags()
  12427. */
  12428. static int test_wc_Sha3_GetFlags(void)
  12429. {
  12430. int res = TEST_SKIPPED;
  12431. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  12432. wc_Sha3 sha3;
  12433. word32 flags = 0;
  12434. int ret = 0;
  12435. /* Initialize */
  12436. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  12437. if (ret != 0) {
  12438. return TEST_FAIL;
  12439. }
  12440. if (ret == 0) {
  12441. ret = wc_Sha3_GetFlags(&sha3, &flags);
  12442. }
  12443. if (ret == 0) {
  12444. if (flags & WC_HASH_FLAG_ISCOPY) {
  12445. ret = 0;
  12446. }
  12447. }
  12448. wc_Sha3_224_Free(&sha3);
  12449. res = TEST_RES_CHECK(ret == 0);
  12450. #endif
  12451. return res;
  12452. } /* END test_wc_Sha3_GetFlags */
  12453. static int test_wc_InitShake256(void)
  12454. {
  12455. int res = TEST_SKIPPED;
  12456. #ifdef WOLFSSL_SHAKE256
  12457. wc_Shake shake;
  12458. int ret = 0;
  12459. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  12460. /* Test bad args. */
  12461. if (ret == 0) {
  12462. ret = wc_InitShake256(NULL, HEAP_HINT, testDevId);
  12463. if (ret == BAD_FUNC_ARG) {
  12464. ret = 0;
  12465. }
  12466. else if (ret == 0) {
  12467. ret = WOLFSSL_FATAL_ERROR;
  12468. }
  12469. }
  12470. wc_Shake256_Free(&shake);
  12471. res = TEST_RES_CHECK(ret == 0);
  12472. #endif
  12473. return res;
  12474. } /* END test_wc_InitSha3 */
  12475. static int testing_wc_Shake256_Update(void)
  12476. {
  12477. int res = TEST_SKIPPED;
  12478. #ifdef WOLFSSL_SHAKE256
  12479. wc_Shake shake;
  12480. byte msg[] = "Everybody's working for the weekend.";
  12481. byte msg2[] = "Everybody gets Friday off.";
  12482. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  12483. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  12484. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  12485. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  12486. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  12487. word32 msglen = sizeof(msg) - 1;
  12488. word32 msg2len = sizeof(msg2);
  12489. word32 msgCmplen = sizeof(msgCmp);
  12490. int ret = 0;
  12491. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  12492. if (ret != 0) {
  12493. return TEST_FAIL;
  12494. }
  12495. ret = wc_Shake256_Update(&shake, msg, msglen);
  12496. if (XMEMCMP(msg, shake.t, msglen) || shake.i != msglen) {
  12497. ret = WOLFSSL_FATAL_ERROR;
  12498. }
  12499. if (ret == 0) {
  12500. ret = wc_Shake256_Update(&shake, msg2, msg2len);
  12501. if (XMEMCMP(shake.t, msgCmp, msgCmplen) != 0) {
  12502. ret = WOLFSSL_FATAL_ERROR;
  12503. }
  12504. }
  12505. /* Pass bad args. */
  12506. if (ret == 0) {
  12507. ret = wc_Shake256_Update(NULL, msg2, msg2len);
  12508. if (ret == BAD_FUNC_ARG) {
  12509. ret = wc_Shake256_Update(&shake, NULL, 5);
  12510. }
  12511. if (ret == BAD_FUNC_ARG) {
  12512. wc_Shake256_Free(&shake);
  12513. if (wc_InitShake256(&shake, HEAP_HINT, testDevId)) {
  12514. return TEST_FAIL;
  12515. }
  12516. ret = wc_Shake256_Update(&shake, NULL, 0);
  12517. if (ret == 0) {
  12518. ret = wc_Shake256_Update(&shake, msg2, msg2len);
  12519. }
  12520. if (ret == 0 && XMEMCMP(msg2, shake.t, msg2len) != 0) {
  12521. ret = WOLFSSL_FATAL_ERROR;
  12522. }
  12523. }
  12524. }
  12525. wc_Shake256_Free(&shake);
  12526. res = TEST_RES_CHECK(ret == 0);
  12527. #endif /* WOLFSSL_SHAKE256 */
  12528. return res;
  12529. }
  12530. static int test_wc_Shake256_Final(void)
  12531. {
  12532. int res = TEST_SKIPPED;
  12533. #ifdef WOLFSSL_SHAKE256
  12534. wc_Shake shake;
  12535. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12536. "nopnopq";
  12537. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  12538. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  12539. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  12540. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  12541. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  12542. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  12543. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  12544. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  12545. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  12546. byte hash[114];
  12547. int ret = 0;
  12548. /* Init stack variables. */
  12549. XMEMSET(hash, 0, sizeof(hash));
  12550. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  12551. if (ret != 0) {
  12552. return TEST_FAIL;
  12553. }
  12554. ret= wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg));
  12555. if (ret == 0) {
  12556. ret = wc_Shake256_Final(&shake, hash, (word32)sizeof(hash));
  12557. if (ret == 0 && XMEMCMP(expOut, hash, (word32)sizeof(hash)) != 0) {
  12558. ret = WOLFSSL_FATAL_ERROR;
  12559. }
  12560. }
  12561. /* Test bad args. */
  12562. if (ret == 0) {
  12563. ret = wc_Shake256_Final(NULL, hash, (word32)sizeof(hash));
  12564. if (ret == 0) {
  12565. ret = wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash));
  12566. }
  12567. if (ret == BAD_FUNC_ARG) {
  12568. ret = 0;
  12569. }
  12570. else if (ret == 0) {
  12571. ret = WOLFSSL_FATAL_ERROR;
  12572. }
  12573. }
  12574. wc_Shake256_Free(&shake);
  12575. res = TEST_RES_CHECK(ret == 0);
  12576. #endif
  12577. return res;
  12578. }
  12579. /*
  12580. * Testing wc_Shake256_Copy()
  12581. */
  12582. static int test_wc_Shake256_Copy(void)
  12583. {
  12584. int res = TEST_SKIPPED;
  12585. #ifdef WOLFSSL_SHAKE256
  12586. wc_Shake shake, shakeCpy;
  12587. const char* msg = TEST_STRING;
  12588. word32 msglen = (word32)TEST_STRING_SZ;
  12589. byte hash[144];
  12590. byte hashCpy[144];
  12591. word32 hashLen = sizeof(hash);
  12592. word32 hashLenCpy = sizeof(hashCpy);
  12593. int ret;
  12594. XMEMSET(hash, 0, sizeof(hash));
  12595. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12596. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  12597. if (ret != 0) {
  12598. return TEST_FAIL;
  12599. }
  12600. ret = wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId);
  12601. if (ret != 0) {
  12602. wc_Shake256_Free(&shake);
  12603. return TEST_FAIL;
  12604. }
  12605. ret = wc_Shake256_Update(&shake, (byte*)msg, msglen);
  12606. if (ret == 0) {
  12607. ret = wc_Shake256_Copy(&shakeCpy, &shake);
  12608. if (ret == 0) {
  12609. ret = wc_Shake256_Final(&shake, hash, hashLen);
  12610. if (ret == 0) {
  12611. ret = wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy);
  12612. }
  12613. }
  12614. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  12615. ret = WOLFSSL_FATAL_ERROR;
  12616. }
  12617. }
  12618. /* Test bad args. */
  12619. if (ret == 0) {
  12620. ret = wc_Shake256_Copy(NULL, &shake);
  12621. if (ret == BAD_FUNC_ARG) {
  12622. ret = wc_Shake256_Copy(&shakeCpy, NULL);
  12623. }
  12624. if (ret == BAD_FUNC_ARG) {
  12625. ret = 0;
  12626. }
  12627. else if (ret == 0) {
  12628. ret = WOLFSSL_FATAL_ERROR;
  12629. }
  12630. }
  12631. wc_Shake256_Free(&shake);
  12632. res = TEST_RES_CHECK(ret == 0);
  12633. #endif
  12634. return res;
  12635. } /* END test_wc_Shake256_Copy */
  12636. /*
  12637. * Unit test function for wc_Shake256Hash()
  12638. */
  12639. static int test_wc_Shake256Hash(void)
  12640. {
  12641. int res = TEST_SKIPPED;
  12642. #ifdef WOLFSSL_SHAKE256
  12643. const byte data[] = { /* Hello World */
  12644. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  12645. 0x72,0x6c,0x64
  12646. };
  12647. word32 len = sizeof(data);
  12648. byte hash[144];
  12649. word32 hashLen = sizeof(hash);
  12650. int ret;
  12651. ret = wc_Shake256Hash(data, len, hash, hashLen);
  12652. res = TEST_RES_CHECK(ret == 0);
  12653. #endif
  12654. return res;
  12655. } /* END test_wc_Shake256Hash */
  12656. /*
  12657. * Test function for wc_HmacSetKey
  12658. */
  12659. static int test_wc_Md5HmacSetKey(void)
  12660. {
  12661. int res = TEST_SKIPPED;
  12662. #if !defined(NO_HMAC) && !defined(NO_MD5)
  12663. Hmac hmac;
  12664. int ret, times, itr;
  12665. int flag = 0;
  12666. const char* keys[]=
  12667. {
  12668. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  12669. #ifndef HAVE_FIPS
  12670. "Jefe", /* smaller than minimum FIPS key size */
  12671. #endif
  12672. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12673. };
  12674. times = sizeof(keys) / sizeof(char*);
  12675. flag = 0;
  12676. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12677. if (ret != 0)
  12678. return TEST_FAIL;
  12679. for (itr = 0; itr < times; itr++) {
  12680. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  12681. (word32)XSTRLEN(keys[itr]));
  12682. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  12683. wc_HmacFree(&hmac);
  12684. if (ret == BAD_FUNC_ARG) {
  12685. return TEST_SUCCESS;
  12686. }
  12687. else {
  12688. return TEST_FAIL;
  12689. }
  12690. #else
  12691. if (ret != 0) {
  12692. flag = ret;
  12693. }
  12694. #endif
  12695. }
  12696. /* Bad args. */
  12697. if (!flag) {
  12698. ret = wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  12699. (word32)XSTRLEN(keys[0]));
  12700. if (ret != BAD_FUNC_ARG) {
  12701. flag = WOLFSSL_FATAL_ERROR;
  12702. }
  12703. }
  12704. if (!flag) {
  12705. ret = wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0]));
  12706. if (ret != BAD_FUNC_ARG) {
  12707. flag = WOLFSSL_FATAL_ERROR;
  12708. }
  12709. }
  12710. if (!flag) {
  12711. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12712. (word32)XSTRLEN(keys[0]));
  12713. if (ret != BAD_FUNC_ARG) {
  12714. flag = WOLFSSL_FATAL_ERROR;
  12715. }
  12716. }
  12717. if (!flag) {
  12718. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  12719. #ifdef HAVE_FIPS
  12720. if (ret != HMAC_MIN_KEYLEN_E) {
  12721. flag = WOLFSSL_FATAL_ERROR;
  12722. }
  12723. #else
  12724. if (ret != 0) {
  12725. flag = WOLFSSL_FATAL_ERROR;
  12726. }
  12727. #endif
  12728. }
  12729. wc_HmacFree(&hmac);
  12730. res = TEST_RES_CHECK(flag == 0);
  12731. #endif
  12732. return res;
  12733. } /* END test_wc_Md5HmacSetKey */
  12734. /*
  12735. * testing wc_HmacSetKey() on wc_Sha hash.
  12736. */
  12737. static int test_wc_ShaHmacSetKey(void)
  12738. {
  12739. int res = TEST_SKIPPED;
  12740. #if !defined(NO_HMAC) && !defined(NO_SHA)
  12741. Hmac hmac;
  12742. int ret, times, itr;
  12743. int flag = 0;
  12744. const char* keys[]=
  12745. {
  12746. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12747. "\x0b\x0b\x0b",
  12748. #ifndef HAVE_FIPS
  12749. "Jefe", /* smaller than minimum FIPS key size */
  12750. #endif
  12751. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12752. "\xAA\xAA\xAA"
  12753. };
  12754. times = sizeof(keys) / sizeof(char*);
  12755. flag = 0;
  12756. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12757. if (ret != 0)
  12758. return ret;
  12759. for (itr = 0; itr < times; itr++) {
  12760. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  12761. (word32)XSTRLEN(keys[itr]));
  12762. if (ret != 0) {
  12763. flag = ret;
  12764. }
  12765. }
  12766. /* Bad args. */
  12767. if (!flag) {
  12768. ret = wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  12769. (word32)XSTRLEN(keys[0]));
  12770. if (ret != BAD_FUNC_ARG) {
  12771. flag = WOLFSSL_FATAL_ERROR;
  12772. }
  12773. }
  12774. if (!flag) {
  12775. ret = wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0]));
  12776. if (ret != BAD_FUNC_ARG) {
  12777. flag = WOLFSSL_FATAL_ERROR;
  12778. }
  12779. }
  12780. if (!flag) {
  12781. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12782. (word32)XSTRLEN(keys[0]));
  12783. if (ret != BAD_FUNC_ARG) {
  12784. flag = WOLFSSL_FATAL_ERROR;
  12785. }
  12786. }
  12787. if (!flag) {
  12788. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  12789. #ifdef HAVE_FIPS
  12790. if (ret != HMAC_MIN_KEYLEN_E) {
  12791. flag = WOLFSSL_FATAL_ERROR;
  12792. }
  12793. #else
  12794. if (ret != 0) {
  12795. flag = WOLFSSL_FATAL_ERROR;
  12796. }
  12797. #endif
  12798. }
  12799. wc_HmacFree(&hmac);
  12800. res = TEST_RES_CHECK(flag == 0);
  12801. #endif
  12802. return res;
  12803. } /* END test_wc_ShaHmacSetKey() */
  12804. /*
  12805. * testing wc_HmacSetKey() on Sha224 hash.
  12806. */
  12807. static int test_wc_Sha224HmacSetKey(void)
  12808. {
  12809. int res = TEST_SKIPPED;
  12810. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  12811. Hmac hmac;
  12812. int ret, times, itr;
  12813. int flag = 0;
  12814. const char* keys[]=
  12815. {
  12816. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12817. "\x0b\x0b\x0b",
  12818. #ifndef HAVE_FIPS
  12819. "Jefe", /* smaller than minimum FIPS key size */
  12820. #endif
  12821. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12822. "\xAA\xAA\xAA"
  12823. };
  12824. times = sizeof(keys) / sizeof(char*);
  12825. flag = 0;
  12826. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12827. if (ret != 0)
  12828. return ret;
  12829. for (itr = 0; itr < times; itr++) {
  12830. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  12831. (word32)XSTRLEN(keys[itr]));
  12832. if (ret != 0) {
  12833. flag = ret;
  12834. }
  12835. }
  12836. /* Bad args. */
  12837. if (!flag) {
  12838. ret = wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  12839. (word32)XSTRLEN(keys[0]));
  12840. if (ret != BAD_FUNC_ARG) {
  12841. flag = WOLFSSL_FATAL_ERROR;
  12842. }
  12843. }
  12844. if (!flag) {
  12845. ret = wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0]));
  12846. if (ret != BAD_FUNC_ARG) {
  12847. flag = WOLFSSL_FATAL_ERROR;
  12848. }
  12849. }
  12850. if (!flag) {
  12851. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12852. (word32)XSTRLEN(keys[0]));
  12853. if (ret != BAD_FUNC_ARG) {
  12854. flag = WOLFSSL_FATAL_ERROR;
  12855. }
  12856. }
  12857. if (!flag) {
  12858. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  12859. #ifdef HAVE_FIPS
  12860. if (ret != HMAC_MIN_KEYLEN_E) {
  12861. flag = WOLFSSL_FATAL_ERROR;
  12862. }
  12863. #else
  12864. if (ret != 0) {
  12865. flag = WOLFSSL_FATAL_ERROR;
  12866. }
  12867. #endif
  12868. }
  12869. wc_HmacFree(&hmac);
  12870. res = TEST_RES_CHECK(flag == 0);
  12871. #endif
  12872. return res;
  12873. } /* END test_wc_Sha224HmacSetKey() */
  12874. /*
  12875. * testing wc_HmacSetKey() on Sha256 hash
  12876. */
  12877. static int test_wc_Sha256HmacSetKey(void)
  12878. {
  12879. int res = TEST_SKIPPED;
  12880. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  12881. Hmac hmac;
  12882. int ret, times, itr;
  12883. int flag = 0;
  12884. const char* keys[]=
  12885. {
  12886. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12887. "\x0b\x0b\x0b",
  12888. #ifndef HAVE_FIPS
  12889. "Jefe", /* smaller than minimum FIPS key size */
  12890. #endif
  12891. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12892. "\xAA\xAA\xAA"
  12893. };
  12894. times = sizeof(keys) / sizeof(char*);
  12895. flag = 0;
  12896. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12897. if (ret != 0)
  12898. return ret;
  12899. for (itr = 0; itr < times; itr++) {
  12900. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  12901. (word32)XSTRLEN(keys[itr]));
  12902. if (ret != 0) {
  12903. flag = ret;
  12904. }
  12905. }
  12906. /* Bad args. */
  12907. if (!flag) {
  12908. ret = wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  12909. (word32)XSTRLEN(keys[0]));
  12910. if (ret != BAD_FUNC_ARG) {
  12911. flag = WOLFSSL_FATAL_ERROR;
  12912. }
  12913. }
  12914. if (!flag) {
  12915. ret = wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0]));
  12916. if (ret != BAD_FUNC_ARG) {
  12917. flag = WOLFSSL_FATAL_ERROR;
  12918. }
  12919. }
  12920. if (!flag) {
  12921. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12922. (word32)XSTRLEN(keys[0]));
  12923. if (ret != BAD_FUNC_ARG) {
  12924. flag = WOLFSSL_FATAL_ERROR;
  12925. }
  12926. }
  12927. if (!flag) {
  12928. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  12929. #ifdef HAVE_FIPS
  12930. if (ret != HMAC_MIN_KEYLEN_E) {
  12931. flag = WOLFSSL_FATAL_ERROR;
  12932. }
  12933. #else
  12934. if (ret != 0) {
  12935. flag = WOLFSSL_FATAL_ERROR;
  12936. }
  12937. #endif
  12938. }
  12939. wc_HmacFree(&hmac);
  12940. res = TEST_RES_CHECK(flag == 0);
  12941. #endif
  12942. return res;
  12943. } /* END test_wc_Sha256HmacSetKey() */
  12944. /*
  12945. * testing wc_HmacSetKey on Sha384 hash.
  12946. */
  12947. static int test_wc_Sha384HmacSetKey(void)
  12948. {
  12949. int res = TEST_SKIPPED;
  12950. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  12951. Hmac hmac;
  12952. int ret, times, itr;
  12953. int flag = 0;
  12954. const char* keys[]=
  12955. {
  12956. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12957. "\x0b\x0b\x0b",
  12958. #ifndef HAVE_FIPS
  12959. "Jefe", /* smaller than minimum FIPS key size */
  12960. #endif
  12961. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12962. "\xAA\xAA\xAA"
  12963. };
  12964. times = sizeof(keys) / sizeof(char*);
  12965. flag = 0;
  12966. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12967. if (ret != 0)
  12968. return ret;
  12969. for (itr = 0; itr < times; itr++) {
  12970. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  12971. (word32)XSTRLEN(keys[itr]));
  12972. if (ret != 0) {
  12973. flag = ret;
  12974. }
  12975. }
  12976. /* Bad args. */
  12977. if (!flag) {
  12978. ret = wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  12979. (word32)XSTRLEN(keys[0]));
  12980. if (ret != BAD_FUNC_ARG) {
  12981. flag = WOLFSSL_FATAL_ERROR;
  12982. }
  12983. }
  12984. if (!flag) {
  12985. ret = wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0]));
  12986. if (ret != BAD_FUNC_ARG) {
  12987. flag = WOLFSSL_FATAL_ERROR;
  12988. }
  12989. }
  12990. if (!flag) {
  12991. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12992. (word32)XSTRLEN(keys[0]));
  12993. if (ret != BAD_FUNC_ARG) {
  12994. flag = WOLFSSL_FATAL_ERROR;
  12995. }
  12996. }
  12997. if (!flag) {
  12998. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  12999. #ifdef HAVE_FIPS
  13000. if (ret != HMAC_MIN_KEYLEN_E) {
  13001. flag = WOLFSSL_FATAL_ERROR;
  13002. }
  13003. #else
  13004. if (ret != 0) {
  13005. flag = WOLFSSL_FATAL_ERROR;
  13006. }
  13007. #endif
  13008. }
  13009. wc_HmacFree(&hmac);
  13010. res = TEST_RES_CHECK(flag == 0);
  13011. #endif
  13012. return res;
  13013. } /* END test_wc_Sha384HmacSetKey() */
  13014. /*
  13015. * testing wc_HmacUpdate on wc_Md5 hash.
  13016. */
  13017. static int test_wc_Md5HmacUpdate(void)
  13018. {
  13019. int res = TEST_SKIPPED;
  13020. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  13021. Hmac hmac;
  13022. testVector a, b;
  13023. int ret;
  13024. int flag = 0;
  13025. #ifdef HAVE_FIPS
  13026. const char* keys =
  13027. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13028. #else
  13029. const char* keys = "Jefe";
  13030. #endif
  13031. a.input = "what do ya want for nothing?";
  13032. a.inLen = XSTRLEN(a.input);
  13033. b.input = "Hi There";
  13034. b.inLen = XSTRLEN(b.input);
  13035. flag = 0;
  13036. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13037. if (ret != 0)
  13038. return ret;
  13039. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys, (word32)XSTRLEN(keys));
  13040. if (ret != 0) {
  13041. flag = ret;
  13042. }
  13043. if (!flag) {
  13044. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  13045. if (ret != 0) {
  13046. flag = ret;
  13047. }
  13048. }
  13049. /* Update Hmac. */
  13050. if (!flag) {
  13051. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13052. if (ret != 0) {
  13053. flag = ret;
  13054. }
  13055. }
  13056. /* Test bad args. */
  13057. if (!flag) {
  13058. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  13059. if (ret != BAD_FUNC_ARG) {
  13060. flag = WOLFSSL_FATAL_ERROR;
  13061. }
  13062. }
  13063. if (!flag) {
  13064. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  13065. if (ret != BAD_FUNC_ARG) {
  13066. flag = WOLFSSL_FATAL_ERROR;
  13067. }
  13068. }
  13069. if (!flag) {
  13070. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  13071. if (ret != 0) {
  13072. flag = ret;
  13073. }
  13074. }
  13075. wc_HmacFree(&hmac);
  13076. res = TEST_RES_CHECK(flag == 0);
  13077. #endif
  13078. return res;
  13079. } /* END test_wc_Md5HmacUpdate */
  13080. /*
  13081. * testing wc_HmacUpdate on SHA hash.
  13082. */
  13083. static int test_wc_ShaHmacUpdate(void)
  13084. {
  13085. int res = TEST_SKIPPED;
  13086. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13087. Hmac hmac;
  13088. testVector a, b;
  13089. int ret;
  13090. int flag = 0;
  13091. #ifdef HAVE_FIPS
  13092. const char* keys =
  13093. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13094. #else
  13095. const char* keys = "Jefe";
  13096. #endif
  13097. a.input = "what do ya want for nothing?";
  13098. a.inLen = XSTRLEN(a.input);
  13099. b.input = "Hi There";
  13100. b.inLen = XSTRLEN(b.input);
  13101. flag = 0;
  13102. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13103. if (ret != 0)
  13104. return ret;
  13105. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys, (word32)XSTRLEN(keys));
  13106. if (ret != 0) {
  13107. flag = ret;
  13108. }
  13109. if (!flag) {
  13110. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  13111. if (ret != 0) {
  13112. flag = ret;
  13113. }
  13114. }
  13115. /* Update Hmac. */
  13116. if (!flag) {
  13117. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13118. if (ret != 0) {
  13119. flag = ret;
  13120. }
  13121. }
  13122. /* Test bad args. */
  13123. if (!flag) {
  13124. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  13125. if (ret != BAD_FUNC_ARG) {
  13126. flag = WOLFSSL_FATAL_ERROR;
  13127. }
  13128. }
  13129. if (!flag) {
  13130. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  13131. if (ret != BAD_FUNC_ARG) {
  13132. flag = WOLFSSL_FATAL_ERROR;
  13133. }
  13134. }
  13135. if (!flag) {
  13136. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  13137. if (ret != 0) {
  13138. flag = ret;
  13139. }
  13140. }
  13141. wc_HmacFree(&hmac);
  13142. res = TEST_RES_CHECK(flag == 0);
  13143. #endif
  13144. return res;
  13145. } /* END test_wc_ShaHmacUpdate */
  13146. /*
  13147. * testing wc_HmacUpdate on SHA224 hash.
  13148. */
  13149. static int test_wc_Sha224HmacUpdate(void)
  13150. {
  13151. int res = TEST_SKIPPED;
  13152. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13153. Hmac hmac;
  13154. testVector a, b;
  13155. int ret;
  13156. int flag = 0;
  13157. #ifdef HAVE_FIPS
  13158. const char* keys =
  13159. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13160. #else
  13161. const char* keys = "Jefe";
  13162. #endif
  13163. a.input = "what do ya want for nothing?";
  13164. a.inLen = XSTRLEN(a.input);
  13165. b.input = "Hi There";
  13166. b.inLen = XSTRLEN(b.input);
  13167. flag = 0;
  13168. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13169. if (ret != 0)
  13170. return ret;
  13171. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys, (word32)XSTRLEN(keys));
  13172. if (ret != 0) {
  13173. flag = ret;
  13174. }
  13175. if (!flag) {
  13176. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  13177. if (ret != 0) {
  13178. flag = ret;
  13179. }
  13180. }
  13181. /* Update Hmac. */
  13182. if (!flag) {
  13183. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13184. if (ret != 0) {
  13185. flag = ret;
  13186. }
  13187. }
  13188. /* Test bad args. */
  13189. if (!flag) {
  13190. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  13191. if (ret != BAD_FUNC_ARG) {
  13192. flag = WOLFSSL_FATAL_ERROR;
  13193. }
  13194. }
  13195. if (!flag) {
  13196. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  13197. if (ret != BAD_FUNC_ARG) {
  13198. flag = WOLFSSL_FATAL_ERROR;
  13199. }
  13200. }
  13201. if (!flag) {
  13202. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  13203. if (ret != 0) {
  13204. flag = ret;
  13205. }
  13206. }
  13207. wc_HmacFree(&hmac);
  13208. res = TEST_RES_CHECK(flag == 0);
  13209. #endif
  13210. return res;
  13211. } /* END test_wc_Sha224HmacUpdate */
  13212. /*
  13213. * testing wc_HmacUpdate on SHA256 hash.
  13214. */
  13215. static int test_wc_Sha256HmacUpdate(void)
  13216. {
  13217. int res = TEST_SKIPPED;
  13218. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13219. Hmac hmac;
  13220. testVector a, b;
  13221. int ret;
  13222. int flag = 0;
  13223. #ifdef HAVE_FIPS
  13224. const char* keys =
  13225. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13226. #else
  13227. const char* keys = "Jefe";
  13228. #endif
  13229. a.input = "what do ya want for nothing?";
  13230. a.inLen = XSTRLEN(a.input);
  13231. b.input = "Hi There";
  13232. b.inLen = XSTRLEN(b.input);
  13233. flag = 0;
  13234. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13235. if (ret != 0)
  13236. return ret;
  13237. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys, (word32)XSTRLEN(keys));
  13238. if (ret != 0) {
  13239. flag = ret;
  13240. }
  13241. if (!flag) {
  13242. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  13243. if (ret != 0) {
  13244. flag = ret;
  13245. }
  13246. }
  13247. /* Update Hmac. */
  13248. if (!flag) {
  13249. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13250. if (ret != 0) {
  13251. flag = ret;
  13252. }
  13253. }
  13254. /* Test bad args. */
  13255. if (!flag) {
  13256. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  13257. if (ret != BAD_FUNC_ARG) {
  13258. flag = WOLFSSL_FATAL_ERROR;
  13259. }
  13260. }
  13261. if (!flag) {
  13262. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  13263. if (ret != BAD_FUNC_ARG) {
  13264. flag = WOLFSSL_FATAL_ERROR;
  13265. }
  13266. }
  13267. if (!flag) {
  13268. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  13269. if (ret != 0) {
  13270. flag = ret;
  13271. }
  13272. }
  13273. wc_HmacFree(&hmac);
  13274. res = TEST_RES_CHECK(flag == 0);
  13275. #endif
  13276. return res;
  13277. } /* END test_wc_Sha256HmacUpdate */
  13278. /*
  13279. * testing wc_HmacUpdate on SHA384 hash.
  13280. */
  13281. static int test_wc_Sha384HmacUpdate(void)
  13282. {
  13283. int res = TEST_SKIPPED;
  13284. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13285. Hmac hmac;
  13286. testVector a, b;
  13287. int ret;
  13288. int flag = 0;
  13289. #ifdef HAVE_FIPS
  13290. const char* keys =
  13291. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13292. #else
  13293. const char* keys = "Jefe";
  13294. #endif
  13295. a.input = "what do ya want for nothing?";
  13296. a.inLen = XSTRLEN(a.input);
  13297. b.input = "Hi There";
  13298. b.inLen = XSTRLEN(b.input);
  13299. flag = 0;
  13300. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13301. if (ret != 0)
  13302. return ret;
  13303. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys, (word32)XSTRLEN(keys));
  13304. if (ret != 0) {
  13305. flag = ret;
  13306. }
  13307. if (!flag) {
  13308. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  13309. if (ret != 0) {
  13310. flag = ret;
  13311. }
  13312. }
  13313. /* Update Hmac. */
  13314. if (!flag) {
  13315. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13316. if (ret != 0) {
  13317. flag = ret;
  13318. }
  13319. }
  13320. /* Test bad args. */
  13321. if (!flag) {
  13322. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  13323. if (ret != BAD_FUNC_ARG) {
  13324. flag = WOLFSSL_FATAL_ERROR;
  13325. }
  13326. }
  13327. if (!flag) {
  13328. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  13329. if (ret != BAD_FUNC_ARG) {
  13330. flag = WOLFSSL_FATAL_ERROR;
  13331. }
  13332. }
  13333. if (!flag) {
  13334. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  13335. if (ret != 0) {
  13336. flag = ret;
  13337. }
  13338. }
  13339. wc_HmacFree(&hmac);
  13340. res = TEST_RES_CHECK(flag == 0);
  13341. #endif
  13342. return res;
  13343. } /* END test_wc_Sha384HmacUpdate */
  13344. /*
  13345. * Testing wc_HmacFinal() with MD5
  13346. */
  13347. static int test_wc_Md5HmacFinal(void)
  13348. {
  13349. int res = TEST_SKIPPED;
  13350. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  13351. Hmac hmac;
  13352. byte hash[WC_MD5_DIGEST_SIZE];
  13353. testVector a;
  13354. int ret;
  13355. const char* key;
  13356. int flag = 0;
  13357. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13358. a.input = "Hi There";
  13359. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  13360. "\x9d";
  13361. a.inLen = XSTRLEN(a.input);
  13362. a.outLen = XSTRLEN(a.output);
  13363. flag = 0;
  13364. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13365. if (ret != 0)
  13366. return ret;
  13367. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key));
  13368. if (ret != 0) {
  13369. flag = ret;
  13370. }
  13371. if (!flag) {
  13372. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13373. if (ret != 0) {
  13374. flag = ret;
  13375. }
  13376. }
  13377. if (!flag) {
  13378. ret = wc_HmacFinal(&hmac, hash);
  13379. if (ret != 0) {
  13380. flag = ret;
  13381. }
  13382. }
  13383. if (!flag) {
  13384. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  13385. flag = WOLFSSL_FATAL_ERROR;
  13386. }
  13387. }
  13388. /* Try bad parameters. */
  13389. if (!flag) {
  13390. ret = wc_HmacFinal(NULL, hash);
  13391. if (ret != BAD_FUNC_ARG) {
  13392. flag = WOLFSSL_FATAL_ERROR;
  13393. }
  13394. }
  13395. #ifndef HAVE_FIPS
  13396. if (!flag) {
  13397. ret = wc_HmacFinal(&hmac, NULL);
  13398. if (ret != BAD_FUNC_ARG) {
  13399. flag = WOLFSSL_FATAL_ERROR;
  13400. }
  13401. }
  13402. #endif
  13403. wc_HmacFree(&hmac);
  13404. res = TEST_RES_CHECK(flag == 0);
  13405. #endif
  13406. return res;
  13407. } /* END test_wc_Md5HmacFinal */
  13408. /*
  13409. * Testing wc_HmacFinal() with SHA
  13410. */
  13411. static int test_wc_ShaHmacFinal(void)
  13412. {
  13413. int res = TEST_SKIPPED;
  13414. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13415. Hmac hmac;
  13416. byte hash[WC_SHA_DIGEST_SIZE];
  13417. testVector a;
  13418. int ret;
  13419. int flag = 0;
  13420. const char* key;
  13421. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13422. "\x0b\x0b\x0b";
  13423. a.input = "Hi There";
  13424. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  13425. "\x8e\xf1\x46\xbe\x00";
  13426. a.inLen = XSTRLEN(a.input);
  13427. a.outLen = XSTRLEN(a.output);
  13428. flag = 0;
  13429. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13430. if (ret != 0)
  13431. return ret;
  13432. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key));
  13433. if (ret != 0) {
  13434. flag = ret;
  13435. }
  13436. if (!flag) {
  13437. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13438. if (ret != 0) {
  13439. flag = ret;
  13440. }
  13441. }
  13442. if (!flag) {
  13443. ret = wc_HmacFinal(&hmac, hash);
  13444. if (ret != 0) {
  13445. flag = ret;
  13446. }
  13447. }
  13448. if (!flag) {
  13449. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  13450. flag = WOLFSSL_FATAL_ERROR;
  13451. }
  13452. }
  13453. /* Try bad parameters. */
  13454. if (!flag) {
  13455. ret = wc_HmacFinal(NULL, hash);
  13456. if (ret != BAD_FUNC_ARG) {
  13457. flag = WOLFSSL_FATAL_ERROR;
  13458. }
  13459. }
  13460. #ifndef HAVE_FIPS
  13461. if (!flag) {
  13462. ret = wc_HmacFinal(&hmac, NULL);
  13463. if (ret != BAD_FUNC_ARG) {
  13464. flag = WOLFSSL_FATAL_ERROR;
  13465. }
  13466. }
  13467. #endif
  13468. wc_HmacFree(&hmac);
  13469. res = TEST_RES_CHECK(flag == 0);
  13470. #endif
  13471. return res;
  13472. } /* END test_wc_ShaHmacFinal */
  13473. /*
  13474. * Testing wc_HmacFinal() with SHA224
  13475. */
  13476. static int test_wc_Sha224HmacFinal(void)
  13477. {
  13478. int res = TEST_SKIPPED;
  13479. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13480. Hmac hmac;
  13481. byte hash[WC_SHA224_DIGEST_SIZE];
  13482. testVector a;
  13483. int ret;
  13484. int flag = 0;
  13485. const char* key;
  13486. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13487. "\x0b\x0b\x0b";
  13488. a.input = "Hi There";
  13489. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  13490. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  13491. a.inLen = XSTRLEN(a.input);
  13492. a.outLen = XSTRLEN(a.output);
  13493. flag = 0;
  13494. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13495. if (ret != 0)
  13496. return ret;
  13497. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key, (word32)XSTRLEN(key));
  13498. if (ret != 0) {
  13499. flag = ret;
  13500. }
  13501. if (!flag) {
  13502. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13503. if (ret != 0) {
  13504. flag = ret;
  13505. }
  13506. }
  13507. if (!flag) {
  13508. ret = wc_HmacFinal(&hmac, hash);
  13509. if (ret != 0) {
  13510. flag = ret;
  13511. }
  13512. }
  13513. if (!flag) {
  13514. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  13515. flag = WOLFSSL_FATAL_ERROR;
  13516. }
  13517. }
  13518. /* Try bad parameters. */
  13519. if (!flag) {
  13520. ret = wc_HmacFinal(NULL, hash);
  13521. if (ret != BAD_FUNC_ARG) {
  13522. flag = WOLFSSL_FATAL_ERROR;
  13523. }
  13524. }
  13525. #ifndef HAVE_FIPS
  13526. if (!flag) {
  13527. ret = wc_HmacFinal(&hmac, NULL);
  13528. if (ret != BAD_FUNC_ARG) {
  13529. flag = WOLFSSL_FATAL_ERROR;
  13530. }
  13531. }
  13532. #endif
  13533. wc_HmacFree(&hmac);
  13534. res = TEST_RES_CHECK(flag == 0);
  13535. #endif
  13536. return res;
  13537. } /* END test_wc_Sha224HmacFinal */
  13538. /*
  13539. * Testing wc_HmacFinal() with SHA256
  13540. */
  13541. static int test_wc_Sha256HmacFinal(void)
  13542. {
  13543. int res = TEST_SKIPPED;
  13544. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13545. Hmac hmac;
  13546. byte hash[WC_SHA256_DIGEST_SIZE];
  13547. testVector a;
  13548. int ret;
  13549. int flag = 0;
  13550. const char* key;
  13551. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13552. "\x0b\x0b\x0b";
  13553. a.input = "Hi There";
  13554. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  13555. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  13556. "\xcf\xf7";
  13557. a.inLen = XSTRLEN(a.input);
  13558. a.outLen = XSTRLEN(a.output);
  13559. flag = 0;
  13560. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13561. if (ret != 0)
  13562. return TEST_FAIL;
  13563. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key, (word32)XSTRLEN(key));
  13564. if (ret != 0) {
  13565. flag = ret;
  13566. }
  13567. if (!flag) {
  13568. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13569. if (ret != 0) {
  13570. flag = ret;
  13571. }
  13572. }
  13573. if (!flag) {
  13574. ret = wc_HmacFinal(&hmac, hash);
  13575. if (ret != 0) {
  13576. flag = ret;
  13577. }
  13578. }
  13579. if (!flag) {
  13580. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  13581. flag = WOLFSSL_FATAL_ERROR;
  13582. }
  13583. }
  13584. /* Try bad parameters. */
  13585. if (!flag) {
  13586. ret = wc_HmacFinal(NULL, hash);
  13587. if (ret != BAD_FUNC_ARG) {
  13588. flag = WOLFSSL_FATAL_ERROR;
  13589. }
  13590. }
  13591. #ifndef HAVE_FIPS
  13592. if (!flag) {
  13593. ret = wc_HmacFinal(&hmac, NULL);
  13594. if (ret != BAD_FUNC_ARG) {
  13595. flag = WOLFSSL_FATAL_ERROR;
  13596. }
  13597. }
  13598. #endif
  13599. wc_HmacFree(&hmac);
  13600. res = TEST_RES_CHECK(flag == 0);
  13601. #endif
  13602. return res;
  13603. } /* END test_wc_Sha256HmacFinal */
  13604. /*
  13605. * Testing wc_HmacFinal() with SHA384
  13606. */
  13607. static int test_wc_Sha384HmacFinal(void)
  13608. {
  13609. int res = TEST_SKIPPED;
  13610. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13611. Hmac hmac;
  13612. byte hash[WC_SHA384_DIGEST_SIZE];
  13613. testVector a;
  13614. int ret;
  13615. int flag = 0;
  13616. const char* key;
  13617. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13618. "\x0b\x0b\x0b";
  13619. a.input = "Hi There";
  13620. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  13621. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  13622. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  13623. "\xfa\x9c\xb6";
  13624. a.inLen = XSTRLEN(a.input);
  13625. a.outLen = XSTRLEN(a.output);
  13626. flag = 0;
  13627. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13628. if (ret != 0)
  13629. return ret;
  13630. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key, (word32)XSTRLEN(key));
  13631. if (ret != 0) {
  13632. flag = ret;
  13633. }
  13634. if (!flag) {
  13635. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13636. if (ret != 0) {
  13637. flag = ret;
  13638. }
  13639. }
  13640. if (!flag) {
  13641. ret = wc_HmacFinal(&hmac, hash);
  13642. if (ret != 0) {
  13643. flag = ret;
  13644. }
  13645. }
  13646. if (!flag) {
  13647. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  13648. flag = WOLFSSL_FATAL_ERROR;
  13649. }
  13650. }
  13651. /* Try bad parameters. */
  13652. if (!flag) {
  13653. ret = wc_HmacFinal(NULL, hash);
  13654. if (ret != BAD_FUNC_ARG) {
  13655. flag = WOLFSSL_FATAL_ERROR;
  13656. }
  13657. }
  13658. #ifndef HAVE_FIPS
  13659. if (!flag) {
  13660. ret = wc_HmacFinal(&hmac, NULL);
  13661. if (ret != BAD_FUNC_ARG) {
  13662. flag = WOLFSSL_FATAL_ERROR;
  13663. }
  13664. }
  13665. #endif
  13666. wc_HmacFree(&hmac);
  13667. res = TEST_RES_CHECK(flag == 0);
  13668. #endif
  13669. return res;
  13670. } /* END test_wc_Sha384HmacFinal */
  13671. /*
  13672. * Testing wc_InitCmac()
  13673. */
  13674. static int test_wc_InitCmac(void)
  13675. {
  13676. int res = TEST_SKIPPED;
  13677. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  13678. Cmac cmac1, cmac2, cmac3;
  13679. /* AES 128 key. */
  13680. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13681. "\x09\x10\x11\x12\x13\x14\x15\x16";
  13682. /* AES 192 key. */
  13683. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13684. "\x09\x01\x11\x12\x13\x14\x15\x16"
  13685. "\x01\x02\x03\x04\x05\x06\x07\x08";
  13686. /* AES 256 key. */
  13687. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13688. "\x09\x01\x11\x12\x13\x14\x15\x16"
  13689. "\x01\x02\x03\x04\x05\x06\x07\x08"
  13690. "\x09\x01\x11\x12\x13\x14\x15\x16";
  13691. word32 key1Sz = (word32)sizeof(key1) - 1;
  13692. word32 key2Sz = (word32)sizeof(key2) - 1;
  13693. word32 key3Sz = (word32)sizeof(key3) - 1;
  13694. int type = WC_CMAC_AES;
  13695. int ret = 0;
  13696. #ifdef WOLFSSL_AES_128
  13697. ret = wc_InitCmac(&cmac1, key1, key1Sz, type, NULL);
  13698. #endif
  13699. #ifdef WOLFSSL_AES_192
  13700. if (ret == 0) {
  13701. wc_AesFree(&cmac1.aes);
  13702. ret = wc_InitCmac(&cmac2, key2, key2Sz, type, NULL);
  13703. }
  13704. #endif
  13705. #ifdef WOLFSSL_AES_256
  13706. if (ret == 0) {
  13707. wc_AesFree(&cmac2.aes);
  13708. ret = wc_InitCmac(&cmac3, key3, key3Sz, type, NULL);
  13709. }
  13710. #endif
  13711. /* Test bad args. */
  13712. if (ret == 0) {
  13713. wc_AesFree(&cmac3.aes);
  13714. ret = wc_InitCmac(NULL, key3, key3Sz, type, NULL);
  13715. if (ret == BAD_FUNC_ARG) {
  13716. ret = wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL);
  13717. }
  13718. if (ret == BAD_FUNC_ARG) {
  13719. ret = wc_InitCmac(&cmac3, key3, 0, type, NULL);
  13720. }
  13721. if (ret == BAD_FUNC_ARG) {
  13722. ret = wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL);
  13723. }
  13724. if (ret == BAD_FUNC_ARG) {
  13725. ret = 0;
  13726. }
  13727. else {
  13728. ret = WOLFSSL_FATAL_ERROR;
  13729. }
  13730. }
  13731. (void)key1;
  13732. (void)key1Sz;
  13733. (void)key2;
  13734. (void)key2Sz;
  13735. (void)cmac1;
  13736. (void)cmac2;
  13737. res = TEST_RES_CHECK(ret == 0);
  13738. #endif
  13739. return res;
  13740. } /* END test_wc_InitCmac */
  13741. /*
  13742. * Testing wc_CmacUpdate()
  13743. */
  13744. static int test_wc_CmacUpdate(void)
  13745. {
  13746. int res = TEST_SKIPPED;
  13747. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13748. Cmac cmac;
  13749. byte key[] =
  13750. {
  13751. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  13752. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  13753. };
  13754. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  13755. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  13756. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  13757. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  13758. "\xf4";
  13759. word32 inSz = (word32)sizeof(in) - 1;
  13760. word32 keySz = (word32)sizeof(key);
  13761. int type = WC_CMAC_AES;
  13762. int ret = 0;
  13763. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  13764. if (ret != 0) {
  13765. return ret;
  13766. }
  13767. ret = wc_CmacUpdate(&cmac, in, inSz);
  13768. /* Test bad args. */
  13769. if (ret == 0) {
  13770. ret = wc_CmacUpdate(NULL, in, inSz);
  13771. if (ret == BAD_FUNC_ARG) {
  13772. ret = wc_CmacUpdate(&cmac, NULL, 30);
  13773. }
  13774. if (ret == BAD_FUNC_ARG) {
  13775. ret = 0;
  13776. }
  13777. else if (ret == 0) {
  13778. ret = WOLFSSL_FATAL_ERROR;
  13779. }
  13780. wc_AesFree(&cmac.aes);
  13781. }
  13782. res = TEST_RES_CHECK(ret == 0);
  13783. #endif
  13784. return res;
  13785. } /* END test_wc_CmacUpdate */
  13786. /*
  13787. * Testing wc_CmacFinal()
  13788. */
  13789. static int test_wc_CmacFinal(void)
  13790. {
  13791. int res = TEST_SKIPPED;
  13792. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13793. Cmac cmac;
  13794. byte key[] =
  13795. {
  13796. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  13797. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  13798. };
  13799. byte msg[] =
  13800. {
  13801. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  13802. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  13803. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  13804. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  13805. 0xf4
  13806. };
  13807. /* Test vectors from CMACGenAES128.rsp from
  13808. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  13809. * Per RFC4493 truncation of lsb is possible.
  13810. */
  13811. byte expMac[] =
  13812. {
  13813. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  13814. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  13815. };
  13816. byte mac[AES_BLOCK_SIZE];
  13817. word32 msgSz = (word32)sizeof(msg);
  13818. word32 keySz = (word32)sizeof(key);
  13819. word32 macSz = sizeof(mac);
  13820. word32 badMacSz = 17;
  13821. int expMacSz = sizeof(expMac);
  13822. int type = WC_CMAC_AES;
  13823. int ret = 0;
  13824. XMEMSET(mac, 0, macSz);
  13825. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  13826. if (ret != 0) {
  13827. return ret;
  13828. }
  13829. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  13830. if (ret == 0) {
  13831. ret = wc_CmacFinal(&cmac, mac, &macSz);
  13832. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  13833. ret = WOLFSSL_FATAL_ERROR;
  13834. }
  13835. /* Pass in bad args. */
  13836. if (ret == 0) {
  13837. ret = wc_CmacFinal(NULL, mac, &macSz);
  13838. if (ret == BAD_FUNC_ARG) {
  13839. ret = wc_CmacFinal(&cmac, NULL, &macSz);
  13840. }
  13841. if (ret == BAD_FUNC_ARG) {
  13842. ret = wc_CmacFinal(&cmac, mac, &badMacSz);
  13843. if (ret == BUFFER_E) {
  13844. ret = 0;
  13845. }
  13846. }
  13847. else if (ret == 0) {
  13848. ret = WOLFSSL_FATAL_ERROR;
  13849. }
  13850. }
  13851. }
  13852. res = TEST_RES_CHECK(ret == 0);
  13853. #endif
  13854. return res;
  13855. } /* END test_wc_CmacFinal */
  13856. /*
  13857. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  13858. */
  13859. static int test_wc_AesCmacGenerate(void)
  13860. {
  13861. int res = TEST_SKIPPED;
  13862. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13863. Cmac cmac;
  13864. byte key[] =
  13865. {
  13866. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  13867. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  13868. };
  13869. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  13870. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  13871. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  13872. "\x3d\x32\x65\x4c\x66\x23\xc5";
  13873. byte mac[AES_BLOCK_SIZE];
  13874. word32 keySz = sizeof(key);
  13875. word32 macSz = sizeof(mac);
  13876. word32 msgSz = sizeof(msg) - 1;
  13877. word32 expMacSz = sizeof(expMac) - 1;
  13878. int type = WC_CMAC_AES;
  13879. int ret = 0;
  13880. XMEMSET(mac, 0, macSz);
  13881. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  13882. if (ret != 0) {
  13883. return ret;
  13884. }
  13885. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  13886. if (ret != 0) {
  13887. return ret;
  13888. }
  13889. else {
  13890. wc_AesFree(&cmac.aes);
  13891. }
  13892. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz);
  13893. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  13894. ret = WOLFSSL_FATAL_ERROR;
  13895. }
  13896. /* Pass in bad args. */
  13897. if (ret == 0) {
  13898. ret = wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz);
  13899. if (ret == BAD_FUNC_ARG) {
  13900. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz);
  13901. }
  13902. if (ret == BAD_FUNC_ARG) {
  13903. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0);
  13904. }
  13905. if (ret == BAD_FUNC_ARG) {
  13906. ret = wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz);
  13907. }
  13908. if (ret == BAD_FUNC_ARG) {
  13909. ret = 0;
  13910. }
  13911. else if (ret == 0) {
  13912. ret = WOLFSSL_FATAL_ERROR;
  13913. }
  13914. }
  13915. if (ret == 0) {
  13916. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz);
  13917. /* Test bad args. */
  13918. if (ret == 0) {
  13919. ret = wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz);
  13920. if (ret == BAD_FUNC_ARG) {
  13921. ret = wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz);
  13922. }
  13923. if (ret == BAD_FUNC_ARG) {
  13924. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz);
  13925. }
  13926. if (ret == BAD_FUNC_ARG) {
  13927. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0);
  13928. }
  13929. if (ret == BAD_FUNC_ARG) {
  13930. ret = wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz);
  13931. }
  13932. if (ret == BAD_FUNC_ARG) {
  13933. ret = 0;
  13934. }
  13935. else if (ret == 0) {
  13936. ret = WOLFSSL_FATAL_ERROR;
  13937. }
  13938. }
  13939. }
  13940. res = TEST_RES_CHECK(ret == 0);
  13941. #endif
  13942. return res;
  13943. } /* END test_wc_AesCmacGenerate */
  13944. /*
  13945. * Testing streaming AES-GCM API.
  13946. */
  13947. static int test_wc_AesGcmStream(void)
  13948. {
  13949. int res = TEST_SKIPPED;
  13950. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  13951. defined(WOLFSSL_AESGCM_STREAM)
  13952. int ret = 0;
  13953. int i;
  13954. WC_RNG rng[1];
  13955. Aes aesEnc[1];
  13956. Aes aesDec[1];
  13957. byte tag[AES_BLOCK_SIZE];
  13958. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  13959. byte out[AES_BLOCK_SIZE * 3 + 2];
  13960. byte plain[AES_BLOCK_SIZE * 3 + 2];
  13961. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  13962. byte key[AES_128_KEY_SIZE] = { 0, };
  13963. byte iv[AES_IV_SIZE] = { 1, };
  13964. byte ivOut[AES_IV_SIZE];
  13965. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  13966. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  13967. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  13968. };
  13969. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  13970. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  13971. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  13972. };
  13973. static const byte expTag[AES_BLOCK_SIZE] = {
  13974. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  13975. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  13976. };
  13977. /* Create a random for generating IV/nonce. */
  13978. AssertIntEQ(wc_InitRng(rng), 0);
  13979. /* Initialize data structures. */
  13980. AssertIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  13981. AssertIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  13982. /* BadParameters to streaming init. */
  13983. AssertIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  13984. AssertIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  13985. AssertIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  13986. BAD_FUNC_ARG);
  13987. AssertIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  13988. BAD_FUNC_ARG);
  13989. /* Bad parameters to encrypt update. */
  13990. AssertIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  13991. BAD_FUNC_ARG);
  13992. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  13993. BAD_FUNC_ARG);
  13994. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  13995. BAD_FUNC_ARG);
  13996. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  13997. BAD_FUNC_ARG);
  13998. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  13999. BAD_FUNC_ARG);
  14000. /* Bad parameters to decrypt update. */
  14001. AssertIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  14002. BAD_FUNC_ARG);
  14003. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  14004. BAD_FUNC_ARG);
  14005. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  14006. BAD_FUNC_ARG);
  14007. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  14008. BAD_FUNC_ARG);
  14009. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  14010. BAD_FUNC_ARG);
  14011. /* Bad parameters to encrypt final. */
  14012. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14013. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14014. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14015. BAD_FUNC_ARG);
  14016. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  14017. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  14018. BAD_FUNC_ARG);
  14019. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  14020. BAD_FUNC_ARG);
  14021. /* Bad parameters to decrypt final. */
  14022. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14023. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14024. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14025. BAD_FUNC_ARG);
  14026. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  14027. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  14028. BAD_FUNC_ARG);
  14029. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  14030. BAD_FUNC_ARG);
  14031. /* Check calling final before setting key fails. */
  14032. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  14033. AssertIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  14034. /* Check calling update before setting key else fails. */
  14035. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14036. MISSING_KEY);
  14037. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14038. MISSING_KEY);
  14039. /* Set key but not IV. */
  14040. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  14041. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  14042. /* Check calling final before setting IV fails. */
  14043. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  14044. AssertIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  14045. /* Check calling update before setting IV else fails. */
  14046. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14047. MISSING_IV);
  14048. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14049. MISSING_IV);
  14050. /* Set IV using fixed part IV and external IV APIs. */
  14051. AssertIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  14052. rng), 0);
  14053. AssertIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  14054. GCM_NONCE_MID_SZ), 0);
  14055. AssertIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  14056. AssertIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  14057. /* Encrypt and decrypt data. */
  14058. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  14059. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  14060. AssertIntEQ(XMEMCMP(plain, in, 1), 0);
  14061. /* Finalize and check tag matches. */
  14062. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14063. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14064. /* Set key and IV through streaming init API. */
  14065. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14066. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14067. /* Encrypt/decrypt one block and AAD of one block. */
  14068. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  14069. AES_BLOCK_SIZE), 0);
  14070. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  14071. AES_BLOCK_SIZE), 0);
  14072. AssertIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  14073. /* Finalize and check tag matches. */
  14074. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14075. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14076. /* Set key and IV through streaming init API. */
  14077. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14078. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14079. /* No data to encrypt/decrypt one byte of AAD. */
  14080. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  14081. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  14082. /* Finalize and check tag matches. */
  14083. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14084. AssertIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  14085. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14086. /* Set key and IV through streaming init API. */
  14087. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14088. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14089. /* Encrypt/decrypt one byte and no AAD. */
  14090. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  14091. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  14092. AssertIntEQ(XMEMCMP(plain, in, 1), 0);
  14093. /* Finalize and check tag matches. */
  14094. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14095. AssertIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  14096. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14097. /* Set key and IV through streaming init API. */
  14098. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14099. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14100. /* Encryption AES is one byte at a time */
  14101. for (i = 0; i < (int)sizeof(aad); i++) {
  14102. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  14103. 0);
  14104. }
  14105. for (i = 0; i < (int)sizeof(in); i++) {
  14106. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  14107. 0);
  14108. }
  14109. /* Decryption AES is two bytes at a time */
  14110. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14111. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  14112. 0);
  14113. }
  14114. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14115. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  14116. 0), 0);
  14117. }
  14118. AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14119. /* Finalize and check tag matches. */
  14120. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14121. AssertIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  14122. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14123. /* Check streaming encryption can be decrypted with one shot. */
  14124. AssertIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  14125. AssertIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  14126. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14127. AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14128. wc_AesFree(aesEnc);
  14129. wc_AesFree(aesDec);
  14130. wc_FreeRng(rng);
  14131. res = TEST_RES_CHECK(ret == 0);
  14132. #endif
  14133. return res;
  14134. } /* END test_wc_AesGcmStream */
  14135. /*
  14136. * unit test for wc_Des3_SetIV()
  14137. */
  14138. static int test_wc_Des3_SetIV(void)
  14139. {
  14140. int res = TEST_SKIPPED;
  14141. #ifndef NO_DES3
  14142. Des3 des;
  14143. int ret = 0;
  14144. const byte key[] =
  14145. {
  14146. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14147. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14148. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14149. };
  14150. const byte iv[] =
  14151. {
  14152. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14153. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14154. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14155. };
  14156. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  14157. if (ret != 0)
  14158. return ret;
  14159. /* DES_ENCRYPTION or DES_DECRYPTION */
  14160. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  14161. if (ret == 0) {
  14162. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  14163. ret = WOLFSSL_FATAL_ERROR;
  14164. }
  14165. }
  14166. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  14167. /* Test explicitly wc_Des3_SetIV() */
  14168. if (ret == 0) {
  14169. ret = wc_Des3_SetIV(NULL, iv);
  14170. if (ret == BAD_FUNC_ARG) {
  14171. ret = wc_Des3_SetIV(&des, NULL);
  14172. }
  14173. else if (ret == 0) {
  14174. ret = WOLFSSL_FATAL_ERROR;
  14175. }
  14176. }
  14177. #endif
  14178. wc_Des3Free(&des);
  14179. res = TEST_RES_CHECK(ret == 0);
  14180. #endif
  14181. return res;
  14182. } /* END test_wc_Des3_SetIV */
  14183. /*
  14184. * unit test for wc_Des3_SetKey()
  14185. */
  14186. static int test_wc_Des3_SetKey(void)
  14187. {
  14188. int res = TEST_SKIPPED;
  14189. #ifndef NO_DES3
  14190. Des3 des;
  14191. int ret = 0;
  14192. const byte key[] =
  14193. {
  14194. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14195. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14196. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14197. };
  14198. const byte iv[] =
  14199. {
  14200. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14201. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14202. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14203. };
  14204. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  14205. if (ret != 0)
  14206. return ret;
  14207. /* DES_ENCRYPTION or DES_DECRYPTION */
  14208. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  14209. if (ret == 0) {
  14210. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  14211. ret = WOLFSSL_FATAL_ERROR;
  14212. }
  14213. }
  14214. /* Test bad args. */
  14215. if (ret == 0) {
  14216. ret = wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION);
  14217. if (ret == BAD_FUNC_ARG) {
  14218. ret = wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION);
  14219. }
  14220. if (ret == BAD_FUNC_ARG) {
  14221. ret = wc_Des3_SetKey(&des, key, iv, -1);
  14222. }
  14223. if (ret == BAD_FUNC_ARG) {
  14224. /* Default case. Should return 0. */
  14225. ret = wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION);
  14226. }
  14227. } /* END if ret != 0 */
  14228. wc_Des3Free(&des);
  14229. res = TEST_RES_CHECK(ret == 0);
  14230. #endif
  14231. return res;
  14232. } /* END test_wc_Des3_SetKey */
  14233. /*
  14234. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  14235. */
  14236. static int test_wc_Des3_CbcEncryptDecrypt(void)
  14237. {
  14238. int res = TEST_SKIPPED;
  14239. #ifndef NO_DES3
  14240. Des3 des;
  14241. int ret = 0;
  14242. byte cipher[24];
  14243. byte plain[24];
  14244. const byte key[] =
  14245. {
  14246. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14247. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14248. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14249. };
  14250. const byte iv[] =
  14251. {
  14252. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14253. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14254. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14255. };
  14256. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  14257. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14258. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14259. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14260. };
  14261. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  14262. if (ret != 0)
  14263. return ret;
  14264. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  14265. if (ret == 0) {
  14266. ret = wc_Des3_CbcEncrypt(&des, cipher, vector, 24);
  14267. if (ret == 0) {
  14268. ret = wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION);
  14269. }
  14270. if (ret == 0) {
  14271. ret = wc_Des3_CbcDecrypt(&des, plain, cipher, 24);
  14272. }
  14273. }
  14274. if (ret == 0) {
  14275. if (XMEMCMP(plain, vector, 24) != 0) {
  14276. ret = WOLFSSL_FATAL_ERROR;
  14277. }
  14278. }
  14279. /* Pass in bad args. */
  14280. if (ret == 0) {
  14281. ret = wc_Des3_CbcEncrypt(NULL, cipher, vector, 24);
  14282. if (ret == BAD_FUNC_ARG) {
  14283. ret = wc_Des3_CbcEncrypt(&des, NULL, vector, 24);
  14284. }
  14285. if (ret == BAD_FUNC_ARG) {
  14286. ret = wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector));
  14287. }
  14288. if (ret != BAD_FUNC_ARG) {
  14289. ret = WOLFSSL_FATAL_ERROR;
  14290. }
  14291. else {
  14292. ret = 0;
  14293. }
  14294. }
  14295. if (ret == 0) {
  14296. ret = wc_Des3_CbcDecrypt(NULL, plain, cipher, 24);
  14297. if (ret == BAD_FUNC_ARG) {
  14298. ret = wc_Des3_CbcDecrypt(&des, NULL, cipher, 24);
  14299. }
  14300. if (ret == BAD_FUNC_ARG) {
  14301. ret = wc_Des3_CbcDecrypt(&des, plain, NULL, 24);
  14302. }
  14303. if (ret != BAD_FUNC_ARG) {
  14304. ret = WOLFSSL_FATAL_ERROR;
  14305. }
  14306. else {
  14307. ret = 0;
  14308. }
  14309. }
  14310. wc_Des3Free(&des);
  14311. res = TEST_RES_CHECK(ret == 0);
  14312. #endif
  14313. return res;
  14314. } /* END wc_Des3_CbcEncrypt */
  14315. /*
  14316. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  14317. */
  14318. static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
  14319. {
  14320. int res = TEST_SKIPPED;
  14321. #ifndef NO_DES3
  14322. int ret = 0;
  14323. word32 vectorSz, cipherSz;
  14324. byte cipher[24];
  14325. byte plain[24];
  14326. byte vector[] = /* Now is the time for all w/o trailing 0 */
  14327. {
  14328. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14329. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14330. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14331. };
  14332. byte key[] =
  14333. {
  14334. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14335. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14336. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14337. };
  14338. byte iv[] =
  14339. {
  14340. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14341. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14342. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14343. };
  14344. vectorSz = sizeof(byte) * 24;
  14345. cipherSz = sizeof(byte) * 24;
  14346. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv);
  14347. if (ret == 0) {
  14348. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv);
  14349. if (ret == 0) {
  14350. if (XMEMCMP(plain, vector, 24) != 0) {
  14351. ret = WOLFSSL_FATAL_ERROR;
  14352. }
  14353. }
  14354. }
  14355. /* pass in bad args. */
  14356. if (ret == 0) {
  14357. ret = wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv);
  14358. if (ret == BAD_FUNC_ARG) {
  14359. ret = wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv);
  14360. }
  14361. if (ret == BAD_FUNC_ARG) {
  14362. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv);
  14363. }
  14364. if (ret == BAD_FUNC_ARG) {
  14365. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz,
  14366. key, NULL);
  14367. }
  14368. else {
  14369. /* Return code catch. */
  14370. ret = WOLFSSL_FAILURE;
  14371. }
  14372. }
  14373. if (ret == 0) {
  14374. ret = wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv);
  14375. if (ret == BAD_FUNC_ARG) {
  14376. ret = wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv);
  14377. }
  14378. if (ret == BAD_FUNC_ARG) {
  14379. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv);
  14380. }
  14381. if (ret == BAD_FUNC_ARG) {
  14382. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL);
  14383. }
  14384. else {
  14385. ret = WOLFSSL_FAILURE;
  14386. }
  14387. }
  14388. res = TEST_RES_CHECK(ret == 0);
  14389. #endif
  14390. return res;
  14391. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  14392. /*
  14393. * Unit test for wc_Des3_EcbEncrypt
  14394. */
  14395. static int test_wc_Des3_EcbEncrypt(void)
  14396. {
  14397. int res = TEST_SKIPPED;
  14398. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  14399. Des3 des;
  14400. int ret = 0;
  14401. byte cipher[24];
  14402. word32 cipherSz = sizeof(cipher);
  14403. const byte key[] =
  14404. {
  14405. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14406. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14407. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14408. };
  14409. const byte iv[] =
  14410. {
  14411. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14412. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14413. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14414. };
  14415. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  14416. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14417. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14418. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14419. };
  14420. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  14421. if (ret != 0) {
  14422. return ret;
  14423. }
  14424. if (ret == 0 ) {
  14425. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  14426. }
  14427. /* Bad Cases */
  14428. if (ret == 0) {
  14429. ret = wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz);
  14430. if (ret == BAD_FUNC_ARG) {
  14431. ret = 0;
  14432. }
  14433. }
  14434. if (ret == 0) {
  14435. ret = wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz);
  14436. if (ret == BAD_FUNC_ARG) {
  14437. ret = 0;
  14438. }
  14439. }
  14440. if (ret == 0) {
  14441. ret = wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz);
  14442. if (ret == BAD_FUNC_ARG) {
  14443. ret = 0;
  14444. }
  14445. }
  14446. if (ret == 0) {
  14447. ret = wc_Des3_EcbEncrypt(&des, cipher, vector, 0);
  14448. if (ret == BAD_FUNC_ARG) {
  14449. ret = 0;
  14450. }
  14451. }
  14452. if (ret == 0) {
  14453. ret = wc_Des3_EcbEncrypt(NULL, 0, NULL, 0);
  14454. if (ret == BAD_FUNC_ARG) {
  14455. ret = 0;
  14456. }
  14457. }
  14458. /* Good Cases */
  14459. if (ret == 0) {
  14460. ret = wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz);
  14461. }
  14462. wc_Des3Free(&des);
  14463. res = TEST_RES_CHECK(ret == 0);
  14464. #endif
  14465. return res;
  14466. } /* END test_wc_Des3_EcbEncrypt */
  14467. /*
  14468. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  14469. */
  14470. static int test_wc_Chacha_SetKey(void)
  14471. {
  14472. int res = TEST_SKIPPED;
  14473. #ifdef HAVE_CHACHA
  14474. ChaCha ctx;
  14475. const byte key[] =
  14476. {
  14477. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14478. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14479. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14480. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14481. };
  14482. byte cipher[128];
  14483. int ret = 0;
  14484. ret = wc_Chacha_SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  14485. /* Test bad args. */
  14486. if (ret == 0) {
  14487. ret = wc_Chacha_SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  14488. if (ret == BAD_FUNC_ARG) {
  14489. ret = wc_Chacha_SetKey(&ctx, key, 18);
  14490. }
  14491. if (ret == BAD_FUNC_ARG) {
  14492. ret = 0;
  14493. }
  14494. else {
  14495. ret = WOLFSSL_FATAL_ERROR;
  14496. }
  14497. }
  14498. if (ret == 0) {
  14499. ret = wc_Chacha_SetIV(&ctx, cipher, 0);
  14500. }
  14501. if (ret == 0) {
  14502. /* Test bad args. */
  14503. ret = wc_Chacha_SetIV(NULL, cipher, 0);
  14504. if (ret == BAD_FUNC_ARG) {
  14505. ret = 0;
  14506. }
  14507. else {
  14508. ret = WOLFSSL_FAILURE;
  14509. }
  14510. }
  14511. res = TEST_RES_CHECK(ret == 0);
  14512. #endif
  14513. return res;
  14514. } /* END test_wc_Chacha_SetKey */
  14515. /*
  14516. * unit test for wc_Poly1305SetKey()
  14517. */
  14518. static int test_wc_Poly1305SetKey(void)
  14519. {
  14520. int res = TEST_SKIPPED;
  14521. #ifdef HAVE_POLY1305
  14522. Poly1305 ctx;
  14523. const byte key[] =
  14524. {
  14525. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14526. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14527. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14528. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14529. };
  14530. int ret = 0;
  14531. ret = wc_Poly1305SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  14532. /* Test bad args. */
  14533. if (ret == 0) {
  14534. ret = wc_Poly1305SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  14535. if (ret == BAD_FUNC_ARG) {
  14536. ret = wc_Poly1305SetKey(&ctx, NULL, (word32)(sizeof(key)/sizeof(byte)));
  14537. }
  14538. if (ret == BAD_FUNC_ARG) {
  14539. ret = wc_Poly1305SetKey(&ctx, key, 18);
  14540. }
  14541. if (ret == BAD_FUNC_ARG) {
  14542. ret = 0;
  14543. }
  14544. else {
  14545. ret = WOLFSSL_FATAL_ERROR;
  14546. }
  14547. }
  14548. res = TEST_RES_CHECK(ret == 0);
  14549. #endif
  14550. return res;
  14551. } /* END test_wc_Poly1305_SetKey() */
  14552. /*
  14553. * Testing wc_Chacha_Process()
  14554. */
  14555. static int test_wc_Chacha_Process(void)
  14556. {
  14557. int res = TEST_SKIPPED;
  14558. #ifdef HAVE_CHACHA
  14559. ChaCha enc, dec;
  14560. byte cipher[128];
  14561. byte plain[128];
  14562. const byte key[] =
  14563. {
  14564. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14565. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14566. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14567. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14568. };
  14569. const char* input = "Everybody gets Friday off.";
  14570. word32 keySz = sizeof(key)/sizeof(byte);
  14571. unsigned long int inlen = XSTRLEN(input);
  14572. int ret = 0;
  14573. /*Initialize stack varialbes.*/
  14574. XMEMSET(cipher, 0, 128);
  14575. XMEMSET(plain, 0, 128);
  14576. ret = wc_Chacha_SetKey(&enc, key, keySz);
  14577. AssertIntEQ(ret, 0);
  14578. ret = wc_Chacha_SetKey(&dec, key, keySz);
  14579. AssertIntEQ(ret, 0);
  14580. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  14581. AssertIntEQ(ret, 0);
  14582. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  14583. AssertIntEQ(ret, 0);
  14584. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen);
  14585. AssertIntEQ(ret, 0);
  14586. ret = wc_Chacha_Process(&dec, plain, cipher, (word32)inlen);
  14587. AssertIntEQ(ret, 0);
  14588. ret = XMEMCMP(input, plain, (int)inlen);
  14589. AssertIntEQ(ret, 0);
  14590. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  14591. /* test checking and using leftovers, currently just in C code */
  14592. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  14593. AssertIntEQ(ret, 0);
  14594. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  14595. AssertIntEQ(ret, 0);
  14596. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen - 2);
  14597. AssertIntEQ(ret, 0);
  14598. ret = wc_Chacha_Process(&enc, cipher + (inlen - 2),
  14599. (byte*)input + (inlen - 2), 2);
  14600. AssertIntEQ(ret, 0);
  14601. ret = wc_Chacha_Process(&dec, plain, (byte*)cipher, (word32)inlen - 2);
  14602. AssertIntEQ(ret, 0);
  14603. ret = wc_Chacha_Process(&dec, cipher + (inlen - 2),
  14604. (byte*)input + (inlen - 2), 2);
  14605. AssertIntEQ(ret, 0);
  14606. ret = XMEMCMP(input, plain, (int)inlen);
  14607. AssertIntEQ(ret, 0);
  14608. /* check edge cases with counter increment */
  14609. {
  14610. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  14611. const byte expected[] = {
  14612. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  14613. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  14614. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  14615. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  14616. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  14617. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  14618. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  14619. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  14620. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  14621. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  14622. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  14623. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  14624. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  14625. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  14626. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  14627. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  14628. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  14629. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  14630. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  14631. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  14632. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  14633. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  14634. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  14635. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  14636. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  14637. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  14638. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  14639. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  14640. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  14641. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  14642. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  14643. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  14644. };
  14645. const byte iv2[] = {
  14646. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  14647. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  14648. };
  14649. byte input2[256];
  14650. int i;
  14651. for (i = 0; i < 256; i++)
  14652. input2[i] = i;
  14653. ret = wc_Chacha_SetIV(&enc, iv2, 0);
  14654. AssertIntEQ(ret, 0);
  14655. ret = wc_Chacha_Process(&enc, cipher, input2, 64);
  14656. AssertIntEQ(ret, 0);
  14657. AssertIntEQ(XMEMCMP(expected, cipher, 64), 0);
  14658. ret = wc_Chacha_Process(&enc, cipher, input2 + 64, 128);
  14659. AssertIntEQ(ret, 0);
  14660. AssertIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  14661. /* partial */
  14662. ret = wc_Chacha_Process(&enc, cipher, input2 + 192, 32);
  14663. AssertIntEQ(ret, 0);
  14664. AssertIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  14665. ret = wc_Chacha_Process(&enc, cipher, input2 + 224, 32);
  14666. AssertIntEQ(ret, 0);
  14667. AssertIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  14668. }
  14669. #endif
  14670. /* Test bad args. */
  14671. ret = wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen);
  14672. AssertIntEQ(ret, BAD_FUNC_ARG);
  14673. if (ret == BAD_FUNC_ARG) {
  14674. ret = 0;
  14675. }
  14676. res = TEST_RES_CHECK(ret == 0);
  14677. #endif
  14678. return res;
  14679. } /* END test_wc_Chacha_Process */
  14680. /*
  14681. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  14682. */
  14683. static int test_wc_ChaCha20Poly1305_aead(void)
  14684. {
  14685. int res = TEST_SKIPPED;
  14686. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  14687. const byte key[] = {
  14688. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  14689. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  14690. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  14691. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  14692. };
  14693. const byte plaintext[] = {
  14694. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  14695. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  14696. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  14697. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  14698. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  14699. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  14700. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  14701. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  14702. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  14703. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  14704. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  14705. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  14706. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  14707. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  14708. 0x74, 0x2e
  14709. };
  14710. const byte iv[] = {
  14711. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  14712. 0x44, 0x45, 0x46, 0x47
  14713. };
  14714. const byte aad[] = { /* additional data */
  14715. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  14716. 0xc4, 0xc5, 0xc6, 0xc7
  14717. };
  14718. const byte cipher[] = { /* expected output from operation */
  14719. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  14720. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  14721. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  14722. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  14723. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  14724. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  14725. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  14726. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  14727. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  14728. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  14729. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  14730. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  14731. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  14732. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  14733. 0x61, 0x16
  14734. };
  14735. const byte authTag[] = { /* expected output from operation */
  14736. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  14737. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  14738. };
  14739. byte generatedCiphertext[272];
  14740. byte generatedPlaintext[272];
  14741. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  14742. int ret = 0;
  14743. /* Initialize stack variables. */
  14744. XMEMSET(generatedCiphertext, 0, 272);
  14745. XMEMSET(generatedPlaintext, 0, 272);
  14746. /* Test Encrypt */
  14747. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), plaintext,
  14748. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  14749. AssertIntEQ(ret, 0);
  14750. ret = XMEMCMP(generatedCiphertext, cipher, sizeof(cipher)/sizeof(byte));
  14751. AssertIntEQ(ret, 0);
  14752. /* Test bad args. */
  14753. ret = wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad), plaintext,
  14754. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  14755. AssertIntEQ(ret, BAD_FUNC_ARG);
  14756. ret = wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  14757. plaintext, sizeof(plaintext),
  14758. generatedCiphertext, generatedAuthTag);
  14759. AssertIntEQ(ret, BAD_FUNC_ARG);
  14760. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  14761. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  14762. AssertIntEQ(ret, BAD_FUNC_ARG);
  14763. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14764. NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  14765. AssertIntEQ(ret, BAD_FUNC_ARG);
  14766. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14767. plaintext, sizeof(plaintext), NULL, generatedAuthTag);
  14768. AssertIntEQ(ret, BAD_FUNC_ARG);
  14769. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14770. plaintext, sizeof(plaintext), generatedCiphertext, NULL);
  14771. if (ret == BAD_FUNC_ARG) {
  14772. ret = 0;
  14773. (void)ret; /* suppress never read */
  14774. }
  14775. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14776. sizeof(cipher), authTag, generatedPlaintext);
  14777. AssertIntEQ(ret, 0);
  14778. ret = XMEMCMP(generatedPlaintext, plaintext,
  14779. sizeof(plaintext)/sizeof(byte));
  14780. AssertIntEQ(ret, 0);
  14781. /* Test bad args. */
  14782. ret = wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  14783. sizeof(cipher), authTag, generatedPlaintext);
  14784. AssertIntEQ(ret, BAD_FUNC_ARG);
  14785. ret = wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  14786. cipher, sizeof(cipher), authTag, generatedPlaintext);
  14787. AssertIntEQ(ret, BAD_FUNC_ARG);
  14788. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  14789. sizeof(cipher), authTag, generatedPlaintext);
  14790. AssertIntEQ(ret, BAD_FUNC_ARG);
  14791. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14792. sizeof(cipher), NULL, generatedPlaintext);
  14793. AssertIntEQ(ret, BAD_FUNC_ARG);
  14794. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14795. sizeof(cipher), authTag, NULL);
  14796. AssertIntEQ(ret, BAD_FUNC_ARG);
  14797. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  14798. sizeof(cipher), authTag, generatedPlaintext);
  14799. AssertIntEQ(ret, BAD_FUNC_ARG);
  14800. if (ret == BAD_FUNC_ARG) {
  14801. ret = 0;
  14802. }
  14803. res = TEST_RES_CHECK(ret == 0);
  14804. #endif
  14805. return res;
  14806. } /* END test-wc_ChaCha20Poly1305_EncryptDecrypt */
  14807. /*
  14808. * Testing function for wc_Rc2SetKey().
  14809. */
  14810. static int test_wc_Rc2SetKey(void)
  14811. {
  14812. int res = TEST_SKIPPED;
  14813. #ifdef WC_RC2
  14814. Rc2 rc2;
  14815. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  14816. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  14817. int ret = 0;
  14818. /* valid key and IV */
  14819. ret = wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  14820. iv, 40);
  14821. if (ret == 0) {
  14822. /* valid key, no IV */
  14823. ret = wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  14824. NULL, 40);
  14825. }
  14826. /* bad arguments */
  14827. if (ret == 0) {
  14828. /* null Rc2 struct */
  14829. ret = wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  14830. iv, 40);
  14831. if (ret == BAD_FUNC_ARG) {
  14832. ret = 0;
  14833. }
  14834. }
  14835. if (ret == 0) {
  14836. /* null key */
  14837. ret = wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  14838. iv, 40);
  14839. if (ret == BAD_FUNC_ARG) {
  14840. ret = 0;
  14841. }
  14842. }
  14843. if (ret == 0) {
  14844. /* key size == 0 */
  14845. ret = wc_Rc2SetKey(&rc2, key40, 0, iv, 40);
  14846. if (ret == WC_KEY_SIZE_E) {
  14847. ret = 0;
  14848. }
  14849. }
  14850. if (ret == 0) {
  14851. /* key size > 128 */
  14852. ret = wc_Rc2SetKey(&rc2, key40, 129, iv, 40);
  14853. if (ret == WC_KEY_SIZE_E) {
  14854. ret = 0;
  14855. }
  14856. }
  14857. if (ret == 0) {
  14858. /* effective bits == 0 */
  14859. ret = wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  14860. iv, 0);
  14861. if (ret == WC_KEY_SIZE_E) {
  14862. ret = 0;
  14863. }
  14864. }
  14865. if (ret == 0) {
  14866. /* effective bits > 1024 */
  14867. ret = wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  14868. iv, 1025);
  14869. if (ret == WC_KEY_SIZE_E) {
  14870. ret = 0;
  14871. }
  14872. }
  14873. res = TEST_RES_CHECK(ret == 0);
  14874. #endif
  14875. return res;
  14876. } /* END test_wc_Rc2SetKey */
  14877. /*
  14878. * Testing function for wc_Rc2SetIV().
  14879. */
  14880. static int test_wc_Rc2SetIV(void)
  14881. {
  14882. int res = TEST_SKIPPED;
  14883. #ifdef WC_RC2
  14884. Rc2 rc2;
  14885. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  14886. int ret = 0;
  14887. /* valid IV */
  14888. ret = wc_Rc2SetIV(&rc2, iv);
  14889. if (ret == 0) {
  14890. /* valid NULL IV */
  14891. ret = wc_Rc2SetIV(&rc2, NULL);
  14892. }
  14893. /* bad arguments */
  14894. if (ret == 0) {
  14895. ret = wc_Rc2SetIV(NULL, iv);
  14896. if (ret == BAD_FUNC_ARG) {
  14897. ret = 0;
  14898. }
  14899. }
  14900. res = TEST_RES_CHECK(ret == 0);
  14901. #endif
  14902. return res;
  14903. } /* END test_wc_Rc2SetKey */
  14904. /*
  14905. * Testing function for wc_Rc2EcbEncrypt().
  14906. */
  14907. static int test_wc_Rc2EcbEncryptDecrypt(void)
  14908. {
  14909. int res = TEST_SKIPPED;
  14910. #ifdef WC_RC2
  14911. Rc2 rc2;
  14912. int ret = 0;
  14913. int effectiveKeyBits = 63;
  14914. byte cipher[RC2_BLOCK_SIZE];
  14915. byte plain[RC2_BLOCK_SIZE];
  14916. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  14917. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  14918. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  14919. XMEMSET(cipher, 0, sizeof(cipher));
  14920. XMEMSET(plain, 0, sizeof(plain));
  14921. ret = wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  14922. NULL, effectiveKeyBits);
  14923. if (ret == 0) {
  14924. ret = wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE);
  14925. if (ret != 0 || XMEMCMP(cipher, output, RC2_BLOCK_SIZE) != 0) {
  14926. ret = WOLFSSL_FATAL_ERROR;
  14927. }
  14928. if (ret == 0) {
  14929. ret = wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE);
  14930. if (ret != 0 || XMEMCMP(plain, input, RC2_BLOCK_SIZE) != 0) {
  14931. ret = WOLFSSL_FATAL_ERROR;
  14932. }
  14933. }
  14934. }
  14935. /* Rc2EcbEncrypt bad arguments */
  14936. if (ret == 0) {
  14937. /* null Rc2 struct */
  14938. ret = wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE);
  14939. if (ret == BAD_FUNC_ARG) {
  14940. ret = 0;
  14941. }
  14942. }
  14943. if (ret == 0) {
  14944. /* null out buffer */
  14945. ret = wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE);
  14946. if (ret == BAD_FUNC_ARG) {
  14947. ret = 0;
  14948. }
  14949. }
  14950. if (ret == 0) {
  14951. /* null input buffer */
  14952. ret = wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE);
  14953. if (ret == BAD_FUNC_ARG) {
  14954. ret = 0;
  14955. }
  14956. }
  14957. if (ret == 0) {
  14958. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  14959. ret = wc_Rc2EcbEncrypt(&rc2, cipher, input, 7);
  14960. if (ret == BUFFER_E) {
  14961. ret = 0;
  14962. }
  14963. }
  14964. /* Rc2EcbDecrypt bad arguments */
  14965. if (ret == 0) {
  14966. /* null Rc2 struct */
  14967. ret = wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE);
  14968. if (ret == BAD_FUNC_ARG) {
  14969. ret = 0;
  14970. }
  14971. }
  14972. if (ret == 0) {
  14973. /* null out buffer */
  14974. ret = wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE);
  14975. if (ret == BAD_FUNC_ARG) {
  14976. ret = 0;
  14977. }
  14978. }
  14979. if (ret == 0) {
  14980. /* null input buffer */
  14981. ret = wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE);
  14982. if (ret == BAD_FUNC_ARG) {
  14983. ret = 0;
  14984. }
  14985. }
  14986. if (ret == 0) {
  14987. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  14988. ret = wc_Rc2EcbDecrypt(&rc2, plain, output, 7);
  14989. if (ret == BUFFER_E) {
  14990. ret = 0;
  14991. }
  14992. }
  14993. res = TEST_RES_CHECK(ret == 0);
  14994. #endif
  14995. return res;
  14996. } /* END test_wc_Rc2SetKey */
  14997. /*
  14998. * Testing function for wc_Rc2CbcEncrypt().
  14999. */
  15000. static int test_wc_Rc2CbcEncryptDecrypt(void)
  15001. {
  15002. int res = TEST_SKIPPED;
  15003. #ifdef WC_RC2
  15004. Rc2 rc2;
  15005. int ret = 0;
  15006. int effectiveKeyBits = 63;
  15007. byte cipher[RC2_BLOCK_SIZE*2];
  15008. byte plain[RC2_BLOCK_SIZE*2];
  15009. /* vector taken from test.c */
  15010. byte key[] = {
  15011. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15012. };
  15013. byte iv[] = {
  15014. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15015. };
  15016. byte input[] = {
  15017. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  15018. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15019. };
  15020. byte output[] = {
  15021. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  15022. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  15023. };
  15024. XMEMSET(cipher, 0, sizeof(cipher));
  15025. XMEMSET(plain, 0, sizeof(plain));
  15026. ret = wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  15027. iv, effectiveKeyBits);
  15028. if (ret == 0) {
  15029. ret = wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input));
  15030. if (ret != 0 || XMEMCMP(cipher, output, sizeof(output)) != 0) {
  15031. ret = WOLFSSL_FATAL_ERROR;
  15032. }
  15033. else {
  15034. /* reset IV for decrypt */
  15035. ret = wc_Rc2SetIV(&rc2, iv);
  15036. }
  15037. if (ret == 0) {
  15038. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher));
  15039. if (ret != 0 || XMEMCMP(plain, input, sizeof(input)) != 0) {
  15040. ret = WOLFSSL_FATAL_ERROR;
  15041. }
  15042. }
  15043. }
  15044. /* Rc2CbcEncrypt bad arguments */
  15045. if (ret == 0) {
  15046. /* null Rc2 struct */
  15047. ret = wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input));
  15048. if (ret == BAD_FUNC_ARG) {
  15049. ret = 0;
  15050. }
  15051. }
  15052. if (ret == 0) {
  15053. /* null out buffer */
  15054. ret = wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input));
  15055. if (ret == BAD_FUNC_ARG) {
  15056. ret = 0;
  15057. }
  15058. }
  15059. if (ret == 0) {
  15060. /* null input buffer */
  15061. ret = wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input));
  15062. if (ret == BAD_FUNC_ARG) {
  15063. ret = 0;
  15064. }
  15065. }
  15066. /* Rc2CbcDecrypt bad arguments */
  15067. if (ret == 0) {
  15068. /* in size is 0 */
  15069. ret = wc_Rc2CbcDecrypt(&rc2, plain, output, 0);
  15070. if (ret != 0) {
  15071. ret = WOLFSSL_FATAL_ERROR;
  15072. }
  15073. }
  15074. if (ret == 0) {
  15075. /* null Rc2 struct */
  15076. ret = wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output));
  15077. if (ret == BAD_FUNC_ARG) {
  15078. ret = 0;
  15079. }
  15080. }
  15081. if (ret == 0) {
  15082. /* null out buffer */
  15083. ret = wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output));
  15084. if (ret == BAD_FUNC_ARG) {
  15085. ret = 0;
  15086. }
  15087. }
  15088. if (ret == 0) {
  15089. /* null input buffer */
  15090. ret = wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output));
  15091. if (ret == BAD_FUNC_ARG) {
  15092. ret = 0;
  15093. }
  15094. }
  15095. res = TEST_RES_CHECK(ret == 0);
  15096. #endif
  15097. return res;
  15098. } /* END test_wc_Rc2SetKey */
  15099. /*
  15100. * Testing function for wc_AesSetIV
  15101. */
  15102. static int test_wc_AesSetIV(void)
  15103. {
  15104. int res = TEST_SKIPPED;
  15105. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  15106. Aes aes;
  15107. int ret = 0;
  15108. byte key16[] =
  15109. {
  15110. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15111. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15112. };
  15113. byte iv1[] = "1234567890abcdef";
  15114. byte iv2[] = "0987654321fedcba";
  15115. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15116. if (ret != 0)
  15117. return ret;
  15118. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  15119. iv1, AES_ENCRYPTION);
  15120. if (ret == 0) {
  15121. ret = wc_AesSetIV(&aes, iv2);
  15122. }
  15123. /* Test bad args. */
  15124. if (ret == 0) {
  15125. ret = wc_AesSetIV(NULL, iv1);
  15126. if (ret == BAD_FUNC_ARG) {
  15127. /* NULL iv should return 0. */
  15128. ret = wc_AesSetIV(&aes, NULL);
  15129. }
  15130. else {
  15131. ret = WOLFSSL_FATAL_ERROR;
  15132. }
  15133. }
  15134. wc_AesFree(&aes);
  15135. res = TEST_RES_CHECK(ret == 0);
  15136. #endif
  15137. return res;
  15138. } /* test_wc_AesSetIV */
  15139. /*
  15140. * Testing function for wc_AesSetKey().
  15141. */
  15142. static int test_wc_AesSetKey(void)
  15143. {
  15144. int res = TEST_SKIPPED;
  15145. #ifndef NO_AES
  15146. Aes aes;
  15147. int ret = 0;
  15148. byte key16[] =
  15149. {
  15150. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15151. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15152. };
  15153. #ifdef WOLFSSL_AES_192
  15154. byte key24[] =
  15155. {
  15156. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15157. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15158. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15159. };
  15160. #endif
  15161. #ifdef WOLFSSL_AES_256
  15162. byte key32[] =
  15163. {
  15164. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15165. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15166. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15167. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15168. };
  15169. #endif
  15170. byte badKey16[] =
  15171. {
  15172. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15173. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15174. };
  15175. byte iv[] = "1234567890abcdef";
  15176. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15177. if (ret != 0)
  15178. return ret;
  15179. #ifdef WOLFSSL_AES_128
  15180. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  15181. iv, AES_ENCRYPTION);
  15182. #endif
  15183. #ifdef WOLFSSL_AES_192
  15184. if (ret == 0) {
  15185. ret = wc_AesSetKey (&aes, key24, (word32) sizeof(key24) / sizeof(byte),
  15186. iv, AES_ENCRYPTION);
  15187. }
  15188. #endif
  15189. #ifdef WOLFSSL_AES_256
  15190. if (ret == 0) {
  15191. ret = wc_AesSetKey (&aes, key32, (word32) sizeof(key32) / sizeof(byte),
  15192. iv, AES_ENCRYPTION);
  15193. }
  15194. #endif
  15195. /* Pass in bad args. */
  15196. if (ret == 0) {
  15197. ret = wc_AesSetKey (NULL, key16, (word32) sizeof(key16) / sizeof(byte),
  15198. iv, AES_ENCRYPTION);
  15199. if (ret == BAD_FUNC_ARG) {
  15200. ret = wc_AesSetKey(&aes, badKey16,
  15201. (word32) sizeof(badKey16) / sizeof(byte),
  15202. iv, AES_ENCRYPTION);
  15203. }
  15204. if (ret == BAD_FUNC_ARG) {
  15205. ret = 0;
  15206. }
  15207. else {
  15208. ret = WOLFSSL_FATAL_ERROR;
  15209. }
  15210. }
  15211. wc_AesFree(&aes);
  15212. res = TEST_RES_CHECK(ret == 0);
  15213. #endif
  15214. return res;
  15215. } /* END test_wc_AesSetKey */
  15216. /*
  15217. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  15218. * and wc_AesCbcDecryptWithKey()
  15219. */
  15220. static int test_wc_AesCbcEncryptDecrypt(void)
  15221. {
  15222. int res = TEST_SKIPPED;
  15223. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  15224. defined(WOLFSSL_AES_256)
  15225. Aes aes;
  15226. int ret = 0;
  15227. byte key32[] =
  15228. {
  15229. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15230. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15231. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15232. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15233. };
  15234. byte vector[] = /* Now is the time for all good men w/o trailing 0 */
  15235. {
  15236. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15237. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15238. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20,
  15239. 0x67,0x6f,0x6f,0x64,0x20,0x6d,0x65,0x6e
  15240. };
  15241. byte iv[] = "1234567890abcdef";
  15242. byte enc[sizeof(vector)];
  15243. byte dec[sizeof(vector)];
  15244. int cbcE = WOLFSSL_FATAL_ERROR;
  15245. int cbcD = WOLFSSL_FATAL_ERROR;
  15246. int cbcDWK = WOLFSSL_FATAL_ERROR;
  15247. byte dec2[sizeof(vector)];
  15248. /* Init stack variables. */
  15249. XMEMSET(enc, 0, sizeof(enc));
  15250. XMEMSET(dec, 0, sizeof(vector));
  15251. XMEMSET(dec2, 0, sizeof(vector));
  15252. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15253. if (ret != 0)
  15254. return ret;
  15255. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv, AES_ENCRYPTION);
  15256. if (ret == 0) {
  15257. ret = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector));
  15258. if (ret == 0) {
  15259. /* Re init for decrypt and set flag. */
  15260. cbcE = 0;
  15261. wc_AesFree(&aes);
  15262. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2,
  15263. iv, AES_DECRYPTION);
  15264. }
  15265. if (ret == 0) {
  15266. ret = wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector));
  15267. if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector)) != 0) {
  15268. ret = WOLFSSL_FATAL_ERROR;
  15269. }
  15270. else {
  15271. /* Set flag. */
  15272. cbcD = 0;
  15273. }
  15274. }
  15275. }
  15276. /* If encrypt succeeds but cbc decrypt fails, we can still test. */
  15277. if (ret == 0 || cbcE == 0) {
  15278. ret = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15279. key32, sizeof(key32)/sizeof(byte), iv);
  15280. if (ret == 0 || XMEMCMP(vector, dec2, AES_BLOCK_SIZE) == 0) {
  15281. cbcDWK = 0;
  15282. }
  15283. }
  15284. /* Pass in bad args */
  15285. if (cbcE == 0) {
  15286. cbcE = wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector));
  15287. if (cbcE == BAD_FUNC_ARG) {
  15288. cbcE = wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector));
  15289. }
  15290. if (cbcE == BAD_FUNC_ARG) {
  15291. cbcE = wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector));
  15292. }
  15293. if (cbcE == BAD_FUNC_ARG) {
  15294. cbcE = 0;
  15295. }
  15296. else {
  15297. cbcE = WOLFSSL_FATAL_ERROR;
  15298. }
  15299. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  15300. if (cbcE == 0) {
  15301. cbcE = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1);
  15302. }
  15303. if (cbcE == BAD_LENGTH_E) {
  15304. cbcE = 0;
  15305. }
  15306. else {
  15307. cbcE = WOLFSSL_FATAL_ERROR;
  15308. }
  15309. #endif
  15310. }
  15311. if (cbcE == 0) {
  15312. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15313. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  15314. fprintf(stderr, "Zero length inputs not supported with AESNI in FIPS "
  15315. "mode (v2), skip test");
  15316. #else
  15317. /* Test passing in size of 0 */
  15318. XMEMSET(enc, 0, sizeof(enc));
  15319. cbcE = wc_AesCbcEncrypt(&aes, enc, vector, 0);
  15320. if (cbcE == 0) {
  15321. /* Check enc was not modified */
  15322. int i;
  15323. for (i = 0; i < (int)sizeof(enc); i++)
  15324. cbcE |= enc[i];
  15325. }
  15326. #endif
  15327. }
  15328. if (cbcE != 0) {
  15329. wc_AesFree(&aes);
  15330. return TEST_FAIL;
  15331. }
  15332. if (cbcD == 0) {
  15333. cbcD = wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE);
  15334. if (cbcD == BAD_FUNC_ARG) {
  15335. cbcD = wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE);
  15336. }
  15337. if (cbcD == BAD_FUNC_ARG) {
  15338. cbcD = wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE);
  15339. }
  15340. if (cbcD == BAD_FUNC_ARG) {
  15341. cbcD = wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1);
  15342. }
  15343. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  15344. if (cbcD == BAD_LENGTH_E) {
  15345. cbcD = 0;
  15346. }
  15347. else {
  15348. cbcD = WOLFSSL_FATAL_ERROR;
  15349. }
  15350. #else
  15351. if (cbcD == BAD_FUNC_ARG) {
  15352. cbcD = 0;
  15353. }
  15354. else {
  15355. cbcD = WOLFSSL_FATAL_ERROR;
  15356. }
  15357. #endif
  15358. }
  15359. if (cbcD == 0) {
  15360. /* Test passing in size of 0 */
  15361. XMEMSET(dec, 0, sizeof(dec));
  15362. cbcD = wc_AesCbcDecrypt(&aes, dec, enc, 0);
  15363. if (cbcD == 0) {
  15364. /* Check dec was not modified */
  15365. int i;
  15366. for (i = 0; i < (int)sizeof(dec); i++)
  15367. cbcD |= dec[i];
  15368. }
  15369. }
  15370. if (cbcD != 0) {
  15371. wc_AesFree(&aes);
  15372. return TEST_FAIL;
  15373. }
  15374. if (cbcDWK == 0) {
  15375. cbcDWK = wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  15376. key32, sizeof(key32)/sizeof(byte), iv);
  15377. if (cbcDWK == BAD_FUNC_ARG) {
  15378. cbcDWK = wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  15379. key32, sizeof(key32)/sizeof(byte), iv);
  15380. }
  15381. if (cbcDWK == BAD_FUNC_ARG) {
  15382. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15383. NULL, sizeof(key32)/sizeof(byte), iv);
  15384. }
  15385. if (cbcDWK == BAD_FUNC_ARG) {
  15386. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15387. key32, sizeof(key32)/sizeof(byte), NULL);
  15388. }
  15389. if (cbcDWK == BAD_FUNC_ARG) {
  15390. cbcDWK = 0;
  15391. }
  15392. else {
  15393. cbcDWK = WOLFSSL_FATAL_ERROR;
  15394. }
  15395. }
  15396. wc_AesFree(&aes);
  15397. res = TEST_RES_CHECK(cbcDWK == 0);
  15398. #endif
  15399. return res;
  15400. } /* END test_wc_AesCbcEncryptDecrypt */
  15401. /*
  15402. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  15403. */
  15404. static int test_wc_AesCtrEncryptDecrypt(void)
  15405. {
  15406. int res = TEST_SKIPPED;
  15407. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  15408. Aes aesEnc, aesDec;
  15409. int ret = 0;
  15410. byte key32[] =
  15411. {
  15412. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15413. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15414. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15415. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15416. };
  15417. byte vector[] = /* Now is the time for all w/o trailing 0 */
  15418. {
  15419. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15420. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15421. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15422. };
  15423. byte iv[] = "1234567890abcdef";
  15424. byte enc[AES_BLOCK_SIZE * 2];
  15425. byte dec[AES_BLOCK_SIZE * 2];
  15426. /* Init stack variables. */
  15427. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  15428. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  15429. ret = wc_AesInit(&aesEnc, NULL, INVALID_DEVID);
  15430. if (ret != 0)
  15431. return ret;
  15432. ret = wc_AesInit(&aesDec, NULL, INVALID_DEVID);
  15433. if (ret != 0) {
  15434. wc_AesFree(&aesEnc);
  15435. return ret;
  15436. }
  15437. ret = wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2,
  15438. iv, AES_ENCRYPTION);
  15439. if (ret == 0) {
  15440. ret = wc_AesCtrEncrypt(&aesEnc, enc, vector,
  15441. sizeof(vector)/sizeof(byte));
  15442. if (ret == 0) {
  15443. /* Decrypt with wc_AesCtrEncrypt() */
  15444. ret = wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2,
  15445. iv, AES_ENCRYPTION);
  15446. }
  15447. if (ret == 0) {
  15448. ret = wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte));
  15449. if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector))) {
  15450. ret = WOLFSSL_FATAL_ERROR;
  15451. }
  15452. }
  15453. }
  15454. /* Test bad args. */
  15455. if (ret == 0) {
  15456. ret = wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte));
  15457. if (ret == BAD_FUNC_ARG) {
  15458. ret = wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte));
  15459. }
  15460. if (ret == BAD_FUNC_ARG) {
  15461. ret = wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte));
  15462. }
  15463. if (ret == BAD_FUNC_ARG) {
  15464. ret = 0;
  15465. }
  15466. else {
  15467. ret = WOLFSSL_FATAL_ERROR;
  15468. }
  15469. }
  15470. wc_AesFree(&aesEnc);
  15471. wc_AesFree(&aesDec);
  15472. res = TEST_RES_CHECK(ret == 0);
  15473. #endif
  15474. return res;
  15475. } /* END test_wc_AesCtrEncryptDecrypt */
  15476. /*
  15477. * test function for wc_AesGcmSetKey()
  15478. */
  15479. static int test_wc_AesGcmSetKey(void)
  15480. {
  15481. int res = TEST_SKIPPED;
  15482. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15483. Aes aes;
  15484. int ret = 0;
  15485. #ifdef WOLFSSL_AES_128
  15486. byte key16[] =
  15487. {
  15488. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15489. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15490. };
  15491. #endif
  15492. #ifdef WOLFSSL_AES_192
  15493. byte key24[] =
  15494. {
  15495. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15496. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15497. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15498. };
  15499. #endif
  15500. #ifdef WOLFSSL_AES_256
  15501. byte key32[] =
  15502. {
  15503. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15504. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15505. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15506. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15507. };
  15508. #endif
  15509. byte badKey16[] =
  15510. {
  15511. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15512. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15513. };
  15514. byte badKey24[] =
  15515. {
  15516. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15517. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15518. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  15519. };
  15520. byte badKey32[] =
  15521. {
  15522. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  15523. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15524. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15525. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15526. };
  15527. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15528. if (ret != 0)
  15529. return ret;
  15530. #ifdef WOLFSSL_AES_128
  15531. ret = wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte));
  15532. #endif
  15533. #ifdef WOLFSSL_AES_192
  15534. if (ret == 0) {
  15535. ret = wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte));
  15536. }
  15537. #endif
  15538. #ifdef WOLFSSL_AES_256
  15539. if (ret == 0) {
  15540. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  15541. }
  15542. #endif
  15543. /* Pass in bad args. */
  15544. if (ret == 0) {
  15545. ret = wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte));
  15546. if (ret == BAD_FUNC_ARG) {
  15547. ret = wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte));
  15548. }
  15549. if (ret == BAD_FUNC_ARG) {
  15550. ret = wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte));
  15551. }
  15552. if (ret == BAD_FUNC_ARG) {
  15553. ret = 0;
  15554. }
  15555. else {
  15556. ret = WOLFSSL_FATAL_ERROR;
  15557. }
  15558. }
  15559. wc_AesFree(&aes);
  15560. res = TEST_RES_CHECK(ret == 0);
  15561. #endif
  15562. return res;
  15563. } /* END test_wc_AesGcmSetKey */
  15564. /*
  15565. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  15566. */
  15567. static int test_wc_AesGcmEncryptDecrypt(void)
  15568. {
  15569. int res = TEST_SKIPPED;
  15570. /* WOLFSSL_AFALG requires 12 byte IV */
  15571. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  15572. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  15573. Aes aes;
  15574. byte key32[] =
  15575. {
  15576. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15577. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15578. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15579. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15580. };
  15581. byte vector[] = /* Now is the time for all w/o trailing 0 */
  15582. {
  15583. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15584. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15585. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15586. };
  15587. const byte a[] =
  15588. {
  15589. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15590. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15591. 0xab, 0xad, 0xda, 0xd2
  15592. };
  15593. byte iv[] = "1234567890a";
  15594. byte longIV[] = "1234567890abcdefghij";
  15595. byte enc[sizeof(vector)];
  15596. byte resultT[AES_BLOCK_SIZE];
  15597. byte dec[sizeof(vector)];
  15598. int gcmD = WOLFSSL_FATAL_ERROR;
  15599. int gcmE = WOLFSSL_FATAL_ERROR;
  15600. int ret = 0;
  15601. /* Init stack variables. */
  15602. XMEMSET(enc, 0, sizeof(vector));
  15603. XMEMSET(dec, 0, sizeof(vector));
  15604. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  15605. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15606. if (ret != 0)
  15607. return ret;
  15608. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  15609. if (ret == 0) {
  15610. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector),
  15611. iv, sizeof(iv)/sizeof(byte), resultT,
  15612. sizeof(resultT), a, sizeof(a));
  15613. }
  15614. if (gcmE == 0) { /* If encrypt fails, no decrypt. */
  15615. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector),
  15616. iv, sizeof(iv)/sizeof(byte), resultT,
  15617. sizeof(resultT), a, sizeof(a));
  15618. if (gcmD == 0 && (XMEMCMP(vector, dec, sizeof(vector)) != 0)) {
  15619. gcmD = WOLFSSL_FATAL_ERROR;
  15620. }
  15621. }
  15622. /*Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  15623. if (gcmE == 0) {
  15624. gcmE = wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector),
  15625. iv, sizeof(iv)/sizeof(byte), resultT, sizeof(resultT),
  15626. a, sizeof(a));
  15627. if (gcmE == BAD_FUNC_ARG) {
  15628. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  15629. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  15630. resultT, sizeof(resultT) + 1, a, sizeof(a));
  15631. }
  15632. if (gcmE == BAD_FUNC_ARG) {
  15633. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  15634. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  15635. resultT, sizeof(resultT) - 5, a, sizeof(a));
  15636. }
  15637. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15638. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  15639. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15640. /* FIPS does not check the lower bound of ivSz */
  15641. #else
  15642. if (gcmE == BAD_FUNC_ARG) {
  15643. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  15644. sizeof(vector), iv, 0,
  15645. resultT, sizeof(resultT), a, sizeof(a));
  15646. }
  15647. #endif
  15648. if (gcmE == BAD_FUNC_ARG) {
  15649. gcmE = 0;
  15650. }
  15651. else {
  15652. gcmE = WOLFSSL_FATAL_ERROR;
  15653. }
  15654. }
  15655. /* This case is now considered good. Long IVs are now allowed.
  15656. * Except for the original FIPS release, it still has an upper
  15657. * bound on the IV length. */
  15658. #if (!defined(HAVE_FIPS) || \
  15659. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  15660. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15661. if (gcmE == 0) {
  15662. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  15663. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT),
  15664. a, sizeof(a));
  15665. }
  15666. #else
  15667. (void)longIV;
  15668. #endif /* Old FIPS */
  15669. /* END wc_AesGcmEncrypt */
  15670. if (gcmE != 0) {
  15671. wc_AesFree(&aes);
  15672. return TEST_FAIL;
  15673. }
  15674. #ifdef HAVE_AES_DECRYPT
  15675. if (gcmD == 0) {
  15676. gcmD = wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte),
  15677. iv, sizeof(iv)/sizeof(byte), resultT,
  15678. sizeof(resultT), a, sizeof(a));
  15679. if (gcmD == BAD_FUNC_ARG) {
  15680. gcmD = wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte),
  15681. iv, sizeof(iv)/sizeof(byte), resultT,
  15682. sizeof(resultT), a, sizeof(a));
  15683. }
  15684. if (gcmD == BAD_FUNC_ARG) {
  15685. gcmD = wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte),
  15686. iv, sizeof(iv)/sizeof(byte), resultT,
  15687. sizeof(resultT), a, sizeof(a));
  15688. }
  15689. if (gcmD == BAD_FUNC_ARG) {
  15690. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  15691. NULL, sizeof(iv)/sizeof(byte), resultT,
  15692. sizeof(resultT), a, sizeof(a));
  15693. }
  15694. if (gcmD == BAD_FUNC_ARG) {
  15695. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  15696. iv, sizeof(iv)/sizeof(byte), NULL,
  15697. sizeof(resultT), a, sizeof(a));
  15698. }
  15699. if (gcmD == BAD_FUNC_ARG) {
  15700. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  15701. iv, sizeof(iv)/sizeof(byte), resultT,
  15702. sizeof(resultT) + 1, a, sizeof(a));
  15703. }
  15704. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15705. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  15706. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15707. /* FIPS does not check the lower bound of ivSz */
  15708. #else
  15709. if (gcmD == BAD_FUNC_ARG) {
  15710. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  15711. iv, 0, resultT,
  15712. sizeof(resultT), a, sizeof(a));
  15713. }
  15714. #endif
  15715. if (gcmD == BAD_FUNC_ARG) {
  15716. gcmD = 0;
  15717. }
  15718. else {
  15719. gcmD = WOLFSSL_FATAL_ERROR;
  15720. }
  15721. res = TEST_RES_CHECK(gcmD == 0);
  15722. } /* END wc_AesGcmDecrypt */
  15723. #endif /* HAVE_AES_DECRYPT */
  15724. wc_AesFree(&aes);
  15725. #endif
  15726. return res;
  15727. } /* END test_wc_AesGcmEncryptDecrypt */
  15728. /*
  15729. * unit test for wc_GmacSetKey()
  15730. */
  15731. static int test_wc_GmacSetKey(void)
  15732. {
  15733. int res = TEST_SKIPPED;
  15734. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15735. Gmac gmac;
  15736. byte key16[] =
  15737. {
  15738. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15739. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15740. };
  15741. #ifdef WOLFSSL_AES_192
  15742. byte key24[] =
  15743. {
  15744. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15745. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15746. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15747. };
  15748. #endif
  15749. #ifdef WOLFSSL_AES_256
  15750. byte key32[] =
  15751. {
  15752. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15753. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15754. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15755. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15756. };
  15757. #endif
  15758. byte badKey16[] =
  15759. {
  15760. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15761. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  15762. };
  15763. byte badKey24[] =
  15764. {
  15765. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  15766. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15767. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15768. };
  15769. byte badKey32[] =
  15770. {
  15771. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15772. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  15773. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15774. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15775. };
  15776. int ret = 0;
  15777. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  15778. if (ret != 0)
  15779. return ret;
  15780. #ifdef WOLFSSL_AES_128
  15781. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte));
  15782. #endif
  15783. #ifdef WOLFSSL_AES_192
  15784. if (ret == 0) {
  15785. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  15786. }
  15787. #endif
  15788. #ifdef WOLFSSL_AES_256
  15789. if (ret == 0) {
  15790. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  15791. }
  15792. #endif
  15793. /* Pass in bad args. */
  15794. if (ret == 0) {
  15795. ret = wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte));
  15796. if (ret == BAD_FUNC_ARG) {
  15797. ret = wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte));
  15798. }
  15799. if (ret == BAD_FUNC_ARG) {
  15800. ret = wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte));
  15801. }
  15802. if (ret == BAD_FUNC_ARG) {
  15803. ret = wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte));
  15804. }
  15805. if (ret == BAD_FUNC_ARG) {
  15806. ret = wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte));
  15807. }
  15808. if (ret == BAD_FUNC_ARG) {
  15809. ret = 0;
  15810. }
  15811. else {
  15812. ret = WOLFSSL_FATAL_ERROR;
  15813. }
  15814. }
  15815. wc_AesFree(&gmac.aes);
  15816. res = TEST_RES_CHECK(ret == 0);
  15817. #endif
  15818. return res;
  15819. } /* END test_wc_GmacSetKey */
  15820. /*
  15821. * unit test for wc_GmacUpdate
  15822. */
  15823. static int test_wc_GmacUpdate(void)
  15824. {
  15825. int res = TEST_SKIPPED;
  15826. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15827. Gmac gmac;
  15828. #ifdef WOLFSSL_AES_128
  15829. const byte key16[] =
  15830. {
  15831. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  15832. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  15833. };
  15834. #endif
  15835. #ifdef WOLFSSL_AES_192
  15836. byte key24[] =
  15837. {
  15838. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  15839. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  15840. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  15841. };
  15842. #endif
  15843. #ifdef WOLFSSL_AES_256
  15844. byte key32[] =
  15845. {
  15846. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  15847. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  15848. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  15849. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  15850. };
  15851. #endif
  15852. #ifdef WOLFSSL_AES_128
  15853. const byte authIn[] =
  15854. {
  15855. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  15856. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  15857. };
  15858. #endif
  15859. #ifdef WOLFSSL_AES_192
  15860. const byte authIn2[] =
  15861. {
  15862. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  15863. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  15864. };
  15865. #endif
  15866. const byte authIn3[] =
  15867. {
  15868. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  15869. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  15870. };
  15871. #ifdef WOLFSSL_AES_128
  15872. const byte tag1[] = /* Known. */
  15873. {
  15874. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  15875. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  15876. };
  15877. #endif
  15878. #ifdef WOLFSSL_AES_192
  15879. const byte tag2[] = /* Known */
  15880. {
  15881. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  15882. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  15883. };
  15884. #endif
  15885. const byte tag3[] = /* Known */
  15886. {
  15887. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  15888. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  15889. };
  15890. #ifdef WOLFSSL_AES_128
  15891. const byte iv[] =
  15892. {
  15893. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  15894. 0xe2, 0x8c, 0x8f, 0x16
  15895. };
  15896. #endif
  15897. #ifdef WOLFSSL_AES_192
  15898. const byte iv2[] =
  15899. {
  15900. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  15901. 0x7e, 0x1a, 0x6f, 0xbc
  15902. };
  15903. #endif
  15904. const byte iv3[] =
  15905. {
  15906. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  15907. 0xc3, 0xfb, 0x6c, 0x8a
  15908. };
  15909. byte tagOut[16];
  15910. byte tagOut2[24];
  15911. byte tagOut3[32];
  15912. int ret = 0;
  15913. /* Init stack variables. */
  15914. XMEMSET(tagOut, 0, sizeof(tagOut));
  15915. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  15916. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  15917. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  15918. if (ret != 0)
  15919. return ret;
  15920. #ifdef WOLFSSL_AES_128
  15921. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16));
  15922. if (ret == 0) {
  15923. ret = wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  15924. tagOut, sizeof(tag1));
  15925. if (ret == 0) {
  15926. ret = XMEMCMP(tag1, tagOut, sizeof(tag1));
  15927. }
  15928. wc_AesFree(&gmac.aes);
  15929. }
  15930. #endif
  15931. #ifdef WOLFSSL_AES_192
  15932. if (ret == 0) {
  15933. XMEMSET(&gmac, 0, sizeof(Gmac));
  15934. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  15935. }
  15936. if (ret == 0) {
  15937. ret = wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2,
  15938. sizeof(authIn2), tagOut2, sizeof(tag2));
  15939. }
  15940. if (ret == 0) {
  15941. ret = XMEMCMP(tagOut2, tag2, sizeof(tag2));
  15942. wc_AesFree(&gmac.aes);
  15943. }
  15944. #endif
  15945. #ifdef WOLFSSL_AES_256
  15946. if (ret == 0) {
  15947. XMEMSET(&gmac, 0, sizeof(Gmac));
  15948. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  15949. }
  15950. if (ret == 0) {
  15951. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  15952. sizeof(authIn3), tagOut3, sizeof(tag3));
  15953. }
  15954. if (ret == 0) {
  15955. ret = XMEMCMP(tag3, tagOut3, sizeof(tag3));
  15956. }
  15957. #endif
  15958. /*Pass bad args. */
  15959. if (ret == 0) {
  15960. ret = wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3,
  15961. sizeof(authIn3), tagOut3, sizeof(tag3));
  15962. if (ret == BAD_FUNC_ARG) {
  15963. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  15964. sizeof(authIn3), tagOut3, sizeof(tag3) - 5);
  15965. }
  15966. if (ret == BAD_FUNC_ARG) {
  15967. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  15968. sizeof(authIn3), tagOut3, sizeof(tag3) + 1);
  15969. }
  15970. if (ret == BAD_FUNC_ARG) {
  15971. ret = 0;
  15972. }
  15973. else {
  15974. ret = WOLFSSL_FATAL_ERROR;
  15975. }
  15976. }
  15977. wc_AesFree(&gmac.aes);
  15978. res = TEST_RES_CHECK(ret == 0);
  15979. #endif
  15980. return res;
  15981. } /* END test_wc_GmacUpdate */
  15982. /*
  15983. * testing wc_CamelliaSetKey
  15984. */
  15985. static int test_wc_CamelliaSetKey(void)
  15986. {
  15987. int res = TEST_SKIPPED;
  15988. #ifdef HAVE_CAMELLIA
  15989. Camellia camellia;
  15990. /*128-bit key*/
  15991. static const byte key16[] =
  15992. {
  15993. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15994. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  15995. };
  15996. /* 192-bit key */
  15997. static const byte key24[] =
  15998. {
  15999. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16000. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16001. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16002. };
  16003. /* 256-bit key */
  16004. static const byte key32[] =
  16005. {
  16006. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16007. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16008. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  16009. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  16010. };
  16011. static const byte iv[] =
  16012. {
  16013. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16014. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16015. };
  16016. int ret = 0;
  16017. ret = wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv);
  16018. if (ret == 0) {
  16019. ret = wc_CamelliaSetKey(&camellia, key16,
  16020. (word32)sizeof(key16), NULL);
  16021. if (ret == 0) {
  16022. ret = wc_CamelliaSetKey(&camellia, key24,
  16023. (word32)sizeof(key24), iv);
  16024. }
  16025. if (ret == 0) {
  16026. ret = wc_CamelliaSetKey(&camellia, key24,
  16027. (word32)sizeof(key24), NULL);
  16028. }
  16029. if (ret == 0) {
  16030. ret = wc_CamelliaSetKey(&camellia, key32,
  16031. (word32)sizeof(key32), iv);
  16032. }
  16033. if (ret == 0) {
  16034. ret = wc_CamelliaSetKey(&camellia, key32,
  16035. (word32)sizeof(key32), NULL);
  16036. }
  16037. }
  16038. /* Bad args. */
  16039. if (ret == 0) {
  16040. ret = wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv);
  16041. if (ret != BAD_FUNC_ARG) {
  16042. ret = WOLFSSL_FATAL_ERROR;
  16043. }
  16044. else {
  16045. ret = 0;
  16046. }
  16047. } /* END bad args. */
  16048. res = TEST_RES_CHECK(ret == 0);
  16049. #endif
  16050. return res;
  16051. } /* END test_wc_CammeliaSetKey */
  16052. /*
  16053. * Testing wc_CamelliaSetIV()
  16054. */
  16055. static int test_wc_CamelliaSetIV(void)
  16056. {
  16057. int res = TEST_SKIPPED;
  16058. #ifdef HAVE_CAMELLIA
  16059. Camellia camellia;
  16060. static const byte iv[] =
  16061. {
  16062. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16063. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16064. };
  16065. int ret = 0;
  16066. ret = wc_CamelliaSetIV(&camellia, iv);
  16067. if (ret == 0) {
  16068. ret = wc_CamelliaSetIV(&camellia, NULL);
  16069. }
  16070. /* Bad args. */
  16071. if (ret == 0) {
  16072. ret = wc_CamelliaSetIV(NULL, NULL);
  16073. if (ret != BAD_FUNC_ARG) {
  16074. ret = WOLFSSL_FATAL_ERROR;
  16075. }
  16076. else {
  16077. ret = 0;
  16078. }
  16079. }
  16080. res = TEST_RES_CHECK(ret == 0);
  16081. #endif
  16082. return res;
  16083. } /*END test_wc_CamelliaSetIV*/
  16084. /*
  16085. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  16086. */
  16087. static int test_wc_CamelliaEncryptDecryptDirect(void)
  16088. {
  16089. int res = TEST_SKIPPED;
  16090. #ifdef HAVE_CAMELLIA
  16091. Camellia camellia;
  16092. static const byte key24[] =
  16093. {
  16094. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16095. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16096. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16097. };
  16098. static const byte iv[] =
  16099. {
  16100. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16101. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16102. };
  16103. static const byte plainT[] =
  16104. {
  16105. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16106. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16107. };
  16108. byte enc[sizeof(plainT)];
  16109. byte dec[sizeof(enc)];
  16110. int camE = WOLFSSL_FATAL_ERROR;
  16111. int camD = WOLFSSL_FATAL_ERROR;
  16112. int ret = 0;
  16113. /*Init stack variables.*/
  16114. XMEMSET(enc, 0, 16);
  16115. XMEMSET(enc, 0, 16);
  16116. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv);
  16117. if (ret == 0) {
  16118. ret = wc_CamelliaEncryptDirect(&camellia, enc, plainT);
  16119. if (ret == 0) {
  16120. ret = wc_CamelliaDecryptDirect(&camellia, dec, enc);
  16121. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  16122. ret = WOLFSSL_FATAL_ERROR;
  16123. }
  16124. }
  16125. }
  16126. /* Pass bad args. */
  16127. if (ret == 0) {
  16128. camE = wc_CamelliaEncryptDirect(NULL, enc, plainT);
  16129. if (camE == BAD_FUNC_ARG) {
  16130. camE = wc_CamelliaEncryptDirect(&camellia, NULL, plainT);
  16131. }
  16132. if (camE == BAD_FUNC_ARG) {
  16133. camE = wc_CamelliaEncryptDirect(&camellia, enc, NULL);
  16134. }
  16135. if (camE == BAD_FUNC_ARG) {
  16136. camE = 0;
  16137. }
  16138. else {
  16139. camE = WOLFSSL_FATAL_ERROR;
  16140. }
  16141. }
  16142. if (camE != 0) {
  16143. return TEST_FAIL;
  16144. }
  16145. if (ret == 0) {
  16146. camD = wc_CamelliaDecryptDirect(NULL, dec, enc);
  16147. if (camD == BAD_FUNC_ARG) {
  16148. camD = wc_CamelliaDecryptDirect(&camellia, NULL, enc);
  16149. }
  16150. if (camD == BAD_FUNC_ARG) {
  16151. camD = wc_CamelliaDecryptDirect(&camellia, dec, NULL);
  16152. }
  16153. if (camD == BAD_FUNC_ARG) {
  16154. camD = 0;
  16155. }
  16156. else {
  16157. camD = WOLFSSL_FATAL_ERROR;
  16158. }
  16159. }
  16160. res = TEST_RES_CHECK(camD == 0);
  16161. #endif
  16162. return res;
  16163. } /* END test-wc_CamelliaEncryptDecryptDirect */
  16164. /*
  16165. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  16166. */
  16167. static int test_wc_CamelliaCbcEncryptDecrypt(void)
  16168. {
  16169. int res = TEST_SKIPPED;
  16170. #ifdef HAVE_CAMELLIA
  16171. Camellia camellia;
  16172. static const byte key24[] =
  16173. {
  16174. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16175. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16176. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16177. };
  16178. static const byte plainT[] =
  16179. {
  16180. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16181. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16182. };
  16183. byte enc[CAMELLIA_BLOCK_SIZE];
  16184. byte dec[CAMELLIA_BLOCK_SIZE];
  16185. int camCbcE = WOLFSSL_FATAL_ERROR;
  16186. int camCbcD = WOLFSSL_FATAL_ERROR;
  16187. int ret = 0;
  16188. /* Init stack variables. */
  16189. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16190. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16191. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  16192. if (ret == 0) {
  16193. ret = wc_CamelliaCbcEncrypt(&camellia, enc, plainT, CAMELLIA_BLOCK_SIZE);
  16194. if (ret != 0) {
  16195. ret = WOLFSSL_FATAL_ERROR;
  16196. }
  16197. }
  16198. if (ret == 0) {
  16199. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  16200. if (ret == 0) {
  16201. ret = wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE);
  16202. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  16203. ret = WOLFSSL_FATAL_ERROR;
  16204. }
  16205. }
  16206. }
  16207. /* Pass in bad args. */
  16208. if (ret == 0) {
  16209. camCbcE = wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE);
  16210. if (camCbcE == BAD_FUNC_ARG) {
  16211. camCbcE = wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  16212. CAMELLIA_BLOCK_SIZE);
  16213. }
  16214. if (camCbcE == BAD_FUNC_ARG) {
  16215. camCbcE = wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  16216. CAMELLIA_BLOCK_SIZE);
  16217. }
  16218. if (camCbcE == BAD_FUNC_ARG) {
  16219. camCbcE = 0;
  16220. }
  16221. else {
  16222. camCbcE = WOLFSSL_FATAL_ERROR;
  16223. }
  16224. }
  16225. if (camCbcE != 0) {
  16226. return TEST_FAIL;
  16227. }
  16228. if (ret == 0) {
  16229. camCbcD = wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE);
  16230. if (camCbcD == BAD_FUNC_ARG) {
  16231. camCbcD = wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  16232. CAMELLIA_BLOCK_SIZE);
  16233. }
  16234. if (camCbcD == BAD_FUNC_ARG) {
  16235. camCbcD = wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  16236. CAMELLIA_BLOCK_SIZE);
  16237. }
  16238. if (camCbcD == BAD_FUNC_ARG) {
  16239. camCbcD = 0;
  16240. }
  16241. else {
  16242. camCbcD = WOLFSSL_FATAL_ERROR;
  16243. }
  16244. } /* END bad args. */
  16245. res = TEST_RES_CHECK(camCbcD == 0);
  16246. #endif
  16247. return res;
  16248. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  16249. /*
  16250. * Testing wc_Arc4SetKey()
  16251. */
  16252. static int test_wc_Arc4SetKey(void)
  16253. {
  16254. int res = TEST_SKIPPED;
  16255. #ifndef NO_RC4
  16256. Arc4 arc;
  16257. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16258. int keyLen = 8;
  16259. int ret = 0;
  16260. ret = wc_Arc4SetKey(&arc, (byte*)key, keyLen);
  16261. /* Test bad args. */
  16262. if (ret == 0) {
  16263. ret = wc_Arc4SetKey(NULL, (byte*)key, keyLen);
  16264. if (ret == BAD_FUNC_ARG)
  16265. ret = wc_Arc4SetKey(&arc, NULL, keyLen); /* NULL key */
  16266. if (ret == BAD_FUNC_ARG)
  16267. ret = wc_Arc4SetKey(&arc, (byte*)key, 0); /* length == 0 */
  16268. if (ret == BAD_FUNC_ARG)
  16269. ret = WOLFSSL_ERROR_NONE;
  16270. else
  16271. ret = WOLFSSL_FATAL_ERROR;
  16272. } /* END test bad args. */
  16273. res = TEST_RES_CHECK(ret == 0);
  16274. #endif
  16275. return res;
  16276. } /* END test_wc_Arc4SetKey */
  16277. /*
  16278. * Testing wc_Arc4Process for ENC/DEC.
  16279. */
  16280. static int test_wc_Arc4Process(void)
  16281. {
  16282. int res = TEST_SKIPPED;
  16283. #ifndef NO_RC4
  16284. Arc4 enc, dec;
  16285. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16286. int keyLen = 8;
  16287. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16288. byte cipher[8];
  16289. byte plain[8];
  16290. int ret;
  16291. /* Init stack variables */
  16292. XMEMSET(cipher, 0, sizeof(cipher));
  16293. XMEMSET(plain, 0, sizeof(plain));
  16294. /* Use for async. */
  16295. ret = wc_Arc4Init(&enc, NULL, INVALID_DEVID);
  16296. if (ret == 0) {
  16297. ret = wc_Arc4Init(&dec, NULL, INVALID_DEVID);
  16298. }
  16299. if (ret == 0) {
  16300. ret = wc_Arc4SetKey(&enc, (byte*)key, keyLen);
  16301. }
  16302. if (ret == 0) {
  16303. ret = wc_Arc4SetKey(&dec, (byte*)key, keyLen);
  16304. }
  16305. if (ret == 0) {
  16306. ret = wc_Arc4Process(&enc, cipher, (byte*)input, keyLen);
  16307. }
  16308. if (ret == 0) {
  16309. ret = wc_Arc4Process(&dec, plain, cipher, keyLen);
  16310. if (ret != 0 || XMEMCMP(plain, input, keyLen)) {
  16311. ret = WOLFSSL_FATAL_ERROR;
  16312. }
  16313. else {
  16314. ret = 0;
  16315. }
  16316. }
  16317. /* Bad args. */
  16318. if (ret == 0) {
  16319. ret = wc_Arc4Process(NULL, plain, cipher, keyLen);
  16320. if (ret == BAD_FUNC_ARG) {
  16321. ret = wc_Arc4Process(&dec, NULL, cipher, keyLen);
  16322. }
  16323. if (ret == BAD_FUNC_ARG) {
  16324. ret = wc_Arc4Process(&dec, plain, NULL, keyLen);
  16325. }
  16326. if (ret == BAD_FUNC_ARG) {
  16327. ret = 0;
  16328. }
  16329. else {
  16330. ret = WOLFSSL_FATAL_ERROR;
  16331. }
  16332. }
  16333. wc_Arc4Free(&enc);
  16334. wc_Arc4Free(&dec);
  16335. res = TEST_RES_CHECK(ret == 0);
  16336. #endif
  16337. return res;
  16338. }/* END test_wc_Arc4Process */
  16339. /*
  16340. * Testing wc_Init RsaKey()
  16341. */
  16342. static int test_wc_InitRsaKey(void)
  16343. {
  16344. int res = TEST_SKIPPED;
  16345. #ifndef NO_RSA
  16346. RsaKey key;
  16347. int ret = 0;
  16348. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16349. /* Test bad args. */
  16350. if (ret == 0) {
  16351. ret = wc_InitRsaKey(NULL, HEAP_HINT);
  16352. #ifndef HAVE_USER_RSA
  16353. if (ret == BAD_FUNC_ARG) {
  16354. ret = 0;
  16355. }
  16356. else {
  16357. #else
  16358. if (ret == USER_CRYPTO_ERROR) {
  16359. ret = 0;
  16360. }
  16361. else {
  16362. #endif
  16363. ret = WOLFSSL_FATAL_ERROR;
  16364. }
  16365. } /* end if */
  16366. if (wc_FreeRsaKey(&key) || ret != 0) {
  16367. ret = WOLFSSL_FATAL_ERROR;
  16368. }
  16369. res = TEST_RES_CHECK(ret == 0);
  16370. #endif
  16371. return res;
  16372. } /* END test_wc_InitRsaKey */
  16373. /*
  16374. * Testing wc_RsaPrivateKeyDecode()
  16375. */
  16376. static int test_wc_RsaPrivateKeyDecode(void)
  16377. {
  16378. int res = TEST_SKIPPED;
  16379. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  16380. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  16381. RsaKey key;
  16382. byte* tmp;
  16383. word32 idx = 0;
  16384. int bytes = 0;
  16385. int ret = 0;
  16386. tmp = (byte*)XMALLOC(FOURK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16387. if (tmp == NULL) {
  16388. ret = WOLFSSL_FATAL_ERROR;
  16389. }
  16390. if (ret == 0) {
  16391. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16392. }
  16393. if (ret == 0) {
  16394. #ifdef USE_CERT_BUFFERS_1024
  16395. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  16396. bytes = sizeof_client_key_der_1024;
  16397. #else
  16398. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  16399. bytes = sizeof_client_key_der_2048;
  16400. #endif /* Use cert buffers. */
  16401. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
  16402. }
  16403. #ifndef HAVE_USER_RSA
  16404. /* Test bad args. */
  16405. if (ret == 0) {
  16406. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  16407. if (ret == BAD_FUNC_ARG) {
  16408. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  16409. }
  16410. if (ret == BAD_FUNC_ARG) {
  16411. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  16412. }
  16413. if (ret == BAD_FUNC_ARG) {
  16414. ret = 0;
  16415. }
  16416. else {
  16417. ret = WOLFSSL_FATAL_ERROR;
  16418. }
  16419. }
  16420. #else
  16421. /* Test bad args. User RSA. */
  16422. if (ret == 0) {
  16423. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  16424. if (ret == USER_CRYPTO_ERROR) {
  16425. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  16426. }
  16427. if (ret == USER_CRYPTO_ERROR) {
  16428. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  16429. }
  16430. if (ret == USER_CRYPTO_ERROR) {
  16431. ret = 0;
  16432. }
  16433. else {
  16434. ret = WOLFSSL_FATAL_ERROR;
  16435. }
  16436. }
  16437. #endif
  16438. if (tmp != NULL) {
  16439. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16440. }
  16441. if (wc_FreeRsaKey(&key) || ret != 0) {
  16442. ret = WOLFSSL_FATAL_ERROR;
  16443. }
  16444. res = TEST_RES_CHECK(ret == 0);
  16445. #endif
  16446. return res;
  16447. } /* END test_wc_RsaPrivateKeyDecode */
  16448. /*
  16449. * Testing wc_RsaPublicKeyDecode()
  16450. */
  16451. static int test_wc_RsaPublicKeyDecode(void)
  16452. {
  16453. int res = TEST_SKIPPED;
  16454. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  16455. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  16456. RsaKey keyPub;
  16457. byte* tmp;
  16458. word32 idx = 0;
  16459. int bytes = 0;
  16460. word32 keySz = 0;
  16461. word32 tstKeySz = 0;
  16462. int ret = 0;
  16463. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  16464. XFILE f;
  16465. const char* rsaPssPubKey = "./certs/rsapss/ca-rsapss-key.der";
  16466. const char* rsaPssPubKeyNoParams = "./certs/rsapss/ca-3072-rsapss-key.der";
  16467. byte buf[4096];
  16468. #endif
  16469. tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16470. if (tmp == NULL) {
  16471. ret = WOLFSSL_FATAL_ERROR;
  16472. }
  16473. if (ret == 0) {
  16474. ret = wc_InitRsaKey(&keyPub, HEAP_HINT);
  16475. }
  16476. if (ret == 0) {
  16477. #ifdef USE_CERT_BUFFERS_1024
  16478. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  16479. bytes = sizeof_client_keypub_der_1024;
  16480. keySz = 1024;
  16481. #else
  16482. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  16483. bytes = sizeof_client_keypub_der_2048;
  16484. keySz = 2048;
  16485. #endif
  16486. ret = wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes);
  16487. }
  16488. #ifndef HAVE_USER_RSA
  16489. /* Pass in bad args. */
  16490. if (ret == 0) {
  16491. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  16492. if (ret == BAD_FUNC_ARG) {
  16493. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  16494. }
  16495. if (ret == BAD_FUNC_ARG) {
  16496. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  16497. }
  16498. if (ret == BAD_FUNC_ARG) {
  16499. ret = 0;
  16500. }
  16501. else {
  16502. ret = WOLFSSL_FATAL_ERROR;
  16503. }
  16504. }
  16505. #else
  16506. /* Pass in bad args. */
  16507. if (ret == 0) {
  16508. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  16509. if (ret == USER_CRYPTO_ERROR) {
  16510. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  16511. }
  16512. if (ret == USER_CRYPTO_ERROR) {
  16513. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  16514. }
  16515. if (ret == USER_CRYPTO_ERROR) {
  16516. ret = 0;
  16517. }
  16518. else {
  16519. ret = WOLFSSL_FATAL_ERROR;
  16520. }
  16521. }
  16522. #endif
  16523. if (wc_FreeRsaKey(&keyPub) || ret != 0) {
  16524. ret = WOLFSSL_FATAL_ERROR;
  16525. }
  16526. if (ret == 0) {
  16527. /* Test for getting modulus key size */
  16528. idx = 0;
  16529. ret = wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  16530. &tstKeySz, NULL, NULL);
  16531. ret = (ret == 0 && tstKeySz == keySz/8) ? 0 : WOLFSSL_FATAL_ERROR;
  16532. }
  16533. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  16534. f = XFOPEN(rsaPssPubKey, "rb");
  16535. AssertTrue((f != XBADFILE));
  16536. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  16537. XFCLOSE(f);
  16538. idx = 0;
  16539. AssertIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  16540. NULL), 0);
  16541. f = XFOPEN(rsaPssPubKeyNoParams, "rb");
  16542. AssertTrue((f != XBADFILE));
  16543. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  16544. XFCLOSE(f);
  16545. idx = 0;
  16546. AssertIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  16547. NULL), 0);
  16548. #endif
  16549. if (tmp != NULL) {
  16550. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16551. }
  16552. res = TEST_RES_CHECK(ret == 0);
  16553. #endif
  16554. return res;
  16555. } /* END test_wc_RsaPublicKeyDecode */
  16556. /*
  16557. * Testing wc_RsaPublicKeyDecodeRaw()
  16558. */
  16559. static int test_wc_RsaPublicKeyDecodeRaw(void)
  16560. {
  16561. int res = TEST_SKIPPED;
  16562. #if !defined(NO_RSA)
  16563. RsaKey key;
  16564. const byte n = 0x23;
  16565. const byte e = 0x03;
  16566. int nSz = sizeof(n);
  16567. int eSz = sizeof(e);
  16568. int ret;
  16569. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16570. if (ret == 0) {
  16571. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key);
  16572. }
  16573. #ifndef HAVE_USER_RSA
  16574. /* Pass in bad args. */
  16575. if (ret == 0) {
  16576. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  16577. if (ret == BAD_FUNC_ARG) {
  16578. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  16579. }
  16580. if (ret == BAD_FUNC_ARG) {
  16581. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  16582. }
  16583. if (ret == BAD_FUNC_ARG) {
  16584. ret = 0;
  16585. }
  16586. else {
  16587. ret = WOLFSSL_FATAL_ERROR;
  16588. }
  16589. }
  16590. #else
  16591. /* Pass in bad args. User RSA. */
  16592. if (ret == 0) {
  16593. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  16594. if (ret == USER_CRYPTO_ERROR) {
  16595. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  16596. }
  16597. if (ret == USER_CRYPTO_ERROR) {
  16598. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  16599. }
  16600. if (ret == USER_CRYPTO_ERROR) {
  16601. ret = 0;
  16602. }
  16603. else {
  16604. ret = WOLFSSL_FATAL_ERROR;
  16605. }
  16606. }
  16607. #endif
  16608. if (wc_FreeRsaKey(&key) || ret != 0) {
  16609. ret = WOLFSSL_FATAL_ERROR;
  16610. }
  16611. res = TEST_RES_CHECK(ret == 0);
  16612. #endif
  16613. return res;
  16614. } /* END test_wc_RsaPublicKeyDecodeRaw */
  16615. #if (!defined(NO_RSA) || !defined(HAVE_FAST_RSA)) && defined(WOLFSSL_KEY_GEN)
  16616. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  16617. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  16618. * trying until it gets a probable prime. */
  16619. #ifdef HAVE_FIPS
  16620. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  16621. {
  16622. int ret;
  16623. for (;;) {
  16624. ret = wc_MakeRsaKey(key, size, e, rng);
  16625. if (ret != PRIME_GEN_E) break;
  16626. fprintf(stderr, "MakeRsaKey couldn't find prime; "
  16627. "trying again.\n");
  16628. }
  16629. return ret;
  16630. }
  16631. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  16632. #else
  16633. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  16634. #endif
  16635. #endif
  16636. /*
  16637. * Testing wc_MakeRsaKey()
  16638. */
  16639. static int test_wc_MakeRsaKey(void)
  16640. {
  16641. int res = TEST_SKIPPED;
  16642. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16643. RsaKey genKey;
  16644. WC_RNG rng;
  16645. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16646. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16647. int bits = 1024;
  16648. #else
  16649. int bits = 2048;
  16650. #endif
  16651. int ret = 0;
  16652. ret = wc_InitRsaKey(&genKey, HEAP_HINT);
  16653. if (ret == 0) {
  16654. ret = wc_InitRng(&rng);
  16655. if (ret == 0) {
  16656. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
  16657. if (ret == 0 && wc_FreeRsaKey(&genKey) != 0) {
  16658. ret = WOLFSSL_FATAL_ERROR;
  16659. }
  16660. }
  16661. }
  16662. #ifndef HAVE_USER_RSA
  16663. /* Test bad args. */
  16664. if (ret == 0) {
  16665. ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
  16666. if (ret == BAD_FUNC_ARG) {
  16667. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
  16668. }
  16669. if (ret == BAD_FUNC_ARG) {
  16670. /* e < 3 */
  16671. ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
  16672. }
  16673. if (ret == BAD_FUNC_ARG) {
  16674. /* e & 1 == 0 */
  16675. ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
  16676. }
  16677. if (ret == BAD_FUNC_ARG) {
  16678. ret = 0;
  16679. }
  16680. else {
  16681. ret = WOLFSSL_FATAL_ERROR;
  16682. }
  16683. }
  16684. #else
  16685. /* Test bad args. */
  16686. if (ret == 0) {
  16687. ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
  16688. if (ret == USER_CRYPTO_ERROR) {
  16689. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
  16690. }
  16691. if (ret == USER_CRYPTO_ERROR) {
  16692. /* e < 3 */
  16693. ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
  16694. }
  16695. if (ret == USER_CRYPTO_ERROR) {
  16696. /* e & 1 == 0 */
  16697. ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
  16698. }
  16699. if (ret == USER_CRYPTO_ERROR) {
  16700. ret = 0;
  16701. }
  16702. else {
  16703. ret = WOLFSSL_FATAL_ERROR;
  16704. }
  16705. }
  16706. #endif
  16707. if (wc_FreeRng(&rng) || ret != 0) {
  16708. ret = WOLFSSL_FATAL_ERROR;
  16709. }
  16710. res = TEST_RES_CHECK(ret == 0);
  16711. #endif
  16712. return res;
  16713. } /* END test_wc_MakeRsaKey */
  16714. /*
  16715. * Test the bounds checking on the cipher text versus the key modulus.
  16716. * 1. Make a new RSA key.
  16717. * 2. Set c to 1.
  16718. * 3. Decrypt c into k. (error)
  16719. * 4. Copy the key modulus to c and sub 1 from the copy.
  16720. * 5. Decrypt c into k. (error)
  16721. * Valid bounds test cases are covered by all the other RSA tests.
  16722. */
  16723. static int test_RsaDecryptBoundsCheck(void)
  16724. {
  16725. int res = TEST_SKIPPED;
  16726. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  16727. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  16728. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  16729. WC_RNG rng;
  16730. RsaKey key;
  16731. byte flatC[256];
  16732. word32 flatCSz;
  16733. byte out[256];
  16734. word32 outSz = sizeof(out);
  16735. int ret;
  16736. XMEMSET(&rng, 0, sizeof(rng));
  16737. ret = wc_InitRng(&rng);
  16738. if (ret == 0)
  16739. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16740. if (ret == 0) {
  16741. const byte* derKey;
  16742. word32 derKeySz;
  16743. word32 idx = 0;
  16744. #ifdef USE_CERT_BUFFERS_1024
  16745. derKey = server_key_der_1024;
  16746. derKeySz = (word32)sizeof_server_key_der_1024;
  16747. flatCSz = 128;
  16748. #else
  16749. derKey = server_key_der_2048;
  16750. derKeySz = (word32)sizeof_server_key_der_2048;
  16751. flatCSz = 256;
  16752. #endif
  16753. ret = wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz);
  16754. }
  16755. if (ret == 0) {
  16756. XMEMSET(flatC, 0, flatCSz);
  16757. flatC[flatCSz-1] = 1;
  16758. ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  16759. RSA_PRIVATE_DECRYPT, &rng);
  16760. if (ret == RSA_OUT_OF_RANGE_E) {
  16761. mp_int c;
  16762. mp_init_copy(&c, &key.n);
  16763. mp_sub_d(&c, 1, &c);
  16764. mp_to_unsigned_bin(&c, flatC);
  16765. ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  16766. RSA_PRIVATE_DECRYPT, NULL);
  16767. mp_clear(&c);
  16768. }
  16769. if (ret == RSA_OUT_OF_RANGE_E)
  16770. ret = 0;
  16771. else
  16772. ret = WOLFSSL_FATAL_ERROR;
  16773. }
  16774. if (wc_FreeRsaKey(&key) || wc_FreeRng(&rng) || ret != 0)
  16775. ret = WOLFSSL_FATAL_ERROR;
  16776. res = TEST_RES_CHECK(ret == 0);
  16777. #endif
  16778. return res;
  16779. } /* END test_wc_RsaDecryptBoundsCheck */
  16780. /*
  16781. * Testing wc_SetKeyUsage()
  16782. */
  16783. static int test_wc_SetKeyUsage(void)
  16784. {
  16785. int res = TEST_SKIPPED;
  16786. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  16787. Cert myCert;
  16788. int ret = 0;
  16789. ret = wc_InitCert(&myCert);
  16790. if (ret == 0) {
  16791. ret = wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement");
  16792. if (ret == 0) {
  16793. ret = wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation");
  16794. }
  16795. if (ret == 0) {
  16796. ret = wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly");
  16797. }
  16798. if (ret == 0) {
  16799. ret = wc_SetKeyUsage(&myCert, "decipherOnly");
  16800. }
  16801. if (ret == 0) {
  16802. ret = wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign");
  16803. }
  16804. }
  16805. /* Test bad args. */
  16806. if (ret == 0) {
  16807. ret = wc_SetKeyUsage(NULL, "decipherOnly");
  16808. if (ret == BAD_FUNC_ARG) {
  16809. ret = wc_SetKeyUsage(&myCert, NULL);
  16810. }
  16811. if (ret == BAD_FUNC_ARG) {
  16812. ret = wc_SetKeyUsage(&myCert, "");
  16813. }
  16814. if (ret == KEYUSAGE_E) {
  16815. ret = wc_SetKeyUsage(&myCert, ",");
  16816. }
  16817. if (ret == KEYUSAGE_E) {
  16818. ret = wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign");
  16819. }
  16820. if (ret == KEYUSAGE_E) {
  16821. ret = 0;
  16822. }
  16823. else {
  16824. ret = WOLFSSL_FATAL_ERROR;
  16825. }
  16826. }
  16827. res = TEST_RES_CHECK(ret == 0);
  16828. #endif
  16829. return res;
  16830. } /* END test_wc_SetKeyUsage */
  16831. /*
  16832. * Testing wc_CheckProbablePrime()
  16833. */
  16834. static int test_wc_CheckProbablePrime(void)
  16835. {
  16836. int res = TEST_SKIPPED;
  16837. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16838. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  16839. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  16840. RsaKey key;
  16841. WC_RNG rng;
  16842. byte e[3];
  16843. word32 eSz = (word32)sizeof(e);
  16844. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  16845. word32 nSz = (word32)sizeof(n);
  16846. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  16847. word32 dSz = (word32)sizeof(d);
  16848. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  16849. word32 pSz = (word32)sizeof(p);
  16850. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  16851. word32 qSz = (word32)sizeof(q);
  16852. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  16853. int ret = 0;
  16854. int* isPrime;
  16855. int test[5];
  16856. isPrime = test;
  16857. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16858. if (ret == 0) {
  16859. ret = wc_InitRng(&rng);
  16860. }
  16861. if (ret == 0) {
  16862. ret = wc_RsaSetRNG(&key, &rng);
  16863. }
  16864. if (ret == 0) {
  16865. ret = wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS, WC_RSA_EXPONENT, &rng);
  16866. }
  16867. if (ret == 0) {
  16868. PRIVATE_KEY_UNLOCK();
  16869. ret = wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz,
  16870. p, &pSz, q, &qSz);
  16871. PRIVATE_KEY_LOCK();
  16872. }
  16873. /* Bad cases */
  16874. if (ret == 0) {
  16875. ret = wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz,
  16876. nlen, isPrime);
  16877. if (ret == BAD_FUNC_ARG) {
  16878. ret = 0;
  16879. }
  16880. }
  16881. if (ret == 0) {
  16882. ret = wc_CheckProbablePrime(p, 0, q, qSz, e, eSz,
  16883. nlen, isPrime);
  16884. if (ret == BAD_FUNC_ARG) {
  16885. ret = 0;
  16886. }
  16887. }
  16888. if (ret == 0) {
  16889. ret = wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz,
  16890. nlen, isPrime);
  16891. if (ret == BAD_FUNC_ARG) {
  16892. ret = 0;
  16893. }
  16894. }
  16895. if (ret == 0) {
  16896. ret = wc_CheckProbablePrime(p, pSz, q, 0, e, eSz,
  16897. nlen, isPrime);
  16898. if (ret == BAD_FUNC_ARG) {
  16899. ret = 0;
  16900. }
  16901. }
  16902. if (ret == 0) {
  16903. ret = wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz,
  16904. nlen, isPrime);
  16905. if (ret == BAD_FUNC_ARG) {
  16906. ret = 0;
  16907. }
  16908. }
  16909. if (ret == 0) {
  16910. ret = wc_CheckProbablePrime(p, pSz, q, qSz, e, 0,
  16911. nlen, isPrime);
  16912. if (ret == BAD_FUNC_ARG) {
  16913. ret = 0;
  16914. }
  16915. }
  16916. if (ret == 0) {
  16917. ret = wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0,
  16918. nlen, isPrime);
  16919. if (ret == BAD_FUNC_ARG) {
  16920. ret = 0;
  16921. }
  16922. }
  16923. /* Good case */
  16924. if (ret == 0) {
  16925. ret = wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz,
  16926. nlen, isPrime);
  16927. }
  16928. wc_FreeRsaKey(&key);
  16929. wc_FreeRng(&rng);
  16930. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  16931. res = TEST_RES_CHECK(ret == 0);
  16932. #endif
  16933. return res;
  16934. } /* END test_wc_CheckProbablePrime */
  16935. /*
  16936. * Testing wc_RsaPSS_Verify()
  16937. */
  16938. static int test_wc_RsaPSS_Verify(void)
  16939. {
  16940. int res = TEST_SKIPPED;
  16941. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16942. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16943. RsaKey key;
  16944. WC_RNG rng;
  16945. int sz = 256;
  16946. byte* pt;
  16947. const char* szMessage = "This is the string to be signed";
  16948. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16949. unsigned char pDecrypted[2048/8];
  16950. word32 outLen = sizeof(pDecrypted);
  16951. int ret = 0;
  16952. pt = pDecrypted;
  16953. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16954. if (ret == 0) {
  16955. ret = wc_InitRng(&rng);
  16956. }
  16957. if (ret == 0) {
  16958. ret = wc_RsaSetRNG(&key, &rng);
  16959. }
  16960. if (ret == 0) {
  16961. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  16962. }
  16963. if (ret == 0) {
  16964. ret = wc_RsaPSS_Sign((byte*)szMessage, (word32)XSTRLEN(szMessage)+1,
  16965. pSignature, sizeof(pSignature),
  16966. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  16967. if (ret > 0) {
  16968. sz = ret;
  16969. ret = 0;
  16970. }
  16971. }
  16972. /* Bad cases */
  16973. if (ret == 0) {
  16974. ret = wc_RsaPSS_Verify(NULL, sz, pt, outLen,
  16975. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16976. if (ret == BAD_FUNC_ARG) {
  16977. ret = 0;
  16978. }
  16979. }
  16980. if (ret == 0) {
  16981. ret = wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  16982. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16983. if (ret == BAD_FUNC_ARG) {
  16984. ret = 0;
  16985. }
  16986. }
  16987. if (ret == 0) {
  16988. ret = wc_RsaPSS_Verify(pSignature, sz, NULL, outLen,
  16989. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16990. if (ret == BAD_FUNC_ARG) {
  16991. ret = 0;
  16992. }
  16993. }
  16994. if (ret == 0) {
  16995. ret = wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  16996. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16997. if (ret == BAD_FUNC_ARG) {
  16998. ret = 0;
  16999. }
  17000. }
  17001. /* Good case */
  17002. if (ret == 0) {
  17003. ret = wc_RsaPSS_Verify(pSignature, sz, pt, outLen,
  17004. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17005. if (ret > 0) {
  17006. ret = 0;
  17007. }
  17008. }
  17009. wc_FreeRsaKey(&key);
  17010. wc_FreeRng(&rng);
  17011. res = TEST_RES_CHECK(ret == 0);
  17012. #endif
  17013. return res;
  17014. } /* END test_wc_RsaPSS_Verify */
  17015. /*
  17016. * Testing wc_RsaPSS_VerifyCheck()
  17017. */
  17018. static int test_wc_RsaPSS_VerifyCheck(void)
  17019. {
  17020. int res = TEST_SKIPPED;
  17021. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17022. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17023. RsaKey key;
  17024. WC_RNG rng;
  17025. int sz = 256; /* 2048/8 */
  17026. byte* pt;
  17027. byte digest[32];
  17028. word32 digestSz = sizeof(digest);
  17029. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17030. word32 pSignatureSz = sizeof(pSignature);
  17031. unsigned char pDecrypted[2048/8];
  17032. word32 outLen = sizeof(pDecrypted);
  17033. int ret = 0;
  17034. pt = pDecrypted;
  17035. XMEMSET(digest, 0, sizeof(digest));
  17036. XMEMSET(pSignature, 0, sizeof(pSignature));
  17037. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17038. if (ret == 0) {
  17039. ret = wc_InitRng(&rng);
  17040. }
  17041. if (ret == 0) {
  17042. ret = wc_RsaSetRNG(&key, &rng);
  17043. }
  17044. if (ret == 0) {
  17045. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  17046. }
  17047. if (ret == 0) {
  17048. digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256);
  17049. ret = wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz);
  17050. }
  17051. if (ret == 0) {
  17052. ret = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  17053. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  17054. if (ret > 0) {
  17055. sz = ret;
  17056. ret = 0;
  17057. }
  17058. }
  17059. /* Bad cases */
  17060. if (ret == 0) {
  17061. ret = wc_RsaPSS_VerifyCheck(NULL, sz, pt, outLen,
  17062. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17063. if (ret == BAD_FUNC_ARG) {
  17064. ret = 0;
  17065. }
  17066. }
  17067. if (ret == 0) {
  17068. ret = wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen,
  17069. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17070. if (ret == BAD_FUNC_ARG) {
  17071. ret = 0;
  17072. }
  17073. }
  17074. if (ret == 0) {
  17075. ret = wc_RsaPSS_VerifyCheck(pSignature, sz, NULL, outLen,
  17076. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17077. if (ret == BAD_FUNC_ARG) {
  17078. ret = 0;
  17079. }
  17080. }
  17081. if (ret == 0) {
  17082. ret = wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen,
  17083. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17084. if (ret == BAD_FUNC_ARG) {
  17085. ret = 0;
  17086. }
  17087. }
  17088. /* Good case */
  17089. if (ret == 0) {
  17090. ret = wc_RsaPSS_VerifyCheck(pSignature, sz, pt, outLen,
  17091. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17092. if (ret > 0) {
  17093. ret = 0;
  17094. }
  17095. }
  17096. wc_FreeRsaKey(&key);
  17097. wc_FreeRng(&rng);
  17098. res = TEST_RES_CHECK(ret == 0);
  17099. #endif
  17100. return res;
  17101. } /* END test_wc_RsaPSS_VerifyCheck */
  17102. /*
  17103. * Testing wc_RsaPSS_VerifyCheckInline()
  17104. */
  17105. static int test_wc_RsaPSS_VerifyCheckInline(void)
  17106. {
  17107. int res = TEST_SKIPPED;
  17108. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17109. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17110. RsaKey key;
  17111. WC_RNG rng;
  17112. int sz = 256;
  17113. byte* pt;
  17114. byte digest[32];
  17115. word32 digestSz = sizeof(digest);
  17116. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17117. unsigned char pDecrypted[2048/8];
  17118. int ret;
  17119. pt = pDecrypted;
  17120. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17121. XMEMSET(digest, 0, sizeof(digest));
  17122. XMEMSET(pSignature, 0, sizeof(pSignature));
  17123. if (ret == 0) {
  17124. ret = wc_InitRng(&rng);
  17125. }
  17126. if (ret == 0) {
  17127. ret = wc_RsaSetRNG(&key, &rng);
  17128. }
  17129. if (ret == 0) {
  17130. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  17131. }
  17132. if (ret == 0) {
  17133. digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256);
  17134. ret = wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz);
  17135. }
  17136. if (ret == 0) {
  17137. ret = wc_RsaPSS_Sign(digest, digestSz, pSignature, sizeof(pSignature),
  17138. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  17139. if (ret > 0) {
  17140. sz = ret;
  17141. ret = 0;
  17142. }
  17143. }
  17144. /* Bad Cases */
  17145. if (ret == 0) {
  17146. ret = wc_RsaPSS_VerifyCheckInline(NULL, sz, &pt,
  17147. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17148. if (ret == BAD_FUNC_ARG) {
  17149. ret = 0;
  17150. }
  17151. }
  17152. if (ret == 0) {
  17153. ret = wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL,
  17154. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17155. if (ret == BAD_FUNC_ARG) {
  17156. ret = 0;
  17157. }
  17158. }
  17159. if (ret == 0) {
  17160. ret = wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt,
  17161. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17162. if (ret == BAD_FUNC_ARG) {
  17163. ret = 0;
  17164. }
  17165. }
  17166. if (ret == 0) {
  17167. ret = wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt,
  17168. digest, digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key);
  17169. if (ret == BAD_FUNC_ARG) {
  17170. ret = 0;
  17171. }
  17172. }
  17173. /* Good case */
  17174. if (ret == 0) {
  17175. ret = wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt,
  17176. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17177. if (ret > 0) {
  17178. ret = 0;
  17179. }
  17180. }
  17181. wc_FreeRsaKey(&key);
  17182. wc_FreeRng(&rng);
  17183. res = TEST_RES_CHECK(ret == 0);
  17184. #endif
  17185. return res;
  17186. } /* END test_wc_RsaPSS_VerifyCheckInline */
  17187. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17188. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  17189. {
  17190. (void)flag;
  17191. (void)type;
  17192. (void)file;
  17193. (void)line;
  17194. }
  17195. #endif
  17196. /*
  17197. * Testing wc_LockMutex_ex
  17198. */
  17199. static int test_wc_LockMutex_ex(void)
  17200. {
  17201. int res = TEST_SKIPPED;
  17202. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17203. int ret = 0;
  17204. int flag = CRYPTO_LOCK;
  17205. int type = 0;
  17206. const char* file = "./test-LockMutex_ex.txt";
  17207. int line = 0;
  17208. /* without SetMutexCb */
  17209. ret = wc_LockMutex_ex(flag, type, file, line);
  17210. if (ret == BAD_STATE_E) {
  17211. ret = 0;
  17212. }
  17213. /* with SetMutexCb */
  17214. if (ret == 0) {
  17215. ret = wc_SetMutexCb(sample_mutex_cb);
  17216. if (ret == 0) {
  17217. ret = wc_LockMutex_ex(flag, type, file, line);
  17218. }
  17219. }
  17220. res = TEST_RES_CHECK(ret == 0);
  17221. #endif
  17222. return res;
  17223. }/*End test_wc_LockMutex_ex*/
  17224. /*
  17225. * Testing wc_SetMutexCb
  17226. */
  17227. static int test_wc_SetMutexCb(void)
  17228. {
  17229. int res = TEST_SKIPPED;
  17230. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17231. int ret = wc_SetMutexCb(sample_mutex_cb);
  17232. res = TEST_RES_CHECK(ret == 0);
  17233. #endif
  17234. return res;
  17235. }/*End test_wc_SetMutexCb*/
  17236. /*
  17237. * Testing wc_RsaKeyToDer()
  17238. */
  17239. static int test_wc_RsaKeyToDer(void)
  17240. {
  17241. int res = TEST_SKIPPED;
  17242. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17243. RsaKey genKey;
  17244. WC_RNG rng;
  17245. byte* der;
  17246. int ret = 0;
  17247. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17248. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17249. int bits = 1024;
  17250. word32 derSz = 611;
  17251. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  17252. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  17253. #else
  17254. int bits = 2048;
  17255. word32 derSz = 1196;
  17256. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  17257. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  17258. #endif
  17259. XMEMSET(&rng, 0, sizeof(rng));
  17260. XMEMSET(&genKey, 0, sizeof(genKey));
  17261. der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17262. if (der == NULL) {
  17263. ret = WOLFSSL_FATAL_ERROR;
  17264. }
  17265. /* Init structures. */
  17266. if (ret == 0) {
  17267. ret = wc_InitRsaKey(&genKey, HEAP_HINT);
  17268. }
  17269. if (ret == 0) {
  17270. ret = wc_InitRng(&rng);
  17271. }
  17272. /* Make key. */
  17273. if (ret == 0) {
  17274. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
  17275. if (ret != 0) {
  17276. ret = WOLFSSL_FATAL_ERROR;
  17277. }
  17278. }
  17279. if (ret == 0) {
  17280. ret = wc_RsaKeyToDer(&genKey, der, derSz);
  17281. if (ret > 0) {
  17282. ret = 0;
  17283. }
  17284. else {
  17285. ret = WOLFSSL_FATAL_ERROR;
  17286. }
  17287. }
  17288. #ifndef HAVE_USER_RSA
  17289. /* Pass good/bad args. */
  17290. if (ret == 0) {
  17291. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  17292. if (ret == BAD_FUNC_ARG) {
  17293. /* Get just the output length */
  17294. ret = wc_RsaKeyToDer(&genKey, NULL, 0);
  17295. }
  17296. if (ret > 0) {
  17297. /* Try Public Key. */
  17298. genKey.type = 0;
  17299. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  17300. #ifdef WOLFSSL_CHECK_MEM_ZERO
  17301. /* Put back to Private Key */
  17302. genKey.type = 1;
  17303. #endif
  17304. }
  17305. if (ret == BAD_FUNC_ARG) {
  17306. ret = 0;
  17307. }
  17308. else {
  17309. ret = WOLFSSL_FATAL_ERROR;
  17310. }
  17311. }
  17312. #else
  17313. /* Pass good/bad args. */
  17314. if (ret == 0) {
  17315. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  17316. if (ret == USER_CRYPTO_ERROR) {
  17317. /* Get just the output length */
  17318. ret = wc_RsaKeyToDer(&genKey, NULL, 0);
  17319. }
  17320. if (ret > 0) {
  17321. /* Try Public Key. */
  17322. genKey.type = 0;
  17323. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  17324. #ifdef WOLFSSL_CHECK_MEM_ZERO
  17325. /* Put back to Private Key */
  17326. genKey.type = 1;
  17327. #endif
  17328. }
  17329. if (ret == USER_CRYPTO_ERROR) {
  17330. ret = 0;
  17331. }
  17332. else {
  17333. ret = WOLFSSL_FATAL_ERROR;
  17334. }
  17335. }
  17336. #endif
  17337. if (der != NULL) {
  17338. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17339. }
  17340. if (wc_FreeRsaKey(&genKey) || ret != 0) {
  17341. ret = WOLFSSL_FATAL_ERROR;
  17342. }
  17343. if (wc_FreeRng(&rng) || ret != 0) {
  17344. ret = WOLFSSL_FATAL_ERROR;
  17345. }
  17346. res = TEST_RES_CHECK(ret == 0);
  17347. #endif
  17348. return res;
  17349. } /* END test_wc_RsaKeyToDer */
  17350. /*
  17351. * Testing wc_RsaKeyToPublicDer()
  17352. */
  17353. static int test_wc_RsaKeyToPublicDer(void)
  17354. {
  17355. int res = TEST_SKIPPED;
  17356. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17357. RsaKey key;
  17358. WC_RNG rng;
  17359. byte* der;
  17360. int ret = 0;
  17361. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17362. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17363. int bits = 1024;
  17364. word32 derLen = 162;
  17365. #else
  17366. int bits = 2048;
  17367. word32 derLen = 294;
  17368. #endif
  17369. XMEMSET(&rng, 0, sizeof(rng));
  17370. XMEMSET(&key, 0, sizeof(key));
  17371. der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17372. if (der == NULL) {
  17373. ret = WOLFSSL_FATAL_ERROR;
  17374. }
  17375. if (ret == 0) {
  17376. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17377. }
  17378. if (ret == 0) {
  17379. ret = wc_InitRng(&rng);
  17380. }
  17381. if (ret == 0) {
  17382. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  17383. }
  17384. if (ret == 0) {
  17385. /* test getting size only */
  17386. ret = wc_RsaKeyToPublicDer(&key, NULL, derLen);
  17387. if (ret >= 0)
  17388. ret = 0;
  17389. }
  17390. if (ret == 0) {
  17391. ret = wc_RsaKeyToPublicDer(&key, der, derLen);
  17392. if (ret >= 0) {
  17393. ret = 0;
  17394. }
  17395. else {
  17396. ret = WOLFSSL_FATAL_ERROR;
  17397. }
  17398. }
  17399. if (ret == 0) {
  17400. /* test getting size only */
  17401. ret = wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0);
  17402. if (ret >= 0)
  17403. ret = 0;
  17404. }
  17405. if (ret == 0) {
  17406. ret = wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0);
  17407. if (ret >= 0) {
  17408. ret = 0;
  17409. }
  17410. else {
  17411. ret = WOLFSSL_FATAL_ERROR;
  17412. }
  17413. }
  17414. #ifndef HAVE_USER_RSA
  17415. /* Pass in bad args. */
  17416. if (ret == 0) {
  17417. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  17418. if (ret == BAD_FUNC_ARG) {
  17419. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  17420. }
  17421. if (ret == BUFFER_E || ret == BAD_FUNC_ARG) {
  17422. ret = 0;
  17423. }
  17424. else {
  17425. ret = WOLFSSL_FATAL_ERROR;
  17426. }
  17427. }
  17428. #else
  17429. /* Pass in bad args. */
  17430. if (ret == 0) {
  17431. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  17432. if (ret == USER_CRYPTO_ERROR) {
  17433. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  17434. }
  17435. if (ret == USER_CRYPTO_ERROR) {
  17436. ret = 0;
  17437. }
  17438. else {
  17439. ret = WOLFSSL_FATAL_ERROR;
  17440. }
  17441. }
  17442. #endif
  17443. if (der != NULL) {
  17444. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17445. }
  17446. if (wc_FreeRsaKey(&key) || ret != 0) {
  17447. ret = WOLFSSL_FATAL_ERROR;
  17448. }
  17449. if (wc_FreeRng(&rng) || ret != 0) {
  17450. ret = WOLFSSL_FATAL_ERROR;
  17451. }
  17452. res = TEST_RES_CHECK(ret == 0);
  17453. #endif
  17454. return res;
  17455. } /* END test_wc_RsaKeyToPublicDer */
  17456. /*
  17457. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  17458. */
  17459. static int test_wc_RsaPublicEncryptDecrypt(void)
  17460. {
  17461. int res = TEST_SKIPPED;
  17462. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17463. RsaKey key;
  17464. WC_RNG rng;
  17465. int ret = 0;
  17466. const char inStr[] = TEST_STRING;
  17467. const word32 plainLen = (word32)TEST_STRING_SZ;
  17468. const word32 inLen = (word32)TEST_STRING_SZ;
  17469. int bits = TEST_RSA_BITS;
  17470. const word32 cipherLen = TEST_RSA_BYTES;
  17471. word32 cipherLenResult = cipherLen;
  17472. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17473. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17474. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17475. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17476. if (in == NULL || plain == NULL || cipher == NULL) {
  17477. fprintf(stderr, "test_wc_RsaPublicEncryptDecrypt malloc failed\n");
  17478. return MEMORY_E;
  17479. }
  17480. #endif
  17481. XMEMCPY(in, inStr, inLen);
  17482. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17483. if (ret == 0) {
  17484. ret = wc_InitRng(&rng);
  17485. }
  17486. if (ret == 0) {
  17487. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  17488. }
  17489. /* Encrypt. */
  17490. if (ret == 0) {
  17491. ret = wc_RsaPublicEncrypt(in, inLen, cipher, cipherLen, &key, &rng);
  17492. if (ret >= 0) {
  17493. cipherLenResult = ret;
  17494. ret = 0;
  17495. }
  17496. else {
  17497. ret = WOLFSSL_FATAL_ERROR;
  17498. }
  17499. }
  17500. /* Pass bad args. */
  17501. /* Tests PsaPublicEncryptEx() which, is tested by another fn. No need dup.*/
  17502. if (ret != 0) {
  17503. return TEST_FAIL;
  17504. }
  17505. /* Decrypt */
  17506. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17507. /* Bind rng */
  17508. if (ret == 0) {
  17509. ret = wc_RsaSetRNG(&key, &rng);
  17510. }
  17511. #endif
  17512. if (ret == 0) {
  17513. ret = wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen, &key);
  17514. }
  17515. if (ret >= 0) {
  17516. ret = XMEMCMP(plain, inStr, plainLen);
  17517. }
  17518. /* Pass in bad args. */
  17519. /* Tests RsaPrivateDecryptEx() which, is tested by another fn. No need dup.*/
  17520. WC_FREE_VAR(in, NULL);
  17521. WC_FREE_VAR(plain, NULL);
  17522. WC_FREE_VAR(cipher, NULL);
  17523. if (wc_FreeRsaKey(&key) || ret != 0) {
  17524. ret = WOLFSSL_FATAL_ERROR;
  17525. }
  17526. if (wc_FreeRng(&rng) || ret != 0) {
  17527. ret = WOLFSSL_FATAL_ERROR;
  17528. }
  17529. res = TEST_RES_CHECK(ret == 0);
  17530. #endif
  17531. return res;
  17532. } /* END test_wc_RsaPublicEncryptDecrypt */
  17533. /*
  17534. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  17535. */
  17536. static int test_wc_RsaPublicEncryptDecrypt_ex(void)
  17537. {
  17538. int result = TEST_SKIPPED;
  17539. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  17540. && !defined(WC_NO_RSA_OAEP) && !defined(HAVE_USER_RSA)\
  17541. && !defined(NO_SHA)
  17542. RsaKey key;
  17543. WC_RNG rng;
  17544. int ret;
  17545. const char inStr[] = TEST_STRING;
  17546. const word32 inLen = (word32)TEST_STRING_SZ;
  17547. const word32 plainSz = (word32)TEST_STRING_SZ;
  17548. byte* res = NULL;
  17549. int idx = 0;
  17550. int bits = TEST_RSA_BITS;
  17551. const word32 cipherSz = TEST_RSA_BYTES;
  17552. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17553. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17554. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17555. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17556. if (in == NULL || plain == NULL || cipher == NULL) {
  17557. fprintf(stderr, "test_wc_RsaPublicEncryptDecrypt_exmalloc failed\n");
  17558. return TEST_FAIL;
  17559. }
  17560. #endif
  17561. XMEMCPY(in, inStr, inLen);
  17562. /* Initialize stack structures. */
  17563. XMEMSET(&rng, 0, sizeof(rng));
  17564. XMEMSET(&key, 0, sizeof(key));
  17565. ret = wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID);
  17566. if (ret == 0) {
  17567. ret = wc_InitRng(&rng);
  17568. }
  17569. if (ret == 0) {
  17570. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  17571. }
  17572. /* Encrypt */
  17573. if (ret == 0) {
  17574. ret = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key, &rng,
  17575. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  17576. if (ret >= 0) {
  17577. idx = ret;
  17578. ret = 0;
  17579. }
  17580. else {
  17581. ret = WOLFSSL_FATAL_ERROR;
  17582. }
  17583. }
  17584. /* Pass bad args. */
  17585. /* Tests RsaPublicEncryptEx again. No need duplicate. */
  17586. if (ret != 0) {
  17587. return TEST_FAIL;
  17588. }
  17589. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17590. /* Decrypt */
  17591. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17592. if (ret == 0) {
  17593. ret = wc_RsaSetRNG(&key, &rng);
  17594. }
  17595. #endif
  17596. if (ret == 0) {
  17597. ret = wc_RsaPrivateDecrypt_ex(cipher, (word32)idx,
  17598. plain, plainSz, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA,
  17599. WC_MGF1SHA1, NULL, 0);
  17600. }
  17601. if (ret >= 0) {
  17602. if (!XMEMCMP(plain, inStr, plainSz)) {
  17603. ret = 0;
  17604. }
  17605. else {
  17606. ret = WOLFSSL_FATAL_ERROR;
  17607. }
  17608. }
  17609. /*Pass bad args.*/
  17610. /* Tests RsaPrivateDecryptEx() again. No need duplicate. */
  17611. if (ret != 0) {
  17612. return TEST_FAIL;
  17613. }
  17614. if (ret == 0) {
  17615. ret = wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx,
  17616. &res, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA,
  17617. WC_MGF1SHA1, NULL, 0);
  17618. if (ret >= 0) {
  17619. if (!XMEMCMP(inStr, res, plainSz)) {
  17620. ret = 0;
  17621. }
  17622. else {
  17623. ret = WOLFSSL_FATAL_ERROR;
  17624. }
  17625. }
  17626. }
  17627. #endif
  17628. WC_FREE_VAR(in, NULL);
  17629. WC_FREE_VAR(plain, NULL);
  17630. WC_FREE_VAR(cipher, NULL);
  17631. if (wc_FreeRsaKey(&key) || ret != 0) {
  17632. ret = WOLFSSL_FATAL_ERROR;
  17633. }
  17634. if (wc_FreeRng(&rng) || ret != 0) {
  17635. ret = WOLFSSL_FATAL_ERROR;
  17636. }
  17637. result = TEST_RES_CHECK(ret == 0);
  17638. #endif
  17639. return result;
  17640. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  17641. /*
  17642. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  17643. */
  17644. static int test_wc_RsaSSL_SignVerify(void)
  17645. {
  17646. int res = TEST_SKIPPED;
  17647. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17648. RsaKey key;
  17649. WC_RNG rng;
  17650. int ret = 0;
  17651. const char inStr[] = TEST_STRING;
  17652. const word32 plainSz = (word32)TEST_STRING_SZ;
  17653. const word32 inLen = (word32)TEST_STRING_SZ;
  17654. word32 idx = 0;
  17655. int bits = TEST_RSA_BITS;
  17656. const word32 outSz = TEST_RSA_BYTES;
  17657. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17658. WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  17659. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17660. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17661. if (in == NULL || out == NULL || plain == NULL) {
  17662. fprintf(stderr, "test_wc_RsaSSL_SignVerify failed\n");
  17663. return TEST_FAIL;
  17664. }
  17665. #endif
  17666. XMEMCPY(in, inStr, inLen);
  17667. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17668. if (ret == 0) {
  17669. ret = wc_InitRng(&rng);
  17670. }
  17671. if (ret == 0) {
  17672. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  17673. }
  17674. /* Sign. */
  17675. if (ret == 0) {
  17676. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng);
  17677. if (ret == (int)outSz) {
  17678. idx = ret;
  17679. ret = 0;
  17680. }
  17681. else {
  17682. ret = WOLFSSL_FATAL_ERROR;
  17683. }
  17684. }
  17685. #ifndef HAVE_USER_RSA
  17686. /* Test bad args. */
  17687. if (ret == 0) {
  17688. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  17689. if (ret == BAD_FUNC_ARG) {
  17690. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  17691. }
  17692. if (ret == BAD_FUNC_ARG) {
  17693. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  17694. }
  17695. if (ret == BAD_FUNC_ARG) {
  17696. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  17697. }
  17698. if (ret == BAD_FUNC_ARG) {
  17699. ret = 0;
  17700. }
  17701. else {
  17702. ret = WOLFSSL_FATAL_ERROR;
  17703. }
  17704. }
  17705. #else
  17706. /* Test bad args. */
  17707. if (ret == 0) {
  17708. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  17709. if (ret == USER_CRYPTO_ERROR) {
  17710. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  17711. }
  17712. if (ret == USER_CRYPTO_ERROR) {
  17713. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  17714. }
  17715. if (ret == USER_CRYPTO_ERROR) {
  17716. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  17717. }
  17718. if (ret == USER_CRYPTO_ERROR) {
  17719. ret = 0;
  17720. }
  17721. else {
  17722. ret = WOLFSSL_FATAL_ERROR;
  17723. }
  17724. }
  17725. #endif
  17726. if (ret != 0) {
  17727. return TEST_FAIL;
  17728. }
  17729. /* Verify. */
  17730. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, &key);
  17731. if (ret == (int)inLen) {
  17732. ret = 0;
  17733. }
  17734. else {
  17735. ret = WOLFSSL_FATAL_ERROR;
  17736. }
  17737. #ifndef HAVE_USER_RSA
  17738. /* Pass bad args. */
  17739. if (ret == 0) {
  17740. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  17741. if (ret == BAD_FUNC_ARG) {
  17742. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  17743. }
  17744. if (ret == BAD_FUNC_ARG) {
  17745. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  17746. }
  17747. if (ret == BAD_FUNC_ARG) {
  17748. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  17749. }
  17750. if (ret == BAD_FUNC_ARG) {
  17751. ret = 0;
  17752. }
  17753. else {
  17754. ret = WOLFSSL_FATAL_ERROR;
  17755. }
  17756. }
  17757. #else
  17758. /* Pass bad args. */
  17759. if (ret == 0) {
  17760. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  17761. if (ret == USER_CRYPTO_ERROR) {
  17762. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  17763. }
  17764. if (ret == USER_CRYPTO_ERROR) {
  17765. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  17766. }
  17767. if (ret == USER_CRYPTO_ERROR) {
  17768. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  17769. }
  17770. if (ret == USER_CRYPTO_ERROR) {
  17771. ret = 0;
  17772. }
  17773. else {
  17774. ret = WOLFSSL_FATAL_ERROR;
  17775. }
  17776. }
  17777. #endif
  17778. WC_FREE_VAR(in, NULL);
  17779. WC_FREE_VAR(out, NULL);
  17780. WC_FREE_VAR(plain, NULL);
  17781. if (wc_FreeRsaKey(&key) || ret != 0) {
  17782. ret = WOLFSSL_FATAL_ERROR;
  17783. }
  17784. if (wc_FreeRng(&rng) || ret != 0) {
  17785. ret = WOLFSSL_FATAL_ERROR;
  17786. }
  17787. res = TEST_RES_CHECK(ret == 0);
  17788. #endif
  17789. return res;
  17790. } /* END test_wc_RsaSSL_SignVerify */
  17791. /*
  17792. * Testing wc_RsaEncryptSize()
  17793. */
  17794. static int test_wc_RsaEncryptSize(void)
  17795. {
  17796. int res = TEST_SKIPPED;
  17797. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17798. RsaKey key;
  17799. WC_RNG rng;
  17800. int ret;
  17801. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17802. if (ret == 0) {
  17803. ret = wc_InitRng(&rng);
  17804. }
  17805. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17806. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17807. if (ret == 0) {
  17808. ret = MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng);
  17809. if (ret == 0) {
  17810. ret = wc_RsaEncryptSize(&key);
  17811. }
  17812. if (ret == 128) {
  17813. ret = 0;
  17814. }
  17815. else {
  17816. ret = WOLFSSL_FATAL_ERROR;
  17817. }
  17818. }
  17819. if (wc_FreeRsaKey(&key) || ret != 0) {
  17820. ret = WOLFSSL_FATAL_ERROR;
  17821. }
  17822. else {
  17823. ret = 0;
  17824. }
  17825. #endif
  17826. if (ret == 0) {
  17827. ret = MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng);
  17828. if (ret == 0) {
  17829. ret = wc_RsaEncryptSize(&key);
  17830. }
  17831. if (ret == 256) {
  17832. ret = 0;
  17833. }
  17834. else {
  17835. ret = WOLFSSL_FATAL_ERROR;
  17836. }
  17837. }
  17838. /* Pass in bad arg. */
  17839. if (ret == 0) {
  17840. ret = wc_RsaEncryptSize(NULL);
  17841. #ifndef HAVE_USER_RSA
  17842. if (ret == BAD_FUNC_ARG) {
  17843. ret = 0;
  17844. }
  17845. else {
  17846. ret = WOLFSSL_FATAL_ERROR;
  17847. }
  17848. #endif
  17849. }
  17850. if (wc_FreeRsaKey(&key) || ret != 0) {
  17851. ret = WOLFSSL_FATAL_ERROR;
  17852. }
  17853. if (wc_FreeRng(&rng) || ret != 0) {
  17854. ret = WOLFSSL_FATAL_ERROR;
  17855. }
  17856. res = TEST_RES_CHECK(ret == 0);
  17857. #endif
  17858. return res;
  17859. } /* END test_wc_RsaEncryptSize*/
  17860. /*
  17861. * Testing wc_RsaFlattenPublicKey()
  17862. */
  17863. static int test_wc_RsaFlattenPublicKey(void)
  17864. {
  17865. int res = TEST_SKIPPED;
  17866. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17867. RsaKey key;
  17868. WC_RNG rng;
  17869. int ret = 0;
  17870. byte e[256];
  17871. byte n[256];
  17872. word32 eSz = sizeof(e);
  17873. word32 nSz = sizeof(n);
  17874. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17875. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17876. int bits = 1024;
  17877. #else
  17878. int bits = 2048;
  17879. #endif
  17880. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17881. if (ret == 0) {
  17882. ret = wc_InitRng(&rng);
  17883. }
  17884. if (ret == 0) {
  17885. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  17886. if (ret >= 0) {
  17887. ret = 0;
  17888. }
  17889. else {
  17890. ret = WOLFSSL_FATAL_ERROR;
  17891. }
  17892. }
  17893. if (ret == 0) {
  17894. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz);
  17895. }
  17896. #ifndef HAVE_USER_RSA
  17897. /* Pass bad args. */
  17898. if (ret == 0) {
  17899. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  17900. if (ret == BAD_FUNC_ARG) {
  17901. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  17902. }
  17903. if (ret == BAD_FUNC_ARG) {
  17904. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  17905. }
  17906. if (ret == BAD_FUNC_ARG) {
  17907. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  17908. }
  17909. if (ret == BAD_FUNC_ARG) {
  17910. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  17911. }
  17912. if (ret == BAD_FUNC_ARG) {
  17913. ret = 0;
  17914. }
  17915. else {
  17916. ret = WOLFSSL_FATAL_ERROR;
  17917. }
  17918. }
  17919. #else
  17920. /* Pass bad args. */
  17921. if (ret == 0) {
  17922. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  17923. if (ret == USER_CRYPTO_ERROR) {
  17924. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  17925. }
  17926. if (ret == USER_CRYPTO_ERROR) {
  17927. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  17928. }
  17929. if (ret == USER_CRYPTO_ERROR) {
  17930. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  17931. }
  17932. if (ret == USER_CRYPTO_ERROR) {
  17933. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  17934. }
  17935. if (ret == USER_CRYPTO_ERROR) {
  17936. ret = 0;
  17937. }
  17938. else {
  17939. ret = WOLFSSL_FATAL_ERROR;
  17940. }
  17941. }
  17942. #endif
  17943. if (wc_FreeRsaKey(&key) || ret != 0) {
  17944. ret = WOLFSSL_FATAL_ERROR;
  17945. }
  17946. if (wc_FreeRng(&rng) || ret != 0) {
  17947. ret = WOLFSSL_FATAL_ERROR;
  17948. }
  17949. res = TEST_RES_CHECK(ret == 0);
  17950. #endif
  17951. return res;
  17952. } /* END test_wc_RsaFlattenPublicKey */
  17953. /*
  17954. * unit test for wc_AesCcmSetKey
  17955. */
  17956. static int test_wc_AesCcmSetKey(void)
  17957. {
  17958. int res = TEST_SKIPPED;
  17959. #ifdef HAVE_AESCCM
  17960. Aes aes;
  17961. int ret = 0;
  17962. const byte key16[] =
  17963. {
  17964. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17965. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17966. };
  17967. const byte key24[] =
  17968. {
  17969. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17970. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17971. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  17972. };
  17973. const byte key32[] =
  17974. {
  17975. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17976. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17977. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17978. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  17979. };
  17980. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  17981. if (ret != 0)
  17982. return ret;
  17983. #ifdef WOLFSSL_AES_128
  17984. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  17985. #endif
  17986. #ifdef WOLFSSL_AES_192
  17987. if (ret == 0) {
  17988. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24));
  17989. }
  17990. #endif
  17991. #ifdef WOLFSSL_AES_256
  17992. if (ret == 0) {
  17993. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32));
  17994. }
  17995. #endif
  17996. /* Test bad args. */
  17997. if (ret == 0) {
  17998. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1);
  17999. if (ret == BAD_FUNC_ARG) {
  18000. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1);
  18001. }
  18002. if (ret == BAD_FUNC_ARG) {
  18003. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1);
  18004. }
  18005. if (ret != BAD_FUNC_ARG) {
  18006. ret = WOLFSSL_FATAL_ERROR;
  18007. }
  18008. else {
  18009. ret = 0;
  18010. }
  18011. }
  18012. wc_AesFree(&aes);
  18013. res = TEST_RES_CHECK(ret == 0);
  18014. #endif
  18015. return res;
  18016. } /* END test_wc_AesCcmSetKey */
  18017. /*
  18018. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  18019. */
  18020. static int test_wc_AesCcmEncryptDecrypt(void)
  18021. {
  18022. int res = TEST_SKIPPED;
  18023. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  18024. Aes aes;
  18025. int ret = 0;
  18026. const byte key16[] =
  18027. {
  18028. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  18029. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  18030. };
  18031. /* plaintext */
  18032. const byte plainT[] =
  18033. {
  18034. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  18035. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18036. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  18037. };
  18038. /* nonce */
  18039. const byte iv[] =
  18040. {
  18041. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  18042. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  18043. };
  18044. const byte c[] = /* cipher text. */
  18045. {
  18046. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  18047. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  18048. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  18049. };
  18050. const byte t[] = /* Auth tag */
  18051. {
  18052. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  18053. };
  18054. const byte authIn[] =
  18055. {
  18056. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  18057. };
  18058. byte cipherOut[sizeof(plainT)];
  18059. byte authTag[sizeof(t)];
  18060. int ccmE = WOLFSSL_FATAL_ERROR;
  18061. #ifdef HAVE_AES_DECRYPT
  18062. int ccmD = WOLFSSL_FATAL_ERROR;
  18063. byte plainOut[sizeof(cipherOut)];
  18064. #endif
  18065. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  18066. if (ret != 0)
  18067. return ret;
  18068. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  18069. if (ret == 0) {
  18070. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18071. iv, sizeof(iv), authTag, sizeof(authTag),
  18072. authIn , sizeof(authIn));
  18073. if ((XMEMCMP(cipherOut, c, sizeof(c)) && ccmE == 0) ||
  18074. XMEMCMP(t, authTag, sizeof(t))) {
  18075. ccmE = WOLFSSL_FATAL_ERROR;
  18076. ret = WOLFSSL_FATAL_ERROR;
  18077. }
  18078. #ifdef HAVE_AES_DECRYPT
  18079. if (ret == 0) {
  18080. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  18081. sizeof(plainOut), iv, sizeof(iv),
  18082. authTag, sizeof(authTag),
  18083. authIn, sizeof(authIn));
  18084. if (XMEMCMP(plainOut, plainT, sizeof(plainT)) && ccmD == 0) {
  18085. ccmD = WOLFSSL_FATAL_ERROR;
  18086. }
  18087. }
  18088. #endif
  18089. }
  18090. /* Pass in bad args. Encrypt*/
  18091. if (ret == 0 && ccmE == 0) {
  18092. ccmE = wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  18093. iv, sizeof(iv), authTag, sizeof(authTag),
  18094. authIn , sizeof(authIn));
  18095. if (ccmE == BAD_FUNC_ARG) {
  18096. ccmE = wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  18097. iv, sizeof(iv), authTag, sizeof(authTag),
  18098. authIn , sizeof(authIn));
  18099. }
  18100. if (ccmE == BAD_FUNC_ARG) {
  18101. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  18102. iv, sizeof(iv), authTag, sizeof(authTag),
  18103. authIn , sizeof(authIn));
  18104. }
  18105. if (ccmE == BAD_FUNC_ARG) {
  18106. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18107. NULL, sizeof(iv), authTag, sizeof(authTag),
  18108. authIn , sizeof(authIn));
  18109. }
  18110. if (ccmE == BAD_FUNC_ARG) {
  18111. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18112. iv, sizeof(iv), NULL, sizeof(authTag),
  18113. authIn , sizeof(authIn));
  18114. }
  18115. if (ccmE == BAD_FUNC_ARG) {
  18116. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18117. iv, sizeof(iv) + 1, authTag, sizeof(authTag),
  18118. authIn , sizeof(authIn));
  18119. }
  18120. if (ccmE == BAD_FUNC_ARG) {
  18121. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18122. iv, sizeof(iv) - 7, authTag, sizeof(authTag),
  18123. authIn , sizeof(authIn));
  18124. }
  18125. if (ccmE != BAD_FUNC_ARG) {
  18126. ccmE = WOLFSSL_FATAL_ERROR;
  18127. }
  18128. else {
  18129. ccmE = 0;
  18130. }
  18131. } /* End Encrypt */
  18132. if (ccmE != 0) {
  18133. wc_AesFree(&aes);
  18134. return TEST_FAIL;
  18135. }
  18136. #ifdef HAVE_AES_DECRYPT
  18137. /* Pass in bad args. Decrypt*/
  18138. if (ret == 0 && ccmD == 0) {
  18139. ccmD = wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  18140. iv, sizeof(iv), authTag, sizeof(authTag),
  18141. authIn, sizeof(authIn));
  18142. if (ccmD == BAD_FUNC_ARG) {
  18143. ccmD = wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  18144. iv, sizeof(iv), authTag, sizeof(authTag),
  18145. authIn, sizeof(authIn));
  18146. }
  18147. if (ccmD == BAD_FUNC_ARG) {
  18148. ccmD = wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  18149. iv, sizeof(iv), authTag, sizeof(authTag),
  18150. authIn, sizeof(authIn));
  18151. }
  18152. if (ccmD == BAD_FUNC_ARG) {
  18153. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  18154. sizeof(plainOut), NULL, sizeof(iv),
  18155. authTag, sizeof(authTag),
  18156. authIn, sizeof(authIn));
  18157. }
  18158. if (ccmD == BAD_FUNC_ARG) {
  18159. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  18160. sizeof(plainOut), iv, sizeof(iv), NULL,
  18161. sizeof(authTag), authIn, sizeof(authIn));
  18162. }
  18163. if (ccmD == BAD_FUNC_ARG) {
  18164. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  18165. sizeof(plainOut), iv, sizeof(iv) + 1,
  18166. authTag, sizeof(authTag),
  18167. authIn, sizeof(authIn));
  18168. }
  18169. if (ccmD == BAD_FUNC_ARG) {
  18170. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  18171. sizeof(plainOut), iv, sizeof(iv) - 7,
  18172. authTag, sizeof(authTag),
  18173. authIn, sizeof(authIn));
  18174. }
  18175. if (ccmD != BAD_FUNC_ARG) {
  18176. ccmD = WOLFSSL_FATAL_ERROR;
  18177. }
  18178. else {
  18179. ccmD = 0;
  18180. }
  18181. } /* END Decrypt */
  18182. res = TEST_RES_CHECK(ccmD == 0);
  18183. #endif
  18184. wc_AesFree(&aes);
  18185. #endif /* HAVE_AESCCM */
  18186. return res;
  18187. } /* END test_wc_AesCcmEncryptDecrypt */
  18188. /*
  18189. * Testing wc_InitDsaKey()
  18190. */
  18191. static int test_wc_InitDsaKey(void)
  18192. {
  18193. int res = TEST_SKIPPED;
  18194. #ifndef NO_DSA
  18195. DsaKey key;
  18196. int ret = 0;
  18197. ret = wc_InitDsaKey(&key);
  18198. /* Pass in bad args. */
  18199. if (ret == 0) {
  18200. ret = wc_InitDsaKey(NULL);
  18201. if (ret == BAD_FUNC_ARG) {
  18202. ret = 0;
  18203. }
  18204. else {
  18205. ret = WOLFSSL_FATAL_ERROR;
  18206. }
  18207. }
  18208. wc_FreeDsaKey(&key);
  18209. res = TEST_RES_CHECK(ret == 0);
  18210. #endif
  18211. return res;
  18212. } /* END test_wc_InitDsaKey */
  18213. /*
  18214. * Testing wc_DsaSign() and wc_DsaVerify()
  18215. */
  18216. static int test_wc_DsaSignVerify(void)
  18217. {
  18218. int res = TEST_SKIPPED;
  18219. #if !defined(NO_DSA)
  18220. DsaKey key;
  18221. WC_RNG rng;
  18222. wc_Sha sha;
  18223. int ret = 0;
  18224. byte signature[DSA_SIG_SIZE];
  18225. byte hash[WC_SHA_DIGEST_SIZE];
  18226. word32 idx = 0;
  18227. word32 bytes;
  18228. int answer;
  18229. #ifdef USE_CERT_BUFFERS_1024
  18230. byte tmp[ONEK_BUF];
  18231. XMEMSET(tmp, 0, sizeof(tmp));
  18232. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18233. bytes = sizeof_dsa_key_der_1024;
  18234. #elif defined(USE_CERT_BUFFERS_2048)
  18235. byte tmp[TWOK_BUF];
  18236. XMEMSET(tmp, 0, sizeof(tmp));
  18237. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18238. bytes = sizeof_dsa_key_der_2048;
  18239. #else
  18240. byte tmp[TWOK_BUF];
  18241. XMEMSET(tmp, 0, sizeof(tmp));
  18242. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  18243. if (fp == XBADFILE) {
  18244. return WOLFSSL_BAD_FILE;
  18245. }
  18246. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  18247. XFCLOSE(fp);
  18248. #endif /* END USE_CERT_BUFFERS_1024 */
  18249. ret = wc_InitSha(&sha);
  18250. if (ret == 0) {
  18251. ret = wc_ShaUpdate(&sha, tmp, bytes);
  18252. if (ret == 0) {
  18253. ret = wc_ShaFinal(&sha, hash);
  18254. }
  18255. if (ret == 0) {
  18256. ret = wc_InitDsaKey(&key);
  18257. }
  18258. if (ret == 0) {
  18259. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  18260. }
  18261. if (ret == 0) {
  18262. ret = wc_InitRng(&rng);
  18263. }
  18264. }
  18265. /* Sign. */
  18266. if (ret == 0) {
  18267. ret = wc_DsaSign(hash, signature, &key, &rng);
  18268. }
  18269. /* Test bad args. */
  18270. if (ret == 0) {
  18271. ret = wc_DsaSign(NULL, signature, &key, &rng);
  18272. if (ret == BAD_FUNC_ARG) {
  18273. ret = wc_DsaSign(hash, NULL, &key, &rng);
  18274. }
  18275. if (ret == BAD_FUNC_ARG) {
  18276. ret = wc_DsaSign(hash, signature, NULL, &rng);
  18277. }
  18278. if (ret == BAD_FUNC_ARG) {
  18279. ret = wc_DsaSign(hash, signature, &key, NULL);
  18280. }
  18281. if (ret == BAD_FUNC_ARG) {
  18282. ret = 0;
  18283. }
  18284. else {
  18285. ret = WOLFSSL_FATAL_ERROR;
  18286. }
  18287. }
  18288. if (ret == 0) {
  18289. /* Verify. */
  18290. ret = wc_DsaVerify(hash, signature, &key, &answer);
  18291. if (ret != 0 || answer != 1) {
  18292. ret = WOLFSSL_FATAL_ERROR;
  18293. }
  18294. else {
  18295. ret = 0;
  18296. }
  18297. }
  18298. /* Pass in bad args. */
  18299. if (ret == 0) {
  18300. ret = wc_DsaVerify(NULL, signature, &key, &answer);
  18301. if (ret == BAD_FUNC_ARG) {
  18302. ret = wc_DsaVerify(hash, NULL, &key, &answer);
  18303. }
  18304. if (ret == BAD_FUNC_ARG) {
  18305. ret = wc_DsaVerify(hash, signature, NULL, &answer);
  18306. }
  18307. if (ret == BAD_FUNC_ARG) {
  18308. ret = wc_DsaVerify(hash, signature, &key, NULL);
  18309. }
  18310. if (ret == BAD_FUNC_ARG) {
  18311. ret = 0;
  18312. }
  18313. else {
  18314. ret = WOLFSSL_FATAL_ERROR;
  18315. }
  18316. }
  18317. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  18318. /* hard set q to 0 and test fail case */
  18319. mp_free(&key.q);
  18320. mp_init(&key.q);
  18321. AssertIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  18322. mp_set(&key.q, 1);
  18323. AssertIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  18324. #endif
  18325. if (wc_FreeRng(&rng) && ret == 0) {
  18326. ret = WOLFSSL_FATAL_ERROR;
  18327. }
  18328. wc_FreeDsaKey(&key);
  18329. wc_ShaFree(&sha);
  18330. res = TEST_RES_CHECK(ret == 0);
  18331. #endif
  18332. return res;
  18333. } /* END test_wc_DsaSign */
  18334. /*
  18335. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  18336. */
  18337. static int test_wc_DsaPublicPrivateKeyDecode(void)
  18338. {
  18339. int res = TEST_SKIPPED;
  18340. #if !defined(NO_DSA)
  18341. DsaKey key;
  18342. word32 bytes;
  18343. word32 idx = 0;
  18344. int priv = 0;
  18345. int pub = 0;
  18346. int ret = 0;
  18347. #ifdef USE_CERT_BUFFERS_1024
  18348. byte tmp[ONEK_BUF];
  18349. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18350. bytes = sizeof_dsa_key_der_1024;
  18351. #elif defined(USE_CERT_BUFFERS_2048)
  18352. byte tmp[TWOK_BUF];
  18353. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18354. bytes = sizeof_dsa_key_der_2048;
  18355. #else
  18356. byte tmp[TWOK_BUF];
  18357. XMEMSET(tmp, 0, sizeof(tmp));
  18358. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  18359. if (fp == XBADFILE)
  18360. {
  18361. return WOLFSSL_BAD_FILE;
  18362. }
  18363. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  18364. XFCLOSE(fp);
  18365. #endif /* END USE_CERT_BUFFERS_1024 */
  18366. ret = wc_InitDsaKey(&key);
  18367. if (ret == 0) {
  18368. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  18369. /* Test bad args. */
  18370. if (priv == 0) {
  18371. priv = wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes);
  18372. if (priv == BAD_FUNC_ARG) {
  18373. priv = wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes);
  18374. }
  18375. if (priv == BAD_FUNC_ARG) {
  18376. priv = wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes);
  18377. }
  18378. if (priv == BAD_FUNC_ARG) {
  18379. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  18380. }
  18381. if (priv == ASN_PARSE_E || priv == BUFFER_E) {
  18382. priv = 0;
  18383. }
  18384. else {
  18385. priv = WOLFSSL_FATAL_ERROR;
  18386. }
  18387. }
  18388. wc_FreeDsaKey(&key);
  18389. ret = wc_InitDsaKey(&key);
  18390. }
  18391. if (ret == 0) {
  18392. idx = 0; /* Reset */
  18393. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  18394. /* Test bad args. */
  18395. if (pub == 0) {
  18396. pub = wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes);
  18397. if (pub == BAD_FUNC_ARG) {
  18398. pub = wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes);
  18399. }
  18400. if (pub == BAD_FUNC_ARG) {
  18401. pub = wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes);
  18402. }
  18403. if (pub == BAD_FUNC_ARG) {
  18404. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  18405. }
  18406. if (pub == ASN_PARSE_E || pub == BUFFER_E) {
  18407. pub = 0;
  18408. }
  18409. else {
  18410. pub = WOLFSSL_FATAL_ERROR;
  18411. }
  18412. }
  18413. } /* END Public Key */
  18414. wc_FreeDsaKey(&key);
  18415. res = TEST_RES_CHECK(ret == 0 && pub == 0 && priv == 0);
  18416. #endif /* !NO_DSA */
  18417. return res;
  18418. } /* END test_wc_DsaPublicPrivateKeyDecode */
  18419. /*
  18420. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  18421. */
  18422. static int test_wc_MakeDsaKey(void)
  18423. {
  18424. int res = TEST_SKIPPED;
  18425. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18426. DsaKey genKey;
  18427. WC_RNG rng;
  18428. int ret = 0;
  18429. XMEMSET(&rng, 0, sizeof(rng));
  18430. XMEMSET(&genKey, 0, sizeof(genKey));
  18431. ret = wc_InitRng(&rng);
  18432. if (ret == 0) {
  18433. ret = wc_InitDsaKey(&genKey);
  18434. }
  18435. if (ret == 0) {
  18436. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  18437. }
  18438. /* Test bad args. */
  18439. if (ret == 0) {
  18440. ret = wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey);
  18441. if (ret == BAD_FUNC_ARG) {
  18442. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, NULL);
  18443. }
  18444. if (ret == BAD_FUNC_ARG) {
  18445. ret = wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey);
  18446. }
  18447. if (ret == BAD_FUNC_ARG) {
  18448. ret = 0;
  18449. }
  18450. else {
  18451. ret = WOLFSSL_FATAL_ERROR;
  18452. }
  18453. }
  18454. if (ret == 0) {
  18455. ret = wc_MakeDsaKey(&rng, &genKey);
  18456. }
  18457. /* Test bad args. */
  18458. if (ret == 0) {
  18459. ret = wc_MakeDsaKey(NULL, &genKey);
  18460. if (ret == BAD_FUNC_ARG) {
  18461. ret = wc_MakeDsaKey(&rng, NULL);
  18462. }
  18463. if (ret == BAD_FUNC_ARG) {
  18464. ret = 0;
  18465. }
  18466. else {
  18467. ret = WOLFSSL_FATAL_ERROR;
  18468. }
  18469. }
  18470. if (wc_FreeRng(&rng) && ret == 0) {
  18471. ret = WOLFSSL_FAILURE;
  18472. }
  18473. wc_FreeDsaKey(&genKey);
  18474. res = TEST_RES_CHECK(ret == 0);
  18475. #endif
  18476. return res;
  18477. } /* END test_wc_MakeDsaKey */
  18478. /*
  18479. * Testing wc_DsaKeyToDer()
  18480. */
  18481. static int test_wc_DsaKeyToDer(void)
  18482. {
  18483. int res = TEST_SKIPPED;
  18484. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18485. DsaKey genKey;
  18486. WC_RNG rng;
  18487. word32 bytes;
  18488. word32 idx = 0;
  18489. int ret = 0;
  18490. #ifdef USE_CERT_BUFFERS_1024
  18491. byte tmp[ONEK_BUF];
  18492. byte der[ONEK_BUF];
  18493. XMEMSET(tmp, 0, sizeof(tmp));
  18494. XMEMSET(der, 0, sizeof(der));
  18495. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18496. bytes = sizeof_dsa_key_der_1024;
  18497. #elif defined(USE_CERT_BUFFERS_2048)
  18498. byte tmp[TWOK_BUF];
  18499. byte der[TWOK_BUF];
  18500. XMEMSET(tmp, 0, sizeof(tmp));
  18501. XMEMSET(der, 0, sizeof(der));
  18502. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18503. bytes = sizeof_dsa_key_der_2048;
  18504. #else
  18505. byte tmp[TWOK_BUF];
  18506. byte der[TWOK_BUF];
  18507. XMEMSET(tmp, 0, sizeof(tmp));
  18508. XMEMSET(der, 0, sizeof(der));
  18509. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  18510. if (fp == XBADFILE) {
  18511. return WOLFSSL_BAD_FILE;
  18512. }
  18513. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  18514. XFCLOSE(fp);
  18515. #endif /* END USE_CERT_BUFFERS_1024 */
  18516. XMEMSET(&rng, 0, sizeof(rng));
  18517. XMEMSET(&genKey, 0, sizeof(genKey));
  18518. ret = wc_InitRng(&rng);
  18519. if (ret == 0) {
  18520. ret = wc_InitDsaKey(&genKey);
  18521. }
  18522. if (ret == 0) {
  18523. ret = wc_MakeDsaParameters(&rng, sizeof(tmp), &genKey);
  18524. if (ret == 0) {
  18525. wc_FreeDsaKey(&genKey);
  18526. ret = wc_InitDsaKey(&genKey);
  18527. }
  18528. }
  18529. if (ret == 0) {
  18530. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &genKey, bytes);
  18531. }
  18532. if (ret == 0) {
  18533. ret = wc_DsaKeyToDer(&genKey, der, bytes);
  18534. if ( ret >= 0 && ( ret = XMEMCMP(der, tmp, bytes) ) == 0 ) {
  18535. ret = 0;
  18536. }
  18537. }
  18538. /* Test bad args. */
  18539. if (ret == 0) {
  18540. ret = wc_DsaKeyToDer(NULL, der, FOURK_BUF);
  18541. if (ret == BAD_FUNC_ARG) {
  18542. ret = wc_DsaKeyToDer(&genKey, NULL, FOURK_BUF);
  18543. }
  18544. if (ret == BAD_FUNC_ARG) {
  18545. ret = 0;
  18546. }
  18547. else {
  18548. ret = WOLFSSL_FATAL_ERROR;
  18549. }
  18550. }
  18551. if (wc_FreeRng(&rng) && ret == 0) {
  18552. ret = WOLFSSL_FATAL_ERROR;
  18553. }
  18554. wc_FreeDsaKey(&genKey);
  18555. res = TEST_RES_CHECK(ret == 0);
  18556. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  18557. return res;
  18558. } /* END test_wc_DsaKeyToDer */
  18559. /*
  18560. * Testing wc_DsaKeyToPublicDer()
  18561. * (indirectly testing setDsaPublicKey())
  18562. */
  18563. static int test_wc_DsaKeyToPublicDer(void)
  18564. {
  18565. int res = TEST_SKIPPED;
  18566. #ifndef HAVE_SELFTEST
  18567. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18568. DsaKey genKey;
  18569. WC_RNG rng;
  18570. byte* der;
  18571. word32 sz;
  18572. int ret = 0;
  18573. der = (byte*)XMALLOC(ONEK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  18574. if (der == NULL) {
  18575. ret = WOLFSSL_FATAL_ERROR;
  18576. }
  18577. if (ret == 0) {
  18578. ret = wc_InitDsaKey(&genKey);
  18579. }
  18580. if (ret == 0) {
  18581. ret = wc_InitRng(&rng);
  18582. }
  18583. if (ret == 0) {
  18584. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  18585. }
  18586. if (ret == 0) {
  18587. ret = wc_MakeDsaKey(&rng, &genKey);
  18588. }
  18589. if (ret == 0) {
  18590. ret = wc_DsaKeyToPublicDer(&genKey, der, ONEK_BUF);
  18591. if (ret >= 0) {
  18592. sz = ret;
  18593. ret = 0;
  18594. }
  18595. else {
  18596. ret = WOLFSSL_FATAL_ERROR;
  18597. }
  18598. }
  18599. if (ret == 0) {
  18600. word32 idx = 0;
  18601. wc_FreeDsaKey(&genKey);
  18602. ret = wc_DsaPublicKeyDecode(der, &idx, &genKey, sz);
  18603. }
  18604. /* Test without the SubjectPublicKeyInfo header */
  18605. if (ret == 0) {
  18606. ret = wc_SetDsaPublicKey(der, &genKey, ONEK_BUF, 0);
  18607. if (ret >= 0) {
  18608. sz = ret;
  18609. ret = 0;
  18610. }
  18611. else {
  18612. ret = WOLFSSL_FATAL_ERROR;
  18613. }
  18614. }
  18615. if (ret == 0) {
  18616. word32 idx = 0;
  18617. wc_FreeDsaKey(&genKey);
  18618. ret = wc_DsaPublicKeyDecode(der, &idx, &genKey, sz);
  18619. }
  18620. /* Test bad args. */
  18621. if (ret == 0) {
  18622. ret = wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF);
  18623. if (ret == BAD_FUNC_ARG) {
  18624. ret = wc_DsaKeyToPublicDer(&genKey, NULL, FOURK_BUF);
  18625. }
  18626. if (ret == BAD_FUNC_ARG) {
  18627. ret = 0;
  18628. }
  18629. else {
  18630. ret = WOLFSSL_FATAL_ERROR;
  18631. }
  18632. }
  18633. if (wc_FreeRng(&rng) && ret == 0) {
  18634. ret = WOLFSSL_FATAL_ERROR;
  18635. }
  18636. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  18637. wc_FreeDsaKey(&genKey);
  18638. res = TEST_RES_CHECK(ret == 0);
  18639. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  18640. #endif /* !HAVE_SELFTEST */
  18641. return res;
  18642. } /* END test_wc_DsaKeyToPublicDer */
  18643. /*
  18644. * Testing wc_DsaImportParamsRaw()
  18645. */
  18646. static int test_wc_DsaImportParamsRaw(void)
  18647. {
  18648. int res = TEST_SKIPPED;
  18649. #if !defined(NO_DSA)
  18650. DsaKey key;
  18651. int ret = 0;
  18652. /* [mod = L=1024, N=160], from CAVP KeyPair */
  18653. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  18654. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  18655. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  18656. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  18657. "47123188f8dc551054ee162b634d60f097f719076640e209"
  18658. "80a0093113a8bd73";
  18659. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  18660. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  18661. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  18662. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  18663. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  18664. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  18665. "76341a7e7d9";
  18666. /* invalid p and q parameters */
  18667. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  18668. const char* invalidQ = "96c5390a";
  18669. ret = wc_InitDsaKey(&key);
  18670. if (ret == 0) {
  18671. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  18672. }
  18673. /* test bad args */
  18674. if (ret == 0) {
  18675. /* null key struct */
  18676. ret = wc_DsaImportParamsRaw(NULL, p, q, g);
  18677. if (ret == BAD_FUNC_ARG) {
  18678. /* null param pointers */
  18679. ret = wc_DsaImportParamsRaw(&key, NULL, NULL, NULL);
  18680. }
  18681. if (ret == BAD_FUNC_ARG) {
  18682. /* illegal p length */
  18683. ret = wc_DsaImportParamsRaw(&key, invalidP, q, g);
  18684. }
  18685. if (ret == BAD_FUNC_ARG) {
  18686. /* illegal q length */
  18687. ret = wc_DsaImportParamsRaw(&key, p, invalidQ, g);
  18688. if (ret == BAD_FUNC_ARG)
  18689. ret = 0;
  18690. }
  18691. }
  18692. wc_FreeDsaKey(&key);
  18693. res = TEST_RES_CHECK(ret == 0);
  18694. #endif
  18695. return res;
  18696. } /* END test_wc_DsaImportParamsRaw */
  18697. /*
  18698. * Testing wc_DsaImportParamsRawCheck()
  18699. */
  18700. static int test_wc_DsaImportParamsRawCheck(void)
  18701. {
  18702. int res = TEST_SKIPPED;
  18703. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  18704. DsaKey key;
  18705. int ret = 0;
  18706. int trusted = 0;
  18707. /* [mod = L=1024, N=160], from CAVP KeyPair */
  18708. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  18709. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  18710. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  18711. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  18712. "47123188f8dc551054ee162b634d60f097f719076640e209"
  18713. "80a0093113a8bd73";
  18714. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  18715. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  18716. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  18717. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  18718. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  18719. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  18720. "76341a7e7d9";
  18721. /* invalid p and q parameters */
  18722. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  18723. const char* invalidQ = "96c5390a";
  18724. ret = wc_InitDsaKey(&key);
  18725. if (ret == 0) {
  18726. ret = wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL);
  18727. }
  18728. /* test bad args */
  18729. if (ret == 0) {
  18730. /* null key struct */
  18731. ret = wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL);
  18732. if (ret == BAD_FUNC_ARG) {
  18733. /* null param pointers */
  18734. ret = wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted, NULL);
  18735. }
  18736. if (ret == BAD_FUNC_ARG) {
  18737. /* illegal p length */
  18738. ret = wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL);
  18739. }
  18740. if (ret == BAD_FUNC_ARG) {
  18741. /* illegal q length */
  18742. ret = wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL);
  18743. if (ret == BAD_FUNC_ARG)
  18744. ret = 0;
  18745. }
  18746. }
  18747. wc_FreeDsaKey(&key);
  18748. res = TEST_RES_CHECK(ret == 0);
  18749. #endif
  18750. return res;
  18751. } /* END test_wc_DsaImportParamsRawCheck */
  18752. /*
  18753. * Testing wc_DsaExportParamsRaw()
  18754. */
  18755. static int test_wc_DsaExportParamsRaw(void)
  18756. {
  18757. int res = TEST_SKIPPED;
  18758. #if !defined(NO_DSA)
  18759. DsaKey key;
  18760. int ret = 0;
  18761. /* [mod = L=1024, N=160], from CAVP KeyPair */
  18762. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  18763. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  18764. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  18765. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  18766. "47123188f8dc551054ee162b634d60f097f719076640e209"
  18767. "80a0093113a8bd73";
  18768. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  18769. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  18770. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  18771. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  18772. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  18773. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  18774. "76341a7e7d9";
  18775. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  18776. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  18777. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  18778. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  18779. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  18780. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  18781. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  18782. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  18783. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  18784. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  18785. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  18786. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  18787. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  18788. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  18789. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  18790. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  18791. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  18792. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  18793. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  18794. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  18795. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  18796. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  18797. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  18798. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  18799. byte pOut[MAX_DSA_PARAM_SIZE];
  18800. byte qOut[MAX_DSA_PARAM_SIZE];
  18801. byte gOut[MAX_DSA_PARAM_SIZE];
  18802. word32 pOutSz, qOutSz, gOutSz;
  18803. ret = wc_InitDsaKey(&key);
  18804. if (ret == 0) {
  18805. /* first test using imported raw parameters, for expected */
  18806. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  18807. }
  18808. if (ret == 0) {
  18809. pOutSz = sizeof(pOut);
  18810. qOutSz = sizeof(qOut);
  18811. gOutSz = sizeof(gOut);
  18812. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  18813. gOut, &gOutSz);
  18814. }
  18815. if (ret == 0) {
  18816. /* validate exported parameters are correct */
  18817. if ((XMEMCMP(pOut, pCompare, pOutSz) != 0) ||
  18818. (XMEMCMP(qOut, qCompare, qOutSz) != 0) ||
  18819. (XMEMCMP(gOut, gCompare, gOutSz) != 0) ) {
  18820. ret = -1;
  18821. }
  18822. }
  18823. /* test bad args */
  18824. if (ret == 0) {
  18825. /* null key struct */
  18826. ret = wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz,
  18827. gOut, &gOutSz);
  18828. if (ret == BAD_FUNC_ARG) {
  18829. /* null output pointers */
  18830. ret = wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz,
  18831. NULL, &gOutSz);
  18832. }
  18833. if (ret == LENGTH_ONLY_E) {
  18834. /* null output size pointers */
  18835. ret = wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL,
  18836. gOut, NULL);
  18837. }
  18838. if (ret == BAD_FUNC_ARG) {
  18839. /* p output buffer size too small */
  18840. pOutSz = 1;
  18841. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  18842. gOut, &gOutSz);
  18843. pOutSz = sizeof(pOut);
  18844. }
  18845. if (ret == BUFFER_E) {
  18846. /* q output buffer size too small */
  18847. qOutSz = 1;
  18848. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  18849. gOut, &gOutSz);
  18850. qOutSz = sizeof(qOut);
  18851. }
  18852. if (ret == BUFFER_E) {
  18853. /* g output buffer size too small */
  18854. gOutSz = 1;
  18855. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  18856. gOut, &gOutSz);
  18857. if (ret == BUFFER_E)
  18858. ret = 0;
  18859. }
  18860. }
  18861. wc_FreeDsaKey(&key);
  18862. res = TEST_RES_CHECK(ret == 0);
  18863. #endif
  18864. return res;
  18865. } /* END test_wc_DsaExportParamsRaw */
  18866. /*
  18867. * Testing wc_DsaExportKeyRaw()
  18868. */
  18869. static int test_wc_DsaExportKeyRaw(void)
  18870. {
  18871. int res = TEST_SKIPPED;
  18872. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18873. DsaKey key;
  18874. WC_RNG rng;
  18875. int ret = 0;
  18876. byte xOut[MAX_DSA_PARAM_SIZE];
  18877. byte yOut[MAX_DSA_PARAM_SIZE];
  18878. word32 xOutSz, yOutSz;
  18879. XMEMSET(&rng, 0, sizeof(rng));
  18880. XMEMSET(&key, 0, sizeof(key));
  18881. ret = wc_InitRng(&rng);
  18882. if (ret == 0) {
  18883. ret = wc_InitDsaKey(&key);
  18884. }
  18885. if (ret == 0) {
  18886. ret = wc_MakeDsaParameters(&rng, 1024, &key);
  18887. if (ret == 0) {
  18888. ret = wc_MakeDsaKey(&rng, &key);
  18889. }
  18890. }
  18891. /* try successful export */
  18892. if (ret == 0) {
  18893. xOutSz = sizeof(xOut);
  18894. yOutSz = sizeof(yOut);
  18895. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  18896. }
  18897. /* test bad args */
  18898. if (ret == 0) {
  18899. /* null key struct */
  18900. ret = wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz);
  18901. if (ret == BAD_FUNC_ARG) {
  18902. /* null output pointers */
  18903. ret = wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz);
  18904. }
  18905. if (ret == LENGTH_ONLY_E) {
  18906. /* null output size pointers */
  18907. ret = wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL);
  18908. }
  18909. if (ret == BAD_FUNC_ARG) {
  18910. /* x output buffer size too small */
  18911. xOutSz = 1;
  18912. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  18913. xOutSz = sizeof(xOut);
  18914. }
  18915. if (ret == BUFFER_E) {
  18916. /* y output buffer size too small */
  18917. yOutSz = 1;
  18918. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  18919. if (ret == BUFFER_E)
  18920. ret = 0;
  18921. }
  18922. }
  18923. wc_FreeDsaKey(&key);
  18924. wc_FreeRng(&rng);
  18925. res = TEST_RES_CHECK(ret == 0);
  18926. #endif
  18927. return res;
  18928. } /* END test_wc_DsaExportParamsRaw */
  18929. /*
  18930. * Testing wc_ed25519_make_key().
  18931. */
  18932. static int test_wc_ed25519_make_key(void)
  18933. {
  18934. int res = TEST_SKIPPED;
  18935. #if defined(HAVE_ED25519)
  18936. ed25519_key key;
  18937. WC_RNG rng;
  18938. unsigned char pubkey[ED25519_PUB_KEY_SIZE];
  18939. int ret = 0;
  18940. ret = wc_InitRng(&rng);
  18941. if (ret == 0) {
  18942. ret = wc_ed25519_init(&key);
  18943. }
  18944. if (ret == 0) {
  18945. ret = wc_ed25519_make_public(&key, pubkey, sizeof(pubkey));
  18946. if (ret == ECC_PRIV_KEY_E) {
  18947. ret = 0;
  18948. }
  18949. else if (ret == 0) {
  18950. ret = -1;
  18951. }
  18952. }
  18953. if (ret == 0) {
  18954. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18955. }
  18956. /* Test bad args. */
  18957. if (ret == 0) {
  18958. ret = wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key);
  18959. if (ret == BAD_FUNC_ARG) {
  18960. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL);
  18961. }
  18962. if (ret == BAD_FUNC_ARG) {
  18963. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key);
  18964. }
  18965. if (ret == BAD_FUNC_ARG) {
  18966. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key);
  18967. }
  18968. if (ret == BAD_FUNC_ARG) {
  18969. ret = 0;
  18970. }
  18971. else if (ret == 0) {
  18972. ret = WOLFSSL_FATAL_ERROR;
  18973. }
  18974. }
  18975. if (wc_FreeRng(&rng) && ret == 0) {
  18976. ret = WOLFSSL_FATAL_ERROR;
  18977. }
  18978. wc_ed25519_free(&key);
  18979. res = TEST_RES_CHECK(ret == 0);
  18980. #endif
  18981. return res;
  18982. } /* END test_wc_ed25519_make_key */
  18983. /*
  18984. * Testing wc_ed25519_init()
  18985. */
  18986. static int test_wc_ed25519_init(void)
  18987. {
  18988. int res = TEST_SKIPPED;
  18989. #if defined(HAVE_ED25519)
  18990. ed25519_key key;
  18991. int ret = 0;
  18992. ret = wc_ed25519_init(&key);
  18993. /* Test bad args. */
  18994. if (ret == 0) {
  18995. ret = wc_ed25519_init(NULL);
  18996. if (ret == BAD_FUNC_ARG) {
  18997. ret = 0;
  18998. }
  18999. else if (ret == 0) {
  19000. ret = WOLFSSL_FATAL_ERROR;
  19001. }
  19002. }
  19003. wc_ed25519_free(&key);
  19004. res = TEST_RES_CHECK(ret == 0);
  19005. #endif
  19006. return res;
  19007. } /* END test_wc_ed25519_init */
  19008. /*
  19009. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  19010. */
  19011. static int test_wc_ed25519_sign_msg(void)
  19012. {
  19013. int res = TEST_SKIPPED;
  19014. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  19015. WC_RNG rng;
  19016. ed25519_key key;
  19017. int ret = 0;
  19018. byte msg[] = "Everybody gets Friday off.\n";
  19019. byte sig[ED25519_SIG_SIZE];
  19020. word32 msglen = sizeof(msg);
  19021. word32 siglen = sizeof(sig);
  19022. word32 badSigLen = sizeof(sig) - 1;
  19023. #ifdef HAVE_ED25519_VERIFY
  19024. int verify_ok = 0; /*1 = Verify success.*/
  19025. #endif
  19026. /* Initialize stack variables. */
  19027. XMEMSET(sig, 0, siglen);
  19028. /* Initialize key. */
  19029. ret = wc_InitRng(&rng);
  19030. if (ret == 0) {
  19031. ret = wc_ed25519_init(&key);
  19032. if (ret == 0) {
  19033. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19034. }
  19035. }
  19036. if (ret == 0) {
  19037. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key);
  19038. }
  19039. /* Test bad args. */
  19040. if (ret == 0 && siglen == ED25519_SIG_SIZE) {
  19041. ret = wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key);
  19042. if (ret == BAD_FUNC_ARG) {
  19043. ret = wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key);
  19044. }
  19045. if (ret == BAD_FUNC_ARG) {
  19046. ret = wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key);
  19047. }
  19048. if (ret == BAD_FUNC_ARG) {
  19049. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL);
  19050. }
  19051. if (ret == BAD_FUNC_ARG) {
  19052. ret = wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key);
  19053. }
  19054. if (ret == BUFFER_E && badSigLen == ED25519_SIG_SIZE) {
  19055. badSigLen -= 1;
  19056. ret = 0;
  19057. }
  19058. else if (ret == 0) {
  19059. ret = WOLFSSL_FATAL_ERROR;
  19060. }
  19061. } /* END sign */
  19062. #ifdef HAVE_ED25519_VERIFY
  19063. if (ret == 0) {
  19064. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key);
  19065. if (ret == 0 && verify_ok == 1) {
  19066. ret = 0;
  19067. }
  19068. else if (ret == 0) {
  19069. ret = WOLFSSL_FATAL_ERROR;
  19070. }
  19071. /* Test bad args. */
  19072. if (ret == 0) {
  19073. AssertIntEQ(wc_ed25519_verify_msg(sig, siglen - 1, msg,
  19074. msglen, &verify_ok, &key),
  19075. BAD_FUNC_ARG);
  19076. AssertIntEQ(wc_ed25519_verify_msg(sig, siglen + 1, msg,
  19077. msglen, &verify_ok, &key),
  19078. BAD_FUNC_ARG);
  19079. ret = wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  19080. &key);
  19081. if (ret == BAD_FUNC_ARG) {
  19082. ret = wc_ed25519_verify_msg(sig, siglen, NULL, msglen,
  19083. &verify_ok, &key);
  19084. }
  19085. if (ret == BAD_FUNC_ARG) {
  19086. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  19087. NULL, &key);
  19088. }
  19089. if (ret == BAD_FUNC_ARG) {
  19090. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  19091. &verify_ok, NULL);
  19092. }
  19093. if (ret == BAD_FUNC_ARG) {
  19094. ret = wc_ed25519_verify_msg(sig, badSigLen, msg, msglen,
  19095. &verify_ok, &key);
  19096. }
  19097. if (ret == BAD_FUNC_ARG) {
  19098. ret = 0;
  19099. }
  19100. else if (ret == 0) {
  19101. ret = WOLFSSL_FATAL_ERROR;
  19102. }
  19103. }
  19104. } /* END verify. */
  19105. #endif /* Verify. */
  19106. if (wc_FreeRng(&rng) && ret == 0) {
  19107. ret = WOLFSSL_FATAL_ERROR;
  19108. }
  19109. wc_ed25519_free(&key);
  19110. res = TEST_RES_CHECK(ret == 0);
  19111. #endif
  19112. return res;
  19113. } /* END test_wc_ed25519_sign_msg */
  19114. /*
  19115. * Testing wc_ed25519_import_public()
  19116. */
  19117. static int test_wc_ed25519_import_public(void)
  19118. {
  19119. int res = TEST_SKIPPED;
  19120. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  19121. WC_RNG rng;
  19122. ed25519_key pubKey;
  19123. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  19124. word32 inlen = sizeof(in);
  19125. int ret = 0;
  19126. ret = wc_InitRng(&rng);
  19127. if (ret == 0) {
  19128. ret = wc_ed25519_init(&pubKey);
  19129. if (ret == 0) {
  19130. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey);
  19131. }
  19132. }
  19133. if (ret == 0) {
  19134. ret = wc_ed25519_import_public_ex(in, inlen, &pubKey, 1);
  19135. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  19136. ret = 0;
  19137. }
  19138. else {
  19139. ret = WOLFSSL_FATAL_ERROR;
  19140. }
  19141. /* Test bad args. */
  19142. if (ret == 0) {
  19143. ret = wc_ed25519_import_public(NULL, inlen, &pubKey);
  19144. if (ret == BAD_FUNC_ARG) {
  19145. ret = wc_ed25519_import_public(in, inlen, NULL);
  19146. }
  19147. if (ret == BAD_FUNC_ARG) {
  19148. ret = wc_ed25519_import_public(in, inlen - 1, &pubKey);
  19149. }
  19150. if (ret == BAD_FUNC_ARG) {
  19151. ret = 0;
  19152. }
  19153. else if (ret == 0) {
  19154. ret = WOLFSSL_FATAL_ERROR;
  19155. }
  19156. }
  19157. }
  19158. if (wc_FreeRng(&rng) && ret == 0) {
  19159. ret = WOLFSSL_FATAL_ERROR;
  19160. }
  19161. wc_ed25519_free(&pubKey);
  19162. res = TEST_RES_CHECK(ret == 0);
  19163. #endif
  19164. return res;
  19165. } /* END wc_ed25519_import_public */
  19166. /*
  19167. * Testing wc_ed25519_import_private_key()
  19168. */
  19169. static int test_wc_ed25519_import_private_key(void)
  19170. {
  19171. int res = TEST_SKIPPED;
  19172. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  19173. WC_RNG rng;
  19174. ed25519_key key;
  19175. int ret;
  19176. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  19177. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  19178. word32 privKeySz = sizeof(privKey);
  19179. word32 pubKeySz = sizeof(pubKey);
  19180. #ifdef HAVE_ED25519_KEY_EXPORT
  19181. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  19182. word32 bothKeysSz = sizeof(bothKeys);
  19183. #endif
  19184. ret = wc_InitRng(&rng);
  19185. if (ret != 0) {
  19186. return ret;
  19187. }
  19188. ret = wc_ed25519_init(&key);
  19189. if (ret != 0) {
  19190. wc_FreeRng(&rng);
  19191. return ret;
  19192. }
  19193. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19194. if (ret == 0) {
  19195. ret = wc_ed25519_import_private_key_ex(privKey, privKeySz, pubKey,
  19196. pubKeySz, &key, 1);
  19197. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
  19198. || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  19199. ret = WOLFSSL_FATAL_ERROR;
  19200. }
  19201. }
  19202. #ifdef HAVE_ED25519_KEY_EXPORT
  19203. if (ret == 0)
  19204. ret = wc_ed25519_export_private(&key, bothKeys, &bothKeysSz);
  19205. if (ret == 0) {
  19206. ret = wc_ed25519_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  19207. &key, 1);
  19208. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
  19209. || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  19210. ret = WOLFSSL_FATAL_ERROR;
  19211. }
  19212. }
  19213. #endif
  19214. /* Test bad args. */
  19215. if (ret == 0) {
  19216. ret = wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  19217. &key);
  19218. if (ret == BAD_FUNC_ARG) {
  19219. ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  19220. pubKeySz, &key);
  19221. }
  19222. if (ret == BAD_FUNC_ARG) {
  19223. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  19224. pubKeySz, NULL);
  19225. }
  19226. if (ret == BAD_FUNC_ARG) {
  19227. ret = wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  19228. pubKeySz, &key);
  19229. }
  19230. if (ret == BAD_FUNC_ARG) {
  19231. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  19232. pubKeySz - 1, &key);
  19233. }
  19234. if (ret == BAD_FUNC_ARG) {
  19235. ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  19236. 0, &key);
  19237. }
  19238. if (ret == BAD_FUNC_ARG) {
  19239. ret = 0;
  19240. }
  19241. else if (ret == 0) {
  19242. ret = WOLFSSL_FATAL_ERROR;
  19243. }
  19244. }
  19245. if (wc_FreeRng(&rng) && ret == 0) {
  19246. ret = WOLFSSL_FATAL_ERROR;
  19247. }
  19248. wc_ed25519_free(&key);
  19249. res = TEST_RES_CHECK(ret == 0);
  19250. #endif
  19251. return res;
  19252. } /* END test_wc_ed25519_import_private_key */
  19253. /*
  19254. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  19255. */
  19256. static int test_wc_ed25519_export(void)
  19257. {
  19258. int res = TEST_SKIPPED;
  19259. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  19260. WC_RNG rng;
  19261. ed25519_key key;
  19262. int ret = 0;
  19263. byte priv[ED25519_PRV_KEY_SIZE];
  19264. byte pub[ED25519_PUB_KEY_SIZE];
  19265. word32 privSz = sizeof(priv);
  19266. word32 pubSz = sizeof(pub);
  19267. ret = wc_InitRng(&rng);
  19268. if (ret != 0) {
  19269. return ret;
  19270. }
  19271. ret = wc_ed25519_init(&key);
  19272. if (ret != 0) {
  19273. wc_FreeRng(&rng);
  19274. return ret;
  19275. }
  19276. if (ret == 0) {
  19277. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19278. }
  19279. if (ret == 0) {
  19280. ret = wc_ed25519_export_public(&key, pub, &pubSz);
  19281. if (ret == 0 && (pubSz != ED25519_KEY_SIZE
  19282. || XMEMCMP(key.p, pub, pubSz) != 0)) {
  19283. ret = WOLFSSL_FATAL_ERROR;
  19284. }
  19285. if (ret == 0) {
  19286. ret = wc_ed25519_export_public(NULL, pub, &pubSz);
  19287. if (ret == BAD_FUNC_ARG) {
  19288. ret = wc_ed25519_export_public(&key, NULL, &pubSz);
  19289. }
  19290. if (ret == BAD_FUNC_ARG) {
  19291. ret = wc_ed25519_export_public(&key, pub, NULL);
  19292. }
  19293. if (ret == BAD_FUNC_ARG) {
  19294. ret = 0;
  19295. }
  19296. else if (ret == 0) {
  19297. ret = WOLFSSL_FATAL_ERROR;
  19298. }
  19299. }
  19300. }
  19301. if (ret == 0) {
  19302. ret = wc_ed25519_export_private_only(&key, priv, &privSz);
  19303. if (ret == 0 && (privSz != ED25519_KEY_SIZE
  19304. || XMEMCMP(key.k, priv, privSz) != 0)) {
  19305. ret = WOLFSSL_FATAL_ERROR;
  19306. }
  19307. if (ret == 0) {
  19308. ret = wc_ed25519_export_private_only(NULL, priv, &privSz);
  19309. if (ret == BAD_FUNC_ARG) {
  19310. ret = wc_ed25519_export_private_only(&key, NULL, &privSz);
  19311. }
  19312. if (ret == BAD_FUNC_ARG) {
  19313. ret = wc_ed25519_export_private_only(&key, priv, NULL);
  19314. }
  19315. if (ret == BAD_FUNC_ARG) {
  19316. ret = 0;
  19317. }
  19318. else if (ret == 0) {
  19319. ret = WOLFSSL_FATAL_ERROR;
  19320. }
  19321. }
  19322. }
  19323. if (wc_FreeRng(&rng) && ret == 0) {
  19324. ret = WOLFSSL_FATAL_ERROR;
  19325. }
  19326. wc_ed25519_free(&key);
  19327. res = TEST_RES_CHECK(ret == 0);
  19328. #endif
  19329. return res;
  19330. } /* END test_wc_ed25519_export */
  19331. /*
  19332. * Testing wc_ed25519_size()
  19333. */
  19334. static int test_wc_ed25519_size(void)
  19335. {
  19336. int res = TEST_SKIPPED;
  19337. #if defined(HAVE_ED25519)
  19338. WC_RNG rng;
  19339. ed25519_key key;
  19340. int ret;
  19341. ret = wc_InitRng(&rng);
  19342. if (ret != 0) {
  19343. return ret;
  19344. }
  19345. ret = wc_ed25519_init(&key);
  19346. if (ret != 0) {
  19347. wc_FreeRng(&rng);
  19348. return ret;
  19349. }
  19350. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19351. if (ret != 0) {
  19352. wc_FreeRng(&rng);
  19353. wc_ed25519_free(&key);
  19354. return ret;
  19355. }
  19356. ret = wc_ed25519_size(&key);
  19357. /* Test bad args. */
  19358. if (ret == ED25519_KEY_SIZE) {
  19359. ret = wc_ed25519_size(NULL);
  19360. if (ret == BAD_FUNC_ARG) {
  19361. ret = 0;
  19362. }
  19363. }
  19364. if (ret == 0) {
  19365. ret = wc_ed25519_sig_size(&key);
  19366. if (ret == ED25519_SIG_SIZE) {
  19367. ret = 0;
  19368. }
  19369. /* Test bad args. */
  19370. if (ret == 0) {
  19371. ret = wc_ed25519_sig_size(NULL);
  19372. if (ret == BAD_FUNC_ARG) {
  19373. ret = 0;
  19374. }
  19375. }
  19376. } /* END wc_ed25519_sig_size() */
  19377. if (ret == 0) {
  19378. ret = wc_ed25519_pub_size(&key);
  19379. if (ret == ED25519_PUB_KEY_SIZE) {
  19380. ret = 0;
  19381. }
  19382. if (ret == 0) {
  19383. ret = wc_ed25519_pub_size(NULL);
  19384. if (ret == BAD_FUNC_ARG) {
  19385. ret = 0;
  19386. }
  19387. }
  19388. } /* END wc_ed25519_pub_size */
  19389. if (ret == 0) {
  19390. ret = wc_ed25519_priv_size(&key);
  19391. if (ret == ED25519_PRV_KEY_SIZE) {
  19392. ret = 0;
  19393. }
  19394. if (ret == 0) {
  19395. ret = wc_ed25519_priv_size(NULL);
  19396. if (ret == BAD_FUNC_ARG) {
  19397. ret = 0;
  19398. }
  19399. }
  19400. } /* END wc_ed25519_pub_size */
  19401. if (wc_FreeRng(&rng) && ret == 0) {
  19402. ret = WOLFSSL_FATAL_ERROR;
  19403. }
  19404. wc_ed25519_free(&key);
  19405. res = TEST_RES_CHECK(ret == 0);
  19406. #endif
  19407. return res;
  19408. } /* END test_wc_ed25519_size */
  19409. /*
  19410. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  19411. */
  19412. static int test_wc_ed25519_exportKey(void)
  19413. {
  19414. int res = TEST_SKIPPED;
  19415. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  19416. WC_RNG rng;
  19417. ed25519_key key;
  19418. int ret = 0;
  19419. byte priv[ED25519_PRV_KEY_SIZE];
  19420. byte pub[ED25519_PUB_KEY_SIZE];
  19421. byte privOnly[ED25519_PRV_KEY_SIZE];
  19422. word32 privSz = sizeof(priv);
  19423. word32 pubSz = sizeof(pub);
  19424. word32 privOnlySz = sizeof(privOnly);
  19425. ret = wc_InitRng(&rng);
  19426. if (ret != 0) {
  19427. return TEST_FAIL;
  19428. }
  19429. ret = wc_ed25519_init(&key);
  19430. if (ret != 0) {
  19431. wc_FreeRng(&rng);
  19432. return TEST_FAIL;
  19433. }
  19434. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19435. if (ret != 0) {
  19436. wc_FreeRng(&rng);
  19437. wc_ed25519_free(&key);
  19438. return TEST_FAIL;
  19439. }
  19440. ret = wc_ed25519_export_private(&key, privOnly, &privOnlySz);
  19441. if (ret == 0) {
  19442. ret = wc_ed25519_export_private(NULL, privOnly, &privOnlySz);
  19443. if (ret == BAD_FUNC_ARG) {
  19444. ret = wc_ed25519_export_private(&key, NULL, &privOnlySz);
  19445. }
  19446. if (ret == BAD_FUNC_ARG) {
  19447. ret = wc_ed25519_export_private(&key, privOnly, NULL);
  19448. }
  19449. if (ret == BAD_FUNC_ARG) {
  19450. ret = 0;
  19451. }
  19452. else if (ret == 0) {
  19453. ret = WOLFSSL_FATAL_ERROR;
  19454. }
  19455. }
  19456. if (ret == 0) {
  19457. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz);
  19458. if (ret == 0) {
  19459. ret = wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz);
  19460. if (ret == BAD_FUNC_ARG) {
  19461. ret = wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz);
  19462. }
  19463. if (ret == BAD_FUNC_ARG) {
  19464. ret = wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz);
  19465. }
  19466. if (ret == BAD_FUNC_ARG) {
  19467. ret = wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz);
  19468. }
  19469. if (ret == BAD_FUNC_ARG) {
  19470. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, NULL);
  19471. }
  19472. if (ret == BAD_FUNC_ARG) {
  19473. ret = 0;
  19474. }
  19475. else if (ret == 0) {
  19476. ret = WOLFSSL_FATAL_ERROR;
  19477. }
  19478. }
  19479. } /* END wc_ed25519_export_key() */
  19480. /* Cross check output. */
  19481. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  19482. ret = WOLFSSL_FATAL_ERROR;
  19483. }
  19484. if (wc_FreeRng(&rng) && ret == 0) {
  19485. ret = WOLFSSL_FATAL_ERROR;
  19486. }
  19487. wc_ed25519_free(&key);
  19488. res = TEST_RES_CHECK(ret == 0);
  19489. #endif
  19490. return res;
  19491. } /* END test_wc_ed25519_exportKey */
  19492. /*
  19493. * Testing wc_Ed25519PublicKeyToDer
  19494. */
  19495. static int test_wc_Ed25519PublicKeyToDer(void)
  19496. {
  19497. int res = TEST_SKIPPED;
  19498. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  19499. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  19500. int tmp;
  19501. ed25519_key key;
  19502. byte derBuf[1024];
  19503. int ret = 0;
  19504. /* Test bad args */
  19505. tmp = wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0);
  19506. if (tmp != BAD_FUNC_ARG) {
  19507. ret = WOLFSSL_FATAL_ERROR;
  19508. }
  19509. if (ret == 0) {
  19510. wc_ed25519_init(&key);
  19511. tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0);
  19512. if (tmp != BUFFER_E) {
  19513. ret = WOLFSSL_FATAL_ERROR;
  19514. }
  19515. wc_ed25519_free(&key);
  19516. }
  19517. /* Test good args */
  19518. if (ret == 0) {
  19519. WC_RNG rng;
  19520. ret = wc_InitRng(&rng);
  19521. if (ret != 0) {
  19522. return TEST_FAIL;
  19523. }
  19524. ret = wc_ed25519_init(&key);
  19525. if (ret != 0) {
  19526. wc_FreeRng(&rng);
  19527. return TEST_FAIL;
  19528. }
  19529. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19530. if (ret != 0) {
  19531. wc_FreeRng(&rng);
  19532. wc_ed25519_free(&key);
  19533. return TEST_FAIL;
  19534. }
  19535. tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1);
  19536. if (tmp <= 0) {
  19537. ret = WOLFSSL_FATAL_ERROR;
  19538. }
  19539. wc_FreeRng(&rng);
  19540. wc_ed25519_free(&key);
  19541. }
  19542. res = TEST_RES_CHECK(ret == 0);
  19543. #endif
  19544. return res;
  19545. } /* END testing wc_Ed25519PublicKeyToDer */
  19546. /*
  19547. * Testing wc_curve25519_init and wc_curve25519_free.
  19548. */
  19549. static int test_wc_curve25519_init(void)
  19550. {
  19551. int res = TEST_SKIPPED;
  19552. #if defined(HAVE_CURVE25519)
  19553. curve25519_key key;
  19554. int ret = 0;
  19555. ret = wc_curve25519_init(&key);
  19556. /* Test bad args for wc_curve25519_init */
  19557. if (ret == 0) {
  19558. ret = wc_curve25519_init(NULL);
  19559. if (ret == BAD_FUNC_ARG) {
  19560. ret = 0;
  19561. }
  19562. else if (ret == 0) {
  19563. ret = WOLFSSL_FATAL_ERROR;
  19564. }
  19565. }
  19566. /* Test good args for wc_curve_25519_free */
  19567. wc_curve25519_free(&key);
  19568. wc_curve25519_free(NULL);
  19569. res = TEST_RES_CHECK(ret == 0);
  19570. #endif
  19571. return res;
  19572. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  19573. /*
  19574. * Testing test_wc_curve25519_size.
  19575. */
  19576. static int test_wc_curve25519_size(void)
  19577. {
  19578. int res = TEST_SKIPPED;
  19579. #if defined(HAVE_CURVE25519)
  19580. curve25519_key key;
  19581. int ret = 0;
  19582. ret = wc_curve25519_init(&key);
  19583. /* Test good args for wc_curve25519_size */
  19584. if (ret == 0) {
  19585. ret = wc_curve25519_size(&key);
  19586. }
  19587. /* Test bad args for wc_curve25519_size */
  19588. if (ret != 0) {
  19589. ret = wc_curve25519_size(NULL);
  19590. }
  19591. wc_curve25519_free(&key);
  19592. res = TEST_RES_CHECK(ret == 0);
  19593. #endif
  19594. return res;
  19595. } /* END test_wc_curve25519_size*/
  19596. /*
  19597. * Testing test_wc_curve25519_export_key_raw().
  19598. */
  19599. static int test_wc_curve25519_export_key_raw(void)
  19600. {
  19601. int res = TEST_SKIPPED;
  19602. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  19603. curve25519_key key;
  19604. WC_RNG rng;
  19605. int ret = 0;
  19606. byte privateKey[CURVE25519_KEYSIZE];
  19607. byte publicKey[CURVE25519_KEYSIZE];
  19608. word32 prvkSz;
  19609. word32 pubkSz;
  19610. byte prik[CURVE25519_KEYSIZE];
  19611. byte pubk[CURVE25519_KEYSIZE];
  19612. word32 prksz;
  19613. word32 pbksz;
  19614. if (0 != wc_InitRng(&rng)) {
  19615. return TEST_FAIL;
  19616. }
  19617. if (0 != wc_curve25519_init(&key)) {
  19618. wc_FreeRng(&rng);
  19619. return TEST_FAIL;
  19620. }
  19621. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19622. /*
  19623. bad-argument-test cases
  19624. target function sould return BAD_FUNC_ARG
  19625. */
  19626. if (ret == 0) {
  19627. prvkSz = CURVE25519_KEYSIZE;
  19628. pubkSz = CURVE25519_KEYSIZE;
  19629. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  19630. NULL, privateKey, &prvkSz, publicKey, &pubkSz)) {
  19631. ret = -1;
  19632. }
  19633. }
  19634. if (ret == 0) {
  19635. prvkSz = CURVE25519_KEYSIZE;
  19636. pubkSz = CURVE25519_KEYSIZE;
  19637. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  19638. &key, NULL, &prvkSz, publicKey, &pubkSz)) {
  19639. ret = -1;
  19640. }
  19641. }
  19642. if (ret == 0) {
  19643. prvkSz = CURVE25519_KEYSIZE;
  19644. pubkSz = CURVE25519_KEYSIZE;
  19645. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  19646. &key, privateKey, NULL, publicKey, &pubkSz)) {
  19647. ret = -1;
  19648. }
  19649. }
  19650. if (ret == 0) {
  19651. /* prvkSz = CURVE25519_KEYSIZE; */
  19652. pubkSz = CURVE25519_KEYSIZE;
  19653. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  19654. &key, privateKey, &prvkSz, NULL, &pubkSz)) {
  19655. ret = -1;
  19656. }
  19657. }
  19658. if (ret == 0) {
  19659. prvkSz = CURVE25519_KEYSIZE;
  19660. pubkSz = CURVE25519_KEYSIZE;
  19661. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  19662. &key, privateKey, &prvkSz, publicKey, NULL )) {
  19663. ret = -1;
  19664. }
  19665. }
  19666. /*
  19667. cross-testing
  19668. */
  19669. if (ret == 0) {
  19670. prksz = CURVE25519_KEYSIZE;
  19671. ret = wc_curve25519_export_private_raw(&key, prik, &prksz);
  19672. }
  19673. if (ret == 0) {
  19674. pbksz = CURVE25519_KEYSIZE;
  19675. ret = wc_curve25519_export_public(&key, pubk, &pbksz);
  19676. }
  19677. if (ret == 0) {
  19678. prvkSz = CURVE25519_KEYSIZE;
  19679. /* pubkSz = CURVE25519_KEYSIZE; */
  19680. ret = wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  19681. publicKey, &pubkSz);
  19682. }
  19683. if (ret == 0) {
  19684. if ((prksz == CURVE25519_KEYSIZE) &&
  19685. (pbksz == CURVE25519_KEYSIZE) &&
  19686. (prvkSz == CURVE25519_KEYSIZE) &&
  19687. (pubkSz == CURVE25519_KEYSIZE)) {
  19688. if (0 != XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE) ||
  19689. 0 != XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE)) {
  19690. ret = -1;
  19691. }
  19692. }
  19693. }
  19694. wc_curve25519_free(&key);
  19695. wc_FreeRng(&rng);
  19696. res = TEST_RES_CHECK(ret == 0);
  19697. #endif
  19698. return res;
  19699. } /* end of test_wc_curve25519_export_key_raw */
  19700. /*
  19701. * Testing test_wc_curve25519_export_key_raw_ex().
  19702. */
  19703. static int test_wc_curve25519_export_key_raw_ex(void)
  19704. {
  19705. int res = TEST_SKIPPED;
  19706. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  19707. curve25519_key key;
  19708. WC_RNG rng;
  19709. int ret;
  19710. byte privateKey[CURVE25519_KEYSIZE];
  19711. byte publicKey[CURVE25519_KEYSIZE];
  19712. word32 prvkSz;
  19713. word32 pubkSz;
  19714. byte prik[CURVE25519_KEYSIZE];
  19715. byte pubk[CURVE25519_KEYSIZE];
  19716. word32 prksz;
  19717. word32 pbksz;
  19718. if (0 != wc_InitRng(&rng)) {
  19719. return TEST_FAIL;
  19720. }
  19721. if (0 != wc_curve25519_init(&key)) {
  19722. wc_FreeRng(&rng);
  19723. return TEST_FAIL;
  19724. }
  19725. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19726. /*
  19727. bad-argument-test cases
  19728. target function sould return BAD_FUNC_ARG
  19729. */
  19730. if (ret == 0) {
  19731. prvkSz = CURVE25519_KEYSIZE;
  19732. pubkSz = CURVE25519_KEYSIZE;
  19733. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL , privateKey,
  19734. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)) {
  19735. ret = -1;
  19736. }
  19737. }
  19738. if (ret == 0) {
  19739. prvkSz = CURVE25519_KEYSIZE;
  19740. pubkSz = CURVE25519_KEYSIZE;
  19741. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key , NULL,
  19742. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)) {
  19743. ret = -1;
  19744. }
  19745. }
  19746. if (ret == 0) {
  19747. prvkSz = CURVE25519_KEYSIZE;
  19748. pubkSz = CURVE25519_KEYSIZE;
  19749. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key,privateKey,
  19750. NULL, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)) {
  19751. ret = -1;
  19752. }
  19753. }
  19754. if (ret == 0) {
  19755. /* prvkSz = CURVE25519_KEYSIZE; */
  19756. pubkSz = CURVE25519_KEYSIZE;
  19757. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19758. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN)) {
  19759. ret = -1;
  19760. }
  19761. }
  19762. if (ret == 0) {
  19763. prvkSz = CURVE25519_KEYSIZE;
  19764. pubkSz = CURVE25519_KEYSIZE;
  19765. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19766. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN)) {
  19767. ret = -1;
  19768. }
  19769. }
  19770. if (ret == 0) {
  19771. prvkSz = CURVE25519_KEYSIZE;
  19772. /* pubkSz = CURVE25519_KEYSIZE; */
  19773. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL, privateKey,
  19774. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
  19775. ret = -1;
  19776. }
  19777. }
  19778. if (ret == 0) {
  19779. prvkSz = CURVE25519_KEYSIZE;
  19780. pubkSz = CURVE25519_KEYSIZE;
  19781. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, NULL,
  19782. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
  19783. ret = -1;
  19784. }
  19785. }
  19786. if (ret == 0) {
  19787. prvkSz = CURVE25519_KEYSIZE;
  19788. pubkSz = CURVE25519_KEYSIZE;
  19789. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19790. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
  19791. ret = -1;
  19792. }
  19793. }
  19794. if (ret == 0) {
  19795. /* prvkSz = CURVE25519_KEYSIZE; */
  19796. pubkSz = CURVE25519_KEYSIZE;
  19797. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19798. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN)) {
  19799. ret = -1;
  19800. }
  19801. }
  19802. if (ret == 0) {
  19803. prvkSz = CURVE25519_KEYSIZE;
  19804. pubkSz = CURVE25519_KEYSIZE;
  19805. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19806. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN)) {
  19807. ret = -1;
  19808. }
  19809. }
  19810. /* illegal value for endien */
  19811. if (ret == 0) {
  19812. prvkSz = CURVE25519_KEYSIZE;
  19813. /* pubkSz = CURVE25519_KEYSIZE; */
  19814. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex(&key, privateKey,
  19815. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN + 10)) {
  19816. ret = -1;
  19817. }
  19818. }
  19819. /*
  19820. cross-testing
  19821. */
  19822. if (ret == 0) {
  19823. prksz = CURVE25519_KEYSIZE;
  19824. ret = wc_curve25519_export_private_raw( &key, prik, &prksz);
  19825. }
  19826. if (ret == 0) {
  19827. pbksz = CURVE25519_KEYSIZE;
  19828. ret = wc_curve25519_export_public( &key, pubk, &pbksz);
  19829. }
  19830. if (ret == 0) {
  19831. prvkSz = CURVE25519_KEYSIZE;
  19832. /* pubkSz = CURVE25519_KEYSIZE; */
  19833. ret = wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  19834. publicKey, &pubkSz, EC25519_BIG_ENDIAN);
  19835. }
  19836. if (ret == 0 && (prksz != CURVE25519_KEYSIZE ||
  19837. pbksz != CURVE25519_KEYSIZE ||
  19838. prvkSz != CURVE25519_KEYSIZE ||
  19839. pubkSz != CURVE25519_KEYSIZE)) {
  19840. ret = -1;
  19841. }
  19842. if (ret == 0 && (0 != XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE) ||
  19843. 0 != XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE))) {
  19844. ret = -1;
  19845. }
  19846. if (ret == 0) {
  19847. ret = wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  19848. publicKey, &pubkSz, EC25519_LITTLE_ENDIAN);
  19849. }
  19850. if (ret == 0 && (prvkSz != CURVE25519_KEYSIZE ||
  19851. pubkSz != CURVE25519_KEYSIZE)) {
  19852. ret = -1;
  19853. }
  19854. /*
  19855. try once with another endian
  19856. */
  19857. if (ret == 0) {
  19858. prvkSz = CURVE25519_KEYSIZE;
  19859. pubkSz = CURVE25519_KEYSIZE;
  19860. ret = wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  19861. publicKey, &pubkSz, EC25519_BIG_ENDIAN);
  19862. }
  19863. if (ret == 0 && (prvkSz != CURVE25519_KEYSIZE ||
  19864. pubkSz != CURVE25519_KEYSIZE)) {
  19865. ret = -1;
  19866. }
  19867. wc_curve25519_free(&key);
  19868. wc_FreeRng(&rng);
  19869. res = TEST_RES_CHECK(ret == 0);
  19870. #endif
  19871. return res;
  19872. } /* end of test_wc_curve25519_export_key_raw_ex */
  19873. /*
  19874. * Testing wc_curve25519_make_key
  19875. */
  19876. static int test_wc_curve25519_make_key(void)
  19877. {
  19878. int res = TEST_SKIPPED;
  19879. #if defined(HAVE_CURVE25519)
  19880. WC_RNG rng;
  19881. curve25519_key key;
  19882. int keysize;
  19883. int ret;
  19884. ret = wc_curve25519_init(&key);
  19885. if (ret == 0) {
  19886. ret = wc_InitRng(&rng);
  19887. }
  19888. if (ret == 0) {
  19889. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19890. if (ret == 0) {
  19891. keysize = wc_curve25519_size(&key);
  19892. if (keysize != CURVE25519_KEYSIZE) {
  19893. ret = WOLFSSL_FATAL_ERROR;
  19894. }
  19895. }
  19896. if (ret == 0) {
  19897. ret = wc_curve25519_make_key(&rng, keysize, &key);
  19898. }
  19899. }
  19900. /*test bad cases*/
  19901. if (ret == 0) {
  19902. ret = wc_curve25519_make_key(NULL, 0, NULL);
  19903. if (ret == BAD_FUNC_ARG) {
  19904. ret = 0;
  19905. }
  19906. }
  19907. if (ret == 0) {
  19908. ret = wc_curve25519_make_key(&rng, keysize, NULL);
  19909. if (ret == BAD_FUNC_ARG) {
  19910. ret = 0;
  19911. }
  19912. }
  19913. if (ret == 0) {
  19914. ret = wc_curve25519_make_key(NULL, keysize, &key);
  19915. if (ret == BAD_FUNC_ARG) {
  19916. ret = 0;
  19917. }
  19918. }
  19919. if (ret == 0) {
  19920. ret = wc_curve25519_make_key(&rng, 0, &key);
  19921. if (ret == ECC_BAD_ARG_E) {
  19922. ret = 0;
  19923. }
  19924. }
  19925. wc_curve25519_free(&key);
  19926. wc_FreeRng(&rng);
  19927. res = TEST_RES_CHECK(ret == 0);
  19928. #endif
  19929. return res;
  19930. } /*END test_wc_curve25519_make_key*/
  19931. /*
  19932. * Testing wc_curve25519_shared_secret_ex
  19933. */
  19934. static int test_wc_curve25519_shared_secret_ex(void)
  19935. {
  19936. int res = TEST_SKIPPED;
  19937. #if defined(HAVE_CURVE25519)
  19938. WC_RNG rng;
  19939. curve25519_key private_key, public_key;
  19940. byte out[CURVE25519_KEYSIZE];
  19941. word32 outLen = sizeof(out);
  19942. int endian = EC25519_BIG_ENDIAN;
  19943. int ret;
  19944. ret = wc_curve25519_init(&private_key);
  19945. if (ret == 0) {
  19946. ret = wc_curve25519_init(&public_key);
  19947. }
  19948. if (ret == 0) {
  19949. ret = wc_InitRng(&rng);
  19950. }
  19951. if (ret == 0) {
  19952. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key);
  19953. }
  19954. if (ret == 0) {
  19955. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key);
  19956. }
  19957. if (ret == 0) {
  19958. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19959. &outLen, endian);
  19960. }
  19961. /*test bad cases*/
  19962. if (ret == 0) {
  19963. ret = wc_curve25519_shared_secret_ex(NULL, NULL, NULL,
  19964. 0, endian);
  19965. if (ret == 0) {
  19966. ret = -1;
  19967. }
  19968. if (ret == BAD_FUNC_ARG) {
  19969. ret = 0;
  19970. }
  19971. }
  19972. if (ret == 0) {
  19973. ret = wc_curve25519_shared_secret_ex(NULL, &public_key, out,
  19974. &outLen, endian);
  19975. if (ret == 0) {
  19976. ret = -1;
  19977. }
  19978. else if (ret == BAD_FUNC_ARG) {
  19979. ret = 0;
  19980. }
  19981. }
  19982. if (ret == 0) {
  19983. ret = wc_curve25519_shared_secret_ex(&private_key, NULL, out,
  19984. &outLen, endian);
  19985. if (ret == 0) {
  19986. ret = -1;
  19987. }
  19988. else if (ret == BAD_FUNC_ARG) {
  19989. ret = 0;
  19990. }
  19991. }
  19992. if (ret == 0) {
  19993. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  19994. &outLen, endian);
  19995. if (ret == 0) {
  19996. ret = -1;
  19997. }
  19998. else if (ret == BAD_FUNC_ARG) {
  19999. ret = 0;
  20000. }
  20001. }
  20002. if (ret == 0) {
  20003. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20004. NULL, endian);
  20005. if (ret == 0) {
  20006. ret = -1;
  20007. }
  20008. else if (ret == BAD_FUNC_ARG) {
  20009. ret = 0;
  20010. }
  20011. }
  20012. if (ret == 0) {
  20013. /*curve25519.c is checking for public_key size less than or equal to 0x7f,
  20014. *increasing to 0x8f checks for error being returned*/
  20015. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  20016. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20017. &outLen, endian);
  20018. if (ret == 0) {
  20019. ret = -1;
  20020. }
  20021. else if (ret == ECC_BAD_ARG_E) {
  20022. ret = 0;
  20023. }
  20024. }
  20025. outLen = outLen - 2;
  20026. if (ret == 0) {
  20027. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20028. &outLen, endian);
  20029. if (ret == 0) {
  20030. ret = -1;
  20031. }
  20032. else if (ret == BAD_FUNC_ARG) {
  20033. ret = 0;
  20034. }
  20035. }
  20036. wc_curve25519_free(&private_key);
  20037. wc_curve25519_free(&public_key);
  20038. wc_FreeRng(&rng);
  20039. res = TEST_RES_CHECK(ret == 0);
  20040. #endif
  20041. return res;
  20042. } /*END test_wc_curve25519_shared_secret_ex*/
  20043. /*
  20044. * Testing wc_curve25519_make_pub
  20045. */
  20046. static int test_wc_curve25519_make_pub(void)
  20047. {
  20048. int res = TEST_SKIPPED;
  20049. #ifdef HAVE_CURVE25519
  20050. WC_RNG rng;
  20051. curve25519_key key;
  20052. byte out[CURVE25519_KEYSIZE];
  20053. int ret;
  20054. ret = wc_curve25519_init(&key);
  20055. if (ret == 0) {
  20056. ret = wc_InitRng(&rng);
  20057. if (ret == 0) {
  20058. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20059. }
  20060. }
  20061. if (ret == 0) {
  20062. ret = wc_curve25519_make_pub((int)sizeof(out), out, (int)sizeof(key.k), key.k);
  20063. }
  20064. /*test bad cases*/
  20065. if (ret == 0) {
  20066. ret = wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k, (int)sizeof out, out);
  20067. if (ret == ECC_BAD_ARG_E) {
  20068. ret = 0;
  20069. }
  20070. }
  20071. if (ret == 0) {
  20072. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), NULL);
  20073. if (ret == ECC_BAD_ARG_E) {
  20074. ret = 0;
  20075. }
  20076. }
  20077. if (ret == 0) {
  20078. ret = wc_curve25519_make_pub((int)sizeof out - 1, out, (int)sizeof(key.k), key.k);
  20079. if (ret == ECC_BAD_ARG_E) {
  20080. ret = 0;
  20081. }
  20082. }
  20083. if (ret == 0) {
  20084. ret = wc_curve25519_make_pub((int)sizeof out, NULL, (int)sizeof(key.k), key.k);
  20085. if (ret == ECC_BAD_ARG_E) {
  20086. ret = 0;
  20087. }
  20088. }
  20089. if (ret == 0) {
  20090. /* verify clamping test */
  20091. key.k[0] |= ~248;
  20092. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), key.k);
  20093. if (ret == ECC_BAD_ARG_E) {
  20094. ret = 0;
  20095. }
  20096. key.k[0] &= 248;
  20097. }
  20098. /* repeat the expected-to-succeed test. */
  20099. if (ret == 0) {
  20100. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), key.k);
  20101. }
  20102. wc_curve25519_free(&key);
  20103. wc_FreeRng(&rng);
  20104. res = TEST_RES_CHECK(ret == 0);
  20105. #endif
  20106. return res;
  20107. } /*END test_wc_curve25519_make_pub */
  20108. /*
  20109. * Testing test_wc_curve25519_export_public_ex
  20110. */
  20111. static int test_wc_curve25519_export_public_ex(void)
  20112. {
  20113. int res = TEST_SKIPPED;
  20114. #if defined(HAVE_CURVE25519)
  20115. WC_RNG rng;
  20116. curve25519_key key;
  20117. byte out[CURVE25519_KEYSIZE];
  20118. word32 outLen = sizeof(out);
  20119. int endian = EC25519_BIG_ENDIAN;
  20120. int ret;
  20121. ret = wc_curve25519_init(&key);
  20122. if (ret == 0) {
  20123. ret = wc_InitRng(&rng);
  20124. }
  20125. if (ret == 0) {
  20126. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20127. if (ret == 0) {
  20128. ret = wc_curve25519_export_public(&key, out, &outLen);
  20129. }
  20130. if (ret == 0) {
  20131. ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
  20132. }
  20133. }
  20134. /*test bad cases*/
  20135. if (ret == 0) {
  20136. ret = wc_curve25519_export_public_ex(NULL, NULL, NULL, endian);
  20137. if (ret == BAD_FUNC_ARG) {
  20138. ret = 0;
  20139. }
  20140. }
  20141. if (ret == 0) {
  20142. ret = wc_curve25519_export_public_ex(NULL, out, &outLen, endian);
  20143. if (ret == BAD_FUNC_ARG) {
  20144. ret = 0;
  20145. }
  20146. }
  20147. if (ret == 0) {
  20148. ret = wc_curve25519_export_public_ex(&key, NULL, &outLen, endian);
  20149. if (ret == BAD_FUNC_ARG) {
  20150. ret = 0;
  20151. }
  20152. }
  20153. if (ret == 0) {
  20154. ret = wc_curve25519_export_public_ex(&key, out, NULL, endian);
  20155. if (ret == BAD_FUNC_ARG) {
  20156. ret = 0;
  20157. }
  20158. }
  20159. outLen = outLen - 2;
  20160. if (ret == 0) {
  20161. ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
  20162. if (ret == ECC_BAD_ARG_E) {
  20163. ret = 0;
  20164. }
  20165. }
  20166. wc_curve25519_free(&key);
  20167. wc_FreeRng(&rng);
  20168. res = TEST_RES_CHECK(ret == 0);
  20169. #endif
  20170. return res;
  20171. } /*END test_wc_curve25519_export_public_ex*/
  20172. /*
  20173. * Testing test_wc_curve25519_import_private_raw_ex
  20174. */
  20175. static int test_wc_curve25519_import_private_raw_ex(void)
  20176. {
  20177. int res = TEST_SKIPPED;
  20178. #if defined(HAVE_CURVE25519)
  20179. WC_RNG rng;
  20180. curve25519_key key;
  20181. byte priv[CURVE25519_KEYSIZE];
  20182. byte pub[CURVE25519_KEYSIZE];
  20183. word32 privSz = sizeof(priv);
  20184. word32 pubSz = sizeof(pub);
  20185. int endian = EC25519_BIG_ENDIAN;
  20186. int ret;
  20187. ret = wc_curve25519_init(&key);
  20188. if (ret == 0) {
  20189. ret = wc_InitRng(&rng);
  20190. }
  20191. if (ret == 0) {
  20192. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20193. if (ret == 0) {
  20194. ret = wc_curve25519_export_private_raw_ex(&key, priv, &privSz, endian);
  20195. }
  20196. if (ret == 0) {
  20197. ret = wc_curve25519_export_public(&key, pub, &pubSz);
  20198. }
  20199. if (ret == 0) {
  20200. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20201. &key, endian);
  20202. }
  20203. }
  20204. /*test bad cases*/
  20205. if (ret == 0) {
  20206. ret = wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  20207. endian);
  20208. if (ret == BAD_FUNC_ARG) {
  20209. ret = 0;
  20210. }
  20211. }
  20212. if (ret == 0) {
  20213. ret = wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  20214. &key, endian);
  20215. if (ret == BAD_FUNC_ARG) {
  20216. ret = 0;
  20217. }
  20218. }
  20219. if (ret == 0) {
  20220. ret = wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  20221. &key, endian);
  20222. if (ret == BAD_FUNC_ARG) {
  20223. ret = 0;
  20224. }
  20225. }
  20226. if (ret == 0) {
  20227. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20228. NULL, endian);
  20229. if (ret == BAD_FUNC_ARG) {
  20230. ret = 0;
  20231. }
  20232. }
  20233. if (ret == 0) {
  20234. ret = wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  20235. &key, endian);
  20236. if (ret == ECC_BAD_ARG_E) {
  20237. ret = 0;
  20238. }
  20239. }
  20240. if (ret == 0) {
  20241. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  20242. &key, endian);
  20243. if (ret == ECC_BAD_ARG_E) {
  20244. ret = 0;
  20245. }
  20246. }
  20247. if (ret == 0) {
  20248. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20249. &key, EC25519_LITTLE_ENDIAN);
  20250. }
  20251. wc_curve25519_free(&key);
  20252. wc_FreeRng(&rng);
  20253. res = TEST_RES_CHECK(ret == 0);
  20254. #endif
  20255. return res;
  20256. } /*END test_wc_curve25519_import_private_raw_ex*/
  20257. /*
  20258. * Testing test_wc_curve25519_import_private
  20259. */
  20260. static int test_wc_curve25519_import_private(void)
  20261. {
  20262. int res = TEST_SKIPPED;
  20263. #if defined(HAVE_CURVE25519)
  20264. curve25519_key key;
  20265. WC_RNG rng;
  20266. byte priv[CURVE25519_KEYSIZE];
  20267. word32 privSz = sizeof(priv);
  20268. int ret;
  20269. ret = wc_curve25519_init(&key);
  20270. if (ret == 0) {
  20271. ret = wc_InitRng(&rng);
  20272. }
  20273. if (ret == 0) {
  20274. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20275. if (ret == 0) {
  20276. ret = wc_curve25519_export_private_raw(&key, priv, &privSz);
  20277. }
  20278. }
  20279. if (ret == 0) {
  20280. ret = wc_curve25519_import_private(priv, privSz, &key);
  20281. }
  20282. wc_curve25519_free(&key);
  20283. wc_FreeRng(&rng);
  20284. res = TEST_RES_CHECK(ret == 0);
  20285. #endif
  20286. return res;
  20287. } /*END test_wc_curve25519_import*/
  20288. /*
  20289. * Testing test_wc_curve25519_export_private_raw_ex
  20290. */
  20291. static int test_wc_curve25519_export_private_raw_ex(void)
  20292. {
  20293. int res = TEST_SKIPPED;
  20294. #if defined(HAVE_CURVE25519)
  20295. curve25519_key key;
  20296. byte out[CURVE25519_KEYSIZE];
  20297. word32 outLen = sizeof(out);
  20298. int endian = EC25519_BIG_ENDIAN;
  20299. int ret;
  20300. ret = wc_curve25519_init(&key);
  20301. if (ret == 0) {
  20302. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
  20303. }
  20304. /*test bad cases*/
  20305. if (ret == 0) {
  20306. ret = wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian);
  20307. if (ret == BAD_FUNC_ARG) {
  20308. ret = 0;
  20309. }
  20310. }
  20311. if (ret == 0) {
  20312. ret = wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian);
  20313. if (ret == BAD_FUNC_ARG) {
  20314. ret = 0;
  20315. }
  20316. }
  20317. if (ret == 0) {
  20318. ret = wc_curve25519_export_private_raw_ex(&key, NULL, &outLen, endian);
  20319. if (ret == BAD_FUNC_ARG) {
  20320. ret = 0;
  20321. }
  20322. }
  20323. if (ret == 0) {
  20324. ret = wc_curve25519_export_private_raw_ex(&key, out, NULL, endian);
  20325. if (ret == BAD_FUNC_ARG) {
  20326. ret = 0;
  20327. }
  20328. }
  20329. if (ret == 0) {
  20330. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  20331. EC25519_LITTLE_ENDIAN);
  20332. }
  20333. outLen = outLen - 2;
  20334. if (ret == 0) {
  20335. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
  20336. if (ret == ECC_BAD_ARG_E) {
  20337. ret = 0;
  20338. }
  20339. }
  20340. wc_curve25519_free(&key);
  20341. res = TEST_RES_CHECK(ret == 0);
  20342. #endif
  20343. return res;
  20344. }/*END test_wc_curve25519_export_private_raw_ex*/
  20345. /*
  20346. * Testing wc_ed448_make_key().
  20347. */
  20348. static int test_wc_ed448_make_key(void)
  20349. {
  20350. int res = TEST_SKIPPED;
  20351. #if defined(HAVE_ED448)
  20352. ed448_key key;
  20353. WC_RNG rng;
  20354. unsigned char pubkey[ED448_PUB_KEY_SIZE];
  20355. int ret;
  20356. ret = wc_InitRng(&rng);
  20357. if (ret == 0) {
  20358. ret = wc_ed448_init(&key);
  20359. }
  20360. if (ret == 0) {
  20361. ret = wc_ed448_make_public(&key, pubkey, sizeof(pubkey));
  20362. if (ret == ECC_PRIV_KEY_E) {
  20363. ret = 0;
  20364. }
  20365. else if (ret == 0) {
  20366. ret = -1;
  20367. }
  20368. }
  20369. if (ret == 0) {
  20370. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20371. }
  20372. /* Test bad args. */
  20373. if (ret == 0) {
  20374. ret = wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key);
  20375. if (ret == BAD_FUNC_ARG) {
  20376. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL);
  20377. }
  20378. if (ret == BAD_FUNC_ARG) {
  20379. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key);
  20380. }
  20381. if (ret == BAD_FUNC_ARG) {
  20382. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key);
  20383. }
  20384. if (ret == BAD_FUNC_ARG) {
  20385. ret = 0;
  20386. }
  20387. else if (ret == 0) {
  20388. ret = WOLFSSL_FATAL_ERROR;
  20389. }
  20390. }
  20391. if (wc_FreeRng(&rng) && ret == 0) {
  20392. ret = WOLFSSL_FATAL_ERROR;
  20393. }
  20394. wc_ed448_free(&key);
  20395. res = TEST_RES_CHECK(ret == 0);
  20396. #endif
  20397. return res;
  20398. } /* END test_wc_ed448_make_key */
  20399. /*
  20400. * Testing wc_ed448_init()
  20401. */
  20402. static int test_wc_ed448_init(void)
  20403. {
  20404. int res = TEST_SKIPPED;
  20405. #if defined(HAVE_ED448)
  20406. ed448_key key;
  20407. int ret;
  20408. ret = wc_ed448_init(&key);
  20409. /* Test bad args. */
  20410. if (ret == 0) {
  20411. ret = wc_ed448_init(NULL);
  20412. if (ret == BAD_FUNC_ARG) {
  20413. ret = 0;
  20414. }
  20415. else if (ret == 0) {
  20416. ret = WOLFSSL_FATAL_ERROR;
  20417. }
  20418. }
  20419. wc_ed448_free(&key);
  20420. res = TEST_RES_CHECK(ret == 0);
  20421. #endif
  20422. return res;
  20423. } /* END test_wc_ed448_init */
  20424. /*
  20425. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  20426. */
  20427. static int test_wc_ed448_sign_msg(void)
  20428. {
  20429. int res = TEST_SKIPPED;
  20430. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  20431. WC_RNG rng;
  20432. ed448_key key;
  20433. byte msg[] = "Everybody gets Friday off.\n";
  20434. byte sig[ED448_SIG_SIZE];
  20435. word32 msglen = sizeof(msg);
  20436. word32 siglen = sizeof(sig);
  20437. word32 badSigLen = sizeof(sig) - 1;
  20438. #ifdef HAVE_ED448_VERIFY
  20439. int verify_ok = 0; /*1 = Verify success.*/
  20440. #endif
  20441. int ret;
  20442. /* Initialize stack variables. */
  20443. XMEMSET(sig, 0, siglen);
  20444. /* Initialize key. */
  20445. ret = wc_InitRng(&rng);
  20446. if (ret == 0) {
  20447. ret = wc_ed448_init(&key);
  20448. if (ret == 0) {
  20449. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20450. }
  20451. }
  20452. if (ret == 0) {
  20453. ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0);
  20454. }
  20455. /* Test bad args. */
  20456. if (ret == 0 && siglen == ED448_SIG_SIZE) {
  20457. ret = wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0);
  20458. if (ret == BAD_FUNC_ARG) {
  20459. ret = wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0);
  20460. }
  20461. if (ret == BAD_FUNC_ARG) {
  20462. ret = wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0);
  20463. }
  20464. if (ret == BAD_FUNC_ARG) {
  20465. ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0);
  20466. }
  20467. if (ret == BAD_FUNC_ARG) {
  20468. ret = wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key,
  20469. NULL, 0);
  20470. }
  20471. if (ret == BUFFER_E && badSigLen == ED448_SIG_SIZE) {
  20472. badSigLen -= 1;
  20473. ret = 0;
  20474. }
  20475. else if (ret == 0) {
  20476. ret = WOLFSSL_FATAL_ERROR;
  20477. }
  20478. } /* END sign */
  20479. #ifdef HAVE_ED448_VERIFY
  20480. if (ret == 0) {
  20481. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  20482. &key, NULL, 0);
  20483. if (ret == 0 && verify_ok == 1) {
  20484. ret = 0;
  20485. }
  20486. else if (ret == 0) {
  20487. ret = WOLFSSL_FATAL_ERROR;
  20488. }
  20489. /* Test bad args. */
  20490. if (ret == 0) {
  20491. AssertIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg,
  20492. msglen, &verify_ok, &key, NULL, 0), BAD_FUNC_ARG);
  20493. AssertIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg,
  20494. msglen, &verify_ok, &key, NULL, 0), BAD_FUNC_ARG);
  20495. ret = wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  20496. &key, NULL, 0);
  20497. if (ret == BAD_FUNC_ARG) {
  20498. ret = wc_ed448_verify_msg(sig, siglen, NULL, msglen,
  20499. &verify_ok, &key, NULL, 0);
  20500. }
  20501. if (ret == BAD_FUNC_ARG) {
  20502. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
  20503. NULL, &key, NULL, 0);
  20504. }
  20505. if (ret == BAD_FUNC_ARG) {
  20506. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
  20507. &verify_ok, NULL, NULL, 0);
  20508. }
  20509. if (ret == BAD_FUNC_ARG) {
  20510. ret = wc_ed448_verify_msg(sig, badSigLen, msg, msglen,
  20511. &verify_ok, &key, NULL, 0);
  20512. }
  20513. if (ret == BAD_FUNC_ARG) {
  20514. ret = 0;
  20515. }
  20516. else if (ret == 0) {
  20517. ret = WOLFSSL_FATAL_ERROR;
  20518. }
  20519. }
  20520. } /* END verify. */
  20521. #endif /* Verify. */
  20522. if (wc_FreeRng(&rng) && ret == 0) {
  20523. ret = WOLFSSL_FATAL_ERROR;
  20524. }
  20525. wc_ed448_free(&key);
  20526. res = TEST_RES_CHECK(ret == 0);
  20527. #endif
  20528. return res;
  20529. } /* END test_wc_ed448_sign_msg */
  20530. /*
  20531. * Testing wc_ed448_import_public()
  20532. */
  20533. static int test_wc_ed448_import_public(void)
  20534. {
  20535. int res = TEST_SKIPPED;
  20536. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20537. WC_RNG rng;
  20538. ed448_key pubKey;
  20539. const byte in[] =
  20540. "Ed448PublicKeyUnitTest.................................\n";
  20541. word32 inlen = sizeof(in);
  20542. int ret = 0;
  20543. ret = wc_InitRng(&rng);
  20544. if (ret == 0) {
  20545. ret = wc_ed448_init(&pubKey);
  20546. if (ret == 0) {
  20547. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey);
  20548. }
  20549. }
  20550. if (ret == 0) {
  20551. ret = wc_ed448_import_public_ex(in, inlen, &pubKey, 1);
  20552. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  20553. ret = 0;
  20554. }
  20555. else {
  20556. ret = WOLFSSL_FATAL_ERROR;
  20557. }
  20558. /* Test bad args. */
  20559. if (ret == 0) {
  20560. ret = wc_ed448_import_public(NULL, inlen, &pubKey);
  20561. if (ret == BAD_FUNC_ARG) {
  20562. ret = wc_ed448_import_public(in, inlen, NULL);
  20563. }
  20564. if (ret == BAD_FUNC_ARG) {
  20565. ret = wc_ed448_import_public(in, inlen - 1, &pubKey);
  20566. }
  20567. if (ret == BAD_FUNC_ARG) {
  20568. ret = 0;
  20569. }
  20570. else if (ret == 0) {
  20571. ret = WOLFSSL_FATAL_ERROR;
  20572. }
  20573. }
  20574. }
  20575. if (wc_FreeRng(&rng) && ret == 0) {
  20576. ret = WOLFSSL_FATAL_ERROR;
  20577. }
  20578. wc_ed448_free(&pubKey);
  20579. res = TEST_RES_CHECK(ret == 0);
  20580. #endif
  20581. return res;
  20582. } /* END wc_ed448_import_public */
  20583. /*
  20584. * Testing wc_ed448_import_private_key()
  20585. */
  20586. static int test_wc_ed448_import_private_key(void)
  20587. {
  20588. int res = TEST_SKIPPED;
  20589. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20590. WC_RNG rng;
  20591. ed448_key key;
  20592. const byte privKey[] =
  20593. "Ed448PrivateKeyUnitTest................................\n";
  20594. const byte pubKey[] =
  20595. "Ed448PublicKeyUnitTest.................................\n";
  20596. word32 privKeySz = sizeof(privKey);
  20597. word32 pubKeySz = sizeof(pubKey);
  20598. #ifdef HAVE_ED448_KEY_EXPORT
  20599. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  20600. word32 bothKeysSz = sizeof(bothKeys);
  20601. #endif
  20602. int ret;
  20603. ret = wc_InitRng(&rng);
  20604. if (ret != 0) {
  20605. return TEST_FAIL;
  20606. }
  20607. ret = wc_ed448_init(&key);
  20608. if (ret != 0) {
  20609. wc_FreeRng(&rng);
  20610. return TEST_FAIL;
  20611. }
  20612. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20613. if (ret == 0) {
  20614. ret = wc_ed448_import_private_key_ex(privKey, privKeySz, pubKey,
  20615. pubKeySz, &key, 1);
  20616. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
  20617. XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  20618. ret = WOLFSSL_FATAL_ERROR;
  20619. }
  20620. }
  20621. #ifdef HAVE_ED448_KEY_EXPORT
  20622. if (ret == 0)
  20623. ret = wc_ed448_export_private(&key, bothKeys, &bothKeysSz);
  20624. if (ret == 0) {
  20625. ret = wc_ed448_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  20626. &key, 1);
  20627. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
  20628. XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  20629. ret = WOLFSSL_FATAL_ERROR;
  20630. }
  20631. }
  20632. #endif
  20633. /* Test bad args. */
  20634. if (ret == 0) {
  20635. ret = wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  20636. &key);
  20637. if (ret == BAD_FUNC_ARG) {
  20638. ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
  20639. pubKeySz, &key);
  20640. }
  20641. if (ret == BAD_FUNC_ARG) {
  20642. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20643. pubKeySz, NULL);
  20644. }
  20645. if (ret == BAD_FUNC_ARG) {
  20646. ret = wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  20647. pubKeySz, &key);
  20648. }
  20649. if (ret == BAD_FUNC_ARG) {
  20650. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20651. pubKeySz - 1, &key);
  20652. }
  20653. if (ret == BAD_FUNC_ARG) {
  20654. ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
  20655. 0, &key);
  20656. }
  20657. if (ret == BAD_FUNC_ARG) {
  20658. ret = 0;
  20659. }
  20660. else if (ret == 0) {
  20661. ret = WOLFSSL_FATAL_ERROR;
  20662. }
  20663. }
  20664. if (wc_FreeRng(&rng) && ret == 0) {
  20665. ret = WOLFSSL_FATAL_ERROR;
  20666. }
  20667. wc_ed448_free(&key);
  20668. res = TEST_RES_CHECK(ret == 0);
  20669. #endif
  20670. return res;
  20671. } /* END test_wc_ed448_import_private_key */
  20672. /*
  20673. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  20674. */
  20675. static int test_wc_ed448_export(void)
  20676. {
  20677. int res = TEST_SKIPPED;
  20678. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20679. WC_RNG rng;
  20680. ed448_key key;
  20681. byte priv[ED448_PRV_KEY_SIZE];
  20682. byte pub[ED448_PUB_KEY_SIZE];
  20683. word32 privSz = sizeof(priv);
  20684. word32 pubSz = sizeof(pub);
  20685. int ret;
  20686. ret = wc_InitRng(&rng);
  20687. if (ret != 0) {
  20688. return TEST_FAIL;
  20689. }
  20690. ret = wc_ed448_init(&key);
  20691. if (ret != 0) {
  20692. wc_FreeRng(&rng);
  20693. return TEST_FAIL;
  20694. }
  20695. if (ret == 0) {
  20696. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20697. }
  20698. if (ret == 0) {
  20699. ret = wc_ed448_export_public(&key, pub, &pubSz);
  20700. if (ret == 0 && (pubSz != ED448_KEY_SIZE ||
  20701. XMEMCMP(key.p, pub, pubSz) != 0)) {
  20702. ret = WOLFSSL_FATAL_ERROR;
  20703. }
  20704. if (ret == 0) {
  20705. ret = wc_ed448_export_public(NULL, pub, &pubSz);
  20706. if (ret == BAD_FUNC_ARG) {
  20707. ret = wc_ed448_export_public(&key, NULL, &pubSz);
  20708. }
  20709. if (ret == BAD_FUNC_ARG) {
  20710. ret = wc_ed448_export_public(&key, pub, NULL);
  20711. }
  20712. if (ret == BAD_FUNC_ARG) {
  20713. ret = 0;
  20714. }
  20715. else if (ret == 0) {
  20716. ret = WOLFSSL_FATAL_ERROR;
  20717. }
  20718. }
  20719. }
  20720. if (ret == 0) {
  20721. ret = wc_ed448_export_private_only(&key, priv, &privSz);
  20722. if (ret == 0 && (privSz != ED448_KEY_SIZE ||
  20723. XMEMCMP(key.k, priv, privSz) != 0)) {
  20724. ret = WOLFSSL_FATAL_ERROR;
  20725. }
  20726. if (ret == 0) {
  20727. ret = wc_ed448_export_private_only(NULL, priv, &privSz);
  20728. if (ret == BAD_FUNC_ARG) {
  20729. ret = wc_ed448_export_private_only(&key, NULL, &privSz);
  20730. }
  20731. if (ret == BAD_FUNC_ARG) {
  20732. ret = wc_ed448_export_private_only(&key, priv, NULL);
  20733. }
  20734. if (ret == BAD_FUNC_ARG) {
  20735. ret = 0;
  20736. }
  20737. else if (ret == 0) {
  20738. ret = WOLFSSL_FATAL_ERROR;
  20739. }
  20740. }
  20741. }
  20742. if (wc_FreeRng(&rng) && ret == 0) {
  20743. ret = WOLFSSL_FATAL_ERROR;
  20744. }
  20745. wc_ed448_free(&key);
  20746. res = TEST_RES_CHECK(ret == 0);
  20747. #endif
  20748. return res;
  20749. } /* END test_wc_ed448_export */
  20750. /*
  20751. * Testing wc_ed448_size()
  20752. */
  20753. static int test_wc_ed448_size(void)
  20754. {
  20755. int res = TEST_SKIPPED;
  20756. #if defined(HAVE_ED448)
  20757. WC_RNG rng;
  20758. ed448_key key;
  20759. int ret = 0;
  20760. ret = wc_InitRng(&rng);
  20761. if (ret != 0) {
  20762. return TEST_FAIL;
  20763. }
  20764. ret = wc_ed448_init(&key);
  20765. if (ret != 0) {
  20766. wc_FreeRng(&rng);
  20767. return TEST_FAIL;
  20768. }
  20769. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20770. if (ret != 0) {
  20771. wc_FreeRng(&rng);
  20772. wc_ed448_free(&key);
  20773. return TEST_FAIL;
  20774. }
  20775. ret = wc_ed448_size(&key);
  20776. /* Test bad args. */
  20777. if (ret == ED448_KEY_SIZE) {
  20778. ret = wc_ed448_size(NULL);
  20779. if (ret == BAD_FUNC_ARG) {
  20780. ret = 0;
  20781. }
  20782. }
  20783. if (ret == 0) {
  20784. ret = wc_ed448_sig_size(&key);
  20785. if (ret == ED448_SIG_SIZE) {
  20786. ret = 0;
  20787. }
  20788. /* Test bad args. */
  20789. if (ret == 0) {
  20790. ret = wc_ed448_sig_size(NULL);
  20791. if (ret == BAD_FUNC_ARG) {
  20792. ret = 0;
  20793. }
  20794. }
  20795. } /* END wc_ed448_sig_size() */
  20796. if (ret == 0) {
  20797. ret = wc_ed448_pub_size(&key);
  20798. if (ret == ED448_PUB_KEY_SIZE) {
  20799. ret = 0;
  20800. }
  20801. if (ret == 0) {
  20802. ret = wc_ed448_pub_size(NULL);
  20803. if (ret == BAD_FUNC_ARG) {
  20804. ret = 0;
  20805. }
  20806. }
  20807. } /* END wc_ed448_pub_size */
  20808. if (ret == 0) {
  20809. ret = wc_ed448_priv_size(&key);
  20810. if (ret == ED448_PRV_KEY_SIZE) {
  20811. ret = 0;
  20812. }
  20813. if (ret == 0) {
  20814. ret = wc_ed448_priv_size(NULL);
  20815. if (ret == BAD_FUNC_ARG) {
  20816. ret = 0;
  20817. }
  20818. }
  20819. } /* END wc_ed448_pub_size */
  20820. if (wc_FreeRng(&rng) && ret == 0) {
  20821. ret = WOLFSSL_FATAL_ERROR;
  20822. }
  20823. wc_ed448_free(&key);
  20824. res = TEST_RES_CHECK(ret == 0);
  20825. #endif
  20826. return res;
  20827. } /* END test_wc_ed448_size */
  20828. /*
  20829. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  20830. */
  20831. static int test_wc_ed448_exportKey(void)
  20832. {
  20833. int res = TEST_SKIPPED;
  20834. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20835. WC_RNG rng;
  20836. ed448_key key;
  20837. byte priv[ED448_PRV_KEY_SIZE];
  20838. byte pub[ED448_PUB_KEY_SIZE];
  20839. byte privOnly[ED448_PRV_KEY_SIZE];
  20840. word32 privSz = sizeof(priv);
  20841. word32 pubSz = sizeof(pub);
  20842. word32 privOnlySz = sizeof(privOnly);
  20843. int ret;
  20844. ret = wc_InitRng(&rng);
  20845. if (ret != 0) {
  20846. return TEST_FAIL;
  20847. }
  20848. ret = wc_ed448_init(&key);
  20849. if (ret != 0) {
  20850. wc_FreeRng(&rng);
  20851. return TEST_FAIL;
  20852. }
  20853. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20854. if (ret != 0) {
  20855. wc_FreeRng(&rng);
  20856. wc_ed448_free(&key);
  20857. return TEST_FAIL;
  20858. }
  20859. ret = wc_ed448_export_private(&key, privOnly, &privOnlySz);
  20860. if (ret == 0) {
  20861. ret = wc_ed448_export_private(NULL, privOnly, &privOnlySz);
  20862. if (ret == BAD_FUNC_ARG) {
  20863. ret = wc_ed448_export_private(&key, NULL, &privOnlySz);
  20864. }
  20865. if (ret == BAD_FUNC_ARG) {
  20866. ret = wc_ed448_export_private(&key, privOnly, NULL);
  20867. }
  20868. if (ret == BAD_FUNC_ARG) {
  20869. ret = 0;
  20870. }
  20871. else if (ret == 0) {
  20872. ret = WOLFSSL_FATAL_ERROR;
  20873. }
  20874. }
  20875. if (ret == 0) {
  20876. ret = wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz);
  20877. if (ret == 0) {
  20878. ret = wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz);
  20879. if (ret == BAD_FUNC_ARG) {
  20880. ret = wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz);
  20881. }
  20882. if (ret == BAD_FUNC_ARG) {
  20883. ret = wc_ed448_export_key(&key, priv, NULL, pub, &pubSz);
  20884. }
  20885. if (ret == BAD_FUNC_ARG) {
  20886. ret = wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz);
  20887. }
  20888. if (ret == BAD_FUNC_ARG) {
  20889. ret = wc_ed448_export_key(&key, priv, &privSz, pub, NULL);
  20890. }
  20891. if (ret == BAD_FUNC_ARG) {
  20892. ret = 0;
  20893. }
  20894. else if (ret == 0) {
  20895. ret = WOLFSSL_FATAL_ERROR;
  20896. }
  20897. }
  20898. } /* END wc_ed448_export_key() */
  20899. /* Cross check output. */
  20900. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  20901. ret = WOLFSSL_FATAL_ERROR;
  20902. }
  20903. if (wc_FreeRng(&rng) && ret == 0) {
  20904. ret = WOLFSSL_FATAL_ERROR;
  20905. }
  20906. wc_ed448_free(&key);
  20907. res = TEST_RES_CHECK(ret == 0);
  20908. #endif
  20909. return res;
  20910. } /* END test_wc_ed448_exportKey */
  20911. /*
  20912. * Testing wc_Ed448PublicKeyToDer
  20913. */
  20914. static int test_wc_Ed448PublicKeyToDer(void)
  20915. {
  20916. int res = TEST_SKIPPED;
  20917. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  20918. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20919. int tmp;
  20920. ed448_key key;
  20921. byte derBuf[1024];
  20922. int ret = 0;
  20923. /* Test bad args */
  20924. tmp = wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0);
  20925. if (tmp != BAD_FUNC_ARG) {
  20926. ret = WOLFSSL_FATAL_ERROR;
  20927. }
  20928. if (ret == 0) {
  20929. wc_ed448_init(&key);
  20930. tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0);
  20931. if (tmp != BUFFER_E) {
  20932. ret = WOLFSSL_FATAL_ERROR;
  20933. }
  20934. wc_ed448_free(&key);
  20935. }
  20936. /* Test good args */
  20937. if (ret == 0) {
  20938. WC_RNG rng;
  20939. ret = wc_InitRng(&rng);
  20940. if (ret != 0) {
  20941. return TEST_FAIL;
  20942. }
  20943. ret = wc_ed448_init(&key);
  20944. if (ret != 0) {
  20945. wc_FreeRng(&rng);
  20946. return TEST_FAIL;
  20947. }
  20948. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20949. if (ret != 0) {
  20950. wc_FreeRng(&rng);
  20951. wc_ed448_free(&key);
  20952. return TEST_FAIL;
  20953. }
  20954. tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1);
  20955. if (tmp <= 0) {
  20956. ret = WOLFSSL_FATAL_ERROR;
  20957. }
  20958. wc_FreeRng(&rng);
  20959. wc_ed448_free(&key);
  20960. }
  20961. res = TEST_RES_CHECK(ret == 0);
  20962. #endif
  20963. return res;
  20964. } /* END testing wc_Ed448PublicKeyToDer */
  20965. /*
  20966. * Testing wc_curve448_init and wc_curve448_free.
  20967. */
  20968. static int test_wc_curve448_init(void)
  20969. {
  20970. int res = TEST_SKIPPED;
  20971. #if defined(HAVE_CURVE448)
  20972. curve448_key key;
  20973. int ret = 0;
  20974. ret = wc_curve448_init(&key);
  20975. /* Test bad args for wc_curve448_init */
  20976. if (ret == 0) {
  20977. ret = wc_curve448_init(NULL);
  20978. if (ret == BAD_FUNC_ARG) {
  20979. ret = 0;
  20980. }
  20981. else if (ret == 0) {
  20982. ret = WOLFSSL_FATAL_ERROR;
  20983. }
  20984. }
  20985. /* Test good args for wc_curve_448_free */
  20986. wc_curve448_free(&key);
  20987. wc_curve448_free(NULL);
  20988. res = TEST_RES_CHECK(ret == 0);
  20989. #endif
  20990. return res;
  20991. } /* END test_wc_curve448_init and wc_curve_448_free*/
  20992. /*
  20993. * Testing wc_curve448_make_key
  20994. */
  20995. static int test_wc_curve448_make_key(void)
  20996. {
  20997. int res = TEST_SKIPPED;
  20998. #if defined(HAVE_CURVE448)
  20999. WC_RNG rng;
  21000. curve448_key key;
  21001. int keysize;
  21002. int ret;
  21003. ret = wc_curve448_init(&key);
  21004. if (ret == 0) {
  21005. ret = wc_InitRng(&rng);
  21006. }
  21007. if (ret == 0) {
  21008. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  21009. if (ret == 0) {
  21010. keysize = wc_curve448_size(&key);
  21011. if (keysize != CURVE448_KEY_SIZE) {
  21012. ret = WOLFSSL_FATAL_ERROR;
  21013. }
  21014. }
  21015. if (ret == 0) {
  21016. ret = wc_curve448_make_key(&rng, keysize, &key);
  21017. }
  21018. }
  21019. /* test bad cases */
  21020. if (ret == 0) {
  21021. ret = wc_curve448_make_key(NULL, 0, NULL);
  21022. if (ret == BAD_FUNC_ARG) {
  21023. ret = 0;
  21024. }
  21025. }
  21026. if (ret == 0) {
  21027. ret = wc_curve448_make_key(&rng, keysize, NULL);
  21028. if (ret == BAD_FUNC_ARG) {
  21029. ret = 0;
  21030. }
  21031. }
  21032. if (ret == 0) {
  21033. ret = wc_curve448_make_key(NULL, keysize, &key);
  21034. if (ret == BAD_FUNC_ARG) {
  21035. ret = 0;
  21036. }
  21037. }
  21038. if (ret == 0) {
  21039. ret = wc_curve448_make_key(&rng, 0, &key);
  21040. if (ret == ECC_BAD_ARG_E) {
  21041. ret = 0;
  21042. }
  21043. }
  21044. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  21045. ret = WOLFSSL_FATAL_ERROR;
  21046. }
  21047. wc_curve448_free(&key);
  21048. res = TEST_RES_CHECK(ret == 0);
  21049. #endif
  21050. return res;
  21051. } /*END test_wc_curve448_make_key*/
  21052. /*
  21053. * Testing test_wc_curve448_shared_secret_ex
  21054. */
  21055. static int test_wc_curve448_shared_secret_ex(void)
  21056. {
  21057. int res = TEST_SKIPPED;
  21058. #if defined(HAVE_CURVE448)
  21059. WC_RNG rng;
  21060. curve448_key private_key, public_key;
  21061. byte out[CURVE448_KEY_SIZE];
  21062. word32 outLen = sizeof(out);
  21063. int endian = EC448_BIG_ENDIAN;
  21064. int ret;
  21065. ret = wc_curve448_init(&private_key);
  21066. if (ret == 0) {
  21067. ret = wc_InitRng(&rng);
  21068. if (ret == 0) {
  21069. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key);
  21070. }
  21071. }
  21072. if (ret == 0) {
  21073. ret = wc_curve448_init(&public_key);
  21074. }
  21075. if (ret == 0) {
  21076. if (ret == 0) {
  21077. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key);
  21078. }
  21079. }
  21080. if (ret == 0) {
  21081. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21082. &outLen, endian);
  21083. }
  21084. /* test bad cases */
  21085. if (ret == 0) {
  21086. ret = wc_curve448_shared_secret_ex(NULL, NULL, NULL, 0, endian);
  21087. if (ret == BAD_FUNC_ARG) {
  21088. ret = 0;
  21089. }
  21090. }
  21091. if (ret == 0) {
  21092. ret = wc_curve448_shared_secret_ex(NULL, &public_key, out,
  21093. &outLen, endian);
  21094. if (ret == BAD_FUNC_ARG) {
  21095. ret = 0;
  21096. }
  21097. }
  21098. if (ret == 0) {
  21099. ret = wc_curve448_shared_secret_ex(&private_key, NULL, out,
  21100. &outLen, endian);
  21101. if (ret == BAD_FUNC_ARG) {
  21102. ret = 0;
  21103. }
  21104. }
  21105. if (ret == 0) {
  21106. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  21107. &outLen, endian);
  21108. if (ret == BAD_FUNC_ARG) {
  21109. ret = 0;
  21110. }
  21111. }
  21112. if (ret == 0) {
  21113. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21114. NULL, endian);
  21115. if (ret == BAD_FUNC_ARG) {
  21116. ret = 0;
  21117. }
  21118. }
  21119. outLen = outLen - 2;
  21120. if (ret == 0) {
  21121. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21122. &outLen, endian);
  21123. if (ret == BAD_FUNC_ARG) {
  21124. ret = 0;
  21125. }
  21126. }
  21127. wc_curve448_free(&private_key);
  21128. wc_curve448_free(&public_key);
  21129. wc_FreeRng(&rng);
  21130. res = TEST_RES_CHECK(ret == 0);
  21131. #endif
  21132. return res;
  21133. } /*END test_wc_curve448_shared_secret_ex*/
  21134. /*
  21135. * Testing test_wc_curve448_export_public_ex
  21136. */
  21137. static int test_wc_curve448_export_public_ex(void)
  21138. {
  21139. int res = TEST_SKIPPED;
  21140. #if defined(HAVE_CURVE448)
  21141. WC_RNG rng;
  21142. curve448_key key;
  21143. byte out[CURVE448_KEY_SIZE];
  21144. word32 outLen = sizeof(out);
  21145. int endian = EC448_BIG_ENDIAN;
  21146. int ret;
  21147. ret = wc_curve448_init(&key);
  21148. if (ret == 0) {
  21149. ret = wc_InitRng(&rng);
  21150. }
  21151. if (ret == 0) {
  21152. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  21153. if (ret == 0) {
  21154. ret = wc_curve448_export_public(&key, out, &outLen);
  21155. }
  21156. if (ret == 0) {
  21157. ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
  21158. }
  21159. }
  21160. /*test bad cases*/
  21161. if (ret == 0) {
  21162. ret = wc_curve448_export_public_ex(NULL, NULL, NULL, endian);
  21163. if (ret == BAD_FUNC_ARG) {
  21164. ret = 0;
  21165. }
  21166. }
  21167. if (ret == 0) {
  21168. ret = wc_curve448_export_public_ex(NULL, out, &outLen, endian);
  21169. if (ret == BAD_FUNC_ARG) {
  21170. ret = 0;
  21171. }
  21172. }
  21173. if (ret == 0) {
  21174. ret = wc_curve448_export_public_ex(&key, NULL, &outLen, endian);
  21175. if (ret == BAD_FUNC_ARG) {
  21176. ret = 0;
  21177. }
  21178. }
  21179. if (ret == 0) {
  21180. ret = wc_curve448_export_public_ex(&key, out, NULL, endian);
  21181. if (ret == BAD_FUNC_ARG) {
  21182. ret = 0;
  21183. }
  21184. }
  21185. outLen = outLen - 2;
  21186. if (ret == 0) {
  21187. ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
  21188. if (ret == ECC_BAD_ARG_E) {
  21189. ret = 0;
  21190. }
  21191. }
  21192. wc_curve448_free(&key);
  21193. wc_FreeRng(&rng);
  21194. res = TEST_RES_CHECK(ret == 0);
  21195. #endif
  21196. return res;
  21197. } /*END test_wc_curve448_export_public_ex*/
  21198. /*
  21199. * Testing test_wc_curve448_export_private_raw_ex
  21200. */
  21201. static int test_wc_curve448_export_private_raw_ex(void)
  21202. {
  21203. int res = TEST_SKIPPED;
  21204. #if defined(HAVE_CURVE448)
  21205. curve448_key key;
  21206. byte out[CURVE448_KEY_SIZE];
  21207. word32 outLen = sizeof(out);
  21208. int endian = EC448_BIG_ENDIAN;
  21209. int ret;
  21210. ret = wc_curve448_init(&key);
  21211. if (ret == 0) {
  21212. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
  21213. }
  21214. /*test bad cases*/
  21215. if (ret == 0) {
  21216. ret = wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian);
  21217. if (ret == BAD_FUNC_ARG) {
  21218. ret = 0;
  21219. }
  21220. }
  21221. if (ret == 0) {
  21222. ret = wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian);
  21223. if (ret == BAD_FUNC_ARG) {
  21224. ret = 0;
  21225. }
  21226. }
  21227. if (ret == 0) {
  21228. ret = wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian);
  21229. if (ret == BAD_FUNC_ARG) {
  21230. ret = 0;
  21231. }
  21232. }
  21233. if (ret == 0) {
  21234. ret = wc_curve448_export_private_raw_ex(&key, out, NULL, endian);
  21235. if (ret == BAD_FUNC_ARG) {
  21236. ret = 0;
  21237. }
  21238. }
  21239. if (ret == 0) {
  21240. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen,
  21241. EC448_LITTLE_ENDIAN);
  21242. }
  21243. outLen = outLen - 2;
  21244. if (ret == 0) {
  21245. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
  21246. if (ret == ECC_BAD_ARG_E) {
  21247. ret = 0;
  21248. }
  21249. }
  21250. wc_curve448_free(&key);
  21251. res = TEST_RES_CHECK(ret == 0);
  21252. #endif
  21253. return res;
  21254. }/*END test_wc_curve448_export_private_raw_ex*/
  21255. /*
  21256. * Testing test_wc_curve448_import_private_raw_ex
  21257. */
  21258. static int test_wc_curve448_import_private_raw_ex(void)
  21259. {
  21260. int res = TEST_SKIPPED;
  21261. #if defined(HAVE_CURVE448)
  21262. WC_RNG rng;
  21263. curve448_key key;
  21264. byte priv[CURVE448_KEY_SIZE];
  21265. byte pub[CURVE448_KEY_SIZE];
  21266. word32 privSz = sizeof(priv);
  21267. word32 pubSz = sizeof(pub);
  21268. int endian = EC448_BIG_ENDIAN;
  21269. int ret;
  21270. ret = wc_curve448_init(&key);
  21271. if (ret == 0) {
  21272. ret = wc_InitRng(&rng);
  21273. }
  21274. if (ret == 0) {
  21275. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  21276. if (ret == 0) {
  21277. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  21278. }
  21279. if (ret == 0) {
  21280. ret = wc_curve448_export_public(&key, pub, &pubSz);
  21281. }
  21282. if (ret == 0) {
  21283. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21284. &key, endian);
  21285. }
  21286. }
  21287. /* test bad cases */
  21288. if (ret == 0) {
  21289. ret = wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0);
  21290. if (ret == BAD_FUNC_ARG) {
  21291. ret = 0;
  21292. }
  21293. }
  21294. if (ret == 0) {
  21295. ret = wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  21296. &key, endian);
  21297. if (ret == BAD_FUNC_ARG) {
  21298. ret = 0;
  21299. }
  21300. }
  21301. if (ret == 0) {
  21302. ret = wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  21303. &key, endian);
  21304. if (ret == BAD_FUNC_ARG) {
  21305. ret = 0;
  21306. }
  21307. }
  21308. if (ret == 0) {
  21309. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21310. NULL, endian);
  21311. if (ret == BAD_FUNC_ARG) {
  21312. ret = 0;
  21313. }
  21314. }
  21315. if (ret == 0) {
  21316. ret = wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  21317. &key, endian);
  21318. if (ret == ECC_BAD_ARG_E) {
  21319. ret = 0;
  21320. }
  21321. }
  21322. if (ret == 0) {
  21323. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  21324. &key, endian);
  21325. if (ret == ECC_BAD_ARG_E) {
  21326. ret = 0;
  21327. }
  21328. }
  21329. if (ret == 0) {
  21330. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21331. &key, EC448_LITTLE_ENDIAN);
  21332. }
  21333. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  21334. ret = WOLFSSL_FATAL_ERROR;
  21335. }
  21336. wc_curve448_free(&key);
  21337. res = TEST_RES_CHECK(ret == 0);
  21338. #endif
  21339. return res;
  21340. } /*END test_wc_curve448_import_private_raw_ex*/
  21341. /*
  21342. * Testing test_curve448_export_key_raw
  21343. */
  21344. static int test_wc_curve448_export_key_raw(void)
  21345. {
  21346. int res = TEST_SKIPPED;
  21347. #if defined(HAVE_CURVE448)
  21348. WC_RNG rng;
  21349. curve448_key key;
  21350. byte priv[CURVE448_KEY_SIZE];
  21351. byte pub[CURVE448_KEY_SIZE];
  21352. word32 privSz = sizeof(priv);
  21353. word32 pubSz = sizeof(pub);
  21354. int ret;
  21355. ret = wc_curve448_init(&key);
  21356. if (ret == 0) {
  21357. ret = wc_InitRng(&rng);
  21358. }
  21359. if (ret == 0) {
  21360. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  21361. if (ret == 0) {
  21362. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  21363. }
  21364. if (ret == 0) {
  21365. ret = wc_curve448_export_public(&key, pub, &pubSz);
  21366. }
  21367. if (ret == 0) {
  21368. ret = wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz);
  21369. }
  21370. }
  21371. wc_curve448_free(&key);
  21372. wc_FreeRng(&rng);
  21373. res = TEST_RES_CHECK(ret == 0);
  21374. #endif
  21375. return res;
  21376. }/*END test_wc_curve448_import_private_raw_ex*/
  21377. /*
  21378. * Testing test_wc_curve448_import_private
  21379. */
  21380. static int test_wc_curve448_import_private(void)
  21381. {
  21382. int res = TEST_SKIPPED;
  21383. #if defined(HAVE_CURVE448)
  21384. curve448_key key;
  21385. WC_RNG rng;
  21386. byte priv[CURVE448_KEY_SIZE];
  21387. word32 privSz = sizeof(priv);
  21388. int ret;
  21389. ret = wc_curve448_init(&key);
  21390. if (ret == 0) {
  21391. ret = wc_InitRng(&rng);
  21392. }
  21393. if (ret == 0) {
  21394. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  21395. if (ret == 0) {
  21396. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  21397. }
  21398. }
  21399. if (ret == 0) {
  21400. ret = wc_curve448_import_private(priv, privSz, &key);
  21401. }
  21402. wc_curve448_free(&key);
  21403. wc_FreeRng(&rng);
  21404. res = TEST_RES_CHECK(ret == 0);
  21405. #endif
  21406. return res;
  21407. } /*END test_wc_curve448_import*/
  21408. /*
  21409. * Testing test_wc_curve448_size.
  21410. */
  21411. static int test_wc_curve448_size(void)
  21412. {
  21413. int res = TEST_SKIPPED;
  21414. #if defined(HAVE_CURVE448)
  21415. curve448_key key;
  21416. int ret = 0;
  21417. ret = wc_curve448_init(&key);
  21418. /* Test good args for wc_curve448_size */
  21419. if (ret == 0) {
  21420. ret = wc_curve448_size(&key);
  21421. }
  21422. /* Test bad args for wc_curve448_size */
  21423. if (ret != 0) {
  21424. ret = wc_curve448_size(NULL);
  21425. }
  21426. wc_curve448_free(&key);
  21427. res = TEST_RES_CHECK(ret == 0);
  21428. #endif
  21429. return res;
  21430. } /* END test_wc_curve448_size*/
  21431. /*
  21432. * Testing wc_ecc_make_key.
  21433. */
  21434. static int test_wc_ecc_make_key(void)
  21435. {
  21436. int res = TEST_SKIPPED;
  21437. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21438. WC_RNG rng;
  21439. ecc_key key;
  21440. int ret;
  21441. ret = wc_InitRng(&rng);
  21442. if (ret != 0)
  21443. return TEST_FAIL;
  21444. ret = wc_ecc_init(&key);
  21445. if (ret == 0) {
  21446. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21447. #if defined(WOLFSSL_ASYNC_CRYPT)
  21448. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21449. #endif
  21450. /* Pass in bad args. */
  21451. if (ret == 0) {
  21452. ret = wc_ecc_make_key(NULL, KEY14, &key);
  21453. if (ret == BAD_FUNC_ARG) {
  21454. ret = wc_ecc_make_key(&rng, KEY14, NULL);
  21455. }
  21456. if (ret == BAD_FUNC_ARG) {
  21457. ret = 0;
  21458. }
  21459. else if (ret == 0) {
  21460. ret = WOLFSSL_FATAL_ERROR;
  21461. }
  21462. }
  21463. wc_ecc_free(&key);
  21464. }
  21465. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  21466. ret = WOLFSSL_FATAL_ERROR;
  21467. }
  21468. #ifdef FP_ECC
  21469. wc_ecc_fp_free();
  21470. #endif
  21471. res = TEST_RES_CHECK(ret == 0);
  21472. #endif
  21473. return res;
  21474. } /* END test_wc_ecc_make_key */
  21475. /*
  21476. * Testing wc_ecc_init()
  21477. */
  21478. static int test_wc_ecc_init(void)
  21479. {
  21480. int res = TEST_SKIPPED;
  21481. #ifdef HAVE_ECC
  21482. ecc_key key;
  21483. int ret;
  21484. ret = wc_ecc_init(&key);
  21485. /* Pass in bad args. */
  21486. if (ret == 0) {
  21487. ret = wc_ecc_init(NULL);
  21488. if (ret == BAD_FUNC_ARG) {
  21489. ret = 0;
  21490. }
  21491. else if (ret == 0) {
  21492. ret = WOLFSSL_FATAL_ERROR;
  21493. }
  21494. }
  21495. wc_ecc_free(&key);
  21496. res = TEST_RES_CHECK(ret == 0);
  21497. #endif
  21498. return res;
  21499. } /* END test_wc_ecc_init */
  21500. /*
  21501. * Testing wc_ecc_check_key()
  21502. */
  21503. static int test_wc_ecc_check_key(void)
  21504. {
  21505. int res = TEST_SKIPPED;
  21506. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21507. WC_RNG rng;
  21508. ecc_key key;
  21509. int ret;
  21510. XMEMSET(&rng, 0, sizeof(rng));
  21511. XMEMSET(&key, 0, sizeof(key));
  21512. ret = wc_InitRng(&rng);
  21513. if (ret == 0) {
  21514. ret = wc_ecc_init(&key);
  21515. if (ret == 0) {
  21516. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21517. #if defined(WOLFSSL_ASYNC_CRYPT)
  21518. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21519. #endif
  21520. }
  21521. }
  21522. if (ret == 0) {
  21523. ret = wc_ecc_check_key(&key);
  21524. }
  21525. /* Pass in bad args. */
  21526. if (ret == 0) {
  21527. ret = wc_ecc_check_key(NULL);
  21528. if (ret == BAD_FUNC_ARG) {
  21529. ret = 0;
  21530. }
  21531. else if (ret == 0) {
  21532. ret = WOLFSSL_FATAL_ERROR;
  21533. }
  21534. }
  21535. if (wc_FreeRng(&rng) && ret == 0) {
  21536. ret = WOLFSSL_FATAL_ERROR;
  21537. }
  21538. wc_ecc_free(&key);
  21539. #ifdef FP_ECC
  21540. wc_ecc_fp_free();
  21541. #endif
  21542. res = TEST_RES_CHECK(ret == 0);
  21543. #endif
  21544. return res;
  21545. } /* END test_wc_ecc_check_key */
  21546. /*
  21547. * Testing wc_ecc_get_generator()
  21548. */
  21549. static int test_wc_ecc_get_generator(void)
  21550. {
  21551. int res = TEST_SKIPPED;
  21552. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  21553. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  21554. ecc_point* pt;
  21555. int ret = 0;
  21556. pt = wc_ecc_new_point();
  21557. if (!pt) {
  21558. ret = WOLFSSL_FATAL_ERROR;
  21559. }
  21560. if (ret == 0) {
  21561. ret = wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1));
  21562. }
  21563. /* Test bad args. */
  21564. if (ret == MP_OKAY) {
  21565. /* Returns Zero for bad arg. */
  21566. ret = wc_ecc_get_generator(pt, -1);
  21567. if (ret != MP_OKAY)
  21568. wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1));
  21569. if (ret != MP_OKAY)
  21570. wc_ecc_get_generator(pt, 1000); /* If we ever get to 1000 curves
  21571. * increase this number */
  21572. if (ret != MP_OKAY)
  21573. wc_ecc_get_generator(NULL, -1);
  21574. ret = (ret == MP_OKAY) ? WOLFSSL_FATAL_ERROR : 0;
  21575. }
  21576. wc_ecc_del_point(pt);
  21577. res = TEST_RES_CHECK(ret == 0);
  21578. #endif
  21579. return res;
  21580. } /* END test_wc_ecc_get_generator */
  21581. /*
  21582. * Testing wc_ecc_size()
  21583. */
  21584. static int test_wc_ecc_size(void)
  21585. {
  21586. int res = TEST_SKIPPED;
  21587. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21588. WC_RNG rng;
  21589. ecc_key key;
  21590. int ret;
  21591. XMEMSET(&rng, 0, sizeof(rng));
  21592. XMEMSET(&key, 0, sizeof(key));
  21593. ret = wc_InitRng(&rng);
  21594. if (ret == 0) {
  21595. ret = wc_ecc_init(&key);
  21596. if (ret == 0) {
  21597. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21598. #if defined(WOLFSSL_ASYNC_CRYPT)
  21599. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21600. #endif
  21601. }
  21602. }
  21603. if (ret == 0) {
  21604. ret = wc_ecc_size(&key);
  21605. if (ret == KEY14) {
  21606. ret = 0;
  21607. }
  21608. else if (ret == 0) {
  21609. ret = WOLFSSL_FATAL_ERROR;
  21610. }
  21611. }
  21612. /* Test bad args. */
  21613. if (ret == 0) {
  21614. /* Returns Zero for bad arg. */
  21615. ret = wc_ecc_size(NULL);
  21616. }
  21617. if (wc_FreeRng(&rng) && ret == 0) {
  21618. ret = WOLFSSL_FATAL_ERROR;
  21619. }
  21620. wc_ecc_free(&key);
  21621. res = TEST_RES_CHECK(ret == 0);
  21622. #endif
  21623. return res;
  21624. } /* END test_wc_ecc_size */
  21625. static int test_wc_ecc_params(void)
  21626. {
  21627. int res = TEST_SKIPPED;
  21628. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  21629. It was added after certifications */
  21630. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  21631. const ecc_set_type* ecc_set;
  21632. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  21633. /* Test for SECP256R1 curve */
  21634. int curve_id = ECC_SECP256R1;
  21635. int curve_idx = wc_ecc_get_curve_idx(curve_id);
  21636. AssertIntNE(curve_idx, ECC_CURVE_INVALID);
  21637. ecc_set = wc_ecc_get_curve_params(curve_idx);
  21638. AssertNotNull(ecc_set);
  21639. AssertIntEQ(ecc_set->id, curve_id);
  21640. #endif
  21641. /* Test case when SECP256R1 is not enabled */
  21642. /* Test that we get curve params for index 0 */
  21643. ecc_set = wc_ecc_get_curve_params(0);
  21644. AssertNotNull(ecc_set);
  21645. res = TEST_RES_CHECK(1);
  21646. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  21647. return res;
  21648. }
  21649. /*
  21650. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  21651. */
  21652. static int test_wc_ecc_signVerify_hash(void)
  21653. {
  21654. int res = TEST_SKIPPED;
  21655. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  21656. WC_RNG rng;
  21657. ecc_key key;
  21658. int ret;
  21659. int signH = WOLFSSL_FATAL_ERROR;
  21660. #ifdef HAVE_ECC_VERIFY
  21661. int verifyH = WOLFSSL_FATAL_ERROR;
  21662. int verify = 0;
  21663. #endif
  21664. word32 siglen = ECC_BUFSIZE;
  21665. byte sig[ECC_BUFSIZE];
  21666. byte adjustedSig[ECC_BUFSIZE+1];
  21667. byte digest[] = TEST_STRING;
  21668. word32 digestlen = (word32)TEST_STRING_SZ;
  21669. /* Init stack var */
  21670. XMEMSET(sig, 0, siglen);
  21671. XMEMSET(&key, 0, sizeof(key));
  21672. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  21673. /* Init structs. */
  21674. ret = wc_InitRng(&rng);
  21675. if (ret == 0) {
  21676. ret = wc_ecc_init(&key);
  21677. if (ret == 0) {
  21678. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21679. #if defined(WOLFSSL_ASYNC_CRYPT)
  21680. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21681. #endif
  21682. }
  21683. }
  21684. if (ret == 0) {
  21685. ret = wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key);
  21686. }
  21687. /* Check bad args. */
  21688. if (ret == 0) {
  21689. signH = wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key);
  21690. if (signH == ECC_BAD_ARG_E) {
  21691. signH = wc_ecc_sign_hash(digest, digestlen, NULL, &siglen,
  21692. &rng, &key);
  21693. }
  21694. if (signH == ECC_BAD_ARG_E) {
  21695. signH = wc_ecc_sign_hash(digest, digestlen, sig, NULL,
  21696. &rng, &key);
  21697. }
  21698. if (signH == ECC_BAD_ARG_E) {
  21699. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  21700. NULL, &key);
  21701. }
  21702. if (signH == ECC_BAD_ARG_E) {
  21703. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  21704. &rng, NULL);
  21705. }
  21706. if (signH == ECC_BAD_ARG_E) {
  21707. signH = 0;
  21708. }
  21709. else if (ret == 0) {
  21710. signH = WOLFSSL_FATAL_ERROR;
  21711. }
  21712. }
  21713. #ifdef HAVE_ECC_VERIFY
  21714. ret = wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify, &key);
  21715. if (verify != 1 && ret == 0) {
  21716. ret = WOLFSSL_FATAL_ERROR;
  21717. }
  21718. /* test check on length of signature passed in */
  21719. XMEMCPY(adjustedSig, sig, siglen);
  21720. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  21721. #ifndef NO_STRICT_ECDSA_LEN
  21722. AssertIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21723. &verify, &key), 0);
  21724. #else
  21725. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  21726. * is allowed */
  21727. AssertIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21728. &verify, &key), 0);
  21729. #endif
  21730. /* Test bad args. */
  21731. if (ret == 0) {
  21732. verifyH = wc_ecc_verify_hash(NULL, siglen, digest, digestlen,
  21733. &verify, &key);
  21734. if (verifyH == ECC_BAD_ARG_E) {
  21735. verifyH = wc_ecc_verify_hash(sig, siglen, NULL, digestlen,
  21736. &verify, &key);
  21737. }
  21738. if (verifyH == ECC_BAD_ARG_E) {
  21739. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  21740. NULL, &key);
  21741. }
  21742. if (verifyH == ECC_BAD_ARG_E) {
  21743. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  21744. &verify, NULL);
  21745. }
  21746. if (verifyH == ECC_BAD_ARG_E) {
  21747. verifyH = 0;
  21748. }
  21749. else if (ret == 0) {
  21750. verifyH = WOLFSSL_FATAL_ERROR;
  21751. }
  21752. }
  21753. #endif /* HAVE_ECC_VERIFY */
  21754. if (wc_FreeRng(&rng) && ret == 0) {
  21755. ret = WOLFSSL_FATAL_ERROR;
  21756. }
  21757. wc_ecc_free(&key);
  21758. #ifdef FP_ECC
  21759. wc_ecc_fp_free();
  21760. #endif
  21761. res = TEST_RES_CHECK(ret == 0 && signH == 0 && verifyH == 0);
  21762. #endif
  21763. return res;
  21764. } /* END test_wc_ecc_sign_hash */
  21765. /*
  21766. * Testing wc_ecc_shared_secret()
  21767. */
  21768. static int test_wc_ecc_shared_secret(void)
  21769. {
  21770. int res = TEST_SKIPPED;
  21771. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  21772. ecc_key key, pubKey;
  21773. WC_RNG rng;
  21774. int ret;
  21775. byte out[KEY32];
  21776. int keySz = sizeof(out);
  21777. word32 outlen = (word32)sizeof(out);
  21778. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21779. const char* qx =
  21780. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21781. const char* qy =
  21782. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21783. const char* d =
  21784. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21785. const char* curveName = "SECP256R1";
  21786. const byte expected_shared_secret[] =
  21787. {
  21788. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  21789. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  21790. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  21791. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  21792. };
  21793. #endif
  21794. PRIVATE_KEY_UNLOCK();
  21795. /* Initialize variables. */
  21796. XMEMSET(out, 0, keySz);
  21797. XMEMSET(&rng, 0, sizeof(rng));
  21798. XMEMSET(&key, 0, sizeof(key));
  21799. XMEMSET(&pubKey, 0, sizeof(pubKey));
  21800. ret = wc_InitRng(&rng);
  21801. if (ret == 0) {
  21802. ret = wc_ecc_init(&key);
  21803. if (ret == 0) {
  21804. ret = wc_ecc_init(&pubKey);
  21805. }
  21806. }
  21807. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21808. if (ret == 0) {
  21809. ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
  21810. }
  21811. if (ret == 0) {
  21812. ret = wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName);
  21813. }
  21814. #else
  21815. if (ret == 0) {
  21816. ret = wc_ecc_make_key(&rng, keySz, &key);
  21817. #if defined(WOLFSSL_ASYNC_CRYPT)
  21818. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21819. #endif
  21820. }
  21821. if (ret == 0) {
  21822. ret = wc_ecc_make_key(&rng, keySz, &pubKey);
  21823. #if defined(WOLFSSL_ASYNC_CRYPT)
  21824. ret = wc_AsyncWait(ret, &pubKey.asyncDev, WC_ASYNC_FLAG_NONE);
  21825. #endif
  21826. }
  21827. #endif
  21828. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21829. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21830. !defined(HAVE_SELFTEST)
  21831. if (ret == 0) {
  21832. ret = wc_ecc_set_rng(&key, &rng);
  21833. }
  21834. #endif
  21835. if (ret == 0) {
  21836. ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
  21837. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21838. if (ret == 0) {
  21839. if (0 != XMEMCMP(out, expected_shared_secret, outlen)) {
  21840. ret = WOLFSSL_FATAL_ERROR;
  21841. }
  21842. }
  21843. #endif
  21844. /* Test bad args. */
  21845. if (ret == 0) {
  21846. ret = wc_ecc_shared_secret(NULL, &pubKey, out, &outlen);
  21847. if (ret == BAD_FUNC_ARG) {
  21848. ret = wc_ecc_shared_secret(&key, NULL, out, &outlen);
  21849. }
  21850. if (ret == BAD_FUNC_ARG) {
  21851. ret = wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen);
  21852. }
  21853. if (ret == BAD_FUNC_ARG) {
  21854. ret = wc_ecc_shared_secret(&key, &pubKey, out, NULL);
  21855. }
  21856. if (ret == BAD_FUNC_ARG) {
  21857. /* Invalid length */
  21858. outlen = 1;
  21859. ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
  21860. }
  21861. if (ret == BUFFER_E) {
  21862. ret = 0;
  21863. }
  21864. else if (ret == 0) {
  21865. ret = WOLFSSL_FATAL_ERROR;
  21866. }
  21867. }
  21868. }
  21869. if (wc_FreeRng(&rng) && ret == 0) {
  21870. ret = WOLFSSL_FATAL_ERROR;
  21871. }
  21872. wc_ecc_free(&key);
  21873. wc_ecc_free(&pubKey);
  21874. #ifdef FP_ECC
  21875. wc_ecc_fp_free();
  21876. #endif
  21877. PRIVATE_KEY_LOCK();
  21878. res = TEST_RES_CHECK(ret == 0);
  21879. #endif
  21880. return res;
  21881. } /* END tests_wc_ecc_shared_secret */
  21882. /*
  21883. * testint wc_ecc_export_x963()
  21884. */
  21885. static int test_wc_ecc_export_x963(void)
  21886. {
  21887. int res = TEST_SKIPPED;
  21888. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21889. ecc_key key;
  21890. WC_RNG rng;
  21891. byte out[ECC_ASN963_MAX_BUF_SZ];
  21892. word32 outlen = sizeof(out);
  21893. int ret = 0;
  21894. PRIVATE_KEY_UNLOCK();
  21895. /* Initialize variables. */
  21896. XMEMSET(out, 0, outlen);
  21897. XMEMSET(&rng, 0, sizeof(rng));
  21898. XMEMSET(&key, 0, sizeof(key));
  21899. ret = wc_InitRng(&rng);
  21900. if (ret == 0) {
  21901. ret = wc_ecc_init(&key);
  21902. if (ret == 0) {
  21903. ret = wc_ecc_make_key(&rng, KEY20, &key);
  21904. #if defined(WOLFSSL_ASYNC_CRYPT)
  21905. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21906. #endif
  21907. }
  21908. }
  21909. if (ret == 0) {
  21910. ret = wc_ecc_export_x963(&key, out, &outlen);
  21911. }
  21912. /* Test bad args. */
  21913. if (ret == 0) {
  21914. ret = wc_ecc_export_x963(NULL, out, &outlen);
  21915. if (ret == ECC_BAD_ARG_E) {
  21916. ret = wc_ecc_export_x963(&key, NULL, &outlen);
  21917. }
  21918. if (ret == LENGTH_ONLY_E) {
  21919. ret = wc_ecc_export_x963(&key, out, NULL);
  21920. }
  21921. if (ret == ECC_BAD_ARG_E) {
  21922. key.idx = -4;
  21923. ret = wc_ecc_export_x963(&key, out, &outlen);
  21924. }
  21925. if (ret == ECC_BAD_ARG_E) {
  21926. ret = 0;
  21927. }
  21928. else {
  21929. ret = WOLFSSL_FATAL_ERROR;
  21930. }
  21931. }
  21932. if (wc_FreeRng(&rng) && ret == 0) {
  21933. ret = WOLFSSL_FATAL_ERROR;
  21934. }
  21935. wc_ecc_free(&key);
  21936. #ifdef FP_ECC
  21937. wc_ecc_fp_free();
  21938. #endif
  21939. PRIVATE_KEY_LOCK();
  21940. res = TEST_RES_CHECK(ret == 0);
  21941. #endif
  21942. return res;
  21943. } /* END test_wc_ecc_export_x963 */
  21944. /*
  21945. * Testing wc_ecc_export_x963_ex()
  21946. * compile with --enable-compkey will use compression.
  21947. */
  21948. static int test_wc_ecc_export_x963_ex(void)
  21949. {
  21950. int res = TEST_SKIPPED;
  21951. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21952. ecc_key key;
  21953. WC_RNG rng;
  21954. int ret = 0;
  21955. byte out[ECC_ASN963_MAX_BUF_SZ];
  21956. word32 outlen = sizeof(out);
  21957. #ifdef HAVE_COMP_KEY
  21958. word32 badOutLen = 5;
  21959. #endif
  21960. /* Init stack variables. */
  21961. XMEMSET(out, 0, outlen);
  21962. XMEMSET(&rng, 0, sizeof(rng));
  21963. XMEMSET(&key, 0, sizeof(key));
  21964. ret = wc_InitRng(&rng);
  21965. if (ret == 0) {
  21966. ret = wc_ecc_init(&key);
  21967. if (ret == 0) {
  21968. ret = wc_ecc_make_key(&rng, KEY64, &key);
  21969. #if defined(WOLFSSL_ASYNC_CRYPT)
  21970. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21971. #endif
  21972. }
  21973. }
  21974. #ifdef HAVE_COMP_KEY
  21975. if (ret == 0) {
  21976. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  21977. }
  21978. #else
  21979. if (ret == 0) {
  21980. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  21981. }
  21982. #endif
  21983. /* Test bad args. */
  21984. #ifdef HAVE_COMP_KEY
  21985. if (ret == 0) {
  21986. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, COMP);
  21987. if (ret == BAD_FUNC_ARG) {
  21988. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP);
  21989. }
  21990. if (ret == BAD_FUNC_ARG) {
  21991. ret = wc_ecc_export_x963_ex(&key, out, NULL, COMP);
  21992. }
  21993. if (ret == BAD_FUNC_ARG) {
  21994. ret = wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP);
  21995. }
  21996. #if defined(HAVE_FIPS) && (!defined(FIPS_VERSION_LT) || FIPS_VERSION_LT(5,3))
  21997. if (ret == BUFFER_E)
  21998. #else
  21999. if (ret == LENGTH_ONLY_E)
  22000. #endif
  22001. {
  22002. key.idx = -4;
  22003. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  22004. }
  22005. if (ret == ECC_BAD_ARG_E) {
  22006. ret = 0;
  22007. }
  22008. else {
  22009. ret = WOLFSSL_FATAL_ERROR;
  22010. }
  22011. }
  22012. #else
  22013. if (ret == 0) {
  22014. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP);
  22015. if (ret == BAD_FUNC_ARG) {
  22016. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP);
  22017. }
  22018. if (ret == BAD_FUNC_ARG) {
  22019. ret = wc_ecc_export_x963_ex(&key, out, &outlen, 1);
  22020. }
  22021. if (ret == NOT_COMPILED_IN) {
  22022. ret = wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP);
  22023. }
  22024. if (ret == BAD_FUNC_ARG) {
  22025. key.idx = -4;
  22026. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  22027. }
  22028. if (ret == ECC_BAD_ARG_E) {
  22029. ret = 0;
  22030. }
  22031. else if (ret == 0) {
  22032. ret = WOLFSSL_FATAL_ERROR;
  22033. }
  22034. }
  22035. #endif
  22036. if (wc_FreeRng(&rng) && ret == 0) {
  22037. ret = WOLFSSL_FATAL_ERROR;
  22038. }
  22039. wc_ecc_free(&key);
  22040. #ifdef FP_ECC
  22041. wc_ecc_fp_free();
  22042. #endif
  22043. res = TEST_RES_CHECK(ret == 0);
  22044. #endif
  22045. return res;
  22046. } /* END test_wc_ecc_export_x963_ex */
  22047. /*
  22048. * testing wc_ecc_import_x963()
  22049. */
  22050. static int test_wc_ecc_import_x963(void)
  22051. {
  22052. int res = TEST_SKIPPED;
  22053. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  22054. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22055. ecc_key pubKey, key;
  22056. WC_RNG rng;
  22057. byte x963[ECC_ASN963_MAX_BUF_SZ];
  22058. word32 x963Len = (word32)sizeof(x963);
  22059. int ret;
  22060. /* Init stack variables. */
  22061. XMEMSET(x963, 0, x963Len);
  22062. XMEMSET(&rng, 0, sizeof(rng));
  22063. XMEMSET(&key, 0, sizeof(key));
  22064. XMEMSET(&pubKey, 0, sizeof(pubKey));
  22065. ret = wc_InitRng(&rng);
  22066. if (ret == 0) {
  22067. ret = wc_ecc_init(&pubKey);
  22068. if (ret == 0) {
  22069. ret = wc_ecc_init(&key);
  22070. }
  22071. if (ret == 0) {
  22072. ret = wc_ecc_make_key(&rng, KEY24, &key);
  22073. #if defined(WOLFSSL_ASYNC_CRYPT)
  22074. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22075. #endif
  22076. }
  22077. if (ret == 0) {
  22078. PRIVATE_KEY_UNLOCK();
  22079. ret = wc_ecc_export_x963(&key, x963, &x963Len);
  22080. PRIVATE_KEY_LOCK();
  22081. }
  22082. }
  22083. if (ret == 0) {
  22084. ret = wc_ecc_import_x963(x963, x963Len, &pubKey);
  22085. }
  22086. /* Test bad args. */
  22087. if (ret == 0) {
  22088. ret = wc_ecc_import_x963(NULL, x963Len, &pubKey);
  22089. if (ret == BAD_FUNC_ARG) {
  22090. ret = wc_ecc_import_x963(x963, x963Len, NULL);
  22091. }
  22092. if (ret == BAD_FUNC_ARG) {
  22093. ret = wc_ecc_import_x963(x963, x963Len + 1, &pubKey);
  22094. }
  22095. if (ret == ECC_BAD_ARG_E) {
  22096. ret = 0;
  22097. }
  22098. else if (ret == 0) {
  22099. ret = WOLFSSL_FATAL_ERROR;
  22100. }
  22101. }
  22102. if (wc_FreeRng(&rng) && ret == 0) {
  22103. ret = WOLFSSL_FATAL_ERROR;
  22104. }
  22105. wc_ecc_free(&key);
  22106. wc_ecc_free(&pubKey);
  22107. #ifdef FP_ECC
  22108. wc_ecc_fp_free();
  22109. #endif
  22110. res = TEST_RES_CHECK(ret == 0);
  22111. #endif
  22112. return res;
  22113. } /* END wc_ecc_import_x963 */
  22114. /*
  22115. * testing wc_ecc_import_private_key()
  22116. */
  22117. static int ecc_import_private_key(void)
  22118. {
  22119. int res = TEST_SKIPPED;
  22120. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  22121. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22122. ecc_key key, keyImp;
  22123. WC_RNG rng;
  22124. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  22125. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  22126. word32 privKeySz = (word32)sizeof(privKey);
  22127. word32 x963KeySz = (word32)sizeof(x963Key);
  22128. int ret;
  22129. /* Init stack variables. */
  22130. XMEMSET(privKey, 0, privKeySz);
  22131. XMEMSET(x963Key, 0, x963KeySz);
  22132. XMEMSET(&rng, 0, sizeof(rng));
  22133. XMEMSET(&key, 0, sizeof(key));
  22134. XMEMSET(&keyImp, 0, sizeof(keyImp));
  22135. ret = wc_InitRng(&rng);
  22136. if (ret == 0) {
  22137. ret = wc_ecc_init(&key);
  22138. if (ret == 0) {
  22139. ret = wc_ecc_init(&keyImp);
  22140. }
  22141. if (ret == 0) {
  22142. ret = wc_ecc_make_key(&rng, KEY48, &key);
  22143. #if defined(WOLFSSL_ASYNC_CRYPT)
  22144. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22145. #endif
  22146. }
  22147. if (ret == 0) {
  22148. PRIVATE_KEY_UNLOCK();
  22149. ret = wc_ecc_export_x963(&key, x963Key, &x963KeySz);
  22150. PRIVATE_KEY_LOCK();
  22151. }
  22152. if (ret == 0) {
  22153. ret = wc_ecc_export_private_only(&key, privKey, &privKeySz);
  22154. }
  22155. }
  22156. if (ret == 0) {
  22157. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  22158. x963KeySz, &keyImp);
  22159. }
  22160. /* Pass in bad args. */
  22161. if (ret == 0) {
  22162. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  22163. x963KeySz, NULL);
  22164. if (ret == BAD_FUNC_ARG) {
  22165. ret = wc_ecc_import_private_key(NULL, privKeySz, x963Key,
  22166. x963KeySz, &keyImp);
  22167. }
  22168. if (ret == BAD_FUNC_ARG) {
  22169. ret = 0;
  22170. }
  22171. else if (ret == 0) {
  22172. ret = WOLFSSL_FATAL_ERROR;
  22173. }
  22174. }
  22175. if (wc_FreeRng(&rng) && ret == 0) {
  22176. ret = WOLFSSL_FATAL_ERROR;
  22177. }
  22178. wc_ecc_free(&key);
  22179. wc_ecc_free(&keyImp);
  22180. #ifdef FP_ECC
  22181. wc_ecc_fp_free();
  22182. #endif
  22183. res = TEST_RES_CHECK(ret == 0);
  22184. #endif
  22185. return res;
  22186. } /* END wc_ecc_import_private_key */
  22187. /*
  22188. * Testing wc_ecc_export_private_only()
  22189. */
  22190. static int test_wc_ecc_export_private_only(void)
  22191. {
  22192. int res = TEST_SKIPPED;
  22193. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22194. ecc_key key;
  22195. WC_RNG rng;
  22196. byte out[ECC_PRIV_KEY_BUF];
  22197. word32 outlen = sizeof(out);
  22198. int ret;
  22199. /* Init stack variables. */
  22200. XMEMSET(out, 0, outlen);
  22201. XMEMSET(&rng, 0, sizeof(rng));
  22202. XMEMSET(&key, 0, sizeof(key));
  22203. ret = wc_InitRng(&rng);
  22204. if (ret == 0) {
  22205. ret = wc_ecc_init(&key);
  22206. if (ret == 0) {
  22207. ret = wc_ecc_make_key(&rng, KEY32, &key);
  22208. #if defined(WOLFSSL_ASYNC_CRYPT)
  22209. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22210. #endif
  22211. }
  22212. }
  22213. if (ret == 0) {
  22214. ret = wc_ecc_export_private_only(&key, out, &outlen);
  22215. }
  22216. /* Pass in bad args. */
  22217. if (ret == 0) {
  22218. ret = wc_ecc_export_private_only(NULL, out, &outlen);
  22219. if (ret == BAD_FUNC_ARG) {
  22220. ret = wc_ecc_export_private_only(&key, NULL, &outlen);
  22221. }
  22222. if (ret == BAD_FUNC_ARG) {
  22223. ret = wc_ecc_export_private_only(&key, out, NULL);
  22224. }
  22225. if (ret == BAD_FUNC_ARG) {
  22226. ret = 0;
  22227. }
  22228. else if (ret == 0) {
  22229. ret = WOLFSSL_FATAL_ERROR;
  22230. }
  22231. }
  22232. if (wc_FreeRng(&rng) && ret == 0) {
  22233. ret = WOLFSSL_FATAL_ERROR;
  22234. }
  22235. wc_ecc_free(&key);
  22236. #ifdef FP_ECC
  22237. wc_ecc_fp_free();
  22238. #endif
  22239. res = TEST_RES_CHECK(ret == 0);
  22240. #endif
  22241. return res;
  22242. } /* END test_wc_ecc_export_private_only */
  22243. /*
  22244. * Testing wc_ecc_rs_to_sig()
  22245. */
  22246. static int test_wc_ecc_rs_to_sig(void)
  22247. {
  22248. int res = TEST_SKIPPED;
  22249. #if defined(HAVE_ECC) && !defined(NO_ASN)
  22250. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  22251. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  22252. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  22253. const char* zeroStr = "0";
  22254. byte sig[ECC_MAX_SIG_SIZE];
  22255. word32 siglen = (word32)sizeof(sig);
  22256. /*R and S max size is the order of curve. 2^192.*/
  22257. int keySz = KEY24;
  22258. byte r[KEY24];
  22259. byte s[KEY24];
  22260. word32 rlen = (word32)sizeof(r);
  22261. word32 slen = (word32)sizeof(s);
  22262. int ret;
  22263. /* Init stack variables. */
  22264. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  22265. XMEMSET(r, 0, keySz);
  22266. XMEMSET(s, 0, keySz);
  22267. ret = wc_ecc_rs_to_sig(R, S, sig, &siglen);
  22268. /* Test bad args. */
  22269. if (ret == 0) {
  22270. ret = wc_ecc_rs_to_sig(NULL, S, sig, &siglen);
  22271. if (ret == ECC_BAD_ARG_E) {
  22272. ret = wc_ecc_rs_to_sig(R, NULL, sig, &siglen);
  22273. }
  22274. if (ret == ECC_BAD_ARG_E) {
  22275. ret = wc_ecc_rs_to_sig(R, S, sig, NULL);
  22276. }
  22277. if (ret == ECC_BAD_ARG_E) {
  22278. ret = wc_ecc_rs_to_sig(R, S, NULL, &siglen);
  22279. }
  22280. if (ret == ECC_BAD_ARG_E) {
  22281. ret = wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen);
  22282. }
  22283. if (ret == MP_ZERO_E) {
  22284. ret = wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen);
  22285. }
  22286. if (ret == MP_ZERO_E) {
  22287. ret = 0;
  22288. }
  22289. else {
  22290. ret = WOLFSSL_FATAL_ERROR;
  22291. }
  22292. }
  22293. if (ret == 0) {
  22294. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen);
  22295. }
  22296. /* Test bad args. */
  22297. if (ret == 0) {
  22298. ret = wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen);
  22299. if (ret == ECC_BAD_ARG_E) {
  22300. ret = wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen);
  22301. }
  22302. if (ret == ECC_BAD_ARG_E) {
  22303. ret = wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen);
  22304. }
  22305. if (ret == ECC_BAD_ARG_E) {
  22306. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen);
  22307. }
  22308. if (ret == ECC_BAD_ARG_E) {
  22309. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL);
  22310. }
  22311. if (ret == ECC_BAD_ARG_E) {
  22312. ret = 0;
  22313. }
  22314. else if (ret == 0) {
  22315. ret = WOLFSSL_FATAL_ERROR;
  22316. }
  22317. }
  22318. res = TEST_RES_CHECK(ret == 0);
  22319. #endif
  22320. return res;
  22321. } /* END test_wc_ecc_rs_to_sig */
  22322. static int test_wc_ecc_import_raw(void)
  22323. {
  22324. int res = TEST_SKIPPED;
  22325. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  22326. ecc_key key;
  22327. int ret = 0;
  22328. const char* qx =
  22329. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  22330. const char* qy =
  22331. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  22332. const char* d =
  22333. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  22334. const char* curveName = "SECP256R1";
  22335. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22336. const char* kNullStr = "";
  22337. #endif
  22338. ret = wc_ecc_init(&key);
  22339. /* Test good import */
  22340. if (ret == 0) {
  22341. ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
  22342. }
  22343. /* Test bad args. */
  22344. if (ret == 0) {
  22345. ret = wc_ecc_import_raw(NULL, qx, qy, d, curveName);
  22346. if (ret == BAD_FUNC_ARG) {
  22347. ret = wc_ecc_import_raw(&key, NULL, qy, d, curveName);
  22348. }
  22349. if (ret == BAD_FUNC_ARG) {
  22350. ret = wc_ecc_import_raw(&key, qx, NULL, d, curveName);
  22351. }
  22352. if (ret == BAD_FUNC_ARG) {
  22353. ret = wc_ecc_import_raw(&key, qx, qy, d, NULL);
  22354. }
  22355. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22356. if (ret == BAD_FUNC_ARG) {
  22357. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22358. wc_ecc_free(&key);
  22359. #endif
  22360. ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr, curveName);
  22361. if (ret == ECC_INF_E)
  22362. ret = BAD_FUNC_ARG; /* This is expected by other tests */
  22363. }
  22364. #endif
  22365. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  22366. if (ret == BAD_FUNC_ARG) {
  22367. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22368. wc_ecc_free(&key);
  22369. #endif
  22370. ret = wc_ecc_import_raw(&key, "0", qy, d, curveName);
  22371. /* Note: SP math "is point" failure returns MP_VAL */
  22372. if (ret == ECC_INF_E || ret == MP_VAL) {
  22373. ret = BAD_FUNC_ARG; /* This is expected by other tests */
  22374. }
  22375. }
  22376. if (ret == BAD_FUNC_ARG) {
  22377. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22378. wc_ecc_free(&key);
  22379. #endif
  22380. ret = wc_ecc_import_raw(&key, qx, "0", d, curveName);
  22381. /* Note: SP math "is point" failure returns MP_VAL */
  22382. if (ret == ECC_INF_E || ret == MP_VAL) {
  22383. ret = BAD_FUNC_ARG; /* This is expected by other tests */
  22384. }
  22385. }
  22386. #endif
  22387. if (ret == BAD_FUNC_ARG) {
  22388. ret = 0;
  22389. }
  22390. }
  22391. wc_ecc_free(&key);
  22392. res = TEST_RES_CHECK(ret == 0);
  22393. #endif
  22394. return res;
  22395. } /* END test_wc_ecc_import_raw */
  22396. static int test_wc_ecc_import_unsigned(void)
  22397. {
  22398. int res = TEST_SKIPPED;
  22399. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  22400. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  22401. ecc_key key;
  22402. const byte qx[] = {
  22403. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  22404. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  22405. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  22406. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  22407. };
  22408. const byte qy[] = {
  22409. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  22410. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  22411. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  22412. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  22413. };
  22414. const byte d[] = {
  22415. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  22416. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  22417. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  22418. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  22419. };
  22420. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22421. const byte nullBytes[32] = {0};
  22422. #endif
  22423. int curveId = ECC_SECP256R1;
  22424. int ret;
  22425. ret = wc_ecc_init(&key);
  22426. if (ret == 0) {
  22427. ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  22428. curveId);
  22429. }
  22430. /* Test bad args. */
  22431. if (ret == 0) {
  22432. ret = wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  22433. curveId);
  22434. if (ret == BAD_FUNC_ARG) {
  22435. ret = wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  22436. curveId);
  22437. }
  22438. if (ret == BAD_FUNC_ARG) {
  22439. ret = wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  22440. curveId);
  22441. }
  22442. if (ret == BAD_FUNC_ARG) {
  22443. ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  22444. ECC_CURVE_INVALID);
  22445. }
  22446. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22447. if (ret == BAD_FUNC_ARG) {
  22448. ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  22449. (byte*)nullBytes, (byte*)nullBytes, curveId);
  22450. }
  22451. #endif
  22452. if (ret == BAD_FUNC_ARG || ret == ECC_INF_E) {
  22453. ret = 0;
  22454. }
  22455. }
  22456. wc_ecc_free(&key);
  22457. res = TEST_RES_CHECK(ret == 0);
  22458. #endif
  22459. return res;
  22460. } /* END test_wc_ecc_import_unsigned */
  22461. /*
  22462. * Testing wc_ecc_sig_size()
  22463. */
  22464. static int test_wc_ecc_sig_size(void)
  22465. {
  22466. int res = TEST_SKIPPED;
  22467. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22468. ecc_key key;
  22469. WC_RNG rng;
  22470. int keySz = KEY16;
  22471. int ret = 0;
  22472. XMEMSET(&rng, 0, sizeof(rng));
  22473. XMEMSET(&key, 0, sizeof(key));
  22474. ret = wc_InitRng(&rng);
  22475. if (ret == 0) {
  22476. ret = wc_ecc_init(&key);
  22477. if (ret == 0) {
  22478. ret = wc_ecc_make_key(&rng, keySz, &key);
  22479. #if defined(WOLFSSL_ASYNC_CRYPT)
  22480. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22481. #endif
  22482. }
  22483. }
  22484. if (ret == 0) {
  22485. ret = wc_ecc_sig_size(&key);
  22486. if (ret <= (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ)) {
  22487. ret = 0;
  22488. }
  22489. }
  22490. if (wc_FreeRng(&rng) && ret == 0) {
  22491. ret = WOLFSSL_FATAL_ERROR;
  22492. }
  22493. wc_ecc_free(&key);
  22494. res = TEST_RES_CHECK(ret == 0);
  22495. #endif
  22496. return res;
  22497. } /* END test_wc_ecc_sig_size */
  22498. /*
  22499. * Testing wc_ecc_ctx_new()
  22500. */
  22501. static int test_wc_ecc_ctx_new(void)
  22502. {
  22503. int res = TEST_SKIPPED;
  22504. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22505. WC_RNG rng;
  22506. int ret = 0;
  22507. ecEncCtx* cli = NULL;
  22508. ecEncCtx* srv = NULL;
  22509. ret = wc_InitRng(&rng);
  22510. if (ret == 0) {
  22511. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
  22512. srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng);
  22513. }
  22514. if (ret == 0 && (cli == NULL || srv == NULL)) {
  22515. ret = WOLFSSL_FATAL_ERROR;
  22516. }
  22517. wc_ecc_ctx_free(cli);
  22518. wc_ecc_ctx_free(srv);
  22519. /* Test bad args. */
  22520. if (ret == 0) {
  22521. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  22522. cli = wc_ecc_ctx_new(0, &rng);
  22523. if (cli != NULL) {
  22524. ret = WOLFSSL_FATAL_ERROR;
  22525. }
  22526. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL);
  22527. if (cli != NULL) {
  22528. ret = WOLFSSL_FATAL_ERROR;
  22529. }
  22530. }
  22531. if (wc_FreeRng(&rng) && ret == 0) {
  22532. ret = WOLFSSL_FATAL_ERROR;
  22533. }
  22534. wc_ecc_ctx_free(cli);
  22535. res = TEST_RES_CHECK(ret == 0);
  22536. #endif
  22537. return res;
  22538. } /* END test_wc_ecc_ctx_new */
  22539. /*
  22540. * Tesing wc_ecc_reset()
  22541. */
  22542. static int test_wc_ecc_ctx_reset(void)
  22543. {
  22544. int res = TEST_SKIPPED;
  22545. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22546. ecEncCtx* ctx = NULL;
  22547. WC_RNG rng;
  22548. int ret = 0;
  22549. ret = wc_InitRng(&rng);
  22550. if (ret == 0) {
  22551. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) {
  22552. ret = WOLFSSL_FATAL_ERROR;
  22553. }
  22554. }
  22555. if (ret == 0) {
  22556. ret = wc_ecc_ctx_reset(ctx, &rng);
  22557. }
  22558. /* Pass in bad args. */
  22559. if (ret == 0) {
  22560. ret = wc_ecc_ctx_reset(NULL, &rng);
  22561. if (ret == BAD_FUNC_ARG) {
  22562. ret = wc_ecc_ctx_reset(ctx, NULL);
  22563. }
  22564. if (ret == BAD_FUNC_ARG) {
  22565. ret = 0;
  22566. }
  22567. else if (ret == 0) {
  22568. ret = WOLFSSL_FATAL_ERROR;
  22569. }
  22570. }
  22571. if (wc_FreeRng(&rng) && ret == 0) {
  22572. ret = WOLFSSL_FATAL_ERROR;
  22573. }
  22574. wc_ecc_ctx_free(ctx);
  22575. res = TEST_RES_CHECK(ret == 0);
  22576. #endif
  22577. return res;
  22578. } /* END test_wc_ecc_ctx_reset */
  22579. /*
  22580. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  22581. */
  22582. static int test_wc_ecc_ctx_set_peer_salt(void)
  22583. {
  22584. int res = TEST_SKIPPED;
  22585. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22586. WC_RNG rng;
  22587. ecEncCtx* cliCtx = NULL;
  22588. ecEncCtx* servCtx = NULL;
  22589. const byte* cliSalt = NULL;
  22590. const byte* servSalt = NULL;
  22591. int ret = 0;
  22592. ret = wc_InitRng(&rng);
  22593. if (ret == 0) {
  22594. if ( ( (cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) ||
  22595. ( (servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng)) == NULL) ) {
  22596. ret = WOLFSSL_FATAL_ERROR;
  22597. }
  22598. }
  22599. /* Test bad args. */
  22600. if (ret == 0) {
  22601. cliSalt = wc_ecc_ctx_get_own_salt(NULL);
  22602. if (cliSalt != NULL) {
  22603. ret = WOLFSSL_FATAL_ERROR;
  22604. }
  22605. }
  22606. if (ret == 0) {
  22607. cliSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  22608. servSalt = wc_ecc_ctx_get_own_salt(servCtx);
  22609. if (cliSalt == NULL || servSalt == NULL) {
  22610. ret = WOLFSSL_FATAL_ERROR;
  22611. }
  22612. }
  22613. if (ret == 0) {
  22614. ret = wc_ecc_ctx_set_peer_salt(cliCtx, servSalt);
  22615. }
  22616. /* Test bad args. */
  22617. if (ret == 0) {
  22618. ret = wc_ecc_ctx_set_peer_salt(NULL, servSalt);
  22619. if (ret == BAD_FUNC_ARG) {
  22620. ret = wc_ecc_ctx_set_peer_salt(cliCtx, NULL);
  22621. }
  22622. if (ret == BAD_FUNC_ARG) {
  22623. ret = 0;
  22624. }
  22625. else if (ret == 0) {
  22626. ret = WOLFSSL_FATAL_ERROR;
  22627. }
  22628. }
  22629. if (wc_FreeRng(&rng) && ret == 0) {
  22630. ret = WOLFSSL_FATAL_ERROR;
  22631. }
  22632. wc_ecc_ctx_free(cliCtx);
  22633. wc_ecc_ctx_free(servCtx);
  22634. res = TEST_RES_CHECK(ret == 0);
  22635. #endif
  22636. return res;
  22637. } /* END test_wc_ecc_ctx_set_peer_salt */
  22638. /*
  22639. * Testing wc_ecc_ctx_set_info()
  22640. */
  22641. static int test_wc_ecc_ctx_set_info(void)
  22642. {
  22643. int res = TEST_SKIPPED;
  22644. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22645. ecEncCtx* ctx = NULL;
  22646. WC_RNG rng;
  22647. int ret;
  22648. const char* optInfo = "Optional Test Info.";
  22649. int optInfoSz = (int)XSTRLEN(optInfo);
  22650. const char* badOptInfo = NULL;
  22651. ret = wc_InitRng(&rng);
  22652. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL || ret != 0 ) {
  22653. ret = WOLFSSL_FATAL_ERROR;
  22654. }
  22655. if (ret == 0) {
  22656. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz);
  22657. }
  22658. /* Test bad args. */
  22659. if (ret == 0) {
  22660. ret = wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz);
  22661. if (ret == BAD_FUNC_ARG) {
  22662. ret = wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz);
  22663. }
  22664. if (ret == BAD_FUNC_ARG) {
  22665. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1);
  22666. }
  22667. if (ret == BAD_FUNC_ARG) {
  22668. ret = 0;
  22669. }
  22670. else if (ret == 0) {
  22671. ret = WOLFSSL_FATAL_ERROR;
  22672. }
  22673. }
  22674. if (wc_FreeRng(&rng) && ret == 0) {
  22675. ret = WOLFSSL_FATAL_ERROR;
  22676. }
  22677. wc_ecc_ctx_free(ctx);
  22678. res = TEST_RES_CHECK(ret == 0);
  22679. #endif
  22680. return res;
  22681. } /* END test_wc_ecc_ctx_set_info */
  22682. /*
  22683. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  22684. */
  22685. static int test_wc_ecc_encryptDecrypt(void)
  22686. {
  22687. int res = TEST_SKIPPED;
  22688. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  22689. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  22690. ecc_key srvKey, cliKey, tmpKey;
  22691. WC_RNG rng;
  22692. int ret;
  22693. const char* msg = "EccBlock Size 16";
  22694. word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
  22695. #ifdef WOLFSSL_ECIES_OLD
  22696. byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22697. #elif defined(WOLFSSL_ECIES_GEN_IV)
  22698. byte out[KEY20 * 2 + 1 + AES_BLOCK_SIZE +
  22699. (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22700. #else
  22701. byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22702. #endif
  22703. word32 outSz = (word32)sizeof(out);
  22704. byte plain[sizeof("EccBlock Size 16")];
  22705. word32 plainSz = (word32)sizeof(plain);
  22706. int keySz = KEY20;
  22707. /* Init stack variables. */
  22708. XMEMSET(out, 0, outSz);
  22709. XMEMSET(plain, 0, plainSz);
  22710. XMEMSET(&rng, 0, sizeof(rng));
  22711. XMEMSET(&srvKey, 0, sizeof(srvKey));
  22712. XMEMSET(&cliKey, 0, sizeof(cliKey));
  22713. ret = wc_InitRng(&rng);
  22714. if (ret == 0) {
  22715. ret = wc_ecc_init(&cliKey);
  22716. if (ret == 0) {
  22717. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  22718. #if defined(WOLFSSL_ASYNC_CRYPT)
  22719. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22720. #endif
  22721. }
  22722. if (ret == 0) {
  22723. ret = wc_ecc_init(&srvKey);
  22724. }
  22725. if (ret == 0) {
  22726. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  22727. #if defined(WOLFSSL_ASYNC_CRYPT)
  22728. ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22729. #endif
  22730. }
  22731. if (ret == 0) {
  22732. ret = wc_ecc_init(&tmpKey);
  22733. }
  22734. }
  22735. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22736. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22737. !defined(HAVE_SELFTEST)
  22738. if (ret == 0) {
  22739. ret = wc_ecc_set_rng(&srvKey, &rng);
  22740. }
  22741. if (ret == 0) {
  22742. ret = wc_ecc_set_rng(&cliKey, &rng);
  22743. }
  22744. #endif
  22745. if (ret == 0) {
  22746. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  22747. &outSz, NULL);
  22748. }
  22749. if (ret == 0) {
  22750. ret = wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out,
  22751. &outSz, NULL);
  22752. if (ret == BAD_FUNC_ARG) {
  22753. ret = wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out,
  22754. &outSz, NULL);
  22755. }
  22756. if (ret == BAD_FUNC_ARG) {
  22757. ret = wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out,
  22758. &outSz, NULL);
  22759. }
  22760. if (ret == BAD_FUNC_ARG) {
  22761. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  22762. &outSz, NULL);
  22763. }
  22764. if (ret == BAD_FUNC_ARG) {
  22765. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  22766. NULL, NULL);
  22767. }
  22768. if (ret == BAD_FUNC_ARG) {
  22769. ret = 0;
  22770. }
  22771. else if (ret == 0) {
  22772. ret = WOLFSSL_FATAL_ERROR;
  22773. }
  22774. }
  22775. #ifdef WOLFSSL_ECIES_OLD
  22776. if (ret == 0) {
  22777. tmpKey.dp = cliKey.dp;
  22778. ret = wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey);
  22779. }
  22780. #endif
  22781. if (ret == 0) {
  22782. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain,
  22783. &plainSz, NULL);
  22784. }
  22785. if (ret == 0) {
  22786. ret = wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain,
  22787. &plainSz, NULL);
  22788. #ifdef WOLFSSL_ECIES_OLD
  22789. /* NULL parameter allowed in new implementations - public key comes from
  22790. * the message. */
  22791. if (ret == BAD_FUNC_ARG) {
  22792. ret = wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain,
  22793. &plainSz, NULL);
  22794. }
  22795. #endif
  22796. if (ret == BAD_FUNC_ARG) {
  22797. ret = wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain,
  22798. &plainSz, NULL);
  22799. }
  22800. if (ret == BAD_FUNC_ARG) {
  22801. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL,
  22802. &plainSz, NULL);
  22803. }
  22804. if (ret == BAD_FUNC_ARG) {
  22805. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz,
  22806. plain, NULL, NULL);
  22807. }
  22808. if (ret == BAD_FUNC_ARG) {
  22809. ret = 0;
  22810. }
  22811. else if (ret == 0) {
  22812. ret = WOLFSSL_FATAL_ERROR;
  22813. }
  22814. }
  22815. if (XMEMCMP(msg, plain, msgSz) != 0) {
  22816. ret = WOLFSSL_FATAL_ERROR;
  22817. }
  22818. if (wc_FreeRng(&rng) && ret == 0) {
  22819. ret = WOLFSSL_FATAL_ERROR;
  22820. }
  22821. wc_ecc_free(&tmpKey);
  22822. wc_ecc_free(&cliKey);
  22823. wc_ecc_free(&srvKey);
  22824. res = TEST_RES_CHECK(ret == 0);
  22825. #endif
  22826. return res;
  22827. } /* END test_wc_ecc_encryptDecrypt */
  22828. /*
  22829. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  22830. */
  22831. static int test_wc_ecc_del_point(void)
  22832. {
  22833. int res = TEST_SKIPPED;
  22834. #if defined(HAVE_ECC)
  22835. ecc_point* pt;
  22836. pt = wc_ecc_new_point();
  22837. wc_ecc_del_point(pt);
  22838. res = TEST_RES_CHECK(pt != NULL);
  22839. #endif
  22840. return res;
  22841. } /* END test_wc_ecc_del_point */
  22842. /*
  22843. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  22844. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  22845. * and wc_ecc_cmp_point()
  22846. */
  22847. static int test_wc_ecc_pointFns(void)
  22848. {
  22849. int res = TEST_SKIPPED;
  22850. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  22851. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22852. !defined(WOLFSSL_ATECC608A)
  22853. ecc_key key;
  22854. WC_RNG rng;
  22855. int ret;
  22856. ecc_point* point = NULL;
  22857. ecc_point* cpypt = NULL;
  22858. int idx = 0;
  22859. int keySz = KEY32;
  22860. byte der[DER_SZ(KEY32)];
  22861. word32 derlenChk = 0;
  22862. word32 derSz = DER_SZ(KEY32);
  22863. /* Init stack variables. */
  22864. XMEMSET(der, 0, derSz);
  22865. XMEMSET(&rng, 0, sizeof(rng));
  22866. XMEMSET(&key, 0, sizeof(key));
  22867. ret = wc_InitRng(&rng);
  22868. if (ret == 0) {
  22869. ret = wc_ecc_init(&key);
  22870. if (ret == 0) {
  22871. ret = wc_ecc_make_key(&rng, keySz, &key);
  22872. #if defined(WOLFSSL_ASYNC_CRYPT)
  22873. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22874. #endif
  22875. }
  22876. }
  22877. if (ret == 0) {
  22878. point = wc_ecc_new_point();
  22879. if (!point) {
  22880. ret = WOLFSSL_FATAL_ERROR;
  22881. }
  22882. }
  22883. if (ret == 0) {
  22884. cpypt = wc_ecc_new_point();
  22885. if (!cpypt) {
  22886. ret = WOLFSSL_FATAL_ERROR;
  22887. }
  22888. }
  22889. /* Export */
  22890. if (ret == 0) {
  22891. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  22892. NULL, &derlenChk);
  22893. /* Check length value. */
  22894. if (derSz == derlenChk && ret == LENGTH_ONLY_E) {
  22895. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  22896. der, &derSz);
  22897. }
  22898. }
  22899. /* Test bad args. */
  22900. if (ret == 0) {
  22901. ret = wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz);
  22902. if (ret == ECC_BAD_ARG_E) {
  22903. ret = wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz);
  22904. }
  22905. if (ret == ECC_BAD_ARG_E) {
  22906. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  22907. der, NULL);
  22908. }
  22909. if (ret == ECC_BAD_ARG_E) {
  22910. ret = 0;
  22911. }
  22912. else if (ret == 0) {
  22913. ret = WOLFSSL_FATAL_ERROR;
  22914. }
  22915. }
  22916. /* Import */
  22917. if (ret == 0) {
  22918. ret = wc_ecc_import_point_der(der, derSz, idx, point);
  22919. /* Condition double checks wc_ecc_cmp_point(). */
  22920. if (ret == 0 &&
  22921. XMEMCMP((void *)&key.pubkey, (void *)point, sizeof(key.pubkey))) {
  22922. ret = wc_ecc_cmp_point(&key.pubkey, point);
  22923. }
  22924. }
  22925. /* Test bad args. */
  22926. if (ret == 0) {
  22927. ret = wc_ecc_import_point_der(NULL, derSz, idx, point);
  22928. if (ret == ECC_BAD_ARG_E) {
  22929. ret = wc_ecc_import_point_der(der, derSz, idx, NULL);
  22930. }
  22931. if (ret == ECC_BAD_ARG_E) {
  22932. ret = wc_ecc_import_point_der(der, derSz, -1, point);
  22933. }
  22934. if (ret == ECC_BAD_ARG_E) {
  22935. ret = wc_ecc_import_point_der(der, derSz + 1, idx, point);
  22936. }
  22937. if (ret == ECC_BAD_ARG_E) {
  22938. ret = 0;
  22939. }
  22940. else if (ret == 0) {
  22941. ret = WOLFSSL_FATAL_ERROR;
  22942. }
  22943. }
  22944. /* Copy */
  22945. if (ret == 0) {
  22946. ret = wc_ecc_copy_point(point, cpypt);
  22947. }
  22948. /* Test bad args. */
  22949. if (ret == 0) {
  22950. ret = wc_ecc_copy_point(NULL, cpypt);
  22951. if (ret == ECC_BAD_ARG_E) {
  22952. ret = wc_ecc_copy_point(point, NULL);
  22953. }
  22954. if (ret == ECC_BAD_ARG_E) {
  22955. ret = 0;
  22956. }
  22957. else if (ret == 0) {
  22958. ret = WOLFSSL_FATAL_ERROR;
  22959. }
  22960. }
  22961. /* Compare point */
  22962. if (ret == 0) {
  22963. ret = wc_ecc_cmp_point(point, cpypt);
  22964. }
  22965. /* Test bad args. */
  22966. if (ret == 0) {
  22967. ret = wc_ecc_cmp_point(NULL, cpypt);
  22968. if (ret == BAD_FUNC_ARG) {
  22969. ret = wc_ecc_cmp_point(point, NULL);
  22970. }
  22971. if (ret == BAD_FUNC_ARG) {
  22972. ret = 0;
  22973. }
  22974. else if (ret == 0) {
  22975. ret = WOLFSSL_FATAL_ERROR;
  22976. }
  22977. }
  22978. /* At infinity if return == 1, otherwise return == 0. */
  22979. if (ret == 0) {
  22980. ret = wc_ecc_point_is_at_infinity(point);
  22981. }
  22982. /* Test bad args. */
  22983. if (ret == 0) {
  22984. ret = wc_ecc_point_is_at_infinity(NULL);
  22985. if (ret == BAD_FUNC_ARG) {
  22986. ret = 0;
  22987. }
  22988. else if (ret == 0) {
  22989. ret = WOLFSSL_FATAL_ERROR;
  22990. }
  22991. }
  22992. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  22993. #ifdef USE_ECC_B_PARAM
  22994. /* On curve if ret == 0 */
  22995. if (ret == 0) {
  22996. ret = wc_ecc_point_is_on_curve(point, idx);
  22997. }
  22998. /* Test bad args. */
  22999. if (ret == 0) {
  23000. ret = wc_ecc_point_is_on_curve(NULL, idx);
  23001. if (ret == BAD_FUNC_ARG) {
  23002. ret = wc_ecc_point_is_on_curve(point, 1000);
  23003. }
  23004. if (ret == ECC_BAD_ARG_E) {
  23005. ret = 0;
  23006. }
  23007. else if (ret == 0) {
  23008. ret = WOLFSSL_FATAL_ERROR;
  23009. }
  23010. }
  23011. #endif /* USE_ECC_B_PARAM */
  23012. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  23013. /* Free */
  23014. wc_ecc_del_point(point);
  23015. wc_ecc_del_point(cpypt);
  23016. wc_ecc_free(&key);
  23017. if (wc_FreeRng(&rng) && ret == 0) {
  23018. ret = WOLFSSL_FATAL_ERROR;
  23019. }
  23020. res = TEST_RES_CHECK(ret == 0);
  23021. #endif
  23022. return res;
  23023. } /* END test_wc_ecc_pointFns */
  23024. /*
  23025. * Testing wc_ecc_sahred_secret_ssh()
  23026. */
  23027. static int test_wc_ecc_shared_secret_ssh(void)
  23028. {
  23029. int res = TEST_SKIPPED;
  23030. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  23031. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  23032. !defined(WOLFSSL_ATECC608A)
  23033. ecc_key key, key2;
  23034. WC_RNG rng;
  23035. int ret;
  23036. int keySz = KEY32;
  23037. int key2Sz = KEY24;
  23038. byte secret[KEY32];
  23039. word32 secretLen = keySz;
  23040. /* Init stack variables. */
  23041. XMEMSET(secret, 0, secretLen);
  23042. XMEMSET(&rng, 0, sizeof(rng));
  23043. XMEMSET(&key, 0, sizeof(key));
  23044. XMEMSET(&key2, 0, sizeof(key2));
  23045. /* Make keys */
  23046. ret = wc_InitRng(&rng);
  23047. if (ret == 0) {
  23048. ret = wc_ecc_init(&key);
  23049. if (ret == 0) {
  23050. ret = wc_ecc_make_key(&rng, keySz, &key);
  23051. #if defined(WOLFSSL_ASYNC_CRYPT)
  23052. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23053. #endif
  23054. }
  23055. if (wc_FreeRng(&rng) && ret == 0) {
  23056. ret = WOLFSSL_FATAL_ERROR;
  23057. }
  23058. }
  23059. if (ret == 0) {
  23060. ret = wc_InitRng(&rng);
  23061. if (ret == 0) {
  23062. ret = wc_ecc_init(&key2);
  23063. }
  23064. if (ret == 0) {
  23065. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  23066. #if defined(WOLFSSL_ASYNC_CRYPT)
  23067. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  23068. #endif
  23069. }
  23070. }
  23071. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23072. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23073. !defined(HAVE_SELFTEST)
  23074. if (ret == 0) {
  23075. ret = wc_ecc_set_rng(&key, &rng);
  23076. }
  23077. #endif
  23078. if (ret == 0) {
  23079. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  23080. }
  23081. /* Pass in bad args. */
  23082. if (ret == 0) {
  23083. ret = wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret, &secretLen);
  23084. if (ret == BAD_FUNC_ARG) {
  23085. ret = wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen);
  23086. }
  23087. if (ret == BAD_FUNC_ARG) {
  23088. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen);
  23089. }
  23090. if (ret == BAD_FUNC_ARG) {
  23091. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL);
  23092. }
  23093. if (ret == BAD_FUNC_ARG) {
  23094. key.type = ECC_PUBLICKEY;
  23095. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  23096. if (ret == ECC_BAD_ARG_E) {
  23097. ret = 0;
  23098. }
  23099. else if (ret == 0) {
  23100. ret = WOLFSSL_FATAL_ERROR;
  23101. }
  23102. }
  23103. else if (ret == 0) {
  23104. ret = WOLFSSL_FATAL_ERROR;
  23105. }
  23106. }
  23107. if (wc_FreeRng(&rng) && ret == 0) {
  23108. ret = WOLFSSL_FATAL_ERROR;
  23109. }
  23110. wc_ecc_free(&key);
  23111. wc_ecc_free(&key2);
  23112. #ifdef FP_ECC
  23113. wc_ecc_fp_free();
  23114. #endif
  23115. res = TEST_RES_CHECK(ret == 0);
  23116. #endif
  23117. return res;
  23118. } /* END test_wc_ecc_shared_secret_ssh */
  23119. /*
  23120. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  23121. */
  23122. static int test_wc_ecc_verify_hash_ex(void)
  23123. {
  23124. int res = TEST_SKIPPED;
  23125. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  23126. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  23127. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
  23128. ecc_key key;
  23129. WC_RNG rng;
  23130. int ret;
  23131. mp_int r;
  23132. mp_int s;
  23133. mp_int z;
  23134. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  23135. unsigned char iHash[] = "Everyone gets Friday off.......";
  23136. unsigned char shortHash[] = TEST_STRING;
  23137. word32 hashlen = sizeof(hash);
  23138. word32 iHashLen = sizeof(iHash);
  23139. word32 shortHashLen = sizeof(shortHash);
  23140. int keySz = KEY32;
  23141. int sig = WOLFSSL_FATAL_ERROR;
  23142. int ver = WOLFSSL_FATAL_ERROR;
  23143. int verify_ok = 0;
  23144. /* Initialize r and s. */
  23145. ret = mp_init_multi(&r, &s, &z, NULL, NULL, NULL);
  23146. if (ret != MP_OKAY) {
  23147. return MP_INIT_E;
  23148. }
  23149. ret = wc_InitRng(&rng);
  23150. if (ret == 0) {
  23151. ret = wc_ecc_init(&key);
  23152. if (ret == 0) {
  23153. ret = wc_ecc_make_key(&rng, keySz, &key);
  23154. #if defined(WOLFSSL_ASYNC_CRYPT)
  23155. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23156. #endif
  23157. }
  23158. }
  23159. if (ret == 0) {
  23160. ret = wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s);
  23161. if (ret == 0) {
  23162. /* verify_ok should be 1. */
  23163. ret = wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key);
  23164. if (verify_ok != 1 && ret == 0) {
  23165. ret = WOLFSSL_FATAL_ERROR;
  23166. }
  23167. }
  23168. if (ret == 0) {
  23169. /* verify_ok should be 0 */
  23170. ret = wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen,
  23171. &verify_ok, &key);
  23172. if (verify_ok != 0 && ret == 0) {
  23173. ret = WOLFSSL_FATAL_ERROR;
  23174. }
  23175. }
  23176. if (ret == 0) {
  23177. /* verify_ok should be 0. */
  23178. ret = wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  23179. &verify_ok, &key);
  23180. if (verify_ok != 0 && ret == 0) {
  23181. ret = WOLFSSL_FATAL_ERROR;
  23182. }
  23183. }
  23184. }
  23185. /* Test bad args. */
  23186. if (ret == 0) {
  23187. if (wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s)
  23188. == ECC_BAD_ARG_E) {
  23189. sig = 0;
  23190. }
  23191. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s)
  23192. != ECC_BAD_ARG_E) {
  23193. sig = WOLFSSL_FATAL_ERROR;
  23194. }
  23195. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s)
  23196. != ECC_BAD_ARG_E) {
  23197. sig = WOLFSSL_FATAL_ERROR;
  23198. }
  23199. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s)
  23200. != ECC_BAD_ARG_E) {
  23201. sig = WOLFSSL_FATAL_ERROR;
  23202. }
  23203. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL)
  23204. != ECC_BAD_ARG_E) {
  23205. sig = WOLFSSL_FATAL_ERROR;
  23206. }
  23207. }
  23208. /* Test bad args. */
  23209. if (ret == 0) {
  23210. if (wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen, &verify_ok, &key)
  23211. == ECC_BAD_ARG_E) {
  23212. ver = 0;
  23213. }
  23214. if (ver == 0 && wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  23215. &verify_ok, &key) != ECC_BAD_ARG_E) {
  23216. ver = WOLFSSL_FATAL_ERROR;
  23217. }
  23218. if (wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen, &verify_ok, &key)
  23219. != MP_ZERO_E) {
  23220. ver = WOLFSSL_FATAL_ERROR;
  23221. }
  23222. if (wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen, &verify_ok, &key)
  23223. != MP_ZERO_E) {
  23224. ver = WOLFSSL_FATAL_ERROR;
  23225. }
  23226. if (wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen, &verify_ok, &key)
  23227. != MP_ZERO_E) {
  23228. ver = WOLFSSL_FATAL_ERROR;
  23229. }
  23230. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  23231. &key) != ECC_BAD_ARG_E) {
  23232. ver = WOLFSSL_FATAL_ERROR;
  23233. }
  23234. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  23235. NULL, &key) != ECC_BAD_ARG_E) {
  23236. ver = WOLFSSL_FATAL_ERROR;
  23237. }
  23238. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  23239. &verify_ok, NULL) != ECC_BAD_ARG_E) {
  23240. ver = WOLFSSL_FATAL_ERROR;
  23241. }
  23242. }
  23243. wc_ecc_free(&key);
  23244. mp_free(&r);
  23245. mp_free(&s);
  23246. if (wc_FreeRng(&rng)) {
  23247. return WOLFSSL_FATAL_ERROR;
  23248. }
  23249. if (ret == 0 && (sig != 0 || ver != 0)) {
  23250. ret = WOLFSSL_FATAL_ERROR;
  23251. }
  23252. res = TEST_RES_CHECK(ret == 0);
  23253. #endif
  23254. return res;
  23255. } /* END test_wc_ecc_verify_hash_ex */
  23256. /*
  23257. * Testing wc_ecc_mulmod()
  23258. */
  23259. static int test_wc_ecc_mulmod(void)
  23260. {
  23261. int res = TEST_SKIPPED;
  23262. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  23263. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  23264. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  23265. ecc_key key1, key2, key3;
  23266. WC_RNG rng;
  23267. int ret = 0;
  23268. ret = wc_InitRng(&rng);
  23269. if (ret == 0) {
  23270. ret = wc_ecc_init(&key1);
  23271. if (ret == 0) {
  23272. ret = wc_ecc_init(&key2);
  23273. }
  23274. if (ret == 0) {
  23275. ret = wc_ecc_init(&key3);
  23276. }
  23277. if (ret == 0) {
  23278. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  23279. #if defined(WOLFSSL_ASYNC_CRYPT)
  23280. ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
  23281. #endif
  23282. }
  23283. wc_FreeRng(&rng);
  23284. }
  23285. if (ret == 0) {
  23286. ret = wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy, key1.dp->Af,
  23287. ECC_SECP256R1);
  23288. if (ret == 0) {
  23289. ret = wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  23290. key1.dp->prime, ECC_SECP256R1);
  23291. }
  23292. }
  23293. if (ret == 0) {
  23294. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey, &key2.k,
  23295. &key3.k, 1);
  23296. }
  23297. /* Test bad args. */
  23298. if (ret == 0) {
  23299. ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey, &key2.k,
  23300. &key3.k, 1);
  23301. if (ret == ECC_BAD_ARG_E) {
  23302. ret = wc_ecc_mulmod(&key1.k, NULL, &key3.pubkey, &key2.k,
  23303. &key3.k, 1);
  23304. }
  23305. if (ret == ECC_BAD_ARG_E) {
  23306. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, NULL, &key2.k,
  23307. &key3.k, 1);
  23308. }
  23309. if (ret == ECC_BAD_ARG_E) {
  23310. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey,
  23311. &key2.k, NULL, 1);
  23312. }
  23313. if (ret == ECC_BAD_ARG_E) {
  23314. ret = 0;
  23315. }
  23316. else if (ret == 0) {
  23317. ret = WOLFSSL_FATAL_ERROR;
  23318. }
  23319. }
  23320. wc_ecc_free(&key1);
  23321. wc_ecc_free(&key2);
  23322. wc_ecc_free(&key3);
  23323. #ifdef FP_ECC
  23324. wc_ecc_fp_free();
  23325. #endif
  23326. res = TEST_RES_CHECK(ret == 0);
  23327. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  23328. return res;
  23329. } /* END test_wc_ecc_mulmod */
  23330. /*
  23331. * Testing wc_ecc_is_valid_idx()
  23332. */
  23333. static int test_wc_ecc_is_valid_idx(void)
  23334. {
  23335. int res = TEST_SKIPPED;
  23336. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  23337. ecc_key key;
  23338. WC_RNG rng;
  23339. int ret;
  23340. int iVal = -2;
  23341. int iVal2 = 3000;
  23342. XMEMSET(&rng, 0, sizeof(rng));
  23343. XMEMSET(&key, 0, sizeof(key));
  23344. ret = wc_InitRng(&rng);
  23345. if (ret == 0) {
  23346. ret = wc_ecc_init(&key);
  23347. if (ret == 0) {
  23348. ret = wc_ecc_make_key(&rng, 32, &key);
  23349. #if defined(WOLFSSL_ASYNC_CRYPT)
  23350. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23351. #endif
  23352. }
  23353. }
  23354. if (ret == 0) {
  23355. ret = wc_ecc_is_valid_idx(key.idx);
  23356. if (ret == 1) {
  23357. ret = 0;
  23358. }
  23359. else {
  23360. ret = WOLFSSL_FATAL_ERROR;
  23361. }
  23362. }
  23363. /* Test bad args. */
  23364. if (ret == 0) {
  23365. ret = wc_ecc_is_valid_idx(iVal); /* should return 0 */
  23366. if (ret == 0) {
  23367. ret = wc_ecc_is_valid_idx(iVal2);
  23368. }
  23369. if (ret != 0) {
  23370. ret = WOLFSSL_FATAL_ERROR;
  23371. }
  23372. }
  23373. if (wc_FreeRng(&rng) && ret == 0) {
  23374. ret = WOLFSSL_FATAL_ERROR;
  23375. }
  23376. wc_ecc_free(&key);
  23377. #ifdef FP_ECC
  23378. wc_ecc_fp_free();
  23379. #endif
  23380. res = TEST_RES_CHECK(ret == 0);
  23381. #endif
  23382. return res;
  23383. } /* END test_wc_ecc_is_valid_idx */
  23384. /*
  23385. * Testing wc_ecc_get_curve_id_from_oid()
  23386. */
  23387. static int test_wc_ecc_get_curve_id_from_oid(void)
  23388. {
  23389. int res = TEST_SKIPPED;
  23390. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  23391. !defined(HAVE_FIPS)
  23392. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  23393. word32 len = sizeof(oid);
  23394. int ret;
  23395. /* Bad Cases */
  23396. ret = wc_ecc_get_curve_id_from_oid(NULL, len);
  23397. if (ret == BAD_FUNC_ARG) {
  23398. ret = 0;
  23399. }
  23400. if (ret == 0) {
  23401. ret = wc_ecc_get_curve_id_from_oid(oid, 0);
  23402. if (ret == ECC_CURVE_INVALID) {
  23403. ret = 0;
  23404. }
  23405. }
  23406. /* Good Case */
  23407. if (ret == 0) {
  23408. ret = wc_ecc_get_curve_id_from_oid(oid, len);
  23409. if (ret == ECC_SECP256R1) {
  23410. ret = 0;
  23411. }
  23412. }
  23413. res = TEST_RES_CHECK(ret == 0);
  23414. #endif
  23415. return res;
  23416. }/* END test_wc_ecc_get_curve_id_from_oid */
  23417. /*
  23418. * Testing wc_ecc_sig_size_calc()
  23419. */
  23420. static int test_wc_ecc_sig_size_calc(void)
  23421. {
  23422. int res = TEST_SKIPPED;
  23423. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  23424. ecc_key key;
  23425. WC_RNG rng;
  23426. int sz = 0;
  23427. int ret = 0;
  23428. ret = wc_InitRng(&rng);
  23429. if (ret == 0) {
  23430. ret = wc_ecc_init(&key);
  23431. if (ret == 0) {
  23432. ret = wc_ecc_make_key(&rng, 16, &key);
  23433. #if defined(WOLFSSL_ASYNC_CRYPT)
  23434. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23435. #endif
  23436. }
  23437. sz = key.dp->size;
  23438. }
  23439. if (ret == 0) {
  23440. ret = wc_ecc_sig_size_calc(sz);
  23441. if (ret > 0) {
  23442. ret = 0;
  23443. }
  23444. }
  23445. wc_ecc_free(&key);
  23446. wc_FreeRng(&rng);
  23447. res = TEST_RES_CHECK(ret == 0);
  23448. #endif
  23449. return res;
  23450. } /* END test_wc_ecc_sig_size_calc */
  23451. /*
  23452. * Testing ToTraditional
  23453. */
  23454. static int test_ToTraditional(void)
  23455. {
  23456. int res = TEST_SKIPPED;
  23457. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  23458. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  23459. defined(OPENSSL_EXTRA_X509_SMALL))
  23460. XFILE f;
  23461. byte input[TWOK_BUF];
  23462. word32 sz;
  23463. int ret;
  23464. f = XFOPEN("./certs/server-keyPkcs8.der", "rb");
  23465. AssertTrue((f != XBADFILE));
  23466. sz = (word32)XFREAD(input, 1, sizeof(input), f);
  23467. XFCLOSE(f);
  23468. /* Good case */
  23469. ret = ToTraditional(input, sz);
  23470. if (ret > 0) {
  23471. ret = 0;
  23472. }
  23473. /* Bad cases */
  23474. if (ret == 0) {
  23475. ret = ToTraditional(NULL, 0);
  23476. if (ret == BAD_FUNC_ARG) {
  23477. ret = 0;
  23478. }
  23479. }
  23480. if (ret == 0) {
  23481. ret = ToTraditional(NULL, sz);
  23482. if (ret == BAD_FUNC_ARG) {
  23483. ret = 0;
  23484. }
  23485. }
  23486. if (ret == 0) {
  23487. ret = ToTraditional(input, 0);
  23488. if (ret == ASN_PARSE_E || ret == BUFFER_E) {
  23489. ret = 0;
  23490. }
  23491. }
  23492. res = TEST_RES_CHECK(ret == 0);
  23493. #endif
  23494. return res;
  23495. }/* End test_ToTraditional*/
  23496. /*
  23497. * Testing wc_EccPrivateKeyToDer
  23498. */
  23499. static int test_wc_EccPrivateKeyToDer(void)
  23500. {
  23501. int res = TEST_SKIPPED;
  23502. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  23503. byte output[ONEK_BUF];
  23504. ecc_key eccKey;
  23505. WC_RNG rng;
  23506. word32 inLen;
  23507. int ret;
  23508. ret = wc_InitRng(&rng);
  23509. if (ret == 0) {
  23510. ret = wc_ecc_init(&eccKey);
  23511. if (ret == 0) {
  23512. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23513. #if defined(WOLFSSL_ASYNC_CRYPT)
  23514. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23515. #endif
  23516. }
  23517. inLen = (word32)sizeof(output);
  23518. /* Bad Cases */
  23519. if (ret == 0) {
  23520. ret = wc_EccPrivateKeyToDer(NULL, NULL, 0);
  23521. if (ret == BAD_FUNC_ARG) {
  23522. ret = 0;
  23523. }
  23524. }
  23525. if (ret == 0) {
  23526. ret = wc_EccPrivateKeyToDer(NULL, output, inLen);
  23527. if (ret == BAD_FUNC_ARG) {
  23528. ret = 0;
  23529. }
  23530. }
  23531. if (ret == 0) {
  23532. ret = wc_EccPrivateKeyToDer(&eccKey, NULL, inLen);
  23533. if (ret == LENGTH_ONLY_E) {
  23534. ret = 0;
  23535. }
  23536. }
  23537. if (ret == 0) {
  23538. ret = wc_EccPrivateKeyToDer(&eccKey, output, 0);
  23539. if (ret == BAD_FUNC_ARG) {
  23540. ret = 0;
  23541. }
  23542. }
  23543. /*Good Case */
  23544. if (ret == 0) {
  23545. ret = wc_EccPrivateKeyToDer(&eccKey, output, inLen);
  23546. if (ret > 0) {
  23547. #if defined(OPENSSL_EXTRA) && defined(HAVE_ALL_CURVES)
  23548. /* test importing private only into a PKEY struct */
  23549. EC_KEY* ec;
  23550. EVP_PKEY* pkey;
  23551. const unsigned char* der = output;
  23552. pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &der, ret);
  23553. AssertNotNull(pkey);
  23554. der = output;
  23555. ec = d2i_ECPrivateKey(NULL, &der, ret);
  23556. AssertNotNull(ec);
  23557. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ec), SSL_SUCCESS);
  23558. EVP_PKEY_free(pkey); /* EC_KEY should be free'd by free'ing pkey */
  23559. #endif
  23560. ret = 0;
  23561. }
  23562. }
  23563. wc_ecc_free(&eccKey);
  23564. }
  23565. wc_FreeRng(&rng);
  23566. res = TEST_RES_CHECK(ret == 0);
  23567. #endif
  23568. return res;
  23569. }/* End test_wc_EccPrivateKeyToDer*/
  23570. /*
  23571. * Testing wc_DhPublicKeyDecode
  23572. */
  23573. static int test_wc_DhPublicKeyDecode(void)
  23574. {
  23575. int res = TEST_SKIPPED;
  23576. #ifndef NO_DH
  23577. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  23578. DhKey key;
  23579. word32 inOutIdx;
  23580. AssertIntEQ(wc_InitDhKey(&key), 0);
  23581. AssertIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0),
  23582. BAD_FUNC_ARG);
  23583. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23584. BAD_FUNC_ARG);
  23585. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23586. BAD_FUNC_ARG);
  23587. inOutIdx = 0;
  23588. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  23589. BAD_FUNC_ARG);
  23590. inOutIdx = 0;
  23591. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  23592. BAD_FUNC_ARG);
  23593. inOutIdx = 0;
  23594. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  23595. sizeof_dh_pub_key_der_2048), 0);
  23596. AssertTrue(key.p.used != 0 && key.g.used != 0 && key.q.used == 0 &&
  23597. key.pub.used != 0 && key.priv.used == 0);
  23598. wc_FreeDhKey(&key);
  23599. res = TEST_RES_CHECK(1);
  23600. #endif
  23601. #endif /* !NO_DH */
  23602. return res;
  23603. }
  23604. /*
  23605. * Testing wc_Ed25519KeyToDer
  23606. */
  23607. static int test_wc_Ed25519KeyToDer(void)
  23608. {
  23609. int res = TEST_SKIPPED;
  23610. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23611. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23612. byte output[ONEK_BUF];
  23613. ed25519_key ed25519Key;
  23614. WC_RNG rng;
  23615. word32 inLen;
  23616. int ret;
  23617. ret = wc_InitRng(&rng);
  23618. if (ret == 0) {
  23619. ret = wc_ed25519_init(&ed25519Key);
  23620. if (ret == 0) {
  23621. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  23622. }
  23623. inLen = (word32)sizeof(output);
  23624. /* Bad Cases */
  23625. if (ret == 0) {
  23626. ret = wc_Ed25519KeyToDer(NULL, NULL, 0);
  23627. if (ret == BAD_FUNC_ARG) {
  23628. ret = 0;
  23629. }
  23630. }
  23631. if (ret == 0) {
  23632. ret = wc_Ed25519KeyToDer(NULL, output, inLen);
  23633. if (ret == BAD_FUNC_ARG) {
  23634. ret = 0;
  23635. }
  23636. }
  23637. if (ret == 0) {
  23638. ret = wc_Ed25519KeyToDer(&ed25519Key, output, 0);
  23639. if (ret == BAD_FUNC_ARG) {
  23640. ret = 0;
  23641. }
  23642. }
  23643. /* Good Cases */
  23644. if (ret == 0) {
  23645. /* length only */
  23646. ret = wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen);
  23647. if (ret > 0) {
  23648. ret = 0;
  23649. }
  23650. }
  23651. if (ret == 0) {
  23652. ret = wc_Ed25519KeyToDer(&ed25519Key, output, inLen);
  23653. if (ret > 0) {
  23654. ret = 0;
  23655. }
  23656. }
  23657. wc_ed25519_free(&ed25519Key);
  23658. }
  23659. wc_FreeRng(&rng);
  23660. res = TEST_RES_CHECK(ret == 0);
  23661. #endif
  23662. return res;
  23663. }/* End test_wc_Ed25519KeyToDer*/
  23664. /*
  23665. * Testing wc_Ed25519PrivateKeyToDer
  23666. */
  23667. static int test_wc_Ed25519PrivateKeyToDer(void)
  23668. {
  23669. int res = TEST_SKIPPED;
  23670. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23671. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23672. byte output[ONEK_BUF];
  23673. ed25519_key ed25519PrivKey;
  23674. WC_RNG rng;
  23675. word32 inLen;
  23676. int ret;
  23677. ret = wc_InitRng(&rng);
  23678. if (ret == 0) {
  23679. ret = wc_ed25519_init(&ed25519PrivKey);
  23680. if (ret == 0) {
  23681. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey);
  23682. }
  23683. inLen = (word32)sizeof(output);
  23684. /* Bad Cases */
  23685. if (ret == 0) {
  23686. ret = wc_Ed25519PrivateKeyToDer(NULL, NULL, 0);
  23687. if (ret == BAD_FUNC_ARG) {
  23688. ret = 0;
  23689. }
  23690. }
  23691. if (ret == 0) {
  23692. ret = wc_Ed25519PrivateKeyToDer(NULL, output, inLen);
  23693. if (ret == BAD_FUNC_ARG) {
  23694. ret = 0;
  23695. }
  23696. }
  23697. if (ret == 0) {
  23698. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0);
  23699. if (ret == BAD_FUNC_ARG) {
  23700. ret = 0;
  23701. }
  23702. }
  23703. /* Good Cases */
  23704. if (ret == 0) {
  23705. /* length only */
  23706. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen);
  23707. if (ret > 0) {
  23708. ret = 0;
  23709. }
  23710. }
  23711. if (ret == 0) {
  23712. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen);
  23713. if (ret > 0) {
  23714. ret = 0;
  23715. }
  23716. }
  23717. wc_ed25519_free(&ed25519PrivKey);
  23718. }
  23719. wc_FreeRng(&rng);
  23720. res = TEST_RES_CHECK(ret == 0);
  23721. #endif
  23722. return res;
  23723. }/* End test_wc_Ed25519PrivateKeyToDer*/
  23724. /*
  23725. * Testing wc_Ed448KeyToDer
  23726. */
  23727. static int test_wc_Ed448KeyToDer(void)
  23728. {
  23729. int res = TEST_SKIPPED;
  23730. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23731. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23732. byte output[ONEK_BUF];
  23733. ed448_key ed448Key;
  23734. WC_RNG rng;
  23735. word32 inLen;
  23736. int ret;
  23737. ret = wc_InitRng(&rng);
  23738. if (ret == 0) {
  23739. ret = wc_ed448_init(&ed448Key);
  23740. if (ret == 0) {
  23741. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  23742. }
  23743. inLen = sizeof(output);
  23744. /* Bad Cases */
  23745. if (ret == 0) {
  23746. ret = wc_Ed448KeyToDer(NULL, NULL, 0);
  23747. if (ret == BAD_FUNC_ARG) {
  23748. ret = 0;
  23749. }
  23750. }
  23751. if (ret == 0) {
  23752. ret = wc_Ed448KeyToDer(NULL, output, inLen);
  23753. if (ret == BAD_FUNC_ARG) {
  23754. ret = 0;
  23755. }
  23756. }
  23757. if (ret == 0) {
  23758. ret = wc_Ed448KeyToDer(&ed448Key, output, 0);
  23759. if (ret == BAD_FUNC_ARG) {
  23760. ret = 0;
  23761. }
  23762. }
  23763. /* Good Cases */
  23764. if (ret == 0) {
  23765. /* length only */
  23766. ret = wc_Ed448KeyToDer(&ed448Key, NULL, inLen);
  23767. if (ret > 0) {
  23768. ret = 0;
  23769. }
  23770. }
  23771. if (ret == 0) {
  23772. ret = wc_Ed448KeyToDer(&ed448Key, output, inLen);
  23773. if (ret > 0) {
  23774. ret = 0;
  23775. }
  23776. }
  23777. wc_ed448_free(&ed448Key);
  23778. }
  23779. wc_FreeRng(&rng);
  23780. res = TEST_RES_CHECK(ret == 0);
  23781. #endif
  23782. return res;
  23783. }/* End test_wc_Ed448KeyToDer*/
  23784. /*
  23785. * Testing wc_Ed448PrivateKeyToDer
  23786. */
  23787. static int test_wc_Ed448PrivateKeyToDer(void)
  23788. {
  23789. int res = TEST_SKIPPED;
  23790. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23791. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23792. byte output[ONEK_BUF];
  23793. ed448_key ed448PrivKey;
  23794. WC_RNG rng;
  23795. word32 inLen;
  23796. int ret;
  23797. ret = wc_InitRng(&rng);
  23798. if (ret == 0) {
  23799. ret = wc_ed448_init(&ed448PrivKey);
  23800. if (ret == 0) {
  23801. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey);
  23802. }
  23803. inLen = sizeof(output);
  23804. /* Bad Cases */
  23805. if (ret == 0) {
  23806. ret = wc_Ed448PrivateKeyToDer(NULL, NULL, 0);
  23807. if (ret == BAD_FUNC_ARG) {
  23808. ret = 0;
  23809. }
  23810. }
  23811. if (ret == 0) {
  23812. ret = wc_Ed448PrivateKeyToDer(NULL, output, inLen);
  23813. if (ret == BAD_FUNC_ARG) {
  23814. ret = 0;
  23815. }
  23816. }
  23817. if (ret == 0) {
  23818. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0);
  23819. if (ret == BAD_FUNC_ARG) {
  23820. ret = 0;
  23821. }
  23822. }
  23823. /* Good cases */
  23824. if (ret == 0) {
  23825. /* length only */
  23826. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen);
  23827. if (ret > 0) {
  23828. ret = 0;
  23829. }
  23830. }
  23831. if (ret == 0) {
  23832. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen);
  23833. if (ret > 0) {
  23834. ret = 0;
  23835. }
  23836. }
  23837. wc_ed448_free(&ed448PrivKey);
  23838. }
  23839. wc_FreeRng(&rng);
  23840. res = TEST_RES_CHECK(ret == 0);
  23841. #endif
  23842. return res;
  23843. }/* End test_wc_Ed448PrivateKeyToDer*/
  23844. /*
  23845. * Testing wc_SetSubjectBuffer
  23846. */
  23847. static int test_wc_SetSubjectBuffer(void)
  23848. {
  23849. int res = TEST_SKIPPED;
  23850. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  23851. Cert cert;
  23852. FILE* file;
  23853. byte* der;
  23854. word32 derSz;
  23855. int ret = 0;
  23856. derSz = FOURK_BUF;
  23857. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23858. if (der == NULL) {
  23859. ret = -1;
  23860. }
  23861. if (ret == 0) {
  23862. file = XFOPEN("./certs/ca-cert.der", "rb");
  23863. if (file != NULL) {
  23864. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  23865. XFCLOSE(file);
  23866. }
  23867. else {
  23868. ret = -1;
  23869. }
  23870. }
  23871. if (ret == 0) {
  23872. ret = wc_InitCert(&cert);
  23873. }
  23874. if (ret == 0) {
  23875. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  23876. }
  23877. if (ret == 0) {
  23878. ret = wc_SetSubjectBuffer(NULL, der, derSz);
  23879. if (ret == BAD_FUNC_ARG) {
  23880. ret = 0;
  23881. }
  23882. }
  23883. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23884. res = TEST_RES_CHECK(ret == 0);
  23885. #endif
  23886. return res;
  23887. }/* End test_wc_SetSubjectBuffer*/
  23888. /*
  23889. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  23890. */
  23891. static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
  23892. {
  23893. int res = TEST_SKIPPED;
  23894. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23895. WC_RNG rng;
  23896. Cert cert;
  23897. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23898. ed25519_key ed25519Key;
  23899. #endif
  23900. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23901. RsaKey rsaKey;
  23902. int bits = 2048;
  23903. #endif
  23904. #if defined(HAVE_ECC)
  23905. ecc_key eccKey;
  23906. #endif
  23907. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23908. ed448_key ed448Key;
  23909. #endif
  23910. int ret = 0;
  23911. #ifndef HAVE_FIPS
  23912. ret = wc_InitRng_ex(&rng, HEAP_HINT, testDevId);
  23913. #else
  23914. ret = wc_InitRng(&rng);
  23915. #endif
  23916. wc_InitCert(&cert);
  23917. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23918. if (ret == 0) { /*ED25519*/
  23919. ret = wc_ed25519_init(&ed25519Key);
  23920. if (ret == 0) {
  23921. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  23922. }
  23923. if (ret == 0) {
  23924. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  23925. &ed25519Key);
  23926. }
  23927. wc_ed25519_free(&ed25519Key);
  23928. }
  23929. #endif
  23930. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  23931. if (ret == 0) { /*RSA*/
  23932. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  23933. if (ret == 0) {
  23934. MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
  23935. }
  23936. if (ret == 0) {
  23937. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
  23938. }
  23939. wc_FreeRsaKey(&rsaKey);
  23940. }
  23941. #endif
  23942. #if defined(HAVE_ECC)
  23943. if (ret == 0) { /*ECC*/
  23944. ret = wc_ecc_init(&eccKey);
  23945. if (ret == 0) {
  23946. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23947. #if defined(WOLFSSL_ASYNC_CRYPT)
  23948. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23949. #endif
  23950. }
  23951. if (ret == 0) {
  23952. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
  23953. }
  23954. wc_ecc_free(&eccKey);
  23955. }
  23956. #endif
  23957. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23958. if (ret == 0) { /*ED448*/
  23959. ret = wc_ed448_init(&ed448Key);
  23960. if (ret == 0) {
  23961. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  23962. }
  23963. if (ret == 0) {
  23964. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  23965. &ed448Key);
  23966. }
  23967. wc_ed448_free(&ed448Key);
  23968. }
  23969. #endif
  23970. wc_FreeRng(&rng);
  23971. res = TEST_RES_CHECK(ret == 0);
  23972. #endif
  23973. return res;
  23974. }/* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  23975. /*
  23976. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  23977. */
  23978. static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
  23979. {
  23980. int res = TEST_SKIPPED;
  23981. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23982. WC_RNG rng;
  23983. Cert cert;
  23984. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23985. ed25519_key ed25519Key;
  23986. #endif
  23987. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23988. RsaKey rsaKey;
  23989. int bits = 2048;
  23990. #endif
  23991. #if defined(HAVE_ECC)
  23992. ecc_key eccKey;
  23993. #endif
  23994. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23995. ed448_key ed448Key;
  23996. #endif
  23997. int ret = 0;
  23998. #ifndef HAVE_FIPS
  23999. ret = wc_InitRng_ex(&rng, HEAP_HINT, testDevId);
  24000. #else
  24001. ret = wc_InitRng(&rng);
  24002. #endif
  24003. wc_InitCert(&cert);
  24004. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  24005. if (ret == 0) { /*ED25519*/
  24006. ret = wc_ed25519_init(&ed25519Key);
  24007. if (ret == 0) {
  24008. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  24009. }
  24010. if (ret == 0) {
  24011. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  24012. &ed25519Key);
  24013. }
  24014. wc_ed25519_free(&ed25519Key);
  24015. }
  24016. #endif
  24017. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  24018. if (ret == 0) { /*RSA*/
  24019. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  24020. if (ret == 0) {
  24021. MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
  24022. }
  24023. if (ret == 0) {
  24024. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
  24025. }
  24026. wc_FreeRsaKey(&rsaKey);
  24027. }
  24028. #endif
  24029. #if defined(HAVE_ECC)
  24030. if (ret == 0) { /*ECC*/
  24031. ret = wc_ecc_init(&eccKey);
  24032. if (ret == 0) {
  24033. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  24034. #if defined(WOLFSSL_ASYNC_CRYPT)
  24035. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  24036. #endif
  24037. }
  24038. if (ret == 0) {
  24039. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
  24040. }
  24041. wc_ecc_free(&eccKey);
  24042. }
  24043. #endif
  24044. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  24045. if (ret == 0) { /*ED448*/
  24046. ret = wc_ed448_init(&ed448Key);
  24047. if (ret == 0) {
  24048. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  24049. }
  24050. if (ret == 0) {
  24051. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  24052. &ed448Key);
  24053. }
  24054. wc_ed448_free(&ed448Key);
  24055. }
  24056. #endif
  24057. wc_FreeRng(&rng);
  24058. res = TEST_RES_CHECK(ret == 0);
  24059. #endif /*defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  24060. return res;
  24061. }/* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  24062. /*
  24063. * Testing wc_PKCS7_New()
  24064. */
  24065. static int test_wc_PKCS7_New(void)
  24066. {
  24067. int res = TEST_SKIPPED;
  24068. #if defined(HAVE_PKCS7)
  24069. PKCS7* pkcs7;
  24070. pkcs7 = wc_PKCS7_New(NULL, testDevId);
  24071. wc_PKCS7_Free(pkcs7);
  24072. res = TEST_RES_CHECK(pkcs7 != NULL);
  24073. #endif
  24074. return res;
  24075. } /* END test-wc_PKCS7_New */
  24076. /*
  24077. * Testing wc_PKCS7_Init()
  24078. */
  24079. static int test_wc_PKCS7_Init(void)
  24080. {
  24081. int res = TEST_SKIPPED;
  24082. #if defined(HAVE_PKCS7)
  24083. PKCS7* pkcs7;
  24084. void* heap = NULL;
  24085. pkcs7 = wc_PKCS7_New(heap, testDevId);
  24086. AssertNotNull(pkcs7);
  24087. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  24088. /* Pass in bad args. */
  24089. AssertIntEQ(wc_PKCS7_Init(NULL, heap, testDevId), BAD_FUNC_ARG);
  24090. wc_PKCS7_Free(pkcs7);
  24091. res = TEST_RES_CHECK(1);
  24092. #endif
  24093. return res;
  24094. } /* END test-wc_PKCS7_Init */
  24095. /*
  24096. * Testing wc_PKCS7_InitWithCert()
  24097. */
  24098. static int test_wc_PKCS7_InitWithCert(void)
  24099. {
  24100. int res = TEST_SKIPPED;
  24101. #if defined(HAVE_PKCS7)
  24102. PKCS7* pkcs7;
  24103. #ifndef NO_RSA
  24104. #if defined(USE_CERT_BUFFERS_2048)
  24105. unsigned char cert[sizeof(client_cert_der_2048)];
  24106. int certSz = (int)sizeof(cert);
  24107. XMEMSET(cert, 0, certSz);
  24108. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  24109. #elif defined(USE_CERT_BUFFERS_1024)
  24110. unsigned char cert[sizeof(client_cert_der_1024)];
  24111. int certSz = (int)sizeof(cert);
  24112. XMEMSET(cert, 0, certSz);
  24113. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  24114. #else
  24115. unsigned char cert[ONEK_BUF];
  24116. XFILE fp;
  24117. int certSz;
  24118. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  24119. AssertTrue(fp != XBADFILE);
  24120. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  24121. XFCLOSE(fp);
  24122. #endif
  24123. #elif defined(HAVE_ECC)
  24124. #if defined(USE_CERT_BUFFERS_256)
  24125. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24126. int certSz = (int)sizeof(cert);
  24127. XMEMSET(cert, 0, certSz);
  24128. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  24129. #else
  24130. unsigned char cert[ONEK_BUF];
  24131. XFILE fp;
  24132. int certSz;
  24133. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  24134. AssertTrue(fp != XBADFILE);
  24135. certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256), fp);
  24136. XFCLOSE(fp);
  24137. #endif
  24138. #else
  24139. #error PKCS7 requires ECC or RSA
  24140. #endif
  24141. #ifdef HAVE_ECC
  24142. {
  24143. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  24144. static unsigned char certWithInvalidEccKey[] = {
  24145. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  24146. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  24147. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  24148. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  24149. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  24150. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  24151. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  24152. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  24153. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  24154. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  24155. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  24156. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  24157. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  24158. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  24159. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  24160. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  24161. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  24162. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  24163. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  24164. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  24165. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  24166. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  24167. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  24168. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  24169. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  24170. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  24171. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  24172. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  24173. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  24174. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  24175. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  24176. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  24177. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  24178. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  24179. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  24180. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  24181. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  24182. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  24183. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  24184. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  24185. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  24186. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  24187. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  24188. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  24189. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  24190. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  24191. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  24192. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  24193. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  24194. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  24195. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  24196. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  24197. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  24198. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  24199. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  24200. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  24201. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  24202. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  24203. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  24204. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  24205. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  24206. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  24207. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  24208. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  24209. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  24210. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  24211. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  24212. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  24213. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  24214. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  24215. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  24216. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  24217. 0x08, 0xA8, 0xB4};
  24218. #endif
  24219. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24220. /* If initialization is not successful, it's free'd in init func. */
  24221. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  24222. wc_PKCS7_Free(pkcs7);
  24223. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24224. /* Valid initialization usage. */
  24225. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24226. /* Pass in bad args. No need free for null checks, free at end.*/
  24227. AssertIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  24228. BAD_FUNC_ARG);
  24229. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  24230. BAD_FUNC_ARG);
  24231. #ifdef HAVE_ECC
  24232. AssertIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  24233. sizeof(certWithInvalidEccKey)), 0);
  24234. }
  24235. #endif
  24236. wc_PKCS7_Free(pkcs7);
  24237. res = TEST_RES_CHECK(1);
  24238. #endif
  24239. return res;
  24240. } /* END test_wc_PKCS7_InitWithCert */
  24241. /*
  24242. * Testing wc_PKCS7_EncodeData()
  24243. */
  24244. static int test_wc_PKCS7_EncodeData(void)
  24245. {
  24246. int res = TEST_SKIPPED;
  24247. #if defined(HAVE_PKCS7)
  24248. PKCS7* pkcs7;
  24249. byte output[FOURK_BUF];
  24250. byte data[] = "My encoded DER cert.";
  24251. #ifndef NO_RSA
  24252. #if defined(USE_CERT_BUFFERS_2048)
  24253. unsigned char cert[sizeof(client_cert_der_2048)];
  24254. unsigned char key[sizeof(client_key_der_2048)];
  24255. int certSz = (int)sizeof(cert);
  24256. int keySz = (int)sizeof(key);
  24257. XMEMSET(cert, 0, certSz);
  24258. XMEMSET(key, 0, keySz);
  24259. XMEMCPY(cert, client_cert_der_2048, certSz);
  24260. XMEMCPY(key, client_key_der_2048, keySz);
  24261. #elif defined(USE_CERT_BUFFERS_1024)
  24262. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  24263. unsigned char key[sizeof_client_key_der_1024];
  24264. int certSz = (int)sizeof(cert);
  24265. int keySz = (int)sizeof(key);
  24266. XMEMSET(cert, 0, certSz);
  24267. XMEMSET(key, 0, keySz);
  24268. XMEMCPY(cert, client_cert_der_1024, certSz);
  24269. XMEMCPY(key, client_key_der_1024, keySz);
  24270. #else
  24271. unsigned char cert[ONEK_BUF];
  24272. unsigned char key[ONEK_BUF];
  24273. XFILE fp;
  24274. int certSz;
  24275. int keySz;
  24276. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  24277. AssertTrue(fp != XBADFILE);
  24278. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  24279. XFCLOSE(fp);
  24280. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  24281. AssertTrue(fp != XBADFILE);
  24282. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  24283. XFCLOSE(fp);
  24284. #endif
  24285. #elif defined(HAVE_ECC)
  24286. #if defined(USE_CERT_BUFFERS_256)
  24287. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24288. unsigned char key[sizeof(ecc_clikey_der_256)];
  24289. int certSz = (int)sizeof(cert);
  24290. int keySz = (int)sizeof(key);
  24291. XMEMSET(cert, 0, certSz);
  24292. XMEMSET(key, 0, keySz);
  24293. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  24294. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  24295. #else
  24296. unsigned char cert[ONEK_BUF];
  24297. unsigned char key[ONEK_BUF];
  24298. XFILE fp;
  24299. int certSz, keySz;
  24300. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  24301. AssertTrue(fp != XBADFILE);
  24302. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  24303. XFCLOSE(fp);
  24304. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  24305. AssertTrue(fp != XBADFILE);
  24306. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  24307. XFCLOSE(fp);
  24308. #endif
  24309. #endif
  24310. XMEMSET(output, 0, sizeof(output));
  24311. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24312. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24313. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, certSz), 0);
  24314. pkcs7->content = data;
  24315. pkcs7->contentSz = sizeof(data);
  24316. pkcs7->privateKey = key;
  24317. pkcs7->privateKeySz = keySz;
  24318. AssertIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  24319. /* Test bad args. */
  24320. AssertIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  24321. BAD_FUNC_ARG);
  24322. AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  24323. BAD_FUNC_ARG);
  24324. AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  24325. wc_PKCS7_Free(pkcs7);
  24326. res = TEST_RES_CHECK(1);
  24327. #endif
  24328. return res;
  24329. } /* END test_wc_PKCS7_EncodeData */
  24330. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  24331. !defined(NO_RSA) && !defined(NO_SHA256)
  24332. /* RSA sign raw digest callback */
  24333. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  24334. byte* out, word32 outSz, byte* privateKey,
  24335. word32 privateKeySz, int devid, int hashOID)
  24336. {
  24337. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  24338. byte digInfoEncoding[] = {
  24339. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  24340. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  24341. 0x00, 0x04, 0x20
  24342. };
  24343. int ret;
  24344. byte digestInfo[ONEK_BUF];
  24345. byte sig[FOURK_BUF];
  24346. word32 digestInfoSz = 0;
  24347. word32 idx = 0;
  24348. RsaKey rsa;
  24349. /* SHA-256 required only for this example callback due to above
  24350. * digInfoEncoding[] */
  24351. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  24352. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  24353. (hashOID != SHA256h)) {
  24354. return -1;
  24355. }
  24356. /* build DigestInfo */
  24357. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  24358. digestInfoSz += sizeof(digInfoEncoding);
  24359. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  24360. digestInfoSz += digestSz;
  24361. /* set up RSA key */
  24362. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  24363. if (ret != 0) {
  24364. return ret;
  24365. }
  24366. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  24367. /* sign DigestInfo */
  24368. if (ret == 0) {
  24369. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  24370. &rsa, pkcs7->rng);
  24371. if (ret > 0) {
  24372. if (ret > (int)outSz) {
  24373. /* output buffer too small */
  24374. ret = -1;
  24375. }
  24376. else {
  24377. /* success, ret holds sig size */
  24378. XMEMCPY(out, sig, ret);
  24379. }
  24380. }
  24381. }
  24382. wc_FreeRsaKey(&rsa);
  24383. return ret;
  24384. }
  24385. #endif
  24386. /*
  24387. * Testing wc_PKCS7_EncodeSignedData()
  24388. */
  24389. static int test_wc_PKCS7_EncodeSignedData(void)
  24390. {
  24391. int res = TEST_SKIPPED;
  24392. #if defined(HAVE_PKCS7)
  24393. PKCS7* pkcs7;
  24394. WC_RNG rng;
  24395. byte output[FOURK_BUF];
  24396. byte badOut[1];
  24397. word32 outputSz = (word32)sizeof(output);
  24398. word32 badOutSz = 0;
  24399. byte data[] = "Test data to encode.";
  24400. #ifndef NO_RSA
  24401. #if defined(USE_CERT_BUFFERS_2048)
  24402. byte key[sizeof(client_key_der_2048)];
  24403. byte cert[sizeof(client_cert_der_2048)];
  24404. word32 keySz = (word32)sizeof(key);
  24405. word32 certSz = (word32)sizeof(cert);
  24406. XMEMSET(key, 0, keySz);
  24407. XMEMSET(cert, 0, certSz);
  24408. XMEMCPY(key, client_key_der_2048, keySz);
  24409. XMEMCPY(cert, client_cert_der_2048, certSz);
  24410. #elif defined(USE_CERT_BUFFERS_1024)
  24411. byte key[sizeof_client_key_der_1024];
  24412. byte cert[sizeof(sizeof_client_cert_der_1024)];
  24413. word32 keySz = (word32)sizeof(key);
  24414. word32 certSz = (word32)sizeof(cert);
  24415. XMEMSET(key, 0, keySz);
  24416. XMEMSET(cert, 0, certSz);
  24417. XMEMCPY(key, client_key_der_1024, keySz);
  24418. XMEMCPY(cert, client_cert_der_1024, certSz);
  24419. #else
  24420. unsigned char cert[ONEK_BUF];
  24421. unsigned char key[ONEK_BUF];
  24422. XFILE fp;
  24423. int certSz;
  24424. int keySz;
  24425. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  24426. AssertTrue(fp != XBADFILE);
  24427. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  24428. XFCLOSE(fp);
  24429. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  24430. AssertTrue(fp != XBADFILE);
  24431. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  24432. XFCLOSE(fp);
  24433. #endif
  24434. #elif defined(HAVE_ECC)
  24435. #if defined(USE_CERT_BUFFERS_256)
  24436. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24437. unsigned char key[sizeof(ecc_clikey_der_256)];
  24438. int certSz = (int)sizeof(cert);
  24439. int keySz = (int)sizeof(key);
  24440. XMEMSET(cert, 0, certSz);
  24441. XMEMSET(key, 0, keySz);
  24442. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  24443. XMEMCPY(key, ecc_clikey_der_256, keySz);
  24444. #else
  24445. unsigned char cert[ONEK_BUF];
  24446. unsigned char key[ONEK_BUF];
  24447. XFILE fp;
  24448. int certSz, keySz;
  24449. fp = XOPEN("./certs/client-ecc-cert.der", "rb");
  24450. AssertTrue(fp != XBADFILE);
  24451. certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp);
  24452. XFCLOSE(fp);
  24453. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  24454. AssertTrue(fp != XBADFILE);
  24455. keySz = (int)XFREAD(key, 1, ONEK_BUF, fp);
  24456. XFCLOSE(fp);
  24457. #endif
  24458. #endif
  24459. XMEMSET(output, 0, outputSz);
  24460. AssertIntEQ(wc_InitRng(&rng), 0);
  24461. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24462. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24463. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24464. pkcs7->content = data;
  24465. pkcs7->contentSz = (word32)sizeof(data);
  24466. pkcs7->privateKey = key;
  24467. pkcs7->privateKeySz = (word32)sizeof(key);
  24468. pkcs7->encryptOID = RSAk;
  24469. pkcs7->hashOID = SHAh;
  24470. pkcs7->rng = &rng;
  24471. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  24472. wc_PKCS7_Free(pkcs7);
  24473. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24474. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24475. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24476. /* Pass in bad args. */
  24477. AssertIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz), BAD_FUNC_ARG);
  24478. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  24479. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  24480. badOutSz), BAD_FUNC_ARG);
  24481. pkcs7->hashOID = 0; /* bad hashOID */
  24482. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), BAD_FUNC_ARG);
  24483. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  24484. !defined(NO_RSA) && !defined(NO_SHA256)
  24485. /* test RSA sign raw digest callback, if using RSA and compiled in.
  24486. * Example callback assumes SHA-256, so only run test if compiled in. */
  24487. wc_PKCS7_Free(pkcs7);
  24488. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24489. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24490. pkcs7->content = data;
  24491. pkcs7->contentSz = (word32)sizeof(data);
  24492. pkcs7->privateKey = key;
  24493. pkcs7->privateKeySz = (word32)sizeof(key);
  24494. pkcs7->encryptOID = RSAk;
  24495. pkcs7->hashOID = SHA256h;
  24496. pkcs7->rng = &rng;
  24497. AssertIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  24498. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  24499. #endif
  24500. wc_PKCS7_Free(pkcs7);
  24501. wc_FreeRng(&rng);
  24502. res = TEST_RES_CHECK(1);
  24503. #endif
  24504. return res;
  24505. } /* END test_wc_PKCS7_EncodeSignedData */
  24506. /*
  24507. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  24508. */
  24509. static int test_wc_PKCS7_EncodeSignedData_ex(void)
  24510. {
  24511. int res = TEST_SKIPPED;
  24512. #if defined(HAVE_PKCS7)
  24513. int ret, i;
  24514. PKCS7* pkcs7;
  24515. WC_RNG rng;
  24516. byte outputHead[FOURK_BUF/2];
  24517. byte outputFoot[FOURK_BUF/2];
  24518. word32 outputHeadSz = (word32)sizeof(outputHead);
  24519. word32 outputFootSz = (word32)sizeof(outputFoot);
  24520. byte data[FOURK_BUF];
  24521. wc_HashAlg hash;
  24522. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24523. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24524. word32 hashSz = wc_HashGetDigestSize(hashType);
  24525. #ifndef NO_RSA
  24526. #if defined(USE_CERT_BUFFERS_2048)
  24527. byte key[sizeof(client_key_der_2048)];
  24528. byte cert[sizeof(client_cert_der_2048)];
  24529. word32 keySz = (word32)sizeof(key);
  24530. word32 certSz = (word32)sizeof(cert);
  24531. XMEMSET(key, 0, keySz);
  24532. XMEMSET(cert, 0, certSz);
  24533. XMEMCPY(key, client_key_der_2048, keySz);
  24534. XMEMCPY(cert, client_cert_der_2048, certSz);
  24535. #elif defined(USE_CERT_BUFFERS_1024)
  24536. byte key[sizeof_client_key_der_1024];
  24537. byte cert[sizeof(sizeof_client_cert_der_1024)];
  24538. word32 keySz = (word32)sizeof(key);
  24539. word32 certSz = (word32)sizeof(cert);
  24540. XMEMSET(key, 0, keySz);
  24541. XMEMSET(cert, 0, certSz);
  24542. XMEMCPY(key, client_key_der_1024, keySz);
  24543. XMEMCPY(cert, client_cert_der_1024, certSz);
  24544. #else
  24545. unsigned char cert[ONEK_BUF];
  24546. unsigned char key[ONEK_BUF];
  24547. XFILE fp;
  24548. int certSz;
  24549. int keySz;
  24550. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  24551. AssertTrue((fp != XBADFILE));
  24552. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  24553. XFCLOSE(fp);
  24554. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  24555. AssertTrue(fp != XBADFILE);
  24556. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  24557. XFCLOSE(fp);
  24558. #endif
  24559. #elif defined(HAVE_ECC)
  24560. #if defined(USE_CERT_BUFFERS_256)
  24561. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24562. unsigned char key[sizeof(ecc_clikey_der_256)];
  24563. int certSz = (int)sizeof(cert);
  24564. int keySz = (int)sizeof(key);
  24565. XMEMSET(cert, 0, certSz);
  24566. XMEMSET(key, 0, keySz);
  24567. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  24568. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  24569. #else
  24570. unsigned char cert[ONEK_BUF];
  24571. unsigned char key[ONEK_BUF];
  24572. XFILE fp;
  24573. int certSz, keySz;
  24574. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  24575. AssertTrue(fp != XBADFILE);
  24576. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  24577. XFCLOSE(fp);
  24578. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  24579. AssertTrue(fp != XBADFILE);
  24580. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  24581. XFCLOSE(fp);
  24582. #endif
  24583. #endif
  24584. /* initialize large data with sequence */
  24585. for (i=0; i<(int)sizeof(data); i++)
  24586. data[i] = i & 0xff;
  24587. XMEMSET(outputHead, 0, outputHeadSz);
  24588. XMEMSET(outputFoot, 0, outputFootSz);
  24589. AssertIntEQ(wc_InitRng(&rng), 0);
  24590. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24591. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24592. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24593. pkcs7->content = NULL; /* not used for ex */
  24594. pkcs7->contentSz = (word32)sizeof(data);
  24595. pkcs7->privateKey = key;
  24596. pkcs7->privateKeySz = (word32)sizeof(key);
  24597. pkcs7->encryptOID = RSAk;
  24598. pkcs7->hashOID = SHAh;
  24599. pkcs7->rng = &rng;
  24600. /* calculate hash for content */
  24601. ret = wc_HashInit(&hash, hashType);
  24602. if (ret == 0) {
  24603. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  24604. if (ret == 0) {
  24605. ret = wc_HashFinal(&hash, hashType, hashBuf);
  24606. }
  24607. wc_HashFree(&hash, hashType);
  24608. }
  24609. AssertIntEQ(ret, 0);
  24610. /* Perform PKCS7 sign using hash directly */
  24611. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24612. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  24613. AssertIntGT(outputHeadSz, 0);
  24614. AssertIntGT(outputFootSz, 0);
  24615. wc_PKCS7_Free(pkcs7);
  24616. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24617. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24618. /* required parameter even on verify when using _ex, if using outputHead
  24619. * and outputFoot */
  24620. pkcs7->contentSz = (word32)sizeof(data);
  24621. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24622. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  24623. wc_PKCS7_Free(pkcs7);
  24624. /* assembly complete PKCS7 sign and use normal verify */
  24625. {
  24626. byte* output = (byte*)XMALLOC(
  24627. outputHeadSz + sizeof(data) + outputFootSz,
  24628. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24629. word32 outputSz = 0;
  24630. AssertNotNull(output);
  24631. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  24632. outputSz += outputHeadSz;
  24633. XMEMCPY(&output[outputSz], data, sizeof(data));
  24634. outputSz += sizeof(data);
  24635. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  24636. outputSz += outputFootSz;
  24637. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24638. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24639. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24640. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24641. }
  24642. /* Pass in bad args. */
  24643. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  24644. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24645. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  24646. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24647. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24648. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24649. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  24650. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24651. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24652. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24653. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24654. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  24655. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24656. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  24657. pkcs7->hashOID = 0; /* bad hashOID */
  24658. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24659. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24660. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  24661. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24662. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  24663. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24664. #ifndef NO_PKCS7_STREAM
  24665. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24666. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  24667. #else
  24668. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24669. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  24670. #endif
  24671. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  24672. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24673. #ifndef NO_PKCS7_STREAM
  24674. /* can pass in 0 buffer length with streaming API */
  24675. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24676. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  24677. #else
  24678. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24679. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24680. #endif
  24681. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24682. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  24683. #ifndef NO_PKCS7_STREAM
  24684. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24685. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  24686. #else
  24687. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24688. outputHead, outputHeadSz, outputFoot, 0), ASN_PARSE_E);
  24689. #endif
  24690. wc_PKCS7_Free(pkcs7);
  24691. wc_FreeRng(&rng);
  24692. res = TEST_RES_CHECK(1);
  24693. #endif
  24694. return res;
  24695. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  24696. #if defined(HAVE_PKCS7)
  24697. /**
  24698. * Loads certs/keys from files or buffers into the argument buffers,
  24699. * helper function called by CreatePKCS7SignedData().
  24700. *
  24701. * Returns 0 on success, negative on error.
  24702. */
  24703. static int LoadPKCS7SignedDataCerts(
  24704. int useIntermediateCertChain, int pkAlgoType,
  24705. byte* intCARoot, word32* intCARootSz,
  24706. byte* intCA1, word32* intCA1Sz,
  24707. byte* intCA2, word32* intCA2Sz,
  24708. byte* cert, word32* certSz,
  24709. byte* key, word32* keySz)
  24710. {
  24711. int ret = 0;
  24712. FILE* fp = NULL;
  24713. #ifndef NO_RSA
  24714. const char* intCARootRSA = "./certs/ca-cert.der";
  24715. const char* intCA1RSA = "./certs/intermediate/ca-int-cert.der";
  24716. const char* intCA2RSA = "./certs/intermediate/ca-int2-cert.der";
  24717. const char* intServCertRSA = "./certs/intermediate/server-int-cert.der";
  24718. const char* intServKeyRSA = "./certs/server-key.der";
  24719. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)
  24720. const char* cli1024Cert = "./certs/1024/client-cert.der";
  24721. const char* cli1024Key = "./certs/1024/client-key.der";
  24722. #endif
  24723. #endif
  24724. #ifdef HAVE_ECC
  24725. const char* intCARootECC = "./certs/ca-ecc-cert.der";
  24726. const char* intCA1ECC = "./certs/intermediate/ca-int-ecc-cert.der";
  24727. const char* intCA2ECC = "./certs/intermediate/ca-int2-ecc-cert.der";
  24728. const char* intServCertECC = "./certs/intermediate/server-int-ecc-cert.der";
  24729. const char* intServKeyECC = "./certs/ecc-key.der";
  24730. #ifndef USE_CERT_BUFFERS_256
  24731. const char* cliEccCert = "./certs/client-ecc-cert.der";
  24732. const char* cliEccKey = "./certs/client-ecc-key.der";
  24733. #endif
  24734. #endif
  24735. if (cert == NULL || certSz == NULL || key == NULL || keySz == NULL ||
  24736. ((useIntermediateCertChain == 1) &&
  24737. (intCARoot == NULL || intCARootSz == NULL || intCA1 == NULL ||
  24738. intCA1Sz == NULL || intCA2 == NULL || intCA2Sz == NULL))) {
  24739. return BAD_FUNC_ARG;
  24740. }
  24741. /* Read/load certs and keys to use for signing based on PK type and chain */
  24742. switch (pkAlgoType) {
  24743. #ifndef NO_RSA
  24744. case RSA_TYPE:
  24745. if (useIntermediateCertChain == 1) {
  24746. fp = XFOPEN(intCARootRSA, "rb");
  24747. AssertNotNull(fp);
  24748. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  24749. XFCLOSE(fp);
  24750. AssertIntGT(*intCARootSz, 0);
  24751. fp = XFOPEN(intCA1RSA, "rb");
  24752. AssertNotNull(fp);
  24753. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  24754. XFCLOSE(fp);
  24755. AssertIntGT(*intCA1Sz, 0);
  24756. fp = XFOPEN(intCA2RSA, "rb");
  24757. AssertNotNull(fp);
  24758. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  24759. XFCLOSE(fp);
  24760. AssertIntGT(*intCA2Sz, 0);
  24761. fp = XFOPEN(intServCertRSA, "rb");
  24762. AssertNotNull(fp);
  24763. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24764. XFCLOSE(fp);
  24765. AssertIntGT(*certSz, 0);
  24766. fp = XFOPEN(intServKeyRSA, "rb");
  24767. AssertNotNull(fp);
  24768. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24769. XFCLOSE(fp);
  24770. AssertIntGT(*keySz, 0);
  24771. }
  24772. else {
  24773. #if defined(USE_CERT_BUFFERS_2048)
  24774. *keySz = sizeof_client_key_der_2048;
  24775. *certSz = sizeof_client_cert_der_2048;
  24776. XMEMCPY(key, client_key_der_2048, *keySz);
  24777. XMEMCPY(cert, client_cert_der_2048, *certSz);
  24778. #elif defined(USE_CERT_BUFFERS_1024)
  24779. *keySz = sizeof_client_key_der_1024;
  24780. *certSz = sizeof_client_cert_der_1024;
  24781. XMEMCPY(key, client_key_der_1024, *keySz);
  24782. XMEMCPY(cert, client_cert_der_1024, *certSz);
  24783. #else
  24784. fp = XFOPEN(cli1024Key, "rb");
  24785. AssertNotNull(fp);
  24786. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24787. XFCLOSE(fp);
  24788. AssertIntGT(*keySz, 0);
  24789. fp = XFOPEN(cli1024Cert, "rb");
  24790. AssertNotNull(fp);
  24791. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24792. XFCLOSE(fp);
  24793. AssertIntGT(*certSz, 0);
  24794. #endif /* USE_CERT_BUFFERS_2048 */
  24795. }
  24796. break;
  24797. #endif /* !NO_RSA */
  24798. #ifdef HAVE_ECC
  24799. case ECC_TYPE:
  24800. if (useIntermediateCertChain == 1) {
  24801. fp = XFOPEN(intCARootECC, "rb");
  24802. AssertNotNull(fp);
  24803. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  24804. XFCLOSE(fp);
  24805. AssertIntGT(*intCARootSz, 0);
  24806. fp = XFOPEN(intCA1ECC, "rb");
  24807. AssertNotNull(fp);
  24808. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  24809. XFCLOSE(fp);
  24810. AssertIntGT(*intCA1Sz, 0);
  24811. fp = XFOPEN(intCA2ECC, "rb");
  24812. AssertNotNull(fp);
  24813. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  24814. XFCLOSE(fp);
  24815. AssertIntGT(*intCA2Sz, 0);
  24816. fp = XFOPEN(intServCertECC, "rb");
  24817. AssertNotNull(fp);
  24818. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24819. XFCLOSE(fp);
  24820. AssertIntGT(*certSz, 0);
  24821. fp = XFOPEN(intServKeyECC, "rb");
  24822. AssertNotNull(fp);
  24823. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24824. XFCLOSE(fp);
  24825. AssertIntGT(*keySz, 0);
  24826. }
  24827. else {
  24828. #if defined(USE_CERT_BUFFERS_256)
  24829. *keySz = sizeof_ecc_clikey_der_256;
  24830. *certSz = sizeof_cliecc_cert_der_256;
  24831. XMEMCPY(key, ecc_clikey_der_256, *keySz);
  24832. XMEMCPY(cert, cliecc_cert_der_256, *certSz);
  24833. #else
  24834. fp = XFOPEN(cliEccKey, "rb");
  24835. AssertNotNull(fp);
  24836. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24837. XFCLOSE(fp);
  24838. AssertIntGT(*keySz, 0);
  24839. fp = XFOPEN(cliEccCert, "rb");
  24840. AssertNotNull(fp);
  24841. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24842. XFCLOSE(fp);
  24843. AssertIntGT(*certSz, 0);
  24844. #endif /* USE_CERT_BUFFERS_256 */
  24845. }
  24846. break;
  24847. #endif /* HAVE_ECC */
  24848. default:
  24849. WOLFSSL_MSG("Unsupported SignedData PK type");
  24850. ret = BAD_FUNC_ARG;
  24851. break;
  24852. }
  24853. return ret;
  24854. }
  24855. /**
  24856. * Creates a PKCS7/CMS SignedData bundle to use for testing.
  24857. *
  24858. * output output buffer to place SignedData
  24859. * outputSz size of output buffer
  24860. * data data buffer to be signed
  24861. * dataSz size of data buffer
  24862. * withAttribs [1/0] include attributes in SignedData message
  24863. * detachedSig [1/0] create detached signature, no content
  24864. * useIntCertChain [1/0] use certificate chain and include intermediate and
  24865. * root CAs in bundle
  24866. * pkAlgoType RSA_TYPE or ECC_TYPE, choose what key/cert type to use
  24867. *
  24868. * Return size of bundle created on success, negative on error */
  24869. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  24870. byte* data, word32 dataSz,
  24871. int withAttribs, int detachedSig,
  24872. int useIntermediateCertChain,
  24873. int pkAlgoType)
  24874. {
  24875. int ret = 0;
  24876. WC_RNG rng;
  24877. PKCS7* pkcs7 = NULL;
  24878. static byte messageTypeOid[] =
  24879. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  24880. 0x09, 0x02 };
  24881. static byte messageType[] = { 0x13, 2, '1', '9' };
  24882. PKCS7Attrib attribs[] =
  24883. {
  24884. { messageTypeOid, sizeof(messageTypeOid), messageType,
  24885. sizeof(messageType) }
  24886. };
  24887. byte intCARoot[TWOK_BUF];
  24888. byte intCA1[TWOK_BUF];
  24889. byte intCA2[TWOK_BUF];
  24890. byte cert[TWOK_BUF];
  24891. byte key[TWOK_BUF];
  24892. word32 intCARootSz = sizeof(intCARoot);
  24893. word32 intCA1Sz = sizeof(intCA1);
  24894. word32 intCA2Sz = sizeof(intCA2);
  24895. word32 certSz = sizeof(cert);
  24896. word32 keySz = sizeof(key);
  24897. XMEMSET(intCARoot, 0, intCARootSz);
  24898. XMEMSET(intCA1, 0, intCA1Sz);
  24899. XMEMSET(intCA2, 0, intCA2Sz);
  24900. XMEMSET(cert, 0, certSz);
  24901. XMEMSET(key, 0, keySz);
  24902. ret = LoadPKCS7SignedDataCerts(useIntermediateCertChain, pkAlgoType,
  24903. intCARoot, &intCARootSz, intCA1, &intCA1Sz, intCA2, &intCA2Sz,
  24904. cert, &certSz, key, &keySz);
  24905. AssertIntEQ(ret, 0);
  24906. XMEMSET(output, 0, outputSz);
  24907. AssertIntEQ(wc_InitRng(&rng), 0);
  24908. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24909. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24910. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24911. if (useIntermediateCertChain == 1) {
  24912. /* Add intermediate and root CA certs into SignedData Certs SET */
  24913. AssertIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA2, intCA2Sz), 0);
  24914. AssertIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA1, intCA1Sz), 0);
  24915. AssertIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCARoot, intCARootSz), 0);
  24916. }
  24917. pkcs7->content = data;
  24918. pkcs7->contentSz = dataSz;
  24919. pkcs7->privateKey = key;
  24920. pkcs7->privateKeySz = (word32)sizeof(key);
  24921. if (pkAlgoType == RSA_TYPE) {
  24922. pkcs7->encryptOID = RSAk;
  24923. }
  24924. else {
  24925. pkcs7->encryptOID = ECDSAk;
  24926. }
  24927. pkcs7->hashOID = SHAh;
  24928. pkcs7->rng = &rng;
  24929. if (withAttribs) {
  24930. /* include a signed attribute */
  24931. pkcs7->signedAttribs = attribs;
  24932. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  24933. }
  24934. if (detachedSig) {
  24935. AssertIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  24936. }
  24937. outputSz = wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz);
  24938. AssertIntGT(outputSz, 0);
  24939. wc_PKCS7_Free(pkcs7);
  24940. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24941. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24942. if (detachedSig) {
  24943. pkcs7->content = data;
  24944. pkcs7->contentSz = dataSz;
  24945. }
  24946. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24947. wc_PKCS7_Free(pkcs7);
  24948. wc_FreeRng(&rng);
  24949. return outputSz;
  24950. }
  24951. #endif
  24952. /*
  24953. * Testing wc_PKCS_VerifySignedData()
  24954. */
  24955. static int test_wc_PKCS7_VerifySignedData(void)
  24956. {
  24957. int res = TEST_SKIPPED;
  24958. #if defined(HAVE_PKCS7)
  24959. PKCS7* pkcs7;
  24960. byte output[6000]; /* Large size needed for bundles with int CA certs */
  24961. word32 outputSz = sizeof(output);
  24962. byte data[] = "Test data to encode.";
  24963. byte badOut[1];
  24964. word32 badOutSz = 0;
  24965. byte badContent[] = "This is different content than was signed";
  24966. int ret;
  24967. wc_HashAlg hash;
  24968. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24969. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24970. word32 hashSz = wc_HashGetDigestSize(hashType);
  24971. #ifndef NO_RSA
  24972. /* Success test with RSA certs/key */
  24973. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24974. (word32)sizeof(data),
  24975. 0, 0, 0, RSA_TYPE)), 0);
  24976. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24977. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24978. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24979. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24980. #endif
  24981. #ifdef HAVE_ECC
  24982. #ifndef NO_RSA
  24983. wc_PKCS7_Free(pkcs7);
  24984. #endif
  24985. /* Success test with ECC certs/key */
  24986. outputSz = sizeof(output);
  24987. XMEMSET(output, 0, outputSz);
  24988. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24989. (word32)sizeof(data),
  24990. 0, 0, 0, ECC_TYPE)), 0);
  24991. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24992. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24993. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24994. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24995. #endif
  24996. /* Test bad args. */
  24997. #if !defined(NO_RSA) || defined(HAVE_ECC)
  24998. AssertIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz),
  24999. BAD_FUNC_ARG);
  25000. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz),
  25001. BAD_FUNC_ARG);
  25002. #ifndef NO_PKCS7_STREAM
  25003. /* can pass in 0 buffer length with streaming API */
  25004. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  25005. badOutSz), WC_PKCS7_WANT_READ_E);
  25006. #else
  25007. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  25008. badOutSz), BAD_FUNC_ARG);
  25009. #endif
  25010. wc_PKCS7_Free(pkcs7);
  25011. #endif /* !NO_RSA || HAVE_ECC */
  25012. /* Invalid content should error, use detached signature so we can
  25013. * easily change content */
  25014. #ifndef NO_RSA
  25015. /* Try RSA certs/key/sig first */
  25016. outputSz = sizeof(output);
  25017. XMEMSET(output, 0, outputSz);
  25018. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25019. (word32)sizeof(data),
  25020. 1, 1, 0, RSA_TYPE)), 0);
  25021. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25022. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25023. pkcs7->content = badContent;
  25024. pkcs7->contentSz = sizeof(badContent);
  25025. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  25026. SIG_VERIFY_E);
  25027. wc_PKCS7_Free(pkcs7);
  25028. /* Test success case with detached signature and valid content */
  25029. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25030. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25031. pkcs7->content = data;
  25032. pkcs7->contentSz = sizeof(data);
  25033. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25034. wc_PKCS7_Free(pkcs7);
  25035. /* verify using pre-computed content digest only (no content) */
  25036. {
  25037. /* calculate hash for content */
  25038. ret = wc_HashInit(&hash, hashType);
  25039. if (ret == 0) {
  25040. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  25041. if (ret == 0) {
  25042. ret = wc_HashFinal(&hash, hashType, hashBuf);
  25043. }
  25044. wc_HashFree(&hash, hashType);
  25045. }
  25046. AssertIntEQ(ret, 0);
  25047. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25048. AssertIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  25049. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25050. output, outputSz,
  25051. NULL, 0), 0);
  25052. wc_PKCS7_Free(pkcs7);
  25053. }
  25054. #endif /* !NO_RSA */
  25055. #ifdef HAVE_ECC
  25056. /* Try ECC certs/key/sig next */
  25057. outputSz = sizeof(output);
  25058. XMEMSET(output, 0, outputSz);
  25059. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25060. (word32)sizeof(data),
  25061. 1, 1, 0, ECC_TYPE)), 0);
  25062. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25063. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25064. pkcs7->content = badContent;
  25065. pkcs7->contentSz = sizeof(badContent);
  25066. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  25067. SIG_VERIFY_E);
  25068. wc_PKCS7_Free(pkcs7);
  25069. /* Test success case with detached signature and valid content */
  25070. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25071. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25072. pkcs7->content = data;
  25073. pkcs7->contentSz = sizeof(data);
  25074. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25075. wc_PKCS7_Free(pkcs7);
  25076. /* verify using pre-computed content digest only (no content) */
  25077. {
  25078. /* calculate hash for content */
  25079. ret = wc_HashInit(&hash, hashType);
  25080. if (ret == 0) {
  25081. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  25082. if (ret == 0) {
  25083. ret = wc_HashFinal(&hash, hashType, hashBuf);
  25084. }
  25085. wc_HashFree(&hash, hashType);
  25086. }
  25087. AssertIntEQ(ret, 0);
  25088. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25089. AssertIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  25090. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25091. output, outputSz,
  25092. NULL, 0), 0);
  25093. wc_PKCS7_Free(pkcs7);
  25094. }
  25095. #endif
  25096. /* Test verify on signedData containing intermediate/root CA certs */
  25097. #ifndef NO_RSA
  25098. outputSz = sizeof(output);
  25099. XMEMSET(output, 0, outputSz);
  25100. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25101. (word32)sizeof(data),
  25102. 0, 0, 1, RSA_TYPE)), 0);
  25103. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25104. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25105. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25106. wc_PKCS7_Free(pkcs7);
  25107. #endif /* !NO_RSA */
  25108. #ifdef HAVE_ECC
  25109. outputSz = sizeof(output);
  25110. XMEMSET(output, 0, outputSz);
  25111. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25112. (word32)sizeof(data),
  25113. 0, 0, 1, ECC_TYPE)), 0);
  25114. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25115. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25116. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25117. wc_PKCS7_Free(pkcs7);
  25118. #endif /* HAVE_ECC */
  25119. res = TEST_RES_CHECK(1);
  25120. #endif
  25121. return res;
  25122. } /* END test_wc_PKCS7_VerifySignedData() */
  25123. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  25124. !defined(NO_AES_256)
  25125. static const byte defKey[] = {
  25126. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25127. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25128. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25129. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25130. };
  25131. static byte aesHandle[32]; /* simulated hardware key handle */
  25132. /* return 0 on success */
  25133. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  25134. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  25135. byte* in, int inSz, byte* out, void* usrCtx)
  25136. {
  25137. int ret;
  25138. Aes aes;
  25139. if (usrCtx == NULL) {
  25140. /* no simulated handle passed in */
  25141. return -1;
  25142. }
  25143. switch (encryptOID) {
  25144. case AES256CBCb:
  25145. if (ivSz != AES_BLOCK_SIZE)
  25146. return BAD_FUNC_ARG;
  25147. break;
  25148. default:
  25149. WOLFSSL_MSG("Unsupported content cipher type for test");
  25150. return ALGO_ID_E;
  25151. };
  25152. /* simulate using handle to get key */
  25153. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  25154. if (ret == 0) {
  25155. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  25156. if (ret == 0)
  25157. ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
  25158. wc_AesFree(&aes);
  25159. }
  25160. (void)aad;
  25161. (void)aadSz;
  25162. (void)authTag;
  25163. (void)authTagSz;
  25164. (void)pkcs7;
  25165. return ret;
  25166. }
  25167. /* returns key size on success */
  25168. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  25169. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  25170. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  25171. {
  25172. int ret = -1;
  25173. if (out == NULL)
  25174. return BAD_FUNC_ARG;
  25175. if (keyId[0] != 0x00) {
  25176. return -1;
  25177. }
  25178. if (type != (int)PKCS7_KEKRI) {
  25179. return -1;
  25180. }
  25181. switch (keyWrapAlgo) {
  25182. case AES256_WRAP:
  25183. /* simulate setting a handle for later decryption but use key
  25184. * as handle in the test case here */
  25185. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  25186. aesHandle, sizeof(aesHandle), NULL);
  25187. if (ret < 0)
  25188. return ret;
  25189. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  25190. if (ret < 0)
  25191. return ret;
  25192. /* return key size on success */
  25193. return sizeof(defKey);
  25194. default:
  25195. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  25196. return BAD_KEYWRAP_ALG_E;
  25197. };
  25198. (void)cekSz;
  25199. (void)cek;
  25200. (void)outSz;
  25201. (void)keyIdSz;
  25202. (void)direction;
  25203. (void)orginKey; /* used with KAKRI */
  25204. (void)orginKeySz;
  25205. return ret;
  25206. }
  25207. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  25208. /*
  25209. * Testing wc_PKCS7_EncodeEnvelopedData()
  25210. */
  25211. static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
  25212. {
  25213. int res = TEST_SKIPPED;
  25214. #if defined(HAVE_PKCS7)
  25215. PKCS7* pkcs7;
  25216. #ifdef ECC_TIMING_RESISTANT
  25217. WC_RNG rng;
  25218. #endif
  25219. word32 tempWrd32 = 0;
  25220. byte* tmpBytePtr = NULL;
  25221. const char input[] = "Test data to encode.";
  25222. int i;
  25223. int testSz = 0;
  25224. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  25225. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25226. byte* rsaCert = NULL;
  25227. byte* rsaPrivKey = NULL;
  25228. word32 rsaCertSz;
  25229. word32 rsaPrivKeySz;
  25230. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  25231. !defined(USE_CERT_BUFFERS_2048) )
  25232. static const char* rsaClientCert = "./certs/client-cert.der";
  25233. static const char* rsaClientKey = "./certs/client-key.der";
  25234. rsaCertSz = (word32)sizeof(rsaClientCert);
  25235. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  25236. #endif
  25237. #endif
  25238. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25239. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25240. byte* eccCert = NULL;
  25241. byte* eccPrivKey = NULL;
  25242. word32 eccCertSz;
  25243. word32 eccPrivKeySz;
  25244. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  25245. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  25246. static const char* eccClientKey = "./certs/ecc-client-key.der";
  25247. #endif
  25248. #endif
  25249. /* Generic buffer size. */
  25250. byte output[ONEK_BUF];
  25251. byte decoded[sizeof(input)/sizeof(char)];
  25252. int decodedSz = 0;
  25253. #ifndef NO_FILESYSTEM
  25254. XFILE certFile;
  25255. XFILE keyFile;
  25256. #endif
  25257. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25258. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25259. /* RSA certs and keys. */
  25260. #if defined(USE_CERT_BUFFERS_1024)
  25261. /* Allocate buffer space. */
  25262. AssertNotNull(rsaCert =
  25263. (byte*)XMALLOC(ONEK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25264. /* Init buffer. */
  25265. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  25266. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  25267. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(ONEK_BUF, HEAP_HINT,
  25268. DYNAMIC_TYPE_TMP_BUFFER));
  25269. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  25270. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  25271. #elif defined(USE_CERT_BUFFERS_2048)
  25272. /* Allocate buffer */
  25273. AssertNotNull(rsaCert =
  25274. (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25275. /* Init buffer. */
  25276. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  25277. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  25278. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  25279. DYNAMIC_TYPE_TMP_BUFFER));
  25280. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  25281. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  25282. #else
  25283. /* File system. */
  25284. certFile = XFOPEN(rsaClientCert, "rb");
  25285. AssertTrue(certFile != XBADFILE);
  25286. rsaCertSz = (word32)FOURK_BUF;
  25287. AssertNotNull(rsaCert =
  25288. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25289. rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz, certFile);
  25290. XFCLOSE(certFile);
  25291. keyFile = XFOPEN(rsaClientKey, "rb");
  25292. AssertTrue(keyFile != XBADFILE);
  25293. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25294. DYNAMIC_TYPE_TMP_BUFFER));
  25295. rsaPrivKeySz = (word32)FOURK_BUF;
  25296. rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz, keyFile);
  25297. XFCLOSE(keyFile);
  25298. #endif /* USE_CERT_BUFFERS */
  25299. #endif /* NO_RSA */
  25300. /* ECC */
  25301. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25302. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25303. #ifdef USE_CERT_BUFFERS_256
  25304. AssertNotNull(eccCert =
  25305. (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25306. /* Init buffer. */
  25307. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  25308. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  25309. AssertNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  25310. DYNAMIC_TYPE_TMP_BUFFER));
  25311. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  25312. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  25313. #else /* File system. */
  25314. certFile = XFOPEN(eccClientCert, "rb");
  25315. AssertTrue(certFile != XBADFILE);
  25316. eccCertSz = (word32)FOURK_BUF;
  25317. AssertNotNull(eccCert =
  25318. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25319. eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz, certFile);
  25320. XFCLOSE(certFile);
  25321. keyFile = XFOPEN(eccClientKey, "rb");
  25322. AssertTrue(keyFile != XBADFILE);
  25323. eccPrivKeySz = (word32)FOURK_BUF;
  25324. AssertNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25325. DYNAMIC_TYPE_TMP_BUFFER));
  25326. eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz, keyFile);
  25327. XFCLOSE(keyFile);
  25328. #endif /* USE_CERT_BUFFERS_256 */
  25329. #endif /* END HAVE_ECC */
  25330. /* Silence. */
  25331. (void)keyFile;
  25332. (void)certFile;
  25333. {
  25334. const pkcs7EnvelopedVector testVectors[] = {
  25335. /* DATA is a global variable defined in the makefile. */
  25336. #if !defined(NO_RSA)
  25337. #ifndef NO_DES3
  25338. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  25339. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25340. #endif /* NO_DES3 */
  25341. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25342. #ifndef NO_AES_128
  25343. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  25344. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25345. #endif
  25346. #ifndef NO_AES_192
  25347. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  25348. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25349. #endif
  25350. #ifndef NO_AES_256
  25351. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  25352. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25353. #endif
  25354. #endif /* NO_AES && HAVE_AES_CBC */
  25355. #endif /* NO_RSA */
  25356. #if defined(HAVE_ECC)
  25357. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25358. #if !defined(NO_SHA) && !defined(NO_AES_128)
  25359. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  25360. AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme, eccCert,
  25361. eccCertSz, eccPrivKey, eccPrivKeySz},
  25362. #endif
  25363. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  25364. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  25365. AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme, eccCert,
  25366. eccCertSz, eccPrivKey, eccPrivKeySz},
  25367. #endif
  25368. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  25369. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  25370. AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme, eccCert,
  25371. eccCertSz, eccPrivKey, eccPrivKeySz},
  25372. #endif
  25373. #endif /* NO_AES && HAVE_AES_CBC*/
  25374. #endif /* END HAVE_ECC */
  25375. }; /* END pkcs7EnvelopedVector */
  25376. #ifdef ECC_TIMING_RESISTANT
  25377. AssertIntEQ(wc_InitRng(&rng), 0);
  25378. #endif
  25379. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25380. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25381. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  25382. for (i = 0; i < testSz; i++) {
  25383. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  25384. (word32)(testVectors + i)->certSz), 0);
  25385. #ifdef ECC_TIMING_RESISTANT
  25386. pkcs7->rng = &rng;
  25387. #endif
  25388. pkcs7->content = (byte*)(testVectors + i)->content;
  25389. pkcs7->contentSz = (testVectors + i)->contentSz;
  25390. pkcs7->contentOID = (testVectors + i)->contentOID;
  25391. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  25392. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  25393. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  25394. pkcs7->privateKey = (testVectors + i)->privateKey;
  25395. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  25396. AssertIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25397. (word32)sizeof(output)), 0);
  25398. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25399. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  25400. AssertIntGE(decodedSz, 0);
  25401. /* Verify the size of each buffer. */
  25402. AssertIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  25403. /* Don't free the last time through the loop. */
  25404. if (i < testSz - 1) {
  25405. wc_PKCS7_Free(pkcs7);
  25406. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25407. }
  25408. } /* END test loop. */
  25409. }
  25410. /* Test bad args. */
  25411. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  25412. (word32)sizeof(output)), BAD_FUNC_ARG);
  25413. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  25414. (word32)sizeof(output)), BAD_FUNC_ARG);
  25415. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  25416. /* Decode. */
  25417. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  25418. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25419. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25420. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25421. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25422. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  25423. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  25424. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25425. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  25426. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25427. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  25428. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  25429. /* only a failure for KARI test cases */
  25430. tempWrd32 = pkcs7->singleCertSz;
  25431. pkcs7->singleCertSz = 0;
  25432. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25433. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25434. pkcs7->singleCertSz = tempWrd32;
  25435. tmpBytePtr = pkcs7->singleCert;
  25436. pkcs7->singleCert = NULL;
  25437. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25438. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25439. pkcs7->singleCert = tmpBytePtr;
  25440. #endif
  25441. tempWrd32 = pkcs7->privateKeySz;
  25442. pkcs7->privateKeySz = 0;
  25443. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25444. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25445. pkcs7->privateKeySz = tempWrd32;
  25446. tmpBytePtr = pkcs7->privateKey;
  25447. pkcs7->privateKey = NULL;
  25448. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25449. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25450. pkcs7->privateKey = tmpBytePtr;
  25451. wc_PKCS7_Free(pkcs7);
  25452. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  25453. /* test of decrypt callback with KEKRI enveloped data */
  25454. {
  25455. int envelopedSz;
  25456. const byte keyId[] = { 0x00 };
  25457. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25458. pkcs7->content = (byte*)input;
  25459. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  25460. pkcs7->contentOID = DATA;
  25461. pkcs7->encryptOID = AES256CBCb;
  25462. AssertIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  25463. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  25464. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  25465. AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  25466. AssertIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25467. (word32)sizeof(output))), 0);
  25468. wc_PKCS7_Free(pkcs7);
  25469. /* decode envelopedData */
  25470. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25471. AssertIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  25472. AssertIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  25473. AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25474. envelopedSz, decoded, sizeof(decoded))), 0);
  25475. wc_PKCS7_Free(pkcs7);
  25476. }
  25477. #endif /* !NO_AES && !NO_AES_256 */
  25478. #ifndef NO_RSA
  25479. if (rsaCert) {
  25480. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25481. }
  25482. if (rsaPrivKey) {
  25483. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25484. }
  25485. #endif /*NO_RSA */
  25486. #ifdef HAVE_ECC
  25487. if (eccCert) {
  25488. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25489. }
  25490. if (eccPrivKey) {
  25491. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25492. }
  25493. #endif /* HAVE_ECC */
  25494. #ifdef ECC_TIMING_RESISTANT
  25495. wc_FreeRng(&rng);
  25496. #endif
  25497. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && !defined(NO_RSA)
  25498. {
  25499. byte out[7];
  25500. byte *cms;
  25501. word32 cmsSz;
  25502. XFILE cmsFile;
  25503. XMEMSET(out, 0, sizeof(out));
  25504. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25505. cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb");
  25506. AssertTrue(cmsFile != XBADFILE);
  25507. cmsSz = (word32)FOURK_BUF;
  25508. AssertNotNull(cms =
  25509. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25510. cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile);
  25511. XFCLOSE(cmsFile);
  25512. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  25513. sizeof_client_cert_der_2048), 0);
  25514. pkcs7->privateKey = (byte*)client_key_der_2048;
  25515. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  25516. AssertIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  25517. 2), 0);
  25518. AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  25519. sizeof(out)), 0);
  25520. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25521. AssertIntEQ(XMEMCMP(out, "test", 4), 0);
  25522. wc_PKCS7_Free(pkcs7);
  25523. }
  25524. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 */
  25525. res = TEST_RES_CHECK(1);
  25526. #endif /* HAVE_PKCS7 */
  25527. return res;
  25528. } /* END test_wc_PKCS7_EncodeEnvelopedData() */
  25529. /*
  25530. * Testing wc_PKCS7_EncodeEncryptedData()
  25531. */
  25532. static int test_wc_PKCS7_EncodeEncryptedData(void)
  25533. {
  25534. int res = TEST_SKIPPED;
  25535. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  25536. PKCS7* pkcs7 = NULL;
  25537. byte* tmpBytePtr = NULL;
  25538. byte encrypted[TWOK_BUF];
  25539. byte decoded[TWOK_BUF];
  25540. word32 tmpWrd32 = 0;
  25541. int tmpInt = 0;
  25542. int decodedSz;
  25543. int encryptedSz;
  25544. int testSz;
  25545. int i;
  25546. const byte data[] = { /* Hello World */
  25547. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  25548. 0x72,0x6c,0x64
  25549. };
  25550. #ifndef NO_DES3
  25551. byte desKey[] = {
  25552. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  25553. };
  25554. byte des3Key[] = {
  25555. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  25556. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  25557. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  25558. };
  25559. #endif
  25560. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25561. #ifndef NO_AES_128
  25562. byte aes128Key[] = {
  25563. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25564. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25565. };
  25566. #endif
  25567. #ifndef NO_AES_192
  25568. byte aes192Key[] = {
  25569. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25570. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25571. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25572. };
  25573. #endif
  25574. #ifndef NO_AES_256
  25575. byte aes256Key[] = {
  25576. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25577. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25578. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25579. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25580. };
  25581. #endif
  25582. #endif /* !NO_AES && HAVE_AES_CBC */
  25583. const pkcs7EncryptedVector testVectors[] =
  25584. {
  25585. #ifndef NO_DES3
  25586. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  25587. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  25588. #endif /* !NO_DES3 */
  25589. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25590. #ifndef NO_AES_128
  25591. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  25592. sizeof(aes128Key)},
  25593. #endif
  25594. #ifndef NO_AES_192
  25595. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  25596. sizeof(aes192Key)},
  25597. #endif
  25598. #ifndef NO_AES_256
  25599. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  25600. sizeof(aes256Key)},
  25601. #endif
  25602. #endif /* !NO_AES && HAVE_AES_CBC */
  25603. };
  25604. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  25605. for (i = 0; i < testSz; i++) {
  25606. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25607. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25608. pkcs7->content = (byte*)testVectors[i].content;
  25609. pkcs7->contentSz = testVectors[i].contentSz;
  25610. pkcs7->contentOID = testVectors[i].contentOID;
  25611. pkcs7->encryptOID = testVectors[i].encryptOID;
  25612. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  25613. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  25614. pkcs7->heap = HEAP_HINT;
  25615. /* encode encryptedData */
  25616. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25617. sizeof(encrypted));
  25618. AssertIntGT(encryptedSz, 0);
  25619. /* Decode encryptedData */
  25620. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25621. decoded, sizeof(decoded));
  25622. AssertIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  25623. /* Keep values for last itr. */
  25624. if (i < testSz - 1) {
  25625. wc_PKCS7_Free(pkcs7);
  25626. }
  25627. }
  25628. if (pkcs7 == NULL || testSz == 0) {
  25629. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25630. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25631. }
  25632. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  25633. sizeof(encrypted)),BAD_FUNC_ARG);
  25634. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  25635. sizeof(encrypted)), BAD_FUNC_ARG);
  25636. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25637. 0), BAD_FUNC_ARG);
  25638. /* Testing the struct. */
  25639. tmpBytePtr = pkcs7->content;
  25640. pkcs7->content = NULL;
  25641. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25642. sizeof(encrypted)), BAD_FUNC_ARG);
  25643. pkcs7->content = tmpBytePtr;
  25644. tmpWrd32 = pkcs7->contentSz;
  25645. pkcs7->contentSz = 0;
  25646. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25647. sizeof(encrypted)), BAD_FUNC_ARG);
  25648. pkcs7->contentSz = tmpWrd32;
  25649. tmpInt = pkcs7->encryptOID;
  25650. pkcs7->encryptOID = 0;
  25651. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25652. sizeof(encrypted)), BAD_FUNC_ARG);
  25653. pkcs7->encryptOID = tmpInt;
  25654. tmpBytePtr = pkcs7->encryptionKey;
  25655. pkcs7->encryptionKey = NULL;
  25656. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25657. sizeof(encrypted)), BAD_FUNC_ARG);
  25658. pkcs7->encryptionKey = tmpBytePtr;
  25659. tmpWrd32 = pkcs7->encryptionKeySz;
  25660. pkcs7->encryptionKeySz = 0;
  25661. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25662. sizeof(encrypted)), BAD_FUNC_ARG);
  25663. pkcs7->encryptionKeySz = tmpWrd32;
  25664. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  25665. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25666. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
  25667. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25668. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  25669. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25670. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25671. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  25672. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25673. decoded, 0), BAD_FUNC_ARG);
  25674. /* Test struct fields */
  25675. tmpBytePtr = pkcs7->encryptionKey;
  25676. pkcs7->encryptionKey = NULL;
  25677. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25678. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25679. pkcs7->encryptionKey = tmpBytePtr;
  25680. pkcs7->encryptionKeySz = 0;
  25681. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25682. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25683. wc_PKCS7_Free(pkcs7);
  25684. res = TEST_RES_CHECK(1);
  25685. #endif
  25686. return res;
  25687. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  25688. /*
  25689. * Testing wc_PKCS7_Degenerate()
  25690. */
  25691. static int test_wc_PKCS7_Degenerate(void)
  25692. {
  25693. int res = TEST_SKIPPED;
  25694. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  25695. PKCS7* pkcs7;
  25696. char fName[] = "./certs/test-degenerate.p7b";
  25697. XFILE f;
  25698. byte der[4096];
  25699. word32 derSz;
  25700. int ret;
  25701. AssertNotNull(f = XFOPEN(fName, "rb"));
  25702. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  25703. derSz = (word32)ret;
  25704. XFCLOSE(f);
  25705. /* test degenerate success */
  25706. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25707. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25708. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25709. #ifndef NO_RSA
  25710. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25711. #else
  25712. AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25713. #endif
  25714. wc_PKCS7_Free(pkcs7);
  25715. /* test with turning off degenerate cases */
  25716. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25717. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25718. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25719. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  25720. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), PKCS7_NO_SIGNER_E);
  25721. wc_PKCS7_Free(pkcs7);
  25722. res = TEST_RES_CHECK(1);
  25723. #endif
  25724. return res;
  25725. } /* END test_wc_PKCS7_Degenerate() */
  25726. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  25727. defined(ASN_BER_TO_DER) && !defined(NO_DES3)
  25728. static byte berContent[] = {
  25729. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  25730. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  25731. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  25732. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  25733. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  25734. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  25735. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  25736. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  25737. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  25738. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  25739. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  25740. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  25741. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  25742. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  25743. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  25744. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  25745. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  25746. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  25747. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  25748. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  25749. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  25750. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  25751. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  25752. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  25753. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  25754. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  25755. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  25756. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  25757. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  25758. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  25759. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  25760. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  25761. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  25762. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  25763. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  25764. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  25765. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  25766. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  25767. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  25768. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  25769. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  25770. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  25771. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  25772. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  25773. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  25774. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  25775. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  25776. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  25777. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  25778. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  25779. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  25780. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  25781. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  25782. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  25783. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  25784. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  25785. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  25786. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  25787. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  25788. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  25789. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  25790. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  25791. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  25792. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  25793. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  25794. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  25795. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  25796. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  25797. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  25798. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  25799. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  25800. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  25801. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  25802. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  25803. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  25804. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  25805. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  25806. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  25807. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  25808. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  25809. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  25810. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  25811. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  25812. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  25813. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  25814. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  25815. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  25816. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  25817. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  25818. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  25819. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  25820. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  25821. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  25822. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  25823. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  25824. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  25825. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  25826. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  25827. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  25828. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  25829. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  25830. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  25831. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  25832. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  25833. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  25834. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  25835. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  25836. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  25837. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  25838. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  25839. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  25840. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  25841. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  25842. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  25843. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  25844. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  25845. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  25846. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  25847. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  25848. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  25849. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  25850. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  25851. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  25852. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  25853. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  25854. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  25855. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  25856. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  25857. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  25858. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  25859. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  25860. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  25861. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  25862. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  25863. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  25864. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  25865. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  25866. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  25867. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  25868. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  25869. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  25870. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  25871. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  25872. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  25873. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  25874. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  25875. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  25876. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  25877. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  25878. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  25879. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  25880. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  25881. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  25882. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  25883. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  25884. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  25885. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  25886. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  25887. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  25888. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  25889. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  25890. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  25891. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  25892. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  25893. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  25894. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  25895. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  25896. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  25897. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  25898. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  25899. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  25900. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  25901. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  25902. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  25903. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  25904. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  25905. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  25906. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  25907. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  25908. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  25909. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  25910. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  25911. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  25912. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  25913. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  25914. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  25915. 0x00, 0x00, 0x00, 0x00, 0x00
  25916. };
  25917. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER && !NO_DES3 */
  25918. /*
  25919. * Testing wc_PKCS7_BER()
  25920. */
  25921. static int test_wc_PKCS7_BER(void)
  25922. {
  25923. int res = TEST_SKIPPED;
  25924. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  25925. defined(ASN_BER_TO_DER)
  25926. PKCS7* pkcs7;
  25927. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  25928. XFILE f;
  25929. byte der[4096];
  25930. #ifndef NO_DES3
  25931. byte decoded[2048];
  25932. #endif
  25933. word32 derSz;
  25934. int ret;
  25935. AssertNotNull(f = XFOPEN(fName, "rb"));
  25936. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  25937. derSz = (word32)ret;
  25938. XFCLOSE(f);
  25939. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25940. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25941. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25942. #ifndef NO_RSA
  25943. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25944. #else
  25945. AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25946. #endif
  25947. wc_PKCS7_Free(pkcs7);
  25948. #ifndef NO_DES3
  25949. /* decode BER content */
  25950. AssertNotNull(f = XFOPEN("./certs/1024/client-cert.der", "rb"));
  25951. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  25952. derSz = (word32)ret;
  25953. XFCLOSE(f);
  25954. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25955. #ifndef NO_RSA
  25956. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  25957. #else
  25958. AssertIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  25959. #endif
  25960. AssertNotNull(f = XFOPEN("./certs/1024/client-key.der", "rb"));
  25961. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  25962. derSz = (word32)ret;
  25963. XFCLOSE(f);
  25964. pkcs7->privateKey = der;
  25965. pkcs7->privateKeySz = derSz;
  25966. #ifndef NO_RSA
  25967. #ifdef WOLFSSL_SP_MATH
  25968. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  25969. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  25970. #else
  25971. AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  25972. sizeof(berContent), decoded, sizeof(decoded)), 0);
  25973. #endif
  25974. #else
  25975. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  25976. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  25977. #endif
  25978. wc_PKCS7_Free(pkcs7);
  25979. #endif /* !NO_DES3 */
  25980. res = TEST_RES_CHECK(1);
  25981. #endif
  25982. return res;
  25983. } /* END test_wc_PKCS7_BER() */
  25984. static int test_PKCS7_signed_enveloped(void)
  25985. {
  25986. int res = TEST_SKIPPED;
  25987. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  25988. !defined(NO_FILESYSTEM)
  25989. XFILE f;
  25990. PKCS7* pkcs7;
  25991. #ifdef HAVE_AES_CBC
  25992. PKCS7* inner;
  25993. #endif
  25994. void* pt;
  25995. WC_RNG rng;
  25996. unsigned char key[FOURK_BUF/2];
  25997. unsigned char cert[FOURK_BUF/2];
  25998. unsigned char env[FOURK_BUF];
  25999. int envSz = FOURK_BUF;
  26000. int keySz;
  26001. int certSz;
  26002. unsigned char sig[FOURK_BUF * 2];
  26003. int sigSz = FOURK_BUF * 2;
  26004. #ifdef HAVE_AES_CBC
  26005. unsigned char decoded[FOURK_BUF];
  26006. int decodedSz = FOURK_BUF;
  26007. #endif
  26008. /* load cert */
  26009. AssertNotNull(f = XFOPEN(cliCertDerFile, "rb"));
  26010. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  26011. XFCLOSE(f);
  26012. /* load key */
  26013. AssertNotNull(f = XFOPEN(cliKeyFile, "rb"));
  26014. AssertIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  26015. XFCLOSE(f);
  26016. keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL);
  26017. /* sign cert for envelope */
  26018. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26019. AssertIntEQ(wc_InitRng(&rng), 0);
  26020. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26021. pkcs7->content = cert;
  26022. pkcs7->contentSz = certSz;
  26023. pkcs7->contentOID = DATA;
  26024. pkcs7->privateKey = key;
  26025. pkcs7->privateKeySz = keySz;
  26026. pkcs7->encryptOID = RSAk;
  26027. pkcs7->hashOID = SHA256h;
  26028. pkcs7->rng = &rng;
  26029. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26030. wc_PKCS7_Free(pkcs7);
  26031. wc_FreeRng(&rng);
  26032. #ifdef HAVE_AES_CBC
  26033. /* create envelope */
  26034. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26035. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26036. pkcs7->content = sig;
  26037. pkcs7->contentSz = sigSz;
  26038. pkcs7->contentOID = DATA;
  26039. pkcs7->encryptOID = AES256CBCb;
  26040. pkcs7->privateKey = key;
  26041. pkcs7->privateKeySz = keySz;
  26042. AssertIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
  26043. AssertIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  26044. wc_PKCS7_Free(pkcs7);
  26045. #endif
  26046. /* create bad signed enveloped data */
  26047. sigSz = FOURK_BUF * 2;
  26048. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26049. AssertIntEQ(wc_InitRng(&rng), 0);
  26050. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26051. pkcs7->content = env;
  26052. pkcs7->contentSz = envSz;
  26053. pkcs7->contentOID = DATA;
  26054. pkcs7->privateKey = key;
  26055. pkcs7->privateKeySz = keySz;
  26056. pkcs7->encryptOID = RSAk;
  26057. pkcs7->hashOID = SHA256h;
  26058. pkcs7->rng = &rng;
  26059. /* Set no certs in bundle for this test. Hang on to the pointer though to
  26060. * free it later. */
  26061. pt = (void*)pkcs7->certList;
  26062. pkcs7->certList = NULL; /* no certs in bundle */
  26063. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26064. pkcs7->certList = (Pkcs7Cert*)pt; /* restore pointer for PKCS7 free call */
  26065. wc_PKCS7_Free(pkcs7);
  26066. /* check verify fails */
  26067. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26068. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26069. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
  26070. PKCS7_SIGNEEDS_CHECK);
  26071. /* try verifying the signature manually */
  26072. {
  26073. RsaKey rKey;
  26074. word32 idx = 0;
  26075. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  26076. WC_MAX_DIGEST_SIZE];
  26077. int digestSz;
  26078. AssertIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  26079. AssertIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
  26080. digestSz = wc_RsaSSL_Verify(pkcs7->signature, pkcs7->signatureSz,
  26081. digest, sizeof(digest), &rKey);
  26082. AssertIntGT(digestSz, 0);
  26083. AssertIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  26084. AssertIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  26085. AssertIntEQ(wc_FreeRsaKey(&rKey), 0);
  26086. /* verify was success */
  26087. }
  26088. wc_PKCS7_Free(pkcs7);
  26089. /* initializing the PKCS7 struct with the signing certificate should pass */
  26090. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26091. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26092. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  26093. wc_PKCS7_Free(pkcs7);
  26094. /* create valid degenerate bundle */
  26095. sigSz = FOURK_BUF * 2;
  26096. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26097. pkcs7->content = env;
  26098. pkcs7->contentSz = envSz;
  26099. pkcs7->contentOID = DATA;
  26100. pkcs7->privateKey = key;
  26101. pkcs7->privateKeySz = keySz;
  26102. pkcs7->encryptOID = RSAk;
  26103. pkcs7->hashOID = SHA256h;
  26104. pkcs7->rng = &rng;
  26105. AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  26106. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26107. wc_PKCS7_Free(pkcs7);
  26108. wc_FreeRng(&rng);
  26109. /* check verify */
  26110. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26111. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26112. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  26113. AssertNotNull(pkcs7->content);
  26114. #ifdef HAVE_AES_CBC
  26115. /* check decode */
  26116. AssertNotNull(inner = wc_PKCS7_New(NULL, 0));
  26117. AssertIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
  26118. inner->privateKey = key;
  26119. inner->privateKeySz = keySz;
  26120. AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  26121. pkcs7->contentSz, decoded, decodedSz)), 0);
  26122. wc_PKCS7_Free(inner);
  26123. #endif
  26124. wc_PKCS7_Free(pkcs7);
  26125. #ifdef HAVE_AES_CBC
  26126. /* check cert set */
  26127. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26128. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26129. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
  26130. AssertNotNull(pkcs7->singleCert);
  26131. AssertIntNE(pkcs7->singleCertSz, 0);
  26132. wc_PKCS7_Free(pkcs7);
  26133. #endif
  26134. res = TEST_RES_CHECK(1);
  26135. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  26136. return res;
  26137. }
  26138. static int test_wc_PKCS7_NoDefaultSignedAttribs(void)
  26139. {
  26140. int res = TEST_SKIPPED;
  26141. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26142. && !defined(NO_AES)
  26143. PKCS7* pkcs7;
  26144. void* heap = NULL;
  26145. pkcs7 = wc_PKCS7_New(heap, testDevId);
  26146. AssertNotNull(pkcs7);
  26147. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26148. AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  26149. AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  26150. wc_PKCS7_Free(pkcs7);
  26151. res = TEST_RES_CHECK(1);
  26152. #endif
  26153. return res;
  26154. }
  26155. static int test_wc_PKCS7_SetOriEncryptCtx(void)
  26156. {
  26157. int res = TEST_SKIPPED;
  26158. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26159. && !defined(NO_AES)
  26160. PKCS7* pkcs7;
  26161. void* heap = NULL;
  26162. WOLFSSL_CTX* ctx;
  26163. ctx = NULL;
  26164. pkcs7 = wc_PKCS7_New(heap, testDevId);
  26165. AssertNotNull(pkcs7);
  26166. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26167. AssertIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26168. AssertIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  26169. wc_PKCS7_Free(pkcs7);
  26170. res = TEST_RES_CHECK(1);
  26171. #endif
  26172. return res;
  26173. }
  26174. static int test_wc_PKCS7_SetOriDecryptCtx(void)
  26175. {
  26176. int res = TEST_SKIPPED;
  26177. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26178. && !defined(NO_AES)
  26179. PKCS7* pkcs7;
  26180. void* heap = NULL;
  26181. WOLFSSL_CTX* ctx;
  26182. ctx = NULL;
  26183. pkcs7 = wc_PKCS7_New(heap, testDevId);
  26184. AssertNotNull(pkcs7);
  26185. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26186. AssertIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26187. AssertIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  26188. wc_PKCS7_Free(pkcs7);
  26189. res = TEST_RES_CHECK(1);
  26190. #endif
  26191. return res;
  26192. }
  26193. static int test_wc_PKCS7_DecodeCompressedData(void)
  26194. {
  26195. int res = TEST_SKIPPED;
  26196. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26197. && !defined(NO_AES) && defined(HAVE_LIBZ)
  26198. PKCS7* pkcs7;
  26199. void* heap = NULL;
  26200. byte out[4096];
  26201. byte *decompressed;
  26202. int outSz, decompressedSz;
  26203. const char* cert = "./certs/client-cert.pem";
  26204. byte* cert_buf = NULL;
  26205. size_t cert_sz = 0;
  26206. AssertIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  26207. AssertNotNull((decompressed =
  26208. (byte*)XMALLOC(cert_sz, heap, DYNAMIC_TYPE_TMP_BUFFER)));
  26209. decompressedSz = (int)cert_sz;
  26210. AssertNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26211. pkcs7->content = (byte*)cert_buf;
  26212. pkcs7->contentSz = (word32)cert_sz;
  26213. pkcs7->contentOID = DATA;
  26214. AssertIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  26215. sizeof(out))), 0);
  26216. wc_PKCS7_Free(pkcs7);
  26217. /* compressed key should be smaller than when started */
  26218. AssertIntLT(outSz, cert_sz);
  26219. /* test decompression */
  26220. AssertNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26221. AssertIntEQ(pkcs7->contentOID, 0);
  26222. /* fail case with out buffer too small */
  26223. AssertIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26224. decompressed, outSz), 0);
  26225. /* success case */
  26226. AssertIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26227. decompressed, decompressedSz), cert_sz);
  26228. AssertIntEQ(pkcs7->contentOID, DATA);
  26229. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26230. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26231. decompressed = NULL;
  26232. /* test decompression function with different 'max' inputs */
  26233. outSz = sizeof(out);
  26234. AssertIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  26235. 0);
  26236. AssertIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  26237. out, outSz, 0, heap), 0);
  26238. AssertNull(decompressed);
  26239. AssertIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  26240. out, outSz, 0, heap), 0);
  26241. AssertNotNull(decompressed);
  26242. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26243. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26244. decompressed = NULL;
  26245. AssertIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  26246. out, outSz, 0, heap), 0);
  26247. AssertNotNull(decompressed);
  26248. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26249. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26250. if (cert_buf)
  26251. free(cert_buf);
  26252. wc_PKCS7_Free(pkcs7);
  26253. res = TEST_RES_CHECK(1);
  26254. #endif
  26255. return res;
  26256. }
  26257. static int test_wc_i2d_PKCS12(void)
  26258. {
  26259. int res = TEST_SKIPPED;
  26260. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  26261. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26262. && !defined(NO_AES) && !defined(NO_DES3) && !defined(NO_SHA)
  26263. WC_PKCS12* pkcs12 = NULL;
  26264. unsigned char der[FOURK_BUF * 2];
  26265. unsigned char* pt;
  26266. int derSz;
  26267. unsigned char out[FOURK_BUF * 2];
  26268. int outSz = FOURK_BUF * 2;
  26269. const char p12_f[] = "./certs/test-servercert.p12";
  26270. XFILE f;
  26271. f = XFOPEN(p12_f, "rb");
  26272. AssertNotNull(f);
  26273. derSz = (int)XFREAD(der, 1, sizeof(der), f);
  26274. AssertIntGT(derSz, 0);
  26275. XFCLOSE(f);
  26276. AssertNotNull(pkcs12 = wc_PKCS12_new());
  26277. AssertIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
  26278. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26279. AssertIntEQ(outSz, derSz);
  26280. outSz = derSz - 1;
  26281. pt = out;
  26282. AssertIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  26283. outSz = derSz;
  26284. AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  26285. AssertIntEQ((pt == out), 0);
  26286. pt = NULL;
  26287. AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  26288. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  26289. wc_PKCS12_free(pkcs12);
  26290. /* Run the same test but use wc_d2i_PKCS12_fp. */
  26291. AssertNotNull(pkcs12 = wc_PKCS12_new());
  26292. AssertIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  26293. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26294. AssertIntEQ(outSz, derSz);
  26295. wc_PKCS12_free(pkcs12);
  26296. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  26297. pkcs12 = NULL;
  26298. AssertIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  26299. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26300. AssertIntEQ(outSz, derSz);
  26301. wc_PKCS12_free(pkcs12);
  26302. res = TEST_RES_CHECK(1);
  26303. #endif
  26304. return res;
  26305. }
  26306. /* Testing wc_SignatureGetSize() for signature type ECC */
  26307. static int test_wc_SignatureGetSize_ecc(void)
  26308. {
  26309. int res = TEST_SKIPPED;
  26310. #ifndef NO_SIG_WRAPPER
  26311. int ret;
  26312. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  26313. enum wc_SignatureType sig_type;
  26314. word32 key_len;
  26315. /* Initialize ECC Key */
  26316. ecc_key ecc;
  26317. const char* qx =
  26318. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  26319. const char* qy =
  26320. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  26321. const char* d =
  26322. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  26323. ret = wc_ecc_init(&ecc);
  26324. if (ret == 0) {
  26325. ret = wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1");
  26326. }
  26327. if (ret == 0) {
  26328. /* Input for signature type ECC */
  26329. sig_type = WC_SIGNATURE_TYPE_ECC;
  26330. key_len = sizeof(ecc_key);
  26331. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  26332. /* Test bad args */
  26333. if (ret > 0) {
  26334. sig_type = (enum wc_SignatureType) 100;
  26335. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  26336. if (ret == BAD_FUNC_ARG) {
  26337. sig_type = WC_SIGNATURE_TYPE_ECC;
  26338. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  26339. }
  26340. if (ret >= 0) {
  26341. key_len = (word32) 0;
  26342. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  26343. }
  26344. if (ret == BAD_FUNC_ARG) {
  26345. ret = SIG_TYPE_E;
  26346. }
  26347. }
  26348. }
  26349. else {
  26350. ret = WOLFSSL_FATAL_ERROR;
  26351. }
  26352. wc_ecc_free(&ecc);
  26353. #else
  26354. ret = SIG_TYPE_E;
  26355. #endif
  26356. res = TEST_RES_CHECK(ret == SIG_TYPE_E);
  26357. #endif /* NO_SIG_WRAPPER */
  26358. return res;
  26359. }/* END test_wc_SignatureGetSize_ecc() */
  26360. /* Testing wc_SignatureGetSize() for signature type rsa */
  26361. static int test_wc_SignatureGetSize_rsa(void)
  26362. {
  26363. int res = TEST_SKIPPED;
  26364. #ifndef NO_SIG_WRAPPER
  26365. int ret = 0;
  26366. #ifndef NO_RSA
  26367. enum wc_SignatureType sig_type;
  26368. word32 key_len;
  26369. word32 idx = 0;
  26370. /* Initialize RSA Key */
  26371. RsaKey rsa_key;
  26372. byte* tmp = NULL;
  26373. size_t bytes;
  26374. #ifdef USE_CERT_BUFFERS_1024
  26375. bytes = (size_t)sizeof_client_key_der_1024;
  26376. if (bytes < (size_t)sizeof_client_key_der_1024)
  26377. bytes = (size_t)sizeof_client_cert_der_1024;
  26378. #elif defined(USE_CERT_BUFFERS_2048)
  26379. bytes = (size_t)sizeof_client_key_der_2048;
  26380. if (bytes < (size_t)sizeof_client_cert_der_2048)
  26381. bytes = (size_t)sizeof_client_cert_der_2048;
  26382. #else
  26383. bytes = FOURK_BUF;
  26384. #endif
  26385. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26386. if (tmp != NULL) {
  26387. #ifdef USE_CERT_BUFFERS_1024
  26388. XMEMCPY(tmp, client_key_der_1024,
  26389. (size_t)sizeof_client_key_der_1024);
  26390. #elif defined(USE_CERT_BUFFERS_2048)
  26391. XMEMCPY(tmp, client_key_der_2048,
  26392. (size_t)sizeof_client_key_der_2048);
  26393. #elif !defined(NO_FILESYSTEM)
  26394. file = XFOPEN(clientKey, "rb");
  26395. if (file != XBADFILE) {
  26396. bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file);
  26397. XFCLOSE(file);
  26398. }
  26399. else {
  26400. ret = WOLFSSL_FATAL_ERROR;
  26401. }
  26402. #else
  26403. ret = WOLFSSL_FATAL_ERROR;
  26404. #endif
  26405. }
  26406. else {
  26407. ret = WOLFSSL_FATAL_ERROR;
  26408. }
  26409. if (ret == 0) {
  26410. ret = wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, testDevId);
  26411. }
  26412. if (ret == 0) {
  26413. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes);
  26414. }
  26415. if (ret == 0) {
  26416. /* Input for signature type RSA */
  26417. sig_type = WC_SIGNATURE_TYPE_RSA;
  26418. key_len = sizeof(RsaKey);
  26419. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  26420. /* Test bad args */
  26421. if (ret > 0) {
  26422. sig_type = (enum wc_SignatureType) 100;
  26423. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  26424. if (ret == BAD_FUNC_ARG) {
  26425. sig_type = WC_SIGNATURE_TYPE_RSA;
  26426. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  26427. }
  26428. #ifndef HAVE_USER_RSA
  26429. if (ret == BAD_FUNC_ARG)
  26430. #else
  26431. if (ret == 0)
  26432. #endif
  26433. {
  26434. key_len = (word32)0;
  26435. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  26436. }
  26437. if (ret == BAD_FUNC_ARG) {
  26438. ret = SIG_TYPE_E;
  26439. }
  26440. }
  26441. }
  26442. else {
  26443. ret = WOLFSSL_FATAL_ERROR;
  26444. }
  26445. wc_FreeRsaKey(&rsa_key);
  26446. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26447. #else
  26448. ret = SIG_TYPE_E;
  26449. #endif
  26450. res = TEST_RES_CHECK(ret == SIG_TYPE_E);
  26451. #endif /* NO_SIG_WRAPPER */
  26452. return res;
  26453. }/* END test_wc_SignatureGetSize_rsa(void) */
  26454. /*----------------------------------------------------------------------------*
  26455. | hash.h Tests
  26456. *----------------------------------------------------------------------------*/
  26457. static int test_wc_HashInit(void)
  26458. {
  26459. int ret = 0, i; /* 0 indicates tests passed, 1 indicates failure */
  26460. wc_HashAlg hash;
  26461. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26462. enum wc_HashType enumArray[] = {
  26463. #ifndef NO_MD5
  26464. WC_HASH_TYPE_MD5,
  26465. #endif
  26466. #ifndef NO_SHA
  26467. WC_HASH_TYPE_SHA,
  26468. #endif
  26469. #ifndef WOLFSSL_SHA224
  26470. WC_HASH_TYPE_SHA224,
  26471. #endif
  26472. #ifndef NO_SHA256
  26473. WC_HASH_TYPE_SHA256,
  26474. #endif
  26475. #ifndef WOLFSSL_SHA384
  26476. WC_HASH_TYPE_SHA384,
  26477. #endif
  26478. #ifndef WOLFSSL_SHA512
  26479. WC_HASH_TYPE_SHA512,
  26480. #endif
  26481. };
  26482. /* dynamically finds the length */
  26483. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26484. /* For loop to test various arguments... */
  26485. for (i = 0; i < enumlen; i++) {
  26486. /* check for bad args */
  26487. if (wc_HashInit(&hash, enumArray[i]) == BAD_FUNC_ARG) {
  26488. ret = 1;
  26489. break;
  26490. }
  26491. wc_HashFree(&hash, enumArray[i]);
  26492. /* check for null ptr */
  26493. if (wc_HashInit(NULL, enumArray[i]) != BAD_FUNC_ARG) {
  26494. ret = 1;
  26495. break;
  26496. }
  26497. } /* end of for loop */
  26498. return TEST_RES_CHECK(ret == 0);
  26499. } /* end of test_wc_HashInit */
  26500. /*
  26501. * Unit test function for wc_HashSetFlags()
  26502. */
  26503. static int test_wc_HashSetFlags(void)
  26504. {
  26505. int res = TEST_SKIPPED;
  26506. #ifdef WOLFSSL_HASH_FLAGS
  26507. wc_HashAlg hash;
  26508. int ret = 0;
  26509. word32 flags = 0;
  26510. int i, j;
  26511. int notSupportedLen;
  26512. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26513. enum wc_HashType enumArray[] = {
  26514. #ifndef NO_MD5
  26515. WC_HASH_TYPE_MD5,
  26516. #endif
  26517. #ifndef NO_SHA
  26518. WC_HASH_TYPE_SHA,
  26519. #endif
  26520. #ifdef WOLFSSL_SHA224
  26521. WC_HASH_TYPE_SHA224,
  26522. #endif
  26523. #ifndef NO_SHA256
  26524. WC_HASH_TYPE_SHA256,
  26525. #endif
  26526. #ifdef WOLFSSL_SHA384
  26527. WC_HASH_TYPE_SHA384,
  26528. #endif
  26529. #ifdef WOLFSSL_SHA512
  26530. WC_HASH_TYPE_SHA512,
  26531. #endif
  26532. #ifdef WOLFSSL_SHA3
  26533. WC_HASH_TYPE_SHA3_224,
  26534. #endif
  26535. };
  26536. enum wc_HashType notSupported[] = {
  26537. WC_HASH_TYPE_MD5_SHA,
  26538. WC_HASH_TYPE_MD2,
  26539. WC_HASH_TYPE_MD4,
  26540. WC_HASH_TYPE_BLAKE2B,
  26541. WC_HASH_TYPE_BLAKE2S,
  26542. WC_HASH_TYPE_NONE,
  26543. };
  26544. /* dynamically finds the length */
  26545. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26546. /* For loop to test various arguments... */
  26547. for (i = 0; i < enumlen; i++) {
  26548. ret = wc_HashInit(&hash, enumArray[i]);
  26549. if (ret == 0) {
  26550. ret = wc_HashSetFlags(&hash, enumArray[i], flags);
  26551. }
  26552. if (ret == 0) {
  26553. if (flags & WC_HASH_FLAG_ISCOPY) {
  26554. ret = 0;
  26555. }
  26556. }
  26557. if (ret == 0) {
  26558. ret = wc_HashSetFlags(NULL, enumArray[i], flags);
  26559. if (ret == BAD_FUNC_ARG) {
  26560. ret = 0;
  26561. }
  26562. }
  26563. wc_HashFree(&hash, enumArray[i]);
  26564. }
  26565. /* For loop to test not supported cases */
  26566. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  26567. for (j = 0; ret == 0 && j < notSupportedLen; j++) {
  26568. ret = wc_HashInit(&hash, notSupported[j]);
  26569. if (ret == 0) {
  26570. ret = -1;
  26571. }
  26572. else if (ret == BAD_FUNC_ARG) {
  26573. ret = wc_HashSetFlags(&hash, notSupported[j], flags);
  26574. if (ret == 0) {
  26575. ret = -1;
  26576. }
  26577. else if (ret == BAD_FUNC_ARG) {
  26578. ret = 0;
  26579. }
  26580. }
  26581. if (ret == 0) {
  26582. ret = wc_HashFree(&hash, notSupported[j]);
  26583. if (ret == 0) {
  26584. ret = -1;
  26585. }
  26586. else if (ret == BAD_FUNC_ARG) {
  26587. ret = 0;
  26588. }
  26589. }
  26590. }
  26591. res = TEST_RES_CHECK(ret == 0);
  26592. #endif
  26593. return res;
  26594. } /* END test_wc_HashSetFlags */
  26595. /*
  26596. * Unit test function for wc_HashGetFlags()
  26597. */
  26598. static int test_wc_HashGetFlags(void)
  26599. {
  26600. int res = TEST_SKIPPED;
  26601. #ifdef WOLFSSL_HASH_FLAGS
  26602. wc_HashAlg hash;
  26603. int ret = 0;
  26604. word32 flags = 0;
  26605. int i, j;
  26606. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26607. enum wc_HashType enumArray[] = {
  26608. #ifndef NO_MD5
  26609. WC_HASH_TYPE_MD5,
  26610. #endif
  26611. #ifndef NO_SHA
  26612. WC_HASH_TYPE_SHA,
  26613. #endif
  26614. #ifdef WOLFSSL_SHA224
  26615. WC_HASH_TYPE_SHA224,
  26616. #endif
  26617. #ifndef NO_SHA256
  26618. WC_HASH_TYPE_SHA256,
  26619. #endif
  26620. #ifdef WOLFSSL_SHA384
  26621. WC_HASH_TYPE_SHA384,
  26622. #endif
  26623. #ifdef WOLFSSL_SHA512
  26624. WC_HASH_TYPE_SHA512,
  26625. #endif
  26626. #ifdef WOLFSSL_SHA3
  26627. WC_HASH_TYPE_SHA3_224,
  26628. #endif
  26629. };
  26630. enum wc_HashType notSupported[] = {
  26631. WC_HASH_TYPE_MD5_SHA,
  26632. WC_HASH_TYPE_MD2,
  26633. WC_HASH_TYPE_MD4,
  26634. WC_HASH_TYPE_BLAKE2B,
  26635. WC_HASH_TYPE_BLAKE2S,
  26636. WC_HASH_TYPE_NONE,
  26637. };
  26638. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26639. int notSupportedLen;
  26640. /* For loop to test various arguments... */
  26641. for (i = 0; i < enumlen; i++) {
  26642. ret = wc_HashInit(&hash, enumArray[i]);
  26643. if (ret == 0) {
  26644. ret = wc_HashGetFlags(&hash, enumArray[i], &flags);
  26645. }
  26646. if (ret == 0) {
  26647. if (flags & WC_HASH_FLAG_ISCOPY) {
  26648. ret = 0;
  26649. }
  26650. }
  26651. if (ret == 0) {
  26652. ret = wc_HashGetFlags(NULL, enumArray[i], &flags);
  26653. if (ret == BAD_FUNC_ARG) {
  26654. ret = 0;
  26655. }
  26656. }
  26657. wc_HashFree(&hash, enumArray[i]);
  26658. if (ret != 0) {
  26659. break;
  26660. }
  26661. }
  26662. /* For loop to test not supported cases */
  26663. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  26664. for (j = 0; ret == 0 && j < notSupportedLen; j++) {
  26665. ret = wc_HashInit(&hash, notSupported[j]);
  26666. if (ret == 0) {
  26667. ret = -1;
  26668. }
  26669. else if (ret == BAD_FUNC_ARG) {
  26670. ret = wc_HashGetFlags(&hash, notSupported[j], &flags);
  26671. if (ret == 0) {
  26672. ret = -1;
  26673. }
  26674. else if (ret == BAD_FUNC_ARG) {
  26675. ret = 0;
  26676. }
  26677. }
  26678. if (ret == 0) {
  26679. ret = wc_HashFree(&hash, notSupported[j]);
  26680. if (ret == 0) {
  26681. ret = -1;
  26682. }
  26683. if (ret == BAD_FUNC_ARG) {
  26684. ret = 0;
  26685. }
  26686. }
  26687. }
  26688. res = TEST_RES_CHECK(ret == 0);
  26689. #endif
  26690. return res;
  26691. } /* END test_wc_HashGetFlags */
  26692. /*----------------------------------------------------------------------------*
  26693. | Compatibility Tests
  26694. *----------------------------------------------------------------------------*/
  26695. static int test_wolfSSL_lhash(void)
  26696. {
  26697. int res = TEST_SKIPPED;
  26698. #ifdef OPENSSL_ALL
  26699. const char testStr[] = "Like a true nature's child\n"
  26700. "We were born\n"
  26701. "Born to be wild";
  26702. AssertIntEQ(lh_strhash(testStr), 0x5b7541dc);
  26703. res = TEST_RES_CHECK(1);
  26704. #endif
  26705. return res;
  26706. }
  26707. static int test_wolfSSL_X509_NAME(void)
  26708. {
  26709. int res = TEST_SKIPPED;
  26710. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  26711. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  26712. && !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  26713. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  26714. defined(OPENSSL_EXTRA))
  26715. X509* x509;
  26716. const unsigned char* c;
  26717. unsigned char buf[4096];
  26718. int bytes;
  26719. XFILE f;
  26720. const X509_NAME* a;
  26721. const X509_NAME* b;
  26722. X509_NAME* d2i_name = NULL;
  26723. int sz;
  26724. unsigned char* tmp;
  26725. char file[] = "./certs/ca-cert.der";
  26726. #ifndef OPENSSL_EXTRA_X509_SMALL
  26727. byte empty[] = { /* CN=empty emailAddress= */
  26728. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  26729. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  26730. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  26731. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  26732. 0x01, 0x16, 0x00
  26733. };
  26734. #endif
  26735. #ifndef OPENSSL_EXTRA_X509_SMALL
  26736. /* test compile of deprecated function, returns 0 */
  26737. AssertIntEQ(CRYPTO_thread_id(), 0);
  26738. #endif
  26739. AssertNotNull(a = X509_NAME_new());
  26740. X509_NAME_free((X509_NAME*)a);
  26741. f = XFOPEN(file, "rb");
  26742. AssertTrue(f != XBADFILE);
  26743. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  26744. XFCLOSE(f);
  26745. c = buf;
  26746. AssertNotNull(x509 = wolfSSL_X509_d2i(NULL, c, bytes));
  26747. /* test cmp function */
  26748. AssertNotNull(a = X509_get_issuer_name(x509));
  26749. AssertNotNull(b = X509_get_subject_name(x509));
  26750. #ifndef OPENSSL_EXTRA_X509_SMALL
  26751. AssertIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  26752. #endif
  26753. tmp = buf;
  26754. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  26755. if (sz > 0 && tmp == buf) {
  26756. fprintf(stderr, "\nERROR - %s line %d failed with:", __FILE__,
  26757. __LINE__);
  26758. fprintf(stderr, " Expected pointer to be incremented\n");
  26759. abort();
  26760. }
  26761. #ifndef OPENSSL_EXTRA_X509_SMALL
  26762. tmp = buf;
  26763. AssertNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  26764. #endif
  26765. /* if output parameter is NULL, should still return required size. */
  26766. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  26767. /* retry but with the function creating a buffer */
  26768. tmp = NULL;
  26769. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  26770. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  26771. AssertNotNull(b = X509_NAME_dup((X509_NAME*)a));
  26772. #ifndef OPENSSL_EXTRA_X509_SMALL
  26773. AssertIntEQ(X509_NAME_cmp(a, b), 0);
  26774. #endif
  26775. X509_NAME_free((X509_NAME*)b);
  26776. X509_NAME_free(d2i_name);
  26777. X509_free(x509);
  26778. #ifndef OPENSSL_EXTRA_X509_SMALL
  26779. /* test with an empty domain component */
  26780. tmp = empty;
  26781. sz = sizeof(empty);
  26782. AssertNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  26783. AssertIntEQ(X509_NAME_entry_count(d2i_name), 2);
  26784. /* size of empty emailAddress will be 0 */
  26785. tmp = buf;
  26786. AssertIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  26787. (char*)tmp, sizeof(buf)), 0);
  26788. /* should contain no organization name */
  26789. tmp = buf;
  26790. AssertIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  26791. (char*)tmp, sizeof(buf)), -1);
  26792. X509_NAME_free(d2i_name);
  26793. #endif
  26794. res = TEST_RES_CHECK(1);
  26795. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  26796. return res;
  26797. }
  26798. static int test_wolfSSL_X509_NAME_hash(void)
  26799. {
  26800. int res = TEST_SKIPPED;
  26801. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) \
  26802. && !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  26803. BIO* bio;
  26804. X509* x509 = NULL;
  26805. AssertNotNull(bio = BIO_new(BIO_s_file()));
  26806. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  26807. AssertNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  26808. AssertIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  26809. AssertIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  26810. X509_free(x509);
  26811. BIO_free(bio);
  26812. res = TEST_RES_CHECK(1);
  26813. #endif
  26814. return res;
  26815. }
  26816. static int test_wolfSSL_X509_NAME_print_ex(void)
  26817. {
  26818. int res = TEST_SKIPPED;
  26819. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  26820. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  26821. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  26822. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  26823. !defined(NO_BIO) && !defined(NO_RSA)
  26824. int memSz;
  26825. byte* mem = NULL;
  26826. BIO* bio = NULL;
  26827. BIO* membio = NULL;
  26828. X509* x509 = NULL;
  26829. X509_NAME* name = NULL;
  26830. const char* expNormal = "C=US, CN=wolfssl.com";
  26831. const char* expReverse = "CN=wolfssl.com, C=US";
  26832. const char* expNotEscaped = "C= US,+\"\\ , CN=#wolfssl.com<>;";
  26833. const char* expNotEscapedRev = "CN=#wolfssl.com<>;, C= US,+\"\\ ";
  26834. const char* expRFC5523 =
  26835. "CN=\\#wolfssl.com\\<\\>\\;, C=\\ US\\,\\+\\\"\\\\\\ ";
  26836. /* Test with real cert (svrCertFile) first */
  26837. AssertNotNull(bio = BIO_new(BIO_s_file()));
  26838. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  26839. AssertNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  26840. AssertNotNull(name = X509_get_subject_name(x509));
  26841. /* Test without flags */
  26842. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26843. AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  26844. BIO_free(membio);
  26845. /* Test flag: XN_FLAG_RFC2253 */
  26846. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26847. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26848. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  26849. BIO_free(membio);
  26850. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  26851. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26852. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26853. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  26854. BIO_free(membio);
  26855. X509_free(x509);
  26856. BIO_free(bio);
  26857. /* Test normal case without escaped characters */
  26858. {
  26859. /* Create name: "/C=US/CN=wolfssl.com" */
  26860. AssertNotNull(name = X509_NAME_new());
  26861. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  26862. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  26863. WOLFSSL_SUCCESS);
  26864. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  26865. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  26866. WOLFSSL_SUCCESS);
  26867. /* Test without flags */
  26868. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26869. AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  26870. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26871. AssertIntEQ(memSz, XSTRLEN(expNormal));
  26872. AssertIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  26873. BIO_free(membio);
  26874. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  26875. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26876. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26877. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  26878. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26879. AssertIntEQ(memSz, XSTRLEN(expReverse));
  26880. BIO_free(membio);
  26881. /* Test flags: XN_FLAG_DN_REV - reversed */
  26882. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26883. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26884. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  26885. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26886. AssertIntEQ(memSz, XSTRLEN(expReverse));
  26887. AssertIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  26888. BIO_free(membio);
  26889. X509_NAME_free(name);
  26890. }
  26891. /* Test RFC2253 characters are escaped with backslashes */
  26892. {
  26893. AssertNotNull(name = X509_NAME_new());
  26894. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  26895. /* space at beginning and end, and: ,+"\ */
  26896. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  26897. WOLFSSL_SUCCESS);
  26898. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  26899. /* # at beginning, and: <>;*/
  26900. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  26901. WOLFSSL_SUCCESS);
  26902. /* Test without flags */
  26903. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26904. AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  26905. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26906. AssertIntEQ(memSz, XSTRLEN(expNotEscaped));
  26907. AssertIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  26908. XSTRLEN(expNotEscaped)), 0);
  26909. BIO_free(membio);
  26910. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  26911. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26912. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26913. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  26914. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26915. AssertIntEQ(memSz, XSTRLEN(expRFC5523));
  26916. AssertIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  26917. BIO_free(membio);
  26918. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  26919. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26920. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26921. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  26922. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26923. AssertIntEQ(memSz, XSTRLEN(expNotEscapedRev));
  26924. AssertIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  26925. XSTRLEN(expNotEscapedRev)), 0);
  26926. BIO_free(membio);
  26927. X509_NAME_free(name);
  26928. }
  26929. res = TEST_RES_CHECK(1);
  26930. #endif
  26931. return res;
  26932. }
  26933. #ifndef NO_BIO
  26934. static int test_wolfSSL_X509_INFO_multiple_info(void)
  26935. {
  26936. int res = TEST_SKIPPED;
  26937. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  26938. STACK_OF(X509_INFO) *info_stack;
  26939. X509_INFO *info;
  26940. int len;
  26941. int i;
  26942. const char* files[] = {
  26943. cliCertFile,
  26944. cliKeyFile,
  26945. /* This needs to be the order as svrCertFile contains the
  26946. * intermediate cert as well. */
  26947. svrKeyFile,
  26948. svrCertFile,
  26949. NULL,
  26950. };
  26951. const char** curFile;
  26952. BIO *fileBIO;
  26953. BIO *concatBIO = NULL;
  26954. byte tmp[FOURK_BUF];
  26955. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  26956. * to group objects together. */
  26957. AssertNotNull(concatBIO = BIO_new(BIO_s_mem()));
  26958. for (curFile = files; *curFile != NULL; curFile++) {
  26959. int fileLen;
  26960. AssertNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  26961. fileLen = wolfSSL_BIO_get_len(fileBIO);
  26962. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  26963. AssertIntEQ(BIO_write(concatBIO, tmp, len), len);
  26964. fileLen -= len;
  26965. }
  26966. /* Make sure we read the entire file */
  26967. AssertIntEQ(fileLen, 0);
  26968. BIO_free(fileBIO);
  26969. }
  26970. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  26971. NULL));
  26972. AssertIntEQ(sk_X509_INFO_num(info_stack), 3);
  26973. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  26974. AssertNotNull(info = sk_X509_INFO_value(info_stack, i));
  26975. AssertNotNull(info->x509);
  26976. AssertNull(info->crl);
  26977. if (i != 0) {
  26978. AssertNotNull(info->x_pkey);
  26979. AssertIntEQ(X509_check_private_key(info->x509,
  26980. info->x_pkey->dec_pkey), 1);
  26981. }
  26982. else {
  26983. AssertNull(info->x_pkey);
  26984. }
  26985. }
  26986. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  26987. BIO_free(concatBIO);
  26988. res = TEST_RES_CHECK(1);
  26989. #endif
  26990. return res;
  26991. }
  26992. #endif
  26993. #ifndef NO_BIO
  26994. static int test_wolfSSL_X509_INFO(void)
  26995. {
  26996. int res = TEST_SKIPPED;
  26997. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  26998. STACK_OF(X509_INFO) *info_stack;
  26999. X509_INFO *info;
  27000. BIO *cert;
  27001. int i;
  27002. /* PEM in hex format to avoid null terminator */
  27003. byte data[] = {
  27004. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  27005. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  27006. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  27007. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  27008. 0x2d, 0x2d, 0x2d
  27009. };
  27010. /* PEM in hex format to avoid null terminator */
  27011. byte data2[] = {
  27012. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  27013. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  27014. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  27015. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  27016. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  27017. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  27018. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  27019. };
  27020. AssertNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  27021. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  27022. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  27023. AssertNotNull(info = sk_X509_INFO_value(info_stack, i));
  27024. AssertNotNull(info->x509);
  27025. AssertNull(info->crl);
  27026. AssertNull(info->x_pkey);
  27027. }
  27028. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  27029. BIO_free(cert);
  27030. AssertNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  27031. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  27032. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  27033. BIO_free(cert);
  27034. /* This case should fail due to invalid input. */
  27035. AssertNotNull(cert = BIO_new(BIO_s_mem()));
  27036. AssertIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  27037. AssertNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  27038. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  27039. BIO_free(cert);
  27040. AssertNotNull(cert = BIO_new(BIO_s_mem()));
  27041. AssertIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  27042. AssertNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  27043. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  27044. BIO_free(cert);
  27045. res = TEST_RES_CHECK(1);
  27046. #endif
  27047. return res;
  27048. }
  27049. #endif
  27050. static int test_wolfSSL_X509_subject_name_hash(void)
  27051. {
  27052. int res = TEST_SKIPPED;
  27053. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  27054. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  27055. X509* x509;
  27056. X509_NAME* subjectName = NULL;
  27057. unsigned long ret1 = 0;
  27058. unsigned long ret2 = 0;
  27059. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  27060. SSL_FILETYPE_PEM));
  27061. AssertNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  27062. /* These two
  27063. * - X509_subject_name_hash(x509)
  27064. * - X509_NAME_hash(X509_get_subject_name(x509))
  27065. * should give the same hash, if !defined(NO_SHA) is true. */
  27066. ret1 = X509_subject_name_hash(x509);
  27067. AssertIntNE(ret1, 0);
  27068. #if !defined(NO_SHA)
  27069. ret2 = X509_NAME_hash(X509_get_subject_name(x509));
  27070. AssertIntNE(ret2, 0);
  27071. AssertIntEQ(ret1, ret2);
  27072. #else
  27073. (void) ret2;
  27074. #endif
  27075. X509_free(x509);
  27076. res = TEST_RES_CHECK(1);
  27077. #endif
  27078. return res;
  27079. }
  27080. static int test_wolfSSL_X509_issuer_name_hash(void)
  27081. {
  27082. int res = TEST_SKIPPED;
  27083. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  27084. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  27085. X509* x509;
  27086. X509_NAME* issuertName = NULL;
  27087. unsigned long ret1 = 0;
  27088. unsigned long ret2 = 0;
  27089. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  27090. SSL_FILETYPE_PEM));
  27091. AssertNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  27092. /* These two
  27093. * - X509_issuer_name_hash(x509)
  27094. * - X509_NAME_hash(X509_get_issuer_name(x509))
  27095. * should give the same hash, if !defined(NO_SHA) is true. */
  27096. ret1 = X509_issuer_name_hash(x509);
  27097. AssertIntNE(ret1, 0);
  27098. #if !defined(NO_SHA)
  27099. ret2 = X509_NAME_hash(X509_get_issuer_name(x509));
  27100. AssertIntNE(ret2, 0);
  27101. AssertIntEQ(ret1, ret2);
  27102. #else
  27103. (void) ret2;
  27104. #endif
  27105. X509_free(x509);
  27106. res = TEST_RES_CHECK(1);
  27107. #endif
  27108. return res;
  27109. }
  27110. static int test_wolfSSL_X509_check_host(void)
  27111. {
  27112. int res = TEST_SKIPPED;
  27113. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  27114. && !defined(NO_SHA) && !defined(NO_RSA)
  27115. X509* x509;
  27116. const char altName[] = "example.com";
  27117. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  27118. SSL_FILETYPE_PEM));
  27119. AssertIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  27120. WOLFSSL_SUCCESS);
  27121. AssertIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  27122. WOLFSSL_FAILURE);
  27123. X509_free(x509);
  27124. AssertIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  27125. WOLFSSL_FAILURE);
  27126. res = TEST_RES_CHECK(1);
  27127. #endif
  27128. return res;
  27129. }
  27130. static int test_wolfSSL_X509_check_email(void)
  27131. {
  27132. int res = TEST_SKIPPED;
  27133. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  27134. X509* x509;
  27135. const char goodEmail[] = "info@wolfssl.com";
  27136. const char badEmail[] = "disinfo@wolfssl.com";
  27137. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  27138. SSL_FILETYPE_PEM));
  27139. /* Should fail on non-matching email address */
  27140. AssertIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  27141. WOLFSSL_FAILURE);
  27142. /* Should succeed on matching email address */
  27143. AssertIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  27144. WOLFSSL_SUCCESS);
  27145. /* Should compute length internally when not provided */
  27146. AssertIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  27147. WOLFSSL_SUCCESS);
  27148. /* Should fail when email address is NULL */
  27149. AssertIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  27150. WOLFSSL_FAILURE);
  27151. X509_free(x509);
  27152. /* Should fail when x509 is NULL */
  27153. AssertIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  27154. WOLFSSL_FAILURE);
  27155. res = TEST_RES_CHECK(1);
  27156. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  27157. return res;
  27158. }
  27159. static int test_wolfSSL_DES(void)
  27160. {
  27161. int res = TEST_SKIPPED;
  27162. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  27163. const_DES_cblock myDes;
  27164. DES_cblock iv;
  27165. DES_key_schedule key;
  27166. word32 i;
  27167. DES_LONG dl;
  27168. unsigned char msg[] = "hello wolfssl";
  27169. DES_check_key(1);
  27170. DES_set_key(&myDes, &key);
  27171. /* check, check of odd parity */
  27172. XMEMSET(myDes, 4, sizeof(const_DES_cblock)); myDes[0] = 6; /*set even parity*/
  27173. XMEMSET(key, 5, sizeof(DES_key_schedule));
  27174. AssertIntEQ(DES_set_key_checked(&myDes, &key), -1);
  27175. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  27176. /* set odd parity for success case */
  27177. DES_set_odd_parity(&myDes);
  27178. AssertIntEQ(DES_check_key_parity(&myDes), 1);
  27179. fprintf(stderr, "%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2],
  27180. myDes[3]);
  27181. AssertIntEQ(DES_set_key_checked(&myDes, &key), 0);
  27182. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  27183. AssertIntEQ(key[i], myDes[i]);
  27184. }
  27185. AssertIntEQ(DES_is_weak_key(&myDes), 0);
  27186. /* check weak key */
  27187. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  27188. XMEMSET(key, 5, sizeof(DES_key_schedule));
  27189. AssertIntEQ(DES_set_key_checked(&myDes, &key), -2);
  27190. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  27191. /* now do unchecked copy of a weak key over */
  27192. DES_set_key_unchecked(&myDes, &key);
  27193. /* compare arrays, should be the same */
  27194. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  27195. AssertIntEQ(key[i], myDes[i]);
  27196. }
  27197. AssertIntEQ(DES_is_weak_key(&myDes), 1);
  27198. /* check DES_key_sched API */
  27199. XMEMSET(key, 1, sizeof(DES_key_schedule));
  27200. AssertIntEQ(DES_key_sched(&myDes, NULL), 0);
  27201. AssertIntEQ(DES_key_sched(NULL, &key), 0);
  27202. AssertIntEQ(DES_key_sched(&myDes, &key), 0);
  27203. /* compare arrays, should be the same */
  27204. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  27205. AssertIntEQ(key[i], myDes[i]);
  27206. }
  27207. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  27208. * DES_cbc_encrypt on the input */
  27209. XMEMSET(iv, 0, sizeof(DES_cblock));
  27210. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  27211. AssertIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  27212. AssertIntEQ(dl, 480052723);
  27213. res = TEST_RES_CHECK(1);
  27214. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  27215. return res;
  27216. }
  27217. static int test_wc_PemToDer(void)
  27218. {
  27219. int res = TEST_SKIPPED;
  27220. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  27221. int ret;
  27222. DerBuffer* pDer = NULL;
  27223. const char* ca_cert = "./certs/server-cert.pem";
  27224. byte* cert_buf = NULL;
  27225. size_t cert_sz = 0;
  27226. int eccKey = 0;
  27227. EncryptedInfo info;
  27228. XMEMSET(&info, 0, sizeof(info));
  27229. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  27230. if (ret == 0) {
  27231. ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE,
  27232. &pDer, NULL, &info, &eccKey);
  27233. AssertIntEQ(ret, 0);
  27234. wc_FreeDer(&pDer);
  27235. }
  27236. if (cert_buf)
  27237. free(cert_buf);
  27238. #ifdef HAVE_ECC
  27239. {
  27240. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  27241. byte key_buf[256] = {0};
  27242. /* Test fail of loading a key with cert type */
  27243. AssertIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  27244. key_buf[0] = '\n';
  27245. XMEMCPY(key_buf + 1, cert_buf, cert_sz);
  27246. AssertIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  27247. &pDer, NULL, &info, &eccKey)), 0);
  27248. #ifdef OPENSSL_EXTRA
  27249. AssertIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  27250. &pDer, NULL, &info, &eccKey)), 0);
  27251. #endif
  27252. wc_FreeDer(&pDer);
  27253. if (cert_buf)
  27254. free(cert_buf);
  27255. }
  27256. #endif
  27257. res = TEST_RES_CHECK(1);
  27258. #endif
  27259. return res;
  27260. }
  27261. static int test_wc_AllocDer(void)
  27262. {
  27263. int res = TEST_SKIPPED;
  27264. #if !defined(NO_CERTS)
  27265. int ret;
  27266. DerBuffer* pDer = NULL;
  27267. word32 testSize = 1024;
  27268. ret = wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT);
  27269. AssertIntEQ(ret, 0);
  27270. AssertNotNull(pDer);
  27271. wc_FreeDer(&pDer);
  27272. res = TEST_RES_CHECK(1);
  27273. #endif
  27274. return res;
  27275. }
  27276. static int test_wc_CertPemToDer(void)
  27277. {
  27278. int res = TEST_SKIPPED;
  27279. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  27280. int ret;
  27281. const char* ca_cert = "./certs/ca-cert.pem";
  27282. byte* cert_buf = NULL;
  27283. size_t cert_sz = 0, cert_dersz = 0;
  27284. byte* cert_der = NULL;
  27285. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  27286. if (ret == 0) {
  27287. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  27288. cert_der = (byte*)malloc(cert_dersz);
  27289. if (cert_der) {
  27290. ret = wc_CertPemToDer(cert_buf, (int)cert_sz,
  27291. cert_der, (int)cert_dersz, CERT_TYPE);
  27292. AssertIntGE(ret, 0);
  27293. }
  27294. }
  27295. if (cert_der)
  27296. free(cert_der);
  27297. if (cert_buf)
  27298. free(cert_buf);
  27299. res = TEST_RES_CHECK(1);
  27300. #endif
  27301. return res;
  27302. }
  27303. static int test_wc_PubKeyPemToDer(void)
  27304. {
  27305. int res = TEST_SKIPPED;
  27306. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && \
  27307. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  27308. int ret;
  27309. const char* key = "./certs/ecc-client-keyPub.pem";
  27310. byte* cert_buf = NULL;
  27311. size_t cert_sz = 0, cert_dersz = 0;
  27312. byte* cert_der = NULL;
  27313. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  27314. cert_der, (int)cert_dersz);
  27315. AssertIntGE(ret, BAD_FUNC_ARG);
  27316. ret = load_file(key, &cert_buf, &cert_sz);
  27317. if (ret == 0) {
  27318. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  27319. cert_der = (byte*)malloc(cert_dersz);
  27320. if (cert_der) {
  27321. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  27322. cert_der, (int)cert_dersz);
  27323. AssertIntGE(ret, 0);
  27324. }
  27325. }
  27326. if (cert_der)
  27327. free(cert_der);
  27328. if (cert_buf)
  27329. free(cert_buf);
  27330. res = TEST_RES_CHECK(1);
  27331. #endif
  27332. return res;
  27333. }
  27334. static int test_wc_PemPubKeyToDer(void)
  27335. {
  27336. int res = TEST_SKIPPED;
  27337. #if !defined(NO_FILESYSTEM) && \
  27338. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  27339. int ret;
  27340. const char* key = "./certs/ecc-client-keyPub.pem";
  27341. size_t cert_dersz = 1024;
  27342. byte* cert_der = (byte*)malloc(cert_dersz);
  27343. ret = wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz);
  27344. AssertIntGE(ret, BAD_FUNC_ARG);
  27345. if (cert_der) {
  27346. ret = wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz);
  27347. AssertIntGE(ret, 0);
  27348. free(cert_der);
  27349. }
  27350. res = TEST_RES_CHECK(1);
  27351. #endif
  27352. return res;
  27353. }
  27354. static int test_wc_GetPubKeyDerFromCert(void)
  27355. {
  27356. int res = TEST_SKIPPED;
  27357. #if !defined(NO_RSA) || defined(HAVE_ECC)
  27358. int ret;
  27359. word32 idx = 0;
  27360. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  27361. word32 keyDerSz = (word32)sizeof(keyDer);
  27362. DecodedCert decoded;
  27363. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ)
  27364. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  27365. word32 certBufSz = sizeof(certBuf);
  27366. #endif
  27367. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  27368. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA)
  27369. XFILE fp;
  27370. #endif
  27371. #ifndef NO_RSA
  27372. RsaKey rsaKey;
  27373. #if defined(USE_CERT_BUFFERS_2048)
  27374. byte* rsaCertDer = (byte*)client_cert_der_2048;
  27375. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  27376. #elif defined(USE_CERT_BUFFERS_1024)
  27377. byte* rsaCertDer = (byte*)client_cert_der_1024;
  27378. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  27379. #else
  27380. unsigned char rsaCertDer[TWOK_BUF];
  27381. word32 rsaCertDerSz;
  27382. #endif
  27383. #endif
  27384. #ifdef HAVE_ECC
  27385. ecc_key eccKey;
  27386. #if defined(USE_CERT_BUFFERS_256)
  27387. byte* eccCert = (byte*)cliecc_cert_der_256;
  27388. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  27389. #else
  27390. unsigned char eccCert[ONEK_BUF];
  27391. word32 eccCertSz;
  27392. XFILE fp2;
  27393. #endif
  27394. #endif
  27395. #ifndef NO_RSA
  27396. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  27397. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  27398. AssertTrue((fp != XBADFILE));
  27399. rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer), fp);
  27400. XFCLOSE(fp);
  27401. #endif
  27402. /* good test case - RSA DER cert */
  27403. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  27404. ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
  27405. AssertIntEQ(ret, 0);
  27406. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  27407. AssertIntEQ(ret, 0);
  27408. AssertIntGT(keyDerSz, 0);
  27409. /* sanity check, verify we can import DER public key */
  27410. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  27411. AssertIntEQ(ret, 0);
  27412. ret = wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz);
  27413. AssertIntEQ(ret, 0);
  27414. wc_FreeRsaKey(&rsaKey);
  27415. /* test LENGTH_ONLY_E case */
  27416. keyDerSz = 0;
  27417. ret = wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz);
  27418. AssertIntEQ(ret, LENGTH_ONLY_E);
  27419. AssertIntGT(keyDerSz, 0);
  27420. /* bad args: DecodedCert NULL */
  27421. ret = wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz);
  27422. AssertIntEQ(ret, BAD_FUNC_ARG);
  27423. /* bad args: output key buff size */
  27424. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL);
  27425. AssertIntEQ(ret, BAD_FUNC_ARG);
  27426. /* bad args: zero size output key buffer */
  27427. keyDerSz = 0;
  27428. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  27429. AssertIntEQ(ret, BAD_FUNC_ARG);
  27430. wc_FreeDecodedCert(&decoded);
  27431. /* Certificate Request Tests */
  27432. #ifdef WOLFSSL_CERT_REQ
  27433. {
  27434. XMEMSET(certBuf, 0, sizeof(certBuf));
  27435. fp = XFOPEN("./certs/csr.signed.der", "rb");
  27436. AssertTrue((fp != XBADFILE));
  27437. certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp);
  27438. XFCLOSE(fp);
  27439. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  27440. ret = wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL);
  27441. AssertIntEQ(ret, 0);
  27442. /* good test case - RSA DER certificate request */
  27443. keyDerSz = sizeof(keyDer);
  27444. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  27445. AssertIntEQ(ret, 0);
  27446. AssertIntGT(keyDerSz, 0);
  27447. /* sanity check, verify we can import DER public key */
  27448. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  27449. AssertIntEQ(ret, 0);
  27450. idx = 0;
  27451. ret = wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz);
  27452. AssertIntEQ(ret, 0);
  27453. wc_FreeRsaKey(&rsaKey);
  27454. wc_FreeDecodedCert(&decoded);
  27455. }
  27456. #endif /* WOLFSSL_CERT_REQ */
  27457. #endif /* NO_RSA */
  27458. #ifdef HAVE_ECC
  27459. #ifndef USE_CERT_BUFFERS_256
  27460. fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb");
  27461. AssertTrue((fp2 != XBADFILE));
  27462. eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2);
  27463. XFCLOSE(fp2);
  27464. #endif
  27465. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  27466. ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
  27467. AssertIntEQ(ret, 0);
  27468. /* good test case - ECC */
  27469. XMEMSET(keyDer, 0, sizeof(keyDer));
  27470. keyDerSz = sizeof(keyDer);
  27471. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  27472. AssertIntEQ(ret, 0);
  27473. AssertIntGT(keyDerSz, 0);
  27474. /* sanity check, verify we can import DER public key */
  27475. ret = wc_ecc_init(&eccKey);
  27476. AssertIntEQ(ret, 0);
  27477. idx = 0; /* reset idx to 0, used above in RSA case */
  27478. ret = wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz);
  27479. AssertIntEQ(ret, 0);
  27480. wc_ecc_free(&eccKey);
  27481. /* test LENGTH_ONLY_E case */
  27482. keyDerSz = 0;
  27483. ret = wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz);
  27484. AssertIntEQ(ret, LENGTH_ONLY_E);
  27485. AssertIntGT(keyDerSz, 0);
  27486. wc_FreeDecodedCert(&decoded);
  27487. #endif
  27488. res = TEST_RES_CHECK(1);
  27489. #endif /* !NO_RSA || HAVE_ECC */
  27490. return res;
  27491. }
  27492. static int test_wc_CheckCertSigPubKey(void)
  27493. {
  27494. int res = TEST_SKIPPED;
  27495. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  27496. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  27497. int ret;
  27498. const char* ca_cert = "./certs/ca-cert.pem";
  27499. byte* cert_buf = NULL;
  27500. size_t cert_sz = 0;
  27501. byte* cert_der = NULL;
  27502. word32 cert_dersz = 0;
  27503. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  27504. word32 keyDerSz = (word32)sizeof(keyDer);
  27505. DecodedCert decoded;
  27506. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  27507. if (ret == 0) {
  27508. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  27509. cert_der = (byte*)malloc(cert_dersz);
  27510. if (cert_der) {
  27511. ret = wc_CertPemToDer(cert_buf, (int)cert_sz,
  27512. cert_der, (int)cert_dersz, CERT_TYPE);
  27513. AssertIntGE(ret, 0);
  27514. }
  27515. }
  27516. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  27517. ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
  27518. AssertIntEQ(ret, 0);
  27519. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  27520. AssertIntEQ(ret, 0);
  27521. AssertIntGT(keyDerSz, 0);
  27522. /* Good test case. */
  27523. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, keyDerSz,
  27524. RSAk);
  27525. AssertIntEQ(ret, 0);
  27526. /* No certificate. */
  27527. ret = wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  27528. ECDSAk);
  27529. AssertIntEQ(ret, BAD_FUNC_ARG);
  27530. /* Bad cert size. */
  27531. ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  27532. RSAk);
  27533. AssertTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  27534. /* No public key. */
  27535. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL, keyDerSz,
  27536. RSAk);
  27537. AssertIntEQ(ret, ASN_NO_SIGNER_E);
  27538. /* Bad public key size. */
  27539. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  27540. RSAk);
  27541. AssertIntEQ(ret, BAD_FUNC_ARG);
  27542. /* Wrong aglo. */
  27543. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, keyDerSz,
  27544. ECDSAk);
  27545. AssertIntEQ(ret, ASN_PARSE_E);
  27546. wc_FreeDecodedCert(&decoded);
  27547. if (cert_der)
  27548. free(cert_der);
  27549. if (cert_buf)
  27550. free(cert_buf);
  27551. res = TEST_RES_CHECK(1);
  27552. #endif
  27553. return res;
  27554. }
  27555. static int test_wolfSSL_certs(void)
  27556. {
  27557. int res = TEST_SKIPPED;
  27558. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  27559. !defined(NO_RSA)
  27560. X509* x509ext;
  27561. #ifdef OPENSSL_ALL
  27562. X509* x509;
  27563. WOLFSSL_X509_EXTENSION* ext;
  27564. ASN1_OBJECT* obj;
  27565. #endif
  27566. WOLFSSL* ssl;
  27567. WOLFSSL_CTX* ctx;
  27568. STACK_OF(ASN1_OBJECT)* sk;
  27569. ASN1_STRING* asn1_str;
  27570. AUTHORITY_KEYID* akey;
  27571. BASIC_CONSTRAINTS* bc;
  27572. int crit;
  27573. #ifndef NO_WOLFSSL_SERVER
  27574. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  27575. #else
  27576. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  27577. #endif
  27578. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  27579. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  27580. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  27581. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27582. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  27583. #endif
  27584. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  27585. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27586. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  27587. #endif
  27588. AssertNotNull(ssl = SSL_new(ctx));
  27589. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27590. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27591. #endif
  27592. #ifdef HAVE_PK_CALLBACKS
  27593. AssertIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  27594. #endif /* HAVE_PK_CALLBACKS */
  27595. /* create and use x509 */
  27596. #ifdef OPENSSL_ALL
  27597. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  27598. AssertNotNull(x509);
  27599. #endif
  27600. x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt, WOLFSSL_FILETYPE_PEM);
  27601. AssertNotNull(x509ext);
  27602. AssertIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  27603. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27604. /* with loading in a new cert the check on private key should now fail */
  27605. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27606. #endif
  27607. #if defined(USE_CERT_BUFFERS_2048)
  27608. AssertIntEQ(SSL_use_certificate_ASN1(ssl,
  27609. (unsigned char*)server_cert_der_2048,
  27610. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  27611. #endif
  27612. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  27613. /************* Get Digest of Certificate ******************/
  27614. {
  27615. byte digest[64]; /* max digest size */
  27616. word32 digestSz;
  27617. XMEMSET(digest, 0, sizeof(digest));
  27618. AssertIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  27619. WOLFSSL_SUCCESS);
  27620. AssertIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  27621. WOLFSSL_SUCCESS);
  27622. AssertIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  27623. WOLFSSL_FAILURE);
  27624. }
  27625. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  27626. /* test and checkout X509 extensions */
  27627. bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext, NID_basic_constraints,
  27628. &crit, NULL);
  27629. AssertNotNull(bc);
  27630. AssertIntEQ(crit, 0);
  27631. #ifdef OPENSSL_ALL
  27632. ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc);
  27633. AssertNotNull(ext);
  27634. X509_EXTENSION_free(ext);
  27635. AssertNotNull(ext = X509_EXTENSION_new());
  27636. X509_EXTENSION_set_critical(ext, 1);
  27637. AssertNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  27638. AssertIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  27639. ASN1_OBJECT_free(obj);
  27640. X509_EXTENSION_free(ext);
  27641. AssertNotNull(ext = X509_EXTENSION_new());
  27642. X509_EXTENSION_set_critical(ext, 0);
  27643. AssertIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  27644. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  27645. NULL);
  27646. AssertIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  27647. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  27648. * and X509_get_ext_d2i has created new */
  27649. X509_EXTENSION_free(ext);
  27650. #endif
  27651. BASIC_CONSTRAINTS_free(bc);
  27652. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit, NULL);
  27653. AssertNotNull(asn1_str);
  27654. AssertIntEQ(crit, 1);
  27655. AssertIntEQ(asn1_str->type, NID_key_usage);
  27656. #ifdef OPENSSL_ALL
  27657. ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str);
  27658. AssertNotNull(ext);
  27659. X509_EXTENSION_free(ext);
  27660. #endif
  27661. ASN1_STRING_free(asn1_str);
  27662. #ifdef OPENSSL_ALL
  27663. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_ext_key_usage,
  27664. &crit, NULL);
  27665. AssertNotNull(sk);
  27666. ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk);
  27667. AssertNotNull(ext);
  27668. X509_EXTENSION_free(ext);
  27669. sk_ASN1_OBJECT_pop_free(sk, NULL);
  27670. #else
  27671. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  27672. &crit, NULL);
  27673. AssertNull(sk);
  27674. #endif
  27675. akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  27676. NID_authority_key_identifier, &crit, NULL);
  27677. AssertNotNull(akey);
  27678. #ifdef OPENSSL_ALL
  27679. ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit, akey);
  27680. AssertNotNull(ext);
  27681. X509_EXTENSION_free(ext);
  27682. #endif
  27683. wolfSSL_AUTHORITY_KEYID_free(akey);
  27684. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  27685. NID_private_key_usage_period, &crit, NULL);
  27686. /* AssertNotNull(sk); NID not yet supported */
  27687. AssertIntEQ(crit, -1);
  27688. sk_ASN1_OBJECT_free(sk);
  27689. sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext, NID_subject_alt_name,
  27690. &crit, NULL);
  27691. {
  27692. int i;
  27693. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  27694. GENERAL_NAME* gen = sk_GENERAL_NAME_value(sk, i);
  27695. AssertIntEQ(gen->type, GEN_DNS);
  27696. AssertIntEQ(gen->d.dNSName->type, V_ASN1_IA5STRING);
  27697. }
  27698. }
  27699. /* AssertNotNull(sk); no alt names set */
  27700. sk_GENERAL_NAME_free(sk);
  27701. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_issuer_alt_name,
  27702. &crit, NULL);
  27703. /* AssertNotNull(sk); NID not yet supported */
  27704. AssertIntEQ(crit, -1);
  27705. sk_ASN1_OBJECT_free(sk);
  27706. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_info_access, &crit,
  27707. NULL);
  27708. /* AssertNotNull(sk); no auth info set */
  27709. sk_ASN1_OBJECT_free(sk);
  27710. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_sinfo_access,
  27711. &crit, NULL);
  27712. /* AssertNotNull(sk); NID not yet supported */
  27713. AssertIntEQ(crit, -1);
  27714. sk_ASN1_OBJECT_free(sk);
  27715. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_name_constraints,
  27716. &crit, NULL);
  27717. /* AssertNotNull(sk); NID not yet supported */
  27718. AssertIntEQ(crit, -1);
  27719. sk_ASN1_OBJECT_free(sk);
  27720. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  27721. NID_certificate_policies, &crit, NULL);
  27722. #if !defined(WOLFSSL_SEP) && !defined(WOLFSSL_CERT_EXT)
  27723. AssertNull(sk);
  27724. #else
  27725. /* AssertNotNull(sk); no cert policy set */
  27726. #endif
  27727. sk_ASN1_OBJECT_free(sk);
  27728. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_policy_mappings,
  27729. &crit, NULL);
  27730. /* AssertNotNull(sk); NID not yet supported */
  27731. AssertIntEQ(crit, -1);
  27732. sk_ASN1_OBJECT_free(sk);
  27733. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_policy_constraints,
  27734. &crit, NULL);
  27735. /* AssertNotNull(sk); NID not yet supported */
  27736. AssertIntEQ(crit, -1);
  27737. sk_ASN1_OBJECT_free(sk);
  27738. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_inhibit_any_policy,
  27739. &crit, NULL);
  27740. /* AssertNotNull(sk); NID not yet supported */
  27741. AssertIntEQ(crit, -1);
  27742. sk_ASN1_OBJECT_free(sk);
  27743. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_tlsfeature, &crit,
  27744. NULL);
  27745. /* AssertNotNull(sk); NID not yet supported */
  27746. AssertIntEQ(crit, -1);
  27747. sk_ASN1_OBJECT_free(sk);
  27748. /* test invalid cases */
  27749. crit = 0;
  27750. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit, NULL);
  27751. AssertNull(sk);
  27752. AssertIntEQ(crit, -1);
  27753. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL, NID_tlsfeature,
  27754. NULL, NULL);
  27755. AssertNull(sk);
  27756. AssertIntEQ(SSL_get_hit(ssl), 0);
  27757. #ifdef OPENSSL_ALL
  27758. X509_free(x509);
  27759. #endif
  27760. X509_free(x509ext);
  27761. SSL_free(ssl);
  27762. SSL_CTX_free(ctx);
  27763. res = TEST_RES_CHECK(1);
  27764. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  27765. return res;
  27766. }
  27767. static int test_wolfSSL_X509_check_private_key(void)
  27768. {
  27769. int res = TEST_SKIPPED;
  27770. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  27771. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  27772. X509* x509;
  27773. EVP_PKEY* pkey = NULL;
  27774. const byte* key;
  27775. /* Check with correct key */
  27776. AssertNotNull((x509 = X509_load_certificate_file(cliCertFile,
  27777. SSL_FILETYPE_PEM)));
  27778. key = client_key_der_2048;
  27779. AssertNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  27780. &key, (long)sizeof_client_key_der_2048));
  27781. AssertIntEQ(X509_check_private_key(x509, pkey), 1);
  27782. EVP_PKEY_free(pkey);
  27783. pkey = NULL;
  27784. /* Check with wrong key */
  27785. key = server_key_der_2048;
  27786. AssertNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  27787. &key, (long)sizeof_server_key_der_2048));
  27788. AssertIntEQ(X509_check_private_key(x509, pkey), 0);
  27789. /* test for incorrect parameter */
  27790. AssertIntEQ(X509_check_private_key(NULL, pkey), 0);
  27791. AssertIntEQ(X509_check_private_key(x509, NULL), 0);
  27792. AssertIntEQ(X509_check_private_key(NULL, NULL), 0);
  27793. EVP_PKEY_free(pkey);
  27794. X509_free(x509);
  27795. res = TEST_RES_CHECK(1);
  27796. #endif
  27797. return res;
  27798. }
  27799. static int test_wolfSSL_ASN1_TIME_print(void)
  27800. {
  27801. int res = TEST_SKIPPED;
  27802. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) \
  27803. && (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  27804. defined(WOLFSSL_HAPROXY)) && defined(USE_CERT_BUFFERS_2048) && \
  27805. !defined(NO_BIO)
  27806. BIO* bio;
  27807. X509* x509;
  27808. const unsigned char* der = client_cert_der_2048;
  27809. ASN1_TIME* t;
  27810. unsigned char buf[25];
  27811. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  27812. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  27813. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  27814. AssertIntEQ(ASN1_TIME_print(bio, X509_get_notBefore(x509)), 1);
  27815. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  27816. AssertIntEQ(XMEMCMP(buf, "Feb 15 12:50:24 2022 GMT", sizeof(buf) - 1), 0);
  27817. /* create a bad time and test results */
  27818. AssertNotNull(t = X509_get_notAfter(x509));
  27819. AssertIntEQ(ASN1_TIME_check(t), WOLFSSL_SUCCESS);
  27820. t->data[8] = 0;
  27821. t->data[3] = 0;
  27822. AssertIntNE(ASN1_TIME_print(bio, t), 1);
  27823. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  27824. AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  27825. AssertIntEQ(ASN1_TIME_check(t), WOLFSSL_FAILURE);
  27826. BIO_free(bio);
  27827. X509_free(x509);
  27828. res = TEST_RES_CHECK(1);
  27829. #endif
  27830. return res;
  27831. }
  27832. static int test_wolfSSL_ASN1_UTCTIME_print(void)
  27833. {
  27834. int res = TEST_SKIPPED;
  27835. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  27836. BIO* bio;
  27837. ASN1_UTCTIME* utc = NULL;
  27838. unsigned char buf[25];
  27839. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  27840. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  27841. /* NULL parameter check */
  27842. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  27843. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  27844. BIO_free(bio);
  27845. /* Valid date */
  27846. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  27847. AssertNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  27848. DYNAMIC_TYPE_ASN1));
  27849. utc->type = ASN_UTC_TIME;
  27850. utc->length = ASN_UTC_TIME_SIZE;
  27851. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  27852. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  27853. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  27854. AssertIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  27855. XMEMSET(buf, 0, sizeof(buf));
  27856. BIO_free(bio);
  27857. /* Invalid format */
  27858. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  27859. utc->type = ASN_UTC_TIME;
  27860. utc->length = ASN_UTC_TIME_SIZE;
  27861. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  27862. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  27863. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  27864. AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  27865. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  27866. BIO_free(bio);
  27867. res = TEST_RES_CHECK(1);
  27868. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  27869. return res;
  27870. }
  27871. static int test_wolfSSL_ASN1_TIME_diff_compare(void)
  27872. {
  27873. int res = TEST_SKIPPED;
  27874. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  27875. ASN1_TIME* fromTime;
  27876. ASN1_TIME* toTime;
  27877. int daysDiff;
  27878. int secsDiff;
  27879. AssertNotNull((fromTime = ASN1_TIME_new()));
  27880. /* Feb 22, 2003, 21:15:15 */
  27881. AssertIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), WOLFSSL_SUCCESS);
  27882. AssertNotNull((toTime = ASN1_TIME_new()));
  27883. /* Dec 19, 2010, 18:10:11 */
  27884. AssertIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), WOLFSSL_SUCCESS);
  27885. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), WOLFSSL_SUCCESS);
  27886. /* Error conditions. */
  27887. AssertIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime),
  27888. WOLFSSL_FAILURE);
  27889. AssertIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime),
  27890. WOLFSSL_FAILURE);
  27891. /* If both times are NULL, difference is 0. */
  27892. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL),
  27893. WOLFSSL_SUCCESS);
  27894. AssertIntEQ(daysDiff, 0);
  27895. AssertIntEQ(secsDiff, 0);
  27896. /* If one time is NULL, it defaults to the current time. */
  27897. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime),
  27898. WOLFSSL_SUCCESS);
  27899. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL),
  27900. WOLFSSL_SUCCESS);
  27901. /* Normal operation. Both times non-NULL. */
  27902. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime),
  27903. WOLFSSL_SUCCESS);
  27904. AssertIntEQ(daysDiff, 2856);
  27905. AssertIntEQ(secsDiff, 75296);
  27906. /* Swapping the times should return negative values. */
  27907. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime),
  27908. WOLFSSL_SUCCESS);
  27909. AssertIntEQ(daysDiff, -2856);
  27910. AssertIntEQ(secsDiff, -75296);
  27911. AssertIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  27912. AssertIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  27913. AssertIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  27914. /* Compare regression test: No seconds difference, just difference in days.
  27915. */
  27916. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  27917. ASN1_TIME_set_string(toTime, "19800101000000Z");
  27918. AssertIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  27919. AssertIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  27920. AssertIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  27921. /* Edge case with Unix epoch. */
  27922. AssertNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  27923. AssertNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  27924. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime),
  27925. WOLFSSL_SUCCESS);
  27926. AssertIntEQ(daysDiff, 3652);
  27927. AssertIntEQ(secsDiff, 0);
  27928. /* Edge case with year > 2038 (year 2038 problem). */
  27929. AssertNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  27930. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime),
  27931. WOLFSSL_SUCCESS);
  27932. AssertIntEQ(daysDiff, 2932896);
  27933. AssertIntEQ(secsDiff, 86399);
  27934. ASN1_TIME_free(fromTime);
  27935. ASN1_TIME_free(toTime);
  27936. res = TEST_RES_CHECK(1);
  27937. #endif
  27938. return res;
  27939. }
  27940. static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  27941. {
  27942. int res = TEST_SKIPPED;
  27943. #if defined(OPENSSL_EXTRA)
  27944. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime;
  27945. unsigned char nullstr[32];
  27946. XMEMSET(nullstr, 0, 32);
  27947. asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  27948. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL,
  27949. DYNAMIC_TYPE_TMP_BUFFER);
  27950. if (asn1_gtime) {
  27951. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  27952. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  27953. AssertIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  27954. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27955. }
  27956. res = TEST_RES_CHECK(1);
  27957. #endif /* OPENSSL_EXTRA */
  27958. return res;
  27959. }
  27960. static int test_wolfSSL_private_keys(void)
  27961. {
  27962. int res = TEST_SKIPPED;
  27963. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  27964. !defined(NO_FILESYSTEM)
  27965. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  27966. WOLFSSL* ssl;
  27967. WOLFSSL_CTX* ctx;
  27968. EVP_PKEY* pkey = NULL;
  27969. OpenSSL_add_all_digests();
  27970. OpenSSL_add_all_algorithms();
  27971. #ifndef NO_RSA
  27972. #ifndef NO_WOLFSSL_SERVER
  27973. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  27974. #else
  27975. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  27976. #endif
  27977. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  27978. /* Have to load a cert before you can check the private key against that
  27979. * certificates public key! */
  27980. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27981. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  27982. #endif
  27983. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  27984. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27985. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  27986. #endif
  27987. AssertNotNull(ssl = SSL_new(ctx));
  27988. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27989. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27990. #endif
  27991. #ifdef USE_CERT_BUFFERS_2048
  27992. {
  27993. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  27994. unsigned char buf[FOURK_BUF];
  27995. word32 bufSz;
  27996. AssertIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  27997. (unsigned char*)client_key_der_2048,
  27998. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  27999. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28000. /* Should mismatch now that a different private key loaded */
  28001. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28002. #endif
  28003. AssertIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  28004. (unsigned char*)server_key,
  28005. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  28006. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28007. /* After loading back in DER format of original key, should match */
  28008. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28009. #endif
  28010. /* test loading private key to the WOLFSSL_CTX */
  28011. AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  28012. (unsigned char*)client_key_der_2048,
  28013. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  28014. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28015. /* Should mismatch now that a different private key loaded */
  28016. AssertIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  28017. #endif
  28018. AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  28019. (unsigned char*)server_key,
  28020. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  28021. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28022. /* After loading back in DER format of original key, should match */
  28023. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  28024. #endif
  28025. /* pkey not set yet, expecting to fail */
  28026. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  28027. /* set PKEY and test again */
  28028. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  28029. &server_key, (long)sizeof_server_key_der_2048));
  28030. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  28031. /* reuse PKEY structure and test
  28032. * this should be checked with a memory management sanity checker */
  28033. AssertFalse(server_key == (const unsigned char*)server_key_der_2048);
  28034. server_key = (const unsigned char*)server_key_der_2048;
  28035. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  28036. &server_key, (long)sizeof_server_key_der_2048));
  28037. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  28038. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  28039. bufSz = FOURK_BUF;
  28040. AssertIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  28041. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  28042. RSAk, NULL, 0)), 0);
  28043. server_key = (const unsigned char*)buf;
  28044. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  28045. (long)bufSz));
  28046. }
  28047. #endif
  28048. EVP_PKEY_free(pkey);
  28049. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  28050. SSL_CTX_free(ctx);
  28051. #endif /* end of RSA private key match tests */
  28052. #ifdef HAVE_ECC
  28053. #ifndef NO_WOLFSSL_SERVER
  28054. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28055. #else
  28056. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28057. #endif
  28058. AssertTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  28059. WOLFSSL_FILETYPE_PEM));
  28060. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  28061. WOLFSSL_FILETYPE_PEM));
  28062. AssertNotNull(ssl = SSL_new(ctx));
  28063. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28064. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28065. #endif
  28066. SSL_free(ssl);
  28067. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  28068. WOLFSSL_FILETYPE_PEM));
  28069. AssertNotNull(ssl = SSL_new(ctx));
  28070. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  28071. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28072. #endif
  28073. SSL_free(ssl);
  28074. SSL_CTX_free(ctx);
  28075. #endif /* end of ECC private key match tests */
  28076. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  28077. #ifndef NO_WOLFSSL_SERVER
  28078. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28079. #else
  28080. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28081. #endif
  28082. AssertTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  28083. WOLFSSL_FILETYPE_PEM));
  28084. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  28085. WOLFSSL_FILETYPE_PEM));
  28086. AssertNotNull(ssl = SSL_new(ctx));
  28087. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28088. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28089. #endif
  28090. SSL_free(ssl);
  28091. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  28092. WOLFSSL_FILETYPE_PEM));
  28093. AssertNotNull(ssl = SSL_new(ctx));
  28094. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28095. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28096. #endif
  28097. SSL_free(ssl);
  28098. SSL_CTX_free(ctx);
  28099. #endif /* end of Ed25519 private key match tests */
  28100. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  28101. #ifndef NO_WOLFSSL_SERVER
  28102. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28103. #else
  28104. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28105. #endif
  28106. AssertTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  28107. WOLFSSL_FILETYPE_PEM));
  28108. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  28109. WOLFSSL_FILETYPE_PEM));
  28110. AssertNotNull(ssl = SSL_new(ctx));
  28111. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28112. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28113. #endif
  28114. SSL_free(ssl);
  28115. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  28116. WOLFSSL_FILETYPE_PEM));
  28117. AssertNotNull(ssl = SSL_new(ctx));
  28118. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28119. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28120. #endif
  28121. SSL_free(ssl);
  28122. SSL_CTX_free(ctx);
  28123. #endif /* end of Ed448 private key match tests */
  28124. EVP_cleanup();
  28125. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  28126. CONF_modules_free();
  28127. ENGINE_cleanup();
  28128. CONF_modules_unload();
  28129. (void)ssl;
  28130. (void)ctx;
  28131. (void)pkey;
  28132. res = TEST_RES_CHECK(1);
  28133. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  28134. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  28135. return res;
  28136. }
  28137. static int test_wolfSSL_PEM_read_PrivateKey(void)
  28138. {
  28139. int res = TEST_SKIPPED;
  28140. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  28141. && !defined(NO_FILESYSTEM)
  28142. XFILE file;
  28143. const char* fname = "./certs/server-key.pem";
  28144. EVP_PKEY* pkey;
  28145. RSA* rsa;
  28146. WOLFSSL_EVP_PKEY_CTX* ctx;
  28147. unsigned char* sig;
  28148. size_t sigLen;
  28149. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  28150. size_t tbsLen = sizeof(tbs);
  28151. /* Check error case. */
  28152. AssertNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  28153. /* Read in an RSA key. */
  28154. file = XFOPEN(fname, "rb");
  28155. AssertTrue(file != XBADFILE);
  28156. AssertNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  28157. XFCLOSE(file);
  28158. /* Make sure the key is usable by signing some data with it. */
  28159. AssertNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  28160. AssertIntGT((sigLen = RSA_size(rsa)), 0);
  28161. AssertNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  28162. DYNAMIC_TYPE_TMP_BUFFER));
  28163. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  28164. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  28165. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  28166. WOLFSSL_SUCCESS);
  28167. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28168. EVP_PKEY_CTX_free(ctx);
  28169. EVP_PKEY_free(pkey);
  28170. res = TEST_RES_CHECK(1);
  28171. #endif
  28172. return res;
  28173. }
  28174. static int test_wolfSSL_PEM_read_PUBKEY(void)
  28175. {
  28176. int res = TEST_SKIPPED;
  28177. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  28178. && !defined(NO_FILESYSTEM)
  28179. XFILE file;
  28180. const char* fname = "./certs/client-keyPub.pem";
  28181. EVP_PKEY* pkey;
  28182. /* Check error case. */
  28183. AssertNull(pkey = PEM_read_PUBKEY(NULL, NULL, NULL, NULL));
  28184. /* Read in an RSA key. */
  28185. file = XFOPEN(fname, "rb");
  28186. AssertTrue(file != XBADFILE);
  28187. AssertNotNull(pkey = PEM_read_PUBKEY(file, NULL, NULL, NULL));
  28188. EVP_PKEY_free(pkey);
  28189. XFCLOSE(file);
  28190. res = TEST_RES_CHECK(1);
  28191. #endif
  28192. return res;
  28193. }
  28194. static int test_wolfSSL_PEM_PrivateKey(void)
  28195. {
  28196. int res = TEST_SKIPPED;
  28197. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28198. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  28199. #ifndef NO_BIO
  28200. BIO* bio = NULL;
  28201. #endif
  28202. EVP_PKEY* pkey = NULL;
  28203. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  28204. #ifndef NO_BIO
  28205. /* test creating new EVP_PKEY with bad arg */
  28206. AssertNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  28207. /* test loading RSA key using BIO */
  28208. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  28209. {
  28210. XFILE file;
  28211. const char* fname = "./certs/server-key.pem";
  28212. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  28213. size_t sz;
  28214. byte* buf;
  28215. EVP_PKEY* pkey2;
  28216. EVP_PKEY* pkey3;
  28217. RSA* rsa_key = NULL;
  28218. file = XFOPEN(fname, "rb");
  28219. AssertTrue((file != XBADFILE));
  28220. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  28221. sz = XFTELL(file);
  28222. XREWIND(file);
  28223. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  28224. if (buf) {
  28225. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  28226. }
  28227. XFCLOSE(file);
  28228. /* Test using BIO new mem and loading PEM private key */
  28229. bio = BIO_new_mem_buf(buf, (int)sz);
  28230. AssertNotNull(bio);
  28231. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  28232. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  28233. BIO_free(bio);
  28234. bio = NULL;
  28235. AssertNotNull(pkey2 = EVP_PKEY_new());
  28236. pkey2->type = EVP_PKEY_RSA;
  28237. /* Test parameter copy */
  28238. AssertIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  28239. EVP_PKEY_free(pkey2);
  28240. EVP_PKEY_free(pkey);
  28241. pkey = NULL;
  28242. /* Qt unit test case : rsa pkcs8 key */
  28243. file = XFOPEN(fname_rsa_p8, "rb");
  28244. AssertTrue((file != XBADFILE));
  28245. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  28246. sz = XFTELL(file);
  28247. XREWIND(file);
  28248. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  28249. if (buf)
  28250. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  28251. XFCLOSE(file);
  28252. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  28253. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  28254. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  28255. BIO_free(bio);
  28256. bio = NULL;
  28257. AssertNotNull(pkey3 = EVP_PKEY_new());
  28258. AssertNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  28259. AssertIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  28260. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  28261. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  28262. #else
  28263. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  28264. #endif
  28265. RSA_free(rsa_key);
  28266. EVP_PKEY_free(pkey3);
  28267. EVP_PKEY_free(pkey);
  28268. pkey = NULL;
  28269. }
  28270. #endif
  28271. /* test loading ECC key using BIO */
  28272. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  28273. {
  28274. XFILE file;
  28275. const char* fname = "./certs/ecc-key.pem";
  28276. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  28277. size_t sz;
  28278. byte* buf;
  28279. EVP_PKEY* pkey2;
  28280. EVP_PKEY* pkey3;
  28281. EC_KEY* ec_key;
  28282. int nid = 0;
  28283. file = XFOPEN(fname, "rb");
  28284. AssertTrue((file != XBADFILE));
  28285. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  28286. sz = XFTELL(file);
  28287. XREWIND(file);
  28288. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  28289. if (buf)
  28290. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  28291. XFCLOSE(file);
  28292. /* Test using BIO new mem and loading PEM private key */
  28293. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  28294. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  28295. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  28296. BIO_free(bio);
  28297. bio = NULL;
  28298. AssertNotNull(pkey2 = EVP_PKEY_new());
  28299. AssertNotNull(pkey3 = EVP_PKEY_new());
  28300. pkey2->type = EVP_PKEY_EC;
  28301. /* Test parameter copy */
  28302. AssertIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  28303. /* Qt unit test case 1*/
  28304. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  28305. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  28306. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  28307. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  28308. #else
  28309. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  28310. #endif
  28311. /* Test default digest */
  28312. AssertIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  28313. AssertIntEQ(nid, NID_sha256);
  28314. EC_KEY_free(ec_key);
  28315. EVP_PKEY_free(pkey3);
  28316. EVP_PKEY_free(pkey2);
  28317. EVP_PKEY_free(pkey);
  28318. pkey = NULL;
  28319. /* Qt unit test case ec pkcs8 key */
  28320. file = XFOPEN(fname_ecc_p8, "rb");
  28321. AssertTrue((file != XBADFILE));
  28322. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  28323. sz = XFTELL(file);
  28324. XREWIND(file);
  28325. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  28326. if (buf)
  28327. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  28328. XFCLOSE(file);
  28329. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  28330. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  28331. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  28332. BIO_free(bio);
  28333. bio = NULL;
  28334. AssertNotNull(pkey3 = EVP_PKEY_new());
  28335. /* Qt unit test case */
  28336. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  28337. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  28338. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  28339. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  28340. #else
  28341. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  28342. #endif
  28343. EC_KEY_free(ec_key);
  28344. EVP_PKEY_free(pkey3);
  28345. EVP_PKEY_free(pkey);
  28346. pkey = NULL;
  28347. }
  28348. #endif
  28349. #if !defined(NO_BIO) && !defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || \
  28350. defined(WOLFSSL_CERT_GEN))
  28351. {
  28352. #define BIO_PEM_TEST_CHAR 'a'
  28353. EVP_PKEY* pkey2 = NULL;
  28354. unsigned char extra[10];
  28355. int i;
  28356. BIO* pub_bio = NULL;
  28357. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  28358. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28359. AssertIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  28360. AssertNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28361. AssertIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  28362. AssertNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
  28363. &server_key, (long)sizeof_server_key_der_2048));
  28364. AssertNull(pkey);
  28365. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  28366. &server_key, (long)sizeof_server_key_der_2048));
  28367. AssertIntEQ(PEM_write_bio_PrivateKey(NULL, pkey, NULL, NULL, 0, NULL,
  28368. NULL), WOLFSSL_FAILURE);
  28369. AssertIntEQ(PEM_write_bio_PrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  28370. NULL), WOLFSSL_FAILURE);
  28371. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  28372. NULL), WOLFSSL_SUCCESS);
  28373. AssertIntGT(BIO_pending(bio), 0);
  28374. AssertIntEQ(BIO_pending(bio), 1679);
  28375. /* Check if the pubkey API writes only the public key */
  28376. #ifdef WOLFSSL_KEY_GEN
  28377. AssertIntEQ(PEM_write_bio_PUBKEY(NULL, pkey), WOLFSSL_FAILURE);
  28378. AssertIntEQ(PEM_write_bio_PUBKEY(pub_bio, NULL), WOLFSSL_FAILURE);
  28379. AssertIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  28380. AssertIntGT(BIO_pending(pub_bio), 0);
  28381. /* Previously both the private key and the pubkey calls would write
  28382. * out the private key and the PEM header was the only difference.
  28383. * The public PEM should be significantly shorter than the
  28384. * private key versison. */
  28385. AssertIntEQ(BIO_pending(pub_bio), 451);
  28386. #endif
  28387. /* test creating new EVP_PKEY with good args */
  28388. AssertNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  28389. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr)
  28390. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz), 0);
  28391. /* test of reuse of EVP_PKEY */
  28392. AssertNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  28393. AssertIntEQ(BIO_pending(bio), 0);
  28394. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  28395. SSL_SUCCESS);
  28396. AssertIntEQ(BIO_write(bio, extra, 10), 10); /* add 10 extra bytes after PEM */
  28397. AssertNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  28398. AssertNotNull(pkey);
  28399. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  28400. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
  28401. }
  28402. AssertIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
  28403. AssertIntEQ(BIO_read(bio, extra, 10), 10);
  28404. for (i = 0; i < 10; i++) {
  28405. AssertIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  28406. }
  28407. BIO_free(pub_bio);
  28408. BIO_free(bio);
  28409. bio = NULL;
  28410. EVP_PKEY_free(pkey);
  28411. pkey = NULL;
  28412. EVP_PKEY_free(pkey2);
  28413. }
  28414. #endif
  28415. /* key is DES encrypted */
  28416. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  28417. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  28418. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && \
  28419. !defined(HAVE_USER_RSA) && !defined(NO_RSA)
  28420. {
  28421. XFILE f;
  28422. wc_pem_password_cb* passwd_cb;
  28423. void* passwd_cb_userdata;
  28424. SSL_CTX* ctx;
  28425. char passwd[] = "bad password";
  28426. #ifndef WOLFSSL_NO_TLS12
  28427. #ifndef NO_WOLFSSL_SERVER
  28428. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  28429. #else
  28430. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  28431. #endif
  28432. #else
  28433. #ifndef NO_WOLFSSL_SERVER
  28434. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  28435. #else
  28436. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  28437. #endif
  28438. #endif
  28439. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  28440. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  28441. AssertNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  28442. AssertNull(passwd_cb_userdata =
  28443. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  28444. /* fail case with password call back */
  28445. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  28446. (void*)passwd));
  28447. BIO_free(bio);
  28448. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  28449. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  28450. (void*)passwd));
  28451. BIO_free(bio);
  28452. f = XFOPEN("./certs/server-keyEnc.pem", "rb");
  28453. AssertNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  28454. /* use callback that works */
  28455. AssertNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  28456. (void*)"yassl123"));
  28457. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  28458. EVP_PKEY_free(pkey);
  28459. pkey = NULL;
  28460. BIO_free(bio);
  28461. bio = NULL;
  28462. SSL_CTX_free(ctx);
  28463. }
  28464. #endif /* !defined(NO_DES3) */
  28465. #endif /* !NO_BIO */
  28466. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  28467. {
  28468. unsigned char buf[2048];
  28469. size_t bytes;
  28470. XFILE f;
  28471. SSL_CTX* ctx;
  28472. #ifndef WOLFSSL_NO_TLS12
  28473. #ifndef NO_WOLFSSL_SERVER
  28474. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  28475. #else
  28476. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  28477. #endif
  28478. #else
  28479. #ifndef NO_WOLFSSL_SERVER
  28480. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  28481. #else
  28482. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  28483. #endif
  28484. #endif
  28485. f = XFOPEN("./certs/ecc-key.der", "rb");
  28486. AssertTrue((f != XBADFILE));
  28487. bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f);
  28488. XFCLOSE(f);
  28489. server_key = buf;
  28490. pkey = NULL;
  28491. AssertNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  28492. AssertNull(pkey);
  28493. AssertNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  28494. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  28495. EVP_PKEY_free(pkey);
  28496. pkey = NULL;
  28497. SSL_CTX_free(ctx);
  28498. }
  28499. #endif
  28500. res = TEST_RES_CHECK(1);
  28501. #ifndef NO_BIO
  28502. (void)bio;
  28503. #endif
  28504. (void)pkey;
  28505. (void)server_key;
  28506. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  28507. return res;
  28508. }
  28509. static int test_wolfSSL_PEM_file_RSAKey(void)
  28510. {
  28511. int res = TEST_SKIPPED;
  28512. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  28513. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  28514. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  28515. RSA* rsa = NULL;
  28516. XFILE fp;
  28517. AssertTrue((fp = XFOPEN("./certs/rsa-pub-2048.pem", "rb")) != XBADFILE);
  28518. AssertNotNull((rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)));
  28519. XFCLOSE(fp);
  28520. AssertIntEQ(RSA_size(rsa), 256);
  28521. AssertIntEQ(PEM_write_RSAPublicKey(XBADFILE, rsa), WOLFSSL_FAILURE);
  28522. AssertIntEQ(PEM_write_RSAPublicKey(stderr, NULL), WOLFSSL_FAILURE);
  28523. AssertIntEQ(PEM_write_RSAPublicKey(stderr, rsa), WOLFSSL_SUCCESS);
  28524. AssertIntEQ(PEM_write_RSA_PUBKEY(XBADFILE, rsa), WOLFSSL_FAILURE);
  28525. AssertIntEQ(PEM_write_RSA_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  28526. AssertIntEQ(PEM_write_RSA_PUBKEY(stderr, rsa), WOLFSSL_SUCCESS);
  28527. RSA_free(rsa);
  28528. res = TEST_RES_CHECK(1);
  28529. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  28530. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  28531. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  28532. return res;
  28533. }
  28534. static int test_wolfSSL_PEM_file_RSAPrivateKey(void)
  28535. {
  28536. int res = TEST_SKIPPED;
  28537. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  28538. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && \
  28539. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  28540. RSA* rsa = NULL;
  28541. XFILE f = NULL;
  28542. f = XFOPEN(svrKeyFile, "r");
  28543. AssertTrue((f != XBADFILE));
  28544. AssertNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  28545. AssertIntEQ(RSA_size(rsa), 256);
  28546. AssertIntEQ(PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0, NULL,
  28547. NULL), WOLFSSL_FAILURE);
  28548. AssertIntEQ(PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  28549. NULL), WOLFSSL_FAILURE);
  28550. AssertIntEQ(PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0, NULL, NULL),
  28551. WOLFSSL_SUCCESS);
  28552. RSA_free(rsa);
  28553. XFCLOSE(f);
  28554. #ifdef HAVE_ECC
  28555. f = XFOPEN(eccKeyFile, "r");
  28556. AssertTrue((f != XBADFILE));
  28557. AssertNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  28558. XFCLOSE(f);
  28559. #endif /* HAVE_ECC */
  28560. res = TEST_RES_CHECK(1);
  28561. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  28562. return res;
  28563. }
  28564. #ifndef NO_BIO
  28565. static int test_wolfSSL_PEM_bio_RSAKey(void)
  28566. {
  28567. int res = TEST_SKIPPED;
  28568. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  28569. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  28570. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  28571. RSA* rsa = NULL;
  28572. BIO* bio = NULL;
  28573. /* PrivateKey */
  28574. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  28575. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  28576. AssertNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  28577. AssertNotNull(rsa);
  28578. AssertIntEQ(RSA_size(rsa), 256);
  28579. AssertIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  28580. NULL), WOLFSSL_FAILURE);
  28581. BIO_free(bio);
  28582. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28583. AssertIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  28584. NULL), WOLFSSL_SUCCESS);
  28585. BIO_free(bio);
  28586. RSA_free(rsa);
  28587. /* PUBKEY */
  28588. AssertNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  28589. AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  28590. AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  28591. AssertIntEQ(RSA_size(rsa), 256);
  28592. AssertIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  28593. BIO_free(bio);
  28594. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28595. AssertIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  28596. BIO_free(bio);
  28597. RSA_free(rsa);
  28598. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  28599. AssertNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  28600. AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  28601. BIO_free(bio);
  28602. RSA_free(rsa);
  28603. #ifdef HAVE_ECC
  28604. /* ensure that non-rsa keys do not work */
  28605. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  28606. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  28607. AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  28608. BIO_free(bio);
  28609. RSA_free(rsa);
  28610. #endif /* HAVE_ECC */
  28611. res = TEST_RES_CHECK(1);
  28612. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  28613. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  28614. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  28615. return res;
  28616. }
  28617. static int test_wolfSSL_PEM_bio_RSAPrivateKey(void)
  28618. {
  28619. int res = TEST_SKIPPED;
  28620. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28621. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  28622. RSA* rsa = NULL;
  28623. RSA* rsa_dup = NULL;
  28624. BIO* bio = NULL;
  28625. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  28626. AssertNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  28627. AssertIntEQ(RSA_size(rsa), 256);
  28628. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  28629. AssertNull(rsa_dup = RSAPublicKey_dup(NULL));
  28630. /* Test duplicating empty key. */
  28631. rsa_dup = RSA_new();
  28632. AssertNull(RSAPublicKey_dup(rsa_dup));
  28633. RSA_free(rsa_dup);
  28634. AssertNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  28635. AssertPtrNE(rsa_dup, rsa);
  28636. #endif
  28637. /* test if valgrind complains about unreleased memory */
  28638. RSA_up_ref(rsa);
  28639. RSA_free(rsa);
  28640. BIO_free(bio);
  28641. RSA_free(rsa);
  28642. RSA_free(rsa_dup);
  28643. #ifdef HAVE_ECC
  28644. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  28645. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  28646. BIO_free(bio);
  28647. #endif /* HAVE_ECC */
  28648. res = TEST_RES_CHECK(1);
  28649. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  28650. return res;
  28651. }
  28652. static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
  28653. {
  28654. int res = TEST_SKIPPED;
  28655. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28656. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  28657. XFILE file;
  28658. const char* fname = "./certs/client-keyPub.pem";
  28659. RSA *rsa;
  28660. AssertNull(wolfSSL_PEM_read_RSA_PUBKEY(XBADFILE, NULL, NULL, NULL));
  28661. file = XFOPEN(fname, "rb");
  28662. AssertTrue((file != XBADFILE));
  28663. AssertNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  28664. AssertIntEQ(RSA_size(rsa), 256);
  28665. RSA_free(rsa);
  28666. XFCLOSE(file);
  28667. res = TEST_RES_CHECK(1);
  28668. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  28669. return res;
  28670. }
  28671. static int test_wolfSSL_PEM_bio_DSAKey(void)
  28672. {
  28673. int res = TEST_SKIPPED;
  28674. #ifndef HAVE_SELFTEST
  28675. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  28676. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  28677. DSA* dsa = NULL;
  28678. BIO* bio = NULL;
  28679. /* PrivateKey */
  28680. AssertNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  28681. AssertNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  28682. AssertNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  28683. AssertIntEQ(BN_num_bytes(dsa->g), 128);
  28684. AssertIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, NULL),
  28685. WOLFSSL_FAILURE);
  28686. BIO_free(bio);
  28687. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28688. AssertIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL, NULL),
  28689. WOLFSSL_SUCCESS);
  28690. BIO_free(bio);
  28691. DSA_free(dsa);
  28692. /* PUBKEY */
  28693. AssertNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  28694. AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  28695. AssertNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  28696. AssertIntEQ(BN_num_bytes(dsa->g), 128);
  28697. AssertIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  28698. BIO_free(bio);
  28699. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28700. AssertIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  28701. BIO_free(bio);
  28702. DSA_free(dsa);
  28703. #ifdef HAVE_ECC
  28704. /* ensure that non-dsa keys do not work */
  28705. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  28706. AssertNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  28707. AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  28708. BIO_free(bio);
  28709. DSA_free(dsa);
  28710. #endif /* HAVE_ECC */
  28711. res = TEST_RES_CHECK(1);
  28712. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  28713. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  28714. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  28715. #endif /* HAVE_SELFTEST */
  28716. return res;
  28717. }
  28718. static int test_wolfSSL_PEM_bio_ECKey(void)
  28719. {
  28720. int res = TEST_SKIPPED;
  28721. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  28722. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  28723. EC_KEY* ec = NULL;
  28724. BIO* bio = NULL;
  28725. /* PrivateKey */
  28726. AssertNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  28727. AssertNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  28728. AssertNotNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  28729. AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  28730. AssertIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  28731. NULL),WOLFSSL_FAILURE);
  28732. BIO_free(bio);
  28733. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28734. AssertIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  28735. NULL), WOLFSSL_SUCCESS);
  28736. BIO_free(bio);
  28737. EC_KEY_free(ec);
  28738. /* PUBKEY */
  28739. AssertNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  28740. AssertNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  28741. AssertNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  28742. AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  28743. AssertIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  28744. BIO_free(bio);
  28745. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28746. AssertIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  28747. BIO_free(bio);
  28748. /* Same test as above, but with a file pointer rather than a BIO. */
  28749. AssertIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  28750. AssertIntEQ(PEM_write_EC_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  28751. AssertIntEQ(PEM_write_EC_PUBKEY(stderr, ec), WOLFSSL_SUCCESS);
  28752. EC_KEY_free(ec);
  28753. #ifndef NO_RSA
  28754. /* ensure that non-ec keys do not work */
  28755. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  28756. AssertNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  28757. AssertNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  28758. BIO_free(bio);
  28759. EC_KEY_free(ec);
  28760. #endif /* HAVE_ECC */
  28761. res = TEST_RES_CHECK(1);
  28762. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  28763. return res;
  28764. }
  28765. static int test_wolfSSL_PEM_PUBKEY(void)
  28766. {
  28767. int res = TEST_SKIPPED;
  28768. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  28769. BIO* bio = NULL;
  28770. EVP_PKEY* pkey = NULL;
  28771. /* test creating new EVP_PKEY with bad arg */
  28772. AssertNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  28773. /* test loading ECC key using BIO */
  28774. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  28775. {
  28776. XFILE file;
  28777. const char* fname = "./certs/ecc-client-keyPub.pem";
  28778. size_t sz;
  28779. byte* buf;
  28780. EVP_PKEY* pkey2;
  28781. EC_KEY* ec_key;
  28782. file = XFOPEN(fname, "rb");
  28783. AssertTrue((file != XBADFILE));
  28784. AssertIntGE(XFSEEK(file, 0, XSEEK_END), 0);
  28785. sz = XFTELL(file);
  28786. XREWIND(file);
  28787. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  28788. if (buf)
  28789. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  28790. XFCLOSE(file);
  28791. /* Test using BIO new mem and loading PEM private key */
  28792. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  28793. AssertNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  28794. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  28795. BIO_free(bio);
  28796. bio = NULL;
  28797. /* Qt unit test case*/
  28798. AssertNotNull(pkey2 = EVP_PKEY_new());
  28799. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  28800. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  28801. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  28802. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  28803. #else
  28804. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  28805. #endif
  28806. EC_KEY_free(ec_key);
  28807. EVP_PKEY_free(pkey2);
  28808. EVP_PKEY_free(pkey);
  28809. pkey = NULL;
  28810. }
  28811. #endif
  28812. (void)bio;
  28813. (void)pkey;
  28814. res = TEST_RES_CHECK(1);
  28815. #endif
  28816. return res;
  28817. }
  28818. #endif /* !NO_BIO */
  28819. static int test_DSA_do_sign_verify(void)
  28820. {
  28821. int res = TEST_SKIPPED;
  28822. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  28823. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  28824. !defined(NO_DSA)
  28825. unsigned char digest[WC_SHA_DIGEST_SIZE];
  28826. DSA_SIG* sig;
  28827. DSA* dsa;
  28828. word32 bytes;
  28829. byte sigBin[DSA_SIG_SIZE];
  28830. int dsacheck;
  28831. #ifdef USE_CERT_BUFFERS_1024
  28832. byte tmp[ONEK_BUF];
  28833. XMEMSET(tmp, 0, sizeof(tmp));
  28834. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  28835. bytes = sizeof_dsa_key_der_1024;
  28836. #elif defined(USE_CERT_BUFFERS_2048)
  28837. byte tmp[TWOK_BUF];
  28838. XMEMSET(tmp, 0, sizeof(tmp));
  28839. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  28840. bytes = sizeof_dsa_key_der_2048;
  28841. #else
  28842. byte tmp[TWOK_BUF];
  28843. XMEMSET(tmp, 0, sizeof(tmp));
  28844. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  28845. if (fp == XBADFILE) {
  28846. return WOLFSSL_BAD_FILE;
  28847. }
  28848. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  28849. XFCLOSE(fp);
  28850. #endif /* END USE_CERT_BUFFERS_1024 */
  28851. XMEMSET(digest, 202, sizeof(digest));
  28852. AssertNotNull(dsa = DSA_new());
  28853. AssertIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
  28854. AssertIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  28855. AssertIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  28856. AssertNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  28857. AssertIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  28858. DSA_SIG_free(sig);
  28859. DSA_free(dsa);
  28860. res = TEST_RES_CHECK(1);
  28861. #endif
  28862. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  28863. return res;
  28864. }
  28865. static int test_wolfSSL_tmp_dh(void)
  28866. {
  28867. int res = TEST_SKIPPED;
  28868. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  28869. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  28870. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  28871. byte buff[6000];
  28872. char file[] = "./certs/dsaparams.pem";
  28873. XFILE f;
  28874. int bytes;
  28875. DSA* dsa;
  28876. DH* dh;
  28877. #if defined(WOLFSSL_DH_EXTRA) && \
  28878. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  28879. DH* dh2;
  28880. #endif
  28881. BIO* bio;
  28882. SSL* ssl;
  28883. SSL_CTX* ctx;
  28884. #ifndef NO_WOLFSSL_SERVER
  28885. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28886. #else
  28887. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28888. #endif
  28889. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  28890. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  28891. AssertNotNull(ssl = SSL_new(ctx));
  28892. f = XFOPEN(file, "rb");
  28893. AssertTrue((f != XBADFILE));
  28894. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  28895. XFCLOSE(f);
  28896. bio = BIO_new_mem_buf((void*)buff, bytes);
  28897. AssertNotNull(bio);
  28898. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  28899. AssertNotNull(dsa);
  28900. dh = wolfSSL_DSA_dup_DH(dsa);
  28901. AssertNotNull(dh);
  28902. #if defined(WOLFSSL_DH_EXTRA) && \
  28903. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  28904. AssertNotNull(dh2 = wolfSSL_DH_dup(dh));
  28905. #endif
  28906. AssertIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  28907. #ifndef NO_WOLFSSL_SERVER
  28908. AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  28909. #else
  28910. AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  28911. #endif
  28912. BIO_free(bio);
  28913. DSA_free(dsa);
  28914. DH_free(dh);
  28915. #if defined(WOLFSSL_DH_EXTRA) && \
  28916. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  28917. DH_free(dh2);
  28918. #endif
  28919. SSL_free(ssl);
  28920. SSL_CTX_free(ctx);
  28921. res = TEST_RES_CHECK(1);
  28922. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  28923. #endif
  28924. return res;
  28925. }
  28926. static int test_wolfSSL_ctrl(void)
  28927. {
  28928. int res = TEST_SKIPPED;
  28929. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  28930. byte buff[6000];
  28931. BIO* bio;
  28932. int bytes;
  28933. BUF_MEM* ptr = NULL;
  28934. XMEMSET(buff, 0, sizeof(buff));
  28935. bytes = sizeof(buff);
  28936. bio = BIO_new_mem_buf((void*)buff, bytes);
  28937. AssertNotNull(bio);
  28938. AssertNotNull(BIO_s_socket());
  28939. AssertIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  28940. /* needs tested after stubs filled out @TODO
  28941. SSL_ctrl
  28942. SSL_CTX_ctrl
  28943. */
  28944. BIO_free(bio);
  28945. res = TEST_RES_CHECK(1);
  28946. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  28947. return res;
  28948. }
  28949. static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
  28950. {
  28951. int res = TEST_SKIPPED;
  28952. #ifdef OPENSSL_EXTRA
  28953. static const unsigned char pw[] = "password";
  28954. static const int pwSz = sizeof(pw) - 1;
  28955. size_t checkPwSz = 0;
  28956. const unsigned char* checkPw = NULL;
  28957. WOLFSSL_EVP_PKEY* key = NULL;
  28958. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  28959. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  28960. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, pwSz));
  28961. if (key) {
  28962. AssertIntEQ(key->type, EVP_PKEY_HMAC);
  28963. AssertIntEQ(key->save_type, EVP_PKEY_HMAC);
  28964. AssertIntEQ(key->pkey_sz, pwSz);
  28965. AssertIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  28966. }
  28967. AssertNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  28968. AssertIntEQ((int)checkPwSz, pwSz);
  28969. if (checkPw) {
  28970. AssertIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  28971. }
  28972. wolfSSL_EVP_PKEY_free(key);
  28973. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, 0));
  28974. if (key) {
  28975. AssertIntEQ(key->pkey_sz, 0);
  28976. }
  28977. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  28978. (void)checkPw;
  28979. AssertIntEQ((int)checkPwSz, 0);
  28980. wolfSSL_EVP_PKEY_free(key);
  28981. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL, 0));
  28982. if (key) {
  28983. AssertIntEQ(key->pkey_sz, 0);
  28984. }
  28985. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  28986. (void)checkPw;
  28987. AssertIntEQ((int)checkPwSz, 0);
  28988. wolfSSL_EVP_PKEY_free(key);
  28989. res = TEST_RES_CHECK(1);
  28990. #endif /* OPENSSL_EXTRA */
  28991. return res;
  28992. }
  28993. static int test_wolfSSL_EVP_PKEY_new_CMAC_key(void)
  28994. {
  28995. int res = TEST_SKIPPED;
  28996. #ifdef OPENSSL_EXTRA
  28997. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
  28998. const char *priv = "ABCDEFGHIJKLMNOP";
  28999. const WOLFSSL_EVP_CIPHER* cipher = EVP_aes_128_cbc();
  29000. WOLFSSL_EVP_PKEY* key = NULL;
  29001. AssertNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  29002. NULL, NULL, AES_128_KEY_SIZE, cipher));
  29003. AssertNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  29004. NULL, (const unsigned char *)priv, 0, cipher));
  29005. AssertNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  29006. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, NULL));
  29007. AssertNotNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  29008. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, cipher));
  29009. wolfSSL_EVP_PKEY_free(key);
  29010. res = TEST_RES_CHECK(1);
  29011. #endif /* defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT) */
  29012. #endif /* OPENSSL_EXTRA */
  29013. return res;
  29014. }
  29015. static int test_wolfSSL_EVP_Digest(void)
  29016. {
  29017. int res = TEST_SKIPPED;
  29018. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  29019. const char* in = "abc";
  29020. int inLen = (int)XSTRLEN(in);
  29021. byte out[WC_SHA256_DIGEST_SIZE];
  29022. unsigned int outLen;
  29023. const char* expOut = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  29024. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  29025. "\x15\xAD";
  29026. AssertIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen, "SHA256", NULL), 1);
  29027. AssertIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  29028. AssertIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  29029. res = TEST_RES_CHECK(1);
  29030. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  29031. return res;
  29032. }
  29033. static int test_wolfSSL_EVP_Digest_all(void)
  29034. {
  29035. int res = TEST_SKIPPED;
  29036. #ifdef OPENSSL_EXTRA
  29037. const char* digests[] = {
  29038. #ifndef NO_MD5
  29039. "MD5",
  29040. #endif
  29041. #ifndef NO_SHA
  29042. "SHA",
  29043. #endif
  29044. #ifdef WOLFSSL_SHA224
  29045. "SHA224",
  29046. #endif
  29047. #ifndef NO_SHA256
  29048. "SHA256",
  29049. #endif
  29050. #ifdef WOLFSSL_SHA384
  29051. "SHA384",
  29052. #endif
  29053. #ifdef WOLFSSL_SHA512
  29054. "SHA512",
  29055. #endif
  29056. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  29057. "SHA512_224",
  29058. #endif
  29059. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  29060. "SHA512_256",
  29061. #endif
  29062. #ifdef WOLFSSL_SHA3
  29063. #ifndef WOLFSSL_NOSHA3_224
  29064. "SHA3_224",
  29065. #endif
  29066. #ifndef WOLFSSL_NOSHA3_256
  29067. "SHA3_256",
  29068. #endif
  29069. "SHA3_384",
  29070. #ifndef WOLFSSL_NOSHA3_512
  29071. "SHA3_512",
  29072. #endif
  29073. #endif /* WOLFSSL_SHA3 */
  29074. NULL
  29075. };
  29076. const char** d;
  29077. const unsigned char in[] = "abc";
  29078. int inLen = XSTR_SIZEOF(in);
  29079. byte out[WC_MAX_DIGEST_SIZE];
  29080. unsigned int outLen;
  29081. for (d = digests; *d != NULL; d++) {
  29082. AssertIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  29083. AssertIntGT(outLen, 0);
  29084. AssertIntEQ(EVP_MD_size(*d), outLen);
  29085. }
  29086. res = TEST_RES_CHECK(1);
  29087. #endif
  29088. return res;
  29089. }
  29090. static int test_wolfSSL_EVP_MD_size(void)
  29091. {
  29092. int res = TEST_SKIPPED;
  29093. #ifdef OPENSSL_EXTRA
  29094. WOLFSSL_EVP_MD_CTX mdCtx;
  29095. #ifdef WOLFSSL_SHA3
  29096. #ifndef WOLFSSL_NOSHA3_224
  29097. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29098. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  29099. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  29100. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  29101. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29102. #endif
  29103. #ifndef WOLFSSL_NOSHA3_256
  29104. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29105. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  29106. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  29107. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  29108. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29109. #endif
  29110. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29111. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  29112. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  29113. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  29114. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29115. #ifndef WOLFSSL_NOSHA3_512
  29116. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29117. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  29118. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  29119. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  29120. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29121. #endif
  29122. #endif /* WOLFSSL_SHA3 */
  29123. #ifndef NO_SHA256
  29124. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29125. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  29126. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA256_DIGEST_SIZE);
  29127. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA256_BLOCK_SIZE);
  29128. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  29129. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  29130. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29131. #endif
  29132. #ifndef NO_MD5
  29133. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29134. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  29135. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_MD5_DIGEST_SIZE);
  29136. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_MD5_BLOCK_SIZE);
  29137. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  29138. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  29139. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29140. #endif
  29141. #ifdef WOLFSSL_SHA224
  29142. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29143. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  29144. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA224_DIGEST_SIZE);
  29145. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA224_BLOCK_SIZE);
  29146. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  29147. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  29148. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29149. #endif
  29150. #ifdef WOLFSSL_SHA384
  29151. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29152. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  29153. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA384_DIGEST_SIZE);
  29154. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA384_BLOCK_SIZE);
  29155. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  29156. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  29157. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29158. #endif
  29159. #ifdef WOLFSSL_SHA512
  29160. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29161. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  29162. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA512_DIGEST_SIZE);
  29163. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA512_BLOCK_SIZE);
  29164. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  29165. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  29166. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29167. #endif
  29168. #ifndef NO_SHA
  29169. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29170. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  29171. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_DIGEST_SIZE);
  29172. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_BLOCK_SIZE);
  29173. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  29174. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  29175. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29176. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29177. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  29178. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_DIGEST_SIZE);
  29179. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_BLOCK_SIZE);
  29180. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  29181. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  29182. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29183. #endif
  29184. /* error case */
  29185. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29186. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  29187. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), BAD_FUNC_ARG);
  29188. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  29189. /* Cleanup is valid on uninit'ed struct */
  29190. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29191. res = TEST_RES_CHECK(1);
  29192. #endif /* OPENSSL_EXTRA */
  29193. return res;
  29194. }
  29195. static int test_wolfSSL_EVP_MD_pkey_type(void)
  29196. {
  29197. int res = TEST_SKIPPED;
  29198. #ifdef OPENSSL_EXTRA
  29199. const WOLFSSL_EVP_MD* md;
  29200. #ifndef NO_MD5
  29201. AssertNotNull(md = EVP_md5());
  29202. AssertIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  29203. #endif
  29204. #ifndef NO_SHA
  29205. AssertNotNull(md = EVP_sha1());
  29206. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  29207. #endif
  29208. #ifdef WOLFSSL_SHA224
  29209. AssertNotNull(md = EVP_sha224());
  29210. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  29211. #endif
  29212. AssertNotNull(md = EVP_sha256());
  29213. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  29214. #ifdef WOLFSSL_SHA384
  29215. AssertNotNull(md = EVP_sha384());
  29216. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  29217. #endif
  29218. #ifdef WOLFSSL_SHA512
  29219. AssertNotNull(md = EVP_sha512());
  29220. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  29221. #endif
  29222. res = TEST_RES_CHECK(1);
  29223. #endif
  29224. return res;
  29225. }
  29226. #ifdef OPENSSL_EXTRA
  29227. static void test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  29228. size_t testKeySz, const char* testData, size_t testDataSz,
  29229. const byte* testResult, size_t testResultSz)
  29230. {
  29231. unsigned char check[WC_MAX_DIGEST_SIZE];
  29232. size_t checkSz = -1;
  29233. WOLFSSL_EVP_PKEY* key;
  29234. WOLFSSL_EVP_MD_CTX mdCtx;
  29235. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  29236. testKey, (int)testKeySz));
  29237. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29238. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  29239. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  29240. (unsigned int)testDataSz), 1);
  29241. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29242. AssertIntEQ((int)checkSz, (int)testResultSz);
  29243. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29244. AssertIntEQ((int)checkSz,(int)testResultSz);
  29245. AssertIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  29246. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29247. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  29248. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  29249. (unsigned int)testDataSz), 1);
  29250. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  29251. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29252. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29253. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  29254. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  29255. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29256. AssertIntEQ((int)checkSz, (int)testResultSz);
  29257. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29258. AssertIntEQ((int)checkSz,(int)testResultSz);
  29259. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  29260. (unsigned int)testDataSz - 4), 1);
  29261. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29262. AssertIntEQ((int)checkSz,(int)testResultSz);
  29263. AssertIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  29264. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29265. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  29266. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  29267. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  29268. (unsigned int)testDataSz - 4), 1);
  29269. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  29270. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29271. wolfSSL_EVP_PKEY_free(key);
  29272. }
  29273. #endif
  29274. static int test_wolfSSL_EVP_MD_hmac_signing(void)
  29275. {
  29276. int res = TEST_SKIPPED;
  29277. #ifdef OPENSSL_EXTRA
  29278. static const unsigned char testKey[] =
  29279. {
  29280. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  29281. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  29282. 0x0b, 0x0b, 0x0b, 0x0b
  29283. };
  29284. static const char testData[] = "Hi There";
  29285. #ifdef WOLFSSL_SHA224
  29286. static const unsigned char testResultSha224[] =
  29287. {
  29288. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  29289. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  29290. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  29291. 0x53, 0x68, 0x4b, 0x22
  29292. };
  29293. #endif
  29294. #ifndef NO_SHA256
  29295. static const unsigned char testResultSha256[] =
  29296. {
  29297. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  29298. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  29299. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  29300. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  29301. };
  29302. #endif
  29303. #ifdef WOLFSSL_SHA384
  29304. static const unsigned char testResultSha384[] =
  29305. {
  29306. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  29307. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  29308. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  29309. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  29310. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  29311. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  29312. };
  29313. #endif
  29314. #ifdef WOLFSSL_SHA512
  29315. static const unsigned char testResultSha512[] =
  29316. {
  29317. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  29318. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  29319. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  29320. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  29321. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  29322. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  29323. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  29324. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  29325. };
  29326. #endif
  29327. #ifdef WOLFSSL_SHA3
  29328. #ifndef WOLFSSL_NOSHA3_224
  29329. static const unsigned char testResultSha3_224[] =
  29330. {
  29331. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  29332. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  29333. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  29334. 0xf3, 0xc8, 0x60, 0xf7
  29335. };
  29336. #endif
  29337. #ifndef WOLFSSL_NOSHA3_256
  29338. static const unsigned char testResultSha3_256[] =
  29339. {
  29340. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  29341. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  29342. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  29343. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  29344. };
  29345. #endif
  29346. #ifndef WOLFSSL_NOSHA3_384
  29347. static const unsigned char testResultSha3_384[] =
  29348. {
  29349. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  29350. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  29351. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  29352. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  29353. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  29354. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  29355. };
  29356. #endif
  29357. #ifndef WOLFSSL_NOSHA3_512
  29358. static const unsigned char testResultSha3_512[] =
  29359. {
  29360. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  29361. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  29362. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  29363. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  29364. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  29365. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  29366. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  29367. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  29368. };
  29369. #endif
  29370. #endif
  29371. #ifndef NO_SHA256
  29372. test_hmac_signing(wolfSSL_EVP_sha256(), testKey, sizeof(testKey), testData,
  29373. XSTRLEN(testData), testResultSha256, sizeof(testResultSha256));
  29374. #endif
  29375. #ifdef WOLFSSL_SHA224
  29376. test_hmac_signing(wolfSSL_EVP_sha224(), testKey, sizeof(testKey), testData,
  29377. XSTRLEN(testData), testResultSha224, sizeof(testResultSha224));
  29378. #endif
  29379. #ifdef WOLFSSL_SHA384
  29380. test_hmac_signing(wolfSSL_EVP_sha384(), testKey, sizeof(testKey), testData,
  29381. XSTRLEN(testData), testResultSha384, sizeof(testResultSha384));
  29382. #endif
  29383. #ifdef WOLFSSL_SHA512
  29384. test_hmac_signing(wolfSSL_EVP_sha512(), testKey, sizeof(testKey), testData,
  29385. XSTRLEN(testData), testResultSha512, sizeof(testResultSha512));
  29386. #endif
  29387. #ifdef WOLFSSL_SHA3
  29388. #ifndef WOLFSSL_NOSHA3_224
  29389. test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey, sizeof(testKey),
  29390. testData, XSTRLEN(testData), testResultSha3_224,
  29391. sizeof(testResultSha3_224));
  29392. #endif
  29393. #ifndef WOLFSSL_NOSHA3_256
  29394. test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey, sizeof(testKey),
  29395. testData, XSTRLEN(testData), testResultSha3_256,
  29396. sizeof(testResultSha3_256));
  29397. #endif
  29398. #ifndef WOLFSSL_NOSHA3_384
  29399. test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey, sizeof(testKey),
  29400. testData, XSTRLEN(testData), testResultSha3_384,
  29401. sizeof(testResultSha3_384));
  29402. #endif
  29403. #ifndef WOLFSSL_NOSHA3_512
  29404. test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey, sizeof(testKey),
  29405. testData, XSTRLEN(testData), testResultSha3_512,
  29406. sizeof(testResultSha3_512));
  29407. #endif
  29408. #endif
  29409. res = TEST_RES_CHECK(1);
  29410. #endif /* OPENSSL_EXTRA */
  29411. return res;
  29412. }
  29413. static int test_wolfSSL_EVP_MD_rsa_signing(void)
  29414. {
  29415. int res = TEST_SKIPPED;
  29416. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  29417. defined(USE_CERT_BUFFERS_2048)
  29418. WOLFSSL_EVP_PKEY* privKey;
  29419. WOLFSSL_EVP_PKEY* pubKey;
  29420. WOLFSSL_EVP_PKEY_CTX* keyCtx;
  29421. const char testData[] = "Hi There";
  29422. WOLFSSL_EVP_MD_CTX mdCtx;
  29423. WOLFSSL_EVP_MD_CTX mdCtxCopy;
  29424. size_t checkSz = -1;
  29425. int sz = 2048 / 8;
  29426. const unsigned char* cp;
  29427. const unsigned char* p;
  29428. unsigned char check[2048/8];
  29429. size_t i;
  29430. int paddings[] = {
  29431. RSA_PKCS1_PADDING,
  29432. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  29433. RSA_PKCS1_PSS_PADDING,
  29434. #endif
  29435. };
  29436. cp = client_key_der_2048;
  29437. AssertNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  29438. sizeof_client_key_der_2048)));
  29439. p = client_keypub_der_2048;
  29440. AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  29441. sizeof_client_keypub_der_2048)));
  29442. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29443. wolfSSL_EVP_MD_CTX_init(&mdCtxCopy);
  29444. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29445. NULL, privKey), 1);
  29446. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  29447. (unsigned int)XSTRLEN(testData)), 1);
  29448. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29449. AssertIntEQ((int)checkSz, sz);
  29450. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29451. AssertIntEQ((int)checkSz,sz);
  29452. AssertIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  29453. AssertIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  29454. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtxCopy), 1);
  29455. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29456. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29457. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29458. NULL, pubKey), 1);
  29459. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  29460. (unsigned int)XSTRLEN(testData)),
  29461. 1);
  29462. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  29463. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29464. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29465. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29466. NULL, privKey), 1);
  29467. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  29468. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29469. AssertIntEQ((int)checkSz, sz);
  29470. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29471. AssertIntEQ((int)checkSz, sz);
  29472. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  29473. (unsigned int)XSTRLEN(testData) - 4), 1);
  29474. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29475. AssertIntEQ((int)checkSz, sz);
  29476. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29477. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29478. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29479. NULL, pubKey), 1);
  29480. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  29481. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  29482. (unsigned int)XSTRLEN(testData) - 4),
  29483. 1);
  29484. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  29485. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29486. /* Check all signing padding types */
  29487. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  29488. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29489. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  29490. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  29491. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  29492. paddings[i]), 1);
  29493. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  29494. (unsigned int)XSTRLEN(testData)), 1);
  29495. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29496. AssertIntEQ((int)checkSz, sz);
  29497. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29498. AssertIntEQ((int)checkSz,sz);
  29499. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29500. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29501. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  29502. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  29503. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  29504. paddings[i]), 1);
  29505. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  29506. (unsigned int)XSTRLEN(testData)), 1);
  29507. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  29508. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29509. }
  29510. wolfSSL_EVP_PKEY_free(pubKey);
  29511. wolfSSL_EVP_PKEY_free(privKey);
  29512. res = TEST_RES_CHECK(1);
  29513. #endif
  29514. return res;
  29515. }
  29516. static int test_wolfSSL_EVP_MD_ecc_signing(void)
  29517. {
  29518. int res = TEST_SKIPPED;
  29519. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  29520. WOLFSSL_EVP_PKEY* privKey;
  29521. WOLFSSL_EVP_PKEY* pubKey;
  29522. const char testData[] = "Hi There";
  29523. WOLFSSL_EVP_MD_CTX mdCtx;
  29524. size_t checkSz = -1;
  29525. const unsigned char* cp;
  29526. const unsigned char* p;
  29527. unsigned char check[2048/8];
  29528. cp = ecc_clikey_der_256;
  29529. privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  29530. sizeof_ecc_clikey_der_256);
  29531. AssertNotNull(privKey);
  29532. p = ecc_clikeypub_der_256;
  29533. AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  29534. sizeof_ecc_clikeypub_der_256)));
  29535. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29536. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29537. NULL, privKey), 1);
  29538. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  29539. (unsigned int)XSTRLEN(testData)), 1);
  29540. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29541. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29542. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29543. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29544. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29545. NULL, pubKey), 1);
  29546. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  29547. (unsigned int)XSTRLEN(testData)),
  29548. 1);
  29549. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  29550. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29551. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29552. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29553. NULL, privKey), 1);
  29554. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  29555. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29556. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29557. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  29558. (unsigned int)XSTRLEN(testData) - 4), 1);
  29559. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29560. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29561. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29562. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29563. NULL, pubKey), 1);
  29564. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  29565. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  29566. (unsigned int)XSTRLEN(testData) - 4),
  29567. 1);
  29568. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  29569. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29570. wolfSSL_EVP_PKEY_free(pubKey);
  29571. wolfSSL_EVP_PKEY_free(privKey);
  29572. res = TEST_RES_CHECK(1);
  29573. #endif
  29574. return res;
  29575. }
  29576. static int test_wolfSSL_CTX_add_extra_chain_cert(void)
  29577. {
  29578. int res = TEST_SKIPPED;
  29579. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29580. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  29581. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  29582. char caFile[] = "./certs/client-ca.pem";
  29583. char clientFile[] = "./certs/client-cert.pem";
  29584. SSL_CTX* ctx;
  29585. X509* x509;
  29586. BIO *bio = NULL;
  29587. X509 *cert = NULL;
  29588. X509 *ca;
  29589. STACK_OF(X509) *chain = NULL;
  29590. STACK_OF(X509) *chain2 = NULL;
  29591. #ifndef NO_WOLFSSL_SERVER
  29592. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29593. #else
  29594. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29595. #endif
  29596. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  29597. AssertNotNull(x509);
  29598. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  29599. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  29600. AssertNotNull(x509);
  29601. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  29602. /* additional test of getting EVP_PKEY key size from X509
  29603. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  29604. * allowed with user RSA */
  29605. {
  29606. EVP_PKEY* pkey;
  29607. #if defined(HAVE_ECC)
  29608. X509* ecX509;
  29609. #endif /* HAVE_ECC */
  29610. AssertNotNull(pkey = X509_get_pubkey(x509));
  29611. /* current RSA key is 2048 bit (256 bytes) */
  29612. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  29613. EVP_PKEY_free(pkey);
  29614. #if defined(HAVE_ECC)
  29615. #if defined(USE_CERT_BUFFERS_256)
  29616. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  29617. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  29618. SSL_FILETYPE_ASN1));
  29619. #else
  29620. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(cliEccCertFile,
  29621. SSL_FILETYPE_PEM));
  29622. #endif
  29623. pkey = X509_get_pubkey(ecX509);
  29624. AssertNotNull(pkey);
  29625. /* current ECC key is 256 bit (32 bytes) */
  29626. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  29627. X509_free(ecX509);
  29628. EVP_PKEY_free(pkey);
  29629. #endif /* HAVE_ECC */
  29630. }
  29631. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  29632. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  29633. #ifdef WOLFSSL_ENCRYPTED_KEYS
  29634. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  29635. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  29636. #endif
  29637. SSL_CTX_free(ctx);
  29638. #ifndef NO_WOLFSSL_SERVER
  29639. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29640. #else
  29641. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29642. #endif
  29643. /* Test haproxy use case */
  29644. AssertNotNull(bio = BIO_new_file(svrCertFile, "r"));
  29645. /* Read Certificate */
  29646. AssertNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  29647. AssertNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  29648. AssertNotNull(chain = sk_X509_new_null());
  29649. AssertIntEQ(sk_X509_push(chain, ca), 1);
  29650. AssertNotNull(chain2 = X509_chain_up_ref(chain));
  29651. AssertNotNull(ca = sk_X509_shift(chain2));
  29652. AssertIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  29653. AssertIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  29654. BIO_free(bio);
  29655. X509_free(cert);
  29656. sk_X509_pop_free(chain, X509_free);
  29657. sk_X509_pop_free(chain2, X509_free);
  29658. SSL_CTX_free(ctx);
  29659. res = TEST_RES_CHECK(1);
  29660. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  29661. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29662. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  29663. return res;
  29664. }
  29665. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  29666. static int test_wolfSSL_ERR_peek_last_error_line(void)
  29667. {
  29668. int res = TEST_SKIPPED;
  29669. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29670. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  29671. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  29672. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  29673. tcp_ready ready;
  29674. func_args client_args;
  29675. func_args server_args;
  29676. #ifndef SINGLE_THREADED
  29677. THREAD_TYPE serverThread;
  29678. #endif
  29679. callback_functions client_cb;
  29680. callback_functions server_cb;
  29681. int line = 0;
  29682. int flag = ERR_TXT_STRING;
  29683. const char* file = NULL;
  29684. const char* data = NULL;
  29685. /* create a failed connection and inspect the error */
  29686. #ifdef WOLFSSL_TIRTOS
  29687. fdOpenSession(Task_self());
  29688. #endif
  29689. XMEMSET(&client_args, 0, sizeof(func_args));
  29690. XMEMSET(&server_args, 0, sizeof(func_args));
  29691. StartTCP();
  29692. InitTcpReady(&ready);
  29693. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  29694. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  29695. client_cb.method = wolfTLSv1_1_client_method;
  29696. server_cb.method = wolfTLSv1_2_server_method;
  29697. server_args.signal = &ready;
  29698. server_args.callbacks = &server_cb;
  29699. client_args.signal = &ready;
  29700. client_args.callbacks = &client_cb;
  29701. #ifndef SINGLE_THREADED
  29702. start_thread(test_server_nofail, &server_args, &serverThread);
  29703. wait_tcp_ready(&server_args);
  29704. test_client_nofail(&client_args, NULL);
  29705. join_thread(serverThread);
  29706. #endif
  29707. FreeTcpReady(&ready);
  29708. AssertIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  29709. AssertNotNull(data);
  29710. /* check clearing error state */
  29711. ERR_remove_state(0);
  29712. AssertIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  29713. ERR_peek_last_error_line(NULL, &line);
  29714. AssertIntEQ(line, 0);
  29715. ERR_peek_last_error_line(&file, NULL);
  29716. AssertNull(file);
  29717. /* retry connection to fill error queue */
  29718. XMEMSET(&client_args, 0, sizeof(func_args));
  29719. XMEMSET(&server_args, 0, sizeof(func_args));
  29720. StartTCP();
  29721. InitTcpReady(&ready);
  29722. client_cb.method = wolfTLSv1_1_client_method;
  29723. server_cb.method = wolfTLSv1_2_server_method;
  29724. server_args.signal = &ready;
  29725. server_args.callbacks = &server_cb;
  29726. client_args.signal = &ready;
  29727. client_args.callbacks = &client_cb;
  29728. start_thread(test_server_nofail, &server_args, &serverThread);
  29729. wait_tcp_ready(&server_args);
  29730. test_client_nofail(&client_args, NULL);
  29731. join_thread(serverThread);
  29732. FreeTcpReady(&ready);
  29733. /* check that error code was stored */
  29734. AssertIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  29735. ERR_peek_last_error_line(NULL, &line);
  29736. AssertIntNE(line, 0);
  29737. ERR_peek_last_error_line(&file, NULL);
  29738. AssertNotNull(file);
  29739. #ifdef WOLFSSL_TIRTOS
  29740. fdOpenSession(Task_self());
  29741. #endif
  29742. fprintf(stderr, "\nTesting error print out\n");
  29743. ERR_print_errors_fp(stderr);
  29744. fprintf(stderr, "Done testing print out\n\n");
  29745. res = TEST_RES_CHECK(1);
  29746. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29747. !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  29748. return res;
  29749. }
  29750. #endif
  29751. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29752. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29753. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  29754. {
  29755. (void) ok;
  29756. (void) ctx;
  29757. fprintf(stderr, "ENTER verify_cb\n");
  29758. return SSL_SUCCESS;
  29759. }
  29760. #endif
  29761. static int test_wolfSSL_X509_Name_canon(void)
  29762. {
  29763. int res = TEST_SKIPPED;
  29764. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  29765. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  29766. defined(WOLFSSL_CERT_GEN) && \
  29767. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  29768. const long ex_hash1 = 0x0fdb2da4;
  29769. const long ex_hash2 = 0x9f3e8c9e;
  29770. X509_NAME *name = NULL;
  29771. X509 *x509 = NULL;
  29772. FILE* file = NULL;
  29773. unsigned long hash = 0;
  29774. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  29775. byte *pbuf = NULL;
  29776. word32 len = 0;
  29777. (void) ex_hash2;
  29778. file = XFOPEN(caCertFile, "rb");
  29779. AssertNotNull(file);
  29780. AssertNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  29781. AssertNotNull(name = X509_get_issuer_name(x509));
  29782. /* When output buffer is NULL, should return necessary output buffer
  29783. * length.*/
  29784. AssertIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  29785. AssertIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  29786. AssertIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  29787. hash = (((unsigned long)digest[3] << 24) |
  29788. ((unsigned long)digest[2] << 16) |
  29789. ((unsigned long)digest[1] << 8) |
  29790. ((unsigned long)digest[0]));
  29791. AssertIntEQ(hash, ex_hash1);
  29792. XFCLOSE(file);
  29793. X509_free(x509);
  29794. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  29795. pbuf = NULL;
  29796. file = XFOPEN(cliCertFile, "rb");
  29797. AssertNotNull(file);
  29798. AssertNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  29799. AssertNotNull(name = X509_get_issuer_name(x509));
  29800. AssertIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  29801. AssertIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  29802. hash = (((unsigned long)digest[3] << 24) |
  29803. ((unsigned long)digest[2] << 16) |
  29804. ((unsigned long)digest[1] << 8) |
  29805. ((unsigned long)digest[0]));
  29806. AssertIntEQ(hash, ex_hash2);
  29807. XFCLOSE(file);
  29808. X509_free(x509);
  29809. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  29810. res = TEST_RES_CHECK(1);
  29811. #endif
  29812. return res;
  29813. }
  29814. static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  29815. {
  29816. int res = TEST_SKIPPED;
  29817. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  29818. const int MAX_DIR = 4;
  29819. const char paths[][32] = {
  29820. "./certs/ed25519",
  29821. "./certs/ecc",
  29822. "./certs/crl",
  29823. "./certs/",
  29824. };
  29825. char CertCrl_path[MAX_FILENAME_SZ];
  29826. char *p;
  29827. X509_STORE* str;
  29828. X509_LOOKUP* lookup;
  29829. WOLFSSL_STACK* sk = NULL;
  29830. int len, total_len, i;
  29831. (void) sk;
  29832. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  29833. /* illegal string */
  29834. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  29835. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29836. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  29837. SSL_FILETYPE_PEM,NULL), 0);
  29838. /* free store */
  29839. X509_STORE_free(str);
  29840. /* short folder string */
  29841. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  29842. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29843. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  29844. SSL_FILETYPE_PEM,NULL), 1);
  29845. #if defined(WOLFSSL_INT_H)
  29846. /* only available when including internal.h */
  29847. AssertNotNull(sk = lookup->dirs->dir_entry);
  29848. #endif
  29849. /* free store */
  29850. X509_STORE_free(str);
  29851. /* typical function check */
  29852. p = &CertCrl_path[0];
  29853. total_len = 0;
  29854. for (i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  29855. len = (int)XSTRLEN((const char*)&paths[i]);
  29856. total_len += len;
  29857. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  29858. p += len;
  29859. if (i != 0) *(p++) = SEPARATOR_CHAR;
  29860. }
  29861. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  29862. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29863. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  29864. SSL_FILETYPE_PEM,NULL), 1);
  29865. #if defined(WOLFSSL_INT_H)
  29866. /* only available when including internal.h */
  29867. AssertNotNull(sk = lookup->dirs->dir_entry);
  29868. #endif
  29869. X509_STORE_free(str);
  29870. res = TEST_RES_CHECK(1);
  29871. #endif
  29872. return res;
  29873. }
  29874. static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  29875. {
  29876. int res = TEST_SKIPPED;
  29877. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  29878. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  29879. defined(WOLFSSL_SIGNER_DER_CERT)
  29880. X509_STORE_CTX* ctx;
  29881. X509_STORE* str;
  29882. X509_LOOKUP* lookup;
  29883. X509* cert1;
  29884. X509* x509Ca;
  29885. X509* x509Svr;
  29886. X509* issuer;
  29887. WOLFSSL_STACK* sk = NULL;
  29888. X509_NAME* caName;
  29889. X509_NAME* issuerName;
  29890. FILE* file1 = NULL;
  29891. int i, cert_count, cmp;
  29892. char der[] = "certs/ca-cert.der";
  29893. #ifdef HAVE_CRL
  29894. char pem[][100] = {
  29895. "./certs/crl/crl.pem",
  29896. "./certs/crl/crl2.pem",
  29897. "./certs/crl/caEccCrl.pem",
  29898. "./certs/crl/eccCliCRL.pem",
  29899. "./certs/crl/eccSrvCRL.pem",
  29900. ""
  29901. };
  29902. #endif
  29903. AssertNotNull(file1=fopen("./certs/ca-cert.pem", "rb"));
  29904. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  29905. fclose(file1);
  29906. AssertNotNull(ctx = X509_STORE_CTX_new());
  29907. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  29908. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29909. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  29910. SSL_FILETYPE_PEM,NULL), 1);
  29911. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  29912. AssertIntEQ((cert_count = sk_X509_num(sk)), 1);
  29913. /* check if CA cert is loaded into the store */
  29914. for (i = 0; i < cert_count; i++) {
  29915. x509Ca = sk_X509_value(sk, i);
  29916. AssertIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  29917. }
  29918. AssertNotNull((x509Svr =
  29919. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  29920. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  29921. AssertNull(X509_STORE_CTX_get0_current_issuer(NULL));
  29922. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  29923. AssertNotNull(issuer);
  29924. caName = X509_get_subject_name(x509Ca);
  29925. AssertNotNull(caName);
  29926. issuerName = X509_get_subject_name(issuer);
  29927. AssertNotNull(issuerName);
  29928. cmp = X509_NAME_cmp(caName, issuerName);
  29929. AssertIntEQ(cmp, 0);
  29930. /* load der format */
  29931. X509_free(issuer);
  29932. X509_STORE_CTX_free(ctx);
  29933. X509_STORE_free(str);
  29934. sk_X509_pop_free(sk, NULL);
  29935. X509_free(x509Svr);
  29936. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  29937. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29938. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  29939. SSL_FILETYPE_ASN1,NULL), 1);
  29940. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  29941. AssertIntEQ((cert_count = sk_X509_num(sk)), 1);
  29942. /* check if CA cert is loaded into the store */
  29943. for (i = 0; i < cert_count; i++) {
  29944. x509Ca = sk_X509_value(sk, i);
  29945. AssertIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  29946. }
  29947. X509_STORE_free(str);
  29948. sk_X509_pop_free(sk, NULL);
  29949. X509_free(cert1);
  29950. #ifdef HAVE_CRL
  29951. AssertNotNull(str = wolfSSL_X509_STORE_new());
  29952. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29953. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  29954. SSL_FILETYPE_PEM,NULL), 1);
  29955. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  29956. "certs/server-revoked-cert.pem",
  29957. SSL_FILETYPE_PEM,NULL), 1);
  29958. if (str) {
  29959. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  29960. WOLFSSL_FILETYPE_PEM), 1);
  29961. /* since store hasn't yet known the revoked cert*/
  29962. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm,
  29963. "certs/server-revoked-cert.pem",
  29964. WOLFSSL_FILETYPE_PEM), 1);
  29965. }
  29966. for (i = 0; pem[i][0] != '\0'; i++)
  29967. {
  29968. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  29969. SSL_FILETYPE_PEM, NULL), 1);
  29970. }
  29971. if (str) {
  29972. /* since store knows crl list */
  29973. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm,
  29974. "certs/server-revoked-cert.pem",
  29975. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  29976. }
  29977. AssertIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  29978. X509_STORE_free(str);
  29979. #endif
  29980. res = TEST_RES_CHECK(1);
  29981. #endif
  29982. return res;
  29983. }
  29984. static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  29985. {
  29986. int res = TEST_SKIPPED;
  29987. #if defined(OPENSSL_EXTRA)
  29988. X509_STORE_CTX_cleanup(NULL);
  29989. X509_STORE_CTX_trusted_stack(NULL, NULL);
  29990. AssertTrue(1); /* to confirm previous call gives no harm */
  29991. res = TEST_RES_CHECK(1);
  29992. #endif
  29993. return res;
  29994. }
  29995. static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  29996. {
  29997. int res = TEST_SKIPPED;
  29998. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  29999. #ifdef WOLFSSL_SIGNER_DER_CERT
  30000. int cmp;
  30001. #endif
  30002. X509_STORE_CTX* ctx;
  30003. X509_STORE* str;
  30004. X509* x509Ca;
  30005. X509* x509Svr;
  30006. X509* issuer;
  30007. X509_NAME* caName;
  30008. X509_NAME* issuerName;
  30009. AssertNotNull(ctx = X509_STORE_CTX_new());
  30010. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  30011. AssertNotNull((x509Ca =
  30012. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  30013. AssertIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  30014. AssertNotNull((x509Svr =
  30015. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  30016. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  30017. AssertNull(X509_STORE_CTX_get0_current_issuer(NULL));
  30018. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  30019. AssertNotNull(issuer);
  30020. caName = X509_get_subject_name(x509Ca);
  30021. AssertNotNull(caName);
  30022. issuerName = X509_get_subject_name(issuer);
  30023. AssertNotNull(issuerName);
  30024. #ifdef WOLFSSL_SIGNER_DER_CERT
  30025. cmp = X509_NAME_cmp(caName, issuerName);
  30026. AssertIntEQ(cmp, 0);
  30027. #endif
  30028. X509_free(issuer);
  30029. X509_STORE_CTX_free(ctx);
  30030. X509_free(x509Svr);
  30031. X509_STORE_free(str);
  30032. X509_free(x509Ca);
  30033. res = TEST_RES_CHECK(1);
  30034. #endif
  30035. return res;
  30036. }
  30037. static int test_wolfSSL_PKCS7_certs(void)
  30038. {
  30039. int res = TEST_SKIPPED;
  30040. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  30041. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  30042. STACK_OF(X509)* sk = NULL;
  30043. STACK_OF(X509_INFO)* info_sk = NULL;
  30044. PKCS7 *p7 = NULL;
  30045. BIO* bio;
  30046. const byte* p = NULL;
  30047. int buflen = 0;
  30048. int i;
  30049. /* Test twice. Once with d2i and once without to test
  30050. * that everything is free'd correctly. */
  30051. for (i = 0; i < 2; i++) {
  30052. AssertNotNull(p7 = PKCS7_new());
  30053. p7->version = 1;
  30054. p7->hashOID = SHAh;
  30055. AssertNotNull(bio = BIO_new(BIO_s_file()));
  30056. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  30057. AssertNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  30058. AssertIntEQ(sk_X509_INFO_num(info_sk), 2);
  30059. AssertNotNull(sk = sk_X509_new_null());
  30060. while (sk_X509_INFO_num(info_sk)) {
  30061. X509_INFO* info;
  30062. AssertNotNull(info = sk_X509_INFO_shift(info_sk));
  30063. AssertIntEQ(sk_X509_push(sk, info->x509), 1);
  30064. info->x509 = NULL;
  30065. X509_INFO_free(info);
  30066. }
  30067. sk_X509_INFO_free(info_sk);
  30068. BIO_free(bio);
  30069. bio = BIO_new(BIO_s_mem());
  30070. AssertIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  30071. AssertIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  30072. if (i == 0) {
  30073. PKCS7_free(p7);
  30074. AssertNotNull(d2i_PKCS7(&p7, &p, buflen));
  30075. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate them */
  30076. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  30077. /* PKCS7_free free's the certs */
  30078. AssertNotNull(wolfSSL_PKCS7_to_stack(p7));
  30079. }
  30080. BIO_free(bio);
  30081. PKCS7_free(p7);
  30082. }
  30083. res = TEST_RES_CHECK(1);
  30084. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  30085. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  30086. return res;
  30087. }
  30088. static int test_wolfSSL_X509_STORE_CTX(void)
  30089. {
  30090. int res = TEST_SKIPPED;
  30091. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30092. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30093. X509_STORE_CTX* ctx;
  30094. X509_STORE* str;
  30095. X509* x509;
  30096. #ifdef OPENSSL_ALL
  30097. X509* x5092;
  30098. STACK_OF(X509) *sk, *sk2, *sk3;
  30099. #endif
  30100. AssertNotNull(ctx = X509_STORE_CTX_new());
  30101. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  30102. AssertNotNull((x509 =
  30103. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  30104. AssertIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  30105. #ifdef OPENSSL_ALL
  30106. /* sk_X509_new only in OPENSSL_ALL */
  30107. sk = sk_X509_new_null();
  30108. AssertNotNull(sk);
  30109. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  30110. #else
  30111. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  30112. #endif
  30113. AssertIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  30114. X509_STORE_CTX_set_error(ctx, -5);
  30115. X509_STORE_CTX_set_error(NULL, -5);
  30116. X509_STORE_CTX_free(ctx);
  30117. #ifdef OPENSSL_ALL
  30118. sk_X509_pop_free(sk, NULL);
  30119. #endif
  30120. X509_STORE_free(str);
  30121. X509_free(x509);
  30122. AssertNotNull(ctx = X509_STORE_CTX_new());
  30123. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  30124. X509_STORE_CTX_free(ctx);
  30125. #ifdef OPENSSL_ALL
  30126. /* test X509_STORE_CTX_get(1)_chain */
  30127. AssertNotNull((x509 = X509_load_certificate_file(svrCertFile,
  30128. SSL_FILETYPE_PEM)));
  30129. AssertNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  30130. SSL_FILETYPE_PEM)));
  30131. AssertNotNull((sk = sk_X509_new_null()));
  30132. AssertIntEQ(sk_X509_push(sk, x509), 1);
  30133. AssertNotNull((str = X509_STORE_new()));
  30134. AssertNotNull((ctx = X509_STORE_CTX_new()));
  30135. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  30136. AssertNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  30137. AssertNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  30138. AssertIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  30139. AssertNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  30140. AssertNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  30141. AssertIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  30142. X509_STORE_CTX_free(ctx);
  30143. X509_STORE_free(str);
  30144. /* CTX certs not freed yet */
  30145. X509_free(x5092);
  30146. sk_X509_pop_free(sk, NULL);
  30147. /* sk3 is dup so free here */
  30148. sk_X509_pop_free(sk3, NULL);
  30149. #endif
  30150. /* test X509_STORE_CTX_get/set_ex_data */
  30151. {
  30152. int i = 0, tmpData = 5;
  30153. void* tmpDataRet;
  30154. AssertNotNull(ctx = X509_STORE_CTX_new());
  30155. #ifdef HAVE_EX_DATA
  30156. for (i = 0; i < MAX_EX_DATA; i++) {
  30157. AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  30158. WOLFSSL_SUCCESS);
  30159. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  30160. AssertNotNull(tmpDataRet);
  30161. AssertIntEQ(tmpData, *(int*)tmpDataRet);
  30162. }
  30163. #else
  30164. AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  30165. WOLFSSL_FAILURE);
  30166. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  30167. AssertNull(tmpDataRet);
  30168. #endif
  30169. X509_STORE_CTX_free(ctx);
  30170. }
  30171. /* test X509_STORE_get/set_ex_data */
  30172. {
  30173. int i = 0, tmpData = 99;
  30174. void* tmpDataRet;
  30175. AssertNotNull(str = X509_STORE_new());
  30176. #ifdef HAVE_EX_DATA
  30177. for (i = 0; i < MAX_EX_DATA; i++) {
  30178. AssertIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  30179. WOLFSSL_SUCCESS);
  30180. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  30181. AssertNotNull(tmpDataRet);
  30182. AssertIntEQ(tmpData, *(int*)tmpDataRet);
  30183. }
  30184. #else
  30185. AssertIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  30186. WOLFSSL_FAILURE);
  30187. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  30188. AssertNull(tmpDataRet);
  30189. #endif
  30190. X509_STORE_free(str);
  30191. }
  30192. res = TEST_RES_CHECK(1);
  30193. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30194. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  30195. return res;
  30196. }
  30197. static int test_wolfSSL_X509_STORE_set_flags(void)
  30198. {
  30199. int res = TEST_SKIPPED;
  30200. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30201. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30202. X509_STORE* store;
  30203. X509* x509;
  30204. AssertNotNull((store = wolfSSL_X509_STORE_new()));
  30205. AssertNotNull((x509 =
  30206. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM)));
  30207. AssertIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  30208. #ifdef HAVE_CRL
  30209. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL), WOLFSSL_SUCCESS);
  30210. #else
  30211. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  30212. NOT_COMPILED_IN);
  30213. #endif
  30214. wolfSSL_X509_free(x509);
  30215. wolfSSL_X509_STORE_free(store);
  30216. res = TEST_RES_CHECK(1);
  30217. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30218. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  30219. return res;
  30220. }
  30221. static int test_wolfSSL_X509_LOOKUP_load_file(void)
  30222. {
  30223. int res = TEST_SKIPPED;
  30224. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  30225. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  30226. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  30227. WOLFSSL_X509_STORE* store;
  30228. WOLFSSL_X509_LOOKUP* lookup;
  30229. AssertNotNull(store = wolfSSL_X509_STORE_new());
  30230. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  30231. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  30232. X509_FILETYPE_PEM), 1);
  30233. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  30234. X509_FILETYPE_PEM), 1);
  30235. if (store) {
  30236. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  30237. WOLFSSL_FILETYPE_PEM), 1);
  30238. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  30239. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  30240. }
  30241. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  30242. X509_FILETYPE_PEM), 1);
  30243. if (store) {
  30244. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  30245. WOLFSSL_FILETYPE_PEM), 1);
  30246. }
  30247. wolfSSL_X509_STORE_free(store);
  30248. res = TEST_RES_CHECK(1);
  30249. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  30250. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  30251. return res;
  30252. }
  30253. static int test_wolfSSL_X509_STORE_CTX_set_time(void)
  30254. {
  30255. int res = TEST_SKIPPED;
  30256. #if defined(OPENSSL_EXTRA)
  30257. WOLFSSL_X509_STORE_CTX* ctx;
  30258. time_t c_time;
  30259. AssertNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  30260. c_time = 365*24*60*60;
  30261. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  30262. AssertTrue(
  30263. (ctx->param->flags & WOLFSSL_USE_CHECK_TIME) == WOLFSSL_USE_CHECK_TIME);
  30264. AssertTrue(ctx->param->check_time == c_time);
  30265. wolfSSL_X509_STORE_CTX_free(ctx);
  30266. res = TEST_RES_CHECK(1);
  30267. #endif /* OPENSSL_EXTRA */
  30268. return res;
  30269. }
  30270. static int test_wolfSSL_CTX_get0_set1_param(void)
  30271. {
  30272. int res = TEST_SKIPPED;
  30273. #if defined(OPENSSL_EXTRA)
  30274. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30275. int ret;
  30276. SSL_CTX* ctx;
  30277. WOLFSSL_X509_VERIFY_PARAM* pParam;
  30278. WOLFSSL_X509_VERIFY_PARAM* pvpm;
  30279. char testIPv4[] = "127.0.0.1";
  30280. char testhostName[] = "foo.hoge.com";
  30281. #ifndef NO_WOLFSSL_SERVER
  30282. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30283. #else
  30284. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30285. #endif
  30286. AssertNull(SSL_CTX_get0_param(NULL));
  30287. AssertNotNull(pParam = SSL_CTX_get0_param(ctx));
  30288. pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  30289. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL);
  30290. AssertNotNull(pvpm);
  30291. XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  30292. wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  30293. (int)XSTRLEN(testhostName));
  30294. wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4);
  30295. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  30296. ret = SSL_CTX_set1_param(ctx, pvpm);
  30297. AssertIntEQ(1, ret);
  30298. AssertIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  30299. (int)XSTRLEN(testhostName)));
  30300. AssertIntEQ(0x01, pParam->hostFlags);
  30301. AssertIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  30302. /* test for incorrect patameter */
  30303. AssertIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  30304. AssertIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  30305. AssertIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  30306. SSL_CTX_free(ctx);
  30307. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  30308. res = TEST_RES_CHECK(1);
  30309. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30310. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  30311. return res;
  30312. }
  30313. static int test_wolfSSL_get0_param(void)
  30314. {
  30315. int res = TEST_SKIPPED;
  30316. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  30317. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30318. SSL_CTX* ctx;
  30319. SSL* ssl;
  30320. WOLFSSL_X509_VERIFY_PARAM* pParam;
  30321. #ifndef NO_WOLFSSL_SERVER
  30322. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30323. #else
  30324. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30325. #endif
  30326. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  30327. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30328. AssertNotNull(ssl = SSL_new(ctx));
  30329. pParam = SSL_get0_param(ssl);
  30330. (void)pParam;
  30331. SSL_free(ssl);
  30332. SSL_CTX_free(ctx);
  30333. res = TEST_RES_CHECK(1);
  30334. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30335. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  30336. return res;
  30337. }
  30338. static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  30339. {
  30340. int res = TEST_SKIPPED;
  30341. #if defined(OPENSSL_EXTRA)
  30342. const char host[] = "www.example.com";
  30343. WOLFSSL_X509_VERIFY_PARAM* pParam;
  30344. AssertNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  30345. sizeof(WOLFSSL_X509_VERIFY_PARAM),
  30346. HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  30347. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  30348. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  30349. AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  30350. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  30351. AssertIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  30352. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  30353. res = TEST_RES_CHECK(1);
  30354. #endif /* OPENSSL_EXTRA */
  30355. return res;
  30356. }
  30357. static int test_wolfSSL_set1_host(void)
  30358. {
  30359. int res = TEST_SKIPPED;
  30360. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  30361. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30362. const char host[] = "www.test_wolfSSL_set1_host.com";
  30363. const char emptyStr[] = "";
  30364. SSL_CTX* ctx;
  30365. SSL* ssl;
  30366. WOLFSSL_X509_VERIFY_PARAM* pParam;
  30367. #ifndef NO_WOLFSSL_SERVER
  30368. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30369. #else
  30370. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30371. #endif
  30372. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  30373. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30374. AssertNotNull(ssl = SSL_new(ctx));
  30375. pParam = SSL_get0_param(ssl);
  30376. /* we should get back host string */
  30377. SSL_set1_host(ssl, host);
  30378. AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  30379. /* we should get back empty string */
  30380. SSL_set1_host(ssl, emptyStr);
  30381. AssertIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  30382. /* we should get back host string */
  30383. SSL_set1_host(ssl, host);
  30384. AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  30385. /* we should get back empty string */
  30386. SSL_set1_host(ssl, NULL);
  30387. AssertIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  30388. SSL_free(ssl);
  30389. SSL_CTX_free(ctx);
  30390. res = TEST_RES_CHECK(1);
  30391. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30392. #endif /* OPENSSL_EXTRA */
  30393. return res;
  30394. }
  30395. static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  30396. {
  30397. int res = TEST_SKIPPED;
  30398. #if defined(OPENSSL_EXTRA)
  30399. unsigned char buf[16] = {0};
  30400. WOLFSSL_X509_VERIFY_PARAM* param;
  30401. AssertNotNull(param = X509_VERIFY_PARAM_new());
  30402. /* test 127.0.0.1 */
  30403. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  30404. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  30405. AssertIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  30406. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  30407. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  30408. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  30409. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  30410. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  30411. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  30412. AssertIntEQ(XSTRNCMP(param->ipasc,
  30413. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  30414. /* test 2001:db8:: */
  30415. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  30416. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  30417. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  30418. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  30419. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  30420. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  30421. /* test ::1234:5678 */
  30422. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  30423. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  30424. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  30425. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  30426. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  30427. AssertIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  30428. /* test 2001:db8::1234:5678 */
  30429. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  30430. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  30431. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  30432. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  30433. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  30434. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  30435. sizeof(param->ipasc)), 0);
  30436. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  30437. /* 2001:db8:1::ab9:c0a8:102 */
  30438. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  30439. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  30440. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  30441. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  30442. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  30443. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  30444. sizeof(param->ipasc)), 0);
  30445. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  30446. res = TEST_RES_CHECK(1);
  30447. #endif /* OPENSSL_EXTRA */
  30448. return res;
  30449. }
  30450. static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
  30451. {
  30452. int res = TEST_SKIPPED;
  30453. #if defined(OPENSSL_EXTRA)
  30454. X509_STORE* store;
  30455. X509_STORE_CTX* ctx;
  30456. X509_STORE_CTX* ctx_no_init;
  30457. AssertNotNull((store = X509_STORE_new()));
  30458. AssertNotNull(ctx = X509_STORE_CTX_new());
  30459. AssertNotNull(ctx_no_init = X509_STORE_CTX_new());
  30460. AssertIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  30461. AssertNull(X509_STORE_CTX_get0_store(NULL));
  30462. /* should return NULL if ctx has not bee initialized */
  30463. AssertNull(X509_STORE_CTX_get0_store(ctx_no_init));
  30464. AssertNotNull(X509_STORE_CTX_get0_store(ctx));
  30465. wolfSSL_X509_STORE_CTX_free(ctx);
  30466. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  30467. X509_STORE_free(store);
  30468. res = TEST_RES_CHECK(1);
  30469. #endif /* OPENSSL_EXTRA */
  30470. return res;
  30471. }
  30472. static int test_wolfSSL_CTX_set_client_CA_list(void)
  30473. {
  30474. int res = TEST_SKIPPED;
  30475. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  30476. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  30477. WOLFSSL_CTX* ctx;
  30478. WOLFSSL* ssl;
  30479. X509_NAME* name = NULL;
  30480. STACK_OF(X509_NAME)* names = NULL;
  30481. STACK_OF(X509_NAME)* ca_list = NULL;
  30482. int i, names_len;
  30483. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  30484. /* Send two X501 names in cert request */
  30485. names = SSL_load_client_CA_file(cliCertFile);
  30486. AssertNotNull(names);
  30487. ca_list = SSL_load_client_CA_file(caCertFile);
  30488. AssertNotNull(ca_list);
  30489. AssertIntEQ(sk_X509_NAME_push(names, sk_X509_NAME_value(ca_list, 0)), 1);
  30490. SSL_CTX_set_client_CA_list(ctx, names);
  30491. /* This should only free the stack structure */
  30492. sk_X509_NAME_free(ca_list);
  30493. AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  30494. AssertIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  30495. AssertIntGT((names_len = sk_X509_NAME_num(names)), 0);
  30496. for (i=0; i<names_len; i++) {
  30497. AssertNotNull(name = sk_X509_NAME_value(names, i));
  30498. AssertIntEQ(sk_X509_NAME_find(names, name), i);
  30499. }
  30500. /* Needed to be able to create ssl object */
  30501. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  30502. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30503. AssertNotNull(ssl = wolfSSL_new(ctx));
  30504. /* load again as old names are responsibility of ctx to free*/
  30505. names = SSL_load_client_CA_file(cliCertFile);
  30506. AssertNotNull(names);
  30507. SSL_set_client_CA_list(ssl, names);
  30508. AssertNotNull(ca_list = SSL_get_client_CA_list(ssl));
  30509. AssertIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  30510. AssertIntGT((names_len = sk_X509_NAME_num(names)), 0);
  30511. for (i=0; i<names_len; i++) {
  30512. AssertNotNull(name = sk_X509_NAME_value(names, i));
  30513. AssertIntEQ(sk_X509_NAME_find(names, name), i);
  30514. }
  30515. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  30516. {
  30517. tcp_ready ready;
  30518. func_args server_args;
  30519. callback_functions server_cb;
  30520. THREAD_TYPE serverThread;
  30521. WOLFSSL* ssl_client;
  30522. WOLFSSL_CTX* ctx_client;
  30523. SOCKET_T sockfd = 0;
  30524. /* wolfSSL_get_client_CA_list() with handshake */
  30525. StartTCP();
  30526. InitTcpReady(&ready);
  30527. XMEMSET(&server_args, 0, sizeof(func_args));
  30528. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  30529. server_args.signal = &ready;
  30530. server_args.callbacks = &server_cb;
  30531. /* we are responsible for free'ing WOLFSSL_CTX */
  30532. server_cb.ctx = ctx;
  30533. server_cb.isSharedCtx = 1;
  30534. AssertIntEQ(WOLFSSL_SUCCESS,
  30535. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  30536. start_thread(test_server_nofail, &server_args, &serverThread);
  30537. wait_tcp_ready(&server_args);
  30538. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  30539. AssertNotNull(ctx_client = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  30540. AssertIntEQ(WOLFSSL_SUCCESS,
  30541. wolfSSL_CTX_load_verify_locations(ctx_client, caCertFile, 0));
  30542. AssertIntEQ(WOLFSSL_SUCCESS,
  30543. wolfSSL_CTX_use_certificate_file(ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  30544. AssertIntEQ(WOLFSSL_SUCCESS,
  30545. wolfSSL_CTX_use_PrivateKey_file(ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  30546. AssertNotNull(ssl_client = wolfSSL_new(ctx_client));
  30547. AssertIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  30548. AssertIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  30549. AssertNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  30550. /* We are expecting two cert names to be sent */
  30551. AssertIntEQ(sk_X509_NAME_num(ca_list), 2);
  30552. AssertNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  30553. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  30554. AssertNotNull(name = sk_X509_NAME_value(ca_list, i));
  30555. AssertIntGE(sk_X509_NAME_find(names, name), 0);
  30556. }
  30557. wolfSSL_shutdown(ssl_client);
  30558. wolfSSL_free(ssl_client);
  30559. wolfSSL_CTX_free(ctx_client);
  30560. join_thread(serverThread);
  30561. FreeTcpReady(&ready);
  30562. }
  30563. #endif
  30564. wolfSSL_free(ssl);
  30565. wolfSSL_CTX_free(ctx);
  30566. res = TEST_RES_CHECK(1);
  30567. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT &&
  30568. * !NO_BIO */
  30569. return res;
  30570. }
  30571. static int test_wolfSSL_CTX_add_client_CA(void)
  30572. {
  30573. int res = TEST_SKIPPED;
  30574. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  30575. !defined(NO_WOLFSSL_CLIENT)
  30576. WOLFSSL_CTX* ctx;
  30577. WOLFSSL_X509* x509;
  30578. WOLFSSL_X509* x509_a;
  30579. STACK_OF(X509_NAME)* ca_list;
  30580. int ret = 0;
  30581. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30582. /* Add client cert */
  30583. x509 = X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
  30584. AssertNotNull(x509);
  30585. ret = SSL_CTX_add_client_CA(ctx, x509);
  30586. AssertIntEQ(ret, SSL_SUCCESS);
  30587. AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  30588. /* Add another client cert */
  30589. AssertNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  30590. SSL_FILETYPE_PEM));
  30591. AssertIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  30592. /* test for incorrect parameter */
  30593. AssertIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  30594. AssertIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  30595. AssertIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  30596. X509_free(x509);
  30597. X509_free(x509_a);
  30598. SSL_CTX_free(ctx);
  30599. res = TEST_RES_CHECK(1);
  30600. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  30601. return res;
  30602. }
  30603. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  30604. static THREAD_RETURN WOLFSSL_THREAD server_task(void* args)
  30605. {
  30606. callback_functions* callbacks = ((func_args*)args)->callbacks;
  30607. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(callbacks->method());
  30608. WOLFSSL* ssl = NULL;
  30609. SOCKET_T sfd = 0;
  30610. SOCKET_T cfd = 0;
  30611. word16 port;
  30612. char msg[] = "I hear you fa shizzle!";
  30613. int len = (int) XSTRLEN(msg);
  30614. char input[1024];
  30615. int idx;
  30616. int ret, err = 0;
  30617. #ifdef WOLFSSL_TIRTOS
  30618. fdOpenSession(Task_self());
  30619. #endif
  30620. ((func_args*)args)->return_code = TEST_FAIL;
  30621. port = ((func_args*)args)->signal->port;
  30622. AssertIntEQ(WOLFSSL_SUCCESS,
  30623. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  30624. AssertIntEQ(WOLFSSL_SUCCESS,
  30625. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  30626. WOLFSSL_FILETYPE_PEM));
  30627. AssertIntEQ(WOLFSSL_SUCCESS,
  30628. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  30629. WOLFSSL_FILETYPE_PEM));
  30630. if (callbacks->ctx_ready)
  30631. callbacks->ctx_ready(ctx);
  30632. ssl = wolfSSL_new(ctx);
  30633. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  30634. CloseSocket(sfd);
  30635. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  30636. if (callbacks->ssl_ready)
  30637. callbacks->ssl_ready(ssl);
  30638. do {
  30639. err = 0; /* Reset error */
  30640. ret = wolfSSL_accept(ssl);
  30641. if (ret != WOLFSSL_SUCCESS) {
  30642. err = wolfSSL_get_error(ssl, 0);
  30643. }
  30644. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  30645. if (ret != WOLFSSL_SUCCESS) {
  30646. char buff[WOLFSSL_MAX_ERROR_SZ];
  30647. fprintf(stderr, "error = %d, %s\n", err,
  30648. wolfSSL_ERR_error_string(err, buff));
  30649. }
  30650. else {
  30651. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  30652. input[idx] = 0;
  30653. fprintf(stderr, "Client message: %s\n", input);
  30654. }
  30655. AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
  30656. #ifdef WOLFSSL_TIRTOS
  30657. Task_yield();
  30658. #endif
  30659. ((func_args*)args)->return_code = TEST_SUCCESS;
  30660. }
  30661. if (callbacks->on_result)
  30662. callbacks->on_result(ssl);
  30663. wolfSSL_shutdown(ssl);
  30664. wolfSSL_free(ssl);
  30665. wolfSSL_CTX_free(ctx);
  30666. CloseSocket(cfd);
  30667. #ifdef WOLFSSL_TIRTOS
  30668. fdCloseSession(Task_self());
  30669. #endif
  30670. #ifndef WOLFSSL_TIRTOS
  30671. return 0;
  30672. #endif
  30673. }
  30674. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  30675. {
  30676. AssertNotNull(ssl);
  30677. AssertNotNull(line);
  30678. XFILE fp;
  30679. const byte lf = '\n';
  30680. fp = XFOPEN("./MyKeyLog.txt", "a");
  30681. XFWRITE( line, 1, strlen(line),fp);
  30682. XFWRITE( (void*)&lf,1,1,fp);
  30683. XFCLOSE(fp);
  30684. }
  30685. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  30686. static int test_wolfSSL_CTX_set_keylog_callback(void)
  30687. {
  30688. int res = TEST_SKIPPED;
  30689. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  30690. !defined(NO_WOLFSSL_CLIENT)
  30691. SSL_CTX* ctx;
  30692. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30693. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  30694. SSL_CTX_free(ctx);
  30695. SSL_CTX_set_keylog_callback(NULL, NULL);
  30696. res = TEST_RES_CHECK(1);
  30697. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  30698. return res;
  30699. }
  30700. static int test_wolfSSL_CTX_get_keylog_callback(void)
  30701. {
  30702. int res = TEST_SKIPPED;
  30703. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  30704. !defined(NO_WOLFSSL_CLIENT)
  30705. SSL_CTX* ctx;
  30706. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30707. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  30708. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  30709. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  30710. SSL_CTX_set_keylog_callback(ctx, NULL );
  30711. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  30712. SSL_CTX_free(ctx);
  30713. res = TEST_RES_CHECK(1);
  30714. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  30715. return res;
  30716. }
  30717. static int test_wolfSSL_Tls12_Key_Logging_test(void)
  30718. {
  30719. int res = TEST_SKIPPED;
  30720. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  30721. /* This test is intended for checking whether keylog callback is called
  30722. * in client during TLS handshake between the client and a server.
  30723. */
  30724. tcp_ready ready;
  30725. func_args client_args;
  30726. func_args server_args;
  30727. THREAD_TYPE serverThread;
  30728. callback_functions server_cbf;
  30729. callback_functions client_cbf;
  30730. SOCKET_T sockfd = 0;
  30731. WOLFSSL_CTX* ctx;
  30732. WOLFSSL* ssl;
  30733. XFILE fp;
  30734. char msg[64] = "hello wolfssl!";
  30735. char reply[1024];
  30736. int msgSz = (int)XSTRLEN(msg);
  30737. #ifdef WOLFSSL_TIRTOS
  30738. fdOpenSession(Task_self());
  30739. #endif
  30740. InitTcpReady(&ready);
  30741. ready.port = 22222;
  30742. XMEMSET(&client_args, 0, sizeof(func_args));
  30743. XMEMSET(&server_args, 0, sizeof(func_args));
  30744. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  30745. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  30746. server_cbf.method = wolfTLSv1_2_server_method;
  30747. server_args.callbacks = &server_cbf;
  30748. server_args.signal = &ready;
  30749. /* clean up keylog file */
  30750. fp = XFOPEN("./MyKeyLog.txt", "w");
  30751. XFCLOSE(fp);
  30752. /* start server task */
  30753. start_thread(server_task, &server_args, &serverThread);
  30754. wait_tcp_ready(&server_args);
  30755. /* run as a TLS1.2 client */
  30756. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  30757. AssertIntEQ(WOLFSSL_SUCCESS,
  30758. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  30759. AssertIntEQ(WOLFSSL_SUCCESS,
  30760. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  30761. AssertIntEQ(WOLFSSL_SUCCESS,
  30762. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  30763. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  30764. /* set keylog callback */
  30765. wolfSSL_CTX_set_keylog_callback(ctx,keyLog_callback);
  30766. /* get connected the server task */
  30767. AssertNotNull(ssl = wolfSSL_new(ctx));
  30768. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  30769. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  30770. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  30771. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  30772. wolfSSL_shutdown(ssl);
  30773. wolfSSL_free(ssl);
  30774. wolfSSL_CTX_free(ctx);
  30775. CloseSocket(sockfd);
  30776. join_thread(serverThread);
  30777. FreeTcpReady(&ready);
  30778. #ifdef WOLFSSL_TIRTOS
  30779. fdOpenSession(Task_self());
  30780. #endif
  30781. /* check if the keylog file exists */
  30782. char buff[300] = {0};
  30783. int found = 0;
  30784. fp = XFOPEN("./MyKeyLog.txt", "r");
  30785. AssertNotNull(fp);
  30786. while (XFGETS( buff, (int)sizeof(buff),fp) != NULL ) {
  30787. if (0 == strncmp(buff,"CLIENT_RANDOM ",
  30788. sizeof("CLIENT_RANDOM ")-1)) {
  30789. found = 1;
  30790. break;
  30791. }
  30792. }
  30793. XFCLOSE(fp);
  30794. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  30795. AssertNotNull( found );
  30796. res = TEST_RES_CHECK(1);
  30797. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  30798. return res;
  30799. }
  30800. static int test_wolfSSL_Tls13_Key_Logging_test(void)
  30801. {
  30802. int res = TEST_SKIPPED;
  30803. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  30804. defined(HAVE_SECRET_CALLBACK)
  30805. /* This test is intended for checking whether keylog callback is called
  30806. * in client during TLS handshake between the client and a server.
  30807. */
  30808. tcp_ready ready;
  30809. func_args client_args;
  30810. func_args server_args;
  30811. THREAD_TYPE serverThread;
  30812. callback_functions server_cbf;
  30813. callback_functions client_cbf;
  30814. SOCKET_T sockfd = 0;
  30815. WOLFSSL_CTX* ctx;
  30816. WOLFSSL* ssl;
  30817. XFILE fp;
  30818. char msg[64] = "hello wolfssl!";
  30819. char reply[1024];
  30820. int msgSz = (int)XSTRLEN(msg);
  30821. #ifdef WOLFSSL_TIRTOS
  30822. fdOpenSession(Task_self());
  30823. #endif
  30824. InitTcpReady(&ready);
  30825. ready.port = 22222;
  30826. XMEMSET(&client_args, 0, sizeof(func_args));
  30827. XMEMSET(&server_args, 0, sizeof(func_args));
  30828. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  30829. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  30830. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  30831. server_args.callbacks = &server_cbf;
  30832. server_args.signal = &ready;
  30833. /* clean up keylog file */
  30834. fp = XFOPEN("./MyKeyLog.txt", "w");
  30835. XFCLOSE(fp);
  30836. /* start server task */
  30837. start_thread(server_task, &server_args, &serverThread);
  30838. wait_tcp_ready(&server_args);
  30839. /* run as a TLS1.3 client */
  30840. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  30841. AssertIntEQ(WOLFSSL_SUCCESS,
  30842. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  30843. AssertIntEQ(WOLFSSL_SUCCESS,
  30844. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  30845. AssertIntEQ(WOLFSSL_SUCCESS,
  30846. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  30847. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  30848. /* set keylog callback */
  30849. wolfSSL_CTX_set_keylog_callback(ctx,keyLog_callback);
  30850. /* get connected the server task */
  30851. AssertNotNull(ssl = wolfSSL_new(ctx));
  30852. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  30853. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  30854. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  30855. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  30856. wolfSSL_free(ssl);
  30857. wolfSSL_CTX_free(ctx);
  30858. join_thread(serverThread);
  30859. FreeTcpReady(&ready);
  30860. #ifdef WOLFSSL_TIRTOS
  30861. fdOpenSession(Task_self());
  30862. #endif
  30863. /* check if the keylog file exists */
  30864. {
  30865. char buff[300] = {0};
  30866. int found[4] = {0};
  30867. int numfnd = 0;
  30868. int i;
  30869. fp = XFOPEN("./MyKeyLog.txt", "r");
  30870. AssertNotNull(fp);
  30871. while (XFGETS( buff, (int)sizeof(buff),fp) != NULL ) {
  30872. if (0 == strncmp(buff,"CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  30873. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  30874. found[0] = 1;
  30875. continue;
  30876. }
  30877. else if (0 == strncmp(buff,"SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  30878. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  30879. found[1] = 1;
  30880. continue;
  30881. }
  30882. else if (0 == strncmp(buff,"CLIENT_TRAFFIC_SECRET_0 ",
  30883. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  30884. found[2] = 1;
  30885. continue;
  30886. }
  30887. else if (0 == strncmp(buff,"SERVER_TRAFFIC_SECRET_0 ",
  30888. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  30889. found[3] = 1;
  30890. continue;
  30891. }
  30892. }
  30893. XFCLOSE(fp);
  30894. for (i = 0; i < 4; i++) {
  30895. if (found[i] != 0)
  30896. numfnd++;
  30897. }
  30898. AssertIntEQ(numfnd, 4);
  30899. }
  30900. res = TEST_RES_CHECK(1);
  30901. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  30902. return res;
  30903. }
  30904. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  30905. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30906. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  30907. static void post_auth_version_cb(WOLFSSL* ssl)
  30908. {
  30909. /* do handshake and then test version error */
  30910. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  30911. AssertStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  30912. }
  30913. static void post_auth_version_client_cb(WOLFSSL* ssl)
  30914. {
  30915. /* do handshake and then test version error */
  30916. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  30917. AssertStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  30918. AssertIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  30919. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  30920. /* check was added to error queue */
  30921. AssertIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  30922. /* check the string matches expected string */
  30923. AssertStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  30924. "WRONG_SSL_VERSION");
  30925. #endif
  30926. }
  30927. static void post_auth_cb(WOLFSSL* ssl)
  30928. {
  30929. WOLFSSL_X509* x509;
  30930. /* do handshake and then test version error */
  30931. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  30932. AssertStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  30933. AssertNull(x509 = wolfSSL_get_peer_certificate(ssl));
  30934. wolfSSL_X509_free(x509);
  30935. AssertIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  30936. }
  30937. static void set_post_auth_cb(WOLFSSL* ssl)
  30938. {
  30939. if (!wolfSSL_is_server(ssl)) {
  30940. AssertIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  30941. }
  30942. else {
  30943. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  30944. }
  30945. }
  30946. #endif
  30947. static int test_wolfSSL_Tls13_postauth(void)
  30948. {
  30949. int res = TEST_SKIPPED;
  30950. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  30951. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30952. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  30953. tcp_ready ready;
  30954. func_args client_args;
  30955. func_args server_args;
  30956. callback_functions server_cbf;
  30957. callback_functions client_cbf;
  30958. THREAD_TYPE serverThread;
  30959. XMEMSET(&client_args, 0, sizeof(func_args));
  30960. XMEMSET(&server_args, 0, sizeof(func_args));
  30961. StartTCP();
  30962. InitTcpReady(&ready);
  30963. #if defined(USE_WINDOWS_API)
  30964. /* use RNG to get random port if using windows */
  30965. ready.port = GetRandomPort();
  30966. #endif
  30967. server_args.signal = &ready;
  30968. client_args.signal = &ready;
  30969. /* test version failure doing post auth with TLS 1.2 connection */
  30970. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  30971. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  30972. server_cbf.method = wolfTLSv1_2_server_method;
  30973. server_cbf.ssl_ready = set_post_auth_cb;
  30974. server_cbf.on_result = post_auth_version_cb;
  30975. client_cbf.ssl_ready = set_post_auth_cb;
  30976. client_cbf.on_result = post_auth_version_client_cb;
  30977. server_args.callbacks = &server_cbf;
  30978. client_args.callbacks = &client_cbf;
  30979. start_thread(test_server_nofail, &server_args, &serverThread);
  30980. wait_tcp_ready(&server_args);
  30981. test_client_nofail(&client_args, NULL);
  30982. join_thread(serverThread);
  30983. /* tests on post auth with TLS 1.3 */
  30984. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  30985. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  30986. server_cbf.method = wolfTLSv1_3_server_method;
  30987. server_cbf.ssl_ready = set_post_auth_cb;
  30988. client_cbf.ssl_ready = set_post_auth_cb;
  30989. server_cbf.on_result = post_auth_cb;
  30990. client_cbf.on_result = NULL;
  30991. server_args.callbacks = &server_cbf;
  30992. client_args.callbacks = &client_cbf;
  30993. start_thread(test_server_nofail, &server_args, &serverThread);
  30994. wait_tcp_ready(&server_args);
  30995. test_client_nofail(&client_args, NULL);
  30996. join_thread(serverThread);
  30997. FreeTcpReady(&ready);
  30998. res = TEST_RES_CHECK(1);
  30999. #endif
  31000. return res;
  31001. }
  31002. static int test_wolfSSL_X509_NID(void)
  31003. {
  31004. int res = TEST_SKIPPED;
  31005. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  31006. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  31007. int sigType;
  31008. int nameSz;
  31009. X509* cert;
  31010. EVP_PKEY* pubKeyTmp;
  31011. X509_NAME* name;
  31012. char commonName[80];
  31013. char countryName[80];
  31014. char localityName[80];
  31015. char stateName[80];
  31016. char orgName[80];
  31017. char orgUnit[80];
  31018. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  31019. /* convert cert from DER to internal WOLFSSL_X509 struct */
  31020. AssertNotNull(cert = wolfSSL_X509_d2i(&cert, client_cert_der_2048,
  31021. sizeof_client_cert_der_2048));
  31022. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  31023. /* extract PUBLIC KEY from cert */
  31024. AssertNotNull(pubKeyTmp = X509_get_pubkey(cert));
  31025. /* extract signatureType */
  31026. AssertIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  31027. /* extract subjectName info */
  31028. AssertNotNull(name = X509_get_subject_name(cert));
  31029. AssertIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  31030. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  31031. NULL, 0)), 0);
  31032. AssertIntEQ(nameSz, 15);
  31033. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  31034. commonName, sizeof(commonName))), 0);
  31035. AssertIntEQ(nameSz, 15);
  31036. AssertIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  31037. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  31038. commonName, 9)), 0);
  31039. AssertIntEQ(nameSz, 8);
  31040. AssertIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  31041. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  31042. countryName, sizeof(countryName))), 0);
  31043. AssertIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  31044. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  31045. localityName, sizeof(localityName))), 0);
  31046. AssertIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  31047. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_stateOrProvinceName,
  31048. stateName, sizeof(stateName))), 0);
  31049. AssertIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  31050. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  31051. orgName, sizeof(orgName))), 0);
  31052. AssertIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  31053. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationalUnitName,
  31054. orgUnit, sizeof(orgUnit))), 0);
  31055. AssertIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  31056. EVP_PKEY_free(pubKeyTmp);
  31057. X509_free(cert);
  31058. res = TEST_RES_CHECK(1);
  31059. #endif
  31060. return res;
  31061. }
  31062. static int test_wolfSSL_CTX_set_srp_username(void)
  31063. {
  31064. int res = TEST_SKIPPED;
  31065. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  31066. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  31067. WOLFSSL_CTX* ctx;
  31068. WOLFSSL* ssl;
  31069. const char *username = "TESTUSER";
  31070. const char *password = "TESTPASSWORD";
  31071. int r;
  31072. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  31073. AssertNotNull(ctx);
  31074. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  31075. AssertIntEQ(r,SSL_SUCCESS);
  31076. wolfSSL_CTX_free(ctx);
  31077. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  31078. AssertNotNull(ctx);
  31079. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  31080. AssertIntEQ(r,SSL_SUCCESS);
  31081. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  31082. AssertIntEQ(r,SSL_SUCCESS);
  31083. AssertNotNull(ssl = SSL_new(ctx));
  31084. AssertNotNull(SSL_get_srp_username(ssl));
  31085. AssertStrEQ(SSL_get_srp_username(ssl), username);
  31086. wolfSSL_free(ssl);
  31087. wolfSSL_CTX_free(ctx);
  31088. res = TEST_RES_CHECK(1);
  31089. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  31090. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  31091. return res;
  31092. }
  31093. static int test_wolfSSL_CTX_set_srp_password(void)
  31094. {
  31095. int res = TEST_SKIPPED;
  31096. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  31097. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  31098. WOLFSSL_CTX* ctx;
  31099. const char *username = "TESTUSER";
  31100. const char *password = "TESTPASSWORD";
  31101. int r;
  31102. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  31103. AssertNotNull(ctx);
  31104. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  31105. AssertIntEQ(r,SSL_SUCCESS);
  31106. wolfSSL_CTX_free(ctx);
  31107. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  31108. AssertNotNull(ctx);
  31109. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  31110. AssertIntEQ(r,SSL_SUCCESS);
  31111. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  31112. AssertIntEQ(r,SSL_SUCCESS);
  31113. wolfSSL_CTX_free(ctx);
  31114. res = TEST_RES_CHECK(1);
  31115. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  31116. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  31117. return res;
  31118. }
  31119. static int test_wolfSSL_X509_STORE(void)
  31120. {
  31121. int res = TEST_SKIPPED;
  31122. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  31123. X509_STORE *store;
  31124. #ifdef HAVE_CRL
  31125. X509_STORE_CTX *storeCtx;
  31126. X509_CRL *crl;
  31127. X509 *ca, *cert;
  31128. const char crlPem[] = "./certs/crl/crl.revoked";
  31129. const char srvCert[] = "./certs/server-revoked-cert.pem";
  31130. const char caCert[] = "./certs/ca-cert.pem";
  31131. XFILE fp;
  31132. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  31133. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  31134. SSL_FILETYPE_PEM)));
  31135. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  31136. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  31137. SSL_FILETYPE_PEM)));
  31138. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  31139. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  31140. AssertIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  31141. X509_STORE_free(store);
  31142. X509_STORE_CTX_free(storeCtx);
  31143. X509_free(cert);
  31144. X509_free(ca);
  31145. /* should fail to verify now after adding in CRL */
  31146. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  31147. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  31148. SSL_FILETYPE_PEM)));
  31149. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  31150. fp = XFOPEN(crlPem, "rb");
  31151. AssertTrue((fp != XBADFILE));
  31152. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  31153. NULL, NULL));
  31154. XFCLOSE(fp);
  31155. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  31156. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  31157. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  31158. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  31159. SSL_FILETYPE_PEM)));
  31160. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  31161. AssertIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  31162. AssertIntEQ(X509_STORE_CTX_get_error(storeCtx), CRL_CERT_REVOKED);
  31163. X509_CRL_free(crl);
  31164. X509_STORE_free(store);
  31165. X509_STORE_CTX_free(storeCtx);
  31166. X509_free(cert);
  31167. X509_free(ca);
  31168. #endif /* HAVE_CRL */
  31169. #ifndef WOLFCRYPT_ONLY
  31170. {
  31171. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31172. SSL_CTX* ctx;
  31173. SSL* ssl;
  31174. int i;
  31175. for (i = 0; i < 2; i++) {
  31176. #ifndef NO_WOLFSSL_SERVER
  31177. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31178. #else
  31179. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31180. #endif
  31181. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  31182. SSL_CTX_set_cert_store(ctx, store);
  31183. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  31184. SSL_CTX_set_cert_store(ctx, store);
  31185. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  31186. AssertIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  31187. SSL_FILETYPE_PEM), SSL_SUCCESS);
  31188. AssertIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  31189. SSL_FILETYPE_PEM), SSL_SUCCESS);
  31190. AssertNotNull(ssl = SSL_new(ctx));
  31191. if (i == 0) {
  31192. AssertIntEQ(SSL_set0_verify_cert_store(ssl, store), SSL_SUCCESS);
  31193. }
  31194. else {
  31195. AssertIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  31196. X509_STORE_free(store);
  31197. }
  31198. SSL_free(ssl);
  31199. SSL_CTX_free(ctx);
  31200. }
  31201. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31202. }
  31203. #endif
  31204. res = TEST_RES_CHECK(1);
  31205. #endif
  31206. return res;
  31207. }
  31208. static int test_wolfSSL_X509_STORE_load_locations(void)
  31209. {
  31210. int res = TEST_SKIPPED;
  31211. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  31212. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  31213. SSL_CTX *ctx;
  31214. X509_STORE *store;
  31215. const char ca_file[] = "./certs/ca-cert.pem";
  31216. const char client_pem_file[] = "./certs/client-cert.pem";
  31217. const char client_der_file[] = "./certs/client-cert.der";
  31218. const char ecc_file[] = "./certs/ecc-key.pem";
  31219. const char certs_path[] = "./certs/";
  31220. const char bad_path[] = "./bad-path/";
  31221. #ifdef HAVE_CRL
  31222. const char crl_path[] = "./certs/crl/";
  31223. const char crl_file[] = "./certs/crl/crl.pem";
  31224. #endif
  31225. #ifndef NO_WOLFSSL_SERVER
  31226. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  31227. #else
  31228. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  31229. #endif
  31230. AssertNotNull(store = SSL_CTX_get_cert_store(ctx));
  31231. AssertIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL), WOLFSSL_SUCCESS);
  31232. /* Test bad arguments */
  31233. AssertIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL), WOLFSSL_FAILURE);
  31234. AssertIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  31235. AssertIntEQ(X509_STORE_load_locations(store, client_der_file, NULL), WOLFSSL_FAILURE);
  31236. AssertIntEQ(X509_STORE_load_locations(store, ecc_file, NULL), WOLFSSL_FAILURE);
  31237. AssertIntEQ(X509_STORE_load_locations(store, NULL, bad_path), WOLFSSL_FAILURE);
  31238. #ifdef HAVE_CRL
  31239. /* Test with CRL */
  31240. AssertIntEQ(X509_STORE_load_locations(store, crl_file, NULL), WOLFSSL_SUCCESS);
  31241. AssertIntEQ(X509_STORE_load_locations(store, NULL, crl_path), WOLFSSL_SUCCESS);
  31242. #endif
  31243. /* Test with CA */
  31244. AssertIntEQ(X509_STORE_load_locations(store, ca_file, NULL), WOLFSSL_SUCCESS);
  31245. /* Test with client_cert and certs path */
  31246. AssertIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL), WOLFSSL_SUCCESS);
  31247. AssertIntEQ(X509_STORE_load_locations(store, NULL, certs_path), WOLFSSL_SUCCESS);
  31248. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  31249. /* Clear nodes */
  31250. ERR_clear_error();
  31251. #endif
  31252. SSL_CTX_free(ctx);
  31253. res = TEST_RES_CHECK(1);
  31254. #endif
  31255. return res;
  31256. }
  31257. static int test_X509_STORE_get0_objects(void)
  31258. {
  31259. int res = TEST_SKIPPED;
  31260. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  31261. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  31262. X509_STORE *store;
  31263. X509_STORE *store_cpy;
  31264. SSL_CTX *ctx;
  31265. X509_OBJECT *obj;
  31266. STACK_OF(X509_OBJECT) *objs;
  31267. int i;
  31268. /* Setup store */
  31269. #ifndef NO_WOLFSSL_SERVER
  31270. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  31271. #else
  31272. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  31273. #endif
  31274. AssertNotNull(store_cpy = X509_STORE_new());
  31275. AssertNotNull(store = SSL_CTX_get_cert_store(ctx));
  31276. AssertIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL), WOLFSSL_SUCCESS);
  31277. AssertIntEQ(X509_STORE_load_locations(store, caCertFile, NULL), WOLFSSL_SUCCESS);
  31278. AssertIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL), WOLFSSL_SUCCESS);
  31279. #ifdef HAVE_CRL
  31280. AssertIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir), WOLFSSL_SUCCESS);
  31281. #endif
  31282. /* Store ready */
  31283. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  31284. AssertNotNull(objs = X509_STORE_get0_objects(store));
  31285. #ifdef HAVE_CRL
  31286. #ifdef WOLFSSL_SIGNER_DER_CERT
  31287. AssertIntEQ(sk_X509_OBJECT_num(objs), 4);
  31288. #else
  31289. AssertIntEQ(sk_X509_OBJECT_num(objs), 1);
  31290. #endif
  31291. #else
  31292. #ifdef WOLFSSL_SIGNER_DER_CERT
  31293. AssertIntEQ(sk_X509_OBJECT_num(objs), 3);
  31294. #else
  31295. AssertIntEQ(sk_X509_OBJECT_num(objs), 0);
  31296. #endif
  31297. #endif
  31298. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  31299. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  31300. switch (X509_OBJECT_get_type(obj)) {
  31301. case X509_LU_X509:
  31302. AssertNotNull(X509_OBJECT_get0_X509(obj));
  31303. AssertIntEQ(X509_STORE_add_cert(store_cpy,
  31304. X509_OBJECT_get0_X509(obj)), WOLFSSL_SUCCESS);
  31305. break;
  31306. case X509_LU_CRL:
  31307. #ifdef HAVE_CRL
  31308. AssertNotNull(X509_OBJECT_get0_X509_CRL(obj));
  31309. AssertIntEQ(X509_STORE_add_crl(store_cpy,
  31310. X509_OBJECT_get0_X509_CRL(obj)), WOLFSSL_SUCCESS);
  31311. break;
  31312. #endif
  31313. case X509_LU_NONE:
  31314. default:
  31315. Fail(("X509_OBJECT_get_type should return x509 or crl "
  31316. "(when built with crl support)"),
  31317. ("Unrecognized X509_OBJECT type or none"));
  31318. }
  31319. }
  31320. X509_STORE_free(store_cpy);
  31321. SSL_CTX_free(ctx);
  31322. res = TEST_RES_CHECK(1);
  31323. #endif
  31324. return res;
  31325. }
  31326. static int test_wolfSSL_BN(void)
  31327. {
  31328. int res = TEST_SKIPPED;
  31329. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  31330. BIGNUM* a;
  31331. BIGNUM* b;
  31332. BIGNUM* c;
  31333. BIGNUM* d;
  31334. ASN1_INTEGER* ai;
  31335. AssertNotNull(b = BN_new());
  31336. AssertNotNull(c = BN_new());
  31337. AssertNotNull(d = BN_new());
  31338. ai = ASN1_INTEGER_new();
  31339. AssertNotNull(ai);
  31340. /* at the moment hard setting since no set function */
  31341. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  31342. ai->data[1] = 0x01; /* length of integer */
  31343. ai->data[2] = 0x03;
  31344. AssertNotNull(a = ASN1_INTEGER_to_BN(ai, NULL));
  31345. ASN1_INTEGER_free(ai);
  31346. AssertIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  31347. AssertIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  31348. /* a + 3 = */
  31349. AssertIntEQ(BN_add_word(NULL, 3), WOLFSSL_FAILURE);
  31350. AssertIntEQ(BN_add_word(a, 3), WOLFSSL_SUCCESS);
  31351. /* check result 3 + 3*/
  31352. AssertIntEQ(BN_get_word(a), 6);
  31353. /* set a back to 3 */
  31354. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  31355. /* a - 3 = */
  31356. AssertIntEQ(BN_sub_word(NULL, 3), WOLFSSL_FAILURE);
  31357. AssertIntEQ(BN_sub_word(a, 3), WOLFSSL_SUCCESS);
  31358. /* check result 3 - 3*/
  31359. AssertIntEQ(BN_get_word(a), 0);
  31360. /* set a back to 3 */
  31361. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  31362. /* a^b mod c = */
  31363. AssertIntEQ(BN_mod_exp(d, NULL, b, c, NULL), WOLFSSL_FAILURE);
  31364. AssertIntEQ(BN_mod_exp(d, a, b, c, NULL), WOLFSSL_SUCCESS);
  31365. /* check result 3^2 mod 5 */
  31366. AssertIntEQ(BN_get_word(d), 4);
  31367. /* a*b = */
  31368. AssertIntEQ(BN_mul(d, NULL, b, NULL), WOLFSSL_FAILURE);
  31369. AssertIntEQ(BN_mul(d, a, b, NULL), WOLFSSL_SUCCESS);
  31370. /* check result 3*2 */
  31371. AssertIntEQ(BN_get_word(d), 6);
  31372. /* c/b => db + a */
  31373. AssertIntEQ(BN_div(d, NULL, c, b, NULL), WOLFSSL_FAILURE);
  31374. AssertIntEQ(BN_div(d, a, c, b, NULL), WOLFSSL_SUCCESS);
  31375. /* check result 5/2 */
  31376. AssertIntEQ(BN_get_word(d), 2); /* check quotient */
  31377. AssertIntEQ(BN_get_word(a), 1); /* check remainder */
  31378. /* set a back to 3 */
  31379. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  31380. /* a*b mod c = */
  31381. AssertIntEQ(BN_mod_mul(d, NULL, b, c, NULL), SSL_FAILURE);
  31382. AssertIntEQ(BN_mod_mul(d, a, b, c, NULL), SSL_SUCCESS);
  31383. /* check result 3*2 mod 5 */
  31384. AssertIntEQ(BN_get_word(d), 1);
  31385. AssertIntEQ(BN_set_word(a, 16), SSL_SUCCESS);
  31386. AssertIntEQ(BN_set_word(b, 24), SSL_SUCCESS);
  31387. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  31388. /* gcd of a and b */
  31389. AssertIntEQ(BN_gcd(d, NULL, b, NULL), SSL_FAILURE);
  31390. AssertIntEQ(BN_gcd(d, a, b, NULL), SSL_SUCCESS);
  31391. /* check result gcd(16, 24) */
  31392. AssertIntEQ(BN_get_word(d), 8);
  31393. #endif /* !NO_RSA && WOLFSSL_KEY_GEN */
  31394. AssertIntEQ(BN_set_word(a, 1 << 6), SSL_SUCCESS);
  31395. AssertIntEQ(BN_rshift(b, a, 6), SSL_SUCCESS);
  31396. AssertIntEQ(BN_is_zero(b), 0);
  31397. AssertIntEQ(BN_rshift(b, a, 7), SSL_SUCCESS);
  31398. AssertIntEQ(BN_is_zero(b), 1);
  31399. AssertIntEQ(BN_rshift1(b, a), SSL_SUCCESS);
  31400. AssertIntEQ(BN_is_zero(b), 0);
  31401. /* set b back to 2 */
  31402. AssertIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  31403. {
  31404. /* BN_mod_inverse test */
  31405. BIGNUM *r = BN_new();
  31406. BIGNUM *val = BN_mod_inverse(r,b,c,NULL);
  31407. AssertIntEQ((int)(BN_get_word(r) & 0x03), 3);
  31408. BN_free(val);
  31409. }
  31410. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  31411. defined(WOLFSSL_SP_INT_NEGATIVE))
  31412. AssertIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  31413. AssertIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  31414. AssertIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  31415. AssertIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  31416. AssertIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  31417. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  31418. {
  31419. char* ret;
  31420. AssertNotNull(ret = BN_bn2dec(c));
  31421. AssertIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  31422. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  31423. }
  31424. #endif
  31425. AssertIntEQ(BN_get_word(c), 4);
  31426. #endif
  31427. BN_free(a);
  31428. BN_free(b);
  31429. BN_free(c);
  31430. BN_clear_free(d);
  31431. /* check that converting NULL and the null string returns an error */
  31432. a = NULL;
  31433. AssertIntLE(BN_hex2bn(&a, NULL), 0);
  31434. AssertIntLE(BN_hex2bn(&a, ""), 0);
  31435. AssertNull(a);
  31436. /* check that getting a string and a bin of the same number are equal,
  31437. * and that the comparison works EQ, LT and GT */
  31438. AssertIntGT(BN_hex2bn(&a, "03"), 0);
  31439. AssertNotNull(b = BN_new());
  31440. AssertIntEQ(BN_set_word(b, 3), SSL_SUCCESS);
  31441. AssertNotNull(c = BN_new());
  31442. AssertIntEQ(BN_set_word(c, 4), SSL_SUCCESS);
  31443. AssertIntEQ(BN_cmp(a, b), 0);
  31444. AssertIntLT(BN_cmp(a, c), 0);
  31445. AssertIntGT(BN_cmp(c, b), 0);
  31446. AssertIntEQ(BN_set_word(a, 0), 1);
  31447. AssertIntEQ(BN_is_zero(a), 1);
  31448. AssertIntEQ(BN_set_bit(a, 0x45), 1);
  31449. AssertIntEQ(BN_is_zero(a), 0);
  31450. AssertIntEQ(BN_is_bit_set(a, 0x45), 1);
  31451. AssertIntEQ(BN_clear_bit(a, 0x45), 1);
  31452. AssertIntEQ(BN_is_bit_set(a, 0x45), 0);
  31453. AssertIntEQ(BN_is_zero(a), 1);
  31454. BN_free(a);
  31455. BN_free(b);
  31456. BN_free(c);
  31457. #if defined(USE_FAST_MATH) && !defined(HAVE_WOLF_BIGINT)
  31458. {
  31459. BIGNUM *ap;
  31460. BIGNUM bv;
  31461. BIGNUM cv;
  31462. BIGNUM dv;
  31463. AssertNotNull(ap = BN_new());
  31464. BN_init(&bv);
  31465. BN_init(&cv);
  31466. BN_init(&dv);
  31467. AssertIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  31468. AssertIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  31469. AssertIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  31470. /* a^b mod c = */
  31471. AssertIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  31472. AssertIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  31473. /* check result 3^2 mod 5 */
  31474. AssertIntEQ(BN_get_word(&dv), 4);
  31475. /* a*b mod c = */
  31476. AssertIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  31477. AssertIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  31478. /* check result 3*2 mod 5 */
  31479. AssertIntEQ(BN_get_word(&dv), 1);
  31480. BN_free(ap);
  31481. }
  31482. #endif
  31483. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  31484. AssertNotNull(a = BN_new());
  31485. AssertIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL),
  31486. SSL_SUCCESS);
  31487. AssertIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), SSL_SUCCESS);
  31488. BN_free(a);
  31489. #endif
  31490. res = TEST_RES_CHECK(1);
  31491. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  31492. return res;
  31493. }
  31494. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31495. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31496. #define TEST_ARG 0x1234
  31497. static void msg_cb(int write_p, int version, int content_type,
  31498. const void *buf, size_t len, SSL *ssl, void *arg)
  31499. {
  31500. (void)write_p;
  31501. (void)version;
  31502. (void)content_type;
  31503. (void)buf;
  31504. (void)len;
  31505. (void)ssl;
  31506. AssertTrue(arg == (void*)TEST_ARG);
  31507. }
  31508. #endif
  31509. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31510. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  31511. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
  31512. !defined(NO_WOLFSSL_SERVER)
  31513. #ifndef SINGLE_THREADED
  31514. #if defined(SESSION_CERTS)
  31515. #include "wolfssl/internal.h"
  31516. #endif
  31517. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  31518. {
  31519. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  31520. STACK_OF(X509)* sk;
  31521. X509* x509;
  31522. int i, num;
  31523. BIO* bio;
  31524. #endif
  31525. (void) ctx;
  31526. fprintf(stderr, "\n===== msgcb called ====\n");
  31527. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  31528. AssertTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  31529. AssertIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  31530. AssertNotNull(SSL_get0_verified_chain(ssl));
  31531. #else
  31532. (void) ssl;
  31533. #endif
  31534. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  31535. bio = BIO_new(BIO_s_file());
  31536. BIO_set_fp(bio, stderr, BIO_NOCLOSE);
  31537. sk = SSL_get_peer_cert_chain(ssl);
  31538. AssertNotNull(sk);
  31539. if (!sk) {
  31540. BIO_free(bio);
  31541. return SSL_FAILURE;
  31542. }
  31543. num = sk_X509_num(sk);
  31544. AssertTrue(num > 0);
  31545. for (i = 0; i < num; i++) {
  31546. x509 = sk_X509_value(sk,i);
  31547. AssertNotNull(x509);
  31548. if (!x509)
  31549. break;
  31550. fprintf(stderr, "Certificate at index [%d] = :\n",i);
  31551. X509_print(bio,x509);
  31552. fprintf(stderr, "\n\n");
  31553. }
  31554. BIO_free(bio);
  31555. #endif
  31556. return SSL_SUCCESS;
  31557. }
  31558. #endif
  31559. #endif
  31560. static int test_wolfSSL_msgCb(void)
  31561. {
  31562. int res = TEST_SKIPPED;
  31563. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31564. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  31565. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
  31566. !defined(NO_WOLFSSL_SERVER)
  31567. tcp_ready ready;
  31568. func_args client_args;
  31569. func_args server_args;
  31570. #ifndef SINGLE_THREADED
  31571. THREAD_TYPE serverThread;
  31572. #endif
  31573. callback_functions client_cb;
  31574. callback_functions server_cb;
  31575. /* create a failed connection and inspect the error */
  31576. #ifdef WOLFSSL_TIRTOS
  31577. fdOpenSession(Task_self());
  31578. #endif
  31579. XMEMSET(&client_args, 0, sizeof(func_args));
  31580. XMEMSET(&server_args, 0, sizeof(func_args));
  31581. StartTCP();
  31582. InitTcpReady(&ready);
  31583. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  31584. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  31585. #ifndef WOLFSSL_NO_TLS12
  31586. client_cb.method = wolfTLSv1_2_client_method;
  31587. server_cb.method = wolfTLSv1_2_server_method;
  31588. #else
  31589. client_cb.method = wolfTLSv1_3_client_method;
  31590. server_cb.method = wolfTLSv1_3_server_method;
  31591. #endif
  31592. server_args.signal = &ready;
  31593. server_args.callbacks = &server_cb;
  31594. client_args.signal = &ready;
  31595. client_args.callbacks = &client_cb;
  31596. client_args.return_code = TEST_FAIL;
  31597. #ifndef SINGLE_THREADED
  31598. start_thread(test_server_nofail, &server_args, &serverThread);
  31599. wait_tcp_ready(&server_args);
  31600. test_client_nofail(&client_args, msgCb);
  31601. join_thread(serverThread);
  31602. #endif
  31603. FreeTcpReady(&ready);
  31604. #ifndef SINGLE_THREADED
  31605. AssertTrue(client_args.return_code);
  31606. AssertTrue(server_args.return_code);
  31607. #endif
  31608. #ifdef WOLFSSL_TIRTOS
  31609. fdOpenSession(Task_self());
  31610. #endif
  31611. res = TEST_RES_CHECK(1);
  31612. #endif
  31613. return res;
  31614. }
  31615. static int test_wolfSSL_either_side(void)
  31616. {
  31617. int res = TEST_SKIPPED;
  31618. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  31619. !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  31620. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  31621. tcp_ready ready;
  31622. func_args client_args;
  31623. func_args server_args;
  31624. #ifndef SINGLE_THREADED
  31625. THREAD_TYPE serverThread;
  31626. #endif
  31627. callback_functions client_cb;
  31628. callback_functions server_cb;
  31629. /* create a failed connection and inspect the error */
  31630. #ifdef WOLFSSL_TIRTOS
  31631. fdOpenSession(Task_self());
  31632. #endif
  31633. XMEMSET(&client_args, 0, sizeof(func_args));
  31634. XMEMSET(&server_args, 0, sizeof(func_args));
  31635. StartTCP();
  31636. InitTcpReady(&ready);
  31637. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  31638. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  31639. /* Use different CTX for client and server */
  31640. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  31641. AssertNotNull(client_cb.ctx);
  31642. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  31643. AssertNotNull(server_cb.ctx);
  31644. /* we are responsible for free'ing WOLFSSL_CTX */
  31645. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  31646. server_args.signal = &ready;
  31647. server_args.callbacks = &server_cb;
  31648. client_args.signal = &ready;
  31649. client_args.callbacks = &client_cb;
  31650. client_args.return_code = TEST_FAIL;
  31651. #ifndef SINGLE_THREADED
  31652. start_thread(test_server_nofail, &server_args, &serverThread);
  31653. wait_tcp_ready(&server_args);
  31654. test_client_nofail(&client_args, NULL);
  31655. join_thread(serverThread);
  31656. #endif
  31657. wolfSSL_CTX_free(client_cb.ctx);
  31658. wolfSSL_CTX_free(server_cb.ctx);
  31659. FreeTcpReady(&ready);
  31660. #ifndef SINGLE_THREADED
  31661. AssertTrue(client_args.return_code);
  31662. AssertTrue(server_args.return_code);
  31663. #endif
  31664. #ifdef WOLFSSL_TIRTOS
  31665. fdOpenSession(Task_self());
  31666. #endif
  31667. res = TEST_RES_CHECK(1);
  31668. #endif
  31669. return res;
  31670. }
  31671. static int test_wolfSSL_DTLS_either_side(void)
  31672. {
  31673. int res = TEST_SKIPPED;
  31674. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  31675. !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  31676. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  31677. defined(WOLFSSL_DTLS)
  31678. tcp_ready ready;
  31679. func_args client_args;
  31680. func_args server_args;
  31681. #ifndef SINGLE_THREADED
  31682. THREAD_TYPE serverThread;
  31683. #endif
  31684. callback_functions client_cb;
  31685. callback_functions server_cb;
  31686. /* create a failed connection and inspect the error */
  31687. #ifdef WOLFSSL_TIRTOS
  31688. fdOpenSession(Task_self());
  31689. #endif
  31690. XMEMSET(&client_args, 0, sizeof(func_args));
  31691. XMEMSET(&server_args, 0, sizeof(func_args));
  31692. StartTCP();
  31693. InitTcpReady(&ready);
  31694. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  31695. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  31696. /* Use different CTX for client and server */
  31697. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  31698. AssertNotNull(client_cb.ctx);
  31699. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  31700. AssertNotNull(server_cb.ctx);
  31701. /* we are responsible for free'ing WOLFSSL_CTX */
  31702. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  31703. server_args.signal = &ready;
  31704. server_args.callbacks = &server_cb;
  31705. client_args.signal = &ready;
  31706. client_args.callbacks = &client_cb;
  31707. client_args.return_code = TEST_FAIL;
  31708. #ifndef SINGLE_THREADED
  31709. start_thread(test_server_nofail, &server_args, &serverThread);
  31710. wait_tcp_ready(&server_args);
  31711. test_client_nofail(&client_args, NULL);
  31712. join_thread(serverThread);
  31713. #endif
  31714. wolfSSL_CTX_free(client_cb.ctx);
  31715. wolfSSL_CTX_free(server_cb.ctx);
  31716. FreeTcpReady(&ready);
  31717. #ifndef SINGLE_THREADED
  31718. AssertTrue(client_args.return_code);
  31719. AssertTrue(server_args.return_code);
  31720. #endif
  31721. #ifdef WOLFSSL_TIRTOS
  31722. fdOpenSession(Task_self());
  31723. #endif
  31724. res = TEST_RES_CHECK(1);
  31725. #endif
  31726. return res;
  31727. }
  31728. static int test_generate_cookie(void)
  31729. {
  31730. int res = TEST_SKIPPED;
  31731. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  31732. SSL_CTX* ctx;
  31733. SSL* ssl;
  31734. byte buf[FOURK_BUF] = {0};
  31735. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  31736. AssertNotNull(ssl = SSL_new(ctx));
  31737. /* Test unconnected */
  31738. AssertIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  31739. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  31740. wolfSSL_SetCookieCtx(ssl, ctx);
  31741. AssertNotNull(wolfSSL_GetCookieCtx(ssl));
  31742. AssertNull(wolfSSL_GetCookieCtx(NULL));
  31743. SSL_free(ssl);
  31744. SSL_CTX_free(ctx);
  31745. res = TEST_RES_CHECK(1);
  31746. #endif
  31747. return res;
  31748. }
  31749. static int test_wolfSSL_set_options(void)
  31750. {
  31751. int res = TEST_SKIPPED;
  31752. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31753. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31754. WOLFSSL* ssl;
  31755. WOLFSSL_CTX* ctx;
  31756. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  31757. char appData[] = "extra msg";
  31758. #endif
  31759. #ifdef OPENSSL_EXTRA
  31760. unsigned char protos[] = {
  31761. 7, 't', 'l', 's', '/', '1', '.', '2',
  31762. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  31763. };
  31764. unsigned int len = sizeof(protos);
  31765. void *arg = (void *)TEST_ARG;
  31766. #endif
  31767. #ifndef NO_WOLFSSL_SERVER
  31768. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  31769. #else
  31770. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  31771. #endif
  31772. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  31773. WOLFSSL_FILETYPE_PEM));
  31774. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  31775. WOLFSSL_FILETYPE_PEM));
  31776. AssertTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
  31777. == WOLFSSL_OP_NO_TLSv1);
  31778. AssertTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
  31779. AssertIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
  31780. WOLFSSL_OP_NO_SSLv2)), 0);
  31781. AssertTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
  31782. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  31783. AssertTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
  31784. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  31785. AssertTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  31786. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  31787. AssertFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  31788. WOLFSSL_OP_NO_COMPRESSION));
  31789. wolfSSL_CTX_free(ctx);
  31790. #ifndef NO_WOLFSSL_SERVER
  31791. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  31792. AssertNotNull(ctx);
  31793. #else
  31794. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  31795. AssertNotNull(ctx);
  31796. #endif
  31797. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  31798. WOLFSSL_FILETYPE_PEM));
  31799. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  31800. WOLFSSL_FILETYPE_PEM));
  31801. #ifdef OPENSSL_EXTRA
  31802. AssertTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
  31803. #endif
  31804. AssertNotNull(ssl = wolfSSL_new(ctx));
  31805. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  31806. #ifdef HAVE_EX_DATA
  31807. AssertIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
  31808. AssertNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  31809. if (ssl) {
  31810. AssertIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
  31811. appData, sizeof(appData)), 0);
  31812. }
  31813. #else
  31814. AssertIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
  31815. AssertNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  31816. #endif
  31817. #endif
  31818. AssertTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
  31819. WOLFSSL_OP_NO_TLSv1);
  31820. AssertTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
  31821. AssertIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
  31822. WOLFSSL_OP_NO_SSLv2)), 0);
  31823. AssertTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
  31824. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  31825. AssertTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
  31826. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  31827. AssertTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  31828. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  31829. #ifdef OPENSSL_EXTRA
  31830. AssertFalse((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  31831. WOLFSSL_OP_NO_COMPRESSION));
  31832. #endif
  31833. #ifdef OPENSSL_EXTRA
  31834. AssertTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
  31835. wolfSSL_set_msg_callback_arg(ssl, arg);
  31836. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  31837. AssertTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  31838. #else
  31839. AssertTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
  31840. #endif
  31841. #endif
  31842. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  31843. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  31844. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  31845. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  31846. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  31847. AssertTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
  31848. #else
  31849. AssertTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
  31850. #endif
  31851. #endif /* HAVE_ALPN && !NO_BIO */
  31852. #endif
  31853. wolfSSL_free(ssl);
  31854. wolfSSL_CTX_free(ctx);
  31855. res = TEST_RES_CHECK(1);
  31856. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31857. #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  31858. return res;
  31859. }
  31860. static int test_wolfSSL_sk_SSL_CIPHER(void)
  31861. {
  31862. int res = TEST_SKIPPED;
  31863. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  31864. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31865. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31866. SSL* ssl;
  31867. SSL_CTX* ctx;
  31868. STACK_OF(SSL_CIPHER) *sk, *dupSk;
  31869. #ifndef NO_WOLFSSL_SERVER
  31870. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31871. #else
  31872. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31873. #endif
  31874. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  31875. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  31876. AssertNotNull(ssl = SSL_new(ctx));
  31877. AssertNotNull(sk = SSL_get_ciphers(ssl));
  31878. AssertNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  31879. AssertIntGT(sk_SSL_CIPHER_num(sk), 0);
  31880. AssertIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  31881. /* error case because connection has not been established yet */
  31882. AssertIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  31883. sk_SSL_CIPHER_free(dupSk);
  31884. /* sk is pointer to internal struct that should be free'd in SSL_free */
  31885. SSL_free(ssl);
  31886. SSL_CTX_free(ctx);
  31887. res = TEST_RES_CHECK(1);
  31888. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31889. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31890. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  31891. return res;
  31892. }
  31893. static int test_wolfSSL_set1_curves_list(void)
  31894. {
  31895. int res = TEST_SKIPPED;
  31896. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  31897. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31898. SSL* ssl = NULL;
  31899. SSL_CTX* ctx = NULL;
  31900. #ifndef NO_WOLFSSL_SERVER
  31901. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31902. #else
  31903. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31904. #endif
  31905. AssertTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  31906. SSL_FILETYPE_PEM));
  31907. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
  31908. AssertNotNull(ssl = SSL_new(ctx));
  31909. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  31910. #ifdef HAVE_ECC
  31911. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  31912. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  31913. #endif
  31914. #ifdef HAVE_CURVE25519
  31915. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
  31916. #else
  31917. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
  31918. #endif
  31919. #ifdef HAVE_CURVE448
  31920. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
  31921. #else
  31922. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
  31923. #endif
  31924. AssertIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  31925. #ifdef HAVE_ECC
  31926. AssertIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  31927. AssertIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  31928. #endif
  31929. #ifdef HAVE_CURVE25519
  31930. AssertIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
  31931. #else
  31932. AssertIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
  31933. #endif
  31934. #ifdef HAVE_CURVE448
  31935. AssertIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
  31936. #else
  31937. AssertIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
  31938. #endif
  31939. SSL_free(ssl);
  31940. SSL_CTX_free(ctx);
  31941. res = TEST_RES_CHECK(1);
  31942. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31943. #endif
  31944. return res;
  31945. }
  31946. static int test_wolfSSL_set1_sigalgs_list(void)
  31947. {
  31948. int res = TEST_SKIPPED;
  31949. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  31950. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31951. SSL* ssl;
  31952. SSL_CTX* ctx;
  31953. #ifndef NO_WOLFSSL_SERVER
  31954. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31955. #else
  31956. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31957. #endif
  31958. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  31959. SSL_FILETYPE_PEM));
  31960. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  31961. AssertNotNull(ssl = SSL_new(ctx));
  31962. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  31963. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  31964. AssertIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  31965. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  31966. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  31967. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  31968. #ifndef NO_RSA
  31969. #ifndef NO_SHA256
  31970. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  31971. WOLFSSL_FAILURE);
  31972. AssertIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  31973. WOLFSSL_FAILURE);
  31974. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  31975. WOLFSSL_SUCCESS);
  31976. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  31977. WOLFSSL_SUCCESS);
  31978. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  31979. WOLFSSL_FAILURE);
  31980. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  31981. WOLFSSL_FAILURE);
  31982. #ifdef WC_RSA_PSS
  31983. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  31984. WOLFSSL_SUCCESS);
  31985. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  31986. WOLFSSL_SUCCESS);
  31987. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  31988. WOLFSSL_SUCCESS);
  31989. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  31990. WOLFSSL_SUCCESS);
  31991. #endif
  31992. #ifdef WOLFSSL_SHA512
  31993. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  31994. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  31995. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  31996. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  31997. #elif defined(WOLFSSL_SHA384)
  31998. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  31999. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  32000. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  32001. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  32002. #endif
  32003. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  32004. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  32005. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  32006. WOLFSSL_FAILURE);
  32007. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  32008. WOLFSSL_FAILURE);
  32009. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  32010. WOLFSSL_FAILURE);
  32011. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  32012. WOLFSSL_FAILURE);
  32013. #endif
  32014. #endif
  32015. #ifdef HAVE_ECC
  32016. #ifndef NO_SHA256
  32017. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  32018. WOLFSSL_SUCCESS);
  32019. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"), WOLFSSL_SUCCESS);
  32020. #ifdef WOLFSSL_SHA512
  32021. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  32022. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  32023. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  32024. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  32025. #elif defined(WOLFSSL_SHA384)
  32026. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  32027. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  32028. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  32029. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  32030. #endif
  32031. #endif
  32032. #endif
  32033. #ifdef HAVE_ED25519
  32034. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  32035. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  32036. #endif
  32037. #ifdef HAVE_ED448
  32038. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  32039. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  32040. #endif
  32041. #ifndef NO_DSA
  32042. #ifndef NO_SHA256
  32043. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  32044. WOLFSSL_SUCCESS);
  32045. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  32046. WOLFSSL_SUCCESS);
  32047. #endif
  32048. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  32049. defined(WOLFSSL_ALLOW_TLS_SHA1))
  32050. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  32051. WOLFSSL_SUCCESS);
  32052. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  32053. WOLFSSL_SUCCESS);
  32054. #endif
  32055. #endif
  32056. SSL_free(ssl);
  32057. SSL_CTX_free(ctx);
  32058. res = TEST_RES_CHECK(1);
  32059. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32060. #endif
  32061. return res;
  32062. }
  32063. /* Testing wolfSSL_set_tlsext_status_type function.
  32064. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  32065. */
  32066. static int test_wolfSSL_set_tlsext_status_type(void)
  32067. {
  32068. int res = TEST_SKIPPED;
  32069. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  32070. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  32071. SSL* ssl;
  32072. SSL_CTX* ctx;
  32073. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32074. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  32075. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  32076. AssertNotNull(ssl = SSL_new(ctx));
  32077. AssertIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  32078. SSL_SUCCESS);
  32079. AssertIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  32080. SSL_free(ssl);
  32081. SSL_CTX_free(ctx);
  32082. res = TEST_RES_CHECK(1);
  32083. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  32084. return res;
  32085. }
  32086. #ifndef NO_BIO
  32087. static int test_wolfSSL_PEM_read_bio(void)
  32088. {
  32089. int res = TEST_SKIPPED;
  32090. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32091. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32092. byte buff[6000];
  32093. XFILE f;
  32094. int bytes;
  32095. X509* x509;
  32096. BIO* bio = NULL;
  32097. BUF_MEM* buf;
  32098. f = XFOPEN(cliCertFile, "rb");
  32099. AssertTrue((f != XBADFILE));
  32100. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  32101. XFCLOSE(f);
  32102. AssertNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  32103. AssertNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  32104. AssertIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  32105. AssertNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  32106. AssertIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  32107. /* BIO should return the set EOF value */
  32108. AssertIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  32109. AssertIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  32110. AssertIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  32111. AssertIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  32112. BIO_free(bio);
  32113. BUF_MEM_free(buf);
  32114. X509_free(x509);
  32115. res = TEST_RES_CHECK(1);
  32116. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32117. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  32118. return res;
  32119. }
  32120. #if defined(OPENSSL_EXTRA)
  32121. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  32122. long argl, long ret)
  32123. {
  32124. (void)bio;
  32125. (void)cmd;
  32126. (void)argp;
  32127. (void)argi;
  32128. (void)argl;
  32129. return ret;
  32130. }
  32131. #endif
  32132. static int test_wolfSSL_BIO(void)
  32133. {
  32134. int res = TEST_SKIPPED;
  32135. #if defined(OPENSSL_EXTRA)
  32136. const unsigned char* p;
  32137. byte buff[20];
  32138. BIO* bio1;
  32139. BIO* bio2;
  32140. BIO* bio3;
  32141. char* bufPt;
  32142. int i;
  32143. for (i = 0; i < 20; i++) {
  32144. buff[i] = i;
  32145. }
  32146. /* test BIO_free with NULL */
  32147. AssertIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  32148. /* Creating and testing type BIO_s_bio */
  32149. AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
  32150. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  32151. AssertNotNull(bio3 = BIO_new(BIO_s_bio()));
  32152. /* read/write before set up */
  32153. AssertIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  32154. AssertIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  32155. AssertIntEQ(BIO_set_nbio(bio1, 1), 1);
  32156. AssertIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  32157. AssertIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  32158. AssertIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  32159. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  32160. XMEMCPY(bufPt, buff, 10);
  32161. AssertIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  32162. /* write buffer full */
  32163. AssertIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  32164. AssertIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  32165. AssertIntEQ((int)BIO_ctrl_pending(bio1), 0);
  32166. /* write the other direction with pair */
  32167. AssertIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  32168. XMEMCPY(bufPt, buff, 8);
  32169. AssertIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  32170. /* try read */
  32171. AssertIntEQ((int)BIO_ctrl_pending(bio1), 8);
  32172. AssertIntEQ((int)BIO_ctrl_pending(bio2), 20);
  32173. /* try read using ctrl function */
  32174. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  32175. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  32176. AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  32177. AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  32178. AssertIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  32179. for (i = 0; i < 20; i++) {
  32180. AssertIntEQ((int)bufPt[i], i);
  32181. }
  32182. AssertIntEQ(BIO_nread(bio2, &bufPt, 1), WOLFSSL_BIO_ERROR);
  32183. AssertIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  32184. for (i = 0; i < 8; i++) {
  32185. AssertIntEQ((int)bufPt[i], i);
  32186. }
  32187. AssertIntEQ(BIO_nread(bio1, &bufPt, 1), WOLFSSL_BIO_ERROR);
  32188. AssertIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  32189. /* new pair */
  32190. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  32191. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  32192. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  32193. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  32194. AssertIntEQ(BIO_nread(bio3, &bufPt, 10), WOLFSSL_BIO_ERROR);
  32195. /* test wrap around... */
  32196. AssertIntEQ(BIO_reset(bio1), 0);
  32197. AssertIntEQ(BIO_reset(bio3), 0);
  32198. /* fill write buffer, read only small amount then write again */
  32199. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  32200. XMEMCPY(bufPt, buff, 20);
  32201. AssertIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  32202. for (i = 0; i < 4; i++) {
  32203. AssertIntEQ(bufPt[i], i);
  32204. }
  32205. /* try writing over read index */
  32206. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  32207. XMEMSET(bufPt, 0, 4);
  32208. AssertIntEQ((int)BIO_ctrl_pending(bio3), 20);
  32209. /* read and write 0 bytes */
  32210. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  32211. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  32212. /* should read only to end of write buffer then need to read again */
  32213. AssertIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  32214. for (i = 0; i < 16; i++) {
  32215. AssertIntEQ(bufPt[i], buff[4 + i]);
  32216. }
  32217. AssertIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  32218. AssertIntEQ(BIO_nread0(bio3, &bufPt), 4);
  32219. for (i = 0; i < 4; i++) {
  32220. AssertIntEQ(bufPt[i], 0);
  32221. }
  32222. /* read index should not have advanced with nread0 */
  32223. AssertIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  32224. for (i = 0; i < 4; i++) {
  32225. AssertIntEQ(bufPt[i], 0);
  32226. }
  32227. /* write and fill up buffer checking reset of index state */
  32228. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  32229. XMEMCPY(bufPt, buff, 20);
  32230. /* test reset on data in bio1 write buffer */
  32231. AssertIntEQ(BIO_reset(bio1), 0);
  32232. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  32233. AssertIntEQ(BIO_nread(bio3, &bufPt, 3), WOLFSSL_BIO_ERROR);
  32234. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  32235. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  32236. AssertNotNull(p);
  32237. XMEMCPY(bufPt, buff, 20);
  32238. AssertIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  32239. for (i = 0; i < 6; i++) {
  32240. AssertIntEQ(bufPt[i], i);
  32241. }
  32242. /* test case of writing twice with offset read index */
  32243. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  32244. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  32245. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  32246. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  32247. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  32248. AssertIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  32249. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  32250. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  32251. BIO_free(bio1);
  32252. BIO_free(bio3);
  32253. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  32254. {
  32255. BIO* bioA = NULL;
  32256. BIO* bioB = NULL;
  32257. AssertIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  32258. AssertIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  32259. BIO_free(bioA);
  32260. bioA = NULL;
  32261. BIO_free(bioB);
  32262. bioB = NULL;
  32263. }
  32264. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  32265. /* BIOs with file pointers */
  32266. #if !defined(NO_FILESYSTEM)
  32267. {
  32268. XFILE f1;
  32269. XFILE f2;
  32270. BIO* f_bio1;
  32271. BIO* f_bio2;
  32272. unsigned char cert[300];
  32273. char testFile[] = "tests/bio_write_test.txt";
  32274. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  32275. AssertNotNull(f_bio1 = BIO_new(BIO_s_file()));
  32276. AssertNotNull(f_bio2 = BIO_new(BIO_s_file()));
  32277. /* Failure due to wrong BIO type */
  32278. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  32279. AssertIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  32280. f1 = XFOPEN(svrCertFile, "rwb");
  32281. AssertTrue((f1 != XBADFILE));
  32282. AssertIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  32283. AssertIntEQ(BIO_write_filename(f_bio2, testFile),
  32284. WOLFSSL_SUCCESS);
  32285. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  32286. AssertIntEQ(BIO_tell(f_bio1),sizeof(cert));
  32287. AssertIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  32288. AssertIntEQ(BIO_tell(f_bio2),sizeof(msg));
  32289. AssertIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  32290. AssertIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  32291. AssertIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  32292. AssertIntEQ(BIO_reset(f_bio2), 0);
  32293. AssertIntEQ(BIO_tell(NULL),-1);
  32294. AssertIntEQ(BIO_tell(f_bio2),0);
  32295. AssertIntEQ(BIO_seek(f_bio2, 4), 0);
  32296. AssertIntEQ(BIO_tell(f_bio2),4);
  32297. BIO_free(f_bio1);
  32298. BIO_free(f_bio2);
  32299. AssertNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  32300. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  32301. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  32302. BIO_free(f_bio1);
  32303. }
  32304. #endif /* !defined(NO_FILESYSTEM) */
  32305. /* BIO info callback */
  32306. {
  32307. const char* testArg = "test";
  32308. BIO* cb_bio;
  32309. AssertNotNull(cb_bio = BIO_new(BIO_s_mem()));
  32310. BIO_set_callback(cb_bio, bioCallback);
  32311. AssertNotNull(BIO_get_callback(cb_bio));
  32312. BIO_set_callback(cb_bio, NULL);
  32313. AssertNull(BIO_get_callback(cb_bio));
  32314. BIO_set_callback_arg(cb_bio, (char*)testArg);
  32315. AssertStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  32316. AssertNull(BIO_get_callback_arg(NULL));
  32317. BIO_free(cb_bio);
  32318. }
  32319. /* BIO_vfree */
  32320. AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
  32321. BIO_vfree(NULL);
  32322. BIO_vfree(bio1);
  32323. res = TEST_RES_CHECK(1);
  32324. #endif
  32325. return res;
  32326. }
  32327. #endif /* !NO_BIO */
  32328. static int test_wolfSSL_ASN1_STRING(void)
  32329. {
  32330. int res = TEST_SKIPPED;
  32331. #if defined(OPENSSL_EXTRA)
  32332. ASN1_STRING* str = NULL;
  32333. const char data[] = "hello wolfSSL";
  32334. AssertNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  32335. AssertIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  32336. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  32337. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  32338. AssertIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  32339. ASN1_STRING_free(str);
  32340. res = TEST_RES_CHECK(1);
  32341. #endif
  32342. return res;
  32343. }
  32344. static int test_wolfSSL_ASN1_BIT_STRING(void)
  32345. {
  32346. int res = TEST_SKIPPED;
  32347. #ifdef OPENSSL_ALL
  32348. ASN1_BIT_STRING* str;
  32349. AssertNotNull(str = ASN1_BIT_STRING_new());
  32350. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  32351. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  32352. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  32353. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  32354. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  32355. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  32356. ASN1_BIT_STRING_free(str);
  32357. res = TEST_RES_CHECK(1);
  32358. #endif
  32359. return res;
  32360. }
  32361. static int test_wolfSSL_a2i_ASN1_INTEGER(void)
  32362. {
  32363. int res = TEST_SKIPPED;
  32364. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  32365. BIO *bio, *out;
  32366. ASN1_INTEGER* ai;
  32367. char buf[] = "123456\n12345\n112345678912345678901234567890\n";
  32368. char tmp[1024];
  32369. int tmpSz;
  32370. const char expected1[] = "123456";
  32371. const char expected2[] = "112345678912345678901234567890";
  32372. AssertNotNull(bio = BIO_new_mem_buf(buf, -1));
  32373. AssertNotNull(out = BIO_new(BIO_s_mem()));
  32374. AssertNotNull(ai = ASN1_INTEGER_new());
  32375. /* read first line */
  32376. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
  32377. AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  32378. XMEMSET(tmp, 0, 1024);
  32379. tmpSz = BIO_read(out, tmp, 1024);
  32380. AssertIntEQ(tmpSz, 6);
  32381. AssertIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  32382. /* fail on second line (not % 2) */
  32383. AssertIntNE(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
  32384. /* read 3rd long line */
  32385. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
  32386. AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  32387. XMEMSET(tmp, 0, 1024);
  32388. tmpSz = BIO_read(out, tmp, 1024);
  32389. AssertIntEQ(tmpSz, 30);
  32390. AssertIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  32391. BIO_free(out);
  32392. BIO_free(bio);
  32393. ASN1_INTEGER_free(ai);
  32394. res = TEST_RES_CHECK(1);
  32395. #endif
  32396. return res;
  32397. }
  32398. static int test_wolfSSL_a2i_IPADDRESS(void)
  32399. {
  32400. int res = TEST_SKIPPED;
  32401. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  32402. const unsigned char* data;
  32403. int dataSz = 0;
  32404. ASN1_OCTET_STRING *st;
  32405. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  32406. const unsigned char ipv6_exp[] = {
  32407. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  32408. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  32409. };
  32410. const unsigned char ipv6_home[] = {
  32411. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32412. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  32413. };
  32414. AssertNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  32415. AssertNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  32416. data = ASN1_STRING_get0_data(st);
  32417. dataSz = ASN1_STRING_length(st);
  32418. AssertIntEQ(dataSz, WOLFSSL_IP4_ADDR_LEN);
  32419. AssertIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  32420. ASN1_STRING_free(st);
  32421. AssertNotNull(st = a2i_IPADDRESS("::1"));
  32422. data = ASN1_STRING_get0_data(st);
  32423. dataSz = ASN1_STRING_length(st);
  32424. AssertIntEQ(dataSz, WOLFSSL_IP6_ADDR_LEN);
  32425. AssertIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  32426. ASN1_STRING_free(st);
  32427. AssertNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  32428. data = ASN1_STRING_get0_data(st);
  32429. dataSz = ASN1_STRING_length(st);
  32430. AssertIntEQ(dataSz, WOLFSSL_IP6_ADDR_LEN);
  32431. AssertIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  32432. ASN1_STRING_free(st);
  32433. res = TEST_RES_CHECK(1);
  32434. #endif
  32435. return res;
  32436. }
  32437. static int test_wolfSSL_DES_ecb_encrypt(void)
  32438. {
  32439. int res = TEST_SKIPPED;
  32440. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  32441. WOLFSSL_DES_cblock input1,input2,output1,output2,back1,back2;
  32442. WOLFSSL_DES_key_schedule key;
  32443. XMEMCPY(key,"12345678",sizeof(WOLFSSL_DES_key_schedule));
  32444. XMEMCPY(input1, "Iamhuman",sizeof(WOLFSSL_DES_cblock));
  32445. XMEMCPY(input2, "Whoisit?",sizeof(WOLFSSL_DES_cblock));
  32446. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  32447. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  32448. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  32449. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  32450. /* Encrypt messages */
  32451. wolfSSL_DES_ecb_encrypt(&input1,&output1,&key,DES_ENCRYPT);
  32452. wolfSSL_DES_ecb_encrypt(&input2,&output2,&key,DES_ENCRYPT);
  32453. {
  32454. /* Decrypt messages */
  32455. int ret1 = 0;
  32456. int ret2 = 0;
  32457. wolfSSL_DES_ecb_encrypt(&output1,&back1,&key,DES_DECRYPT);
  32458. ret1 = XMEMCMP((unsigned char *) back1,(unsigned char *) input1,sizeof(WOLFSSL_DES_cblock));
  32459. AssertIntEQ(ret1,0);
  32460. wolfSSL_DES_ecb_encrypt(&output2,&back2,&key,DES_DECRYPT);
  32461. ret2 = XMEMCMP((unsigned char *) back2,(unsigned char *) input2,sizeof(WOLFSSL_DES_cblock));
  32462. AssertIntEQ(ret2,0);
  32463. }
  32464. res = TEST_RES_CHECK(1);
  32465. #endif
  32466. return res;
  32467. }
  32468. static int test_wolfSSL_ASN1_TIME_adj(void)
  32469. {
  32470. int res = TEST_SKIPPED;
  32471. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  32472. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  32473. const int year = 365*24*60*60;
  32474. const int day = 24*60*60;
  32475. const int hour = 60*60;
  32476. const int mini = 60;
  32477. const byte asn_utc_time = ASN_UTC_TIME;
  32478. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  32479. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  32480. #endif
  32481. WOLFSSL_ASN1_TIME *asn_time, *s;
  32482. int offset_day;
  32483. long offset_sec;
  32484. char date_str[CTC_DATE_SIZE + 1];
  32485. time_t t;
  32486. AssertNotNull(s = wolfSSL_ASN1_TIME_new());
  32487. /* UTC notation test */
  32488. /* 2000/2/15 20:30:00 */
  32489. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  32490. offset_day = 7;
  32491. offset_sec = 45 * mini;
  32492. /* offset_sec = -45 * min;*/
  32493. AssertNotNull(asn_time =
  32494. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  32495. AssertTrue(asn_time->type == asn_utc_time);
  32496. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  32497. date_str[CTC_DATE_SIZE] = '\0';
  32498. AssertIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  32499. /* negative offset */
  32500. offset_sec = -45 * mini;
  32501. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  32502. AssertNotNull(asn_time);
  32503. AssertTrue(asn_time->type == asn_utc_time);
  32504. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  32505. date_str[CTC_DATE_SIZE] = '\0';
  32506. AssertIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  32507. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  32508. XMEMSET(date_str, 0, sizeof(date_str));
  32509. /* Generalized time will overflow time_t if not long */
  32510. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  32511. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  32512. DYNAMIC_TYPE_OPENSSL);
  32513. /* GeneralizedTime notation test */
  32514. /* 2055/03/01 09:00:00 */
  32515. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  32516. offset_day = 12;
  32517. offset_sec = 10 * mini;
  32518. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  32519. AssertTrue(asn_time->type == asn_gen_time);
  32520. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  32521. date_str[CTC_DATE_SIZE] = '\0';
  32522. AssertIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  32523. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  32524. XMEMSET(date_str, 0, sizeof(date_str));
  32525. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  32526. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  32527. s = NULL;
  32528. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  32529. offset_day = 7;
  32530. offset_sec = 45 * mini;
  32531. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  32532. AssertTrue(asn_time->type == asn_utc_time);
  32533. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  32534. date_str[CTC_DATE_SIZE] = '\0';
  32535. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  32536. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  32537. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, offset_sec);
  32538. AssertTrue(asn_time->type == asn_utc_time);
  32539. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  32540. date_str[CTC_DATE_SIZE] = '\0';
  32541. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  32542. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  32543. res = TEST_RES_CHECK(1);
  32544. #endif
  32545. return res;
  32546. }
  32547. static int test_wolfSSL_ASN1_TIME_to_tm(void)
  32548. {
  32549. int res = TEST_SKIPPED;
  32550. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  32551. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) \
  32552. && !defined(NO_ASN_TIME)
  32553. ASN1_TIME asnTime;
  32554. struct tm tm;
  32555. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  32556. AssertIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  32557. AssertIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  32558. AssertIntEQ(tm.tm_sec, 15);
  32559. AssertIntEQ(tm.tm_min, 15);
  32560. AssertIntEQ(tm.tm_hour, 21);
  32561. AssertIntEQ(tm.tm_mday, 22);
  32562. AssertIntEQ(tm.tm_mon, 1);
  32563. AssertIntEQ(tm.tm_year, 100);
  32564. AssertIntEQ(tm.tm_isdst, 0);
  32565. #ifdef XMKTIME
  32566. AssertIntEQ(tm.tm_wday, 2);
  32567. AssertIntEQ(tm.tm_yday, 52);
  32568. #endif
  32569. res = TEST_RES_CHECK(1);
  32570. #endif
  32571. return res;
  32572. }
  32573. static int test_wolfSSL_X509_cmp_time(void)
  32574. {
  32575. int res = TEST_SKIPPED;
  32576. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  32577. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  32578. WOLFSSL_ASN1_TIME asn_time;
  32579. time_t t;
  32580. AssertIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  32581. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  32582. AssertIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  32583. AssertIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  32584. AssertIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  32585. res = TEST_RES_CHECK(1);
  32586. #endif
  32587. return res;
  32588. }
  32589. static int test_wolfSSL_X509_time_adj(void)
  32590. {
  32591. int res = TEST_SKIPPED;
  32592. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  32593. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  32594. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  32595. !defined(NO_ASN_TIME)
  32596. X509* x509;
  32597. time_t t, not_before, not_after;
  32598. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  32599. client_cert_der_2048, sizeof_client_cert_der_2048,
  32600. WOLFSSL_FILETYPE_ASN1));
  32601. t = 0;
  32602. not_before = wc_Time(0);
  32603. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  32604. AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  32605. AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  32606. /* Check X509_gmtime_adj, too. */
  32607. AssertNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  32608. X509_free(x509);
  32609. res = TEST_RES_CHECK(1);
  32610. #endif
  32611. return res;
  32612. }
  32613. static int test_wolfSSL_X509(void)
  32614. {
  32615. int res = TEST_SKIPPED;
  32616. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)\
  32617. && !defined(NO_RSA)
  32618. X509* x509;
  32619. #ifndef NO_BIO
  32620. BIO* bio;
  32621. X509_STORE_CTX* ctx;
  32622. X509_STORE* store;
  32623. #endif
  32624. char der[] = "certs/ca-cert.der";
  32625. XFILE fp;
  32626. AssertNotNull(x509 = X509_new());
  32627. X509_free(x509);
  32628. #ifndef NO_BIO
  32629. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
  32630. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  32631. #ifdef WOLFSSL_CERT_GEN
  32632. AssertIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  32633. #endif
  32634. AssertNotNull(ctx = X509_STORE_CTX_new());
  32635. AssertIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  32636. AssertNotNull(store = X509_STORE_new());
  32637. AssertIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  32638. AssertIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  32639. AssertIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  32640. X509_STORE_CTX_free(ctx);
  32641. X509_STORE_free(store);
  32642. X509_free(x509);
  32643. BIO_free(bio);
  32644. #endif
  32645. /** d2i_X509_fp test **/
  32646. fp = XFOPEN(der, "rb");
  32647. AssertTrue((fp != XBADFILE));
  32648. AssertNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  32649. AssertNotNull(x509);
  32650. X509_free(x509);
  32651. XFCLOSE(fp);
  32652. fp = XFOPEN(der, "rb");
  32653. AssertTrue((fp != XBADFILE));
  32654. AssertNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  32655. AssertNotNull(x509);
  32656. X509_free(x509);
  32657. XFCLOSE(fp);
  32658. /* X509_up_ref test */
  32659. AssertIntEQ(X509_up_ref(NULL), 0);
  32660. AssertNotNull(x509 = X509_new()); /* refCount = 1 */
  32661. AssertIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  32662. AssertIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  32663. X509_free(x509); /* refCount = 2 */
  32664. X509_free(x509); /* refCount = 1 */
  32665. X509_free(x509); /* refCount = 0, free */
  32666. res = TEST_RES_CHECK(1);
  32667. #endif
  32668. return res;
  32669. }
  32670. static int test_wolfSSL_X509_get_ext_count(void)
  32671. {
  32672. int res = TEST_SKIPPED;
  32673. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  32674. !defined(NO_RSA)
  32675. int ret = 0;
  32676. WOLFSSL_X509* x509;
  32677. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  32678. FILE* f;
  32679. /* NULL parameter check */
  32680. AssertIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  32681. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  32682. SSL_FILETYPE_PEM));
  32683. AssertIntEQ(X509_get_ext_count(x509), 5);
  32684. wolfSSL_X509_free(x509);
  32685. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  32686. SSL_FILETYPE_PEM));
  32687. AssertIntEQ(X509_get_ext_count(x509), 5);
  32688. wolfSSL_X509_free(x509);
  32689. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  32690. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  32691. fclose(f);
  32692. /* wolfSSL_X509_get_ext_count() valid input */
  32693. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  32694. /* wolfSSL_X509_get_ext_count() NULL argument */
  32695. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  32696. wolfSSL_X509_free(x509);
  32697. res = TEST_RES_CHECK(1);
  32698. #endif
  32699. return res;
  32700. }
  32701. static int test_wolfSSL_X509_sign2(void)
  32702. {
  32703. int res = TEST_SKIPPED;
  32704. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  32705. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  32706. defined(WOLFSSL_CERT_EXT) && \
  32707. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  32708. WOLFSSL_X509 *x509, *ca;
  32709. const unsigned char *der;
  32710. const unsigned char *pt;
  32711. WOLFSSL_EVP_PKEY *priv;
  32712. WOLFSSL_X509_NAME *name;
  32713. WOLFSSL_ASN1_TIME *notBefore, *notAfter;
  32714. int derSz;
  32715. const int year = 365*24*60*60;
  32716. const int day = 24*60*60;
  32717. const int hour = 60*60;
  32718. const int mini = 60;
  32719. time_t t;
  32720. const unsigned char expected[] = {
  32721. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xfb, 0xa0, 0x03, 0x02, 0x01,
  32722. 0x02, 0x02, 0x14, 0x01, 0x1a, 0xeb, 0x56, 0xab, 0xdc, 0x8b, 0xf3, 0xa6,
  32723. 0x1e, 0xf4, 0x93, 0x60, 0x89, 0xb7, 0x05, 0x07, 0x29, 0x01, 0x2c, 0x30,
  32724. 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
  32725. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55,
  32726. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03,
  32727. 0x55, 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61,
  32728. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42,
  32729. 0x6f, 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x11, 0x30, 0x0f, 0x06, 0x03,
  32730. 0x55, 0x04, 0x0a, 0x0c, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6f, 0x6f, 0x74,
  32731. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0a,
  32732. 0x43, 0x6f, 0x6e, 0x73, 0x75, 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x31, 0x18,
  32733. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77, 0x77,
  32734. 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  32735. 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  32736. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f,
  32737. 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17,
  32738. 0x0d, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  32739. 0x30, 0x5a, 0x17, 0x0d, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  32740. 0x33, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09,
  32741. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  32742. 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74,
  32743. 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07,
  32744. 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x15, 0x30,
  32745. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0c, 0x77, 0x6f, 0x6c, 0x66,
  32746. 0x53, 0x53, 0x4c, 0x5f, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  32747. 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x10, 0x50, 0x72, 0x6f, 0x67, 0x72,
  32748. 0x61, 0x6d, 0x6d, 0x69, 0x6e, 0x67, 0x2d, 0x32, 0x30, 0x34, 0x38, 0x31,
  32749. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77,
  32750. 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f,
  32751. 0x6d, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
  32752. 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77,
  32753. 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x82,
  32754. 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  32755. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82,
  32756. 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xc3, 0x03, 0xd1, 0x2b, 0xfe,
  32757. 0x39, 0xa4, 0x32, 0x45, 0x3b, 0x53, 0xc8, 0x84, 0x2b, 0x2a, 0x7c, 0x74,
  32758. 0x9a, 0xbd, 0xaa, 0x2a, 0x52, 0x07, 0x47, 0xd6, 0xa6, 0x36, 0xb2, 0x07,
  32759. 0x32, 0x8e, 0xd0, 0xba, 0x69, 0x7b, 0xc6, 0xc3, 0x44, 0x9e, 0xd4, 0x81,
  32760. 0x48, 0xfd, 0x2d, 0x68, 0xa2, 0x8b, 0x67, 0xbb, 0xa1, 0x75, 0xc8, 0x36,
  32761. 0x2c, 0x4a, 0xd2, 0x1b, 0xf7, 0x8b, 0xba, 0xcf, 0x0d, 0xf9, 0xef, 0xec,
  32762. 0xf1, 0x81, 0x1e, 0x7b, 0x9b, 0x03, 0x47, 0x9a, 0xbf, 0x65, 0xcc, 0x7f,
  32763. 0x65, 0x24, 0x69, 0xa6, 0xe8, 0x14, 0x89, 0x5b, 0xe4, 0x34, 0xf7, 0xc5,
  32764. 0xb0, 0x14, 0x93, 0xf5, 0x67, 0x7b, 0x3a, 0x7a, 0x78, 0xe1, 0x01, 0x56,
  32765. 0x56, 0x91, 0xa6, 0x13, 0x42, 0x8d, 0xd2, 0x3c, 0x40, 0x9c, 0x4c, 0xef,
  32766. 0xd1, 0x86, 0xdf, 0x37, 0x51, 0x1b, 0x0c, 0xa1, 0x3b, 0xf5, 0xf1, 0xa3,
  32767. 0x4a, 0x35, 0xe4, 0xe1, 0xce, 0x96, 0xdf, 0x1b, 0x7e, 0xbf, 0x4e, 0x97,
  32768. 0xd0, 0x10, 0xe8, 0xa8, 0x08, 0x30, 0x81, 0xaf, 0x20, 0x0b, 0x43, 0x14,
  32769. 0xc5, 0x74, 0x67, 0xb4, 0x32, 0x82, 0x6f, 0x8d, 0x86, 0xc2, 0x88, 0x40,
  32770. 0x99, 0x36, 0x83, 0xba, 0x1e, 0x40, 0x72, 0x22, 0x17, 0xd7, 0x52, 0x65,
  32771. 0x24, 0x73, 0xb0, 0xce, 0xef, 0x19, 0xcd, 0xae, 0xff, 0x78, 0x6c, 0x7b,
  32772. 0xc0, 0x12, 0x03, 0xd4, 0x4e, 0x72, 0x0d, 0x50, 0x6d, 0x3b, 0xa3, 0x3b,
  32773. 0xa3, 0x99, 0x5e, 0x9d, 0xc8, 0xd9, 0x0c, 0x85, 0xb3, 0xd9, 0x8a, 0xd9,
  32774. 0x54, 0x26, 0xdb, 0x6d, 0xfa, 0xac, 0xbb, 0xff, 0x25, 0x4c, 0xc4, 0xd1,
  32775. 0x79, 0xf4, 0x71, 0xd3, 0x86, 0x40, 0x18, 0x13, 0xb0, 0x63, 0xb5, 0x72,
  32776. 0x4e, 0x30, 0xc4, 0x97, 0x84, 0x86, 0x2d, 0x56, 0x2f, 0xd7, 0x15, 0xf7,
  32777. 0x7f, 0xc0, 0xae, 0xf5, 0xfc, 0x5b, 0xe5, 0xfb, 0xa1, 0xba, 0xd3, 0x02,
  32778. 0x03, 0x01, 0x00, 0x01, 0xa3, 0x82, 0x01, 0x4f, 0x30, 0x82, 0x01, 0x4b,
  32779. 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  32780. 0x01, 0xff, 0x30, 0x1c, 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x15, 0x30,
  32781. 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63,
  32782. 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01, 0x30, 0x1d, 0x06, 0x03,
  32783. 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x33, 0xd8, 0x45, 0x66, 0xd7,
  32784. 0x68, 0x87, 0x18, 0x7e, 0x54, 0x0d, 0x70, 0x27, 0x91, 0xc7, 0x26, 0xd7,
  32785. 0x85, 0x65, 0xc0, 0x30, 0x81, 0xde, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04,
  32786. 0x81, 0xd6, 0x30, 0x81, 0xd3, 0x80, 0x14, 0x33, 0xd8, 0x45, 0x66, 0xd7,
  32787. 0x68, 0x87, 0x18, 0x7e, 0x54, 0x0d, 0x70, 0x27, 0x91, 0xc7, 0x26, 0xd7,
  32788. 0x85, 0x65, 0xc0, 0xa1, 0x81, 0xa4, 0xa4, 0x81, 0xa1, 0x30, 0x81, 0x9e,
  32789. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  32790. 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x07,
  32791. 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06,
  32792. 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d, 0x61,
  32793. 0x6e, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0c,
  32794. 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x5f, 0x32, 0x30, 0x34, 0x38,
  32795. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x10, 0x50,
  32796. 0x72, 0x6f, 0x67, 0x72, 0x61, 0x6d, 0x6d, 0x69, 0x6e, 0x67, 0x2d, 0x32,
  32797. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  32798. 0x0c, 0x0f, 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73,
  32799. 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a,
  32800. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e,
  32801. 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63,
  32802. 0x6f, 0x6d, 0x82, 0x14, 0x01, 0x1a, 0xeb, 0x56, 0xab, 0xdc, 0x8b, 0xf3,
  32803. 0xa6, 0x1e, 0xf4, 0x93, 0x60, 0x89, 0xb7, 0x05, 0x07, 0x29, 0x01, 0x2c,
  32804. 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  32805. 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2b,
  32806. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0d, 0x06, 0x09, 0x2a,
  32807. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82,
  32808. 0x01, 0x01, 0x00, 0xa3, 0x41, 0x43, 0x93, 0x30, 0x92, 0x98, 0xfe, 0x57,
  32809. 0xd0, 0x39, 0x7c, 0x50, 0x06, 0x50, 0x20, 0x80, 0x0e, 0x28, 0x95, 0x79,
  32810. 0xb4, 0xf1, 0x6b, 0x6a, 0xab, 0x78, 0x30, 0x93, 0x49, 0x0a, 0x6a, 0x19,
  32811. 0x09, 0xae, 0x31, 0xc6, 0x8e, 0xcc, 0x69, 0x26, 0x89, 0x37, 0xc1, 0x57,
  32812. 0x58, 0x75, 0xae, 0xbf, 0x13, 0xc8, 0xd6, 0xad, 0xd0, 0x0f, 0x57, 0xcd,
  32813. 0x32, 0xa8, 0xda, 0xa8, 0x1b, 0xbf, 0xb5, 0xcd, 0x16, 0x14, 0x56, 0x86,
  32814. 0x84, 0xb4, 0xab, 0x93, 0x52, 0x74, 0xfd, 0x96, 0x9f, 0x6d, 0xbe, 0xdb,
  32815. 0x75, 0x5e, 0x76, 0xfe, 0xa6, 0x37, 0xe5, 0x5f, 0xcb, 0x62, 0x77, 0xc7,
  32816. 0xd6, 0xcb, 0xb4, 0xf6, 0x43, 0xc8, 0x47, 0xdf, 0x12, 0x16, 0x28, 0x29,
  32817. 0x61, 0xd1, 0xdc, 0x9d, 0x37, 0x9f, 0xe5, 0x71, 0x52, 0xae, 0xb8, 0x12,
  32818. 0xec, 0x32, 0x9f, 0x03, 0x1a, 0x66, 0x98, 0xd8, 0xb0, 0x40, 0x71, 0x4c,
  32819. 0xee, 0x64, 0x15, 0x48, 0x0c, 0x5c, 0x8a, 0x47, 0x20, 0xbd, 0x07, 0xc0,
  32820. 0x30, 0xf8, 0x84, 0xe6, 0x29, 0x6d, 0xa9, 0x32, 0x53, 0x02, 0x4d, 0x3c,
  32821. 0x99, 0x6e, 0x63, 0xfe, 0x39, 0x9c, 0x05, 0xa6, 0xa0, 0x0c, 0x1e, 0x11,
  32822. 0xa4, 0x86, 0x6a, 0x89, 0x76, 0x54, 0x17, 0x68, 0x5d, 0x35, 0x9a, 0xd7,
  32823. 0x5e, 0x27, 0x0e, 0xbb, 0xba, 0x67, 0x4d, 0x62, 0x12, 0xa8, 0x46, 0x1f,
  32824. 0x0e, 0xd8, 0x7d, 0xc0, 0xae, 0x30, 0xc2, 0x45, 0x71, 0xab, 0xb1, 0xc1,
  32825. 0xfb, 0xdc, 0x03, 0x7a, 0x52, 0xe6, 0x57, 0xf9, 0x7f, 0x65, 0x6b, 0x4e,
  32826. 0x44, 0x64, 0xe8, 0x77, 0x82, 0x1c, 0xc8, 0xfa, 0x09, 0xc7, 0x2f, 0xa9,
  32827. 0x40, 0x87, 0x8e, 0x0e, 0x49, 0xc2, 0x7d, 0x97, 0x27, 0x79, 0x90, 0xc2,
  32828. 0x90, 0x13, 0xa7, 0x49, 0xb7, 0xd7, 0xc5, 0x02, 0x32, 0x4f, 0x1e, 0x34,
  32829. 0x4a, 0xa6, 0xe4, 0xbd, 0xa5, 0xc6, 0xec
  32830. };
  32831. pt = ca_key_der_2048;
  32832. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  32833. sizeof_ca_key_der_2048));
  32834. pt = client_cert_der_2048;
  32835. AssertNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  32836. sizeof_client_cert_der_2048));
  32837. pt = ca_cert_der_2048;
  32838. AssertNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  32839. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  32840. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  32841. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  32842. AssertNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  32843. AssertNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  32844. AssertIntEQ(notAfter->length, 13);
  32845. AssertTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  32846. AssertTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  32847. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  32848. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  32849. AssertIntEQ(derSz, sizeof(expected));
  32850. AssertIntEQ(XMEMCMP(der, expected, derSz), 0);
  32851. wolfSSL_X509_free(ca);
  32852. wolfSSL_X509_free(x509);
  32853. wolfSSL_EVP_PKEY_free(priv);
  32854. wolfSSL_ASN1_TIME_free(notBefore);
  32855. wolfSSL_ASN1_TIME_free(notAfter);
  32856. res = TEST_RES_CHECK(1);
  32857. #endif
  32858. return res;
  32859. }
  32860. static int test_wolfSSL_X509_sign(void)
  32861. {
  32862. int res = TEST_SKIPPED;
  32863. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32864. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  32865. int ret;
  32866. char *cn;
  32867. word32 cnSz;
  32868. X509_NAME *name;
  32869. X509 *x509, *ca;
  32870. DecodedCert dCert;
  32871. EVP_PKEY *pub;
  32872. EVP_PKEY *priv;
  32873. EVP_MD_CTX *mctx;
  32874. #if defined(USE_CERT_BUFFERS_1024)
  32875. const unsigned char* rsaPriv = client_key_der_1024;
  32876. const unsigned char* rsaPub = client_keypub_der_1024;
  32877. const unsigned char* certIssuer = client_cert_der_1024;
  32878. long clientKeySz = (long)sizeof_client_key_der_1024;
  32879. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  32880. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  32881. #elif defined(USE_CERT_BUFFERS_2048)
  32882. const unsigned char* rsaPriv = client_key_der_2048;
  32883. const unsigned char* rsaPub = client_keypub_der_2048;
  32884. const unsigned char* certIssuer = client_cert_der_2048;
  32885. long clientKeySz = (long)sizeof_client_key_der_2048;
  32886. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  32887. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  32888. #endif
  32889. byte sn[16];
  32890. int snSz = sizeof(sn);
  32891. /* Set X509_NAME fields */
  32892. AssertNotNull(name = X509_NAME_new());
  32893. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  32894. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  32895. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  32896. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  32897. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  32898. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  32899. /* Get private and public keys */
  32900. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  32901. clientKeySz));
  32902. AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  32903. AssertNotNull(x509 = X509_new());
  32904. /* Set version 3 */
  32905. AssertIntNE(X509_set_version(x509, 2L), 0);
  32906. /* Set subject name, add pubkey, and sign certificate */
  32907. AssertIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  32908. X509_NAME_free(name);
  32909. AssertIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  32910. #ifdef WOLFSSL_ALT_NAMES
  32911. /* Add some subject alt names */
  32912. AssertIntNE(wolfSSL_X509_add_altname(NULL,
  32913. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  32914. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  32915. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  32916. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  32917. "sphygmomanometer",
  32918. ASN_DNS_TYPE), SSL_SUCCESS);
  32919. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  32920. "supercalifragilisticexpialidocious",
  32921. ASN_DNS_TYPE), SSL_SUCCESS);
  32922. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  32923. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  32924. ASN_DNS_TYPE), SSL_SUCCESS);
  32925. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  32926. {
  32927. unsigned char ip4_type[] = {127,128,0,255};
  32928. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  32929. 0xff, 0xee, 0x99, 0x88,
  32930. 0x77, 0x66, 0x55, 0x44,
  32931. 0x00, 0x33, 0x22, 0x11};
  32932. AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  32933. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  32934. AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  32935. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  32936. }
  32937. #endif
  32938. #endif /* WOLFSSL_ALT_NAMES */
  32939. /* test valid sign case */
  32940. ret = X509_sign(x509, priv, EVP_sha256());
  32941. /* test valid X509_sign_ctx case */
  32942. AssertNotNull(mctx = EVP_MD_CTX_new());
  32943. AssertIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  32944. AssertIntGT(X509_sign_ctx(x509, mctx), 0);
  32945. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  32946. AssertIntEQ(X509_get_ext_count(x509), 1);
  32947. #endif
  32948. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  32949. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  32950. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  32951. #endif
  32952. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  32953. WOLFSSL_SUCCESS);
  32954. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  32955. /* Variation in size depends on ASN.1 encoding when MSB is set.
  32956. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  32957. * with the MSB set. See GenerateInteger in asn.c */
  32958. #ifndef USE_CERT_BUFFERS_1024
  32959. #ifndef WOLFSSL_ALT_NAMES
  32960. /* Valid case - size should be 798-797 with 16 byte serial number */
  32961. AssertTrue((ret == 781 + snSz) || (ret == 782 + snSz));
  32962. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  32963. /* Valid case - size should be 955-956 with 16 byte serial number */
  32964. AssertTrue((ret == 939 + snSz) || (ret == 940 + snSz));
  32965. #else
  32966. /* Valid case - size should be 926-927 with 16 byte serial number */
  32967. AssertTrue((ret == 910 + snSz) || (ret == 911 + snSz));
  32968. #endif
  32969. #else
  32970. #ifndef WOLFSSL_ALT_NAMES
  32971. /* Valid case - size should be 537-538 with 16 byte serial number */
  32972. AssertTrue((ret == 521 + snSz) || (ret == 522 + snSz));
  32973. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  32974. /* Valid case - size should be 695-696 with 16 byte serial number */
  32975. AssertTrue((ret == 679 + snSz) || (ret == 680 + snSz));
  32976. #else
  32977. /* Valid case - size should be 666-667 with 16 byte serial number */
  32978. AssertTrue((ret == 650 + snSz) || (ret == 651 + snSz));
  32979. #endif
  32980. #endif
  32981. /* check that issuer name is as expected after signature */
  32982. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  32983. AssertIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  32984. AssertNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  32985. AssertNotNull(name = X509_get_subject_name(ca));
  32986. cnSz = X509_NAME_get_sz(name);
  32987. AssertNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  32988. AssertNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  32989. AssertIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  32990. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  32991. #ifdef WOLFSSL_MULTI_ATTRIB
  32992. /* test adding multiple OU's to the signer */
  32993. AssertNotNull(name = X509_get_subject_name(ca));
  32994. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  32995. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  32996. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  32997. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  32998. AssertIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  32999. #endif
  33000. AssertNotNull(name = X509_get_subject_name(ca));
  33001. AssertIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  33002. AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  33003. AssertNotNull(name = X509_get_issuer_name(x509));
  33004. cnSz = X509_NAME_get_sz(name);
  33005. AssertNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  33006. AssertNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  33007. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  33008. AssertIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  33009. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  33010. FreeDecodedCert(&dCert);
  33011. /* Test invalid parameters */
  33012. AssertIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  33013. AssertIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  33014. AssertIntEQ(X509_sign(x509, priv, NULL), 0);
  33015. AssertIntEQ(X509_sign_ctx(NULL, mctx), 0);
  33016. EVP_MD_CTX_free(mctx);
  33017. AssertNotNull(mctx = EVP_MD_CTX_new());
  33018. AssertIntEQ(X509_sign_ctx(x509, mctx), 0);
  33019. AssertIntEQ(X509_sign_ctx(x509, NULL), 0);
  33020. /* test invalid version number */
  33021. #if defined(OPENSSL_ALL)
  33022. AssertIntNE(X509_set_version(x509, 6L), 0);
  33023. AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  33024. /* uses ParseCert which fails on bad version number */
  33025. AssertIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  33026. #endif
  33027. EVP_MD_CTX_free(mctx);
  33028. EVP_PKEY_free(priv);
  33029. EVP_PKEY_free(pub);
  33030. X509_free(x509);
  33031. X509_free(ca);
  33032. res = TEST_RES_CHECK(1);
  33033. #endif
  33034. return res;
  33035. }
  33036. static int test_wolfSSL_X509_get0_tbs_sigalg(void)
  33037. {
  33038. int res = TEST_SKIPPED;
  33039. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  33040. X509* x509 = NULL;
  33041. const X509_ALGOR* alg;
  33042. AssertNotNull(x509 = X509_new());
  33043. AssertNull(alg = X509_get0_tbs_sigalg(NULL));
  33044. AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
  33045. X509_free(x509);
  33046. res = TEST_RES_CHECK(1);
  33047. #endif
  33048. return res;
  33049. }
  33050. static int test_wolfSSL_X509_ALGOR_get0(void)
  33051. {
  33052. int res = TEST_SKIPPED;
  33053. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  33054. !defined(NO_SHA256) && !defined(NO_RSA)
  33055. X509* x509 = NULL;
  33056. const ASN1_OBJECT* obj = NULL;
  33057. const X509_ALGOR* alg;
  33058. int pptype = 0;
  33059. const void *ppval = NULL;
  33060. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  33061. SSL_FILETYPE_PEM));
  33062. AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
  33063. /* Invalid case */
  33064. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  33065. AssertNull(obj);
  33066. /* Valid case */
  33067. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  33068. AssertNotNull(obj);
  33069. AssertNull(ppval);
  33070. AssertIntNE(pptype, 0);
  33071. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  33072. AssertIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  33073. X509_free(x509);
  33074. res = TEST_RES_CHECK(1);
  33075. #endif
  33076. return res;
  33077. }
  33078. static int test_wolfSSL_X509_VERIFY_PARAM(void)
  33079. {
  33080. int res = TEST_SKIPPED;
  33081. #if defined(OPENSSL_EXTRA)
  33082. X509_VERIFY_PARAM *paramTo;
  33083. X509_VERIFY_PARAM *paramFrom;
  33084. int ret;
  33085. char testIPv4[] = "127.0.0.1";
  33086. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  33087. char testhostName1[] = "foo.hoge.com";
  33088. char testhostName2[] = "foobar.hoge.com";
  33089. paramTo = X509_VERIFY_PARAM_new();
  33090. AssertNotNull(paramTo);
  33091. XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM ));
  33092. paramFrom = X509_VERIFY_PARAM_new();
  33093. AssertNotNull(paramFrom);
  33094. XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM ));
  33095. ret = X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  33096. (int)XSTRLEN(testhostName1));
  33097. AssertIntEQ(1, ret);
  33098. AssertIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  33099. (int)XSTRLEN(testhostName1)));
  33100. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  33101. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  33102. AssertIntEQ(0x01, paramFrom->hostFlags);
  33103. ret = X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4);
  33104. AssertIntEQ(0, ret);
  33105. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4);
  33106. AssertIntEQ(1, ret);
  33107. AssertIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  33108. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL);
  33109. AssertIntEQ(1, ret);
  33110. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6);
  33111. AssertIntEQ(1, ret);
  33112. AssertIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  33113. /* null pointer */
  33114. ret = X509_VERIFY_PARAM_set1(NULL, paramFrom);
  33115. AssertIntEQ(WOLFSSL_FAILURE, ret);
  33116. /* in the case of "from" null, returns success */
  33117. ret = X509_VERIFY_PARAM_set1(paramTo, NULL);
  33118. AssertIntEQ(WOLFSSL_SUCCESS, ret);
  33119. ret = X509_VERIFY_PARAM_set1(NULL, NULL);
  33120. AssertIntEQ(WOLFSSL_FAILURE, ret);
  33121. /* inherit flags test : VPARAM_DEFAULT */
  33122. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  33123. AssertIntEQ(1, ret);
  33124. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  33125. (int)XSTRLEN(testhostName1)));
  33126. AssertIntEQ(0x01, paramTo->hostFlags);
  33127. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  33128. /* inherit flags test : VPARAM OVERWRITE */
  33129. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  33130. (int)XSTRLEN(testhostName2));
  33131. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  33132. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  33133. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  33134. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  33135. AssertIntEQ(1, ret);
  33136. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  33137. (int)XSTRLEN(testhostName1)));
  33138. AssertIntEQ(0x01, paramTo->hostFlags);
  33139. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  33140. /* inherit flags test : VPARAM_RESET_FLAGS */
  33141. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  33142. (int)XSTRLEN(testhostName2));
  33143. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  33144. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  33145. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  33146. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  33147. AssertIntEQ(1, ret);
  33148. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  33149. (int)XSTRLEN(testhostName1)));
  33150. AssertIntEQ(0x01, paramTo->hostFlags);
  33151. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  33152. /* inherit flags test : VPARAM_LOCKED */
  33153. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  33154. (int)XSTRLEN(testhostName2));
  33155. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  33156. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  33157. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  33158. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  33159. AssertIntEQ(1, ret);
  33160. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  33161. (int)XSTRLEN(testhostName2)));
  33162. AssertIntEQ(0x00, paramTo->hostFlags);
  33163. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  33164. /* test for incorrect parameters */
  33165. ret = X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL );
  33166. AssertIntEQ(0, ret);
  33167. ret = X509_VERIFY_PARAM_set_flags(NULL, 0 );
  33168. AssertIntEQ(0, ret);
  33169. /* inherit flags test : VPARAM_ONCE, not testable yet */
  33170. ret = X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL);
  33171. AssertIntEQ(1, ret);
  33172. ret = X509_VERIFY_PARAM_get_flags(paramTo);
  33173. AssertIntEQ(X509_V_FLAG_CRL_CHECK_ALL, ret);
  33174. ret = X509_VERIFY_PARAM_clear_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL);
  33175. AssertIntEQ(1, ret);
  33176. ret = X509_VERIFY_PARAM_get_flags(paramTo);
  33177. AssertIntEQ(0, ret);
  33178. X509_VERIFY_PARAM_free(paramTo);
  33179. X509_VERIFY_PARAM_free(paramFrom);
  33180. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  33181. res = TEST_RES_CHECK(1);
  33182. #endif
  33183. return res;
  33184. }
  33185. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  33186. static int test_wolfSSL_check_domain_verify_count = 0;
  33187. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  33188. WOLFSSL_X509_STORE_CTX* store)
  33189. {
  33190. AssertIntEQ(X509_STORE_CTX_get_error(store), 0);
  33191. AssertIntEQ(preverify, 1);
  33192. test_wolfSSL_check_domain_verify_count++;
  33193. return 1;
  33194. }
  33195. static void test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  33196. {
  33197. X509_VERIFY_PARAM *param = SSL_get0_param(ssl);
  33198. /* Domain check should only be done on the leaf cert */
  33199. X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  33200. AssertIntEQ(X509_VERIFY_PARAM_set1_host(param,
  33201. "wolfSSL Server Chain", 0), 1);
  33202. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  33203. test_wolfSSL_check_domain_verify_cb);
  33204. }
  33205. static void test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  33206. {
  33207. /* Use a cert with different domains in chain */
  33208. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  33209. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  33210. }
  33211. static int test_wolfSSL_check_domain(void)
  33212. {
  33213. tcp_ready ready;
  33214. func_args client_args;
  33215. func_args server_args;
  33216. THREAD_TYPE serverThread;
  33217. callback_functions func_cb_client;
  33218. callback_functions func_cb_server;
  33219. XMEMSET(&client_args, 0, sizeof(func_args));
  33220. XMEMSET(&server_args, 0, sizeof(func_args));
  33221. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  33222. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  33223. #ifdef WOLFSSL_TIRTOS
  33224. fdOpenSession(Task_self());
  33225. #endif
  33226. StartTCP();
  33227. InitTcpReady(&ready);
  33228. #if defined(USE_WINDOWS_API)
  33229. /* use RNG to get random port if using windows */
  33230. ready.port = GetRandomPort();
  33231. #endif
  33232. server_args.signal = &ready;
  33233. client_args.signal = &ready;
  33234. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  33235. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  33236. client_args.callbacks = &func_cb_client;
  33237. server_args.callbacks = &func_cb_server;
  33238. start_thread(test_server_nofail, &server_args, &serverThread);
  33239. wait_tcp_ready(&server_args);
  33240. test_client_nofail(&client_args, NULL);
  33241. join_thread(serverThread);
  33242. AssertTrue(client_args.return_code);
  33243. AssertTrue(server_args.return_code);
  33244. FreeTcpReady(&ready);
  33245. /* Should have been called once for each cert in sent chain */
  33246. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  33247. AssertIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  33248. #else
  33249. AssertIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  33250. #endif
  33251. return TEST_RES_CHECK(1);
  33252. }
  33253. #endif /* OPENSSL_EXTRA && HAVE_IO_TESTS_DEPENDENCIES */
  33254. static int test_wolfSSL_X509_get_X509_PUBKEY(void)
  33255. {
  33256. int res = TEST_SKIPPED;
  33257. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  33258. X509* x509 = NULL;
  33259. X509_PUBKEY* pubKey;
  33260. AssertNotNull(x509 = X509_new());
  33261. AssertNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  33262. AssertNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  33263. X509_free(x509);
  33264. res = TEST_RES_CHECK(1);
  33265. #endif
  33266. return res;
  33267. }
  33268. static int test_wolfSSL_X509_PUBKEY_RSA(void)
  33269. {
  33270. int res = TEST_SKIPPED;
  33271. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  33272. !defined(NO_SHA256) && !defined(NO_RSA)
  33273. X509* x509 = NULL;
  33274. ASN1_OBJECT* obj = NULL;
  33275. const ASN1_OBJECT* pa_oid = NULL;
  33276. X509_PUBKEY* pubKey;
  33277. X509_PUBKEY* pubKey2;
  33278. EVP_PKEY* evpKey;
  33279. const unsigned char *pk;
  33280. int ppklen, pptype;
  33281. X509_ALGOR *pa;
  33282. const void *pval;
  33283. AssertNotNull(x509 = X509_load_certificate_file(cliCertFile,
  33284. SSL_FILETYPE_PEM));
  33285. AssertNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  33286. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  33287. AssertNotNull(pk);
  33288. AssertNotNull(pa);
  33289. AssertNotNull(pubKey);
  33290. AssertIntGT(ppklen, 0);
  33291. AssertIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  33292. AssertNotNull(evpKey = X509_PUBKEY_get(pubKey));
  33293. AssertNotNull(pubKey2 = X509_PUBKEY_new());
  33294. AssertIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  33295. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  33296. AssertNotNull(pk);
  33297. AssertNotNull(pa);
  33298. AssertIntGT(ppklen, 0);
  33299. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  33300. AssertNotNull(pa_oid);
  33301. AssertNull(pval);
  33302. AssertIntEQ(pptype, V_ASN1_NULL);
  33303. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  33304. X509_PUBKEY_free(pubKey2);
  33305. X509_free(x509);
  33306. EVP_PKEY_free(evpKey);
  33307. res = TEST_RES_CHECK(1);
  33308. #endif
  33309. return res;
  33310. }
  33311. static int test_wolfSSL_X509_PUBKEY_EC(void)
  33312. {
  33313. int res = TEST_SKIPPED;
  33314. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  33315. X509* x509 = NULL;
  33316. ASN1_OBJECT* obj = NULL;
  33317. ASN1_OBJECT* poid;
  33318. const ASN1_OBJECT* pa_oid = NULL;
  33319. X509_PUBKEY* pubKey;
  33320. X509_PUBKEY* pubKey2;
  33321. EVP_PKEY* evpKey;
  33322. const unsigned char *pk;
  33323. int ppklen, pptype;
  33324. X509_ALGOR *pa;
  33325. const void *pval;
  33326. char buf[50];
  33327. AssertNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  33328. SSL_FILETYPE_PEM));
  33329. AssertNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  33330. AssertNotNull(evpKey = X509_PUBKEY_get(pubKey));
  33331. AssertNotNull(pubKey2 = X509_PUBKEY_new());
  33332. AssertIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  33333. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  33334. AssertNotNull(pk);
  33335. AssertNotNull(pa);
  33336. AssertIntGT(ppklen, 0);
  33337. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  33338. AssertNotNull(pa_oid);
  33339. AssertNotNull(pval);
  33340. AssertIntEQ(pptype, V_ASN1_OBJECT);
  33341. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  33342. poid = (ASN1_OBJECT *)pval;
  33343. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  33344. AssertIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  33345. X509_PUBKEY_free(pubKey2);
  33346. X509_free(x509);
  33347. EVP_PKEY_free(evpKey);
  33348. res = TEST_RES_CHECK(1);
  33349. #endif
  33350. return res;
  33351. }
  33352. static int test_wolfSSL_X509_PUBKEY_DSA(void)
  33353. {
  33354. int res = TEST_SKIPPED;
  33355. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  33356. word32 bytes;
  33357. #ifdef USE_CERT_BUFFERS_1024
  33358. byte tmp[ONEK_BUF];
  33359. #elif defined(USE_CERT_BUFFERS_2048)
  33360. byte tmp[TWOK_BUF];
  33361. #else
  33362. byte tmp[TWOK_BUF];
  33363. #endif /* END USE_CERT_BUFFERS_1024 */
  33364. const unsigned char* dsaKeyDer = tmp;
  33365. ASN1_OBJECT* obj = NULL;
  33366. ASN1_STRING* str;
  33367. const ASN1_OBJECT* pa_oid = NULL;
  33368. X509_PUBKEY* pubKey = NULL;
  33369. EVP_PKEY* evpKey = NULL;
  33370. const unsigned char *pk;
  33371. int ppklen, pptype;
  33372. X509_ALGOR *pa;
  33373. const void *pval;
  33374. #ifdef USE_CERT_BUFFERS_1024
  33375. XMEMSET(tmp, 0, sizeof(tmp));
  33376. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  33377. bytes = sizeof_dsa_key_der_1024;
  33378. #elif defined(USE_CERT_BUFFERS_2048)
  33379. XMEMSET(tmp, 0, sizeof(tmp));
  33380. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  33381. bytes = sizeof_dsa_key_der_2048;
  33382. #else
  33383. {
  33384. XFILE fp;
  33385. XMEMSET(tmp, 0, sizeof(tmp));
  33386. fp = XFOPEN("./certs/dsa2048.der", "rb");
  33387. if (fp == XBADFILE) {
  33388. return WOLFSSL_BAD_FILE;
  33389. }
  33390. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  33391. XFCLOSE(fp);
  33392. }
  33393. #endif
  33394. /* Initialize pkey with der format dsa key */
  33395. AssertNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  33396. AssertNotNull(pubKey = X509_PUBKEY_new());
  33397. AssertIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  33398. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  33399. AssertNotNull(pk);
  33400. AssertNotNull(pa);
  33401. AssertIntGT(ppklen, 0);
  33402. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  33403. AssertNotNull(pa_oid);
  33404. AssertNotNull(pval);
  33405. AssertIntEQ(pptype, V_ASN1_SEQUENCE);
  33406. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  33407. str = (ASN1_STRING *)pval;
  33408. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  33409. #ifdef USE_CERT_BUFFERS_1024
  33410. AssertIntEQ(ASN1_STRING_length(str), 291);
  33411. #else
  33412. AssertIntEQ(ASN1_STRING_length(str), 549);
  33413. #endif /* END USE_CERT_BUFFERS_1024 */
  33414. X509_PUBKEY_free(pubKey);
  33415. EVP_PKEY_free(evpKey);
  33416. res = TEST_RES_CHECK(1);
  33417. #endif
  33418. return res;
  33419. }
  33420. static int test_wolfSSL_RAND(void)
  33421. {
  33422. int res = TEST_SKIPPED;
  33423. #if defined(OPENSSL_EXTRA)
  33424. byte seed[16];
  33425. XMEMSET(seed, 0, sizeof(seed));
  33426. RAND_seed(seed, sizeof(seed));
  33427. AssertIntEQ(RAND_poll(), 1);
  33428. RAND_cleanup();
  33429. AssertIntEQ(RAND_egd(NULL), -1);
  33430. #ifndef NO_FILESYSTEM
  33431. {
  33432. char fname[100];
  33433. AssertNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  33434. AssertIntEQ(RAND_write_file(NULL), 0);
  33435. }
  33436. #endif
  33437. res = TEST_RES_CHECK(1);
  33438. #endif
  33439. return res;
  33440. }
  33441. static int test_wolfSSL_BUF(void)
  33442. {
  33443. int res = TEST_SKIPPED;
  33444. #if defined(OPENSSL_EXTRA)
  33445. BUF_MEM* buf;
  33446. AssertNotNull(buf = BUF_MEM_new());
  33447. AssertIntEQ(BUF_MEM_grow(buf, 10), 10);
  33448. AssertIntEQ(BUF_MEM_grow(buf, -1), 0);
  33449. BUF_MEM_free(buf);
  33450. res = TEST_RES_CHECK(1);
  33451. #endif
  33452. return res;
  33453. }
  33454. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  33455. static int stub_rand_seed(const void *buf, int num)
  33456. {
  33457. (void)buf;
  33458. (void)num;
  33459. return 123;
  33460. }
  33461. static int stub_rand_bytes(unsigned char *buf, int num)
  33462. {
  33463. (void)buf;
  33464. (void)num;
  33465. return 456;
  33466. }
  33467. static byte* was_stub_rand_cleanup_called(void)
  33468. {
  33469. static byte was_called = 0;
  33470. return &was_called;
  33471. }
  33472. static void stub_rand_cleanup(void)
  33473. {
  33474. byte* was_called = was_stub_rand_cleanup_called();
  33475. *was_called = 1;
  33476. return;
  33477. }
  33478. static byte* was_stub_rand_add_called(void)
  33479. {
  33480. static byte was_called = 0;
  33481. return &was_called;
  33482. }
  33483. static int stub_rand_add(const void *buf, int num, double entropy)
  33484. {
  33485. byte* was_called = was_stub_rand_add_called();
  33486. (void)buf;
  33487. (void)num;
  33488. (void)entropy;
  33489. *was_called = 1;
  33490. return 0;
  33491. }
  33492. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  33493. {
  33494. (void)buf;
  33495. (void)num;
  33496. return 9876;
  33497. }
  33498. static int stub_rand_status(void)
  33499. {
  33500. return 5432;
  33501. }
  33502. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  33503. static int test_wolfSSL_RAND_set_rand_method(void)
  33504. {
  33505. int res = TEST_SKIPPED;
  33506. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  33507. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  33508. unsigned char* buf = NULL;
  33509. int num = 0;
  33510. double entropy = 0;
  33511. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  33512. byte* was_add_called = was_stub_rand_add_called();
  33513. buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  33514. DYNAMIC_TYPE_TMP_BUFFER);
  33515. AssertIntNE(wolfSSL_RAND_status(), 5432);
  33516. AssertIntEQ(*was_cleanup_called, 0);
  33517. RAND_cleanup();
  33518. AssertIntEQ(*was_cleanup_called, 0);
  33519. rand_methods.seed = &stub_rand_seed;
  33520. rand_methods.bytes = &stub_rand_bytes;
  33521. rand_methods.cleanup = &stub_rand_cleanup;
  33522. rand_methods.add = &stub_rand_add;
  33523. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  33524. rand_methods.status = &stub_rand_status;
  33525. AssertIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  33526. AssertIntEQ(RAND_seed(buf, num), 123);
  33527. AssertIntEQ(RAND_bytes(buf, num), 456);
  33528. AssertIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  33529. AssertIntEQ(RAND_status(), 5432);
  33530. AssertIntEQ(*was_add_called, 0);
  33531. /* The function pointer for RAND_add returns int, but RAND_add itself returns void. */
  33532. RAND_add(buf, num, entropy);
  33533. AssertIntEQ(*was_add_called, 1);
  33534. was_add_called = 0;
  33535. AssertIntEQ(*was_cleanup_called, 0);
  33536. RAND_cleanup();
  33537. AssertIntEQ(*was_cleanup_called, 1);
  33538. *was_cleanup_called = 0;
  33539. AssertIntEQ(RAND_set_rand_method(NULL), WOLFSSL_SUCCESS);
  33540. AssertIntNE(RAND_status(), 5432);
  33541. AssertIntEQ(*was_cleanup_called, 0);
  33542. RAND_cleanup();
  33543. AssertIntEQ(*was_cleanup_called, 0);
  33544. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  33545. res = TEST_RES_CHECK(1);
  33546. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  33547. return res;
  33548. }
  33549. static int test_wolfSSL_RAND_bytes(void)
  33550. {
  33551. int res = TEST_SKIPPED;
  33552. #if defined(OPENSSL_EXTRA)
  33553. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  33554. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  33555. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  33556. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  33557. int max_bufsize;
  33558. byte *my_buf;
  33559. /* sanity check */
  33560. AssertIntEQ(RAND_bytes(NULL, 16), 0);
  33561. AssertIntEQ(RAND_bytes(NULL, 0), 0);
  33562. max_bufsize = size4;
  33563. my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  33564. DYNAMIC_TYPE_TMP_BUFFER);
  33565. AssertIntEQ(RAND_bytes(my_buf, 0), 1);
  33566. AssertIntEQ(RAND_bytes(my_buf, -1), 0);
  33567. AssertNotNull(my_buf);
  33568. XMEMSET(my_buf, 0, max_bufsize);
  33569. AssertIntEQ(RAND_bytes(my_buf, size1), 1);
  33570. AssertIntEQ(RAND_bytes(my_buf, size2), 1);
  33571. AssertIntEQ(RAND_bytes(my_buf, size3), 1);
  33572. AssertIntEQ(RAND_bytes(my_buf, size4), 1);
  33573. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  33574. res = TEST_RES_CHECK(1);
  33575. #endif
  33576. return res;
  33577. }
  33578. static int test_wolfSSL_BN_rand(void)
  33579. {
  33580. int res = TEST_SKIPPED;
  33581. #if defined(OPENSSL_EXTRA)
  33582. BIGNUM* bn;
  33583. BIGNUM* range;
  33584. /* Error conditions. */
  33585. /* NULL BN. */
  33586. AssertIntEQ(BN_rand(NULL, 0, 0, 0), SSL_FAILURE);
  33587. AssertNotNull(bn = BN_new());
  33588. /* Negative bits. */
  33589. AssertIntEQ(BN_rand(bn, -2, 0, 0), SSL_FAILURE);
  33590. /* 0 bits and top is not -1. */
  33591. AssertIntEQ(BN_rand(bn, 0, 1, 0), SSL_FAILURE);
  33592. /* 0 bits and bottom is not 0. */
  33593. AssertIntEQ(BN_rand(bn, 0, 0, 1), SSL_FAILURE);
  33594. /* 1 bit and top is 1. */
  33595. AssertIntEQ(BN_rand(bn, 1, 1, 0), SSL_FAILURE);
  33596. AssertIntEQ(BN_rand(bn, 0, -1, 0), SSL_SUCCESS);
  33597. AssertIntEQ(BN_num_bits(bn), 0);
  33598. AssertIntEQ(BN_rand(bn, 8, 0, 0), SSL_SUCCESS);
  33599. AssertIntEQ(BN_num_bits(bn), 8);
  33600. /* When top is 0, top bit should be 1. */
  33601. AssertIntEQ(BN_is_bit_set(bn, 7), SSL_SUCCESS);
  33602. AssertIntEQ(BN_rand(bn, 8, 1, 0), SSL_SUCCESS);
  33603. /* When top is 1, top 2 bits should be 1. */
  33604. AssertIntEQ(BN_is_bit_set(bn, 7), SSL_SUCCESS);
  33605. AssertIntEQ(BN_is_bit_set(bn, 6), SSL_SUCCESS);
  33606. AssertIntEQ(BN_rand(bn, 8, 0, 1), SSL_SUCCESS);
  33607. /* When bottom is 1, bottom bit should be 1. */
  33608. AssertIntEQ(BN_is_bit_set(bn, 0), SSL_SUCCESS);
  33609. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  33610. * requested number of bits up to the nearest multiple of 8. E.g. in this
  33611. * case, requesting a 13-bit random number would actually return a 16-bit
  33612. * random number. */
  33613. AssertIntEQ(BN_rand(bn, 13, 0, 0), SSL_SUCCESS);
  33614. AssertIntEQ(BN_num_bits(bn), 13);
  33615. AssertNotNull(range = BN_new());
  33616. AssertIntEQ(BN_rand(range, 64, 0, 0), SSL_SUCCESS);
  33617. AssertIntEQ(BN_rand_range(bn, range), SSL_SUCCESS);
  33618. BN_free(bn);
  33619. BN_free(range);
  33620. res = TEST_RES_CHECK(1);
  33621. #endif
  33622. return res;
  33623. }
  33624. static int test_wolfSSL_pseudo_rand(void)
  33625. {
  33626. int res = TEST_SKIPPED;
  33627. #if defined(OPENSSL_EXTRA)
  33628. BIGNUM* bn;
  33629. unsigned char bin[8];
  33630. int i;
  33631. /* BN_pseudo_rand returns 1 on success 0 on failure
  33632. * int BN_pseudo_rand(BIGNUM* bn, int bits, int top, int bottom) */
  33633. for (i = 0; i < 10; i++) {
  33634. AssertNotNull(bn = BN_new());
  33635. AssertIntEQ(BN_pseudo_rand(bn, 8, 0, 0), SSL_SUCCESS);
  33636. AssertIntGT(BN_bn2bin(bn, bin),0);
  33637. AssertIntEQ((bin[0] & 0x80), 0x80); /* top bit should be set */
  33638. BN_free(bn);
  33639. }
  33640. for (i = 0; i < 10; i++) {
  33641. AssertNotNull(bn = BN_new());
  33642. AssertIntEQ(BN_pseudo_rand(bn, 8, 1, 1), SSL_SUCCESS);
  33643. AssertIntGT(BN_bn2bin(bn, bin),0);
  33644. AssertIntEQ((bin[0] & 0xc1), 0xc1); /* top bit should be set */
  33645. BN_free(bn);
  33646. }
  33647. res = TEST_RES_CHECK(1);
  33648. #endif
  33649. return res;
  33650. }
  33651. static int test_wolfSSL_PKCS8_Compat(void)
  33652. {
  33653. int res = TEST_SKIPPED;
  33654. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  33655. #ifndef NO_BIO
  33656. PKCS8_PRIV_KEY_INFO* pt;
  33657. BIO* bio;
  33658. XFILE f;
  33659. int bytes;
  33660. char pkcs8_buffer[512];
  33661. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  33662. EVP_PKEY *pkey = NULL;
  33663. #endif
  33664. /* file from wolfssl/certs/ directory */
  33665. f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb");
  33666. AssertTrue(f != XBADFILE);
  33667. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)), 0);
  33668. XFCLOSE(f);
  33669. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  33670. AssertNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  33671. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  33672. AssertNotNull(pkey = EVP_PKCS82PKEY(pt));
  33673. AssertIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  33674. /* gets PKCS8 pointer to pkey */
  33675. AssertNotNull(EVP_PKEY2PKCS8(pkey));
  33676. EVP_PKEY_free(pkey);
  33677. #endif
  33678. BIO_free(bio);
  33679. PKCS8_PRIV_KEY_INFO_free(pt);
  33680. res = TEST_RES_CHECK(1);
  33681. #endif
  33682. #endif
  33683. return res;
  33684. }
  33685. static int test_wolfSSL_PKCS8_d2i(void)
  33686. {
  33687. int res = TEST_SKIPPED;
  33688. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  33689. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  33690. * requires a 12 byte password in FIPS mode. This test ends up
  33691. * trying to use an 8 byte password. */
  33692. #ifndef NO_FILESYSTEM
  33693. unsigned char pkcs8_buffer[2048];
  33694. const unsigned char* p;
  33695. int bytes;
  33696. XFILE file;
  33697. WOLFSSL_EVP_PKEY* pkey = NULL;
  33698. #ifndef NO_BIO
  33699. BIO* bio;
  33700. #if defined(OPENSSL_ALL) && \
  33701. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  33702. defined(HAVE_ECC)) && \
  33703. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  33704. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  33705. #endif
  33706. #endif
  33707. #ifndef NO_RSA
  33708. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  33709. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  33710. #ifndef NO_DES3
  33711. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  33712. #endif
  33713. #endif /* NO_RSA */
  33714. #ifdef HAVE_ECC
  33715. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  33716. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  33717. #ifndef NO_DES3
  33718. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  33719. #endif
  33720. #endif /* HAVE_ECC */
  33721. #endif /* !NO_FILESYSTEM */
  33722. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  33723. #ifndef NO_RSA
  33724. #ifdef USE_CERT_BUFFERS_1024
  33725. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  33726. int rsaSz = sizeof_server_key_der_1024;
  33727. #else
  33728. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  33729. int rsaSz = sizeof_server_key_der_2048;
  33730. #endif
  33731. #endif
  33732. #ifdef HAVE_ECC
  33733. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  33734. int ecSz = sizeof_ecc_key_der_256;
  33735. #endif
  33736. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  33737. #ifndef NO_FILESYSTEM
  33738. (void)pkcs8_buffer;
  33739. (void)p;
  33740. (void)bytes;
  33741. (void)file;
  33742. #ifndef NO_BIO
  33743. (void)bio;
  33744. #endif
  33745. #endif
  33746. #ifdef OPENSSL_ALL
  33747. #ifndef NO_RSA
  33748. /* Try to auto-detect normal RSA private key */
  33749. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  33750. EVP_PKEY_free(pkey);
  33751. #endif
  33752. #ifdef HAVE_ECC
  33753. /* Try to auto-detect normal EC private key */
  33754. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  33755. EVP_PKEY_free(pkey);
  33756. #endif
  33757. #endif /* OPENSSL_ALL */
  33758. #ifndef NO_FILESYSTEM
  33759. #ifndef NO_RSA
  33760. /* Get DER encoded RSA PKCS#8 data. */
  33761. file = XFOPEN(rsaDerPkcs8File, "rb");
  33762. AssertTrue(file != XBADFILE);
  33763. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  33764. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33765. file)), 0);
  33766. XFCLOSE(file);
  33767. p = pkcs8_buffer;
  33768. #ifdef OPENSSL_ALL
  33769. /* Try to decode - auto-detect key type. */
  33770. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  33771. #else
  33772. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  33773. #endif
  33774. /* Get PEM encoded RSA PKCS#8 data. */
  33775. file = XFOPEN(rsaPemPkcs8File, "rb");
  33776. AssertTrue(file != XBADFILE);
  33777. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33778. file)), 0);
  33779. XFCLOSE(file);
  33780. #if defined(OPENSSL_ALL) && \
  33781. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  33782. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33783. /* Write PKCS#8 PEM to BIO. */
  33784. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  33785. NULL), bytes);
  33786. /* Compare file and written data */
  33787. AssertIntEQ(BIO_get_mem_data(bio, &p), bytes);
  33788. AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  33789. BIO_free(bio);
  33790. #ifndef NO_DES3
  33791. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33792. /* Write Encrypted PKCS#8 PEM to BIO. */
  33793. bytes = 1834;
  33794. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  33795. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  33796. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  33797. (void*)"yassl123"));
  33798. EVP_PKEY_free(evpPkey);
  33799. BIO_free(bio);
  33800. #endif /* !NO_DES3 */
  33801. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  33802. EVP_PKEY_free(pkey);
  33803. /* PKCS#8 encrypted RSA key */
  33804. #ifndef NO_DES3
  33805. file = XFOPEN(rsaDerPkcs8EncFile, "rb");
  33806. AssertTrue(file != XBADFILE);
  33807. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  33808. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33809. file)), 0);
  33810. XFCLOSE(file);
  33811. #if defined(OPENSSL_ALL) && \
  33812. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  33813. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  33814. AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  33815. (void*)"yassl123"));
  33816. EVP_PKEY_free(pkey);
  33817. BIO_free(bio);
  33818. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  33819. #endif /* !NO_DES3 */
  33820. #endif /* NO_RSA */
  33821. #ifdef HAVE_ECC
  33822. /* PKCS#8 encode EC key */
  33823. file = XFOPEN(ecDerPkcs8File, "rb");
  33824. AssertTrue(file != XBADFILE);
  33825. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  33826. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33827. file)), 0);
  33828. XFCLOSE(file);
  33829. p = pkcs8_buffer;
  33830. #ifdef OPENSSL_ALL
  33831. /* Try to decode - auto-detect key type. */
  33832. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  33833. #else
  33834. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  33835. #endif
  33836. /* Get PEM encoded RSA PKCS#8 data. */
  33837. file = XFOPEN(ecPemPkcs8File, "rb");
  33838. AssertTrue(file != XBADFILE);
  33839. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  33840. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33841. file)), 0);
  33842. XFCLOSE(file);
  33843. #if defined(OPENSSL_ALL) && \
  33844. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  33845. defined(HAVE_AES_CBC)
  33846. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33847. /* Write PKCS#8 PEM to BIO. */
  33848. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  33849. NULL), bytes);
  33850. /* Compare file and written data */
  33851. AssertIntEQ(BIO_get_mem_data(bio, &p), bytes);
  33852. AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  33853. BIO_free(bio);
  33854. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33855. /* Write Encrypted PKCS#8 PEM to BIO. */
  33856. bytes = 379;
  33857. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  33858. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  33859. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  33860. (void*)"yassl123"));
  33861. EVP_PKEY_free(evpPkey);
  33862. BIO_free(bio);
  33863. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  33864. EVP_PKEY_free(pkey);
  33865. /* PKCS#8 encrypted EC key */
  33866. #ifndef NO_DES3
  33867. file = XFOPEN(ecDerPkcs8EncFile, "rb");
  33868. AssertTrue(file != XBADFILE);
  33869. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  33870. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33871. file)), 0);
  33872. XFCLOSE(file);
  33873. #if defined(OPENSSL_ALL) && \
  33874. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  33875. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  33876. AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  33877. (void*)"yassl123"));
  33878. EVP_PKEY_free(pkey);
  33879. BIO_free(bio);
  33880. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  33881. #endif /* !NO_DES3 */
  33882. #endif /* HAVE_ECC */
  33883. #endif /* !NO_FILESYSTEM */
  33884. res = TEST_RES_CHECK(1);
  33885. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  33886. return res;
  33887. }
  33888. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  33889. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  33890. #define LOGGING_THREADS 5
  33891. #define ERROR_COUNT 10
  33892. /* copied from logging.c since this is not exposed otherwise */
  33893. #ifndef ERROR_QUEUE_MAX
  33894. #ifdef ERROR_QUEUE_PER_THREAD
  33895. #define ERROR_QUEUE_MAX 16
  33896. #else
  33897. /* this breaks from compat of unlimited error queue size */
  33898. #define ERROR_QUEUE_MAX 100
  33899. #endif
  33900. #endif
  33901. static volatile int loggingThreadsReady;
  33902. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  33903. {
  33904. const char* file;
  33905. int line;
  33906. unsigned long err;
  33907. int errorCount = 0;
  33908. int i;
  33909. (void)args;
  33910. while (!loggingThreadsReady);
  33911. for (i = 0; i < ERROR_COUNT; i++)
  33912. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  33913. while ((err = ERR_get_error_line(&file, &line))) {
  33914. AssertIntEQ(err, 990 + errorCount);
  33915. errorCount++;
  33916. }
  33917. AssertIntEQ(errorCount, ERROR_COUNT);
  33918. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  33919. ERR_clear_error(); /* ERR_get_error_line() does not remove */
  33920. errorCount = 0;
  33921. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  33922. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  33923. while ((err = ERR_get_error_line(&file, &line))) {
  33924. AssertIntEQ(err, 990 + errorCount);
  33925. errorCount++;
  33926. }
  33927. /* test that the 3 errors over the max were dropped */
  33928. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  33929. return 0;
  33930. }
  33931. #endif
  33932. static int test_error_queue_per_thread(void)
  33933. {
  33934. int res = TEST_SKIPPED;
  33935. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  33936. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  33937. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  33938. int i;
  33939. ERR_clear_error(); /* clear out any error nodes */
  33940. loggingThreadsReady = 0;
  33941. for (i = 0; i < LOGGING_THREADS; i++)
  33942. start_thread(test_logging, NULL, &loggingThreads[i]);
  33943. loggingThreadsReady = 1;
  33944. for (i = 0; i < LOGGING_THREADS; i++)
  33945. join_thread(loggingThreads[i]);
  33946. res = TEST_RES_CHECK(1);
  33947. #endif
  33948. return res;
  33949. }
  33950. static int test_wolfSSL_ERR_put_error(void)
  33951. {
  33952. int res = TEST_SKIPPED;
  33953. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  33954. defined(DEBUG_WOLFSSL)
  33955. const char* file;
  33956. int line;
  33957. ERR_clear_error(); /* clear out any error nodes */
  33958. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  33959. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  33960. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  33961. AssertIntEQ(ERR_get_error_line(&file, &line), 1);
  33962. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  33963. AssertIntEQ(ERR_get_error_line(&file, &line), 2);
  33964. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  33965. AssertIntEQ(ERR_get_error_line(&file, &line), 3);
  33966. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  33967. AssertIntEQ(ERR_get_error_line(&file, &line), 4);
  33968. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  33969. AssertIntEQ(ERR_get_error_line(&file, &line), 5);
  33970. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  33971. AssertIntEQ(ERR_get_error_line(&file, &line), 6);
  33972. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  33973. AssertIntEQ(ERR_get_error_line(&file, &line), 7);
  33974. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  33975. AssertIntEQ(ERR_get_error_line(&file, &line), 8);
  33976. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  33977. AssertIntEQ(ERR_get_error_line(&file, &line), 9);
  33978. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  33979. AssertIntEQ(ERR_get_error_line(&file, &line), 10);
  33980. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  33981. AssertIntEQ(ERR_get_error_line(&file, &line), 11);
  33982. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  33983. AssertIntEQ(ERR_get_error_line(&file, &line), 12);
  33984. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  33985. AssertIntEQ(ERR_get_error_line(&file, &line), 13);
  33986. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  33987. AssertIntEQ(ERR_get_error_line(&file, &line), 14);
  33988. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  33989. AssertIntEQ(ERR_get_error_line(&file, &line), 15);
  33990. #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
  33991. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  33992. "this file", 100);
  33993. AssertIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  33994. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  33995. AssertIntEQ(line, 100);
  33996. AssertIntEQ(wolfSSL_ERR_peek_error(),
  33997. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  33998. AssertIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  33999. #endif
  34000. /* try reading past end of error queue */
  34001. file = NULL;
  34002. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  34003. AssertNull(file);
  34004. AssertIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  34005. PEMerr(4,4);
  34006. AssertIntEQ(ERR_get_error(), 4);
  34007. /* Empty and free up all error nodes */
  34008. ERR_clear_error();
  34009. /* Verify all nodes are cleared */
  34010. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  34011. ERR_clear_error();
  34012. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  34013. res = TEST_RES_CHECK(1);
  34014. #endif
  34015. return res;
  34016. }
  34017. /*
  34018. * This is a regression test for a bug where the peek/get error functions were
  34019. * drawing from the end of the queue rather than the front.
  34020. */
  34021. static int test_wolfSSL_ERR_get_error_order(void)
  34022. {
  34023. int res = TEST_SKIPPED;
  34024. #ifdef WOLFSSL_HAVE_ERROR_QUEUE
  34025. /* Empty the queue. */
  34026. wolfSSL_ERR_clear_error();
  34027. wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
  34028. wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
  34029. AssertIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
  34030. AssertIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
  34031. AssertIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
  34032. AssertIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
  34033. res = TEST_RES_CHECK(1);
  34034. #endif /* WOLFSSL_HAVE_ERROR_QUEUE */
  34035. return res;
  34036. }
  34037. #ifndef NO_BIO
  34038. static int test_wolfSSL_ERR_print_errors(void)
  34039. {
  34040. int res = TEST_SKIPPED;
  34041. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  34042. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  34043. BIO* bio;
  34044. char buf[1024];
  34045. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34046. ERR_clear_error(); /* clear out any error nodes */
  34047. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  34048. /* Choosing -299 as an unused errno between MIN_CODE_E < x < WC_LAST_E. */
  34049. ERR_put_error(0,SYS_F_BIND, -299, "asn.c", 100);
  34050. ERR_print_errors(bio);
  34051. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  34052. AssertIntEQ(XSTRNCMP("error:173:wolfSSL library:Bad function argument:ssl.c:0",
  34053. buf, 55), 0);
  34054. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  34055. AssertIntEQ(XSTRNCMP("error:299:wolfSSL library:unknown error number:asn.c:100",
  34056. buf, 56), 0);
  34057. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  34058. AssertIntEQ(buf[0], '\0');
  34059. AssertIntEQ(ERR_get_error_line(NULL, NULL), 0);
  34060. BIO_free(bio);
  34061. res = TEST_RES_CHECK(1);
  34062. #endif
  34063. return res;
  34064. }
  34065. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  34066. defined(DEBUG_WOLFSSL)
  34067. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  34068. {
  34069. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  34070. return 0;
  34071. }
  34072. #endif
  34073. static int test_wolfSSL_ERR_print_errors_cb(void)
  34074. {
  34075. int res = TEST_SKIPPED;
  34076. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  34077. defined(DEBUG_WOLFSSL)
  34078. BIO* bio;
  34079. char buf[1024];
  34080. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34081. ERR_clear_error(); /* clear out any error nodes */
  34082. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  34083. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  34084. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  34085. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  34086. AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  34087. buf, 53), 0);
  34088. AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 275 line:100 file:asn.c",
  34089. buf + 53, 55), 0);
  34090. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  34091. BIO_free(bio);
  34092. res = TEST_RES_CHECK(1);
  34093. #endif
  34094. return res;
  34095. }
  34096. /*
  34097. * Testing WOLFSSL_ERROR_MSG
  34098. */
  34099. static int test_WOLFSSL_ERROR_MSG(void)
  34100. {
  34101. int res = TEST_SKIPPED;
  34102. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  34103. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  34104. const char* msg = TEST_STRING;
  34105. WOLFSSL_ERROR_MSG(msg);
  34106. res = TEST_RES_CHECK(1);
  34107. #endif
  34108. return res;
  34109. }/*End test_WOLFSSL_ERROR_MSG*/
  34110. /*
  34111. * Testing wc_ERR_remove_state
  34112. */
  34113. static int test_wc_ERR_remove_state(void)
  34114. {
  34115. int res = TEST_SKIPPED;
  34116. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  34117. wc_ERR_remove_state();
  34118. res = TEST_RES_CHECK(1);
  34119. #endif
  34120. return res;
  34121. }/*End test_wc_ERR_remove_state*/
  34122. /*
  34123. * Testing wc_ERR_print_errors_fp
  34124. */
  34125. static int test_wc_ERR_print_errors_fp(void)
  34126. {
  34127. int res = TEST_SKIPPED;
  34128. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  34129. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  34130. long sz;
  34131. XFILE fp;
  34132. int ret = 0;
  34133. WOLFSSL_ERROR(BAD_FUNC_ARG);
  34134. fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar");
  34135. wc_ERR_print_errors_fp(fp);
  34136. #if defined(DEBUG_WOLFSSL)
  34137. AssertTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  34138. sz = XFTELL(fp);
  34139. #ifdef NO_ERROR_QUEUE
  34140. /* File should be empty when NO_ERROR_QUEUE is defined */
  34141. if (sz != 0) {
  34142. ret = BAD_FUNC_ARG;
  34143. }
  34144. #else
  34145. if (sz == 0) {
  34146. ret = BAD_FUNC_ARG;
  34147. }
  34148. #endif
  34149. #endif
  34150. XFCLOSE(fp);
  34151. (void)sz;
  34152. res = TEST_RES_CHECK(ret == 0);
  34153. #endif
  34154. return res;
  34155. }/*End test_wc_ERR_print_errors_fp*/
  34156. #ifdef DEBUG_WOLFSSL
  34157. static void Logging_cb(const int logLevel, const char *const logMessage)
  34158. {
  34159. (void)logLevel;
  34160. (void)logMessage;
  34161. }
  34162. #endif
  34163. /*
  34164. * Testing wolfSSL_GetLoggingCb
  34165. */
  34166. static int test_wolfSSL_GetLoggingCb(void)
  34167. {
  34168. int ret = 0;
  34169. #ifdef DEBUG_WOLFSSL
  34170. /* Testing without wolfSSL_SetLoggingCb() */
  34171. if (ret == 0) {
  34172. if (wolfSSL_GetLoggingCb() == NULL) { /* Should be true */
  34173. ret = 0;
  34174. }
  34175. if (wolfSSL_GetLoggingCb() != NULL) { /* Should not be true */
  34176. ret = -1;
  34177. }
  34178. }
  34179. /* Testing with wolfSSL_SetLoggingCb() */
  34180. if (ret == 0) {
  34181. ret = wolfSSL_SetLoggingCb(Logging_cb);
  34182. if (ret == 0) {
  34183. if (wolfSSL_GetLoggingCb() == NULL) { /* Should not be true */
  34184. ret = -1;
  34185. }
  34186. if (ret == 0) {
  34187. if (wolfSSL_GetLoggingCb() == Logging_cb) { /* Should be true */
  34188. ret = 0;
  34189. }
  34190. }
  34191. /* reset logging callback */
  34192. wolfSSL_SetLoggingCb(NULL);
  34193. }
  34194. }
  34195. #endif
  34196. if (ret == 0) {
  34197. if (wolfSSL_GetLoggingCb() != NULL) {
  34198. ret = -1;
  34199. }
  34200. }
  34201. return TEST_RES_CHECK(ret == 0);
  34202. }/*End test_wolfSSL_GetLoggingCb*/
  34203. #endif /* !NO_BIO */
  34204. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  34205. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  34206. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  34207. static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  34208. {
  34209. static const unsigned char key[] = "simple test key";
  34210. HMAC_CTX* hmac;
  34211. ENGINE* e = NULL;
  34212. unsigned char hash[WC_MAX_DIGEST_SIZE];
  34213. unsigned int len;
  34214. AssertNotNull(hmac = HMAC_CTX_new());
  34215. HMAC_CTX_init(hmac);
  34216. AssertIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e),
  34217. SSL_SUCCESS);
  34218. /* re-using test key as data to hash */
  34219. AssertIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), SSL_SUCCESS);
  34220. AssertIntEQ(HMAC_Update(hmac, NULL, 0), SSL_SUCCESS);
  34221. AssertIntEQ(HMAC_Final(hmac, hash, &len), SSL_SUCCESS);
  34222. AssertIntEQ(len, md_len);
  34223. AssertIntEQ(HMAC_size(hmac), md_len);
  34224. AssertStrEQ(HMAC_CTX_get_md(hmac), md);
  34225. HMAC_cleanup(hmac);
  34226. HMAC_CTX_free(hmac);
  34227. len = 0;
  34228. AssertNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  34229. AssertIntEQ(len, md_len);
  34230. return 0;
  34231. }
  34232. #endif
  34233. static int test_wolfSSL_HMAC(void)
  34234. {
  34235. int res = TEST_SKIPPED;
  34236. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  34237. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  34238. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  34239. #ifndef NO_SHA256
  34240. test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE);
  34241. #endif
  34242. #ifdef WOLFSSL_SHA224
  34243. test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE);
  34244. #endif
  34245. #ifdef WOLFSSL_SHA384
  34246. test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE);
  34247. #endif
  34248. #ifdef WOLFSSL_SHA512
  34249. test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE);
  34250. #endif
  34251. #ifdef WOLFSSL_SHA3
  34252. #ifndef WOLFSSL_NOSHA3_224
  34253. test_openssl_hmac(EVP_sha3_224(), (int)WC_SHA3_224_DIGEST_SIZE);
  34254. #endif
  34255. #ifndef WOLFSSL_NOSHA3_256
  34256. test_openssl_hmac(EVP_sha3_256(), (int)WC_SHA3_256_DIGEST_SIZE);
  34257. #endif
  34258. #ifndef WOLFSSL_NOSHA3_384
  34259. test_openssl_hmac(EVP_sha3_384(), (int)WC_SHA3_384_DIGEST_SIZE);
  34260. #endif
  34261. #ifndef WOLFSSL_NOSHA3_512
  34262. test_openssl_hmac(EVP_sha3_512(), (int)WC_SHA3_512_DIGEST_SIZE);
  34263. #endif
  34264. #endif
  34265. #ifndef NO_SHA
  34266. test_openssl_hmac(EVP_sha1(), (int)WC_SHA_DIGEST_SIZE);
  34267. #endif
  34268. res = TEST_RES_CHECK(1);
  34269. #endif
  34270. return res;
  34271. }
  34272. static int test_wolfSSL_CMAC(void)
  34273. {
  34274. int res = TEST_SKIPPED;
  34275. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  34276. defined(WOLFSSL_AES_DIRECT)
  34277. int i;
  34278. byte key[AES_128_KEY_SIZE];
  34279. CMAC_CTX* cmacCtx = NULL;
  34280. byte out[AES_BLOCK_SIZE];
  34281. size_t outLen = AES_BLOCK_SIZE;
  34282. for (i=0; i < AES_128_KEY_SIZE; ++i) {
  34283. key[i] = i;
  34284. }
  34285. AssertNotNull(cmacCtx = CMAC_CTX_new());
  34286. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  34287. AssertNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  34288. AssertIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  34289. NULL), SSL_SUCCESS);
  34290. /* re-using test key as data to hash */
  34291. AssertIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), SSL_SUCCESS);
  34292. AssertIntEQ(CMAC_Update(cmacCtx, NULL, 0), SSL_SUCCESS);
  34293. AssertIntEQ(CMAC_Final(cmacCtx, out, &outLen), SSL_SUCCESS);
  34294. AssertIntEQ(outLen, AES_BLOCK_SIZE);
  34295. CMAC_CTX_free(cmacCtx);
  34296. res = TEST_RES_CHECK(1);
  34297. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  34298. return res;
  34299. }
  34300. static int test_wolfSSL_OBJ(void)
  34301. {
  34302. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  34303. * mode
  34304. */
  34305. int res = TEST_SKIPPED;
  34306. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  34307. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  34308. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  34309. ASN1_OBJECT *obj = NULL;
  34310. ASN1_OBJECT *obj2 = NULL;
  34311. char buf[50];
  34312. XFILE fp;
  34313. X509 *x509 = NULL;
  34314. X509_NAME *x509Name;
  34315. X509_NAME_ENTRY *x509NameEntry;
  34316. ASN1_OBJECT *asn1Name = NULL;
  34317. int numNames;
  34318. BIO *bio = NULL;
  34319. int nid;
  34320. int i, j;
  34321. const char *f[] = {
  34322. #ifndef NO_RSA
  34323. "./certs/ca-cert.der",
  34324. #endif
  34325. #ifdef HAVE_ECC
  34326. "./certs/ca-ecc-cert.der",
  34327. "./certs/ca-ecc384-cert.der",
  34328. #endif
  34329. NULL};
  34330. ASN1_OBJECT *field_name_obj = NULL;
  34331. int lastpos = -1;
  34332. int tmp = -1;
  34333. ASN1_STRING *asn1 = NULL;
  34334. unsigned char *buf_dyn = NULL;
  34335. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  34336. AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
  34337. AssertIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  34338. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  34339. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  34340. ASN1_OBJECT_free(obj);
  34341. AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
  34342. AssertIntEQ(OBJ_obj2nid(obj), NID_sha256);
  34343. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  34344. #ifdef WOLFSSL_CERT_EXT
  34345. AssertIntEQ(OBJ_txt2nid(buf), NID_sha256);
  34346. #endif
  34347. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  34348. AssertNotNull(obj2 = OBJ_dup(obj));
  34349. AssertIntEQ(OBJ_cmp(obj, obj2), 0);
  34350. ASN1_OBJECT_free(obj);
  34351. ASN1_OBJECT_free(obj2);
  34352. for (i = 0; f[i] != NULL; i++)
  34353. {
  34354. AssertTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  34355. AssertNotNull(x509 = d2i_X509_fp(fp, NULL));
  34356. XFCLOSE(fp);
  34357. AssertNotNull(x509Name = X509_get_issuer_name(x509));
  34358. AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  34359. /* Get the Common Name by using OBJ_txt2obj */
  34360. AssertNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  34361. do
  34362. {
  34363. lastpos = tmp;
  34364. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  34365. } while (tmp > -1);
  34366. AssertIntNE(lastpos, -1);
  34367. ASN1_OBJECT_free(field_name_obj);
  34368. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  34369. AssertNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  34370. AssertIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  34371. /*
  34372. * All Common Names should be www.wolfssl.com
  34373. * This makes testing easier as we can test for the expected value.
  34374. */
  34375. AssertStrEQ((char*)buf_dyn, "www.wolfssl.com");
  34376. OPENSSL_free(buf_dyn);
  34377. bio = BIO_new(BIO_s_mem());
  34378. AssertTrue(bio != NULL);
  34379. for (j = 0; j < numNames; j++)
  34380. {
  34381. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  34382. AssertNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  34383. AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  34384. }
  34385. BIO_free(bio);
  34386. X509_free(x509);
  34387. }
  34388. #ifdef HAVE_PKCS12
  34389. {
  34390. PKCS12 *p12;
  34391. int boolRet;
  34392. EVP_PKEY *pkey = NULL;
  34393. const char *p12_f[] = {
  34394. #if !defined(NO_DES3) && !defined(NO_RSA)
  34395. "./certs/test-servercert.p12",
  34396. #endif
  34397. NULL};
  34398. for (i = 0; p12_f[i] != NULL; i++)
  34399. {
  34400. AssertTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  34401. AssertNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  34402. XFCLOSE(fp);
  34403. AssertTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  34404. &pkey, &x509, NULL)) > 0);
  34405. wc_PKCS12_free(p12);
  34406. EVP_PKEY_free(pkey);
  34407. x509Name = X509_get_issuer_name(x509);
  34408. AssertNotNull(x509Name);
  34409. AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  34410. AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  34411. for (j = 0; j < numNames; j++)
  34412. {
  34413. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  34414. AssertNotNull(asn1Name =
  34415. X509_NAME_ENTRY_get_object(x509NameEntry));
  34416. AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  34417. }
  34418. BIO_free(bio);
  34419. X509_free(x509);
  34420. }
  34421. }
  34422. #endif /* HAVE_PKCS12 */
  34423. res = TEST_RES_CHECK(1);
  34424. #endif
  34425. return res;
  34426. }
  34427. static int test_wolfSSL_i2a_ASN1_OBJECT(void)
  34428. {
  34429. int res = TEST_SKIPPED;
  34430. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  34431. ASN1_OBJECT *obj = NULL;
  34432. BIO *bio = NULL;
  34433. AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
  34434. AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  34435. AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  34436. AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  34437. AssertIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  34438. BIO_free(bio);
  34439. ASN1_OBJECT_free(obj);
  34440. res = TEST_RES_CHECK(1);
  34441. #endif
  34442. return res;
  34443. }
  34444. static int test_wolfSSL_OBJ_cmp(void)
  34445. {
  34446. int res = TEST_SKIPPED;
  34447. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  34448. ASN1_OBJECT *obj = NULL;
  34449. ASN1_OBJECT *obj2 = NULL;
  34450. AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
  34451. AssertNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  34452. AssertIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  34453. AssertIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  34454. AssertIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  34455. AssertIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  34456. AssertIntEQ(OBJ_cmp(obj, obj), 0);
  34457. AssertIntEQ(OBJ_cmp(obj2, obj2), 0);
  34458. ASN1_OBJECT_free(obj);
  34459. ASN1_OBJECT_free(obj2);
  34460. res = TEST_RES_CHECK(1);
  34461. #endif
  34462. return res;
  34463. }
  34464. static int test_wolfSSL_OBJ_txt2nid(void)
  34465. {
  34466. int res = TEST_SKIPPED;
  34467. #if !defined(NO_WOLFSSL_STUB) && defined(WOLFSSL_APACHE_HTTPD)
  34468. int i;
  34469. static const struct {
  34470. const char* sn;
  34471. const char* ln;
  34472. const char* oid;
  34473. int nid;
  34474. } testVals[] = {
  34475. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  34476. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  34477. NID_id_on_dnsSRV },
  34478. { "msUPN", "Microsoft User Principal Name",
  34479. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  34480. { NULL, NULL, NULL, NID_undef }
  34481. };
  34482. /* Invalid cases */
  34483. AssertIntEQ(OBJ_txt2nid(NULL), NID_undef);
  34484. AssertIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  34485. /* Valid cases */
  34486. for (i = 0; testVals[i].sn != NULL; i++) {
  34487. AssertIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  34488. AssertIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  34489. AssertIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  34490. }
  34491. res = TEST_RES_CHECK(1);
  34492. #endif
  34493. return res;
  34494. }
  34495. static int test_wolfSSL_OBJ_txt2obj(void)
  34496. {
  34497. int res = TEST_SKIPPED;
  34498. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  34499. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  34500. int i;
  34501. char buf[50];
  34502. ASN1_OBJECT* obj;
  34503. static const struct {
  34504. const char* oidStr;
  34505. const char* sn;
  34506. const char* ln;
  34507. } objs_list[] = {
  34508. #if defined(WOLFSSL_APACHE_HTTPD)
  34509. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  34510. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  34511. #endif
  34512. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  34513. { NULL, NULL, NULL }
  34514. };
  34515. static const struct {
  34516. const char* numeric;
  34517. const char* name;
  34518. } objs_named[] = {
  34519. /* In dictionary but not in normal list. */
  34520. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  34521. /* Made up OID. */
  34522. { "1.3.5.7", "1.3.5.7" },
  34523. { NULL, NULL }
  34524. };
  34525. AssertNull(obj = OBJ_txt2obj("Bad name", 0));
  34526. AssertNull(obj = OBJ_txt2obj(NULL, 0));
  34527. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  34528. /* Test numerical value of oid (oidStr) */
  34529. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  34530. /* Convert object back to text to confirm oid is correct */
  34531. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  34532. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  34533. ASN1_OBJECT_free(obj);
  34534. XMEMSET(buf, 0, sizeof(buf));
  34535. /* Test short name (sn) */
  34536. AssertNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  34537. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  34538. /* Convert object back to text to confirm oid is correct */
  34539. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  34540. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  34541. ASN1_OBJECT_free(obj);
  34542. XMEMSET(buf, 0, sizeof(buf));
  34543. /* Test long name (ln) - should fail when no_name = 1 */
  34544. AssertNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  34545. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  34546. /* Convert object back to text to confirm oid is correct */
  34547. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  34548. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  34549. ASN1_OBJECT_free(obj);
  34550. XMEMSET(buf, 0, sizeof(buf));
  34551. }
  34552. for (i = 0; objs_named[i].numeric != NULL; i++) {
  34553. AssertNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  34554. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  34555. AssertIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  34556. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  34557. AssertIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  34558. ASN1_OBJECT_free(obj);
  34559. }
  34560. res = TEST_RES_CHECK(1);
  34561. #endif
  34562. return res;
  34563. }
  34564. static int test_wolfSSL_i2t_ASN1_OBJECT(void)
  34565. {
  34566. int res = TEST_SKIPPED;
  34567. #if defined(OPENSSL_EXTRA) && \
  34568. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  34569. char buf[50] = {0};
  34570. ASN1_OBJECT* obj;
  34571. const char* oid = "2.5.29.19";
  34572. const char* ln = "X509v3 Basic Constraints";
  34573. obj = NULL;
  34574. AssertIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), WOLFSSL_FAILURE);
  34575. AssertIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), WOLFSSL_FAILURE);
  34576. AssertIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), WOLFSSL_FAILURE);
  34577. AssertNotNull(obj = OBJ_txt2obj(oid, 0));
  34578. XMEMSET(buf, 0, sizeof(buf));
  34579. AssertIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  34580. AssertIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  34581. ASN1_OBJECT_free(obj);
  34582. res = TEST_RES_CHECK(1);
  34583. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  34584. return res;
  34585. }
  34586. static int test_wolfSSL_PEM_write_bio_X509(void)
  34587. {
  34588. int res = TEST_SKIPPED;
  34589. #if defined(OPENSSL_EXTRA) && defined(OPENSSL_ALL) && \
  34590. defined(WOLFSSL_AKID_NAME) && defined(WOLFSSL_CERT_EXT) && \
  34591. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && !defined(NO_RSA) && \
  34592. !defined(NO_FILESYSTEM)
  34593. /* This test contains the hard coded expected
  34594. * lengths. Update if necessary */
  34595. FILE* fp = NULL;
  34596. WOLFSSL_EVP_PKEY *priv = NULL;
  34597. BIO* input = NULL;
  34598. BIO* output = NULL;
  34599. X509* x509a = NULL;
  34600. X509* x509b = NULL;
  34601. ASN1_TIME* notBeforeA = NULL;
  34602. ASN1_TIME* notAfterA = NULL;
  34603. ASN1_TIME* notBeforeB = NULL;
  34604. ASN1_TIME* notAfterB = NULL;
  34605. int expectedLen;
  34606. fp = XFOPEN("certs/server-key.pem", "rb");
  34607. AssertNotNull(fp);
  34608. priv = wolfSSL_PEM_read_PrivateKey(fp, NULL, NULL, NULL);
  34609. XFCLOSE(fp);
  34610. fp = NULL;
  34611. AssertNotNull(priv);
  34612. AssertNotNull(input = BIO_new_file(
  34613. "certs/test/cert-ext-multiple.pem", "rb"));
  34614. AssertIntEQ(wolfSSL_BIO_get_len(input), 2000);
  34615. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  34616. AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  34617. AssertNotNull(notBeforeA = X509_get_notBefore(x509a));
  34618. AssertNotNull(notAfterA = X509_get_notAfter(x509a));
  34619. /* write X509 back to PEM BIO; no need to sign as nothing changed. */
  34620. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  34621. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  34622. /* compare length against expected */
  34623. expectedLen = 2000;
  34624. AssertIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  34625. /* read exported X509 PEM back into struct, sanity check on export,
  34626. * make sure notBefore/notAfter are the same and certs are identical. */
  34627. AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  34628. AssertNotNull(notBeforeB = X509_get_notBefore(x509b));
  34629. AssertNotNull(notAfterB = X509_get_notAfter(x509b));
  34630. AssertIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  34631. AssertIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  34632. AssertIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  34633. X509_free(x509b);
  34634. /* Reset output buffer */
  34635. BIO_free(output);
  34636. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  34637. /* Test forcing the AKID to be generated just from KeyIdentifier */
  34638. if (x509a->authKeyIdSrc != NULL) {
  34639. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  34640. x509a->authKeyId = x509a->authKeyIdSrc;
  34641. x509a->authKeyIdSrc = NULL;
  34642. x509a->authKeyIdSrcSz = 0;
  34643. }
  34644. /* Resign to re-generate the der */
  34645. AssertIntGT(wolfSSL_X509_sign(x509a, priv, EVP_sha256()), 0);
  34646. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  34647. /* Check that we generate a smaller output since the AKID will
  34648. * only contain the KeyIdentifier without any additional
  34649. * information */
  34650. /* Here we copy the validity struct from the original */
  34651. expectedLen = 1688;
  34652. AssertIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  34653. /* Reset buffers and x509 */
  34654. BIO_free(input);
  34655. BIO_free(output);
  34656. X509_free(x509a);
  34657. /* test CA and basicConstSet values are encoded when
  34658. * the cert is a CA */
  34659. AssertNotNull(input = BIO_new_file(
  34660. "certs/server-cert.pem", "rb"));
  34661. /* read PEM into X509 struct */
  34662. AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  34663. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  34664. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  34665. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  34666. /* read exported X509 PEM back into struct, ensure isCa and basicConstSet
  34667. * values are maintained and certs are identical.*/
  34668. AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  34669. AssertIntEQ(x509b->isCa, 1);
  34670. AssertIntEQ(x509b->basicConstSet, 1);
  34671. AssertIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  34672. X509_free(x509a);
  34673. X509_free(x509b);
  34674. BIO_free(input);
  34675. BIO_free(output);
  34676. /* test CA and basicConstSet values are encoded when
  34677. * the cert is not CA */
  34678. AssertNotNull(input = BIO_new_file(
  34679. "certs/client-uri-cert.pem", "rb"));
  34680. /* read PEM into X509 struct */
  34681. AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  34682. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  34683. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  34684. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  34685. /* read exported X509 PEM back into struct, ensure isCa and
  34686. * basicConstSet values are maintained and certs are identical */
  34687. AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  34688. AssertIntEQ(x509b->isCa, 0);
  34689. AssertIntEQ(x509b->basicConstSet, 1);
  34690. AssertIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  34691. wolfSSL_EVP_PKEY_free(priv);
  34692. X509_free(x509a);
  34693. X509_free(x509b);
  34694. BIO_free(input);
  34695. BIO_free(output);
  34696. res = TEST_RES_CHECK(1);
  34697. #endif
  34698. return res;
  34699. }
  34700. static int test_wolfSSL_X509_NAME_ENTRY(void)
  34701. {
  34702. int res = TEST_SKIPPED;
  34703. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  34704. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  34705. X509* x509;
  34706. #ifndef NO_BIO
  34707. BIO* bio;
  34708. #endif
  34709. X509_NAME* nm;
  34710. X509_NAME_ENTRY* entry;
  34711. unsigned char cn[] = "another name to add";
  34712. #ifdef OPENSSL_ALL
  34713. int i, names_len;
  34714. #endif
  34715. AssertNotNull(x509 =
  34716. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  34717. #ifndef NO_BIO
  34718. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34719. AssertIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  34720. #endif
  34721. #ifdef WOLFSSL_CERT_REQ
  34722. {
  34723. X509_REQ* req;
  34724. #ifndef NO_BIO
  34725. BIO* bReq;
  34726. #endif
  34727. AssertNotNull(req =
  34728. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  34729. #ifndef NO_BIO
  34730. AssertNotNull(bReq = BIO_new(BIO_s_mem()));
  34731. AssertIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  34732. BIO_free(bReq);
  34733. #endif
  34734. X509_free(req);
  34735. }
  34736. #endif
  34737. AssertNotNull(nm = X509_get_subject_name(x509));
  34738. /* Test add entry */
  34739. AssertNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  34740. 0x0c, cn, (int)sizeof(cn)));
  34741. AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  34742. #ifdef WOLFSSL_CERT_EXT
  34743. AssertIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  34744. (byte*)"support@wolfssl.com", 19, -1,
  34745. 1), WOLFSSL_SUCCESS);
  34746. #endif
  34747. X509_NAME_ENTRY_free(entry);
  34748. #ifdef WOLFSSL_CERT_REQ
  34749. {
  34750. unsigned char srv_pkcs9p[] = "Server";
  34751. char* subject;
  34752. AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  34753. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  34754. subject = X509_NAME_oneline(nm, 0, 0);
  34755. #ifdef DEBUG_WOLFSSL
  34756. fprintf(stderr, "\n\t%s\n", subject);
  34757. #endif
  34758. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  34759. }
  34760. #endif
  34761. /* Test add entry by text */
  34762. AssertNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  34763. 0x0c, cn, (int)sizeof(cn)));
  34764. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  34765. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  34766. AssertNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  34767. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  34768. #endif
  34769. AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  34770. X509_NAME_ENTRY_free(entry);
  34771. /* Test add entry by NID */
  34772. AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  34773. cn, -1, -1, 0), SSL_SUCCESS);
  34774. #ifdef OPENSSL_ALL
  34775. /* stack of name entry */
  34776. AssertIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  34777. for (i=0; i<names_len; i++) {
  34778. AssertNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  34779. }
  34780. #endif
  34781. #ifndef NO_BIO
  34782. BIO_free(bio);
  34783. #endif
  34784. X509_free(x509); /* free's nm */
  34785. res = TEST_RES_CHECK(1);
  34786. #endif
  34787. return res;
  34788. }
  34789. static int test_wolfSSL_X509_set_name(void)
  34790. {
  34791. int res = TEST_SKIPPED;
  34792. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34793. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  34794. X509* x509;
  34795. X509_NAME* name;
  34796. AssertNotNull(name = X509_NAME_new());
  34797. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  34798. (byte*)"wolfssl.com", 11, 0, 1),
  34799. WOLFSSL_SUCCESS);
  34800. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  34801. (byte*)"support@wolfssl.com", 19, -1,
  34802. 1), WOLFSSL_SUCCESS);
  34803. AssertNotNull(x509 = X509_new());
  34804. AssertIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  34805. AssertIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  34806. AssertIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  34807. AssertIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  34808. AssertIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  34809. AssertIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  34810. AssertIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  34811. AssertIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  34812. X509_free(x509);
  34813. X509_NAME_free(name);
  34814. res = TEST_RES_CHECK(1);
  34815. #endif /* OPENSSL_ALL && !NO_CERTS */
  34816. return res;
  34817. }
  34818. static int test_wolfSSL_X509_set_notAfter(void)
  34819. {
  34820. int res = TEST_SKIPPED;
  34821. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  34822. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  34823. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  34824. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  34825. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  34826. /* Generalized time will overflow time_t if not long */
  34827. X509* x;
  34828. BIO* bio;
  34829. ASN1_TIME *asn_time, *time_check;
  34830. const int year = 365*24*60*60;
  34831. const int day = 24*60*60;
  34832. const int hour = 60*60;
  34833. const int mini = 60;
  34834. int offset_day;
  34835. unsigned char buf[25];
  34836. time_t t;
  34837. /*
  34838. * Setup asn_time. APACHE HTTPD uses time(NULL)
  34839. */
  34840. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  34841. offset_day = 7;
  34842. /*
  34843. * Free these.
  34844. */
  34845. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  34846. AssertNotNull(asn_time);
  34847. AssertNotNull(x = X509_new());
  34848. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34849. /*
  34850. * Tests
  34851. */
  34852. AssertTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  34853. /* time_check is simply (ANS1_TIME*)x->notAfter */
  34854. AssertNotNull(time_check = X509_get_notAfter(x));
  34855. /* ANS1_TIME_check validates by checking if argument can be parsed */
  34856. AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  34857. /* Convert to human readable format and compare to intended date */
  34858. AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
  34859. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  34860. AssertIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  34861. /*
  34862. * Cleanup
  34863. */
  34864. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  34865. X509_free(x);
  34866. BIO_free(bio);
  34867. res = TEST_RES_CHECK(1);
  34868. #endif
  34869. return res;
  34870. }
  34871. static int test_wolfSSL_X509_set_notBefore(void)
  34872. {
  34873. int res = TEST_SKIPPED;
  34874. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  34875. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  34876. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  34877. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  34878. X509* x;
  34879. BIO* bio;
  34880. ASN1_TIME *asn_time, *time_check;
  34881. const int year = 365*24*60*60;
  34882. const int day = 24*60*60;
  34883. const int hour = 60*60;
  34884. const int mini = 60;
  34885. int offset_day;
  34886. unsigned char buf[25];
  34887. time_t t;
  34888. /*
  34889. * Setup asn_time. APACHE HTTPD uses time(NULL)
  34890. */
  34891. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  34892. offset_day = 7;
  34893. /*
  34894. * Free these.
  34895. */
  34896. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  34897. AssertNotNull(asn_time);
  34898. AssertNotNull(x = X509_new());
  34899. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34900. AssertIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  34901. /*
  34902. * Main Tests
  34903. */
  34904. AssertTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  34905. /* time_check == (ANS1_TIME*)x->notBefore */
  34906. AssertNotNull(time_check = X509_get_notBefore(x));
  34907. /* ANS1_TIME_check validates by checking if argument can be parsed */
  34908. AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  34909. /* Convert to human readable format and compare to intended date */
  34910. AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
  34911. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  34912. AssertIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  34913. /*
  34914. * Cleanup
  34915. */
  34916. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  34917. X509_free(x);
  34918. BIO_free(bio);
  34919. res = TEST_RES_CHECK(1);
  34920. #endif
  34921. return res;
  34922. }
  34923. static int test_wolfSSL_X509_set_version(void)
  34924. {
  34925. int res = TEST_SKIPPED;
  34926. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  34927. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  34928. X509* x509;
  34929. long v = 2L;
  34930. long maxInt = INT_MAX;
  34931. AssertNotNull(x509 = X509_new());
  34932. /* These should pass. */
  34933. AssertTrue(wolfSSL_X509_set_version(x509, v));
  34934. AssertIntEQ(v, wolfSSL_X509_get_version(x509));
  34935. /* Fail Case: When v(long) is greater than x509->version(int). */
  34936. v = maxInt+1;
  34937. AssertFalse(wolfSSL_X509_set_version(x509, v));
  34938. /* Cleanup */
  34939. X509_free(x509);
  34940. res = TEST_RES_CHECK(1);
  34941. #endif
  34942. return res;
  34943. }
  34944. #ifndef NO_BIO
  34945. static int test_wolfSSL_BIO_gets(void)
  34946. {
  34947. int res = TEST_SKIPPED;
  34948. #if defined(OPENSSL_EXTRA)
  34949. BIO* bio;
  34950. BIO* bio2;
  34951. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  34952. char emp[] = "";
  34953. char bio_buffer[20];
  34954. int bufferSz = 20;
  34955. /* try with bad args */
  34956. AssertNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  34957. /* try with real msg */
  34958. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  34959. XMEMSET(bio_buffer, 0, bufferSz);
  34960. AssertNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  34961. AssertNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  34962. AssertNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  34963. AssertFalse(bio2 != BIO_next(bio));
  34964. /* make buffer filled with no terminating characters */
  34965. XMEMSET(bio_buffer, 1, bufferSz);
  34966. /* BIO_gets reads a line of data */
  34967. AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  34968. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  34969. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  34970. AssertStrEQ(bio_buffer, "hello wolfSSL\n");
  34971. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  34972. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  34973. AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  34974. /* check not null terminated string */
  34975. BIO_free(bio);
  34976. msg[0] = 0x33;
  34977. msg[1] = 0x33;
  34978. msg[2] = 0x33;
  34979. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  34980. AssertIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  34981. AssertIntEQ(bio_buffer[0], msg[0]);
  34982. AssertIntEQ(bio_buffer[1], msg[1]);
  34983. AssertIntNE(bio_buffer[2], msg[2]);
  34984. BIO_free(bio);
  34985. msg[3] = 0x33;
  34986. bio_buffer[3] = 0x33;
  34987. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  34988. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  34989. AssertIntEQ(bio_buffer[0], msg[0]);
  34990. AssertIntEQ(bio_buffer[1], msg[1]);
  34991. AssertIntEQ(bio_buffer[2], msg[2]);
  34992. AssertIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  34993. /* check reading an empty string */
  34994. BIO_free(bio);
  34995. AssertNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  34996. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  34997. AssertStrEQ(emp, bio_buffer);
  34998. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  34999. /* check error cases */
  35000. BIO_free(bio);
  35001. AssertIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  35002. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  35003. AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  35004. #if !defined(NO_FILESYSTEM)
  35005. {
  35006. BIO* f_bio;
  35007. XFILE f;
  35008. AssertNotNull(f_bio = BIO_new(BIO_s_file()));
  35009. AssertIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  35010. f = XFOPEN(svrCertFile, "rb");
  35011. AssertTrue((f != XBADFILE));
  35012. AssertIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  35013. AssertIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  35014. BIO_free(f_bio);
  35015. }
  35016. #endif /* NO_FILESYSTEM */
  35017. BIO_free(bio);
  35018. BIO_free(bio2);
  35019. /* try with type BIO */
  35020. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  35021. sizeof(msg));
  35022. AssertNotNull(bio = BIO_new(BIO_s_bio()));
  35023. AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  35024. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  35025. AssertIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  35026. AssertIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  35027. AssertIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  35028. AssertIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  35029. AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  35030. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  35031. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  35032. AssertStrEQ(bio_buffer, "hello wolfSSL\n");
  35033. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  35034. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  35035. AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  35036. BIO_free(bio);
  35037. BIO_free(bio2);
  35038. /* check reading an empty string */
  35039. AssertNotNull(bio = BIO_new(BIO_s_bio()));
  35040. AssertIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  35041. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  35042. AssertStrEQ(emp, bio_buffer);
  35043. BIO_free(bio);
  35044. res = TEST_RES_CHECK(1);
  35045. #endif
  35046. return res;
  35047. }
  35048. static int test_wolfSSL_BIO_puts(void)
  35049. {
  35050. int res = TEST_SKIPPED;
  35051. #if defined(OPENSSL_EXTRA)
  35052. BIO* bio;
  35053. char input[] = "hello\0world\n.....ok\n\0";
  35054. char output[128];
  35055. XMEMSET(output, 0, sizeof(output));
  35056. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  35057. AssertIntEQ(BIO_puts(bio, input), 5);
  35058. AssertIntEQ(BIO_pending(bio), 5);
  35059. AssertIntEQ(BIO_puts(bio, input + 6), 14);
  35060. AssertIntEQ(BIO_pending(bio), 19);
  35061. AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  35062. AssertStrEQ(output, "helloworld\n");
  35063. AssertIntEQ(BIO_pending(bio), 8);
  35064. AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  35065. AssertStrEQ(output, ".....ok\n");
  35066. AssertIntEQ(BIO_pending(bio), 0);
  35067. AssertIntEQ(BIO_puts(bio, ""), -1);
  35068. BIO_free(bio);
  35069. res = TEST_RES_CHECK(1);
  35070. #endif
  35071. return res;
  35072. }
  35073. static int test_wolfSSL_BIO_dump(void)
  35074. {
  35075. int res = TEST_SKIPPED;
  35076. #if defined(OPENSSL_EXTRA)
  35077. BIO* bio;
  35078. static const unsigned char data[] = {
  35079. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  35080. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  35081. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  35082. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  35083. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  35084. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  35085. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  35086. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  35087. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  35088. 0xB4
  35089. };
  35090. /* Generated with OpenSSL. */
  35091. static const char expected[] =
  35092. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  35093. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  35094. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  35095. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  35096. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  35097. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  35098. static const char expectedAll[] =
  35099. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  35100. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  35101. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  35102. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  35103. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  35104. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  35105. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  35106. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  35107. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  35108. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  35109. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  35110. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  35111. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  35112. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  35113. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  35114. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  35115. char output[16 * 80];
  35116. int i;
  35117. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  35118. /* Example key dumped. */
  35119. AssertIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  35120. sizeof(expected) - 1);
  35121. AssertIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  35122. AssertIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  35123. /* Try every possible value for a character. */
  35124. for (i = 0; i < 256; i++)
  35125. output[i] = i;
  35126. AssertIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  35127. AssertIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  35128. AssertIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  35129. BIO_free(bio);
  35130. res = TEST_RES_CHECK(1);
  35131. #endif
  35132. return res;
  35133. }
  35134. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  35135. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  35136. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  35137. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  35138. {
  35139. (void)ssl;
  35140. (void)buf;
  35141. (void)sz;
  35142. (void)ctx;
  35143. return WOLFSSL_CBIO_ERR_WANT_READ;
  35144. }
  35145. #endif
  35146. static int test_wolfSSL_BIO_should_retry(void)
  35147. {
  35148. int res = TEST_SKIPPED;
  35149. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  35150. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  35151. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  35152. tcp_ready ready;
  35153. func_args server_args;
  35154. THREAD_TYPE serverThread;
  35155. SOCKET_T sockfd = 0;
  35156. WOLFSSL_CTX* ctx;
  35157. WOLFSSL* ssl;
  35158. char msg[64] = "hello wolfssl!";
  35159. char reply[1024];
  35160. int msgSz = (int)XSTRLEN(msg);
  35161. int ret;
  35162. BIO* bio;
  35163. XMEMSET(&server_args, 0, sizeof(func_args));
  35164. #ifdef WOLFSSL_TIRTOS
  35165. fdOpenSession(Task_self());
  35166. #endif
  35167. StartTCP();
  35168. InitTcpReady(&ready);
  35169. #if defined(USE_WINDOWS_API)
  35170. /* use RNG to get random port if using windows */
  35171. ready.port = GetRandomPort();
  35172. #endif
  35173. server_args.signal = &ready;
  35174. start_thread(test_server_nofail, &server_args, &serverThread);
  35175. wait_tcp_ready(&server_args);
  35176. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35177. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  35178. AssertIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  35179. #endif
  35180. AssertIntEQ(WOLFSSL_SUCCESS,
  35181. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  35182. AssertIntEQ(WOLFSSL_SUCCESS,
  35183. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  35184. AssertIntEQ(WOLFSSL_SUCCESS,
  35185. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  35186. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  35187. /* force retry */
  35188. ssl = wolfSSL_new(ctx);
  35189. AssertNotNull(ssl);
  35190. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  35191. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  35192. AssertNotNull(bio = BIO_new(BIO_f_ssl()));
  35193. BIO_set_ssl(bio, ssl, BIO_CLOSE);
  35194. AssertIntLE(BIO_write(bio, msg, msgSz), 0);
  35195. AssertIntNE(BIO_should_retry(bio), 0);
  35196. /* now perform successful connection */
  35197. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  35198. AssertIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  35199. BIO_read(bio, reply, sizeof(reply));
  35200. ret = wolfSSL_get_error(ssl, -1);
  35201. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  35202. AssertIntNE(BIO_should_retry(bio), 0);
  35203. }
  35204. else {
  35205. AssertIntEQ(BIO_should_retry(bio), 0);
  35206. }
  35207. AssertIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  35208. XSTRLEN("I hear you fa shizzle!")), 0);
  35209. BIO_free(bio);
  35210. wolfSSL_CTX_free(ctx);
  35211. join_thread(serverThread);
  35212. FreeTcpReady(&ready);
  35213. #ifdef WOLFSSL_TIRTOS
  35214. fdOpenSession(Task_self());
  35215. #endif
  35216. res = TEST_RES_CHECK(1);
  35217. #endif
  35218. return res;
  35219. }
  35220. static int test_wolfSSL_BIO_connect(void)
  35221. {
  35222. int res = TEST_SKIPPED;
  35223. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  35224. defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
  35225. tcp_ready ready;
  35226. func_args server_args;
  35227. THREAD_TYPE serverThread;
  35228. BIO *tcpBio;
  35229. BIO *sslBio;
  35230. SSL_CTX* ctx;
  35231. SSL *ssl;
  35232. SSL *sslPtr;
  35233. char msg[] = "hello wolfssl!";
  35234. char reply[30];
  35235. char buff[10] = {0};
  35236. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35237. AssertIntEQ(WOLFSSL_SUCCESS,
  35238. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  35239. AssertIntEQ(WOLFSSL_SUCCESS,
  35240. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  35241. AssertIntEQ(WOLFSSL_SUCCESS,
  35242. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  35243. /* Setup server */
  35244. XMEMSET(&server_args, 0, sizeof(func_args));
  35245. StartTCP();
  35246. InitTcpReady(&ready);
  35247. #if defined(USE_WINDOWS_API)
  35248. /* use RNG to get random port if using windows */
  35249. ready.port = GetRandomPort();
  35250. #endif
  35251. server_args.signal = &ready;
  35252. start_thread(test_server_nofail, &server_args, &serverThread);
  35253. wait_tcp_ready(&server_args);
  35254. AssertIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  35255. /* Start the test proper */
  35256. /* Setup the TCP BIO */
  35257. AssertNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  35258. AssertIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  35259. /* Setup the SSL object */
  35260. AssertNotNull(ssl = SSL_new(ctx));
  35261. SSL_set_connect_state(ssl);
  35262. /* Setup the SSL BIO */
  35263. AssertNotNull(sslBio = BIO_new(BIO_f_ssl()));
  35264. AssertIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  35265. /* Verify that BIO_get_ssl works. */
  35266. AssertIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  35267. AssertPtrEq(ssl, sslPtr);
  35268. /* Link BIO's so that sslBio uses tcpBio for IO */
  35269. AssertPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  35270. /* Do TCP connect */
  35271. AssertIntEQ(BIO_do_connect(sslBio), 1);
  35272. /* Do TLS handshake */
  35273. AssertIntEQ(BIO_do_handshake(sslBio), 1);
  35274. /* Test writing */
  35275. AssertIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  35276. /* Expect length of default wolfSSL reply */
  35277. AssertIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  35278. /* Clean it all up */
  35279. BIO_free_all(sslBio);
  35280. /* Server clean up */
  35281. join_thread(serverThread);
  35282. FreeTcpReady(&ready);
  35283. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  35284. * after. */
  35285. XMEMSET(&server_args, 0, sizeof(func_args));
  35286. StartTCP();
  35287. InitTcpReady(&ready);
  35288. #if defined(USE_WINDOWS_API)
  35289. /* use RNG to get random port if using windows */
  35290. ready.port = GetRandomPort();
  35291. #endif
  35292. server_args.signal = &ready;
  35293. start_thread(test_server_nofail, &server_args, &serverThread);
  35294. wait_tcp_ready(&server_args);
  35295. AssertIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  35296. AssertNotNull(sslBio = BIO_new_ssl_connect(ctx));
  35297. AssertIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  35298. AssertIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  35299. AssertIntEQ(BIO_do_connect(sslBio), 1);
  35300. AssertIntEQ(BIO_do_handshake(sslBio), 1);
  35301. AssertIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  35302. AssertIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  35303. /* Attempt to close the TLS connection gracefully. */
  35304. BIO_ssl_shutdown(sslBio);
  35305. BIO_free_all(sslBio);
  35306. join_thread(serverThread);
  35307. FreeTcpReady(&ready);
  35308. SSL_CTX_free(ctx);
  35309. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  35310. wc_ecc_fp_free(); /* free per thread cache */
  35311. #endif
  35312. res = TEST_RES_CHECK(1);
  35313. #endif
  35314. return res;
  35315. }
  35316. static int test_wolfSSL_BIO_tls(void)
  35317. {
  35318. int res = TEST_SKIPPED;
  35319. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  35320. SSL_CTX* ctx;
  35321. SSL *ssl;
  35322. BIO *readBio;
  35323. BIO *writeBio;
  35324. int ret, err = 0;
  35325. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  35326. AssertNotNull(ssl = SSL_new(ctx));
  35327. AssertNotNull(readBio = BIO_new(BIO_s_mem()));
  35328. AssertNotNull(writeBio = BIO_new(BIO_s_mem()));
  35329. /* Qt reads data from write-bio,
  35330. * then writes the read data into plain packet.
  35331. * Qt reads data from plain packet,
  35332. * then writes the read data into read-bio.
  35333. */
  35334. SSL_set_bio(ssl, readBio, writeBio);
  35335. do {
  35336. #ifdef WOLFSSL_ASYNC_CRYPT
  35337. if (err == WC_PENDING_E) {
  35338. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  35339. if (ret < 0) { break; } else if (ret == 0) { continue; }
  35340. }
  35341. #endif
  35342. ret = SSL_connect(ssl);
  35343. err = SSL_get_error(ssl, 0);
  35344. } while (err == WC_PENDING_E);
  35345. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  35346. /* in this use case, should return WANT READ
  35347. * so that Qt will read the data from plain packet for next state.
  35348. */
  35349. AssertIntEQ(err, SSL_ERROR_WANT_READ);
  35350. SSL_free(ssl);
  35351. SSL_CTX_free(ctx);
  35352. res = TEST_RES_CHECK(1);
  35353. #endif
  35354. return res;
  35355. }
  35356. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
  35357. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  35358. {
  35359. BIO* clientBio;
  35360. SSL* sslClient;
  35361. SSL_CTX* ctx;
  35362. char connectAddr[20]; /* IP + port */;
  35363. (void)args;
  35364. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  35365. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  35366. AssertIntEQ(BIO_do_connect(clientBio), 1);
  35367. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  35368. AssertNotNull(sslClient = SSL_new(ctx));
  35369. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  35370. SSL_set_bio(sslClient, clientBio, clientBio);
  35371. AssertIntEQ(SSL_connect(sslClient), 1);
  35372. SSL_free(sslClient);
  35373. SSL_CTX_free(ctx);
  35374. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  35375. wc_ecc_fp_free(); /* free per thread cache */
  35376. #endif
  35377. return 0;
  35378. }
  35379. #endif
  35380. static int test_wolfSSL_BIO_accept(void)
  35381. {
  35382. int res = TEST_SKIPPED;
  35383. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
  35384. BIO* serverBindBio;
  35385. BIO* serverAcceptBio;
  35386. SSL* sslServer;
  35387. SSL_CTX* ctx;
  35388. func_args args;
  35389. THREAD_TYPE thread;
  35390. char port[10]; /* 10 bytes should be enough to store the string
  35391. * representation of the port */
  35392. AssertIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  35393. AssertNotNull(serverBindBio = BIO_new_accept(port));
  35394. /* First BIO_do_accept binds the port */
  35395. AssertIntEQ(BIO_do_accept(serverBindBio), 1);
  35396. XMEMSET(&args, 0, sizeof(func_args));
  35397. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  35398. AssertIntEQ(BIO_do_accept(serverBindBio), 1);
  35399. /* Let's plug it into SSL to test */
  35400. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  35401. AssertIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  35402. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  35403. AssertNotNull(sslServer = SSL_new(ctx));
  35404. AssertNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  35405. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  35406. AssertIntEQ(SSL_accept(sslServer), 1);
  35407. join_thread(thread);
  35408. BIO_free(serverBindBio);
  35409. SSL_free(sslServer);
  35410. SSL_CTX_free(ctx);
  35411. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  35412. wc_ecc_fp_free(); /* free per thread cache */
  35413. #endif
  35414. res = TEST_RES_CHECK(1);
  35415. #endif
  35416. return res;
  35417. }
  35418. static int test_wolfSSL_BIO_write(void)
  35419. {
  35420. int res = TEST_SKIPPED;
  35421. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  35422. BIO* bio;
  35423. BIO* bio64;
  35424. BIO* ptr;
  35425. int sz;
  35426. char msg[] = "conversion test";
  35427. char out[40];
  35428. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  35429. void* bufPtr = NULL;
  35430. BUF_MEM* buf = NULL;
  35431. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  35432. AssertNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  35433. /* now should convert to base64 then write to memory */
  35434. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  35435. BIO_flush(bio);
  35436. /* test BIO chain */
  35437. AssertIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  35438. AssertNotNull(buf);
  35439. AssertIntEQ(buf->length, 25);
  35440. AssertIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  35441. AssertPtrEq(buf->data, bufPtr);
  35442. AssertNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  35443. sz = sizeof(out);
  35444. XMEMSET(out, 0, sz);
  35445. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  35446. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  35447. /* write then read should return the same message */
  35448. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  35449. sz = sizeof(out);
  35450. XMEMSET(out, 0, sz);
  35451. AssertIntEQ(BIO_read(bio, out, sz), 16);
  35452. AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  35453. /* now try encoding with no line ending */
  35454. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  35455. #ifdef HAVE_EX_DATA
  35456. BIO_set_ex_data(bio64, 0, (void*) "data");
  35457. AssertIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  35458. #endif
  35459. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  35460. BIO_flush(bio);
  35461. sz = sizeof(out);
  35462. XMEMSET(out, 0, sz);
  35463. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  35464. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  35465. BIO_free_all(bio); /* frees bio64 also */
  35466. /* test with more than one bio64 in list */
  35467. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  35468. AssertNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  35469. AssertNotNull(BIO_push(bio64, BIO_new(BIO_s_mem())));
  35470. /* now should convert to base64 when stored and then decode with read */
  35471. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  35472. BIO_flush(bio);
  35473. sz = sizeof(out);
  35474. XMEMSET(out, 0, sz);
  35475. AssertIntEQ((sz = BIO_read(bio, out, sz)), 16);
  35476. AssertIntEQ(XMEMCMP(out, msg, sz), 0);
  35477. BIO_clear_flags(bio64, ~0);
  35478. BIO_set_retry_read(bio);
  35479. BIO_free_all(bio); /* frees bio64s also */
  35480. AssertNotNull(bio = BIO_new_mem_buf(out, 0));
  35481. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  35482. BIO_free(bio);
  35483. res = TEST_RES_CHECK(1);
  35484. #endif
  35485. return res;
  35486. }
  35487. static int test_wolfSSL_BIO_printf(void)
  35488. {
  35489. int res = TEST_SKIPPED;
  35490. #if defined(OPENSSL_ALL)
  35491. BIO* bio;
  35492. int sz = 7;
  35493. char msg[] = "TLS 1.3 for the world";
  35494. char out[60];
  35495. char expected[] = "TLS 1.3 for the world : sz = 7";
  35496. XMEMSET(out, 0, sizeof(out));
  35497. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  35498. AssertIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  35499. AssertIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  35500. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  35501. AssertIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  35502. BIO_free(bio);
  35503. res = TEST_RES_CHECK(1);
  35504. #endif
  35505. return res;
  35506. }
  35507. static int test_wolfSSL_BIO_f_md(void)
  35508. {
  35509. int res = TEST_SKIPPED;
  35510. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  35511. BIO *bio, *mem;
  35512. char msg[] = "message to hash";
  35513. char out[60];
  35514. EVP_MD_CTX* ctx;
  35515. const unsigned char testKey[] =
  35516. {
  35517. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  35518. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  35519. 0x0b, 0x0b, 0x0b, 0x0b
  35520. };
  35521. const char testData[] = "Hi There";
  35522. const unsigned char testResult[] =
  35523. {
  35524. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  35525. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  35526. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  35527. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  35528. };
  35529. const unsigned char expectedHash[] =
  35530. {
  35531. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  35532. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  35533. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  35534. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  35535. };
  35536. const unsigned char emptyHash[] =
  35537. {
  35538. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  35539. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  35540. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  35541. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  35542. };
  35543. unsigned char check[sizeof(testResult) + 1];
  35544. size_t checkSz = -1;
  35545. EVP_PKEY* key;
  35546. XMEMSET(out, 0, sizeof(out));
  35547. AssertNotNull(bio = BIO_new(BIO_f_md()));
  35548. AssertNotNull(mem = BIO_new(BIO_s_mem()));
  35549. AssertIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  35550. AssertIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  35551. /* should not be able to write/read yet since just digest wrapper and no
  35552. * data is passing through the bio */
  35553. AssertIntEQ(BIO_write(bio, msg, 0), 0);
  35554. AssertIntEQ(BIO_pending(bio), 0);
  35555. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  35556. AssertIntEQ(BIO_gets(bio, out, 3), 0);
  35557. AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  35558. AssertIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  35559. BIO_reset(bio);
  35560. /* append BIO mem to bio in order to read/write */
  35561. AssertNotNull(bio = BIO_push(bio, mem));
  35562. XMEMSET(out, 0, sizeof(out));
  35563. AssertIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  35564. AssertIntEQ(BIO_pending(bio), 16);
  35565. /* this just reads the message and does not hash it (gets calls final) */
  35566. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  35567. AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  35568. /* create a message digest using BIO */
  35569. XMEMSET(out, 0, sizeof(out));
  35570. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  35571. AssertIntEQ(BIO_pending(mem), 16);
  35572. AssertIntEQ(BIO_pending(bio), 16);
  35573. AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  35574. AssertIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  35575. BIO_free(bio);
  35576. BIO_free(mem);
  35577. /* test with HMAC */
  35578. XMEMSET(out, 0, sizeof(out));
  35579. AssertNotNull(bio = BIO_new(BIO_f_md()));
  35580. AssertNotNull(mem = BIO_new(BIO_s_mem()));
  35581. BIO_get_md_ctx(bio, &ctx);
  35582. AssertNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  35583. testKey, (int)sizeof(testKey)));
  35584. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  35585. AssertNotNull(bio = BIO_push(bio, mem));
  35586. BIO_write(bio, testData, (int)strlen(testData));
  35587. EVP_DigestSignFinal(ctx, NULL, &checkSz);
  35588. EVP_DigestSignFinal(ctx, check, &checkSz);
  35589. AssertIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  35590. EVP_PKEY_free(key);
  35591. BIO_free(bio);
  35592. BIO_free(mem);
  35593. res = TEST_RES_CHECK(1);
  35594. #endif
  35595. return res;
  35596. }
  35597. static int test_wolfSSL_BIO_up_ref(void)
  35598. {
  35599. int res = TEST_SKIPPED;
  35600. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  35601. BIO* bio;
  35602. AssertNotNull(bio = BIO_new(BIO_f_md()));
  35603. AssertIntEQ(BIO_up_ref(NULL), 0);
  35604. AssertIntEQ(BIO_up_ref(bio), 1);
  35605. BIO_free(bio);
  35606. AssertIntEQ(BIO_up_ref(bio), 1);
  35607. BIO_free(bio);
  35608. BIO_free(bio);
  35609. res = TEST_RES_CHECK(1);
  35610. #endif
  35611. return res;
  35612. }
  35613. #endif /* !NO_BIO */
  35614. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  35615. /* test that the callback arg is correct */
  35616. static int certCbArg = 0;
  35617. static int clientCertCb(WOLFSSL* ssl, void* arg)
  35618. {
  35619. if (ssl == NULL || arg != &certCbArg)
  35620. return 0;
  35621. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  35622. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  35623. return 0;
  35624. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  35625. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  35626. return 0;
  35627. return 1;
  35628. }
  35629. static void clientCertSetupCb(WOLFSSL_CTX* ctx)
  35630. {
  35631. SSL_CTX_set_cert_cb(ctx, clientCertCb, &certCbArg);
  35632. }
  35633. /**
  35634. * This is only done because test_client_nofail has no way to stop
  35635. * certificate and key loading
  35636. */
  35637. static void clientCertClearCb(WOLFSSL* ssl)
  35638. {
  35639. /* Clear the loaded certs to force the callbacks to set them up */
  35640. SSL_certs_clear(ssl);
  35641. }
  35642. static int serverCertCb(WOLFSSL* ssl, void* arg)
  35643. {
  35644. if (ssl == NULL || arg != &certCbArg)
  35645. return 0;
  35646. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  35647. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  35648. return 0;
  35649. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  35650. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  35651. return 0;
  35652. return 1;
  35653. }
  35654. static void serverCertSetupCb(WOLFSSL_CTX* ctx)
  35655. {
  35656. SSL_CTX_set_cert_cb(ctx, serverCertCb, &certCbArg);
  35657. }
  35658. /**
  35659. * This is only done because test_server_nofail has no way to stop
  35660. * certificate and key loading
  35661. */
  35662. static void serverCertClearCb(WOLFSSL* ssl)
  35663. {
  35664. /* Clear the loaded certs to force the callbacks to set them up */
  35665. SSL_certs_clear(ssl);
  35666. }
  35667. #endif
  35668. static int test_wolfSSL_cert_cb(void)
  35669. {
  35670. int res = TEST_SKIPPED;
  35671. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  35672. callback_functions func_cb_client;
  35673. callback_functions func_cb_server;
  35674. tcp_ready ready;
  35675. func_args client_args;
  35676. func_args server_args;
  35677. THREAD_TYPE serverThread;
  35678. XMEMSET(&client_args, 0, sizeof(func_args));
  35679. XMEMSET(&server_args, 0, sizeof(func_args));
  35680. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  35681. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  35682. #ifdef WOLFSSL_TIRTOS
  35683. fdOpenSession(Task_self());
  35684. #endif
  35685. StartTCP();
  35686. InitTcpReady(&ready);
  35687. #if defined(USE_WINDOWS_API)
  35688. /* use RNG to get random port if using windows */
  35689. ready.port = GetRandomPort();
  35690. #endif
  35691. server_args.signal = &ready;
  35692. client_args.signal = &ready;
  35693. client_args.callbacks = &func_cb_client;
  35694. server_args.callbacks = &func_cb_server;
  35695. func_cb_client.ctx_ready = clientCertSetupCb;
  35696. func_cb_client.ssl_ready = clientCertClearCb;
  35697. func_cb_server.ctx_ready = serverCertSetupCb;
  35698. func_cb_server.ssl_ready = serverCertClearCb;
  35699. start_thread(test_server_nofail, &server_args, &serverThread);
  35700. wait_tcp_ready(&server_args);
  35701. test_client_nofail(&client_args, NULL);
  35702. join_thread(serverThread);
  35703. AssertTrue(client_args.return_code);
  35704. AssertTrue(server_args.return_code);
  35705. FreeTcpReady(&ready);
  35706. #ifdef WOLFSSL_TIRTOS
  35707. fdOpenSession(Task_self());
  35708. #endif
  35709. res = TEST_RES_CHECK(1);
  35710. #endif
  35711. return res;
  35712. }
  35713. static int test_wolfSSL_SESSION(void)
  35714. {
  35715. int res = TEST_SKIPPED;
  35716. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  35717. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  35718. !defined(NO_SESSION_CACHE)
  35719. WOLFSSL* ssl;
  35720. WOLFSSL_CTX* ctx;
  35721. WOLFSSL_SESSION* sess;
  35722. WOLFSSL_SESSION* sess_copy;
  35723. #ifdef OPENSSL_EXTRA
  35724. unsigned char* sessDer = NULL;
  35725. unsigned char* ptr = NULL;
  35726. const unsigned char context[] = "user app context";
  35727. unsigned int contextSz = (unsigned int)sizeof(context);
  35728. int sz;
  35729. #endif
  35730. int ret, err;
  35731. SOCKET_T sockfd;
  35732. tcp_ready ready;
  35733. func_args server_args;
  35734. THREAD_TYPE serverThread;
  35735. char msg[80];
  35736. const char* sendGET = "GET";
  35737. /* TLS v1.3 requires session tickets */
  35738. /* CHACHA and POLY1305 required for myTicketEncCb */
  35739. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  35740. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  35741. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  35742. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  35743. #else
  35744. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35745. #endif
  35746. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  35747. WOLFSSL_FILETYPE_PEM));
  35748. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  35749. WOLFSSL_FILETYPE_PEM));
  35750. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  35751. WOLFSSL_SUCCESS);
  35752. #ifdef WOLFSSL_ENCRYPTED_KEYS
  35753. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  35754. #endif
  35755. #ifdef HAVE_SESSION_TICKET
  35756. /* Use session tickets, for ticket tests below */
  35757. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  35758. #endif
  35759. XMEMSET(&server_args, 0, sizeof(func_args));
  35760. #ifdef WOLFSSL_TIRTOS
  35761. fdOpenSession(Task_self());
  35762. #endif
  35763. StartTCP();
  35764. InitTcpReady(&ready);
  35765. #if defined(USE_WINDOWS_API)
  35766. /* use RNG to get random port if using windows */
  35767. ready.port = GetRandomPort();
  35768. #endif
  35769. server_args.signal = &ready;
  35770. start_thread(test_server_nofail, &server_args, &serverThread);
  35771. wait_tcp_ready(&server_args);
  35772. /* client connection */
  35773. ssl = wolfSSL_new(ctx);
  35774. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  35775. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  35776. #ifdef WOLFSSL_ASYNC_CRYPT
  35777. err = 0; /* Reset error */
  35778. #endif
  35779. do {
  35780. #ifdef WOLFSSL_ASYNC_CRYPT
  35781. if (err == WC_PENDING_E) {
  35782. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  35783. if (ret < 0) { break; } else if (ret == 0) { continue; }
  35784. }
  35785. #endif
  35786. ret = wolfSSL_connect(ssl);
  35787. err = wolfSSL_get_error(ssl, 0);
  35788. } while (err == WC_PENDING_E);
  35789. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  35790. #ifdef WOLFSSL_ASYNC_CRYPT
  35791. err = 0; /* Reset error */
  35792. #endif
  35793. do {
  35794. #ifdef WOLFSSL_ASYNC_CRYPT
  35795. if (err == WC_PENDING_E) {
  35796. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  35797. if (ret < 0) { break; } else if (ret == 0) { continue; }
  35798. }
  35799. #endif
  35800. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  35801. err = wolfSSL_get_error(ssl, 0);
  35802. } while (err == WC_PENDING_E);
  35803. AssertIntEQ(ret, (int)XSTRLEN(sendGET));
  35804. #ifdef WOLFSSL_ASYNC_CRYPT
  35805. err = 0; /* Reset error */
  35806. #endif
  35807. do {
  35808. #ifdef WOLFSSL_ASYNC_CRYPT
  35809. if (err == WC_PENDING_E) {
  35810. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  35811. if (ret < 0) { break; } else if (ret == 0) { continue; }
  35812. }
  35813. #endif
  35814. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  35815. err = wolfSSL_get_error(ssl, 0);
  35816. } while (err == WC_PENDING_E);
  35817. AssertIntEQ(ret, 23);
  35818. AssertPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  35819. AssertPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  35820. #ifdef HAVE_EXT_CACHE
  35821. AssertPtrEq(sess, sess_copy); /* they should be the same pointer but without
  35822. * HAVE_EXT_CACHE we get new objects each time */
  35823. #endif
  35824. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  35825. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  35826. sess = wolfSSL_get_session(ssl);
  35827. #ifdef OPENSSL_EXTRA
  35828. AssertIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  35829. AssertIntEQ(SSL_SESSION_is_resumable(sess), 1);
  35830. AssertIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  35831. AssertIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  35832. #ifdef HAVE_SESSION_TICKET
  35833. AssertIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  35834. AssertIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  35835. SESSION_TICKET_HINT_DEFAULT);
  35836. #else
  35837. AssertIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  35838. #endif
  35839. #else
  35840. (void)sess;
  35841. #endif /* OPENSSL_EXTRA */
  35842. /* Retain copy of the session for later testing */
  35843. AssertNotNull(sess = wolfSSL_get1_session(ssl));
  35844. wolfSSL_shutdown(ssl);
  35845. wolfSSL_free(ssl);
  35846. join_thread(serverThread);
  35847. FreeTcpReady(&ready);
  35848. #ifdef WOLFSSL_TIRTOS
  35849. fdOpenSession(Task_self());
  35850. #endif
  35851. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  35852. {
  35853. X509 *x509;
  35854. char buf[30];
  35855. int bufSz;
  35856. AssertNotNull(x509 = SSL_SESSION_get0_peer(sess));
  35857. AssertIntGT((bufSz = X509_NAME_get_text_by_NID(
  35858. X509_get_subject_name(x509), NID_organizationalUnitName,
  35859. buf, sizeof(buf))), 0);
  35860. AssertIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  35861. if (bufSz == 7) {
  35862. AssertIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  35863. }
  35864. if (bufSz == 16) {
  35865. AssertIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  35866. }
  35867. }
  35868. #endif
  35869. #ifdef HAVE_EXT_CACHE
  35870. AssertNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  35871. wolfSSL_SESSION_free(sess_copy);
  35872. sess_copy = NULL;
  35873. #endif
  35874. #ifdef OPENSSL_EXTRA
  35875. /* get session from DER and update the timeout */
  35876. AssertIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  35877. AssertIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  35878. wolfSSL_SESSION_free(sess);
  35879. sess = NULL;
  35880. ptr = sessDer;
  35881. AssertNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  35882. AssertNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  35883. (const unsigned char**)&ptr, sz));
  35884. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  35885. sessDer = NULL;
  35886. AssertIntGT(wolfSSL_SESSION_get_time(sess), 0);
  35887. AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  35888. #endif
  35889. /* successful set session test */
  35890. AssertNotNull(ssl = wolfSSL_new(ctx));
  35891. AssertIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  35892. #ifdef HAVE_SESSION_TICKET
  35893. /* Test set/get session ticket */
  35894. {
  35895. const char* ticket = "This is a session ticket";
  35896. char buf[64] = {0};
  35897. word32 bufSz = (word32)sizeof(buf);
  35898. AssertIntEQ(SSL_SUCCESS,
  35899. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  35900. (word32)XSTRLEN(ticket)));
  35901. AssertIntEQ(SSL_SUCCESS,
  35902. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  35903. AssertStrEQ(ticket, buf);
  35904. }
  35905. #endif
  35906. #ifdef OPENSSL_EXTRA
  35907. /* session timeout case */
  35908. /* make the session to be expired */
  35909. AssertIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  35910. XSLEEP_MS(1200);
  35911. /* SSL_set_session should reject specified session but return success
  35912. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  35913. */
  35914. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  35915. AssertIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  35916. #else
  35917. AssertIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  35918. #endif
  35919. AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  35920. /* fail case with miss match session context IDs (use compatibility API) */
  35921. AssertIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  35922. SSL_SUCCESS);
  35923. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  35924. wolfSSL_free(ssl);
  35925. AssertIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  35926. SSL_FAILURE);
  35927. AssertIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  35928. SSL_SUCCESS);
  35929. AssertNotNull(ssl = wolfSSL_new(ctx));
  35930. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  35931. #endif /* OPENSSL_EXTRA */
  35932. wolfSSL_free(ssl);
  35933. wolfSSL_SESSION_free(sess);
  35934. wolfSSL_CTX_free(ctx);
  35935. res = TEST_RES_CHECK(1);
  35936. #endif
  35937. return res;
  35938. }
  35939. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  35940. defined(HAVE_EX_DATA)
  35941. static int clientSessRemCountMalloc = 0;
  35942. static int serverSessRemCountMalloc = 0;
  35943. static int clientSessRemCountFree = 0;
  35944. static int serverSessRemCountFree = 0;
  35945. static WOLFSSL_CTX* serverSessCtx = NULL;
  35946. static WOLFSSL_SESSION* serverSess = NULL;
  35947. #ifndef NO_SESSION_CACHE_REF
  35948. static WOLFSSL_CTX* clientSessCtx = NULL;
  35949. static WOLFSSL_SESSION* clientSess = NULL;
  35950. #endif
  35951. static int serverSessRemIdx = 3;
  35952. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  35953. {
  35954. int* mallocedData = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  35955. (void)ctx;
  35956. AssertNotNull(mallocedData);
  35957. if (!*mallocedData)
  35958. clientSessRemCountFree++;
  35959. else
  35960. serverSessRemCountFree++;
  35961. XFREE(mallocedData, NULL, DYNAMIC_TYPE_SESSION);
  35962. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  35963. }
  35964. static void SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  35965. {
  35966. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  35967. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  35968. !defined(NO_SESSION_CACHE_REF)
  35969. /* Allow downgrade, set min version, and disable TLS 1.3.
  35970. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  35971. * reference to the session cache. But with WOLFSSL_TLS13 and without
  35972. * HAVE_SESSION_TICKET we won't have a session ID to be able to place the
  35973. * session in the cache. In this case we need to downgrade to previous
  35974. * versions to just use the legacy session ID field. */
  35975. AssertIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION), SSL_SUCCESS);
  35976. AssertIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION), SSL_SUCCESS);
  35977. #endif
  35978. }
  35979. static void SessRemSslSetupCb(WOLFSSL* ssl)
  35980. {
  35981. int* mallocedData = (int*)XMALLOC(sizeof(int), NULL, DYNAMIC_TYPE_SESSION);
  35982. AssertNotNull(mallocedData);
  35983. *mallocedData = SSL_is_server(ssl);
  35984. if (!*mallocedData) {
  35985. clientSessRemCountMalloc++;
  35986. #ifndef NO_SESSION_CACHE_REF
  35987. AssertNotNull(clientSess = SSL_get1_session(ssl));
  35988. AssertIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  35989. SSL_SUCCESS);
  35990. #endif
  35991. }
  35992. else {
  35993. serverSessRemCountMalloc++;
  35994. AssertNotNull(serverSess = SSL_get1_session(ssl));
  35995. AssertIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  35996. SSL_SUCCESS);
  35997. }
  35998. AssertIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl), serverSessRemIdx,
  35999. mallocedData), SSL_SUCCESS);
  36000. }
  36001. #endif
  36002. static int test_wolfSSL_CTX_sess_set_remove_cb(void)
  36003. {
  36004. int res = TEST_SKIPPED;
  36005. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  36006. defined(HAVE_EX_DATA)
  36007. /* Check that the remove callback gets called for external data in a
  36008. * session object */
  36009. callback_functions func_cb;
  36010. tcp_ready ready;
  36011. func_args client_args;
  36012. func_args server_args;
  36013. THREAD_TYPE serverThread;
  36014. XMEMSET(&client_args, 0, sizeof(func_args));
  36015. XMEMSET(&server_args, 0, sizeof(func_args));
  36016. XMEMSET(&func_cb, 0, sizeof(callback_functions));
  36017. #ifdef WOLFSSL_TIRTOS
  36018. fdOpenSession(Task_self());
  36019. #endif
  36020. StartTCP();
  36021. InitTcpReady(&ready);
  36022. #if defined(USE_WINDOWS_API)
  36023. /* use RNG to get random port if using windows */
  36024. ready.port = GetRandomPort();
  36025. #endif
  36026. server_args.signal = &ready;
  36027. client_args.signal = &ready;
  36028. client_args.callbacks = &func_cb;
  36029. server_args.callbacks = &func_cb;
  36030. func_cb.ctx_ready = SessRemCtxSetupCb;
  36031. func_cb.on_result = SessRemSslSetupCb;
  36032. start_thread(test_server_nofail, &server_args, &serverThread);
  36033. wait_tcp_ready(&server_args);
  36034. test_client_nofail(&client_args, NULL);
  36035. join_thread(serverThread);
  36036. AssertTrue(client_args.return_code);
  36037. AssertTrue(server_args.return_code);
  36038. FreeTcpReady(&ready);
  36039. #ifdef WOLFSSL_TIRTOS
  36040. fdOpenSession(Task_self());
  36041. #endif
  36042. /* Both should have been allocated */
  36043. AssertIntEQ(clientSessRemCountMalloc, 1);
  36044. AssertIntEQ(serverSessRemCountMalloc, 1);
  36045. #ifdef NO_SESSION_CACHE_REF
  36046. /* Client session should not be added to cache so this should be free'd when
  36047. * the SSL object was being free'd */
  36048. AssertIntEQ(clientSessRemCountFree, 1);
  36049. #else
  36050. /* Client session is in cache due to requiring a persistent reference */
  36051. AssertIntEQ(clientSessRemCountFree, 0);
  36052. /* Force a cache lookup */
  36053. AssertNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  36054. /* Force a cache update */
  36055. AssertNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  36056. /* This should set the timeout to 0 and call the remove callback from within
  36057. * the session cache. */
  36058. AssertIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  36059. AssertNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  36060. AssertIntEQ(clientSessRemCountFree, 1);
  36061. #endif
  36062. /* Server session is in the cache so ex_data isn't free'd with the SSL
  36063. * object */
  36064. AssertIntEQ(serverSessRemCountFree, 0);
  36065. /* Force a cache lookup */
  36066. AssertNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  36067. /* Force a cache update */
  36068. AssertNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  36069. /* This should set the timeout to 0 and call the remove callback from within
  36070. * the session cache. */
  36071. AssertIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  36072. AssertNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  36073. AssertIntEQ(serverSessRemCountFree, 1);
  36074. /* Need to free the references that we kept */
  36075. SSL_CTX_free(serverSessCtx);
  36076. SSL_SESSION_free(serverSess);
  36077. #ifndef NO_SESSION_CACHE_REF
  36078. SSL_CTX_free(clientSessCtx);
  36079. SSL_SESSION_free(clientSess);
  36080. #endif
  36081. res = TEST_RES_CHECK(1);
  36082. #endif
  36083. return res;
  36084. }
  36085. static int test_wolfSSL_ticket_keys(void)
  36086. {
  36087. int res = TEST_SKIPPED;
  36088. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  36089. !defined(NO_WOLFSSL_SERVER)
  36090. WOLFSSL_CTX* ctx;
  36091. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  36092. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36093. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  36094. WOLFSSL_FAILURE);
  36095. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  36096. WOLFSSL_FAILURE);
  36097. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  36098. WOLFSSL_FAILURE);
  36099. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  36100. WOLFSSL_FAILURE);
  36101. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  36102. WOLFSSL_FAILURE);
  36103. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  36104. WOLFSSL_FAILURE);
  36105. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  36106. WOLFSSL_FAILURE);
  36107. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  36108. WOLFSSL_FAILURE);
  36109. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  36110. WOLFSSL_FAILURE);
  36111. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  36112. WOLFSSL_FAILURE);
  36113. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  36114. WOLFSSL_FAILURE);
  36115. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  36116. WOLFSSL_FAILURE);
  36117. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  36118. WOLFSSL_FAILURE);
  36119. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  36120. WOLFSSL_FAILURE);
  36121. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  36122. WOLFSSL_SUCCESS);
  36123. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  36124. WOLFSSL_SUCCESS);
  36125. wolfSSL_CTX_free(ctx);
  36126. res = TEST_RES_CHECK(1);
  36127. #endif
  36128. return res;
  36129. }
  36130. #ifndef NO_BIO
  36131. static int test_wolfSSL_d2i_PUBKEY(void)
  36132. {
  36133. int res = TEST_SKIPPED;
  36134. #if defined(OPENSSL_EXTRA)
  36135. BIO* bio;
  36136. EVP_PKEY* pkey;
  36137. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  36138. AssertNull(d2i_PUBKEY_bio(NULL, NULL));
  36139. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  36140. /* RSA PUBKEY test */
  36141. AssertIntGT(BIO_write(bio, client_keypub_der_2048,
  36142. sizeof_client_keypub_der_2048), 0);
  36143. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  36144. EVP_PKEY_free(pkey);
  36145. #endif
  36146. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  36147. /* ECC PUBKEY test */
  36148. AssertIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  36149. sizeof_ecc_clikeypub_der_256), 0);
  36150. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  36151. EVP_PKEY_free(pkey);
  36152. #endif
  36153. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  36154. /* DSA PUBKEY test */
  36155. AssertIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  36156. sizeof_dsa_pub_key_der_2048), 0);
  36157. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  36158. EVP_PKEY_free(pkey);
  36159. #endif
  36160. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  36161. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  36162. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  36163. (HAVE_FIPS_VERSION > 2))
  36164. /* DH PUBKEY test */
  36165. AssertIntGT(BIO_write(bio, dh_pub_key_der_2048,
  36166. sizeof_dh_pub_key_der_2048), 0);
  36167. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  36168. EVP_PKEY_free(pkey);
  36169. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  36170. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  36171. BIO_free(bio);
  36172. (void)pkey;
  36173. res = TEST_RES_CHECK(1);
  36174. #endif
  36175. return res;
  36176. }
  36177. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  36178. static int test_wolfSSL_d2i_PrivateKeys_bio(void)
  36179. {
  36180. BIO* bio = NULL;
  36181. EVP_PKEY* pkey = NULL;
  36182. #ifndef NO_RSA
  36183. #endif
  36184. WOLFSSL_CTX* ctx;
  36185. #if defined(WOLFSSL_KEY_GEN)
  36186. unsigned char buff[4096];
  36187. unsigned char* bufPtr = buff;
  36188. #endif
  36189. /* test creating new EVP_PKEY with bad arg */
  36190. AssertNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  36191. /* test loading RSA key using BIO */
  36192. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  36193. {
  36194. XFILE file;
  36195. const char* fname = "./certs/server-key.der";
  36196. size_t sz;
  36197. byte* buf;
  36198. file = XFOPEN(fname, "rb");
  36199. AssertTrue((file != XBADFILE));
  36200. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  36201. sz = XFTELL(file);
  36202. XREWIND(file);
  36203. AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  36204. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  36205. XFCLOSE(file);
  36206. /* Test using BIO new mem and loading DER private key */
  36207. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  36208. AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  36209. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  36210. BIO_free(bio);
  36211. bio = NULL;
  36212. EVP_PKEY_free(pkey);
  36213. pkey = NULL;
  36214. }
  36215. #endif
  36216. /* test loading ECC key using BIO */
  36217. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  36218. {
  36219. XFILE file;
  36220. const char* fname = "./certs/ecc-key.der";
  36221. size_t sz;
  36222. byte* buf;
  36223. file = XFOPEN(fname, "rb");
  36224. AssertTrue((file != XBADFILE));
  36225. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  36226. sz = XFTELL(file);
  36227. XREWIND(file);
  36228. AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  36229. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  36230. XFCLOSE(file);
  36231. /* Test using BIO new mem and loading DER private key */
  36232. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  36233. AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  36234. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  36235. BIO_free(bio);
  36236. bio = NULL;
  36237. EVP_PKEY_free(pkey);
  36238. pkey = NULL;
  36239. }
  36240. #endif
  36241. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  36242. #ifndef NO_WOLFSSL_SERVER
  36243. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  36244. #else
  36245. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  36246. #endif
  36247. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  36248. !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  36249. {
  36250. RSA* rsa = NULL;
  36251. /* Tests bad parameters */
  36252. AssertNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  36253. /* RSA not set yet, expecting to fail*/
  36254. AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  36255. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  36256. /* set RSA using bio*/
  36257. AssertIntGT(BIO_write(bio, client_key_der_2048,
  36258. sizeof_client_key_der_2048), 0);
  36259. AssertNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  36260. AssertNotNull(rsa);
  36261. AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  36262. /*i2d RSAprivate key tests */
  36263. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  36264. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  36265. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  36266. sizeof_client_key_der_2048);
  36267. bufPtr -= sizeof_client_key_der_2048;
  36268. AssertIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  36269. sizeof_client_key_der_2048), 0);
  36270. bufPtr = NULL;
  36271. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  36272. sizeof_client_key_der_2048);
  36273. AssertNotNull(bufPtr);
  36274. AssertIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  36275. sizeof_client_key_der_2048), 0);
  36276. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  36277. RSA_free(rsa);
  36278. rsa = RSA_new();
  36279. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  36280. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  36281. RSA_free(rsa);
  36282. }
  36283. #endif /* !HAVE_FAST_RSA && WOLFSSL_KEY_GEN && !NO_RSA && !HAVE_USER_RSA*/
  36284. SSL_CTX_free(ctx);
  36285. ctx = NULL;
  36286. BIO_free(bio);
  36287. bio = NULL;
  36288. return TEST_RES_CHECK(1);
  36289. }
  36290. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  36291. #endif /* !NO_BIO */
  36292. static int test_wolfSSL_sk_GENERAL_NAME(void)
  36293. {
  36294. int res = TEST_SKIPPED;
  36295. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  36296. !defined(NO_RSA)
  36297. X509* x509;
  36298. GENERAL_NAME* gn;
  36299. unsigned char buf[4096];
  36300. const unsigned char* bufPt;
  36301. int bytes, i;
  36302. int j;
  36303. XFILE f;
  36304. STACK_OF(GENERAL_NAME)* sk;
  36305. f = XFOPEN(cliCertDerFileExt, "rb");
  36306. AssertTrue((f != XBADFILE));
  36307. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  36308. XFCLOSE(f);
  36309. for (j = 0; j < 2; ++j) {
  36310. bufPt = buf;
  36311. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  36312. AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  36313. NID_subject_alt_name, NULL, NULL));
  36314. AssertIntEQ(sk_GENERAL_NAME_num(sk), 1);
  36315. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  36316. AssertNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  36317. switch (gn->type) {
  36318. case GEN_DNS:
  36319. fprintf(stderr, "found type GEN_DNS\n");
  36320. break;
  36321. case GEN_EMAIL:
  36322. fprintf(stderr, "found type GEN_EMAIL\n");
  36323. break;
  36324. case GEN_URI:
  36325. fprintf(stderr, "found type GEN_URI\n");
  36326. break;
  36327. }
  36328. }
  36329. X509_free(x509);
  36330. if (j == 0) {
  36331. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  36332. }
  36333. else {
  36334. /*
  36335. * We had a bug where GENERAL_NAMES_free didn't free all the memory
  36336. * it was supposed to. This is a regression test for that bug.
  36337. */
  36338. GENERAL_NAMES_free(sk);
  36339. }
  36340. }
  36341. res = TEST_RES_CHECK(1);
  36342. #endif
  36343. return res;
  36344. }
  36345. static int test_wolfSSL_GENERAL_NAME_print(void)
  36346. {
  36347. int res = TEST_SKIPPED;
  36348. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  36349. X509* x509;
  36350. GENERAL_NAME* gn;
  36351. unsigned char buf[4096];
  36352. const unsigned char* bufPt;
  36353. int bytes;
  36354. XFILE f;
  36355. STACK_OF(GENERAL_NAME)* sk;
  36356. BIO* out;
  36357. unsigned char outbuf[128];
  36358. X509_EXTENSION* ext;
  36359. AUTHORITY_INFO_ACCESS* aia;
  36360. ACCESS_DESCRIPTION* ad;
  36361. const unsigned char v4Addr[] = {192,168,53,1};
  36362. const unsigned char v6Addr[] =
  36363. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  36364. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  36365. const unsigned char email[] =
  36366. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  36367. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  36368. const char* dnsStr = "DNS:example.com";
  36369. const char* uriStr = "URI:http://127.0.0.1:22220";
  36370. const char* v4addStr = "IP Address:192.168.53.1";
  36371. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  36372. const char* emailStr = "email:info@wolfssl.com";
  36373. const char* othrStr = "othername:<unsupported>";
  36374. const char* x400Str = "X400Name:<unsupported>";
  36375. const char* ediStr = "EdiPartyName:<unsupported>";
  36376. /* BIO to output */
  36377. AssertNotNull(out = BIO_new(BIO_s_mem()));
  36378. /* test for NULL param */
  36379. gn = NULL;
  36380. AssertIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  36381. AssertIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  36382. AssertIntEQ(GENERAL_NAME_print(out, NULL), 0);
  36383. /* test for GEN_DNS */
  36384. f = XFOPEN(cliCertDerFileExt, "rb");
  36385. AssertTrue((f != XBADFILE));
  36386. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  36387. XFCLOSE(f);
  36388. bufPt = buf;
  36389. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  36390. AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  36391. NID_subject_alt_name, NULL, NULL));
  36392. AssertNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  36393. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36394. XMEMSET(outbuf,0,sizeof(outbuf));
  36395. BIO_read(out, outbuf, sizeof(outbuf));
  36396. AssertIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  36397. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  36398. X509_free(x509);
  36399. /* test for GEN_URI */
  36400. f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb");
  36401. AssertTrue((f != XBADFILE));
  36402. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  36403. XFCLOSE(f);
  36404. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  36405. aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  36406. AssertNotNull(aia);
  36407. ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0);
  36408. gn = ad->location;
  36409. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36410. XMEMSET(outbuf,0,sizeof(outbuf));
  36411. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36412. AssertIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  36413. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  36414. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  36415. AssertNotNull(aia);
  36416. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  36417. X509_free(x509);
  36418. /* test for GEN_IPADD */
  36419. /* ip v4 address */
  36420. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36421. gn->type = GEN_IPADD;
  36422. gn->d.iPAddress->length = sizeof(v4Addr);
  36423. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  36424. sizeof(v4Addr)), 1);
  36425. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36426. XMEMSET(outbuf,0,sizeof(outbuf));
  36427. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36428. AssertIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  36429. GENERAL_NAME_free(gn);
  36430. /* ip v6 address */
  36431. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36432. gn->type = GEN_IPADD;
  36433. gn->d.iPAddress->length = sizeof(v6Addr);
  36434. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  36435. sizeof(v6Addr)), 1);
  36436. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36437. XMEMSET(outbuf,0,sizeof(outbuf));
  36438. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36439. AssertIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  36440. GENERAL_NAME_free(gn);
  36441. /* test for GEN_EMAIL */
  36442. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36443. gn->type = GEN_EMAIL;
  36444. gn->d.rfc822Name->length = sizeof(email);
  36445. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email,
  36446. sizeof(email)), 1);
  36447. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36448. XMEMSET(outbuf,0,sizeof(outbuf));
  36449. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36450. AssertIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  36451. GENERAL_NAME_free(gn);
  36452. /* test for GEN_OTHERNAME */
  36453. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36454. gn->type = GEN_OTHERNAME;
  36455. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36456. XMEMSET(outbuf,0,sizeof(outbuf));
  36457. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36458. AssertIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  36459. GENERAL_NAME_free(gn);
  36460. /* test for GEN_X400 */
  36461. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36462. gn->type = GEN_X400;
  36463. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36464. XMEMSET(outbuf,0,sizeof(outbuf));
  36465. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36466. AssertIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  36467. GENERAL_NAME_free(gn);
  36468. /* test for GEN_EDIPARTY */
  36469. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36470. gn->type = GEN_EDIPARTY;
  36471. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36472. XMEMSET(outbuf,0,sizeof(outbuf));
  36473. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36474. AssertIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  36475. GENERAL_NAME_free(gn);
  36476. BIO_free(out);
  36477. res = TEST_RES_CHECK(1);
  36478. #endif /* OPENSSL_ALL */
  36479. return res;
  36480. }
  36481. static int test_wolfSSL_sk_DIST_POINT(void)
  36482. {
  36483. int res = TEST_SKIPPED;
  36484. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  36485. !defined(NO_RSA)
  36486. X509* x509;
  36487. unsigned char buf[4096];
  36488. const unsigned char* bufPt;
  36489. int bytes, i, j;
  36490. XFILE f;
  36491. DIST_POINT* dp;
  36492. DIST_POINT_NAME* dpn;
  36493. GENERAL_NAME* gn;
  36494. ASN1_IA5STRING* uri;
  36495. STACK_OF(DIST_POINT)* dps;
  36496. STACK_OF(GENERAL_NAME)* gns;
  36497. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  36498. f = XFOPEN(cliCertDerCrlDistPoint, "rb");
  36499. AssertTrue((f != XBADFILE));
  36500. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  36501. XFCLOSE(f);
  36502. bufPt = buf;
  36503. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  36504. AssertNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  36505. NID_crl_distribution_points, NULL, NULL));
  36506. AssertIntEQ(sk_DIST_POINT_num(dps), 1);
  36507. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  36508. AssertNotNull(dp = sk_DIST_POINT_value(dps, i));
  36509. AssertNotNull(dpn = dp->distpoint);
  36510. /* this should be type 0, fullname */
  36511. AssertIntEQ(dpn->type, 0);
  36512. gns = dp->distpoint->name.fullname;
  36513. AssertNotNull(gns);
  36514. AssertIntEQ(sk_GENERAL_NAME_num(gns), 1);
  36515. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  36516. gn = sk_GENERAL_NAME_value(gns, j);
  36517. AssertIntEQ(gn->type, GEN_URI);
  36518. AssertNotNull(uri = gn->d.uniformResourceIdentifier);
  36519. AssertNotNull(uri->data);
  36520. AssertIntGT(uri->length, 0);
  36521. }
  36522. }
  36523. X509_free(x509);
  36524. CRL_DIST_POINTS_free(dps);
  36525. res = TEST_RES_CHECK(1);
  36526. #endif
  36527. return res;
  36528. }
  36529. static int test_wolfSSL_MD4(void)
  36530. {
  36531. int res = TEST_SKIPPED;
  36532. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  36533. MD4_CTX md4;
  36534. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  36535. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  36536. "789012345678901234567890";
  36537. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  36538. "\xcc\x05\x36";
  36539. int msgSz = (int)XSTRLEN(msg);
  36540. XMEMSET(out, 0, sizeof(out));
  36541. MD4_Init(&md4);
  36542. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  36543. MD4_Final(out, &md4);
  36544. AssertIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  36545. res = TEST_RES_CHECK(1);
  36546. #endif
  36547. return res;
  36548. }
  36549. static int test_wolfSSL_verify_mode(void)
  36550. {
  36551. int res = TEST_SKIPPED;
  36552. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  36553. WOLFSSL* ssl;
  36554. WOLFSSL_CTX* ctx;
  36555. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36556. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  36557. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  36558. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  36559. AssertNotNull(ssl = SSL_new(ctx));
  36560. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  36561. SSL_free(ssl);
  36562. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  36563. AssertNotNull(ssl = SSL_new(ctx));
  36564. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  36565. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  36566. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  36567. AssertIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  36568. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  36569. SSL_free(ssl);
  36570. wolfSSL_CTX_set_verify(ctx,
  36571. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  36572. AssertNotNull(ssl = SSL_new(ctx));
  36573. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  36574. AssertIntEQ(SSL_get_verify_mode(ssl),
  36575. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  36576. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  36577. AssertIntEQ(SSL_CTX_get_verify_mode(ctx),
  36578. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  36579. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  36580. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  36581. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  36582. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  36583. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  36584. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  36585. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  36586. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  36587. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  36588. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  36589. #endif
  36590. AssertIntEQ(SSL_CTX_get_verify_mode(ctx),
  36591. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  36592. SSL_free(ssl);
  36593. SSL_CTX_free(ctx);
  36594. res = TEST_RES_CHECK(1);
  36595. #endif
  36596. return res;
  36597. }
  36598. static int test_wolfSSL_verify_depth(void)
  36599. {
  36600. int res = TEST_SKIPPED;
  36601. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  36602. WOLFSSL* ssl;
  36603. WOLFSSL_CTX* ctx;
  36604. long depth;
  36605. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36606. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  36607. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  36608. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  36609. AssertIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  36610. AssertNotNull(ssl = SSL_new(ctx));
  36611. AssertIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  36612. SSL_free(ssl);
  36613. SSL_CTX_set_verify_depth(ctx, -1);
  36614. AssertIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  36615. SSL_CTX_set_verify_depth(ctx, 2);
  36616. AssertIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  36617. AssertNotNull(ssl = SSL_new(ctx));
  36618. AssertIntEQ(2, SSL_get_verify_depth(ssl));
  36619. SSL_free(ssl);
  36620. SSL_CTX_free(ctx);
  36621. res = TEST_RES_CHECK(1);
  36622. #endif
  36623. return res;
  36624. }
  36625. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  36626. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  36627. * buffer of 64 bytes.
  36628. *
  36629. * returns the size of the digest buffer on success and a negative value on
  36630. * failure.
  36631. */
  36632. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest)
  36633. {
  36634. HMAC_CTX ctx1;
  36635. HMAC_CTX ctx2;
  36636. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  36637. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  36638. unsigned char long_key[] =
  36639. "0123456789012345678901234567890123456789"
  36640. "0123456789012345678901234567890123456789"
  36641. "0123456789012345678901234567890123456789"
  36642. "0123456789012345678901234567890123456789";
  36643. unsigned char msg[] = "message to hash";
  36644. unsigned int digestSz = 64;
  36645. int keySz = sizeof(key);
  36646. int long_keySz = sizeof(long_key);
  36647. int msgSz = sizeof(msg);
  36648. unsigned char digest2[64];
  36649. unsigned int digestSz2 = 64;
  36650. HMAC_CTX_init(&ctx1);
  36651. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  36652. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36653. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36654. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36655. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36656. HMAC_CTX_cleanup(&ctx1);
  36657. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36658. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  36659. HMAC_CTX_cleanup(&ctx2);
  36660. AssertIntEQ(digestSz, digestSz2);
  36661. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36662. /* test HMAC_Init with NULL key */
  36663. /* init after copy */
  36664. HMAC_CTX_init(&ctx1);
  36665. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  36666. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36667. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36668. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  36669. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36670. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36671. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36672. HMAC_CTX_cleanup(&ctx1);
  36673. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  36674. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36675. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36676. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  36677. HMAC_CTX_cleanup(&ctx2);
  36678. AssertIntEQ(digestSz, digestSz2);
  36679. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36680. /* long key */
  36681. HMAC_CTX_init(&ctx1);
  36682. AssertIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type), SSL_SUCCESS);
  36683. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36684. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36685. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  36686. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36687. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36688. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36689. HMAC_CTX_cleanup(&ctx1);
  36690. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  36691. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36692. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36693. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  36694. HMAC_CTX_cleanup(&ctx2);
  36695. AssertIntEQ(digestSz, digestSz2);
  36696. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36697. /* init before copy */
  36698. HMAC_CTX_init(&ctx1);
  36699. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  36700. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36701. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  36702. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36703. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36704. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36705. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36706. HMAC_CTX_cleanup(&ctx1);
  36707. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36708. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36709. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  36710. HMAC_CTX_cleanup(&ctx2);
  36711. AssertIntEQ(digestSz, digestSz2);
  36712. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36713. return digestSz;
  36714. }
  36715. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  36716. static int test_wolfSSL_HMAC_CTX(void)
  36717. {
  36718. int res = TEST_SKIPPED;
  36719. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  36720. unsigned char digest[64];
  36721. int digestSz;
  36722. #ifndef NO_SHA
  36723. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha1(), digest)), 20);
  36724. AssertIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  36725. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  36726. #endif /* !NO_SHA */
  36727. #ifdef WOLFSSL_SHA224
  36728. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha224(), digest)), 28);
  36729. AssertIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  36730. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  36731. "\x02\x0E", digest, digestSz), 0);
  36732. #endif /* WOLFSSL_SHA224 */
  36733. #ifndef NO_SHA256
  36734. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha256(), digest)), 32);
  36735. AssertIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  36736. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  36737. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  36738. #endif /* !NO_SHA256 */
  36739. #ifdef WOLFSSL_SHA384
  36740. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha384(), digest)), 48);
  36741. AssertIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  36742. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  36743. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  36744. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  36745. digest, digestSz), 0);
  36746. #endif /* WOLFSSL_SHA384 */
  36747. #ifdef WOLFSSL_SHA512
  36748. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha512(), digest)), 64);
  36749. AssertIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  36750. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  36751. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  36752. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  36753. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  36754. digest, digestSz), 0);
  36755. #endif /* WOLFSSL_SHA512 */
  36756. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || HAVE_FIPS_VERSION <= 2)
  36757. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_md5(), digest)), 16);
  36758. AssertIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  36759. "\xE4\x98\xDD", digest, digestSz), 0);
  36760. #endif /* !NO_MD5 */
  36761. res = TEST_RES_CHECK(1);
  36762. #endif
  36763. return res;
  36764. }
  36765. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  36766. static void sslMsgCb(int w, int version, int type, const void* buf,
  36767. size_t sz, SSL* ssl, void* arg)
  36768. {
  36769. int i;
  36770. unsigned char* pt = (unsigned char*)buf;
  36771. fprintf(stderr, "%s %d bytes of version %d , type %d : ",
  36772. (w)?"Writing":"Reading", (int)sz, version, type);
  36773. for (i = 0; i < (int)sz; i++) fprintf(stderr, "%02X", pt[i]);
  36774. fprintf(stderr, "\n");
  36775. (void)ssl;
  36776. (void)arg;
  36777. }
  36778. #endif /* OPENSSL_EXTRA */
  36779. static int test_wolfSSL_msg_callback(void)
  36780. {
  36781. int res = TEST_SKIPPED;
  36782. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  36783. WOLFSSL* ssl;
  36784. WOLFSSL_CTX* ctx;
  36785. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36786. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  36787. SSL_FILETYPE_PEM));
  36788. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  36789. SSL_FILETYPE_PEM));
  36790. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  36791. SSL_SUCCESS);
  36792. AssertNotNull(ssl = SSL_new(ctx));
  36793. AssertIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  36794. AssertIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  36795. AssertIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  36796. SSL_free(ssl);
  36797. SSL_CTX_free(ctx);
  36798. res = TEST_RES_CHECK(1);
  36799. #endif
  36800. return res;
  36801. }
  36802. static int test_wolfSSL_SHA(void)
  36803. {
  36804. int res = TEST_SKIPPED;
  36805. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  36806. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  36807. (!defined(HAVE_FIPS) || \
  36808. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  36809. {
  36810. const unsigned char in[] = "abc";
  36811. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  36812. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  36813. unsigned char out[WC_SHA_DIGEST_SIZE];
  36814. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  36815. AssertNotNull(SHA1(in, XSTRLEN((char*)in), out));
  36816. AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  36817. /* SHA interface test */
  36818. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  36819. AssertNull(SHA(NULL, XSTRLEN((char*)in), out));
  36820. AssertNotNull(SHA(in, 0, out));
  36821. AssertNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  36822. AssertNotNull(SHA(NULL, 0, out));
  36823. AssertNotNull(SHA(NULL, 0, NULL));
  36824. AssertNotNull(SHA(in, XSTRLEN((char*)in), out));
  36825. AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  36826. }
  36827. #endif
  36828. #if !defined(NO_SHA256)
  36829. {
  36830. const unsigned char in[] = "abc";
  36831. unsigned char expected[] = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  36832. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  36833. "\x15\xAD";
  36834. unsigned char out[WC_SHA256_DIGEST_SIZE];
  36835. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  36836. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  36837. AssertNotNull(SHA256(in, XSTRLEN((char*)in), out));
  36838. #else
  36839. AssertNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  36840. #endif
  36841. AssertIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  36842. }
  36843. #endif
  36844. #if defined(WOLFSSL_SHA384)
  36845. {
  36846. const unsigned char in[] = "abc";
  36847. unsigned char expected[] = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  36848. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  36849. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  36850. "\xc8\x25\xa7";
  36851. unsigned char out[WC_SHA384_DIGEST_SIZE];
  36852. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  36853. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  36854. AssertNotNull(SHA384(in, XSTRLEN((char*)in), out));
  36855. #else
  36856. AssertNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  36857. #endif
  36858. AssertIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  36859. }
  36860. #endif
  36861. #if defined(WOLFSSL_SHA512)
  36862. {
  36863. const unsigned char in[] = "abc";
  36864. unsigned char expected[] = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  36865. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  36866. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  36867. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  36868. "\xa5\x4c\xa4\x9f";
  36869. unsigned char out[WC_SHA512_DIGEST_SIZE];
  36870. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  36871. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  36872. AssertNotNull(SHA512(in, XSTRLEN((char*)in), out));
  36873. #else
  36874. AssertNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  36875. #endif
  36876. AssertIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  36877. }
  36878. #endif
  36879. res = TEST_RES_CHECK(1);
  36880. #endif
  36881. return res;
  36882. }
  36883. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  36884. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  36885. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  36886. static void binary_dump(void *ptr, int size)
  36887. {
  36888. #ifdef WOLFSSL_EVP_PRINT
  36889. int i = 0;
  36890. unsigned char *p = (unsigned char *) ptr;
  36891. fprintf(stderr, "{");
  36892. while ((p != NULL) && (i < size)) {
  36893. if ((i % 8) == 0) {
  36894. fprintf(stderr, "\n");
  36895. fprintf(stderr, " ");
  36896. }
  36897. fprintf(stderr, "0x%02x, ", p[i]);
  36898. i++;
  36899. }
  36900. fprintf(stderr, "\n};\n");
  36901. #else
  36902. (void) ptr;
  36903. (void) size;
  36904. #endif
  36905. }
  36906. static int last_val = 0x0f;
  36907. static int check_result(unsigned char *data, int len)
  36908. {
  36909. int i;
  36910. for ( ; len; ) {
  36911. last_val = (last_val + 1) % 16;
  36912. for (i = 0; i < 16; len--, i++, data++)
  36913. if (*data != last_val) {
  36914. return -1;
  36915. }
  36916. }
  36917. return 0;
  36918. }
  36919. static int r_offset;
  36920. static int w_offset;
  36921. static void init_offset(void)
  36922. {
  36923. r_offset = 0;
  36924. w_offset = 0;
  36925. }
  36926. static void get_record(unsigned char *data, unsigned char *buf, int len)
  36927. {
  36928. XMEMCPY(buf, data+r_offset, len);
  36929. r_offset += len;
  36930. }
  36931. static void set_record(unsigned char *data, unsigned char *buf, int len)
  36932. {
  36933. XMEMCPY(data+w_offset, buf, len);
  36934. w_offset += len;
  36935. }
  36936. static void set_plain(unsigned char *plain, int rec)
  36937. {
  36938. int i, j;
  36939. unsigned char *p = plain;
  36940. #define BLOCKSZ 16
  36941. for (i=0; i<(rec/BLOCKSZ); i++) {
  36942. for (j=0; j<BLOCKSZ; j++)
  36943. *p++ = (i % 16);
  36944. }
  36945. }
  36946. #endif
  36947. static int test_wolfSSL_EVP_Cipher_extra(void)
  36948. {
  36949. int res = TEST_SKIPPED;
  36950. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  36951. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  36952. /* aes128-cbc, keylen=16, ivlen=16 */
  36953. byte aes128_cbc_key[] = {
  36954. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  36955. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  36956. };
  36957. byte aes128_cbc_iv[] = {
  36958. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  36959. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  36960. };
  36961. /* teset data size table */
  36962. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  36963. int test_drive2[] = {8, 3, 8, 512, 0};
  36964. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  36965. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  36966. int test_drive_len[100];
  36967. int ret = 0;
  36968. EVP_CIPHER_CTX *evp = NULL;
  36969. int ilen = 0;
  36970. int klen = 0;
  36971. int i, j;
  36972. const EVP_CIPHER *type;
  36973. byte *iv;
  36974. byte *key;
  36975. int ivlen;
  36976. int keylen;
  36977. #define RECORDS 16
  36978. #define BUFFSZ 512
  36979. byte plain [BUFFSZ * RECORDS];
  36980. byte cipher[BUFFSZ * RECORDS];
  36981. byte inb[BUFFSZ];
  36982. byte outb[BUFFSZ+16];
  36983. int outl, inl;
  36984. iv = aes128_cbc_iv;
  36985. ivlen = sizeof(aes128_cbc_iv);
  36986. key = aes128_cbc_key;
  36987. keylen = sizeof(aes128_cbc_key);
  36988. type = EVP_aes_128_cbc();
  36989. set_plain(plain, BUFFSZ * RECORDS);
  36990. SSL_library_init();
  36991. AssertNotNull(evp = EVP_CIPHER_CTX_new());
  36992. AssertIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  36993. AssertIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  36994. klen = EVP_CIPHER_CTX_key_length(evp);
  36995. if (klen > 0 && keylen != klen) {
  36996. AssertIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  36997. }
  36998. ilen = EVP_CIPHER_CTX_iv_length(evp);
  36999. if (ilen > 0 && ivlen != ilen) {
  37000. AssertIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  37001. }
  37002. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  37003. for (j = 0; j<RECORDS; j++)
  37004. {
  37005. inl = BUFFSZ;
  37006. get_record(plain, inb, inl);
  37007. AssertIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  37008. set_record(cipher, outb, outl);
  37009. }
  37010. for (i = 0; test_drive[i]; i++) {
  37011. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  37012. init_offset();
  37013. test_drive_len[i] = 0;
  37014. for (j = 0; test_drive[i][j]; j++)
  37015. {
  37016. inl = test_drive[i][j];
  37017. test_drive_len[i] += inl;
  37018. get_record(plain, inb, inl);
  37019. AssertIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)), 0);
  37020. /* output to cipher buffer, so that following Dec test can detect
  37021. if any error */
  37022. set_record(cipher, outb, outl);
  37023. }
  37024. EVP_CipherFinal(evp, outb, &outl);
  37025. if (outl > 0)
  37026. set_record(cipher, outb, outl);
  37027. }
  37028. for (i = 0; test_drive[i]; i++) {
  37029. last_val = 0x0f;
  37030. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  37031. init_offset();
  37032. for (j = 0; test_drive[i][j]; j++) {
  37033. inl = test_drive[i][j];
  37034. get_record(cipher, inb, inl);
  37035. AssertIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)), 0);
  37036. binary_dump(outb, outl);
  37037. AssertIntEQ((ret = check_result(outb, outl)), 0);
  37038. AssertFalse(outl > ((inl/16+1)*16) && outl > 16);
  37039. }
  37040. ret = EVP_CipherFinal(evp, outb, &outl);
  37041. binary_dump(outb, outl);
  37042. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  37043. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  37044. AssertTrue(ret);
  37045. }
  37046. EVP_CIPHER_CTX_free(evp);
  37047. res = TEST_RES_CHECK(1);
  37048. #endif /* test_EVP_Cipher */
  37049. return res;
  37050. }
  37051. static int test_wolfSSL_PEM_read_DHparams(void)
  37052. {
  37053. int res = TEST_SKIPPED;
  37054. #if defined(OPENSSL_ALL) && !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && \
  37055. !defined(NO_FILESYSTEM)
  37056. DH* dh;
  37057. XFILE fp;
  37058. unsigned char derOut[300];
  37059. unsigned char* derOutBuf = derOut;
  37060. int derOutSz = 0;
  37061. unsigned char derExpected[300];
  37062. int derExpectedSz = 0;
  37063. XMEMSET(derOut, 0, sizeof(derOut));
  37064. XMEMSET(derExpected, 0, sizeof(derExpected));
  37065. /* open DH param file, read into DH struct */
  37066. AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
  37067. /* bad args */
  37068. AssertNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  37069. AssertNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  37070. /* good args */
  37071. AssertNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  37072. XFCLOSE(fp);
  37073. /* read in certs/dh2048.der for comparison against exported params */
  37074. fp = XFOPEN("./certs/dh2048.der", "rb");
  37075. AssertTrue(fp != XBADFILE);
  37076. derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected), fp);
  37077. XFCLOSE(fp);
  37078. /* export DH back to DER and compare */
  37079. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  37080. AssertIntEQ(derOutSz, derExpectedSz);
  37081. AssertIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  37082. DH_free(dh);
  37083. dh = NULL;
  37084. /* Test parsing with X9.42 header */
  37085. fp = XFOPEN("./certs/x942dh2048.pem", "rb");
  37086. AssertNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  37087. XFCLOSE(fp);
  37088. DH_free(dh);
  37089. res = TEST_RES_CHECK(1);
  37090. #endif
  37091. return res;
  37092. }
  37093. static int test_wolfSSL_AES_ecb_encrypt(void)
  37094. {
  37095. int res = TEST_SKIPPED;
  37096. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB)
  37097. AES_KEY aes;
  37098. const byte msg[] =
  37099. {
  37100. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  37101. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  37102. };
  37103. const byte verify[] =
  37104. {
  37105. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  37106. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  37107. };
  37108. const byte key[] =
  37109. {
  37110. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  37111. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  37112. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  37113. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  37114. };
  37115. byte out[AES_BLOCK_SIZE];
  37116. AssertIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  37117. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37118. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  37119. AssertIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  37120. #ifdef HAVE_AES_DECRYPT
  37121. AssertIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  37122. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37123. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  37124. AssertIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  37125. #endif
  37126. /* test bad arguments */
  37127. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  37128. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  37129. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  37130. res = TEST_RES_CHECK(1);
  37131. #endif
  37132. return res;
  37133. }
  37134. static int test_wolfSSL_MD5(void)
  37135. {
  37136. int res = TEST_SKIPPED;
  37137. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  37138. byte input1[] = "";
  37139. byte input2[] = "message digest";
  37140. byte hash[WC_MD5_DIGEST_SIZE];
  37141. unsigned char output1[] =
  37142. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  37143. unsigned char output2[] =
  37144. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  37145. WOLFSSL_MD5_CTX md5;
  37146. XMEMSET(&md5, 0, sizeof(md5));
  37147. /* Test cases for illegal parameters */
  37148. AssertIntEQ(MD5_Init(NULL), 0);
  37149. AssertIntEQ(MD5_Init(&md5), 1);
  37150. AssertIntEQ(MD5_Update(NULL, input1, 0), 0);
  37151. AssertIntEQ(MD5_Update(NULL, NULL, 0), 0);
  37152. AssertIntEQ(MD5_Update(&md5, NULL, 1), 0);
  37153. AssertIntEQ(MD5_Final(NULL, &md5), 0);
  37154. AssertIntEQ(MD5_Final(hash, NULL), 0);
  37155. AssertIntEQ(MD5_Final(NULL, NULL), 0);
  37156. /* Init MD5 CTX */
  37157. AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
  37158. AssertIntEQ(wolfSSL_MD5_Update(&md5, input1,
  37159. XSTRLEN((const char*)&input1)), 1);
  37160. AssertIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  37161. AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  37162. /* Init MD5 CTX */
  37163. AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
  37164. AssertIntEQ(wolfSSL_MD5_Update(&md5, input2,
  37165. (int)XSTRLEN((const char*)input2)), 1);
  37166. AssertIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  37167. AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  37168. #if !defined(NO_OLD_NAMES) && \
  37169. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  37170. AssertPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  37171. AssertPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  37172. AssertPtrNE(MD5(input1, 1, NULL), NULL);
  37173. AssertPtrNE(MD5(NULL, 0, NULL), NULL);
  37174. AssertPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash), &hash);
  37175. AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  37176. AssertPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash), &hash);
  37177. AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  37178. {
  37179. byte data[] = "Data to be hashed.";
  37180. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  37181. AssertNotNull(MD5(data, sizeof(data), NULL));
  37182. AssertNotNull(MD5(data, sizeof(data), hash));
  37183. AssertNotNull(MD5(NULL, 0, hash));
  37184. AssertNull(MD5(NULL, sizeof(data), hash));
  37185. }
  37186. #endif
  37187. res = TEST_RES_CHECK(1);
  37188. #endif
  37189. return res;
  37190. }
  37191. static int test_wolfSSL_MD5_Transform(void)
  37192. {
  37193. int res = TEST_SKIPPED;
  37194. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  37195. byte input1[] = "";
  37196. byte input2[] = "abc";
  37197. byte local[WC_MD5_BLOCK_SIZE];
  37198. word32 sLen = 0;
  37199. #ifdef BIG_ENDIAN_ORDER
  37200. unsigned char output1[] =
  37201. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  37202. unsigned char output2[] =
  37203. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  37204. #else
  37205. unsigned char output1[] =
  37206. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  37207. unsigned char output2[] =
  37208. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  37209. #endif
  37210. union {
  37211. wc_Md5 native;
  37212. MD5_CTX compat;
  37213. } md5;
  37214. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  37215. XMEMSET(&local, 0, sizeof(local));
  37216. /* sanity check */
  37217. AssertIntEQ(MD5_Transform(NULL, NULL), 0);
  37218. AssertIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  37219. AssertIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  37220. AssertIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  37221. AssertIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37222. AssertIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  37223. /* Init MD5 CTX */
  37224. AssertIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  37225. /* Do Transform*/
  37226. sLen = (word32)XSTRLEN((char*)input1);
  37227. XMEMCPY(local, input1, sLen);
  37228. AssertIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  37229. AssertIntEQ(XMEMCMP(md5.native.digest, output1,
  37230. WC_MD5_DIGEST_SIZE), 0);
  37231. /* Init MD5 CTX */
  37232. AssertIntEQ(MD5_Init(&md5.compat), 1);
  37233. sLen = (word32)XSTRLEN((char*)input2);
  37234. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  37235. XMEMCPY(local, input2, sLen);
  37236. AssertIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  37237. AssertIntEQ(XMEMCMP(md5.native.digest, output2,
  37238. WC_MD5_DIGEST_SIZE), 0);
  37239. res = TEST_RES_CHECK(1);
  37240. #endif
  37241. return res;
  37242. }
  37243. static int test_wolfSSL_SHA224(void)
  37244. {
  37245. int res = TEST_SKIPPED;
  37246. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  37247. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37248. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  37249. unsigned char input[] =
  37250. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  37251. unsigned char output[] =
  37252. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  37253. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  37254. size_t inLen;
  37255. byte hash[WC_SHA224_DIGEST_SIZE];
  37256. inLen = XSTRLEN((char*)input);
  37257. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  37258. AssertNull(SHA224(NULL, inLen, hash));
  37259. AssertNotNull(SHA224(input, 0, hash));
  37260. AssertNotNull(SHA224(input, inLen, NULL));
  37261. AssertNotNull(SHA224(NULL, 0, hash));
  37262. AssertNotNull(SHA224(NULL, 0, NULL));
  37263. AssertNotNull(SHA224(input, inLen, hash));
  37264. AssertIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  37265. res = TEST_RES_CHECK(1);
  37266. #endif
  37267. return res;
  37268. }
  37269. static int test_wolfSSL_SHA_Transform(void)
  37270. {
  37271. int res = TEST_SKIPPED;
  37272. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  37273. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37274. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  37275. byte input1[] = "";
  37276. byte input2[] = "abc";
  37277. byte local[WC_SHA_BLOCK_SIZE];
  37278. word32 sLen = 0;
  37279. #ifdef BIG_ENDIAN_ORDER
  37280. unsigned char output1[] =
  37281. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  37282. "\x09\xf7\x3a\x93";
  37283. unsigned char output2[] =
  37284. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  37285. "\x7c\x6e\x08\xb8";
  37286. #else
  37287. unsigned char output1[] =
  37288. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  37289. "\x93\x3a\xf7\x09";
  37290. unsigned char output2[] =
  37291. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  37292. "\xb8\x08\x6e\x7c";
  37293. #endif
  37294. union {
  37295. wc_Sha native;
  37296. SHA_CTX compat;
  37297. } sha;
  37298. union {
  37299. wc_Sha native;
  37300. SHA_CTX compat;
  37301. } sha1;
  37302. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  37303. XMEMSET(&local, 0, sizeof(local));
  37304. /* sanity check */
  37305. AssertIntEQ(SHA_Transform(NULL, NULL), 0);
  37306. AssertIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  37307. AssertIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  37308. AssertIntEQ(SHA1_Transform(NULL, NULL), 0);
  37309. AssertIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  37310. AssertIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  37311. AssertIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  37312. AssertIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37313. AssertIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  37314. /* Init SHA CTX */
  37315. AssertIntEQ(SHA_Init(&sha.compat), 1);
  37316. /* Do Transform*/
  37317. sLen = (word32)XSTRLEN((char*)input1);
  37318. XMEMCPY(local, input1, sLen);
  37319. AssertIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  37320. AssertIntEQ(XMEMCMP(sha.native.digest, output1,
  37321. WC_SHA_DIGEST_SIZE), 0);
  37322. AssertIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  37323. /* Init SHA CTX */
  37324. AssertIntEQ(SHA_Init(&sha.compat), 1);
  37325. sLen = (word32)XSTRLEN((char*)input2);
  37326. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37327. XMEMCPY(local, input2, sLen);
  37328. AssertIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  37329. AssertIntEQ(XMEMCMP(sha.native.digest, output2,
  37330. WC_SHA_DIGEST_SIZE), 0);
  37331. AssertIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  37332. /* SHA1 */
  37333. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37334. /* Init SHA CTX */
  37335. AssertIntEQ(SHA1_Init(&sha1.compat), 1);
  37336. /* Do Transform*/
  37337. sLen = (word32)XSTRLEN((char*)input1);
  37338. XMEMCPY(local, input1, sLen);
  37339. AssertIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  37340. AssertIntEQ(XMEMCMP(sha1.native.digest, output1,
  37341. WC_SHA_DIGEST_SIZE), 0);
  37342. AssertIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  37343. /* Init SHA CTX */
  37344. AssertIntEQ(SHA1_Init(&sha1.compat), 1);
  37345. sLen = (word32)XSTRLEN((char*)input2);
  37346. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37347. XMEMCPY(local, input2, sLen);
  37348. AssertIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  37349. AssertIntEQ(XMEMCMP(sha1.native.digest, output2,
  37350. WC_SHA_DIGEST_SIZE), 0);
  37351. AssertIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  37352. res = TEST_RES_CHECK(1);
  37353. #endif
  37354. #endif
  37355. return res;
  37356. }
  37357. static int test_wolfSSL_SHA256_Transform(void)
  37358. {
  37359. int res = TEST_SKIPPED;
  37360. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  37361. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37362. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  37363. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH)
  37364. byte input1[] = "";
  37365. byte input2[] = "abc";
  37366. byte local[WC_SHA256_BLOCK_SIZE];
  37367. word32 sLen = 0;
  37368. #ifdef BIG_ENDIAN_ORDER
  37369. unsigned char output1[] =
  37370. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  37371. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  37372. unsigned char output2[] =
  37373. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  37374. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  37375. #else
  37376. unsigned char output1[] =
  37377. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  37378. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  37379. unsigned char output2[] =
  37380. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  37381. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  37382. #endif
  37383. union {
  37384. wc_Sha256 native;
  37385. SHA256_CTX compat;
  37386. } sha256;
  37387. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  37388. XMEMSET(&local, 0, sizeof(local));
  37389. /* sanity check */
  37390. AssertIntEQ(SHA256_Transform(NULL, NULL), 0);
  37391. AssertIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  37392. AssertIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  37393. AssertIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  37394. AssertIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37395. AssertIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  37396. /* Init SHA256 CTX */
  37397. AssertIntEQ(SHA256_Init(&sha256.compat), 1);
  37398. /* Do Transform*/
  37399. sLen = (word32)XSTRLEN((char*)input1);
  37400. XMEMCPY(local, input1, sLen);
  37401. AssertIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  37402. AssertIntEQ(XMEMCMP(sha256.native.digest, output1,
  37403. WC_SHA256_DIGEST_SIZE), 0);
  37404. AssertIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  37405. /* Init SHA256 CTX */
  37406. AssertIntEQ(SHA256_Init(&sha256.compat), 1);
  37407. sLen = (word32)XSTRLEN((char*)input2);
  37408. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  37409. XMEMCPY(local, input2, sLen);
  37410. AssertIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  37411. AssertIntEQ(XMEMCMP(sha256.native.digest, output2,
  37412. WC_SHA256_DIGEST_SIZE), 0);
  37413. AssertIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  37414. res = TEST_RES_CHECK(1);
  37415. #endif
  37416. #endif
  37417. return res;
  37418. }
  37419. static int test_wolfSSL_SHA256(void)
  37420. {
  37421. int res = TEST_SKIPPED;
  37422. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  37423. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  37424. unsigned char input[] =
  37425. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  37426. unsigned char output[] =
  37427. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  37428. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  37429. "\x06\xC1";
  37430. size_t inLen;
  37431. byte hash[WC_SHA256_DIGEST_SIZE];
  37432. inLen = XSTRLEN((char*)input);
  37433. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  37434. AssertNotNull(SHA256(input, inLen, hash));
  37435. AssertIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  37436. res = TEST_RES_CHECK(1);
  37437. #endif
  37438. return res;
  37439. }
  37440. static int test_wolfSSL_SHA512_Transform(void)
  37441. {
  37442. int res = TEST_SKIPPED;
  37443. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  37444. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37445. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  37446. byte input1[] = "";
  37447. byte input2[] = "abc";
  37448. byte local[WC_SHA512_BLOCK_SIZE];
  37449. word32 sLen = 0;
  37450. #ifdef BIG_ENDIAN_ORDER
  37451. unsigned char output1[] =
  37452. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  37453. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  37454. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  37455. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  37456. unsigned char output2[] =
  37457. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  37458. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  37459. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  37460. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  37461. #else
  37462. unsigned char output1[] =
  37463. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  37464. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  37465. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  37466. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  37467. unsigned char output2[] =
  37468. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  37469. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  37470. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  37471. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  37472. #endif
  37473. union {
  37474. wc_Sha512 native;
  37475. SHA512_CTX compat;
  37476. } sha512;
  37477. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  37478. XMEMSET(&local, 0, sizeof(local));
  37479. /* sanity check */
  37480. AssertIntEQ(SHA512_Transform(NULL, NULL), 0);
  37481. AssertIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  37482. AssertIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  37483. AssertIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  37484. AssertIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37485. AssertIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  37486. /* Init SHA512 CTX */
  37487. AssertIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  37488. /* Do Transform*/
  37489. sLen = (word32)XSTRLEN((char*)input1);
  37490. XMEMCPY(local, input1, sLen);
  37491. AssertIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  37492. AssertIntEQ(XMEMCMP(sha512.native.digest, output1,
  37493. WC_SHA512_DIGEST_SIZE), 0);
  37494. AssertIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  37495. /* Init SHA512 CTX */
  37496. AssertIntEQ(SHA512_Init(&sha512.compat), 1);
  37497. sLen = (word32)XSTRLEN((char*)input2);
  37498. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  37499. XMEMCPY(local, input2, sLen);
  37500. AssertIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  37501. AssertIntEQ(XMEMCMP(sha512.native.digest, output2,
  37502. WC_SHA512_DIGEST_SIZE), 0);
  37503. AssertIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  37504. (void)input1;
  37505. res = TEST_RES_CHECK(1);
  37506. #endif
  37507. #endif
  37508. return res;
  37509. }
  37510. static int test_wolfSSL_X509_get_serialNumber(void)
  37511. {
  37512. int res = TEST_SKIPPED;
  37513. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  37514. ASN1_INTEGER* a;
  37515. BIGNUM* bn;
  37516. X509* x509;
  37517. char *serialHex;
  37518. byte serial[3];
  37519. int serialSz;
  37520. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  37521. SSL_FILETYPE_PEM));
  37522. AssertNotNull(a = X509_get_serialNumber(x509));
  37523. /* check on value of ASN1 Integer */
  37524. AssertNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  37525. /* test setting serial number and then retrieving it */
  37526. AssertNotNull(a = ASN1_INTEGER_new());
  37527. ASN1_INTEGER_set(a, 3);
  37528. AssertIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  37529. serialSz = sizeof(serial);
  37530. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  37531. WOLFSSL_SUCCESS);
  37532. AssertIntEQ(serialSz, 1);
  37533. AssertIntEQ(serial[0], 3);
  37534. ASN1_INTEGER_free(a);
  37535. /* test setting serial number with 0's in it */
  37536. serial[0] = 0x01;
  37537. serial[1] = 0x00;
  37538. serial[2] = 0x02;
  37539. AssertNotNull(a = wolfSSL_ASN1_INTEGER_new());
  37540. a->data[0] = ASN_INTEGER;
  37541. a->data[1] = sizeof(serial);
  37542. XMEMCPY(&a->data[2], serial, sizeof(serial));
  37543. a->length = sizeof(serial) + 2;
  37544. AssertIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  37545. XMEMSET(serial, 0, sizeof(serial));
  37546. serialSz = sizeof(serial);
  37547. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  37548. WOLFSSL_SUCCESS);
  37549. AssertIntEQ(serialSz, 3);
  37550. AssertIntEQ(serial[0], 0x01);
  37551. AssertIntEQ(serial[1], 0x00);
  37552. AssertIntEQ(serial[2], 0x02);
  37553. ASN1_INTEGER_free(a);
  37554. X509_free(x509); /* free's a */
  37555. AssertNotNull(serialHex = BN_bn2hex(bn));
  37556. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  37557. AssertStrEQ(serialHex, "01");
  37558. #else
  37559. AssertStrEQ(serialHex, "1");
  37560. #endif
  37561. OPENSSL_free(serialHex);
  37562. AssertIntEQ(BN_get_word(bn), 1);
  37563. BN_free(bn);
  37564. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  37565. a = ASN1_INTEGER_new();
  37566. if (a) {
  37567. AssertNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  37568. DYNAMIC_TYPE_OPENSSL));
  37569. a->isDynamic = 1;
  37570. ASN1_INTEGER_free(a);
  37571. }
  37572. res = TEST_RES_CHECK(1);
  37573. #endif
  37574. return res;
  37575. }
  37576. static int test_wolfSSL_OpenSSL_add_all_algorithms(void)
  37577. {
  37578. int res = TEST_SKIPPED;
  37579. #if defined(OPENSSL_EXTRA)
  37580. AssertIntEQ(wolfSSL_add_all_algorithms(),WOLFSSL_SUCCESS);
  37581. AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(),WOLFSSL_SUCCESS);
  37582. AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(),WOLFSSL_SUCCESS);
  37583. res = TEST_RES_CHECK(1);
  37584. #endif
  37585. return res;
  37586. }
  37587. static int test_wolfSSL_OPENSSL_hexstr2buf(void)
  37588. {
  37589. int res = TEST_SKIPPED;
  37590. #if defined(OPENSSL_EXTRA)
  37591. #define MAX_HEXSTR_BUFSZ 9
  37592. #define NUM_CASES 5
  37593. struct Output {
  37594. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  37595. long ret;
  37596. };
  37597. int i;
  37598. int j;
  37599. const char* inputs[NUM_CASES] = {
  37600. "aabcd1357e",
  37601. "01:12:23:34:a5:b6:c7:d8:e9",
  37602. ":01:02",
  37603. "012",
  37604. ":ab:ac:d"
  37605. };
  37606. struct Output expectedOutputs[NUM_CASES] = {
  37607. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  37608. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  37609. {{0x01, 0x02}, 2},
  37610. {{0x00}, 0},
  37611. {{0x00}, 0}
  37612. };
  37613. long len = 0;
  37614. unsigned char* returnedBuf = NULL;
  37615. for (i = 0; i < NUM_CASES; ++i) {
  37616. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  37617. if (returnedBuf == NULL) {
  37618. AssertIntEQ(expectedOutputs[i].ret, 0);
  37619. continue;
  37620. }
  37621. AssertIntEQ(expectedOutputs[i].ret, len);
  37622. for (j = 0; j < len; ++j) {
  37623. AssertIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  37624. }
  37625. OPENSSL_free(returnedBuf);
  37626. }
  37627. res = TEST_RES_CHECK(1);
  37628. #endif
  37629. return res;
  37630. }
  37631. static int test_wolfSSL_ASN1_STRING_print_ex(void)
  37632. {
  37633. int res = TEST_SKIPPED;
  37634. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  37635. #ifndef NO_BIO
  37636. ASN1_STRING* asn_str;
  37637. const char data[] = "Hello wolfSSL!";
  37638. ASN1_STRING* esc_str;
  37639. const char esc_data[] = "a+;<>";
  37640. BIO *bio;
  37641. unsigned long flags;
  37642. int p_len;
  37643. unsigned char rbuf[255];
  37644. /* setup */
  37645. XMEMSET(rbuf, 0, 255);
  37646. bio = BIO_new(BIO_s_mem());
  37647. BIO_set_write_buf_size(bio,255);
  37648. asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  37649. ASN1_STRING_set(asn_str, (const void*)data, sizeof(data));
  37650. esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  37651. ASN1_STRING_set(esc_str, (const void*)esc_data, sizeof(esc_data));
  37652. /* no flags */
  37653. XMEMSET(rbuf, 0, 255);
  37654. flags = 0;
  37655. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  37656. AssertIntEQ(p_len, 15);
  37657. BIO_read(bio, (void*)rbuf, 15);
  37658. AssertStrEQ((char*)rbuf, "Hello wolfSSL!");
  37659. /* RFC2253 Escape */
  37660. XMEMSET(rbuf, 0, 255);
  37661. flags = ASN1_STRFLGS_ESC_2253;
  37662. p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags);
  37663. AssertIntEQ(p_len, 9);
  37664. BIO_read(bio, (void*)rbuf, 9);
  37665. AssertStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  37666. /* Show type */
  37667. XMEMSET(rbuf, 0, 255);
  37668. flags = ASN1_STRFLGS_SHOW_TYPE;
  37669. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  37670. AssertIntEQ(p_len, 28);
  37671. BIO_read(bio, (void*)rbuf, 28);
  37672. AssertStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  37673. /* Dump All */
  37674. XMEMSET(rbuf, 0, 255);
  37675. flags = ASN1_STRFLGS_DUMP_ALL;
  37676. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  37677. AssertIntEQ(p_len, 31);
  37678. BIO_read(bio, (void*)rbuf, 31);
  37679. AssertStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  37680. /* Dump Der */
  37681. XMEMSET(rbuf, 0, 255);
  37682. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  37683. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  37684. AssertIntEQ(p_len, 35);
  37685. BIO_read(bio, (void*)rbuf, 35);
  37686. AssertStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  37687. /* Dump All + Show type */
  37688. XMEMSET(rbuf, 0, 255);
  37689. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  37690. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  37691. AssertIntEQ(p_len, 44);
  37692. BIO_read(bio, (void*)rbuf, 44);
  37693. AssertStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  37694. BIO_free(bio);
  37695. ASN1_STRING_free(asn_str);
  37696. ASN1_STRING_free(esc_str);
  37697. res = TEST_RES_CHECK(1);
  37698. #endif /* !NO_BIO */
  37699. #endif
  37700. return res;
  37701. }
  37702. static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void)
  37703. {
  37704. int res = TEST_SKIPPED;
  37705. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  37706. WOLFSSL_ASN1_TIME *t;
  37707. WOLFSSL_ASN1_TIME *out;
  37708. WOLFSSL_ASN1_TIME *gtime;
  37709. int tlen = 0;
  37710. unsigned char *data;
  37711. /* UTC Time test */
  37712. AssertNotNull(t = wolfSSL_ASN1_TIME_new());
  37713. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  37714. AssertNotNull(out = wolfSSL_ASN1_TIME_new());
  37715. t->type = ASN_UTC_TIME;
  37716. t->length = ASN_UTC_TIME_SIZE;
  37717. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  37718. tlen = wolfSSL_ASN1_TIME_get_length(t);
  37719. AssertIntEQ(tlen, ASN_UTC_TIME_SIZE);
  37720. data = wolfSSL_ASN1_TIME_get_data(t);
  37721. AssertStrEQ((char*)data, "050727123456Z");
  37722. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  37723. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  37724. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  37725. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  37726. /* Generalized Time test */
  37727. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  37728. XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE);
  37729. XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE);
  37730. t->type = ASN_GENERALIZED_TIME;
  37731. t->length = ASN_GENERALIZED_TIME_SIZE;
  37732. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  37733. tlen = wolfSSL_ASN1_TIME_get_length(t);
  37734. AssertIntEQ(tlen, ASN_GENERALIZED_TIME_SIZE);
  37735. data = wolfSSL_ASN1_TIME_get_data(t);
  37736. AssertStrEQ((char*)data, "20050727123456Z");
  37737. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  37738. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  37739. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  37740. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  37741. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  37742. /* Null parameter test */
  37743. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  37744. gtime = NULL;
  37745. out = NULL;
  37746. t->type = ASN_UTC_TIME;
  37747. t->length = ASN_UTC_TIME_SIZE;
  37748. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  37749. AssertNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  37750. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  37751. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  37752. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  37753. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  37754. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  37755. res = TEST_RES_CHECK(1);
  37756. #endif
  37757. return res;
  37758. }
  37759. static int test_wolfSSL_X509_CA_num(void)
  37760. {
  37761. int res = TEST_SKIPPED;
  37762. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  37763. defined(HAVE_ECC) && !defined(NO_RSA)
  37764. WOLFSSL_X509_STORE *store;
  37765. WOLFSSL_X509 *x509_1, *x509_2;
  37766. int ca_num = 0;
  37767. store = wolfSSL_X509_STORE_new();
  37768. x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  37769. wolfSSL_X509_STORE_add_cert(store, x509_1);
  37770. ca_num = wolfSSL_X509_CA_num(store);
  37771. AssertIntEQ(ca_num, 1);
  37772. x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile, WOLFSSL_FILETYPE_PEM);
  37773. wolfSSL_X509_STORE_add_cert(store, x509_2);
  37774. ca_num = wolfSSL_X509_CA_num(store);
  37775. AssertIntEQ(ca_num, 2);
  37776. wolfSSL_X509_free(x509_1);
  37777. wolfSSL_X509_free(x509_2);
  37778. wolfSSL_X509_STORE_free(store);
  37779. res = TEST_RES_CHECK(1);
  37780. #endif
  37781. return res;
  37782. }
  37783. static int test_wolfSSL_X509_check_ca(void)
  37784. {
  37785. int res = TEST_SKIPPED;
  37786. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  37787. WOLFSSL_X509 *x509;
  37788. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  37789. AssertIntEQ(wolfSSL_X509_check_ca(x509), 1);
  37790. wolfSSL_X509_free(x509);
  37791. res = TEST_RES_CHECK(1);
  37792. #endif
  37793. return res;
  37794. }
  37795. static int test_wolfSSL_X509_check_ip_asc(void)
  37796. {
  37797. int res = TEST_SKIPPED;
  37798. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  37799. WOLFSSL_X509 *x509;
  37800. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  37801. #if 0
  37802. /* TODO: add cert gen for testing positive case */
  37803. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  37804. #endif
  37805. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  37806. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  37807. wolfSSL_X509_free(x509);
  37808. res = TEST_RES_CHECK(1);
  37809. #endif
  37810. return res;
  37811. }
  37812. static int test_wolfSSL_make_cert(void)
  37813. {
  37814. int res = TEST_SKIPPED;
  37815. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  37816. int ret;
  37817. Cert cert;
  37818. CertName name;
  37819. RsaKey key;
  37820. WC_RNG rng;
  37821. byte der[FOURK_BUF];
  37822. word32 idx;
  37823. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  37824. #ifdef OPENSSL_EXTRA
  37825. const unsigned char* pt;
  37826. int certSz;
  37827. X509* x509;
  37828. X509_NAME* x509name;
  37829. X509_NAME_ENTRY* entry;
  37830. ASN1_STRING* entryValue;
  37831. #endif
  37832. XMEMSET(&name, 0, sizeof(CertName));
  37833. /* set up cert name */
  37834. XMEMCPY(name.country, "US", sizeof("US"));
  37835. name.countryEnc = CTC_PRINTABLE;
  37836. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  37837. name.stateEnc = CTC_UTF8;
  37838. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  37839. name.localityEnc = CTC_UTF8;
  37840. XMEMCPY(name.sur, "Test", sizeof("Test"));
  37841. name.surEnc = CTC_UTF8;
  37842. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  37843. name.orgEnc = CTC_UTF8;
  37844. XMEMCPY(name.unit, "Development", sizeof("Development"));
  37845. name.unitEnc = CTC_UTF8;
  37846. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  37847. name.commonNameEnc = CTC_UTF8;
  37848. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  37849. name.serialDevEnc = CTC_PRINTABLE;
  37850. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  37851. name.userIdEnc = CTC_PRINTABLE;
  37852. #ifdef WOLFSSL_MULTI_ATTRIB
  37853. #if CTC_MAX_ATTRIB > 2
  37854. {
  37855. NameAttrib* n;
  37856. n = &name.name[0];
  37857. n->id = ASN_DOMAIN_COMPONENT;
  37858. n->type = CTC_UTF8;
  37859. n->sz = sizeof("com");
  37860. XMEMCPY(n->value, "com", sizeof("com"));
  37861. n = &name.name[1];
  37862. n->id = ASN_DOMAIN_COMPONENT;
  37863. n->type = CTC_UTF8;
  37864. n->sz = sizeof("wolfssl");
  37865. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  37866. }
  37867. #endif
  37868. #endif /* WOLFSSL_MULTI_ATTRIB */
  37869. AssertIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  37870. #ifndef HAVE_FIPS
  37871. AssertIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  37872. #else
  37873. AssertIntEQ(wc_InitRng(&rng), 0);
  37874. #endif
  37875. /* load test RSA key */
  37876. idx = 0;
  37877. #if defined(USE_CERT_BUFFERS_1024)
  37878. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  37879. sizeof_server_key_der_1024), 0);
  37880. #elif defined(USE_CERT_BUFFERS_2048)
  37881. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  37882. sizeof_server_key_der_2048), 0);
  37883. #else
  37884. /* error case, no RSA key loaded, happens later */
  37885. (void)idx;
  37886. #endif
  37887. XMEMSET(&cert, 0 , sizeof(Cert));
  37888. AssertIntEQ(wc_InitCert(&cert), 0);
  37889. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  37890. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  37891. cert.serialSz = (int)sizeof(mySerial);
  37892. cert.isCA = 1;
  37893. #ifndef NO_SHA256
  37894. cert.sigType = CTC_SHA256wRSA;
  37895. #else
  37896. cert.sigType = CTC_SHAwRSA;
  37897. #endif
  37898. /* add SKID from the Public Key */
  37899. AssertIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  37900. /* add AKID from the Public Key */
  37901. AssertIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  37902. ret = 0;
  37903. do {
  37904. #if defined(WOLFSSL_ASYNC_CRYPT)
  37905. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  37906. #endif
  37907. if (ret >= 0) {
  37908. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  37909. }
  37910. } while (ret == WC_PENDING_E);
  37911. AssertIntGT(ret, 0);
  37912. #ifdef OPENSSL_EXTRA
  37913. /* der holds a certificate with DC's now check X509 parsing of it */
  37914. certSz = ret;
  37915. pt = der;
  37916. AssertNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  37917. AssertNotNull(x509name = X509_get_subject_name(x509));
  37918. #ifdef WOLFSSL_MULTI_ATTRIB
  37919. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  37920. -1)), 5);
  37921. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  37922. idx)), 6);
  37923. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  37924. idx)), -1);
  37925. #endif /* WOLFSSL_MULTI_ATTRIB */
  37926. /* compare DN at index 0 */
  37927. AssertNotNull(entry = X509_NAME_get_entry(x509name, 0));
  37928. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  37929. AssertIntEQ(ASN1_STRING_length(entryValue), 2);
  37930. AssertStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  37931. #ifdef WOLFSSL_MULTI_ATTRIB
  37932. /* get first and second DC and compare result */
  37933. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  37934. -1)), 5);
  37935. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  37936. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  37937. AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  37938. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  37939. idx)), 6);
  37940. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  37941. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  37942. AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  37943. #endif /* WOLFSSL_MULTI_ATTRIB */
  37944. /* try invalid index locations for regression test and sanity check */
  37945. AssertNull(entry = X509_NAME_get_entry(x509name, 11));
  37946. AssertNull(entry = X509_NAME_get_entry(x509name, 20));
  37947. X509_free(x509);
  37948. #endif /* OPENSSL_EXTRA */
  37949. wc_FreeRsaKey(&key);
  37950. wc_FreeRng(&rng);
  37951. res = TEST_RES_CHECK(1);
  37952. #endif
  37953. return res;
  37954. }
  37955. static int test_wolfSSL_X509_get_version(void)
  37956. {
  37957. int res = TEST_SKIPPED;
  37958. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  37959. WOLFSSL_X509 *x509;
  37960. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  37961. AssertNotNull(x509);
  37962. AssertIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  37963. wolfSSL_X509_free(x509);
  37964. res = TEST_RES_CHECK(1);
  37965. #endif
  37966. return res;
  37967. }
  37968. static int test_wolfSSL_DES_ncbc(void)
  37969. {
  37970. int res = TEST_SKIPPED;
  37971. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  37972. const_DES_cblock myDes;
  37973. DES_cblock iv = {1};
  37974. DES_key_schedule key = {0};
  37975. unsigned char msg[] = "hello wolfssl";
  37976. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  37977. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  37978. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  37979. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  37980. /* partial block test */
  37981. DES_set_key(&key, &myDes);
  37982. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  37983. AssertIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  37984. AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  37985. DES_set_key(&key, &myDes);
  37986. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  37987. *((byte*)&iv) = 1;
  37988. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  37989. AssertIntEQ(XMEMCMP(msg, pln, 3), 0);
  37990. AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  37991. /* full block test */
  37992. DES_set_key(&key, &myDes);
  37993. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  37994. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  37995. *((byte*)&iv) = 1;
  37996. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  37997. AssertIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  37998. AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  37999. DES_set_key(&key, &myDes);
  38000. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38001. *((byte*)&iv) = 1;
  38002. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  38003. AssertIntEQ(XMEMCMP(msg, pln, 8), 0);
  38004. AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  38005. res = TEST_RES_CHECK(1);
  38006. #endif
  38007. return res;
  38008. }
  38009. static int test_wolfSSL_AES_cbc_encrypt(void)
  38010. {
  38011. int res = TEST_SKIPPED;
  38012. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA)
  38013. AES_KEY aes;
  38014. AES_KEY* aesN = NULL;
  38015. size_t len = 0;
  38016. size_t lenB = 0;
  38017. int keySz0 = 0;
  38018. int keySzN = -1;
  38019. byte out[AES_BLOCK_SIZE] = {0};
  38020. byte* outN = NULL;
  38021. /* Test vectors retrieved from:
  38022. * <begin URL>
  38023. * https://csrc.nist.gov/
  38024. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  38025. * documents/aes/KAT_AES.zip
  38026. * </end URL>
  38027. */
  38028. const byte* pt128N = NULL;
  38029. byte* key128N = NULL;
  38030. byte* iv128N = NULL;
  38031. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  38032. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38033. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  38034. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  38035. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  38036. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38037. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  38038. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  38039. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  38040. len = sizeof(pt128);
  38041. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  38042. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  38043. AssertIntNE(XMEMCMP(b, g, h), i)
  38044. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  38045. /* Stressing wolfSSL_AES_cbc_encrypt() */
  38046. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  38047. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  38048. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  38049. AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38050. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  38051. AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38052. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  38053. /* Stressing wolfSSL_AES_set_encrypt_key */
  38054. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  38055. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  38056. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  38057. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  38058. /* Stressing wolfSSL_AES_set_decrypt_key */
  38059. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  38060. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  38061. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  38062. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  38063. #ifdef WOLFSSL_AES_128
  38064. /* wolfSSL_AES_cbc_encrypt() 128-bit */
  38065. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38066. RESET_IV(iv128tmp, iv128);
  38067. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  38068. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  38069. AssertIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38070. #ifdef HAVE_AES_DECRYPT
  38071. /* wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode */
  38072. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38073. RESET_IV(iv128tmp, iv128);
  38074. len = sizeof(ct128);
  38075. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  38076. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  38077. AssertIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  38078. #endif
  38079. #endif /* WOLFSSL_AES_128 */
  38080. #ifdef WOLFSSL_AES_192
  38081. {
  38082. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  38083. * Appendix F.2.3 */
  38084. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  38085. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  38086. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  38087. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  38088. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  38089. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  38090. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  38091. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  38092. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  38093. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  38094. len = sizeof(pt192);
  38095. /* wolfSSL_AES_cbc_encrypt() 192-bit */
  38096. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38097. RESET_IV(iv192tmp, iv192);
  38098. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  38099. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  38100. AssertIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  38101. #ifdef HAVE_AES_DECRYPT
  38102. /* wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode */
  38103. len = sizeof(ct192);
  38104. RESET_IV(iv192tmp, iv192);
  38105. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38106. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  38107. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  38108. AssertIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  38109. #endif
  38110. }
  38111. #endif /* WOLFSSL_AES_192 */
  38112. #ifdef WOLFSSL_AES_256
  38113. {
  38114. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  38115. * Appendix F.2.5 */
  38116. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  38117. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  38118. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  38119. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  38120. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  38121. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  38122. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  38123. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  38124. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  38125. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  38126. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  38127. len = sizeof(pt256);
  38128. /* wolfSSL_AES_cbc_encrypt() 256-bit */
  38129. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38130. RESET_IV(iv256tmp, iv256);
  38131. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38132. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  38133. AssertIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  38134. #ifdef HAVE_AES_DECRYPT
  38135. /* wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode */
  38136. len = sizeof(ct256);
  38137. RESET_IV(iv256tmp, iv256);
  38138. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38139. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38140. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  38141. AssertIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  38142. #endif
  38143. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  38144. {
  38145. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  38146. byte wrapPlain[sizeof(key256)] = { 0 };
  38147. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  38148. /* wolfSSL_AES_wrap_key() 256-bit NULL iv */
  38149. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38150. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  38151. 15), WOLFSSL_FAILURE);
  38152. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  38153. sizeof(key256)), sizeof(wrapCipher));
  38154. /* wolfSSL_AES_unwrap_key() 256-bit NULL iv */
  38155. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38156. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  38157. 23), WOLFSSL_FAILURE);
  38158. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  38159. sizeof(wrapCipher)), sizeof(wrapPlain));
  38160. AssertIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  38161. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  38162. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  38163. /* wolfSSL_AES_wrap_key() 256-bit custom iv */
  38164. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38165. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  38166. sizeof(key256)), sizeof(wrapCipher));
  38167. /* wolfSSL_AES_unwrap_key() 256-bit custom iv */
  38168. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38169. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  38170. sizeof(wrapCipher)), sizeof(wrapPlain));
  38171. AssertIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  38172. }
  38173. #endif /* HAVE_AES_KEYWRAP */
  38174. }
  38175. #endif /* WOLFSSL_AES_256 */
  38176. res = TEST_RES_CHECK(1);
  38177. #endif
  38178. return res;
  38179. }
  38180. static int test_wolfSSL_CRYPTO_cts128(void)
  38181. {
  38182. int res = TEST_SKIPPED;
  38183. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) \
  38184. && defined(HAVE_CTS)
  38185. byte tmp[64]; /* Largest vector size */
  38186. /* Test vectors taken form RFC3962 Appendix B */
  38187. const testVector vects[] = {
  38188. {
  38189. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38190. "\x20",
  38191. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  38192. "\x97",
  38193. 17, 17
  38194. },
  38195. {
  38196. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38197. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  38198. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  38199. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  38200. 31, 31
  38201. },
  38202. {
  38203. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38204. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  38205. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  38206. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  38207. 32, 32
  38208. },
  38209. {
  38210. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38211. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  38212. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  38213. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  38214. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  38215. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  38216. 47, 47
  38217. },
  38218. {
  38219. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38220. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  38221. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  38222. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  38223. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  38224. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  38225. 48, 48
  38226. },
  38227. {
  38228. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38229. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  38230. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  38231. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  38232. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  38233. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  38234. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  38235. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  38236. 64, 64
  38237. }
  38238. };
  38239. byte keyBytes[AES_128_KEY_SIZE] = {
  38240. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  38241. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  38242. };
  38243. size_t i;
  38244. XMEMSET(tmp, 0, sizeof(tmp));
  38245. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  38246. AES_KEY encKey;
  38247. AES_KEY decKey;
  38248. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  38249. XMEMSET(iv, 0, sizeof(iv));
  38250. AssertIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8, &encKey), 0);
  38251. AssertIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8, &decKey), 0);
  38252. AssertIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  38253. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  38254. vects[i].outLen);
  38255. AssertIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  38256. XMEMSET(iv, 0, sizeof(iv));
  38257. AssertIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  38258. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  38259. vects[i].inLen);
  38260. AssertIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  38261. }
  38262. res = TEST_RES_CHECK(1);
  38263. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  38264. return res;
  38265. }
  38266. #if defined(OPENSSL_ALL)
  38267. #if !defined(NO_ASN)
  38268. static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
  38269. {
  38270. int res = TEST_SKIPPED;
  38271. #if !defined(NO_RSA)
  38272. WOLFSSL_X509* x509;
  38273. WOLFSSL_X509_NAME* subject;
  38274. WOLFSSL_X509_NAME_ENTRY* e;
  38275. WOLFSSL_ASN1_STRING* a;
  38276. FILE* file;
  38277. int idx = 0;
  38278. char targetOutput[16] = "www.wolfssl.com";
  38279. unsigned char* actual_output;
  38280. int len = 0;
  38281. int result = 0;
  38282. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  38283. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  38284. fclose(file);
  38285. /* wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName */
  38286. AssertNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  38287. AssertIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  38288. NID_commonName, -1)), 5);
  38289. AssertNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  38290. AssertNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  38291. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  38292. result = strncmp((const char*)actual_output, targetOutput, len);
  38293. AssertIntEQ(result, 0);
  38294. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, valid) */
  38295. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)),
  38296. WOLFSSL_FATAL_ERROR);
  38297. /* wolfSSL_ASN1_STRING_to_UTF8(valid, NULL) */
  38298. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)),
  38299. WOLFSSL_FATAL_ERROR);
  38300. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL) */
  38301. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)),
  38302. WOLFSSL_FATAL_ERROR);
  38303. wolfSSL_X509_free(x509);
  38304. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38305. res = TEST_RES_CHECK(1);
  38306. #endif
  38307. return res;
  38308. }
  38309. static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  38310. {
  38311. ASN1_STRING* asn1str_test;
  38312. ASN1_STRING* asn1str_answer;
  38313. /* Each character is encoded using 4 bytes */
  38314. char input[] = {
  38315. 0, 0, 0, 'T',
  38316. 0, 0, 0, 'e',
  38317. 0, 0, 0, 's',
  38318. 0, 0, 0, 't',
  38319. };
  38320. char output[] = "Test";
  38321. AssertNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  38322. AssertIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  38323. AssertIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  38324. AssertNotNull(asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  38325. AssertIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  38326. AssertIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  38327. ASN1_STRING_free(asn1str_test);
  38328. ASN1_STRING_free(asn1str_answer);
  38329. return TEST_RES_CHECK(1);
  38330. }
  38331. #endif /* !defined(NO_ASN) */
  38332. static int test_wolfSSL_sk_CIPHER_description(void)
  38333. {
  38334. int res = TEST_SKIPPED;
  38335. #if !defined(NO_RSA)
  38336. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  38337. int i,j,k;
  38338. int numCiphers = 0;
  38339. const SSL_METHOD *method = NULL;
  38340. const SSL_CIPHER *cipher = NULL;
  38341. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  38342. SSL_CTX *ctx = NULL;
  38343. SSL *ssl = NULL;
  38344. char buf[256];
  38345. char test_str[9] = "0000000";
  38346. const char badStr[] = "unknown";
  38347. const char certPath[] = "./certs/client-cert.pem";
  38348. XMEMSET(buf, 0, sizeof(buf));
  38349. AssertNotNull(method = TLSv1_2_client_method());
  38350. AssertNotNull(ctx = SSL_CTX_new(method));
  38351. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  38352. SSL_CTX_set_verify_depth(ctx, 4);
  38353. SSL_CTX_set_options(ctx, flags);
  38354. AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  38355. WOLFSSL_SUCCESS);
  38356. AssertNotNull(ssl = SSL_new(ctx));
  38357. /* SSL_get_ciphers returns a stack of all configured ciphers
  38358. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  38359. */
  38360. AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  38361. /* loop through the amount of supportedCiphers */
  38362. numCiphers = sk_num(supportedCiphers);
  38363. for (i = 0; i < numCiphers; ++i) {
  38364. /* sk_value increments "sk->data.cipher->cipherOffset".
  38365. * wolfSSL_sk_CIPHER_description sets the description for
  38366. * the cipher based on the provided offset.
  38367. */
  38368. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  38369. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  38370. }
  38371. /* Search cipher description string for "unknown" descriptor */
  38372. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  38373. k = 0;
  38374. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  38375. test_str[k] = badStr[k];
  38376. j++;
  38377. k++;
  38378. }
  38379. }
  38380. /* Fail if test_str == badStr == "unknown" */
  38381. AssertStrNE(test_str,badStr);
  38382. }
  38383. SSL_free(ssl);
  38384. SSL_CTX_free(ctx);
  38385. res = TEST_RES_CHECK(1);
  38386. #endif
  38387. return res;
  38388. }
  38389. static int test_wolfSSL_get_ciphers_compat(void)
  38390. {
  38391. int res = TEST_SKIPPED;
  38392. #if !defined(NO_RSA)
  38393. const SSL_METHOD *method = NULL;
  38394. const char certPath[] = "./certs/client-cert.pem";
  38395. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  38396. SSL_CTX *ctx = NULL;
  38397. WOLFSSL *ssl = NULL;
  38398. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  38399. method = SSLv23_client_method();
  38400. AssertNotNull(method);
  38401. ctx = SSL_CTX_new(method);
  38402. AssertNotNull(ctx);
  38403. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  38404. SSL_CTX_set_verify_depth(ctx, 4);
  38405. SSL_CTX_set_options(ctx, flags);
  38406. AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  38407. WOLFSSL_SUCCESS);
  38408. AssertNotNull(ssl = SSL_new(ctx));
  38409. /* Test Bad NULL input */
  38410. AssertNull(supportedCiphers = SSL_get_ciphers(NULL));
  38411. /* Test for Good input */
  38412. AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  38413. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  38414. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  38415. SSL_free(ssl);
  38416. SSL_CTX_free(ctx);
  38417. res = TEST_RES_CHECK(1);
  38418. #endif
  38419. return res;
  38420. }
  38421. static int test_wolfSSL_X509_PUBKEY_get(void)
  38422. {
  38423. WOLFSSL_X509_PUBKEY pubkey;
  38424. WOLFSSL_X509_PUBKEY* key;
  38425. WOLFSSL_EVP_PKEY evpkey ;
  38426. WOLFSSL_EVP_PKEY* evpPkey;
  38427. WOLFSSL_EVP_PKEY* retEvpPkey;
  38428. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  38429. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  38430. key = &pubkey;
  38431. evpPkey = &evpkey;
  38432. evpPkey->type = WOLFSSL_SUCCESS;
  38433. key->pkey = evpPkey;
  38434. AssertNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  38435. AssertIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  38436. AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  38437. key->pkey = NULL;
  38438. AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  38439. return TEST_RES_CHECK(retEvpPkey == NULL);
  38440. }
  38441. static int test_wolfSSL_EC_KEY_dup(void)
  38442. {
  38443. int res = TEST_SKIPPED;
  38444. #if defined(HAVE_ECC) && (defined(OPENSSL_EXTRA) || \
  38445. defined(OPENSSL_EXTRA_X509_SMALL))
  38446. WOLFSSL_EC_KEY* ecKey;
  38447. WOLFSSL_EC_KEY* dupKey;
  38448. ecc_key* srcKey;
  38449. ecc_key* destKey;
  38450. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38451. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  38452. /* Valid cases */
  38453. AssertNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  38454. AssertIntEQ(EC_KEY_check_key(dupKey), 1);
  38455. /* Compare pubkey */
  38456. srcKey = (ecc_key*)ecKey->internal;
  38457. destKey = (ecc_key*)dupKey->internal;
  38458. AssertIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  38459. /* compare EC_GROUP */
  38460. AssertIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  38461. /* compare EC_POINT */
  38462. AssertIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  38463. dupKey->pub_key, NULL), MP_EQ);
  38464. /* compare BIGNUM */
  38465. AssertIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  38466. wolfSSL_EC_KEY_free(dupKey);
  38467. /* Invalid cases */
  38468. /* NULL key */
  38469. AssertNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  38470. /* NULL ecc_key */
  38471. wc_ecc_free((ecc_key*)ecKey->internal);
  38472. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  38473. ecKey->internal = NULL; /* Set ecc_key to NULL */
  38474. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  38475. wolfSSL_EC_KEY_free(ecKey);
  38476. wolfSSL_EC_KEY_free(dupKey);
  38477. /* NULL Group */
  38478. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38479. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  38480. wolfSSL_EC_GROUP_free(ecKey->group);
  38481. ecKey->group = NULL; /* Set group to NULL */
  38482. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  38483. wolfSSL_EC_KEY_free(ecKey);
  38484. wolfSSL_EC_KEY_free(dupKey);
  38485. /* NULL public key */
  38486. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38487. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  38488. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  38489. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  38490. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  38491. wolfSSL_EC_POINT_free(ecKey->pub_key);
  38492. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  38493. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  38494. wolfSSL_EC_KEY_free(ecKey);
  38495. wolfSSL_EC_KEY_free(dupKey);
  38496. /* NULL private key */
  38497. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38498. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  38499. wolfSSL_BN_free(ecKey->priv_key);
  38500. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  38501. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  38502. wolfSSL_EC_KEY_free(ecKey);
  38503. wolfSSL_EC_KEY_free(dupKey);
  38504. /* Test EC_KEY_up_ref */
  38505. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38506. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  38507. AssertIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  38508. AssertIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  38509. /* reference count doesn't follow duplicate */
  38510. AssertNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  38511. AssertIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  38512. AssertIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  38513. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  38514. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  38515. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  38516. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  38517. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  38518. res = TEST_RES_CHECK(1);
  38519. #endif
  38520. return res;
  38521. }
  38522. static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  38523. {
  38524. int res = TEST_SKIPPED;
  38525. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  38526. DSA *dsa = NULL;
  38527. DSA *setDsa = NULL;
  38528. EVP_PKEY *pkey = NULL;
  38529. EVP_PKEY *set1Pkey = NULL;
  38530. SHA_CTX sha;
  38531. byte signature[DSA_SIG_SIZE];
  38532. byte hash[WC_SHA_DIGEST_SIZE];
  38533. word32 bytes;
  38534. int answer;
  38535. #ifdef USE_CERT_BUFFERS_1024
  38536. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  38537. int dsaKeySz = sizeof_dsa_key_der_1024;
  38538. byte tmp[ONEK_BUF];
  38539. XMEMSET(tmp, 0, sizeof(tmp));
  38540. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  38541. bytes = dsaKeySz;
  38542. #elif defined(USE_CERT_BUFFERS_2048)
  38543. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  38544. int dsaKeySz = sizeof_dsa_key_der_2048;
  38545. byte tmp[TWOK_BUF];
  38546. XMEMSET(tmp, 0, sizeof(tmp));
  38547. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  38548. bytes = dsaKeySz;
  38549. #else
  38550. byte tmp[TWOK_BUF];
  38551. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  38552. int dsaKeySz;
  38553. XMEMSET(tmp, 0, sizeof(tmp));
  38554. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  38555. if (fp == XBADFILE) {
  38556. return WOLFSSL_BAD_FILE;
  38557. }
  38558. dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  38559. XFCLOSE(fp);
  38560. #endif /* END USE_CERT_BUFFERS_1024 */
  38561. /* Create hash to later Sign and Verify */
  38562. AssertIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  38563. AssertIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  38564. AssertIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  38565. /* Initialize pkey with der format dsa key */
  38566. AssertNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey,
  38567. &dsaKeyDer ,(long)dsaKeySz));
  38568. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  38569. /* Should Fail: NULL argument */
  38570. AssertNull(dsa = EVP_PKEY_get0_DSA(NULL));
  38571. AssertNull(dsa = EVP_PKEY_get1_DSA(NULL));
  38572. /* Should Pass: Initialized pkey argument */
  38573. AssertNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  38574. AssertNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  38575. #ifdef USE_CERT_BUFFERS_1024
  38576. AssertIntEQ(DSA_bits(dsa), 1024);
  38577. #else
  38578. AssertIntEQ(DSA_bits(dsa), 2048);
  38579. #endif
  38580. /* Sign */
  38581. AssertIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  38582. /* Verify. */
  38583. AssertIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  38584. WOLFSSL_SUCCESS);
  38585. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  38586. /* Should Fail: set1Pkey not initialized */
  38587. AssertIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  38588. /* Initialize set1Pkey */
  38589. set1Pkey = EVP_PKEY_new();
  38590. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  38591. AssertIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  38592. WOLFSSL_SUCCESS);
  38593. /* Should Pass: set dsa into set1Pkey */
  38594. AssertIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  38595. DSA_free(dsa);
  38596. DSA_free(setDsa);
  38597. EVP_PKEY_free(pkey);
  38598. EVP_PKEY_free(set1Pkey);
  38599. res = TEST_RES_CHECK(1);
  38600. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  38601. return res;
  38602. } /* END test_EVP_PKEY_set1_get1_DSA */
  38603. static int test_wolfSSL_DSA_SIG(void)
  38604. {
  38605. int res = TEST_SKIPPED;
  38606. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  38607. !defined(HAVE_FIPS)
  38608. DSA *dsa = NULL;
  38609. DSA *dsa2 = NULL;
  38610. DSA_SIG *sig = NULL;
  38611. const BIGNUM *p = NULL;
  38612. const BIGNUM *q = NULL;
  38613. const BIGNUM *g = NULL;
  38614. const BIGNUM *pub = NULL;
  38615. const BIGNUM *priv = NULL;
  38616. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  38617. AssertNotNull(dsa = DSA_generate_parameters(2048,
  38618. NULL, 0, NULL, NULL, NULL, NULL));
  38619. DSA_free(dsa);
  38620. AssertNotNull(dsa = DSA_new());
  38621. AssertIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  38622. NULL, 0, NULL, NULL, NULL), 1);
  38623. AssertIntEQ(DSA_generate_key(dsa), 1);
  38624. DSA_get0_pqg(dsa, &p, &q, &g);
  38625. DSA_get0_key(dsa, &pub, &priv);
  38626. AssertNotNull(p = BN_dup(p));
  38627. AssertNotNull(q = BN_dup(q));
  38628. AssertNotNull(g = BN_dup(g));
  38629. AssertNotNull(pub = BN_dup(pub));
  38630. AssertNotNull(priv = BN_dup(priv));
  38631. AssertNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  38632. AssertNotNull(dsa2 = DSA_new());
  38633. AssertIntEQ(DSA_set0_pqg(dsa2, (BIGNUM*)p, (BIGNUM*)q, (BIGNUM*)g), 1);
  38634. AssertIntEQ(DSA_set0_key(dsa2, (BIGNUM*)pub, (BIGNUM*)priv), 1);
  38635. AssertIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  38636. DSA_free(dsa);
  38637. DSA_free(dsa2);
  38638. DSA_SIG_free(sig);
  38639. res = TEST_RES_CHECK(1);
  38640. #endif
  38641. return res;
  38642. }
  38643. static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  38644. {
  38645. int res = TEST_SKIPPED;
  38646. #ifdef HAVE_ECC
  38647. WOLFSSL_EC_KEY *ecKey = NULL;
  38648. WOLFSSL_EC_KEY *ecGet1 = NULL;
  38649. EVP_PKEY *pkey = NULL;
  38650. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38651. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38652. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  38653. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  38654. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  38655. /* Should fail since ecKey is empty */
  38656. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  38657. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  38658. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  38659. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  38660. AssertNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  38661. AssertNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  38662. wolfSSL_EC_KEY_free(ecKey);
  38663. wolfSSL_EC_KEY_free(ecGet1);
  38664. EVP_PKEY_free(pkey);
  38665. res = TEST_RES_CHECK(1);
  38666. #endif /* HAVE_ECC */
  38667. return res;
  38668. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  38669. static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  38670. {
  38671. int res = TEST_SKIPPED;
  38672. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  38673. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  38674. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  38675. DH *dh = NULL;
  38676. DH *setDh = NULL;
  38677. EVP_PKEY *pkey = NULL;
  38678. FILE* f = NULL;
  38679. unsigned char buf[4096];
  38680. const unsigned char* pt = buf;
  38681. const char* dh2048 = "./certs/dh2048.der";
  38682. long len = 0;
  38683. int code = -1;
  38684. XMEMSET(buf, 0, sizeof(buf));
  38685. f = XFOPEN(dh2048, "rb");
  38686. AssertTrue(f != XBADFILE);
  38687. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  38688. XFCLOSE(f);
  38689. /* Load dh2048.der into DH with internal format */
  38690. AssertNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  38691. AssertIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  38692. AssertIntEQ(code, 0);
  38693. code = -1;
  38694. pkey = wolfSSL_EVP_PKEY_new();
  38695. /* Set DH into PKEY */
  38696. AssertIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  38697. /* Get DH from PKEY */
  38698. AssertNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  38699. AssertIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  38700. AssertIntEQ(code, 0);
  38701. EVP_PKEY_free(pkey);
  38702. DH_free(setDh);
  38703. DH_free(dh);
  38704. res = TEST_RES_CHECK(1);
  38705. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  38706. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  38707. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  38708. return res;
  38709. } /* END test_EVP_PKEY_set1_get1_DH */
  38710. static int test_wolfSSL_CTX_ctrl(void)
  38711. {
  38712. int res = TEST_SKIPPED;
  38713. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38714. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  38715. char caFile[] = "./certs/client-ca.pem";
  38716. char clientFile[] = "./certs/client-cert.pem";
  38717. SSL_CTX* ctx;
  38718. X509* x509 = NULL;
  38719. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  38720. byte buf[6000];
  38721. char file[] = "./certs/dsaparams.pem";
  38722. XFILE f;
  38723. int bytes;
  38724. BIO* bio;
  38725. DSA* dsa;
  38726. DH* dh;
  38727. #endif
  38728. #ifdef HAVE_ECC
  38729. WOLFSSL_EC_KEY* ecKey;
  38730. #endif
  38731. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  38732. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  38733. AssertNotNull(x509);
  38734. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  38735. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  38736. AssertNotNull(x509);
  38737. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  38738. /* Initialize DH */
  38739. f = XFOPEN(file, "rb");
  38740. AssertTrue((f != XBADFILE));
  38741. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  38742. XFCLOSE(f);
  38743. bio = BIO_new_mem_buf((void*)buf, bytes);
  38744. AssertNotNull(bio);
  38745. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  38746. AssertNotNull(dsa);
  38747. dh = wolfSSL_DSA_dup_DH(dsa);
  38748. AssertNotNull(dh);
  38749. #endif
  38750. #ifdef HAVE_ECC
  38751. /* Initialize WOLFSSL_EC_KEY */
  38752. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38753. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey),1);
  38754. #endif
  38755. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  38756. /* additional test of getting EVP_PKEY key size from X509
  38757. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  38758. * allowed with user RSA */
  38759. {
  38760. EVP_PKEY* pkey;
  38761. #if defined(HAVE_ECC)
  38762. X509* ecX509;
  38763. #endif /* HAVE_ECC */
  38764. AssertNotNull(pkey = X509_get_pubkey(x509));
  38765. /* current RSA key is 2048 bit (256 bytes) */
  38766. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  38767. EVP_PKEY_free(pkey);
  38768. #if defined(HAVE_ECC)
  38769. #if defined(USE_CERT_BUFFERS_256)
  38770. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  38771. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  38772. SSL_FILETYPE_ASN1));
  38773. #else
  38774. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  38775. cliEccCertFile, SSL_FILETYPE_PEM));
  38776. #endif
  38777. AssertNotNull(pkey = X509_get_pubkey(ecX509));
  38778. /* current ECC key is 256 bit (32 bytes) */
  38779. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  38780. X509_free(ecX509);
  38781. EVP_PKEY_free(pkey);
  38782. #endif /* HAVE_ECC */
  38783. }
  38784. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  38785. /* Tests should fail with passed in NULL pointer */
  38786. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,NULL),
  38787. SSL_FAILURE);
  38788. #if !defined(NO_DH) && !defined(NO_DSA)
  38789. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,NULL),
  38790. SSL_FAILURE);
  38791. #endif
  38792. #ifdef HAVE_ECC
  38793. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,NULL),
  38794. SSL_FAILURE);
  38795. #endif
  38796. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  38797. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  38798. */
  38799. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,x509),
  38800. SSL_SUCCESS);
  38801. /* Test with SSL_CTRL_OPTIONS
  38802. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  38803. */
  38804. AssertTrue(wolfSSL_CTX_ctrl(ctx,SSL_CTRL_OPTIONS,SSL_OP_NO_TLSv1,NULL)
  38805. == SSL_OP_NO_TLSv1);
  38806. AssertTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  38807. /* Test with SSL_CTRL_SET_TMP_DH
  38808. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  38809. */
  38810. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  38811. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,dh),
  38812. SSL_SUCCESS);
  38813. #endif
  38814. /* Test with SSL_CTRL_SET_TMP_ECDH
  38815. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  38816. */
  38817. #ifdef HAVE_ECC
  38818. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,ecKey),
  38819. SSL_SUCCESS);
  38820. #endif
  38821. #ifdef WOLFSSL_ENCRYPTED_KEYS
  38822. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  38823. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  38824. #endif
  38825. /* Test for min/max proto */
  38826. #ifndef WOLFSSL_NO_TLS12
  38827. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  38828. 0, NULL), SSL_SUCCESS);
  38829. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  38830. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  38831. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  38832. #endif
  38833. #ifdef WOLFSSL_TLS13
  38834. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  38835. 0, NULL), SSL_SUCCESS);
  38836. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  38837. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  38838. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  38839. #ifndef WOLFSSL_NO_TLS12
  38840. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  38841. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  38842. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  38843. #endif
  38844. #endif
  38845. /* Cleanup and Pass */
  38846. #if !defined(NO_DH) && !defined(NO_DSA)
  38847. #ifndef NO_BIO
  38848. BIO_free(bio);
  38849. DSA_free(dsa);
  38850. DH_free(dh);
  38851. #endif
  38852. #endif
  38853. #ifdef HAVE_ECC
  38854. wolfSSL_EC_KEY_free(ecKey);
  38855. #endif
  38856. SSL_CTX_free(ctx);
  38857. res = TEST_RES_CHECK(1);
  38858. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38859. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  38860. return res;
  38861. }
  38862. static int test_wolfSSL_EVP_PKEY_assign(void)
  38863. {
  38864. int res = TEST_SKIPPED;
  38865. int type;
  38866. WOLFSSL_EVP_PKEY* pkey;
  38867. #ifndef NO_RSA
  38868. WOLFSSL_RSA* rsa;
  38869. #endif
  38870. #ifndef NO_DSA
  38871. WOLFSSL_DSA* dsa;
  38872. #endif
  38873. #ifdef HAVE_ECC
  38874. WOLFSSL_EC_KEY* ecKey;
  38875. #endif
  38876. (void)pkey;
  38877. #ifndef NO_RSA
  38878. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  38879. type = EVP_PKEY_RSA;
  38880. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38881. AssertNotNull(rsa = wolfSSL_RSA_new());
  38882. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,rsa), WOLFSSL_FAILURE);
  38883. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  38884. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,rsa), WOLFSSL_FAILURE);
  38885. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,rsa), WOLFSSL_SUCCESS);
  38886. wolfSSL_EVP_PKEY_free(pkey);
  38887. res = TEST_RES_CHECK(1);
  38888. }
  38889. #endif /* NO_RSA */
  38890. #ifndef NO_DSA
  38891. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  38892. type = EVP_PKEY_DSA;
  38893. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38894. AssertNotNull(dsa = wolfSSL_DSA_new());
  38895. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,dsa), WOLFSSL_FAILURE);
  38896. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  38897. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,dsa), WOLFSSL_FAILURE);
  38898. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,dsa), WOLFSSL_SUCCESS);
  38899. wolfSSL_EVP_PKEY_free(pkey);
  38900. res = TEST_RES_CHECK(1);
  38901. }
  38902. #endif /* NO_DSA */
  38903. #ifdef HAVE_ECC
  38904. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  38905. type = EVP_PKEY_EC;
  38906. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38907. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38908. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,ecKey), WOLFSSL_FAILURE);
  38909. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  38910. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,ecKey), WOLFSSL_FAILURE);
  38911. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_FAILURE);
  38912. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  38913. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_SUCCESS);
  38914. wolfSSL_EVP_PKEY_free(pkey);
  38915. res = TEST_RES_CHECK(1);
  38916. }
  38917. #endif /* HAVE_ECC */
  38918. (void)type;
  38919. return res;
  38920. }
  38921. static int test_wolfSSL_EVP_PKEY_base_id(void)
  38922. {
  38923. WOLFSSL_EVP_PKEY* pkey;
  38924. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38925. AssertIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  38926. AssertIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  38927. EVP_PKEY_free(pkey);
  38928. return TEST_RES_CHECK(1);
  38929. }
  38930. static int test_wolfSSL_EVP_PKEY_id(void)
  38931. {
  38932. WOLFSSL_EVP_PKEY* pkey;
  38933. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38934. AssertIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  38935. AssertIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  38936. EVP_PKEY_free(pkey);
  38937. return TEST_RES_CHECK(1);
  38938. }
  38939. static int test_wolfSSL_EVP_PKEY_paramgen(void)
  38940. {
  38941. int res = TEST_SKIPPED;
  38942. #if defined(OPENSSL_ALL) && \
  38943. !defined(NO_ECC_SECP) && \
  38944. /* This last bit is taken from ecc.c. It is the condition that
  38945. * defines ECC256 */ \
  38946. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  38947. ECC_MIN_KEY_SZ <= 256)
  38948. EVP_PKEY_CTX* ctx;
  38949. EVP_PKEY* pkey = NULL;
  38950. /* Test error conditions. */
  38951. AssertIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  38952. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  38953. AssertIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  38954. #ifndef NO_RSA
  38955. EVP_PKEY_CTX_free(ctx);
  38956. /* Parameter generation for RSA not supported yet. */
  38957. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  38958. AssertIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  38959. #endif
  38960. #ifdef HAVE_ECC
  38961. EVP_PKEY_CTX_free(ctx);
  38962. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  38963. AssertIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  38964. AssertIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  38965. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  38966. AssertIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  38967. AssertIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  38968. WOLFSSL_SUCCESS);
  38969. AssertIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  38970. AssertIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  38971. #endif
  38972. EVP_PKEY_CTX_free(ctx);
  38973. EVP_PKEY_free(pkey);
  38974. res = TEST_RES_CHECK(1);
  38975. #endif
  38976. return res;
  38977. }
  38978. static int test_wolfSSL_EVP_PKEY_keygen(void)
  38979. {
  38980. WOLFSSL_EVP_PKEY* pkey = NULL;
  38981. EVP_PKEY_CTX* ctx = NULL;
  38982. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  38983. WOLFSSL_EVP_PKEY* params = NULL;
  38984. DH* dh = NULL;
  38985. const BIGNUM* pubkey = NULL;
  38986. const BIGNUM* privkey = NULL;
  38987. ASN1_INTEGER* asn1int = NULL;
  38988. unsigned int length = 0;
  38989. byte* derBuffer = NULL;
  38990. #endif
  38991. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38992. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  38993. /* Bad cases */
  38994. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  38995. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  38996. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  38997. /* Good case */
  38998. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  38999. EVP_PKEY_CTX_free(ctx);
  39000. EVP_PKEY_free(pkey);
  39001. pkey = NULL;
  39002. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  39003. /* Test DH keygen */
  39004. {
  39005. AssertNotNull(params = wolfSSL_EVP_PKEY_new());
  39006. AssertNotNull(dh = DH_get_2048_256());
  39007. AssertIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  39008. AssertNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  39009. AssertIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  39010. AssertIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  39011. DH_free(dh);
  39012. EVP_PKEY_CTX_free(ctx);
  39013. EVP_PKEY_free(params);
  39014. /* try exporting generated key to DER, to verify */
  39015. AssertNotNull(dh = EVP_PKEY_get1_DH(pkey));
  39016. DH_get0_key(dh, &pubkey, &privkey);
  39017. AssertNotNull(pubkey);
  39018. AssertNotNull(privkey);
  39019. AssertNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  39020. AssertIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  39021. ASN1_INTEGER_free(asn1int);
  39022. DH_free(dh);
  39023. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39024. EVP_PKEY_free(pkey);
  39025. }
  39026. #endif
  39027. return TEST_RES_CHECK(1);
  39028. }
  39029. static int test_wolfSSL_EVP_PKEY_keygen_init(void)
  39030. {
  39031. WOLFSSL_EVP_PKEY* pkey;
  39032. EVP_PKEY_CTX *ctx;
  39033. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39034. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39035. AssertIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  39036. EVP_PKEY_CTX_free(ctx);
  39037. EVP_PKEY_free(pkey);
  39038. return TEST_RES_CHECK(1);
  39039. }
  39040. static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
  39041. {
  39042. int res = TEST_SKIPPED;
  39043. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  39044. WOLFSSL_EVP_PKEY* pkey;
  39045. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39046. AssertIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  39047. EVP_PKEY_free(pkey);
  39048. res = TEST_RES_CHECK(1);
  39049. #endif
  39050. return res;
  39051. }
  39052. static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
  39053. {
  39054. int res = TEST_SKIPPED;
  39055. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  39056. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  39057. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  39058. !defined(NO_FILESYSTEM)
  39059. WOLFSSL_EVP_PKEY* params = NULL;
  39060. WOLFSSL_EVP_PKEY* copy = NULL;
  39061. DH* dh = NULL;
  39062. BIGNUM* p1;
  39063. BIGNUM* g1;
  39064. BIGNUM* q1;
  39065. BIGNUM* p2;
  39066. BIGNUM* g2;
  39067. BIGNUM* q2;
  39068. /* create DH with DH_get_2048_256 params */
  39069. AssertNotNull(params = wolfSSL_EVP_PKEY_new());
  39070. AssertNotNull(dh = DH_get_2048_256());
  39071. AssertIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  39072. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  39073. (const BIGNUM**)&q1,
  39074. (const BIGNUM**)&g1);
  39075. DH_free(dh);
  39076. /* create DH with random generated DH params */
  39077. AssertNotNull(copy = wolfSSL_EVP_PKEY_new());
  39078. AssertNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  39079. AssertIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  39080. DH_free(dh);
  39081. AssertIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  39082. AssertNotNull(dh = EVP_PKEY_get1_DH(copy));
  39083. AssertNotNull(dh->p);
  39084. AssertNotNull(dh->g);
  39085. AssertNotNull(dh->q);
  39086. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  39087. (const BIGNUM**)&q2,
  39088. (const BIGNUM**)&g2);
  39089. AssertIntEQ(BN_cmp(p1, p2), 0);
  39090. AssertIntEQ(BN_cmp(q1, q2), 0);
  39091. AssertIntEQ(BN_cmp(g1, g2), 0);
  39092. DH_free(dh);
  39093. EVP_PKEY_free(copy);
  39094. EVP_PKEY_free(params);
  39095. res = TEST_RES_CHECK(1);
  39096. #endif
  39097. return res;
  39098. }
  39099. static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  39100. {
  39101. WOLFSSL_EVP_PKEY* pkey;
  39102. EVP_PKEY_CTX *ctx;
  39103. int bits = 2048;
  39104. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39105. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39106. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  39107. WOLFSSL_SUCCESS);
  39108. EVP_PKEY_CTX_free(ctx);
  39109. EVP_PKEY_free(pkey);
  39110. return TEST_RES_CHECK(1);
  39111. }
  39112. static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  39113. {
  39114. /* This is large enough to be used for all key sizes */
  39115. byte key[AES_256_KEY_SIZE] = {0};
  39116. byte iv[AES_BLOCK_SIZE] = {0};
  39117. int i, enumlen;
  39118. EVP_CIPHER_CTX *ctx;
  39119. const EVP_CIPHER *init;
  39120. int enumArray[] = {
  39121. #ifdef HAVE_AES_CBC
  39122. NID_aes_128_cbc,
  39123. #endif
  39124. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  39125. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39126. #ifdef HAVE_AESGCM
  39127. NID_aes_128_gcm,
  39128. #endif
  39129. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  39130. #ifdef WOLFSSL_AES_COUNTER
  39131. NID_aes_128_ctr,
  39132. #endif
  39133. #ifndef NO_DES3
  39134. NID_des_cbc,
  39135. NID_des_ede3_cbc,
  39136. #endif
  39137. };
  39138. int iv_lengths[] = {
  39139. #ifdef HAVE_AES_CBC
  39140. AES_BLOCK_SIZE,
  39141. #endif
  39142. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  39143. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39144. #ifdef HAVE_AESGCM
  39145. GCM_NONCE_MID_SZ,
  39146. #endif
  39147. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  39148. #ifdef WOLFSSL_AES_COUNTER
  39149. AES_BLOCK_SIZE,
  39150. #endif
  39151. #ifndef NO_DES3
  39152. DES_BLOCK_SIZE,
  39153. DES_BLOCK_SIZE,
  39154. #endif
  39155. };
  39156. enumlen = (sizeof(enumArray)/sizeof(int));
  39157. for (i = 0; i < enumlen; i++) {
  39158. ctx = EVP_CIPHER_CTX_new();
  39159. init = wolfSSL_EVP_get_cipherbynid(enumArray[i]);
  39160. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39161. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39162. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  39163. EVP_CIPHER_CTX_free(ctx);
  39164. }
  39165. return TEST_RES_CHECK(1);
  39166. }
  39167. static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  39168. {
  39169. int res = TEST_SKIPPED;
  39170. #if !defined(NO_DES3)
  39171. byte key[AES_256_KEY_SIZE] = {0};
  39172. byte iv[AES_BLOCK_SIZE] = {0};
  39173. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  39174. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  39175. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39176. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39177. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), 24);
  39178. EVP_CIPHER_CTX_free(ctx);
  39179. res = TEST_RES_CHECK(1);
  39180. #endif
  39181. return res;
  39182. }
  39183. static int test_wolfSSL_EVP_CIPHER_CTX_set_key_length(void)
  39184. {
  39185. int res = TEST_SKIPPED;
  39186. #if !defined(NO_DES3)
  39187. byte key[AES_256_KEY_SIZE] = {0};
  39188. byte iv[AES_BLOCK_SIZE] = {0};
  39189. int keylen;
  39190. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  39191. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  39192. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39193. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39194. keylen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  39195. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, keylen),
  39196. WOLFSSL_SUCCESS);
  39197. EVP_CIPHER_CTX_free(ctx);
  39198. res = TEST_RES_CHECK(1);
  39199. #endif
  39200. return res;
  39201. }
  39202. static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  39203. {
  39204. int res = TEST_SKIPPED;
  39205. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  39206. byte key[DES3_KEY_SIZE] = {0};
  39207. byte iv[DES_BLOCK_SIZE] = {0};
  39208. int ivLen, keyLen;
  39209. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  39210. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  39211. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39212. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39213. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  39214. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  39215. /* Bad cases */
  39216. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen), WOLFSSL_FAILURE);
  39217. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen), WOLFSSL_FAILURE);
  39218. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  39219. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  39220. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen), WOLFSSL_FAILURE);
  39221. /* Good case */
  39222. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  39223. EVP_CIPHER_CTX_free(ctx);
  39224. res = TEST_RES_CHECK(1);
  39225. #endif
  39226. return res;
  39227. }
  39228. static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  39229. {
  39230. WOLFSSL_ENGINE* e = NULL;
  39231. int id = 0;
  39232. EVP_PKEY_CTX *ctx;
  39233. AssertNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  39234. EVP_PKEY_CTX_free(ctx);
  39235. return TEST_RES_CHECK(1);
  39236. }
  39237. static int test_wolfSSL_EVP_rc4(void)
  39238. {
  39239. int res = TEST_SKIPPED;
  39240. #if !defined(NO_RC4)
  39241. res = TEST_RES_CHECK(wolfSSL_EVP_rc4() != NULL);
  39242. #endif
  39243. return res;
  39244. }
  39245. static int test_wolfSSL_EVP_enc_null(void)
  39246. {
  39247. return TEST_RES_CHECK(wolfSSL_EVP_enc_null() != NULL);
  39248. }
  39249. static int test_wolfSSL_EVP_rc2_cbc(void)
  39250. {
  39251. int res = TEST_SKIPPED;
  39252. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  39253. res = TEST_RES_CHECK(wolfSSL_EVP_rc2_cbc() == NULL);
  39254. #endif
  39255. return res;
  39256. }
  39257. static int test_wolfSSL_EVP_mdc2(void)
  39258. {
  39259. int res = TEST_SKIPPED;
  39260. #if !defined(NO_WOLFSSL_STUB)
  39261. res = TEST_RES_CHECK(wolfSSL_EVP_mdc2() == NULL);
  39262. #endif
  39263. return res;
  39264. }
  39265. static int test_wolfSSL_EVP_md4(void)
  39266. {
  39267. int res = TEST_SKIPPED;
  39268. #if !defined(NO_MD4)
  39269. res = TEST_RES_CHECK(wolfSSL_EVP_md4() != NULL);
  39270. #endif
  39271. return res;
  39272. }
  39273. static int test_wolfSSL_EVP_aes_256_gcm(void)
  39274. {
  39275. return TEST_RES_CHECK(wolfSSL_EVP_aes_256_gcm() != NULL);
  39276. }
  39277. static int test_wolfSSL_EVP_aes_192_gcm(void)
  39278. {
  39279. return TEST_RES_CHECK(wolfSSL_EVP_aes_192_gcm() != NULL);
  39280. }
  39281. static int test_wolfSSL_EVP_ripemd160(void)
  39282. {
  39283. int res = TEST_SKIPPED;
  39284. #if !defined(NO_WOLFSSL_STUB)
  39285. res = TEST_RES_CHECK(wolfSSL_EVP_ripemd160() == NULL);
  39286. #endif
  39287. return res;
  39288. }
  39289. static int test_wolfSSL_EVP_get_digestbynid(void)
  39290. {
  39291. #ifndef NO_MD5
  39292. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  39293. #endif
  39294. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  39295. AssertNull(wolfSSL_EVP_get_digestbynid(0));
  39296. return TEST_RES_CHECK(1);
  39297. }
  39298. static int test_wolfSSL_EVP_MD_nid(void)
  39299. {
  39300. #ifndef NO_MD5
  39301. AssertIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  39302. #endif
  39303. #ifndef NO_SHA
  39304. AssertIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  39305. #endif
  39306. #ifndef NO_SHA256
  39307. AssertIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  39308. #endif
  39309. AssertIntEQ(EVP_MD_nid(NULL), NID_undef);
  39310. return TEST_RES_CHECK(1);
  39311. }
  39312. static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  39313. {
  39314. int res = TEST_SKIPPED;
  39315. #if defined(HAVE_ECC)
  39316. WOLFSSL_EVP_PKEY* pkey;
  39317. AssertNotNull(pkey = EVP_PKEY_new());
  39318. AssertNull(EVP_PKEY_get0_EC_KEY(pkey));
  39319. EVP_PKEY_free(pkey);
  39320. res = TEST_RES_CHECK(1);
  39321. #endif
  39322. return res;
  39323. }
  39324. static int test_wolfSSL_EVP_X_STATE(void)
  39325. {
  39326. int res = TEST_SKIPPED;
  39327. #if !defined(NO_DES3) && !defined(NO_RC4)
  39328. byte key[DES3_KEY_SIZE] = {0};
  39329. byte iv[DES_IV_SIZE] = {0};
  39330. EVP_CIPHER_CTX *ctx;
  39331. const EVP_CIPHER *init;
  39332. /* Bad test cases */
  39333. ctx = EVP_CIPHER_CTX_new();
  39334. init = EVP_des_ede3_cbc();
  39335. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39336. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39337. AssertNull(wolfSSL_EVP_X_STATE(NULL));
  39338. AssertNull(wolfSSL_EVP_X_STATE(ctx));
  39339. EVP_CIPHER_CTX_free(ctx);
  39340. /* Good test case */
  39341. ctx = EVP_CIPHER_CTX_new();
  39342. init = wolfSSL_EVP_rc4();
  39343. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39344. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39345. AssertNotNull(wolfSSL_EVP_X_STATE(ctx));
  39346. EVP_CIPHER_CTX_free(ctx);
  39347. res = TEST_RES_CHECK(1);
  39348. #endif
  39349. return res;
  39350. }
  39351. static int test_wolfSSL_EVP_X_STATE_LEN(void)
  39352. {
  39353. int res = TEST_SKIPPED;
  39354. #if !defined(NO_DES3) && !defined(NO_RC4)
  39355. byte key[DES3_KEY_SIZE] = {0};
  39356. byte iv[DES_IV_SIZE] = {0};
  39357. EVP_CIPHER_CTX *ctx;
  39358. const EVP_CIPHER *init;
  39359. /* Bad test cases */
  39360. ctx = EVP_CIPHER_CTX_new();
  39361. init = EVP_des_ede3_cbc();
  39362. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39363. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39364. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  39365. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  39366. EVP_CIPHER_CTX_free(ctx);
  39367. /* Good test case */
  39368. ctx = EVP_CIPHER_CTX_new();
  39369. init = wolfSSL_EVP_rc4();
  39370. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39371. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39372. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  39373. EVP_CIPHER_CTX_free(ctx);
  39374. res = TEST_RES_CHECK(1);
  39375. #endif
  39376. return res;
  39377. }
  39378. static int test_wolfSSL_EVP_CIPHER_block_size(void)
  39379. {
  39380. int res = TEST_SKIPPED;
  39381. #ifdef HAVE_AES_CBC
  39382. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  39383. #ifdef WOLFSSL_AES_128
  39384. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  39385. #endif
  39386. #ifdef WOLFSSL_AES_192
  39387. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  39388. #endif
  39389. #ifdef WOLFSSL_AES_256
  39390. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  39391. #endif
  39392. res = TEST_RES_CHECK(1);
  39393. }
  39394. #endif
  39395. #ifdef HAVE_AESGCM
  39396. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  39397. #ifdef WOLFSSL_AES_128
  39398. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  39399. #endif
  39400. #ifdef WOLFSSL_AES_192
  39401. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  39402. #endif
  39403. #ifdef WOLFSSL_AES_256
  39404. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  39405. #endif
  39406. res = TEST_RES_CHECK(1);
  39407. }
  39408. #endif
  39409. #ifdef WOLFSSL_AES_COUNTER
  39410. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  39411. #ifdef WOLFSSL_AES_128
  39412. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  39413. #endif
  39414. #ifdef WOLFSSL_AES_192
  39415. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  39416. #endif
  39417. #ifdef WOLFSSL_AES_256
  39418. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  39419. #endif
  39420. res = TEST_RES_CHECK(1);
  39421. }
  39422. #endif
  39423. #ifdef HAVE_AES_ECB
  39424. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  39425. #ifdef WOLFSSL_AES_128
  39426. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  39427. #endif
  39428. #ifdef WOLFSSL_AES_192
  39429. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  39430. #endif
  39431. #ifdef WOLFSSL_AES_256
  39432. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  39433. #endif
  39434. res = TEST_RES_CHECK(1);
  39435. }
  39436. #endif
  39437. #ifdef WOLFSSL_AES_OFB
  39438. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  39439. #ifdef WOLFSSL_AES_128
  39440. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  39441. #endif
  39442. #ifdef WOLFSSL_AES_192
  39443. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  39444. #endif
  39445. #ifdef WOLFSSL_AES_256
  39446. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  39447. #endif
  39448. res = TEST_RES_CHECK(1);
  39449. }
  39450. #endif
  39451. #ifndef NO_RC4
  39452. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  39453. AssertIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  39454. res = TEST_RES_CHECK(1);
  39455. }
  39456. #endif
  39457. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  39458. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  39459. AssertIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
  39460. res = TEST_RES_CHECK(1);
  39461. }
  39462. #endif
  39463. return res;
  39464. }
  39465. static int test_wolfSSL_EVP_CIPHER_iv_length(void)
  39466. {
  39467. int i, enumlen;
  39468. int enumArray[] = {
  39469. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  39470. #ifdef WOLFSSL_AES_128
  39471. NID_aes_128_cbc,
  39472. #endif
  39473. #ifdef WOLFSSL_AES_192
  39474. NID_aes_192_cbc,
  39475. #endif
  39476. #ifdef WOLFSSL_AES_256
  39477. NID_aes_256_cbc,
  39478. #endif
  39479. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  39480. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  39481. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39482. #ifdef HAVE_AESGCM
  39483. #ifdef WOLFSSL_AES_128
  39484. NID_aes_128_gcm,
  39485. #endif
  39486. #ifdef WOLFSSL_AES_192
  39487. NID_aes_192_gcm,
  39488. #endif
  39489. #ifdef WOLFSSL_AES_256
  39490. NID_aes_256_gcm,
  39491. #endif
  39492. #endif /* HAVE_AESGCM */
  39493. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  39494. #ifdef WOLFSSL_AES_COUNTER
  39495. #ifdef WOLFSSL_AES_128
  39496. NID_aes_128_ctr,
  39497. #endif
  39498. #ifdef WOLFSSL_AES_192
  39499. NID_aes_192_ctr,
  39500. #endif
  39501. #ifdef WOLFSSL_AES_256
  39502. NID_aes_256_ctr,
  39503. #endif
  39504. #endif
  39505. #ifndef NO_DES3
  39506. NID_des_cbc,
  39507. NID_des_ede3_cbc,
  39508. #endif
  39509. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  39510. NID_chacha20_poly1305,
  39511. #endif
  39512. };
  39513. int iv_lengths[] = {
  39514. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  39515. #ifdef WOLFSSL_AES_128
  39516. AES_BLOCK_SIZE,
  39517. #endif
  39518. #ifdef WOLFSSL_AES_192
  39519. AES_BLOCK_SIZE,
  39520. #endif
  39521. #ifdef WOLFSSL_AES_256
  39522. AES_BLOCK_SIZE,
  39523. #endif
  39524. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  39525. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  39526. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39527. #ifdef HAVE_AESGCM
  39528. #ifdef WOLFSSL_AES_128
  39529. GCM_NONCE_MID_SZ,
  39530. #endif
  39531. #ifdef WOLFSSL_AES_192
  39532. GCM_NONCE_MID_SZ,
  39533. #endif
  39534. #ifdef WOLFSSL_AES_256
  39535. GCM_NONCE_MID_SZ,
  39536. #endif
  39537. #endif /* HAVE_AESGCM */
  39538. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  39539. #ifdef WOLFSSL_AES_COUNTER
  39540. #ifdef WOLFSSL_AES_128
  39541. AES_BLOCK_SIZE,
  39542. #endif
  39543. #ifdef WOLFSSL_AES_192
  39544. AES_BLOCK_SIZE,
  39545. #endif
  39546. #ifdef WOLFSSL_AES_256
  39547. AES_BLOCK_SIZE,
  39548. #endif
  39549. #endif
  39550. #ifndef NO_DES3
  39551. DES_BLOCK_SIZE,
  39552. DES_BLOCK_SIZE,
  39553. #endif
  39554. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  39555. CHACHA20_POLY1305_AEAD_IV_SIZE,
  39556. #endif
  39557. };
  39558. enumlen = (sizeof(enumArray)/sizeof(int));
  39559. for (i = 0; i < enumlen; i++) {
  39560. const EVP_CIPHER *c = EVP_get_cipherbynid(enumArray[i]);
  39561. AssertIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  39562. }
  39563. return TEST_RES_CHECK(1);
  39564. }
  39565. static int test_wolfSSL_EVP_SignInit_ex(void)
  39566. {
  39567. WOLFSSL_EVP_MD_CTX mdCtx;
  39568. WOLFSSL_ENGINE* e = 0;
  39569. const EVP_MD* md;
  39570. md = "SHA256";
  39571. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  39572. AssertIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  39573. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  39574. return TEST_RES_CHECK(1);
  39575. }
  39576. static int test_wolfSSL_EVP_DigestFinal_ex(void)
  39577. {
  39578. int res = TEST_SKIPPED;
  39579. #if !defined(NO_SHA256)
  39580. WOLFSSL_EVP_MD_CTX mdCtx;
  39581. unsigned int s = 0;
  39582. unsigned char md[WC_SHA256_DIGEST_SIZE];
  39583. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  39584. /* Bad Case */
  39585. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39586. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  39587. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  39588. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  39589. #else
  39590. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  39591. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  39592. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  39593. #endif
  39594. /* Good Case */
  39595. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  39596. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), WOLFSSL_SUCCESS);
  39597. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  39598. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  39599. res = TEST_RES_CHECK(1);
  39600. #endif
  39601. return res;
  39602. }
  39603. static int test_wolfSSL_EVP_PKEY_assign_DH(void)
  39604. {
  39605. int res = TEST_SKIPPED;
  39606. #if !defined(NO_DH) && \
  39607. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39608. FILE* f = NULL;
  39609. unsigned char buf[4096];
  39610. const unsigned char* pt = buf;
  39611. const char* params1 = "./certs/dh2048.der";
  39612. long len = 0;
  39613. WOLFSSL_DH* dh = NULL;
  39614. WOLFSSL_EVP_PKEY* pkey;
  39615. XMEMSET(buf, 0, sizeof(buf));
  39616. f = XFOPEN(params1, "rb");
  39617. AssertTrue(f != XBADFILE);
  39618. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  39619. XFCLOSE(f);
  39620. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  39621. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  39622. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39623. /* Bad cases */
  39624. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  39625. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  39626. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  39627. /* Good case */
  39628. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  39629. EVP_PKEY_free(pkey);
  39630. res = TEST_RES_CHECK(1);
  39631. #endif
  39632. return res;
  39633. }
  39634. static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  39635. {
  39636. int res = TEST_SKIPPED;
  39637. #if defined(OPENSSL_EXTRA)
  39638. EVP_PKEY* pkey;
  39639. EVP_PKEY_CTX* ctx;
  39640. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39641. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39642. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  39643. /* void */
  39644. EVP_PKEY_CTX_free(ctx);
  39645. AssertTrue(1);
  39646. #else
  39647. /* int */
  39648. AssertIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  39649. #endif
  39650. EVP_PKEY_free(pkey);
  39651. res = TEST_RES_CHECK(1);
  39652. #endif
  39653. return res;
  39654. }
  39655. static int test_wolfSSL_EVP_PKEY_param_check(void)
  39656. {
  39657. int res = TEST_SKIPPED;
  39658. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  39659. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  39660. DH *dh = NULL;
  39661. DH *setDh = NULL;
  39662. EVP_PKEY *pkey = NULL;
  39663. EVP_PKEY_CTX* ctx = NULL;
  39664. FILE* f = NULL;
  39665. unsigned char buf[512];
  39666. const unsigned char* pt = buf;
  39667. const char* dh2048 = "./certs/dh2048.der";
  39668. long len = 0;
  39669. int code = -1;
  39670. XMEMSET(buf, 0, sizeof(buf));
  39671. f = XFOPEN(dh2048, "rb");
  39672. AssertTrue(f != XBADFILE);
  39673. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  39674. XFCLOSE(f);
  39675. /* Load dh2048.der into DH with internal format */
  39676. AssertNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  39677. AssertIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  39678. AssertIntEQ(code, 0);
  39679. code = -1;
  39680. pkey = wolfSSL_EVP_PKEY_new();
  39681. /* Set DH into PKEY */
  39682. AssertIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  39683. /* create ctx from pkey */
  39684. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39685. AssertIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  39686. /* */
  39687. /* TO DO invlaid case */
  39688. /* */
  39689. EVP_PKEY_CTX_free(ctx);
  39690. EVP_PKEY_free(pkey);
  39691. DH_free(setDh);
  39692. DH_free(dh);
  39693. res = TEST_RES_CHECK(1);
  39694. #endif
  39695. #endif
  39696. return res;
  39697. }
  39698. static int test_wolfSSL_EVP_BytesToKey(void)
  39699. {
  39700. int res = TEST_SKIPPED;
  39701. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  39702. byte key[AES_BLOCK_SIZE] = {0};
  39703. byte iv[AES_BLOCK_SIZE] = {0};
  39704. int sz = 5;
  39705. int count = 0;
  39706. const EVP_MD* md = "SHA256";
  39707. const EVP_CIPHER *type;
  39708. const unsigned char *salt = (unsigned char *)"salt1234";
  39709. const byte data[] = {
  39710. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  39711. 0x72,0x6c,0x64
  39712. };
  39713. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  39714. /* Bad cases */
  39715. AssertIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  39716. 0);
  39717. AssertIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  39718. 16);
  39719. md = "2";
  39720. AssertIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  39721. WOLFSSL_FAILURE);
  39722. /* Good case */
  39723. md = "SHA256";
  39724. AssertIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  39725. 16);
  39726. res = TEST_RES_CHECK(1);
  39727. #endif
  39728. return res;
  39729. }
  39730. static int test_evp_cipher_aes_gcm(void)
  39731. {
  39732. int res = TEST_SKIPPED;
  39733. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  39734. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  39735. (HAVE_FIPS_VERSION >= 2)))
  39736. /*
  39737. * This test checks data at various points in the encrypt/decrypt process
  39738. * against known values produced using the same test with OpenSSL. This
  39739. * interop testing is critical for verifying the correctness of our
  39740. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  39741. * a flow supported by OpenSSL that uses the control command
  39742. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  39743. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  39744. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  39745. * wolfSSL OpenSSH clients because there was a bug in this flow that
  39746. * happened to "cancel out" if both sides of the connection had the bug.
  39747. */
  39748. enum {
  39749. NUM_ENCRYPTIONS = 3,
  39750. AAD_SIZE = 4
  39751. };
  39752. byte plainText1[] = {
  39753. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  39754. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  39755. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  39756. };
  39757. byte plainText2[] = {
  39758. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  39759. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  39760. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  39761. };
  39762. byte plainText3[] = {
  39763. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  39764. 0x5b, 0x57, 0x10
  39765. };
  39766. byte* plainTexts[NUM_ENCRYPTIONS] = {
  39767. plainText1,
  39768. plainText2,
  39769. plainText3
  39770. };
  39771. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  39772. sizeof(plainText1),
  39773. sizeof(plainText2),
  39774. sizeof(plainText3)
  39775. };
  39776. byte aad1[AAD_SIZE] = {
  39777. 0x00, 0x00, 0x00, 0x01
  39778. };
  39779. byte aad2[AAD_SIZE] = {
  39780. 0x00, 0x00, 0x00, 0x10
  39781. };
  39782. byte aad3[AAD_SIZE] = {
  39783. 0x00, 0x00, 0x01, 0x00
  39784. };
  39785. byte* aads[NUM_ENCRYPTIONS] = {
  39786. aad1,
  39787. aad2,
  39788. aad3
  39789. };
  39790. const byte iv[GCM_NONCE_MID_SZ] = {
  39791. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  39792. };
  39793. byte currentIv[GCM_NONCE_MID_SZ];
  39794. const byte key[] = {
  39795. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  39796. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  39797. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  39798. };
  39799. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  39800. {
  39801. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  39802. 0xEF
  39803. },
  39804. {
  39805. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  39806. 0xF0
  39807. },
  39808. {
  39809. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  39810. 0xF1
  39811. }
  39812. };
  39813. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  39814. {
  39815. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  39816. 0x3D, 0x32, 0x18, 0x34, 0xA9
  39817. },
  39818. {
  39819. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  39820. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  39821. },
  39822. {
  39823. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  39824. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  39825. }
  39826. };
  39827. const byte expCipherText1[] = {
  39828. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  39829. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  39830. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  39831. };
  39832. const byte expCipherText2[] = {
  39833. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  39834. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  39835. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  39836. };
  39837. const byte expCipherText3[] = {
  39838. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  39839. 0x80, 0x5E, 0x6B,
  39840. };
  39841. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  39842. expCipherText1,
  39843. expCipherText2,
  39844. expCipherText3
  39845. };
  39846. byte* cipherText;
  39847. byte* calcPlainText;
  39848. byte tag[AES_BLOCK_SIZE];
  39849. EVP_CIPHER_CTX* encCtx = NULL;
  39850. EVP_CIPHER_CTX* decCtx = NULL;
  39851. int i, j, outl;
  39852. /****************************************************/
  39853. for (i = 0; i < 3; ++i) {
  39854. AssertNotNull(encCtx = EVP_CIPHER_CTX_new());
  39855. AssertNotNull(decCtx = EVP_CIPHER_CTX_new());
  39856. /* First iteration, set key before IV. */
  39857. if (i == 0) {
  39858. AssertIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  39859. SSL_SUCCESS);
  39860. /*
  39861. * The call to EVP_CipherInit below (with NULL key) should clear the
  39862. * gcmIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  39863. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  39864. * behavior.
  39865. */
  39866. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  39867. (void*)iv), SSL_SUCCESS);
  39868. AssertIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  39869. SSL_SUCCESS);
  39870. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  39871. currentIv), SSL_FAILURE);
  39872. AssertIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  39873. SSL_SUCCESS);
  39874. AssertIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  39875. SSL_SUCCESS);
  39876. }
  39877. /* Second iteration, IV before key. */
  39878. else {
  39879. AssertIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  39880. SSL_SUCCESS);
  39881. AssertIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  39882. SSL_SUCCESS);
  39883. AssertIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  39884. SSL_SUCCESS);
  39885. AssertIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  39886. SSL_SUCCESS);
  39887. }
  39888. /*
  39889. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  39890. * been issued first.
  39891. */
  39892. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  39893. currentIv), SSL_FAILURE);
  39894. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  39895. (void*)iv), SSL_SUCCESS);
  39896. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  39897. (void*)iv), SSL_SUCCESS);
  39898. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  39899. /*************** Encrypt ***************/
  39900. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  39901. currentIv), SSL_SUCCESS);
  39902. /* Check current IV against expected. */
  39903. AssertIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  39904. /* Add AAD. */
  39905. if (i == 2) {
  39906. /* Test streaming API. */
  39907. AssertIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  39908. AAD_SIZE), SSL_SUCCESS);
  39909. }
  39910. else {
  39911. AssertIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  39912. AAD_SIZE);
  39913. }
  39914. AssertNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  39915. DYNAMIC_TYPE_TMP_BUFFER));
  39916. /* Encrypt plaintext. */
  39917. if (i == 2) {
  39918. AssertIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  39919. plainTexts[j], plainTextSzs[j]),
  39920. SSL_SUCCESS);
  39921. }
  39922. else {
  39923. AssertIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  39924. plainTextSzs[j]), plainTextSzs[j]);
  39925. }
  39926. if (i == 2) {
  39927. AssertIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  39928. SSL_SUCCESS);
  39929. }
  39930. else {
  39931. /*
  39932. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  39933. * akin to calling EVP_CipherFinal.
  39934. */
  39935. AssertIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  39936. }
  39937. /* Check ciphertext against expected. */
  39938. AssertIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  39939. 0);
  39940. /* Get and check tag against expected. */
  39941. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  39942. sizeof(tag), tag), SSL_SUCCESS);
  39943. AssertIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  39944. /*************** Decrypt ***************/
  39945. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  39946. currentIv), SSL_SUCCESS);
  39947. /* Check current IV against expected. */
  39948. AssertIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  39949. /* Add AAD. */
  39950. if (i == 2) {
  39951. /* Test streaming API. */
  39952. AssertIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  39953. AAD_SIZE), SSL_SUCCESS);
  39954. }
  39955. else {
  39956. AssertIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  39957. AAD_SIZE);
  39958. }
  39959. /* Set expected tag. */
  39960. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  39961. sizeof(tag), tag), SSL_SUCCESS);
  39962. /* Decrypt ciphertext. */
  39963. AssertNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  39964. DYNAMIC_TYPE_TMP_BUFFER));
  39965. if (i == 2) {
  39966. AssertIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  39967. cipherText, plainTextSzs[j]),
  39968. SSL_SUCCESS);
  39969. }
  39970. else {
  39971. /* This first EVP_Cipher call will check the tag, too. */
  39972. AssertIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  39973. plainTextSzs[j]), plainTextSzs[j]);
  39974. }
  39975. if (i == 2) {
  39976. AssertIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  39977. SSL_SUCCESS);
  39978. }
  39979. else {
  39980. AssertIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  39981. }
  39982. /* Check plaintext against expected. */
  39983. AssertIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  39984. 0);
  39985. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39986. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39987. }
  39988. EVP_CIPHER_CTX_free(encCtx);
  39989. EVP_CIPHER_CTX_free(decCtx);
  39990. }
  39991. res = TEST_RES_CHECK(1);
  39992. #endif
  39993. return res;
  39994. }
  39995. static int test_wolfSSL_OBJ_ln(void)
  39996. {
  39997. const int nid_set[] = {
  39998. NID_commonName,
  39999. NID_serialNumber,
  40000. NID_countryName,
  40001. NID_localityName,
  40002. NID_stateOrProvinceName,
  40003. NID_organizationName,
  40004. NID_organizationalUnitName,
  40005. NID_domainComponent,
  40006. NID_businessCategory,
  40007. NID_jurisdictionCountryName,
  40008. NID_jurisdictionStateOrProvinceName,
  40009. NID_emailAddress
  40010. };
  40011. const char* ln_set[] = {
  40012. "commonName",
  40013. "serialNumber",
  40014. "countryName",
  40015. "localityName",
  40016. "stateOrProvinceName",
  40017. "organizationName",
  40018. "organizationalUnitName",
  40019. "domainComponent",
  40020. "businessCategory",
  40021. "jurisdictionCountryName",
  40022. "jurisdictionStateOrProvinceName",
  40023. "emailAddress",
  40024. };
  40025. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  40026. AssertIntEQ(OBJ_ln2nid(NULL), NID_undef);
  40027. #ifdef HAVE_ECC
  40028. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  40029. {
  40030. EC_builtin_curve r[27];
  40031. size_t nCurves = sizeof(r) / sizeof(r[0]);
  40032. nCurves = EC_get_builtin_curves(r,nCurves);
  40033. for (i = 0; i < nCurves; i++) {
  40034. /* skip ECC_CURVE_INVALID */
  40035. if (r[i].nid != ECC_CURVE_INVALID) {
  40036. AssertIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  40037. AssertStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  40038. }
  40039. }
  40040. }
  40041. #endif
  40042. #endif
  40043. for (i = 0; i < maxIdx; i++) {
  40044. AssertIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  40045. AssertStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  40046. }
  40047. return TEST_RES_CHECK(1);
  40048. }
  40049. static int test_wolfSSL_OBJ_sn(void)
  40050. {
  40051. int i = 0, maxIdx = 7;
  40052. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  40053. NID_stateOrProvinceName,NID_organizationName,
  40054. NID_organizationalUnitName,NID_emailAddress};
  40055. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  40056. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  40057. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  40058. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  40059. WOLFSSL_EMAIL_ADDR};
  40060. AssertIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  40061. for (i = 0; i < maxIdx; i++) {
  40062. AssertIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  40063. AssertStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  40064. }
  40065. return TEST_RES_CHECK(1);
  40066. }
  40067. #if !defined(NO_BIO)
  40068. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  40069. {
  40070. return lh_strhash(s[3]);
  40071. }
  40072. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  40073. {
  40074. return XSTRCMP(a[3], b[3]);
  40075. }
  40076. #endif
  40077. static int test_wolfSSL_TXT_DB(void)
  40078. {
  40079. int res = TEST_SKIPPED;
  40080. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  40081. BIO *bio;
  40082. TXT_DB *db = NULL;
  40083. const int columns = 6;
  40084. const char *fields[6] = {
  40085. "V",
  40086. "320926161116Z",
  40087. "",
  40088. "12BD",
  40089. "unknown",
  40090. "/CN=rsa doe",
  40091. };
  40092. char** fields_copy;
  40093. /* Test read */
  40094. AssertNotNull(bio = BIO_new(BIO_s_file()));
  40095. AssertIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  40096. AssertNotNull(db = TXT_DB_read(bio, columns));
  40097. AssertNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  40098. DYNAMIC_TYPE_OPENSSL));
  40099. XMEMCPY(fields_copy, fields, sizeof(fields));
  40100. AssertIntEQ(TXT_DB_insert(db, fields_copy), 1);
  40101. BIO_free(bio);
  40102. /* Test write */
  40103. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  40104. AssertIntEQ(TXT_DB_write(bio, db), 1484);
  40105. BIO_free(bio);
  40106. /* Test index */
  40107. AssertIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  40108. (wolf_lh_compare_cb)TXT_DB_cmp), 1);
  40109. AssertNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  40110. fields[3] = "12DA";
  40111. AssertNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  40112. fields[3] = "FFFF";
  40113. AssertNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  40114. fields[3] = "";
  40115. AssertNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  40116. TXT_DB_free(db);
  40117. res = TEST_RES_CHECK(1);
  40118. #endif
  40119. return res;
  40120. }
  40121. static int test_wolfSSL_NCONF(void)
  40122. {
  40123. int res = TEST_SKIPPED;
  40124. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  40125. const char* confFile = "./tests/NCONF_test.cnf";
  40126. CONF* conf = NULL;
  40127. long eline = 0;
  40128. long num = 0;
  40129. AssertNotNull(conf = NCONF_new(NULL));
  40130. AssertIntEQ(NCONF_load(conf, confFile, &eline), 1);
  40131. AssertIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  40132. AssertIntEQ(num, 1234);
  40133. AssertIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  40134. AssertIntEQ(num, 4321);
  40135. AssertStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  40136. AssertStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  40137. "./test-dir/file1");
  40138. AssertStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  40139. "./test-dir/file1:./test-dir/file2:./section1:file2");
  40140. NCONF_free(conf);
  40141. res = TEST_RES_CHECK(1);
  40142. #endif
  40143. return res;
  40144. }
  40145. #endif /* OPENSSL_ALL */
  40146. static int test_wolfSSL_EC_KEY_set_group(void)
  40147. {
  40148. int res = TEST_SKIPPED;
  40149. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  40150. defined(OPENSSL_EXTRA)
  40151. EC_KEY *key = NULL;
  40152. EC_GROUP *group = NULL;
  40153. const EC_GROUP *group2 = NULL;
  40154. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  40155. AssertNotNull(key = EC_KEY_new());
  40156. AssertIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  40157. AssertNotNull(group2 = EC_KEY_get0_group(key));
  40158. AssertIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  40159. EC_GROUP_free(group);
  40160. EC_KEY_free(key);
  40161. res = TEST_RES_CHECK(1);
  40162. #endif
  40163. return res;
  40164. }
  40165. static int test_wolfSSL_EC_KEY_set_conv_form(void)
  40166. {
  40167. int res = TEST_SKIPPED;
  40168. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  40169. BIO* bio;
  40170. EC_KEY* key;
  40171. /* Error condition: NULL key. */
  40172. AssertIntLT(EC_KEY_get_conv_form(NULL), 0);
  40173. AssertNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  40174. AssertNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  40175. /* Conversion form defaults to uncompressed. */
  40176. AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  40177. #ifdef HAVE_COMP_KEY
  40178. /* Explicitly set to compressed. */
  40179. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  40180. AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  40181. #endif
  40182. BIO_free(bio);
  40183. EC_KEY_free(key);
  40184. res = TEST_RES_CHECK(1);
  40185. #endif
  40186. return res;
  40187. }
  40188. static int test_wolfSSL_EC_KEY_print_fp(void)
  40189. {
  40190. int res = TEST_SKIPPED;
  40191. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  40192. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224 && \
  40193. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  40194. !defined(NO_STDIO_FILESYSTEM)
  40195. EC_KEY* key = NULL;
  40196. /* Bad file pointer. */
  40197. AssertIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  40198. /* NULL key. */
  40199. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, NULL, 0), WOLFSSL_FAILURE);
  40200. AssertNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  40201. /* Negative indent. */
  40202. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, -1), WOLFSSL_FAILURE);
  40203. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  40204. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  40205. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  40206. wolfSSL_EC_KEY_free(key);
  40207. AssertNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  40208. NID_X9_62_prime256v1)));
  40209. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  40210. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  40211. wolfSSL_EC_KEY_free(key);
  40212. res = TEST_RES_CHECK(1);
  40213. #endif
  40214. return res;
  40215. }
  40216. static int test_wolfSSL_X509V3_EXT_get(void) {
  40217. int res = TEST_SKIPPED;
  40218. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40219. FILE* f;
  40220. int numOfExt =0;
  40221. int extNid = 0;
  40222. int i = 0;
  40223. WOLFSSL_X509* x509;
  40224. WOLFSSL_X509_EXTENSION* ext;
  40225. const WOLFSSL_v3_ext_method* method;
  40226. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  40227. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40228. fclose(f);
  40229. /* wolfSSL_X509V3_EXT_get() return struct and nid test */
  40230. AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  40231. for (i = 0; i < numOfExt; i++) {
  40232. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40233. AssertIntNE((extNid = ext->obj->nid), NID_undef);
  40234. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  40235. AssertIntEQ(method->ext_nid, extNid);
  40236. }
  40237. /* wolfSSL_X509V3_EXT_get() NULL argument test */
  40238. AssertNull(method = wolfSSL_X509V3_EXT_get(NULL));
  40239. wolfSSL_X509_free(x509);
  40240. res = TEST_RES_CHECK(1);
  40241. #endif
  40242. return res;
  40243. }
  40244. static int test_wolfSSL_X509V3_EXT_nconf(void)
  40245. {
  40246. int res = TEST_SKIPPED;
  40247. #ifdef OPENSSL_ALL
  40248. const char *ext_names[] = {
  40249. "subjectKeyIdentifier",
  40250. "authorityKeyIdentifier",
  40251. "subjectAltName",
  40252. "keyUsage",
  40253. };
  40254. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  40255. int ext_nids[] = {
  40256. NID_subject_key_identifier,
  40257. NID_authority_key_identifier,
  40258. NID_subject_alt_name,
  40259. NID_key_usage,
  40260. };
  40261. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  40262. const char *ext_values[] = {
  40263. "hash",
  40264. "hash",
  40265. "DNS:example.com, IP:127.0.0.1",
  40266. "digitalSignature,keyEncipherment,dataEncipherment",
  40267. };
  40268. size_t i;
  40269. X509_EXTENSION* ext;
  40270. X509* x509 = X509_new();
  40271. for (i = 0; i < ext_names_count; i++) {
  40272. ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i], ext_values[i]);
  40273. AssertNotNull(ext);
  40274. X509_EXTENSION_free(ext);
  40275. }
  40276. for (i = 0; i < ext_nids_count; i++) {
  40277. ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i], ext_values[i]);
  40278. AssertNotNull(ext);
  40279. X509_EXTENSION_free(ext);
  40280. }
  40281. /* Test adding extension to X509 */
  40282. for (i = 0; i < ext_nids_count; i++) {
  40283. ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i], ext_values[i]);
  40284. AssertIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  40285. X509_EXTENSION_free(ext);
  40286. }
  40287. X509_free(x509);
  40288. res = TEST_RES_CHECK(1);
  40289. #endif
  40290. return res;
  40291. }
  40292. static int test_wolfSSL_X509V3_EXT(void) {
  40293. int res = TEST_SKIPPED;
  40294. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40295. FILE* f;
  40296. int numOfExt = 0, nid = 0, i = 0, expected, actual;
  40297. char* str;
  40298. unsigned char* data;
  40299. const WOLFSSL_v3_ext_method* method;
  40300. WOLFSSL_X509* x509;
  40301. WOLFSSL_X509_EXTENSION* ext;
  40302. WOLFSSL_X509_EXTENSION* ext2;
  40303. WOLFSSL_ASN1_OBJECT *obj, *adObj;
  40304. WOLFSSL_ASN1_STRING* asn1str;
  40305. WOLFSSL_AUTHORITY_KEYID* aKeyId;
  40306. WOLFSSL_AUTHORITY_INFO_ACCESS* aia;
  40307. WOLFSSL_BASIC_CONSTRAINTS* bc;
  40308. WOLFSSL_ACCESS_DESCRIPTION* ad;
  40309. WOLFSSL_GENERAL_NAME* gn;
  40310. /* Check NULL argument */
  40311. AssertNull(wolfSSL_X509V3_EXT_d2i(NULL));
  40312. /* Using OCSP cert with X509V3 extensions */
  40313. AssertNotNull(f = fopen("./certs/ocsp/root-ca-cert.pem", "rb"));
  40314. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40315. fclose(f);
  40316. AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  40317. /* Basic Constraints */
  40318. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40319. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40320. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  40321. AssertNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  40322. AssertIntEQ(bc->ca, 1);
  40323. AssertNull(bc->pathlen);
  40324. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  40325. i++;
  40326. /* Subject Key Identifier */
  40327. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40328. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40329. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  40330. AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  40331. AssertNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  40332. asn1str));
  40333. X509_EXTENSION_free(ext2);
  40334. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  40335. AssertNotNull(method->i2s);
  40336. AssertNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  40337. wolfSSL_ASN1_STRING_free(asn1str);
  40338. actual = strcmp(str,
  40339. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  40340. AssertIntEQ(actual, 0);
  40341. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40342. i++;
  40343. /* Authority Key Identifier */
  40344. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40345. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40346. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  40347. AssertNotNull(aKeyId =
  40348. (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(ext));
  40349. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  40350. AssertNotNull(asn1str = aKeyId->keyid);
  40351. AssertNotNull(str =
  40352. wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method, asn1str));
  40353. actual = strcmp(str,
  40354. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  40355. AssertIntEQ(actual, 0);
  40356. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40357. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  40358. i++;
  40359. /* Key Usage */
  40360. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40361. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40362. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  40363. AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  40364. #if defined(WOLFSSL_QT)
  40365. AssertNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  40366. #else
  40367. AssertNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  40368. #endif
  40369. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  40370. #ifdef BIG_ENDIAN_ORDER
  40371. actual = data[1];
  40372. #else
  40373. actual = data[0];
  40374. #endif
  40375. AssertIntEQ(actual, expected);
  40376. wolfSSL_ASN1_STRING_free(asn1str);
  40377. #if 1
  40378. i++;
  40379. /* Authority Info Access */
  40380. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40381. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40382. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  40383. AssertNotNull(aia =
  40384. (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext));
  40385. #if defined(WOLFSSL_QT)
  40386. AssertIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  40387. #else
  40388. AssertIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  40389. #endif
  40390. /* URI entry is an ACCESS_DESCRIPTION type */
  40391. #if defined(WOLFSSL_QT)
  40392. AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  40393. #else
  40394. AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  40395. #endif
  40396. AssertNotNull(adObj = ad->method);
  40397. /* Make sure nid is OCSP */
  40398. AssertIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  40399. /* GENERAL_NAME stores URI as an ASN1_STRING */
  40400. AssertNotNull(gn = ad->location);
  40401. AssertIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  40402. AssertNotNull(asn1str = gn->d.uniformResourceIdentifier);
  40403. AssertIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  40404. #if defined(WOLFSSL_QT)
  40405. str = (char*)ASN1_STRING_get0_data(asn1str);
  40406. #else
  40407. str = (char*)wolfSSL_ASN1_STRING_data(asn1str);
  40408. #endif
  40409. actual = strcmp(str, "http://127.0.0.1:22220");
  40410. AssertIntEQ(actual, 0);
  40411. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  40412. #else
  40413. (void) aia; (void) ad; (void) adObj; (void) gn;
  40414. #endif
  40415. wolfSSL_X509_free(x509);
  40416. res = TEST_RES_CHECK(1);
  40417. #endif
  40418. return res;
  40419. }
  40420. static int test_wolfSSL_X509_get_extension_flags(void)
  40421. {
  40422. int res = TEST_SKIPPED;
  40423. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40424. XFILE f;
  40425. X509* x509;
  40426. unsigned int extFlags;
  40427. unsigned int keyUsageFlags;
  40428. unsigned int extKeyUsageFlags;
  40429. /* client-int-cert.pem has the following extension flags. */
  40430. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  40431. /* and the following key usage flags. */
  40432. keyUsageFlags = KU_DIGITAL_SIGNATURE
  40433. | KU_NON_REPUDIATION
  40434. | KU_KEY_ENCIPHERMENT;
  40435. /* and the following extended key usage flags. */
  40436. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  40437. f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb");
  40438. AssertTrue(f != XBADFILE);
  40439. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  40440. XFCLOSE(f);
  40441. AssertIntEQ(X509_get_extension_flags(x509), extFlags);
  40442. AssertIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  40443. AssertIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  40444. X509_free(x509);
  40445. /* client-cert-ext.pem has the following extension flags. */
  40446. extFlags = EXFLAG_KUSAGE;
  40447. /* and the following key usage flags. */
  40448. keyUsageFlags = KU_DIGITAL_SIGNATURE
  40449. | KU_KEY_CERT_SIGN
  40450. | KU_CRL_SIGN;
  40451. AssertNotNull(f = fopen("./certs/client-cert-ext.pem", "rb"));
  40452. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  40453. XFCLOSE(f);
  40454. AssertIntEQ(X509_get_extension_flags(x509), extFlags);
  40455. AssertIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  40456. X509_free(x509);
  40457. res = TEST_RES_CHECK(1);
  40458. #endif /* OPENSSL_ALL */
  40459. return res;
  40460. }
  40461. static int test_wolfSSL_X509_get_ext(void)
  40462. {
  40463. int res = TEST_SKIPPED;
  40464. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40465. int ret = 0;
  40466. FILE* f;
  40467. WOLFSSL_X509* x509;
  40468. WOLFSSL_X509_EXTENSION* foundExtension;
  40469. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  40470. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40471. fclose(f);
  40472. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  40473. /* wolfSSL_X509_get_ext() valid input */
  40474. AssertNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  40475. /* wolfSSL_X509_get_ext() valid x509, idx out of bounds */
  40476. AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  40477. AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  40478. /* wolfSSL_X509_get_ext() NULL x509, idx out of bounds */
  40479. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  40480. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  40481. /* wolfSSL_X509_get_ext() NULL x509, valid idx */
  40482. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  40483. wolfSSL_X509_free(x509);
  40484. res = TEST_RES_CHECK(1);
  40485. #endif
  40486. return res;
  40487. }
  40488. static int test_wolfSSL_X509_get_ext_by_NID(void)
  40489. {
  40490. int res = TEST_SKIPPED;
  40491. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40492. int rc;
  40493. FILE* f;
  40494. WOLFSSL_X509* x509;
  40495. ASN1_OBJECT* obj = NULL;
  40496. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  40497. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40498. fclose(f);
  40499. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
  40500. AssertIntGE(rc, 0);
  40501. /* Start search from last location (should fail) */
  40502. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, rc);
  40503. AssertIntGE(rc, -1);
  40504. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -2);
  40505. AssertIntGE(rc, -1);
  40506. rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints, -1);
  40507. AssertIntEQ(rc, -1);
  40508. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1);
  40509. AssertIntEQ(rc, -1);
  40510. /* NID_ext_key_usage, check also its nid and oid */
  40511. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1);
  40512. AssertIntGT(rc, -1);
  40513. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(x509, rc)));
  40514. AssertIntEQ(obj->nid, NID_ext_key_usage);
  40515. AssertIntEQ(obj->type, EXT_KEY_USAGE_OID);
  40516. wolfSSL_X509_free(x509);
  40517. res = TEST_RES_CHECK(1);
  40518. #endif
  40519. return res;
  40520. }
  40521. static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
  40522. {
  40523. int res = TEST_SKIPPED;
  40524. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40525. int rc;
  40526. XFILE f;
  40527. WOLFSSL_X509* x509;
  40528. WOLFSSL_X509_EXTENSION* ext;
  40529. WOLFSSL_ASN1_STRING* sanString;
  40530. byte* sanDer;
  40531. const byte expectedDer[] = {
  40532. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  40533. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  40534. f = XFOPEN("./certs/server-cert.pem", "rb");
  40535. AssertTrue(f != XBADFILE);
  40536. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  40537. fclose(f);
  40538. rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1);
  40539. AssertIntNE(rc, -1);
  40540. AssertNotNull(ext = X509_get_ext(x509, rc));
  40541. AssertNotNull(sanString = X509_EXTENSION_get_data(ext));
  40542. AssertIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  40543. AssertNotNull(sanDer = ASN1_STRING_data(sanString));
  40544. AssertIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  40545. X509_free(x509);
  40546. res = TEST_RES_CHECK(1);
  40547. #endif
  40548. return res;
  40549. }
  40550. static int test_wolfSSL_X509_EXTENSION_new(void)
  40551. {
  40552. int res = TEST_SKIPPED;
  40553. #if defined (OPENSSL_ALL)
  40554. WOLFSSL_X509_EXTENSION* ext;
  40555. AssertNotNull(ext = wolfSSL_X509_EXTENSION_new());
  40556. AssertNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  40557. ext->obj->nid = WOLFSSL_SUCCESS;
  40558. AssertIntEQ(WOLFSSL_SUCCESS, ext->obj->nid);
  40559. wolfSSL_X509_EXTENSION_free(ext);
  40560. res = TEST_RES_CHECK(1);
  40561. #endif
  40562. return res;
  40563. }
  40564. static int test_wolfSSL_X509_EXTENSION_get_object(void)
  40565. {
  40566. int res = TEST_SKIPPED;
  40567. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40568. WOLFSSL_X509* x509;
  40569. WOLFSSL_X509_EXTENSION* ext;
  40570. WOLFSSL_ASN1_OBJECT* o;
  40571. FILE* file;
  40572. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  40573. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  40574. fclose(file);
  40575. /* wolfSSL_X509_EXTENSION_get_object() testing ext idx 0 */
  40576. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  40577. AssertNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  40578. AssertIntEQ(o->nid, 128);
  40579. /* wolfSSL_X509_EXTENSION_get_object() NULL argument */
  40580. AssertNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  40581. wolfSSL_X509_free(x509);
  40582. res = TEST_RES_CHECK(1);
  40583. #endif
  40584. return res;
  40585. }
  40586. static int test_wolfSSL_X509_EXTENSION_get_data(void)
  40587. {
  40588. int res = TEST_SKIPPED;
  40589. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40590. WOLFSSL_X509* x509;
  40591. WOLFSSL_X509_EXTENSION* ext;
  40592. WOLFSSL_ASN1_STRING* str;
  40593. FILE* file;
  40594. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  40595. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  40596. fclose(file);
  40597. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  40598. AssertNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  40599. wolfSSL_X509_free(x509);
  40600. res = TEST_RES_CHECK(1);
  40601. #endif
  40602. return res;
  40603. }
  40604. static int test_wolfSSL_X509_EXTENSION_get_critical(void)
  40605. {
  40606. int res = TEST_SKIPPED;
  40607. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40608. WOLFSSL_X509* x509;
  40609. WOLFSSL_X509_EXTENSION* ext;
  40610. FILE* file;
  40611. int crit;
  40612. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  40613. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  40614. fclose(file);
  40615. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  40616. crit = wolfSSL_X509_EXTENSION_get_critical(ext);
  40617. AssertIntEQ(crit, 0);
  40618. wolfSSL_X509_free(x509);
  40619. res = TEST_RES_CHECK(1);
  40620. #endif
  40621. return res;
  40622. }
  40623. static int test_wolfSSL_X509V3_EXT_print(void)
  40624. {
  40625. int res = TEST_SKIPPED;
  40626. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  40627. !defined(NO_RSA)
  40628. {
  40629. FILE* f;
  40630. WOLFSSL_X509* x509;
  40631. X509_EXTENSION * ext = NULL;
  40632. int loc;
  40633. BIO *bio = NULL;
  40634. AssertNotNull(f = fopen(svrCertFile, "rb"));
  40635. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40636. fclose(f);
  40637. AssertNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  40638. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
  40639. AssertIntGT(loc, -1);
  40640. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  40641. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  40642. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_subject_key_identifier, -1);
  40643. AssertIntGT(loc, -1);
  40644. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  40645. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  40646. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_authority_key_identifier, -1);
  40647. AssertIntGT(loc, -1);
  40648. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  40649. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  40650. wolfSSL_BIO_free(bio);
  40651. wolfSSL_X509_free(x509);
  40652. }
  40653. {
  40654. X509 *x509;
  40655. BIO *bio;
  40656. X509_EXTENSION *ext;
  40657. unsigned int i;
  40658. unsigned int idx;
  40659. /* Some NIDs to test with */
  40660. int nids[] = {
  40661. /* NID_key_usage, currently X509_get_ext returns this as a bit
  40662. * string, which messes up X509V3_EXT_print */
  40663. /* NID_ext_key_usage, */
  40664. NID_subject_alt_name,
  40665. };
  40666. int* n;
  40667. AssertNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  40668. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  40669. WOLFSSL_FILETYPE_PEM));
  40670. fprintf(stderr, "\nPrinting extension values:\n");
  40671. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  40672. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  40673. * update the index */
  40674. AssertIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  40675. AssertNotNull(ext = X509_get_ext(x509, idx));
  40676. AssertIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  40677. fprintf(stderr, "\n");
  40678. }
  40679. BIO_free(bio);
  40680. X509_free(x509);
  40681. }
  40682. res = TEST_RES_CHECK(1);
  40683. #endif
  40684. return res;
  40685. }
  40686. static int test_wolfSSL_X509_cmp(void)
  40687. {
  40688. int res = TEST_SKIPPED;
  40689. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40690. FILE* file1;
  40691. FILE* file2;
  40692. WOLFSSL_X509* cert1;
  40693. WOLFSSL_X509* cert2;
  40694. AssertNotNull(file1=fopen("./certs/server-cert.pem", "rb"));
  40695. AssertNotNull(file2=fopen("./certs/3072/client-cert.pem", "rb"));
  40696. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  40697. AssertNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  40698. fclose(file1);
  40699. fclose(file2);
  40700. /* wolfSSL_X509_cmp() testing matching certs */
  40701. AssertIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  40702. /* wolfSSL_X509_cmp() testing mismatched certs */
  40703. AssertIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  40704. /* wolfSSL_X509_cmp() testing NULL, valid args */
  40705. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  40706. /* wolfSSL_X509_cmp() testing valid, NULL args */
  40707. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  40708. /* wolfSSL_X509_cmp() testing NULL, NULL args */
  40709. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  40710. wolfSSL_X509_free(cert1);
  40711. wolfSSL_X509_free(cert2);
  40712. res = TEST_RES_CHECK(1);
  40713. #endif
  40714. return res;
  40715. }
  40716. static int test_wolfSSL_PKEY_up_ref(void)
  40717. {
  40718. int res = TEST_SKIPPED;
  40719. #if defined(OPENSSL_ALL)
  40720. EVP_PKEY* pkey;
  40721. pkey = EVP_PKEY_new();
  40722. AssertIntEQ(EVP_PKEY_up_ref(NULL), 0);
  40723. AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
  40724. EVP_PKEY_free(pkey);
  40725. AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
  40726. EVP_PKEY_free(pkey);
  40727. EVP_PKEY_free(pkey);
  40728. res = TEST_RES_CHECK(1);
  40729. #endif
  40730. return res;
  40731. }
  40732. static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
  40733. {
  40734. int res = TEST_SKIPPED;
  40735. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  40736. EVP_PKEY* pkey;
  40737. const unsigned char* p;
  40738. unsigned char* der = NULL;
  40739. int derLen;
  40740. p = client_keypub_der_2048;
  40741. /* Check that key can be successfully decoded. */
  40742. AssertNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  40743. sizeof_client_keypub_der_2048));
  40744. /* Check that key can be successfully encoded. */
  40745. AssertIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  40746. /* Ensure that the encoded version matches the original. */
  40747. AssertIntEQ(derLen, sizeof_client_keypub_der_2048);
  40748. AssertIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  40749. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  40750. EVP_PKEY_free(pkey);
  40751. res = TEST_RES_CHECK(1);
  40752. #endif
  40753. return res;
  40754. }
  40755. static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
  40756. {
  40757. int res = TEST_SKIPPED;
  40758. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  40759. DSA* dsa;
  40760. char file[] = "./certs/dsaparams.der";
  40761. XFILE f;
  40762. int derInLen;
  40763. byte* derIn;
  40764. int derOutLen;
  40765. byte* derOut = NULL;
  40766. f = XFOPEN(file, "rb");
  40767. AssertTrue(f != XBADFILE);
  40768. AssertTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  40769. derInLen = (int)XFTELL(f);
  40770. XREWIND(f);
  40771. AssertNotNull(derIn = (byte*)XMALLOC(derInLen, HEAP_HINT,
  40772. DYNAMIC_TYPE_TMP_BUFFER));
  40773. AssertIntEQ(XFREAD(derIn, 1, derInLen, f), derInLen);
  40774. XFCLOSE(f);
  40775. /* Check that params can be successfully decoded. */
  40776. AssertNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&derIn, derInLen));
  40777. /* Check that params can be successfully encoded. */
  40778. AssertIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  40779. /* Ensure that the encoded version matches the original. */
  40780. AssertIntEQ(derInLen, derOutLen);
  40781. AssertIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  40782. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40783. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  40784. DSA_free(dsa);
  40785. res = TEST_RES_CHECK(1);
  40786. #endif
  40787. return res;
  40788. }
  40789. static int test_wolfSSL_i2d_PrivateKey(void)
  40790. {
  40791. int res = TEST_SKIPPED;
  40792. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_PWDBASED)
  40793. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  40794. {
  40795. EVP_PKEY* pkey;
  40796. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  40797. unsigned char buf[FOURK_BUF];
  40798. unsigned char* pt = NULL;
  40799. int bufSz;
  40800. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  40801. (long)sizeof_server_key_der_2048));
  40802. AssertIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  40803. pt = buf;
  40804. AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  40805. AssertIntNE((pt - buf), 0);
  40806. AssertIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  40807. EVP_PKEY_free(pkey);
  40808. }
  40809. #endif
  40810. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  40811. {
  40812. EVP_PKEY* pkey;
  40813. const unsigned char* client_key =
  40814. (const unsigned char*)ecc_clikey_der_256;
  40815. unsigned char buf[FOURK_BUF];
  40816. unsigned char* pt = NULL;
  40817. int bufSz;
  40818. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  40819. sizeof_ecc_clikey_der_256)));
  40820. AssertIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  40821. pt = buf;
  40822. AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  40823. AssertIntNE((pt - buf), 0);
  40824. AssertIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  40825. EVP_PKEY_free(pkey);
  40826. }
  40827. #endif
  40828. res = TEST_RES_CHECK(1);
  40829. #endif
  40830. return res;
  40831. }
  40832. static int test_wolfSSL_OCSP_id_get0_info(void)
  40833. {
  40834. int res = TEST_SKIPPED;
  40835. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP) && \
  40836. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  40837. X509* cert;
  40838. X509* issuer;
  40839. OCSP_CERTID* id;
  40840. OCSP_CERTID* id2;
  40841. ASN1_STRING* name = NULL;
  40842. ASN1_OBJECT* pmd = NULL;
  40843. ASN1_STRING* keyHash = NULL;
  40844. ASN1_INTEGER* serial = NULL;
  40845. ASN1_INTEGER* x509Int;
  40846. AssertNotNull(cert =
  40847. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM));
  40848. AssertNotNull(issuer =
  40849. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
  40850. id = OCSP_cert_to_id(NULL, cert, issuer);
  40851. AssertNotNull(id);
  40852. id2 = OCSP_cert_to_id(NULL, cert, issuer);
  40853. AssertNotNull(id2);
  40854. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  40855. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  40856. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  40857. AssertIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  40858. AssertIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  40859. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  40860. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  40861. AssertNotNull(serial);
  40862. /* compare serial number to one in cert, should be equal */
  40863. x509Int = X509_get_serialNumber(cert);
  40864. AssertNotNull(x509Int);
  40865. AssertIntEQ(x509Int->length, serial->length);
  40866. AssertIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  40867. /* test OCSP_id_cmp */
  40868. AssertIntNE(OCSP_id_cmp(NULL, NULL), 0);
  40869. AssertIntNE(OCSP_id_cmp(id, NULL), 0);
  40870. AssertIntNE(OCSP_id_cmp(NULL, id2), 0);
  40871. AssertIntEQ(OCSP_id_cmp(id, id2), 0);
  40872. id->issuerHash[0] = ~id->issuerHash[0];
  40873. AssertIntNE(OCSP_id_cmp(id, id2), 0);
  40874. OCSP_CERTID_free(id);
  40875. OCSP_CERTID_free(id2);
  40876. X509_free(cert); /* free's x509Int */
  40877. X509_free(issuer);
  40878. res = TEST_RES_CHECK(1);
  40879. #endif
  40880. return res;
  40881. }
  40882. static int test_wolfSSL_i2d_OCSP_CERTID(void)
  40883. {
  40884. int res = TEST_SKIPPED;
  40885. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  40886. WOLFSSL_OCSP_CERTID certId;
  40887. byte* targetBuffer;
  40888. byte* beginTargetBuffer;
  40889. /* OCSP CertID bytes taken from PCAP */
  40890. byte rawCertId[] = {
  40891. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  40892. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  40893. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  40894. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  40895. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  40896. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  40897. 0xcf, 0xbc, 0x91
  40898. };
  40899. int ret, i;
  40900. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  40901. certId.rawCertId = rawCertId;
  40902. certId.rawCertIdSize = sizeof(rawCertId);
  40903. targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40904. beginTargetBuffer = targetBuffer;
  40905. ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer);
  40906. /* If target buffer is not null, function increments targetBuffer to point
  40907. just past the end of the encoded data. */
  40908. AssertPtrEq(targetBuffer, (beginTargetBuffer + sizeof(rawCertId)));
  40909. /* Function returns the size of the encoded data. */
  40910. AssertIntEQ(ret, sizeof(rawCertId));
  40911. for (i = 0; i < ret; ++i)
  40912. {
  40913. AssertIntEQ(beginTargetBuffer[i], rawCertId[i]);
  40914. }
  40915. XFREE(beginTargetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40916. targetBuffer = NULL;
  40917. ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer);
  40918. /* If target buffer is null, function allocates memory for a buffer and
  40919. copies the encoded data into it. targetBuffer then points to the start of
  40920. this newly allocate buffer. */
  40921. AssertIntEQ(ret, sizeof(rawCertId));
  40922. for (i = 0; i < ret; ++i)
  40923. {
  40924. AssertIntEQ(targetBuffer[i], rawCertId[i]);
  40925. }
  40926. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  40927. res = TEST_RES_CHECK(1);
  40928. #endif
  40929. return res;
  40930. }
  40931. static int test_wolfSSL_d2i_OCSP_CERTID(void)
  40932. {
  40933. int res = TEST_SKIPPED;
  40934. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  40935. WOLFSSL_OCSP_CERTID* certId;
  40936. WOLFSSL_OCSP_CERTID* certIdBad;
  40937. const unsigned char* rawCertIdPtr;
  40938. const unsigned char rawCertId[] = {
  40939. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  40940. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  40941. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  40942. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  40943. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  40944. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  40945. 0xcf, 0xbc, 0x91
  40946. };
  40947. rawCertIdPtr = &rawCertId[0];
  40948. /* If the cert ID is NULL the function should allocate it and copy the
  40949. * data to it. */
  40950. certId = NULL;
  40951. certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, sizeof(rawCertId));
  40952. AssertNotNull(certId);
  40953. AssertIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  40954. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  40955. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  40956. /* If the cert ID is not NULL the function will just copy the data to it. */
  40957. certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
  40958. DYNAMIC_TYPE_TMP_BUFFER);
  40959. AssertNotNull(certId);
  40960. XMEMSET(certId, 0, sizeof(*certId));
  40961. /* Reset rawCertIdPtr since it was push forward in the previous call. */
  40962. rawCertIdPtr = &rawCertId[0];
  40963. certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, sizeof(rawCertId));
  40964. AssertNotNull(certId);
  40965. AssertIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  40966. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  40967. XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40968. /* The below tests should fail when passed bad parameters. NULL should
  40969. * always be returned. */
  40970. certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr, sizeof(rawCertId));
  40971. AssertNull(certIdBad);
  40972. certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL, sizeof(rawCertId));
  40973. AssertNull(certIdBad);
  40974. certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0);
  40975. AssertNull(certIdBad);
  40976. res = TEST_RES_CHECK(1);
  40977. #endif
  40978. return res;
  40979. }
  40980. static int test_wolfSSL_OCSP_id_cmp(void)
  40981. {
  40982. int res = TEST_SKIPPED;
  40983. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  40984. OCSP_CERTID id1;
  40985. OCSP_CERTID id2;
  40986. XMEMSET(&id1, 0, sizeof(id1));
  40987. XMEMSET(&id2, 0, sizeof(id2));
  40988. AssertIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  40989. res = TEST_RES_CHECK(1);
  40990. #endif
  40991. return res;
  40992. }
  40993. static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  40994. {
  40995. int res = TEST_SKIPPED;
  40996. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  40997. WOLFSSL_OCSP_SINGLERESP single;
  40998. const WOLFSSL_OCSP_CERTID* certId;
  40999. XMEMSET(&single, 0, sizeof(single));
  41000. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  41001. AssertPtrEq(&single, certId);
  41002. res = TEST_RES_CHECK(1);
  41003. #endif
  41004. return res;
  41005. }
  41006. static int test_wolfSSL_OCSP_single_get0_status(void)
  41007. {
  41008. int res = TEST_SKIPPED;
  41009. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  41010. WOLFSSL_OCSP_SINGLERESP single;
  41011. CertStatus certStatus;
  41012. WOLFSSL_ASN1_TIME* thisDate;
  41013. WOLFSSL_ASN1_TIME* nextDate;
  41014. int ret, i;
  41015. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  41016. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  41017. /* Fill the date fields with some dummy data. */
  41018. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  41019. certStatus.thisDateParsed.data[i] = i;
  41020. certStatus.nextDateParsed.data[i] = i;
  41021. }
  41022. certStatus.status = CERT_GOOD;
  41023. single.status = &certStatus;
  41024. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  41025. &nextDate);
  41026. AssertIntEQ(ret, CERT_GOOD);
  41027. AssertPtrEq(thisDate, &certStatus.thisDateParsed);
  41028. AssertPtrEq(nextDate, &certStatus.nextDateParsed);
  41029. res = TEST_RES_CHECK(1);
  41030. #endif
  41031. return res;
  41032. }
  41033. static int test_wolfSSL_OCSP_resp_count(void)
  41034. {
  41035. int res = TEST_SKIPPED;
  41036. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  41037. WOLFSSL_OCSP_BASICRESP basicResp;
  41038. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  41039. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  41040. int count;
  41041. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  41042. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  41043. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  41044. count = wolfSSL_OCSP_resp_count(&basicResp);
  41045. AssertIntEQ(count, 0);
  41046. basicResp.single = &singleRespOne;
  41047. count = wolfSSL_OCSP_resp_count(&basicResp);
  41048. AssertIntEQ(count, 1);
  41049. singleRespOne.next = &singleRespTwo;
  41050. count = wolfSSL_OCSP_resp_count(&basicResp);
  41051. AssertIntEQ(count, 2);
  41052. res = TEST_RES_CHECK(1);
  41053. #endif
  41054. return res;
  41055. }
  41056. static int test_wolfSSL_OCSP_resp_get0(void)
  41057. {
  41058. int res = TEST_SKIPPED;
  41059. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  41060. WOLFSSL_OCSP_BASICRESP basicResp;
  41061. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  41062. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  41063. WOLFSSL_OCSP_SINGLERESP* ret;
  41064. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  41065. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  41066. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  41067. basicResp.single = &singleRespOne;
  41068. singleRespOne.next = &singleRespTwo;
  41069. ret = wolfSSL_OCSP_resp_get0(&basicResp, 0);
  41070. AssertPtrEq(ret, &singleRespOne);
  41071. ret = wolfSSL_OCSP_resp_get0(&basicResp, 1);
  41072. AssertPtrEq(ret, &singleRespTwo);
  41073. res = TEST_RES_CHECK(1);
  41074. #endif
  41075. return res;
  41076. }
  41077. static int test_wolfSSL_EVP_PKEY_derive(void)
  41078. {
  41079. int res = TEST_SKIPPED;
  41080. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  41081. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  41082. EVP_PKEY_CTX *ctx;
  41083. unsigned char *skey;
  41084. size_t skeylen;
  41085. EVP_PKEY *pkey, *peerkey;
  41086. const unsigned char* key;
  41087. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  41088. /* DH */
  41089. key = dh_key_der_2048;
  41090. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  41091. sizeof_dh_key_der_2048)));
  41092. AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  41093. key = dh_key_der_2048;
  41094. AssertNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  41095. sizeof_dh_key_der_2048)));
  41096. AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  41097. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41098. AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
  41099. AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  41100. AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  41101. AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
  41102. AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  41103. EVP_PKEY_CTX_free(ctx);
  41104. EVP_PKEY_free(peerkey);
  41105. EVP_PKEY_free(pkey);
  41106. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  41107. #endif
  41108. #ifdef HAVE_ECC
  41109. /* ECDH */
  41110. key = ecc_clikey_der_256;
  41111. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  41112. sizeof_ecc_clikey_der_256)));
  41113. key = ecc_clikeypub_der_256;
  41114. AssertNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  41115. sizeof_ecc_clikeypub_der_256)));
  41116. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41117. AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
  41118. AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  41119. AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  41120. AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
  41121. AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  41122. EVP_PKEY_CTX_free(ctx);
  41123. EVP_PKEY_free(peerkey);
  41124. EVP_PKEY_free(pkey);
  41125. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  41126. #endif /* HAVE_ECC */
  41127. res = TEST_RES_CHECK(1);
  41128. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  41129. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  41130. return res;
  41131. }
  41132. static int test_wolfSSL_EVP_PBE_scrypt(void)
  41133. {
  41134. int res = TEST_SKIPPED;
  41135. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  41136. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  41137. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  41138. int ret;
  41139. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  41140. int pwdlen = sizeof(pwd);
  41141. const byte salt[] = {'N','a','C','l'};
  41142. int saltlen = sizeof(salt);
  41143. byte key[80];
  41144. word64 numOvr32 = (word64)INT32_MAX + 1;
  41145. /* expected derived key for N:16, r:1, p:1 */
  41146. const byte expectedKey[] = {
  41147. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  41148. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  41149. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  41150. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  41151. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  41152. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  41153. 0xE7, 0xE9, 0xC0, 0x9A};
  41154. /* N r p mx key keylen */
  41155. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  41156. AssertIntEQ(ret, 0); /* N must be greater than 1 */
  41157. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  41158. AssertIntEQ(ret, 0); /* N must be power of 2 */
  41159. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  41160. AssertIntEQ(ret, 0); /* r must be greater than 0 */
  41161. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  41162. AssertIntEQ(ret, 0); /* p must be greater than 0 */
  41163. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  41164. AssertIntEQ(ret, 0); /* keylen must be greater than 0 */
  41165. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  41166. AssertIntEQ(ret, 0); /* r must be smaller than 9 */
  41167. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  41168. AssertIntEQ(ret, 1); /* should succeed if key is NULL */
  41169. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  41170. AssertIntEQ(ret, 1); /* should succeed */
  41171. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  41172. key, 64);
  41173. AssertIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  41174. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  41175. key, 64);
  41176. AssertIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  41177. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  41178. AssertIntEQ(ret, 1); /* should succeed even if salt is NULL */
  41179. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  41180. AssertIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  41181. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  41182. AssertIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  41183. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  41184. AssertIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  41185. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  41186. AssertIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  41187. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  41188. AssertIntEQ(ret, 1);
  41189. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  41190. AssertIntEQ(ret, 0); /* derived key must be the same as expected-key */
  41191. res = TEST_RES_CHECK(1);
  41192. #endif /* !NO_PWDBASED && !NO_SHA256 */
  41193. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  41194. return res;
  41195. }
  41196. static int test_wolfSSL_EC_get_builtin_curves(void)
  41197. {
  41198. int res = TEST_SKIPPED;
  41199. #if defined(HAVE_ECC) && (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL))
  41200. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41201. EC_builtin_curve* curves = NULL;
  41202. size_t crv_len = 0;
  41203. size_t i = 0;
  41204. AssertIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  41205. AssertNotNull(curves = (EC_builtin_curve*)
  41206. XMALLOC(sizeof(EC_builtin_curve)*crv_len, NULL,
  41207. DYNAMIC_TYPE_TMP_BUFFER));
  41208. AssertIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  41209. for (i = 0; i < crv_len; i++)
  41210. {
  41211. if (curves[i].comment != NULL)
  41212. AssertStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  41213. }
  41214. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41215. res = TEST_RES_CHECK(1);
  41216. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  41217. #endif /* defined(HAVE_ECC) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  41218. return res;
  41219. }
  41220. static int test_no_op_functions(void)
  41221. {
  41222. int res = TEST_SKIPPED;
  41223. #if defined(OPENSSL_EXTRA)
  41224. /* this makes sure wolfSSL can compile and run these no-op functions */
  41225. SSL_load_error_strings();
  41226. ENGINE_load_builtin_engines();
  41227. OpenSSL_add_all_ciphers();
  41228. AssertIntEQ(CRYPTO_malloc_init(), 0);
  41229. res = TEST_RES_CHECK(1);
  41230. #endif
  41231. return res;
  41232. }
  41233. static int test_wolfSSL_CRYPTO_memcmp(void)
  41234. {
  41235. int res = TEST_SKIPPED;
  41236. #ifdef OPENSSL_EXTRA
  41237. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  41238. "implementation of TLS/SSL for embedded devices to the cloud.";
  41239. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  41240. "implementation of TLS/SSL for embedded devices to the cloud.";
  41241. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  41242. "implementation of TLS/SSL for embedded devices to the cloud!";
  41243. AssertIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  41244. AssertIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  41245. res = TEST_RES_CHECK(1);
  41246. #endif
  41247. return res;
  41248. }
  41249. /*----------------------------------------------------------------------------*
  41250. | wolfCrypt ASN
  41251. *----------------------------------------------------------------------------*/
  41252. static int test_wc_CreateEncryptedPKCS8Key(void)
  41253. {
  41254. int res = TEST_SKIPPED;
  41255. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  41256. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  41257. WC_RNG rng;
  41258. byte* encKey = NULL;
  41259. word32 encKeySz = 0;
  41260. word32 decKeySz = 0;
  41261. const char password[] = "Lorem ipsum dolor sit amet";
  41262. word32 passwordSz = (word32)XSTRLEN(password);
  41263. word32 tradIdx = 0;
  41264. AssertIntEQ(wc_InitRng(&rng), 0);
  41265. /* Call with NULL for out buffer to get necessary length. */
  41266. AssertIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  41267. sizeof_server_key_der_2048, NULL, &encKeySz, password, passwordSz,
  41268. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  41269. LENGTH_ONLY_E);
  41270. AssertNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  41271. DYNAMIC_TYPE_TMP_BUFFER));
  41272. /* Call with the allocated out buffer. */
  41273. AssertIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  41274. sizeof_server_key_der_2048, encKey, &encKeySz, password, passwordSz,
  41275. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  41276. 0);
  41277. /* Decrypt the encrypted PKCS8 key we just made. */
  41278. AssertIntGT((decKeySz = wc_DecryptPKCS8Key(encKey, encKeySz, password,
  41279. passwordSz)), 0);
  41280. /* encKey now holds the decrypted key (decrypted in place). */
  41281. AssertIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  41282. /* Check that the decrypted key matches the key prior to encryption. */
  41283. AssertIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  41284. sizeof_server_key_der_2048), 0);
  41285. if (encKey != NULL)
  41286. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41287. wc_FreeRng(&rng);
  41288. res = TEST_RES_CHECK(1);
  41289. #endif
  41290. return res;
  41291. }
  41292. static int test_wc_GetPkcs8TraditionalOffset(void)
  41293. {
  41294. int res = TEST_SKIPPED;
  41295. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  41296. int length, derSz;
  41297. word32 inOutIdx;
  41298. const char* path = "./certs/server-keyPkcs8.der";
  41299. XFILE file;
  41300. byte der[2048];
  41301. file = XFOPEN(path, "rb");
  41302. AssertTrue(file != XBADFILE);
  41303. derSz = (int)XFREAD(der, 1, sizeof(der), file);
  41304. XFCLOSE(file);
  41305. /* valid case */
  41306. inOutIdx = 0;
  41307. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  41308. AssertIntGT(length, 0);
  41309. /* inOutIdx > sz */
  41310. inOutIdx = 4000;
  41311. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  41312. AssertIntEQ(length, BAD_FUNC_ARG);
  41313. /* null input */
  41314. inOutIdx = 0;
  41315. length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0);
  41316. AssertIntEQ(length, BAD_FUNC_ARG);
  41317. /* invalid input, fill buffer with 1's */
  41318. XMEMSET(der, 1, sizeof(der));
  41319. inOutIdx = 0;
  41320. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  41321. AssertIntEQ(length, ASN_PARSE_E);
  41322. res = TEST_RES_CHECK(1);
  41323. #endif /* NO_ASN */
  41324. return res;
  41325. }
  41326. static int test_wc_SetSubjectRaw(void)
  41327. {
  41328. int res = TEST_SKIPPED;
  41329. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41330. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  41331. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  41332. WOLFSSL_X509* x509;
  41333. int peerCertSz;
  41334. const byte* peerCertBuf;
  41335. Cert forgedCert;
  41336. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  41337. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  41338. AssertIntEQ(0, wc_InitCert(&forgedCert));
  41339. AssertIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  41340. wolfSSL_FreeX509(x509);
  41341. res = TEST_RES_CHECK(1);
  41342. #endif
  41343. return res;
  41344. }
  41345. static int test_wc_GetSubjectRaw(void)
  41346. {
  41347. int res = TEST_SKIPPED;
  41348. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41349. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  41350. Cert cert;
  41351. byte *subjectRaw;
  41352. AssertIntEQ(0, wc_InitCert(&cert));
  41353. AssertIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  41354. res = TEST_RES_CHECK(1);
  41355. #endif
  41356. return res;
  41357. }
  41358. static int test_wc_SetIssuerRaw(void)
  41359. {
  41360. int res = TEST_SKIPPED;
  41361. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41362. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  41363. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  41364. WOLFSSL_X509* x509;
  41365. int peerCertSz;
  41366. const byte* peerCertBuf;
  41367. Cert forgedCert;
  41368. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  41369. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  41370. AssertIntEQ(0, wc_InitCert(&forgedCert));
  41371. AssertIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  41372. wolfSSL_FreeX509(x509);
  41373. res = TEST_RES_CHECK(1);
  41374. #endif
  41375. return res;
  41376. }
  41377. static int test_wc_SetIssueBuffer(void)
  41378. {
  41379. int res = TEST_SKIPPED;
  41380. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41381. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  41382. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  41383. WOLFSSL_X509* x509;
  41384. int peerCertSz;
  41385. const byte* peerCertBuf;
  41386. Cert forgedCert;
  41387. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  41388. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  41389. AssertIntEQ(0, wc_InitCert(&forgedCert));
  41390. AssertIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  41391. wolfSSL_FreeX509(x509);
  41392. res = TEST_RES_CHECK(1);
  41393. #endif
  41394. return res;
  41395. }
  41396. /*
  41397. * Testing wc_SetSubjectKeyId
  41398. */
  41399. static int test_wc_SetSubjectKeyId(void)
  41400. {
  41401. int res = TEST_SKIPPED;
  41402. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41403. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  41404. Cert cert;
  41405. const char* file = "certs/ecc-client-keyPub.pem";
  41406. AssertIntEQ(0, wc_InitCert(&cert));
  41407. AssertIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  41408. AssertIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  41409. AssertIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  41410. res = TEST_RES_CHECK(1);
  41411. #endif
  41412. return res;
  41413. } /* END test_wc_SetSubjectKeyId */
  41414. /*
  41415. * Testing wc_SetSubject
  41416. */
  41417. static int test_wc_SetSubject(void)
  41418. {
  41419. int res = TEST_SKIPPED;
  41420. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41421. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  41422. Cert cert;
  41423. const char* file = "./certs/ca-ecc-cert.pem";
  41424. AssertIntEQ(0, wc_InitCert(&cert));
  41425. AssertIntEQ(0, wc_SetSubject(&cert, file));
  41426. AssertIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  41427. AssertIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  41428. res = TEST_RES_CHECK(1);
  41429. #endif
  41430. return res;
  41431. } /* END test_wc_SetSubject */
  41432. static int test_CheckCertSignature(void)
  41433. {
  41434. int res = TEST_SKIPPED;
  41435. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  41436. WOLFSSL_CERT_MANAGER* cm = NULL;
  41437. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  41438. FILE* fp;
  41439. byte cert[4096];
  41440. int certSz;
  41441. #endif
  41442. AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
  41443. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  41444. AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
  41445. #ifndef NO_RSA
  41446. #ifdef USE_CERT_BUFFERS_1024
  41447. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
  41448. sizeof_server_cert_der_1024, NULL, cm));
  41449. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  41450. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  41451. WOLFSSL_FILETYPE_ASN1));
  41452. AssertIntEQ(0, CheckCertSignature(server_cert_der_1024,
  41453. sizeof_server_cert_der_1024, NULL, cm));
  41454. #elif defined(USE_CERT_BUFFERS_2048)
  41455. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
  41456. sizeof_server_cert_der_2048, NULL, cm));
  41457. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  41458. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  41459. WOLFSSL_FILETYPE_ASN1));
  41460. AssertIntEQ(0, CheckCertSignature(server_cert_der_2048,
  41461. sizeof_server_cert_der_2048, NULL, cm));
  41462. #endif
  41463. #endif
  41464. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  41465. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
  41466. sizeof_serv_ecc_der_256, NULL, cm));
  41467. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  41468. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  41469. WOLFSSL_FILETYPE_ASN1));
  41470. AssertIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  41471. NULL, cm));
  41472. #endif
  41473. #if !defined(NO_FILESYSTEM)
  41474. wolfSSL_CertManagerFree(cm);
  41475. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  41476. #ifndef NO_RSA
  41477. AssertNotNull(fp = XFOPEN("./certs/server-cert.der", "rb"));
  41478. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  41479. XFCLOSE(fp);
  41480. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  41481. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  41482. "./certs/ca-cert.pem", NULL));
  41483. AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  41484. #endif
  41485. #ifdef HAVE_ECC
  41486. AssertNotNull(fp = XFOPEN("./certs/server-ecc.der", "rb"));
  41487. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  41488. XFCLOSE(fp);
  41489. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  41490. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  41491. "./certs/ca-ecc-cert.pem", NULL));
  41492. AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  41493. #endif
  41494. #endif
  41495. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  41496. (void)fp;
  41497. (void)cert;
  41498. (void)certSz;
  41499. #endif
  41500. wolfSSL_CertManagerFree(cm);
  41501. res = TEST_RES_CHECK(1);
  41502. #endif
  41503. return res;
  41504. }
  41505. static int test_wc_ParseCert(void)
  41506. {
  41507. int res = TEST_SKIPPED;
  41508. #if !defined(NO_CERTS) && !defined(NO_RSA)
  41509. DecodedCert decodedCert;
  41510. const byte* rawCert = client_cert_der_2048;
  41511. const int rawCertSize = sizeof_client_cert_der_2048;
  41512. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  41513. AssertIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  41514. #ifndef IGNORE_NAME_CONSTRAINTS
  41515. /* check that the subjects emailAddress was not put in the alt name list */
  41516. AssertNotNull(decodedCert.subjectEmail);
  41517. AssertNull(decodedCert.altEmailNames);
  41518. #endif
  41519. wc_FreeDecodedCert(&decodedCert);
  41520. res = TEST_RES_CHECK(1);
  41521. #endif
  41522. return res;
  41523. }
  41524. static int test_MakeCertWithPathLen(void)
  41525. {
  41526. int res = TEST_SKIPPED;
  41527. #if defined(WOLFSSL_CERT_REQ) && defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  41528. const byte expectedPathLen = 7;
  41529. Cert cert;
  41530. DecodedCert decodedCert;
  41531. byte der[FOURK_BUF];
  41532. int derSize = 0;
  41533. WC_RNG rng;
  41534. ecc_key key;
  41535. AssertIntEQ(wc_InitRng(&rng), 0);
  41536. AssertIntEQ(wc_ecc_init(&key), 0);
  41537. AssertIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  41538. AssertIntEQ(wc_InitCert(&cert), 0);
  41539. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  41540. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  41541. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  41542. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  41543. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  41544. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com", CTC_NAME_SIZE);
  41545. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com", CTC_NAME_SIZE);
  41546. cert.selfSigned = 1;
  41547. cert.isCA = 1;
  41548. cert.pathLen = expectedPathLen;
  41549. cert.pathLenSet = 1;
  41550. cert.sigType = CTC_SHA256wECDSA;
  41551. #ifdef WOLFSSL_CERT_EXT
  41552. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  41553. #endif
  41554. AssertIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  41555. derSize = wc_SignCert(cert.bodySz, cert.sigType, der, FOURK_BUF, NULL,
  41556. &key, &rng);
  41557. AssertIntGE(derSize, 0);
  41558. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  41559. AssertIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  41560. AssertIntEQ(decodedCert.pathLength, expectedPathLen);
  41561. wc_FreeDecodedCert(&decodedCert);
  41562. AssertIntEQ(wc_ecc_free(&key), 0);
  41563. AssertIntEQ(wc_FreeRng(&rng), 0);
  41564. res = TEST_RES_CHECK(1);
  41565. #endif
  41566. return res;
  41567. }
  41568. /*----------------------------------------------------------------------------*
  41569. | wolfCrypt ECC
  41570. *----------------------------------------------------------------------------*/
  41571. static int test_wc_ecc_get_curve_size_from_name(void)
  41572. {
  41573. int res = TEST_SKIPPED;
  41574. #ifdef HAVE_ECC
  41575. int ret;
  41576. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  41577. ret = wc_ecc_get_curve_size_from_name("SECP256R1");
  41578. AssertIntEQ(ret, 32);
  41579. #endif
  41580. /* invalid case */
  41581. ret = wc_ecc_get_curve_size_from_name("BADCURVE");
  41582. AssertIntEQ(ret, -1);
  41583. /* NULL input */
  41584. ret = wc_ecc_get_curve_size_from_name(NULL);
  41585. AssertIntEQ(ret, BAD_FUNC_ARG);
  41586. res = TEST_RES_CHECK(1);
  41587. #endif /* HAVE_ECC */
  41588. return res;
  41589. }
  41590. static int test_wc_ecc_get_curve_id_from_name(void)
  41591. {
  41592. int res = TEST_SKIPPED;
  41593. #ifdef HAVE_ECC
  41594. int id;
  41595. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  41596. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  41597. AssertIntEQ(id, ECC_SECP256R1);
  41598. #endif
  41599. /* invalid case */
  41600. id = wc_ecc_get_curve_id_from_name("BADCURVE");
  41601. AssertIntEQ(id, -1);
  41602. /* NULL input */
  41603. id = wc_ecc_get_curve_id_from_name(NULL);
  41604. AssertIntEQ(id, BAD_FUNC_ARG);
  41605. res = TEST_RES_CHECK(1);
  41606. #endif /* HAVE_ECC */
  41607. return res;
  41608. }
  41609. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  41610. !defined(HAVE_SELFTEST) && \
  41611. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  41612. static int test_wc_ecc_get_curve_id_from_dp_params(void)
  41613. {
  41614. int id;
  41615. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  41616. int curve_id;
  41617. ecc_key* key;
  41618. const ecc_set_type* params;
  41619. int ret;
  41620. #endif
  41621. WOLFSSL_EC_KEY *ecKey = NULL;
  41622. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  41623. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  41624. AssertIntEQ(id, ECC_SECP256R1);
  41625. ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  41626. AssertNotNull(ecKey);
  41627. ret = EC_KEY_generate_key(ecKey);
  41628. if (ret == 0) {
  41629. /* normal test */
  41630. key = (ecc_key*)ecKey->internal;
  41631. params = key->dp;
  41632. curve_id = wc_ecc_get_curve_id_from_dp_params(params);
  41633. AssertIntEQ(curve_id, id);
  41634. }
  41635. #endif
  41636. /* invalid case, NULL input*/
  41637. id = wc_ecc_get_curve_id_from_dp_params(NULL);
  41638. AssertIntEQ(id, BAD_FUNC_ARG);
  41639. wolfSSL_EC_KEY_free(ecKey);
  41640. return TEST_RES_CHECK(1);
  41641. }
  41642. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  41643. static int test_wc_ecc_get_curve_id_from_params(void)
  41644. {
  41645. int res = TEST_SKIPPED;
  41646. #ifdef HAVE_ECC
  41647. int id;
  41648. const byte prime[] =
  41649. {
  41650. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  41651. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41652. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  41653. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  41654. };
  41655. const byte primeInvalid[] =
  41656. {
  41657. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  41658. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41659. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  41660. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  41661. };
  41662. const byte Af[] =
  41663. {
  41664. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  41665. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41666. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  41667. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  41668. };
  41669. const byte Bf[] =
  41670. {
  41671. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  41672. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  41673. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  41674. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  41675. };
  41676. const byte order[] =
  41677. {
  41678. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  41679. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  41680. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  41681. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  41682. };
  41683. const byte Gx[] =
  41684. {
  41685. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  41686. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  41687. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  41688. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  41689. };
  41690. const byte Gy[] =
  41691. {
  41692. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  41693. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  41694. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  41695. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  41696. };
  41697. int cofactor = 1;
  41698. int fieldSize = 256;
  41699. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  41700. id = wc_ecc_get_curve_id_from_params(fieldSize, prime, sizeof(prime),
  41701. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  41702. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  41703. AssertIntEQ(id, ECC_SECP256R1);
  41704. #endif
  41705. /* invalid case, fieldSize = 0 */
  41706. id = wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  41707. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  41708. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  41709. AssertIntEQ(id, ECC_CURVE_INVALID);
  41710. /* invalid case, NULL prime */
  41711. id = wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  41712. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  41713. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  41714. AssertIntEQ(id, BAD_FUNC_ARG);
  41715. /* invalid case, invalid prime */
  41716. id = wc_ecc_get_curve_id_from_params(fieldSize,
  41717. primeInvalid, sizeof(primeInvalid),
  41718. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  41719. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  41720. AssertIntEQ(id, ECC_CURVE_INVALID);
  41721. res = TEST_RES_CHECK(1);
  41722. #endif
  41723. return res;
  41724. }
  41725. static int test_wolfSSL_EVP_PKEY_encrypt(void)
  41726. {
  41727. int res = TEST_SKIPPED;
  41728. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41729. !defined(HAVE_FAST_RSA)
  41730. WOLFSSL_RSA* rsa = NULL;
  41731. WOLFSSL_EVP_PKEY* pkey = NULL;
  41732. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  41733. const char* in = "What is easy to do is easy not to do.";
  41734. size_t inlen = XSTRLEN(in);
  41735. size_t outEncLen = 0;
  41736. byte* outEnc = NULL;
  41737. byte* outDec = NULL;
  41738. size_t outDecLen = 0;
  41739. size_t rsaKeySz = 2048/8; /* Bytes */
  41740. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  41741. byte* inTmp = NULL;
  41742. byte* outEncTmp = NULL;
  41743. byte* outDecTmp = NULL;
  41744. #endif
  41745. AssertNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41746. XMEMSET(outEnc, 0, rsaKeySz);
  41747. AssertNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41748. XMEMSET(outDec, 0, rsaKeySz);
  41749. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  41750. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41751. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  41752. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41753. AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  41754. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  41755. WOLFSSL_SUCCESS);
  41756. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  41757. since ctx uses it.*/
  41758. AssertIntEQ(pkey->references, 2);
  41759. EVP_PKEY_free(pkey);
  41760. AssertIntEQ(pkey->references, 1);
  41761. /* Encrypt data */
  41762. /* Check that we can get the required output buffer length by passing in a
  41763. * NULL output buffer. */
  41764. AssertIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  41765. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  41766. AssertIntEQ(rsaKeySz, outEncLen);
  41767. /* Now do the actual encryption. */
  41768. AssertIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  41769. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  41770. /* Decrypt data */
  41771. AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  41772. /* Check that we can get the required output buffer length by passing in a
  41773. * NULL output buffer. */
  41774. AssertIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  41775. WOLFSSL_SUCCESS);
  41776. AssertIntEQ(rsaKeySz, outDecLen);
  41777. /* Now do the actual decryption. */
  41778. AssertIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  41779. WOLFSSL_SUCCESS);
  41780. AssertIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  41781. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  41782. /* The input length must be the same size as the RSA key.*/
  41783. AssertNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41784. XMEMSET(inTmp, 9, rsaKeySz);
  41785. AssertNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41786. XMEMSET(outEncTmp, 0, rsaKeySz);
  41787. AssertNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41788. XMEMSET(outDecTmp, 0, rsaKeySz);
  41789. AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  41790. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  41791. WOLFSSL_SUCCESS);
  41792. AssertIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  41793. WOLFSSL_SUCCESS);
  41794. AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  41795. AssertIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp, outEncLen),
  41796. WOLFSSL_SUCCESS);
  41797. AssertIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  41798. #endif
  41799. EVP_PKEY_CTX_free(ctx);
  41800. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41801. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41802. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  41803. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41804. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41805. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41806. #endif
  41807. res = TEST_RES_CHECK(1);
  41808. #endif
  41809. return res;
  41810. }
  41811. static int test_wolfSSL_EVP_PKEY_sign_verify(void)
  41812. {
  41813. int res = TEST_SKIPPED;
  41814. #if defined(OPENSSL_EXTRA)
  41815. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  41816. WOLFSSL_DSA* dsa = NULL;
  41817. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  41818. WOLFSSL_EVP_PKEY* pkey = NULL;
  41819. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  41820. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  41821. const char* in = "What is easy to do is easy not to do.";
  41822. size_t inlen = XSTRLEN(in);
  41823. byte hash[SHA256_DIGEST_LENGTH] = {0};
  41824. byte zero[SHA256_DIGEST_LENGTH] = {0};
  41825. SHA256_CTX c;
  41826. byte* sig = NULL;
  41827. byte* sigVerify = NULL;
  41828. size_t siglen;
  41829. size_t siglenOnlyLen;
  41830. size_t keySz = 2048/8; /* Bytes */
  41831. int i;
  41832. int encs[3] = {0};
  41833. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41834. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  41835. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41836. encs[0] = EVP_PKEY_RSA;
  41837. #endif
  41838. #endif
  41839. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  41840. encs[1] = EVP_PKEY_DSA;
  41841. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  41842. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  41843. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41844. encs[2] = EVP_PKEY_EC;
  41845. #endif
  41846. #endif
  41847. AssertNotNull(sig =
  41848. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41849. AssertNotNull(sigVerify =
  41850. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41851. for (i = 0; i < 3; i++) {
  41852. if (encs[i] == 0)
  41853. continue;
  41854. siglen = keySz;
  41855. XMEMSET(sig, 0, keySz);
  41856. XMEMSET(sigVerify, 0, keySz);
  41857. /* Generate hash */
  41858. SHA256_Init(&c);
  41859. SHA256_Update(&c, in, inlen);
  41860. SHA256_Final(hash, &c);
  41861. #ifdef WOLFSSL_SMALL_STACK_CACHE
  41862. /* workaround for small stack cache case */
  41863. wc_Sha256Free((wc_Sha256*)&c);
  41864. #endif
  41865. /* Generate key */
  41866. AssertNotNull(pkey = EVP_PKEY_new());
  41867. switch (encs[i]) {
  41868. case EVP_PKEY_RSA:
  41869. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41870. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  41871. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41872. {
  41873. WOLFSSL_RSA* rsa = NULL;
  41874. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  41875. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  41876. }
  41877. #endif
  41878. #endif
  41879. break;
  41880. case EVP_PKEY_DSA:
  41881. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  41882. AssertNotNull(dsa = DSA_new());
  41883. AssertIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  41884. NULL, 0, NULL, NULL, NULL), 1);
  41885. AssertIntEQ(DSA_generate_key(dsa), 1);
  41886. AssertIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  41887. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  41888. break;
  41889. case EVP_PKEY_EC:
  41890. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  41891. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41892. {
  41893. WOLFSSL_EC_KEY* ecKey = NULL;
  41894. AssertNotNull(ecKey = EC_KEY_new());
  41895. AssertIntEQ(EC_KEY_generate_key(ecKey), 1);
  41896. AssertIntEQ(
  41897. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  41898. }
  41899. #endif
  41900. #endif
  41901. break;
  41902. }
  41903. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41904. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  41905. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41906. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  41907. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41908. if (encs[i] == EVP_PKEY_RSA)
  41909. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  41910. WOLFSSL_SUCCESS);
  41911. #endif
  41912. #endif
  41913. /* Check returning only length */
  41914. AssertIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  41915. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  41916. AssertIntGT(siglenOnlyLen, 0);
  41917. /* Sign data */
  41918. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  41919. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  41920. AssertIntGE(siglenOnlyLen, siglen);
  41921. /* Verify signature */
  41922. AssertNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  41923. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  41924. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41925. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  41926. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41927. if (encs[i] == EVP_PKEY_RSA)
  41928. AssertIntEQ(
  41929. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  41930. WOLFSSL_SUCCESS);
  41931. #endif
  41932. #endif
  41933. AssertIntEQ(EVP_PKEY_verify(
  41934. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  41935. WOLFSSL_SUCCESS);
  41936. AssertIntEQ(EVP_PKEY_verify(
  41937. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  41938. WOLFSSL_FAILURE);
  41939. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41940. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  41941. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41942. if (encs[i] == EVP_PKEY_RSA) {
  41943. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  41944. /* Try RSA sign/verify with no padding. */
  41945. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  41946. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  41947. WOLFSSL_SUCCESS);
  41948. AssertIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  41949. siglen), WOLFSSL_SUCCESS);
  41950. AssertIntGE(siglenOnlyLen, siglen);
  41951. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  41952. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  41953. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  41954. AssertIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  41955. siglen), WOLFSSL_SUCCESS);
  41956. #endif
  41957. /* Wrong padding schemes. */
  41958. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  41959. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  41960. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  41961. AssertIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  41962. siglen), WOLFSSL_SUCCESS);
  41963. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  41964. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  41965. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  41966. AssertIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  41967. siglen), WOLFSSL_SUCCESS);
  41968. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  41969. WOLFSSL_SUCCESS);
  41970. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  41971. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  41972. }
  41973. #endif
  41974. #endif
  41975. /* error cases */
  41976. siglen = keySz; /* Reset because sig size may vary slightly */
  41977. AssertIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  41978. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  41979. AssertIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  41980. WOLFSSL_SUCCESS);
  41981. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  41982. WOLFSSL_SUCCESS);
  41983. EVP_PKEY_free(pkey);
  41984. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  41985. DSA_free(dsa);
  41986. dsa = NULL;
  41987. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  41988. EVP_PKEY_CTX_free(ctx_verify);
  41989. EVP_PKEY_CTX_free(ctx);
  41990. }
  41991. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41992. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41993. res = TEST_RES_CHECK(1);
  41994. #endif /* OPENSSL_EXTRA */
  41995. return res;
  41996. }
  41997. static int test_EVP_PKEY_rsa(void)
  41998. {
  41999. int res = TEST_SKIPPED;
  42000. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  42001. WOLFSSL_RSA* rsa;
  42002. WOLFSSL_EVP_PKEY* pkey;
  42003. AssertNotNull(rsa = wolfSSL_RSA_new());
  42004. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42005. AssertIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  42006. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  42007. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  42008. AssertPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  42009. wolfSSL_EVP_PKEY_free(pkey);
  42010. res = TEST_RES_CHECK(1);
  42011. #endif
  42012. return res;
  42013. }
  42014. static int test_EVP_PKEY_ec(void)
  42015. {
  42016. int res = TEST_SKIPPED;
  42017. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  42018. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42019. WOLFSSL_EC_KEY* ecKey;
  42020. WOLFSSL_EVP_PKEY* pkey;
  42021. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  42022. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42023. AssertIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  42024. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  42025. /* Should fail since ecKey is empty */
  42026. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  42027. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  42028. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  42029. wolfSSL_EVP_PKEY_free(pkey);
  42030. res = TEST_RES_CHECK(1);
  42031. #endif
  42032. #endif
  42033. return res;
  42034. }
  42035. static int test_EVP_PKEY_cmp(void)
  42036. {
  42037. int res = TEST_SKIPPED;
  42038. #if defined(OPENSSL_EXTRA)
  42039. EVP_PKEY *a, *b;
  42040. const unsigned char *in;
  42041. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  42042. in = client_key_der_2048;
  42043. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  42044. &in, (long)sizeof_client_key_der_2048));
  42045. in = client_key_der_2048;
  42046. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  42047. &in, (long)sizeof_client_key_der_2048));
  42048. /* Test success case RSA */
  42049. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42050. AssertIntEQ(EVP_PKEY_cmp(a, b), 1);
  42051. #else
  42052. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  42053. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  42054. EVP_PKEY_free(b);
  42055. EVP_PKEY_free(a);
  42056. #endif
  42057. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  42058. in = ecc_clikey_der_256;
  42059. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  42060. &in, (long)sizeof_ecc_clikey_der_256));
  42061. in = ecc_clikey_der_256;
  42062. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  42063. &in, (long)sizeof_ecc_clikey_der_256));
  42064. /* Test success case ECC */
  42065. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42066. AssertIntEQ(EVP_PKEY_cmp(a, b), 1);
  42067. #else
  42068. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  42069. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  42070. EVP_PKEY_free(b);
  42071. EVP_PKEY_free(a);
  42072. #endif
  42073. /* Test failure cases */
  42074. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  42075. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  42076. in = client_key_der_2048;
  42077. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  42078. &in, (long)sizeof_client_key_der_2048));
  42079. in = ecc_clikey_der_256;
  42080. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  42081. &in, (long)sizeof_ecc_clikey_der_256));
  42082. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42083. AssertIntEQ(EVP_PKEY_cmp(a, b), -1);
  42084. #else
  42085. AssertIntNE(EVP_PKEY_cmp(a, b), 0);
  42086. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  42087. EVP_PKEY_free(b);
  42088. EVP_PKEY_free(a);
  42089. #endif
  42090. /* invalid or empty failure cases */
  42091. a = EVP_PKEY_new();
  42092. b = EVP_PKEY_new();
  42093. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42094. AssertIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  42095. AssertIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  42096. AssertIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  42097. #ifdef NO_RSA
  42098. /* Type check will fail since RSA is the default EVP key type */
  42099. AssertIntEQ(EVP_PKEY_cmp(a, b), -2);
  42100. #else
  42101. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  42102. #endif
  42103. #else
  42104. AssertIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  42105. AssertIntNE(EVP_PKEY_cmp(a, NULL), 0);
  42106. AssertIntNE(EVP_PKEY_cmp(NULL, b), 0);
  42107. AssertIntNE(EVP_PKEY_cmp(a, b), 0);
  42108. #endif
  42109. EVP_PKEY_free(b);
  42110. EVP_PKEY_free(a);
  42111. (void)in;
  42112. res = TEST_RES_CHECK(1);
  42113. #endif
  42114. return res;
  42115. }
  42116. static int test_ERR_load_crypto_strings(void)
  42117. {
  42118. int res = TEST_SKIPPED;
  42119. #if defined(OPENSSL_ALL)
  42120. ERR_load_crypto_strings();
  42121. res = TEST_RES_CHECK(1);
  42122. #endif
  42123. return res;
  42124. }
  42125. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  42126. static void free_x509(X509* x)
  42127. {
  42128. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  42129. }
  42130. #endif
  42131. static int test_sk_X509(void)
  42132. {
  42133. int res = TEST_SKIPPED;
  42134. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  42135. {
  42136. STACK_OF(X509)* s;
  42137. AssertNotNull(s = sk_X509_new_null());
  42138. AssertIntEQ(sk_X509_num(s), 0);
  42139. sk_X509_pop_free(s, NULL);
  42140. AssertNotNull(s = sk_X509_new_null());
  42141. AssertIntEQ(sk_X509_num(s), 0);
  42142. sk_X509_pop_free(s, NULL);
  42143. AssertNotNull(s = sk_X509_new_null());
  42144. sk_X509_push(s, (X509*)1);
  42145. AssertIntEQ(sk_X509_num(s), 1);
  42146. AssertIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  42147. sk_X509_push(s, (X509*)2);
  42148. AssertIntEQ(sk_X509_num(s), 2);
  42149. AssertIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  42150. AssertIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  42151. sk_X509_push(s, (X509*)2);
  42152. sk_X509_pop_free(s, free_x509);
  42153. }
  42154. {
  42155. /* Push a list of 10 X509s onto stack, then verify that
  42156. * value(), push(), shift(), and pop() behave as expected. */
  42157. STACK_OF(X509)* s;
  42158. X509* xList[10];
  42159. int i = 0;
  42160. const int len = (sizeof(xList) / sizeof(xList[0]));
  42161. for (i = 0; i < len; ++i)
  42162. AssertNotNull(xList[i] = X509_new());
  42163. /* test push, pop, and free */
  42164. AssertNotNull(s = sk_X509_new_null());
  42165. for (i = 0; i < len; ++i) {
  42166. sk_X509_push(s, xList[i]);
  42167. AssertIntEQ(sk_X509_num(s), i + 1);
  42168. AssertIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  42169. AssertIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  42170. }
  42171. /* pop returns and removes last pushed on stack, which is index 0
  42172. * in sk_x509_value */
  42173. for (i = 0; i < len; ++i) {
  42174. X509 * x = sk_X509_value(s, 0);
  42175. X509 * y = sk_X509_pop(s);
  42176. X509 * z = xList[len - 1 - i];
  42177. AssertIntEQ((x == y), 1);
  42178. AssertIntEQ((x == z), 1);
  42179. AssertIntEQ(sk_X509_num(s), len - 1 - i);
  42180. }
  42181. sk_free(s);
  42182. /* test push, shift, and free */
  42183. AssertNotNull(s = sk_X509_new_null());
  42184. for (i = 0; i < len; ++i) {
  42185. sk_X509_push(s, xList[i]);
  42186. AssertIntEQ(sk_X509_num(s), i + 1);
  42187. AssertIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  42188. AssertIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  42189. }
  42190. /* shift returns and removes first pushed on stack, which is index i
  42191. * in sk_x509_value() */
  42192. for (i = 0; i < len; ++i) {
  42193. X509 * x = sk_X509_value(s, len - 1 - i);
  42194. X509 * y = sk_X509_shift(s);
  42195. X509 * z = xList[i];
  42196. AssertIntEQ((x == y), 1);
  42197. AssertIntEQ((x == z), 1);
  42198. AssertIntEQ(sk_X509_num(s), len - 1 - i);
  42199. }
  42200. sk_free(s);
  42201. for (i = 0; i < len; ++i)
  42202. X509_free(xList[i]);
  42203. }
  42204. res = TEST_RES_CHECK(1);
  42205. #endif
  42206. return res;
  42207. }
  42208. static int test_sk_X509_CRL(void)
  42209. {
  42210. int res = TEST_SKIPPED;
  42211. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  42212. X509_CRL* crl;
  42213. XFILE fp;
  42214. STACK_OF(X509_CRL)* s;
  42215. fp = XFOPEN("./certs/crl/crl.pem", "rb");
  42216. AssertTrue((fp != XBADFILE));
  42217. AssertNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
  42218. XFCLOSE(fp);
  42219. AssertNotNull(s = sk_X509_CRL_new());
  42220. AssertIntEQ(sk_X509_CRL_num(s), 0);
  42221. AssertIntEQ(sk_X509_CRL_push(s, crl), 1);
  42222. AssertIntEQ(sk_X509_CRL_num(s), 1);
  42223. AssertPtrEq(sk_X509_CRL_value(s, 0), crl);
  42224. sk_X509_CRL_free(s);
  42225. res = TEST_RES_CHECK(1);
  42226. #endif
  42227. return res;
  42228. }
  42229. static int test_X509_get_signature_nid(void)
  42230. {
  42231. int res = TEST_SKIPPED;
  42232. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  42233. X509* x509;
  42234. AssertIntEQ(X509_get_signature_nid(NULL), 0);
  42235. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42236. SSL_FILETYPE_PEM));
  42237. AssertIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  42238. X509_free(x509);
  42239. res = TEST_RES_CHECK(1);
  42240. #endif
  42241. return res;
  42242. }
  42243. static int test_X509_REQ(void)
  42244. {
  42245. int res = TEST_SKIPPED;
  42246. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  42247. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  42248. X509_NAME* name;
  42249. #ifndef NO_RSA
  42250. X509_NAME* subject;
  42251. #endif
  42252. #if !defined(NO_RSA) || defined(HAVE_ECC)
  42253. X509_REQ* req;
  42254. EVP_PKEY* priv;
  42255. EVP_PKEY* pub;
  42256. unsigned char* der = NULL;
  42257. int len;
  42258. #endif
  42259. #ifndef NO_RSA
  42260. EVP_MD_CTX *mctx = NULL;
  42261. EVP_PKEY_CTX *pkctx = NULL;
  42262. #ifdef USE_CERT_BUFFERS_1024
  42263. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  42264. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  42265. #elif defined(USE_CERT_BUFFERS_2048)
  42266. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  42267. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  42268. #endif
  42269. #endif
  42270. #ifdef HAVE_ECC
  42271. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  42272. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  42273. #endif
  42274. AssertNotNull(name = X509_NAME_new());
  42275. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  42276. (byte*)"wolfssl.com", 11, 0, 1),
  42277. WOLFSSL_SUCCESS);
  42278. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  42279. (byte*)"support@wolfssl.com", 19, -1,
  42280. 1), WOLFSSL_SUCCESS);
  42281. #ifndef NO_RSA
  42282. AssertNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  42283. (long)sizeof_client_key_der_2048));
  42284. AssertNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  42285. (long)sizeof_client_keypub_der_2048));
  42286. AssertNotNull(req = X509_REQ_new());
  42287. AssertIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  42288. AssertIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  42289. AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  42290. AssertIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  42291. AssertIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  42292. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  42293. AssertIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  42294. AssertIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  42295. AssertIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  42296. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  42297. len = i2d_X509_REQ(req, &der);
  42298. DEBUG_WRITE_DER(der, len, "req.der");
  42299. #ifdef USE_CERT_BUFFERS_1024
  42300. AssertIntEQ(len, 381);
  42301. #else
  42302. AssertIntEQ(len, 643);
  42303. #endif
  42304. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  42305. der = NULL;
  42306. mctx = EVP_MD_CTX_new();
  42307. AssertIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv), WOLFSSL_SUCCESS);
  42308. AssertIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  42309. EVP_MD_CTX_free(mctx);
  42310. X509_REQ_free(NULL);
  42311. X509_REQ_free(req);
  42312. /* Test getting the subject from a newly created X509_REQ */
  42313. AssertNotNull(req = X509_REQ_new());
  42314. AssertNotNull(subject = X509_REQ_get_subject_name(req));
  42315. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  42316. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  42317. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  42318. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  42319. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  42320. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  42321. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  42322. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  42323. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  42324. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  42325. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  42326. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  42327. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  42328. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  42329. len = i2d_X509_REQ(req, &der);
  42330. DEBUG_WRITE_DER(der, len, "req2.der");
  42331. #ifdef USE_CERT_BUFFERS_1024
  42332. AssertIntEQ(len, 435);
  42333. #else
  42334. AssertIntEQ(len, 696);
  42335. #endif
  42336. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  42337. der = NULL;
  42338. EVP_PKEY_free(pub);
  42339. EVP_PKEY_free(priv);
  42340. X509_REQ_free(req);
  42341. #endif
  42342. #ifdef HAVE_ECC
  42343. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  42344. sizeof_ecc_clikey_der_256));
  42345. AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  42346. sizeof_ecc_clikeypub_der_256));
  42347. AssertNotNull(req = X509_REQ_new());
  42348. AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  42349. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  42350. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  42351. /* Signature is random and may be shorter or longer. */
  42352. AssertIntGE((len = i2d_X509_REQ(req, &der)), 245);
  42353. AssertIntLE(len, 253);
  42354. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  42355. X509_REQ_free(req);
  42356. EVP_PKEY_free(pub);
  42357. EVP_PKEY_free(priv);
  42358. #ifdef FP_ECC
  42359. wc_ecc_fp_free();
  42360. #endif
  42361. #endif /* HAVE_ECC */
  42362. X509_NAME_free(name);
  42363. res = TEST_RES_CHECK(1);
  42364. #endif
  42365. return res;
  42366. }
  42367. static int test_wolfssl_PKCS7(void)
  42368. {
  42369. int res = TEST_SKIPPED;
  42370. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO)
  42371. PKCS7* pkcs7;
  42372. byte data[FOURK_BUF];
  42373. word32 len = sizeof(data);
  42374. const byte* p = data;
  42375. byte content[] = "Test data to encode.";
  42376. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  42377. BIO* bio;
  42378. byte key[sizeof(client_key_der_2048)];
  42379. word32 keySz = (word32)sizeof(key);
  42380. byte* out = NULL;
  42381. #endif
  42382. AssertIntGT((len = CreatePKCS7SignedData(data, len, content,
  42383. (word32)sizeof(content),
  42384. 0, 0, 0, RSA_TYPE)), 0);
  42385. AssertNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
  42386. AssertNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  42387. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  42388. AssertIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  42389. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  42390. PKCS7_free(pkcs7);
  42391. /* fail case, without PKCS7_NOVERIFY */
  42392. p = data;
  42393. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  42394. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  42395. 0), WOLFSSL_FAILURE);
  42396. PKCS7_free(pkcs7);
  42397. /* success case, with PKCS7_NOVERIFY */
  42398. p = data;
  42399. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  42400. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  42401. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  42402. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  42403. /* test i2d */
  42404. XMEMCPY(key, client_key_der_2048, keySz);
  42405. pkcs7->privateKey = key;
  42406. pkcs7->privateKeySz = (word32)sizeof(key);
  42407. pkcs7->encryptOID = RSAk;
  42408. pkcs7->hashOID = SHAh;
  42409. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  42410. AssertIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  42411. AssertIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  42412. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42413. BIO_free(bio);
  42414. #endif
  42415. PKCS7_free(NULL);
  42416. PKCS7_free(pkcs7);
  42417. res = TEST_RES_CHECK(1);
  42418. #endif
  42419. return res;
  42420. }
  42421. static int test_wolfSSL_PKCS7_sign(void)
  42422. {
  42423. int res = TEST_SKIPPED;
  42424. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  42425. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  42426. PKCS7* p7 = NULL;
  42427. PKCS7* p7Ver = NULL;
  42428. byte* out = NULL;
  42429. byte* tmpPtr = NULL;
  42430. int outLen = 0;
  42431. int flags = 0;
  42432. byte data[] = "Test data to encode.";
  42433. const char* cert = "./certs/server-cert.pem";
  42434. const char* key = "./certs/server-key.pem";
  42435. const char* ca = "./certs/ca-cert.pem";
  42436. WOLFSSL_BIO* certBio = NULL;
  42437. WOLFSSL_BIO* keyBio = NULL;
  42438. WOLFSSL_BIO* caBio = NULL;
  42439. WOLFSSL_BIO* inBio = NULL;
  42440. X509* signCert = NULL;
  42441. EVP_PKEY* signKey = NULL;
  42442. X509* caCert = NULL;
  42443. X509_STORE* store = NULL;
  42444. /* read signer cert/key into BIO */
  42445. AssertNotNull(certBio = BIO_new_file(cert, "r"));
  42446. AssertNotNull(keyBio = BIO_new_file(key, "r"));
  42447. AssertNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  42448. AssertNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  42449. /* read CA cert into store (for verify) */
  42450. AssertNotNull(caBio = BIO_new_file(ca, "r"));
  42451. AssertNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  42452. AssertNotNull(store = X509_STORE_new());
  42453. AssertIntEQ(X509_STORE_add_cert(store, caCert), 1);
  42454. /* data to be signed into BIO */
  42455. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42456. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42457. /* PKCS7_sign, bad args: signer NULL */
  42458. AssertNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  42459. /* PKCS7_sign, bad args: signer key NULL */
  42460. AssertNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  42461. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  42462. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  42463. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  42464. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  42465. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  42466. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  42467. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  42468. {
  42469. flags = PKCS7_BINARY;
  42470. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42471. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  42472. /* verify with d2i_PKCS7 */
  42473. tmpPtr = out;
  42474. AssertNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  42475. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  42476. PKCS7_free(p7Ver);
  42477. /* verify with wc_PKCS7_VerifySignedData */
  42478. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  42479. AssertIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  42480. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  42481. /* compare the signer found to expected signer */
  42482. AssertIntNE(p7Ver->verifyCertSz, 0);
  42483. tmpPtr = NULL;
  42484. AssertIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  42485. AssertIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  42486. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  42487. tmpPtr = NULL;
  42488. wc_PKCS7_Free(p7Ver);
  42489. AssertNotNull(out);
  42490. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42491. out = NULL;
  42492. PKCS7_free(p7);
  42493. }
  42494. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  42495. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  42496. {
  42497. /* re-populate input BIO, may have been consumed */
  42498. BIO_free(inBio);
  42499. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42500. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42501. flags = PKCS7_BINARY | PKCS7_STREAM;
  42502. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42503. AssertIntEQ(PKCS7_final(p7, inBio, flags), 1);
  42504. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  42505. /* PKCS7_final, bad args: PKCS7 null */
  42506. AssertIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  42507. /* PKCS7_final, bad args: PKCS7 null */
  42508. AssertIntEQ(PKCS7_final(p7, NULL, 0), 0);
  42509. tmpPtr = out;
  42510. AssertNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  42511. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  42512. PKCS7_free(p7Ver);
  42513. AssertNotNull(out);
  42514. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42515. out = NULL;
  42516. PKCS7_free(p7);
  42517. }
  42518. /* TEST SUCCESS: Detached, not streaming, not MIME */
  42519. {
  42520. /* re-populate input BIO, may have been consumed */
  42521. BIO_free(inBio);
  42522. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42523. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42524. flags = PKCS7_BINARY | PKCS7_DETACHED;
  42525. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42526. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  42527. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  42528. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  42529. p7Ver->content = data;
  42530. p7Ver->contentSz = sizeof(data);
  42531. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  42532. wc_PKCS7_Free(p7Ver);
  42533. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  42534. * yet support detached content */
  42535. tmpPtr = out;
  42536. AssertNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  42537. PKCS7_free(p7Ver);
  42538. AssertNotNull(out);
  42539. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42540. out = NULL;
  42541. PKCS7_free(p7);
  42542. }
  42543. /* TEST SUCCESS: Detached, streaming, not MIME */
  42544. {
  42545. /* re-populate input BIO, may have been consumed */
  42546. BIO_free(inBio);
  42547. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42548. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42549. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  42550. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42551. AssertIntEQ(PKCS7_final(p7, inBio, flags), 1);
  42552. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  42553. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  42554. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  42555. p7Ver->content = data;
  42556. p7Ver->contentSz = sizeof(data);
  42557. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  42558. wc_PKCS7_Free(p7Ver);
  42559. AssertNotNull(out);
  42560. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42561. PKCS7_free(p7);
  42562. }
  42563. X509_STORE_free(store);
  42564. X509_free(caCert);
  42565. X509_free(signCert);
  42566. EVP_PKEY_free(signKey);
  42567. BIO_free(inBio);
  42568. BIO_free(keyBio);
  42569. BIO_free(certBio);
  42570. BIO_free(caBio);
  42571. res = TEST_RES_CHECK(1);
  42572. #endif
  42573. return res;
  42574. }
  42575. static int test_wolfSSL_PKCS7_SIGNED_new(void)
  42576. {
  42577. int res = TEST_SKIPPED;
  42578. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  42579. PKCS7_SIGNED* pkcs7;
  42580. pkcs7 = PKCS7_SIGNED_new();
  42581. AssertNotNull(pkcs7);
  42582. AssertIntEQ(pkcs7->contentOID, SIGNED_DATA);
  42583. PKCS7_SIGNED_free(pkcs7);
  42584. res = TEST_RES_CHECK(1);
  42585. #endif
  42586. return res;
  42587. }
  42588. #ifndef NO_BIO
  42589. static int test_wolfSSL_PEM_write_bio_PKCS7(void)
  42590. {
  42591. int res = TEST_SKIPPED;
  42592. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  42593. PKCS7* pkcs7 = NULL;
  42594. BIO* bio = NULL;
  42595. const byte* cert_buf = NULL;
  42596. int ret = 0;
  42597. WC_RNG rng;
  42598. const byte data[] = { /* Hello World */
  42599. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  42600. 0x72,0x6c,0x64
  42601. };
  42602. #ifndef NO_RSA
  42603. #if defined(USE_CERT_BUFFERS_2048)
  42604. byte key[sizeof(client_key_der_2048)];
  42605. byte cert[sizeof(client_cert_der_2048)];
  42606. word32 keySz = (word32)sizeof(key);
  42607. word32 certSz = (word32)sizeof(cert);
  42608. XMEMSET(key, 0, keySz);
  42609. XMEMSET(cert, 0, certSz);
  42610. XMEMCPY(key, client_key_der_2048, keySz);
  42611. XMEMCPY(cert, client_cert_der_2048, certSz);
  42612. #elif defined(USE_CERT_BUFFERS_1024)
  42613. byte key[sizeof_client_key_der_1024];
  42614. byte cert[sizeof(sizeof_client_cert_der_1024)];
  42615. word32 keySz = (word32)sizeof(key);
  42616. word32 certSz = (word32)sizeof(cert);
  42617. XMEMSET(key, 0, keySz);
  42618. XMEMSET(cert, 0, certSz);
  42619. XMEMCPY(key, client_key_der_1024, keySz);
  42620. XMEMCPY(cert, client_cert_der_1024, certSz);
  42621. #else
  42622. unsigned char cert[ONEK_BUF];
  42623. unsigned char key[ONEK_BUF];
  42624. XFILE fp;
  42625. int certSz;
  42626. int keySz;
  42627. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  42628. AssertTrue((fp != XBADFILE));
  42629. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  42630. XFCLOSE(fp);
  42631. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  42632. AssertTrue(fp != XBADFILE);
  42633. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  42634. XFCLOSE(fp);
  42635. #endif
  42636. #elif defined(HAVE_ECC)
  42637. #if defined(USE_CERT_BUFFERS_256)
  42638. unsigned char cert[sizeof(cliecc_cert_der_256)];
  42639. unsigned char key[sizeof(ecc_clikey_der_256)];
  42640. int certSz = (int)sizeof(cert);
  42641. int keySz = (int)sizeof(key);
  42642. XMEMSET(cert, 0, certSz);
  42643. XMEMSET(key, 0, keySz);
  42644. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  42645. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  42646. #else
  42647. unsigned char cert[ONEK_BUF];
  42648. unsigned char key[ONEK_BUF];
  42649. XFILE fp;
  42650. int certSz, keySz;
  42651. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  42652. AssertTrue(fp != XBADFILE);
  42653. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  42654. XFCLOSE(fp);
  42655. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  42656. AssertTrue(fp != XBADFILE);
  42657. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  42658. XFCLOSE(fp);
  42659. #endif
  42660. #else
  42661. #error PKCS7 requires ECC or RSA
  42662. #endif
  42663. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  42664. /* initialize with DER encoded cert */
  42665. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  42666. /* init rng */
  42667. AssertIntEQ(wc_InitRng(&rng), 0);
  42668. pkcs7->rng = &rng;
  42669. pkcs7->content = (byte*)data; /* not used for ex */
  42670. pkcs7->contentSz = (word32)sizeof(data);
  42671. pkcs7->contentOID = SIGNED_DATA;
  42672. pkcs7->privateKey = key;
  42673. pkcs7->privateKeySz = (word32)sizeof(key);
  42674. pkcs7->encryptOID = RSAk;
  42675. pkcs7->hashOID = SHAh;
  42676. pkcs7->signedAttribs = NULL;
  42677. pkcs7->signedAttribsSz = 0;
  42678. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  42679. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  42680. AssertIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  42681. /* Read PKCS#7 PEM from BIO */
  42682. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  42683. AssertIntGE(ret, 0);
  42684. BIO_free(bio);
  42685. wc_PKCS7_Free(pkcs7);
  42686. wc_FreeRng(&rng);
  42687. res = TEST_RES_CHECK(1);
  42688. #endif
  42689. return res;
  42690. }
  42691. #ifdef HAVE_SMIME
  42692. static int test_wolfSSL_SMIME_read_PKCS7(void)
  42693. {
  42694. int res = TEST_SKIPPED;
  42695. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  42696. !defined(NO_RSA)
  42697. PKCS7* pkcs7 = NULL;
  42698. BIO* bio = NULL;
  42699. BIO* bcont = NULL;
  42700. BIO* out = NULL;
  42701. const byte* outBuf = NULL;
  42702. int outBufLen = 0;
  42703. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  42704. XFILE smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r");
  42705. /* smime-test.p7s */
  42706. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  42707. AssertNotNull(bio);
  42708. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  42709. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  42710. AssertNotNull(pkcs7);
  42711. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  42712. PKCS7_NOVERIFY), SSL_SUCCESS);
  42713. XFCLOSE(smimeTestFile);
  42714. if (bcont) BIO_free(bcont);
  42715. wolfSSL_PKCS7_free(pkcs7);
  42716. /* smime-test-multipart.p7s */
  42717. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  42718. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  42719. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  42720. AssertNotNull(pkcs7);
  42721. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  42722. PKCS7_NOVERIFY), SSL_SUCCESS);
  42723. XFCLOSE(smimeTestFile);
  42724. if (bcont) BIO_free(bcont);
  42725. wolfSSL_PKCS7_free(pkcs7);
  42726. /* smime-test-multipart-badsig.p7s */
  42727. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  42728. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  42729. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  42730. AssertNull(pkcs7);
  42731. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  42732. PKCS7_NOVERIFY), SSL_FAILURE);
  42733. XFCLOSE(smimeTestFile);
  42734. if (bcont) BIO_free(bcont);
  42735. wolfSSL_PKCS7_free(pkcs7);
  42736. /* smime-test-canon.p7s */
  42737. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  42738. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  42739. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  42740. AssertNotNull(pkcs7);
  42741. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  42742. PKCS7_NOVERIFY), SSL_SUCCESS);
  42743. XFCLOSE(smimeTestFile);
  42744. if (bcont) BIO_free(bcont);
  42745. wolfSSL_PKCS7_free(pkcs7);
  42746. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  42747. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  42748. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  42749. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  42750. AssertNotNull(pkcs7);
  42751. out = wolfSSL_BIO_new(BIO_s_mem());
  42752. AssertNotNull(out);
  42753. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  42754. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  42755. AssertIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  42756. /* Content-Type should not show up at beginning of output buffer */
  42757. AssertIntGT(outBufLen, XSTRLEN(contTypeText));
  42758. AssertIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  42759. BIO_free(out);
  42760. BIO_free(bio);
  42761. if (bcont) BIO_free(bcont);
  42762. wolfSSL_PKCS7_free(pkcs7);
  42763. res = TEST_RES_CHECK(1);
  42764. #endif
  42765. return res;
  42766. }
  42767. static int test_wolfSSL_SMIME_write_PKCS7(void)
  42768. {
  42769. int res = TEST_SKIPPED;
  42770. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  42771. PKCS7* p7 = NULL;
  42772. PKCS7* p7Ver = NULL;
  42773. int flags = 0;
  42774. byte data[] = "Test data to encode.";
  42775. const char* cert = "./certs/server-cert.pem";
  42776. const char* key = "./certs/server-key.pem";
  42777. const char* ca = "./certs/ca-cert.pem";
  42778. WOLFSSL_BIO* certBio = NULL;
  42779. WOLFSSL_BIO* keyBio = NULL;
  42780. WOLFSSL_BIO* caBio = NULL;
  42781. WOLFSSL_BIO* inBio = NULL;
  42782. WOLFSSL_BIO* outBio = NULL;
  42783. WOLFSSL_BIO* content = NULL;
  42784. X509* signCert = NULL;
  42785. EVP_PKEY* signKey = NULL;
  42786. X509* caCert = NULL;
  42787. X509_STORE* store = NULL;
  42788. /* read signer cert/key into BIO */
  42789. AssertNotNull(certBio = BIO_new_file(cert, "r"));
  42790. AssertNotNull(keyBio = BIO_new_file(key, "r"));
  42791. AssertNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  42792. AssertNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  42793. /* read CA cert into store (for verify) */
  42794. AssertNotNull(caBio = BIO_new_file(ca, "r"));
  42795. AssertNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  42796. AssertNotNull(store = X509_STORE_new());
  42797. AssertIntEQ(X509_STORE_add_cert(store, caCert), 1);
  42798. /* generate and verify SMIME: not detached */
  42799. {
  42800. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42801. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42802. flags = PKCS7_STREAM;
  42803. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42804. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  42805. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  42806. /* bad arg: out NULL */
  42807. AssertIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  42808. /* bad arg: pkcs7 NULL */
  42809. AssertIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  42810. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  42811. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  42812. BIO_free(content);
  42813. BIO_free(inBio);
  42814. BIO_free(outBio);
  42815. PKCS7_free(p7Ver);
  42816. PKCS7_free(p7);
  42817. }
  42818. /* generate and verify SMIME: not detached, add Content-Type */
  42819. {
  42820. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42821. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42822. flags = PKCS7_STREAM | PKCS7_TEXT;
  42823. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42824. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  42825. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  42826. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  42827. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  42828. BIO_free(content);
  42829. BIO_free(inBio);
  42830. BIO_free(outBio);
  42831. PKCS7_free(p7Ver);
  42832. PKCS7_free(p7);
  42833. }
  42834. /* generate and verify SMIME: detached */
  42835. {
  42836. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42837. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42838. flags = PKCS7_DETACHED | PKCS7_STREAM;
  42839. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42840. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  42841. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  42842. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  42843. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  42844. BIO_free(content);
  42845. BIO_free(inBio);
  42846. BIO_free(outBio);
  42847. PKCS7_free(p7Ver);
  42848. PKCS7_free(p7);
  42849. }
  42850. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  42851. {
  42852. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42853. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42854. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  42855. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42856. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  42857. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  42858. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  42859. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  42860. BIO_free(content);
  42861. BIO_free(inBio);
  42862. BIO_free(outBio);
  42863. PKCS7_free(p7Ver);
  42864. PKCS7_free(p7);
  42865. }
  42866. X509_STORE_free(store);
  42867. X509_free(caCert);
  42868. X509_free(signCert);
  42869. EVP_PKEY_free(signKey);
  42870. BIO_free(keyBio);
  42871. BIO_free(certBio);
  42872. BIO_free(caBio);
  42873. res = TEST_RES_CHECK(1);
  42874. #endif
  42875. return res;
  42876. }
  42877. #endif /* HAVE_SMIME */
  42878. #endif /* !NO_BIO */
  42879. /* Test of X509 store use outside of SSL context w/ CRL lookup (ALWAYS
  42880. * returns 0) */
  42881. static int test_X509_STORE_No_SSL_CTX(void)
  42882. {
  42883. int res = TEST_SKIPPED;
  42884. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  42885. !defined(NO_WOLFSSL_DIR) && defined(HAVE_CRL) && \
  42886. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  42887. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  42888. X509_STORE * store;
  42889. X509_STORE_CTX * storeCtx;
  42890. X509_CRL * crl;
  42891. X509 * ca;
  42892. X509 * cert;
  42893. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  42894. const char srvCert[] = "./certs/server-cert.pem";
  42895. const char caCert[] = "./certs/ca-cert.pem";
  42896. const char caDir[] = "./certs/crl/hash_pem";
  42897. XFILE fp;
  42898. X509_LOOKUP * lookup;
  42899. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  42900. /* Set up store with CA */
  42901. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  42902. SSL_FILETYPE_PEM)));
  42903. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  42904. /* Add CRL lookup directory to store
  42905. * NOTE: test uses ./certs/crl/hash_pem/0fdb2da4.r0, which is a copy
  42906. * of crl.pem */
  42907. AssertNotNull((lookup = X509_STORE_add_lookup(store,
  42908. X509_LOOKUP_hash_dir())));
  42909. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, caDir,
  42910. X509_FILETYPE_PEM, NULL), SSL_SUCCESS);
  42911. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  42912. SSL_SUCCESS);
  42913. /* Add CRL to store NOT containing the verified certificate, which
  42914. * forces use of the CRL lookup directory */
  42915. fp = XFOPEN(cliCrlPem, "rb");
  42916. AssertTrue((fp != XBADFILE));
  42917. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  42918. NULL, NULL));
  42919. XFCLOSE(fp);
  42920. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  42921. /* Create verification context outside of an SSL session */
  42922. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  42923. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  42924. SSL_FILETYPE_PEM)));
  42925. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  42926. /* Perform verification, which should NOT indicate CRL missing due to the
  42927. * store CM's X509 store pointer being NULL */
  42928. AssertIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  42929. X509_CRL_free(crl);
  42930. X509_STORE_free(store);
  42931. X509_STORE_CTX_free(storeCtx);
  42932. X509_free(cert);
  42933. X509_free(ca);
  42934. res = TEST_RES_CHECK(1);
  42935. #endif
  42936. return res;
  42937. }
  42938. /* Test of X509 store use outside of SSL context w/ CRL lookup, but
  42939. * with X509_LOOKUP_add_dir and X509_FILETYPE_ASN1. */
  42940. static int test_X509_LOOKUP_add_dir(void)
  42941. {
  42942. int res = TEST_SKIPPED;
  42943. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  42944. !defined(NO_WOLFSSL_DIR) && defined(HAVE_CRL) && \
  42945. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  42946. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  42947. X509_STORE * store;
  42948. X509_STORE_CTX * storeCtx;
  42949. X509_CRL * crl;
  42950. X509 * ca;
  42951. X509 * cert;
  42952. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  42953. const char srvCert[] = "./certs/server-cert.pem";
  42954. const char caCert[] = "./certs/ca-cert.pem";
  42955. const char caDir[] = "./certs/crl/hash_der";
  42956. XFILE fp;
  42957. X509_LOOKUP * lookup;
  42958. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  42959. /* Set up store with CA */
  42960. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  42961. SSL_FILETYPE_PEM)));
  42962. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  42963. /* Add CRL lookup directory to store.
  42964. * Test uses ./certs/crl/hash_der/0fdb2da4.r0, which is a copy
  42965. * of crl.der */
  42966. AssertNotNull((lookup = X509_STORE_add_lookup(store,
  42967. X509_LOOKUP_hash_dir())));
  42968. AssertIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_ASN1),
  42969. SSL_SUCCESS);
  42970. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  42971. SSL_SUCCESS);
  42972. /* Add CRL to store NOT containing the verified certificate, which
  42973. * forces use of the CRL lookup directory */
  42974. fp = XFOPEN(cliCrlPem, "rb");
  42975. AssertTrue((fp != XBADFILE));
  42976. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  42977. NULL, NULL));
  42978. XFCLOSE(fp);
  42979. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  42980. /* Create verification context outside of an SSL session */
  42981. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  42982. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  42983. SSL_FILETYPE_PEM)));
  42984. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  42985. /* Perform verification, which should NOT return CRL missing */
  42986. AssertIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  42987. X509_CRL_free(crl);
  42988. X509_STORE_free(store);
  42989. X509_STORE_CTX_free(storeCtx);
  42990. X509_free(cert);
  42991. X509_free(ca);
  42992. /* Now repeat the same, but look for X509_FILETYPE_PEM.
  42993. * We should get CRL_MISSING at the end, because the lookup
  42994. * dir has only ASN1 CRLs. */
  42995. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  42996. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  42997. SSL_FILETYPE_PEM)));
  42998. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  42999. AssertNotNull((lookup = X509_STORE_add_lookup(store,
  43000. X509_LOOKUP_hash_dir())));
  43001. AssertIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_PEM),
  43002. SSL_SUCCESS);
  43003. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  43004. SSL_SUCCESS);
  43005. fp = XFOPEN(cliCrlPem, "rb");
  43006. AssertTrue((fp != XBADFILE));
  43007. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  43008. NULL, NULL));
  43009. XFCLOSE(fp);
  43010. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  43011. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  43012. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  43013. SSL_FILETYPE_PEM)));
  43014. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  43015. /* Now we SHOULD get CRL_MISSING, because we looked for PEM
  43016. * in dir containing only ASN1/DER. */
  43017. AssertIntEQ(X509_verify_cert(storeCtx), CRL_MISSING);
  43018. X509_CRL_free(crl);
  43019. X509_STORE_free(store);
  43020. X509_STORE_CTX_free(storeCtx);
  43021. X509_free(cert);
  43022. X509_free(ca);
  43023. res = TEST_RES_CHECK(1);
  43024. #endif
  43025. return res;
  43026. }
  43027. /*----------------------------------------------------------------------------*
  43028. | Certificate Failure Checks
  43029. *----------------------------------------------------------------------------*/
  43030. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  43031. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  43032. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  43033. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  43034. int type)
  43035. {
  43036. int ret;
  43037. WOLFSSL_CERT_MANAGER* cm = NULL;
  43038. switch (type) {
  43039. case TESTING_RSA:
  43040. #ifdef NO_RSA
  43041. fprintf(stderr, "RSA disabled, skipping test\n");
  43042. return ASN_SIG_CONFIRM_E;
  43043. #else
  43044. break;
  43045. #endif
  43046. case TESTING_ECC:
  43047. #ifndef HAVE_ECC
  43048. fprintf(stderr, "ECC disabled, skipping test\n");
  43049. return ASN_SIG_CONFIRM_E;
  43050. #else
  43051. break;
  43052. #endif
  43053. default:
  43054. fprintf(stderr, "Bad function argument\n");
  43055. return BAD_FUNC_ARG;
  43056. }
  43057. cm = wolfSSL_CertManagerNew();
  43058. if (cm == NULL) {
  43059. fprintf(stderr, "wolfSSL_CertManagerNew failed\n");
  43060. return -1;
  43061. }
  43062. #ifndef NO_FILESYSTEM
  43063. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  43064. if (ret != WOLFSSL_SUCCESS) {
  43065. fprintf(stderr, "wolfSSL_CertManagerLoadCA failed\n");
  43066. wolfSSL_CertManagerFree(cm);
  43067. return ret;
  43068. }
  43069. #else
  43070. (void)ca;
  43071. #endif
  43072. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz, WOLFSSL_FILETYPE_ASN1);
  43073. /* Let AssertIntEQ handle return code */
  43074. wolfSSL_CertManagerFree(cm);
  43075. return ret;
  43076. }
  43077. #if !defined(NO_FILESYSTEM)
  43078. static int test_RsaSigFailure_cm(void)
  43079. {
  43080. int ret = 0;
  43081. const char* ca_cert = "./certs/ca-cert.pem";
  43082. const char* server_cert = "./certs/server-cert.der";
  43083. byte* cert_buf = NULL;
  43084. size_t cert_sz = 0;
  43085. ret = load_file(server_cert, &cert_buf, &cert_sz);
  43086. if (ret == 0) {
  43087. /* corrupt DER - invert last byte, which is signature */
  43088. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  43089. /* test bad cert */
  43090. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA);
  43091. }
  43092. if (cert_buf)
  43093. free(cert_buf);
  43094. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  43095. if (ret == WOLFSSL_FATAL_ERROR) {
  43096. ret = 0;
  43097. }
  43098. #else
  43099. if (ret == ASN_SIG_CONFIRM_E) {
  43100. ret = 0;
  43101. }
  43102. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  43103. return TEST_RES_CHECK(ret == 0);
  43104. }
  43105. static int test_EccSigFailure_cm(void)
  43106. {
  43107. int ret = 0;
  43108. /* self-signed ECC cert, so use server cert as CA */
  43109. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  43110. const char* server_cert = "./certs/server-ecc.der";
  43111. byte* cert_buf = NULL;
  43112. size_t cert_sz = 0;
  43113. ret = load_file(server_cert, &cert_buf, &cert_sz);
  43114. if (ret == 0) {
  43115. /* corrupt DER - invert last byte, which is signature */
  43116. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  43117. /* test bad cert */
  43118. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC);
  43119. }
  43120. if (cert_buf)
  43121. free(cert_buf);
  43122. #ifdef FP_ECC
  43123. wc_ecc_fp_free();
  43124. #endif
  43125. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  43126. if (ret == WOLFSSL_FATAL_ERROR) {
  43127. ret = 0;
  43128. }
  43129. #else
  43130. if (ret == ASN_SIG_CONFIRM_E) {
  43131. ret = 0;
  43132. }
  43133. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  43134. return TEST_RES_CHECK(ret == 0);
  43135. }
  43136. #endif /* !NO_FILESYSTEM */
  43137. #endif /* NO_CERTS */
  43138. #ifdef WOLFSSL_TLS13
  43139. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  43140. #ifdef WC_SHA384_DIGEST_SIZE
  43141. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  43142. #else
  43143. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  43144. #endif
  43145. #endif
  43146. #ifdef WOLFSSL_EARLY_DATA
  43147. static const char earlyData[] = "Early Data";
  43148. static char earlyDataBuffer[1];
  43149. #endif
  43150. static int test_tls13_apis(void)
  43151. {
  43152. int ret = 0;
  43153. #ifndef WOLFSSL_NO_TLS12
  43154. #ifndef NO_WOLFSSL_CLIENT
  43155. WOLFSSL_CTX* clientTls12Ctx;
  43156. WOLFSSL* clientTls12Ssl;
  43157. #endif
  43158. #ifndef NO_WOLFSSL_SERVER
  43159. WOLFSSL_CTX* serverTls12Ctx;
  43160. WOLFSSL* serverTls12Ssl;
  43161. #endif
  43162. #endif
  43163. #ifndef NO_WOLFSSL_CLIENT
  43164. WOLFSSL_CTX* clientCtx;
  43165. WOLFSSL* clientSsl;
  43166. #endif
  43167. #ifndef NO_WOLFSSL_SERVER
  43168. WOLFSSL_CTX* serverCtx;
  43169. WOLFSSL* serverSsl;
  43170. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  43171. const char* ourCert = svrCertFile;
  43172. const char* ourKey = svrKeyFile;
  43173. #endif
  43174. #endif
  43175. int required;
  43176. #ifdef WOLFSSL_EARLY_DATA
  43177. int outSz;
  43178. #endif
  43179. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  43180. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  43181. #ifdef HAVE_PQC
  43182. WOLFSSL_KYBER_LEVEL1
  43183. #else
  43184. WOLFSSL_ECC_SECP256R1
  43185. #endif
  43186. };
  43187. #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
  43188. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  43189. #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
  43190. int numGroups = 2;
  43191. #endif
  43192. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  43193. char groupList[] =
  43194. #ifndef NO_ECC_SECP
  43195. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  43196. "P-521:"
  43197. #endif
  43198. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  43199. "P-384:"
  43200. #endif
  43201. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  43202. "P-256"
  43203. #ifdef HAVE_PQC
  43204. ":P256_KYBER_LEVEL1"
  43205. #endif
  43206. #endif
  43207. #ifdef HAVE_PQC
  43208. ":KYBER_LEVEL1"
  43209. #endif
  43210. "";
  43211. #endif /* !defined(NO_ECC_SECP) */
  43212. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  43213. (void)ret;
  43214. #ifndef WOLFSSL_NO_TLS12
  43215. #ifndef NO_WOLFSSL_CLIENT
  43216. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  43217. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  43218. #endif
  43219. #ifndef NO_WOLFSSL_SERVER
  43220. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  43221. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  43222. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  43223. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey, WOLFSSL_FILETYPE_PEM);
  43224. #endif
  43225. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  43226. #endif
  43227. #endif
  43228. #ifndef NO_WOLFSSL_CLIENT
  43229. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  43230. clientSsl = wolfSSL_new(clientCtx);
  43231. #endif
  43232. #ifndef NO_WOLFSSL_SERVER
  43233. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  43234. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  43235. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  43236. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  43237. #endif
  43238. serverSsl = wolfSSL_new(serverCtx);
  43239. #endif
  43240. #ifdef WOLFSSL_SEND_HRR_COOKIE
  43241. AssertIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  43242. #ifndef NO_WOLFSSL_CLIENT
  43243. AssertIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  43244. #endif
  43245. #ifndef NO_WOLFSSL_SERVER
  43246. #ifndef WOLFSSL_NO_TLS12
  43247. AssertIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0), BAD_FUNC_ARG);
  43248. #endif
  43249. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  43250. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  43251. WOLFSSL_SUCCESS);
  43252. #endif
  43253. #endif
  43254. #ifdef HAVE_SUPPORTED_CURVES
  43255. #ifdef HAVE_ECC
  43256. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  43257. #ifndef NO_WOLFSSL_SERVER
  43258. do {
  43259. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  43260. #ifdef WOLFSSL_ASYNC_CRYPT
  43261. if (ret == WC_PENDING_E)
  43262. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  43263. #endif
  43264. } while (ret == WC_PENDING_E);
  43265. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  43266. #endif
  43267. #ifndef NO_WOLFSSL_CLIENT
  43268. #ifndef WOLFSSL_NO_TLS12
  43269. do {
  43270. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  43271. #ifdef WOLFSSL_ASYNC_CRYPT
  43272. if (ret == WC_PENDING_E)
  43273. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  43274. #endif
  43275. } while (ret == WC_PENDING_E);
  43276. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  43277. #endif
  43278. do {
  43279. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  43280. #ifdef WOLFSSL_ASYNC_CRYPT
  43281. if (ret == WC_PENDING_E)
  43282. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  43283. #endif
  43284. } while (ret == WC_PENDING_E);
  43285. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  43286. #endif
  43287. #elif defined(HAVE_CURVE25519)
  43288. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  43289. #ifndef NO_WOLFSSL_SERVER
  43290. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  43291. WOLFSSL_SUCCESS);
  43292. #endif
  43293. #ifndef NO_WOLFSSL_CLIENT
  43294. #ifndef WOLFSSL_NO_TLS12
  43295. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  43296. WOLFSSL_SUCCESS);
  43297. #endif
  43298. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  43299. WOLFSSL_SUCCESS);
  43300. #endif
  43301. #elif defined(HAVE_CURVE448)
  43302. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  43303. #ifndef NO_WOLFSSL_SERVER
  43304. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  43305. WOLFSSL_SUCCESS);
  43306. #endif
  43307. #ifndef NO_WOLFSSL_CLIENT
  43308. #ifndef WOLFSSL_NO_TLS12
  43309. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  43310. WOLFSSL_SUCCESS);
  43311. #endif
  43312. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  43313. WOLFSSL_SUCCESS);
  43314. #endif
  43315. #else
  43316. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  43317. #ifndef NO_WOLFSSL_CLIENT
  43318. #ifndef WOLFSSL_NO_TLS12
  43319. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  43320. NOT_COMPILED_IN);
  43321. #endif
  43322. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  43323. NOT_COMPILED_IN);
  43324. #endif
  43325. #endif
  43326. #if defined(HAVE_PQC)
  43327. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  43328. #ifndef NO_WOLFSSL_SERVER
  43329. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  43330. WOLFSSL_SUCCESS);
  43331. #endif
  43332. #ifndef NO_WOLFSSL_CLIENT
  43333. #ifndef WOLFSSL_NO_TLS12
  43334. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  43335. BAD_FUNC_ARG);
  43336. #endif
  43337. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  43338. WOLFSSL_SUCCESS);
  43339. #endif
  43340. #endif
  43341. AssertIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  43342. #ifndef NO_WOLFSSL_SERVER
  43343. AssertIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  43344. #endif
  43345. #ifndef NO_WOLFSSL_CLIENT
  43346. #ifndef WOLFSSL_NO_TLS12
  43347. AssertIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  43348. #endif
  43349. AssertIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  43350. #endif
  43351. #endif /* HAVE_SUPPORTED_CURVES */
  43352. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  43353. #ifndef NO_WOLFSSL_CLIENT
  43354. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  43355. #endif
  43356. #ifndef NO_WOLFSSL_SERVER
  43357. #ifndef WOLFSSL_NO_TLS12
  43358. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  43359. #endif
  43360. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  43361. #endif
  43362. AssertIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  43363. #ifndef NO_WOLFSSL_CLIENT
  43364. AssertIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  43365. #endif
  43366. #ifndef NO_WOLFSSL_SERVER
  43367. #ifndef WOLFSSL_NO_TLS12
  43368. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  43369. #endif
  43370. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  43371. #endif
  43372. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  43373. #ifndef NO_WOLFSSL_CLIENT
  43374. #ifndef WOLFSSL_NO_TLS12
  43375. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  43376. #endif
  43377. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  43378. #endif
  43379. #ifndef NO_WOLFSSL_SERVER
  43380. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  43381. #endif
  43382. AssertIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  43383. #ifndef NO_WOLFSSL_CLIENT
  43384. #ifndef WOLFSSL_NO_TLS12
  43385. AssertIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  43386. #endif
  43387. AssertIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  43388. #endif
  43389. #ifndef NO_WOLFSSL_SERVER
  43390. AssertIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  43391. #endif
  43392. AssertIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  43393. #ifndef NO_WOLFSSL_CLIENT
  43394. #ifndef WOLFSSL_NO_TLS12
  43395. AssertIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  43396. #endif
  43397. AssertIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  43398. #endif
  43399. #ifndef NO_WOLFSSL_SERVER
  43400. AssertIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  43401. #endif
  43402. AssertIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  43403. AssertIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  43404. #ifndef NO_WOLFSSL_CLIENT
  43405. #ifndef WOLFSSL_NO_TLS12
  43406. AssertIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  43407. BAD_FUNC_ARG);
  43408. #endif
  43409. AssertIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  43410. #endif
  43411. #ifndef NO_WOLFSSL_SERVER
  43412. AssertIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  43413. #endif
  43414. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  43415. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  43416. #ifndef NO_WOLFSSL_SERVER
  43417. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  43418. #endif
  43419. #ifndef NO_WOLFSSL_CLIENT
  43420. #ifndef WOLFSSL_NO_TLS12
  43421. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  43422. BAD_FUNC_ARG);
  43423. #endif
  43424. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  43425. #endif
  43426. AssertIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  43427. #ifndef NO_WOLFSSL_SERVER
  43428. AssertIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  43429. #endif
  43430. #ifndef NO_WOLFSSL_CLIENT
  43431. #ifndef WOLFSSL_NO_TLS12
  43432. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  43433. BAD_FUNC_ARG);
  43434. #endif
  43435. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  43436. #endif
  43437. AssertIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  43438. #ifndef NO_WOLFSSL_CLIENT
  43439. AssertIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  43440. #endif
  43441. #ifndef NO_WOLFSSL_SERVER
  43442. #ifndef WOLFSSL_NO_TLS12
  43443. AssertIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  43444. BAD_FUNC_ARG);
  43445. #endif
  43446. AssertIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  43447. #endif
  43448. #endif
  43449. #ifdef HAVE_ECC
  43450. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  43451. AssertIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  43452. #ifndef NO_WOLFSSL_SERVER
  43453. AssertIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  43454. #endif
  43455. #ifndef NO_WOLFSSL_CLIENT
  43456. #ifndef WOLFSSL_NO_TLS12
  43457. AssertIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  43458. #endif
  43459. AssertIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  43460. #endif
  43461. #endif
  43462. #ifdef HAVE_SUPPORTED_CURVES
  43463. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  43464. #ifndef NO_WOLFSSL_CLIENT
  43465. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  43466. #endif
  43467. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  43468. #ifndef NO_WOLFSSL_CLIENT
  43469. #ifndef WOLFSSL_NO_TLS12
  43470. AssertIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  43471. BAD_FUNC_ARG);
  43472. #endif
  43473. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  43474. WOLFSSL_MAX_GROUP_COUNT + 1),
  43475. BAD_FUNC_ARG);
  43476. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  43477. WOLFSSL_SUCCESS);
  43478. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  43479. BAD_FUNC_ARG);
  43480. #endif
  43481. #ifndef NO_WOLFSSL_SERVER
  43482. AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  43483. WOLFSSL_SUCCESS);
  43484. AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  43485. BAD_FUNC_ARG);
  43486. #endif
  43487. AssertIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  43488. #ifndef NO_WOLFSSL_CLIENT
  43489. AssertIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  43490. #endif
  43491. AssertIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  43492. #ifndef NO_WOLFSSL_CLIENT
  43493. #ifndef WOLFSSL_NO_TLS12
  43494. AssertIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  43495. BAD_FUNC_ARG);
  43496. #endif
  43497. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups,
  43498. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  43499. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  43500. WOLFSSL_SUCCESS);
  43501. AssertIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  43502. BAD_FUNC_ARG);
  43503. #endif
  43504. #ifndef NO_WOLFSSL_SERVER
  43505. AssertIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  43506. WOLFSSL_SUCCESS);
  43507. AssertIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  43508. BAD_FUNC_ARG);
  43509. #endif
  43510. #ifdef OPENSSL_EXTRA
  43511. AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  43512. #ifndef NO_WOLFSSL_CLIENT
  43513. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL), WOLFSSL_FAILURE);
  43514. #endif
  43515. AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  43516. #ifndef NO_WOLFSSL_CLIENT
  43517. #ifndef WOLFSSL_NO_TLS12
  43518. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  43519. WOLFSSL_FAILURE);
  43520. #endif
  43521. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  43522. WOLFSSL_SUCCESS);
  43523. #endif
  43524. #ifndef NO_WOLFSSL_SERVER
  43525. AssertIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  43526. WOLFSSL_SUCCESS);
  43527. #endif
  43528. AssertIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  43529. #ifndef NO_WOLFSSL_CLIENT
  43530. AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  43531. #endif
  43532. AssertIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  43533. #ifndef NO_WOLFSSL_CLIENT
  43534. #ifndef WOLFSSL_NO_TLS12
  43535. AssertIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  43536. WOLFSSL_FAILURE);
  43537. #endif
  43538. AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  43539. WOLFSSL_SUCCESS);
  43540. #endif
  43541. #ifndef NO_WOLFSSL_SERVER
  43542. AssertIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  43543. WOLFSSL_SUCCESS);
  43544. #endif
  43545. #endif /* OPENSSL_EXTRA */
  43546. #endif /* HAVE_SUPPORTED_CURVES */
  43547. #endif /* HAVE_ECC */
  43548. #ifdef WOLFSSL_EARLY_DATA
  43549. #ifndef OPENSSL_EXTRA
  43550. AssertIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  43551. AssertIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  43552. #else
  43553. AssertIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  43554. AssertIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  43555. #endif
  43556. #ifndef NO_WOLFSSL_CLIENT
  43557. #ifndef OPENSSL_EXTRA
  43558. AssertIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  43559. AssertIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  43560. #else
  43561. AssertIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  43562. AssertIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  43563. #endif
  43564. #endif
  43565. #ifndef NO_WOLFSSL_SERVER
  43566. #ifndef WOLFSSL_NO_TLS12
  43567. #ifndef OPENSSL_EXTRA
  43568. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  43569. BAD_FUNC_ARG);
  43570. AssertIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  43571. #else
  43572. AssertIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  43573. BAD_FUNC_ARG);
  43574. AssertIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  43575. #endif
  43576. #endif
  43577. #ifndef OPENSSL_EXTRA
  43578. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  43579. AssertIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  43580. #else
  43581. AssertIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  43582. AssertIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  43583. #endif
  43584. #endif
  43585. #ifndef OPENSSL_EXTRA
  43586. AssertIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  43587. AssertIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  43588. #else
  43589. AssertIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  43590. AssertIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  43591. #endif
  43592. #ifndef NO_WOLFSSL_CLIENT
  43593. #ifndef OPENSSL_EXTRA
  43594. AssertIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), 0);
  43595. AssertIntEQ(wolfSSL_get_max_early_data(clientSsl), 17);
  43596. #else
  43597. AssertIntEQ(SSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  43598. AssertIntEQ(SSL_get_max_early_data(clientSsl), 17);
  43599. #endif
  43600. #endif
  43601. #ifndef NO_WOLFSSL_SERVER
  43602. #ifndef WOLFSSL_NO_TLS12
  43603. #ifndef OPENSSL_EXTRA
  43604. AssertIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  43605. AssertIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  43606. #else
  43607. AssertIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  43608. AssertIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  43609. #endif
  43610. #endif
  43611. #ifndef OPENSSL_EXTRA
  43612. AssertIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  43613. AssertIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  43614. #else
  43615. AssertIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  43616. AssertIntEQ(SSL_get_max_early_data(serverSsl), 16);
  43617. #endif
  43618. #endif
  43619. AssertIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  43620. &outSz), BAD_FUNC_ARG);
  43621. #ifndef NO_WOLFSSL_CLIENT
  43622. AssertIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  43623. &outSz), BAD_FUNC_ARG);
  43624. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  43625. BAD_FUNC_ARG);
  43626. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  43627. sizeof(earlyData), NULL),
  43628. BAD_FUNC_ARG);
  43629. #endif
  43630. #ifndef NO_WOLFSSL_SERVER
  43631. AssertIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  43632. sizeof(earlyData), &outSz),
  43633. SIDE_ERROR);
  43634. #endif
  43635. #ifndef NO_WOLFSSL_CLIENT
  43636. #ifndef WOLFSSL_NO_TLS12
  43637. AssertIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  43638. sizeof(earlyData), &outSz),
  43639. BAD_FUNC_ARG);
  43640. #endif
  43641. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  43642. sizeof(earlyData), &outSz),
  43643. WOLFSSL_FATAL_ERROR);
  43644. #endif
  43645. AssertIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  43646. sizeof(earlyDataBuffer), &outSz),
  43647. BAD_FUNC_ARG);
  43648. #ifndef NO_WOLFSSL_SERVER
  43649. AssertIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  43650. sizeof(earlyDataBuffer), &outSz),
  43651. BAD_FUNC_ARG);
  43652. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1, &outSz),
  43653. BAD_FUNC_ARG);
  43654. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  43655. sizeof(earlyDataBuffer), NULL),
  43656. BAD_FUNC_ARG);
  43657. #endif
  43658. #ifndef NO_WOLFSSL_CLIENT
  43659. AssertIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  43660. sizeof(earlyDataBuffer), &outSz),
  43661. SIDE_ERROR);
  43662. #endif
  43663. #ifndef NO_WOLFSSL_SERVER
  43664. #ifndef WOLFSSL_NO_TLS12
  43665. AssertIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  43666. sizeof(earlyDataBuffer), &outSz),
  43667. BAD_FUNC_ARG);
  43668. #endif
  43669. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  43670. sizeof(earlyDataBuffer), &outSz),
  43671. WOLFSSL_FATAL_ERROR);
  43672. #endif
  43673. #endif
  43674. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  43675. AssertIntLT(SSL_get_early_data_status(NULL), 0);
  43676. #endif
  43677. #ifndef NO_WOLFSSL_SERVER
  43678. wolfSSL_free(serverSsl);
  43679. wolfSSL_CTX_free(serverCtx);
  43680. #endif
  43681. #ifndef NO_WOLFSSL_CLIENT
  43682. wolfSSL_free(clientSsl);
  43683. wolfSSL_CTX_free(clientCtx);
  43684. #endif
  43685. #ifndef WOLFSSL_NO_TLS12
  43686. #ifndef NO_WOLFSSL_SERVER
  43687. wolfSSL_free(serverTls12Ssl);
  43688. wolfSSL_CTX_free(serverTls12Ctx);
  43689. #endif
  43690. #ifndef NO_WOLFSSL_CLIENT
  43691. wolfSSL_free(clientTls12Ssl);
  43692. wolfSSL_CTX_free(clientTls12Ctx);
  43693. #endif
  43694. #endif
  43695. return TEST_RES_CHECK(1);
  43696. }
  43697. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  43698. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  43699. defined(BUILD_TLS_AES_256_GCM_SHA384)
  43700. /* Called when writing. */
  43701. static int CsSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  43702. {
  43703. (void)ssl;
  43704. (void)buf;
  43705. (void)sz;
  43706. (void)ctx;
  43707. /* Force error return from wolfSSL_accept_TLSv13(). */
  43708. return WANT_WRITE;
  43709. }
  43710. /* Called when reading. */
  43711. static int CsRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  43712. {
  43713. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  43714. int len = (int)msg->length;
  43715. (void)ssl;
  43716. (void)sz;
  43717. /* Pass back as much of message as will fit in buffer. */
  43718. if (len > sz)
  43719. len = sz;
  43720. XMEMCPY(buf, msg->buffer, len);
  43721. /* Move over returned data. */
  43722. msg->buffer += len;
  43723. msg->length -= len;
  43724. /* Amount actually copied. */
  43725. return len;
  43726. }
  43727. #endif
  43728. static int test_tls13_cipher_suites(void)
  43729. {
  43730. int res = TEST_SKIPPED;
  43731. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  43732. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  43733. defined(BUILD_TLS_AES_256_GCM_SHA384)
  43734. WOLFSSL_CTX* ctx;
  43735. WOLFSSL *ssl;
  43736. int i;
  43737. byte clientHello[] = {
  43738. 0x16, 0x03, 0x03, 0x01, 0x9b, 0x01, 0x00, 0x01,
  43739. 0x97, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  43740. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  43741. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  43742. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  43743. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  43744. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  43745. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  43746. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  43747. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  43748. /* Cipher suites: 0x13, 0x01 = TLS13-AES128-GCM-SHA256, twice. */
  43749. 0x13, 0x01,
  43750. 0x13, 0x01, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x2d,
  43751. 0x00, 0x03, 0x02, 0x00, 0x01, 0x00, 0x33, 0x00,
  43752. 0x47, 0x00, 0x45, 0x00, 0x17, 0x00, 0x41, 0x04,
  43753. 0x90, 0xfc, 0xe2, 0x97, 0x05, 0x7c, 0xb5, 0x23,
  43754. 0x5d, 0x5f, 0x5b, 0xcd, 0x0c, 0x1e, 0xe0, 0xe9,
  43755. 0xab, 0x38, 0x6b, 0x1e, 0x20, 0x5c, 0x1c, 0x90,
  43756. 0x2a, 0x9e, 0x68, 0x8e, 0x70, 0x05, 0x10, 0xa8,
  43757. 0x02, 0x1b, 0xf9, 0x5c, 0xef, 0xc9, 0xaf, 0xca,
  43758. 0x1a, 0x3b, 0x16, 0x8b, 0xe4, 0x1b, 0x3c, 0x15,
  43759. 0xb8, 0x0d, 0xbd, 0xaf, 0x62, 0x8d, 0xa7, 0x13,
  43760. 0xa0, 0x7c, 0xe0, 0x59, 0x0c, 0x4f, 0x8a, 0x6d,
  43761. 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00,
  43762. 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x03, 0x05,
  43763. 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06, 0x08,
  43764. 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08,
  43765. 0x09, 0x06, 0x01, 0x05, 0x01, 0x04, 0x01, 0x03,
  43766. 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00,
  43767. 0x02, 0x00, 0x17, 0x00, 0x16, 0x00, 0x00, 0x00,
  43768. 0x23, 0x00, 0x00, 0x00, 0x29, 0x00, 0xb9, 0x00,
  43769. 0x94, 0x00, 0x8e, 0x0f, 0x12, 0xfa, 0x84, 0x1f,
  43770. 0x76, 0x94, 0xd7, 0x09, 0x5e, 0xad, 0x08, 0x51,
  43771. 0xb6, 0x80, 0x28, 0x31, 0x8b, 0xfd, 0xc6, 0xbd,
  43772. 0x9e, 0xf5, 0x3b, 0x4d, 0x02, 0xbe, 0x1d, 0x73,
  43773. 0xea, 0x13, 0x68, 0x00, 0x4c, 0xfd, 0x3d, 0x48,
  43774. 0x51, 0xf9, 0x06, 0xbb, 0x92, 0xed, 0x42, 0x9f,
  43775. 0x7f, 0x2c, 0x73, 0x9f, 0xd9, 0xb4, 0xef, 0x05,
  43776. 0x26, 0x5b, 0x60, 0x5c, 0x0a, 0xfc, 0xa3, 0xbd,
  43777. 0x2d, 0x2d, 0x8b, 0xf9, 0xaa, 0x5c, 0x96, 0x3a,
  43778. 0xf2, 0xec, 0xfa, 0xe5, 0x57, 0x2e, 0x87, 0xbe,
  43779. 0x27, 0xc5, 0x3d, 0x4f, 0x5d, 0xdd, 0xde, 0x1c,
  43780. 0x1b, 0xb3, 0xcc, 0x27, 0x27, 0x57, 0x5a, 0xd9,
  43781. 0xea, 0x99, 0x27, 0x23, 0xa6, 0x0e, 0xea, 0x9c,
  43782. 0x0d, 0x85, 0xcb, 0x72, 0xeb, 0xd7, 0x93, 0xe3,
  43783. 0xfe, 0xf7, 0x5c, 0xc5, 0x5b, 0x75, 0x8c, 0x47,
  43784. 0x0a, 0x0e, 0xc4, 0x1a, 0xda, 0xef, 0x75, 0xe5,
  43785. 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  43786. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  43787. 0x00, 0xfb, 0x92, 0xce, 0xaa, 0x00, 0x21, 0x20,
  43788. 0xcb, 0x73, 0x25, 0x80, 0x46, 0x78, 0x4f, 0xe5,
  43789. 0x34, 0xf6, 0x91, 0x13, 0x7f, 0xc8, 0x8d, 0xdc,
  43790. 0x81, 0x04, 0xb7, 0x0d, 0x49, 0x85, 0x2e, 0x12,
  43791. 0x7a, 0x07, 0x23, 0xe9, 0x13, 0xa4, 0x6d, 0x8c
  43792. };
  43793. WOLFSSL_BUFFER_INFO msg;
  43794. /* Offset into ClientHello message data of first cipher suite. */
  43795. const int csOff = 78;
  43796. /* Server cipher list. */
  43797. const char* serverCs = "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256";
  43798. /* Suite list with duplicates. */
  43799. const char* dupCs = "TLS13-AES128-GCM-SHA256:"
  43800. "TLS13-AES128-GCM-SHA256:"
  43801. "TLS13-AES256-GCM-SHA384:"
  43802. "TLS13-AES256-GCM-SHA384:"
  43803. "TLS13-AES128-GCM-SHA256";
  43804. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  43805. const byte dupCsBytes[] = { TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  43806. TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  43807. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  43808. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  43809. TLS13_BYTE, TLS_AES_256_GCM_SHA384 };
  43810. #endif
  43811. /* Set up wolfSSL context. */
  43812. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  43813. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  43814. WOLFSSL_FILETYPE_PEM));
  43815. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  43816. WOLFSSL_FILETYPE_PEM));
  43817. /* Read from 'msg'. */
  43818. wolfSSL_SetIORecv(ctx, CsRecv);
  43819. /* No where to send to - dummy sender. */
  43820. wolfSSL_SetIOSend(ctx, CsSend);
  43821. /* Test cipher suite list with many copies of a cipher suite. */
  43822. AssertNotNull(ssl = wolfSSL_new(ctx));
  43823. msg.buffer = clientHello;
  43824. msg.length = (unsigned int)sizeof(clientHello);
  43825. wolfSSL_SetIOReadCtx(ssl, &msg);
  43826. /* Force server to have as many occurrences of same cipher suite as
  43827. * possible. */
  43828. ssl->suites->suiteSz = WOLFSSL_MAX_SUITE_SZ;
  43829. for (i = 0; i < ssl->suites->suiteSz; i += 2) {
  43830. ssl->suites->suites[i + 0] = TLS13_BYTE;
  43831. ssl->suites->suites[i + 1] = TLS_AES_128_GCM_SHA256;
  43832. }
  43833. /* Test multiple occurrences of same cipher suite. */
  43834. wolfSSL_accept_TLSv13(ssl);
  43835. wolfSSL_free(ssl);
  43836. /* Set client order opposite to server order:
  43837. * TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-SHA384 */
  43838. clientHello[csOff + 0] = TLS13_BYTE;
  43839. clientHello[csOff + 1] = TLS_AES_128_GCM_SHA256;
  43840. clientHello[csOff + 2] = TLS13_BYTE;
  43841. clientHello[csOff + 3] = TLS_AES_256_GCM_SHA384;
  43842. /* Test server order negotiation. */
  43843. AssertNotNull(ssl = wolfSSL_new(ctx));
  43844. msg.buffer = clientHello;
  43845. msg.length = (unsigned int)sizeof(clientHello);
  43846. wolfSSL_SetIOReadCtx(ssl, &msg);
  43847. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  43848. AssertIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  43849. /* Negotiate cipher suites in server order: TLS13-AES256-GCM-SHA384 */
  43850. wolfSSL_accept_TLSv13(ssl);
  43851. /* Check refined order - server order. */
  43852. AssertIntEQ(ssl->suites->suiteSz, 4);
  43853. AssertIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  43854. AssertIntEQ(ssl->suites->suites[1], TLS_AES_256_GCM_SHA384);
  43855. AssertIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  43856. AssertIntEQ(ssl->suites->suites[3], TLS_AES_128_GCM_SHA256);
  43857. wolfSSL_free(ssl);
  43858. /* Test client order negotiation. */
  43859. AssertNotNull(ssl = wolfSSL_new(ctx));
  43860. msg.buffer = clientHello;
  43861. msg.length = (unsigned int)sizeof(clientHello);
  43862. wolfSSL_SetIOReadCtx(ssl, &msg);
  43863. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  43864. AssertIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  43865. AssertIntEQ(wolfSSL_UseClientSuites(ssl), 0);
  43866. /* Negotiate cipher suites in client order: TLS13-AES128-GCM-SHA256 */
  43867. wolfSSL_accept_TLSv13(ssl);
  43868. /* Check refined order - client order. */
  43869. AssertIntEQ(ssl->suites->suiteSz, 4);
  43870. AssertIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  43871. AssertIntEQ(ssl->suites->suites[1], TLS_AES_128_GCM_SHA256);
  43872. AssertIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  43873. AssertIntEQ(ssl->suites->suites[3], TLS_AES_256_GCM_SHA384);
  43874. wolfSSL_free(ssl);
  43875. /* Check duplicate detection is working. */
  43876. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, dupCs), WOLFSSL_SUCCESS);
  43877. AssertIntEQ(ctx->suites->suiteSz, 4);
  43878. AssertIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  43879. AssertIntEQ(ctx->suites->suites[1], TLS_AES_128_GCM_SHA256);
  43880. AssertIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  43881. AssertIntEQ(ctx->suites->suites[3], TLS_AES_256_GCM_SHA384);
  43882. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  43883. AssertIntEQ(wolfSSL_CTX_set_cipher_list_bytes(ctx, dupCsBytes,
  43884. sizeof(dupCsBytes)), WOLFSSL_SUCCESS);
  43885. AssertIntEQ(ctx->suites->suiteSz, 4);
  43886. AssertIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  43887. AssertIntEQ(ctx->suites->suites[1], TLS_AES_256_GCM_SHA384);
  43888. AssertIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  43889. AssertIntEQ(ctx->suites->suites[3], TLS_AES_128_GCM_SHA256);
  43890. #endif
  43891. wolfSSL_CTX_free(ctx);
  43892. res = TEST_RES_CHECK(1);
  43893. #endif
  43894. return res;
  43895. }
  43896. #endif
  43897. #if defined(HAVE_PK_CALLBACKS) && (!defined(WOLFSSL_NO_TLS12) || \
  43898. !defined(NO_OLD_TLS))
  43899. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  43900. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && \
  43901. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  43902. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  43903. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  43904. const unsigned char* priv, unsigned int privSz,
  43905. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  43906. unsigned char* out, unsigned int* outlen,
  43907. void* ctx)
  43908. {
  43909. int result;
  43910. /* Test fail when context associated with WOLFSSL is NULL */
  43911. if (ctx == NULL) {
  43912. return -1;
  43913. }
  43914. (void)ssl;
  43915. /* return 0 on success */
  43916. PRIVATE_KEY_UNLOCK();
  43917. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  43918. PRIVATE_KEY_LOCK();
  43919. return result;
  43920. }
  43921. static void test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  43922. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  43923. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  43924. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  43925. WOLFSSL_SUCCESS);
  43926. #endif
  43927. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  43928. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  43929. WOLFSSL_SUCCESS);
  43930. #endif
  43931. }
  43932. static void test_dh_ssl_setup(WOLFSSL* ssl)
  43933. {
  43934. static int dh_test_ctx = 1;
  43935. int ret;
  43936. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  43937. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  43938. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  43939. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  43940. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  43941. }
  43942. }
  43943. static void test_dh_ssl_setup_fail(WOLFSSL* ssl)
  43944. {
  43945. int ret;
  43946. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  43947. AssertNull(wolfSSL_GetDhAgreeCtx(ssl));
  43948. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  43949. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  43950. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  43951. }
  43952. }
  43953. #endif
  43954. static int test_DhCallbacks(void)
  43955. {
  43956. int res = TEST_SKIPPED;
  43957. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  43958. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && \
  43959. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  43960. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  43961. WOLFSSL_CTX *ctx;
  43962. WOLFSSL *ssl;
  43963. tcp_ready ready;
  43964. func_args server_args;
  43965. func_args client_args;
  43966. THREAD_TYPE serverThread;
  43967. callback_functions func_cb_client;
  43968. callback_functions func_cb_server;
  43969. int test;
  43970. #ifndef NO_WOLFSSL_CLIENT
  43971. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  43972. #else
  43973. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  43974. #endif
  43975. AssertIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  43976. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  43977. /* load client ca cert */
  43978. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  43979. WOLFSSL_SUCCESS);
  43980. /* test with NULL arguments */
  43981. wolfSSL_SetDhAgreeCtx(NULL, &test);
  43982. AssertNull(wolfSSL_GetDhAgreeCtx(NULL));
  43983. /* test success case */
  43984. test = 1;
  43985. AssertNotNull(ssl = wolfSSL_new(ctx));
  43986. wolfSSL_SetDhAgreeCtx(ssl, &test);
  43987. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  43988. wolfSSL_free(ssl);
  43989. wolfSSL_CTX_free(ctx);
  43990. /* test a connection where callback is used */
  43991. #ifdef WOLFSSL_TIRTOS
  43992. fdOpenSession(Task_self());
  43993. #endif
  43994. XMEMSET(&server_args, 0, sizeof(func_args));
  43995. XMEMSET(&client_args, 0, sizeof(func_args));
  43996. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  43997. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  43998. StartTCP();
  43999. InitTcpReady(&ready);
  44000. #if defined(USE_WINDOWS_API)
  44001. /* use RNG to get random port if using windows */
  44002. ready.port = GetRandomPort();
  44003. #endif
  44004. server_args.signal = &ready;
  44005. client_args.signal = &ready;
  44006. server_args.return_code = TEST_FAIL;
  44007. client_args.return_code = TEST_FAIL;
  44008. /* set callbacks to use DH functions */
  44009. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  44010. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  44011. #ifndef WOLFSSL_NO_TLS12
  44012. func_cb_client.method = wolfTLSv1_2_client_method;
  44013. #else
  44014. func_cb_client.method = wolfTLSv1_3_client_method;
  44015. #endif
  44016. client_args.callbacks = &func_cb_client;
  44017. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  44018. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  44019. #ifndef WOLFSSL_NO_TLS12
  44020. func_cb_server.method = wolfTLSv1_2_server_method;
  44021. #else
  44022. func_cb_server.method = wolfTLSv1_3_server_method;
  44023. #endif
  44024. server_args.callbacks = &func_cb_server;
  44025. start_thread(test_server_nofail, &server_args, &serverThread);
  44026. wait_tcp_ready(&server_args);
  44027. test_client_nofail(&client_args, NULL);
  44028. join_thread(serverThread);
  44029. AssertTrue(client_args.return_code);
  44030. AssertTrue(server_args.return_code);
  44031. FreeTcpReady(&ready);
  44032. #ifdef WOLFSSL_TIRTOS
  44033. fdOpenSession(Task_self());
  44034. #endif
  44035. /* now set user ctx to not be 1 so that the callback returns fail case */
  44036. #ifdef WOLFSSL_TIRTOS
  44037. fdOpenSession(Task_self());
  44038. #endif
  44039. XMEMSET(&server_args, 0, sizeof(func_args));
  44040. XMEMSET(&client_args, 0, sizeof(func_args));
  44041. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  44042. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  44043. StartTCP();
  44044. InitTcpReady(&ready);
  44045. #if defined(USE_WINDOWS_API)
  44046. /* use RNG to get random port if using windows */
  44047. ready.port = GetRandomPort();
  44048. #endif
  44049. server_args.signal = &ready;
  44050. client_args.signal = &ready;
  44051. server_args.return_code = TEST_FAIL;
  44052. client_args.return_code = TEST_FAIL;
  44053. /* set callbacks to use DH functions */
  44054. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  44055. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  44056. #ifndef WOLFSSL_NO_TLS12
  44057. func_cb_client.method = wolfTLSv1_2_client_method;
  44058. #else
  44059. func_cb_client.method = wolfTLSv1_3_client_method;
  44060. #endif
  44061. client_args.callbacks = &func_cb_client;
  44062. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  44063. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  44064. #ifndef WOLFSSL_NO_TLS12
  44065. func_cb_server.method = wolfTLSv1_2_server_method;
  44066. #else
  44067. func_cb_server.method = wolfTLSv1_3_server_method;
  44068. #endif
  44069. server_args.callbacks = &func_cb_server;
  44070. start_thread(test_server_nofail, &server_args, &serverThread);
  44071. wait_tcp_ready(&server_args);
  44072. test_client_nofail(&client_args, NULL);
  44073. join_thread(serverThread);
  44074. AssertIntEQ(client_args.return_code, TEST_FAIL);
  44075. AssertIntEQ(server_args.return_code, TEST_FAIL);
  44076. FreeTcpReady(&ready);
  44077. #ifdef WOLFSSL_TIRTOS
  44078. fdOpenSession(Task_self());
  44079. #endif
  44080. res = TEST_RES_CHECK(1);
  44081. #endif
  44082. return res;
  44083. }
  44084. #endif /* HAVE_PK_CALLBACKS */
  44085. #ifdef HAVE_HASHDRBG
  44086. #ifdef TEST_RESEED_INTERVAL
  44087. static int test_wc_RNG_GenerateBlock_Reseed(void)
  44088. {
  44089. int i, ret;
  44090. WC_RNG rng;
  44091. byte key[32];
  44092. ret = wc_InitRng(&rng);
  44093. if (ret == 0) {
  44094. for (i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  44095. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  44096. if (ret != 0) {
  44097. break;
  44098. }
  44099. }
  44100. }
  44101. wc_FreeRng(&rng);
  44102. return TEST_RES_CHECK(ret == 0);
  44103. }
  44104. #endif /* TEST_RESEED_INTERVAL */
  44105. static int test_wc_RNG_GenerateBlock(void)
  44106. {
  44107. int i, ret;
  44108. WC_RNG rng;
  44109. byte key[32];
  44110. ret = wc_InitRng(&rng);
  44111. if (ret == 0) {
  44112. for (i = 0; i < 10; i++) {
  44113. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  44114. if (ret != 0) {
  44115. break;
  44116. }
  44117. }
  44118. }
  44119. wc_FreeRng(&rng);
  44120. (void)rng; /* for WC_NO_RNG case */
  44121. (void)key;
  44122. return TEST_RES_CHECK(ret == 0);
  44123. }
  44124. #endif
  44125. /*
  44126. * Testing get_rand_digit
  44127. */
  44128. static int test_get_rand_digit(void)
  44129. {
  44130. int res = TEST_SKIPPED;
  44131. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  44132. int ret = 0;
  44133. WC_RNG rng;
  44134. mp_digit d;
  44135. ret = wc_InitRng(&rng);
  44136. if (ret == 0) {
  44137. ret = get_rand_digit(&rng, &d);
  44138. }
  44139. if (ret == 0) {
  44140. ret = get_rand_digit(NULL, NULL);
  44141. if (ret == BAD_FUNC_ARG) {
  44142. ret = 0;
  44143. }
  44144. }
  44145. if (ret == 0) {
  44146. ret = get_rand_digit(NULL, &d);
  44147. if (ret == BAD_FUNC_ARG) {
  44148. ret = 0;
  44149. }
  44150. }
  44151. if (ret == 0) {
  44152. ret = get_rand_digit(&rng, NULL);
  44153. if (ret == BAD_FUNC_ARG) {
  44154. ret = 0;
  44155. }
  44156. }
  44157. if (ret == 0) {
  44158. ret = wc_FreeRng(&rng);
  44159. }
  44160. res = TEST_RES_CHECK(ret == 0);
  44161. #endif
  44162. return res;
  44163. }/* End test_get_rand_digit*/
  44164. /*
  44165. * Testing get_digit_count
  44166. */
  44167. static int test_get_digit_count(void)
  44168. {
  44169. int res = TEST_SKIPPED;
  44170. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  44171. int ret = 0;
  44172. mp_int a;
  44173. if (mp_init(&a) != MP_OKAY) {
  44174. ret = -1;
  44175. }
  44176. if (ret == 0) {
  44177. ret = get_digit_count(NULL);
  44178. }
  44179. if (ret == 0) {
  44180. ret = get_digit_count(&a);
  44181. }
  44182. mp_clear(&a);
  44183. res = TEST_RES_CHECK(ret == 0);
  44184. #endif
  44185. return res;
  44186. }/* End test_get_digit_count*/
  44187. /*
  44188. * Testing mp_cond_copy
  44189. */
  44190. static int test_mp_cond_copy(void)
  44191. {
  44192. int res = TEST_SKIPPED;
  44193. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  44194. defined(WOLFSSL_PUBLIC_MP)
  44195. int ret = 0;
  44196. mp_int a;
  44197. mp_int b;
  44198. int copy = 0;
  44199. if (mp_init(&a) != MP_OKAY) {
  44200. ret = -1;
  44201. }
  44202. if (ret == 0) {
  44203. if (mp_init(&b) != MP_OKAY) {
  44204. ret = -1;
  44205. }
  44206. }
  44207. if (ret == 0) {
  44208. ret = mp_cond_copy(NULL, copy, NULL);
  44209. if (ret == BAD_FUNC_ARG) {
  44210. ret = 0;
  44211. }
  44212. }
  44213. if (ret == 0) {
  44214. ret = mp_cond_copy(NULL, copy, &b);
  44215. if (ret == BAD_FUNC_ARG) {
  44216. ret = 0;
  44217. }
  44218. }
  44219. if (ret == 0) {
  44220. ret = mp_cond_copy(&a, copy, NULL);
  44221. if (ret == BAD_FUNC_ARG) {
  44222. ret = 0;
  44223. }
  44224. }
  44225. if (ret == 0) {
  44226. ret = mp_cond_copy(&a, copy, &b);
  44227. }
  44228. mp_clear(&a);
  44229. mp_clear(&b);
  44230. res = TEST_RES_CHECK(ret == 0);
  44231. #endif
  44232. return res;
  44233. }/* End test_mp_cond_copy*/
  44234. /*
  44235. * Testing mp_rand
  44236. */
  44237. static int test_mp_rand(void)
  44238. {
  44239. int res = TEST_SKIPPED;
  44240. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  44241. int ret = 0;
  44242. mp_int a;
  44243. int digits = 1;
  44244. WC_RNG rng;
  44245. if (mp_init(&a) != MP_OKAY) {
  44246. ret = -1;
  44247. }
  44248. if (ret == 0) {
  44249. ret = wc_InitRng(&rng);
  44250. }
  44251. if (ret == 0) {
  44252. ret = mp_rand(&a, digits, NULL);
  44253. if (ret == MISSING_RNG_E) {
  44254. ret = 0;
  44255. }
  44256. }
  44257. if (ret == 0) {
  44258. ret = mp_rand(NULL, digits, &rng);
  44259. if (ret == BAD_FUNC_ARG) {
  44260. ret = 0;
  44261. }
  44262. }
  44263. if (ret == 0) {
  44264. ret = mp_rand(&a, 0, &rng);
  44265. if (ret == BAD_FUNC_ARG) {
  44266. ret = 0;
  44267. }
  44268. }
  44269. if (ret == 0) {
  44270. ret = mp_rand(&a, digits, &rng);
  44271. }
  44272. mp_clear(&a);
  44273. wc_FreeRng(&rng);
  44274. res = TEST_RES_CHECK(ret == 0);
  44275. #endif
  44276. return res;
  44277. }/* End test_mp_rand*/
  44278. /*
  44279. * Testing get_digit
  44280. */
  44281. static int test_get_digit(void)
  44282. {
  44283. int res = TEST_SKIPPED;
  44284. #if defined(WOLFSSL_PUBLIC_MP)
  44285. int ret = 0;
  44286. mp_int a;
  44287. int n = 0;
  44288. if (mp_init(&a) != MP_OKAY) {
  44289. ret = -1;
  44290. }
  44291. if (ret == 0) {
  44292. if (get_digit(NULL, n) != 0) { /* Should not hit this */
  44293. ret = -1;
  44294. }
  44295. }
  44296. if (ret == 0) {
  44297. if (get_digit(NULL, n) == 0) { /* Should hit this */
  44298. ret = 0;
  44299. }
  44300. }
  44301. if (ret == 0) {
  44302. if (get_digit(&a, n) != 0) { /* Should not hit this */
  44303. ret = -1;
  44304. }
  44305. }
  44306. if (ret == 0) {
  44307. if (get_digit(&a, n) == 0) { /* Should hit this */
  44308. ret = 0;
  44309. }
  44310. }
  44311. mp_clear(&a);
  44312. res = TEST_RES_CHECK(ret == 0);
  44313. #endif
  44314. return res;
  44315. }/* End test_get_digit*/
  44316. /*
  44317. * Testing wc_export_int
  44318. */
  44319. static int test_wc_export_int(void)
  44320. {
  44321. int res = TEST_SKIPPED;
  44322. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  44323. defined(WOLFSSL_PUBLIC_MP)
  44324. int ret = 0;
  44325. mp_int mp;
  44326. byte buf[32];
  44327. word32 keySz = (word32)sizeof(buf);
  44328. word32 len = (word32)sizeof(buf);
  44329. if (mp_init(&mp) != MP_OKAY) {
  44330. ret = -1;
  44331. }
  44332. if (ret == 0) {
  44333. ret = mp_set(&mp, 1234);
  44334. }
  44335. if (ret == 0) {
  44336. ret = wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  44337. if (ret == BAD_FUNC_ARG) {
  44338. ret = 0;
  44339. }
  44340. }
  44341. if (ret == 0) {
  44342. len = sizeof(buf)-1;
  44343. ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  44344. if (ret == BUFFER_E) {
  44345. ret = 0;
  44346. }
  44347. }
  44348. if (ret == 0) {
  44349. len = sizeof(buf);
  44350. ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  44351. }
  44352. if (ret == 0) {
  44353. len = 4; /* test input too small */
  44354. ret = wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR);
  44355. if (ret == BUFFER_E) {
  44356. ret = 0;
  44357. }
  44358. }
  44359. if (ret == 0) {
  44360. len = sizeof(buf);
  44361. ret = wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR);
  44362. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  44363. if (ret == 0 && len != 5) {
  44364. ret = BAD_FUNC_ARG;
  44365. }
  44366. }
  44367. mp_clear(&mp);
  44368. res = TEST_RES_CHECK(ret == 0);
  44369. #endif
  44370. return res;
  44371. }/* End test_wc_export_int*/
  44372. static int test_wc_InitRngNonce(void)
  44373. {
  44374. int res = TEST_SKIPPED;
  44375. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  44376. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  44377. int ret;
  44378. WC_RNG rng;
  44379. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  44380. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  44381. word32 nonceSz = sizeof(nonce);
  44382. ret = wc_InitRngNonce(&rng, nonce, nonceSz);
  44383. wc_FreeRng(&rng);
  44384. res = TEST_RES_CHECK(ret == 0);
  44385. #endif
  44386. return res;
  44387. }/* End test_wc_InitRngNonce*/
  44388. /*
  44389. * Testing wc_InitRngNonce_ex
  44390. */
  44391. static int test_wc_InitRngNonce_ex(void)
  44392. {
  44393. int res = TEST_SKIPPED;
  44394. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  44395. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  44396. int ret;
  44397. WC_RNG rng;
  44398. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  44399. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  44400. word32 nonceSz = sizeof(nonce);
  44401. ret = wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId);
  44402. wc_FreeRng(&rng);
  44403. res = TEST_RES_CHECK(ret == 0);
  44404. #endif
  44405. return res;
  44406. }/*End test_wc_InitRngNonce_ex*/
  44407. static int test_wolfSSL_X509_CRL(void)
  44408. {
  44409. int res = TEST_SKIPPED;
  44410. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  44411. X509_CRL *crl;
  44412. char pem[][100] = {
  44413. "./certs/crl/crl.pem",
  44414. "./certs/crl/crl2.pem",
  44415. "./certs/crl/caEccCrl.pem",
  44416. "./certs/crl/eccCliCRL.pem",
  44417. "./certs/crl/eccSrvCRL.pem",
  44418. ""
  44419. };
  44420. #ifndef NO_BIO
  44421. BIO *bio;
  44422. #endif
  44423. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  44424. char der[][100] = {
  44425. "./certs/crl/crl.der",
  44426. "./certs/crl/crl2.der",
  44427. ""};
  44428. #endif
  44429. XFILE fp;
  44430. int i;
  44431. for (i = 0; pem[i][0] != '\0'; i++)
  44432. {
  44433. fp = XFOPEN(pem[i], "rb");
  44434. AssertTrue((fp != XBADFILE));
  44435. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
  44436. AssertNotNull(crl);
  44437. X509_CRL_free(crl);
  44438. XFCLOSE(fp);
  44439. fp = XFOPEN(pem[i], "rb");
  44440. AssertTrue((fp != XBADFILE));
  44441. AssertNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL, NULL));
  44442. AssertNotNull(crl);
  44443. X509_CRL_free(crl);
  44444. XFCLOSE(fp);
  44445. }
  44446. #ifndef NO_BIO
  44447. for (i = 0; pem[i][0] != '\0'; i++)
  44448. {
  44449. AssertNotNull(bio = BIO_new_file(pem[i], "rb"));
  44450. AssertNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  44451. X509_CRL_free(crl);
  44452. BIO_free(bio);
  44453. }
  44454. #endif
  44455. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  44456. for (i = 0; der[i][0] != '\0'; i++) {
  44457. fp = XFOPEN(der[i], "rb");
  44458. AssertTrue((fp != XBADFILE));
  44459. AssertNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  44460. AssertNotNull(crl);
  44461. X509_CRL_free(crl);
  44462. XFCLOSE(fp);
  44463. fp = XFOPEN(der[i], "rb");
  44464. AssertTrue((fp != XBADFILE));
  44465. AssertNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  44466. AssertNotNull(crl);
  44467. X509_CRL_free(crl);
  44468. XFCLOSE(fp);
  44469. }
  44470. #endif
  44471. res = TEST_RES_CHECK(1);
  44472. #endif
  44473. return res;
  44474. }
  44475. static int test_wolfSSL_X509_load_crl_file(void)
  44476. {
  44477. int res = TEST_SKIPPED;
  44478. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  44479. !defined(NO_RSA) && !defined(NO_BIO)
  44480. int i;
  44481. char pem[][100] = {
  44482. "./certs/crl/crl.pem",
  44483. "./certs/crl/crl2.pem",
  44484. "./certs/crl/caEccCrl.pem",
  44485. "./certs/crl/eccCliCRL.pem",
  44486. "./certs/crl/eccSrvCRL.pem",
  44487. ""
  44488. };
  44489. char der[][100] = {
  44490. "./certs/crl/crl.der",
  44491. "./certs/crl/crl2.der",
  44492. ""
  44493. };
  44494. WOLFSSL_X509_STORE* store;
  44495. WOLFSSL_X509_LOOKUP* lookup;
  44496. AssertNotNull(store = wolfSSL_X509_STORE_new());
  44497. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  44498. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  44499. X509_FILETYPE_PEM), 1);
  44500. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  44501. X509_FILETYPE_PEM), 1);
  44502. if (store) {
  44503. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  44504. WOLFSSL_FILETYPE_PEM), 1);
  44505. /* since store hasn't yet known the revoked cert*/
  44506. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  44507. WOLFSSL_FILETYPE_PEM), 1);
  44508. }
  44509. for (i = 0; pem[i][0] != '\0'; i++)
  44510. {
  44511. AssertIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM), 1);
  44512. }
  44513. if (store) {
  44514. /* since store knows crl list */
  44515. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  44516. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  44517. }
  44518. /* once feeing store */
  44519. X509_STORE_free(store);
  44520. store = NULL;
  44521. AssertNotNull(store = wolfSSL_X509_STORE_new());
  44522. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  44523. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  44524. X509_FILETYPE_PEM), 1);
  44525. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  44526. X509_FILETYPE_PEM), 1);
  44527. if (store) {
  44528. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  44529. WOLFSSL_FILETYPE_PEM), 1);
  44530. /* since store hasn't yet known the revoked cert*/
  44531. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  44532. WOLFSSL_FILETYPE_PEM), 1);
  44533. }
  44534. for (i = 0; der[i][0] != '\0'; i++)
  44535. {
  44536. AssertIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1), 1);
  44537. }
  44538. if (store) {
  44539. /* since store knows crl list */
  44540. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  44541. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  44542. }
  44543. /* test for incorrect parameter */
  44544. AssertIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  44545. AssertIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  44546. AssertIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  44547. X509_STORE_free(store);
  44548. store = NULL;
  44549. res = TEST_RES_CHECK(1);
  44550. #endif
  44551. return res;
  44552. }
  44553. static int test_wolfSSL_d2i_X509_REQ(void)
  44554. {
  44555. int res = TEST_SKIPPED;
  44556. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  44557. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
  44558. !defined(WOLFSSL_SP_MATH)
  44559. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der were
  44560. * generated by libest
  44561. * ./certs/csr.attr.der contains sample attributes
  44562. * ./certs/csr.ext.der contains sample extensions */
  44563. const char* csrFile = "./certs/csr.signed.der";
  44564. const char* csrPopFile = "./certs/csr.attr.der";
  44565. const char* csrExtFile = "./certs/csr.ext.der";
  44566. /* ./certs/csr.dsa.pem is generated using
  44567. * openssl req -newkey dsa:certs/dsaparams.pem \
  44568. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  44569. * -outform PEM
  44570. * with the passphrase "wolfSSL"
  44571. */
  44572. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  44573. const char* csrDsaFile = "./certs/csr.dsa.pem";
  44574. XFILE f;
  44575. #endif
  44576. BIO* bio = NULL;
  44577. X509* req = NULL;
  44578. EVP_PKEY *pub_key = NULL;
  44579. {
  44580. AssertNotNull(bio = BIO_new_file(csrFile, "rb"));
  44581. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  44582. /*
  44583. * Extract the public key from the CSR
  44584. */
  44585. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  44586. /*
  44587. * Verify the signature in the CSR
  44588. */
  44589. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  44590. X509_free(req);
  44591. BIO_free(bio);
  44592. EVP_PKEY_free(pub_key);
  44593. }
  44594. {
  44595. #ifdef OPENSSL_ALL
  44596. X509_ATTRIBUTE* attr;
  44597. ASN1_TYPE *at;
  44598. #endif
  44599. AssertNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  44600. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  44601. /*
  44602. * Extract the public key from the CSR
  44603. */
  44604. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  44605. /*
  44606. * Verify the signature in the CSR
  44607. */
  44608. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  44609. #ifdef OPENSSL_ALL
  44610. /*
  44611. * Obtain the challenge password from the CSR
  44612. */
  44613. AssertIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword, -1),
  44614. 1);
  44615. AssertNotNull(attr = X509_REQ_get_attr(req, 1));
  44616. AssertNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  44617. AssertNotNull(at->value.asn1_string);
  44618. AssertStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "2xIE+qqp/rhyTXP+");
  44619. AssertIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  44620. #endif
  44621. X509_free(req);
  44622. BIO_free(bio);
  44623. EVP_PKEY_free(pub_key);
  44624. }
  44625. {
  44626. #ifdef OPENSSL_ALL
  44627. X509_ATTRIBUTE* attr;
  44628. ASN1_TYPE *at;
  44629. STACK_OF(X509_EXTENSION) *exts = NULL;
  44630. #endif
  44631. AssertNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  44632. /* This CSR contains an Extension Request attribute so
  44633. * we test extension parsing in a CSR attribute here. */
  44634. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  44635. /*
  44636. * Extract the public key from the CSR
  44637. */
  44638. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  44639. /*
  44640. * Verify the signature in the CSR
  44641. */
  44642. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  44643. #ifdef OPENSSL_ALL
  44644. AssertNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(req));
  44645. AssertIntEQ(sk_X509_EXTENSION_num(exts), 2);
  44646. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  44647. /*
  44648. * Obtain the challenge password from the CSR
  44649. */
  44650. AssertIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword, -1),
  44651. 0);
  44652. AssertNotNull(attr = X509_REQ_get_attr(req, 0));
  44653. AssertNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  44654. AssertNotNull(at->value.asn1_string);
  44655. AssertStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  44656. AssertIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  44657. AssertIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  44658. #endif
  44659. X509_free(req);
  44660. BIO_free(bio);
  44661. EVP_PKEY_free(pub_key);
  44662. }
  44663. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  44664. {
  44665. AssertNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  44666. AssertNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  44667. /*
  44668. * Extract the public key from the CSR
  44669. */
  44670. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  44671. /*
  44672. * Verify the signature in the CSR
  44673. */
  44674. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  44675. X509_free(req);
  44676. BIO_free(bio);
  44677. /* Run the same test, but with a file pointer instead of a BIO.
  44678. * (PEM_read_X509_REQ)*/
  44679. AssertTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  44680. AssertNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  44681. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  44682. X509_free(req);
  44683. EVP_PKEY_free(pub_key);
  44684. }
  44685. res = TEST_RES_CHECK(1);
  44686. #endif /* !NO_DSA && !HAVE_SELFTEST */
  44687. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  44688. return res;
  44689. }
  44690. static int test_wolfSSL_PEM_read_X509(void)
  44691. {
  44692. int res = TEST_SKIPPED;
  44693. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  44694. !defined(NO_RSA)
  44695. X509 *x509 = NULL;
  44696. XFILE fp;
  44697. fp = XFOPEN(svrCertFile, "rb");
  44698. AssertTrue((fp != XBADFILE));
  44699. AssertNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  44700. X509_free(x509);
  44701. XFCLOSE(fp);
  44702. res = TEST_RES_CHECK(1);
  44703. #endif
  44704. return res;
  44705. }
  44706. static int test_wolfSSL_PEM_read(void)
  44707. {
  44708. int res = TEST_SKIPPED;
  44709. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  44710. const char* filename = "./certs/server-keyEnc.pem";
  44711. XFILE fp;
  44712. char* name = NULL;
  44713. char* header = NULL;
  44714. byte* data = NULL;
  44715. long len;
  44716. EVP_CIPHER_INFO cipher;
  44717. WOLFSSL_BIO* bio;
  44718. byte* fileData;
  44719. size_t fileDataSz;
  44720. byte* out;
  44721. fp = XFOPEN(filename, "rb");
  44722. AssertTrue((fp != XBADFILE));
  44723. /* Fail cases. */
  44724. AssertIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  44725. AssertIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  44726. AssertIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  44727. AssertIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  44728. AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  44729. AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  44730. AssertIntGT(XSTRLEN(header), 0);
  44731. AssertIntGT(len, 0);
  44732. AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  44733. AssertIntGT((fileDataSz = XFTELL(fp)), 0);
  44734. AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  44735. AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  44736. DYNAMIC_TYPE_TMP_BUFFER));
  44737. AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  44738. XFCLOSE(fp);
  44739. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  44740. /* Fail cases. */
  44741. AssertIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  44742. AssertIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  44743. AssertIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  44744. AssertIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  44745. AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  44746. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  44747. AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  44748. /* Fail cases. */
  44749. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  44750. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  44751. AssertIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  44752. #ifndef NO_DES3
  44753. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  44754. #endif
  44755. /* Fail cases. */
  44756. AssertIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  44757. (void*)"yassl123"), WOLFSSL_FAILURE);
  44758. AssertIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  44759. (void*)"yassl123"), WOLFSSL_FAILURE);
  44760. AssertIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  44761. (void*)"yassl123"), WOLFSSL_FAILURE);
  44762. #if !defined(NO_DES3) && !defined(NO_MD5)
  44763. AssertIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  44764. (void*)"yassl123"), WOLFSSL_SUCCESS);
  44765. #endif
  44766. BIO_free(bio);
  44767. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44768. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44769. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44770. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44771. name = NULL;
  44772. header = NULL;
  44773. data = NULL;
  44774. fp = XFOPEN(svrKeyFile, "rb");
  44775. AssertTrue((fp != XBADFILE));
  44776. AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  44777. AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  44778. AssertIntEQ(XSTRLEN(header), 0);
  44779. AssertIntGT(len, 0);
  44780. AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  44781. AssertIntGT((fileDataSz = XFTELL(fp)), 0);
  44782. AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  44783. AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  44784. DYNAMIC_TYPE_TMP_BUFFER));
  44785. AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  44786. XFCLOSE(fp);
  44787. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  44788. AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  44789. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  44790. AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  44791. BIO_free(bio);
  44792. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44793. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44794. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44795. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44796. res = TEST_RES_CHECK(1);
  44797. #endif
  44798. return res;
  44799. }
  44800. static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  44801. {
  44802. int res = TEST_SKIPPED;
  44803. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  44804. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  44805. const byte iv[12] = { 0 };
  44806. const byte key[16] = { 0 };
  44807. const byte cleartext[16] = { 0 };
  44808. const byte aad[] = {
  44809. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  44810. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  44811. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  44812. 0x4f
  44813. };
  44814. byte out1Part[16];
  44815. byte outTag1Part[16];
  44816. byte out2Part[16];
  44817. byte outTag2Part[16];
  44818. byte decryptBuf[16];
  44819. int len;
  44820. int tlen;
  44821. EVP_CIPHER_CTX* ctx = NULL;
  44822. /* ENCRYPT */
  44823. /* Send AAD and data in 1 part */
  44824. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  44825. tlen = 0;
  44826. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  44827. 1);
  44828. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  44829. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  44830. AssertIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  44831. sizeof(cleartext)), 1);
  44832. tlen += len;
  44833. AssertIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  44834. tlen += len;
  44835. AssertIntEQ(tlen, sizeof(cleartext));
  44836. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  44837. outTag1Part), 1);
  44838. EVP_CIPHER_CTX_free(ctx);
  44839. /* DECRYPT */
  44840. /* Send AAD and data in 1 part */
  44841. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  44842. tlen = 0;
  44843. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  44844. 1);
  44845. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  44846. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  44847. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  44848. sizeof(cleartext)), 1);
  44849. tlen += len;
  44850. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  44851. outTag1Part), 1);
  44852. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  44853. tlen += len;
  44854. AssertIntEQ(tlen, sizeof(cleartext));
  44855. EVP_CIPHER_CTX_free(ctx);
  44856. AssertIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  44857. /* ENCRYPT */
  44858. /* Send AAD and data in 2 parts */
  44859. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  44860. tlen = 0;
  44861. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  44862. 1);
  44863. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  44864. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  44865. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  44866. 1);
  44867. AssertIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  44868. tlen += len;
  44869. AssertIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  44870. sizeof(cleartext) - 1), 1);
  44871. tlen += len;
  44872. AssertIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  44873. tlen += len;
  44874. AssertIntEQ(tlen, sizeof(cleartext));
  44875. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  44876. outTag2Part), 1);
  44877. AssertIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  44878. AssertIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  44879. EVP_CIPHER_CTX_free(ctx);
  44880. /* DECRYPT */
  44881. /* Send AAD and data in 2 parts */
  44882. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  44883. tlen = 0;
  44884. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  44885. 1);
  44886. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  44887. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  44888. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  44889. 1);
  44890. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  44891. tlen += len;
  44892. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  44893. sizeof(cleartext) - 1), 1);
  44894. tlen += len;
  44895. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  44896. outTag1Part), 1);
  44897. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  44898. tlen += len;
  44899. AssertIntEQ(tlen, sizeof(cleartext));
  44900. AssertIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  44901. /* Test AAD re-use */
  44902. EVP_CIPHER_CTX_free(ctx);
  44903. res = TEST_RES_CHECK(1);
  44904. #endif
  44905. return res;
  44906. }
  44907. static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
  44908. {
  44909. int res = TEST_SKIPPED;
  44910. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  44911. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  44912. /* Zero length plain text */
  44913. byte key[] = {
  44914. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  44915. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  44916. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  44917. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  44918. }; /* align */
  44919. byte iv[] = {
  44920. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  44921. }; /* align */
  44922. byte plaintxt[1];
  44923. int ivSz = 12;
  44924. int plaintxtSz = 0;
  44925. unsigned char tag[16];
  44926. unsigned char tag_kat[] =
  44927. {0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  44928. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b};
  44929. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  44930. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  44931. int ciphertxtSz = 0;
  44932. int decryptedtxtSz = 0;
  44933. int len = 0;
  44934. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  44935. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  44936. AssertIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  44937. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  44938. AssertIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  44939. plaintxtSz));
  44940. AssertIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  44941. ciphertxtSz += len;
  44942. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  44943. AssertIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  44944. AssertIntEQ(0, ciphertxtSz);
  44945. AssertIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  44946. EVP_CIPHER_CTX_init(de);
  44947. AssertIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  44948. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  44949. AssertIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  44950. decryptedtxtSz = len;
  44951. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  44952. AssertIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  44953. decryptedtxtSz += len;
  44954. AssertIntEQ(0, decryptedtxtSz);
  44955. EVP_CIPHER_CTX_free(en);
  44956. EVP_CIPHER_CTX_free(de);
  44957. res = TEST_RES_CHECK(1);
  44958. #endif
  44959. return res;
  44960. }
  44961. static int test_wolfssl_EVP_aes_gcm(void)
  44962. {
  44963. int res = TEST_SKIPPED;
  44964. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  44965. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  44966. /* A 256 bit key, AES_128 will use the first 128 bit*/
  44967. byte *key = (byte*)"01234567890123456789012345678901";
  44968. /* A 128 bit IV */
  44969. byte *iv = (byte*)"0123456789012345";
  44970. int ivSz = AES_BLOCK_SIZE;
  44971. /* Message to be encrypted */
  44972. byte *plaintxt = (byte*)"for things to change you have to change";
  44973. /* Additional non-confidential data */
  44974. byte *aad = (byte*)"Don't spend major time on minor things.";
  44975. unsigned char tag[AES_BLOCK_SIZE] = {0};
  44976. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  44977. int aadSz = (int)XSTRLEN((char*)aad);
  44978. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  44979. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  44980. int ciphertxtSz = 0;
  44981. int decryptedtxtSz = 0;
  44982. int len = 0;
  44983. int i = 0;
  44984. EVP_CIPHER_CTX en[2];
  44985. EVP_CIPHER_CTX de[2];
  44986. for (i = 0; i < 2; i++) {
  44987. EVP_CIPHER_CTX_init(&en[i]);
  44988. if (i == 0) {
  44989. /* Default uses 96-bits IV length */
  44990. #ifdef WOLFSSL_AES_128
  44991. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, key, iv));
  44992. #elif defined(WOLFSSL_AES_192)
  44993. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, key, iv));
  44994. #elif defined(WOLFSSL_AES_256)
  44995. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, key, iv));
  44996. #endif
  44997. }
  44998. else {
  44999. #ifdef WOLFSSL_AES_128
  45000. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
  45001. #elif defined(WOLFSSL_AES_192)
  45002. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
  45003. #elif defined(WOLFSSL_AES_256)
  45004. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
  45005. #endif
  45006. /* non-default must to set the IV length first */
  45007. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  45008. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  45009. }
  45010. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  45011. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  45012. ciphertxtSz = len;
  45013. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  45014. ciphertxtSz += len;
  45015. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, AES_BLOCK_SIZE, tag));
  45016. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  45017. EVP_CIPHER_CTX_init(&de[i]);
  45018. if (i == 0) {
  45019. /* Default uses 96-bits IV length */
  45020. #ifdef WOLFSSL_AES_128
  45021. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, key, iv));
  45022. #elif defined(WOLFSSL_AES_192)
  45023. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, key, iv));
  45024. #elif defined(WOLFSSL_AES_256)
  45025. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, key, iv));
  45026. #endif
  45027. }
  45028. else {
  45029. #ifdef WOLFSSL_AES_128
  45030. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
  45031. #elif defined(WOLFSSL_AES_192)
  45032. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
  45033. #elif defined(WOLFSSL_AES_256)
  45034. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
  45035. #endif
  45036. /* non-default must to set the IV length first */
  45037. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  45038. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  45039. }
  45040. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  45041. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  45042. decryptedtxtSz = len;
  45043. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
  45044. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  45045. decryptedtxtSz += len;
  45046. AssertIntEQ(ciphertxtSz, decryptedtxtSz);
  45047. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  45048. /* modify tag*/
  45049. tag[AES_BLOCK_SIZE-1]+=0xBB;
  45050. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  45051. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
  45052. /* fail due to wrong tag */
  45053. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  45054. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  45055. AssertIntEQ(0, len);
  45056. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  45057. }
  45058. res = TEST_RES_CHECK(1);
  45059. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  45060. return res;
  45061. }
  45062. static int test_wolfssl_EVP_chacha20_poly1305(void)
  45063. {
  45064. int res = TEST_SKIPPED;
  45065. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  45066. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  45067. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  45068. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  45069. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  45070. byte cipherText[sizeof(plainText)];
  45071. byte decryptedText[sizeof(plainText)];
  45072. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  45073. EVP_CIPHER_CTX* ctx;
  45074. int outSz;
  45075. /* Encrypt. */
  45076. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  45077. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  45078. NULL), WOLFSSL_SUCCESS);
  45079. /* Invalid IV length. */
  45080. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  45081. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  45082. /* Valid IV length. */
  45083. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  45084. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  45085. /* Invalid tag length. */
  45086. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  45087. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  45088. /* Valid tag length. */
  45089. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  45090. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  45091. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  45092. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  45093. WOLFSSL_SUCCESS);
  45094. AssertIntEQ(outSz, sizeof(aad));
  45095. AssertIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  45096. sizeof(plainText)), WOLFSSL_SUCCESS);
  45097. AssertIntEQ(outSz, sizeof(plainText));
  45098. AssertIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  45099. AssertIntEQ(outSz, 0);
  45100. /* Invalid tag length. */
  45101. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  45102. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  45103. /* Valid tag length. */
  45104. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  45105. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  45106. EVP_CIPHER_CTX_free(ctx);
  45107. /* Decrypt. */
  45108. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  45109. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  45110. NULL), WOLFSSL_SUCCESS);
  45111. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  45112. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  45113. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  45114. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  45115. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  45116. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  45117. WOLFSSL_SUCCESS);
  45118. AssertIntEQ(outSz, sizeof(aad));
  45119. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45120. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45121. AssertIntEQ(outSz, sizeof(cipherText));
  45122. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  45123. WOLFSSL_SUCCESS);
  45124. AssertIntEQ(outSz, 0);
  45125. EVP_CIPHER_CTX_free(ctx);
  45126. /* Test partial Inits. CipherInit() allow setting of key and iv
  45127. * in separate calls. */
  45128. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  45129. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20_poly1305(),
  45130. key, NULL, 1), WOLFSSL_SUCCESS);
  45131. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  45132. WOLFSSL_SUCCESS);
  45133. AssertIntEQ(wolfSSL_EVP_CipherUpdate(ctx, NULL, &outSz,
  45134. aad, sizeof(aad)), WOLFSSL_SUCCESS);
  45135. AssertIntEQ(outSz, sizeof(aad));
  45136. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45137. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45138. AssertIntEQ(outSz, sizeof(cipherText));
  45139. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  45140. WOLFSSL_SUCCESS);
  45141. AssertIntEQ(outSz, 0);
  45142. EVP_CIPHER_CTX_free(ctx);
  45143. res = TEST_RES_CHECK(1);
  45144. #endif
  45145. return res;
  45146. }
  45147. static int test_wolfssl_EVP_chacha20(void)
  45148. {
  45149. int res = TEST_SKIPPED;
  45150. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA)
  45151. byte key[CHACHA_MAX_KEY_SZ];
  45152. byte iv [WOLFSSL_EVP_CHACHA_IV_BYTES];
  45153. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  45154. byte cipherText[sizeof(plainText)];
  45155. byte decryptedText[sizeof(plainText)];
  45156. EVP_CIPHER_CTX* ctx;
  45157. int outSz;
  45158. /* Encrypt. */
  45159. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  45160. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  45161. NULL), WOLFSSL_SUCCESS);
  45162. /* Any tag length must fail - not an AEAD cipher. */
  45163. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  45164. 16, NULL), WOLFSSL_FAILURE);
  45165. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  45166. AssertIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  45167. sizeof(plainText)), WOLFSSL_SUCCESS);
  45168. AssertIntEQ(outSz, sizeof(plainText));
  45169. AssertIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  45170. AssertIntEQ(outSz, 0);
  45171. EVP_CIPHER_CTX_free(ctx);
  45172. /* Decrypt. */
  45173. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  45174. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  45175. NULL), WOLFSSL_SUCCESS);
  45176. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  45177. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45178. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45179. AssertIntEQ(outSz, sizeof(cipherText));
  45180. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  45181. WOLFSSL_SUCCESS);
  45182. AssertIntEQ(outSz, 0);
  45183. EVP_CIPHER_CTX_free(ctx);
  45184. /* Test partial Inits. CipherInit() allow setting of key and iv
  45185. * in separate calls. */
  45186. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  45187. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20(),
  45188. key, NULL, 1), WOLFSSL_SUCCESS);
  45189. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  45190. WOLFSSL_SUCCESS);
  45191. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45192. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45193. AssertIntEQ(outSz, sizeof(cipherText));
  45194. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  45195. WOLFSSL_SUCCESS);
  45196. AssertIntEQ(outSz, 0);
  45197. EVP_CIPHER_CTX_free(ctx);
  45198. res = TEST_RES_CHECK(1);
  45199. #endif
  45200. return res;
  45201. }
  45202. static int test_wolfSSL_EVP_PKEY_hkdf(void)
  45203. {
  45204. int res = TEST_SKIPPED;
  45205. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  45206. EVP_PKEY_CTX* ctx;
  45207. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  45208. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  45209. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  45210. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  45211. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  45212. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  45213. byte outKey[34];
  45214. size_t outKeySz = sizeof(outKey);
  45215. /* These expected outputs were gathered by running the same test below using
  45216. * OpenSSL. */
  45217. const byte extractAndExpand[] = {
  45218. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  45219. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  45220. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  45221. };
  45222. const byte extractOnly[] = {
  45223. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  45224. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  45225. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  45226. };
  45227. const byte expandOnly[] = {
  45228. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  45229. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  45230. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  45231. };
  45232. const byte extractAndExpandAddInfo[] = {
  45233. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  45234. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  45235. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  45236. };
  45237. AssertNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  45238. AssertIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  45239. /* NULL ctx. */
  45240. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  45241. /* NULL md. */
  45242. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  45243. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  45244. /* NULL ctx. */
  45245. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  45246. WOLFSSL_FAILURE);
  45247. /* NULL salt is ok. */
  45248. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  45249. WOLFSSL_SUCCESS);
  45250. /* Salt length <= 0. */
  45251. /* Length 0 salt is ok. */
  45252. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  45253. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  45254. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  45255. WOLFSSL_SUCCESS);
  45256. /* NULL ctx. */
  45257. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  45258. WOLFSSL_FAILURE);
  45259. /* NULL key. */
  45260. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  45261. WOLFSSL_FAILURE);
  45262. /* Key length <= 0 */
  45263. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  45264. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  45265. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  45266. WOLFSSL_SUCCESS);
  45267. /* NULL ctx. */
  45268. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  45269. WOLFSSL_FAILURE);
  45270. /* NULL info is ok. */
  45271. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  45272. WOLFSSL_SUCCESS);
  45273. /* Info length <= 0 */
  45274. /* Length 0 info is ok. */
  45275. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  45276. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  45277. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  45278. WOLFSSL_SUCCESS);
  45279. /* NULL ctx. */
  45280. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  45281. WOLFSSL_FAILURE);
  45282. /* Extract and expand (default). */
  45283. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  45284. AssertIntEQ(outKeySz, sizeof(extractAndExpand));
  45285. AssertIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  45286. /* Extract only. */
  45287. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  45288. WOLFSSL_SUCCESS);
  45289. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  45290. AssertIntEQ(outKeySz, sizeof(extractOnly));
  45291. AssertIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  45292. outKeySz = sizeof(outKey);
  45293. /* Expand only. */
  45294. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  45295. WOLFSSL_SUCCESS);
  45296. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  45297. AssertIntEQ(outKeySz, sizeof(expandOnly));
  45298. AssertIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  45299. outKeySz = sizeof(outKey);
  45300. /* Extract and expand with appended additional info. */
  45301. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  45302. WOLFSSL_SUCCESS);
  45303. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  45304. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  45305. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  45306. AssertIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  45307. AssertIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  45308. EVP_PKEY_CTX_free(ctx);
  45309. res = TEST_RES_CHECK(1);
  45310. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  45311. return res;
  45312. }
  45313. #ifndef NO_BIO
  45314. static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
  45315. {
  45316. int res = TEST_SKIPPED;
  45317. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45318. BIO* bio;
  45319. X509_INFO* info;
  45320. STACK_OF(X509_INFO)* sk;
  45321. char* subject;
  45322. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  45323. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  45324. AssertNotNull(bio = BIO_new(BIO_s_file()));
  45325. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  45326. AssertNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  45327. AssertIntEQ(sk_X509_INFO_num(sk), 2);
  45328. /* using dereference to maintain testing for Apache port*/
  45329. AssertNotNull(info = sk_X509_INFO_pop(sk));
  45330. AssertNotNull(subject =
  45331. X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
  45332. AssertIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  45333. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  45334. X509_INFO_free(info);
  45335. AssertNotNull(info = sk_X509_INFO_pop(sk));
  45336. AssertNotNull(subject =
  45337. X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
  45338. AssertIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  45339. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  45340. X509_INFO_free(info);
  45341. AssertNull(info = sk_X509_INFO_pop(sk));
  45342. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  45343. BIO_free(bio);
  45344. res = TEST_RES_CHECK(1);
  45345. #endif
  45346. return res;
  45347. }
  45348. #endif /* !NO_BIO */
  45349. static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  45350. {
  45351. int res = TEST_SKIPPED;
  45352. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45353. X509 *x509;
  45354. X509_NAME* name;
  45355. int idx = 0;
  45356. X509_NAME_ENTRY *ne;
  45357. ASN1_OBJECT *object = NULL;
  45358. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  45359. AssertNotNull(x509);
  45360. name = X509_get_subject_name(x509);
  45361. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  45362. AssertIntGE(idx, 0);
  45363. ne = X509_NAME_get_entry(name, idx);
  45364. AssertNotNull(ne);
  45365. AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
  45366. X509_free(x509);
  45367. res = TEST_RES_CHECK(1);
  45368. #endif
  45369. return res;
  45370. }
  45371. static int test_wolfSSL_ASN1_INTEGER_get_set(void)
  45372. {
  45373. int res = TEST_SKIPPED;
  45374. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  45375. ASN1_INTEGER *a;
  45376. long val;
  45377. int ret;
  45378. a = ASN1_INTEGER_new();
  45379. val = 0;
  45380. ret = ASN1_INTEGER_set(NULL, val);
  45381. AssertIntEQ(ret, 0);
  45382. ASN1_INTEGER_free(a);
  45383. /* 0 */
  45384. a = ASN1_INTEGER_new();
  45385. val = 0;
  45386. ret = ASN1_INTEGER_set(a, val);
  45387. AssertIntEQ(ret, 1);
  45388. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45389. ASN1_INTEGER_free(a);
  45390. /* 40 */
  45391. a = ASN1_INTEGER_new();
  45392. val = 40;
  45393. ret = ASN1_INTEGER_set(a, val);
  45394. AssertIntEQ(ret, 1);
  45395. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45396. ASN1_INTEGER_free(a);
  45397. /* -40 */
  45398. a = ASN1_INTEGER_new();
  45399. val = -40;
  45400. ret = ASN1_INTEGER_set(a, val);
  45401. AssertIntEQ(ret, 1);
  45402. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45403. ASN1_INTEGER_free(a);
  45404. /* 128 */
  45405. a = ASN1_INTEGER_new();
  45406. val = 128;
  45407. ret = ASN1_INTEGER_set(a, val);
  45408. AssertIntEQ(ret, 1);
  45409. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45410. ASN1_INTEGER_free(a);
  45411. /* -128 */
  45412. a = ASN1_INTEGER_new();
  45413. val = -128;
  45414. ret = ASN1_INTEGER_set(a, val);
  45415. AssertIntEQ(ret, 1);
  45416. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45417. ASN1_INTEGER_free(a);
  45418. /* 200 */
  45419. a = ASN1_INTEGER_new();
  45420. val = 200;
  45421. ret = ASN1_INTEGER_set(a, val);
  45422. AssertIntEQ(ret, 1);
  45423. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45424. ASN1_INTEGER_free(a);
  45425. /* int max (2147483647) */
  45426. a = ASN1_INTEGER_new();
  45427. val = 2147483647;
  45428. ret = ASN1_INTEGER_set(a, val);
  45429. AssertIntEQ(ret, 1);
  45430. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45431. ASN1_INTEGER_free(a);
  45432. /* int min (-2147483648) */
  45433. a = ASN1_INTEGER_new();
  45434. val = -2147483647 - 1;
  45435. ret = ASN1_INTEGER_set(a, val);
  45436. AssertIntEQ(ret, 1);
  45437. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45438. ASN1_INTEGER_free(a);
  45439. res = TEST_RES_CHECK(1);
  45440. #endif
  45441. return res;
  45442. }
  45443. #if defined(OPENSSL_EXTRA)
  45444. typedef struct ASN1IntTestVector {
  45445. const byte* der;
  45446. const size_t derSz;
  45447. const long value;
  45448. } ASN1IntTestVector;
  45449. #endif
  45450. static int test_wolfSSL_d2i_ASN1_INTEGER(void)
  45451. {
  45452. int res = TEST_SKIPPED;
  45453. #if defined(OPENSSL_EXTRA)
  45454. size_t i;
  45455. WOLFSSL_ASN1_INTEGER* a = NULL;
  45456. WOLFSSL_ASN1_INTEGER* b = NULL;
  45457. WOLFSSL_ASN1_INTEGER* c = NULL;
  45458. const byte* p = NULL;
  45459. byte* reEncoded = NULL;
  45460. int reEncodedSz;
  45461. static const byte zeroDer[] = {
  45462. 0x02, 0x01, 0x00
  45463. };
  45464. static const byte oneDer[] = {
  45465. 0x02, 0x01, 0x01
  45466. };
  45467. static const byte negativeDer[] = {
  45468. 0x02, 0x03, 0xC1, 0x16, 0x0D
  45469. };
  45470. static const byte positiveDer[] = {
  45471. 0x02, 0x03, 0x01, 0x00, 0x01
  45472. };
  45473. static const byte primeDer[] = {
  45474. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  45475. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  45476. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  45477. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  45478. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  45479. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  45480. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  45481. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  45482. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  45483. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  45484. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  45485. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  45486. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  45487. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  45488. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  45489. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  45490. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  45491. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  45492. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  45493. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  45494. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  45495. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  45496. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  45497. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  45498. };
  45499. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  45500. static const ASN1IntTestVector testVectors[] = {
  45501. {zeroDer, sizeof(zeroDer), 0},
  45502. {oneDer, sizeof(oneDer), 1},
  45503. {negativeDer, sizeof(negativeDer), -4123123},
  45504. {positiveDer, sizeof(positiveDer), 65537},
  45505. {primeDer, sizeof(primeDer), 0}
  45506. };
  45507. static const size_t NUM_TEST_VECTORS = sizeof(testVectors)/sizeof(testVectors[0]);
  45508. /* Check d2i error conditions */
  45509. /* NULL pointer to input. */
  45510. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  45511. AssertNull(b);
  45512. /* NULL input. */
  45513. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  45514. AssertNull(b);
  45515. /* 0 length. */
  45516. p = testVectors[0].der;
  45517. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  45518. AssertNull(b);
  45519. /* Negative length. */
  45520. p = testVectors[0].der;
  45521. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  45522. AssertNull(b);
  45523. /* Garbage DER input. */
  45524. p = garbageDer;
  45525. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  45526. AssertNull(b);
  45527. {
  45528. /* Check i2d error conditions */
  45529. /* NULL input. */
  45530. byte* p2 = NULL;
  45531. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  45532. /* 0 length input data buffer (a->length == 0). */
  45533. AssertNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  45534. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  45535. a->data = NULL;
  45536. /* NULL input data buffer. */
  45537. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  45538. /* Reset a->data. */
  45539. a->data = a->intData;
  45540. /* Set a to valid value. */
  45541. AssertIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  45542. /* NULL output buffer. */
  45543. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  45544. wolfSSL_ASN1_INTEGER_free(a);
  45545. }
  45546. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  45547. p = testVectors[i].der;
  45548. a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, testVectors[i].derSz);
  45549. AssertIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  45550. if (testVectors[i].derSz <= sizeof(long)) {
  45551. c = wolfSSL_ASN1_INTEGER_new();
  45552. wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value);
  45553. AssertIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  45554. wolfSSL_ASN1_INTEGER_free(c);
  45555. }
  45556. /* Convert to DER without a pre-allocated output buffer. */
  45557. AssertIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  45558. AssertIntEQ(reEncodedSz, testVectors[i].derSz);
  45559. AssertIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  45560. /* Convert to DER with a pre-allocated output buffer. In this case, the
  45561. * output buffer pointer should be incremented just past the end of the
  45562. * encoded data. */
  45563. p = reEncoded;
  45564. AssertIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  45565. AssertIntEQ(reEncodedSz, testVectors[i].derSz);
  45566. AssertPtrEq(p, reEncoded - reEncodedSz);
  45567. AssertIntEQ(XMEMCMP(p, testVectors[i].der, reEncodedSz), 0);
  45568. XFREE(reEncoded - reEncodedSz, NULL, DYNAMIC_TYPE_ASN1);
  45569. reEncoded = NULL;
  45570. wolfSSL_ASN1_INTEGER_free(a);
  45571. }
  45572. res = TEST_RES_CHECK(1);
  45573. #endif /* OPENSSL_EXTRA */
  45574. return res;
  45575. }
  45576. static int test_wolfSSL_X509_STORE_get1_certs(void)
  45577. {
  45578. int res = TEST_SKIPPED;
  45579. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  45580. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45581. X509_STORE_CTX *storeCtx;
  45582. X509_STORE *store;
  45583. X509 *caX509;
  45584. X509 *svrX509;
  45585. X509_NAME *subject;
  45586. WOLF_STACK_OF(WOLFSSL_X509) *certs;
  45587. AssertNotNull(caX509 =
  45588. X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
  45589. AssertNotNull((svrX509 =
  45590. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  45591. AssertNotNull(storeCtx = X509_STORE_CTX_new());
  45592. AssertNotNull(store = X509_STORE_new());
  45593. AssertNotNull(subject = X509_get_subject_name(caX509));
  45594. /* Errors */
  45595. AssertNull(X509_STORE_get1_certs(storeCtx, subject));
  45596. AssertNull(X509_STORE_get1_certs(NULL, subject));
  45597. AssertNull(X509_STORE_get1_certs(storeCtx, NULL));
  45598. AssertIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  45599. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL), SSL_SUCCESS);
  45600. /* Should find the cert */
  45601. AssertNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  45602. AssertIntEQ(1, wolfSSL_sk_X509_num(certs));
  45603. sk_X509_pop_free(certs, NULL);
  45604. /* Should not find the cert */
  45605. AssertNotNull(subject = X509_get_subject_name(svrX509));
  45606. AssertNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  45607. AssertIntEQ(0, wolfSSL_sk_X509_num(certs));
  45608. sk_X509_pop_free(certs, NULL);
  45609. X509_STORE_free(store);
  45610. X509_STORE_CTX_free(storeCtx);
  45611. X509_free(svrX509);
  45612. X509_free(caX509);
  45613. res = TEST_RES_CHECK(1);
  45614. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  45615. return res;
  45616. }
  45617. /* Testing code used in dpp.c in hostap */
  45618. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  45619. typedef struct {
  45620. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  45621. * as an OID identifying the curve */
  45622. X509_ALGOR *alg;
  45623. /* Compressed format public key per ANSI X9.63 */
  45624. ASN1_BIT_STRING *pub_key;
  45625. } DPP_BOOTSTRAPPING_KEY;
  45626. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  45627. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  45628. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  45629. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  45630. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  45631. #endif
  45632. static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  45633. {
  45634. int res = TEST_SKIPPED;
  45635. /* Testing code used in dpp.c in hostap */
  45636. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  45637. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45638. EC_KEY *eckey;
  45639. EVP_PKEY *key;
  45640. size_t len;
  45641. unsigned char *der = NULL;
  45642. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  45643. const unsigned char *in = ecc_clikey_der_256;
  45644. const EC_GROUP *group;
  45645. const EC_POINT *point;
  45646. int nid;
  45647. AssertNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  45648. AssertNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  45649. (long)sizeof_ecc_clikey_der_256));
  45650. AssertNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  45651. AssertNotNull(group = EC_KEY_get0_group(eckey));
  45652. AssertNotNull(point = EC_KEY_get0_public_key(eckey));
  45653. nid = EC_GROUP_get_curve_name(group);
  45654. AssertIntEQ(X509_ALGOR_set0(bootstrap->alg, OBJ_nid2obj(EVP_PKEY_EC),
  45655. V_ASN1_OBJECT, OBJ_nid2obj(nid)), 1);
  45656. #ifdef HAVE_COMP_KEY
  45657. AssertIntGT((len = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  45658. NULL, 0, NULL)), 0);
  45659. #else
  45660. AssertIntGT((len = EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  45661. NULL, 0, NULL)), 0);
  45662. #endif
  45663. AssertNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  45664. #ifdef HAVE_COMP_KEY
  45665. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  45666. der, len, NULL), len);
  45667. #else
  45668. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  45669. der, len, NULL), len);
  45670. #endif
  45671. bootstrap->pub_key->data = der;
  45672. bootstrap->pub_key->length = (int)len;
  45673. /* Not actually used */
  45674. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  45675. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  45676. der = NULL;
  45677. AssertIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  45678. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  45679. EVP_PKEY_free(key);
  45680. EC_KEY_free(eckey);
  45681. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  45682. res = TEST_RES_CHECK(1);
  45683. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  45684. #endif /* WOLFSSL_WPAS && HAVE_ECC && USE_CERT_BUFFERS_256 */
  45685. return res;
  45686. }
  45687. static int test_wolfSSL_i2c_ASN1_INTEGER(void)
  45688. {
  45689. int res = TEST_SKIPPED;
  45690. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  45691. ASN1_INTEGER *a;
  45692. unsigned char *pp,*tpp;
  45693. int ret;
  45694. a = wolfSSL_ASN1_INTEGER_new();
  45695. /* 40 */
  45696. a->intData[0] = ASN_INTEGER;
  45697. a->intData[1] = 1;
  45698. a->intData[2] = 40;
  45699. ret = i2c_ASN1_INTEGER(a, NULL);
  45700. AssertIntEQ(ret, 1);
  45701. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  45702. DYNAMIC_TYPE_TMP_BUFFER));
  45703. tpp = pp;
  45704. XMEMSET(pp, 0, ret + 1);
  45705. i2c_ASN1_INTEGER(a, &pp);
  45706. pp--;
  45707. AssertIntEQ(*pp, 40);
  45708. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45709. /* 128 */
  45710. a->intData[0] = ASN_INTEGER;
  45711. a->intData[1] = 1;
  45712. a->intData[2] = 128;
  45713. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  45714. AssertIntEQ(ret, 2);
  45715. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  45716. DYNAMIC_TYPE_TMP_BUFFER));
  45717. tpp = pp;
  45718. XMEMSET(pp, 0, ret + 1);
  45719. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  45720. pp--;
  45721. AssertIntEQ(*(pp--), 128);
  45722. AssertIntEQ(*pp, 0);
  45723. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45724. /* -40 */
  45725. a->intData[0] = ASN_INTEGER;
  45726. a->intData[1] = 1;
  45727. a->intData[2] = 40;
  45728. a->negative = 1;
  45729. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  45730. AssertIntEQ(ret, 1);
  45731. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  45732. DYNAMIC_TYPE_TMP_BUFFER));
  45733. tpp = pp;
  45734. XMEMSET(pp, 0, ret + 1);
  45735. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  45736. pp--;
  45737. AssertIntEQ(*pp, 216);
  45738. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45739. /* -128 */
  45740. a->intData[0] = ASN_INTEGER;
  45741. a->intData[1] = 1;
  45742. a->intData[2] = 128;
  45743. a->negative = 1;
  45744. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  45745. AssertIntEQ(ret, 1);
  45746. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  45747. DYNAMIC_TYPE_TMP_BUFFER));
  45748. tpp = pp;
  45749. XMEMSET(pp, 0, ret + 1);
  45750. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  45751. pp--;
  45752. AssertIntEQ(*pp, 128);
  45753. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45754. /* -200 */
  45755. a->intData[0] = ASN_INTEGER;
  45756. a->intData[1] = 1;
  45757. a->intData[2] = 200;
  45758. a->negative = 1;
  45759. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  45760. AssertIntEQ(ret, 2);
  45761. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  45762. DYNAMIC_TYPE_TMP_BUFFER));
  45763. tpp = pp;
  45764. XMEMSET(pp, 0, ret + 1);
  45765. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  45766. pp--;
  45767. AssertIntEQ(*(pp--), 56);
  45768. AssertIntEQ(*pp, 255);
  45769. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45770. wolfSSL_ASN1_INTEGER_free(a);
  45771. res = TEST_RES_CHECK(1);
  45772. #endif /* OPENSSL_EXTRA && !NO_ASN */
  45773. return res;
  45774. }
  45775. #ifndef NO_INLINE
  45776. #define WOLFSSL_MISC_INCLUDED
  45777. #include <wolfcrypt/src/misc.c>
  45778. #else
  45779. #include <wolfssl/wolfcrypt/misc.h>
  45780. #endif
  45781. static int test_ForceZero(void)
  45782. {
  45783. unsigned char data[32];
  45784. unsigned int i, j, len;
  45785. /* Test case with 0 length */
  45786. ForceZero(data, 0);
  45787. /* Test ForceZero */
  45788. for (i = 0; i < sizeof(data); i++) {
  45789. for (len = 1; len < sizeof(data) - i; len++) {
  45790. for (j = 0; j < sizeof(data); j++)
  45791. data[j] = j + 1;
  45792. ForceZero(data + i, len);
  45793. for (j = 0; j < sizeof(data); j++) {
  45794. if (j < i || j >= i + len) {
  45795. if (data[j] == 0x00)
  45796. return -10200;
  45797. }
  45798. else if (data[j] != 0x00)
  45799. return -10201;
  45800. }
  45801. }
  45802. }
  45803. return TEST_RES_CHECK(1);
  45804. }
  45805. #ifndef NO_BIO
  45806. static int test_wolfSSL_X509_print(void)
  45807. {
  45808. int res = TEST_SKIPPED;
  45809. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  45810. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(XSNPRINTF)
  45811. X509 *x509;
  45812. BIO *bio;
  45813. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  45814. const X509_ALGOR *cert_sig_alg;
  45815. #endif
  45816. x509 = X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  45817. AssertNotNull(x509);
  45818. /* print to memory */
  45819. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  45820. AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  45821. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  45822. #if defined(WC_DISABLE_RADIX_ZERO_PAD)
  45823. /* Will print IP address subject alt name. */
  45824. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3349);
  45825. #else
  45826. /* Will print IP address subject alt name. */
  45827. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3350);
  45828. #endif
  45829. #else
  45830. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3328);
  45831. #endif
  45832. BIO_free(bio);
  45833. AssertNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  45834. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  45835. /* Print signature */
  45836. AssertNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  45837. AssertIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  45838. #endif
  45839. /* print to stderr */
  45840. #if !defined(NO_WOLFSSL_DIR)
  45841. AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  45842. #endif
  45843. /* print again */
  45844. AssertIntEQ(X509_print_fp(stderr, x509), SSL_SUCCESS);
  45845. X509_free(x509);
  45846. BIO_free(bio);
  45847. res = TEST_RES_CHECK(1);
  45848. #endif
  45849. return res;
  45850. }
  45851. static int test_wolfSSL_X509_CRL_print(void)
  45852. {
  45853. int res = TEST_SKIPPED;
  45854. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
  45855. && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
  45856. X509_CRL* crl;
  45857. BIO *bio;
  45858. XFILE fp;
  45859. fp = XFOPEN("./certs/crl/crl.pem", "rb");
  45860. AssertTrue((fp != XBADFILE));
  45861. AssertNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  45862. NULL, NULL));
  45863. XFCLOSE(fp);
  45864. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  45865. AssertIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
  45866. X509_CRL_free(crl);
  45867. BIO_free(bio);
  45868. res = TEST_RES_CHECK(1);
  45869. #endif
  45870. return res;
  45871. }
  45872. static int test_wolfSSL_BIO_get_len(void)
  45873. {
  45874. int res = TEST_SKIPPED;
  45875. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  45876. BIO *bio = NULL;
  45877. const char txt[] = "Some example text to push to the BIO.";
  45878. AssertIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  45879. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  45880. AssertIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  45881. AssertIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  45882. BIO_free(bio);
  45883. AssertNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  45884. AssertIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  45885. BIO_free(bio);
  45886. res = TEST_RES_CHECK(1);
  45887. #endif
  45888. return res;
  45889. }
  45890. static int test_wolfSSL_ASN1_STRING_print(void)
  45891. {
  45892. int res = TEST_SKIPPED;
  45893. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS)
  45894. ASN1_STRING* asnStr = NULL;
  45895. const char HELLO_DATA[]= \
  45896. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  45897. #define MAX_UNPRINTABLE_CHAR 32
  45898. #define MAX_BUF 255
  45899. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  45900. unsigned char expected[sizeof(unprintableData)+1];
  45901. unsigned char rbuf[MAX_BUF];
  45902. BIO *bio;
  45903. int p_len, i;
  45904. /* setup */
  45905. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  45906. unprintableData[i] = HELLO_DATA[i];
  45907. expected[i] = HELLO_DATA[i];
  45908. }
  45909. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  45910. unprintableData[sizeof(HELLO_DATA)+i] = i;
  45911. if (i == (int)'\n' || i == (int)'\r')
  45912. expected[sizeof(HELLO_DATA)+i] = i;
  45913. else
  45914. expected[sizeof(HELLO_DATA)+i] = '.';
  45915. }
  45916. unprintableData[sizeof(unprintableData)-1] = '\0';
  45917. expected[sizeof(expected)-1] = '\0';
  45918. XMEMSET(rbuf, 0, MAX_BUF);
  45919. bio = BIO_new(BIO_s_mem());
  45920. BIO_set_write_buf_size(bio, MAX_BUF);
  45921. asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  45922. ASN1_STRING_set(asnStr,(const void*)unprintableData,
  45923. (int)sizeof(unprintableData));
  45924. /* test */
  45925. p_len = wolfSSL_ASN1_STRING_print(bio, asnStr);
  45926. AssertIntEQ(p_len, 46);
  45927. BIO_read(bio, (void*)rbuf, 46);
  45928. AssertStrEQ((char*)rbuf, (const char*)expected);
  45929. BIO_free(bio);
  45930. ASN1_STRING_free(asnStr);
  45931. res = TEST_RES_CHECK(1);
  45932. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS */
  45933. return res;
  45934. }
  45935. #endif /* !NO_BIO */
  45936. static int test_wolfSSL_ASN1_get_object(void)
  45937. {
  45938. int res = TEST_SKIPPED;
  45939. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  45940. const unsigned char* derBuf = cliecc_cert_der_256;
  45941. int len = sizeof_cliecc_cert_der_256;
  45942. long asnLen = 0;
  45943. int tag = 0, cls = 0;
  45944. ASN1_OBJECT *a;
  45945. /* Read a couple TLV triplets and make sure they match the expected values */
  45946. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  45947. AssertIntEQ(asnLen, 862);
  45948. AssertIntEQ(tag, 0x10);
  45949. AssertIntEQ(cls, 0);
  45950. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  45951. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  45952. AssertIntEQ(asnLen, 772);
  45953. AssertIntEQ(tag, 0x10);
  45954. AssertIntEQ(cls, 0);
  45955. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  45956. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  45957. AssertIntEQ(asnLen, 3);
  45958. AssertIntEQ(tag, 0);
  45959. AssertIntEQ(cls, 0x80);
  45960. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  45961. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  45962. AssertIntEQ(asnLen, 1);
  45963. AssertIntEQ(tag, 0x2);
  45964. AssertIntEQ(cls, 0);
  45965. derBuf += asnLen;
  45966. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  45967. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  45968. AssertIntEQ(asnLen, 20);
  45969. AssertIntEQ(tag, 0x2);
  45970. AssertIntEQ(cls, 0);
  45971. derBuf += asnLen;
  45972. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  45973. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  45974. AssertIntEQ(asnLen, 10);
  45975. AssertIntEQ(tag, 0x10);
  45976. AssertIntEQ(cls, 0);
  45977. /* Read an ASN OBJECT */
  45978. AssertNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  45979. ASN1_OBJECT_free(a);
  45980. res = TEST_RES_CHECK(1);
  45981. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  45982. return res;
  45983. }
  45984. static int test_wolfSSL_RSA(void)
  45985. {
  45986. int res = TEST_SKIPPED;
  45987. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  45988. defined(WOLFSSL_KEY_GEN)
  45989. RSA* rsa;
  45990. const BIGNUM *n;
  45991. const BIGNUM *e;
  45992. const BIGNUM *d;
  45993. const BIGNUM *p;
  45994. const BIGNUM *q;
  45995. const BIGNUM *dmp1;
  45996. const BIGNUM *dmq1;
  45997. const BIGNUM *iqmp;
  45998. AssertNotNull(rsa = RSA_new());
  45999. AssertIntEQ(RSA_size(NULL), 0);
  46000. AssertIntEQ(RSA_size(rsa), 0);
  46001. AssertIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  46002. AssertIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  46003. AssertIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  46004. #ifdef WOLFSSL_RSA_KEY_CHECK
  46005. AssertIntEQ(RSA_check_key(rsa), 0);
  46006. #endif
  46007. RSA_free(rsa);
  46008. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46009. AssertIntEQ(RSA_size(rsa), 256);
  46010. #ifdef WOLFSSL_RSA_KEY_CHECK
  46011. AssertIntEQ(RSA_check_key(NULL), 0);
  46012. AssertIntEQ(RSA_check_key(rsa), 1);
  46013. #endif
  46014. /* sanity check */
  46015. AssertIntEQ(RSA_bits(NULL), 0);
  46016. /* key */
  46017. AssertIntEQ(RSA_bits(rsa), 2048);
  46018. RSA_get0_key(rsa, &n, &e, &d);
  46019. AssertPtrEq(rsa->n, n);
  46020. AssertPtrEq(rsa->e, e);
  46021. AssertPtrEq(rsa->d, d);
  46022. AssertNotNull(n = BN_new());
  46023. AssertNotNull(e = BN_new());
  46024. AssertNotNull(d = BN_new());
  46025. AssertIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  46026. AssertPtrEq(rsa->n, n);
  46027. AssertPtrEq(rsa->e, e);
  46028. AssertPtrEq(rsa->d, d);
  46029. AssertIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  46030. AssertIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  46031. /* crt_params */
  46032. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  46033. AssertPtrEq(rsa->dmp1, dmp1);
  46034. AssertPtrEq(rsa->dmq1, dmq1);
  46035. AssertPtrEq(rsa->iqmp, iqmp);
  46036. AssertNotNull(dmp1 = BN_new());
  46037. AssertNotNull(dmq1 = BN_new());
  46038. AssertNotNull(iqmp = BN_new());
  46039. AssertIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  46040. (BIGNUM*)iqmp), 1);
  46041. AssertPtrEq(rsa->dmp1, dmp1);
  46042. AssertPtrEq(rsa->dmq1, dmq1);
  46043. AssertPtrEq(rsa->iqmp, iqmp);
  46044. AssertIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  46045. AssertIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  46046. (BIGNUM*)iqmp), 0);
  46047. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  46048. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  46049. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  46050. AssertNull(dmp1);
  46051. AssertNull(dmq1);
  46052. AssertNull(iqmp);
  46053. /* factors */
  46054. RSA_get0_factors(rsa, NULL, NULL);
  46055. RSA_get0_factors(rsa, &p, &q);
  46056. AssertPtrEq(rsa->p, p);
  46057. AssertPtrEq(rsa->q, q);
  46058. AssertNotNull(p = BN_new());
  46059. AssertNotNull(q = BN_new());
  46060. AssertIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  46061. AssertPtrEq(rsa->p, p);
  46062. AssertPtrEq(rsa->q, q);
  46063. AssertIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  46064. AssertIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  46065. RSA_get0_factors(NULL, NULL, NULL);
  46066. RSA_get0_factors(NULL, &p, &q);
  46067. AssertNull(p);
  46068. AssertNull(q);
  46069. AssertIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  46070. AssertIntEQ(RSA_bits(rsa), 21);
  46071. RSA_free(rsa);
  46072. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  46073. AssertNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  46074. AssertIntEQ(RSA_size(rsa), 384);
  46075. AssertIntEQ(RSA_bits(rsa), 3072);
  46076. RSA_free(rsa);
  46077. #endif
  46078. /* remove for now with odd key size until adjusting rsa key size check with
  46079. wc_MakeRsaKey()
  46080. AssertNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  46081. RSA_free(rsa);
  46082. */
  46083. AssertNull(RSA_generate_key(-1, 3, NULL, NULL));
  46084. AssertNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  46085. AssertNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  46086. AssertNull(RSA_generate_key(2048, 0, NULL, NULL));
  46087. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  46088. {
  46089. byte buff[FOURK_BUF];
  46090. byte der[FOURK_BUF];
  46091. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  46092. XFILE f;
  46093. int bytes;
  46094. /* test loading encrypted RSA private pem w/o password */
  46095. f = XFOPEN(PrivKeyPemFile, "rb");
  46096. AssertTrue((f != XBADFILE));
  46097. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  46098. XFCLOSE(f);
  46099. XMEMSET(der, 0, sizeof(der));
  46100. /* test that error value is returned with no password */
  46101. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""), 0);
  46102. }
  46103. #endif
  46104. res = TEST_RES_CHECK(1);
  46105. #endif
  46106. return res;
  46107. }
  46108. static int test_wolfSSL_RSA_DER(void)
  46109. {
  46110. int res = TEST_SKIPPED;
  46111. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46112. !defined(NO_RSA) && !defined(HAVE_USER_RSA) && defined(OPENSSL_EXTRA)
  46113. RSA *rsa;
  46114. int i;
  46115. const unsigned char *buff = NULL;
  46116. unsigned char *newBuff = NULL;
  46117. struct tbl_s
  46118. {
  46119. const unsigned char *der;
  46120. int sz;
  46121. } tbl[] = {
  46122. #ifdef USE_CERT_BUFFERS_1024
  46123. {client_key_der_1024, sizeof_client_key_der_1024},
  46124. {server_key_der_1024, sizeof_server_key_der_1024},
  46125. #endif
  46126. #ifdef USE_CERT_BUFFERS_2048
  46127. {client_key_der_2048, sizeof_client_key_der_2048},
  46128. {server_key_der_2048, sizeof_server_key_der_2048},
  46129. #endif
  46130. {NULL, 0}
  46131. };
  46132. /* Public Key DER */
  46133. struct tbl_s pub[] = {
  46134. #ifdef USE_CERT_BUFFERS_1024
  46135. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  46136. #endif
  46137. #ifdef USE_CERT_BUFFERS_2048
  46138. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  46139. #endif
  46140. {NULL, 0}
  46141. };
  46142. AssertNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  46143. buff = pub[0].der;
  46144. AssertNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  46145. AssertNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  46146. buff = tbl[0].der;
  46147. AssertNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  46148. AssertIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  46149. rsa = RSA_new();
  46150. AssertIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  46151. RSA_free(rsa);
  46152. for (i = 0; tbl[i].der != NULL; i++)
  46153. {
  46154. /* Passing in pointer results in pointer moving. */
  46155. buff = tbl[i].der;
  46156. AssertNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  46157. AssertNotNull(rsa);
  46158. RSA_free(rsa);
  46159. }
  46160. for (i = 0; tbl[i].der != NULL; i++)
  46161. {
  46162. /* Passing in pointer results in pointer moving. */
  46163. buff = tbl[i].der;
  46164. AssertNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  46165. AssertNotNull(rsa);
  46166. RSA_free(rsa);
  46167. }
  46168. for (i = 0; pub[i].der != NULL; i++)
  46169. {
  46170. buff = pub[i].der;
  46171. AssertNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  46172. AssertNotNull(rsa);
  46173. AssertIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  46174. newBuff = NULL;
  46175. AssertIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  46176. AssertNotNull(newBuff);
  46177. AssertIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  46178. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46179. RSA_free(rsa);
  46180. }
  46181. res = TEST_RES_CHECK(1);
  46182. #endif
  46183. return res;
  46184. }
  46185. static int test_wolfSSL_RSA_print(void)
  46186. {
  46187. int res = TEST_SKIPPED;
  46188. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  46189. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46190. !defined(HAVE_FAST_RSA) && !defined(NO_BIO)
  46191. BIO *bio;
  46192. WOLFSSL_RSA* rsa = NULL;
  46193. AssertNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  46194. AssertNotNull(rsa = RSA_new());
  46195. AssertIntEQ(RSA_print(NULL, rsa, 0), -1);
  46196. AssertIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  46197. AssertIntEQ(RSA_print(bio, NULL, 0), -1);
  46198. AssertIntEQ(RSA_print_fp(stderr, NULL, 0), 0);
  46199. /* Some very large number of indent spaces. */
  46200. AssertIntEQ(RSA_print(bio, rsa, 128), -1);
  46201. /* RSA is empty. */
  46202. AssertIntEQ(RSA_print(bio, rsa, 0), 0);
  46203. AssertIntEQ(RSA_print_fp(stderr, rsa, 0), 0);
  46204. RSA_free(rsa);
  46205. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46206. AssertIntEQ(RSA_print(bio, rsa, 0), 1);
  46207. AssertIntEQ(RSA_print(bio, rsa, 4), 1);
  46208. AssertIntEQ(RSA_print(bio, rsa, -1), 1);
  46209. AssertIntEQ(RSA_print_fp(stderr, rsa, 0), 1);
  46210. AssertIntEQ(RSA_print_fp(stderr, rsa, 4), 1);
  46211. AssertIntEQ(RSA_print_fp(stderr, rsa, -1), 1);
  46212. BIO_free(bio);
  46213. RSA_free(rsa);
  46214. res = TEST_RES_CHECK(1);
  46215. #endif
  46216. return res;
  46217. }
  46218. #ifndef NO_RSA
  46219. static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  46220. {
  46221. int res = TEST_SKIPPED;
  46222. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  46223. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46224. RSA *rsa;
  46225. const unsigned char *derBuf = client_key_der_2048;
  46226. unsigned char em[256] = {0}; /* len = 2048/8 */
  46227. /* Random data simulating a hash */
  46228. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  46229. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  46230. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  46231. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  46232. };
  46233. AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  46234. AssertIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  46235. RSA_PSS_SALTLEN_DIGEST), 0);
  46236. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  46237. RSA_PSS_SALTLEN_DIGEST), 0);
  46238. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  46239. RSA_PSS_SALTLEN_DIGEST), 0);
  46240. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  46241. RSA_PSS_SALTLEN_DIGEST), 0);
  46242. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  46243. AssertIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  46244. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  46245. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  46246. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  46247. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  46248. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  46249. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  46250. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  46251. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  46252. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  46253. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  46254. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  46255. RSA_PSS_SALTLEN_DIGEST), 1);
  46256. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  46257. RSA_PSS_SALTLEN_DIGEST), 1);
  46258. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  46259. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  46260. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  46261. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  46262. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  46263. RSA_PSS_SALTLEN_MAX), 1);
  46264. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  46265. RSA_PSS_SALTLEN_MAX), 1);
  46266. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  46267. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  46268. RSA_free(rsa);
  46269. res = TEST_RES_CHECK(1);
  46270. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  46271. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  46272. return res;
  46273. }
  46274. #endif
  46275. static int test_wolfSSL_RSA_sign_sha3(void)
  46276. {
  46277. int res = TEST_SKIPPED;
  46278. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  46279. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  46280. RSA *rsa;
  46281. const unsigned char *derBuf = client_key_der_2048;
  46282. unsigned char sigRet[256] = {0};
  46283. unsigned int sigLen = sizeof(sigRet);
  46284. /* Random data simulating a hash */
  46285. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  46286. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  46287. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  46288. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  46289. };
  46290. AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  46291. AssertIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet,
  46292. &sigLen, rsa), 1);
  46293. RSA_free(rsa);
  46294. res = TEST_RES_CHECK(1);
  46295. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  46296. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  46297. return res;
  46298. }
  46299. static int test_wolfSSL_RSA_get0_key(void)
  46300. {
  46301. int res = TEST_SKIPPED;
  46302. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  46303. RSA *rsa = NULL;
  46304. const BIGNUM* n = NULL;
  46305. const BIGNUM* e = NULL;
  46306. const BIGNUM* d = NULL;
  46307. const unsigned char* der;
  46308. int derSz;
  46309. #ifdef USE_CERT_BUFFERS_1024
  46310. der = client_key_der_1024;
  46311. derSz = sizeof_client_key_der_1024;
  46312. #elif defined(USE_CERT_BUFFERS_2048)
  46313. der = client_key_der_2048;
  46314. derSz = sizeof_client_key_der_2048;
  46315. #else
  46316. der = NULL;
  46317. derSz = 0;
  46318. #endif
  46319. if (der != NULL) {
  46320. RSA_get0_key(NULL, NULL, NULL, NULL);
  46321. RSA_get0_key(rsa, NULL, NULL, NULL);
  46322. RSA_get0_key(NULL, &n, &e, &d);
  46323. AssertNull(n);
  46324. AssertNull(e);
  46325. AssertNull(d);
  46326. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  46327. AssertNotNull(rsa);
  46328. RSA_get0_key(rsa, NULL, NULL, NULL);
  46329. RSA_get0_key(rsa, &n, NULL, NULL);
  46330. AssertNotNull(n);
  46331. RSA_get0_key(rsa, NULL, &e, NULL);
  46332. AssertNotNull(e);
  46333. RSA_get0_key(rsa, NULL, NULL, &d);
  46334. AssertNotNull(d);
  46335. RSA_get0_key(rsa, &n, &e, &d);
  46336. AssertNotNull(n);
  46337. AssertNotNull(e);
  46338. AssertNotNull(d);
  46339. RSA_free(rsa);
  46340. }
  46341. res = TEST_RES_CHECK(1);
  46342. #endif
  46343. return res;
  46344. }
  46345. static int test_wolfSSL_RSA_meth(void)
  46346. {
  46347. int res = TEST_SKIPPED;
  46348. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  46349. RSA *rsa;
  46350. RSA_METHOD *rsa_meth;
  46351. #ifdef WOLFSSL_KEY_GEN
  46352. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46353. RSA_free(rsa);
  46354. #else
  46355. AssertNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46356. #endif
  46357. AssertNotNull(RSA_get_default_method());
  46358. wolfSSL_RSA_meth_free(NULL);
  46359. AssertNull(wolfSSL_RSA_meth_new(NULL, 0));
  46360. AssertNotNull(rsa_meth =
  46361. RSA_meth_new("placeholder RSA method", RSA_METHOD_FLAG_NO_CHECK));
  46362. #ifndef NO_WOLFSSL_STUB
  46363. AssertIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  46364. AssertIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  46365. AssertIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  46366. AssertIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  46367. AssertIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  46368. AssertIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  46369. AssertIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  46370. #endif
  46371. AssertIntEQ(RSA_flags(NULL), 0);
  46372. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  46373. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  46374. AssertIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  46375. AssertNotNull(rsa = RSA_new());
  46376. /* No method set. */
  46377. AssertIntEQ(RSA_flags(rsa), 0);
  46378. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  46379. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  46380. AssertIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  46381. AssertIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  46382. AssertIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  46383. AssertNull(RSA_get_method(NULL));
  46384. AssertPtrEq(RSA_get_method(rsa), rsa_meth);
  46385. AssertIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  46386. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  46387. AssertIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  46388. AssertIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  46389. RSA_METHOD_FLAG_NO_CHECK);
  46390. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  46391. AssertIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  46392. AssertIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  46393. /* rsa_meth is freed here */
  46394. RSA_free(rsa);
  46395. res = TEST_RES_CHECK(1);
  46396. #endif
  46397. return res;
  46398. }
  46399. static int test_wolfSSL_RSA_verify(void)
  46400. {
  46401. int res = TEST_SKIPPED;
  46402. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && \
  46403. !defined(NO_FILESYSTEM)
  46404. #ifndef NO_BIO
  46405. XFILE fp;
  46406. RSA *pKey, *pubKey;
  46407. X509 *cert;
  46408. const char *text = "Hello wolfSSL !";
  46409. unsigned char hash[SHA256_DIGEST_LENGTH];
  46410. unsigned char signature[2048/8];
  46411. unsigned int signatureLength;
  46412. byte *buf;
  46413. BIO *bio;
  46414. SHA256_CTX c;
  46415. EVP_PKEY *evpPkey, *evpPubkey;
  46416. size_t sz;
  46417. /* generate hash */
  46418. SHA256_Init(&c);
  46419. SHA256_Update(&c, text, strlen(text));
  46420. SHA256_Final(hash, &c);
  46421. #ifdef WOLFSSL_SMALL_STACK_CACHE
  46422. /* workaround for small stack cache case */
  46423. wc_Sha256Free((wc_Sha256*)&c);
  46424. #endif
  46425. /* read privete key file */
  46426. fp = XFOPEN(svrKeyFile, "rb");
  46427. AssertTrue((fp != XBADFILE));
  46428. AssertIntGE(XFSEEK(fp, 0, XSEEK_END), 0);
  46429. sz = XFTELL(fp);
  46430. XREWIND(fp);
  46431. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  46432. AssertIntEQ(XFREAD(buf, 1, sz, fp), sz);
  46433. XFCLOSE(fp);
  46434. /* read private key and sign hash data */
  46435. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  46436. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  46437. AssertNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  46438. AssertIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  46439. signature, &signatureLength, pKey), SSL_SUCCESS);
  46440. /* read public key and verify signed data */
  46441. fp = XFOPEN(svrCertFile,"rb");
  46442. AssertTrue((fp != XBADFILE));
  46443. cert = PEM_read_X509(fp, 0, 0, 0 );
  46444. XFCLOSE(fp);
  46445. evpPubkey = X509_get_pubkey(cert);
  46446. pubKey = EVP_PKEY_get1_RSA(evpPubkey);
  46447. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  46448. signatureLength, pubKey), SSL_SUCCESS);
  46449. AssertIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  46450. signatureLength, NULL), SSL_FAILURE);
  46451. AssertIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  46452. signatureLength, pubKey), SSL_FAILURE);
  46453. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  46454. signatureLength, pubKey), SSL_FAILURE);
  46455. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  46456. signatureLength, NULL), SSL_FAILURE);
  46457. RSA_free(pKey);
  46458. EVP_PKEY_free(evpPkey);
  46459. RSA_free(pubKey);
  46460. EVP_PKEY_free(evpPubkey);
  46461. X509_free(cert);
  46462. BIO_free(bio);
  46463. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  46464. res = TEST_RES_CHECK(1);
  46465. #endif
  46466. #endif
  46467. return res;
  46468. }
  46469. static int test_wolfSSL_RSA_sign(void)
  46470. {
  46471. int res = TEST_SKIPPED;
  46472. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  46473. RSA *rsa;
  46474. unsigned char hash[SHA256_DIGEST_LENGTH];
  46475. #ifdef USE_CERT_BUFFERS_1024
  46476. const unsigned char* privDer = client_key_der_1024;
  46477. size_t privDerSz = sizeof_client_key_der_1024;
  46478. const unsigned char* pubDer = client_keypub_der_1024;
  46479. size_t pubDerSz = sizeof_client_keypub_der_1024;
  46480. unsigned char signature[1024/8];
  46481. #else
  46482. const unsigned char* privDer = client_key_der_2048;
  46483. size_t privDerSz = sizeof_client_key_der_2048;
  46484. const unsigned char* pubDer = client_keypub_der_2048;
  46485. size_t pubDerSz = sizeof_client_keypub_der_2048;
  46486. unsigned char signature[2048/8];
  46487. #endif
  46488. unsigned int signatureLen;
  46489. const unsigned char* der;
  46490. XMEMSET(hash, 0, sizeof(hash));
  46491. der = privDer;
  46492. rsa = NULL;
  46493. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  46494. AssertIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  46495. AssertIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  46496. &signatureLen, rsa), 0);
  46497. AssertIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  46498. &signatureLen, rsa), 0);
  46499. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  46500. &signatureLen, rsa), 0);
  46501. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  46502. NULL, rsa), 0);
  46503. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  46504. &signatureLen, NULL), 0);
  46505. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  46506. &signatureLen, rsa), 1);
  46507. RSA_free(rsa);
  46508. der = pubDer;
  46509. rsa = NULL;
  46510. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  46511. AssertIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  46512. signatureLen, rsa), 1);
  46513. RSA_free(rsa);
  46514. res = TEST_RES_CHECK(1);
  46515. #endif
  46516. return res;
  46517. }
  46518. static int test_wolfSSL_RSA_sign_ex(void)
  46519. {
  46520. int res = TEST_SKIPPED;
  46521. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  46522. RSA *rsa;
  46523. unsigned char hash[SHA256_DIGEST_LENGTH];
  46524. #ifdef USE_CERT_BUFFERS_1024
  46525. const unsigned char* privDer = client_key_der_1024;
  46526. size_t privDerSz = sizeof_client_key_der_1024;
  46527. const unsigned char* pubDer = client_keypub_der_1024;
  46528. size_t pubDerSz = sizeof_client_keypub_der_1024;
  46529. unsigned char signature[1024/8];
  46530. #else
  46531. const unsigned char* privDer = client_key_der_2048;
  46532. size_t privDerSz = sizeof_client_key_der_2048;
  46533. const unsigned char* pubDer = client_keypub_der_2048;
  46534. size_t pubDerSz = sizeof_client_keypub_der_2048;
  46535. unsigned char signature[2048/8];
  46536. #endif
  46537. unsigned int signatureLen;
  46538. const unsigned char* der;
  46539. unsigned char encodedHash[51];
  46540. unsigned int encodedHashLen;
  46541. const unsigned char expEncHash[] = {
  46542. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  46543. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  46544. 0x00, 0x04, 0x20,
  46545. /* Hash data */
  46546. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46547. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46548. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46549. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46550. };
  46551. XMEMSET(hash, 0, sizeof(hash));
  46552. AssertNotNull(rsa = wolfSSL_RSA_new());
  46553. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  46554. &signatureLen, rsa, 1), 0);
  46555. wolfSSL_RSA_free(rsa);
  46556. der = privDer;
  46557. rsa = NULL;
  46558. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  46559. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  46560. -1), 0);
  46561. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  46562. signature, &signatureLen, rsa, 1), 0);
  46563. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  46564. &signatureLen, rsa, 1), 0);
  46565. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  46566. &signatureLen, rsa, 1), 0);
  46567. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  46568. NULL, rsa, 1), 0);
  46569. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  46570. &signatureLen, NULL, 1), 0);
  46571. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  46572. &signatureLen, rsa, -1), 0);
  46573. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  46574. &signatureLen, rsa, 0), 0);
  46575. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  46576. &signatureLen, rsa, 0), 0);
  46577. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  46578. NULL, rsa, 0), 0);
  46579. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  46580. &signatureLen, rsa, 1), 1);
  46581. /* Test returning encoded hash. */
  46582. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  46583. &encodedHashLen, rsa, 0), 1);
  46584. AssertIntEQ(encodedHashLen, sizeof(expEncHash));
  46585. AssertIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  46586. RSA_free(rsa);
  46587. der = pubDer;
  46588. rsa = NULL;
  46589. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  46590. AssertIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  46591. signatureLen, rsa), 1);
  46592. RSA_free(rsa);
  46593. res = TEST_RES_CHECK(1);
  46594. #endif
  46595. return res;
  46596. }
  46597. static int test_wolfSSL_RSA_public_decrypt(void)
  46598. {
  46599. int res = TEST_SKIPPED;
  46600. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  46601. RSA *rsa;
  46602. unsigned char msg[SHA256_DIGEST_LENGTH];
  46603. #ifdef USE_CERT_BUFFERS_1024
  46604. const unsigned char* pubDer = client_keypub_der_1024;
  46605. size_t pubDerSz = sizeof_client_keypub_der_1024;
  46606. unsigned char decMsg[1024/8];
  46607. const unsigned char encMsg[] = {
  46608. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  46609. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  46610. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  46611. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  46612. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  46613. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  46614. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  46615. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  46616. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  46617. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  46618. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  46619. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  46620. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  46621. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  46622. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  46623. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  46624. };
  46625. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  46626. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  46627. defined(WC_RSA_NO_PADDING)
  46628. const unsigned char encMsgNoPad[] = {
  46629. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  46630. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  46631. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  46632. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  46633. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  46634. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  46635. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  46636. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  46637. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  46638. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  46639. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  46640. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  46641. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  46642. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  46643. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  46644. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  46645. };
  46646. #endif
  46647. #else
  46648. const unsigned char* pubDer = client_keypub_der_2048;
  46649. size_t pubDerSz = sizeof_client_keypub_der_2048;
  46650. unsigned char decMsg[2048/8];
  46651. const unsigned char encMsg[] = {
  46652. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  46653. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  46654. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  46655. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  46656. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  46657. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  46658. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  46659. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  46660. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  46661. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  46662. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  46663. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  46664. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  46665. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  46666. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  46667. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  46668. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  46669. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  46670. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  46671. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  46672. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  46673. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  46674. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  46675. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  46676. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  46677. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  46678. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  46679. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  46680. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  46681. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  46682. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  46683. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  46684. };
  46685. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  46686. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  46687. defined(WC_RSA_NO_PADDING)
  46688. const unsigned char encMsgNoPad[] = {
  46689. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  46690. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  46691. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  46692. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  46693. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  46694. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  46695. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  46696. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  46697. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  46698. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  46699. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  46700. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  46701. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  46702. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  46703. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  46704. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  46705. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  46706. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  46707. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  46708. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  46709. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  46710. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  46711. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  46712. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  46713. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  46714. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  46715. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  46716. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  46717. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  46718. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  46719. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  46720. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  46721. };
  46722. #endif
  46723. #endif
  46724. const unsigned char* der;
  46725. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  46726. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  46727. defined(WC_RSA_NO_PADDING)
  46728. int i;
  46729. #endif
  46730. XMEMSET(msg, 0, sizeof(msg));
  46731. der = pubDer;
  46732. rsa = NULL;
  46733. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  46734. AssertIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  46735. AssertIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  46736. RSA_PKCS1_PADDING), -1);
  46737. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  46738. RSA_PKCS1_PADDING), -1);
  46739. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  46740. RSA_PKCS1_PADDING), -1);
  46741. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  46742. RSA_PKCS1_PADDING), -1);
  46743. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  46744. RSA_PKCS1_PSS_PADDING), -1);
  46745. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  46746. RSA_PKCS1_PADDING), 32);
  46747. AssertIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  46748. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  46749. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  46750. defined(WC_RSA_NO_PADDING)
  46751. AssertIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  46752. rsa, RSA_NO_PADDING), sizeof(decMsg));
  46753. /* Zeros before actual data. */
  46754. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  46755. AssertIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  46756. }
  46757. /* Check actual data. */
  46758. XMEMSET(msg, 0x01, sizeof(msg));
  46759. AssertIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  46760. #endif
  46761. RSA_free(rsa);
  46762. res = TEST_RES_CHECK(1);
  46763. #endif
  46764. return res;
  46765. }
  46766. static int test_wolfSSL_RSA_private_encrypt(void)
  46767. {
  46768. int res = TEST_SKIPPED;
  46769. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  46770. RSA *rsa;
  46771. unsigned char msg[SHA256_DIGEST_LENGTH];
  46772. #ifdef USE_CERT_BUFFERS_1024
  46773. const unsigned char* privDer = client_key_der_1024;
  46774. size_t privDerSz = sizeof_client_key_der_1024;
  46775. unsigned char encMsg[1024/8];
  46776. const unsigned char expEncMsg[] = {
  46777. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  46778. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  46779. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  46780. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  46781. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  46782. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  46783. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  46784. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  46785. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  46786. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  46787. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  46788. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  46789. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  46790. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  46791. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  46792. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  46793. };
  46794. #ifdef WC_RSA_NO_PADDING
  46795. const unsigned char expEncMsgNoPad[] = {
  46796. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  46797. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  46798. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  46799. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  46800. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  46801. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  46802. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  46803. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  46804. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  46805. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  46806. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  46807. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  46808. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  46809. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  46810. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  46811. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  46812. };
  46813. #endif
  46814. #else
  46815. const unsigned char* privDer = client_key_der_2048;
  46816. size_t privDerSz = sizeof_client_key_der_2048;
  46817. unsigned char encMsg[2048/8];
  46818. const unsigned char expEncMsg[] = {
  46819. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  46820. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  46821. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  46822. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  46823. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  46824. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  46825. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  46826. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  46827. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  46828. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  46829. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  46830. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  46831. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  46832. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  46833. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  46834. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  46835. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  46836. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  46837. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  46838. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  46839. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  46840. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  46841. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  46842. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  46843. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  46844. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  46845. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  46846. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  46847. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  46848. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  46849. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  46850. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  46851. };
  46852. #ifdef WC_RSA_NO_PADDING
  46853. const unsigned char expEncMsgNoPad[] = {
  46854. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  46855. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  46856. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  46857. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  46858. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  46859. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  46860. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  46861. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  46862. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  46863. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  46864. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  46865. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  46866. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  46867. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  46868. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  46869. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  46870. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  46871. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  46872. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  46873. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  46874. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  46875. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  46876. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  46877. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  46878. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  46879. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  46880. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  46881. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  46882. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  46883. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  46884. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  46885. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  46886. };
  46887. #endif
  46888. #endif
  46889. const unsigned char* der;
  46890. XMEMSET(msg, 0x00, sizeof(msg));
  46891. der = privDer;
  46892. rsa = NULL;
  46893. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  46894. AssertIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  46895. AssertIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  46896. -1);
  46897. AssertIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  46898. RSA_PKCS1_PADDING), -1);
  46899. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  46900. RSA_PKCS1_PADDING), -1);
  46901. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  46902. RSA_PKCS1_PADDING), -1);
  46903. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  46904. RSA_PKCS1_PSS_PADDING), -1);
  46905. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  46906. RSA_PKCS1_PADDING), sizeof(encMsg));
  46907. AssertIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  46908. #ifdef WC_RSA_NO_PADDING
  46909. /* Non-zero message. */
  46910. XMEMSET(msg, 0x01, sizeof(msg));
  46911. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  46912. RSA_NO_PADDING), sizeof(encMsg));
  46913. AssertIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  46914. #endif
  46915. RSA_free(rsa);
  46916. res = TEST_RES_CHECK(1);
  46917. #endif
  46918. return res;
  46919. }
  46920. static int test_wolfSSL_RSA_public_encrypt(void)
  46921. {
  46922. int res = TEST_SKIPPED;
  46923. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  46924. RSA* rsa;
  46925. const unsigned char msg[2048/8] = { 0 };
  46926. unsigned char encMsg[2048/8];
  46927. AssertNotNull(rsa = RSA_new());
  46928. AssertIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  46929. RSA_PKCS1_PADDING), -1);
  46930. AssertIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  46931. RSA_PKCS1_PADDING), -1);
  46932. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  46933. RSA_PKCS1_PADDING), -1);
  46934. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  46935. RSA_PKCS1_PADDING), -1);
  46936. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  46937. RSA_PKCS1_PSS_PADDING), -1);
  46938. /* Empty RSA key. */
  46939. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  46940. RSA_PKCS1_PADDING), -1);
  46941. RSA_free(rsa);
  46942. res = TEST_RES_CHECK(1);
  46943. #endif
  46944. return res;
  46945. }
  46946. static int test_wolfSSL_RSA_private_decrypt(void)
  46947. {
  46948. int res = TEST_SKIPPED;
  46949. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  46950. RSA* rsa;
  46951. unsigned char msg[2048/8];
  46952. const unsigned char encMsg[2048/8] = { 0 };
  46953. AssertNotNull(rsa = RSA_new());
  46954. AssertIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  46955. RSA_PKCS1_PADDING), -1);
  46956. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  46957. RSA_PKCS1_PADDING), -1);
  46958. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  46959. RSA_PKCS1_PADDING), -1);
  46960. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  46961. RSA_PKCS1_PADDING), -1);
  46962. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  46963. RSA_PKCS1_PSS_PADDING), -1);
  46964. /* Empty RSA key. */
  46965. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  46966. RSA_PKCS1_PADDING), -1);
  46967. RSA_free(rsa);
  46968. res = TEST_RES_CHECK(1);
  46969. #endif
  46970. return res;
  46971. }
  46972. static int test_wolfSSL_RSA_GenAdd(void)
  46973. {
  46974. int res = TEST_SKIPPED;
  46975. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  46976. RSA *rsa;
  46977. #ifdef USE_CERT_BUFFERS_1024
  46978. const unsigned char* privDer = client_key_der_1024;
  46979. size_t privDerSz = sizeof_client_key_der_1024;
  46980. const unsigned char* pubDer = client_keypub_der_1024;
  46981. size_t pubDerSz = sizeof_client_keypub_der_1024;
  46982. #else
  46983. const unsigned char* privDer = client_key_der_2048;
  46984. size_t privDerSz = sizeof_client_key_der_2048;
  46985. const unsigned char* pubDer = client_keypub_der_2048;
  46986. size_t pubDerSz = sizeof_client_keypub_der_2048;
  46987. #endif
  46988. const unsigned char* der;
  46989. der = privDer;
  46990. rsa = NULL;
  46991. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  46992. AssertIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  46993. #ifndef RSA_LOW_MEM
  46994. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  46995. #else
  46996. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  46997. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  46998. #endif
  46999. RSA_free(rsa);
  47000. der = pubDer;
  47001. rsa = NULL;
  47002. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  47003. /* Need private values. */
  47004. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  47005. RSA_free(rsa);
  47006. res = TEST_RES_CHECK(1);
  47007. #endif
  47008. return res;
  47009. }
  47010. static int test_wolfSSL_RSA_blinding_on(void)
  47011. {
  47012. int res = TEST_SKIPPED;
  47013. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  47014. RSA *rsa;
  47015. WOLFSSL_BN_CTX *bnCtx;
  47016. #ifdef USE_CERT_BUFFERS_1024
  47017. const unsigned char* privDer = client_key_der_1024;
  47018. size_t privDerSz = sizeof_client_key_der_1024;
  47019. #else
  47020. const unsigned char* privDer = client_key_der_2048;
  47021. size_t privDerSz = sizeof_client_key_der_2048;
  47022. #endif
  47023. const unsigned char* der;
  47024. der = privDer;
  47025. rsa = NULL;
  47026. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47027. AssertNotNull(bnCtx = wolfSSL_BN_CTX_new());
  47028. /* Does nothing so all parameters are valid. */
  47029. AssertIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  47030. AssertIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  47031. AssertIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  47032. AssertIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  47033. wolfSSL_BN_CTX_free(bnCtx);
  47034. RSA_free(rsa);
  47035. res = TEST_RES_CHECK(1);
  47036. #endif
  47037. return res;
  47038. }
  47039. static int test_wolfSSL_RSA_ex_data(void)
  47040. {
  47041. int res = TEST_SKIPPED;
  47042. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  47043. RSA* rsa;
  47044. unsigned char data[1];
  47045. rsa = RSA_new();
  47046. AssertNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  47047. AssertNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  47048. #ifdef MAX_EX_DATA
  47049. AssertNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  47050. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  47051. #endif
  47052. AssertIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  47053. AssertIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  47054. #ifdef HAVE_EX_DATA
  47055. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  47056. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  47057. AssertPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  47058. #else
  47059. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  47060. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  47061. AssertNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  47062. #endif
  47063. RSA_free(rsa);
  47064. res = TEST_RES_CHECK(1);
  47065. #endif /* !NO_RSA && OPENSSL_EXTRA */
  47066. return res;
  47067. }
  47068. static int test_wolfSSL_RSA_LoadDer(void)
  47069. {
  47070. int res = TEST_SKIPPED;
  47071. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  47072. defined(OPENSSL_EXTRA_X509_SMALL))
  47073. RSA *rsa;
  47074. #ifdef USE_CERT_BUFFERS_1024
  47075. const unsigned char* privDer = client_key_der_1024;
  47076. size_t privDerSz = sizeof_client_key_der_1024;
  47077. #else
  47078. const unsigned char* privDer = client_key_der_2048;
  47079. size_t privDerSz = sizeof_client_key_der_2048;
  47080. #endif
  47081. AssertNotNull(rsa = RSA_new());
  47082. AssertIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  47083. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  47084. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  47085. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  47086. RSA_free(rsa);
  47087. res = TEST_RES_CHECK(1);
  47088. #endif /* !NO_RSA && OPENSSL_EXTRA */
  47089. return res;
  47090. }
  47091. /* Local API. */
  47092. static int test_wolfSSL_RSA_To_Der(void)
  47093. {
  47094. int res = TEST_SKIPPED;
  47095. #ifdef WOLFSSL_TEST_STATIC_BUILD
  47096. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA) && \
  47097. defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  47098. RSA* rsa;
  47099. #ifdef USE_CERT_BUFFERS_1024
  47100. const unsigned char* privDer = client_key_der_1024;
  47101. size_t privDerSz = sizeof_client_key_der_1024;
  47102. const unsigned char* pubDer = client_keypub_der_1024;
  47103. size_t pubDerSz = sizeof_client_keypub_der_1024;
  47104. unsigned char out[sizeof(client_key_der_1024)];
  47105. #else
  47106. const unsigned char* privDer = client_key_der_2048;
  47107. size_t privDerSz = sizeof_client_key_der_2048;
  47108. const unsigned char* pubDer = client_keypub_der_2048;
  47109. size_t pubDerSz = sizeof_client_keypub_der_2048;
  47110. unsigned char out[sizeof(client_key_der_2048)];
  47111. #endif
  47112. const unsigned char* der;
  47113. unsigned char* outDer = NULL;
  47114. der = privDer;
  47115. rsa = NULL;
  47116. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47117. AssertIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  47118. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  47119. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  47120. outDer = out;
  47121. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  47122. AssertIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  47123. outDer = NULL;
  47124. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  47125. AssertNotNull(outDer);
  47126. AssertIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  47127. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47128. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  47129. outDer = out;
  47130. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  47131. AssertIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  47132. RSA_free(rsa);
  47133. AssertNotNull(rsa = RSA_new());
  47134. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  47135. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  47136. RSA_free(rsa);
  47137. der = pubDer;
  47138. rsa = NULL;
  47139. AssertNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  47140. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  47141. RSA_free(rsa);
  47142. res = TEST_RES_CHECK(1);
  47143. #endif
  47144. #endif
  47145. return res;
  47146. }
  47147. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  47148. static int test_wolfSSL_PEM_read_RSAPublicKey(void)
  47149. {
  47150. int res = TEST_SKIPPED;
  47151. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  47152. XFILE file;
  47153. const char* fname = "./certs/server-keyPub.pem";
  47154. RSA *rsa;
  47155. AssertNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  47156. file = XFOPEN(fname, "rb");
  47157. AssertTrue((file != XBADFILE));
  47158. AssertNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  47159. AssertIntEQ(RSA_size(rsa), 256);
  47160. RSA_free(rsa);
  47161. XFCLOSE(file);
  47162. res = TEST_RES_CHECK(1);
  47163. #endif
  47164. return res;
  47165. }
  47166. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  47167. static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
  47168. {
  47169. int res = TEST_SKIPPED;
  47170. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  47171. defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA)
  47172. RSA* rsa = NULL;
  47173. AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
  47174. AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, NULL), 0);
  47175. /* Valid but stub so returns 0. */
  47176. AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, rsa), 0);
  47177. res = TEST_RES_CHECK(1);
  47178. #endif
  47179. return res;
  47180. }
  47181. static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
  47182. {
  47183. int res = TEST_SKIPPED;
  47184. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  47185. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  47186. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  47187. RSA* rsa;
  47188. #ifdef USE_CERT_BUFFERS_1024
  47189. const unsigned char* privDer = client_key_der_1024;
  47190. size_t privDerSz = sizeof_client_key_der_1024;
  47191. #else
  47192. const unsigned char* privDer = client_key_der_2048;
  47193. size_t privDerSz = sizeof_client_key_der_2048;
  47194. #endif
  47195. const unsigned char* der;
  47196. #ifndef NO_AES
  47197. unsigned char passwd[] = "password";
  47198. #endif
  47199. AssertNotNull(rsa = RSA_new());
  47200. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  47201. NULL, NULL), 0);
  47202. RSA_free(rsa);
  47203. der = privDer;
  47204. rsa = NULL;
  47205. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47206. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  47207. NULL, NULL), 0);
  47208. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0,
  47209. NULL, NULL), 0);
  47210. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  47211. NULL, NULL), 1);
  47212. #ifndef NO_AES
  47213. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  47214. NULL, 0, NULL, NULL), 1);
  47215. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  47216. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  47217. #endif
  47218. RSA_free(rsa);
  47219. res = TEST_RES_CHECK(1);
  47220. #endif
  47221. return res;
  47222. }
  47223. static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  47224. {
  47225. int res = TEST_SKIPPED;
  47226. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  47227. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  47228. defined(WOLFSSL_DER_TO_PEM))
  47229. RSA* rsa;
  47230. #ifdef USE_CERT_BUFFERS_1024
  47231. const unsigned char* privDer = client_key_der_1024;
  47232. size_t privDerSz = sizeof_client_key_der_1024;
  47233. #else
  47234. const unsigned char* privDer = client_key_der_2048;
  47235. size_t privDerSz = sizeof_client_key_der_2048;
  47236. #endif
  47237. const unsigned char* der;
  47238. #ifndef NO_AES
  47239. unsigned char passwd[] = "password";
  47240. #endif
  47241. unsigned char* pem;
  47242. int plen;
  47243. AssertNotNull(rsa = RSA_new());
  47244. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  47245. &plen), 0);
  47246. RSA_free(rsa);
  47247. der = privDer;
  47248. rsa = NULL;
  47249. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47250. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  47251. &plen), 0);
  47252. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  47253. &plen), 0);
  47254. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  47255. NULL), 0);
  47256. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  47257. &plen), 1);
  47258. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  47259. #ifndef NO_AES
  47260. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  47261. NULL, 0, &pem, &plen), 1);
  47262. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  47263. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  47264. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  47265. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  47266. #endif
  47267. RSA_free(rsa);
  47268. res = TEST_RES_CHECK(1);
  47269. #endif
  47270. return res;
  47271. }
  47272. static int test_wolfSSL_DH(void)
  47273. {
  47274. int res = TEST_SKIPPED;
  47275. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  47276. DH *dh = NULL;
  47277. BIGNUM* p;
  47278. BIGNUM* q;
  47279. BIGNUM* g;
  47280. BIGNUM* pub;
  47281. BIGNUM* priv;
  47282. #if defined(OPENSSL_ALL)
  47283. #if !defined(HAVE_FIPS) || \
  47284. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  47285. FILE* f = NULL;
  47286. unsigned char buf[268];
  47287. const unsigned char* pt = buf;
  47288. long len = 0;
  47289. dh = NULL;
  47290. XMEMSET(buf, 0, sizeof(buf));
  47291. /* Test 2048 bit parameters */
  47292. f = XFOPEN("./certs/dh2048.der", "rb");
  47293. AssertTrue(f != XBADFILE);
  47294. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  47295. XFCLOSE(f);
  47296. AssertNotNull(dh = d2i_DHparams(NULL, &pt, len));
  47297. AssertNotNull(dh->p);
  47298. AssertNotNull(dh->g);
  47299. AssertTrue(pt == buf);
  47300. AssertIntEQ(DH_generate_key(dh), 1);
  47301. AssertIntEQ(DH_generate_key(dh), 1);
  47302. AssertIntEQ(DH_compute_key(NULL, NULL, NULL), -1);
  47303. AssertNotNull(pub = BN_new());
  47304. AssertIntEQ(BN_set_word(pub, 1), 1);
  47305. AssertIntEQ(DH_compute_key(buf, NULL, NULL), -1);
  47306. AssertIntEQ(DH_compute_key(NULL, pub, NULL), -1);
  47307. AssertIntEQ(DH_compute_key(NULL, NULL, dh), -1);
  47308. AssertIntEQ(DH_compute_key(buf, pub, NULL), -1);
  47309. AssertIntEQ(DH_compute_key(buf, NULL, dh), -1);
  47310. AssertIntEQ(DH_compute_key(NULL, pub, dh), -1);
  47311. AssertIntEQ(DH_compute_key(buf, pub, dh), -1);
  47312. BN_free(pub);
  47313. DH_get0_pqg(dh, (const BIGNUM**)&p,
  47314. (const BIGNUM**)&q,
  47315. (const BIGNUM**)&g);
  47316. AssertPtrEq(p, dh->p);
  47317. AssertPtrEq(q, dh->q);
  47318. AssertPtrEq(g, dh->g);
  47319. DH_get0_key(NULL, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  47320. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  47321. AssertPtrEq(pub, dh->pub_key);
  47322. AssertPtrEq(priv, dh->priv_key);
  47323. DH_get0_key(dh, (const BIGNUM**)&pub, NULL);
  47324. AssertPtrEq(pub, dh->pub_key);
  47325. DH_get0_key(dh, NULL, (const BIGNUM**)&priv);
  47326. AssertPtrEq(priv, dh->priv_key);
  47327. AssertNotNull(pub = BN_new());
  47328. AssertNotNull(priv = BN_new());
  47329. AssertIntEQ(DH_set0_key(NULL, pub, priv), 0);
  47330. AssertIntEQ(DH_set0_key(dh, pub, priv), 1);
  47331. AssertNotNull(pub = BN_new());
  47332. AssertIntEQ(DH_set0_key(dh, pub, NULL), 1);
  47333. AssertNotNull(priv = BN_new());
  47334. AssertIntEQ(DH_set0_key(dh, NULL, priv), 1);
  47335. AssertPtrEq(pub, dh->pub_key);
  47336. AssertPtrEq(priv, dh->priv_key);
  47337. DH_free(dh);
  47338. AssertNotNull(dh = DH_new());
  47339. AssertNotNull(p = BN_new());
  47340. AssertIntEQ(BN_set_word(p, 1), 1);
  47341. AssertIntEQ(DH_compute_key(buf, p, dh), -1);
  47342. AssertNotNull(pub = BN_new());
  47343. AssertNotNull(priv = BN_new());
  47344. AssertIntEQ(DH_set0_key(dh, pub, priv), 1);
  47345. AssertIntEQ(DH_compute_key(buf, p, dh), -1);
  47346. BN_free(p);
  47347. DH_free(dh);
  47348. #ifdef WOLFSSL_KEY_GEN
  47349. AssertNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  47350. AssertIntEQ(wolfSSL_DH_generate_parameters_ex(NULL, 2048, 2, NULL), 0);
  47351. #endif
  47352. DH_free(dh);
  47353. #endif
  47354. #endif
  47355. (void)dh;
  47356. (void)p;
  47357. (void)q;
  47358. (void)g;
  47359. (void)pub;
  47360. (void)priv;
  47361. dh = wolfSSL_DH_new();
  47362. AssertNotNull(dh);
  47363. /* invalid parameters test */
  47364. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  47365. (const BIGNUM**)&q,
  47366. (const BIGNUM**)&g);
  47367. DH_get0_pqg(dh, NULL,
  47368. (const BIGNUM**)&q,
  47369. (const BIGNUM**)&g);
  47370. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  47371. DH_get0_pqg(dh, NULL, NULL, NULL);
  47372. AssertTrue(1);
  47373. DH_get0_pqg(dh, (const BIGNUM**)&p,
  47374. (const BIGNUM**)&q,
  47375. (const BIGNUM**)&g);
  47376. AssertPtrEq(p, NULL);
  47377. AssertPtrEq(q, NULL);
  47378. AssertPtrEq(g, NULL);
  47379. DH_free(dh);
  47380. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
  47381. || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
  47382. #if defined(OPENSSL_ALL) || \
  47383. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  47384. dh = wolfSSL_DH_new();
  47385. AssertNotNull(dh);
  47386. p = wolfSSL_BN_new();
  47387. AssertNotNull(p);
  47388. AssertIntEQ(BN_set_word(p, 11), 1);
  47389. g = wolfSSL_BN_new();
  47390. AssertNotNull(g);
  47391. AssertIntEQ(BN_set_word(g, 2), 1);
  47392. q = wolfSSL_BN_new();
  47393. AssertNotNull(q);
  47394. AssertIntEQ(BN_set_word(q, 5), 1);
  47395. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, NULL), 0);
  47396. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 0);
  47397. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, p, NULL, NULL), 0);
  47398. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, q, NULL), 0);
  47399. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, g), 0);
  47400. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, p, q, g), 0);
  47401. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, g), 0);
  47402. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, NULL), 0);
  47403. /* Don't need q. */
  47404. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  47405. /* Setting again will free the p and g. */
  47406. wolfSSL_BN_free(q);
  47407. DH_free(dh);
  47408. dh = wolfSSL_DH_new();
  47409. AssertNotNull(dh);
  47410. p = wolfSSL_BN_new();
  47411. AssertNotNull(p);
  47412. AssertIntEQ(BN_set_word(p, 11), 1);
  47413. g = wolfSSL_BN_new();
  47414. AssertNotNull(g);
  47415. AssertIntEQ(BN_set_word(g, 2), 1);
  47416. q = wolfSSL_BN_new();
  47417. AssertNotNull(q);
  47418. AssertIntEQ(BN_set_word(q, 5), 1);
  47419. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, g), 1);
  47420. /* p, q and g are now owned by dh - don't free. */
  47421. p = wolfSSL_BN_new();
  47422. AssertNotNull(p);
  47423. AssertIntEQ(BN_set_word(p, 11), 1);
  47424. g = wolfSSL_BN_new();
  47425. AssertNotNull(g);
  47426. AssertIntEQ(BN_set_word(g, 2), 1);
  47427. q = wolfSSL_BN_new();
  47428. AssertNotNull(q);
  47429. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, NULL), 1);
  47430. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, NULL), 1);
  47431. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, g), 1);
  47432. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 1);
  47433. /* p, q and g are now owned by dh - don't free. */
  47434. DH_free(dh);
  47435. AssertIntEQ(DH_generate_key(NULL), 0);
  47436. AssertNotNull(dh = DH_new());
  47437. AssertIntEQ(DH_generate_key(dh), 0);
  47438. p = wolfSSL_BN_new();
  47439. AssertNotNull(p);
  47440. AssertIntEQ(BN_set_word(p, 0), 1);
  47441. g = wolfSSL_BN_new();
  47442. AssertNotNull(g);
  47443. AssertIntEQ(BN_set_word(g, 2), 1);
  47444. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  47445. AssertIntEQ(DH_generate_key(dh), 0);
  47446. DH_free(dh);
  47447. #endif
  47448. #endif
  47449. /* Test DH_up_ref() */
  47450. dh = wolfSSL_DH_new();
  47451. AssertNotNull(dh);
  47452. AssertIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  47453. AssertIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  47454. DH_free(dh); /* decrease ref count */
  47455. DH_free(dh); /* free WOLFSSL_DH */
  47456. AssertNull((dh = DH_new_by_nid(NID_sha1)));
  47457. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  47458. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  47459. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  47460. #ifdef HAVE_FFDHE_2048
  47461. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  47462. DH_free(dh);
  47463. #endif
  47464. #ifdef HAVE_FFDHE_3072
  47465. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  47466. DH_free(dh);
  47467. #endif
  47468. #ifdef HAVE_FFDHE_4096
  47469. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  47470. DH_free(dh);
  47471. #endif
  47472. #else
  47473. AssertNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  47474. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  47475. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  47476. AssertIntEQ(wolfSSL_DH_size(NULL), -1);
  47477. res = TEST_RES_CHECK(1);
  47478. #endif /* OPENSSL_EXTRA && !NO_DH */
  47479. return res;
  47480. }
  47481. static int test_wolfSSL_DH_dup(void)
  47482. {
  47483. int res = TEST_SKIPPED;
  47484. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  47485. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
  47486. defined(OPENSSL_EXTRA)
  47487. DH *dh;
  47488. DH *dhDup;
  47489. WOLFSSL_BIGNUM* p;
  47490. WOLFSSL_BIGNUM* g;
  47491. AssertNotNull(p = wolfSSL_BN_new());
  47492. AssertNotNull(g = wolfSSL_BN_new());
  47493. AssertIntEQ(wolfSSL_BN_set_word(p, 11), WOLFSSL_SUCCESS);
  47494. AssertIntEQ(wolfSSL_BN_set_word(g, 2), WOLFSSL_SUCCESS);
  47495. dhDup = wolfSSL_DH_dup(NULL);
  47496. AssertNull(dhDup);
  47497. dh = wolfSSL_DH_new();
  47498. AssertNotNull(dh);
  47499. dhDup = wolfSSL_DH_dup(dh);
  47500. AssertNull(dhDup);
  47501. #if defined(OPENSSL_ALL) || \
  47502. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  47503. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  47504. dhDup = wolfSSL_DH_dup(dh);
  47505. AssertNotNull(dhDup);
  47506. wolfSSL_DH_free(dhDup);
  47507. #endif
  47508. wolfSSL_DH_free(dh);
  47509. res = TEST_RES_CHECK(1);
  47510. #endif
  47511. #endif
  47512. return res;
  47513. }
  47514. static int test_wolfSSL_DH_check(void)
  47515. {
  47516. int res = TEST_SKIPPED;
  47517. #ifdef OPENSSL_ALL
  47518. #ifndef NO_DH
  47519. #ifndef NO_BIO
  47520. #ifndef NO_DSA
  47521. byte buf[6000];
  47522. char file[] = "./certs/dsaparams.pem";
  47523. XFILE f;
  47524. int bytes;
  47525. BIO* bio;
  47526. DSA* dsa;
  47527. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  47528. static const byte dh2048[] = {
  47529. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  47530. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  47531. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  47532. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  47533. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  47534. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  47535. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  47536. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  47537. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  47538. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  47539. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  47540. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  47541. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  47542. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  47543. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  47544. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  47545. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  47546. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  47547. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  47548. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  47549. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  47550. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  47551. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  47552. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  47553. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  47554. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  47555. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  47556. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  47557. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  47558. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  47559. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  47560. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  47561. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  47562. 0x93, 0x02, 0x01, 0x02
  47563. };
  47564. const byte* params;
  47565. #endif
  47566. DH* dh = NULL;
  47567. WOLFSSL_BIGNUM* p;
  47568. WOLFSSL_BIGNUM* g;
  47569. WOLFSSL_BIGNUM* pTmp = NULL;
  47570. WOLFSSL_BIGNUM* gTmp = NULL;
  47571. int codes = -1;
  47572. #ifndef NO_DSA
  47573. /* Initialize DH */
  47574. f = XFOPEN(file, "rb");
  47575. AssertTrue((f != XBADFILE));
  47576. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  47577. XFCLOSE(f);
  47578. bio = BIO_new_mem_buf((void*)buf, bytes);
  47579. AssertNotNull(bio);
  47580. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  47581. AssertNotNull(dsa);
  47582. dh = wolfSSL_DSA_dup_DH(dsa);
  47583. AssertNotNull(dh);
  47584. BIO_free(bio);
  47585. DSA_free(dsa);
  47586. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  47587. params = dh2048;
  47588. dh = wolfSSL_d2i_DHparams(NULL, &params, (long)sizeof(dh2048));
  47589. AssertNotNull(dh);
  47590. #else
  47591. dh = wolfSSL_DH_new_by_nid(NID_ffdhe2048);
  47592. AssertNotNull(dh);
  47593. #endif
  47594. /* Test assumed to be valid dh.
  47595. * Should return WOLFSSL_SUCCESS
  47596. * codes should be 0
  47597. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  47598. */
  47599. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  47600. AssertIntEQ(codes, 0);
  47601. /* Test NULL dh: expected BAD_FUNC_ARG */
  47602. AssertIntEQ(wolfSSL_DH_check(NULL, &codes), 0);
  47603. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  47604. pTmp = dh->p;
  47605. dh->p = NULL;
  47606. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  47607. AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  47608. AssertIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  47609. /* set dh->p back to normal so it wont fail on next tests */
  47610. dh->p = pTmp;
  47611. pTmp = NULL;
  47612. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  47613. gTmp = dh->g;
  47614. dh->g = NULL;
  47615. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  47616. AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  47617. AssertIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  47618. dh->g = gTmp;
  47619. gTmp = NULL;
  47620. /* Cleanup */
  47621. DH_free(dh);
  47622. dh = DH_new();
  47623. AssertNotNull(dh);
  47624. /* Check empty DH. */
  47625. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  47626. AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  47627. AssertIntEQ(codes, DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME);
  47628. /* Check non-prime valued p. */
  47629. AssertNotNull(p = BN_new());
  47630. AssertIntEQ(BN_set_word(p, 4), 1);
  47631. AssertNotNull(g = BN_new());
  47632. AssertIntEQ(BN_set_word(g, 2), 1);
  47633. AssertIntEQ(DH_set0_pqg(dh, p, NULL, g), 1);
  47634. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  47635. AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  47636. AssertIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  47637. DH_free(dh);
  47638. res = TEST_RES_CHECK(1);
  47639. #endif
  47640. #endif /* !NO_DH && !NO_DSA */
  47641. #endif
  47642. return res;
  47643. }
  47644. static int test_wolfSSL_DH_prime(void)
  47645. {
  47646. int res = TEST_SKIPPED;
  47647. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  47648. WOLFSSL_BIGNUM* bn;
  47649. #if WOLFSSL_MAX_BN_BITS >= 768
  47650. WOLFSSL_BIGNUM* bn2;
  47651. #endif
  47652. bn = wolfSSL_DH_768_prime(NULL);
  47653. #if WOLFSSL_MAX_BN_BITS >= 768
  47654. AssertNotNull(bn);
  47655. bn2 = wolfSSL_DH_768_prime(bn);
  47656. AssertNotNull(bn2);
  47657. AssertTrue(bn == bn2);
  47658. wolfSSL_BN_free(bn);
  47659. #else
  47660. AssertNull(bn);
  47661. #endif
  47662. bn = wolfSSL_DH_1024_prime(NULL);
  47663. #if WOLFSSL_MAX_BN_BITS >= 1024
  47664. AssertNotNull(bn);
  47665. wolfSSL_BN_free(bn);
  47666. #else
  47667. AssertNull(bn);
  47668. #endif
  47669. bn = wolfSSL_DH_2048_prime(NULL);
  47670. #if WOLFSSL_MAX_BN_BITS >= 2048
  47671. AssertNotNull(bn);
  47672. wolfSSL_BN_free(bn);
  47673. #else
  47674. AssertNull(bn);
  47675. #endif
  47676. bn = wolfSSL_DH_3072_prime(NULL);
  47677. #if WOLFSSL_MAX_BN_BITS >= 3072
  47678. AssertNotNull(bn);
  47679. wolfSSL_BN_free(bn);
  47680. #else
  47681. AssertNull(bn);
  47682. #endif
  47683. bn = wolfSSL_DH_4096_prime(NULL);
  47684. #if WOLFSSL_MAX_BN_BITS >= 4096
  47685. AssertNotNull(bn);
  47686. wolfSSL_BN_free(bn);
  47687. #else
  47688. AssertNull(bn);
  47689. #endif
  47690. bn = wolfSSL_DH_6144_prime(NULL);
  47691. #if WOLFSSL_MAX_BN_BITS >= 6144
  47692. AssertNotNull(bn);
  47693. wolfSSL_BN_free(bn);
  47694. #else
  47695. AssertNull(bn);
  47696. #endif
  47697. bn = wolfSSL_DH_8192_prime(NULL);
  47698. #if WOLFSSL_MAX_BN_BITS >= 8192
  47699. AssertNotNull(bn);
  47700. wolfSSL_BN_free(bn);
  47701. #else
  47702. AssertNull(bn);
  47703. #endif
  47704. res = TEST_RES_CHECK(1);
  47705. #endif
  47706. return res;
  47707. }
  47708. static int test_wolfSSL_DH_1536_prime(void)
  47709. {
  47710. int res = TEST_SKIPPED;
  47711. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  47712. BIGNUM* bn;
  47713. unsigned char bits[200];
  47714. int sz = 192; /* known binary size */
  47715. const byte expected[] = {
  47716. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  47717. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  47718. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  47719. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  47720. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  47721. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  47722. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  47723. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  47724. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  47725. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  47726. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  47727. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  47728. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  47729. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  47730. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  47731. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  47732. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  47733. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  47734. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  47735. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  47736. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  47737. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  47738. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  47739. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  47740. };
  47741. bn = get_rfc3526_prime_1536(NULL);
  47742. AssertNotNull(bn);
  47743. AssertIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  47744. AssertIntEQ(0, XMEMCMP(expected, bits, sz));
  47745. BN_free(bn);
  47746. res = TEST_RES_CHECK(1);
  47747. #endif
  47748. return res;
  47749. }
  47750. static int test_wolfSSL_DH_get_2048_256(void)
  47751. {
  47752. int res = TEST_SKIPPED;
  47753. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  47754. WOLFSSL_DH* dh;
  47755. const WOLFSSL_BIGNUM* pBn;
  47756. const WOLFSSL_BIGNUM* gBn;
  47757. const WOLFSSL_BIGNUM* qBn;
  47758. const byte pExpected[] = {
  47759. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  47760. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  47761. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  47762. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  47763. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  47764. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  47765. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  47766. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  47767. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  47768. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  47769. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  47770. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  47771. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  47772. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  47773. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  47774. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  47775. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  47776. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  47777. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  47778. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  47779. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  47780. 0x1E, 0x1A, 0x15, 0x97
  47781. };
  47782. const byte gExpected[] = {
  47783. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  47784. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  47785. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  47786. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  47787. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  47788. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  47789. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  47790. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  47791. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  47792. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  47793. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  47794. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  47795. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  47796. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  47797. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  47798. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  47799. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  47800. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  47801. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  47802. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  47803. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  47804. 0x6C, 0xC4, 0x16, 0x59
  47805. };
  47806. const byte qExpected[] = {
  47807. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  47808. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  47809. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  47810. };
  47811. int pSz;
  47812. int qSz;
  47813. int gSz;
  47814. byte* pReturned;
  47815. byte* qReturned;
  47816. byte* gReturned;
  47817. AssertNotNull((dh = wolfSSL_DH_get_2048_256()));
  47818. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  47819. AssertIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  47820. AssertNotNull(pReturned = (byte*)XMALLOC(pSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  47821. AssertIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  47822. AssertIntEQ(pSz, sizeof(pExpected));
  47823. AssertIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  47824. AssertIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  47825. AssertNotNull(qReturned = (byte*)XMALLOC(qSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  47826. AssertIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  47827. AssertIntEQ(qSz, sizeof(qExpected));
  47828. AssertIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  47829. AssertIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  47830. AssertNotNull(gReturned = (byte*)XMALLOC(gSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  47831. AssertIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  47832. AssertIntEQ(gSz, sizeof(gExpected));
  47833. AssertIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  47834. wolfSSL_DH_free(dh);
  47835. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47836. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47837. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47838. res = TEST_RES_CHECK(1);
  47839. #endif
  47840. return res;
  47841. }
  47842. static int test_wolfSSL_PEM_write_DHparams(void)
  47843. {
  47844. int res = TEST_SKIPPED;
  47845. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  47846. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  47847. DH* dh;
  47848. BIO* bio;
  47849. XFILE fp;
  47850. byte pem[2048];
  47851. int pemSz;
  47852. const char expected[] =
  47853. "-----BEGIN DH PARAMETERS-----\n"
  47854. "MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n"
  47855. "v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n"
  47856. "nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n"
  47857. "joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n"
  47858. "wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n"
  47859. "tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n"
  47860. "-----END DH PARAMETERS-----\n";
  47861. const char badPem[] =
  47862. "-----BEGIN DH PARAMETERS-----\n"
  47863. "-----END DH PARAMETERS-----\n";
  47864. const char emptySeqPem[] =
  47865. "-----BEGIN DH PARAMETERS-----\n"
  47866. "MAA=\n"
  47867. "-----END DH PARAMETERS-----\n";
  47868. AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
  47869. AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  47870. XFCLOSE(fp);
  47871. AssertNull(PEM_read_bio_DHparams(NULL, NULL, NULL, NULL));
  47872. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  47873. AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  47874. AssertIntEQ(BIO_write(bio, badPem, (int)sizeof(badPem)),
  47875. (int)sizeof(badPem));
  47876. AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  47877. BIO_free(bio);
  47878. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  47879. AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  47880. AssertIntEQ(BIO_write(bio, emptySeqPem, (int)sizeof(emptySeqPem)),
  47881. (int)sizeof(emptySeqPem));
  47882. AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  47883. BIO_free(bio);
  47884. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  47885. AssertIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  47886. AssertNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  47887. BIO_free(bio);
  47888. AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  47889. AssertIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  47890. AssertIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  47891. DH_free(dh);
  47892. dh = wolfSSL_DH_new();
  47893. AssertIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_FAILURE);
  47894. XFCLOSE(fp);
  47895. wolfSSL_DH_free(dh);
  47896. /* check results */
  47897. XMEMSET(pem, 0, sizeof(pem));
  47898. AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "rb"));
  47899. AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  47900. AssertIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  47901. XFCLOSE(fp);
  47902. res = TEST_RES_CHECK(1);
  47903. #endif
  47904. return res;
  47905. }
  47906. static int test_wolfSSL_d2i_DHparams(void)
  47907. {
  47908. int res = TEST_SKIPPED;
  47909. #ifdef OPENSSL_ALL
  47910. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  47911. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47912. FILE* f = NULL;
  47913. unsigned char buf[4096];
  47914. const unsigned char* pt = buf;
  47915. #ifdef HAVE_FFDHE_2048
  47916. const char* params1 = "./certs/dh2048.der";
  47917. #endif
  47918. #ifdef HAVE_FFDHE_3072
  47919. const char* params2 = "./certs/dh3072.der";
  47920. #endif
  47921. long len = 0;
  47922. WOLFSSL_DH* dh = NULL;
  47923. XMEMSET(buf, 0, sizeof(buf));
  47924. /* Test 2048 bit parameters */
  47925. #ifdef HAVE_FFDHE_2048
  47926. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  47927. f = XFOPEN(params1, "rb");
  47928. AssertTrue(f != XBADFILE);
  47929. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  47930. XFCLOSE(f);
  47931. /* Valid case */
  47932. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  47933. AssertNotNull(dh->p);
  47934. AssertNotNull(dh->g);
  47935. AssertTrue(pt == buf);
  47936. AssertIntEQ(DH_set_length(NULL, BN_num_bits(dh->p)), 0);
  47937. AssertIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), 1);
  47938. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  47939. /* Invalid cases */
  47940. AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  47941. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  47942. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  47943. DH_free(dh);
  47944. *buf = 0;
  47945. pt = buf;
  47946. res = TEST_RES_CHECK(1);
  47947. }
  47948. #endif /* HAVE_FFDHE_2048 */
  47949. /* Test 3072 bit parameters */
  47950. #ifdef HAVE_FFDHE_3072
  47951. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  47952. f = XFOPEN(params2, "rb");
  47953. AssertTrue(f != XBADFILE);
  47954. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  47955. XFCLOSE(f);
  47956. /* Valid case */
  47957. AssertNotNull(dh = wolfSSL_d2i_DHparams(&dh, &pt, len));
  47958. AssertNotNull(dh->p);
  47959. AssertNotNull(dh->g);
  47960. AssertTrue(pt != buf);
  47961. AssertIntEQ(DH_generate_key(dh), 1);
  47962. /* Invalid cases */
  47963. AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  47964. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  47965. DH_free(dh);
  47966. res = TEST_RES_CHECK(1);
  47967. }
  47968. #endif /* HAVE_FFDHE_3072 */
  47969. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  47970. #endif /* !NO_DH */
  47971. #endif
  47972. return res;
  47973. }
  47974. static int test_wolfSSL_DH_LoadDer(void)
  47975. {
  47976. int res = TEST_SKIPPED;
  47977. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)) && \
  47978. defined(OPENSSL_EXTRA)
  47979. static const byte dh2048[] = {
  47980. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  47981. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  47982. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  47983. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  47984. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  47985. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  47986. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  47987. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  47988. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  47989. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  47990. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  47991. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  47992. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  47993. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  47994. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  47995. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  47996. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  47997. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  47998. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  47999. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  48000. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  48001. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  48002. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  48003. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  48004. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  48005. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  48006. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  48007. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  48008. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  48009. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  48010. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  48011. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  48012. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  48013. 0x93, 0x02, 0x01, 0x02
  48014. };
  48015. WOLFSSL_DH* dh;
  48016. dh = wolfSSL_DH_new();
  48017. AssertNotNull(dh);
  48018. AssertIntEQ(wolfSSL_DH_LoadDer(NULL, NULL, 0), -1);
  48019. AssertIntEQ(wolfSSL_DH_LoadDer(dh, NULL, 0), -1);
  48020. AssertIntEQ(wolfSSL_DH_LoadDer(NULL, dh2048, sizeof(dh2048)), -1);
  48021. AssertIntEQ(wolfSSL_DH_LoadDer(dh, dh2048, sizeof(dh2048)), 1);
  48022. wolfSSL_DH_free(dh);
  48023. res = TEST_RES_CHECK(1);
  48024. #endif
  48025. return res;
  48026. }
  48027. static int test_wolfSSL_i2d_DHparams(void)
  48028. {
  48029. int res = TEST_SKIPPED;
  48030. #ifdef OPENSSL_ALL
  48031. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  48032. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  48033. FILE* f;
  48034. unsigned char buf[4096];
  48035. const unsigned char* pt;
  48036. unsigned char* pt2;
  48037. #ifdef HAVE_FFDHE_2048
  48038. const char* params1 = "./certs/dh2048.der";
  48039. #endif
  48040. #ifdef HAVE_FFDHE_3072
  48041. const char* params2 = "./certs/dh3072.der";
  48042. #endif
  48043. long len;
  48044. WOLFSSL_DH* dh;
  48045. /* Test 2048 bit parameters */
  48046. #ifdef HAVE_FFDHE_2048
  48047. pt = buf;
  48048. pt2 = buf;
  48049. f = XFOPEN(params1, "rb");
  48050. AssertTrue(f != XBADFILE);
  48051. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  48052. XFCLOSE(f);
  48053. /* Valid case */
  48054. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  48055. AssertTrue(pt == buf);
  48056. AssertIntEQ(DH_generate_key(dh), 1);
  48057. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  48058. /* Invalid case */
  48059. AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  48060. /* Return length only */
  48061. AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  48062. DH_free(dh);
  48063. *buf = 0;
  48064. #endif
  48065. /* Test 3072 bit parameters */
  48066. #ifdef HAVE_FFDHE_3072
  48067. pt = buf;
  48068. pt2 = buf;
  48069. f = XFOPEN(params2, "rb");
  48070. AssertTrue(f != XBADFILE);
  48071. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  48072. XFCLOSE(f);
  48073. /* Valid case */
  48074. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  48075. AssertTrue(pt == buf);
  48076. AssertIntEQ(DH_generate_key(dh), 1);
  48077. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  48078. /* Invalid case */
  48079. AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  48080. /* Return length only */
  48081. AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  48082. DH_free(dh);
  48083. #endif
  48084. dh = DH_new();
  48085. AssertNotNull(dh);
  48086. pt2 = buf;
  48087. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 0);
  48088. DH_free(dh);
  48089. res = TEST_RES_CHECK(1);
  48090. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  48091. #endif /* !NO_DH && (HAVE_FFDHE_2048 || HAVE_FFDHE_3072) */
  48092. #endif
  48093. return res;
  48094. }
  48095. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  48096. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  48097. static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey)
  48098. {
  48099. X509* x509 = NULL;
  48100. BIGNUM* serial_number = NULL;
  48101. X509_NAME* name = NULL;
  48102. time_t epoch_off = 0;
  48103. ASN1_INTEGER* asn1_serial_number;
  48104. long not_before, not_after;
  48105. AssertNotNull(x509 = X509_new());
  48106. AssertIntNE(X509_set_pubkey(x509, pkey), 0);
  48107. AssertNotNull(serial_number = BN_new());
  48108. AssertIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  48109. AssertNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  48110. AssertNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  48111. /* version 3 */
  48112. AssertIntNE(X509_set_version(x509, 2L), 0);
  48113. AssertNotNull(name = X509_NAME_new());
  48114. AssertIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  48115. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  48116. AssertIntNE(X509_set_subject_name(x509, name), 0);
  48117. AssertIntNE(X509_set_issuer_name(x509, name), 0);
  48118. not_before = (long)wc_Time(NULL);
  48119. not_after = not_before + (365 * 24 * 60 * 60);
  48120. AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &epoch_off));
  48121. AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &epoch_off));
  48122. AssertIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  48123. BN_free(serial_number);
  48124. X509_NAME_free(name);
  48125. X509_free(x509);
  48126. return 0;
  48127. }
  48128. #endif
  48129. static int test_openssl_generate_key_and_cert(void)
  48130. {
  48131. int res = TEST_SKIPPED;
  48132. #if defined(OPENSSL_EXTRA)
  48133. #if !defined(NO_RSA)
  48134. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  48135. EVP_PKEY* pkey = EVP_PKEY_new();
  48136. int key_length = 2048;
  48137. BIGNUM* exponent = BN_new();
  48138. RSA* rsa = RSA_new();
  48139. AssertNotNull(pkey);
  48140. AssertNotNull(exponent);
  48141. AssertNotNull(rsa);
  48142. AssertIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  48143. #ifndef WOLFSSL_KEY_GEN
  48144. AssertIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  48145. #if defined(USE_CERT_BUFFERS_1024)
  48146. AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  48147. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  48148. key_length = 1024;
  48149. #elif defined(USE_CERT_BUFFERS_2048)
  48150. AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  48151. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  48152. #else
  48153. RSA_free(rsa);
  48154. rsa = NULL;
  48155. #endif
  48156. #else
  48157. AssertIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  48158. AssertIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  48159. AssertIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  48160. AssertIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  48161. #endif
  48162. if (rsa) {
  48163. AssertIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  48164. BN_free(exponent);
  48165. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  48166. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  48167. test_openssl_make_self_signed_certificate(pkey);
  48168. #endif
  48169. }
  48170. EVP_PKEY_free(pkey);
  48171. res = TEST_RES_CHECK(1);
  48172. }
  48173. #endif /* !NO_RSA */
  48174. #ifdef HAVE_ECC
  48175. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  48176. EVP_PKEY* pkey = EVP_PKEY_new();
  48177. EC_KEY* ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  48178. AssertNotNull(pkey);
  48179. AssertNotNull(ec_key);
  48180. #ifndef NO_WOLFSSL_STUB
  48181. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  48182. #endif
  48183. AssertIntNE(EC_KEY_generate_key(ec_key), 0);
  48184. AssertIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  48185. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  48186. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  48187. test_openssl_make_self_signed_certificate(pkey);
  48188. #endif
  48189. EVP_PKEY_free(pkey);
  48190. res = TEST_RES_CHECK(1);
  48191. }
  48192. #endif /* HAVE_ECC */
  48193. #endif /* OPENSSL_EXTRA */
  48194. return res;
  48195. }
  48196. static int test_stubs_are_stubs(void)
  48197. {
  48198. int res = TEST_SKIPPED;
  48199. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB)
  48200. WOLFSSL_CTX* ctx = NULL;
  48201. WOLFSSL_CTX* ctxN = NULL;
  48202. #ifndef NO_WOLFSSL_CLIENT
  48203. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  48204. AssertNotNull(ctx);
  48205. #elif !defined(NO_WOLFSSL_SERVER)
  48206. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  48207. AssertNotNull(ctx);
  48208. #else
  48209. return res;
  48210. #endif
  48211. #define CHECKZERO_RET(x, y, z) AssertIntEQ((int) x(y), 0); \
  48212. AssertIntEQ((int) x(z), 0)
  48213. /* test logic, all stubs return same result regardless of ctx being NULL
  48214. * as there are no sanity checks, it's just a stub! If at some
  48215. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  48216. * if invalid inputs are supplied. Test calling both
  48217. * with and without valid inputs, if a stub functionality remains unchanged.
  48218. */
  48219. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  48220. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  48221. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  48222. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  48223. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  48224. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  48225. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  48226. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  48227. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  48228. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  48229. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  48230. wolfSSL_CTX_free(ctx);
  48231. ctx = NULL;
  48232. res = TEST_RES_CHECK(1);
  48233. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB */
  48234. return res;
  48235. }
  48236. static int test_CONF_modules_xxx(void)
  48237. {
  48238. int res = TEST_SKIPPED;
  48239. #if defined(OPENSSL_EXTRA)
  48240. CONF_modules_free();
  48241. AssertTrue(1); /* to confirm previous call gives no harm */
  48242. CONF_modules_unload(0);
  48243. AssertTrue(1);
  48244. CONF_modules_unload(1);
  48245. AssertTrue(1);
  48246. CONF_modules_unload(-1);
  48247. AssertTrue(1);
  48248. res = TEST_RES_CHECK(1);
  48249. #endif /* OPENSSL_EXTRA */
  48250. return res;
  48251. }
  48252. static int test_CRYPTO_set_dynlock_xxx(void)
  48253. {
  48254. int res = TEST_SKIPPED;
  48255. #if defined(OPENSSL_EXTRA)
  48256. CRYPTO_set_dynlock_create_callback(
  48257. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  48258. CRYPTO_set_dynlock_create_callback(
  48259. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  48260. CRYPTO_set_dynlock_destroy_callback(
  48261. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  48262. CRYPTO_set_dynlock_destroy_callback(
  48263. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  48264. CRYPTO_set_dynlock_lock_callback(
  48265. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  48266. CRYPTO_set_dynlock_lock_callback(
  48267. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  48268. AssertTrue(1); /* to confirm previous call gives no harm */
  48269. res = TEST_RES_CHECK(1);
  48270. #endif /* OPENSSL_EXTRA */
  48271. return res;
  48272. }
  48273. static int test_CRYPTO_THREADID_xxx(void)
  48274. {
  48275. int res = TEST_SKIPPED;
  48276. #if defined(OPENSSL_EXTRA)
  48277. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  48278. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  48279. AssertIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  48280. res = TEST_RES_CHECK(1);
  48281. #endif /* OPENSSL_EXTRA */
  48282. return res;
  48283. }
  48284. static int test_ENGINE_cleanup(void)
  48285. {
  48286. int res = TEST_SKIPPED;
  48287. #if defined(OPENSSL_EXTRA)
  48288. ENGINE_cleanup();
  48289. AssertTrue(1); /* to confirm previous call gives no harm */
  48290. res = TEST_RES_CHECK(1);
  48291. #endif /* OPENSSL_EXTRA */
  48292. return res;
  48293. }
  48294. static int test_wolfSSL_CTX_LoadCRL(void)
  48295. {
  48296. int res = TEST_SKIPPED;
  48297. #if defined(HAVE_CRL) && !defined(NO_RSA)
  48298. WOLFSSL_CTX* ctx = NULL;
  48299. WOLFSSL* ssl = NULL;
  48300. const char* badPath = "dummypath";
  48301. const char* validPath = "./certs/crl";
  48302. const char* validFilePath = "./certs/crl/cliCrl.pem";
  48303. const char* issuerCert = "./certs/client-cert.pem";
  48304. int derType = WOLFSSL_FILETYPE_ASN1;
  48305. int pemType = WOLFSSL_FILETYPE_PEM;
  48306. int monitor = WOLFSSL_CRL_MONITOR;
  48307. WOLFSSL_CERT_MANAGER* cm = NULL;
  48308. #define FAIL_T1(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
  48309. BAD_FUNC_ARG)
  48310. #define SUCC_T(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
  48311. WOLFSSL_SUCCESS)
  48312. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  48313. #ifndef NO_WOLFSSL_CLIENT
  48314. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  48315. #elif !defined(NO_WOLFSSL_SERVER)
  48316. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  48317. #else
  48318. return;
  48319. #endif
  48320. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  48321. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  48322. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  48323. wolfSSL_CTX_free(ctx);
  48324. #ifndef NO_WOLFSSL_CLIENT
  48325. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  48326. #elif !defined(NO_WOLFSSL_SERVER)
  48327. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  48328. #else
  48329. return;
  48330. #endif
  48331. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  48332. WOLFSSL_SUCCESS);
  48333. AssertIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  48334. wolfSSL_CTX_free(ctx);
  48335. #ifndef NO_WOLFSSL_CLIENT
  48336. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  48337. #elif !defined(NO_WOLFSSL_SERVER)
  48338. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  48339. #else
  48340. return;
  48341. #endif
  48342. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  48343. WOLFSSL_SUCCESS);
  48344. AssertNotNull(ssl = wolfSSL_new(ctx));
  48345. AssertIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  48346. wolfSSL_free(ssl);
  48347. wolfSSL_CTX_free(ctx);
  48348. AssertNotNull(cm = wolfSSL_CertManagerNew());
  48349. AssertIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  48350. WOLFSSL_SUCCESS);
  48351. AssertIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType), WOLFSSL_SUCCESS);
  48352. wolfSSL_CertManagerFree(cm);
  48353. res = TEST_RES_CHECK(1);
  48354. #endif
  48355. return res;
  48356. }
  48357. static int test_SetTmpEC_DHE_Sz(void)
  48358. {
  48359. int res = TEST_SKIPPED;
  48360. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  48361. WOLFSSL_CTX *ctx;
  48362. WOLFSSL *ssl;
  48363. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  48364. AssertNotNull(ctx);
  48365. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  48366. ssl = wolfSSL_new(ctx);
  48367. AssertNotNull(ssl);
  48368. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  48369. wolfSSL_free(ssl);
  48370. wolfSSL_CTX_free(ctx);
  48371. res = TEST_RES_CHECK(1);
  48372. #endif
  48373. return res;
  48374. }
  48375. static int test_wolfSSL_CTX_get0_privatekey(void)
  48376. {
  48377. int res = TEST_SKIPPED;
  48378. #ifdef OPENSSL_ALL
  48379. WOLFSSL_CTX* ctx = NULL;
  48380. #ifndef NO_RSA
  48381. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  48382. AssertNull(SSL_CTX_get0_privatekey(ctx));
  48383. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  48384. WOLFSSL_FILETYPE_PEM));
  48385. AssertNull(SSL_CTX_get0_privatekey(ctx));
  48386. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  48387. WOLFSSL_FILETYPE_PEM));
  48388. AssertNotNull(SSL_CTX_get0_privatekey(ctx));
  48389. wolfSSL_CTX_free(ctx);
  48390. #endif
  48391. #ifdef HAVE_ECC
  48392. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  48393. AssertNull(SSL_CTX_get0_privatekey(ctx));
  48394. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  48395. WOLFSSL_FILETYPE_PEM));
  48396. AssertNull(SSL_CTX_get0_privatekey(ctx));
  48397. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  48398. WOLFSSL_FILETYPE_PEM));
  48399. AssertNotNull(SSL_CTX_get0_privatekey(ctx));
  48400. wolfSSL_CTX_free(ctx);
  48401. #endif
  48402. res = TEST_RES_CHECK(1);
  48403. #endif
  48404. return res;
  48405. }
  48406. static int test_wolfSSL_dtls_set_mtu(void)
  48407. {
  48408. int res = TEST_SKIPPED;
  48409. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  48410. !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_DTLS)
  48411. WOLFSSL_CTX* ctx = NULL;
  48412. WOLFSSL* ssl = NULL;
  48413. const char* testCertFile;
  48414. const char* testKeyFile;
  48415. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  48416. #ifndef NO_RSA
  48417. testCertFile = svrCertFile;
  48418. testKeyFile = svrKeyFile;
  48419. #elif defined(HAVE_ECC)
  48420. testCertFile = eccCertFile;
  48421. testKeyFile = eccKeyFile;
  48422. #endif
  48423. if (testCertFile != NULL && testKeyFile != NULL) {
  48424. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  48425. WOLFSSL_FILETYPE_PEM));
  48426. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  48427. WOLFSSL_FILETYPE_PEM));
  48428. }
  48429. AssertNotNull(ssl = wolfSSL_new(ctx));
  48430. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  48431. AssertIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  48432. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  48433. AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  48434. AssertIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  48435. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  48436. AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  48437. wolfSSL_free(ssl);
  48438. wolfSSL_CTX_free(ctx);
  48439. res = TEST_RES_CHECK(1);
  48440. #endif
  48441. return res;
  48442. }
  48443. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  48444. defined(WOLFSSL_DTLS)
  48445. static WC_INLINE void generateDTLSMsg(byte* out, int outSz, word32 seq,
  48446. enum HandShakeType hsType, word16 length)
  48447. {
  48448. size_t idx = 0;
  48449. byte* l;
  48450. /* record layer */
  48451. /* handshake type */
  48452. out[idx++] = handshake;
  48453. /* protocol version */
  48454. out[idx++] = 0xfe;
  48455. out[idx++] = 0xfd; /* DTLS 1.2 */
  48456. /* epoch 0 */
  48457. XMEMSET(out + idx, 0, 2);
  48458. idx += 2;
  48459. /* sequence number */
  48460. XMEMSET(out + idx, 0, 6);
  48461. c32toa(seq, out + idx + 2);
  48462. idx += 6;
  48463. /* length in BE */
  48464. if (length)
  48465. c16toa(length, out + idx);
  48466. else
  48467. c16toa(outSz - idx - 2, out + idx);
  48468. idx += 2;
  48469. /* handshake layer */
  48470. /* handshake type */
  48471. out[idx++] = (byte)hsType;
  48472. /* length */
  48473. l = out + idx;
  48474. idx += 3;
  48475. /* message seq */
  48476. c16toa(0, out + idx);
  48477. idx += 2;
  48478. /* frag offset */
  48479. c32to24(0, out + idx);
  48480. idx += 3;
  48481. /* frag length */
  48482. c32to24((word32)outSz - (word32)idx - 3, l);
  48483. c32to24((word32)outSz - (word32)idx - 3, out + idx);
  48484. idx += 3;
  48485. XMEMSET(out + idx, 0, outSz - idx);
  48486. }
  48487. static void test_wolfSSL_dtls_plaintext_server(WOLFSSL* ssl)
  48488. {
  48489. byte msg[] = "This is a msg for the client";
  48490. byte reply[40];
  48491. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  48492. reply[sizeof(reply) - 1] = '\0';
  48493. fprintf(stderr, "Client message: %s\n", reply);
  48494. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  48495. }
  48496. static void test_wolfSSL_dtls_plaintext_client(WOLFSSL* ssl)
  48497. {
  48498. byte ch[50];
  48499. int fd = wolfSSL_get_fd(ssl);
  48500. byte msg[] = "This is a msg for the server";
  48501. byte reply[40];
  48502. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 0);
  48503. /* Server should ignore this datagram */
  48504. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  48505. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 10000);
  48506. /* Server should ignore this datagram */
  48507. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  48508. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  48509. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  48510. reply[sizeof(reply) - 1] = '\0';
  48511. fprintf(stderr, "Server response: %s\n", reply);
  48512. }
  48513. static int test_wolfSSL_dtls_plaintext(void)
  48514. {
  48515. callback_functions func_cb_client;
  48516. callback_functions func_cb_server;
  48517. size_t i;
  48518. struct test_params {
  48519. method_provider client_meth;
  48520. method_provider server_meth;
  48521. ssl_callback on_result_server;
  48522. ssl_callback on_result_client;
  48523. } params[] = {
  48524. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  48525. test_wolfSSL_dtls_plaintext_server,
  48526. test_wolfSSL_dtls_plaintext_client},
  48527. };
  48528. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  48529. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  48530. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  48531. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  48532. func_cb_server.method = params[i].server_meth;
  48533. func_cb_client.method = params[i].client_meth;
  48534. func_cb_client.on_result = params[i].on_result_client;
  48535. func_cb_server.on_result = params[i].on_result_server;
  48536. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  48537. if (!func_cb_client.return_code)
  48538. return TEST_FAIL;
  48539. if (!func_cb_server.return_code)
  48540. return TEST_FAIL;
  48541. }
  48542. return TEST_RES_CHECK(1);
  48543. }
  48544. #else
  48545. static int test_wolfSSL_dtls_plaintext(void) {
  48546. return TEST_SKIPPED;
  48547. }
  48548. #endif
  48549. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  48550. defined(WOLFSSL_DTLS)
  48551. static void test_wolfSSL_dtls12_fragments_spammer(WOLFSSL* ssl)
  48552. {
  48553. byte b[1100]; /* buffer for the messages to send */
  48554. size_t idx = 0;
  48555. size_t seq_offset = 0;
  48556. size_t msg_offset = 0;
  48557. int i;
  48558. int fd = wolfSSL_get_fd(ssl);
  48559. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  48560. word32 seq_number = 100; /* start high so server definitely reads this */
  48561. word16 msg_number = 50; /* start high so server has to buffer this */
  48562. AssertIntEQ(ret, 1);
  48563. /* Now let's start spamming the peer with fragments it needs to store */
  48564. XMEMSET(b, -1, sizeof(b));
  48565. /* record layer */
  48566. /* handshake type */
  48567. b[idx++] = 22;
  48568. /* protocol version */
  48569. b[idx++] = 0xfe;
  48570. b[idx++] = 0xfd; /* DTLS 1.2 */
  48571. /* epoch 0 */
  48572. XMEMSET(b + idx, 0, 2);
  48573. idx += 2;
  48574. /* sequence number */
  48575. XMEMSET(b + idx, 0, 6);
  48576. seq_offset = idx + 2; /* increment only the low 32 bits */
  48577. idx += 6;
  48578. /* static length in BE */
  48579. c16toa(42, b + idx);
  48580. idx += 2;
  48581. /* handshake layer */
  48582. /* cert type */
  48583. b[idx++] = 11;
  48584. /* length */
  48585. c32to24(1000, b + idx);
  48586. idx += 3;
  48587. /* message seq */
  48588. c16toa(0, b + idx);
  48589. msg_offset = idx;
  48590. idx += 2;
  48591. /* frag offset */
  48592. c32to24(500, b + idx);
  48593. idx += 3;
  48594. /* frag length */
  48595. c32to24(30, b + idx);
  48596. idx += 3;
  48597. (void)idx; /* inhibit clang-analyzer-deadcode.DeadStores */
  48598. for (i = 0; i < DTLS_POOL_SZ * 2 && ret > 0;
  48599. seq_number++, msg_number++, i++) {
  48600. struct timespec delay;
  48601. XMEMSET(&delay, 0, sizeof(delay));
  48602. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  48603. c32toa(seq_number, b + seq_offset);
  48604. c16toa(msg_number, b + msg_offset);
  48605. ret = (int)send(fd, b, 55, 0);
  48606. nanosleep(&delay, NULL);
  48607. }
  48608. }
  48609. #ifdef WOLFSSL_DTLS13
  48610. static void test_wolfSSL_dtls13_fragments_spammer(WOLFSSL* ssl)
  48611. {
  48612. byte b[150]; /* buffer for the messages to send */
  48613. size_t idx = 0;
  48614. size_t msg_offset = 0;
  48615. int fd = wolfSSL_get_fd(ssl);
  48616. word16 msg_number = 10; /* start high so server has to buffer this */
  48617. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  48618. AssertIntEQ(ret, 1);
  48619. /* Now let's start spamming the peer with fragments it needs to store */
  48620. XMEMSET(b, -1, sizeof(b));
  48621. /* handshake type */
  48622. b[idx++] = 11;
  48623. /* length */
  48624. c32to24(10000, b + idx);
  48625. idx += 3;
  48626. /* message_seq */
  48627. msg_offset = idx;
  48628. idx += 2;
  48629. /* fragment_offset */
  48630. c32to24(5000, b + idx);
  48631. idx += 3;
  48632. /* fragment_length */
  48633. c32to24(100, b + idx);
  48634. idx += 3;
  48635. /* fragment contents */
  48636. idx += 100;
  48637. for (; ret > 0; msg_number++) {
  48638. byte sendBuf[150];
  48639. int sendSz = sizeof(sendBuf);
  48640. struct timespec delay;
  48641. XMEMSET(&delay, 0, sizeof(delay));
  48642. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  48643. c16toa(msg_number, b + msg_offset);
  48644. sendSz = BuildTls13Message(ssl, sendBuf, sendSz, b,
  48645. (int)idx, handshake, 0, 0, 0);
  48646. ret = (int)send(fd, sendBuf, (size_t)sendSz, 0);
  48647. nanosleep(&delay, NULL);
  48648. }
  48649. }
  48650. #endif
  48651. static int test_wolfSSL_dtls_fragments(void)
  48652. {
  48653. callback_functions func_cb_client;
  48654. callback_functions func_cb_server;
  48655. size_t i;
  48656. struct test_params {
  48657. method_provider client_meth;
  48658. method_provider server_meth;
  48659. ssl_callback spammer;
  48660. } params[] = {
  48661. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  48662. test_wolfSSL_dtls12_fragments_spammer},
  48663. #ifdef WOLFSSL_DTLS13
  48664. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  48665. test_wolfSSL_dtls13_fragments_spammer},
  48666. #endif
  48667. };
  48668. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  48669. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  48670. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  48671. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  48672. func_cb_server.method = params[i].server_meth;
  48673. func_cb_client.method = params[i].client_meth;
  48674. func_cb_client.ssl_ready = params[i].spammer;
  48675. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  48676. AssertFalse(func_cb_client.return_code);
  48677. AssertFalse(func_cb_server.return_code);
  48678. /* The socket should be closed by the server resulting in a
  48679. * socket error */
  48680. AssertIntEQ(func_cb_client.last_err, SOCKET_ERROR_E);
  48681. /* Check the server returned an error indicating the msg buffer
  48682. * was full */
  48683. AssertIntEQ(func_cb_server.last_err, DTLS_TOO_MANY_FRAGMENTS_E);
  48684. }
  48685. return TEST_RES_CHECK(1);
  48686. }
  48687. static void test_wolfSSL_dtls_send_alert(WOLFSSL* ssl)
  48688. {
  48689. int fd, ret;
  48690. byte alert_msg[] = {
  48691. 0x15, /* alert type */
  48692. 0xfe, 0xfd, /* version */
  48693. 0x00, 0x00, /* epoch */
  48694. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* seq number */
  48695. 0x00, 0x02, /* length */
  48696. 0x02, /* level: fatal */
  48697. 0x46 /* protocol version */
  48698. };
  48699. fd = wolfSSL_get_fd(ssl);
  48700. ret = (int)send(fd, alert_msg, sizeof(alert_msg), 0);
  48701. AssertIntGT(ret, 0);
  48702. }
  48703. static int _test_wolfSSL_ignore_alert_before_cookie(byte version12)
  48704. {
  48705. callback_functions client_cbs, server_cbs;
  48706. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  48707. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  48708. client_cbs.doUdp = server_cbs.doUdp = 1;
  48709. if (version12) {
  48710. client_cbs.method = wolfDTLSv1_2_client_method;
  48711. server_cbs.method = wolfDTLSv1_2_server_method;
  48712. }
  48713. else {
  48714. #ifdef WOLFSSL_DTLS13
  48715. client_cbs.method = wolfDTLSv1_3_client_method;
  48716. server_cbs.method = wolfDTLSv1_3_server_method;
  48717. #else
  48718. return TEST_SKIPPED;
  48719. #endif /* WOLFSSL_DTLS13 */
  48720. }
  48721. client_cbs.ssl_ready = test_wolfSSL_dtls_send_alert;
  48722. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  48723. if (!client_cbs.return_code)
  48724. return TEST_FAIL;
  48725. if (!server_cbs.return_code)
  48726. return TEST_FAIL;
  48727. return TEST_SUCCESS;
  48728. }
  48729. static int test_wolfSSL_ignore_alert_before_cookie(void)
  48730. {
  48731. int ret;
  48732. ret =_test_wolfSSL_ignore_alert_before_cookie(0);
  48733. if (ret != 0)
  48734. return ret;
  48735. ret =_test_wolfSSL_ignore_alert_before_cookie(1);
  48736. if (ret != 0)
  48737. return ret;
  48738. return 0;
  48739. }
  48740. static void test_wolfSSL_send_bad_record(WOLFSSL* ssl)
  48741. {
  48742. int ret;
  48743. int fd;
  48744. byte bad_msg[] = {
  48745. 0x17, /* app data */
  48746. 0xaa, 0xfd, /* bad version */
  48747. 0x00, 0x01, /* epoch 1 */
  48748. 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, /* not seen seq number */
  48749. 0x00, 0x26, /* length: 38 bytes */
  48750. 0xae, 0x30, 0x31, 0xb1, 0xf1, 0xb9, 0x6f, 0xda, 0x17, 0x19, 0xd9, 0x57,
  48751. 0xa9, 0x9d, 0x5c, 0x51, 0x9b, 0x53, 0x63, 0xa5, 0x24, 0x70, 0xa1,
  48752. 0xae, 0xdf, 0x1c, 0xb9, 0xfc, 0xe3, 0xd7, 0x77, 0x6d, 0xb6, 0x89, 0x0f,
  48753. 0x03, 0x18, 0x72
  48754. };
  48755. fd = wolfSSL_get_fd(ssl);
  48756. AssertIntGE(fd, 0);
  48757. ret = (int)send(fd, bad_msg, sizeof(bad_msg), 0);
  48758. AssertIntEQ(ret, sizeof(bad_msg));
  48759. ret = wolfSSL_write(ssl, "badrecordtest", sizeof("badrecordtest"));
  48760. AssertIntEQ(ret, sizeof("badrecordtest"));
  48761. }
  48762. static void test_wolfSSL_read_string(WOLFSSL* ssl)
  48763. {
  48764. byte buf[100];
  48765. int ret;
  48766. ret = wolfSSL_read(ssl, buf, sizeof(buf));
  48767. AssertIntGT(ret, 0);
  48768. AssertIntEQ(strcmp((char*)buf, "badrecordtest"), 0);
  48769. }
  48770. static int _test_wolfSSL_dtls_bad_record(
  48771. method_provider client_method, method_provider server_method)
  48772. {
  48773. callback_functions client_cbs, server_cbs;
  48774. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  48775. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  48776. client_cbs.doUdp = server_cbs.doUdp = 1;
  48777. client_cbs.method = client_method;
  48778. server_cbs.method = server_method;
  48779. client_cbs.on_result = test_wolfSSL_send_bad_record;
  48780. server_cbs.on_result = test_wolfSSL_read_string;
  48781. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  48782. if (!client_cbs.return_code)
  48783. return TEST_FAIL;
  48784. if (!server_cbs.return_code)
  48785. return TEST_FAIL;
  48786. return TEST_SUCCESS;
  48787. }
  48788. static int test_wolfSSL_dtls_bad_record(void)
  48789. {
  48790. int ret;
  48791. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_2_client_method,
  48792. wolfDTLSv1_2_server_method);
  48793. #ifdef WOLFSSL_DTLS13
  48794. if (ret != TEST_SUCCESS)
  48795. return ret;
  48796. return _test_wolfSSL_dtls_bad_record(wolfDTLSv1_3_client_method,
  48797. wolfDTLSv1_3_server_method);
  48798. #else
  48799. return ret;
  48800. #endif /* WOLFSSL_DTLS13 */
  48801. }
  48802. #else
  48803. static int test_wolfSSL_dtls_fragments(void) {
  48804. return TEST_SKIPPED;
  48805. }
  48806. static int test_wolfSSL_ignore_alert_before_cookie(void) {
  48807. return TEST_SKIPPED;
  48808. }
  48809. static int test_wolfSSL_dtls_bad_record(void) {
  48810. return TEST_SKIPPED;
  48811. }
  48812. #endif
  48813. #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_TLS13_IGNORE_AEAD_LIMITS) && \
  48814. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  48815. defined(HAVE_IO_TESTS_DEPENDENCIES)
  48816. static byte test_AEAD_fail_decryption = 0;
  48817. static byte test_AEAD_seq_num = 0;
  48818. static byte test_AEAD_done = 0;
  48819. static int test_AEAD_cbiorecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  48820. {
  48821. int ret = (int)recv(wolfSSL_get_fd(ssl), buf, sz, 0);
  48822. if (ret > 0) {
  48823. if (test_AEAD_fail_decryption) {
  48824. /* Modify the packet to trigger a decryption failure */
  48825. buf[ret/2] ^= 0xFF;
  48826. if (test_AEAD_fail_decryption == 1)
  48827. test_AEAD_fail_decryption = 0;
  48828. }
  48829. }
  48830. (void)ctx;
  48831. return ret;
  48832. }
  48833. static void test_AEAD_get_limits(WOLFSSL* ssl, w64wrapper* hardLimit,
  48834. w64wrapper* keyUpdateLimit, w64wrapper* sendLimit)
  48835. {
  48836. if (sendLimit)
  48837. w64Zero(sendLimit);
  48838. switch (ssl->specs.bulk_cipher_algorithm) {
  48839. case wolfssl_aes_gcm:
  48840. if (sendLimit)
  48841. *sendLimit = AEAD_AES_LIMIT;
  48842. FALL_THROUGH;
  48843. case wolfssl_chacha:
  48844. if (hardLimit)
  48845. *hardLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT;
  48846. if (keyUpdateLimit)
  48847. *keyUpdateLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT;
  48848. break;
  48849. case wolfssl_aes_ccm:
  48850. if (sendLimit)
  48851. *sendLimit = DTLS_AEAD_AES_CCM_LIMIT;
  48852. if (ssl->specs.aead_mac_size == AES_CCM_8_AUTH_SZ) {
  48853. if (hardLimit)
  48854. *hardLimit = DTLS_AEAD_AES_CCM_8_FAIL_LIMIT;
  48855. if (keyUpdateLimit)
  48856. *keyUpdateLimit = DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT;
  48857. }
  48858. else {
  48859. if (hardLimit)
  48860. *hardLimit = DTLS_AEAD_AES_CCM_FAIL_LIMIT;
  48861. if (keyUpdateLimit)
  48862. *keyUpdateLimit = DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT;
  48863. }
  48864. break;
  48865. default:
  48866. fprintf(stderr, "Unrecognized bulk cipher");
  48867. AssertFalse(1);
  48868. break;
  48869. }
  48870. }
  48871. static void test_AEAD_limit_client(WOLFSSL* ssl)
  48872. {
  48873. int ret;
  48874. int i;
  48875. int didReKey = 0;
  48876. char msgBuf[20];
  48877. w64wrapper hardLimit;
  48878. w64wrapper keyUpdateLimit;
  48879. w64wrapper counter;
  48880. w64wrapper sendLimit;
  48881. test_AEAD_get_limits(ssl, &hardLimit, &keyUpdateLimit, &sendLimit);
  48882. w64Zero(&counter);
  48883. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter));
  48884. wolfSSL_SSLSetIORecv(ssl, test_AEAD_cbiorecv);
  48885. for (i = 0; i < 10; i++) {
  48886. /* Test some failed decryptions */
  48887. test_AEAD_fail_decryption = 1;
  48888. w64Increment(&counter);
  48889. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  48890. /* Should succeed since decryption failures are dropped */
  48891. AssertIntGT(ret, 0);
  48892. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter));
  48893. }
  48894. test_AEAD_fail_decryption = 1;
  48895. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = keyUpdateLimit;
  48896. w64Increment(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  48897. /* 100 read calls should be enough to complete the key update */
  48898. w64Zero(&counter);
  48899. for (i = 0; i < 100; i++) {
  48900. /* Key update should be sent and negotiated */
  48901. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  48902. AssertIntGT(ret, 0);
  48903. /* Epoch after one key update is 4 */
  48904. if (w64Equal(ssl->dtls13PeerEpoch, w64From32(0, 4)) &&
  48905. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter)) {
  48906. didReKey = 1;
  48907. break;
  48908. }
  48909. }
  48910. AssertTrue(didReKey);
  48911. if (!w64IsZero(sendLimit)) {
  48912. /* Test the sending limit for AEAD ciphers */
  48913. Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->nextSeqNumber = sendLimit;
  48914. test_AEAD_seq_num = 1;
  48915. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  48916. AssertIntGT(ret, 0);
  48917. didReKey = 0;
  48918. w64Zero(&counter);
  48919. /* 100 read calls should be enough to complete the key update */
  48920. for (i = 0; i < 100; i++) {
  48921. /* Key update should be sent and negotiated */
  48922. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  48923. AssertIntGT(ret, 0);
  48924. /* Epoch after another key update is 5 */
  48925. if (w64Equal(ssl->dtls13Epoch, w64From32(0, 5)) &&
  48926. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter)) {
  48927. didReKey = 1;
  48928. break;
  48929. }
  48930. }
  48931. AssertTrue(didReKey);
  48932. }
  48933. test_AEAD_fail_decryption = 2;
  48934. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = hardLimit;
  48935. w64Decrement(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  48936. /* Connection should fail with a DECRYPT_ERROR */
  48937. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  48938. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  48939. AssertIntEQ(wolfSSL_get_error(ssl, ret), DECRYPT_ERROR);
  48940. test_AEAD_done = 1;
  48941. }
  48942. int counter = 0;
  48943. static void test_AEAD_limit_server(WOLFSSL* ssl)
  48944. {
  48945. char msgBuf[] = "Sending data";
  48946. int ret = WOLFSSL_SUCCESS;
  48947. w64wrapper sendLimit;
  48948. SOCKET_T fd = wolfSSL_get_fd(ssl);
  48949. struct timespec delay;
  48950. XMEMSET(&delay, 0, sizeof(delay));
  48951. delay.tv_nsec = 100000000; /* wait 0.1 seconds */
  48952. tcp_set_nonblocking(&fd); /* So that read doesn't block */
  48953. test_AEAD_get_limits(ssl, NULL, NULL, &sendLimit);
  48954. while (!test_AEAD_done && ret > 0) {
  48955. counter++;
  48956. if (test_AEAD_seq_num) {
  48957. /* We need to update the seq number so that we can understand the
  48958. * peer. Otherwise we will incorrectly interpret the seq number. */
  48959. Dtls13Epoch* e = Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch);
  48960. AssertNotNull(e);
  48961. e->nextPeerSeqNumber = sendLimit;
  48962. test_AEAD_seq_num = 0;
  48963. }
  48964. (void)wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  48965. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  48966. nanosleep(&delay, NULL);
  48967. }
  48968. }
  48969. static int test_wolfSSL_dtls_AEAD_limit(void)
  48970. {
  48971. callback_functions func_cb_client;
  48972. callback_functions func_cb_server;
  48973. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  48974. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  48975. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  48976. func_cb_server.method = wolfDTLSv1_3_server_method;
  48977. func_cb_client.method = wolfDTLSv1_3_client_method;
  48978. func_cb_server.on_result = test_AEAD_limit_server;
  48979. func_cb_client.on_result = test_AEAD_limit_client;
  48980. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  48981. if (!func_cb_client.return_code)
  48982. return TEST_FAIL;
  48983. if (!func_cb_server.return_code)
  48984. return TEST_FAIL;
  48985. return TEST_SUCCESS;
  48986. }
  48987. #else
  48988. static int test_wolfSSL_dtls_AEAD_limit(void)
  48989. {
  48990. return TEST_SKIPPED;
  48991. }
  48992. #endif
  48993. #if defined(WOLFSSL_DTLS) && \
  48994. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  48995. static void test_wolfSSL_dtls_send_ch(WOLFSSL* ssl)
  48996. {
  48997. int fd, ret;
  48998. byte ch_msg[] = {
  48999. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  49000. 0xfa, 0x01, 0x00, 0x01, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  49001. 0xee, 0xfe, 0xfd, 0xc0, 0xca, 0xb5, 0x6f, 0x3d, 0x23, 0xcc, 0x53, 0x9a,
  49002. 0x67, 0x17, 0x70, 0xd3, 0xfb, 0x23, 0x16, 0x9e, 0x4e, 0xd6, 0x7e, 0x29,
  49003. 0xab, 0xfa, 0x4c, 0xa5, 0x84, 0x95, 0xc3, 0xdb, 0x21, 0x9a, 0x52, 0x00,
  49004. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  49005. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  49006. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  49007. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  49008. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  49009. 0x8e, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  49010. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  49011. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  49012. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  49013. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  49014. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17,
  49015. 0x00, 0x41, 0x04, 0x96, 0xcb, 0x2e, 0x4e, 0xd9, 0x88, 0x71, 0xc7, 0xf3,
  49016. 0x1a, 0x16, 0xdd, 0x7a, 0x7c, 0xf7, 0x67, 0x8a, 0x5d, 0x9a, 0x55, 0xa6,
  49017. 0x4a, 0x90, 0xd9, 0xfb, 0xc7, 0xfb, 0xbe, 0x09, 0xa9, 0x8a, 0xb5, 0x7a,
  49018. 0xd1, 0xde, 0x83, 0x74, 0x27, 0x31, 0x1c, 0xaa, 0xae, 0xef, 0x58, 0x43,
  49019. 0x13, 0x7d, 0x15, 0x4d, 0x7f, 0x68, 0xf6, 0x8a, 0x38, 0xef, 0x0e, 0xb3,
  49020. 0xcf, 0xb8, 0x4a, 0xa9, 0xb4, 0xd7, 0xcb, 0x01, 0x00, 0x01, 0x00, 0x1d,
  49021. 0x0a, 0x22, 0x8a, 0xd1, 0x78, 0x85, 0x1e, 0x5a, 0xe1, 0x1d, 0x1e, 0xb7,
  49022. 0x2d, 0xbc, 0x5f, 0x52, 0xbc, 0x97, 0x5d, 0x8b, 0x6a, 0x8b, 0x9d, 0x1e,
  49023. 0xb1, 0xfc, 0x8a, 0xb2, 0x56, 0xcd, 0xed, 0x4b, 0xfb, 0x66, 0x3f, 0x59,
  49024. 0x3f, 0x15, 0x5d, 0x09, 0x9e, 0x2f, 0x60, 0x5b, 0x31, 0x81, 0x27, 0xf0,
  49025. 0x1c, 0xda, 0xcd, 0x48, 0x66, 0xc6, 0xbb, 0x25, 0xf0, 0x5f, 0xda, 0x4c,
  49026. 0xcf, 0x1d, 0x88, 0xc8, 0xda, 0x1b, 0x53, 0xea, 0xbd, 0xce, 0x6d, 0xf6,
  49027. 0x4a, 0x76, 0xdb, 0x75, 0x99, 0xaf, 0xcf, 0x76, 0x4a, 0xfb, 0xe3, 0xef,
  49028. 0xb2, 0xcb, 0xae, 0x4a, 0xc0, 0xe8, 0x63, 0x1f, 0xd6, 0xe8, 0xe6, 0x45,
  49029. 0xf9, 0xea, 0x0d, 0x06, 0x19, 0xfc, 0xb1, 0xfd, 0x5d, 0x92, 0x89, 0x7b,
  49030. 0xc7, 0x9f, 0x1a, 0xb3, 0x2b, 0xc7, 0xad, 0x0e, 0xfb, 0x13, 0x41, 0x83,
  49031. 0x84, 0x58, 0x3a, 0x25, 0xb9, 0x49, 0x35, 0x1c, 0x23, 0xcb, 0xd6, 0xe7,
  49032. 0xc2, 0x8c, 0x4b, 0x2a, 0x73, 0xa1, 0xdf, 0x4f, 0x73, 0x9b, 0xb3, 0xd2,
  49033. 0xb2, 0x95, 0x00, 0x3c, 0x26, 0x09, 0x89, 0x71, 0x05, 0x39, 0xc8, 0x98,
  49034. 0x8f, 0xed, 0x32, 0x15, 0x78, 0xcd, 0xd3, 0x7e, 0xfb, 0x5a, 0x78, 0x2a,
  49035. 0xdc, 0xca, 0x20, 0x09, 0xb5, 0x14, 0xf9, 0xd4, 0x58, 0xf6, 0x69, 0xf8,
  49036. 0x65, 0x9f, 0xb7, 0xe4, 0x93, 0xf1, 0xa3, 0x84, 0x7e, 0x1b, 0x23, 0x5d,
  49037. 0xea, 0x59, 0x3e, 0x4d, 0xca, 0xfd, 0xa5, 0x55, 0xdd, 0x99, 0xb5, 0x02,
  49038. 0xf8, 0x0d, 0xe5, 0xf4, 0x06, 0xb0, 0x43, 0x9e, 0x2e, 0xbf, 0x05, 0x33,
  49039. 0x65, 0x7b, 0x13, 0x8c, 0xf9, 0x16, 0x4d, 0xc5, 0x15, 0x0b, 0x40, 0x2f,
  49040. 0x66, 0x94, 0xf2, 0x43, 0x95, 0xe7, 0xa9, 0xb6, 0x39, 0x99, 0x73, 0xb3,
  49041. 0xb0, 0x06, 0xfe, 0x52, 0x9e, 0x57, 0xba, 0x75, 0xfd, 0x76, 0x7b, 0x20,
  49042. 0x31, 0x68, 0x4c
  49043. };
  49044. fd = wolfSSL_get_fd(ssl);
  49045. ret = (int)send(fd, ch_msg, sizeof(ch_msg), 0);
  49046. AssertIntGT(ret, 0);
  49047. /* consume the HRR otherwise handshake will fail */
  49048. ret = (int)recv(fd, ch_msg, sizeof(ch_msg), 0);
  49049. AssertIntGT(ret, 0);
  49050. }
  49051. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  49052. static void test_wolfSSL_dtls_enable_hrrcookie(WOLFSSL* ssl)
  49053. {
  49054. int ret;
  49055. ret = wolfSSL_send_hrr_cookie(ssl, NULL, 0);
  49056. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  49057. }
  49058. #endif
  49059. static int test_wolfSSL_dtls_stateless(void)
  49060. {
  49061. callback_functions client_cbs, server_cbs;
  49062. size_t i;
  49063. struct {
  49064. method_provider client_meth;
  49065. method_provider server_meth;
  49066. ssl_callback client_ssl_ready;
  49067. ssl_callback server_ssl_ready;
  49068. } test_params[] = {
  49069. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  49070. test_wolfSSL_dtls_send_ch, NULL},
  49071. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  49072. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  49073. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_enable_hrrcookie},
  49074. #endif
  49075. };
  49076. for (i = 0; i < sizeof(test_params)/sizeof(*test_params); i++) {
  49077. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  49078. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  49079. client_cbs.doUdp = server_cbs.doUdp = 1;
  49080. client_cbs.method = test_params[i].client_meth;
  49081. server_cbs.method = test_params[i].server_meth;
  49082. client_cbs.ssl_ready = test_params[i].client_ssl_ready;
  49083. server_cbs.ssl_ready = test_params[i].server_ssl_ready;
  49084. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  49085. if (!client_cbs.return_code)
  49086. return TEST_FAIL;
  49087. if (!server_cbs.return_code)
  49088. return TEST_FAIL;
  49089. }
  49090. return TEST_SUCCESS;
  49091. }
  49092. #else
  49093. static int test_wolfSSL_dtls_stateless(void)
  49094. {
  49095. return TEST_SKIPPED;
  49096. }
  49097. #endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE &&
  49098. * HAVE_IO_TESTS_DEPENDENCIES && !SINGLE_THREADED */
  49099. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  49100. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  49101. !defined(WOLFSSL_NO_CLIENT_AUTH))
  49102. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  49103. {
  49104. int ret;
  49105. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  49106. fprintf(stderr, "loading cert %s failed\n", certA);
  49107. fprintf(stderr, "Error: (%d): %s\n", ret,
  49108. wolfSSL_ERR_reason_error_string(ret));
  49109. return -1;
  49110. }
  49111. return 0;
  49112. }
  49113. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  49114. {
  49115. int ret;
  49116. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  49117. != WOLFSSL_SUCCESS) {
  49118. fprintf(stderr, "could not verify the cert: %s\n", certA);
  49119. fprintf(stderr, "Error: (%d): %s\n", ret,
  49120. wolfSSL_ERR_reason_error_string(ret));
  49121. return -1;
  49122. }
  49123. else {
  49124. fprintf(stderr, "successfully verified: %s\n", certA);
  49125. }
  49126. return 0;
  49127. }
  49128. #define LOAD_ONE_CA(a, b, c, d) \
  49129. do { \
  49130. (a) = load_ca_into_cm(c, d); \
  49131. if ((a) != 0) \
  49132. return (b); \
  49133. else \
  49134. (b)--; \
  49135. } while(0)
  49136. #define VERIFY_ONE_CERT(a, b, c, d) \
  49137. do { \
  49138. (a) = verify_cert_with_cm(c, d); \
  49139. if ((a) != 0) \
  49140. return (b); \
  49141. else \
  49142. (b)--; \
  49143. } while(0)
  49144. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  49145. {
  49146. int ret;
  49147. int i = -1;
  49148. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  49149. char chainGArr[9][50] = {"certs/ca-cert.pem",
  49150. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  49151. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  49152. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  49153. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  49154. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  49155. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  49156. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  49157. "certs/test-pathlen/chainG-entity.pem"};
  49158. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  49159. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  49160. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  49161. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  49162. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  49163. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  49164. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  49165. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  49166. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  49167. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  49168. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  49169. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  49170. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  49171. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  49172. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  49173. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  49174. /* test validating the entity twice, should have no effect on pathLen since
  49175. * entity/leaf cert */
  49176. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  49177. return ret;
  49178. }
  49179. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  49180. {
  49181. int ret;
  49182. int i = -1;
  49183. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  49184. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  49185. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  49186. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  49187. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  49188. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  49189. */
  49190. char chainHArr[6][50] = {"certs/ca-cert.pem",
  49191. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  49192. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  49193. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  49194. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  49195. "certs/test-pathlen/chainH-entity.pem"};
  49196. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  49197. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  49198. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  49199. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  49200. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  49201. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  49202. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  49203. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  49204. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  49205. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  49206. return ret;
  49207. }
  49208. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  49209. {
  49210. int ret;
  49211. int i = -1;
  49212. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  49213. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  49214. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  49215. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  49216. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  49217. */
  49218. char chainIArr[5][50] = {"certs/ca-cert.pem",
  49219. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  49220. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  49221. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  49222. "certs/test-pathlen/chainI-entity.pem"};
  49223. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  49224. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  49225. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  49226. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  49227. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  49228. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  49229. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  49230. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  49231. return ret;
  49232. }
  49233. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  49234. {
  49235. int ret;
  49236. int i = -1;
  49237. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  49238. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  49239. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  49240. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  49241. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  49242. */
  49243. char chainJArr[6][50] = {"certs/ca-cert.pem",
  49244. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  49245. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  49246. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  49247. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  49248. "certs/test-pathlen/chainJ-entity.pem"};
  49249. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  49250. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  49251. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  49252. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  49253. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  49254. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  49255. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  49256. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  49257. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  49258. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  49259. return ret;
  49260. }
  49261. static int test_various_pathlen_chains(void)
  49262. {
  49263. int ret;
  49264. WOLFSSL_CERT_MANAGER* cm;
  49265. /* Test chain G (large chain with varying pathLens) */
  49266. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  49267. fprintf(stderr, "cert manager new failed\n");
  49268. return -1;
  49269. }
  49270. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  49271. AssertIntEQ(test_chainG(cm), -1);
  49272. #else
  49273. AssertIntEQ(test_chainG(cm), 0);
  49274. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  49275. ret = wolfSSL_CertManagerUnloadCAs(cm);
  49276. if (ret != WOLFSSL_SUCCESS)
  49277. return -1;
  49278. wolfSSL_CertManagerFree(cm);
  49279. /* end test chain G */
  49280. /* Test chain H (5 chain with same pathLens) */
  49281. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  49282. fprintf(stderr, "cert manager new failed\n");
  49283. return -1;
  49284. }
  49285. AssertIntLT(test_chainH(cm), 0);
  49286. wolfSSL_CertManagerUnloadCAs(cm);
  49287. wolfSSL_CertManagerFree(cm);
  49288. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  49289. fprintf(stderr, "cert manager new failed\n");
  49290. return -1;
  49291. }
  49292. ret = wolfSSL_CertManagerUnloadCAs(cm);
  49293. if (ret != WOLFSSL_SUCCESS)
  49294. return -1;
  49295. wolfSSL_CertManagerFree(cm);
  49296. /* end test chain H */
  49297. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  49298. * followed by 2 ICA's, should pass) */
  49299. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  49300. fprintf(stderr, "cert manager new failed\n");
  49301. return -1;
  49302. }
  49303. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  49304. AssertIntEQ(test_chainI(cm), -1);
  49305. #else
  49306. AssertIntEQ(test_chainI(cm), 0);
  49307. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  49308. wolfSSL_CertManagerUnloadCAs(cm);
  49309. wolfSSL_CertManagerFree(cm);
  49310. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  49311. fprintf(stderr, "cert manager new failed\n");
  49312. return -1;
  49313. }
  49314. ret = wolfSSL_CertManagerUnloadCAs(cm);
  49315. if (ret != WOLFSSL_SUCCESS)
  49316. return -1;
  49317. wolfSSL_CertManagerFree(cm);
  49318. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  49319. * this time followed by 3 ICA's, should fail */
  49320. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  49321. fprintf(stderr, "cert manager new failed\n");
  49322. return -1;
  49323. }
  49324. AssertIntLT(test_chainJ(cm), 0);
  49325. wolfSSL_CertManagerUnloadCAs(cm);
  49326. wolfSSL_CertManagerFree(cm);
  49327. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  49328. fprintf(stderr, "cert manager new failed\n");
  49329. return -1;
  49330. }
  49331. ret = wolfSSL_CertManagerUnloadCAs(cm);
  49332. wolfSSL_CertManagerFree(cm);
  49333. return TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  49334. }
  49335. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  49336. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  49337. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  49338. {
  49339. byte ekm[100] = {0};
  49340. (void)ctx;
  49341. /* Succes Cases */
  49342. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  49343. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  49344. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  49345. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  49346. /* Use some random context */
  49347. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  49348. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  49349. /* Failure cases */
  49350. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  49351. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  49352. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  49353. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  49354. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  49355. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  49356. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  49357. "extended master secret", XSTR_SIZEOF("extended master secret"), NULL, 0, 0), 0);
  49358. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  49359. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  49360. return TEST_RES_CHECK(1);
  49361. }
  49362. static void test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  49363. {
  49364. wolfSSL_KeepArrays(ssl);
  49365. }
  49366. static int test_export_keying_material(void)
  49367. {
  49368. int res = TEST_SKIPPED;
  49369. #ifndef SINGLE_THREADED
  49370. tcp_ready ready;
  49371. callback_functions clientCb;
  49372. func_args client_args;
  49373. func_args server_args;
  49374. THREAD_TYPE serverThread;
  49375. XMEMSET(&client_args, 0, sizeof(func_args));
  49376. XMEMSET(&server_args, 0, sizeof(func_args));
  49377. XMEMSET(&clientCb, 0, sizeof(callback_functions));
  49378. #ifdef WOLFSSL_TIRTOS
  49379. fdOpenSession(Task_self());
  49380. #endif
  49381. StartTCP();
  49382. InitTcpReady(&ready);
  49383. #if defined(USE_WINDOWS_API)
  49384. /* use RNG to get random port if using windows */
  49385. ready.port = GetRandomPort();
  49386. #endif
  49387. server_args.signal = &ready;
  49388. client_args.signal = &ready;
  49389. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  49390. client_args.callbacks = &clientCb;
  49391. start_thread(test_server_nofail, &server_args, &serverThread);
  49392. wait_tcp_ready(&server_args);
  49393. test_client_nofail(&client_args, test_export_keying_material_cb);
  49394. join_thread(serverThread);
  49395. AssertTrue(client_args.return_code);
  49396. AssertTrue(server_args.return_code);
  49397. FreeTcpReady(&ready);
  49398. #ifdef WOLFSSL_TIRTOS
  49399. fdOpenSession(Task_self());
  49400. #endif
  49401. res = TEST_RES_CHECK(1);
  49402. #endif /* !SINGLE_THREADED */
  49403. return res;
  49404. }
  49405. #endif /* HAVE_KEYING_MATERIAL */
  49406. static int test_wolfSSL_THREADID_hash(void)
  49407. {
  49408. int result = TEST_SKIPPED;
  49409. #if defined(OPENSSL_EXTRA)
  49410. unsigned long res;
  49411. CRYPTO_THREADID id;
  49412. CRYPTO_THREADID_current(NULL);
  49413. AssertTrue(1);
  49414. res = CRYPTO_THREADID_hash(NULL);
  49415. AssertTrue( res == 0UL);
  49416. XMEMSET(&id, 0, sizeof(id));
  49417. res = CRYPTO_THREADID_hash(&id);
  49418. AssertTrue( res == 0UL);
  49419. result = TEST_RES_CHECK(1);
  49420. #endif /* OPENSSL_EXTRA */
  49421. return result;
  49422. }
  49423. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  49424. {
  49425. int res = TEST_SKIPPED;
  49426. #if defined(OPENSSL_EXTRA)
  49427. WOLFSSL_CTX* ctx = NULL;
  49428. AssertIntEQ( SSL_CTX_set_ecdh_auto(NULL,0),1);
  49429. AssertIntEQ( SSL_CTX_set_ecdh_auto(NULL,1),1);
  49430. AssertIntEQ( SSL_CTX_set_ecdh_auto(ctx,0),1);
  49431. AssertIntEQ( SSL_CTX_set_ecdh_auto(ctx,1),1);
  49432. res = TEST_RES_CHECK(1);
  49433. #endif /* OPENSSL_EXTRA */
  49434. return res;
  49435. }
  49436. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  49437. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  49438. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  49439. {
  49440. callback_functions* callbacks = NULL;
  49441. WOLFSSL_CTX* ctx = NULL;
  49442. WOLFSSL* ssl = NULL;
  49443. SOCKET_T sfd = 0;
  49444. SOCKET_T cfd = 0;
  49445. word16 port;
  49446. char msg[] = "I hear you fa shizzle!";
  49447. int len = (int) XSTRLEN(msg);
  49448. char input[1024];
  49449. int ret, err;
  49450. if (!args)
  49451. return 0;
  49452. ((func_args*)args)->return_code = TEST_FAIL;
  49453. callbacks = ((func_args*)args)->callbacks;
  49454. ctx = wolfSSL_CTX_new(callbacks->method());
  49455. #if defined(USE_WINDOWS_API)
  49456. port = ((func_args*)args)->signal->port;
  49457. #else
  49458. /* Let tcp_listen assign port */
  49459. port = 0;
  49460. #endif
  49461. #ifdef WOLFSSL_TIRTOS
  49462. fdOpenSession(Task_self());
  49463. #endif
  49464. AssertIntEQ(WOLFSSL_SUCCESS,
  49465. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  49466. AssertIntEQ(WOLFSSL_SUCCESS,
  49467. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  49468. WOLFSSL_FILETYPE_PEM));
  49469. AssertIntEQ(WOLFSSL_SUCCESS,
  49470. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  49471. WOLFSSL_FILETYPE_PEM));
  49472. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  49473. AssertIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  49474. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  49475. #elif !defined(NO_DH)
  49476. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  49477. #endif
  49478. if (callbacks->ctx_ready)
  49479. callbacks->ctx_ready(ctx);
  49480. ssl = wolfSSL_new(ctx);
  49481. AssertNotNull(ssl);
  49482. /* listen and accept */
  49483. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  49484. CloseSocket(sfd);
  49485. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  49486. if (callbacks->ssl_ready)
  49487. callbacks->ssl_ready(ssl);
  49488. do {
  49489. err = 0; /* Reset error */
  49490. ret = wolfSSL_accept(ssl);
  49491. if (ret != WOLFSSL_SUCCESS) {
  49492. err = wolfSSL_get_error(ssl, 0);
  49493. }
  49494. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  49495. if (ret != WOLFSSL_SUCCESS) {
  49496. wolfSSL_free(ssl);
  49497. wolfSSL_CTX_free(ctx);
  49498. CloseSocket(cfd);
  49499. ((func_args*)args)->return_code = TEST_FAIL;
  49500. return 0;
  49501. }
  49502. /* read and write data */
  49503. XMEMSET( input, 0, sizeof(input));
  49504. while (1) {
  49505. ret = wolfSSL_read(ssl, input, sizeof(input));
  49506. if (ret > 0) {
  49507. break;
  49508. }
  49509. else {
  49510. err = wolfSSL_get_error(ssl,ret);
  49511. if (err == WOLFSSL_ERROR_WANT_READ) {
  49512. continue;
  49513. }
  49514. break;
  49515. }
  49516. }
  49517. if (err == WOLFSSL_ERROR_ZERO_RETURN) {
  49518. do {
  49519. ret = wolfSSL_write(ssl, msg, len);
  49520. if (ret > 0) {
  49521. break;
  49522. }
  49523. } while (ret < 0);
  49524. }
  49525. /* bidirectional shutdown */
  49526. while (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  49527. continue;
  49528. }
  49529. /* wait for the peer to disconnect the tcp connection */
  49530. do {
  49531. ret = wolfSSL_read(ssl, input, sizeof(input));
  49532. err = wolfSSL_get_error(ssl, ret);
  49533. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  49534. /* detect TCP disconnect */
  49535. AssertIntLE(ret,WOLFSSL_FAILURE);
  49536. AssertIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  49537. ((func_args*)args)->return_code = TEST_SUCCESS;
  49538. wolfSSL_free(ssl);
  49539. wolfSSL_CTX_free(ctx);
  49540. CloseSocket(cfd);
  49541. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  49542. wc_ecc_fp_free(); /* free per thread cache */
  49543. #endif
  49544. return 0;
  49545. }
  49546. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  49547. {
  49548. callback_functions* callbacks = NULL;
  49549. WOLFSSL_CTX* ctx = NULL;
  49550. WOLFSSL* ssl = NULL;
  49551. SOCKET_T sfd = 0;
  49552. char msg[] = "hello wolfssl server!";
  49553. int len = (int) XSTRLEN(msg);
  49554. char input[1024];
  49555. int idx;
  49556. int ret, err;
  49557. if (!args)
  49558. return 0;
  49559. ((func_args*)args)->return_code = TEST_FAIL;
  49560. callbacks = ((func_args*)args)->callbacks;
  49561. ctx = wolfSSL_CTX_new(callbacks->method());
  49562. #ifdef WOLFSSL_TIRTOS
  49563. fdOpenSession(Task_self());
  49564. #endif
  49565. AssertIntEQ(WOLFSSL_SUCCESS,
  49566. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  49567. AssertIntEQ(WOLFSSL_SUCCESS,
  49568. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  49569. WOLFSSL_FILETYPE_PEM));
  49570. AssertIntEQ(WOLFSSL_SUCCESS,
  49571. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  49572. WOLFSSL_FILETYPE_PEM));
  49573. AssertNotNull((ssl = wolfSSL_new(ctx)));
  49574. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  49575. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  49576. do {
  49577. err = 0; /* Reset error */
  49578. ret = wolfSSL_connect(ssl);
  49579. if (ret != WOLFSSL_SUCCESS) {
  49580. err = wolfSSL_get_error(ssl, 0);
  49581. }
  49582. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  49583. AssertIntGE(wolfSSL_write(ssl, msg, len), 0);
  49584. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  49585. input[idx] = 0;
  49586. }
  49587. ret = wolfSSL_shutdown(ssl);
  49588. if ( ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  49589. ret = wolfSSL_shutdown(ssl);
  49590. }
  49591. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  49592. ((func_args*)args)->return_code = TEST_SUCCESS;
  49593. wolfSSL_free(ssl);
  49594. wolfSSL_CTX_free(ctx);
  49595. CloseSocket(sfd);
  49596. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  49597. wc_ecc_fp_free(); /* free per thread cache */
  49598. #endif
  49599. return 0;
  49600. }
  49601. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  49602. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  49603. /* This test is to check wolfSSL_read behaves as same as
  49604. * openSSL when it is called after SSL_shutdown completes.
  49605. */
  49606. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  49607. {
  49608. int res = TEST_SKIPPED;
  49609. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  49610. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  49611. tcp_ready ready;
  49612. func_args client_args;
  49613. func_args server_args;
  49614. THREAD_TYPE serverThread;
  49615. THREAD_TYPE clientThread;
  49616. callback_functions server_cbf;
  49617. callback_functions client_cbf;
  49618. #ifdef WOLFSSL_TIRTOS
  49619. fdOpenSession(Task_self());
  49620. #endif
  49621. StartTCP();
  49622. InitTcpReady(&ready);
  49623. #if defined(USE_WINDOWS_API)
  49624. /* use RNG to get random port if using windows */
  49625. ready.port = GetRandomPort();
  49626. #endif
  49627. XMEMSET(&client_args, 0, sizeof(func_args));
  49628. XMEMSET(&server_args, 0, sizeof(func_args));
  49629. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  49630. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  49631. server_cbf.method = wolfTLSv1_2_server_method;
  49632. client_cbf.method = wolfTLSv1_2_client_method;
  49633. server_args.callbacks = &server_cbf;
  49634. client_args.callbacks = &client_cbf;
  49635. server_args.signal = &ready;
  49636. client_args.signal = &ready;
  49637. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  49638. wait_tcp_ready(&server_args);
  49639. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  49640. join_thread(clientThread);
  49641. join_thread(serverThread);
  49642. AssertTrue(client_args.return_code);
  49643. AssertTrue(server_args.return_code);
  49644. FreeTcpReady(&ready);
  49645. res = TEST_RES_CHECK(1);
  49646. #endif
  49647. return res;
  49648. }
  49649. static int test_wolfSSL_CTX_get_min_proto_version(void)
  49650. {
  49651. int res = TEST_SKIPPED;
  49652. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  49653. WOLFSSL_CTX *ctx;
  49654. (void)ctx;
  49655. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  49656. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION), WOLFSSL_SUCCESS);
  49657. #ifdef WOLFSSL_ALLOW_SSLV3
  49658. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  49659. #else
  49660. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  49661. #endif
  49662. wolfSSL_CTX_free(ctx);
  49663. #ifdef WOLFSSL_ALLOW_TLSV10
  49664. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  49665. #else
  49666. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  49667. #endif
  49668. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION), WOLFSSL_SUCCESS);
  49669. #ifdef WOLFSSL_ALLOW_TLSV10
  49670. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  49671. #else
  49672. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  49673. #endif
  49674. wolfSSL_CTX_free(ctx);
  49675. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  49676. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION), WOLFSSL_SUCCESS);
  49677. #ifndef NO_OLD_TLS
  49678. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  49679. #else
  49680. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  49681. #endif
  49682. wolfSSL_CTX_free(ctx);
  49683. #ifndef WOLFSSL_NO_TLS12
  49684. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  49685. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION), WOLFSSL_SUCCESS);
  49686. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  49687. wolfSSL_CTX_free(ctx);
  49688. #endif
  49689. #ifdef WOLFSSL_TLS13
  49690. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  49691. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION), WOLFSSL_SUCCESS);
  49692. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  49693. wolfSSL_CTX_free(ctx);
  49694. #endif
  49695. res = TEST_RES_CHECK(1);
  49696. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  49697. return res;
  49698. }
  49699. static int test_wolfSSL_security_level(void)
  49700. {
  49701. int res = TEST_SKIPPED;
  49702. #if defined(OPENSSL_EXTRA)
  49703. SSL_CTX *ctx;
  49704. #ifdef WOLFSSL_TLS13
  49705. #ifdef NO_WOLFSSL_SERVER
  49706. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  49707. #else
  49708. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  49709. #endif
  49710. SSL_CTX_set_security_level(ctx, 1);
  49711. AssertTrue(1);
  49712. AssertIntEQ(SSL_CTX_get_security_level(ctx), 0);
  49713. SSL_CTX_free(ctx);
  49714. #else
  49715. (void)ctx;
  49716. #endif
  49717. res = TEST_RES_CHECK(1);
  49718. #endif
  49719. return res;
  49720. }
  49721. static int test_wolfSSL_SSL_in_init(void)
  49722. {
  49723. int res = TEST_SKIPPED;
  49724. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  49725. SSL_CTX* ctx;
  49726. SSL* ssl;
  49727. const char* testCertFile;
  49728. const char* testKeyFile;
  49729. #ifdef WOLFSSL_TLS13
  49730. #ifdef NO_WOLFSSL_SERVER
  49731. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  49732. #else
  49733. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  49734. #endif
  49735. #ifndef NO_RSA
  49736. testCertFile = svrCertFile;
  49737. testKeyFile = svrKeyFile;
  49738. #elif defined(HAVE_ECC)
  49739. testCertFile = eccCertFile;
  49740. testKeyFile = eccKeyFile;
  49741. #else
  49742. testCertFile = NULL;
  49743. testKeyFile = NULL;
  49744. #endif
  49745. if (testCertFile != NULL && testKeyFile != NULL) {
  49746. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  49747. SSL_FILETYPE_PEM));
  49748. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  49749. SSL_FILETYPE_PEM));
  49750. }
  49751. ssl = SSL_new(ctx);
  49752. AssertNotNull(ssl);
  49753. AssertIntEQ(SSL_in_init(ssl), 1);
  49754. SSL_CTX_free(ctx);
  49755. SSL_free(ssl);
  49756. #else
  49757. (void)ctx;
  49758. (void)ssl;
  49759. (void)testCertFile;
  49760. (void)testKeyFile;
  49761. #endif
  49762. res = TEST_RES_CHECK(1);
  49763. #endif
  49764. return res;
  49765. }
  49766. static int test_wolfSSL_EC_curve(void)
  49767. {
  49768. int res = TEST_SKIPPED;
  49769. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  49770. int nid = NID_secp160k1;
  49771. const char* nid_name;
  49772. AssertNotNull(nid_name = EC_curve_nid2nist(nid));
  49773. AssertIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  49774. AssertIntEQ(EC_curve_nist2nid(nid_name), nid);
  49775. res = TEST_RES_CHECK(1);
  49776. #endif
  49777. return res;
  49778. }
  49779. static int test_wolfSSL_CTX_set_timeout(void)
  49780. {
  49781. int res = TEST_SKIPPED;
  49782. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  49783. int timeout;
  49784. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  49785. (void)timeout;
  49786. AssertNotNull(ctx);
  49787. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  49788. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  49789. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  49790. */
  49791. AssertIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  49792. /* giving 0 as timeout value sets default timeout */
  49793. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  49794. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  49795. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  49796. #else
  49797. AssertIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  49798. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  49799. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  49800. #endif
  49801. wolfSSL_CTX_free(ctx);
  49802. res = TEST_RES_CHECK(1);
  49803. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  49804. return res;
  49805. }
  49806. static int test_wolfSSL_OpenSSL_version(void)
  49807. {
  49808. int res = TEST_SKIPPED;
  49809. #if defined(OPENSSL_EXTRA)
  49810. const char* ver;
  49811. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  49812. AssertNotNull(ver = OpenSSL_version(0));
  49813. #else
  49814. AssertNotNull(ver = OpenSSL_version());
  49815. #endif
  49816. AssertIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  49817. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  49818. res = TEST_RES_CHECK(1);
  49819. #endif
  49820. return res;
  49821. }
  49822. static int test_CONF_CTX_CMDLINE(void)
  49823. {
  49824. int res = TEST_SKIPPED;
  49825. #if defined(OPENSSL_ALL)
  49826. SSL_CTX* ctx = NULL;
  49827. SSL_CONF_CTX* cctx = NULL;
  49828. AssertNotNull(cctx = SSL_CONF_CTX_new());
  49829. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  49830. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  49831. AssertTrue(1);
  49832. /* set flags */
  49833. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  49834. WOLFSSL_CONF_FLAG_CMDLINE);
  49835. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  49836. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  49837. /* cmd invalid command */
  49838. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  49839. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  49840. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  49841. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  49842. AssertIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  49843. /* cmd Certificate and Private Key*/
  49844. {
  49845. #if !defined(NO_CERTS) && !defined(NO_RSA)
  49846. const char* ourCert = svrCertFile;
  49847. const char* ourKey = svrKeyFile;
  49848. AssertIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  49849. AssertIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert),
  49850. WOLFSSL_SUCCESS);
  49851. AssertIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  49852. AssertIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  49853. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  49854. #endif
  49855. }
  49856. /* cmd curves */
  49857. {
  49858. #if defined(HAVE_ECC)
  49859. const char* curve = "secp256r1";
  49860. AssertIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  49861. AssertIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  49862. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  49863. #endif
  49864. }
  49865. /* cmd CipherString */
  49866. {
  49867. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  49868. AssertIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  49869. AssertIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  49870. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  49871. }
  49872. /* cmd DH parameter */
  49873. {
  49874. #if !defined(NO_DH) && !defined(NO_BIO)
  49875. const char* ourdhcert = "./certs/dh2048.pem";
  49876. AssertIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL),
  49877. -3);
  49878. AssertIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert),
  49879. WOLFSSL_SUCCESS);
  49880. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  49881. #endif
  49882. }
  49883. SSL_CTX_free(ctx);
  49884. SSL_CONF_CTX_free(cctx);
  49885. res = TEST_RES_CHECK(1);
  49886. #endif /* OPENSSL_EXTRA */
  49887. return res;
  49888. }
  49889. static int test_CONF_CTX_FILE(void)
  49890. {
  49891. int res = TEST_SKIPPED;
  49892. #if defined(OPENSSL_ALL)
  49893. SSL_CTX* ctx = NULL;
  49894. SSL_CONF_CTX* cctx = NULL;
  49895. AssertNotNull(cctx = SSL_CONF_CTX_new());
  49896. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  49897. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  49898. AssertTrue(1);
  49899. /* set flags */
  49900. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  49901. WOLFSSL_CONF_FLAG_FILE);
  49902. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  49903. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  49904. /* sanity check */
  49905. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  49906. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  49907. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  49908. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  49909. AssertIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  49910. /* cmd Certificate and Private Key*/
  49911. {
  49912. #if !defined(NO_CERTS) && !defined(NO_RSA)
  49913. const char* ourCert = svrCertFile;
  49914. const char* ourKey = svrKeyFile;
  49915. AssertIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  49916. AssertIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  49917. AssertIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  49918. WOLFSSL_SUCCESS);
  49919. AssertIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  49920. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  49921. #endif
  49922. }
  49923. /* cmd curves */
  49924. {
  49925. #if defined(HAVE_ECC)
  49926. const char* curve = "secp256r1";
  49927. AssertIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  49928. AssertIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  49929. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  49930. #endif
  49931. }
  49932. /* cmd CipherString */
  49933. {
  49934. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  49935. AssertIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  49936. AssertIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher), WOLFSSL_SUCCESS);
  49937. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  49938. }
  49939. /* cmd DH parameter */
  49940. {
  49941. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  49942. const char* ourdhcert = "./certs/dh3072.pem";
  49943. AssertIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  49944. AssertIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  49945. WOLFSSL_SUCCESS);
  49946. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  49947. #endif
  49948. }
  49949. SSL_CTX_free(ctx);
  49950. SSL_CONF_CTX_free(cctx);
  49951. res = TEST_RES_CHECK(1);
  49952. #endif /* OPENSSL_EXTRA */
  49953. return res;
  49954. }
  49955. static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
  49956. {
  49957. int res = TEST_SKIPPED;
  49958. #ifdef HAVE_EX_DATA
  49959. int idx1, idx2;
  49960. /* test for unsupported class index */
  49961. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  49962. 0,NULL, NULL, NULL, NULL ), -1);
  49963. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  49964. 0,NULL, NULL, NULL, NULL ), -1);
  49965. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  49966. 0,NULL, NULL, NULL, NULL ), -1);
  49967. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  49968. 0,NULL, NULL, NULL, NULL ), -1);
  49969. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  49970. 0,NULL, NULL, NULL, NULL ), -1);
  49971. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  49972. 0,NULL, NULL, NULL, NULL ), -1);
  49973. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  49974. 0,NULL, NULL, NULL, NULL ), -1);
  49975. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  49976. 0,NULL, NULL, NULL, NULL ), -1);
  49977. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  49978. 0,NULL, NULL, NULL, NULL ), -1);
  49979. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  49980. 0,NULL, NULL, NULL, NULL ), -1);
  49981. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  49982. 0,NULL, NULL, NULL, NULL ), -1);
  49983. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  49984. 0,NULL, NULL, NULL, NULL ), -1);
  49985. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20, 0,NULL, NULL, NULL, NULL ), -1);
  49986. /* test for supported class index */
  49987. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  49988. 0,NULL, NULL, NULL, NULL );
  49989. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  49990. 0,NULL, NULL, NULL, NULL );
  49991. AssertIntNE(idx1, -1);
  49992. AssertIntNE(idx2, -1);
  49993. AssertIntNE(idx1, idx2);
  49994. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  49995. 0,NULL, NULL, NULL, NULL );
  49996. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  49997. 0,NULL, NULL, NULL, NULL );
  49998. AssertIntNE(idx1, -1);
  49999. AssertIntNE(idx2, -1);
  50000. AssertIntNE(idx1, idx2);
  50001. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  50002. 0,NULL, NULL, NULL, NULL );
  50003. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  50004. 0,NULL, NULL, NULL, NULL );
  50005. AssertIntNE(idx1, -1);
  50006. AssertIntNE(idx2, -1);
  50007. AssertIntNE(idx1, idx2);
  50008. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  50009. 0,NULL, NULL, NULL, NULL );
  50010. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  50011. 0,NULL, NULL, NULL, NULL );
  50012. AssertIntNE(idx1, -1);
  50013. AssertIntNE(idx2, -1);
  50014. AssertIntNE(idx1, idx2);
  50015. res = TEST_RES_CHECK(1);
  50016. #endif /* HAVE_EX_DATA */
  50017. return res;
  50018. }
  50019. static int test_wolfSSL_set_psk_use_session_callback(void)
  50020. {
  50021. int res = TEST_SKIPPED;
  50022. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  50023. SSL_CTX* ctx;
  50024. SSL* ssl;
  50025. const char* testCertFile;
  50026. const char* testKeyFile;
  50027. #ifdef WOLFSSL_TLS13
  50028. #ifdef NO_WOLFSSL_SERVER
  50029. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  50030. #else
  50031. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  50032. #endif
  50033. #ifndef NO_RSA
  50034. testCertFile = svrCertFile;
  50035. testKeyFile = svrKeyFile;
  50036. #elif defined(HAVE_ECC)
  50037. testCertFile = eccCertFile;
  50038. testKeyFile = eccKeyFile;
  50039. #else
  50040. testCertFile = NULL;
  50041. testKeyFile = NULL;
  50042. #endif
  50043. if (testCertFile != NULL && testKeyFile != NULL) {
  50044. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  50045. SSL_FILETYPE_PEM));
  50046. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  50047. SSL_FILETYPE_PEM));
  50048. }
  50049. ssl = SSL_new(ctx);
  50050. AssertNotNull(ssl);
  50051. SSL_set_psk_use_session_callback(ssl,
  50052. my_psk_use_session_cb);
  50053. AssertTrue(1);
  50054. SSL_CTX_free(ctx);
  50055. SSL_free(ssl);
  50056. #else
  50057. (void)ctx;
  50058. (void)ssl;
  50059. (void)testCertFile;
  50060. (void)testKeyFile;
  50061. #endif
  50062. res = TEST_RES_CHECK(1);
  50063. #endif
  50064. return res;
  50065. }
  50066. static int test_wolfSSL_ERR_strings(void)
  50067. {
  50068. int res = TEST_SKIPPED;
  50069. #if !defined(NO_ERROR_STRINGS)
  50070. const char* err1 = "unsupported cipher suite";
  50071. const char* err2 = "wolfSSL PEM routines";
  50072. const char* err = NULL;
  50073. (void)err;
  50074. (void)err1;
  50075. (void)err2;
  50076. #if defined(OPENSSL_EXTRA)
  50077. err = ERR_reason_error_string(UNSUPPORTED_SUITE);
  50078. AssertTrue(err != NULL);
  50079. AssertIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  50080. err = ERR_func_error_string(UNSUPPORTED_SUITE);
  50081. AssertTrue(err != NULL);
  50082. AssertIntEQ((*err == '\0'), 1);
  50083. err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD);
  50084. AssertTrue(err != NULL);
  50085. AssertIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  50086. #else
  50087. err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE);
  50088. AssertTrue(err != NULL);
  50089. AssertIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  50090. err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE);
  50091. AssertTrue(err != NULL);
  50092. AssertIntEQ((*err == '\0'), 1);
  50093. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  50094. err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2);
  50095. AssertTrue(err != NULL);
  50096. AssertIntEQ((*err == '\0'), 1);
  50097. #endif
  50098. res = TEST_RES_CHECK(1);
  50099. #endif
  50100. return res;
  50101. }
  50102. static int test_wolfSSL_EVP_shake128(void)
  50103. {
  50104. int res = TEST_SKIPPED;
  50105. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  50106. defined(WOLFSSL_SHAKE128)
  50107. const EVP_MD* md = NULL;
  50108. md = EVP_shake128();
  50109. AssertTrue(md != NULL);
  50110. AssertIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  50111. res = TEST_RES_CHECK(1);
  50112. #endif
  50113. return res;
  50114. }
  50115. static int test_wolfSSL_EVP_shake256(void)
  50116. {
  50117. int res = TEST_SKIPPED;
  50118. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  50119. defined(WOLFSSL_SHAKE256)
  50120. const EVP_MD* md = NULL;
  50121. md = EVP_shake256();
  50122. AssertTrue(md != NULL);
  50123. AssertIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  50124. res = TEST_RES_CHECK(1);
  50125. #endif
  50126. return res;
  50127. }
  50128. static int test_EVP_blake2(void)
  50129. {
  50130. int res = TEST_SKIPPED;
  50131. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  50132. const EVP_MD* md = NULL;
  50133. (void)md;
  50134. #if defined(HAVE_BLAKE2)
  50135. md = EVP_blake2b512();
  50136. AssertTrue(md != NULL);
  50137. AssertIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  50138. #endif
  50139. #if defined(HAVE_BLAKE2S)
  50140. md = EVP_blake2s256();
  50141. AssertTrue(md != NULL);
  50142. AssertIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  50143. #endif
  50144. res = TEST_RES_CHECK(1);
  50145. #endif
  50146. return res;
  50147. }
  50148. #if defined(OPENSSL_EXTRA)
  50149. static void list_md_fn(const EVP_MD* m, const char* from,
  50150. const char* to, void* arg)
  50151. {
  50152. const char* mn;
  50153. BIO *bio;
  50154. (void) from;
  50155. (void) to;
  50156. (void) arg;
  50157. (void) mn;
  50158. (void) bio;
  50159. if (!m) {
  50160. /* alias */
  50161. AssertNull(m);
  50162. AssertNotNull(to);
  50163. }
  50164. else {
  50165. AssertNotNull(m);
  50166. AssertNull(to);
  50167. }
  50168. AssertNotNull(from);
  50169. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  50170. mn = EVP_get_digestbyname(from);
  50171. /* print to stderr */
  50172. AssertNotNull(arg);
  50173. bio = BIO_new(BIO_s_file());
  50174. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  50175. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  50176. BIO_free(bio);
  50177. #endif
  50178. }
  50179. #endif
  50180. static int test_EVP_MD_do_all(void)
  50181. {
  50182. int res = TEST_SKIPPED;
  50183. #if defined(OPENSSL_EXTRA)
  50184. EVP_MD_do_all(NULL, stderr);
  50185. /* to confirm previous call gives no harm */
  50186. AssertTrue(1);
  50187. EVP_MD_do_all(list_md_fn, stderr);
  50188. /* to confirm previous call gives no harm */
  50189. AssertTrue(1);
  50190. res = TEST_RES_CHECK(1);
  50191. #endif
  50192. return res;
  50193. }
  50194. #if defined(OPENSSL_EXTRA)
  50195. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  50196. {
  50197. (void)arg;
  50198. (void)nm;
  50199. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  50200. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  50201. /* print to stderr */
  50202. AssertNotNull(arg);
  50203. bio = BIO_new(BIO_s_file());
  50204. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  50205. BIO_printf(bio, "%s\n", mn);
  50206. BIO_free(bio);
  50207. #endif
  50208. }
  50209. #endif
  50210. static int test_OBJ_NAME_do_all(void)
  50211. {
  50212. int res = TEST_SKIPPED;
  50213. #if defined(OPENSSL_EXTRA)
  50214. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  50215. /* to confirm previous call gives no harm */
  50216. AssertTrue(1);
  50217. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stderr);
  50218. /* to confirm previous call gives no harm */
  50219. AssertTrue(1);
  50220. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stderr);
  50221. AssertTrue(1);
  50222. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stderr);
  50223. AssertTrue(1);
  50224. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stderr);
  50225. AssertTrue(1);
  50226. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stderr);
  50227. AssertTrue(1);
  50228. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stderr);
  50229. AssertTrue(1);
  50230. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stderr);
  50231. AssertTrue(1);
  50232. OBJ_NAME_do_all(-1, obj_name_t, stderr);
  50233. AssertTrue(1);
  50234. res = TEST_RES_CHECK(1);
  50235. #endif
  50236. return res;
  50237. }
  50238. static int test_SSL_CIPHER_get_xxx(void)
  50239. {
  50240. int res = TEST_SKIPPED;
  50241. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  50242. !defined(NO_FILESYSTEM)
  50243. const SSL_CIPHER* cipher = NULL;
  50244. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  50245. int i, numCiphers = 0;
  50246. SSL_CTX* ctx = NULL;
  50247. SSL* ssl = NULL;
  50248. const char* testCertFile;
  50249. const char* testKeyFile;
  50250. char buf[256] = {0};
  50251. const char* cipher_id = NULL;
  50252. int expect_nid1 = NID_undef;
  50253. int expect_nid2 = NID_undef;
  50254. int expect_nid3 = NID_undef;
  50255. int expect_nid4 = NID_undef;
  50256. int expect_nid5 = 0;
  50257. const char* cipher_id2 = NULL;
  50258. int expect_nid21 = NID_undef;
  50259. int expect_nid22 = NID_undef;
  50260. int expect_nid23 = NID_undef;
  50261. int expect_nid24 = NID_undef;
  50262. int expect_nid25 = 0;
  50263. (void)cipher;
  50264. (void)supportedCiphers;
  50265. (void)i;
  50266. (void)numCiphers;
  50267. (void)ctx;
  50268. (void)ssl;
  50269. (void)testCertFile;
  50270. (void)testKeyFile;
  50271. #if defined(WOLFSSL_TLS13)
  50272. cipher_id = "TLS13-AES128-GCM-SHA256";
  50273. expect_nid1 = NID_auth_rsa;
  50274. expect_nid2 = NID_aes_128_gcm;
  50275. expect_nid3 = NID_sha256;
  50276. expect_nid4 = NID_kx_any;
  50277. expect_nid5 = 1;
  50278. #if !defined(WOLFSSL_NO_TLS12)
  50279. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  50280. expect_nid21 = NID_auth_rsa;
  50281. expect_nid22 = NID_aes_256_gcm;
  50282. expect_nid23 = NID_sha384;
  50283. expect_nid24 = NID_kx_ecdhe;
  50284. expect_nid25 = 1;
  50285. #endif
  50286. #endif
  50287. #ifdef NO_WOLFSSL_SERVER
  50288. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  50289. #else
  50290. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  50291. #endif
  50292. if (cipher_id) {
  50293. #ifndef NO_RSA
  50294. testCertFile = svrCertFile;
  50295. testKeyFile = svrKeyFile;
  50296. #elif defined(HAVE_ECC)
  50297. testCertFile = eccCertFile;
  50298. testKeyFile = eccKeyFile;
  50299. #else
  50300. testCertFile = NULL;
  50301. testKeyFile = NULL;
  50302. #endif
  50303. if (testCertFile != NULL && testKeyFile != NULL) {
  50304. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  50305. SSL_FILETYPE_PEM));
  50306. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  50307. SSL_FILETYPE_PEM));
  50308. }
  50309. ssl = SSL_new(ctx);
  50310. AssertNotNull(ssl);
  50311. AssertIntEQ(SSL_in_init(ssl), 1);
  50312. supportedCiphers = SSL_get_ciphers(ssl);
  50313. numCiphers = sk_num(supportedCiphers);
  50314. for (i = 0; i < numCiphers; ++i) {
  50315. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  50316. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  50317. }
  50318. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  50319. break;
  50320. }
  50321. }
  50322. /* test case for */
  50323. if (i != numCiphers) {
  50324. AssertIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  50325. AssertIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  50326. AssertIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  50327. AssertIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  50328. AssertIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  50329. }
  50330. if (cipher_id2) {
  50331. for (i = 0; i < numCiphers; ++i) {
  50332. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  50333. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  50334. }
  50335. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  50336. break;
  50337. }
  50338. }
  50339. /* test case for */
  50340. if (i != numCiphers) {
  50341. AssertIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  50342. AssertIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  50343. AssertIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  50344. AssertIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  50345. AssertIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  50346. }
  50347. }
  50348. }
  50349. if (ctx)
  50350. SSL_CTX_free(ctx);
  50351. if (ssl)
  50352. SSL_free(ssl);
  50353. res = TEST_RES_CHECK(1);
  50354. #endif
  50355. return res;
  50356. }
  50357. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  50358. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  50359. int* keyFormat)
  50360. {
  50361. int ret;
  50362. byte* key_buf = NULL;
  50363. size_t key_sz = 0;
  50364. EncryptedInfo encInfo;
  50365. XMEMSET(&encInfo, 0, sizeof(encInfo));
  50366. ret = load_file(privKeyFile, &key_buf, &key_sz);
  50367. if (ret == 0) {
  50368. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  50369. NULL, &encInfo, keyFormat);
  50370. }
  50371. if (key_buf != NULL) {
  50372. free(key_buf); key_buf = NULL;
  50373. }
  50374. (void)encInfo; /* not used in this test */
  50375. #ifdef DEBUG_WOLFSSL
  50376. fprintf(stderr, "%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  50377. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  50378. (*pDer)->length);
  50379. #endif
  50380. return ret;
  50381. }
  50382. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  50383. {
  50384. int ret = CRYPTOCB_UNAVAILABLE;
  50385. const char* privKeyFile = (const char*)ctx;
  50386. DerBuffer* pDer = NULL;
  50387. int keyFormat = 0;
  50388. if (info->algo_type == WC_ALGO_TYPE_PK) {
  50389. #ifdef DEBUG_WOLFSSL
  50390. fprintf(stderr, "test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  50391. #endif
  50392. #ifndef NO_RSA
  50393. if (info->pk.type == WC_PK_TYPE_RSA) {
  50394. switch (info->pk.rsa.type) {
  50395. case RSA_PUBLIC_ENCRYPT:
  50396. case RSA_PUBLIC_DECRYPT:
  50397. /* perform software based RSA public op */
  50398. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  50399. break;
  50400. case RSA_PRIVATE_ENCRYPT:
  50401. case RSA_PRIVATE_DECRYPT:
  50402. {
  50403. RsaKey key;
  50404. /* perform software based RSA private op */
  50405. #ifdef DEBUG_WOLFSSL
  50406. fprintf(stderr, "test_CryptoCb_Func: RSA Priv\n");
  50407. #endif
  50408. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  50409. &keyFormat);
  50410. if (ret != 0) {
  50411. return ret;
  50412. }
  50413. ret = wc_InitRsaKey(&key, HEAP_HINT);
  50414. if (ret == 0) {
  50415. word32 keyIdx = 0;
  50416. /* load RSA private key and perform private transform */
  50417. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  50418. &key, pDer->length);
  50419. if (ret == 0) {
  50420. ret = wc_RsaFunction(
  50421. info->pk.rsa.in, info->pk.rsa.inLen,
  50422. info->pk.rsa.out, info->pk.rsa.outLen,
  50423. info->pk.rsa.type, &key, info->pk.rsa.rng);
  50424. }
  50425. else {
  50426. /* if decode fails, then fall-back to software based crypto */
  50427. fprintf(stderr, "test_CryptoCb_Func: RSA private "
  50428. "key decode failed %d, falling back to "
  50429. "software\n", ret);
  50430. ret = CRYPTOCB_UNAVAILABLE;
  50431. }
  50432. wc_FreeRsaKey(&key);
  50433. }
  50434. wc_FreeDer(&pDer); pDer = NULL;
  50435. break;
  50436. }
  50437. }
  50438. #ifdef DEBUG_WOLFSSL
  50439. fprintf(stderr, "test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  50440. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  50441. #endif
  50442. }
  50443. #endif /* !NO_RSA */
  50444. #ifdef HAVE_ECC
  50445. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  50446. /* mark this key as ephemeral */
  50447. if (info->pk.eckg.key != NULL) {
  50448. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  50449. sizeof(info->pk.eckg.key->label));
  50450. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  50451. }
  50452. }
  50453. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  50454. ecc_key key;
  50455. /* perform software based ECC sign */
  50456. #ifdef DEBUG_WOLFSSL
  50457. fprintf(stderr, "test_CryptoCb_Func: ECC Sign\n");
  50458. #endif
  50459. if (info->pk.eccsign.key != NULL &&
  50460. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  50461. /* this is an empheral key */
  50462. #ifdef DEBUG_WOLFSSL
  50463. fprintf(stderr, "test_CryptoCb_Func: skipping signing op on "
  50464. "ephemeral key\n");
  50465. #endif
  50466. return CRYPTOCB_UNAVAILABLE;
  50467. }
  50468. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  50469. if (ret != 0) {
  50470. return ret;
  50471. }
  50472. ret = wc_ecc_init(&key);
  50473. if (ret == 0) {
  50474. word32 keyIdx = 0;
  50475. /* load ECC private key and perform private transform */
  50476. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  50477. &key, pDer->length);
  50478. if (ret == 0) {
  50479. ret = wc_ecc_sign_hash(
  50480. info->pk.eccsign.in, info->pk.eccsign.inlen,
  50481. info->pk.eccsign.out, info->pk.eccsign.outlen,
  50482. info->pk.eccsign.rng, &key);
  50483. }
  50484. else {
  50485. /* if decode fails, then fall-back to software based crypto */
  50486. fprintf(stderr, "test_CryptoCb_Func: ECC private key "
  50487. "decode failed %d, falling back to software\n", ret);
  50488. ret = CRYPTOCB_UNAVAILABLE;
  50489. }
  50490. wc_ecc_free(&key);
  50491. }
  50492. wc_FreeDer(&pDer); pDer = NULL;
  50493. #ifdef DEBUG_WOLFSSL
  50494. fprintf(stderr, "test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  50495. ret, *info->pk.eccsign.outlen);
  50496. #endif
  50497. }
  50498. #endif /* HAVE_ECC */
  50499. #ifdef HAVE_ED25519
  50500. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  50501. ed25519_key key;
  50502. /* perform software based ED25519 sign */
  50503. #ifdef DEBUG_WOLFSSL
  50504. fprintf(stderr, "test_CryptoCb_Func: ED25519 Sign\n");
  50505. #endif
  50506. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  50507. if (ret != 0) {
  50508. return ret;
  50509. }
  50510. ret = wc_ed25519_init(&key);
  50511. if (ret == 0) {
  50512. word32 keyIdx = 0;
  50513. /* load ED25519 private key and perform private transform */
  50514. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  50515. &key, pDer->length);
  50516. if (ret == 0) {
  50517. /* calculate public key */
  50518. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  50519. if (ret == 0) {
  50520. key.pubKeySet = 1;
  50521. ret = wc_ed25519_sign_msg_ex(
  50522. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  50523. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  50524. &key, info->pk.ed25519sign.type,
  50525. info->pk.ed25519sign.context,
  50526. info->pk.ed25519sign.contextLen);
  50527. }
  50528. }
  50529. else {
  50530. /* if decode fails, then fall-back to software based crypto */
  50531. fprintf(stderr, "test_CryptoCb_Func: ED25519 private key "
  50532. "decode failed %d, falling back to software\n", ret);
  50533. ret = CRYPTOCB_UNAVAILABLE;
  50534. }
  50535. wc_ed25519_free(&key);
  50536. }
  50537. wc_FreeDer(&pDer); pDer = NULL;
  50538. #ifdef DEBUG_WOLFSSL
  50539. fprintf(stderr, "test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  50540. ret, *info->pk.ed25519sign.outLen);
  50541. #endif
  50542. }
  50543. #endif /* HAVE_ED25519 */
  50544. }
  50545. (void)thisDevId;
  50546. (void)keyFormat;
  50547. return ret;
  50548. }
  50549. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  50550. static void test_wc_CryptoCb_TLS(int tlsVer,
  50551. const char* cliCaPemFile, const char* cliCertPemFile,
  50552. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  50553. const char* svrCaPemFile, const char* svrCertPemFile,
  50554. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  50555. {
  50556. callback_functions client_cbf;
  50557. callback_functions server_cbf;
  50558. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  50559. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  50560. if (tlsVer == WOLFSSL_TLSV1_3) {
  50561. #ifdef WOLFSSL_TLS13
  50562. server_cbf.method = wolfTLSv1_3_server_method;
  50563. client_cbf.method = wolfTLSv1_3_client_method;
  50564. #endif
  50565. }
  50566. else if (tlsVer == WOLFSSL_TLSV1_2) {
  50567. #ifndef WOLFSSL_NO_TLS12
  50568. server_cbf.method = wolfTLSv1_2_server_method;
  50569. client_cbf.method = wolfTLSv1_2_client_method;
  50570. #endif
  50571. }
  50572. else if (tlsVer == WOLFSSL_TLSV1_1) {
  50573. #ifndef NO_OLD_TLS
  50574. server_cbf.method = wolfTLSv1_1_server_method;
  50575. client_cbf.method = wolfTLSv1_1_client_method;
  50576. #endif
  50577. }
  50578. else if (tlsVer == WOLFSSL_TLSV1) {
  50579. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  50580. server_cbf.method = wolfTLSv1_server_method;
  50581. client_cbf.method = wolfTLSv1_client_method;
  50582. #endif
  50583. }
  50584. else if (tlsVer == WOLFSSL_SSLV3) {
  50585. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  50586. defined(WOLFSSL_STATIC_RSA)
  50587. server_cbf.method = wolfSSLv3_server_method;
  50588. client_cbf.method = wolfSSLv3_client_method;
  50589. #endif
  50590. }
  50591. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  50592. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  50593. server_cbf.method = wolfDTLSv1_2_server_method;
  50594. client_cbf.method = wolfDTLSv1_2_client_method;
  50595. #endif
  50596. }
  50597. else if (tlsVer == WOLFSSL_DTLSV1) {
  50598. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  50599. server_cbf.method = wolfDTLSv1_server_method;
  50600. client_cbf.method = wolfDTLSv1_client_method;
  50601. #endif
  50602. }
  50603. if (server_cbf.method == NULL) {
  50604. /* not enabled */
  50605. return;
  50606. }
  50607. /* Setup the keys for the TLS test */
  50608. client_cbf.certPemFile = cliCertPemFile;
  50609. client_cbf.keyPemFile = cliPubKeyPemFile;
  50610. client_cbf.caPemFile = cliCaPemFile;
  50611. server_cbf.certPemFile = svrCertPemFile;
  50612. server_cbf.keyPemFile = svrPubKeyPemFile;
  50613. server_cbf.caPemFile = svrCaPemFile;
  50614. /* Setup a crypto callback with pointer to private key file for testing */
  50615. client_cbf.devId = 1;
  50616. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  50617. (void*)cliPrivKeyPemFile);
  50618. server_cbf.devId = 2;
  50619. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  50620. (void*)svrPrivKeyPemFile);
  50621. /* Perform TLS server and client test */
  50622. /* First test is at WOLFSSL_CTX level */
  50623. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  50624. /* Check for success */
  50625. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  50626. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  50627. /* Second test is a WOLFSSL object level */
  50628. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  50629. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  50630. /* Check for success */
  50631. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  50632. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  50633. /* Un register the devId's */
  50634. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  50635. client_cbf.devId = INVALID_DEVID;
  50636. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  50637. server_cbf.devId = INVALID_DEVID;
  50638. }
  50639. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  50640. static int test_wc_CryptoCb(void)
  50641. {
  50642. int res = TEST_SKIPPED;
  50643. #ifdef WOLF_CRYPTO_CB
  50644. /* TODO: Add crypto callback API tests */
  50645. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  50646. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  50647. int tlsVer;
  50648. #endif
  50649. #ifndef NO_RSA
  50650. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  50651. test_wc_CryptoCb_TLS(tlsVer,
  50652. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  50653. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile);
  50654. }
  50655. #endif
  50656. #ifdef HAVE_ECC
  50657. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  50658. test_wc_CryptoCb_TLS(tlsVer,
  50659. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  50660. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile);
  50661. }
  50662. #endif
  50663. #ifdef HAVE_ED25519
  50664. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  50665. if (tlsVer == WOLFSSL_DTLSV1) continue;
  50666. test_wc_CryptoCb_TLS(tlsVer,
  50667. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  50668. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile);
  50669. }
  50670. #endif
  50671. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  50672. res = TEST_RES_CHECK(1);
  50673. #endif /* WOLF_CRYPTO_CB */
  50674. return res;
  50675. }
  50676. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  50677. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  50678. static void test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  50679. const char* cliCaPemFile, const char* cliCertPemFile,
  50680. const char* cliPrivKeyPemFile,
  50681. const char* svrCaPemFile, const char* svrCertPemFile,
  50682. const char* svrPrivKeyPemFile,
  50683. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  50684. {
  50685. callback_functions client_cbf;
  50686. callback_functions server_cbf;
  50687. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  50688. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  50689. if (tlsVer == WOLFSSL_TLSV1_3) {
  50690. #ifdef WOLFSSL_TLS13
  50691. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  50692. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  50693. #endif
  50694. }
  50695. else if (tlsVer == WOLFSSL_TLSV1_2) {
  50696. #ifndef WOLFSSL_NO_TLS12
  50697. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  50698. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  50699. #endif
  50700. }
  50701. else if (tlsVer == WOLFSSL_TLSV1_1) {
  50702. #ifndef NO_OLD_TLS
  50703. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  50704. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  50705. #endif
  50706. }
  50707. else if (tlsVer == WOLFSSL_TLSV1) {
  50708. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  50709. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  50710. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  50711. #endif
  50712. }
  50713. else if (tlsVer == WOLFSSL_SSLV3) {
  50714. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  50715. defined(WOLFSSL_STATIC_RSA)
  50716. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  50717. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  50718. #endif
  50719. }
  50720. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  50721. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  50722. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  50723. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  50724. #endif
  50725. }
  50726. else if (tlsVer == WOLFSSL_DTLSV1) {
  50727. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  50728. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  50729. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  50730. #endif
  50731. }
  50732. if (server_cbf.method_ex == NULL) {
  50733. /* not enabled */
  50734. return;
  50735. }
  50736. /* Setup the keys for the TLS test */
  50737. client_cbf.certPemFile = cliCertPemFile;
  50738. client_cbf.keyPemFile = cliPrivKeyPemFile;
  50739. client_cbf.caPemFile = cliCaPemFile;
  50740. server_cbf.certPemFile = svrCertPemFile;
  50741. server_cbf.keyPemFile = svrPrivKeyPemFile;
  50742. server_cbf.caPemFile = svrCaPemFile;
  50743. client_cbf.mem = cliMem;
  50744. client_cbf.memSz = cliMemSz;
  50745. server_cbf.mem = svrMem;
  50746. server_cbf.memSz = svrMemSz;
  50747. client_cbf.devId = INVALID_DEVID;
  50748. server_cbf.devId = INVALID_DEVID;
  50749. /* Perform TLS server and client test */
  50750. /* First test is at WOLFSSL_CTX level */
  50751. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  50752. /* Check for success */
  50753. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  50754. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  50755. /* Second test is a WOLFSSL object level */
  50756. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  50757. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  50758. /* Check for success */
  50759. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  50760. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  50761. }
  50762. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  50763. #ifdef WOLFSSL_STATIC_MEMORY
  50764. #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) || \
  50765. defined(SESSION_CERTS)
  50766. #ifdef OPENSSL_EXTRA
  50767. #define TEST_TLS_STATIC_MEMSZ (400000)
  50768. #else
  50769. #define TEST_TLS_STATIC_MEMSZ (320000)
  50770. #endif
  50771. #else
  50772. #define TEST_TLS_STATIC_MEMSZ (80000)
  50773. #endif
  50774. static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  50775. {
  50776. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  50777. WOLFSSL_MEM_STATS mem_stats;
  50778. WOLFSSL_MEM_CONN_STATS ssl_stats;
  50779. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  50780. AssertIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  50781. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  50782. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  50783. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  50784. #endif
  50785. AssertNotNull((ssl1 = wolfSSL_new(ctx)));
  50786. AssertNotNull((ssl2 = wolfSSL_new(ctx)));
  50787. /* this should fail because kMaxCtxClients == 2 */
  50788. AssertNull((ssl3 = wolfSSL_new(ctx)));
  50789. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  50790. #ifdef DEBUG_WOLFSSL
  50791. wolfSSL_PrintStatsConn(&ssl_stats);
  50792. #endif
  50793. (void)ssl_stats;
  50794. }
  50795. /* display collected statistics */
  50796. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  50797. #ifdef DEBUG_WOLFSSL
  50798. wolfSSL_PrintStats(&mem_stats);
  50799. #endif
  50800. (void)mem_stats;
  50801. }
  50802. wolfSSL_free(ssl1);
  50803. wolfSSL_free(ssl2);
  50804. return TEST_RES_CHECK(1);
  50805. }
  50806. #endif /* WOLFSSL_STATIC_MEMORY */
  50807. static int test_wolfSSL_CTX_StaticMemory(void)
  50808. {
  50809. int res = TEST_SKIPPED;
  50810. #ifdef WOLFSSL_STATIC_MEMORY
  50811. wolfSSL_method_func method_func;
  50812. WOLFSSL_CTX* ctx;
  50813. const int kMaxCtxClients = 2;
  50814. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  50815. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  50816. int tlsVer;
  50817. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  50818. #endif
  50819. #endif
  50820. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  50821. #ifndef NO_WOLFSSL_SERVER
  50822. #ifndef WOLFSSL_NO_TLS12
  50823. method_func = wolfTLSv1_2_server_method_ex;
  50824. #else
  50825. method_func = wolfTLSv1_3_server_method_ex;
  50826. #endif
  50827. #else
  50828. #ifndef WOLFSSL_NO_TLS12
  50829. method_func = wolfTLSv1_2_client_method_ex;
  50830. #else
  50831. method_func = wolfTLSv1_3_client_method_ex;
  50832. #endif
  50833. #endif
  50834. /* Test creating CTX directly from static memory pool */
  50835. ctx = NULL;
  50836. AssertIntEQ(wolfSSL_CTX_load_static_memory(
  50837. &ctx, method_func, svrMem, sizeof(svrMem),
  50838. 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  50839. test_wolfSSL_CTX_StaticMemory_SSL(ctx);
  50840. wolfSSL_CTX_free(ctx);
  50841. ctx = NULL;
  50842. /* Test for heap allocated CTX, then assigning static pool to it */
  50843. AssertNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  50844. AssertIntEQ(wolfSSL_CTX_load_static_memory(&ctx,
  50845. NULL, svrMem, sizeof(svrMem),
  50846. 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  50847. test_wolfSSL_CTX_StaticMemory_SSL(ctx);
  50848. wolfSSL_CTX_free(ctx);
  50849. /* TLS Level Tests using static memory */
  50850. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  50851. #ifndef NO_RSA
  50852. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  50853. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  50854. svrCertFile, cliCertFile, cliKeyFile,
  50855. cliCertFile, svrCertFile, svrKeyFile,
  50856. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  50857. }
  50858. #endif
  50859. #ifdef HAVE_ECC
  50860. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  50861. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  50862. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  50863. cliEccCertFile, eccCertFile, eccKeyFile,
  50864. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  50865. }
  50866. #endif
  50867. #ifdef HAVE_ED25519
  50868. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  50869. if (tlsVer == WOLFSSL_DTLSV1) continue;
  50870. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  50871. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  50872. cliEdCertFile, edCertFile, edKeyFile,
  50873. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  50874. }
  50875. #endif
  50876. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  50877. res = TEST_RES_CHECK(1);
  50878. #endif
  50879. return res;
  50880. }
  50881. static int test_openssl_FIPS_drbg(void)
  50882. {
  50883. int res = TEST_SKIPPED;
  50884. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  50885. DRBG_CTX* dctx;
  50886. byte data1[32], data2[32], zeroData[32];
  50887. byte testSeed[16];
  50888. size_t dlen = sizeof(data1);
  50889. int i;
  50890. XMEMSET(data1, 0, dlen);
  50891. XMEMSET(data2, 0, dlen);
  50892. XMEMSET(zeroData, 0, sizeof(zeroData));
  50893. for (i=0; i<(int)sizeof(testSeed); i++) {
  50894. testSeed[i] = (byte)i;
  50895. }
  50896. AssertNotNull(dctx = FIPS_get_default_drbg());
  50897. AssertIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  50898. AssertIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  50899. WOLFSSL_SUCCESS);
  50900. AssertIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  50901. AssertIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  50902. WOLFSSL_SUCCESS);
  50903. AssertIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  50904. AssertIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  50905. WOLFSSL_SUCCESS);
  50906. AssertIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  50907. WOLFSSL_SUCCESS);
  50908. AssertIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  50909. AssertIntNE(XMEMCMP(data1, data2, dlen), 0);
  50910. AssertIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  50911. res = TEST_RES_CHECK(1);
  50912. #endif
  50913. return res;
  50914. }
  50915. static int test_wolfSSL_FIPS_mode(void)
  50916. {
  50917. int res = TEST_SKIPPED;
  50918. #if defined(OPENSSL_ALL)
  50919. #ifdef HAVE_FIPS
  50920. AssertIntEQ(wolfSSL_FIPS_mode(), 1);
  50921. AssertIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  50922. AssertIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  50923. #else
  50924. AssertIntEQ(wolfSSL_FIPS_mode(), 0);
  50925. AssertIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  50926. AssertIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  50927. #endif
  50928. res = TEST_RES_CHECK(1);
  50929. #endif
  50930. return res;
  50931. }
  50932. #ifdef WOLFSSL_DTLS
  50933. /* Prints out the current window */
  50934. static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
  50935. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  50936. int i;
  50937. for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
  50938. word32 b = w[i];
  50939. int j;
  50940. /* Prints out a 32 bit binary number in big endian order */
  50941. for (j = 0; j < 32; j++, b <<= 1) {
  50942. if (b & (((word32)1) << 31))
  50943. fprintf(stderr, "1");
  50944. else
  50945. fprintf(stderr, "0");
  50946. }
  50947. fprintf(stderr, " ");
  50948. }
  50949. fprintf(stderr, "cur_hi %u cur_lo %u\n", h, l);
  50950. #else
  50951. (void)h;
  50952. (void)l;
  50953. (void)w;
  50954. #endif
  50955. }
  50956. /* a - cur_hi
  50957. * b - cur_lo
  50958. * c - next_hi
  50959. * d - next_lo
  50960. * e - window
  50961. * f - expected next_hi
  50962. * g - expected next_lo
  50963. * h - expected window[1]
  50964. * i - expected window[0]
  50965. */
  50966. #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
  50967. wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)); \
  50968. DUW_TEST_print_window_binary((a), (b), (e)); \
  50969. AssertIntEQ((c), (f)); \
  50970. AssertIntEQ((d), (g)); \
  50971. AssertIntEQ((e)[1], (h)); \
  50972. AssertIntEQ((e)[0], (i)); \
  50973. } while (0)
  50974. static int test_wolfSSL_DtlsUpdateWindow(void)
  50975. {
  50976. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  50977. word32 next_lo = 0;
  50978. word16 next_hi = 0;
  50979. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  50980. fprintf(stderr, "\n");
  50981. #endif
  50982. XMEMSET(window, 0, sizeof window);
  50983. DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
  50984. DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
  50985. DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
  50986. DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
  50987. DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
  50988. DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
  50989. DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
  50990. DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
  50991. DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
  50992. DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
  50993. DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
  50994. DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
  50995. DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
  50996. DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
  50997. DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
  50998. DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
  50999. DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
  51000. DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
  51001. DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
  51002. DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
  51003. DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
  51004. DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  51005. DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  51006. DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
  51007. DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
  51008. DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  51009. DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  51010. DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
  51011. DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
  51012. DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
  51013. DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
  51014. DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
  51015. return TEST_RES_CHECK(1);
  51016. }
  51017. #endif /* WOLFSSL_DTLS */
  51018. #ifdef WOLFSSL_DTLS
  51019. static int DFB_TEST(WOLFSSL* ssl, word32 seq, word32 len, word32 f_offset,
  51020. word32 f_len, word32 f_count, byte ready, word32 bytesReceived)
  51021. {
  51022. DtlsMsg* cur;
  51023. static byte msg[100];
  51024. static byte msgInit = 0;
  51025. if (!msgInit) {
  51026. int i;
  51027. for (i = 0; i < 100; i++)
  51028. msg[i] = i + 1;
  51029. msgInit = 1;
  51030. }
  51031. /* Sanitize test parameters */
  51032. if (len > sizeof(msg))
  51033. return -1;
  51034. if (f_offset + f_len > sizeof(msg))
  51035. return -1;
  51036. DtlsMsgStore(ssl, 0, seq, msg + f_offset, len, certificate, f_offset, f_len, NULL);
  51037. if (ssl->dtls_rx_msg_list == NULL)
  51038. return -100;
  51039. if ((cur = DtlsMsgFind(ssl->dtls_rx_msg_list, 0, seq)) == NULL)
  51040. return -200;
  51041. if (cur->fragBucketListCount != f_count)
  51042. return -300;
  51043. if (cur->ready != ready)
  51044. return -400;
  51045. if (cur->bytesReceived != bytesReceived)
  51046. return -500;
  51047. if (ready) {
  51048. if (cur->fragBucketList != NULL)
  51049. return -600;
  51050. if (XMEMCMP(cur->fullMsg, msg, cur->sz) != 0)
  51051. return -700;
  51052. }
  51053. else {
  51054. DtlsFragBucket* fb;
  51055. if (cur->fragBucketList == NULL)
  51056. return -800;
  51057. for (fb = cur->fragBucketList; fb != NULL; fb = fb->m.m.next) {
  51058. if (XMEMCMP(fb->buf, msg + fb->m.m.offset, fb->m.m.sz) != 0)
  51059. return -900;
  51060. }
  51061. }
  51062. return 0;
  51063. }
  51064. static void DFB_TEST_RESET(WOLFSSL* ssl)
  51065. {
  51066. DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
  51067. ssl->dtls_rx_msg_list = NULL;
  51068. ssl->dtls_rx_msg_list_sz = 0;
  51069. }
  51070. static int test_wolfSSL_DTLS_fragment_buckets(void)
  51071. {
  51072. WOLFSSL ssl[1];
  51073. XMEMSET(ssl, 0, sizeof(*ssl));
  51074. AssertIntEQ(DFB_TEST(ssl, 0, 100, 0, 100, 0, 1, 100), 0); /* 0-100 */
  51075. AssertIntEQ(DFB_TEST(ssl, 1, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  51076. AssertIntEQ(DFB_TEST(ssl, 1, 100, 20, 20, 1, 0, 40), 0); /* 20-40 */
  51077. AssertIntEQ(DFB_TEST(ssl, 1, 100, 40, 20, 1, 0, 60), 0); /* 40-60 */
  51078. AssertIntEQ(DFB_TEST(ssl, 1, 100, 60, 20, 1, 0, 80), 0); /* 60-80 */
  51079. AssertIntEQ(DFB_TEST(ssl, 1, 100, 80, 20, 0, 1, 100), 0); /* 80-100 */
  51080. /* Test all permutations of 3 regions */
  51081. /* 1 2 3 */
  51082. AssertIntEQ(DFB_TEST(ssl, 2, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  51083. AssertIntEQ(DFB_TEST(ssl, 2, 100, 30, 30, 1, 0, 60), 0); /* 30-60 */
  51084. AssertIntEQ(DFB_TEST(ssl, 2, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  51085. /* 1 3 2 */
  51086. AssertIntEQ(DFB_TEST(ssl, 3, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  51087. AssertIntEQ(DFB_TEST(ssl, 3, 100, 60, 40, 2, 0, 70), 0); /* 60-100 */
  51088. AssertIntEQ(DFB_TEST(ssl, 3, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  51089. /* 2 1 3 */
  51090. AssertIntEQ(DFB_TEST(ssl, 4, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  51091. AssertIntEQ(DFB_TEST(ssl, 4, 100, 0, 30, 1, 0, 60), 0); /* 0-30 */
  51092. AssertIntEQ(DFB_TEST(ssl, 4, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  51093. /* 2 3 1 */
  51094. AssertIntEQ(DFB_TEST(ssl, 5, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  51095. AssertIntEQ(DFB_TEST(ssl, 5, 100, 60, 40, 1, 0, 70), 0); /* 60-100 */
  51096. AssertIntEQ(DFB_TEST(ssl, 5, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  51097. /* 3 1 2 */
  51098. AssertIntEQ(DFB_TEST(ssl, 6, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  51099. AssertIntEQ(DFB_TEST(ssl, 6, 100, 0, 30, 2, 0, 70), 0); /* 0-30 */
  51100. AssertIntEQ(DFB_TEST(ssl, 6, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  51101. /* 3 2 1 */
  51102. AssertIntEQ(DFB_TEST(ssl, 7, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  51103. AssertIntEQ(DFB_TEST(ssl, 7, 100, 30, 30, 1, 0, 70), 0); /* 30-60 */
  51104. AssertIntEQ(DFB_TEST(ssl, 7, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  51105. /* Test overlapping regions */
  51106. AssertIntEQ(DFB_TEST(ssl, 8, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  51107. AssertIntEQ(DFB_TEST(ssl, 8, 100, 20, 10, 1, 0, 30), 0); /* 20-30 */
  51108. AssertIntEQ(DFB_TEST(ssl, 8, 100, 70, 10, 2, 0, 40), 0); /* 70-80 */
  51109. AssertIntEQ(DFB_TEST(ssl, 8, 100, 20, 30, 2, 0, 60), 0); /* 20-50 */
  51110. AssertIntEQ(DFB_TEST(ssl, 8, 100, 40, 60, 0, 1, 100), 0); /* 40-100 */
  51111. /* Test overlapping multiple regions */
  51112. AssertIntEQ(DFB_TEST(ssl, 9, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  51113. AssertIntEQ(DFB_TEST(ssl, 9, 100, 30, 5, 2, 0, 25), 0); /* 30-35 */
  51114. AssertIntEQ(DFB_TEST(ssl, 9, 100, 40, 5, 3, 0, 30), 0); /* 40-45 */
  51115. AssertIntEQ(DFB_TEST(ssl, 9, 100, 50, 5, 4, 0, 35), 0); /* 50-55 */
  51116. AssertIntEQ(DFB_TEST(ssl, 9, 100, 60, 5, 5, 0, 40), 0); /* 60-65 */
  51117. AssertIntEQ(DFB_TEST(ssl, 9, 100, 70, 5, 6, 0, 45), 0); /* 70-75 */
  51118. AssertIntEQ(DFB_TEST(ssl, 9, 100, 30, 25, 4, 0, 55), 0); /* 30-55 */
  51119. AssertIntEQ(DFB_TEST(ssl, 9, 100, 55, 15, 2, 0, 65), 0); /* 55-70 */
  51120. AssertIntEQ(DFB_TEST(ssl, 9, 100, 75, 25, 2, 0, 90), 0); /* 75-100 */
  51121. AssertIntEQ(DFB_TEST(ssl, 9, 100, 10, 25, 0, 1, 100), 0); /* 10-35 */
  51122. AssertIntEQ(DFB_TEST(ssl, 10, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  51123. AssertIntEQ(DFB_TEST(ssl, 10, 100, 30, 20, 2, 0, 40), 0); /* 30-50 */
  51124. AssertIntEQ(DFB_TEST(ssl, 10, 100, 0, 40, 1, 0, 50), 0); /* 0-40 */
  51125. AssertIntEQ(DFB_TEST(ssl, 10, 100, 50, 50, 0, 1, 100), 0); /* 10-35 */
  51126. DFB_TEST_RESET(ssl);
  51127. return TEST_RES_CHECK(1);
  51128. }
  51129. #endif
  51130. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  51131. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  51132. static int test_wolfSSL_dtls_stateless2(void)
  51133. {
  51134. WOLFSSL *ssl_c, *ssl_c2, *ssl_s;
  51135. struct test_memio_ctx test_ctx;
  51136. WOLFSSL_CTX *ctx_c, *ctx_s;
  51137. int ret;
  51138. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  51139. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  51140. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method);
  51141. if (ret != 0)
  51142. return -1;
  51143. ssl_c2 = wolfSSL_new(ctx_c);
  51144. if (ssl_c2 == NULL)
  51145. return -2;
  51146. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  51147. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  51148. /* send CH */
  51149. ret = wolfSSL_connect(ssl_c2);
  51150. if (ret == 0 || ssl_c2->error != WANT_READ)
  51151. return -3;
  51152. ret = wolfSSL_accept(ssl_s);
  51153. if (ret == 0 || ssl_s->error != WANT_READ)
  51154. return -4;
  51155. if (test_ctx.c_len == 0)
  51156. return -5;
  51157. /* consume HRR */
  51158. test_ctx.c_len = 0;
  51159. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  51160. if (ret != 0)
  51161. return -6;
  51162. wolfSSL_free(ssl_c2);
  51163. wolfSSL_free(ssl_c);
  51164. wolfSSL_free(ssl_s);
  51165. wolfSSL_CTX_free(ctx_c);
  51166. wolfSSL_CTX_free(ctx_s);
  51167. return TEST_SUCCESS;
  51168. }
  51169. #ifdef HAVE_MAX_FRAGMENT
  51170. static int test_wolfSSL_dtls_stateless_maxfrag(void)
  51171. {
  51172. WOLFSSL *ssl_c, *ssl_c2, *ssl_s;
  51173. struct test_memio_ctx test_ctx;
  51174. WOLFSSL_CTX *ctx_c, *ctx_s;
  51175. word16 max_fragment;
  51176. int ret;
  51177. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  51178. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  51179. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method);
  51180. if (ret != 0)
  51181. return -1;
  51182. ssl_c2 = wolfSSL_new(ctx_c);
  51183. if (ssl_c2 == NULL)
  51184. return -2;
  51185. ret = wolfSSL_UseMaxFragment(ssl_c2, WOLFSSL_MFL_2_8);
  51186. if (ret != WOLFSSL_SUCCESS)
  51187. return -3;
  51188. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  51189. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  51190. max_fragment = ssl_s->max_fragment;
  51191. /* send CH */
  51192. ret = wolfSSL_connect(ssl_c2);
  51193. if (ret == 0 || ssl_c2->error != WANT_READ)
  51194. return -4;
  51195. ret = wolfSSL_accept(ssl_s);
  51196. if (ret == 0 || ssl_s->error != WANT_READ)
  51197. return -5;
  51198. /* CH without cookie shouldn't change state */
  51199. if (ssl_s->max_fragment != max_fragment)
  51200. return -6;
  51201. if (test_ctx.c_len == 0)
  51202. return -7;
  51203. /* consume HRR from buffer */
  51204. test_ctx.c_len = 0;
  51205. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  51206. if (ret != 0)
  51207. return -8;
  51208. wolfSSL_free(ssl_c2);
  51209. wolfSSL_free(ssl_c);
  51210. wolfSSL_free(ssl_s);
  51211. wolfSSL_CTX_free(ctx_c);
  51212. wolfSSL_CTX_free(ctx_s);
  51213. return TEST_SUCCESS;
  51214. }
  51215. #endif /* HAVE_MAX_FRAGMENT */
  51216. #if defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME)
  51217. #define ROUNDS_WITH_HVR 4
  51218. #define ROUNDS_WITHOUT_HVR 2
  51219. #define HANDSHAKE_TYPE_OFFSET DTLS_RECORD_HEADER_SZ
  51220. static int buf_is_hvr(const byte *data, int len)
  51221. {
  51222. if (len < DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ)
  51223. return 0;
  51224. return data[HANDSHAKE_TYPE_OFFSET] == hello_verify_request;
  51225. }
  51226. static int _test_wolfSSL_dtls_stateless_resume(byte useticket, byte bad)
  51227. {
  51228. struct test_memio_ctx test_ctx;
  51229. WOLFSSL_CTX *ctx_c, *ctx_s;
  51230. WOLFSSL *ssl_c, *ssl_s;
  51231. WOLFSSL_SESSION *sess;
  51232. int ret, round_trips;
  51233. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  51234. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  51235. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method);
  51236. if (ret != 0)
  51237. return -1;
  51238. #ifdef HAVE_SESSION_TICKET
  51239. if (useticket) {
  51240. ret = wolfSSL_UseSessionTicket(ssl_c);
  51241. if (ret != WOLFSSL_SUCCESS)
  51242. return -2;
  51243. }
  51244. #endif
  51245. round_trips = ROUNDS_WITH_HVR;
  51246. ret = test_memio_do_handshake(ssl_c, ssl_s, round_trips, &round_trips);
  51247. if (ret != 0)
  51248. return -3;
  51249. if (round_trips != ROUNDS_WITH_HVR)
  51250. return -4;
  51251. sess = wolfSSL_get1_session(ssl_c);
  51252. if (sess == NULL)
  51253. return -5;
  51254. wolfSSL_shutdown(ssl_c);
  51255. wolfSSL_shutdown(ssl_s);
  51256. wolfSSL_free(ssl_c);
  51257. wolfSSL_free(ssl_s);
  51258. test_ctx.c_len = test_ctx.s_len = 0;
  51259. /* make resumption invalid */
  51260. if (bad) {
  51261. if (useticket) {
  51262. #ifdef HAVE_SESSION_TICKET
  51263. sess->ticket[0] = !sess->ticket[0];
  51264. #endif /* HAVE_SESSION_TICKET */
  51265. }
  51266. else {
  51267. sess->sessionID[0] = !sess->sessionID[0];
  51268. }
  51269. }
  51270. ssl_c = wolfSSL_new(ctx_c);
  51271. ssl_s = wolfSSL_new(ctx_s);
  51272. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  51273. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  51274. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  51275. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  51276. ret = wolfSSL_set_session(ssl_c, sess);
  51277. if (ret != WOLFSSL_SUCCESS)
  51278. return -6;
  51279. ret = wolfSSL_connect(ssl_c);
  51280. if (ret == WOLFSSL_SUCCESS || ssl_c->error != WANT_READ)
  51281. return -7;
  51282. ret = wolfSSL_accept(ssl_s);
  51283. if (ret == WOLFSSL_SUCCESS || ssl_s->error != WANT_READ)
  51284. return -8;
  51285. if (bad && !buf_is_hvr(test_ctx.c_buff, test_ctx.c_len))
  51286. return -9;
  51287. if (!bad && buf_is_hvr(test_ctx.c_buff, test_ctx.c_len))
  51288. return -10;
  51289. if (!useticket) {
  51290. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, &round_trips);
  51291. if (ret != 0)
  51292. return -11;
  51293. if (bad && round_trips != ROUNDS_WITH_HVR - 1)
  51294. return -12;
  51295. if (!bad && round_trips != ROUNDS_WITHOUT_HVR - 1)
  51296. return -13;
  51297. }
  51298. wolfSSL_SESSION_free(sess);
  51299. wolfSSL_free(ssl_c);
  51300. wolfSSL_free(ssl_s);
  51301. wolfSSL_CTX_free(ctx_c);
  51302. wolfSSL_CTX_free(ctx_s);
  51303. return TEST_SUCCESS;
  51304. }
  51305. static int test_wolfSSL_dtls_stateless_resume(void)
  51306. {
  51307. int ret;
  51308. #ifdef HAVE_SESSION_TICKET
  51309. ret = _test_wolfSSL_dtls_stateless_resume(1, 0);
  51310. if (ret != 0)
  51311. return ret;
  51312. ret = _test_wolfSSL_dtls_stateless_resume(1, 1);
  51313. if (ret != 0)
  51314. return ret - 100;
  51315. #endif /* HAVE_SESION_TICKET */
  51316. ret = _test_wolfSSL_dtls_stateless_resume(0, 0);
  51317. if (ret != 0)
  51318. return ret - 200;
  51319. ret = _test_wolfSSL_dtls_stateless_resume(0, 1);
  51320. if (ret != 0)
  51321. return ret - 300;
  51322. return TEST_SUCCESS;
  51323. }
  51324. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  51325. #if !defined(NO_OLD_TLS)
  51326. static int test_wolfSSL_dtls_stateless_downgrade(void)
  51327. {
  51328. WOLFSSL_CTX *ctx_c, *ctx_c2, *ctx_s;
  51329. WOLFSSL *ssl_c, *ssl_c2, *ssl_s;
  51330. struct test_memio_ctx test_ctx;
  51331. int ret;
  51332. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  51333. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  51334. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method);
  51335. if (ret != 0)
  51336. return -1;
  51337. ret = wolfSSL_CTX_SetMinVersion(ctx_s, WOLFSSL_DTLSV1);
  51338. if (ret != WOLFSSL_SUCCESS)
  51339. return -2;
  51340. ctx_c2 = wolfSSL_CTX_new(wolfDTLSv1_client_method());
  51341. if (ctx_c2 == NULL)
  51342. return -3;
  51343. wolfSSL_SetIORecv(ctx_c2, test_memio_read_cb);
  51344. wolfSSL_SetIOSend(ctx_c2, test_memio_write_cb);
  51345. ssl_c2 = wolfSSL_new(ctx_c2);
  51346. if (ssl_c2 == NULL)
  51347. return -4;
  51348. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  51349. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  51350. /* send CH */
  51351. ret = wolfSSL_connect(ssl_c2);
  51352. if (ret == 0 || ssl_c2->error != WANT_READ)
  51353. return -5;
  51354. ret = wolfSSL_accept(ssl_s);
  51355. if (ret == 0 || ssl_s->error != WANT_READ)
  51356. return -6;
  51357. if (test_ctx.c_len == 0)
  51358. return -7;
  51359. /* consume HRR */
  51360. test_ctx.c_len = 0;
  51361. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  51362. if (ret != 0)
  51363. return -8;
  51364. wolfSSL_free(ssl_c2);
  51365. wolfSSL_free(ssl_c);
  51366. wolfSSL_free(ssl_s);
  51367. wolfSSL_CTX_free(ctx_c);
  51368. wolfSSL_CTX_free(ctx_c2);
  51369. wolfSSL_CTX_free(ctx_s);
  51370. return TEST_SUCCESS;
  51371. }
  51372. #endif /* !defined(NO_OLD_TLS) */
  51373. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  51374. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)*/
  51375. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  51376. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  51377. !defined(NO_OLD_TLS)
  51378. static int test_WOLFSSL_dtls_version_alert(void)
  51379. {
  51380. struct test_memio_ctx test_ctx;
  51381. WOLFSSL_CTX *ctx_c, *ctx_s;
  51382. WOLFSSL *ssl_c, *ssl_s;
  51383. int ret;
  51384. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  51385. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  51386. wolfDTLSv1_2_client_method, wolfDTLSv1_server_method);
  51387. if (ret != 0)
  51388. return -1;
  51389. /* client hello */
  51390. ret = wolfSSL_connect(ssl_c);
  51391. if (ret == 0 || ssl_c->error != WANT_READ )
  51392. return -2;
  51393. /* hrr */
  51394. ret = wolfSSL_accept(ssl_s);
  51395. if (ret == 0 || ssl_s->error != WANT_READ )
  51396. return -3;
  51397. /* client hello 1 */
  51398. ret = wolfSSL_connect(ssl_c);
  51399. if (ret == 0 || ssl_c->error != WANT_READ )
  51400. return -4;
  51401. /* server hello */
  51402. ret = wolfSSL_accept(ssl_s);
  51403. if (ret == 0 || ssl_s->error != WANT_READ )
  51404. return -5;
  51405. /* should fail */
  51406. ret = wolfSSL_connect(ssl_c);
  51407. if (ret == 0 || ssl_c->error != VERSION_ERROR)
  51408. return -6;
  51409. /* shuould fail */
  51410. ret = wolfSSL_accept(ssl_s);
  51411. if (ret == 0 ||
  51412. (ssl_s->error != VERSION_ERROR && ssl_s->error != FATAL_ERROR))
  51413. return -7;
  51414. wolfSSL_free(ssl_c);
  51415. wolfSSL_free(ssl_s);
  51416. wolfSSL_CTX_free(ctx_c);
  51417. wolfSSL_CTX_free(ctx_s);
  51418. return TEST_RES_CHECK(1);
  51419. }
  51420. #else
  51421. static int test_WOLFSSL_dtls_version_alert(void)
  51422. {
  51423. return TEST_SKIPPED;
  51424. }
  51425. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) &&
  51426. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) &&
  51427. * !defined(NO_OLD_TLS)
  51428. */
  51429. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  51430. && defined(WOLFSSL_TLS13) && \
  51431. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  51432. static int send_new_session_ticket(WOLFSSL *ssl, byte nonceLength, byte filler)
  51433. {
  51434. struct test_memio_ctx *test_ctx;
  51435. byte buf[2048];
  51436. int idx, sz;
  51437. word32 tmp;
  51438. int ret;
  51439. idx = 5; /* space for record header */
  51440. buf[idx] = session_ticket; /* type */
  51441. idx++;
  51442. tmp = OPAQUE32_LEN +
  51443. OPAQUE32_LEN +
  51444. OPAQUE8_LEN + nonceLength +
  51445. OPAQUE16_LEN + OPAQUE8_LEN + OPAQUE16_LEN;
  51446. c32to24(tmp, buf + idx);
  51447. idx += OPAQUE24_LEN;
  51448. c32toa((word32)12345, buf+idx); /* lifetime */
  51449. idx += OPAQUE32_LEN;
  51450. c32toa((word32)12345, buf+idx); /* add */
  51451. idx += OPAQUE32_LEN;
  51452. buf[idx] = nonceLength; /* nonce length */
  51453. idx++;
  51454. XMEMSET(&buf[idx], filler, nonceLength); /* nonce */
  51455. idx += nonceLength;
  51456. tmp = 1; /* ticket len */
  51457. c16toa((word16)tmp, buf+idx);
  51458. idx += 2;
  51459. buf[idx] = 0xFF; /* ticket */
  51460. idx++;
  51461. tmp = 0; /* ext len */
  51462. c16toa((word16)tmp, buf+idx);
  51463. idx += 2;
  51464. sz = BuildTls13Message(ssl, buf, 2048, buf+5, idx - 5,
  51465. handshake, 0, 0, 0);
  51466. test_ctx = (struct test_memio_ctx*)wolfSSL_GetIOWriteCtx(ssl);
  51467. ret = test_memio_write_cb(ssl, (char*)buf, sz, test_ctx);
  51468. return !(ret == sz);
  51469. }
  51470. static int test_ticket_nonce_check(WOLFSSL_SESSION *sess, byte len)
  51471. {
  51472. int i;
  51473. if (sess == NULL)
  51474. return -1;
  51475. if (sess->ticketNonce.len != len)
  51476. return -1;
  51477. for (i = 0; i < len; i++)
  51478. if (sess->ticketNonce.data[i] != len)
  51479. return -1;
  51480. return 0;
  51481. }
  51482. static int test_ticket_nonce_malloc_do(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  51483. {
  51484. char *buf[1024];
  51485. int ret;
  51486. ret = send_new_session_ticket(ssl_s, len, len);
  51487. if (ret != 0)
  51488. return -1;
  51489. ret = wolfSSL_recv(ssl_c, buf, 1024, 0);
  51490. if (ret != WOLFSSL_SUCCESS && ssl_c->error != WANT_READ)
  51491. return -1;
  51492. return test_ticket_nonce_check(ssl_c->session, len);
  51493. }
  51494. static int test_ticket_nonce_cache(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  51495. {
  51496. WOLFSSL_SESSION *sess, *cached;
  51497. WOLFSSL_CTX *ctx;
  51498. int ret;
  51499. ctx = ssl_c->ctx;
  51500. ret = test_ticket_nonce_malloc_do(ssl_s, ssl_c, len);
  51501. if (ret != 0)
  51502. return -1;
  51503. sess = wolfSSL_get1_session(ssl_c);
  51504. if (sess == NULL)
  51505. return -1;
  51506. ret = AddSessionToCache(ctx, sess, sess->sessionID, sess->sessionIDSz,
  51507. NULL, ssl_c->options.side, 1,NULL);
  51508. if (ret != 0)
  51509. return -1;
  51510. cached = wolfSSL_SESSION_new();
  51511. if (cached == NULL)
  51512. return -1;
  51513. ret = wolfSSL_GetSessionFromCache(ssl_c, cached);
  51514. if (ret != WOLFSSL_SUCCESS)
  51515. return -1;
  51516. ret = test_ticket_nonce_check(cached, len);
  51517. if (ret != 0)
  51518. return -1;
  51519. wolfSSL_SESSION_free(cached);
  51520. wolfSSL_SESSION_free(sess);
  51521. return 0;
  51522. }
  51523. static int test_ticket_nonce_malloc(void)
  51524. {
  51525. struct test_memio_ctx test_ctx;
  51526. WOLFSSL_CTX *ctx_c, *ctx_s;
  51527. byte small, medium, big;
  51528. WOLFSSL *ssl_c, *ssl_s;
  51529. int ret;
  51530. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  51531. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  51532. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method);
  51533. if (ret != 0)
  51534. return -1;
  51535. /* will send ticket manually */
  51536. wolfSSL_no_ticket_TLSv13(ssl_s);
  51537. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  51538. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  51539. while (!ssl_c->options.handShakeDone && !ssl_s->options.handShakeDone) {
  51540. ret = wolfSSL_connect(ssl_c);
  51541. if (ret != WOLFSSL_SUCCESS && ssl_c->error != WANT_READ)
  51542. return -2;
  51543. ret = wolfSSL_accept(ssl_s);
  51544. if (ret != WOLFSSL_SUCCESS && ssl_s->error != WANT_READ)
  51545. return -3;
  51546. }
  51547. small = TLS13_TICKET_NONCE_STATIC_SZ;
  51548. medium = small + 20 <= 255 ? small + 20 : 255;
  51549. big = medium + 20 <= 255 ? small + 20 : 255;
  51550. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, small))
  51551. return -1;
  51552. if (ssl_c->session->ticketNonce.data !=
  51553. ssl_c->session->ticketNonce.dataStatic)
  51554. return -1;
  51555. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium))
  51556. return -1;
  51557. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, big))
  51558. return -1;
  51559. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium))
  51560. return -5;
  51561. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, small))
  51562. return -6;
  51563. if (test_ticket_nonce_cache(ssl_s, ssl_c, small))
  51564. return -1;
  51565. if (test_ticket_nonce_cache(ssl_s, ssl_c, medium))
  51566. return -1;
  51567. if (test_ticket_nonce_cache(ssl_s, ssl_c, big))
  51568. return -1;
  51569. if (test_ticket_nonce_cache(ssl_s, ssl_c, medium))
  51570. return -1;
  51571. if (test_ticket_nonce_cache(ssl_s, ssl_c, small))
  51572. return -1;
  51573. wolfSSL_free(ssl_c);
  51574. wolfSSL_free(ssl_s);
  51575. wolfSSL_CTX_free(ctx_c);
  51576. wolfSSL_CTX_free(ctx_s);
  51577. return 0;
  51578. }
  51579. #endif /* WOLFSSL_TICKET_NONCE_MALLOC */
  51580. /*----------------------------------------------------------------------------*
  51581. | Main
  51582. *----------------------------------------------------------------------------*/
  51583. typedef int (*TEST_FUNC)(void);
  51584. typedef struct {
  51585. const char *name;
  51586. TEST_FUNC func;
  51587. byte run:1;
  51588. } TEST_CASE;
  51589. #define TEST_DECL(func) { #func, func, 0 }
  51590. int testAll = 1;
  51591. TEST_CASE testCases[] = {
  51592. TEST_DECL(test_fileAccess),
  51593. TEST_DECL(test_wolfSSL_Init),
  51594. TEST_DECL(test_wolfSSL_Method_Allocators),
  51595. #ifndef NO_WOLFSSL_SERVER
  51596. TEST_DECL(test_wolfSSL_CTX_new),
  51597. #endif
  51598. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  51599. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  51600. TEST_DECL(test_for_double_Free),
  51601. #endif
  51602. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  51603. TEST_DECL(test_wolfSSL_get_finished),
  51604. TEST_DECL(test_wolfSSL_CTX_add_session),
  51605. #endif
  51606. TEST_DECL(test_SSL_CIPHER_get_xxx),
  51607. TEST_DECL(test_wolfSSL_ERR_strings),
  51608. TEST_DECL(test_wolfSSL_EVP_shake128),
  51609. TEST_DECL(test_wolfSSL_EVP_shake256),
  51610. TEST_DECL(test_EVP_blake2),
  51611. TEST_DECL(test_EVP_MD_do_all),
  51612. TEST_DECL(test_OBJ_NAME_do_all),
  51613. TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
  51614. TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
  51615. TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
  51616. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
  51617. TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
  51618. TEST_DECL(test_wolfSSL_CTX_load_system_CA_certs),
  51619. TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
  51620. TEST_DECL(test_wolfSSL_CheckOCSPResponse),
  51621. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
  51622. TEST_DECL(test_wolfSSL_CertManagerGetCerts),
  51623. TEST_DECL(test_wolfSSL_CertManagerSetVerify),
  51624. TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
  51625. TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
  51626. TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
  51627. TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
  51628. TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
  51629. TEST_DECL(test_wolfSSL_FPKI),
  51630. TEST_DECL(test_wolfSSL_OtherName),
  51631. TEST_DECL(test_wolfSSL_CertRsaPss),
  51632. TEST_DECL(test_wolfSSL_CertManagerCRL),
  51633. TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
  51634. TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
  51635. TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
  51636. TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
  51637. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
  51638. TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
  51639. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
  51640. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
  51641. TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
  51642. TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
  51643. TEST_DECL(test_wolfSSL_CTX_enable_disable),
  51644. TEST_DECL(test_wolfSSL_CTX_ticket_API),
  51645. TEST_DECL(test_server_wolfSSL_new),
  51646. TEST_DECL(test_client_wolfSSL_new),
  51647. TEST_DECL(test_wolfSSL_SetTmpDH_file),
  51648. TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
  51649. TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
  51650. TEST_DECL(test_SetTmpEC_DHE_Sz),
  51651. TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
  51652. TEST_DECL(test_wolfSSL_dtls_set_mtu),
  51653. TEST_DECL(test_wolfSSL_dtls_plaintext),
  51654. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  51655. defined(HAVE_IO_TESTS_DEPENDENCIES)
  51656. TEST_DECL(test_wolfSSL_read_write),
  51657. TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
  51658. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient),
  51659. TEST_DECL(test_wolfSSL_CTX_set_cipher_list),
  51660. TEST_DECL(test_wolfSSL_dtls_export),
  51661. TEST_DECL(test_wolfSSL_tls_export),
  51662. #endif
  51663. TEST_DECL(test_wolfSSL_SetMinVersion),
  51664. TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
  51665. /* TLS extensions tests */
  51666. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  51667. #ifdef HAVE_SNI
  51668. TEST_DECL(test_wolfSSL_UseSNI_params),
  51669. TEST_DECL(test_wolfSSL_UseSNI_connection),
  51670. TEST_DECL(test_wolfSSL_SNI_GetFromBuffer),
  51671. #endif /* HAVE_SNI */
  51672. #endif
  51673. TEST_DECL(test_wolfSSL_UseTrustedCA),
  51674. TEST_DECL(test_wolfSSL_UseMaxFragment),
  51675. TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
  51676. TEST_DECL(test_wolfSSL_UseSupportedCurve),
  51677. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  51678. TEST_DECL(test_wolfSSL_UseALPN_connection),
  51679. TEST_DECL(test_wolfSSL_UseALPN_params),
  51680. #endif
  51681. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  51682. TEST_DECL(test_wolfSSL_set_alpn_protos),
  51683. #endif
  51684. TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
  51685. TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
  51686. TEST_DECL(test_tls_ext_duplicate),
  51687. /* X509 tests */
  51688. TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
  51689. TEST_DECL(test_wolfSSL_PKCS12),
  51690. TEST_DECL(test_wolfSSL_no_password_cb),
  51691. TEST_DECL(test_wolfSSL_PKCS8),
  51692. TEST_DECL(test_wolfSSL_PKCS8_ED25519),
  51693. TEST_DECL(test_wolfSSL_PKCS8_ED448),
  51694. TEST_DECL(test_wolfSSL_PKCS5),
  51695. TEST_DECL(test_wolfSSL_URI),
  51696. TEST_DECL(test_wolfSSL_TBS),
  51697. TEST_DECL(test_wolfSSL_X509_verify),
  51698. TEST_DECL(test_wolfSSL_X509_TLS_version),
  51699. TEST_DECL(test_wc_PemToDer),
  51700. TEST_DECL(test_wc_AllocDer),
  51701. TEST_DECL(test_wc_CertPemToDer),
  51702. TEST_DECL(test_wc_PubKeyPemToDer),
  51703. TEST_DECL(test_wc_PemPubKeyToDer),
  51704. TEST_DECL(test_wc_GetPubKeyDerFromCert),
  51705. TEST_DECL(test_wc_CheckCertSigPubKey),
  51706. /* OCSP Stapling */
  51707. TEST_DECL(test_wolfSSL_UseOCSPStapling),
  51708. TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
  51709. /* Multicast */
  51710. TEST_DECL(test_wolfSSL_mcast),
  51711. /* compatibility tests */
  51712. TEST_DECL(test_wolfSSL_lhash),
  51713. TEST_DECL(test_wolfSSL_X509_NAME),
  51714. TEST_DECL(test_wolfSSL_X509_NAME_hash),
  51715. TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
  51716. #ifndef NO_BIO
  51717. TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
  51718. TEST_DECL(test_wolfSSL_X509_INFO),
  51719. #endif
  51720. TEST_DECL(test_wolfSSL_X509_subject_name_hash),
  51721. TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
  51722. TEST_DECL(test_wolfSSL_X509_check_host),
  51723. TEST_DECL(test_wolfSSL_X509_check_email),
  51724. TEST_DECL(test_wolfSSL_DES),
  51725. TEST_DECL(test_wolfSSL_certs),
  51726. TEST_DECL(test_wolfSSL_X509_check_private_key),
  51727. TEST_DECL(test_wolfSSL_ASN1_TIME_print),
  51728. TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
  51729. TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
  51730. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
  51731. TEST_DECL(test_wolfSSL_private_keys),
  51732. TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
  51733. #ifndef NO_BIO
  51734. TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
  51735. #endif
  51736. TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
  51737. TEST_DECL(test_wolfSSL_PEM_PrivateKey),
  51738. TEST_DECL(test_wolfSSL_PEM_file_RSAKey),
  51739. TEST_DECL(test_wolfSSL_PEM_file_RSAPrivateKey),
  51740. #ifndef NO_BIO
  51741. TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
  51742. TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
  51743. TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
  51744. TEST_DECL(test_wolfSSL_PEM_bio_RSAPrivateKey),
  51745. TEST_DECL(test_wolfSSL_PEM_PUBKEY),
  51746. #endif
  51747. TEST_DECL(test_DSA_do_sign_verify),
  51748. TEST_DECL(test_wolfSSL_tmp_dh),
  51749. TEST_DECL(test_wolfSSL_ctrl),
  51750. TEST_DECL(test_wolfSSL_EVP_MD_size),
  51751. TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
  51752. TEST_DECL(test_wolfSSL_EVP_Digest),
  51753. TEST_DECL(test_wolfSSL_EVP_Digest_all),
  51754. TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
  51755. TEST_DECL(test_wolfSSL_EVP_PKEY_new_CMAC_key),
  51756. TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
  51757. TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
  51758. TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
  51759. TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
  51760. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
  51761. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
  51762. TEST_DECL(test_wolfSSL_EVP_EncodeInit),
  51763. TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
  51764. TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
  51765. TEST_DECL(test_wolfSSL_EVP_DecodeInit),
  51766. TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
  51767. TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
  51768. TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
  51769. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  51770. TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
  51771. #endif
  51772. #ifndef NO_BIO
  51773. TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
  51774. TEST_DECL(test_wolfSSL_GetLoggingCb),
  51775. TEST_DECL(test_WOLFSSL_ERROR_MSG),
  51776. TEST_DECL(test_wc_ERR_remove_state),
  51777. TEST_DECL(test_wc_ERR_print_errors_fp),
  51778. #endif
  51779. TEST_DECL(test_wolfSSL_set_options),
  51780. TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
  51781. TEST_DECL(test_wolfSSL_set1_curves_list),
  51782. TEST_DECL(test_wolfSSL_set1_sigalgs_list),
  51783. TEST_DECL(test_wolfSSL_PKCS7_certs),
  51784. TEST_DECL(test_wolfSSL_X509_STORE_CTX),
  51785. TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
  51786. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
  51787. TEST_DECL(test_wolfSSL_msgCb),
  51788. TEST_DECL(test_wolfSSL_either_side),
  51789. TEST_DECL(test_wolfSSL_DTLS_either_side),
  51790. TEST_DECL(test_wolfSSL_dtls_fragments),
  51791. TEST_DECL(test_wolfSSL_dtls_AEAD_limit),
  51792. TEST_DECL(test_wolfSSL_ignore_alert_before_cookie),
  51793. TEST_DECL(test_wolfSSL_dtls_bad_record),
  51794. TEST_DECL(test_wolfSSL_dtls_stateless),
  51795. TEST_DECL(test_generate_cookie),
  51796. TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
  51797. TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
  51798. TEST_DECL(test_wolfSSL_X509_Name_canon),
  51799. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
  51800. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
  51801. TEST_DECL(test_wolfSSL_X509_NID),
  51802. TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
  51803. TEST_DECL(test_wolfSSL_get0_param),
  51804. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
  51805. TEST_DECL(test_wolfSSL_set1_host),
  51806. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
  51807. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
  51808. TEST_DECL(test_wolfSSL_X509_STORE),
  51809. TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
  51810. TEST_DECL(test_X509_STORE_get0_objects),
  51811. TEST_DECL(test_wolfSSL_X509_load_crl_file),
  51812. TEST_DECL(test_wolfSSL_BN),
  51813. TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
  51814. #ifndef NO_BIO
  51815. TEST_DECL(test_wolfSSL_PEM_read_bio),
  51816. TEST_DECL(test_wolfSSL_BIO),
  51817. #endif
  51818. TEST_DECL(test_wolfSSL_ASN1_STRING),
  51819. TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
  51820. TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
  51821. TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
  51822. TEST_DECL(test_wolfSSL_X509),
  51823. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
  51824. TEST_DECL(test_wolfSSL_X509_sign),
  51825. TEST_DECL(test_wolfSSL_X509_sign2),
  51826. TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
  51827. TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
  51828. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  51829. TEST_DECL(test_wolfSSL_check_domain),
  51830. #endif
  51831. TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
  51832. TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
  51833. TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
  51834. TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
  51835. TEST_DECL(test_wolfSSL_RAND),
  51836. TEST_DECL(test_wolfSSL_BUF),
  51837. TEST_DECL(test_wolfSSL_set_tlsext_status_type),
  51838. TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
  51839. TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
  51840. TEST_DECL(test_wolfSSL_X509_cmp_time),
  51841. TEST_DECL(test_wolfSSL_X509_time_adj),
  51842. TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
  51843. TEST_DECL(test_wolfSSL_CTX_add_client_CA),
  51844. TEST_DECL(test_wolfSSL_CTX_set_srp_username),
  51845. TEST_DECL(test_wolfSSL_CTX_set_srp_password),
  51846. TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
  51847. TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
  51848. TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
  51849. TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
  51850. TEST_DECL(test_wolfSSL_Tls13_postauth),
  51851. TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
  51852. TEST_DECL(test_wolfSSL_set_minmax_proto_version),
  51853. TEST_DECL(test_wolfSSL_THREADID_hash),
  51854. TEST_DECL(test_wolfSSL_RAND_set_rand_method),
  51855. TEST_DECL(test_wolfSSL_RAND_bytes),
  51856. TEST_DECL(test_wolfSSL_BN_rand),
  51857. TEST_DECL(test_wolfSSL_pseudo_rand),
  51858. TEST_DECL(test_wolfSSL_PKCS8_Compat),
  51859. TEST_DECL(test_wolfSSL_PKCS8_d2i),
  51860. TEST_DECL(test_error_queue_per_thread),
  51861. TEST_DECL(test_wolfSSL_ERR_put_error),
  51862. TEST_DECL(test_wolfSSL_ERR_get_error_order),
  51863. #ifndef NO_BIO
  51864. TEST_DECL(test_wolfSSL_ERR_print_errors),
  51865. #endif
  51866. TEST_DECL(test_wolfSSL_HMAC),
  51867. TEST_DECL(test_wolfSSL_CMAC),
  51868. TEST_DECL(test_wolfSSL_OBJ),
  51869. TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
  51870. TEST_DECL(test_wolfSSL_OBJ_cmp),
  51871. TEST_DECL(test_wolfSSL_OBJ_txt2nid),
  51872. TEST_DECL(test_wolfSSL_OBJ_txt2obj),
  51873. TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
  51874. TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
  51875. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
  51876. TEST_DECL(test_wolfSSL_X509_set_name),
  51877. TEST_DECL(test_wolfSSL_X509_set_notAfter),
  51878. TEST_DECL(test_wolfSSL_X509_set_notBefore),
  51879. TEST_DECL(test_wolfSSL_X509_set_version),
  51880. #ifndef NO_BIO
  51881. TEST_DECL(test_wolfSSL_BIO_gets),
  51882. TEST_DECL(test_wolfSSL_BIO_puts),
  51883. TEST_DECL(test_wolfSSL_BIO_dump),
  51884. TEST_DECL(test_wolfSSL_BIO_should_retry),
  51885. TEST_DECL(test_wolfSSL_d2i_PUBKEY),
  51886. TEST_DECL(test_wolfSSL_BIO_write),
  51887. TEST_DECL(test_wolfSSL_BIO_connect),
  51888. TEST_DECL(test_wolfSSL_BIO_accept),
  51889. TEST_DECL(test_wolfSSL_BIO_printf),
  51890. TEST_DECL(test_wolfSSL_BIO_f_md),
  51891. TEST_DECL(test_wolfSSL_BIO_up_ref),
  51892. TEST_DECL(test_wolfSSL_BIO_tls),
  51893. #endif
  51894. TEST_DECL(test_wolfSSL_cert_cb),
  51895. TEST_DECL(test_wolfSSL_SESSION),
  51896. TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
  51897. TEST_DECL(test_wolfSSL_ticket_keys),
  51898. TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
  51899. TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
  51900. TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
  51901. TEST_DECL(test_wolfSSL_sk_DIST_POINT),
  51902. TEST_DECL(test_wolfSSL_MD4),
  51903. TEST_DECL(test_wolfSSL_verify_mode),
  51904. TEST_DECL(test_wolfSSL_verify_depth),
  51905. TEST_DECL(test_wolfSSL_HMAC_CTX),
  51906. TEST_DECL(test_wolfSSL_msg_callback),
  51907. TEST_DECL(test_wolfSSL_SHA),
  51908. TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
  51909. TEST_DECL(test_wolfSSL_MD5),
  51910. TEST_DECL(test_wolfSSL_MD5_Transform),
  51911. TEST_DECL(test_wolfSSL_SHA_Transform),
  51912. TEST_DECL(test_wolfSSL_SHA256),
  51913. TEST_DECL(test_wolfSSL_SHA256_Transform),
  51914. TEST_DECL(test_wolfSSL_SHA224),
  51915. TEST_DECL(test_wolfSSL_SHA512_Transform),
  51916. TEST_DECL(test_wolfSSL_X509_get_serialNumber),
  51917. TEST_DECL(test_wolfSSL_X509_CRL),
  51918. TEST_DECL(test_wolfSSL_d2i_X509_REQ),
  51919. TEST_DECL(test_wolfSSL_PEM_read_X509),
  51920. TEST_DECL(test_wolfSSL_PEM_read),
  51921. #ifndef NO_BIO
  51922. TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
  51923. TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
  51924. #endif
  51925. TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
  51926. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
  51927. TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
  51928. TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
  51929. TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
  51930. TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
  51931. TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
  51932. TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
  51933. TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
  51934. TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
  51935. TEST_DECL(test_wolfSSL_X509_check_ca),
  51936. TEST_DECL(test_wolfSSL_X509_check_ip_asc),
  51937. TEST_DECL(test_wolfSSL_make_cert),
  51938. TEST_DECL(test_wolfSSL_DES_ncbc),
  51939. TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
  51940. TEST_DECL(test_wolfSSL_CRYPTO_cts128),
  51941. TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
  51942. TEST_DECL(test_wolfssl_EVP_aes_gcm),
  51943. TEST_DECL(test_wolfssl_EVP_aes_gcm_zeroLen),
  51944. TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
  51945. TEST_DECL(test_wolfssl_EVP_chacha20),
  51946. TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
  51947. TEST_DECL(test_wolfSSL_PKEY_up_ref),
  51948. TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
  51949. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
  51950. TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
  51951. TEST_DECL(test_wolfSSL_i2d_PrivateKey),
  51952. TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
  51953. TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
  51954. TEST_DECL(test_wolfSSL_d2i_OCSP_CERTID),
  51955. TEST_DECL(test_wolfSSL_OCSP_id_cmp),
  51956. TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
  51957. TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
  51958. TEST_DECL(test_wolfSSL_OCSP_resp_count),
  51959. TEST_DECL(test_wolfSSL_OCSP_resp_get0),
  51960. TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
  51961. TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
  51962. TEST_DECL(test_CONF_modules_xxx),
  51963. TEST_DECL(test_CRYPTO_set_dynlock_xxx),
  51964. TEST_DECL(test_CRYPTO_THREADID_xxx),
  51965. TEST_DECL(test_ENGINE_cleanup),
  51966. TEST_DECL(test_wolfSSL_EC_KEY_set_group),
  51967. TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
  51968. TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
  51969. #ifdef OPENSSL_ALL
  51970. TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
  51971. TEST_DECL(test_wolfSSL_sk_CIPHER_description),
  51972. TEST_DECL(test_wolfSSL_get_ciphers_compat),
  51973. TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
  51974. TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
  51975. TEST_DECL(test_wolfSSL_EC_KEY_dup),
  51976. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
  51977. TEST_DECL(test_wolfSSL_DSA_SIG),
  51978. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
  51979. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
  51980. TEST_DECL(test_wolfSSL_CTX_ctrl),
  51981. TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
  51982. TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
  51983. TEST_DECL(test_wolfSSL_EVP_PKEY_id),
  51984. TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
  51985. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
  51986. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
  51987. TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
  51988. TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
  51989. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
  51990. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
  51991. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
  51992. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_key_length),
  51993. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
  51994. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
  51995. TEST_DECL(test_wolfSSL_EVP_rc4),
  51996. TEST_DECL(test_wolfSSL_EVP_enc_null),
  51997. TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
  51998. TEST_DECL(test_wolfSSL_EVP_mdc2),
  51999. TEST_DECL(test_wolfSSL_EVP_md4),
  52000. TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
  52001. TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
  52002. TEST_DECL(test_wolfSSL_EVP_ripemd160),
  52003. TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
  52004. TEST_DECL(test_wolfSSL_EVP_MD_nid),
  52005. TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
  52006. TEST_DECL(test_wolfSSL_EVP_X_STATE),
  52007. TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
  52008. TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
  52009. TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
  52010. TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
  52011. TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
  52012. TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
  52013. TEST_DECL(test_wolfSSL_EVP_BytesToKey),
  52014. TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
  52015. TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
  52016. TEST_DECL(test_evp_cipher_aes_gcm),
  52017. TEST_DECL(test_wolfSSL_OBJ_ln),
  52018. TEST_DECL(test_wolfSSL_OBJ_sn),
  52019. TEST_DECL(test_wolfSSL_TXT_DB),
  52020. TEST_DECL(test_wolfSSL_NCONF),
  52021. #endif /* OPENSSL_ALL */
  52022. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  52023. TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
  52024. #ifndef NO_BIO
  52025. TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
  52026. #endif /* !NO_BIO */
  52027. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  52028. TEST_DECL(test_wolfSSL_X509_CA_num),
  52029. TEST_DECL(test_wolfSSL_X509_get_version),
  52030. #ifndef NO_BIO
  52031. TEST_DECL(test_wolfSSL_X509_print),
  52032. TEST_DECL(test_wolfSSL_X509_CRL_print),
  52033. TEST_DECL(test_wolfSSL_BIO_get_len),
  52034. #endif
  52035. TEST_DECL(test_wolfSSL_RSA),
  52036. TEST_DECL(test_wolfSSL_RSA_DER),
  52037. TEST_DECL(test_wolfSSL_RSA_print),
  52038. #ifndef NO_RSA
  52039. TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
  52040. #endif
  52041. TEST_DECL(test_wolfSSL_RSA_sign_sha3),
  52042. TEST_DECL(test_wolfSSL_RSA_get0_key),
  52043. TEST_DECL(test_wolfSSL_RSA_meth),
  52044. TEST_DECL(test_wolfSSL_RSA_verify),
  52045. TEST_DECL(test_wolfSSL_RSA_sign),
  52046. TEST_DECL(test_wolfSSL_RSA_sign_ex),
  52047. TEST_DECL(test_wolfSSL_RSA_public_decrypt),
  52048. TEST_DECL(test_wolfSSL_RSA_private_encrypt),
  52049. TEST_DECL(test_wolfSSL_RSA_public_encrypt),
  52050. TEST_DECL(test_wolfSSL_RSA_private_decrypt),
  52051. TEST_DECL(test_wolfSSL_RSA_GenAdd),
  52052. TEST_DECL(test_wolfSSL_RSA_blinding_on),
  52053. TEST_DECL(test_wolfSSL_RSA_ex_data),
  52054. TEST_DECL(test_wolfSSL_RSA_LoadDer),
  52055. TEST_DECL(test_wolfSSL_RSA_To_Der),
  52056. TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
  52057. TEST_DECL(test_wolfSSL_PEM_write_RSA_PUBKEY),
  52058. TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
  52059. TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
  52060. TEST_DECL(test_wolfSSL_DH),
  52061. TEST_DECL(test_wolfSSL_DH_dup),
  52062. TEST_DECL(test_wolfSSL_DH_check),
  52063. TEST_DECL(test_wolfSSL_DH_prime),
  52064. TEST_DECL(test_wolfSSL_DH_1536_prime),
  52065. TEST_DECL(test_wolfSSL_DH_get_2048_256),
  52066. TEST_DECL(test_wolfSSL_PEM_write_DHparams),
  52067. TEST_DECL(test_wolfSSL_PEM_read_DHparams),
  52068. TEST_DECL(test_wolfSSL_d2i_DHparams),
  52069. TEST_DECL(test_wolfSSL_DH_LoadDer),
  52070. TEST_DECL(test_wolfSSL_i2d_DHparams),
  52071. TEST_DECL(test_wolfSSL_X509V3_EXT_get),
  52072. TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
  52073. TEST_DECL(test_wolfSSL_X509V3_EXT),
  52074. TEST_DECL(test_wolfSSL_X509_get_extension_flags),
  52075. TEST_DECL(test_wolfSSL_X509_get_ext),
  52076. TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
  52077. TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
  52078. TEST_DECL(test_wolfSSL_X509_get_ext_count),
  52079. TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
  52080. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
  52081. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
  52082. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
  52083. TEST_DECL(test_wolfSSL_X509V3_EXT_print),
  52084. TEST_DECL(test_wolfSSL_X509_cmp),
  52085. #ifndef NO_BIO
  52086. TEST_DECL(test_wolfSSL_ASN1_STRING_print),
  52087. #endif
  52088. TEST_DECL(test_wolfSSL_ASN1_get_object),
  52089. TEST_DECL(test_openssl_generate_key_and_cert),
  52090. TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
  52091. TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
  52092. TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
  52093. /* test the no op functions for compatibility */
  52094. TEST_DECL(test_no_op_functions),
  52095. /* OpenSSL EVP_PKEY API tests */
  52096. TEST_DECL(test_EVP_PKEY_rsa),
  52097. TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
  52098. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify),
  52099. TEST_DECL(test_EVP_PKEY_ec),
  52100. TEST_DECL(test_EVP_PKEY_cmp),
  52101. /* OpenSSL error API tests */
  52102. TEST_DECL(test_ERR_load_crypto_strings),
  52103. /* OpenSSL sk_X509 API test */
  52104. TEST_DECL(test_sk_X509),
  52105. /* OpenSSL sk_X509_CRL API test */
  52106. TEST_DECL(test_sk_X509_CRL),
  52107. /* OpenSSL X509 API test */
  52108. TEST_DECL(test_X509_get_signature_nid),
  52109. /* OpenSSL X509 REQ API test */
  52110. TEST_DECL(test_X509_REQ),
  52111. /* OpenSSL PKCS7 API test */
  52112. TEST_DECL(test_wolfssl_PKCS7),
  52113. TEST_DECL(test_wolfSSL_PKCS7_sign),
  52114. TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
  52115. #ifndef NO_BIO
  52116. TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
  52117. #ifdef HAVE_SMIME
  52118. TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
  52119. TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
  52120. #endif /* HAVE_SMIME */
  52121. #endif /* !NO_BIO */
  52122. /* OpenSSL compatibility outside SSL context w/ CRL lookup directory */
  52123. TEST_DECL(test_X509_STORE_No_SSL_CTX),
  52124. TEST_DECL(test_X509_LOOKUP_add_dir),
  52125. /* wolfCrypt ASN tests */
  52126. TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
  52127. TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
  52128. TEST_DECL(test_wc_SetSubjectRaw),
  52129. TEST_DECL(test_wc_GetSubjectRaw),
  52130. TEST_DECL(test_wc_SetIssuerRaw),
  52131. TEST_DECL(test_wc_SetIssueBuffer),
  52132. TEST_DECL(test_wc_SetSubjectKeyId),
  52133. TEST_DECL(test_wc_SetSubject),
  52134. TEST_DECL(test_CheckCertSignature),
  52135. TEST_DECL(test_wc_ParseCert),
  52136. TEST_DECL(test_MakeCertWithPathLen),
  52137. /* wolfCrypt ECC tests */
  52138. TEST_DECL(test_wc_ecc_get_curve_size_from_name),
  52139. TEST_DECL(test_wc_ecc_get_curve_id_from_name),
  52140. TEST_DECL(test_wc_ecc_get_curve_id_from_params),
  52141. #ifdef WOLFSSL_TLS13
  52142. /* TLS v1.3 API tests */
  52143. TEST_DECL(test_tls13_apis),
  52144. TEST_DECL(test_tls13_cipher_suites),
  52145. #endif
  52146. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  52147. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  52148. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  52149. /* Bad certificate signature tests */
  52150. TEST_DECL(test_EccSigFailure_cm),
  52151. TEST_DECL(test_RsaSigFailure_cm),
  52152. #endif /* NO_CERTS */
  52153. #if defined(HAVE_PK_CALLBACKS) && (!defined(WOLFSSL_NO_TLS12) || \
  52154. !defined(NO_OLD_TLS))
  52155. TEST_DECL(test_DhCallbacks),
  52156. #endif
  52157. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  52158. TEST_DECL(test_export_keying_material),
  52159. #endif
  52160. TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
  52161. TEST_DECL(test_wolfSSL_security_level),
  52162. TEST_DECL(test_wolfSSL_SSL_in_init),
  52163. TEST_DECL(test_wolfSSL_EC_curve),
  52164. TEST_DECL(test_wolfSSL_CTX_set_timeout),
  52165. TEST_DECL(test_wolfSSL_OpenSSL_version),
  52166. TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
  52167. TEST_DECL(test_CONF_CTX_FILE),
  52168. TEST_DECL(test_CONF_CTX_CMDLINE),
  52169. TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
  52170. /* wolfcrypt */
  52171. TEST_DECL(test_wolfCrypt_Init),
  52172. TEST_DECL(test_wc_InitMd5),
  52173. TEST_DECL(test_wc_Md5Update),
  52174. TEST_DECL(test_wc_Md5Final),
  52175. TEST_DECL(test_wc_InitSha),
  52176. TEST_DECL(test_wc_ShaUpdate),
  52177. TEST_DECL(test_wc_ShaFinal),
  52178. TEST_DECL(test_wc_InitSha256),
  52179. TEST_DECL(test_wc_Sha256Update),
  52180. TEST_DECL(test_wc_Sha256Final),
  52181. TEST_DECL(test_wc_Sha256FinalRaw),
  52182. TEST_DECL(test_wc_Sha256GetFlags),
  52183. TEST_DECL(test_wc_Sha256Free),
  52184. TEST_DECL(test_wc_Sha256GetHash),
  52185. TEST_DECL(test_wc_Sha256Copy),
  52186. TEST_DECL(test_wc_InitSha512),
  52187. TEST_DECL(test_wc_Sha512Update),
  52188. TEST_DECL(test_wc_Sha512Final),
  52189. TEST_DECL(test_wc_Sha512GetFlags),
  52190. TEST_DECL(test_wc_Sha512FinalRaw),
  52191. TEST_DECL(test_wc_Sha512Free),
  52192. TEST_DECL(test_wc_Sha512GetHash),
  52193. TEST_DECL(test_wc_Sha512Copy),
  52194. TEST_DECL(test_wc_InitSha512_224),
  52195. TEST_DECL(test_wc_Sha512_224Update),
  52196. TEST_DECL(test_wc_Sha512_224Final),
  52197. TEST_DECL(test_wc_Sha512_224GetFlags),
  52198. TEST_DECL(test_wc_Sha512_224FinalRaw),
  52199. TEST_DECL(test_wc_Sha512_224Free),
  52200. TEST_DECL(test_wc_Sha512_224GetHash),
  52201. TEST_DECL(test_wc_Sha512_224Copy),
  52202. TEST_DECL(test_wc_InitSha512_256),
  52203. TEST_DECL(test_wc_Sha512_256Update),
  52204. TEST_DECL(test_wc_Sha512_256Final),
  52205. TEST_DECL(test_wc_Sha512_256GetFlags),
  52206. TEST_DECL(test_wc_Sha512_256FinalRaw),
  52207. TEST_DECL(test_wc_Sha512_256Free),
  52208. TEST_DECL(test_wc_Sha512_256GetHash),
  52209. TEST_DECL(test_wc_Sha512_256Copy),
  52210. TEST_DECL(test_wc_InitSha384),
  52211. TEST_DECL(test_wc_Sha384Update),
  52212. TEST_DECL(test_wc_Sha384Final),
  52213. TEST_DECL(test_wc_Sha384GetFlags),
  52214. TEST_DECL(test_wc_Sha384FinalRaw),
  52215. TEST_DECL(test_wc_Sha384Free),
  52216. TEST_DECL(test_wc_Sha384GetHash),
  52217. TEST_DECL(test_wc_Sha384Copy),
  52218. TEST_DECL(test_wc_InitSha224),
  52219. TEST_DECL(test_wc_Sha224Update),
  52220. TEST_DECL(test_wc_Sha224Final),
  52221. TEST_DECL(test_wc_Sha224SetFlags),
  52222. TEST_DECL(test_wc_Sha224GetFlags),
  52223. TEST_DECL(test_wc_Sha224Free),
  52224. TEST_DECL(test_wc_Sha224GetHash),
  52225. TEST_DECL(test_wc_Sha224Copy),
  52226. TEST_DECL(test_wc_InitBlake2b),
  52227. TEST_DECL(test_wc_InitBlake2b_WithKey),
  52228. TEST_DECL(test_wc_InitBlake2s_WithKey),
  52229. TEST_DECL(test_wc_InitRipeMd),
  52230. TEST_DECL(test_wc_RipeMdUpdate),
  52231. TEST_DECL(test_wc_RipeMdFinal),
  52232. TEST_DECL(test_wc_InitSha3),
  52233. TEST_DECL(testing_wc_Sha3_Update),
  52234. TEST_DECL(test_wc_Sha3_224_Final),
  52235. TEST_DECL(test_wc_Sha3_256_Final),
  52236. TEST_DECL(test_wc_Sha3_384_Final),
  52237. TEST_DECL(test_wc_Sha3_512_Final),
  52238. TEST_DECL(test_wc_Sha3_224_Copy),
  52239. TEST_DECL(test_wc_Sha3_256_Copy),
  52240. TEST_DECL(test_wc_Sha3_384_Copy),
  52241. TEST_DECL(test_wc_Sha3_512_Copy),
  52242. TEST_DECL(test_wc_Sha3_GetFlags),
  52243. TEST_DECL(test_wc_InitShake256),
  52244. TEST_DECL(testing_wc_Shake256_Update),
  52245. TEST_DECL(test_wc_Shake256_Final),
  52246. TEST_DECL(test_wc_Shake256_Copy),
  52247. TEST_DECL(test_wc_Shake256Hash),
  52248. TEST_DECL(test_wc_Md5HmacSetKey),
  52249. TEST_DECL(test_wc_Md5HmacUpdate),
  52250. TEST_DECL(test_wc_Md5HmacFinal),
  52251. TEST_DECL(test_wc_ShaHmacSetKey),
  52252. TEST_DECL(test_wc_ShaHmacUpdate),
  52253. TEST_DECL(test_wc_ShaHmacFinal),
  52254. TEST_DECL(test_wc_Sha224HmacSetKey),
  52255. TEST_DECL(test_wc_Sha224HmacUpdate),
  52256. TEST_DECL(test_wc_Sha224HmacFinal),
  52257. TEST_DECL(test_wc_Sha256HmacSetKey),
  52258. TEST_DECL(test_wc_Sha256HmacUpdate),
  52259. TEST_DECL(test_wc_Sha256HmacFinal),
  52260. TEST_DECL(test_wc_Sha384HmacSetKey),
  52261. TEST_DECL(test_wc_Sha384HmacUpdate),
  52262. TEST_DECL(test_wc_Sha384HmacFinal),
  52263. TEST_DECL(test_wc_HashInit),
  52264. TEST_DECL(test_wc_HashSetFlags),
  52265. TEST_DECL(test_wc_HashGetFlags),
  52266. TEST_DECL(test_wc_InitCmac),
  52267. TEST_DECL(test_wc_CmacUpdate),
  52268. TEST_DECL(test_wc_CmacFinal),
  52269. TEST_DECL(test_wc_AesCmacGenerate),
  52270. TEST_DECL(test_wc_AesGcmStream),
  52271. TEST_DECL(test_wc_Des3_SetIV),
  52272. TEST_DECL(test_wc_Des3_SetKey),
  52273. TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
  52274. TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
  52275. TEST_DECL(test_wc_Des3_EcbEncrypt),
  52276. TEST_DECL(test_wc_Chacha_SetKey),
  52277. TEST_DECL(test_wc_Chacha_Process),
  52278. TEST_DECL(test_wc_ChaCha20Poly1305_aead),
  52279. TEST_DECL(test_wc_Poly1305SetKey),
  52280. TEST_DECL(test_wc_CamelliaSetKey),
  52281. TEST_DECL(test_wc_CamelliaSetIV),
  52282. TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
  52283. TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
  52284. TEST_DECL(test_wc_Arc4SetKey),
  52285. TEST_DECL(test_wc_Arc4Process),
  52286. TEST_DECL(test_wc_Rc2SetKey),
  52287. TEST_DECL(test_wc_Rc2SetIV),
  52288. TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
  52289. TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
  52290. TEST_DECL(test_wc_AesSetKey),
  52291. TEST_DECL(test_wc_AesSetIV),
  52292. TEST_DECL(test_wc_AesCbcEncryptDecrypt),
  52293. TEST_DECL(test_wc_AesCtrEncryptDecrypt),
  52294. TEST_DECL(test_wc_AesGcmSetKey),
  52295. TEST_DECL(test_wc_AesGcmEncryptDecrypt),
  52296. TEST_DECL(test_wc_GmacSetKey),
  52297. TEST_DECL(test_wc_GmacUpdate),
  52298. TEST_DECL(test_wc_InitRsaKey),
  52299. TEST_DECL(test_wc_RsaPrivateKeyDecode),
  52300. TEST_DECL(test_wc_RsaPublicKeyDecode),
  52301. TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
  52302. TEST_DECL(test_wc_MakeRsaKey),
  52303. TEST_DECL(test_wc_SetKeyUsage),
  52304. TEST_DECL(test_wc_CheckProbablePrime),
  52305. TEST_DECL(test_wc_RsaPSS_Verify),
  52306. TEST_DECL(test_wc_RsaPSS_VerifyCheck),
  52307. TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
  52308. TEST_DECL(test_wc_SetMutexCb),
  52309. TEST_DECL(test_wc_LockMutex_ex),
  52310. TEST_DECL(test_wc_RsaKeyToDer),
  52311. TEST_DECL(test_wc_RsaKeyToPublicDer),
  52312. TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
  52313. TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
  52314. TEST_DECL(test_wc_RsaEncryptSize),
  52315. TEST_DECL(test_wc_RsaSSL_SignVerify),
  52316. TEST_DECL(test_wc_RsaFlattenPublicKey),
  52317. TEST_DECL(test_RsaDecryptBoundsCheck),
  52318. TEST_DECL(test_wc_AesCcmSetKey),
  52319. TEST_DECL(test_wc_AesCcmEncryptDecrypt),
  52320. TEST_DECL(test_wc_InitDsaKey),
  52321. TEST_DECL(test_wc_DsaSignVerify),
  52322. TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
  52323. TEST_DECL(test_wc_MakeDsaKey),
  52324. TEST_DECL(test_wc_DsaKeyToDer),
  52325. TEST_DECL(test_wc_DsaKeyToPublicDer),
  52326. TEST_DECL(test_wc_DsaImportParamsRaw),
  52327. TEST_DECL(test_wc_DsaImportParamsRawCheck),
  52328. TEST_DECL(test_wc_DsaExportParamsRaw),
  52329. TEST_DECL(test_wc_DsaExportKeyRaw),
  52330. TEST_DECL(test_wc_SignatureGetSize_ecc),
  52331. TEST_DECL(test_wc_SignatureGetSize_rsa),
  52332. /*
  52333. * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
  52334. * avoid memory leaks.
  52335. */
  52336. TEST_DECL(test_wolfCrypt_Cleanup),
  52337. #ifdef OPENSSL_EXTRA
  52338. TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
  52339. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
  52340. TEST_DECL(test_wolfSSL_EC),
  52341. TEST_DECL(test_wolfSSL_ECDSA_SIG),
  52342. TEST_DECL(test_ECDSA_size_sign),
  52343. TEST_DECL(test_ED25519),
  52344. TEST_DECL(test_ED448),
  52345. TEST_DECL(test_EC_i2d),
  52346. #endif
  52347. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  52348. !defined(HAVE_SELFTEST) && \
  52349. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  52350. TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
  52351. #endif
  52352. #ifdef HAVE_HASHDRBG
  52353. #ifdef TEST_RESEED_INTERVAL
  52354. TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
  52355. #endif
  52356. TEST_DECL(test_wc_RNG_GenerateBlock),
  52357. #endif
  52358. TEST_DECL(test_get_rand_digit),
  52359. TEST_DECL(test_get_digit_count),
  52360. TEST_DECL(test_mp_cond_copy),
  52361. TEST_DECL(test_mp_rand),
  52362. TEST_DECL(test_get_digit),
  52363. TEST_DECL(test_wc_export_int),
  52364. TEST_DECL(test_wc_InitRngNonce),
  52365. TEST_DECL(test_wc_InitRngNonce_ex),
  52366. TEST_DECL(test_wc_ed25519_make_key),
  52367. TEST_DECL(test_wc_ed25519_init),
  52368. TEST_DECL(test_wc_ed25519_sign_msg),
  52369. TEST_DECL(test_wc_ed25519_import_public),
  52370. TEST_DECL(test_wc_ed25519_import_private_key),
  52371. TEST_DECL(test_wc_ed25519_export),
  52372. TEST_DECL(test_wc_ed25519_size),
  52373. TEST_DECL(test_wc_ed25519_exportKey),
  52374. TEST_DECL(test_wc_Ed25519PublicKeyToDer),
  52375. TEST_DECL(test_wc_curve25519_init),
  52376. TEST_DECL(test_wc_curve25519_size),
  52377. TEST_DECL(test_wc_curve25519_export_key_raw),
  52378. TEST_DECL(test_wc_curve25519_export_key_raw_ex),
  52379. TEST_DECL(test_wc_curve25519_make_key),
  52380. TEST_DECL(test_wc_curve25519_shared_secret_ex),
  52381. TEST_DECL(test_wc_curve25519_make_pub),
  52382. TEST_DECL(test_wc_curve25519_export_public_ex),
  52383. TEST_DECL(test_wc_curve25519_export_private_raw_ex),
  52384. TEST_DECL(test_wc_curve25519_import_private_raw_ex),
  52385. TEST_DECL(test_wc_curve25519_import_private),
  52386. TEST_DECL(test_wc_ed448_make_key),
  52387. TEST_DECL(test_wc_ed448_init),
  52388. TEST_DECL(test_wc_ed448_sign_msg),
  52389. TEST_DECL(test_wc_ed448_import_public),
  52390. TEST_DECL(test_wc_ed448_import_private_key),
  52391. TEST_DECL(test_wc_ed448_export),
  52392. TEST_DECL(test_wc_ed448_size),
  52393. TEST_DECL(test_wc_ed448_exportKey),
  52394. TEST_DECL(test_wc_Ed448PublicKeyToDer),
  52395. TEST_DECL(test_wc_curve448_make_key),
  52396. TEST_DECL(test_wc_curve448_shared_secret_ex),
  52397. TEST_DECL(test_wc_curve448_export_public_ex),
  52398. TEST_DECL(test_wc_curve448_export_private_raw_ex),
  52399. TEST_DECL(test_wc_curve448_export_key_raw),
  52400. TEST_DECL(test_wc_curve448_import_private_raw_ex),
  52401. TEST_DECL(test_wc_curve448_import_private),
  52402. TEST_DECL(test_wc_curve448_init),
  52403. TEST_DECL(test_wc_curve448_size),
  52404. TEST_DECL(test_wc_ecc_make_key),
  52405. TEST_DECL(test_wc_ecc_init),
  52406. TEST_DECL(test_wc_ecc_check_key),
  52407. TEST_DECL(test_wc_ecc_get_generator),
  52408. TEST_DECL(test_wc_ecc_size),
  52409. TEST_DECL(test_wc_ecc_params),
  52410. TEST_DECL(test_wc_ecc_signVerify_hash),
  52411. TEST_DECL(test_wc_ecc_shared_secret),
  52412. TEST_DECL(test_wc_ecc_export_x963),
  52413. TEST_DECL(test_wc_ecc_export_x963_ex),
  52414. TEST_DECL(test_wc_ecc_import_x963),
  52415. TEST_DECL(ecc_import_private_key),
  52416. TEST_DECL(test_wc_ecc_export_private_only),
  52417. TEST_DECL(test_wc_ecc_rs_to_sig),
  52418. TEST_DECL(test_wc_ecc_import_raw),
  52419. TEST_DECL(test_wc_ecc_import_unsigned),
  52420. TEST_DECL(test_wc_ecc_sig_size),
  52421. TEST_DECL(test_wc_ecc_ctx_new),
  52422. TEST_DECL(test_wc_ecc_ctx_reset),
  52423. TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
  52424. TEST_DECL(test_wc_ecc_ctx_set_info),
  52425. TEST_DECL(test_wc_ecc_encryptDecrypt),
  52426. TEST_DECL(test_wc_ecc_del_point),
  52427. TEST_DECL(test_wc_ecc_pointFns),
  52428. TEST_DECL(test_wc_ecc_shared_secret_ssh),
  52429. TEST_DECL(test_wc_ecc_verify_hash_ex),
  52430. TEST_DECL(test_wc_ecc_mulmod),
  52431. TEST_DECL(test_wc_ecc_is_valid_idx),
  52432. TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
  52433. TEST_DECL(test_wc_ecc_sig_size_calc),
  52434. TEST_DECL(test_ToTraditional),
  52435. TEST_DECL(test_wc_EccPrivateKeyToDer),
  52436. TEST_DECL(test_wc_DhPublicKeyDecode),
  52437. TEST_DECL(test_wc_Ed25519KeyToDer),
  52438. TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
  52439. TEST_DECL(test_wc_Ed448KeyToDer),
  52440. TEST_DECL(test_wc_Ed448PrivateKeyToDer),
  52441. TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
  52442. TEST_DECL(test_wc_SetSubjectBuffer),
  52443. TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
  52444. TEST_DECL(test_wc_PKCS7_New),
  52445. TEST_DECL(test_wc_PKCS7_Init),
  52446. TEST_DECL(test_wc_PKCS7_InitWithCert),
  52447. TEST_DECL(test_wc_PKCS7_EncodeData),
  52448. TEST_DECL(test_wc_PKCS7_EncodeSignedData),
  52449. TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
  52450. TEST_DECL(test_wc_PKCS7_VerifySignedData),
  52451. TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
  52452. TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
  52453. TEST_DECL(test_wc_PKCS7_Degenerate),
  52454. TEST_DECL(test_wc_PKCS7_BER),
  52455. TEST_DECL(test_PKCS7_signed_enveloped),
  52456. TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
  52457. TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
  52458. TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
  52459. TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
  52460. TEST_DECL(test_wc_i2d_PKCS12),
  52461. TEST_DECL(test_wolfSSL_CTX_LoadCRL),
  52462. TEST_DECL(test_openssl_FIPS_drbg),
  52463. TEST_DECL(test_wc_CryptoCb),
  52464. TEST_DECL(test_wolfSSL_CTX_StaticMemory),
  52465. TEST_DECL(test_wolfSSL_FIPS_mode),
  52466. #ifdef WOLFSSL_DTLS
  52467. TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
  52468. TEST_DECL(test_wolfSSL_DTLS_fragment_buckets),
  52469. #endif
  52470. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  52471. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  52472. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  52473. TEST_DECL(test_wolfSSL_dtls_stateless_resume),
  52474. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  52475. #ifdef HAVE_MAX_FRAGMENT
  52476. TEST_DECL(test_wolfSSL_dtls_stateless_maxfrag),
  52477. #endif /* HAVE_MAX_FRAGMENT */
  52478. TEST_DECL(test_wolfSSL_dtls_stateless2),
  52479. #if !defined(NO_OLD_TLS)
  52480. TEST_DECL(test_wolfSSL_dtls_stateless_downgrade),
  52481. #endif /* !defined(NO_OLD_TLS) */
  52482. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  52483. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) */
  52484. TEST_DECL(test_WOLFSSL_dtls_version_alert),
  52485. TEST_DECL(test_ForceZero),
  52486. TEST_DECL(test_wolfSSL_Cleanup),
  52487. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  52488. && defined(WOLFSSL_TLS13) && \
  52489. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  52490. TEST_DECL(test_ticket_nonce_malloc),
  52491. #endif
  52492. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  52493. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  52494. !defined(WOLFSSL_NO_CLIENT_AUTH))
  52495. TEST_DECL(test_various_pathlen_chains),
  52496. #endif
  52497. /* If at some point a stub get implemented this test should fail indicating
  52498. * a need to implement a new test case
  52499. */
  52500. TEST_DECL(test_stubs_are_stubs)
  52501. };
  52502. #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
  52503. static void TestSetup(void)
  52504. {
  52505. /* Stub, for now. Add common test setup code here. */
  52506. }
  52507. static void TestCleanup(void)
  52508. {
  52509. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  52510. /* Clear any errors added to the error queue during the test run. */
  52511. wolfSSL_ERR_clear_error();
  52512. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  52513. }
  52514. /* Print out all API test cases with numeric identifier.
  52515. */
  52516. void ApiTest_PrintTestCases(void)
  52517. {
  52518. int i;
  52519. printf("All Test Cases:");
  52520. for (i = 0; i < TEST_CASE_CNT; i++) {
  52521. printf("%3d: %s\n", i + 1, testCases[i].name);
  52522. }
  52523. }
  52524. /* Add test case with index to the list to run.
  52525. *
  52526. * @param [in] idx Index of test case to run starting at 1.
  52527. * @return 0 on success.
  52528. * @return BAD_FUNC_ARG when index is out of range of test case identifiers.
  52529. */
  52530. int ApiTest_RunIdx(int idx)
  52531. {
  52532. if (idx < 1 || idx > TEST_CASE_CNT) {
  52533. printf("Index out of range (1 - %d): %d\n", TEST_CASE_CNT, idx);
  52534. return BAD_FUNC_ARG;
  52535. }
  52536. testAll = 0;
  52537. testCases[idx-1].run = 1;
  52538. return 0;
  52539. }
  52540. /* Add test case with name to the list to run.
  52541. *
  52542. * @param [in] name Name of test case to run.
  52543. * @return 0 on success.
  52544. * @return BAD_FUNC_ARG when name is not a known test case name.
  52545. */
  52546. int ApiTest_RunName(char* name)
  52547. {
  52548. int i;
  52549. for (i = 0; i < TEST_CASE_CNT; i++) {
  52550. if (XSTRCMP(testCases[i].name, name) == 0) {
  52551. testAll = 0;
  52552. testCases[i].run = 1;
  52553. return 0;
  52554. }
  52555. }
  52556. printf("Test case name not found: %s\n", name);
  52557. printf("Use -list to see all test case names.\n");
  52558. return BAD_FUNC_ARG;
  52559. }
  52560. /* Converts the result code to a string.
  52561. *
  52562. * @param [in] res Test result code.
  52563. * @return String describing test result.
  52564. */
  52565. static const char* apitest_res_string(int res)
  52566. {
  52567. const char* str = "invalid result";
  52568. switch (res) {
  52569. case TEST_SUCCESS:
  52570. str = "passed";
  52571. break;
  52572. case TEST_FAIL:
  52573. str = "failed";
  52574. break;
  52575. case TEST_SKIPPED:
  52576. str = "skipped";
  52577. break;
  52578. }
  52579. return str;
  52580. }
  52581. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  52582. static double gettime_secs(void)
  52583. {
  52584. struct timeval tv;
  52585. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  52586. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
  52587. }
  52588. #endif
  52589. void ApiTest(void)
  52590. {
  52591. int i;
  52592. int ret;
  52593. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  52594. double timeDiff;
  52595. #endif
  52596. printf(" Begin API Tests\n");
  52597. fflush(stdout);
  52598. for (i = 0; i < TEST_CASE_CNT; ++i) {
  52599. /* When not testing all cases then skip if not marked for running. */
  52600. if (!testAll && !testCases[i].run) {
  52601. continue;
  52602. }
  52603. TestSetup();
  52604. printf(" %3d: %-52s:", i + 1, testCases[i].name);
  52605. fflush(stdout);
  52606. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  52607. timeDiff = gettime_secs();
  52608. #endif
  52609. ret = testCases[i].func();
  52610. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  52611. timeDiff = gettime_secs() - timeDiff;
  52612. #endif
  52613. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  52614. if (ret != TEST_SKIPPED) {
  52615. printf(" %s (%9.5lf)\n", apitest_res_string(ret), timeDiff);
  52616. }
  52617. else
  52618. #endif
  52619. {
  52620. printf(" %s\n", apitest_res_string(ret));
  52621. }
  52622. fflush(stdout);
  52623. AssertIntNE(ret, TEST_FAIL);
  52624. TestCleanup();
  52625. }
  52626. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  52627. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  52628. wc_ecc_fp_free(); /* free per thread cache */
  52629. #endif
  52630. wolfSSL_Cleanup();
  52631. (void)testDevId;
  52632. printf(" End API Tests\n");
  52633. fflush(stdout);
  52634. }