api.c 1.7 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947369483694936950369513695236953369543695536956369573695836959369603696136962369633696436965369663696736968369693697036971369723697336974369753697636977369783697936980369813698236983369843698536986369873698836989369903699136992369933699436995369963699736998369993700037001370023700337004370053700637007370083700937010370113701237013370143701537016370173701837019370203702137022370233702437025370263702737028370293703037031370323703337034370353703637037370383703937040370413704237043370443704537046370473704837049370503705137052370533705437055370563705737058370593706037061370623706337064370653706637067370683706937070370713707237073370743707537076370773707837079370803708137082370833708437085370863708737088370893709037091370923709337094370953709637097370983709937100371013710237103371043710537106371073710837109371103711137112371133711437115371163711737118371193712037121371223712337124371253712637127371283712937130371313713237133371343713537136371373713837139371403714137142371433714437145371463714737148371493715037151371523715337154371553715637157371583715937160371613716237163371643716537166371673716837169371703717137172371733717437175371763717737178371793718037181371823718337184371853718637187371883718937190371913719237193371943719537196371973719837199372003720137202372033720437205372063720737208372093721037211372123721337214372153721637217372183721937220372213722237223372243722537226372273722837229372303723137232372333723437235372363723737238372393724037241372423724337244372453724637247372483724937250372513725237253372543725537256372573725837259372603726137262372633726437265372663726737268372693727037271372723727337274372753727637277372783727937280372813728237283372843728537286372873728837289372903729137292372933729437295372963729737298372993730037301373023730337304373053730637307373083730937310373113731237313373143731537316373173731837319373203732137322373233732437325373263732737328373293733037331373323733337334373353733637337373383733937340373413734237343373443734537346373473734837349373503735137352373533735437355373563735737358373593736037361373623736337364373653736637367373683736937370373713737237373373743737537376373773737837379373803738137382373833738437385373863738737388373893739037391373923739337394373953739637397373983739937400374013740237403374043740537406374073740837409374103741137412374133741437415374163741737418374193742037421374223742337424374253742637427374283742937430374313743237433374343743537436374373743837439374403744137442374433744437445374463744737448374493745037451374523745337454374553745637457374583745937460374613746237463374643746537466374673746837469374703747137472374733747437475374763747737478374793748037481374823748337484374853748637487374883748937490374913749237493374943749537496374973749837499375003750137502375033750437505375063750737508375093751037511375123751337514375153751637517375183751937520375213752237523375243752537526375273752837529375303753137532375333753437535375363753737538375393754037541375423754337544375453754637547375483754937550375513755237553375543755537556375573755837559375603756137562375633756437565375663756737568375693757037571375723757337574375753757637577375783757937580375813758237583375843758537586375873758837589375903759137592375933759437595375963759737598375993760037601376023760337604376053760637607376083760937610376113761237613376143761537616376173761837619376203762137622376233762437625376263762737628376293763037631376323763337634376353763637637376383763937640376413764237643376443764537646376473764837649376503765137652376533765437655376563765737658376593766037661376623766337664376653766637667376683766937670376713767237673376743767537676376773767837679376803768137682376833768437685376863768737688376893769037691376923769337694376953769637697376983769937700377013770237703377043770537706377073770837709377103771137712377133771437715377163771737718377193772037721377223772337724377253772637727377283772937730377313773237733377343773537736377373773837739377403774137742377433774437745377463774737748377493775037751377523775337754377553775637757377583775937760377613776237763377643776537766377673776837769377703777137772377733777437775377763777737778377793778037781377823778337784377853778637787377883778937790377913779237793377943779537796377973779837799378003780137802378033780437805378063780737808378093781037811378123781337814378153781637817378183781937820378213782237823378243782537826378273782837829378303783137832378333783437835378363783737838378393784037841378423784337844378453784637847378483784937850378513785237853378543785537856378573785837859378603786137862378633786437865378663786737868378693787037871378723787337874378753787637877378783787937880378813788237883378843788537886378873788837889378903789137892378933789437895378963789737898378993790037901379023790337904379053790637907379083790937910379113791237913379143791537916379173791837919379203792137922379233792437925379263792737928379293793037931379323793337934379353793637937379383793937940379413794237943379443794537946379473794837949379503795137952379533795437955379563795737958379593796037961379623796337964379653796637967379683796937970379713797237973379743797537976379773797837979379803798137982379833798437985379863798737988379893799037991379923799337994379953799637997379983799938000380013800238003380043800538006380073800838009380103801138012380133801438015380163801738018380193802038021380223802338024380253802638027380283802938030380313803238033380343803538036380373803838039380403804138042380433804438045380463804738048380493805038051380523805338054380553805638057380583805938060380613806238063380643806538066380673806838069380703807138072380733807438075380763807738078380793808038081380823808338084380853808638087380883808938090380913809238093380943809538096380973809838099381003810138102381033810438105381063810738108381093811038111381123811338114381153811638117381183811938120381213812238123381243812538126381273812838129381303813138132381333813438135381363813738138381393814038141381423814338144381453814638147381483814938150381513815238153381543815538156381573815838159381603816138162381633816438165381663816738168381693817038171381723817338174381753817638177381783817938180381813818238183381843818538186381873818838189381903819138192381933819438195381963819738198381993820038201382023820338204382053820638207382083820938210382113821238213382143821538216382173821838219382203822138222382233822438225382263822738228382293823038231382323823338234382353823638237382383823938240382413824238243382443824538246382473824838249382503825138252382533825438255382563825738258382593826038261382623826338264382653826638267382683826938270382713827238273382743827538276382773827838279382803828138282382833828438285382863828738288382893829038291382923829338294382953829638297382983829938300383013830238303383043830538306383073830838309383103831138312383133831438315383163831738318383193832038321383223832338324383253832638327383283832938330383313833238333383343833538336383373833838339383403834138342383433834438345383463834738348383493835038351383523835338354383553835638357383583835938360383613836238363383643836538366383673836838369383703837138372383733837438375383763837738378383793838038381383823838338384383853838638387383883838938390383913839238393383943839538396383973839838399384003840138402384033840438405384063840738408384093841038411384123841338414384153841638417384183841938420384213842238423384243842538426384273842838429384303843138432384333843438435384363843738438384393844038441384423844338444384453844638447384483844938450384513845238453384543845538456384573845838459384603846138462384633846438465384663846738468384693847038471384723847338474384753847638477384783847938480384813848238483384843848538486384873848838489384903849138492384933849438495384963849738498384993850038501385023850338504385053850638507385083850938510385113851238513385143851538516385173851838519385203852138522385233852438525385263852738528385293853038531385323853338534385353853638537385383853938540385413854238543385443854538546385473854838549385503855138552385533855438555385563855738558385593856038561385623856338564385653856638567385683856938570385713857238573385743857538576385773857838579385803858138582385833858438585385863858738588385893859038591385923859338594385953859638597385983859938600386013860238603386043860538606386073860838609386103861138612386133861438615386163861738618386193862038621386223862338624386253862638627386283862938630386313863238633386343863538636386373863838639386403864138642386433864438645386463864738648386493865038651386523865338654386553865638657386583865938660386613866238663386643866538666386673866838669386703867138672386733867438675386763867738678386793868038681386823868338684386853868638687386883868938690386913869238693386943869538696386973869838699387003870138702387033870438705387063870738708387093871038711387123871338714387153871638717387183871938720387213872238723387243872538726387273872838729387303873138732387333873438735387363873738738387393874038741387423874338744387453874638747387483874938750387513875238753387543875538756387573875838759387603876138762387633876438765387663876738768387693877038771387723877338774387753877638777387783877938780387813878238783387843878538786387873878838789387903879138792387933879438795387963879738798387993880038801388023880338804388053880638807388083880938810388113881238813388143881538816388173881838819388203882138822388233882438825388263882738828388293883038831388323883338834388353883638837388383883938840388413884238843388443884538846388473884838849388503885138852388533885438855388563885738858388593886038861388623886338864388653886638867388683886938870388713887238873388743887538876388773887838879388803888138882388833888438885388863888738888388893889038891388923889338894388953889638897388983889938900389013890238903389043890538906389073890838909389103891138912389133891438915389163891738918389193892038921389223892338924389253892638927389283892938930389313893238933389343893538936389373893838939389403894138942389433894438945389463894738948389493895038951389523895338954389553895638957389583895938960389613896238963389643896538966389673896838969389703897138972389733897438975389763897738978389793898038981389823898338984389853898638987389883898938990389913899238993389943899538996389973899838999390003900139002390033900439005390063900739008390093901039011390123901339014390153901639017390183901939020390213902239023390243902539026390273902839029390303903139032390333903439035390363903739038390393904039041390423904339044390453904639047390483904939050390513905239053390543905539056390573905839059390603906139062390633906439065390663906739068390693907039071390723907339074390753907639077390783907939080390813908239083390843908539086390873908839089390903909139092390933909439095390963909739098390993910039101391023910339104391053910639107391083910939110391113911239113391143911539116391173911839119391203912139122391233912439125391263912739128391293913039131391323913339134391353913639137391383913939140391413914239143391443914539146391473914839149391503915139152391533915439155391563915739158391593916039161391623916339164391653916639167391683916939170391713917239173391743917539176391773917839179391803918139182391833918439185391863918739188391893919039191391923919339194391953919639197391983919939200392013920239203392043920539206392073920839209392103921139212392133921439215392163921739218392193922039221392223922339224392253922639227392283922939230392313923239233392343923539236392373923839239392403924139242392433924439245392463924739248392493925039251392523925339254392553925639257392583925939260392613926239263392643926539266392673926839269392703927139272392733927439275392763927739278392793928039281392823928339284392853928639287392883928939290392913929239293392943929539296392973929839299393003930139302393033930439305393063930739308393093931039311393123931339314393153931639317393183931939320393213932239323393243932539326393273932839329393303933139332393333933439335393363933739338393393934039341393423934339344393453934639347393483934939350393513935239353393543935539356393573935839359393603936139362393633936439365393663936739368393693937039371393723937339374393753937639377393783937939380393813938239383393843938539386393873938839389393903939139392393933939439395393963939739398393993940039401394023940339404394053940639407394083940939410394113941239413394143941539416394173941839419394203942139422394233942439425394263942739428394293943039431394323943339434394353943639437394383943939440394413944239443394443944539446394473944839449394503945139452394533945439455394563945739458394593946039461394623946339464394653946639467394683946939470394713947239473394743947539476394773947839479394803948139482394833948439485394863948739488394893949039491394923949339494394953949639497394983949939500395013950239503395043950539506395073950839509395103951139512395133951439515395163951739518395193952039521395223952339524395253952639527395283952939530395313953239533395343953539536395373953839539395403954139542395433954439545395463954739548395493955039551395523955339554395553955639557395583955939560395613956239563395643956539566395673956839569395703957139572395733957439575395763957739578395793958039581395823958339584395853958639587395883958939590395913959239593395943959539596395973959839599396003960139602396033960439605396063960739608396093961039611396123961339614396153961639617396183961939620396213962239623396243962539626396273962839629396303963139632396333963439635396363963739638396393964039641396423964339644396453964639647396483964939650396513965239653396543965539656396573965839659396603966139662396633966439665396663966739668396693967039671396723967339674396753967639677396783967939680396813968239683396843968539686396873968839689396903969139692396933969439695396963969739698396993970039701397023970339704397053970639707397083970939710397113971239713397143971539716397173971839719397203972139722397233972439725397263972739728397293973039731397323973339734397353973639737397383973939740397413974239743397443974539746397473974839749397503975139752397533975439755397563975739758397593976039761397623976339764397653976639767397683976939770397713977239773397743977539776397773977839779397803978139782397833978439785397863978739788397893979039791397923979339794397953979639797397983979939800398013980239803398043980539806398073980839809398103981139812398133981439815398163981739818398193982039821398223982339824398253982639827398283982939830398313983239833398343983539836398373983839839398403984139842398433984439845398463984739848398493985039851398523985339854398553985639857398583985939860398613986239863398643986539866398673986839869398703987139872398733987439875398763987739878398793988039881398823988339884398853988639887398883988939890398913989239893398943989539896398973989839899399003990139902399033990439905399063990739908399093991039911399123991339914399153991639917399183991939920399213992239923399243992539926399273992839929399303993139932399333993439935399363993739938399393994039941399423994339944399453994639947399483994939950399513995239953399543995539956399573995839959399603996139962399633996439965399663996739968399693997039971399723997339974399753997639977399783997939980399813998239983399843998539986399873998839989399903999139992399933999439995399963999739998399994000040001400024000340004400054000640007400084000940010400114001240013400144001540016400174001840019400204002140022400234002440025400264002740028400294003040031400324003340034400354003640037400384003940040400414004240043400444004540046400474004840049400504005140052400534005440055400564005740058400594006040061400624006340064400654006640067400684006940070400714007240073400744007540076400774007840079400804008140082400834008440085400864008740088400894009040091400924009340094400954009640097400984009940100401014010240103401044010540106401074010840109401104011140112401134011440115401164011740118401194012040121401224012340124401254012640127401284012940130401314013240133401344013540136401374013840139401404014140142401434014440145401464014740148401494015040151401524015340154401554015640157401584015940160401614016240163401644016540166401674016840169401704017140172401734017440175401764017740178401794018040181401824018340184401854018640187401884018940190401914019240193401944019540196401974019840199402004020140202402034020440205402064020740208402094021040211402124021340214402154021640217402184021940220402214022240223402244022540226402274022840229402304023140232402334023440235402364023740238402394024040241402424024340244402454024640247402484024940250402514025240253402544025540256402574025840259402604026140262402634026440265402664026740268402694027040271402724027340274402754027640277402784027940280402814028240283402844028540286402874028840289402904029140292402934029440295402964029740298402994030040301403024030340304403054030640307403084030940310403114031240313403144031540316403174031840319403204032140322403234032440325403264032740328403294033040331403324033340334403354033640337403384033940340403414034240343403444034540346403474034840349403504035140352403534035440355403564035740358403594036040361403624036340364403654036640367403684036940370403714037240373403744037540376403774037840379403804038140382403834038440385403864038740388403894039040391403924039340394403954039640397403984039940400404014040240403404044040540406404074040840409404104041140412404134041440415404164041740418404194042040421404224042340424404254042640427404284042940430404314043240433404344043540436404374043840439404404044140442404434044440445404464044740448404494045040451404524045340454404554045640457404584045940460404614046240463404644046540466404674046840469404704047140472404734047440475404764047740478404794048040481404824048340484404854048640487404884048940490404914049240493404944049540496404974049840499405004050140502405034050440505405064050740508405094051040511405124051340514405154051640517405184051940520405214052240523405244052540526405274052840529405304053140532405334053440535405364053740538405394054040541405424054340544405454054640547405484054940550405514055240553405544055540556405574055840559405604056140562405634056440565405664056740568405694057040571405724057340574405754057640577405784057940580405814058240583405844058540586405874058840589405904059140592405934059440595405964059740598405994060040601406024060340604406054060640607406084060940610406114061240613406144061540616406174061840619406204062140622406234062440625406264062740628406294063040631406324063340634406354063640637406384063940640406414064240643406444064540646406474064840649406504065140652406534065440655406564065740658406594066040661406624066340664406654066640667406684066940670406714067240673406744067540676406774067840679406804068140682406834068440685406864068740688406894069040691406924069340694406954069640697406984069940700407014070240703407044070540706407074070840709407104071140712407134071440715407164071740718407194072040721407224072340724407254072640727407284072940730407314073240733407344073540736407374073840739407404074140742407434074440745407464074740748407494075040751407524075340754407554075640757407584075940760407614076240763407644076540766407674076840769407704077140772407734077440775407764077740778407794078040781407824078340784407854078640787407884078940790407914079240793407944079540796407974079840799408004080140802408034080440805408064080740808408094081040811408124081340814408154081640817408184081940820408214082240823408244082540826408274082840829408304083140832408334083440835408364083740838408394084040841408424084340844408454084640847408484084940850408514085240853408544085540856408574085840859408604086140862408634086440865408664086740868408694087040871408724087340874408754087640877408784087940880408814088240883408844088540886408874088840889408904089140892408934089440895408964089740898408994090040901409024090340904409054090640907409084090940910409114091240913409144091540916409174091840919409204092140922409234092440925409264092740928409294093040931409324093340934409354093640937409384093940940409414094240943409444094540946409474094840949409504095140952409534095440955409564095740958409594096040961409624096340964409654096640967409684096940970409714097240973409744097540976409774097840979409804098140982409834098440985409864098740988409894099040991409924099340994409954099640997409984099941000410014100241003410044100541006410074100841009410104101141012410134101441015410164101741018410194102041021410224102341024410254102641027410284102941030410314103241033410344103541036410374103841039410404104141042410434104441045410464104741048410494105041051410524105341054410554105641057410584105941060410614106241063410644106541066410674106841069410704107141072410734107441075410764107741078410794108041081410824108341084410854108641087410884108941090410914109241093410944109541096410974109841099411004110141102411034110441105411064110741108411094111041111411124111341114411154111641117411184111941120411214112241123411244112541126411274112841129411304113141132411334113441135411364113741138411394114041141411424114341144411454114641147411484114941150411514115241153411544115541156411574115841159411604116141162411634116441165411664116741168411694117041171411724117341174411754117641177411784117941180411814118241183411844118541186411874118841189411904119141192411934119441195411964119741198411994120041201412024120341204412054120641207412084120941210412114121241213412144121541216412174121841219412204122141222412234122441225412264122741228412294123041231412324123341234412354123641237412384123941240412414124241243412444124541246412474124841249412504125141252412534125441255412564125741258412594126041261412624126341264412654126641267412684126941270412714127241273412744127541276412774127841279412804128141282412834128441285412864128741288412894129041291412924129341294412954129641297412984129941300413014130241303413044130541306413074130841309413104131141312413134131441315413164131741318413194132041321413224132341324413254132641327413284132941330413314133241333413344133541336413374133841339413404134141342413434134441345413464134741348413494135041351413524135341354413554135641357413584135941360413614136241363413644136541366413674136841369413704137141372413734137441375413764137741378413794138041381413824138341384413854138641387413884138941390413914139241393413944139541396413974139841399414004140141402414034140441405414064140741408414094141041411414124141341414414154141641417414184141941420414214142241423414244142541426414274142841429414304143141432414334143441435414364143741438414394144041441414424144341444414454144641447414484144941450414514145241453414544145541456414574145841459414604146141462414634146441465414664146741468414694147041471414724147341474414754147641477414784147941480414814148241483414844148541486414874148841489414904149141492414934149441495414964149741498414994150041501415024150341504415054150641507415084150941510415114151241513415144151541516415174151841519415204152141522415234152441525415264152741528415294153041531415324153341534415354153641537415384153941540415414154241543415444154541546415474154841549415504155141552415534155441555415564155741558415594156041561415624156341564415654156641567415684156941570415714157241573415744157541576415774157841579415804158141582415834158441585415864158741588415894159041591415924159341594415954159641597415984159941600416014160241603416044160541606416074160841609416104161141612416134161441615416164161741618416194162041621416224162341624416254162641627416284162941630416314163241633416344163541636416374163841639416404164141642416434164441645416464164741648416494165041651416524165341654416554165641657416584165941660416614166241663416644166541666416674166841669416704167141672416734167441675416764167741678416794168041681416824168341684416854168641687416884168941690416914169241693416944169541696416974169841699417004170141702417034170441705417064170741708417094171041711417124171341714417154171641717417184171941720417214172241723417244172541726417274172841729417304173141732417334173441735417364173741738417394174041741417424174341744417454174641747417484174941750417514175241753417544175541756417574175841759417604176141762417634176441765417664176741768417694177041771417724177341774417754177641777417784177941780417814178241783417844178541786417874178841789417904179141792417934179441795417964179741798417994180041801418024180341804418054180641807418084180941810418114181241813418144181541816418174181841819418204182141822418234182441825418264182741828418294183041831418324183341834418354183641837418384183941840418414184241843418444184541846418474184841849418504185141852418534185441855418564185741858418594186041861418624186341864418654186641867418684186941870418714187241873418744187541876418774187841879418804188141882418834188441885418864188741888418894189041891418924189341894418954189641897418984189941900419014190241903419044190541906419074190841909419104191141912419134191441915419164191741918419194192041921419224192341924419254192641927419284192941930419314193241933419344193541936419374193841939419404194141942419434194441945419464194741948419494195041951419524195341954419554195641957419584195941960419614196241963419644196541966419674196841969419704197141972419734197441975419764197741978419794198041981419824198341984419854198641987419884198941990419914199241993419944199541996419974199841999420004200142002420034200442005420064200742008420094201042011420124201342014420154201642017420184201942020420214202242023420244202542026420274202842029420304203142032420334203442035420364203742038420394204042041420424204342044420454204642047420484204942050420514205242053420544205542056420574205842059420604206142062420634206442065420664206742068420694207042071420724207342074420754207642077420784207942080420814208242083420844208542086420874208842089420904209142092420934209442095420964209742098420994210042101421024210342104421054210642107421084210942110421114211242113421144211542116421174211842119421204212142122421234212442125421264212742128421294213042131421324213342134421354213642137421384213942140421414214242143421444214542146421474214842149421504215142152421534215442155421564215742158421594216042161421624216342164421654216642167421684216942170421714217242173421744217542176421774217842179421804218142182421834218442185421864218742188421894219042191421924219342194421954219642197421984219942200422014220242203422044220542206422074220842209422104221142212422134221442215422164221742218422194222042221422224222342224422254222642227422284222942230422314223242233422344223542236422374223842239422404224142242422434224442245422464224742248422494225042251422524225342254422554225642257422584225942260422614226242263422644226542266422674226842269422704227142272422734227442275422764227742278422794228042281422824228342284422854228642287422884228942290422914229242293422944229542296422974229842299423004230142302423034230442305423064230742308423094231042311423124231342314423154231642317423184231942320423214232242323423244232542326423274232842329423304233142332423334233442335423364233742338423394234042341423424234342344423454234642347423484234942350423514235242353423544235542356423574235842359423604236142362423634236442365423664236742368423694237042371423724237342374423754237642377423784237942380423814238242383423844238542386423874238842389423904239142392423934239442395423964239742398423994240042401424024240342404424054240642407424084240942410424114241242413424144241542416424174241842419424204242142422424234242442425424264242742428424294243042431424324243342434424354243642437424384243942440424414244242443424444244542446424474244842449424504245142452424534245442455424564245742458424594246042461424624246342464424654246642467424684246942470424714247242473424744247542476424774247842479424804248142482424834248442485424864248742488424894249042491424924249342494424954249642497424984249942500425014250242503425044250542506425074250842509425104251142512425134251442515425164251742518425194252042521425224252342524425254252642527425284252942530425314253242533425344253542536425374253842539425404254142542425434254442545425464254742548425494255042551425524255342554425554255642557425584255942560425614256242563425644256542566425674256842569425704257142572425734257442575425764257742578425794258042581425824258342584425854258642587425884258942590425914259242593425944259542596425974259842599426004260142602426034260442605426064260742608426094261042611426124261342614426154261642617426184261942620426214262242623426244262542626426274262842629426304263142632426334263442635426364263742638426394264042641426424264342644426454264642647426484264942650426514265242653426544265542656426574265842659426604266142662426634266442665426664266742668426694267042671426724267342674426754267642677426784267942680426814268242683426844268542686426874268842689426904269142692426934269442695426964269742698426994270042701427024270342704427054270642707427084270942710427114271242713427144271542716427174271842719427204272142722427234272442725427264272742728427294273042731427324273342734427354273642737427384273942740427414274242743427444274542746427474274842749427504275142752427534275442755427564275742758427594276042761427624276342764427654276642767427684276942770427714277242773427744277542776427774277842779427804278142782427834278442785427864278742788427894279042791427924279342794427954279642797427984279942800428014280242803428044280542806428074280842809428104281142812428134281442815428164281742818428194282042821428224282342824428254282642827428284282942830428314283242833428344283542836428374283842839428404284142842428434284442845428464284742848428494285042851428524285342854428554285642857428584285942860428614286242863428644286542866428674286842869428704287142872428734287442875428764287742878428794288042881428824288342884428854288642887428884288942890428914289242893428944289542896428974289842899429004290142902429034290442905429064290742908429094291042911429124291342914429154291642917429184291942920429214292242923429244292542926429274292842929429304293142932429334293442935429364293742938429394294042941429424294342944429454294642947429484294942950429514295242953429544295542956429574295842959429604296142962429634296442965429664296742968429694297042971429724297342974429754297642977429784297942980429814298242983429844298542986429874298842989429904299142992429934299442995429964299742998429994300043001430024300343004430054300643007430084300943010430114301243013430144301543016430174301843019430204302143022430234302443025430264302743028430294303043031430324303343034430354303643037430384303943040430414304243043430444304543046430474304843049430504305143052430534305443055430564305743058430594306043061430624306343064430654306643067430684306943070430714307243073430744307543076430774307843079430804308143082430834308443085430864308743088430894309043091430924309343094430954309643097430984309943100431014310243103431044310543106431074310843109431104311143112431134311443115431164311743118431194312043121431224312343124431254312643127431284312943130431314313243133431344313543136431374313843139431404314143142431434314443145431464314743148431494315043151431524315343154431554315643157431584315943160431614316243163431644316543166431674316843169431704317143172431734317443175431764317743178431794318043181431824318343184431854318643187431884318943190431914319243193431944319543196431974319843199432004320143202432034320443205432064320743208432094321043211432124321343214432154321643217432184321943220432214322243223432244322543226432274322843229432304323143232432334323443235432364323743238432394324043241432424324343244432454324643247432484324943250432514325243253432544325543256432574325843259432604326143262432634326443265432664326743268432694327043271432724327343274432754327643277432784327943280432814328243283432844328543286432874328843289432904329143292432934329443295432964329743298432994330043301433024330343304433054330643307433084330943310433114331243313433144331543316433174331843319433204332143322433234332443325433264332743328433294333043331433324333343334433354333643337433384333943340433414334243343433444334543346433474334843349433504335143352433534335443355433564335743358433594336043361433624336343364433654336643367433684336943370433714337243373433744337543376433774337843379433804338143382433834338443385433864338743388433894339043391433924339343394433954339643397433984339943400434014340243403434044340543406434074340843409434104341143412434134341443415434164341743418434194342043421434224342343424434254342643427434284342943430434314343243433434344343543436434374343843439434404344143442434434344443445434464344743448434494345043451434524345343454434554345643457434584345943460434614346243463434644346543466434674346843469434704347143472434734347443475434764347743478434794348043481434824348343484434854348643487434884348943490434914349243493434944349543496434974349843499435004350143502435034350443505435064350743508435094351043511435124351343514435154351643517435184351943520435214352243523435244352543526435274352843529435304353143532435334353443535435364353743538435394354043541435424354343544435454354643547435484354943550435514355243553435544355543556435574355843559435604356143562435634356443565435664356743568435694357043571435724357343574435754357643577435784357943580435814358243583435844358543586435874358843589435904359143592435934359443595435964359743598435994360043601436024360343604436054360643607436084360943610436114361243613436144361543616436174361843619436204362143622436234362443625436264362743628436294363043631436324363343634436354363643637436384363943640436414364243643436444364543646436474364843649436504365143652436534365443655436564365743658436594366043661436624366343664436654366643667436684366943670436714367243673436744367543676436774367843679436804368143682436834368443685436864368743688436894369043691436924369343694436954369643697436984369943700437014370243703437044370543706437074370843709437104371143712437134371443715437164371743718437194372043721437224372343724437254372643727437284372943730437314373243733437344373543736437374373843739437404374143742437434374443745437464374743748437494375043751437524375343754437554375643757437584375943760437614376243763437644376543766437674376843769437704377143772437734377443775437764377743778437794378043781437824378343784437854378643787437884378943790437914379243793437944379543796437974379843799438004380143802438034380443805438064380743808438094381043811438124381343814438154381643817438184381943820438214382243823438244382543826438274382843829438304383143832438334383443835438364383743838438394384043841438424384343844438454384643847438484384943850438514385243853438544385543856438574385843859438604386143862438634386443865438664386743868438694387043871438724387343874438754387643877438784387943880438814388243883438844388543886438874388843889438904389143892438934389443895438964389743898438994390043901439024390343904439054390643907439084390943910439114391243913439144391543916439174391843919439204392143922439234392443925439264392743928439294393043931439324393343934439354393643937439384393943940439414394243943439444394543946439474394843949439504395143952439534395443955439564395743958439594396043961439624396343964439654396643967439684396943970439714397243973439744397543976439774397843979439804398143982439834398443985439864398743988439894399043991439924399343994439954399643997439984399944000440014400244003440044400544006440074400844009440104401144012440134401444015440164401744018440194402044021440224402344024440254402644027440284402944030440314403244033440344403544036440374403844039440404404144042440434404444045440464404744048440494405044051440524405344054440554405644057440584405944060440614406244063440644406544066440674406844069440704407144072440734407444075440764407744078440794408044081440824408344084440854408644087440884408944090440914409244093440944409544096440974409844099441004410144102441034410444105441064410744108441094411044111441124411344114441154411644117441184411944120441214412244123441244412544126441274412844129441304413144132441334413444135441364413744138441394414044141441424414344144441454414644147441484414944150441514415244153441544415544156441574415844159441604416144162441634416444165441664416744168441694417044171441724417344174441754417644177441784417944180441814418244183441844418544186441874418844189441904419144192441934419444195441964419744198441994420044201442024420344204442054420644207442084420944210442114421244213442144421544216442174421844219442204422144222442234422444225442264422744228442294423044231442324423344234442354423644237442384423944240442414424244243442444424544246442474424844249442504425144252442534425444255442564425744258442594426044261442624426344264442654426644267442684426944270442714427244273442744427544276442774427844279442804428144282442834428444285442864428744288442894429044291442924429344294442954429644297442984429944300443014430244303443044430544306443074430844309443104431144312443134431444315443164431744318443194432044321443224432344324443254432644327443284432944330443314433244333443344433544336443374433844339443404434144342443434434444345443464434744348443494435044351443524435344354443554435644357443584435944360443614436244363443644436544366443674436844369443704437144372443734437444375443764437744378443794438044381443824438344384443854438644387443884438944390443914439244393443944439544396443974439844399444004440144402444034440444405444064440744408444094441044411444124441344414444154441644417444184441944420444214442244423444244442544426444274442844429444304443144432444334443444435444364443744438444394444044441444424444344444444454444644447444484444944450444514445244453444544445544456444574445844459444604446144462444634446444465444664446744468444694447044471444724447344474444754447644477444784447944480444814448244483444844448544486444874448844489444904449144492444934449444495444964449744498444994450044501445024450344504445054450644507445084450944510445114451244513445144451544516445174451844519445204452144522445234452444525445264452744528445294453044531445324453344534445354453644537445384453944540445414454244543445444454544546445474454844549445504455144552445534455444555445564455744558445594456044561445624456344564445654456644567445684456944570445714457244573445744457544576445774457844579445804458144582445834458444585445864458744588445894459044591445924459344594445954459644597445984459944600446014460244603446044460544606446074460844609446104461144612446134461444615446164461744618446194462044621446224462344624446254462644627446284462944630446314463244633446344463544636446374463844639446404464144642446434464444645446464464744648446494465044651446524465344654446554465644657446584465944660446614466244663446644466544666446674466844669446704467144672446734467444675446764467744678446794468044681446824468344684446854468644687446884468944690446914469244693446944469544696446974469844699447004470144702447034470444705447064470744708447094471044711447124471344714447154471644717447184471944720447214472244723447244472544726447274472844729447304473144732447334473444735447364473744738447394474044741447424474344744447454474644747447484474944750447514475244753447544475544756447574475844759447604476144762447634476444765447664476744768447694477044771447724477344774447754477644777447784477944780447814478244783447844478544786447874478844789447904479144792447934479444795447964479744798447994480044801448024480344804448054480644807448084480944810448114481244813448144481544816448174481844819448204482144822448234482444825448264482744828448294483044831448324483344834448354483644837448384483944840448414484244843448444484544846448474484844849448504485144852448534485444855448564485744858448594486044861448624486344864448654486644867448684486944870448714487244873448744487544876448774487844879448804488144882448834488444885448864488744888448894489044891448924489344894448954489644897448984489944900449014490244903449044490544906449074490844909449104491144912449134491444915449164491744918449194492044921449224492344924449254492644927449284492944930449314493244933449344493544936449374493844939449404494144942449434494444945449464494744948449494495044951449524495344954449554495644957449584495944960449614496244963449644496544966449674496844969449704497144972449734497444975449764497744978449794498044981449824498344984449854498644987449884498944990449914499244993449944499544996449974499844999450004500145002450034500445005450064500745008450094501045011450124501345014450154501645017450184501945020450214502245023450244502545026450274502845029450304503145032450334503445035450364503745038450394504045041450424504345044450454504645047450484504945050450514505245053450544505545056450574505845059450604506145062450634506445065450664506745068450694507045071450724507345074450754507645077450784507945080450814508245083450844508545086450874508845089450904509145092450934509445095450964509745098450994510045101451024510345104451054510645107451084510945110451114511245113451144511545116451174511845119451204512145122451234512445125451264512745128451294513045131451324513345134451354513645137451384513945140451414514245143451444514545146451474514845149451504515145152451534515445155451564515745158451594516045161451624516345164451654516645167451684516945170451714517245173451744517545176451774517845179451804518145182451834518445185451864518745188451894519045191451924519345194451954519645197451984519945200452014520245203452044520545206452074520845209452104521145212452134521445215452164521745218452194522045221452224522345224452254522645227452284522945230452314523245233452344523545236452374523845239452404524145242452434524445245452464524745248452494525045251452524525345254452554525645257452584525945260452614526245263452644526545266452674526845269452704527145272452734527445275452764527745278452794528045281452824528345284452854528645287452884528945290452914529245293452944529545296452974529845299453004530145302453034530445305453064530745308453094531045311453124531345314453154531645317453184531945320453214532245323453244532545326453274532845329453304533145332453334533445335453364533745338453394534045341453424534345344453454534645347453484534945350453514535245353453544535545356453574535845359453604536145362453634536445365453664536745368453694537045371453724537345374453754537645377453784537945380453814538245383453844538545386453874538845389453904539145392453934539445395453964539745398453994540045401454024540345404454054540645407454084540945410454114541245413454144541545416454174541845419454204542145422454234542445425454264542745428454294543045431454324543345434454354543645437454384543945440454414544245443454444544545446454474544845449454504545145452454534545445455454564545745458454594546045461454624546345464454654546645467454684546945470454714547245473454744547545476454774547845479454804548145482454834548445485454864548745488454894549045491454924549345494454954549645497454984549945500455014550245503455044550545506455074550845509455104551145512455134551445515455164551745518455194552045521455224552345524455254552645527455284552945530455314553245533455344553545536455374553845539455404554145542455434554445545455464554745548455494555045551455524555345554455554555645557455584555945560455614556245563455644556545566455674556845569455704557145572455734557445575455764557745578455794558045581455824558345584455854558645587455884558945590455914559245593455944559545596455974559845599456004560145602456034560445605456064560745608456094561045611456124561345614456154561645617456184561945620456214562245623456244562545626456274562845629456304563145632456334563445635456364563745638456394564045641456424564345644456454564645647456484564945650456514565245653456544565545656456574565845659456604566145662456634566445665456664566745668456694567045671456724567345674456754567645677456784567945680456814568245683456844568545686456874568845689456904569145692456934569445695456964569745698456994570045701457024570345704457054570645707457084570945710457114571245713457144571545716457174571845719457204572145722457234572445725457264572745728457294573045731457324573345734457354573645737457384573945740457414574245743457444574545746457474574845749457504575145752457534575445755457564575745758457594576045761457624576345764457654576645767457684576945770457714577245773457744577545776457774577845779457804578145782457834578445785457864578745788457894579045791457924579345794457954579645797457984579945800458014580245803458044580545806458074580845809458104581145812458134581445815458164581745818458194582045821458224582345824458254582645827458284582945830458314583245833458344583545836458374583845839458404584145842458434584445845458464584745848458494585045851458524585345854458554585645857458584585945860458614586245863458644586545866458674586845869458704587145872458734587445875458764587745878458794588045881458824588345884458854588645887458884588945890458914589245893458944589545896458974589845899459004590145902459034590445905459064590745908459094591045911459124591345914459154591645917459184591945920459214592245923459244592545926459274592845929459304593145932459334593445935459364593745938459394594045941459424594345944459454594645947459484594945950459514595245953459544595545956459574595845959459604596145962459634596445965459664596745968459694597045971459724597345974459754597645977459784597945980459814598245983459844598545986459874598845989459904599145992459934599445995459964599745998459994600046001460024600346004460054600646007460084600946010460114601246013460144601546016460174601846019460204602146022460234602446025460264602746028460294603046031460324603346034460354603646037460384603946040460414604246043460444604546046460474604846049460504605146052460534605446055460564605746058460594606046061460624606346064460654606646067460684606946070460714607246073460744607546076460774607846079460804608146082460834608446085460864608746088460894609046091460924609346094460954609646097460984609946100461014610246103461044610546106461074610846109461104611146112461134611446115461164611746118461194612046121461224612346124461254612646127461284612946130461314613246133461344613546136461374613846139461404614146142461434614446145461464614746148461494615046151461524615346154461554615646157461584615946160461614616246163461644616546166461674616846169461704617146172461734617446175461764617746178461794618046181461824618346184461854618646187461884618946190461914619246193461944619546196461974619846199462004620146202462034620446205462064620746208462094621046211462124621346214462154621646217462184621946220462214622246223462244622546226462274622846229462304623146232462334623446235462364623746238462394624046241462424624346244462454624646247462484624946250462514625246253462544625546256462574625846259462604626146262462634626446265462664626746268462694627046271462724627346274462754627646277462784627946280462814628246283462844628546286462874628846289462904629146292462934629446295462964629746298462994630046301463024630346304463054630646307463084630946310463114631246313463144631546316463174631846319463204632146322463234632446325463264632746328463294633046331463324633346334463354633646337463384633946340463414634246343463444634546346463474634846349463504635146352463534635446355463564635746358463594636046361463624636346364463654636646367463684636946370463714637246373463744637546376463774637846379463804638146382463834638446385463864638746388463894639046391463924639346394463954639646397463984639946400464014640246403464044640546406464074640846409464104641146412464134641446415464164641746418464194642046421464224642346424464254642646427464284642946430464314643246433464344643546436464374643846439464404644146442464434644446445464464644746448464494645046451464524645346454464554645646457464584645946460464614646246463464644646546466464674646846469464704647146472464734647446475464764647746478464794648046481464824648346484464854648646487464884648946490464914649246493464944649546496464974649846499465004650146502465034650446505465064650746508465094651046511465124651346514465154651646517465184651946520465214652246523465244652546526465274652846529465304653146532465334653446535465364653746538465394654046541465424654346544465454654646547465484654946550465514655246553465544655546556465574655846559465604656146562465634656446565465664656746568465694657046571465724657346574465754657646577465784657946580465814658246583465844658546586465874658846589465904659146592465934659446595465964659746598465994660046601466024660346604466054660646607466084660946610466114661246613466144661546616466174661846619466204662146622466234662446625466264662746628466294663046631466324663346634466354663646637466384663946640466414664246643466444664546646466474664846649466504665146652466534665446655466564665746658466594666046661466624666346664466654666646667466684666946670466714667246673466744667546676466774667846679466804668146682466834668446685466864668746688466894669046691466924669346694466954669646697466984669946700467014670246703467044670546706467074670846709467104671146712467134671446715467164671746718467194672046721467224672346724467254672646727467284672946730467314673246733467344673546736467374673846739467404674146742467434674446745467464674746748467494675046751467524675346754467554675646757467584675946760467614676246763467644676546766467674676846769467704677146772467734677446775467764677746778467794678046781467824678346784467854678646787467884678946790467914679246793467944679546796467974679846799468004680146802468034680446805468064680746808468094681046811468124681346814468154681646817468184681946820468214682246823468244682546826468274682846829468304683146832468334683446835468364683746838468394684046841468424684346844468454684646847468484684946850468514685246853468544685546856468574685846859468604686146862468634686446865468664686746868468694687046871468724687346874468754687646877468784687946880468814688246883468844688546886468874688846889468904689146892468934689446895468964689746898468994690046901469024690346904469054690646907469084690946910469114691246913469144691546916469174691846919469204692146922469234692446925469264692746928469294693046931469324693346934469354693646937469384693946940469414694246943469444694546946469474694846949469504695146952469534695446955469564695746958469594696046961469624696346964469654696646967469684696946970469714697246973469744697546976469774697846979469804698146982469834698446985469864698746988469894699046991469924699346994469954699646997469984699947000470014700247003470044700547006470074700847009470104701147012470134701447015470164701747018470194702047021470224702347024470254702647027470284702947030470314703247033470344703547036470374703847039470404704147042470434704447045470464704747048470494705047051470524705347054470554705647057470584705947060470614706247063470644706547066470674706847069470704707147072470734707447075470764707747078470794708047081470824708347084470854708647087470884708947090470914709247093470944709547096470974709847099471004710147102471034710447105471064710747108471094711047111471124711347114471154711647117471184711947120471214712247123471244712547126471274712847129471304713147132471334713447135471364713747138471394714047141471424714347144471454714647147471484714947150471514715247153471544715547156471574715847159471604716147162471634716447165471664716747168471694717047171471724717347174471754717647177471784717947180471814718247183471844718547186471874718847189471904719147192471934719447195471964719747198471994720047201472024720347204472054720647207472084720947210472114721247213472144721547216472174721847219472204722147222472234722447225472264722747228472294723047231472324723347234472354723647237472384723947240472414724247243472444724547246472474724847249472504725147252472534725447255472564725747258472594726047261472624726347264472654726647267472684726947270472714727247273472744727547276472774727847279472804728147282472834728447285472864728747288472894729047291472924729347294472954729647297472984729947300473014730247303473044730547306473074730847309473104731147312473134731447315473164731747318473194732047321473224732347324473254732647327473284732947330473314733247333473344733547336473374733847339473404734147342473434734447345473464734747348473494735047351473524735347354473554735647357473584735947360473614736247363473644736547366473674736847369473704737147372473734737447375473764737747378473794738047381473824738347384473854738647387473884738947390473914739247393473944739547396473974739847399474004740147402474034740447405474064740747408474094741047411474124741347414474154741647417474184741947420474214742247423474244742547426474274742847429474304743147432474334743447435474364743747438474394744047441474424744347444474454744647447474484744947450474514745247453474544745547456474574745847459474604746147462474634746447465474664746747468474694747047471474724747347474474754747647477474784747947480474814748247483474844748547486474874748847489474904749147492474934749447495474964749747498474994750047501475024750347504475054750647507475084750947510475114751247513475144751547516475174751847519475204752147522475234752447525475264752747528475294753047531475324753347534475354753647537475384753947540475414754247543475444754547546475474754847549475504755147552475534755447555475564755747558475594756047561475624756347564475654756647567475684756947570475714757247573475744757547576475774757847579475804758147582475834758447585475864758747588475894759047591475924759347594475954759647597475984759947600476014760247603476044760547606476074760847609476104761147612476134761447615476164761747618476194762047621476224762347624476254762647627476284762947630476314763247633476344763547636476374763847639476404764147642476434764447645476464764747648476494765047651476524765347654476554765647657476584765947660476614766247663476644766547666476674766847669476704767147672476734767447675476764767747678476794768047681476824768347684476854768647687476884768947690476914769247693476944769547696476974769847699477004770147702477034770447705477064770747708477094771047711477124771347714477154771647717477184771947720477214772247723477244772547726477274772847729477304773147732477334773447735477364773747738477394774047741477424774347744477454774647747477484774947750477514775247753477544775547756477574775847759477604776147762477634776447765477664776747768477694777047771477724777347774477754777647777477784777947780477814778247783477844778547786477874778847789477904779147792477934779447795477964779747798477994780047801478024780347804478054780647807478084780947810478114781247813478144781547816478174781847819478204782147822478234782447825478264782747828478294783047831478324783347834478354783647837478384783947840478414784247843478444784547846478474784847849478504785147852478534785447855478564785747858478594786047861478624786347864478654786647867478684786947870478714787247873478744787547876478774787847879478804788147882478834788447885478864788747888478894789047891478924789347894478954789647897478984789947900479014790247903479044790547906479074790847909479104791147912479134791447915479164791747918479194792047921479224792347924479254792647927479284792947930479314793247933479344793547936479374793847939479404794147942479434794447945479464794747948479494795047951479524795347954479554795647957479584795947960479614796247963479644796547966479674796847969479704797147972479734797447975479764797747978479794798047981479824798347984479854798647987479884798947990479914799247993479944799547996479974799847999480004800148002480034800448005480064800748008480094801048011480124801348014480154801648017480184801948020480214802248023480244802548026480274802848029480304803148032480334803448035480364803748038480394804048041480424804348044480454804648047480484804948050480514805248053480544805548056480574805848059480604806148062480634806448065480664806748068480694807048071480724807348074480754807648077480784807948080480814808248083480844808548086480874808848089480904809148092480934809448095480964809748098480994810048101481024810348104481054810648107481084810948110481114811248113481144811548116481174811848119481204812148122481234812448125481264812748128481294813048131481324813348134481354813648137481384813948140481414814248143481444814548146481474814848149481504815148152481534815448155481564815748158481594816048161481624816348164481654816648167481684816948170481714817248173481744817548176481774817848179481804818148182481834818448185481864818748188481894819048191481924819348194481954819648197481984819948200482014820248203482044820548206482074820848209482104821148212482134821448215482164821748218482194822048221482224822348224482254822648227482284822948230482314823248233482344823548236482374823848239482404824148242482434824448245482464824748248482494825048251482524825348254482554825648257482584825948260482614826248263482644826548266482674826848269482704827148272482734827448275482764827748278482794828048281482824828348284482854828648287482884828948290482914829248293482944829548296482974829848299483004830148302483034830448305483064830748308483094831048311483124831348314483154831648317483184831948320483214832248323483244832548326483274832848329483304833148332483334833448335483364833748338483394834048341483424834348344483454834648347483484834948350483514835248353483544835548356483574835848359483604836148362483634836448365483664836748368483694837048371483724837348374483754837648377483784837948380483814838248383483844838548386483874838848389483904839148392483934839448395483964839748398483994840048401484024840348404484054840648407484084840948410484114841248413484144841548416484174841848419484204842148422484234842448425484264842748428484294843048431484324843348434484354843648437484384843948440484414844248443484444844548446484474844848449484504845148452484534845448455484564845748458484594846048461484624846348464484654846648467484684846948470484714847248473484744847548476484774847848479484804848148482484834848448485484864848748488484894849048491484924849348494484954849648497484984849948500485014850248503485044850548506485074850848509485104851148512485134851448515485164851748518485194852048521485224852348524485254852648527485284852948530485314853248533485344853548536485374853848539485404854148542485434854448545485464854748548485494855048551485524855348554485554855648557485584855948560485614856248563485644856548566485674856848569485704857148572485734857448575485764857748578485794858048581485824858348584485854858648587485884858948590485914859248593485944859548596485974859848599486004860148602486034860448605486064860748608486094861048611486124861348614486154861648617486184861948620486214862248623486244862548626486274862848629486304863148632486334863448635486364863748638486394864048641486424864348644486454864648647486484864948650486514865248653486544865548656486574865848659486604866148662486634866448665486664866748668486694867048671486724867348674486754867648677486784867948680486814868248683486844868548686486874868848689486904869148692486934869448695486964869748698486994870048701487024870348704487054870648707487084870948710487114871248713487144871548716487174871848719487204872148722487234872448725487264872748728487294873048731487324873348734487354873648737487384873948740487414874248743487444874548746487474874848749487504875148752487534875448755487564875748758487594876048761487624876348764487654876648767487684876948770487714877248773487744877548776487774877848779487804878148782487834878448785487864878748788487894879048791487924879348794487954879648797487984879948800488014880248803488044880548806488074880848809488104881148812488134881448815488164881748818488194882048821488224882348824488254882648827488284882948830488314883248833488344883548836488374883848839488404884148842488434884448845488464884748848488494885048851488524885348854488554885648857488584885948860488614886248863488644886548866488674886848869488704887148872488734887448875488764887748878488794888048881488824888348884488854888648887488884888948890488914889248893488944889548896488974889848899489004890148902489034890448905489064890748908489094891048911489124891348914489154891648917489184891948920489214892248923489244892548926489274892848929489304893148932489334893448935489364893748938489394894048941489424894348944489454894648947489484894948950489514895248953489544895548956489574895848959489604896148962489634896448965489664896748968489694897048971489724897348974489754897648977489784897948980489814898248983489844898548986489874898848989489904899148992489934899448995489964899748998489994900049001490024900349004490054900649007490084900949010490114901249013490144901549016490174901849019490204902149022490234902449025490264902749028490294903049031490324903349034490354903649037490384903949040490414904249043490444904549046490474904849049490504905149052490534905449055490564905749058490594906049061490624906349064490654906649067490684906949070490714907249073490744907549076490774907849079490804908149082490834908449085490864908749088490894909049091490924909349094490954909649097490984909949100491014910249103491044910549106491074910849109491104911149112491134911449115491164911749118491194912049121491224912349124491254912649127491284912949130491314913249133491344913549136491374913849139491404914149142491434914449145491464914749148491494915049151491524915349154491554915649157491584915949160491614916249163491644916549166491674916849169491704917149172491734917449175491764917749178491794918049181491824918349184491854918649187491884918949190491914919249193491944919549196491974919849199492004920149202492034920449205492064920749208492094921049211492124921349214492154921649217492184921949220492214922249223492244922549226492274922849229492304923149232492334923449235492364923749238492394924049241492424924349244492454924649247492484924949250492514925249253492544925549256492574925849259492604926149262492634926449265492664926749268492694927049271492724927349274492754927649277492784927949280492814928249283492844928549286492874928849289492904929149292492934929449295492964929749298492994930049301493024930349304493054930649307493084930949310493114931249313493144931549316493174931849319493204932149322493234932449325493264932749328493294933049331493324933349334493354933649337493384933949340493414934249343493444934549346493474934849349493504935149352493534935449355493564935749358493594936049361493624936349364493654936649367493684936949370493714937249373493744937549376493774937849379493804938149382493834938449385493864938749388493894939049391493924939349394493954939649397493984939949400494014940249403494044940549406494074940849409494104941149412494134941449415494164941749418494194942049421494224942349424494254942649427494284942949430494314943249433494344943549436494374943849439494404944149442494434944449445494464944749448494494945049451494524945349454494554945649457494584945949460494614946249463494644946549466494674946849469494704947149472494734947449475494764947749478494794948049481494824948349484494854948649487494884948949490494914949249493494944949549496494974949849499495004950149502495034950449505495064950749508495094951049511495124951349514495154951649517495184951949520495214952249523495244952549526495274952849529495304953149532495334953449535495364953749538495394954049541495424954349544495454954649547495484954949550495514955249553495544955549556495574955849559495604956149562495634956449565495664956749568495694957049571495724957349574495754957649577495784957949580495814958249583495844958549586495874958849589495904959149592495934959449595495964959749598495994960049601496024960349604496054960649607496084960949610496114961249613496144961549616496174961849619496204962149622496234962449625496264962749628496294963049631496324963349634496354963649637496384963949640496414964249643496444964549646496474964849649496504965149652496534965449655496564965749658496594966049661496624966349664496654966649667496684966949670496714967249673496744967549676496774967849679496804968149682496834968449685496864968749688496894969049691496924969349694496954969649697496984969949700497014970249703497044970549706497074970849709497104971149712497134971449715497164971749718497194972049721497224972349724497254972649727497284972949730497314973249733497344973549736497374973849739497404974149742497434974449745497464974749748497494975049751497524975349754497554975649757497584975949760497614976249763497644976549766497674976849769497704977149772497734977449775497764977749778497794978049781497824978349784497854978649787497884978949790497914979249793497944979549796497974979849799498004980149802498034980449805498064980749808498094981049811498124981349814498154981649817498184981949820498214982249823498244982549826498274982849829498304983149832498334983449835498364983749838498394984049841498424984349844498454984649847498484984949850498514985249853498544985549856498574985849859498604986149862498634986449865498664986749868498694987049871498724987349874498754987649877498784987949880498814988249883498844988549886498874988849889498904989149892498934989449895498964989749898498994990049901499024990349904499054990649907499084990949910499114991249913499144991549916499174991849919499204992149922499234992449925499264992749928499294993049931499324993349934499354993649937499384993949940499414994249943499444994549946499474994849949499504995149952499534995449955499564995749958499594996049961499624996349964499654996649967499684996949970499714997249973499744997549976499774997849979499804998149982499834998449985499864998749988499894999049991499924999349994499954999649997499984999950000500015000250003500045000550006500075000850009500105001150012500135001450015500165001750018500195002050021500225002350024500255002650027500285002950030500315003250033500345003550036500375003850039500405004150042500435004450045500465004750048500495005050051500525005350054500555005650057500585005950060500615006250063500645006550066500675006850069500705007150072500735007450075500765007750078500795008050081500825008350084500855008650087500885008950090500915009250093500945009550096500975009850099501005010150102501035010450105501065010750108501095011050111501125011350114501155011650117501185011950120501215012250123501245012550126501275012850129501305013150132501335013450135501365013750138501395014050141501425014350144501455014650147501485014950150501515015250153501545015550156501575015850159501605016150162501635016450165501665016750168501695017050171501725017350174501755017650177501785017950180501815018250183501845018550186501875018850189501905019150192501935019450195501965019750198501995020050201502025020350204502055020650207502085020950210502115021250213502145021550216502175021850219502205022150222502235022450225502265022750228502295023050231502325023350234502355023650237502385023950240502415024250243502445024550246502475024850249502505025150252502535025450255502565025750258502595026050261502625026350264502655026650267502685026950270502715027250273502745027550276502775027850279502805028150282502835028450285502865028750288502895029050291502925029350294502955029650297502985029950300503015030250303503045030550306503075030850309503105031150312503135031450315503165031750318503195032050321503225032350324503255032650327503285032950330503315033250333503345033550336503375033850339503405034150342503435034450345503465034750348503495035050351503525035350354503555035650357503585035950360503615036250363503645036550366503675036850369503705037150372503735037450375503765037750378503795038050381503825038350384503855038650387503885038950390503915039250393503945039550396503975039850399504005040150402504035040450405504065040750408504095041050411504125041350414504155041650417504185041950420504215042250423504245042550426504275042850429504305043150432504335043450435504365043750438504395044050441504425044350444504455044650447504485044950450504515045250453504545045550456504575045850459504605046150462504635046450465504665046750468504695047050471504725047350474504755047650477504785047950480504815048250483504845048550486504875048850489504905049150492504935049450495504965049750498504995050050501505025050350504505055050650507505085050950510505115051250513505145051550516505175051850519505205052150522505235052450525505265052750528505295053050531505325053350534505355053650537505385053950540505415054250543505445054550546505475054850549505505055150552505535055450555505565055750558505595056050561505625056350564505655056650567505685056950570505715057250573505745057550576505775057850579505805058150582505835058450585505865058750588505895059050591505925059350594505955059650597505985059950600506015060250603506045060550606506075060850609506105061150612506135061450615506165061750618506195062050621506225062350624506255062650627506285062950630506315063250633506345063550636506375063850639506405064150642506435064450645506465064750648506495065050651506525065350654506555065650657506585065950660506615066250663506645066550666506675066850669506705067150672506735067450675506765067750678506795068050681506825068350684506855068650687506885068950690506915069250693506945069550696506975069850699507005070150702507035070450705507065070750708507095071050711507125071350714507155071650717507185071950720507215072250723507245072550726507275072850729507305073150732507335073450735507365073750738507395074050741507425074350744507455074650747507485074950750507515075250753507545075550756507575075850759507605076150762507635076450765507665076750768507695077050771507725077350774507755077650777507785077950780507815078250783507845078550786507875078850789507905079150792507935079450795507965079750798507995080050801508025080350804508055080650807508085080950810508115081250813508145081550816508175081850819508205082150822508235082450825508265082750828508295083050831508325083350834508355083650837508385083950840508415084250843508445084550846508475084850849508505085150852508535085450855508565085750858508595086050861508625086350864508655086650867508685086950870508715087250873508745087550876508775087850879508805088150882508835088450885508865088750888508895089050891508925089350894508955089650897508985089950900509015090250903509045090550906509075090850909509105091150912509135091450915509165091750918509195092050921509225092350924509255092650927509285092950930509315093250933509345093550936509375093850939509405094150942509435094450945509465094750948509495095050951509525095350954509555095650957509585095950960509615096250963509645096550966509675096850969509705097150972509735097450975509765097750978509795098050981509825098350984509855098650987509885098950990509915099250993509945099550996509975099850999510005100151002510035100451005510065100751008510095101051011510125101351014510155101651017510185101951020510215102251023510245102551026510275102851029510305103151032510335103451035510365103751038510395104051041510425104351044510455104651047510485104951050510515105251053510545105551056510575105851059510605106151062510635106451065510665106751068510695107051071510725107351074510755107651077510785107951080510815108251083510845108551086510875108851089510905109151092510935109451095510965109751098510995110051101511025110351104511055110651107511085110951110511115111251113511145111551116511175111851119511205112151122511235112451125511265112751128511295113051131511325113351134511355113651137511385113951140511415114251143511445114551146511475114851149511505115151152511535115451155511565115751158511595116051161511625116351164511655116651167511685116951170511715117251173511745117551176511775117851179511805118151182511835118451185511865118751188511895119051191511925119351194511955119651197511985119951200512015120251203512045120551206512075120851209512105121151212512135121451215512165121751218512195122051221512225122351224512255122651227512285122951230512315123251233512345123551236512375123851239512405124151242512435124451245512465124751248512495125051251512525125351254512555125651257512585125951260512615126251263512645126551266512675126851269512705127151272512735127451275512765127751278512795128051281512825128351284512855128651287512885128951290512915129251293512945129551296512975129851299513005130151302513035130451305513065130751308513095131051311513125131351314513155131651317513185131951320513215132251323513245132551326513275132851329513305133151332513335133451335513365133751338513395134051341513425134351344513455134651347513485134951350513515135251353513545135551356513575135851359513605136151362513635136451365513665136751368513695137051371513725137351374513755137651377513785137951380513815138251383513845138551386513875138851389513905139151392513935139451395513965139751398513995140051401514025140351404514055140651407514085140951410514115141251413514145141551416514175141851419514205142151422514235142451425514265142751428514295143051431514325143351434514355143651437514385143951440514415144251443514445144551446514475144851449514505145151452514535145451455514565145751458514595146051461514625146351464514655146651467514685146951470514715147251473514745147551476514775147851479514805148151482514835148451485514865148751488514895149051491514925149351494514955149651497514985149951500515015150251503515045150551506515075150851509515105151151512515135151451515515165151751518515195152051521515225152351524515255152651527515285152951530515315153251533515345153551536515375153851539515405154151542515435154451545515465154751548515495155051551515525155351554515555155651557515585155951560515615156251563515645156551566515675156851569515705157151572515735157451575515765157751578515795158051581515825158351584515855158651587515885158951590515915159251593515945159551596515975159851599516005160151602516035160451605516065160751608516095161051611516125161351614516155161651617516185161951620516215162251623516245162551626516275162851629516305163151632516335163451635516365163751638516395164051641516425164351644516455164651647516485164951650516515165251653516545165551656516575165851659516605166151662516635166451665516665166751668516695167051671516725167351674516755167651677516785167951680516815168251683516845168551686516875168851689516905169151692516935169451695516965169751698516995170051701517025170351704517055170651707517085170951710517115171251713517145171551716517175171851719517205172151722517235172451725517265172751728517295173051731517325173351734517355173651737517385173951740517415174251743517445174551746517475174851749517505175151752517535175451755517565175751758517595176051761517625176351764517655176651767517685176951770517715177251773517745177551776517775177851779517805178151782517835178451785517865178751788517895179051791517925179351794517955179651797517985179951800518015180251803518045180551806518075180851809518105181151812518135181451815518165181751818518195182051821518225182351824518255182651827518285182951830518315183251833518345183551836518375183851839518405184151842518435184451845518465184751848518495185051851518525185351854518555185651857518585185951860518615186251863518645186551866518675186851869518705187151872518735187451875518765187751878518795188051881518825188351884518855188651887518885188951890518915189251893518945189551896518975189851899519005190151902519035190451905519065190751908519095191051911519125191351914519155191651917519185191951920519215192251923519245192551926519275192851929519305193151932519335193451935519365193751938519395194051941519425194351944519455194651947519485194951950519515195251953519545195551956519575195851959519605196151962519635196451965519665196751968519695197051971519725197351974519755197651977519785197951980519815198251983519845198551986519875198851989519905199151992519935199451995519965199751998519995200052001520025200352004520055200652007520085200952010520115201252013520145201552016520175201852019520205202152022520235202452025520265202752028520295203052031520325203352034520355203652037520385203952040520415204252043520445204552046520475204852049520505205152052520535205452055520565205752058520595206052061520625206352064520655206652067520685206952070520715207252073520745207552076520775207852079520805208152082520835208452085520865208752088520895209052091520925209352094520955209652097520985209952100521015210252103521045210552106521075210852109521105211152112521135211452115521165211752118521195212052121521225212352124521255212652127521285212952130521315213252133521345213552136521375213852139521405214152142521435214452145521465214752148521495215052151521525215352154521555215652157521585215952160521615216252163521645216552166521675216852169521705217152172521735217452175521765217752178521795218052181521825218352184521855218652187521885218952190521915219252193521945219552196521975219852199522005220152202522035220452205522065220752208522095221052211522125221352214522155221652217522185221952220522215222252223522245222552226522275222852229522305223152232522335223452235522365223752238522395224052241522425224352244522455224652247522485224952250522515225252253522545225552256522575225852259522605226152262522635226452265522665226752268522695227052271522725227352274522755227652277522785227952280522815228252283522845228552286522875228852289522905229152292522935229452295522965229752298522995230052301523025230352304523055230652307523085230952310523115231252313523145231552316523175231852319523205232152322523235232452325523265232752328523295233052331523325233352334523355233652337523385233952340523415234252343523445234552346523475234852349523505235152352523535235452355523565235752358523595236052361523625236352364523655236652367523685236952370523715237252373523745237552376523775237852379523805238152382523835238452385523865238752388523895239052391523925239352394523955239652397523985239952400524015240252403524045240552406524075240852409524105241152412524135241452415524165241752418524195242052421524225242352424524255242652427524285242952430524315243252433524345243552436524375243852439524405244152442524435244452445524465244752448524495245052451524525245352454524555245652457524585245952460524615246252463524645246552466524675246852469524705247152472524735247452475524765247752478524795248052481524825248352484524855248652487524885248952490524915249252493524945249552496524975249852499525005250152502525035250452505525065250752508525095251052511525125251352514525155251652517525185251952520525215252252523525245252552526525275252852529525305253152532525335253452535525365253752538525395254052541525425254352544525455254652547525485254952550525515255252553525545255552556525575255852559525605256152562525635256452565525665256752568525695257052571525725257352574525755257652577525785257952580525815258252583525845258552586525875258852589525905259152592525935259452595525965259752598525995260052601526025260352604526055260652607526085260952610526115261252613526145261552616526175261852619526205262152622526235262452625526265262752628526295263052631526325263352634526355263652637526385263952640526415264252643526445264552646526475264852649526505265152652526535265452655526565265752658526595266052661526625266352664526655266652667526685266952670526715267252673526745267552676526775267852679526805268152682526835268452685526865268752688526895269052691526925269352694526955269652697526985269952700527015270252703527045270552706527075270852709527105271152712527135271452715527165271752718527195272052721527225272352724527255272652727527285272952730527315273252733527345273552736527375273852739527405274152742527435274452745527465274752748527495275052751527525275352754527555275652757527585275952760527615276252763527645276552766527675276852769527705277152772527735277452775527765277752778527795278052781527825278352784527855278652787527885278952790527915279252793527945279552796527975279852799528005280152802528035280452805528065280752808528095281052811528125281352814528155281652817528185281952820528215282252823528245282552826528275282852829528305283152832528335283452835528365283752838528395284052841528425284352844528455284652847528485284952850528515285252853528545285552856528575285852859528605286152862528635286452865528665286752868528695287052871528725287352874528755287652877528785287952880528815288252883528845288552886528875288852889528905289152892528935289452895528965289752898528995290052901529025290352904529055290652907529085290952910529115291252913529145291552916529175291852919529205292152922529235292452925529265292752928529295293052931529325293352934529355293652937529385293952940529415294252943529445294552946529475294852949529505295152952529535295452955529565295752958529595296052961529625296352964529655296652967529685296952970529715297252973529745297552976529775297852979529805298152982529835298452985529865298752988529895299052991529925299352994529955299652997529985299953000530015300253003530045300553006530075300853009530105301153012530135301453015530165301753018530195302053021530225302353024530255302653027530285302953030530315303253033530345303553036530375303853039530405304153042530435304453045530465304753048530495305053051530525305353054530555305653057530585305953060530615306253063530645306553066530675306853069530705307153072530735307453075530765307753078530795308053081530825308353084530855308653087530885308953090530915309253093530945309553096530975309853099531005310153102531035310453105531065310753108531095311053111531125311353114531155311653117531185311953120531215312253123531245312553126531275312853129531305313153132531335313453135531365313753138531395314053141531425314353144531455314653147531485314953150531515315253153531545315553156531575315853159531605316153162531635316453165531665316753168531695317053171531725317353174531755317653177531785317953180531815318253183531845318553186531875318853189531905319153192531935319453195531965319753198531995320053201532025320353204532055320653207532085320953210532115321253213532145321553216532175321853219532205322153222532235322453225532265322753228532295323053231532325323353234532355323653237532385323953240532415324253243532445324553246532475324853249532505325153252532535325453255532565325753258532595326053261532625326353264532655326653267532685326953270532715327253273532745327553276532775327853279532805328153282532835328453285532865328753288532895329053291532925329353294532955329653297532985329953300533015330253303533045330553306533075330853309533105331153312533135331453315533165331753318533195332053321533225332353324533255332653327533285332953330533315333253333533345333553336533375333853339533405334153342533435334453345533465334753348533495335053351533525335353354533555335653357533585335953360533615336253363533645336553366533675336853369533705337153372533735337453375533765337753378533795338053381533825338353384533855338653387533885338953390533915339253393533945339553396533975339853399534005340153402534035340453405534065340753408534095341053411534125341353414534155341653417534185341953420534215342253423534245342553426534275342853429534305343153432534335343453435534365343753438534395344053441534425344353444534455344653447534485344953450534515345253453534545345553456534575345853459534605346153462534635346453465534665346753468534695347053471534725347353474534755347653477534785347953480534815348253483534845348553486534875348853489534905349153492534935349453495534965349753498534995350053501535025350353504535055350653507535085350953510535115351253513535145351553516535175351853519535205352153522535235352453525535265352753528535295353053531535325353353534535355353653537535385353953540535415354253543535445354553546535475354853549535505355153552535535355453555535565355753558535595356053561535625356353564535655356653567535685356953570535715357253573535745357553576535775357853579535805358153582535835358453585535865358753588535895359053591535925359353594535955359653597535985359953600536015360253603536045360553606536075360853609536105361153612536135361453615536165361753618536195362053621536225362353624536255362653627536285362953630536315363253633536345363553636536375363853639536405364153642536435364453645536465364753648536495365053651536525365353654536555365653657536585365953660536615366253663536645366553666536675366853669536705367153672536735367453675536765367753678536795368053681536825368353684536855368653687536885368953690536915369253693536945369553696536975369853699537005370153702537035370453705537065370753708537095371053711537125371353714537155371653717537185371953720537215372253723537245372553726537275372853729537305373153732537335373453735537365373753738537395374053741537425374353744537455374653747537485374953750537515375253753537545375553756537575375853759537605376153762537635376453765537665376753768537695377053771537725377353774537755377653777537785377953780537815378253783537845378553786537875378853789537905379153792537935379453795537965379753798537995380053801538025380353804538055380653807538085380953810538115381253813538145381553816538175381853819538205382153822538235382453825538265382753828538295383053831538325383353834538355383653837538385383953840538415384253843538445384553846538475384853849538505385153852538535385453855538565385753858538595386053861538625386353864538655386653867538685386953870538715387253873538745387553876538775387853879538805388153882538835388453885538865388753888538895389053891538925389353894538955389653897538985389953900539015390253903539045390553906539075390853909539105391153912539135391453915539165391753918539195392053921539225392353924539255392653927539285392953930539315393253933539345393553936539375393853939539405394153942539435394453945539465394753948539495395053951539525395353954539555395653957539585395953960539615396253963539645396553966539675396853969539705397153972539735397453975539765397753978539795398053981539825398353984539855398653987539885398953990539915399253993539945399553996539975399853999540005400154002540035400454005540065400754008540095401054011540125401354014540155401654017540185401954020540215402254023540245402554026540275402854029540305403154032540335403454035540365403754038540395404054041540425404354044540455404654047540485404954050540515405254053540545405554056540575405854059540605406154062540635406454065540665406754068540695407054071540725407354074540755407654077540785407954080540815408254083540845408554086540875408854089540905409154092540935409454095540965409754098540995410054101541025410354104541055410654107541085410954110541115411254113541145411554116541175411854119541205412154122541235412454125541265412754128541295413054131541325413354134541355413654137541385413954140541415414254143541445414554146541475414854149541505415154152541535415454155541565415754158541595416054161541625416354164541655416654167541685416954170541715417254173541745417554176541775417854179541805418154182541835418454185541865418754188541895419054191541925419354194541955419654197541985419954200542015420254203542045420554206542075420854209542105421154212542135421454215542165421754218542195422054221542225422354224542255422654227542285422954230542315423254233542345423554236542375423854239542405424154242542435424454245542465424754248542495425054251542525425354254542555425654257542585425954260542615426254263542645426554266542675426854269542705427154272542735427454275542765427754278542795428054281542825428354284542855428654287542885428954290542915429254293542945429554296542975429854299543005430154302543035430454305543065430754308543095431054311543125431354314543155431654317543185431954320543215432254323543245432554326543275432854329543305433154332543335433454335543365433754338543395434054341543425434354344543455434654347543485434954350543515435254353543545435554356543575435854359543605436154362543635436454365543665436754368543695437054371543725437354374543755437654377543785437954380543815438254383543845438554386543875438854389543905439154392543935439454395543965439754398543995440054401544025440354404544055440654407544085440954410544115441254413544145441554416544175441854419544205442154422544235442454425544265442754428544295443054431544325443354434544355443654437544385443954440544415444254443544445444554446544475444854449544505445154452544535445454455544565445754458544595446054461544625446354464544655446654467544685446954470544715447254473544745447554476544775447854479544805448154482544835448454485544865448754488544895449054491544925449354494544955449654497544985449954500545015450254503545045450554506545075450854509545105451154512545135451454515545165451754518545195452054521545225452354524545255452654527545285452954530545315453254533545345453554536545375453854539545405454154542545435454454545545465454754548545495455054551545525455354554545555455654557545585455954560545615456254563545645456554566545675456854569545705457154572545735457454575545765457754578545795458054581545825458354584545855458654587545885458954590545915459254593545945459554596545975459854599546005460154602546035460454605546065460754608546095461054611546125461354614546155461654617546185461954620546215462254623546245462554626546275462854629546305463154632546335463454635546365463754638546395464054641546425464354644546455464654647546485464954650546515465254653546545465554656546575465854659546605466154662546635466454665546665466754668546695467054671546725467354674546755467654677546785467954680546815468254683546845468554686546875468854689546905469154692546935469454695546965469754698546995470054701547025470354704547055470654707547085470954710547115471254713547145471554716547175471854719547205472154722547235472454725547265472754728547295473054731547325473354734547355473654737547385473954740547415474254743547445474554746547475474854749547505475154752547535475454755547565475754758547595476054761547625476354764547655476654767547685476954770547715477254773547745477554776547775477854779547805478154782547835478454785547865478754788547895479054791547925479354794547955479654797547985479954800548015480254803548045480554806548075480854809548105481154812548135481454815548165481754818548195482054821548225482354824548255482654827548285482954830548315483254833548345483554836548375483854839548405484154842548435484454845548465484754848548495485054851548525485354854548555485654857548585485954860548615486254863548645486554866548675486854869548705487154872548735487454875548765487754878548795488054881548825488354884548855488654887548885488954890548915489254893548945489554896548975489854899549005490154902549035490454905549065490754908549095491054911549125491354914549155491654917549185491954920549215492254923549245492554926549275492854929549305493154932549335493454935549365493754938549395494054941549425494354944549455494654947549485494954950549515495254953549545495554956549575495854959549605496154962549635496454965549665496754968549695497054971549725497354974549755497654977549785497954980549815498254983549845498554986549875498854989549905499154992549935499454995549965499754998549995500055001550025500355004550055500655007550085500955010550115501255013550145501555016550175501855019550205502155022550235502455025550265502755028550295503055031550325503355034550355503655037550385503955040550415504255043550445504555046550475504855049550505505155052550535505455055550565505755058550595506055061550625506355064550655506655067550685506955070550715507255073550745507555076550775507855079550805508155082550835508455085550865508755088550895509055091550925509355094550955509655097550985509955100551015510255103551045510555106551075510855109551105511155112551135511455115551165511755118551195512055121551225512355124551255512655127551285512955130551315513255133551345513555136551375513855139551405514155142551435514455145551465514755148551495515055151551525515355154551555515655157551585515955160551615516255163551645516555166551675516855169551705517155172551735517455175551765517755178551795518055181551825518355184551855518655187551885518955190551915519255193551945519555196551975519855199552005520155202552035520455205552065520755208552095521055211552125521355214552155521655217552185521955220552215522255223552245522555226552275522855229552305523155232552335523455235552365523755238552395524055241552425524355244552455524655247552485524955250552515525255253552545525555256552575525855259552605526155262552635526455265552665526755268552695527055271552725527355274552755527655277552785527955280552815528255283552845528555286552875528855289552905529155292552935529455295552965529755298552995530055301553025530355304553055530655307553085530955310553115531255313553145531555316553175531855319553205532155322553235532455325553265532755328553295533055331553325533355334553355533655337553385533955340553415534255343553445534555346553475534855349553505535155352553535535455355553565535755358553595536055361553625536355364553655536655367553685536955370553715537255373553745537555376553775537855379553805538155382553835538455385553865538755388553895539055391553925539355394553955539655397553985539955400554015540255403554045540555406554075540855409554105541155412554135541455415554165541755418554195542055421554225542355424554255542655427554285542955430554315543255433554345543555436554375543855439554405544155442554435544455445554465544755448554495545055451554525545355454554555545655457554585545955460554615546255463554645546555466554675546855469554705547155472554735547455475554765547755478554795548055481554825548355484554855548655487554885548955490554915549255493554945549555496554975549855499555005550155502555035550455505555065550755508555095551055511555125551355514555155551655517555185551955520555215552255523555245552555526555275552855529555305553155532555335553455535555365553755538555395554055541555425554355544555455554655547555485554955550555515555255553555545555555556555575555855559555605556155562555635556455565555665556755568555695557055571555725557355574555755557655577555785557955580555815558255583555845558555586555875558855589555905559155592555935559455595555965559755598555995560055601556025560355604556055560655607556085560955610556115561255613556145561555616556175561855619556205562155622556235562455625556265562755628556295563055631556325563355634556355563655637556385563955640556415564255643556445564555646556475564855649556505565155652556535565455655556565565755658556595566055661556625566355664556655566655667556685566955670556715567255673556745567555676556775567855679556805568155682556835568455685556865568755688556895569055691556925569355694556955569655697556985569955700557015570255703557045570555706557075570855709557105571155712557135571455715557165571755718557195572055721557225572355724557255572655727557285572955730557315573255733557345573555736557375573855739557405574155742557435574455745557465574755748557495575055751557525575355754557555575655757557585575955760557615576255763557645576555766557675576855769557705577155772557735577455775557765577755778557795578055781557825578355784557855578655787557885578955790557915579255793557945579555796557975579855799558005580155802558035580455805558065580755808558095581055811558125581355814558155581655817558185581955820558215582255823558245582555826558275582855829558305583155832558335583455835558365583755838558395584055841558425584355844558455584655847558485584955850558515585255853558545585555856558575585855859558605586155862558635586455865558665586755868558695587055871558725587355874558755587655877558785587955880558815588255883558845588555886558875588855889558905589155892558935589455895558965589755898558995590055901559025590355904559055590655907559085590955910559115591255913559145591555916559175591855919559205592155922559235592455925559265592755928559295593055931559325593355934559355593655937559385593955940559415594255943559445594555946559475594855949559505595155952559535595455955559565595755958559595596055961559625596355964559655596655967559685596955970559715597255973559745597555976559775597855979559805598155982559835598455985559865598755988559895599055991559925599355994559955599655997559985599956000560015600256003560045600556006560075600856009560105601156012560135601456015560165601756018560195602056021560225602356024560255602656027560285602956030560315603256033560345603556036560375603856039560405604156042560435604456045560465604756048560495605056051560525605356054560555605656057560585605956060560615606256063560645606556066560675606856069560705607156072560735607456075560765607756078560795608056081560825608356084560855608656087560885608956090560915609256093560945609556096560975609856099561005610156102561035610456105561065610756108561095611056111561125611356114561155611656117561185611956120561215612256123561245612556126561275612856129561305613156132561335613456135561365613756138561395614056141561425614356144561455614656147561485614956150561515615256153561545615556156561575615856159561605616156162561635616456165561665616756168561695617056171561725617356174561755617656177561785617956180561815618256183561845618556186561875618856189561905619156192561935619456195561965619756198561995620056201562025620356204562055620656207562085620956210562115621256213562145621556216562175621856219562205622156222562235622456225562265622756228562295623056231562325623356234562355623656237562385623956240562415624256243562445624556246562475624856249562505625156252562535625456255562565625756258562595626056261562625626356264562655626656267562685626956270562715627256273562745627556276562775627856279562805628156282562835628456285562865628756288562895629056291562925629356294562955629656297562985629956300563015630256303563045630556306563075630856309563105631156312563135631456315563165631756318563195632056321563225632356324563255632656327563285632956330563315633256333563345633556336563375633856339563405634156342563435634456345563465634756348563495635056351563525635356354563555635656357563585635956360563615636256363563645636556366563675636856369563705637156372563735637456375563765637756378563795638056381563825638356384563855638656387563885638956390563915639256393563945639556396
  1. /* api.c API unit tests
  2. *
  3. * Copyright (C) 2006-2021 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. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || defined(OPENSSL_ALL))
  250. #include <wolfssl/openssl/ssl.h>
  251. #ifndef NO_ASN
  252. /* for ASN_COMMON_NAME DN_tags enum */
  253. #include <wolfssl/wolfcrypt/asn.h>
  254. #endif
  255. #ifdef HAVE_OCSP
  256. #include <wolfssl/openssl/ocsp.h>
  257. #endif
  258. #endif
  259. #ifdef OPENSSL_EXTRA
  260. #include <wolfssl/openssl/cmac.h>
  261. #include <wolfssl/openssl/x509v3.h>
  262. #include <wolfssl/openssl/asn1.h>
  263. #include <wolfssl/openssl/crypto.h>
  264. #include <wolfssl/openssl/pkcs12.h>
  265. #include <wolfssl/openssl/evp.h>
  266. #include <wolfssl/openssl/dh.h>
  267. #include <wolfssl/openssl/bn.h>
  268. #include <wolfssl/openssl/buffer.h>
  269. #include <wolfssl/openssl/pem.h>
  270. #include <wolfssl/openssl/ec.h>
  271. #include <wolfssl/openssl/engine.h>
  272. #include <wolfssl/openssl/hmac.h>
  273. #include <wolfssl/openssl/objects.h>
  274. #include <wolfssl/openssl/rand.h>
  275. #include <wolfssl/openssl/modes.h>
  276. #include <wolfssl/openssl/fips_rand.h>
  277. #include <wolfssl/openssl/kdf.h>
  278. #ifdef OPENSSL_ALL
  279. #include <wolfssl/openssl/txt_db.h>
  280. #include <wolfssl/openssl/lhash.h>
  281. #endif
  282. #ifndef NO_AES
  283. #include <wolfssl/openssl/aes.h>
  284. #endif
  285. #ifndef NO_DES3
  286. #include <wolfssl/openssl/des.h>
  287. #endif
  288. #ifdef HAVE_ECC
  289. #include <wolfssl/openssl/ecdsa.h>
  290. #endif
  291. #ifdef HAVE_PKCS7
  292. #include <wolfssl/openssl/pkcs7.h>
  293. #endif
  294. #ifdef HAVE_ED25519
  295. #include <wolfssl/openssl/ed25519.h>
  296. #endif
  297. #ifdef HAVE_ED448
  298. #include <wolfssl/openssl/ed448.h>
  299. #endif
  300. #endif /* OPENSSL_EXTRA */
  301. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  302. && !defined(NO_SHA256) && !defined(RC_NO_RNG)
  303. #include <wolfssl/wolfcrypt/srp.h>
  304. #endif
  305. #if (defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)) || \
  306. defined(HAVE_SESSION_TICKET) || (defined(OPENSSL_EXTRA) && \
  307. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)) || \
  308. defined(WOLFSSL_TEST_STATIC_BUILD)
  309. /* for testing SSL_get_peer_cert_chain, or SESSION_TICKET_HINT_DEFAULT,
  310. * or for setting authKeyIdSrc in WOLFSSL_X509 */
  311. #include "wolfssl/internal.h"
  312. #endif
  313. /* force enable test buffers */
  314. #ifndef USE_CERT_BUFFERS_2048
  315. #define USE_CERT_BUFFERS_2048
  316. #endif
  317. #ifndef USE_CERT_BUFFERS_256
  318. #define USE_CERT_BUFFERS_256
  319. #endif
  320. #include <wolfssl/certs_test.h>
  321. typedef struct testVector {
  322. const char* input;
  323. const char* output;
  324. size_t inLen;
  325. size_t outLen;
  326. } testVector;
  327. #if defined(HAVE_PKCS7)
  328. typedef struct {
  329. const byte* content;
  330. word32 contentSz;
  331. int contentOID;
  332. int encryptOID;
  333. int keyWrapOID;
  334. int keyAgreeOID;
  335. byte* cert;
  336. size_t certSz;
  337. byte* privateKey;
  338. word32 privateKeySz;
  339. } pkcs7EnvelopedVector;
  340. #ifndef NO_PKCS7_ENCRYPTED_DATA
  341. typedef struct {
  342. const byte* content;
  343. word32 contentSz;
  344. int contentOID;
  345. int encryptOID;
  346. byte* encryptionKey;
  347. word32 encryptionKeySz;
  348. } pkcs7EncryptedVector;
  349. #endif
  350. #endif /* HAVE_PKCS7 */
  351. /*----------------------------------------------------------------------------*
  352. | Constants
  353. *----------------------------------------------------------------------------*/
  354. #define TEST_SUCCESS (1)
  355. #define TEST_FAIL (0)
  356. #define testingFmt " %s:"
  357. #define resultFmt " %s\n"
  358. static const char* passed = "passed";
  359. static const char* failed = "failed";
  360. #define TEST_STRING "Everyone gets Friday off."
  361. #define TEST_STRING_SZ 25
  362. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  363. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  364. #define TEST_RSA_BITS 1024
  365. #else
  366. #define TEST_RSA_BITS 2048
  367. #endif
  368. #define TEST_RSA_BYTES (TEST_RSA_BITS/8)
  369. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  370. (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT))
  371. static const char* bogusFile =
  372. #ifdef _WIN32
  373. "NUL"
  374. #else
  375. "/dev/null"
  376. #endif
  377. ;
  378. #endif /* !NO_FILESYSTEM && !NO_CERTS && (!NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT) */
  379. enum {
  380. TESTING_RSA = 1,
  381. TESTING_ECC = 2
  382. };
  383. #ifdef WOLFSSL_QNX_CAAM
  384. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  385. static int devId = WOLFSSL_CAAM_DEVID;
  386. #else
  387. static int devId = INVALID_DEVID;
  388. #endif
  389. /*----------------------------------------------------------------------------*
  390. | Setup
  391. *----------------------------------------------------------------------------*/
  392. static int test_wolfSSL_Init(void)
  393. {
  394. int result;
  395. printf(testingFmt, "wolfSSL_Init()");
  396. result = wolfSSL_Init();
  397. printf(resultFmt, result == WOLFSSL_SUCCESS ? passed : failed);
  398. return result;
  399. }
  400. static int test_wolfSSL_Cleanup(void)
  401. {
  402. int result;
  403. printf(testingFmt, "wolfSSL_Cleanup()");
  404. result = wolfSSL_Cleanup();
  405. printf(resultFmt, result == WOLFSSL_SUCCESS ? passed : failed);
  406. return result;
  407. }
  408. /* Initialize the wolfCrypt state.
  409. * POST: 0 success.
  410. */
  411. static int test_wolfCrypt_Init(void)
  412. {
  413. int result;
  414. printf(testingFmt, "wolfCrypt_Init()");
  415. result = wolfCrypt_Init();
  416. printf(resultFmt, result == 0 ? passed : failed);
  417. return result;
  418. } /* END test_wolfCrypt_Init */
  419. /*----------------------------------------------------------------------------*
  420. | Platform dependent function test
  421. *----------------------------------------------------------------------------*/
  422. static int test_fileAccess(void)
  423. {
  424. #if defined(WOLFSSL_TEST_PLATFORMDEPEND) && !defined(NO_FILESYSTEM)
  425. const char *fname[] = {
  426. svrCertFile, svrKeyFile, caCertFile,
  427. eccCertFile, eccKeyFile, eccRsaCertFile,
  428. cliCertFile, cliCertDerFile, cliKeyFile,
  429. dhParamFile,
  430. cliEccKeyFile, cliEccCertFile, caEccCertFile, edCertFile, edKeyFile,
  431. cliEdCertFile, cliEdKeyFile, caEdCertFile,
  432. NULL
  433. };
  434. const char derfile[] = "./certs/server-cert.der";
  435. XFILE f;
  436. size_t sz;
  437. byte *buff;
  438. int i;
  439. printf(testingFmt, "test_fileAccess()");
  440. AssertTrue(XFOPEN("badfilename", "rb") == XBADFILE);
  441. for(i=0; fname[i] != NULL ; i++){
  442. AssertTrue((f = XFOPEN(fname[i], "rb")) != XBADFILE);
  443. XFCLOSE(f);
  444. }
  445. AssertTrue((f = XFOPEN(derfile, "rb")) != XBADFILE);
  446. AssertTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  447. sz = (size_t) XFTELL(f);
  448. XREWIND(f);
  449. AssertTrue(sz == sizeof_server_cert_der_2048);
  450. AssertTrue((buff = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE)) != NULL) ;
  451. AssertTrue(XFREAD(buff, 1, sz, f) == sz);
  452. XMEMCMP(server_cert_der_2048, buff, sz);
  453. printf(resultFmt, passed);
  454. #endif
  455. return WOLFSSL_SUCCESS;
  456. }
  457. /*----------------------------------------------------------------------------*
  458. | Method Allocators
  459. *----------------------------------------------------------------------------*/
  460. static void test_wolfSSL_Method_Allocators(void)
  461. {
  462. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  463. do { \
  464. WOLFSSL_METHOD *method; \
  465. condition(method = allocator()); \
  466. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  467. } while(0)
  468. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  469. TEST_METHOD_ALLOCATOR(a, AssertNotNull)
  470. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  471. TEST_METHOD_ALLOCATOR(a, AssertNull)
  472. #ifndef NO_OLD_TLS
  473. #ifdef WOLFSSL_ALLOW_SSLV3
  474. #ifndef NO_WOLFSSL_SERVER
  475. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  476. #endif
  477. #ifndef NO_WOLFSSL_CLIENT
  478. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  479. #endif
  480. #endif
  481. #ifdef WOLFSSL_ALLOW_TLSV10
  482. #ifndef NO_WOLFSSL_SERVER
  483. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  484. #endif
  485. #ifndef NO_WOLFSSL_CLIENT
  486. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  487. #endif
  488. #endif
  489. #ifndef NO_WOLFSSL_SERVER
  490. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  491. #endif
  492. #ifndef NO_WOLFSSL_CLIENT
  493. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  494. #endif
  495. #endif /* !NO_OLD_TLS */
  496. #ifndef WOLFSSL_NO_TLS12
  497. #ifndef NO_WOLFSSL_SERVER
  498. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  499. #endif
  500. #ifndef NO_WOLFSSL_CLIENT
  501. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  502. #endif
  503. #endif /* !WOLFSSL_NO_TLS12 */
  504. #ifdef WOLFSSL_TLS13
  505. #ifndef NO_WOLFSSL_SERVER
  506. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
  507. #endif
  508. #ifndef NO_WOLFSSL_CLIENT
  509. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
  510. #endif
  511. #endif /* WOLFSSL_TLS13 */
  512. #ifndef NO_WOLFSSL_SERVER
  513. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
  514. #endif
  515. #ifndef NO_WOLFSSL_CLIENT
  516. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  517. #endif
  518. #ifdef WOLFSSL_DTLS
  519. #ifndef NO_OLD_TLS
  520. #ifndef NO_WOLFSSL_SERVER
  521. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  522. #endif
  523. #ifndef NO_WOLFSSL_CLIENT
  524. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  525. #endif
  526. #endif
  527. #ifndef WOLFSSL_NO_TLS12
  528. #ifndef NO_WOLFSSL_SERVER
  529. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  530. #endif
  531. #ifndef NO_WOLFSSL_CLIENT
  532. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  533. #endif
  534. #endif
  535. #endif /* WOLFSSL_DTLS */
  536. #if !defined(NO_OLD_TLS) && defined(OPENSSL_EXTRA)
  537. /* Stubs */
  538. #ifndef NO_WOLFSSL_SERVER
  539. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  540. #endif
  541. #ifndef NO_WOLFSSL_CLIENT
  542. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  543. #endif
  544. #endif
  545. /* Test Either Method (client or server) */
  546. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  547. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_method);
  548. #ifndef NO_OLD_TLS
  549. #ifdef WOLFSSL_ALLOW_TLSV10
  550. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_method);
  551. #endif
  552. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_method);
  553. #endif /* !NO_OLD_TLS */
  554. #ifndef WOLFSSL_NO_TLS12
  555. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_method);
  556. #endif /* !WOLFSSL_NO_TLS12 */
  557. #ifdef WOLFSSL_TLS13
  558. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_method);
  559. #endif /* WOLFSSL_TLS13 */
  560. #ifdef WOLFSSL_DTLS
  561. TEST_VALID_METHOD_ALLOCATOR(wolfDTLS_method);
  562. #ifndef NO_OLD_TLS
  563. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_method);
  564. #endif /* !NO_OLD_TLS */
  565. #ifndef WOLFSSL_NO_TLS12
  566. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_method);
  567. #endif /* !WOLFSSL_NO_TLS12 */
  568. #endif /* WOLFSSL_DTLS */
  569. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  570. }
  571. /*----------------------------------------------------------------------------*
  572. | Context
  573. *----------------------------------------------------------------------------*/
  574. #ifndef NO_WOLFSSL_SERVER
  575. static void test_wolfSSL_CTX_new(WOLFSSL_METHOD *method)
  576. {
  577. WOLFSSL_CTX *ctx;
  578. AssertNull(ctx = wolfSSL_CTX_new(NULL));
  579. AssertNotNull(method);
  580. AssertNotNull(ctx = wolfSSL_CTX_new(method));
  581. wolfSSL_CTX_free(ctx);
  582. }
  583. #endif
  584. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  585. (!defined(NO_RSA) || defined(HAVE_ECC))
  586. static void test_for_double_Free(void)
  587. {
  588. WOLFSSL_CTX* ctx;
  589. WOLFSSL* ssl;
  590. int skipTest = 0;
  591. const char* testCertFile;
  592. const char* testKeyFile;
  593. char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
  594. ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
  595. "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
  596. "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
  597. "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
  598. "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
  599. "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
  600. "NULL-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
  601. "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
  602. "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
  603. "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
  604. ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
  605. "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
  606. ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
  607. "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
  608. "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
  609. "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
  610. "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
  611. "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
  612. "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
  613. "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
  614. "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
  615. "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
  616. "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
  617. "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
  618. "LY1305-OLD:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
  619. "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
  620. "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
  621. "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
  622. "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
  623. #ifndef NO_RSA
  624. testCertFile = svrCertFile;
  625. testKeyFile = svrKeyFile;
  626. #elif defined(HAVE_ECC)
  627. testCertFile = eccCertFile;
  628. testKeyFile = eccKeyFile;
  629. #else
  630. skipTest = 1;
  631. #endif
  632. if (skipTest != 1) {
  633. #ifndef NO_WOLFSSL_SERVER
  634. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  635. AssertNotNull(ctx);
  636. #else
  637. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  638. AssertNotNull(ctx);
  639. #endif
  640. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  641. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  642. ssl = wolfSSL_new(ctx);
  643. AssertNotNull(ssl);
  644. /* First test freeing SSL, then CTX */
  645. wolfSSL_free(ssl);
  646. wolfSSL_CTX_free(ctx);
  647. #ifndef NO_WOLFSSL_CLIENT
  648. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  649. AssertNotNull(ctx);
  650. #else
  651. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  652. AssertNotNull(ctx);
  653. #endif
  654. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  655. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  656. ssl = wolfSSL_new(ctx);
  657. AssertNotNull(ssl);
  658. /* Next test freeing CTX then SSL */
  659. wolfSSL_CTX_free(ctx);
  660. wolfSSL_free(ssl);
  661. #ifndef NO_WOLFSSL_SERVER
  662. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  663. AssertNotNull(ctx);
  664. #else
  665. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  666. AssertNotNull(ctx);
  667. #endif
  668. /* Test setting ciphers at ctx level */
  669. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  670. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  671. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
  672. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  673. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  674. /* only update TLSv13 suites */
  675. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384"));
  676. #endif
  677. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  678. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  679. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  680. /* only update pre-TLSv13 suites */
  681. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-GCM-SHA256"));
  682. #endif
  683. AssertNotNull(ssl = wolfSSL_new(ctx));
  684. wolfSSL_CTX_free(ctx);
  685. wolfSSL_free(ssl);
  686. #ifndef NO_WOLFSSL_CLIENT
  687. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  688. AssertNotNull(ctx);
  689. #else
  690. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  691. AssertNotNull(ctx);
  692. #endif
  693. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  694. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  695. ssl = wolfSSL_new(ctx);
  696. AssertNotNull(ssl);
  697. /* test setting ciphers at SSL level */
  698. AssertTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
  699. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  700. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  701. /* only update TLSv13 suites */
  702. AssertTrue(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"));
  703. #endif
  704. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  705. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  706. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  707. /* only update pre-TLSv13 suites */
  708. AssertTrue(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"));
  709. #endif
  710. wolfSSL_CTX_free(ctx);
  711. wolfSSL_free(ssl);
  712. }
  713. }
  714. #endif
  715. static void test_wolfSSL_CTX_use_certificate_file(void)
  716. {
  717. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  718. WOLFSSL_CTX *ctx;
  719. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  720. /* invalid context */
  721. AssertFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  722. WOLFSSL_FILETYPE_PEM));
  723. /* invalid cert file */
  724. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  725. WOLFSSL_FILETYPE_PEM));
  726. /* invalid cert type */
  727. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  728. #ifdef NO_RSA
  729. /* rsa needed */
  730. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,WOLFSSL_FILETYPE_PEM));
  731. #else
  732. /* success */
  733. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  734. #endif
  735. wolfSSL_CTX_free(ctx);
  736. #endif
  737. }
  738. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  739. static int test_wolfSSL_CTX_use_certificate_ASN1(void)
  740. {
  741. #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
  742. WOLFSSL_CTX* ctx;
  743. int ret;
  744. printf(testingFmt, "wolfSSL_CTX_use_certificate_ASN1()");
  745. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  746. ret = SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
  747. server_cert_der_2048);
  748. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  749. wolfSSL_CTX_free(ctx);
  750. return ret;
  751. #else
  752. return WOLFSSL_SUCCESS;
  753. #endif
  754. }
  755. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  756. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  757. * context using buffer.
  758. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  759. * --enable-testcert flag.
  760. */
  761. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  762. {
  763. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  764. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  765. WOLFSSL_CTX* ctx;
  766. int ret;
  767. printf(testingFmt, "wolfSSL_CTX_use_certificate_buffer()");
  768. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  769. ret = wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  770. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1);
  771. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  772. wolfSSL_CTX_free(ctx);
  773. return ret;
  774. #else
  775. return WOLFSSL_SUCCESS;
  776. #endif
  777. } /*END test_wolfSSL_CTX_use_certificate_buffer*/
  778. static void test_wolfSSL_CTX_use_PrivateKey_file(void)
  779. {
  780. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  781. WOLFSSL_CTX *ctx;
  782. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  783. /* invalid context */
  784. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  785. WOLFSSL_FILETYPE_PEM));
  786. /* invalid key file */
  787. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  788. WOLFSSL_FILETYPE_PEM));
  789. /* invalid key type */
  790. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  791. /* success */
  792. #ifdef NO_RSA
  793. /* rsa needed */
  794. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  795. #else
  796. /* success */
  797. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  798. #endif
  799. wolfSSL_CTX_free(ctx);
  800. #endif
  801. }
  802. /* test both file and buffer versions along with unloading trusted peer certs */
  803. static void test_wolfSSL_CTX_trust_peer_cert(void)
  804. {
  805. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  806. !defined(NO_WOLFSSL_CLIENT)
  807. WOLFSSL_CTX *ctx;
  808. WOLFSSL* ssl;
  809. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  810. AssertNotNull(ssl = wolfSSL_new(ctx));
  811. #if !defined(NO_FILESYSTEM)
  812. /* invalid file */
  813. AssertIntNE(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  814. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  815. AssertIntNE(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  816. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  817. AssertIntNE(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  818. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  819. /* success */
  820. AssertIntEQ(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  821. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  822. /* unload cert */
  823. AssertIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  824. AssertIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  825. /* invalid file */
  826. AssertIntNE(wolfSSL_trust_peer_cert(ssl, NULL,
  827. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  828. AssertIntNE(wolfSSL_trust_peer_cert(ssl, bogusFile,
  829. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  830. AssertIntNE(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  831. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  832. /* success */
  833. AssertIntEQ(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  834. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  835. #ifdef WOLFSSL_LOCAL_X509_STORE
  836. /* unload cert */
  837. AssertIntNE(wolfSSL_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  838. AssertIntEQ(wolfSSL_Unload_trust_peers(ssl), WOLFSSL_SUCCESS);
  839. #endif
  840. #endif
  841. /* Test of loading certs from buffers */
  842. /* invalid buffer */
  843. AssertIntNE(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  844. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  845. /* success */
  846. #ifdef USE_CERT_BUFFERS_1024
  847. AssertIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  848. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  849. #endif
  850. #ifdef USE_CERT_BUFFERS_2048
  851. AssertIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  852. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  853. #endif
  854. /* unload cert */
  855. AssertIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  856. AssertIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  857. wolfSSL_free(ssl);
  858. wolfSSL_CTX_free(ctx);
  859. #endif
  860. }
  861. static void test_wolfSSL_CTX_load_verify_locations(void)
  862. {
  863. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  864. WOLFSSL_CTX *ctx;
  865. #ifndef NO_RSA
  866. WOLFSSL_CERT_MANAGER* cm;
  867. #ifdef PERSIST_CERT_CACHE
  868. int cacheSz;
  869. #endif
  870. #endif
  871. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  872. const char* load_certs_path = "./certs/external";
  873. const char* load_no_certs_path = "./examples";
  874. const char* load_expired_path = "./certs/test/expired";
  875. #endif
  876. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  877. /* invalid arguments */
  878. AssertIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL), WOLFSSL_FAILURE);
  879. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL), WOLFSSL_FAILURE);
  880. /* invalid ca file */
  881. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
  882. WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
  883. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
  884. (defined(WOLFSSL_QT) && \
  885. !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
  886. /* invalid path */
  887. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
  888. WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
  889. #endif
  890. /* load ca cert */
  891. #ifdef NO_RSA
  892. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  893. WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
  894. #else /* Skip the following test without RSA certs. */
  895. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL), WOLFSSL_SUCCESS);
  896. #ifdef PERSIST_CERT_CACHE
  897. /* Get cert cache size */
  898. cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx);
  899. #endif
  900. /* Test unloading CA's */
  901. AssertIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
  902. #ifdef PERSIST_CERT_CACHE
  903. /* Verify no certs (result is less than cacheSz) */
  904. AssertIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  905. #endif
  906. /* load ca cert again */
  907. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL), WOLFSSL_SUCCESS);
  908. /* Test getting CERT_MANAGER */
  909. AssertNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  910. /* Test unloading CA's using CM */
  911. AssertIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  912. #ifdef PERSIST_CERT_CACHE
  913. /* Verify no certs (result is less than cacheSz) */
  914. AssertIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  915. #endif
  916. #endif
  917. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  918. /* Test loading CA certificates using a path */
  919. #ifdef NO_RSA
  920. /* failure here okay since certs in external directory are RSA */
  921. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  922. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  923. #else
  924. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  925. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  926. #endif
  927. /* Test loading path with no files */
  928. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_no_certs_path,
  929. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
  930. /* Test loading expired CA certificates */
  931. #ifdef NO_RSA
  932. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_expired_path,
  933. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  934. WOLFSSL_SUCCESS);
  935. #else
  936. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_expired_path,
  937. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  938. WOLFSSL_SUCCESS);
  939. #endif
  940. /* Test loading CA certificates and ignoring all errors */
  941. #ifdef NO_RSA
  942. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  943. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
  944. #else
  945. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  946. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
  947. #endif
  948. #endif
  949. wolfSSL_CTX_free(ctx);
  950. #endif
  951. }
  952. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  953. static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz, int file_type)
  954. {
  955. int ret;
  956. WOLFSSL_CERT_MANAGER* cm;
  957. cm = wolfSSL_CertManagerNew();
  958. if (cm == NULL) {
  959. printf("test_cm_load_ca failed\n");
  960. return -1;
  961. }
  962. ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
  963. wolfSSL_CertManagerFree(cm);
  964. return ret;
  965. }
  966. static int test_cm_load_ca_file(const char* ca_cert_file)
  967. {
  968. int ret = 0;
  969. byte* cert_buf = NULL;
  970. size_t cert_sz = 0;
  971. #if defined(WOLFSSL_PEM_TO_DER)
  972. DerBuffer* pDer = NULL;
  973. #endif
  974. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  975. if (ret == 0) {
  976. /* normal test */
  977. ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
  978. if (ret == WOLFSSL_SUCCESS) {
  979. /* test including null terminator in length */
  980. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  981. if (tmp == NULL) {
  982. ret = MEMORY_E;
  983. }
  984. else {
  985. cert_buf = tmp;
  986. cert_buf[cert_sz] = '\0';
  987. ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
  988. WOLFSSL_FILETYPE_PEM);
  989. }
  990. }
  991. #if defined(WOLFSSL_PEM_TO_DER)
  992. if (ret == WOLFSSL_SUCCESS) {
  993. /* test loading DER */
  994. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  995. if (ret == 0 && pDer != NULL) {
  996. ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
  997. WOLFSSL_FILETYPE_ASN1);
  998. wc_FreeDer(&pDer);
  999. }
  1000. }
  1001. #endif
  1002. }
  1003. free(cert_buf);
  1004. return ret;
  1005. }
  1006. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  1007. static void test_wolfSSL_CertManagerCheckOCSPResponse(void)
  1008. {
  1009. #if defined(HAVE_OCSP) && !defined(NO_RSA)
  1010. /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
  1011. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  1012. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
  1013. defined(HAVE_LIGHTY)
  1014. WOLFSSL_CERT_MANAGER* cm = NULL;
  1015. /* Raw OCSP response bytes captured using the following setup:
  1016. * - Run responder with
  1017. * openssl ocsp -port 9999 -ndays 9999
  1018. * -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
  1019. * -rsigner certs/ocsp/ocsp-responder-cert.pem
  1020. * -rkey certs/ocsp/ocsp-responder-key.pem
  1021. * -CA certs/ocsp/intermediate1-ca-cert.pem
  1022. * - Run client with
  1023. * openssl ocsp -host 127.0.0.1:9999 -respout resp.out
  1024. * -issuer certs/ocsp/intermediate1-ca-cert.pem
  1025. * -cert certs/ocsp/server1-cert.pem
  1026. * -CAfile certs/ocsp/root-ca-cert.pem -noverify
  1027. * - Copy raw response from Wireshark.
  1028. */
  1029. byte response[] = {
  1030. 0x30, 0x82, 0x07, 0x40, 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, 0x82, 0x07, 0x35, 0x06,
  1031. 0x09, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, 0x07, 0x26, 0x30, 0x82,
  1032. 0x07, 0x22, 0x30, 0x82, 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09,
  1033. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
  1034. 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10,
  1035. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65,
  1036. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53,
  1037. 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67,
  1038. 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04,
  1039. 0x03, 0x0c, 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20,
  1040. 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a,
  1041. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77,
  1042. 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x31,
  1043. 0x30, 0x35, 0x30, 0x33, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30, 0x5a, 0x30, 0x64, 0x30, 0x62, 0x30,
  1044. 0x3a, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0x71, 0x4d,
  1045. 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18,
  1046. 0xda, 0x04, 0x04, 0x14, 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2,
  1047. 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, 0x00, 0x18, 0x0f, 0x32,
  1048. 0x30, 0x32, 0x31, 0x30, 0x35, 0x30, 0x33, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30, 0x5a, 0xa0, 0x11,
  1049. 0x18, 0x0f, 0x32, 0x30, 0x34, 0x38, 0x30, 0x39, 0x31, 0x37, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30,
  1050. 0x5a, 0xa1, 0x23, 0x30, 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30,
  1051. 0x01, 0x02, 0x04, 0x12, 0x04, 0x10, 0x38, 0x31, 0x60, 0x99, 0xc8, 0x05, 0x09, 0x68, 0x1c, 0x33,
  1052. 0x49, 0xea, 0x45, 0x26, 0x2f, 0x6d, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  1053. 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x4d, 0x58, 0xcc, 0x69, 0x42, 0xe2,
  1054. 0x9e, 0x64, 0xf6, 0x57, 0xce, 0xcb, 0x5f, 0x14, 0xaf, 0x08, 0x6c, 0xc1, 0x52, 0x7a, 0x40, 0x0a,
  1055. 0xfd, 0xb6, 0xce, 0xbb, 0x40, 0xf4, 0xb9, 0xa5, 0x88, 0xc7, 0xf3, 0x42, 0x9f, 0xa9, 0x94, 0xbe,
  1056. 0x6e, 0x7e, 0x09, 0x30, 0x9d, 0x0e, 0x10, 0x6f, 0x9c, 0xd9, 0x4c, 0x71, 0x81, 0x41, 0x64, 0x95,
  1057. 0xf5, 0x85, 0x77, 0x94, 0x81, 0x61, 0x88, 0xc8, 0x0b, 0x50, 0xbb, 0x37, 0xc8, 0x86, 0x76, 0xd8,
  1058. 0xa2, 0xed, 0x66, 0x34, 0xfb, 0xe4, 0xe7, 0x09, 0x8c, 0xf5, 0xb5, 0x85, 0xd0, 0x4b, 0xb5, 0xe6,
  1059. 0x23, 0x62, 0xc3, 0xd0, 0xef, 0xf7, 0x42, 0x89, 0x02, 0x80, 0x64, 0xc9, 0xed, 0xdd, 0x7c, 0x8f,
  1060. 0x0d, 0xe7, 0x43, 0x9b, 0x88, 0x1f, 0xb0, 0xfd, 0x24, 0x01, 0xc7, 0x55, 0xc3, 0x73, 0x12, 0x84,
  1061. 0x09, 0x7c, 0x57, 0xa8, 0x5d, 0xab, 0x75, 0x29, 0x5c, 0x36, 0x97, 0x64, 0x40, 0x0b, 0x55, 0x34,
  1062. 0x0a, 0x5d, 0xb1, 0x1b, 0x61, 0x1b, 0xdc, 0xe5, 0x89, 0xdd, 0x92, 0x62, 0x57, 0xa7, 0x52, 0xb4,
  1063. 0x38, 0x9a, 0x48, 0xc8, 0x3a, 0x14, 0xde, 0x69, 0x42, 0xe9, 0x37, 0xa4, 0xe7, 0x2d, 0x00, 0xa7,
  1064. 0x0b, 0x29, 0x18, 0xd5, 0xce, 0xd9, 0x0d, 0xdd, 0xfe, 0xae, 0x86, 0xb3, 0x32, 0x1c, 0xc9, 0x33,
  1065. 0xb0, 0x2b, 0xb7, 0x3c, 0x0d, 0x43, 0xd8, 0x6c, 0xf2, 0xb7, 0xcd, 0x7b, 0xd5, 0x7d, 0xf0, 0xde,
  1066. 0x34, 0x9f, 0x6d, 0x83, 0xb9, 0xd5, 0xed, 0xe3, 0xda, 0x96, 0x40, 0x9e, 0xd6, 0xa6, 0xfd, 0x70,
  1067. 0x80, 0x70, 0x87, 0x61, 0x0f, 0xc5, 0x9f, 0x75, 0xfe, 0x11, 0x78, 0x34, 0xc9, 0x42, 0x16, 0x73,
  1068. 0x46, 0x7b, 0x05, 0x53, 0x28, 0x43, 0xbe, 0xee, 0x88, 0x67, 0x1d, 0xcc, 0x74, 0xa7, 0xb6, 0x58,
  1069. 0x7b, 0x29, 0x68, 0x40, 0xcf, 0xce, 0x7b, 0x19, 0x33, 0x68, 0xa0, 0x82, 0x04, 0xc6, 0x30, 0x82,
  1070. 0x04, 0xc2, 0x30, 0x82, 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02,
  1071. 0x01, 0x04, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  1072. 0x00, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  1073. 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68,
  1074. 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c,
  1075. 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04,
  1076. 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03,
  1077. 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67,
  1078. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, 0x53,
  1079. 0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09,
  1080. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40,
  1081. 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32,
  1082. 0x31, 0x30, 0x32, 0x31, 0x30, 0x31, 0x39, 0x34, 0x39, 0x35, 0x34, 0x5a, 0x17, 0x0d, 0x32, 0x33,
  1083. 0x31, 0x31, 0x30, 0x37, 0x31, 0x39, 0x34, 0x39, 0x35, 0x34, 0x5a, 0x30, 0x81, 0x9e, 0x31, 0x0b,
  1084. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06,
  1085. 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e,
  1086. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74,
  1087. 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c,
  1088. 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
  1089. 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03,
  1090. 0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, 0x43, 0x53,
  1091. 0x50, 0x20, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, 0x1d, 0x06,
  1092. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f,
  1093. 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x82, 0x01, 0x22,
  1094. 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03,
  1095. 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xb8, 0xba, 0x23,
  1096. 0xb4, 0xf6, 0xc3, 0x7b, 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, 0x1e, 0x63, 0xb9, 0x85,
  1097. 0x23, 0x34, 0x50, 0x6d, 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, 0x5c, 0x2d, 0xf7, 0x63,
  1098. 0x88, 0xd1, 0x07, 0x7a, 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, 0x22, 0xb4, 0x94, 0x41,
  1099. 0x38, 0xe2, 0x9d, 0x74, 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, 0xca, 0x3f, 0x46, 0x2b,
  1100. 0xfe, 0xe5, 0x5a, 0x3f, 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, 0xc3, 0xee, 0x42, 0xf8,
  1101. 0x8d, 0xeb, 0x92, 0x95, 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, 0x16, 0x80, 0x90, 0xce,
  1102. 0x24, 0x35, 0x21, 0xc4, 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, 0x0a, 0x5a, 0x4f, 0x4a,
  1103. 0x73, 0x31, 0x50, 0xee, 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, 0x87, 0xb1, 0x99, 0xe2,
  1104. 0x10, 0xa7, 0x06, 0x72, 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, 0x76, 0xf8, 0xe0, 0x4a,
  1105. 0xec, 0xbc, 0x93, 0xf4, 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, 0xb4, 0x90, 0x30, 0xbb,
  1106. 0x17, 0xb0, 0xfe, 0x97, 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, 0x19, 0x12, 0x3c, 0xab,
  1107. 0x82, 0x71, 0x78, 0xff, 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, 0x8c, 0x27, 0xac, 0x11,
  1108. 0xb8, 0xd8, 0x43, 0x49, 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, 0x24, 0x87, 0x17, 0x3b,
  1109. 0xd8, 0x04, 0x65, 0x6c, 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, 0x73, 0x68, 0x26, 0x14,
  1110. 0x87, 0x95, 0xc3, 0x5f, 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, 0x0a, 0x8b, 0x98, 0xf3,
  1111. 0xe3, 0xff, 0x4e, 0x44, 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, 0x39, 0x02, 0x03, 0x01,
  1112. 0x00, 0x01, 0xa3, 0x82, 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, 0x06, 0x03, 0x55, 0x1d,
  1113. 0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14,
  1114. 0x32, 0x67, 0xe1, 0xb1, 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, 0x40, 0x50, 0xb5, 0x46,
  1115. 0x56, 0xb8, 0x30, 0x36, 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x81, 0xbc, 0x30,
  1116. 0x81, 0xb9, 0x80, 0x14, 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, 0x47, 0xa5, 0x38, 0xd7,
  1117. 0xb0, 0x04, 0x82, 0x3a, 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, 0x81, 0x9a, 0x30, 0x81,
  1118. 0x97, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13,
  1119. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67,
  1120. 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65,
  1121. 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07,
  1122. 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
  1123. 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
  1124. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
  1125. 0x72, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  1126. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
  1127. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, 0x13, 0x06, 0x03, 0x55,
  1128. 0x1d, 0x25, 0x04, 0x0c, 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x09,
  1129. 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03,
  1130. 0x82, 0x01, 0x01, 0x00, 0x07, 0xca, 0xa6, 0xa1, 0x9f, 0xbf, 0xaf, 0x92, 0x41, 0x35, 0x66, 0x51,
  1131. 0xac, 0xbc, 0x2c, 0xec, 0xe7, 0x8d, 0x65, 0x7e, 0xe9, 0x40, 0xfe, 0x5a, 0xab, 0x8a, 0x1d, 0x3d,
  1132. 0x13, 0xdb, 0xb4, 0x43, 0x2c, 0x9a, 0x36, 0x98, 0x21, 0xa5, 0xe8, 0xca, 0xa9, 0x4d, 0xfc, 0xe3,
  1133. 0xf7, 0x45, 0x88, 0xcd, 0x33, 0xbf, 0x8a, 0x62, 0x10, 0x2f, 0xb2, 0xb7, 0x04, 0xef, 0x26, 0x43,
  1134. 0x51, 0x1d, 0x43, 0x62, 0x7d, 0x1e, 0x50, 0xc8, 0xd5, 0x98, 0x94, 0x71, 0x8f, 0x3b, 0x23, 0x26,
  1135. 0xf1, 0x71, 0x8e, 0x1e, 0x3d, 0x3f, 0x21, 0xfd, 0xb7, 0x2d, 0x65, 0xe4, 0x07, 0x65, 0xac, 0x3c,
  1136. 0xfc, 0xc0, 0x47, 0xa9, 0x32, 0xf6, 0xda, 0x26, 0x93, 0x10, 0xb2, 0xd1, 0x6d, 0xc8, 0x81, 0x31,
  1137. 0x7c, 0xb0, 0x6b, 0xc5, 0x22, 0x8d, 0xb3, 0xfa, 0xbe, 0x82, 0xea, 0x41, 0x42, 0xc4, 0xc0, 0xef,
  1138. 0xe3, 0x84, 0x0f, 0x6f, 0x9a, 0x03, 0x63, 0xb3, 0x30, 0xe0, 0x31, 0x81, 0x2a, 0x16, 0xb3, 0x47,
  1139. 0xd9, 0x5b, 0x38, 0x93, 0x07, 0xd0, 0x6e, 0x79, 0x52, 0x2c, 0xe5, 0x50, 0x84, 0x79, 0x10, 0xe7,
  1140. 0xf6, 0x31, 0x7a, 0x3e, 0x48, 0xa2, 0x38, 0x21, 0x90, 0x7a, 0xf2, 0x5f, 0x48, 0xa4, 0x46, 0x93,
  1141. 0x87, 0xdd, 0x5c, 0x83, 0x64, 0xea, 0xb5, 0x99, 0xa2, 0xe9, 0x01, 0x40, 0xfe, 0xf0, 0x48, 0x66,
  1142. 0x4f, 0x96, 0xf7, 0x83, 0x52, 0xf8, 0x6d, 0xf8, 0x5f, 0xed, 0x0c, 0xbb, 0xbe, 0xd0, 0x69, 0x10,
  1143. 0x4b, 0x99, 0x8f, 0xf8, 0x61, 0x53, 0x9d, 0x12, 0xca, 0x86, 0xaa, 0xb1, 0x80, 0xb4, 0xa6, 0xc1,
  1144. 0xcb, 0xb7, 0x48, 0xf7, 0x9f, 0x55, 0xb4, 0x6e, 0xab, 0xd3, 0xa1, 0xaa, 0x4b, 0xa7, 0x21, 0x6e,
  1145. 0x16, 0x7f, 0xad, 0xbb, 0xea, 0x0f, 0x41, 0x80, 0x9b, 0x7f, 0xd6, 0x46, 0xa2, 0xc0, 0x61, 0x72,
  1146. 0x59, 0x59, 0xa0, 0x07
  1147. };
  1148. OcspEntry entry[1];
  1149. CertStatus status[1];
  1150. OcspRequest* request;
  1151. byte serial[] = {0x05};
  1152. byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
  1153. byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
  1154. printf(testingFmt, "wolfSSL_CertManagerCheckOCSPResponse()");
  1155. XMEMSET(entry, 0, sizeof(OcspEntry));
  1156. XMEMSET(status, 0, sizeof(CertStatus));
  1157. AssertNotNull(request = wolfSSL_OCSP_REQUEST_new());
  1158. request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  1159. DYNAMIC_TYPE_OCSP_REQUEST);
  1160. AssertNotNull(request->serial);
  1161. request->serialSz = sizeof(serial);
  1162. XMEMCPY(request->serial, serial, sizeof(serial));
  1163. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  1164. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  1165. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1166. AssertIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  1167. AssertIntEQ(wolfSSL_CertManagerLoadCA(cm,
  1168. "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
  1169. /* Response should be valid. */
  1170. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, response,
  1171. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  1172. /* Flip a byte in the request serial number, response should be invalid
  1173. * now. */
  1174. request->serial[0] ^= request->serial[0];
  1175. AssertIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, response,
  1176. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  1177. wolfSSL_OCSP_REQUEST_free(request);
  1178. wolfSSL_CertManagerFree(cm);
  1179. printf(resultFmt, passed);
  1180. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  1181. * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
  1182. #endif /* HAVE_OCSP */
  1183. }
  1184. static int test_wolfSSL_CertManagerLoadCABuffer(void)
  1185. {
  1186. int ret = 0;
  1187. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1188. const char* ca_cert = "./certs/ca-cert.pem";
  1189. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1190. ret = test_cm_load_ca_file(ca_cert);
  1191. #ifdef NO_RSA
  1192. AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
  1193. #else
  1194. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1195. #endif
  1196. ret = test_cm_load_ca_file(ca_expired_cert);
  1197. #ifdef NO_RSA
  1198. AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
  1199. #else
  1200. #if !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  1201. !defined(OPENSSL_COMPATIBLE_DEFAULTS)
  1202. AssertIntEQ(ret, ASN_AFTER_DATE_E);
  1203. #else
  1204. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1205. #endif
  1206. #endif
  1207. #endif
  1208. return ret;
  1209. }
  1210. static void test_wolfSSL_CertManagerGetCerts(void)
  1211. {
  1212. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1213. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1214. defined(WOLFSSL_SIGNER_DER_CERT)
  1215. WOLFSSL_CERT_MANAGER* cm = NULL;
  1216. WOLFSSL_STACK* sk = NULL;
  1217. X509* x509 = NULL;
  1218. X509* cert1 = NULL;
  1219. FILE* file1 = NULL;
  1220. #ifdef DEBUG_WOLFSSL_VERBOSE
  1221. WOLFSSL_BIO* bio = NULL;
  1222. #endif
  1223. int i = 0;
  1224. printf(testingFmt, "wolfSSL_CertManagerGetCerts()");
  1225. AssertNotNull(file1=fopen("./certs/ca-cert.pem", "rb"));
  1226. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  1227. fclose(file1);
  1228. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1229. AssertNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1230. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  1231. "./certs/ca-cert.pem", NULL));
  1232. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1233. for (i = 0; i < sk_X509_num(sk); i++) {
  1234. x509 = sk_X509_value(sk, i);
  1235. AssertIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  1236. #ifdef DEBUG_WOLFSSL_VERBOSE
  1237. bio = BIO_new(wolfSSL_BIO_s_file());
  1238. if (bio != NULL) {
  1239. BIO_set_fp(bio, stdout, BIO_NOCLOSE);
  1240. X509_print(bio, x509);
  1241. BIO_free(bio);
  1242. }
  1243. #endif /* DEBUG_WOLFSSL_VERBOSE */
  1244. }
  1245. wolfSSL_X509_free(cert1);
  1246. sk_X509_pop_free(sk, NULL);
  1247. wolfSSL_CertManagerFree(cm);
  1248. printf(resultFmt, passed);
  1249. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1250. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1251. defined(WOLFSSL_SIGNER_DER_CERT) */
  1252. }
  1253. static int test_wolfSSL_CertManagerSetVerify(void)
  1254. {
  1255. int ret = 0;
  1256. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1257. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1258. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  1259. WOLFSSL_CERT_MANAGER* cm;
  1260. int tmp = myVerifyAction;
  1261. const char* ca_cert = "./certs/ca-cert.pem";
  1262. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  1263. cm = wolfSSL_CertManagerNew();
  1264. AssertNotNull(cm);
  1265. wolfSSL_CertManagerSetVerify(cm, myVerify);
  1266. ret = wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL);
  1267. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1268. /* Use the test CB that always accepts certs */
  1269. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  1270. ret = wolfSSL_CertManagerVerify(cm, expiredCert, WOLFSSL_FILETYPE_PEM);
  1271. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1272. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  1273. {
  1274. const char* verifyCert = "./certs/server-cert.pem";
  1275. /* Use the test CB that always fails certs */
  1276. myVerifyAction = VERIFY_FORCE_FAIL;
  1277. ret = wolfSSL_CertManagerVerify(cm, verifyCert, WOLFSSL_FILETYPE_PEM);
  1278. AssertIntEQ(ret, VERIFY_CERT_ERROR);
  1279. }
  1280. #endif
  1281. wolfSSL_CertManagerFree(cm);
  1282. myVerifyAction = tmp;
  1283. #endif
  1284. return ret;
  1285. }
  1286. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  1287. defined(DEBUG_UNIT_TEST_CERTS)
  1288. /* Used when debugging name constraint tests. Not static to allow use in
  1289. * multiple locations with complex define guards. */
  1290. void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
  1291. {
  1292. BIO* out = BIO_new_file(fileName, "wb");
  1293. if (out != NULL) {
  1294. PEM_write_bio_X509(out, x509);
  1295. BIO_free(out);
  1296. }
  1297. }
  1298. void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName)
  1299. {
  1300. BIO* out = BIO_new_file(fileName, "wb");
  1301. if (out != NULL) {
  1302. BIO_write(out, der, derSz);
  1303. BIO_free(out);
  1304. }
  1305. }
  1306. #else
  1307. #define DEBUG_WRITE_CERT_X509(x509, fileName)
  1308. #define DEBUG_WRITE_DER(der, derSz, fileName)
  1309. #endif
  1310. static void test_wolfSSL_CertManagerNameConstraint(void)
  1311. {
  1312. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1313. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1314. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1315. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  1316. !defined(NO_SHA256)
  1317. WOLFSSL_CERT_MANAGER* cm;
  1318. WOLFSSL_EVP_PKEY *priv;
  1319. WOLFSSL_X509_NAME* name;
  1320. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  1321. const char* server_cert = "./certs/test/server-goodcn.pem";
  1322. int i = 0;
  1323. static const byte extNameConsOid[] = {85, 29, 30};
  1324. RsaKey key;
  1325. WC_RNG rng;
  1326. byte *der;
  1327. int derSz;
  1328. word32 idx = 0;
  1329. byte *pt;
  1330. WOLFSSL_X509 *x509, *ca;
  1331. wc_InitRng(&rng);
  1332. /* load in CA private key for signing */
  1333. AssertIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, devId), 0);
  1334. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  1335. sizeof_server_key_der_2048), 0);
  1336. /* get ca certificate then alter it */
  1337. AssertNotNull(der =
  1338. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  1339. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  1340. WOLFSSL_FILETYPE_ASN1));
  1341. AssertNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  1342. XMEMCPY(der, pt, derSz);
  1343. /* find the name constraint extension and alter it */
  1344. pt = der;
  1345. for (i = 0; i < derSz - 3; i++) {
  1346. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  1347. pt += 3;
  1348. break;
  1349. }
  1350. pt++;
  1351. }
  1352. AssertIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  1353. /* go to the length value and set it to 0 */
  1354. while (i < derSz && *pt != 0x81) {
  1355. pt++;
  1356. i++;
  1357. }
  1358. AssertIntNE(i, derSz); /* did not place to alter */
  1359. pt++;
  1360. *pt = 0x00;
  1361. /* resign the altered certificate */
  1362. AssertIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  1363. FOURK_BUF, &key, NULL, &rng)), 0);
  1364. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1365. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1366. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  1367. wolfSSL_CertManagerFree(cm);
  1368. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1369. wolfSSL_X509_free(x509);
  1370. wc_FreeRsaKey(&key);
  1371. wc_FreeRng(&rng);
  1372. /* add email alt name to satisfy constraint */
  1373. pt = (byte*)server_key_der_2048;
  1374. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  1375. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  1376. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1377. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1378. WOLFSSL_FILETYPE_ASN1));
  1379. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  1380. DEBUG_WRITE_DER(der, derSz, "ca.der");
  1381. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1382. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1383. /* Good cert test with proper alt email name */
  1384. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1385. WOLFSSL_FILETYPE_PEM));
  1386. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1387. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1388. AssertNotNull(name = X509_NAME_new());
  1389. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1390. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1391. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1392. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1393. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1394. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  1395. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1396. X509_NAME_free(name);
  1397. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  1398. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1399. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  1400. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1401. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1402. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1403. wolfSSL_X509_free(x509);
  1404. /* Cert with bad alt name list */
  1405. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1406. WOLFSSL_FILETYPE_PEM));
  1407. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1408. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1409. AssertNotNull(name = X509_NAME_new());
  1410. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1411. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1412. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1413. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1414. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1415. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  1416. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1417. X509_NAME_free(name);
  1418. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  1419. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  1420. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1421. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  1422. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1423. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1424. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1425. wolfSSL_CertManagerFree(cm);
  1426. wolfSSL_X509_free(x509);
  1427. wolfSSL_X509_free(ca);
  1428. wolfSSL_EVP_PKEY_free(priv);
  1429. #endif
  1430. }
  1431. static void test_wolfSSL_CertManagerNameConstraint2(void)
  1432. {
  1433. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1434. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1435. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1436. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  1437. const char* ca_cert = "./certs/test/cert-ext-ndir.der";
  1438. const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
  1439. const char* server_cert = "./certs/server-cert.pem";
  1440. WOLFSSL_CERT_MANAGER* cm;
  1441. WOLFSSL_X509 *x509, *ca;
  1442. const unsigned char *der;
  1443. const unsigned char *pt;
  1444. WOLFSSL_EVP_PKEY *priv;
  1445. WOLFSSL_X509_NAME* name;
  1446. int derSz;
  1447. /* C=US*/
  1448. char altName[] = {
  1449. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  1450. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
  1451. };
  1452. /* C=ID */
  1453. char altNameFail[] = {
  1454. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  1455. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
  1456. };
  1457. /* C=US ST=California*/
  1458. char altNameExc[] = {
  1459. 0x30, 0x22,
  1460. 0x31, 0x0B,
  1461. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  1462. 0x31, 0x13,
  1463. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
  1464. 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
  1465. };
  1466. /* load in CA private key for signing */
  1467. pt = ca_key_der_2048;
  1468. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  1469. sizeof_ca_key_der_2048));
  1470. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1471. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1472. WOLFSSL_FILETYPE_ASN1));
  1473. AssertNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  1474. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1475. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1476. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1477. WOLFSSL_FILETYPE_PEM));
  1478. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1479. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1480. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1481. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1482. #else
  1483. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1484. #endif
  1485. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1486. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1487. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1488. /* add in matching DIR alt name and resign */
  1489. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  1490. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1491. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1492. #else
  1493. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1494. #endif
  1495. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1496. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1497. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1498. wolfSSL_X509_free(x509);
  1499. /* check verify fail */
  1500. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1501. WOLFSSL_FILETYPE_PEM));
  1502. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1503. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1504. /* add in miss matching DIR alt name and resign */
  1505. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  1506. ASN_DIR_TYPE);
  1507. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1508. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1509. #else
  1510. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1511. #endif
  1512. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1513. #ifndef WOLFSSL_NO_ASN_STRICT
  1514. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1515. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1516. #else
  1517. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1518. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1519. #endif
  1520. /* check that it still fails if one bad altname and one good altname is in
  1521. * the certificate */
  1522. wolfSSL_X509_free(x509);
  1523. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1524. WOLFSSL_FILETYPE_PEM));
  1525. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1526. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1527. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  1528. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  1529. ASN_DIR_TYPE);
  1530. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1531. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1532. #else
  1533. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1534. #endif
  1535. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1536. #ifndef WOLFSSL_NO_ASN_STRICT
  1537. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1538. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1539. #else
  1540. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1541. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1542. #endif
  1543. /* check it fails with switching position of bad altname */
  1544. wolfSSL_X509_free(x509);
  1545. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1546. WOLFSSL_FILETYPE_PEM));
  1547. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1548. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1549. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  1550. ASN_DIR_TYPE);
  1551. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  1552. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1553. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1554. #else
  1555. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1556. #endif
  1557. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1558. #ifndef WOLFSSL_NO_ASN_STRICT
  1559. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1560. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1561. #else
  1562. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1563. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1564. #endif
  1565. wolfSSL_CertManagerFree(cm);
  1566. wolfSSL_X509_free(x509);
  1567. wolfSSL_X509_free(ca);
  1568. /* now test with excluded name constraint */
  1569. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1570. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
  1571. WOLFSSL_FILETYPE_ASN1));
  1572. AssertNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  1573. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1574. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1575. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1576. WOLFSSL_FILETYPE_PEM));
  1577. wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
  1578. ASN_DIR_TYPE);
  1579. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1580. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1581. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1582. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1583. #else
  1584. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1585. #endif
  1586. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1587. #ifndef WOLFSSL_NO_ASN_STRICT
  1588. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1589. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1590. #else
  1591. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1592. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1593. #endif
  1594. wolfSSL_CertManagerFree(cm);
  1595. wolfSSL_X509_free(x509);
  1596. wolfSSL_X509_free(ca);
  1597. wolfSSL_EVP_PKEY_free(priv);
  1598. #endif
  1599. }
  1600. static void test_wolfSSL_CertManagerNameConstraint3(void)
  1601. {
  1602. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1603. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1604. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1605. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  1606. !defined(NO_SHA256)
  1607. WOLFSSL_CERT_MANAGER* cm;
  1608. WOLFSSL_EVP_PKEY *priv;
  1609. WOLFSSL_X509_NAME* name;
  1610. const char* ca_cert = "./certs/test/cert-ext-mnc.der";
  1611. const char* server_cert = "./certs/test/server-goodcn.pem";
  1612. byte *der;
  1613. int derSz;
  1614. byte *pt;
  1615. WOLFSSL_X509 *x509, *ca;
  1616. pt = (byte*)server_key_der_2048;
  1617. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  1618. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  1619. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1620. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1621. WOLFSSL_FILETYPE_ASN1));
  1622. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  1623. DEBUG_WRITE_DER(der, derSz, "ca.der");
  1624. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1625. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1626. /* check satisfying .wolfssl.com constraint passes */
  1627. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1628. WOLFSSL_FILETYPE_PEM));
  1629. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1630. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1631. AssertNotNull(name = X509_NAME_new());
  1632. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1633. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1634. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1635. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1636. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1637. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  1638. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1639. X509_NAME_free(name);
  1640. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  1641. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1642. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  1643. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1644. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1645. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1646. wolfSSL_X509_free(x509);
  1647. /* check satisfying .random.com constraint passes */
  1648. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1649. WOLFSSL_FILETYPE_PEM));
  1650. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1651. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1652. AssertNotNull(name = X509_NAME_new());
  1653. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1654. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1655. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1656. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1657. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1658. (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
  1659. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1660. X509_NAME_free(name);
  1661. wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
  1662. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1663. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  1664. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1665. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1666. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1667. wolfSSL_X509_free(x509);
  1668. /* check fail case when neither constraint is matched */
  1669. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1670. WOLFSSL_FILETYPE_PEM));
  1671. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1672. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1673. AssertNotNull(name = X509_NAME_new());
  1674. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1675. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1676. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1677. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1678. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1679. (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
  1680. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1681. X509_NAME_free(name);
  1682. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  1683. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1684. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  1685. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1686. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1687. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1688. wolfSSL_CertManagerFree(cm);
  1689. wolfSSL_X509_free(x509);
  1690. wolfSSL_X509_free(ca);
  1691. wolfSSL_EVP_PKEY_free(priv);
  1692. #endif
  1693. }
  1694. static void test_wolfSSL_CertManagerNameConstraint4(void)
  1695. {
  1696. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1697. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1698. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1699. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  1700. !defined(NO_SHA256)
  1701. WOLFSSL_CERT_MANAGER* cm;
  1702. WOLFSSL_EVP_PKEY *priv;
  1703. WOLFSSL_X509_NAME* name;
  1704. const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
  1705. const char* server_cert = "./certs/test/server-goodcn.pem";
  1706. byte *der;
  1707. int derSz;
  1708. byte *pt;
  1709. WOLFSSL_X509 *x509, *ca;
  1710. pt = (byte*)server_key_der_2048;
  1711. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  1712. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  1713. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1714. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1715. WOLFSSL_FILETYPE_ASN1));
  1716. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  1717. DEBUG_WRITE_DER(der, derSz, "ca.der");
  1718. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1719. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1720. /* check satisfying wolfssl.com constraint passes */
  1721. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1722. WOLFSSL_FILETYPE_PEM));
  1723. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1724. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1725. AssertNotNull(name = X509_NAME_new());
  1726. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1727. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1728. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1729. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1730. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1731. X509_NAME_free(name);
  1732. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  1733. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1734. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  1735. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1736. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1737. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1738. wolfSSL_X509_free(x509);
  1739. /* check satisfying example.com constraint passes */
  1740. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1741. WOLFSSL_FILETYPE_PEM));
  1742. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1743. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1744. AssertNotNull(name = X509_NAME_new());
  1745. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1746. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1747. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1748. (byte*)"example.com", 11, -1, 0), SSL_SUCCESS);
  1749. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1750. X509_NAME_free(name);
  1751. wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
  1752. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1753. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  1754. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1755. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1756. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1757. wolfSSL_X509_free(x509);
  1758. /* check satisfying wolfssl.com constraint passes with list of DNS's */
  1759. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1760. WOLFSSL_FILETYPE_PEM));
  1761. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1762. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1763. AssertNotNull(name = X509_NAME_new());
  1764. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1765. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1766. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1767. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1768. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1769. X509_NAME_free(name);
  1770. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  1771. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  1772. wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
  1773. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1774. DEBUG_WRITE_CERT_X509(x509, "good-multiple-constraint-cert.pem");
  1775. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1776. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1777. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1778. wolfSSL_X509_free(x509);
  1779. /* check fail when one DNS in the list is bad */
  1780. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1781. WOLFSSL_FILETYPE_PEM));
  1782. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1783. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1784. AssertNotNull(name = X509_NAME_new());
  1785. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1786. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1787. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1788. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1789. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1790. X509_NAME_free(name);
  1791. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  1792. wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
  1793. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  1794. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1795. DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
  1796. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1797. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1798. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1799. wolfSSL_X509_free(x509);
  1800. /* check fail case when neither constraint is matched */
  1801. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1802. WOLFSSL_FILETYPE_PEM));
  1803. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1804. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1805. AssertNotNull(name = X509_NAME_new());
  1806. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1807. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1808. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1809. (byte*)"common", 6, -1, 0), SSL_SUCCESS);
  1810. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1811. X509_NAME_free(name);
  1812. wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
  1813. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1814. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  1815. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1816. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1817. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1818. wolfSSL_CertManagerFree(cm);
  1819. wolfSSL_X509_free(x509);
  1820. wolfSSL_X509_free(ca);
  1821. wolfSSL_EVP_PKEY_free(priv);
  1822. #endif
  1823. }
  1824. static void test_wolfSSL_CertManagerNameConstraint5(void)
  1825. {
  1826. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1827. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1828. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1829. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  1830. !defined(NO_SHA256)
  1831. WOLFSSL_CERT_MANAGER* cm;
  1832. WOLFSSL_EVP_PKEY *priv;
  1833. WOLFSSL_X509_NAME* name;
  1834. const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
  1835. const char* server_cert = "./certs/test/server-goodcn.pem";
  1836. byte *der;
  1837. int derSz;
  1838. byte *pt;
  1839. WOLFSSL_X509 *x509, *ca;
  1840. pt = (byte*)server_key_der_2048;
  1841. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  1842. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  1843. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1844. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1845. WOLFSSL_FILETYPE_ASN1));
  1846. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  1847. DEBUG_WRITE_DER(der, derSz, "ca.der");
  1848. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1849. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1850. /* check satisfying wolfssl.com constraint passes */
  1851. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1852. WOLFSSL_FILETYPE_PEM));
  1853. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1854. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1855. AssertNotNull(name = X509_NAME_new());
  1856. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1857. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1858. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1859. (byte*)"example", 7, -1, 0), SSL_SUCCESS);
  1860. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1861. X509_NAME_free(name);
  1862. wolfSSL_X509_add_altname(x509, "good.example", ASN_DNS_TYPE);
  1863. wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
  1864. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1865. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  1866. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1867. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1868. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1869. wolfSSL_X509_free(x509);
  1870. /* fail with DNS check because of common name */
  1871. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1872. WOLFSSL_FILETYPE_PEM));
  1873. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1874. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1875. AssertNotNull(name = X509_NAME_new());
  1876. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1877. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1878. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1879. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1880. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1881. X509_NAME_free(name);
  1882. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  1883. wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
  1884. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1885. DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
  1886. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1887. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1888. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1889. wolfSSL_X509_free(x509);
  1890. /* fail on permitted DNS name constraint */
  1891. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1892. WOLFSSL_FILETYPE_PEM));
  1893. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1894. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1895. AssertNotNull(name = X509_NAME_new());
  1896. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1897. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1898. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1899. X509_NAME_free(name);
  1900. wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
  1901. wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
  1902. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  1903. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1904. DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
  1905. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1906. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1907. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1908. wolfSSL_X509_free(x509);
  1909. /* fail on permitted email name constraint */
  1910. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1911. WOLFSSL_FILETYPE_PEM));
  1912. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1913. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1914. AssertNotNull(name = X509_NAME_new());
  1915. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1916. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1917. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1918. X509_NAME_free(name);
  1919. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  1920. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  1921. wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
  1922. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1923. DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
  1924. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1925. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1926. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1927. wolfSSL_X509_free(x509);
  1928. /* success with empty email name */
  1929. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1930. WOLFSSL_FILETYPE_PEM));
  1931. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1932. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1933. AssertNotNull(name = X509_NAME_new());
  1934. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1935. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1936. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1937. X509_NAME_free(name);
  1938. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  1939. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1940. DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
  1941. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1942. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1943. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1944. wolfSSL_X509_free(x509);
  1945. wolfSSL_CertManagerFree(cm);
  1946. wolfSSL_X509_free(ca);
  1947. wolfSSL_EVP_PKEY_free(priv);
  1948. #endif
  1949. }
  1950. static void test_wolfSSL_CertManagerCRL(void)
  1951. {
  1952. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  1953. !defined(NO_RSA)
  1954. const char* ca_cert = "./certs/ca-cert.pem";
  1955. const char* crl1 = "./certs/crl/crl.pem";
  1956. const char* crl2 = "./certs/crl/crl2.pem";
  1957. WOLFSSL_CERT_MANAGER* cm = NULL;
  1958. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1959. AssertIntEQ(WOLFSSL_SUCCESS,
  1960. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  1961. AssertIntEQ(WOLFSSL_SUCCESS,
  1962. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  1963. AssertIntEQ(WOLFSSL_SUCCESS,
  1964. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  1965. wolfSSL_CertManagerFreeCRL(cm);
  1966. AssertIntEQ(WOLFSSL_SUCCESS,
  1967. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  1968. AssertIntEQ(WOLFSSL_SUCCESS,
  1969. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  1970. wolfSSL_CertManagerFree(cm);
  1971. #endif
  1972. }
  1973. static void test_wolfSSL_CTX_load_verify_locations_ex(void)
  1974. {
  1975. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  1976. !defined(NO_WOLFSSL_CLIENT)
  1977. WOLFSSL_CTX* ctx;
  1978. const char* ca_cert = "./certs/ca-cert.pem";
  1979. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1980. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1981. AssertNotNull(ctx);
  1982. /* test good CA */
  1983. AssertTrue(WOLFSSL_SUCCESS ==
  1984. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  1985. WOLFSSL_LOAD_FLAG_NONE));
  1986. /* test expired CA */
  1987. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  1988. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  1989. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  1990. #else
  1991. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  1992. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  1993. #endif
  1994. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  1995. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  1996. wolfSSL_CTX_free(ctx);
  1997. #endif
  1998. }
  1999. static void test_wolfSSL_CTX_load_verify_buffer_ex(void)
  2000. {
  2001. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2002. defined(USE_CERT_BUFFERS_2048)
  2003. WOLFSSL_CTX* ctx;
  2004. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  2005. byte ca_expired_cert[TWOK_BUF];
  2006. word32 sizeof_ca_expired_cert;
  2007. XFILE fp;
  2008. #ifndef NO_WOLFSSL_CLIENT
  2009. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2010. #else
  2011. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2012. #endif
  2013. AssertNotNull(ctx);
  2014. /* test good CA */
  2015. AssertTrue(WOLFSSL_SUCCESS ==
  2016. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  2017. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  2018. WOLFSSL_LOAD_FLAG_NONE));
  2019. /* load expired CA */
  2020. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  2021. fp = XFOPEN(ca_expired_cert_file, "rb");
  2022. AssertTrue(fp != XBADFILE);
  2023. sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  2024. sizeof(ca_expired_cert), fp);
  2025. XFCLOSE(fp);
  2026. /* test expired CA failure */
  2027. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  2028. AssertIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2029. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2030. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2031. #else
  2032. AssertIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2033. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2034. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2035. #endif
  2036. /* test expired CA success */
  2037. AssertIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2038. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2039. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  2040. wolfSSL_CTX_free(ctx);
  2041. #endif
  2042. }
  2043. static void test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  2044. {
  2045. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  2046. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048)
  2047. WOLFSSL_CTX* ctx;
  2048. #ifndef NO_WOLFSSL_CLIENT
  2049. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2050. #else
  2051. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2052. #endif
  2053. AssertTrue(WOLFSSL_SUCCESS ==
  2054. wolfSSL_CTX_load_verify_chain_buffer_format(ctx, ca_cert_chain_der,
  2055. sizeof_ca_cert_chain_der,
  2056. WOLFSSL_FILETYPE_ASN1));
  2057. wolfSSL_CTX_free(ctx);
  2058. #endif
  2059. }
  2060. static void test_wolfSSL_CTX_add1_chain_cert(void)
  2061. {
  2062. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
  2063. defined(KEEP_OUR_CERT) && !defined(NO_RSA)
  2064. WOLFSSL_CTX* ctx;
  2065. WOLFSSL* ssl;
  2066. const char *certChain[] = {
  2067. "./certs/intermediate/client-int-cert.pem",
  2068. "./certs/intermediate/ca-int2-cert.pem",
  2069. "./certs/intermediate/ca-int-cert.pem",
  2070. "./certs/ca-cert.pem",
  2071. NULL
  2072. };
  2073. const char** cert;
  2074. WOLFSSL_X509* x509;
  2075. WOLF_STACK_OF(X509)* chain = NULL;
  2076. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2077. AssertNotNull(ssl = wolfSSL_new(ctx));
  2078. for (cert = certChain; *cert != NULL; cert++) {
  2079. x509 = wolfSSL_X509_load_certificate_file(*cert, WOLFSSL_FILETYPE_PEM);
  2080. AssertNotNull(x509);
  2081. AssertIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
  2082. X509_free(x509);
  2083. }
  2084. for (cert = certChain; *cert != NULL; cert++) {
  2085. x509 = wolfSSL_X509_load_certificate_file(*cert, WOLFSSL_FILETYPE_PEM);
  2086. AssertNotNull(x509);
  2087. AssertIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
  2088. X509_free(x509);
  2089. }
  2090. AssertIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
  2091. AssertIntEQ(sk_X509_num(chain), 3);
  2092. AssertIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
  2093. AssertIntEQ(sk_X509_num(chain), 3);
  2094. SSL_free(ssl);
  2095. SSL_CTX_free(ctx);
  2096. #endif
  2097. }
  2098. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  2099. {
  2100. int ret = 0;
  2101. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  2102. const char* server_chain_der = "./certs/server-cert-chain.der";
  2103. const char* client_single_pem = "./certs/client-cert.pem";
  2104. WOLFSSL_CTX* ctx;
  2105. #ifndef NO_WOLFSSL_CLIENT
  2106. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2107. AssertNotNull(ctx);
  2108. #else
  2109. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2110. AssertNotNull(ctx);
  2111. #endif
  2112. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  2113. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2114. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  2115. client_single_pem, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  2116. wolfSSL_CTX_free(ctx);
  2117. #endif
  2118. return ret;
  2119. }
  2120. static void test_wolfSSL_CTX_SetTmpDH_file(void)
  2121. {
  2122. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH)
  2123. WOLFSSL_CTX *ctx;
  2124. #ifndef NO_WOLFSSL_CLIENT
  2125. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2126. #else
  2127. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2128. #endif
  2129. /* invalid context */
  2130. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  2131. dhParamFile, WOLFSSL_FILETYPE_PEM));
  2132. /* invalid dhParamFile file */
  2133. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  2134. NULL, WOLFSSL_FILETYPE_PEM));
  2135. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  2136. bogusFile, WOLFSSL_FILETYPE_PEM));
  2137. /* success */
  2138. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  2139. WOLFSSL_FILETYPE_PEM));
  2140. wolfSSL_CTX_free(ctx);
  2141. #endif
  2142. }
  2143. static void test_wolfSSL_CTX_SetTmpDH_buffer(void)
  2144. {
  2145. #if !defined(NO_CERTS) && !defined(NO_DH)
  2146. WOLFSSL_CTX *ctx;
  2147. #ifndef NO_WOLFSSL_CLIENT
  2148. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2149. #else
  2150. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2151. #endif
  2152. /* invalid context */
  2153. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, dh_key_der_2048,
  2154. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2155. /* invalid dhParamFile file */
  2156. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  2157. 0, WOLFSSL_FILETYPE_ASN1));
  2158. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dsa_key_der_2048,
  2159. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2160. /* success */
  2161. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2162. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2163. wolfSSL_CTX_free(ctx);
  2164. #endif
  2165. }
  2166. static void test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  2167. {
  2168. #if !defined(NO_CERTS) && !defined(NO_DH)
  2169. WOLFSSL_CTX *ctx;
  2170. #ifndef NO_WOLFSSL_CLIENT
  2171. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2172. AssertNotNull(ctx);
  2173. #else
  2174. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2175. AssertNotNull(ctx);
  2176. #endif
  2177. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  2178. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2179. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2180. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  2181. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2182. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2183. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  2184. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2185. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2186. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  2187. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2188. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2189. wolfSSL_CTX_free(ctx);
  2190. #endif
  2191. }
  2192. static void test_wolfSSL_CTX_der_load_verify_locations(void)
  2193. {
  2194. #ifdef WOLFSSL_DER_LOAD
  2195. WOLFSSL_CTX* ctx = NULL;
  2196. const char* derCert = "./certs/server-cert.der";
  2197. const char* nullPath = NULL;
  2198. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  2199. const char* emptyPath = "";
  2200. /* der load Case 1 ctx NULL */
  2201. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2202. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2203. #ifndef NO_WOLFSSL_CLIENT
  2204. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2205. #else
  2206. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2207. #endif
  2208. /* Case 2 filePath NULL */
  2209. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  2210. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2211. /* Case 3 invalid format */
  2212. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2213. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  2214. /* Case 4 filePath not valid */
  2215. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  2216. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2217. /* Case 5 filePath empty */
  2218. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  2219. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2220. #ifndef NO_RSA
  2221. /* Case 6 success case */
  2222. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2223. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2224. #endif
  2225. wolfSSL_CTX_free(ctx);
  2226. #endif
  2227. }
  2228. static void test_wolfSSL_CTX_enable_disable(void)
  2229. {
  2230. #ifndef NO_CERTS
  2231. WOLFSSL_CTX* ctx = NULL;
  2232. #ifdef HAVE_CRL
  2233. AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  2234. AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  2235. #endif
  2236. #ifdef HAVE_OCSP
  2237. AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  2238. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  2239. #endif
  2240. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2241. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2242. AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  2243. AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  2244. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  2245. AssertIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  2246. #endif
  2247. #ifndef NO_WOLFSSL_CLIENT
  2248. #ifdef HAVE_EXTENDED_MASTER
  2249. AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  2250. #endif
  2251. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2252. AssertNotNull(ctx);
  2253. #ifdef HAVE_EXTENDED_MASTER
  2254. AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  2255. #endif
  2256. #elif !defined(NO_WOLFSSL_SERVER)
  2257. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2258. #else
  2259. return;
  2260. #endif
  2261. #ifdef HAVE_CRL
  2262. AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  2263. AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  2264. #endif
  2265. #ifdef HAVE_OCSP
  2266. AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  2267. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  2268. WOLFSSL_SUCCESS);
  2269. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  2270. WOLFSSL_SUCCESS);
  2271. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  2272. WOLFSSL_SUCCESS);
  2273. #endif
  2274. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2275. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2276. AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  2277. AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  2278. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  2279. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  2280. #endif
  2281. wolfSSL_CTX_free(ctx);
  2282. #endif /* NO_CERTS */
  2283. }
  2284. static void test_wolfSSL_CTX_ticket_API(void)
  2285. {
  2286. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  2287. WOLFSSL_CTX* ctx = NULL;
  2288. void *userCtx = (void*)"this is my ctx";
  2289. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2290. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
  2291. AssertTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
  2292. wolfSSL_CTX_free(ctx);
  2293. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
  2294. AssertNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
  2295. #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
  2296. }
  2297. static void test_wolfSSL_set_minmax_proto_version(void)
  2298. {
  2299. #ifdef OPENSSL_EXTRA
  2300. WOLFSSL_CTX *ctx;
  2301. WOLFSSL *ssl;
  2302. int ret;
  2303. (void)ret;
  2304. (void)ssl;
  2305. printf(testingFmt, "test_wolfSSL_set_minmax_proto_version");
  2306. #ifndef NO_WOLFSSL_CLIENT
  2307. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2308. AssertNotNull(ssl = wolfSSL_new(ctx));
  2309. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  2310. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  2311. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  2312. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  2313. AssertIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
  2314. AssertIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
  2315. AssertIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
  2316. AssertIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
  2317. wolfSSL_free(ssl);
  2318. wolfSSL_CTX_free(ctx);
  2319. #else
  2320. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2321. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  2322. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  2323. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  2324. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  2325. wolfSSL_CTX_free(ctx);
  2326. #endif
  2327. printf(resultFmt, passed);
  2328. #endif
  2329. }
  2330. /*----------------------------------------------------------------------------*
  2331. | SSL
  2332. *----------------------------------------------------------------------------*/
  2333. static void test_server_wolfSSL_new(void)
  2334. {
  2335. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2336. !defined(NO_WOLFSSL_SERVER)
  2337. WOLFSSL_CTX *ctx;
  2338. WOLFSSL_CTX *ctx_nocert;
  2339. WOLFSSL *ssl;
  2340. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2341. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2342. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  2343. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  2344. /* invalid context */
  2345. AssertNull(ssl = wolfSSL_new(NULL));
  2346. #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && !defined(OPENSSL_EXTRA)
  2347. AssertNull(ssl = wolfSSL_new(ctx_nocert));
  2348. #endif
  2349. /* success */
  2350. AssertNotNull(ssl = wolfSSL_new(ctx));
  2351. wolfSSL_free(ssl);
  2352. wolfSSL_CTX_free(ctx);
  2353. wolfSSL_CTX_free(ctx_nocert);
  2354. #endif
  2355. }
  2356. static void test_client_wolfSSL_new(void)
  2357. {
  2358. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2359. !defined(NO_WOLFSSL_CLIENT)
  2360. WOLFSSL_CTX *ctx;
  2361. WOLFSSL_CTX *ctx_nocert;
  2362. WOLFSSL *ssl;
  2363. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2364. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2365. AssertTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  2366. /* invalid context */
  2367. AssertNull(ssl = wolfSSL_new(NULL));
  2368. /* success */
  2369. AssertNotNull(ssl = wolfSSL_new(ctx_nocert));
  2370. wolfSSL_free(ssl);
  2371. /* success */
  2372. AssertNotNull(ssl = wolfSSL_new(ctx));
  2373. wolfSSL_free(ssl);
  2374. wolfSSL_CTX_free(ctx);
  2375. wolfSSL_CTX_free(ctx_nocert);
  2376. #endif
  2377. }
  2378. static void test_wolfSSL_SetTmpDH_file(void)
  2379. {
  2380. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  2381. !defined(NO_WOLFSSL_SERVER)
  2382. WOLFSSL_CTX *ctx;
  2383. WOLFSSL *ssl;
  2384. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2385. #ifndef NO_RSA
  2386. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  2387. WOLFSSL_FILETYPE_PEM));
  2388. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  2389. WOLFSSL_FILETYPE_PEM));
  2390. #elif defined(HAVE_ECC)
  2391. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  2392. WOLFSSL_FILETYPE_PEM));
  2393. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  2394. WOLFSSL_FILETYPE_PEM));
  2395. #elif defined(HAVE_ED25519)
  2396. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  2397. WOLFSSL_FILETYPE_PEM));
  2398. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  2399. WOLFSSL_FILETYPE_PEM));
  2400. #elif defined(HAVE_ED448)
  2401. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  2402. WOLFSSL_FILETYPE_PEM));
  2403. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  2404. WOLFSSL_FILETYPE_PEM));
  2405. #endif
  2406. AssertNotNull(ssl = wolfSSL_new(ctx));
  2407. /* invalid ssl */
  2408. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  2409. dhParamFile, WOLFSSL_FILETYPE_PEM));
  2410. /* invalid dhParamFile file */
  2411. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  2412. NULL, WOLFSSL_FILETYPE_PEM));
  2413. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  2414. bogusFile, WOLFSSL_FILETYPE_PEM));
  2415. /* success */
  2416. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  2417. WOLFSSL_FILETYPE_PEM));
  2418. wolfSSL_free(ssl);
  2419. wolfSSL_CTX_free(ctx);
  2420. #endif
  2421. }
  2422. static void test_wolfSSL_SetTmpDH_buffer(void)
  2423. {
  2424. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  2425. WOLFSSL_CTX *ctx;
  2426. WOLFSSL *ssl;
  2427. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2428. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  2429. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  2430. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  2431. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2432. AssertNotNull(ssl = wolfSSL_new(ctx));
  2433. /* invalid ssl */
  2434. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  2435. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2436. /* invalid dhParamFile file */
  2437. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  2438. 0, WOLFSSL_FILETYPE_ASN1));
  2439. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  2440. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2441. /* success */
  2442. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  2443. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2444. wolfSSL_free(ssl);
  2445. wolfSSL_CTX_free(ctx);
  2446. #endif
  2447. }
  2448. static void test_wolfSSL_SetMinMaxDhKey_Sz(void)
  2449. {
  2450. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  2451. WOLFSSL_CTX *ctx, *ctx2;
  2452. WOLFSSL *ssl, *ssl2;
  2453. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2454. AssertNotNull(ctx);
  2455. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  2456. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  2457. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  2458. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2459. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  2460. ssl = wolfSSL_new(ctx);
  2461. AssertNotNull(ssl);
  2462. ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2463. AssertNotNull(ctx2);
  2464. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  2465. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  2466. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  2467. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2468. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  2469. ssl2 = wolfSSL_new(ctx2);
  2470. AssertNotNull(ssl2);
  2471. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  2472. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2473. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  2474. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  2475. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2476. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  2477. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  2478. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2479. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  2480. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2481. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  2482. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  2483. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2484. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  2485. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  2486. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2487. wolfSSL_free(ssl2);
  2488. wolfSSL_CTX_free(ctx2);
  2489. wolfSSL_free(ssl);
  2490. wolfSSL_CTX_free(ctx);
  2491. #endif
  2492. }
  2493. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  2494. * allowed.
  2495. * POST: return 1 on success.
  2496. */
  2497. static int test_wolfSSL_SetMinVersion(void)
  2498. {
  2499. int failFlag = WOLFSSL_SUCCESS;
  2500. #ifndef NO_WOLFSSL_CLIENT
  2501. WOLFSSL_CTX* ctx;
  2502. WOLFSSL* ssl;
  2503. int itr;
  2504. #ifndef NO_OLD_TLS
  2505. const int versions[] = {
  2506. #ifdef WOLFSSL_ALLOW_TLSV10
  2507. WOLFSSL_TLSV1,
  2508. #endif
  2509. WOLFSSL_TLSV1_1,
  2510. WOLFSSL_TLSV1_2};
  2511. #elif !defined(WOLFSSL_NO_TLS12)
  2512. const int versions[] = { WOLFSSL_TLSV1_2 };
  2513. #else
  2514. const int versions[] = { WOLFSSL_TLSV1_3 };
  2515. #endif
  2516. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2517. ssl = wolfSSL_new(ctx);
  2518. printf(testingFmt, "wolfSSL_SetMinVersion()");
  2519. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++){
  2520. if(wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS){
  2521. failFlag = WOLFSSL_FAILURE;
  2522. }
  2523. }
  2524. printf(resultFmt, failFlag == WOLFSSL_SUCCESS ? passed : failed);
  2525. wolfSSL_free(ssl);
  2526. wolfSSL_CTX_free(ctx);
  2527. #endif
  2528. return failFlag;
  2529. } /* END test_wolfSSL_SetMinVersion */
  2530. /*----------------------------------------------------------------------------*
  2531. | EC
  2532. *----------------------------------------------------------------------------*/
  2533. /* Test function for EC_POINT_new, EC_POINT_mul, EC_POINT_free,
  2534. EC_GROUP_new_by_curve_name, EC_GROUP_order_bits
  2535. */
  2536. #ifdef OPENSSL_EXTRA
  2537. static void test_wolfSSL_EC(void)
  2538. {
  2539. #if !defined(WOLFSSL_SP_MATH) && \
  2540. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  2541. #if defined(HAVE_ECC)
  2542. BN_CTX *ctx;
  2543. EC_GROUP *group;
  2544. EC_GROUP *group2;
  2545. EC_POINT *Gxy, *new_point, *set_point;
  2546. BIGNUM *k = NULL, *Gx = NULL, *Gy = NULL, *Gz = NULL;
  2547. BIGNUM *X, *Y;
  2548. BIGNUM *set_point_bn;
  2549. char* hexStr;
  2550. int group_bits;
  2551. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD17AF381913FF7A96314EA47055EA0FD0";
  2552. /* NISTP256R1 Gx/Gy */
  2553. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
  2554. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5";
  2555. #ifndef HAVE_SELFTEST
  2556. EC_POINT *tmp;
  2557. size_t bin_len;
  2558. unsigned char* buf = NULL;
  2559. const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C2964FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5";
  2560. const unsigned char binUncompG[] = {
  2561. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  2562. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  2563. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  2564. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  2565. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  2566. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  2567. };
  2568. #ifdef HAVE_COMP_KEY
  2569. const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
  2570. const unsigned char binCompG[] = {
  2571. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  2572. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  2573. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  2574. };
  2575. #endif
  2576. #endif
  2577. AssertNotNull(ctx = BN_CTX_new());
  2578. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  2579. AssertNotNull(group2 = EC_GROUP_dup(group));
  2580. AssertIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  2581. AssertNotNull(Gxy = EC_POINT_new(group));
  2582. AssertNotNull(new_point = EC_POINT_new(group));
  2583. AssertNotNull(set_point = EC_POINT_new(group));
  2584. AssertNotNull(X = BN_new());
  2585. AssertNotNull(Y = BN_new());
  2586. AssertNotNull(set_point_bn = BN_new());
  2587. /* load test values */
  2588. AssertIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  2589. AssertIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  2590. AssertIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  2591. AssertIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  2592. /* populate coordinates for input point */
  2593. Gxy->X = Gx;
  2594. Gxy->Y = Gy;
  2595. Gxy->Z = Gz;
  2596. #ifndef HAVE_SELFTEST
  2597. /* perform point multiplication */
  2598. AssertIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), WOLFSSL_SUCCESS);
  2599. AssertIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), WOLFSSL_SUCCESS);
  2600. AssertIntEQ(BN_is_zero(new_point->X), 0);
  2601. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  2602. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  2603. AssertIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), WOLFSSL_SUCCESS);
  2604. AssertIntEQ(BN_is_zero(new_point->X), 0);
  2605. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  2606. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  2607. AssertIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), WOLFSSL_SUCCESS);
  2608. AssertIntEQ(BN_is_zero(new_point->X), 0);
  2609. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  2610. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  2611. #else
  2612. AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy, ctx), WOLFSSL_SUCCESS);
  2613. AssertIntEQ(BN_is_zero(new_point->X), 0);
  2614. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  2615. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  2616. #endif
  2617. /* check if point X coordinate is zero */
  2618. AssertIntEQ(BN_is_zero(new_point->X), 0);
  2619. #ifdef USE_ECC_B_PARAM
  2620. AssertIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  2621. #endif /* USE_ECC_B_PARAM */
  2622. /* Force non-affine coordinates */
  2623. AssertIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  2624. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  2625. new_point->inSet = 0;
  2626. /* extract the coordinates from point */
  2627. AssertIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y, ctx), WOLFSSL_SUCCESS);
  2628. /* check if point X coordinate is zero */
  2629. AssertIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  2630. /* set the same X and Y points in another object */
  2631. AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y, ctx), WOLFSSL_SUCCESS);
  2632. /* compare points as they should be the same */
  2633. AssertIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  2634. /* Test copying */
  2635. AssertIntEQ(EC_POINT_copy(new_point, set_point), 1);
  2636. /* Test inverting */
  2637. AssertIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  2638. AssertPtrEq(EC_POINT_point2bn(group, set_point, POINT_CONVERSION_UNCOMPRESSED,
  2639. set_point_bn, ctx), set_point_bn);
  2640. /* check bn2hex */
  2641. hexStr = BN_bn2hex(k);
  2642. AssertStrEQ(hexStr, kTest);
  2643. #ifndef NO_FILESYSTEM
  2644. BN_print_fp(stdout, k);
  2645. printf("\n");
  2646. #endif
  2647. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  2648. hexStr = BN_bn2hex(Gx);
  2649. AssertStrEQ(hexStr, kGx);
  2650. #ifndef NO_FILESYSTEM
  2651. BN_print_fp(stdout, Gx);
  2652. printf("\n");
  2653. #endif
  2654. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  2655. hexStr = BN_bn2hex(Gy);
  2656. AssertStrEQ(hexStr, kGy);
  2657. #ifndef NO_FILESYSTEM
  2658. BN_print_fp(stdout, Gy);
  2659. printf("\n");
  2660. #endif
  2661. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  2662. #ifndef HAVE_SELFTEST
  2663. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  2664. AssertStrEQ(hexStr, uncompG);
  2665. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  2666. #ifdef HAVE_COMP_KEY
  2667. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  2668. AssertStrEQ(hexStr, compG);
  2669. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  2670. #endif
  2671. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx);
  2672. AssertIntEQ(bin_len, sizeof(binUncompG));
  2673. AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL, DYNAMIC_TYPE_ECC));
  2674. AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, buf,
  2675. bin_len, ctx), bin_len);
  2676. AssertIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  2677. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  2678. #ifdef HAVE_COMP_KEY
  2679. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL, 0, ctx);
  2680. AssertIntEQ(bin_len, sizeof(binCompG));
  2681. AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL, DYNAMIC_TYPE_ECC));
  2682. AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  2683. bin_len, ctx), bin_len);
  2684. AssertIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  2685. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  2686. #endif
  2687. AssertNotNull(tmp = EC_POINT_new(group));
  2688. AssertIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG), ctx), 1);
  2689. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  2690. EC_POINT_free(tmp);
  2691. #ifdef HAVE_COMP_KEY
  2692. AssertNotNull(tmp = EC_POINT_new(group));
  2693. AssertIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx), 1);
  2694. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  2695. EC_POINT_free(tmp);
  2696. #endif
  2697. #endif
  2698. /* test BN_mod_add */
  2699. AssertIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  2700. (WOLFSSL_BIGNUM*)BN_value_one(),
  2701. (WOLFSSL_BIGNUM*)BN_value_one(), NULL), 1);
  2702. AssertIntEQ(BN_is_zero(new_point->Z), 1);
  2703. /* cleanup */
  2704. BN_free(X);
  2705. BN_free(Y);
  2706. BN_free(k);
  2707. BN_free(set_point_bn);
  2708. EC_POINT_free(new_point);
  2709. EC_POINT_free(set_point);
  2710. EC_POINT_free(Gxy);
  2711. EC_GROUP_free(group);
  2712. EC_GROUP_free(group2);
  2713. BN_CTX_free(ctx);
  2714. #endif /* HAVE_ECC */
  2715. #endif /* OPENSSL_EXTRA && !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  2716. }
  2717. #endif /* OPENSSL_EXTRA */
  2718. #ifndef NO_BIO
  2719. static void test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  2720. {
  2721. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA)
  2722. EC_GROUP *group;
  2723. BIO* bio;
  2724. AssertNotNull(bio = BIO_new(BIO_s_file()));
  2725. AssertIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  2726. AssertNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  2727. AssertIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  2728. EC_GROUP_free(group);
  2729. BIO_free(bio);
  2730. #endif /* HAVE_ECC */
  2731. }
  2732. #endif /* !NO_BIO */
  2733. # if defined(OPENSSL_EXTRA)
  2734. static void test_wolfSSL_ECDSA_SIG(void)
  2735. {
  2736. #ifdef HAVE_ECC
  2737. WOLFSSL_ECDSA_SIG* sig = NULL;
  2738. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  2739. const unsigned char* cp;
  2740. unsigned char* p;
  2741. unsigned char outSig[8];
  2742. unsigned char sigData[8] =
  2743. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  2744. sig = wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData));
  2745. AssertNull(sig);
  2746. cp = sigData;
  2747. AssertNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  2748. AssertIntEQ((cp == sigData + 8), 1);
  2749. cp = sigData;
  2750. AssertNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  2751. AssertNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  2752. AssertIntEQ((sig == sig2), 1);
  2753. cp = outSig;
  2754. p = outSig;
  2755. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  2756. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  2757. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  2758. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  2759. AssertIntEQ((p == outSig + 8), 1);
  2760. AssertIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  2761. wolfSSL_ECDSA_SIG_free(sig);
  2762. #endif /* HAVE_ECC */
  2763. }
  2764. static void test_EC_i2d(void)
  2765. {
  2766. #if defined(HAVE_ECC) && !defined(HAVE_FIPS)
  2767. EC_KEY *key;
  2768. EC_KEY *copy;
  2769. int len;
  2770. unsigned char *buf = NULL;
  2771. const unsigned char *tmp = NULL;
  2772. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  2773. AssertIntEQ(EC_KEY_generate_key(key), 1);
  2774. AssertIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  2775. AssertIntEQ(i2d_EC_PUBKEY(key, &buf), len);
  2776. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2777. buf = NULL;
  2778. AssertIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  2779. AssertIntEQ(i2d_ECPrivateKey(key, &buf), len);
  2780. tmp = buf;
  2781. AssertNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  2782. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2783. buf = NULL;
  2784. AssertIntGT((len = i2o_ECPublicKey(key, &buf)), 0);
  2785. tmp = buf;
  2786. AssertNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  2787. AssertIntEQ(EC_KEY_check_key(key), 1);
  2788. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  2789. EC_KEY_free(key);
  2790. EC_KEY_free(copy);
  2791. #endif /* HAVE_ECC */
  2792. }
  2793. static void test_ECDSA_size_sign(void)
  2794. {
  2795. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  2796. EC_KEY *key;
  2797. int id;
  2798. byte hash[WC_MAX_DIGEST_SIZE];
  2799. byte sig[ECC_MAX_SIG_SIZE];
  2800. unsigned int sigSz = sizeof(sig);
  2801. XMEMSET(hash, 123, sizeof(hash));
  2802. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  2803. AssertIntEQ(id, ECC_SECP256R1);
  2804. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  2805. AssertIntEQ(EC_KEY_generate_key(key), 1);
  2806. AssertIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  2807. AssertIntGE(ECDSA_size(key), sigSz);
  2808. AssertIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, key), 1);
  2809. EC_KEY_free(key);
  2810. #endif /* HAVE_ECC && !NO_ECC256 && !NO_ECC_SECP */
  2811. }
  2812. static void test_ED25519(void)
  2813. {
  2814. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  2815. defined(WOLFSSL_KEY_GEN)
  2816. byte priv[ED25519_PRV_KEY_SIZE];
  2817. unsigned int privSz = (unsigned int)sizeof(priv);
  2818. byte pub[ED25519_PUB_KEY_SIZE];
  2819. unsigned int pubSz = (unsigned int)sizeof(pub);
  2820. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  2821. const char* msg = TEST_STRING;
  2822. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  2823. byte sig[ED25519_SIG_SIZE];
  2824. unsigned int sigSz = (unsigned int)sizeof(sig);
  2825. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  2826. AssertIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  2827. WOLFSSL_SUCCESS);
  2828. AssertIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  2829. AssertIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  2830. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  2831. AssertIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  2832. &sigSz), WOLFSSL_SUCCESS);
  2833. AssertIntEQ(sigSz, ED25519_SIG_SIZE);
  2834. #ifdef HAVE_ED25519_VERIFY
  2835. AssertIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  2836. sigSz), WOLFSSL_SUCCESS);
  2837. #endif /* HAVE_ED25519_VERIFY */
  2838. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  2839. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
  2840. }
  2841. static void test_ED448(void)
  2842. {
  2843. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  2844. defined(WOLFSSL_KEY_GEN)
  2845. byte priv[ED448_PRV_KEY_SIZE];
  2846. unsigned int privSz = (unsigned int)sizeof(priv);
  2847. byte pub[ED448_PUB_KEY_SIZE];
  2848. unsigned int pubSz = (unsigned int)sizeof(pub);
  2849. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  2850. const char* msg = TEST_STRING;
  2851. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  2852. byte sig[ED448_SIG_SIZE];
  2853. unsigned int sigSz = (unsigned int)sizeof(sig);
  2854. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  2855. AssertIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz),
  2856. WOLFSSL_SUCCESS);
  2857. AssertIntEQ(privSz, ED448_PRV_KEY_SIZE);
  2858. AssertIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  2859. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  2860. AssertIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  2861. &sigSz), WOLFSSL_SUCCESS);
  2862. AssertIntEQ(sigSz, ED448_SIG_SIZE);
  2863. #ifdef HAVE_ED448_VERIFY
  2864. AssertIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  2865. sigSz), WOLFSSL_SUCCESS);
  2866. #endif /* HAVE_ED448_VERIFY */
  2867. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  2868. #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
  2869. }
  2870. #endif /* OPENSSL_EXTRA */
  2871. #include <wolfssl/openssl/pem.h>
  2872. /*----------------------------------------------------------------------------*
  2873. | EVP
  2874. *----------------------------------------------------------------------------*/
  2875. static void test_wolfSSL_EVP_PKEY_print_public(void)
  2876. {
  2877. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  2878. WOLFSSL_BIO* rbio = NULL;
  2879. WOLFSSL_BIO* wbio = NULL;
  2880. WOLFSSL_EVP_PKEY* pkey = NULL;
  2881. char line[256] = { 0 };
  2882. char line1[256] = { 0 };
  2883. int i;
  2884. printf(testingFmt, "EVP_PKEY_print_public()");
  2885. /* test error cases */
  2886. AssertIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
  2887. /*
  2888. * test RSA public key print
  2889. * in this test, pass '3' for indent
  2890. */
  2891. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
  2892. rbio = BIO_new_mem_buf( client_keypub_der_1024,
  2893. sizeof_client_keypub_der_1024);
  2894. AssertNotNull(rbio);
  2895. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  2896. AssertNotNull(pkey);
  2897. wbio = BIO_new(BIO_s_mem());
  2898. AssertNotNull(wbio);
  2899. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
  2900. BIO_gets(wbio, line, sizeof(line));
  2901. strcpy(line1, " RSA Public-Key: (1024 bit)\n");
  2902. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2903. BIO_gets(wbio, line, sizeof(line));
  2904. strcpy(line1, " Modulus:\n");
  2905. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2906. BIO_gets(wbio, line, sizeof(line));
  2907. strcpy(line1, " 00:BC:73:0E:A8:49:F3:74:A2:A9:EF:18:A5:DA:55:\n");
  2908. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2909. /* skip to the end of modulus element*/
  2910. for( i = 0; i < 8 ;i++) {
  2911. BIO_gets(wbio, line, sizeof(line));
  2912. }
  2913. BIO_gets(wbio, line, sizeof(line));
  2914. strcpy(line1, " Exponent: 65537 (0x010001)\n");
  2915. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2916. /* should reach EOF */
  2917. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  2918. EVP_PKEY_free(pkey);
  2919. pkey = NULL;
  2920. BIO_free(rbio);
  2921. BIO_free(wbio);
  2922. rbio = NULL;
  2923. wbio = NULL;
  2924. #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
  2925. /*
  2926. * test DSA public key print
  2927. */
  2928. #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
  2929. rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
  2930. sizeof_dsa_pub_key_der_2048);
  2931. AssertNotNull(rbio);
  2932. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  2933. AssertNotNull(pkey);
  2934. wbio = BIO_new(BIO_s_mem());
  2935. AssertNotNull(wbio);
  2936. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  2937. BIO_gets(wbio, line, sizeof(line));
  2938. strcpy(line1, "DSA Public-Key: (2048 bit)\n");
  2939. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2940. BIO_gets(wbio, line, sizeof(line));
  2941. strcpy(line1, "pub:\n");
  2942. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2943. BIO_gets(wbio, line, sizeof(line));
  2944. strcpy(line1,
  2945. " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
  2946. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2947. /* skip to the end of pub element*/
  2948. for( i = 0; i < 17 ;i++) {
  2949. BIO_gets(wbio, line, sizeof(line));
  2950. }
  2951. BIO_gets(wbio, line, sizeof(line));
  2952. strcpy(line1, "P:\n");
  2953. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2954. /* skip to the end of P element*/
  2955. for( i = 0; i < 18 ;i++) {
  2956. BIO_gets(wbio, line, sizeof(line));
  2957. }
  2958. BIO_gets(wbio, line, sizeof(line));
  2959. strcpy(line1, "Q:\n");
  2960. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2961. /* skip to the end of Q element*/
  2962. for( i = 0; i < 3 ;i++) {
  2963. BIO_gets(wbio, line, sizeof(line));
  2964. }
  2965. BIO_gets(wbio, line, sizeof(line));
  2966. strcpy(line1, "G:\n");
  2967. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2968. /* skip to the end of G element*/
  2969. for( i = 0; i < 18 ;i++) {
  2970. BIO_gets(wbio, line, sizeof(line));
  2971. }
  2972. /* should reach EOF */
  2973. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  2974. EVP_PKEY_free(pkey);
  2975. pkey = NULL;
  2976. BIO_free(rbio);
  2977. BIO_free(wbio);
  2978. rbio = NULL;
  2979. wbio = NULL;
  2980. #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
  2981. /*
  2982. * test ECC public key print
  2983. */
  2984. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  2985. rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
  2986. sizeof_ecc_clikeypub_der_256);
  2987. AssertNotNull(rbio);
  2988. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  2989. AssertNotNull(pkey);
  2990. wbio = BIO_new(BIO_s_mem());
  2991. AssertNotNull(wbio);
  2992. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  2993. BIO_gets(wbio, line, sizeof(line));
  2994. strcpy(line1, "Public-Key: (256 bit)\n");
  2995. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2996. BIO_gets(wbio, line, sizeof(line));
  2997. strcpy(line1, "pub:\n");
  2998. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2999. BIO_gets(wbio, line, sizeof(line));
  3000. strcpy(line1,
  3001. " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
  3002. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3003. /* skip to the end of pub element*/
  3004. for( i = 0; i < 4 ;i++) {
  3005. BIO_gets(wbio, line, sizeof(line));
  3006. }
  3007. BIO_gets(wbio, line, sizeof(line));
  3008. strcpy(line1, "ASN1 OID: prime256v1\n");
  3009. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3010. BIO_gets(wbio, line, sizeof(line));
  3011. strcpy(line1, "NIST CURVE: P-256\n");
  3012. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3013. /* should reach EOF */
  3014. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3015. EVP_PKEY_free(pkey);
  3016. pkey = NULL;
  3017. BIO_free(rbio);
  3018. BIO_free(wbio);
  3019. rbio = NULL;
  3020. wbio = NULL;
  3021. #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
  3022. /*
  3023. * test DH public key print
  3024. */
  3025. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  3026. rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
  3027. sizeof_dh_pub_key_der_2048);
  3028. AssertNotNull(rbio);
  3029. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3030. AssertNotNull(pkey);
  3031. wbio = BIO_new(BIO_s_mem());
  3032. AssertNotNull(wbio);
  3033. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3034. BIO_gets(wbio, line, sizeof(line));
  3035. strcpy(line1, "DH Public-Key: (2048 bit)\n");
  3036. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3037. BIO_gets(wbio, line, sizeof(line));
  3038. strcpy(line1, "public-key:\n");
  3039. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3040. BIO_gets(wbio, line, sizeof(line));
  3041. strcpy(line1,
  3042. " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
  3043. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3044. /* skip to the end of public-key element*/
  3045. for( i = 0; i < 17 ;i++) {
  3046. BIO_gets(wbio, line, sizeof(line));
  3047. }
  3048. BIO_gets(wbio, line, sizeof(line));
  3049. strcpy(line1, "prime:\n");
  3050. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3051. BIO_gets(wbio, line, sizeof(line));
  3052. strcpy(line1,
  3053. " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
  3054. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3055. /* skip to the end of prime element*/
  3056. for( i = 0; i < 17 ;i++) {
  3057. BIO_gets(wbio, line, sizeof(line));
  3058. }
  3059. BIO_gets(wbio, line, sizeof(line));
  3060. strcpy(line1, "generator: 2 (0x02)\n");
  3061. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3062. /* should reach EOF */
  3063. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3064. EVP_PKEY_free(pkey);
  3065. pkey = NULL;
  3066. BIO_free(rbio);
  3067. BIO_free(wbio);
  3068. rbio = NULL;
  3069. wbio = NULL;
  3070. #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
  3071. /* to prevent "unused variable" warning */
  3072. (void)pkey;
  3073. (void)wbio;
  3074. (void)rbio;
  3075. (void)line;
  3076. (void)line1;
  3077. (void)i;
  3078. printf(resultFmt, passed);
  3079. #endif /* OPENSSL_EXTRA */
  3080. }
  3081. /* Test functions for base64 encode/decode */
  3082. static void test_wolfSSL_EVP_ENCODE_CTX_new(void)
  3083. {
  3084. #if defined(OPENSSL_EXTRA) && \
  3085. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  3086. EVP_ENCODE_CTX* ctx = NULL;
  3087. printf(testingFmt, "EVP_ENCODE_CTX_new()");
  3088. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3089. AssertIntEQ( ctx->remaining,0);
  3090. AssertIntEQ( ctx->data[0],0);
  3091. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3092. EVP_ENCODE_CTX_free(ctx);
  3093. printf(resultFmt, passed);
  3094. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
  3095. }
  3096. static void test_wolfSSL_EVP_ENCODE_CTX_free(void)
  3097. {
  3098. #if defined(OPENSSL_EXTRA) && \
  3099. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  3100. EVP_ENCODE_CTX* ctx = NULL;
  3101. printf(testingFmt, "EVP_ENCODE_CTX_free()");
  3102. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3103. EVP_ENCODE_CTX_free(ctx);
  3104. printf(resultFmt, passed);
  3105. #endif /*OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
  3106. }
  3107. static void test_wolfSSL_EVP_EncodeInit(void)
  3108. {
  3109. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3110. EVP_ENCODE_CTX* ctx = NULL;
  3111. printf(testingFmt, "EVP_EncodeInit()");
  3112. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3113. AssertIntEQ( ctx->remaining,0);
  3114. AssertIntEQ( ctx->data[0],0);
  3115. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3116. /* make ctx dirty */
  3117. ctx->remaining = 10;
  3118. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  3119. EVP_EncodeInit(ctx);
  3120. AssertIntEQ( ctx->remaining,0);
  3121. AssertIntEQ( ctx->data[0],0);
  3122. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3123. EVP_ENCODE_CTX_free(ctx);
  3124. printf(resultFmt, passed);
  3125. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3126. }
  3127. static void test_wolfSSL_EVP_EncodeUpdate(void)
  3128. {
  3129. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3130. int outl;
  3131. int total;
  3132. const unsigned char plain0[] = {"Th"};
  3133. const unsigned char plain1[] = {"This is a base64 encodeing test."};
  3134. const unsigned char plain2[] = {"This is additional data."};
  3135. const unsigned char enc0[] = {"VGg=\n"};
  3136. /* expected encoded result for the first output 64 chars plus trailing LF*/
  3137. const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
  3138. const unsigned char enc2[] =
  3139. {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
  3140. unsigned char encOutBuff[300];
  3141. EVP_ENCODE_CTX* ctx = NULL;
  3142. printf(testingFmt, "EVP_EncodeUpdate()");
  3143. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3144. EVP_EncodeInit(ctx);
  3145. /* illegal parameter test */
  3146. AssertIntEQ(
  3147. EVP_EncodeUpdate(
  3148. NULL, /* pass NULL as ctx */
  3149. encOutBuff,
  3150. &outl,
  3151. plain1,
  3152. sizeof(plain1)-1),
  3153. 0 /* expected result code 0: fail */
  3154. );
  3155. AssertIntEQ(
  3156. EVP_EncodeUpdate(
  3157. ctx,
  3158. NULL, /* pass NULL as out buff */
  3159. &outl,
  3160. plain1,
  3161. sizeof(plain1)-1),
  3162. 0 /* expected result code 0: fail */
  3163. );
  3164. AssertIntEQ(
  3165. EVP_EncodeUpdate(
  3166. ctx,
  3167. encOutBuff,
  3168. NULL, /* pass NULL as outl */
  3169. plain1,
  3170. sizeof(plain1)-1),
  3171. 0 /* expected result code 0: fail */
  3172. );
  3173. AssertIntEQ(
  3174. EVP_EncodeUpdate(
  3175. ctx,
  3176. encOutBuff,
  3177. &outl,
  3178. NULL, /* pass NULL as in */
  3179. sizeof(plain1)-1),
  3180. 0 /* expected result code 0: fail */
  3181. );
  3182. AssertIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
  3183. /* meaningless parameter test */
  3184. AssertIntEQ(
  3185. EVP_EncodeUpdate(
  3186. ctx,
  3187. encOutBuff,
  3188. &outl,
  3189. plain1,
  3190. 0), /* pass zero input */
  3191. 1 /* expected result code 1: success */
  3192. );
  3193. /* very small data encoding test */
  3194. EVP_EncodeInit(ctx);
  3195. AssertIntEQ(
  3196. EVP_EncodeUpdate(
  3197. ctx,
  3198. encOutBuff,
  3199. &outl,
  3200. plain0,
  3201. sizeof(plain0)-1),
  3202. 1 /* expected result code 1: success */
  3203. );
  3204. AssertIntEQ(outl,0);
  3205. EVP_EncodeFinal(
  3206. ctx,
  3207. encOutBuff + outl,
  3208. &outl);
  3209. AssertIntEQ( outl, sizeof(enc0)-1);
  3210. AssertIntEQ(
  3211. XSTRNCMP(
  3212. (const char*)encOutBuff,
  3213. (const char*)enc0,sizeof(enc0) ),
  3214. 0);
  3215. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  3216. AssertIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
  3217. sizeof(enc0)-1);
  3218. AssertIntEQ(
  3219. XSTRNCMP(
  3220. (const char*)encOutBuff,
  3221. (const char*)enc0,sizeof(enc0) ),
  3222. 0);
  3223. /* pass small size( < 48bytes ) input, then make sure they are not
  3224. * encoded and just stored in ctx
  3225. */
  3226. EVP_EncodeInit(ctx);
  3227. total = 0;
  3228. outl = 0;
  3229. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  3230. AssertIntEQ(
  3231. EVP_EncodeUpdate(
  3232. ctx,
  3233. encOutBuff, /* buffer for output */
  3234. &outl, /* size of output */
  3235. plain1, /* input */
  3236. sizeof(plain1)-1), /* size of input */
  3237. 1); /* expected result code 1:success */
  3238. total += outl;
  3239. AssertIntEQ(outl, 0); /* no output expected */
  3240. AssertIntEQ(ctx->remaining, sizeof(plain1) -1);
  3241. AssertTrue(
  3242. XSTRNCMP((const char*)(ctx->data),
  3243. (const char*)plain1,
  3244. ctx->remaining) ==0 );
  3245. AssertTrue(encOutBuff[0] == 0);
  3246. /* call wolfSSL_EVP_EncodeUpdate again to make it encode
  3247. * the stored data and the new input together
  3248. */
  3249. AssertIntEQ(
  3250. EVP_EncodeUpdate(
  3251. ctx,
  3252. encOutBuff + outl, /* buffer for output */
  3253. &outl, /* size of output */
  3254. plain2, /* additional input */
  3255. sizeof(plain2) -1), /* size of additional input */
  3256. 1); /* expected result code 1:success */
  3257. total += outl;
  3258. AssertIntNE(outl, 0); /* some output is expected this time*/
  3259. AssertIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
  3260. AssertIntEQ(
  3261. XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
  3262. /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
  3263. EVP_EncodeFinal(
  3264. ctx,
  3265. encOutBuff + outl,
  3266. &outl);
  3267. total += outl;
  3268. AssertIntNE(total,0);
  3269. AssertIntNE(outl,0);
  3270. AssertIntEQ(XSTRNCMP(
  3271. (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
  3272. /* test with illeagal parameters */
  3273. outl = 1;
  3274. EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
  3275. AssertIntEQ(outl, 0);
  3276. outl = 1;
  3277. EVP_EncodeFinal(ctx, NULL, &outl);
  3278. AssertIntEQ(outl, 0);
  3279. EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
  3280. EVP_EncodeFinal(NULL, NULL, NULL);
  3281. EVP_ENCODE_CTX_free(ctx);
  3282. printf(resultFmt, passed);
  3283. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3284. }
  3285. static void test_wolfSSL_EVP_EncodeFinal(void)
  3286. {
  3287. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3288. printf(testingFmt, "wolfSSL_EVP_EncodeFinal()");
  3289. /* tests for wolfSSL_EVP_EncodeFinal are included in
  3290. * test_wolfSSL_EVP_EncodeUpdate
  3291. */
  3292. printf(resultFmt, passed);
  3293. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3294. }
  3295. static void test_wolfSSL_EVP_DecodeInit(void)
  3296. {
  3297. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  3298. EVP_ENCODE_CTX* ctx = NULL;
  3299. printf(testingFmt, "EVP_DecodeInit()");
  3300. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3301. AssertIntEQ( ctx->remaining,0);
  3302. AssertIntEQ( ctx->data[0],0);
  3303. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3304. /* make ctx dirty */
  3305. ctx->remaining = 10;
  3306. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  3307. EVP_DecodeInit(ctx);
  3308. AssertIntEQ( ctx->remaining,0);
  3309. AssertIntEQ( ctx->data[0],0);
  3310. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3311. EVP_ENCODE_CTX_free(ctx);
  3312. printf(resultFmt, passed);
  3313. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  3314. }
  3315. static void test_wolfSSL_EVP_DecodeUpdate(void)
  3316. {
  3317. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  3318. int outl;
  3319. unsigned char decOutBuff[300];
  3320. EVP_ENCODE_CTX* ctx;
  3321. static const unsigned char enc1[] =
  3322. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  3323. /* const unsigned char plain1[] =
  3324. {"This is a base64 decoding test."} */
  3325. printf(testingFmt, "EVP_DecodeUpdate()");
  3326. ctx = EVP_ENCODE_CTX_new();
  3327. EVP_DecodeInit(ctx);
  3328. /* illegal parameter tests */
  3329. /* pass NULL as ctx */
  3330. AssertIntEQ(
  3331. EVP_DecodeUpdate(
  3332. NULL, /* pass NULL as ctx */
  3333. decOutBuff,
  3334. &outl,
  3335. enc1,
  3336. sizeof(enc1)-1),
  3337. -1 /* expected result code -1: fail */
  3338. );
  3339. AssertIntEQ( outl, 0);
  3340. /* pass NULL as output */
  3341. AssertIntEQ(
  3342. EVP_DecodeUpdate(
  3343. ctx,
  3344. NULL, /* pass NULL as out buff */
  3345. &outl,
  3346. enc1,
  3347. sizeof(enc1)-1),
  3348. -1 /* expected result code -1: fail */
  3349. );
  3350. AssertIntEQ( outl, 0);
  3351. /* pass NULL as outl */
  3352. AssertIntEQ(
  3353. EVP_DecodeUpdate(
  3354. ctx,
  3355. decOutBuff,
  3356. NULL, /* pass NULL as outl */
  3357. enc1,
  3358. sizeof(enc1)-1),
  3359. -1 /* expected result code -1: fail */
  3360. );
  3361. /* pass NULL as input */
  3362. AssertIntEQ(
  3363. EVP_DecodeUpdate(
  3364. ctx,
  3365. decOutBuff,
  3366. &outl,
  3367. NULL, /* pass NULL as in */
  3368. sizeof(enc1)-1),
  3369. -1 /* expected result code -1: fail */
  3370. );
  3371. AssertIntEQ( outl, 0);
  3372. AssertIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
  3373. /* pass zero length input */
  3374. AssertIntEQ(
  3375. EVP_DecodeUpdate(
  3376. ctx,
  3377. decOutBuff,
  3378. &outl,
  3379. enc1,
  3380. 0), /* pass zero as input len */
  3381. 1 /* expected result code 1: success */
  3382. );
  3383. /* decode correct base64 string */
  3384. {
  3385. static const unsigned char enc2[] =
  3386. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  3387. static const unsigned char plain2[] =
  3388. {"This is a base64 decoding test."};
  3389. EVP_EncodeInit(ctx);
  3390. AssertIntEQ(
  3391. EVP_DecodeUpdate(
  3392. ctx,
  3393. decOutBuff,
  3394. &outl,
  3395. enc2,
  3396. sizeof(enc2)-1),
  3397. 0 /* expected result code 0: success */
  3398. );
  3399. AssertIntEQ(outl,sizeof(plain2) -1);
  3400. AssertIntEQ(
  3401. EVP_DecodeFinal(
  3402. ctx,
  3403. decOutBuff + outl,
  3404. &outl),
  3405. 1 /* expected result code 1: success */
  3406. );
  3407. AssertIntEQ(outl, 0); /* expected DecodeFinal outout no data */
  3408. AssertIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  3409. sizeof(plain2) -1 ),0);
  3410. AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
  3411. sizeof(plain2)-1);
  3412. AssertIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  3413. sizeof(plain2) -1 ),0);
  3414. }
  3415. /* decode correct base64 string which does not have '\n' in its last*/
  3416. {
  3417. static const unsigned char enc3[] =
  3418. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
  3419. static const unsigned char plain3[] =
  3420. {"This is a base64 decoding test."}; /* 31 chars */
  3421. EVP_EncodeInit(ctx);
  3422. AssertIntEQ(
  3423. EVP_DecodeUpdate(
  3424. ctx,
  3425. decOutBuff,
  3426. &outl,
  3427. enc3,
  3428. sizeof(enc3)-1),
  3429. 0 /* expected result code 0: success */
  3430. );
  3431. AssertIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
  3432. AssertIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  3433. sizeof(plain3) -1 ),0);
  3434. AssertIntEQ(
  3435. EVP_DecodeFinal(
  3436. ctx,
  3437. decOutBuff + outl,
  3438. &outl),
  3439. 1 /* expected result code 1: success */
  3440. );
  3441. AssertIntEQ(outl,0 );
  3442. AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
  3443. sizeof(plain3)-1);
  3444. AssertIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  3445. sizeof(plain3) -1 ),0);
  3446. }
  3447. /* decode string which has a padding char ('=') in the illegal position*/
  3448. {
  3449. static const unsigned char enc4[] =
  3450. {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
  3451. EVP_EncodeInit(ctx);
  3452. AssertIntEQ(
  3453. EVP_DecodeUpdate(
  3454. ctx,
  3455. decOutBuff,
  3456. &outl,
  3457. enc4,
  3458. sizeof(enc4)-1),
  3459. -1 /* expected result code -1: error */
  3460. );
  3461. AssertIntEQ(outl,0);
  3462. AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
  3463. }
  3464. /* small data decode test */
  3465. {
  3466. static const unsigned char enc00[] = {"VG"};
  3467. static const unsigned char enc01[] = {"g=\n"};
  3468. static const unsigned char plain4[] = {"Th"};
  3469. EVP_EncodeInit(ctx);
  3470. AssertIntEQ(
  3471. EVP_DecodeUpdate(
  3472. ctx,
  3473. decOutBuff,
  3474. &outl,
  3475. enc00,
  3476. sizeof(enc00)-1),
  3477. 1 /* expected result code 1: success */
  3478. );
  3479. AssertIntEQ(outl,0);
  3480. AssertIntEQ(
  3481. EVP_DecodeUpdate(
  3482. ctx,
  3483. decOutBuff + outl,
  3484. &outl,
  3485. enc01,
  3486. sizeof(enc01)-1),
  3487. 0 /* expected result code 0: success */
  3488. );
  3489. AssertIntEQ(outl,sizeof(plain4)-1);
  3490. /* test with illegal parameters */
  3491. AssertIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
  3492. AssertIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
  3493. AssertIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
  3494. AssertIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
  3495. EVP_DecodeFinal(
  3496. ctx,
  3497. decOutBuff + outl,
  3498. &outl);
  3499. AssertIntEQ( outl, 0);
  3500. AssertIntEQ(
  3501. XSTRNCMP(
  3502. (const char*)decOutBuff,
  3503. (const char*)plain4,sizeof(plain4)-1 ),
  3504. 0);
  3505. }
  3506. EVP_ENCODE_CTX_free(ctx);
  3507. printf(resultFmt, passed);
  3508. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  3509. }
  3510. static void test_wolfSSL_EVP_DecodeFinal(void)
  3511. {
  3512. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  3513. printf(testingFmt, "EVP_DecodeFinal()");
  3514. /* tests for wolfSSL_EVP_DecodeFinal are included in
  3515. * test_wolfSSL_EVP_DecodeUpdate
  3516. */
  3517. printf(resultFmt, passed);
  3518. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  3519. }
  3520. /* Test function for wolfSSL_EVP_get_cipherbynid.
  3521. */
  3522. #ifdef OPENSSL_EXTRA
  3523. static void test_wolfSSL_EVP_get_cipherbynid(void)
  3524. {
  3525. #ifndef NO_AES
  3526. const WOLFSSL_EVP_CIPHER* c;
  3527. c = wolfSSL_EVP_get_cipherbynid(419);
  3528. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  3529. defined(WOLFSSL_AES_128)
  3530. AssertNotNull(c);
  3531. AssertNotNull(strcmp("EVP_AES_128_CBC", c));
  3532. #else
  3533. AssertNull(c);
  3534. #endif
  3535. c = wolfSSL_EVP_get_cipherbynid(423);
  3536. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  3537. defined(WOLFSSL_AES_192)
  3538. AssertNotNull(c);
  3539. AssertNotNull(strcmp("EVP_AES_192_CBC", c));
  3540. #else
  3541. AssertNull(c);
  3542. #endif
  3543. c = wolfSSL_EVP_get_cipherbynid(427);
  3544. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  3545. defined(WOLFSSL_AES_256)
  3546. AssertNotNull(c);
  3547. AssertNotNull(strcmp("EVP_AES_256_CBC", c));
  3548. #else
  3549. AssertNull(c);
  3550. #endif
  3551. c = wolfSSL_EVP_get_cipherbynid(904);
  3552. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  3553. AssertNotNull(c);
  3554. AssertNotNull(strcmp("EVP_AES_128_CTR", c));
  3555. #else
  3556. AssertNull(c);
  3557. #endif
  3558. c = wolfSSL_EVP_get_cipherbynid(905);
  3559. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  3560. AssertNotNull(c);
  3561. AssertNotNull(strcmp("EVP_AES_192_CTR", c));
  3562. #else
  3563. AssertNull(c);
  3564. #endif
  3565. c = wolfSSL_EVP_get_cipherbynid(906);
  3566. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  3567. AssertNotNull(c);
  3568. AssertNotNull(strcmp("EVP_AES_256_CTR", c));
  3569. #else
  3570. AssertNull(c);
  3571. #endif
  3572. c = wolfSSL_EVP_get_cipherbynid(418);
  3573. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  3574. AssertNotNull(c);
  3575. AssertNotNull(strcmp("EVP_AES_128_ECB", c));
  3576. #else
  3577. AssertNull(c);
  3578. #endif
  3579. c = wolfSSL_EVP_get_cipherbynid(422);
  3580. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  3581. AssertNotNull(c);
  3582. AssertNotNull(strcmp("EVP_AES_192_ECB", c));
  3583. #else
  3584. AssertNull(c);
  3585. #endif
  3586. c = wolfSSL_EVP_get_cipherbynid(426);
  3587. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  3588. AssertNotNull(c);
  3589. AssertNotNull(strcmp("EVP_AES_256_ECB", c));
  3590. #else
  3591. AssertNull(c);
  3592. #endif
  3593. #endif /* !NO_AES */
  3594. #ifndef NO_DES3
  3595. AssertNotNull(strcmp("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  3596. #ifdef WOLFSSL_DES_ECB
  3597. AssertNotNull(strcmp("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  3598. #endif
  3599. AssertNotNull(strcmp("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  3600. #ifdef WOLFSSL_DES_ECB
  3601. AssertNotNull(strcmp("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  3602. #endif
  3603. #endif /* !NO_DES3 */
  3604. /* test for nid is out of range */
  3605. AssertNull(wolfSSL_EVP_get_cipherbynid(1));
  3606. }
  3607. static void test_wolfSSL_EVP_CIPHER_CTX(void)
  3608. {
  3609. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  3610. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  3611. const EVP_CIPHER *init = EVP_aes_128_cbc();
  3612. const EVP_CIPHER *test;
  3613. byte key[AES_BLOCK_SIZE] = {0};
  3614. byte iv[AES_BLOCK_SIZE] = {0};
  3615. AssertNotNull(ctx);
  3616. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  3617. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  3618. test = EVP_CIPHER_CTX_cipher(ctx);
  3619. AssertTrue(init == test);
  3620. AssertIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  3621. AssertIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  3622. AssertIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  3623. EVP_CIPHER_CTX_free(ctx);
  3624. /* test EVP_CIPHER_CTX_cleanup with NULL */
  3625. AssertIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
  3626. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  3627. }
  3628. #endif /* OPENSSL_EXTRA */
  3629. /*----------------------------------------------------------------------------*
  3630. | IO
  3631. *----------------------------------------------------------------------------*/
  3632. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  3633. !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
  3634. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  3635. #define HAVE_IO_TESTS_DEPENDENCIES
  3636. #endif
  3637. /* helper functions */
  3638. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  3639. #ifdef WOLFSSL_SESSION_EXPORT
  3640. #ifdef WOLFSSL_DTLS
  3641. /* set up function for sending session information */
  3642. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  3643. {
  3644. WOLFSSL_CTX* ctx = NULL;
  3645. WOLFSSL* ssl = NULL;
  3646. AssertNotNull(inSsl);
  3647. AssertNotNull(buf);
  3648. AssertIntNE(0, sz);
  3649. /* Set ctx to DTLS 1.2 */
  3650. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  3651. AssertNotNull(ctx);
  3652. ssl = wolfSSL_new(ctx);
  3653. AssertNotNull(ssl);
  3654. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  3655. wolfSSL_free(ssl);
  3656. wolfSSL_CTX_free(ctx);
  3657. (void)userCtx;
  3658. return WOLFSSL_SUCCESS;
  3659. }
  3660. #endif
  3661. /* returns negative value on fail and positive (including 0) on success */
  3662. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  3663. {
  3664. int ret, err, loop_count, count, timeout = 10;
  3665. char msg[] = "I hear you fa shizzle!";
  3666. char input[1024];
  3667. loop_count = ((func_args*)args)->argc;
  3668. #ifdef WOLFSSL_ASYNC_CRYPT
  3669. err = 0; /* Reset error */
  3670. #endif
  3671. do {
  3672. #ifdef WOLFSSL_ASYNC_CRYPT
  3673. if (err == WC_PENDING_E) {
  3674. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  3675. if (ret < 0) { break; } else if (ret == 0) { continue; }
  3676. }
  3677. #endif
  3678. ret = wolfSSL_accept(ssl);
  3679. err = wolfSSL_get_error(ssl, 0);
  3680. if (err == WOLFSSL_ERROR_WANT_READ ||
  3681. err == WOLFSSL_ERROR_WANT_WRITE) {
  3682. int select_ret;
  3683. err = WC_PENDING_E;
  3684. select_ret = tcp_select(*sockfd, timeout);
  3685. if (select_ret == TEST_TIMEOUT) {
  3686. return WOLFSSL_FATAL_ERROR;
  3687. }
  3688. }
  3689. } while (err == WC_PENDING_E);
  3690. if (ret != WOLFSSL_SUCCESS) {
  3691. char buff[WOLFSSL_MAX_ERROR_SZ];
  3692. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  3693. return ret;
  3694. }
  3695. for (count = 0; count < loop_count; count++) {
  3696. int select_ret;
  3697. select_ret = tcp_select(*sockfd, timeout);
  3698. if (select_ret == TEST_TIMEOUT) {
  3699. ret = WOLFSSL_FATAL_ERROR;
  3700. break;
  3701. }
  3702. do {
  3703. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  3704. if (ret > 0) {
  3705. input[ret] = '\0';
  3706. printf("Client message: %s\n", input);
  3707. }
  3708. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  3709. do {
  3710. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  3711. return WOLFSSL_FATAL_ERROR;
  3712. }
  3713. err = wolfSSL_get_error(ssl, ret);
  3714. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  3715. }
  3716. return ret;
  3717. }
  3718. #endif /* WOLFSSL_SESSION_EXPORT */
  3719. /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
  3720. #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  3721. defined(HAVE_AES_CBC)
  3722. typedef struct openssl_key_ctx {
  3723. byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
  3724. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  3725. byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
  3726. byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
  3727. } openssl_key_ctx;
  3728. static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
  3729. static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
  3730. static WC_INLINE int OpenSSLTicketInit(void)
  3731. {
  3732. int ret = wc_InitRng(&myOpenSSLKey_rng);
  3733. if (ret != 0) return ret;
  3734. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
  3735. sizeof(myOpenSSLKey_ctx.name));
  3736. if (ret != 0) return ret;
  3737. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
  3738. sizeof(myOpenSSLKey_ctx.key));
  3739. if (ret != 0) return ret;
  3740. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
  3741. sizeof(myOpenSSLKey_ctx.hmacKey));
  3742. if (ret != 0) return ret;
  3743. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
  3744. sizeof(myOpenSSLKey_ctx.iv));
  3745. if (ret != 0) return ret;
  3746. return 0;
  3747. }
  3748. static WC_INLINE int myTicketEncCbOpenSSL(WOLFSSL* ssl,
  3749. byte name[WOLFSSL_TICKET_NAME_SZ],
  3750. byte iv[WOLFSSL_TICKET_IV_SZ],
  3751. WOLFSSL_EVP_CIPHER_CTX *ectx,
  3752. WOLFSSL_HMAC_CTX *hctx, int enc) {
  3753. (void)ssl;
  3754. if (enc) {
  3755. XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
  3756. XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
  3757. }
  3758. else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
  3759. sizeof(myOpenSSLKey_ctx.name)) != 0 ||
  3760. XMEMCMP(iv, myOpenSSLKey_ctx.iv,
  3761. sizeof(myOpenSSLKey_ctx.iv)) != 0) {
  3762. return 0;
  3763. }
  3764. HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
  3765. if (enc)
  3766. EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  3767. else
  3768. EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  3769. return 1;
  3770. }
  3771. static WC_INLINE void OpenSSLTicketCleanup(void)
  3772. {
  3773. wc_FreeRng(&myOpenSSLKey_rng);
  3774. }
  3775. #endif
  3776. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  3777. #ifdef WC_SHA512_DIGEST_SIZE
  3778. #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
  3779. #else
  3780. #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
  3781. #endif
  3782. byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
  3783. byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
  3784. byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
  3785. byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
  3786. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  3787. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  3788. {
  3789. SOCKET_T sockfd = 0;
  3790. SOCKET_T clientfd = 0;
  3791. word16 port;
  3792. callback_functions* cbf;
  3793. WOLFSSL_CTX* ctx = 0;
  3794. WOLFSSL* ssl = 0;
  3795. func_args* opts = (func_args*)args;
  3796. char msg[] = "I hear you fa shizzle!";
  3797. char input[1024];
  3798. int idx;
  3799. int ret, err = 0;
  3800. int sharedCtx = 0;
  3801. int doUdp = 0;
  3802. SOCKADDR_IN_T cliAddr;
  3803. socklen_t cliLen;
  3804. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  3805. size_t msg_len = 0;
  3806. #endif
  3807. #ifdef WOLFSSL_TIRTOS
  3808. fdOpenSession(Task_self());
  3809. #endif
  3810. opts->return_code = TEST_FAIL;
  3811. cbf = opts->callbacks;
  3812. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  3813. if (cbf != NULL && cbf->ctx) {
  3814. ctx = cbf->ctx;
  3815. sharedCtx = 1;
  3816. }
  3817. else
  3818. #endif
  3819. {
  3820. WOLFSSL_METHOD* method = NULL;
  3821. if (cbf != NULL && cbf->method != NULL) {
  3822. method = cbf->method();
  3823. }
  3824. else {
  3825. method = wolfSSLv23_server_method();
  3826. }
  3827. ctx = wolfSSL_CTX_new(method);
  3828. }
  3829. if (ctx == NULL) {
  3830. goto done;
  3831. }
  3832. if (cbf == NULL || !cbf->ticNoInit) {
  3833. #if defined(HAVE_SESSION_TICKET) && \
  3834. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  3835. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  3836. OpenSSLTicketInit();
  3837. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  3838. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  3839. TicketInit();
  3840. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  3841. #endif
  3842. #endif
  3843. }
  3844. #if defined(USE_WINDOWS_API)
  3845. port = opts->signal->port;
  3846. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  3847. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  3848. /* Let tcp_listen assign port */
  3849. port = 0;
  3850. #else
  3851. /* Use default port */
  3852. port = wolfSSLPort;
  3853. #endif
  3854. if (cbf != NULL)
  3855. doUdp = cbf->doUdp;
  3856. /* do it here to detect failure */
  3857. tcp_accept(
  3858. &sockfd, &clientfd, opts, port, 0, doUdp, 0, 0, 1, 0, 0);
  3859. if (doUdp) {
  3860. cliLen = sizeof(cliAddr);
  3861. idx = (int)recvfrom(sockfd, input, sizeof(input), MSG_PEEK,
  3862. (struct sockaddr*)&cliAddr, &cliLen);
  3863. AssertIntGT(idx, 0);
  3864. }
  3865. else {
  3866. CloseSocket(sockfd);
  3867. }
  3868. wolfSSL_CTX_set_verify(ctx,
  3869. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  3870. #ifdef WOLFSSL_ENCRYPTED_KEYS
  3871. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  3872. #endif
  3873. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  3874. != WOLFSSL_SUCCESS) {
  3875. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  3876. goto done;
  3877. }
  3878. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  3879. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  3880. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3881. #else
  3882. if (wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  3883. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3884. #endif
  3885. /*err_sys("can't load server cert chain file, "
  3886. "Please run from wolfSSL home dir");*/
  3887. goto done;
  3888. }
  3889. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  3890. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  3891. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3892. #else
  3893. if (wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  3894. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3895. #endif
  3896. /*err_sys("can't load server key file, "
  3897. "Please run from wolfSSL home dir");*/
  3898. goto done;
  3899. }
  3900. /* call ctx setup callback */
  3901. if (cbf != NULL && cbf->ctx_ready != NULL) {
  3902. cbf->ctx_ready(ctx);
  3903. }
  3904. ssl = wolfSSL_new(ctx);
  3905. if (ssl == NULL) {
  3906. goto done;
  3907. }
  3908. if (doUdp) {
  3909. err = wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  3910. if (err != WOLFSSL_SUCCESS)
  3911. goto done;
  3912. }
  3913. #ifdef WOLFSSL_SESSION_EXPORT
  3914. /* only add in more complex nonblocking case with session export tests */
  3915. if (args && opts->argc > 0) {
  3916. /* set as nonblock and time out for waiting on read/write */
  3917. tcp_set_nonblocking(&clientfd);
  3918. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  3919. }
  3920. #endif
  3921. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  3922. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  3923. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3924. #else
  3925. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  3926. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3927. #endif
  3928. /*err_sys("can't load server cert chain file, "
  3929. "Please run from wolfSSL home dir");*/
  3930. goto done;
  3931. }
  3932. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  3933. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  3934. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3935. #else
  3936. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  3937. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3938. #endif
  3939. /*err_sys("can't load server key file, "
  3940. "Please run from wolfSSL home dir");*/
  3941. goto done;
  3942. }
  3943. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  3944. /*err_sys("SSL_set_fd failed");*/
  3945. goto done;
  3946. }
  3947. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  3948. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  3949. #elif !defined(NO_DH)
  3950. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  3951. #endif
  3952. /* call ssl setup callback */
  3953. if (cbf != NULL && cbf->ssl_ready != NULL) {
  3954. cbf->ssl_ready(ssl);
  3955. }
  3956. #ifdef WOLFSSL_SESSION_EXPORT
  3957. /* only add in more complex nonblocking case with session export tests */
  3958. if (opts->argc > 0) {
  3959. ret = nonblocking_accept_read(args, ssl, &clientfd);
  3960. if (ret >= 0) {
  3961. opts->return_code = TEST_SUCCESS;
  3962. }
  3963. #ifdef WOLFSSL_TIRTOS
  3964. Task_yield();
  3965. #endif
  3966. goto done;
  3967. }
  3968. #endif
  3969. #ifdef WOLFSSL_ASYNC_CRYPT
  3970. err = 0; /* Reset error */
  3971. #endif
  3972. do {
  3973. #ifdef WOLFSSL_ASYNC_CRYPT
  3974. if (err == WC_PENDING_E) {
  3975. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  3976. if (ret < 0) { break; } else if (ret == 0) { continue; }
  3977. }
  3978. #endif
  3979. ret = wolfSSL_accept(ssl);
  3980. err = wolfSSL_get_error(ssl, 0);
  3981. } while (err == WC_PENDING_E);
  3982. if (ret != WOLFSSL_SUCCESS) {
  3983. char buff[WOLFSSL_MAX_ERROR_SZ];
  3984. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  3985. /*err_sys("SSL_accept failed");*/
  3986. goto done;
  3987. }
  3988. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  3989. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  3990. msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
  3991. AssertIntGE(msg_len, 0);
  3992. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  3993. msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
  3994. AssertIntGE(msg_len, 0);
  3995. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  3996. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  3997. if (idx > 0) {
  3998. input[idx] = '\0';
  3999. printf("Client message: %s\n", input);
  4000. }
  4001. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  4002. /*err_sys("SSL_write failed");*/
  4003. #ifdef WOLFSSL_TIRTOS
  4004. return;
  4005. #else
  4006. return 0;
  4007. #endif
  4008. }
  4009. if (cbf != NULL && cbf->on_result != NULL)
  4010. cbf->on_result(ssl);
  4011. #ifdef WOLFSSL_TIRTOS
  4012. Task_yield();
  4013. #endif
  4014. opts->return_code = TEST_SUCCESS;
  4015. done:
  4016. wolfSSL_shutdown(ssl);
  4017. wolfSSL_free(ssl);
  4018. if (!sharedCtx)
  4019. wolfSSL_CTX_free(ctx);
  4020. CloseSocket(clientfd);
  4021. #ifdef WOLFSSL_TIRTOS
  4022. fdCloseSession(Task_self());
  4023. #endif
  4024. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4025. && defined(HAVE_THREAD_LS)
  4026. wc_ecc_fp_free(); /* free per thread cache */
  4027. #endif
  4028. if (cbf == NULL || !cbf->ticNoInit) {
  4029. #if defined(HAVE_SESSION_TICKET) && \
  4030. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  4031. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  4032. OpenSSLTicketCleanup();
  4033. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  4034. TicketCleanup();
  4035. #endif
  4036. #endif
  4037. }
  4038. #ifndef WOLFSSL_TIRTOS
  4039. return 0;
  4040. #endif
  4041. }
  4042. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  4043. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  4044. {
  4045. SOCKET_T sockfd = 0;
  4046. SOCKET_T clientfd = 0;
  4047. word16 port;
  4048. callback_functions* cbf;
  4049. WOLFSSL_CTX* ctx = 0;
  4050. WOLFSSL* ssl = 0;
  4051. char msg[] = "I hear you fa shizzle!";
  4052. char input[1024];
  4053. int idx;
  4054. int ret, err = 0;
  4055. int sharedCtx = 0;
  4056. int loop_count = ((func_args*)args)->argc;
  4057. int count = 0;
  4058. #ifdef WOLFSSL_TIRTOS
  4059. fdOpenSession(Task_self());
  4060. #endif
  4061. ((func_args*)args)->return_code = TEST_FAIL;
  4062. cbf = ((func_args*)args)->callbacks;
  4063. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4064. if (cbf != NULL && cbf->ctx) {
  4065. ctx = cbf->ctx;
  4066. sharedCtx = 1;
  4067. }
  4068. else
  4069. #endif
  4070. {
  4071. WOLFSSL_METHOD* method = NULL;
  4072. if (cbf != NULL && cbf->method != NULL) {
  4073. method = cbf->method();
  4074. }
  4075. else {
  4076. method = wolfSSLv23_server_method();
  4077. }
  4078. ctx = wolfSSL_CTX_new(method);
  4079. }
  4080. #if defined(USE_WINDOWS_API)
  4081. port = ((func_args*)args)->signal->port;
  4082. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  4083. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  4084. /* Let tcp_listen assign port */
  4085. port = 0;
  4086. #else
  4087. /* Use default port */
  4088. port = wolfSSLPort;
  4089. #endif
  4090. wolfSSL_CTX_set_verify(ctx,
  4091. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  4092. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4093. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4094. #endif
  4095. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  4096. != WOLFSSL_SUCCESS) {
  4097. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4098. goto done;
  4099. }
  4100. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4101. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4102. /*err_sys("can't load server cert chain file, "
  4103. "Please run from wolfSSL home dir");*/
  4104. goto done;
  4105. }
  4106. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4107. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4108. /*err_sys("can't load server key file, "
  4109. "Please run from wolfSSL home dir");*/
  4110. goto done;
  4111. }
  4112. /* call ctx setup callback */
  4113. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4114. cbf->ctx_ready(ctx);
  4115. }
  4116. while(count != loop_count) {
  4117. ssl = wolfSSL_new(ctx);
  4118. if (ssl == NULL) {
  4119. goto done;
  4120. }
  4121. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  4122. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4123. /*err_sys("can't load server cert chain file, "
  4124. "Please run from wolfSSL home dir");*/
  4125. goto done;
  4126. }
  4127. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  4128. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4129. /*err_sys("can't load server key file, "
  4130. "Please run from wolfSSL home dir");*/
  4131. goto done;
  4132. }
  4133. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  4134. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  4135. #elif !defined(NO_DH)
  4136. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  4137. #endif
  4138. /* call ssl setup callback */
  4139. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4140. cbf->ssl_ready(ssl);
  4141. }
  4142. /* do it here to detect failure */
  4143. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  4144. CloseSocket(sockfd);
  4145. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  4146. /*err_sys("SSL_set_fd failed");*/
  4147. goto done;
  4148. }
  4149. #ifdef WOLFSSL_ASYNC_CRYPT
  4150. err = 0; /* Reset error */
  4151. #endif
  4152. do {
  4153. #ifdef WOLFSSL_ASYNC_CRYPT
  4154. if (err == WC_PENDING_E) {
  4155. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4156. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4157. }
  4158. #endif
  4159. ret = wolfSSL_accept(ssl);
  4160. err = wolfSSL_get_error(ssl, 0);
  4161. } while (err == WC_PENDING_E);
  4162. if (ret != WOLFSSL_SUCCESS) {
  4163. char buff[WOLFSSL_MAX_ERROR_SZ];
  4164. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4165. /*err_sys("SSL_accept failed");*/
  4166. goto done;
  4167. }
  4168. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  4169. if (idx > 0) {
  4170. input[idx] = '\0';
  4171. printf("Client message: %s\n", input);
  4172. }
  4173. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  4174. /*err_sys("SSL_write failed");*/
  4175. #ifdef WOLFSSL_TIRTOS
  4176. return;
  4177. #else
  4178. return 0;
  4179. #endif
  4180. }
  4181. /* free ssl for this connection */
  4182. wolfSSL_shutdown(ssl);
  4183. wolfSSL_free(ssl); ssl = NULL;
  4184. CloseSocket(clientfd);
  4185. count++;
  4186. }
  4187. #ifdef WOLFSSL_TIRTOS
  4188. Task_yield();
  4189. #endif
  4190. ((func_args*)args)->return_code = TEST_SUCCESS;
  4191. done:
  4192. if(ssl != NULL) {
  4193. wolfSSL_shutdown(ssl);
  4194. wolfSSL_free(ssl);
  4195. }
  4196. if (!sharedCtx)
  4197. wolfSSL_CTX_free(ctx);
  4198. CloseSocket(clientfd);
  4199. #ifdef WOLFSSL_TIRTOS
  4200. fdCloseSession(Task_self());
  4201. #endif
  4202. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4203. && defined(HAVE_THREAD_LS)
  4204. wc_ecc_fp_free(); /* free per thread cache */
  4205. #endif
  4206. #ifndef WOLFSSL_TIRTOS
  4207. return 0;
  4208. #endif
  4209. }
  4210. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  4211. typedef int (*cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  4212. static void test_client_nofail(void* args, cbType cb)
  4213. {
  4214. SOCKET_T sockfd = 0;
  4215. callback_functions* cbf;
  4216. WOLFSSL_CTX* ctx = 0;
  4217. WOLFSSL* ssl = 0;
  4218. WOLFSSL_CIPHER* cipher;
  4219. char msg[64] = "hello wolfssl!";
  4220. char reply[1024];
  4221. int input;
  4222. int msgSz = (int)XSTRLEN(msg);
  4223. int ret, err = 0;
  4224. int cipherSuite;
  4225. int sharedCtx = 0;
  4226. const char* cipherName1, *cipherName2;
  4227. #ifdef WOLFSSL_TIRTOS
  4228. fdOpenSession(Task_self());
  4229. #endif
  4230. ((func_args*)args)->return_code = TEST_FAIL;
  4231. cbf = ((func_args*)args)->callbacks;
  4232. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4233. if (cbf != NULL && cbf->ctx) {
  4234. ctx = cbf->ctx;
  4235. sharedCtx = cbf->isSharedCtx;
  4236. }
  4237. else
  4238. #endif
  4239. {
  4240. WOLFSSL_METHOD* method = NULL;
  4241. if (cbf != NULL && cbf->method != NULL) {
  4242. method = cbf->method();
  4243. }
  4244. else {
  4245. method = wolfSSLv23_client_method();
  4246. }
  4247. ctx = wolfSSL_CTX_new(method);
  4248. }
  4249. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4250. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4251. #endif
  4252. /* Do connect here so server detects failures */
  4253. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  4254. 0, 0, NULL);
  4255. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  4256. {
  4257. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4258. goto done;
  4259. }
  4260. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4261. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  4262. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4263. #else
  4264. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  4265. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4266. #endif
  4267. /*err_sys("can't load client cert file, "
  4268. "Please run from wolfSSL home dir");*/
  4269. goto done;
  4270. }
  4271. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4272. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  4273. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4274. #else
  4275. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  4276. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4277. #endif
  4278. /*err_sys("can't load client key file, "
  4279. "Please run from wolfSSL home dir");*/
  4280. goto done;
  4281. }
  4282. /* call ctx setup callback */
  4283. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4284. cbf->ctx_ready(ctx);
  4285. }
  4286. ssl = wolfSSL_new(ctx);
  4287. if (ssl == NULL) {
  4288. goto done;
  4289. }
  4290. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4291. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  4292. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4293. #else
  4294. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  4295. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4296. #endif
  4297. /*err_sys("can't load client cert file, "
  4298. "Please run from wolfSSL home dir");*/
  4299. goto done;
  4300. }
  4301. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4302. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  4303. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4304. #else
  4305. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  4306. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4307. #endif
  4308. /*err_sys("can't load client key file, "
  4309. "Please run from wolfSSL home dir");*/
  4310. goto done;
  4311. }
  4312. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  4313. /*err_sys("SSL_set_fd failed");*/
  4314. goto done;
  4315. }
  4316. /* call ssl setup callback */
  4317. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4318. cbf->ssl_ready(ssl);
  4319. }
  4320. #ifdef WOLFSSL_ASYNC_CRYPT
  4321. err = 0; /* Reset error */
  4322. #endif
  4323. do {
  4324. #ifdef WOLFSSL_ASYNC_CRYPT
  4325. if (err == WC_PENDING_E) {
  4326. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4327. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4328. }
  4329. #endif
  4330. ret = wolfSSL_connect(ssl);
  4331. err = wolfSSL_get_error(ssl, 0);
  4332. } while (err == WC_PENDING_E);
  4333. if (ret != WOLFSSL_SUCCESS) {
  4334. char buff[WOLFSSL_MAX_ERROR_SZ];
  4335. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4336. /*err_sys("SSL_connect failed");*/
  4337. goto done;
  4338. }
  4339. /* test the various get cipher methods */
  4340. /* Internal cipher suite names */
  4341. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  4342. cipherName1 = wolfSSL_get_cipher_name(ssl);
  4343. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  4344. (cipherSuite >> 8), cipherSuite & 0xFF);
  4345. AssertStrEQ(cipherName1, cipherName2);
  4346. /* IANA Cipher Suites Names */
  4347. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  4348. then it's the internal cipher suite name */
  4349. cipher = wolfSSL_get_current_cipher(ssl);
  4350. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  4351. cipherName2 = wolfSSL_get_cipher(ssl);
  4352. AssertStrEQ(cipherName1, cipherName2);
  4353. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  4354. !defined(WOLFSSL_QT)
  4355. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  4356. (cipherSuite >> 8), cipherSuite & 0xFF);
  4357. AssertStrEQ(cipherName1, cipherName2);
  4358. #endif
  4359. if (cb != NULL)
  4360. (cb)(ctx, ssl);
  4361. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  4362. /*err_sys("SSL_write failed");*/
  4363. goto done;
  4364. }
  4365. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  4366. if (input > 0) {
  4367. reply[input] = '\0';
  4368. printf("Server response: %s\n", reply);
  4369. }
  4370. if (cbf != NULL && cbf->on_result != NULL)
  4371. cbf->on_result(ssl);
  4372. ((func_args*)args)->return_code = TEST_SUCCESS;
  4373. done:
  4374. wolfSSL_free(ssl);
  4375. if (!sharedCtx)
  4376. wolfSSL_CTX_free(ctx);
  4377. CloseSocket(sockfd);
  4378. #ifdef WOLFSSL_TIRTOS
  4379. fdCloseSession(Task_self());
  4380. #endif
  4381. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4382. && defined(HAVE_THREAD_LS)
  4383. wc_ecc_fp_free(); /* free per thread cache */
  4384. #endif
  4385. return;
  4386. }
  4387. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  4388. static void test_client_reuse_WOLFSSLobj(void* args, void *cb, void* server_args)
  4389. {
  4390. SOCKET_T sockfd = 0;
  4391. callback_functions* cbf;
  4392. WOLFSSL_CTX* ctx = 0;
  4393. WOLFSSL* ssl = 0;
  4394. WOLFSSL_SESSION* session = NULL;
  4395. char msg[64] = "hello wolfssl!";
  4396. char reply[1024];
  4397. int input;
  4398. int msgSz = (int)XSTRLEN(msg);
  4399. int ret, err = 0;
  4400. int sharedCtx = 0;
  4401. #ifdef WOLFSSL_TIRTOS
  4402. fdOpenSession(Task_self());
  4403. #endif
  4404. ((func_args*)args)->return_code = TEST_FAIL;
  4405. cbf = ((func_args*)args)->callbacks;
  4406. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4407. if (cbf != NULL && cbf->ctx) {
  4408. ctx = cbf->ctx;
  4409. sharedCtx = 1;
  4410. }
  4411. else
  4412. #endif
  4413. {
  4414. WOLFSSL_METHOD* method = NULL;
  4415. if (cbf != NULL && cbf->method != NULL) {
  4416. method = cbf->method();
  4417. }
  4418. else {
  4419. method = wolfSSLv23_client_method();
  4420. }
  4421. ctx = wolfSSL_CTX_new(method);
  4422. }
  4423. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4424. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4425. #endif
  4426. /* Do connect here so server detects failures */
  4427. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  4428. 0, 0, NULL);
  4429. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  4430. {
  4431. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4432. goto done;
  4433. }
  4434. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  4435. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4436. /*err_sys("can't load client cert file, "
  4437. "Please run from wolfSSL home dir");*/
  4438. goto done;
  4439. }
  4440. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  4441. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4442. /*err_sys("can't load client key file, "
  4443. "Please run from wolfSSL home dir");*/
  4444. goto done;
  4445. }
  4446. /* call ctx setup callback */
  4447. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4448. cbf->ctx_ready(ctx);
  4449. }
  4450. ssl = wolfSSL_new(ctx);
  4451. if (ssl == NULL) {
  4452. goto done;
  4453. }
  4454. /* keep handshakre resources for re-using WOLFSSL obj */
  4455. wolfSSL_KeepArrays(ssl);
  4456. if(wolfSSL_KeepHandshakeResources(ssl)) {
  4457. /* err_sys("SSL_KeepHandshakeResources failed"); */
  4458. goto done;
  4459. }
  4460. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  4461. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4462. /*err_sys("can't load client cert file, "
  4463. "Please run from wolfSSL home dir");*/
  4464. goto done;
  4465. }
  4466. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  4467. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4468. /*err_sys("can't load client key file, "
  4469. "Please run from wolfSSL home dir");*/
  4470. goto done;
  4471. }
  4472. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  4473. /*err_sys("SSL_set_fd failed");*/
  4474. goto done;
  4475. }
  4476. /* call ssl setup callback */
  4477. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4478. cbf->ssl_ready(ssl);
  4479. }
  4480. #ifdef WOLFSSL_ASYNC_CRYPT
  4481. err = 0; /* Reset error */
  4482. #endif
  4483. do {
  4484. #ifdef WOLFSSL_ASYNC_CRYPT
  4485. if (err == WC_PENDING_E) {
  4486. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4487. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4488. }
  4489. #endif
  4490. ret = wolfSSL_connect(ssl);
  4491. err = wolfSSL_get_error(ssl, 0);
  4492. } while (err == WC_PENDING_E);
  4493. if (ret != WOLFSSL_SUCCESS) {
  4494. char buff[WOLFSSL_MAX_ERROR_SZ];
  4495. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4496. /*err_sys("SSL_connect failed");*/
  4497. goto done;
  4498. }
  4499. /* Build first session */
  4500. if (cb != NULL)
  4501. ((cbType)cb)(ctx, ssl);
  4502. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  4503. /*err_sys("SSL_write failed");*/
  4504. goto done;
  4505. }
  4506. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  4507. if (input > 0) {
  4508. reply[input] = '\0';
  4509. printf("Server response: %s\n", reply);
  4510. }
  4511. /* Session Resumption by re-using WOLFSSL object */
  4512. wolfSSL_set_quiet_shutdown(ssl, 1);
  4513. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  4514. /* err_sys ("SSL shutdown failed"); */
  4515. goto done;
  4516. }
  4517. session = wolfSSL_get1_session(ssl);
  4518. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  4519. /* err_sys ("SSL_clear failed"); */
  4520. goto done;
  4521. }
  4522. wolfSSL_set_session(ssl, session);
  4523. wolfSSL_SESSION_free(session);
  4524. session = NULL;
  4525. /* close socket once */
  4526. CloseSocket(sockfd);
  4527. sockfd = 0;
  4528. /* wait until server ready */
  4529. wait_tcp_ready((func_args*)server_args);
  4530. printf("session resumption\n");
  4531. /* Do re-connect */
  4532. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  4533. 0, 0, NULL);
  4534. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  4535. /*err_sys("SSL_set_fd failed");*/
  4536. goto done;
  4537. }
  4538. #ifdef WOLFSSL_ASYNC_CRYPT
  4539. err = 0; /* Reset error */
  4540. #endif
  4541. do {
  4542. #ifdef WOLFSSL_ASYNC_CRYPT
  4543. if (err == WC_PENDING_E) {
  4544. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4545. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4546. }
  4547. #endif
  4548. ret = wolfSSL_connect(ssl);
  4549. err = wolfSSL_get_error(ssl, 0);
  4550. } while (err == WC_PENDING_E);
  4551. if (ret != WOLFSSL_SUCCESS) {
  4552. char buff[WOLFSSL_MAX_ERROR_SZ];
  4553. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4554. /*err_sys("SSL_connect failed");*/
  4555. goto done;
  4556. }
  4557. /* Build first session */
  4558. if (cb != NULL)
  4559. ((cbType)cb)(ctx, ssl);
  4560. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  4561. /*err_sys("SSL_write failed");*/
  4562. goto done;
  4563. }
  4564. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  4565. if (input > 0) {
  4566. reply[input] = '\0';
  4567. printf("Server response: %s\n", reply);
  4568. }
  4569. ((func_args*)args)->return_code = TEST_SUCCESS;
  4570. done:
  4571. wolfSSL_free(ssl);
  4572. if (!sharedCtx)
  4573. wolfSSL_CTX_free(ctx);
  4574. CloseSocket(sockfd);
  4575. #ifdef WOLFSSL_TIRTOS
  4576. fdCloseSession(Task_self());
  4577. #endif
  4578. return;
  4579. }
  4580. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  4581. static void test_client_verifyDepth(void* args)
  4582. {
  4583. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT)
  4584. SOCKET_T sockfd = 0;
  4585. callback_functions* cbf;
  4586. WOLFSSL_CTX* ctx = 0;
  4587. WOLFSSL* ssl = 0;
  4588. char msg[64] = "hello wolfssl!";
  4589. char reply[1024];
  4590. int input;
  4591. int msgSz = (int)XSTRLEN(msg);
  4592. int ret, err = 0;
  4593. int verify_depth = ((func_args*)args)->argc;
  4594. ((func_args*)args)->return_code = TEST_FAIL;
  4595. cbf = ((func_args*)args)->callbacks;
  4596. {
  4597. WOLFSSL_METHOD* method = NULL;
  4598. if (cbf != NULL && cbf->method != NULL) {
  4599. method = cbf->method();
  4600. }
  4601. else {
  4602. method = wolfSSLv23_client_method();
  4603. }
  4604. ctx = wolfSSL_CTX_new(method);
  4605. }
  4606. /* Do connect here so server detects failures */
  4607. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  4608. 0, 0, NULL);
  4609. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0)
  4610. != WOLFSSL_SUCCESS)
  4611. {
  4612. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4613. goto done;
  4614. }
  4615. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  4616. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4617. /*err_sys("can't load client cert file, "
  4618. "Please run from wolfSSL home dir");*/
  4619. goto done;
  4620. }
  4621. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  4622. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4623. /*err_sys("can't load client key file, "
  4624. "Please run from wolfSSL home dir");*/
  4625. goto done;
  4626. }
  4627. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  4628. /* set verify depth */
  4629. if (verify_depth == 0) {
  4630. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  4631. SSL_CTX_set_verify_depth(ctx, verify_depth);
  4632. } else if (verify_depth == -1) {
  4633. myVerifyAction = VERIFY_USE_PREVERFIY;
  4634. SSL_CTX_set_verify_depth(ctx, 0);
  4635. } else if (verify_depth > 0) {
  4636. myVerifyAction = VERIFY_USE_PREVERFIY;
  4637. SSL_CTX_set_verify_depth(ctx, verify_depth);
  4638. }
  4639. ssl = wolfSSL_new(ctx);
  4640. if (ssl == NULL) {
  4641. goto done;
  4642. }
  4643. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  4644. /*err_sys("SSL_set_fd failed");*/
  4645. goto done;
  4646. }
  4647. #ifdef WOLFSSL_ASYNC_CRYPT
  4648. err = 0; /* Reset error */
  4649. #endif
  4650. do {
  4651. #ifdef WOLFSSL_ASYNC_CRYPT
  4652. if (err == WC_PENDING_E) {
  4653. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4654. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4655. }
  4656. #endif
  4657. ret = wolfSSL_connect(ssl);
  4658. err = wolfSSL_get_error(ssl, 0);
  4659. } while (err == WC_PENDING_E);
  4660. if (ret != WOLFSSL_SUCCESS) {
  4661. char buff[WOLFSSL_MAX_ERROR_SZ];
  4662. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4663. goto done;
  4664. }
  4665. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  4666. goto done;
  4667. }
  4668. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  4669. if (input > 0) {
  4670. reply[input] = '\0';
  4671. printf("Server response: %s\n", reply);
  4672. }
  4673. ((func_args*)args)->return_code = TEST_SUCCESS;
  4674. done:
  4675. wolfSSL_free(ssl);
  4676. wolfSSL_CTX_free(ctx);
  4677. CloseSocket(sockfd);
  4678. #else
  4679. (void)args;
  4680. #endif /* defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT) */
  4681. }
  4682. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  4683. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
  4684. defined(HAVE_ALPN) && defined(HAVE_SNI) && \
  4685. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
  4686. #define HAVE_ALPN_PROTOS_SUPPORT
  4687. #endif
  4688. /* Generic TLS client / server with callbacks for API unit tests
  4689. * Used by SNI / ALPN / crypto callback helper functions */
  4690. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  4691. (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
  4692. defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
  4693. #define ENABLE_TLS_CALLBACK_TEST
  4694. #endif
  4695. #if defined(ENABLE_TLS_CALLBACK_TEST) || \
  4696. (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
  4697. /* TLS server for API unit testing - generic */
  4698. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  4699. {
  4700. callback_functions* callbacks = ((func_args*)args)->callbacks;
  4701. WOLFSSL_CTX* ctx = NULL;
  4702. WOLFSSL* ssl = NULL;
  4703. SOCKET_T sfd = 0;
  4704. SOCKET_T cfd = 0;
  4705. word16 port;
  4706. char msg[] = "I hear you fa shizzle!";
  4707. int len = (int) XSTRLEN(msg);
  4708. char input[1024];
  4709. int idx;
  4710. int ret, err = 0;
  4711. ((func_args*)args)->return_code = TEST_FAIL;
  4712. #ifdef WOLFSSL_STATIC_MEMORY
  4713. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  4714. callbacks->memSz > 0) {
  4715. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  4716. callbacks->mem, callbacks->memSz, 0, 1);
  4717. if (ret != WOLFSSL_SUCCESS) {
  4718. printf("CTX static new failed %d\n", ret);
  4719. return 0;
  4720. }
  4721. }
  4722. #endif
  4723. if (ctx == NULL) {
  4724. ctx = wolfSSL_CTX_new(callbacks->method());
  4725. }
  4726. if (ctx == NULL) {
  4727. printf("CTX new failed\n");
  4728. return 0;
  4729. }
  4730. /* set defaults */
  4731. if (callbacks->caPemFile == NULL)
  4732. callbacks->caPemFile = cliCertFile;
  4733. if (callbacks->certPemFile == NULL)
  4734. callbacks->certPemFile = svrCertFile;
  4735. if (callbacks->keyPemFile == NULL)
  4736. callbacks->keyPemFile = svrKeyFile;
  4737. #ifdef WOLFSSL_TIRTOS
  4738. fdOpenSession(Task_self());
  4739. #endif
  4740. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  4741. #if defined(USE_WINDOWS_API)
  4742. port = ((func_args*)args)->signal->port;
  4743. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  4744. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  4745. /* Let tcp_listen assign port */
  4746. port = 0;
  4747. #else
  4748. /* Use default port */
  4749. port = wolfSSLPort;
  4750. #endif
  4751. wolfSSL_CTX_set_verify(ctx,
  4752. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  4753. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4754. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4755. #endif
  4756. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  4757. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_dtls_set_export(ctx, test_export));
  4758. #endif
  4759. AssertIntEQ(WOLFSSL_SUCCESS,
  4760. wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0));
  4761. AssertIntEQ(WOLFSSL_SUCCESS,
  4762. wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  4763. WOLFSSL_FILETYPE_PEM));
  4764. AssertIntEQ(WOLFSSL_SUCCESS,
  4765. wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  4766. WOLFSSL_FILETYPE_PEM));
  4767. if (callbacks->ctx_ready)
  4768. callbacks->ctx_ready(ctx);
  4769. ssl = wolfSSL_new(ctx);
  4770. if (ssl == NULL) {
  4771. printf("SSL new failed\n");
  4772. wolfSSL_CTX_free(ctx);
  4773. return 0;
  4774. }
  4775. if (wolfSSL_dtls(ssl)) {
  4776. SOCKADDR_IN_T cliAddr;
  4777. socklen_t cliLen;
  4778. cliLen = sizeof(cliAddr);
  4779. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  4780. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  4781. (struct sockaddr*)&cliAddr, &cliLen);
  4782. AssertIntGT(idx, 0);
  4783. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  4784. }
  4785. else {
  4786. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  4787. CloseSocket(sfd);
  4788. }
  4789. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  4790. if (callbacks->loadToSSL) {
  4791. wolfSSL_SetDevId(ssl, callbacks->devId);
  4792. AssertIntEQ(WOLFSSL_SUCCESS,
  4793. wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  4794. WOLFSSL_FILETYPE_PEM));
  4795. AssertIntEQ(WOLFSSL_SUCCESS,
  4796. wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  4797. WOLFSSL_FILETYPE_PEM));
  4798. }
  4799. #ifdef NO_PSK
  4800. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  4801. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  4802. #elif !defined(NO_DH)
  4803. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  4804. #endif
  4805. #endif
  4806. if (callbacks->ssl_ready)
  4807. callbacks->ssl_ready(ssl);
  4808. #ifdef WOLFSSL_ASYNC_CRYPT
  4809. err = 0; /* Reset error */
  4810. #endif
  4811. do {
  4812. #ifdef WOLFSSL_ASYNC_CRYPT
  4813. if (err == WC_PENDING_E) {
  4814. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4815. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4816. }
  4817. #endif
  4818. ret = wolfSSL_accept(ssl);
  4819. err = wolfSSL_get_error(ssl, 0);
  4820. } while (err == WC_PENDING_E);
  4821. if (ret != WOLFSSL_SUCCESS) {
  4822. char buff[WOLFSSL_MAX_ERROR_SZ];
  4823. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4824. /*err_sys("SSL_accept failed");*/
  4825. }
  4826. else {
  4827. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  4828. input[idx] = 0;
  4829. printf("Client message: %s\n", input);
  4830. }
  4831. AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
  4832. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
  4833. defined(WOLFSSL_DTLS)
  4834. if (wolfSSL_dtls(ssl)) {
  4835. byte* import;
  4836. word32 sz;
  4837. wolfSSL_dtls_export(ssl, NULL, &sz);
  4838. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4839. AssertNotNull(import);
  4840. idx = wolfSSL_dtls_export(ssl, import, &sz);
  4841. AssertIntGE(idx, 0);
  4842. AssertIntGE(wolfSSL_dtls_import(ssl, import, idx), 0);
  4843. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4844. }
  4845. #endif
  4846. #ifdef WOLFSSL_TIRTOS
  4847. Task_yield();
  4848. #endif
  4849. ((func_args*)args)->return_code = TEST_SUCCESS;
  4850. }
  4851. if (callbacks->on_result)
  4852. callbacks->on_result(ssl);
  4853. wolfSSL_shutdown(ssl);
  4854. wolfSSL_free(ssl);
  4855. wolfSSL_CTX_free(ctx);
  4856. CloseSocket(cfd);
  4857. #ifdef WOLFSSL_TIRTOS
  4858. fdCloseSession(Task_self());
  4859. #endif
  4860. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4861. && defined(HAVE_THREAD_LS)
  4862. wc_ecc_fp_free(); /* free per thread cache */
  4863. #endif
  4864. #ifndef WOLFSSL_TIRTOS
  4865. return 0;
  4866. #endif
  4867. }
  4868. /* TLS Client for API unit testing - generic */
  4869. static void run_wolfssl_client(void* args)
  4870. {
  4871. callback_functions* callbacks = ((func_args*)args)->callbacks;
  4872. WOLFSSL_CTX* ctx = NULL;
  4873. WOLFSSL* ssl = NULL;
  4874. SOCKET_T sfd = 0;
  4875. char msg[] = "hello wolfssl server!";
  4876. int len = (int) XSTRLEN(msg);
  4877. char input[1024];
  4878. int ret, err = 0;
  4879. ((func_args*)args)->return_code = TEST_FAIL;
  4880. /* set defaults */
  4881. if (callbacks->caPemFile == NULL)
  4882. callbacks->caPemFile = caCertFile;
  4883. if (callbacks->certPemFile == NULL)
  4884. callbacks->certPemFile = cliCertFile;
  4885. if (callbacks->keyPemFile == NULL)
  4886. callbacks->keyPemFile = cliKeyFile;
  4887. #ifdef WOLFSSL_STATIC_MEMORY
  4888. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  4889. callbacks->memSz > 0) {
  4890. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  4891. callbacks->mem, callbacks->memSz, 0, 1);
  4892. if (ret != WOLFSSL_SUCCESS) {
  4893. printf("CTX static new failed %d\n", ret);
  4894. return;
  4895. }
  4896. }
  4897. #endif
  4898. if (ctx == NULL) {
  4899. ctx = wolfSSL_CTX_new(callbacks->method());
  4900. }
  4901. if (ctx == NULL) {
  4902. printf("CTX new failed\n");
  4903. return;
  4904. }
  4905. #ifdef WOLFSSL_TIRTOS
  4906. fdOpenSession(Task_self());
  4907. #endif
  4908. if (!callbacks->loadToSSL) {
  4909. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  4910. }
  4911. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4912. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4913. #endif
  4914. AssertIntEQ(WOLFSSL_SUCCESS,
  4915. wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0));
  4916. if (!callbacks->loadToSSL) {
  4917. AssertIntEQ(WOLFSSL_SUCCESS,
  4918. wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  4919. WOLFSSL_FILETYPE_PEM));
  4920. AssertIntEQ(WOLFSSL_SUCCESS,
  4921. wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  4922. WOLFSSL_FILETYPE_PEM));
  4923. }
  4924. if (callbacks->ctx_ready)
  4925. callbacks->ctx_ready(ctx);
  4926. ssl = wolfSSL_new(ctx);
  4927. if (wolfSSL_dtls(ssl)) {
  4928. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  4929. 1, 0, ssl);
  4930. }
  4931. else {
  4932. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  4933. 0, 0, ssl);
  4934. }
  4935. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  4936. if (callbacks->loadToSSL) {
  4937. wolfSSL_SetDevId(ssl, callbacks->devId);
  4938. AssertIntEQ(WOLFSSL_SUCCESS,
  4939. wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  4940. WOLFSSL_FILETYPE_PEM));
  4941. AssertIntEQ(WOLFSSL_SUCCESS,
  4942. wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  4943. WOLFSSL_FILETYPE_PEM));
  4944. }
  4945. if (callbacks->ssl_ready)
  4946. callbacks->ssl_ready(ssl);
  4947. #ifdef WOLFSSL_ASYNC_CRYPT
  4948. err = 0; /* Reset error */
  4949. #endif
  4950. do {
  4951. #ifdef WOLFSSL_ASYNC_CRYPT
  4952. if (err == WC_PENDING_E) {
  4953. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4954. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4955. }
  4956. #endif
  4957. ret = wolfSSL_connect(ssl);
  4958. err = wolfSSL_get_error(ssl, 0);
  4959. } while (err == WC_PENDING_E);
  4960. if (ret != WOLFSSL_SUCCESS) {
  4961. char buff[WOLFSSL_MAX_ERROR_SZ];
  4962. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4963. /*err_sys("SSL_connect failed");*/
  4964. }
  4965. else {
  4966. #ifdef WOLFSSL_ASYNC_CRYPT
  4967. err = 0; /* Reset error */
  4968. #endif
  4969. do {
  4970. #ifdef WOLFSSL_ASYNC_CRYPT
  4971. if (err == WC_PENDING_E) {
  4972. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4973. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4974. }
  4975. #endif
  4976. ret = wolfSSL_write(ssl, msg, len);
  4977. err = wolfSSL_get_error(ssl, 0);
  4978. } while (err == WC_PENDING_E);
  4979. AssertIntEQ(len, ret);
  4980. #ifdef WOLFSSL_ASYNC_CRYPT
  4981. err = 0; /* Reset error */
  4982. #endif
  4983. do {
  4984. #ifdef WOLFSSL_ASYNC_CRYPT
  4985. if (err == WC_PENDING_E) {
  4986. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4987. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4988. }
  4989. #endif
  4990. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  4991. err = wolfSSL_get_error(ssl, 0);
  4992. } while (err == WC_PENDING_E);
  4993. if (ret > 0) {
  4994. input[ret] = '\0'; /* null term */
  4995. printf("Server response: %s\n", input);
  4996. }
  4997. ((func_args*)args)->return_code = TEST_SUCCESS;
  4998. }
  4999. if (callbacks->on_result)
  5000. callbacks->on_result(ssl);
  5001. wolfSSL_free(ssl);
  5002. wolfSSL_CTX_free(ctx);
  5003. CloseSocket(sfd);
  5004. #ifdef WOLFSSL_TIRTOS
  5005. fdCloseSession(Task_self());
  5006. #endif
  5007. }
  5008. #endif /* ENABLE_TLS_CALLBACK_TEST */
  5009. static void test_wolfSSL_read_write(void)
  5010. {
  5011. /* The unit testing for read and write shall happen simultaneously, since
  5012. * one can't do anything with one without the other. (Except for a failure
  5013. * test case.) This function will call all the others that will set up,
  5014. * execute, and report their test findings.
  5015. *
  5016. * Set up the success case first. This function will become the template
  5017. * for the other tests. This should eventually be renamed
  5018. *
  5019. * The success case isn't interesting, how can this fail?
  5020. * - Do not give the client context a CA certificate. The connect should
  5021. * fail. Do not need server for this?
  5022. * - Using NULL for the ssl object on server. Do not need client for this.
  5023. * - Using NULL for the ssl object on client. Do not need server for this.
  5024. * - Good ssl objects for client and server. Client write() without server
  5025. * read().
  5026. * - Good ssl objects for client and server. Server write() without client
  5027. * read().
  5028. * - Forgetting the password callback?
  5029. */
  5030. tcp_ready ready;
  5031. func_args client_args;
  5032. func_args server_args;
  5033. THREAD_TYPE serverThread;
  5034. XMEMSET(&client_args, 0, sizeof(func_args));
  5035. XMEMSET(&server_args, 0, sizeof(func_args));
  5036. #ifdef WOLFSSL_TIRTOS
  5037. fdOpenSession(Task_self());
  5038. #endif
  5039. StartTCP();
  5040. InitTcpReady(&ready);
  5041. #if defined(USE_WINDOWS_API)
  5042. /* use RNG to get random port if using windows */
  5043. ready.port = GetRandomPort();
  5044. #endif
  5045. server_args.signal = &ready;
  5046. client_args.signal = &ready;
  5047. start_thread(test_server_nofail, &server_args, &serverThread);
  5048. wait_tcp_ready(&server_args);
  5049. test_client_nofail(&client_args, NULL);
  5050. join_thread(serverThread);
  5051. AssertTrue(client_args.return_code);
  5052. AssertTrue(server_args.return_code);
  5053. FreeTcpReady(&ready);
  5054. #ifdef WOLFSSL_TIRTOS
  5055. fdOpenSession(Task_self());
  5056. #endif
  5057. }
  5058. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  5059. static void test_wolfSSL_reuse_WOLFSSLobj(void)
  5060. {
  5061. /* The unit test for session resumption by re-using WOLFSSL object.
  5062. * WOLFSSL object is not cleared after first session. It re-use the obeject
  5063. * for second connection.
  5064. */
  5065. tcp_ready ready;
  5066. func_args client_args;
  5067. func_args server_args;
  5068. THREAD_TYPE serverThread;
  5069. XMEMSET(&client_args, 0, sizeof(func_args));
  5070. XMEMSET(&server_args, 0, sizeof(func_args));
  5071. #ifdef WOLFSSL_TIRTOS
  5072. fdOpenSession(Task_self());
  5073. #endif
  5074. StartTCP();
  5075. InitTcpReady(&ready);
  5076. #if defined(USE_WINDOWS_API)
  5077. /* use RNG to get random port if using windows */
  5078. ready.port = GetRandomPort();
  5079. #endif
  5080. server_args.signal = &ready;
  5081. client_args.signal = &ready;
  5082. /* the var is used for loop number */
  5083. server_args.argc = 2;
  5084. start_thread(test_server_loop, &server_args, &serverThread);
  5085. wait_tcp_ready(&server_args);
  5086. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  5087. join_thread(serverThread);
  5088. AssertTrue(client_args.return_code);
  5089. AssertTrue(server_args.return_code);
  5090. FreeTcpReady(&ready);
  5091. #ifdef WOLFSSL_TIRTOS
  5092. fdOpenSession(Task_self());
  5093. #endif
  5094. }
  5095. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  5096. static void test_wolfSSL_CTX_verifyDepth_ServerClient(void)
  5097. {
  5098. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT)
  5099. /* This unit test is to check set verify Depth */
  5100. tcp_ready ready;
  5101. func_args client_args;
  5102. func_args server_args;
  5103. THREAD_TYPE serverThread;
  5104. callback_functions client_cbf;
  5105. XMEMSET(&client_args, 0, sizeof(func_args));
  5106. XMEMSET(&server_args, 0, sizeof(func_args));
  5107. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  5108. printf(testingFmt, "test_wolfSSL_CTX_verifyDepth_ServerClient()\n");
  5109. #ifdef WOLFSSL_TLS13
  5110. client_cbf.method = wolfTLSv1_3_client_method;
  5111. #endif /* WOLFSSL_TLS13 */
  5112. client_args.callbacks = &client_cbf;
  5113. StartTCP();
  5114. InitTcpReady(&ready);
  5115. #if defined(USE_WINDOWS_API)
  5116. /* use RNG to get random port if using windows */
  5117. ready.port = GetRandomPort();
  5118. #endif
  5119. server_args.signal = &ready;
  5120. client_args.signal = &ready;
  5121. /* the var is used for loop number */
  5122. server_args.argc = 1;
  5123. /* test case 1 verify depth is equal to peer chain */
  5124. {
  5125. start_thread(test_server_nofail, &server_args, &serverThread);
  5126. wait_tcp_ready(&server_args);
  5127. /* the var is used for verify depth */
  5128. client_args.argc = 2;
  5129. test_client_verifyDepth(&client_args);
  5130. join_thread(serverThread);
  5131. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5132. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5133. }
  5134. /* test case 2
  5135. * verify depth is zero, number of peer's chain is 2.
  5136. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  5137. * callback.
  5138. */
  5139. /* the var is used for verify depth 0 and VERIFY_OVERRIDE_ERROR */
  5140. {
  5141. start_thread(test_server_nofail, &server_args, &serverThread);
  5142. wait_tcp_ready(&server_args);
  5143. client_args.argc = 0;
  5144. test_client_verifyDepth(&client_args);
  5145. join_thread(serverThread);
  5146. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5147. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5148. }
  5149. /* test case 3
  5150. * verify depth is zero, number of peer's chain is 2
  5151. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  5152. * therefore, handshake becomes failure.
  5153. */
  5154. /* the var is used for verify depth 0 and VERIFY_USE_PREVERFIY */
  5155. {
  5156. start_thread(test_server_nofail, &server_args, &serverThread);
  5157. wait_tcp_ready(&server_args);
  5158. client_args.argc = -1;
  5159. test_client_verifyDepth(&client_args);
  5160. join_thread(serverThread);
  5161. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5162. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5163. }
  5164. FreeTcpReady(&ready);
  5165. printf(resultFmt, passed);
  5166. #else
  5167. (void)test_client_verifyDepth;
  5168. #endif /* (OPENSSL_EXTRA) && !(WOLFSSL_TIRTOS) && (NO_WOLFSSL_CLIENT) */
  5169. }
  5170. static void test_client_get_finished(void* args, cbType cb)
  5171. {
  5172. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5173. SOCKET_T sockfd = 0;
  5174. callback_functions* cbf;
  5175. WOLFSSL_CTX* ctx = 0;
  5176. WOLFSSL* ssl = 0;
  5177. char msg[64] = "hello wolfssl!";
  5178. char reply[1024];
  5179. int msgSz = (int)XSTRLEN(msg);
  5180. int ret, err = 0;
  5181. WOLFSSL_METHOD* method = NULL;
  5182. size_t msg_len = 0;
  5183. (void) args;
  5184. (void) cb;
  5185. ((func_args*)args)->return_code = TEST_FAIL;
  5186. cbf = ((func_args*)args)->callbacks;
  5187. if (cbf != NULL && cbf->method != NULL) {
  5188. method = cbf->method();
  5189. }
  5190. else {
  5191. method = wolfSSLv23_client_method();
  5192. }
  5193. ctx = wolfSSL_CTX_new(method);
  5194. /* Do connect here so server detects failures */
  5195. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5196. 0, 0, NULL);
  5197. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  5198. {
  5199. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5200. goto done;
  5201. }
  5202. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5203. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5204. goto done;
  5205. }
  5206. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5207. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5208. goto done;
  5209. }
  5210. /* call ctx setup callback */
  5211. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5212. cbf->ctx_ready(ctx);
  5213. }
  5214. ssl = wolfSSL_new(ctx);
  5215. if (ssl == NULL) {
  5216. goto done;
  5217. }
  5218. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5219. goto done;
  5220. }
  5221. /* call ssl setup callback */
  5222. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5223. cbf->ssl_ready(ssl);
  5224. }
  5225. #ifdef WOLFSSL_ASYNC_CRYPT
  5226. err = 0; /* Reset error */
  5227. #endif
  5228. do {
  5229. #ifdef WOLFSSL_ASYNC_CRYPT
  5230. if (err == WC_PENDING_E) {
  5231. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5232. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5233. }
  5234. #endif
  5235. ret = wolfSSL_connect(ssl);
  5236. err = wolfSSL_get_error(ssl, 0);
  5237. } while (err == WC_PENDING_E);
  5238. if (ret != WOLFSSL_SUCCESS) {
  5239. char buff[WOLFSSL_MAX_ERROR_SZ];
  5240. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  5241. goto done;
  5242. }
  5243. /* get_finished test */
  5244. /* 1. get own sent message */
  5245. XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
  5246. msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
  5247. AssertIntGE(msg_len, 0);
  5248. /* 2. get peer message */
  5249. XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
  5250. msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
  5251. AssertIntGE(msg_len, 0);
  5252. if (cb != NULL)
  5253. (cb)(ctx, ssl);
  5254. #ifdef WOLFSSL_ASYNC_CRYPT
  5255. err = 0; /* Reset error */
  5256. #endif
  5257. do {
  5258. #ifdef WOLFSSL_ASYNC_CRYPT
  5259. if (err == WC_PENDING_E) {
  5260. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5261. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5262. }
  5263. #endif
  5264. ret = wolfSSL_write(ssl, msg, msgSz);
  5265. err = wolfSSL_get_error(ssl, 0);
  5266. } while (err == WC_PENDING_E);
  5267. if (ret != msgSz) {
  5268. /*err_sys("SSL_write failed");*/
  5269. goto done;
  5270. }
  5271. #ifdef WOLFSSL_ASYNC_CRYPT
  5272. err = 0; /* Reset error */
  5273. #endif
  5274. do {
  5275. #ifdef WOLFSSL_ASYNC_CRYPT
  5276. if (err == WC_PENDING_E) {
  5277. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5278. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5279. }
  5280. #endif
  5281. ret = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5282. err = wolfSSL_get_error(ssl, 0);
  5283. } while (err == WC_PENDING_E);
  5284. if (ret > 0) {
  5285. reply[ret] = '\0';
  5286. printf("Server response: %s\n", reply);
  5287. }
  5288. ((func_args*)args)->return_code = TEST_SUCCESS;
  5289. done:
  5290. wolfSSL_free(ssl);
  5291. wolfSSL_CTX_free(ctx);
  5292. CloseSocket(sockfd);
  5293. return;
  5294. #else
  5295. (void)args;
  5296. (void)cb;
  5297. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5298. }
  5299. static void test_wolfSSL_get_finished(void)
  5300. {
  5301. #if !defined(NO_RSA) && defined(WOLFSSL_HAVE_TLS_UNIQUE)
  5302. tcp_ready ready;
  5303. func_args client_args;
  5304. func_args server_args;
  5305. THREAD_TYPE serverThread;
  5306. XMEMSET(&client_args, 0, sizeof(func_args));
  5307. XMEMSET(&server_args, 0, sizeof(func_args));
  5308. StartTCP();
  5309. InitTcpReady(&ready);
  5310. #if defined(USE_WINDOWS_API)
  5311. /* use RNG to get random port if using windows */
  5312. ready.port = GetRandomPort();
  5313. #endif
  5314. server_args.signal = &ready;
  5315. client_args.signal = &ready;
  5316. start_thread(test_server_nofail, &server_args, &serverThread);
  5317. wait_tcp_ready(&server_args);
  5318. test_client_get_finished(&client_args, NULL);
  5319. join_thread(serverThread);
  5320. AssertTrue(client_args.return_code);
  5321. AssertTrue(server_args.return_code);
  5322. /* test received msg vs sent msg */
  5323. AssertIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
  5324. AssertIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
  5325. FreeTcpReady(&ready);
  5326. #else
  5327. (void)test_client_get_finished;
  5328. #endif /* !NO_RSA && WOLFSSL_HAVE_TLS_UNIQUE */
  5329. }
  5330. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  5331. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  5332. !defined(NO_SESSION_CACHE)
  5333. /* Sessions to restore/store */
  5334. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_client_sess;
  5335. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_server_sess;
  5336. static WOLFSSL_CTX* test_wolfSSL_CTX_add_session_server_ctx;
  5337. static void test_wolfSSL_CTX_add_session_ctx_ready(WOLFSSL_CTX* ctx)
  5338. {
  5339. /* Don't store sessions. Lookup is still enabled. */
  5340. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  5341. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  5342. /* Require both peers to provide certs */
  5343. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  5344. }
  5345. static void test_wolfSSL_CTX_add_session_on_result(WOLFSSL* ssl)
  5346. {
  5347. WOLFSSL_SESSION** sess;
  5348. if (wolfSSL_is_server(ssl))
  5349. sess = &test_wolfSSL_CTX_add_session_server_sess;
  5350. else
  5351. sess = &test_wolfSSL_CTX_add_session_client_sess;
  5352. if (*sess == NULL) {
  5353. #ifdef NO_SESSION_CACHE_REF
  5354. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  5355. #else
  5356. /* Test for backwards compatibility */
  5357. if (wolfSSL_is_server(ssl)) {
  5358. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  5359. }
  5360. else {
  5361. AssertNotNull(*sess = wolfSSL_get_session(ssl));
  5362. }
  5363. #endif
  5364. /* Now save the session in the internal store to make it available
  5365. * for lookup. For TLS 1.3, we can't save the session without
  5366. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  5367. * session from cache. */
  5368. if (wolfSSL_is_server(ssl)
  5369. #ifndef WOLFSSL_TICKET_HAVE_ID
  5370. && wolfSSL_version(ssl) != TLS1_3_VERSION
  5371. #endif
  5372. )
  5373. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  5374. *sess), WOLFSSL_SUCCESS);
  5375. }
  5376. else {
  5377. /* If we have a session retrieved then remaining connections should be
  5378. * resuming on that session */
  5379. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  5380. }
  5381. /* Save CTX to be able to decrypt tickets */
  5382. if (wolfSSL_is_server(ssl) &&
  5383. test_wolfSSL_CTX_add_session_server_ctx == NULL) {
  5384. AssertNotNull(test_wolfSSL_CTX_add_session_server_ctx
  5385. = wolfSSL_get_SSL_CTX(ssl));
  5386. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  5387. WOLFSSL_SUCCESS);
  5388. }
  5389. #ifdef SESSION_CERTS
  5390. #ifndef WOLFSSL_TICKET_HAVE_ID
  5391. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  5392. wolfSSL_session_reused(ssl))
  5393. #endif
  5394. {
  5395. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  5396. * for all connections. TLS 1.3 only has tickets so if we don't
  5397. * include the session id in the ticket then the certificates
  5398. * will not be available on resumption. */
  5399. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  5400. AssertNotNull(peer);
  5401. wolfSSL_X509_free(peer);
  5402. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  5403. AssertNotNull(wolfSSL_SESSION_get0_peer(*sess));
  5404. }
  5405. #endif
  5406. }
  5407. static void test_wolfSSL_CTX_add_session_ssl_ready(WOLFSSL* ssl)
  5408. {
  5409. /* Set the session to reuse for the client */
  5410. AssertIntEQ(wolfSSL_set_session(ssl,
  5411. test_wolfSSL_CTX_add_session_client_sess), WOLFSSL_SUCCESS);
  5412. }
  5413. #endif
  5414. static void test_wolfSSL_CTX_add_session(void)
  5415. {
  5416. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  5417. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  5418. !defined(NO_SESSION_CACHE)
  5419. tcp_ready ready;
  5420. func_args client_args;
  5421. func_args server_args;
  5422. THREAD_TYPE serverThread;
  5423. callback_functions client_cb;
  5424. callback_functions server_cb;
  5425. method_provider methods[][2] = {
  5426. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  5427. !defined(NO_DES3))
  5428. /* Without AES there are almost no ciphersuites available. This leads
  5429. * to no ciphersuites being available and an error. */
  5430. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method },
  5431. #endif
  5432. #ifndef WOLFSSL_NO_TLS12
  5433. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method },
  5434. #endif
  5435. /* Needs the default ticket callback since it is tied to the
  5436. * connection context and this makes it easy to carry over the ticket
  5437. * crypto context between connections */
  5438. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  5439. defined(HAVE_SESSION_TICKET)
  5440. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method },
  5441. #endif
  5442. };
  5443. const size_t methodsLen = sizeof(methods)/sizeof(*methods);
  5444. size_t i, j;
  5445. printf(testingFmt, "wolfSSL_CTX_add_session()");
  5446. for (i = 0; i < methodsLen; i++) {
  5447. /* First run creates a connection while the second+ run will attempt
  5448. * to resume the connection. The trick is that the internal cache
  5449. * is turned off. wolfSSL_CTX_add_session should put the session in
  5450. * the cache anyway. */
  5451. test_wolfSSL_CTX_add_session_client_sess = NULL;
  5452. test_wolfSSL_CTX_add_session_server_sess = NULL;
  5453. test_wolfSSL_CTX_add_session_server_ctx = NULL;
  5454. for (j = 0; j < 5; j++) {
  5455. #ifdef WOLFSSL_TIRTOS
  5456. fdOpenSession(Task_self());
  5457. #endif
  5458. StartTCP();
  5459. InitTcpReady(&ready);
  5460. XMEMSET(&client_args, 0, sizeof(func_args));
  5461. XMEMSET(&server_args, 0, sizeof(func_args));
  5462. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  5463. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  5464. client_cb.method = methods[i][0];
  5465. server_cb.method = methods[i][1];
  5466. server_args.signal = &ready;
  5467. server_args.callbacks = &server_cb;
  5468. client_args.signal = &ready;
  5469. client_args.callbacks = &client_cb;
  5470. if (test_wolfSSL_CTX_add_session_server_ctx != NULL) {
  5471. server_cb.ctx = test_wolfSSL_CTX_add_session_server_ctx;
  5472. server_cb.isSharedCtx = 1;
  5473. }
  5474. server_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  5475. client_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  5476. if (j != 0)
  5477. client_cb.ssl_ready = test_wolfSSL_CTX_add_session_ssl_ready;
  5478. server_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  5479. client_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  5480. server_cb.ticNoInit = 1; /* Use default builtin */
  5481. start_thread(test_server_nofail, &server_args, &serverThread);
  5482. wait_tcp_ready(&server_args);
  5483. test_client_nofail(&client_args, NULL);
  5484. join_thread(serverThread);
  5485. AssertTrue(client_args.return_code);
  5486. AssertTrue(server_args.return_code);
  5487. FreeTcpReady(&ready);
  5488. }
  5489. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_client_sess);
  5490. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_server_sess);
  5491. wolfSSL_CTX_free(test_wolfSSL_CTX_add_session_server_ctx);
  5492. }
  5493. printf(resultFmt, passed);
  5494. #endif
  5495. }
  5496. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  5497. /* canned export of a session using older version 3 */
  5498. static unsigned char version_3[] = {
  5499. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  5500. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  5501. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  5502. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  5503. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  5504. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5505. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  5506. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  5507. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  5508. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5509. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  5510. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  5511. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5512. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  5513. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  5514. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  5515. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  5516. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  5517. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  5518. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  5519. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5520. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5521. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5522. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5523. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5524. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5525. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5526. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5527. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5528. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5529. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5530. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
  5531. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  5532. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  5533. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  5534. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  5535. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  5536. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  5537. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  5538. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  5539. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  5540. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  5541. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  5542. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  5543. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5544. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  5545. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  5546. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  5547. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  5548. 0x2E, 0x31, 0xED, 0x4F
  5549. };
  5550. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  5551. static void test_wolfSSL_dtls_export(void)
  5552. {
  5553. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  5554. tcp_ready ready;
  5555. func_args client_args;
  5556. func_args server_args;
  5557. THREAD_TYPE serverThread;
  5558. callback_functions server_cbf;
  5559. callback_functions client_cbf;
  5560. #ifdef WOLFSSL_TIRTOS
  5561. fdOpenSession(Task_self());
  5562. #endif
  5563. InitTcpReady(&ready);
  5564. #if defined(USE_WINDOWS_API)
  5565. /* use RNG to get random port if using windows */
  5566. ready.port = GetRandomPort();
  5567. #endif
  5568. /* set using dtls */
  5569. XMEMSET(&client_args, 0, sizeof(func_args));
  5570. XMEMSET(&server_args, 0, sizeof(func_args));
  5571. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  5572. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  5573. server_cbf.method = wolfDTLSv1_2_server_method;
  5574. client_cbf.method = wolfDTLSv1_2_client_method;
  5575. server_args.callbacks = &server_cbf;
  5576. client_args.callbacks = &client_cbf;
  5577. server_args.signal = &ready;
  5578. client_args.signal = &ready;
  5579. start_thread(run_wolfssl_server, &server_args, &serverThread);
  5580. wait_tcp_ready(&server_args);
  5581. run_wolfssl_client(&client_args);
  5582. join_thread(serverThread);
  5583. AssertTrue(client_args.return_code);
  5584. AssertTrue(server_args.return_code);
  5585. FreeTcpReady(&ready);
  5586. #ifdef WOLFSSL_TIRTOS
  5587. fdOpenSession(Task_self());
  5588. #endif
  5589. {
  5590. SOCKET_T sockfd = 0;
  5591. WOLFSSL_CTX* ctx;
  5592. WOLFSSL* ssl;
  5593. char msg[64] = "hello wolfssl!";
  5594. char reply[1024];
  5595. int msgSz = (int)XSTRLEN(msg);
  5596. byte *session, *window;
  5597. unsigned int sessionSz, windowSz;
  5598. #ifndef TEST_IPV6
  5599. struct sockaddr_in peerAddr;
  5600. #else
  5601. struct sockaddr_in6 peerAddr;
  5602. #endif /* TEST_IPV6 */
  5603. int i;
  5604. /* Set ctx to DTLS 1.2 */
  5605. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  5606. AssertNotNull(ssl = wolfSSL_new(ctx));
  5607. /* test importing version 3 */
  5608. AssertIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  5609. /* test importing bad length and bad version */
  5610. version_3[2] += 1;
  5611. AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  5612. version_3[2] -= 1; version_3[1] = 0XA0;
  5613. AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  5614. wolfSSL_free(ssl);
  5615. wolfSSL_CTX_free(ctx);
  5616. /* check storing client state after connection and storing window only */
  5617. #ifdef WOLFSSL_TIRTOS
  5618. fdOpenSession(Task_self());
  5619. #endif
  5620. InitTcpReady(&ready);
  5621. #if defined(USE_WINDOWS_API)
  5622. /* use RNG to get random port if using windows */
  5623. ready.port = GetRandomPort();
  5624. #endif
  5625. /* set using dtls */
  5626. XMEMSET(&server_args, 0, sizeof(func_args));
  5627. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  5628. server_cbf.method = wolfDTLSv1_2_server_method;
  5629. server_cbf.doUdp = 1;
  5630. server_args.callbacks = &server_cbf;
  5631. server_args.argc = 3; /* set loop_count to 3 */
  5632. server_args.signal = &ready;
  5633. start_thread(test_server_nofail, &server_args, &serverThread);
  5634. wait_tcp_ready(&server_args);
  5635. /* create and connect with client */
  5636. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  5637. AssertIntEQ(WOLFSSL_SUCCESS,
  5638. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  5639. AssertIntEQ(WOLFSSL_SUCCESS,
  5640. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  5641. AssertIntEQ(WOLFSSL_SUCCESS,
  5642. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  5643. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 1, 0, NULL);
  5644. AssertNotNull(ssl = wolfSSL_new(ctx));
  5645. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  5646. /* store server information connected too */
  5647. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  5648. #ifndef TEST_IPV6
  5649. peerAddr.sin_family = AF_INET;
  5650. AssertIntEQ(XINET_PTON(AF_INET, wolfSSLIP, &peerAddr.sin_addr),1);
  5651. peerAddr.sin_port = XHTONS(server_args.signal->port);
  5652. #else
  5653. peerAddr.sin6_family = AF_INET6;
  5654. AssertIntEQ(
  5655. XINET_PTON(AF_INET6, wolfSSLIP, &peerAddr.sin6_addr),1);
  5656. peerAddr.sin6_port = XHTONS(server_args.signal->port);
  5657. #endif
  5658. AssertIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  5659. WOLFSSL_SUCCESS);
  5660. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  5661. AssertIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  5662. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5663. AssertIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  5664. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  5665. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  5666. AssertIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  5667. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5668. AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  5669. wolfSSL_free(ssl);
  5670. for (i = 1; i < server_args.argc; i++) {
  5671. /* restore state */
  5672. AssertNotNull(ssl = wolfSSL_new(ctx));
  5673. AssertIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  5674. AssertIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  5675. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  5676. AssertIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  5677. WOLFSSL_SUCCESS);
  5678. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  5679. AssertIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  5680. AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  5681. wolfSSL_free(ssl);
  5682. }
  5683. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5684. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5685. wolfSSL_CTX_free(ctx);
  5686. printf("done and waiting for server\n");
  5687. join_thread(serverThread);
  5688. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5689. FreeTcpReady(&ready);
  5690. #ifdef WOLFSSL_TIRTOS
  5691. fdOpenSession(Task_self());
  5692. #endif
  5693. }
  5694. printf(testingFmt, "wolfSSL_dtls_export()");
  5695. printf(resultFmt, passed);
  5696. #endif
  5697. }
  5698. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  5699. #ifdef WOLFSSL_TLS13
  5700. static const byte canned_client_tls13_session[] = {
  5701. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
  5702. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  5703. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
  5704. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  5705. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  5706. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  5707. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  5708. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
  5709. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  5710. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5711. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5712. 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  5713. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  5714. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  5715. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  5716. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  5717. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  5718. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  5719. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  5720. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  5721. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  5722. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  5723. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  5724. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  5725. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  5726. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  5727. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  5728. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  5729. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  5730. 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
  5731. 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
  5732. 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
  5733. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  5734. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  5735. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  5736. 0x00, 0x03
  5737. };
  5738. static const byte canned_server_tls13_session[] = {
  5739. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
  5740. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  5741. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
  5742. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  5743. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5744. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  5745. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  5746. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
  5747. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  5748. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5749. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5750. 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  5751. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  5752. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  5753. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  5754. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  5755. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  5756. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  5757. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  5758. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  5759. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  5760. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  5761. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  5762. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  5763. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  5764. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  5765. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  5766. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  5767. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  5768. 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
  5769. 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
  5770. 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
  5771. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  5772. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  5773. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  5774. 0x00, 0x04
  5775. };
  5776. #endif /* WOLFSSL_TLS13 */
  5777. static const byte canned_client_session[] = {
  5778. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  5779. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  5780. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
  5781. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  5782. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  5783. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5784. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  5785. 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
  5786. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  5787. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5788. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5789. 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  5790. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  5791. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  5792. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  5793. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  5794. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  5795. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  5796. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  5797. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  5798. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  5799. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  5800. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  5801. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  5802. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  5803. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  5804. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  5805. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  5806. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  5807. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  5808. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5809. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5810. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5811. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  5812. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  5813. 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  5814. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  5815. 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  5816. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  5817. 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  5818. 0x00, 0x03
  5819. };
  5820. static const byte canned_server_session[] = {
  5821. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  5822. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  5823. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  5824. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  5825. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  5826. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5827. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  5828. 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
  5829. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  5830. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5831. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5832. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  5833. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  5834. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  5835. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  5836. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  5837. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  5838. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  5839. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  5840. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  5841. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  5842. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  5843. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  5844. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  5845. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  5846. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  5847. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  5848. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  5849. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  5850. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  5851. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5852. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5853. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5854. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  5855. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  5856. 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  5857. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  5858. 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  5859. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  5860. 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  5861. 0x00, 0x04
  5862. };
  5863. static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
  5864. {
  5865. SOCKET_T sockfd = 0;
  5866. SOCKET_T clientfd = 0;
  5867. word16 port;
  5868. callback_functions* cbf;
  5869. WOLFSSL_CTX* ctx = 0;
  5870. WOLFSSL* ssl = 0;
  5871. char msg[] = "I hear you fa shizzle!";
  5872. char input[1024];
  5873. int idx;
  5874. #ifdef WOLFSSL_TIRTOS
  5875. fdOpenSession(Task_self());
  5876. #endif
  5877. ((func_args*)args)->return_code = TEST_FAIL;
  5878. cbf = ((func_args*)args)->callbacks;
  5879. {
  5880. WOLFSSL_METHOD* method = NULL;
  5881. if (cbf != NULL && cbf->method != NULL) {
  5882. method = cbf->method();
  5883. }
  5884. else {
  5885. method = wolfTLSv1_2_server_method();
  5886. }
  5887. ctx = wolfSSL_CTX_new(method);
  5888. }
  5889. if (ctx == NULL) {
  5890. goto done;
  5891. }
  5892. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  5893. #if defined(USE_WINDOWS_API)
  5894. port = ((func_args*)args)->signal->port;
  5895. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  5896. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  5897. /* Let tcp_listen assign port */
  5898. port = 0;
  5899. #else
  5900. /* Use default port */
  5901. port = wolfSSLPort;
  5902. #endif
  5903. /* do it here to detect failure */
  5904. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  5905. CloseSocket(sockfd);
  5906. /* call ctx setup callback */
  5907. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5908. cbf->ctx_ready(ctx);
  5909. }
  5910. ssl = wolfSSL_new(ctx);
  5911. if (ssl == NULL) {
  5912. goto done;
  5913. }
  5914. wolfSSL_set_fd(ssl, clientfd);
  5915. /* call ssl setup callback */
  5916. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5917. cbf->ssl_ready(ssl);
  5918. }
  5919. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  5920. if (idx > 0) {
  5921. input[idx] = '\0';
  5922. printf("Client message export/import: %s\n", input);
  5923. }
  5924. else {
  5925. printf("ret = %d error = %d\n", idx, wolfSSL_get_error(ssl, idx));
  5926. goto done;
  5927. }
  5928. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  5929. /*err_sys("SSL_write failed");*/
  5930. #ifdef WOLFSSL_TIRTOS
  5931. return;
  5932. #else
  5933. return 0;
  5934. #endif
  5935. }
  5936. #ifdef WOLFSSL_TIRTOS
  5937. Task_yield();
  5938. #endif
  5939. ((func_args*)args)->return_code = TEST_SUCCESS;
  5940. done:
  5941. wolfSSL_shutdown(ssl);
  5942. wolfSSL_free(ssl);
  5943. wolfSSL_CTX_free(ctx);
  5944. CloseSocket(clientfd);
  5945. #ifdef WOLFSSL_TIRTOS
  5946. fdCloseSession(Task_self());
  5947. #endif
  5948. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5949. && defined(HAVE_THREAD_LS)
  5950. wc_ecc_fp_free(); /* free per thread cache */
  5951. #endif
  5952. #if defined(HAVE_SESSION_TICKET) && \
  5953. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5954. #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
  5955. OpenSSLTicketCleanup();
  5956. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5957. TicketCleanup();
  5958. #endif
  5959. #endif
  5960. #ifndef WOLFSSL_TIRTOS
  5961. return 0;
  5962. #endif
  5963. }
  5964. static void load_tls12_canned_server(WOLFSSL* ssl)
  5965. {
  5966. int clientfd = wolfSSL_get_fd(ssl);
  5967. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
  5968. sizeof(canned_server_session)), sizeof(canned_server_session));
  5969. wolfSSL_set_fd(ssl, clientfd);
  5970. }
  5971. #ifdef WOLFSSL_TLS13
  5972. static void load_tls13_canned_server(WOLFSSL* ssl)
  5973. {
  5974. int clientfd = wolfSSL_get_fd(ssl);
  5975. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
  5976. sizeof(canned_server_tls13_session)),
  5977. sizeof(canned_server_tls13_session));
  5978. wolfSSL_set_fd(ssl, clientfd);
  5979. }
  5980. #endif
  5981. /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  5982. static void test_wolfSSL_tls_export_run(int v)
  5983. {
  5984. SOCKET_T sockfd = 0;
  5985. WOLFSSL_CTX* ctx = 0;
  5986. WOLFSSL* ssl = 0;
  5987. char msg[64] = "hello wolfssl!";
  5988. char reply[1024];
  5989. word32 replySz;
  5990. int msgSz = (int)XSTRLEN(msg);
  5991. const byte* clientSession = NULL;
  5992. int clientSessionSz = 0;
  5993. tcp_ready ready;
  5994. func_args server_args;
  5995. THREAD_TYPE serverThread;
  5996. callback_functions server_cbf;
  5997. #ifdef WOLFSSL_TIRTOS
  5998. fdOpenSession(Task_self());
  5999. #endif
  6000. InitTcpReady(&ready);
  6001. #if defined(USE_WINDOWS_API)
  6002. /* use RNG to get random port if using windows */
  6003. ready.port = GetRandomPort();
  6004. #endif
  6005. XMEMSET(&server_args, 0, sizeof(func_args));
  6006. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6007. switch (v) {
  6008. case WOLFSSL_TLSV1_2:
  6009. server_cbf.method = wolfTLSv1_2_server_method;
  6010. server_cbf.ssl_ready = load_tls12_canned_server;
  6011. /* setup the client side */
  6012. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  6013. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  6014. clientSession = canned_client_session;
  6015. clientSessionSz = sizeof(canned_client_session);
  6016. break;
  6017. #ifdef WOLFSSL_TLS13
  6018. case WOLFSSL_TLSV1_3:
  6019. server_cbf.method = wolfTLSv1_3_server_method;
  6020. server_cbf.ssl_ready = load_tls13_canned_server;
  6021. /* setup the client side */
  6022. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  6023. clientSession = canned_client_tls13_session;
  6024. clientSessionSz = sizeof(canned_client_tls13_session);
  6025. break;
  6026. #endif
  6027. }
  6028. server_args.callbacks = &server_cbf;
  6029. server_args.signal = &ready;
  6030. start_thread(tls_export_server, &server_args, &serverThread);
  6031. wait_tcp_ready(&server_args);
  6032. #ifdef WOLFSSL_TIRTOS
  6033. fdOpenSession(Task_self());
  6034. #endif
  6035. AssertNotNull(ssl = wolfSSL_new(ctx));
  6036. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  6037. AssertIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
  6038. clientSessionSz);
  6039. replySz = sizeof(reply);
  6040. AssertIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
  6041. #if !defined(NO_PSK) && defined(HAVE_ANON)
  6042. /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
  6043. AssertIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
  6044. #endif
  6045. wolfSSL_set_fd(ssl, sockfd);
  6046. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  6047. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
  6048. wolfSSL_free(ssl);
  6049. wolfSSL_CTX_free(ctx);
  6050. CloseSocket(sockfd);
  6051. #ifdef WOLFSSL_TIRTOS
  6052. fdCloseSession(Task_self());
  6053. #endif
  6054. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6055. && defined(HAVE_THREAD_LS)
  6056. wc_ecc_fp_free(); /* free per thread cache */
  6057. #endif
  6058. join_thread(serverThread);
  6059. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  6060. FreeTcpReady(&ready);
  6061. #ifdef WOLFSSL_TIRTOS
  6062. fdOpenSession(Task_self());
  6063. #endif
  6064. }
  6065. #endif
  6066. static void test_wolfSSL_tls_export(void)
  6067. {
  6068. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  6069. printf(testingFmt, "wolfSSL_tls_export()");
  6070. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
  6071. #ifdef WOLFSSL_TLS13
  6072. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
  6073. #endif
  6074. printf(resultFmt, passed);
  6075. #endif
  6076. }
  6077. /*----------------------------------------------------------------------------*
  6078. | TLS extensions tests
  6079. *----------------------------------------------------------------------------*/
  6080. #ifdef ENABLE_TLS_CALLBACK_TEST
  6081. /* Connection test runner - generic */
  6082. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  6083. callback_functions* server_callbacks)
  6084. {
  6085. tcp_ready ready;
  6086. func_args client_args;
  6087. func_args server_args;
  6088. THREAD_TYPE serverThread;
  6089. XMEMSET(&client_args, 0, sizeof(func_args));
  6090. XMEMSET(&server_args, 0, sizeof(func_args));
  6091. StartTCP();
  6092. client_args.callbacks = client_callbacks;
  6093. server_args.callbacks = server_callbacks;
  6094. #ifdef WOLFSSL_TIRTOS
  6095. fdOpenSession(Task_self());
  6096. #endif
  6097. /* RUN Server side */
  6098. InitTcpReady(&ready);
  6099. #if defined(USE_WINDOWS_API)
  6100. /* use RNG to get random port if using windows */
  6101. ready.port = GetRandomPort();
  6102. #endif
  6103. server_args.signal = &ready;
  6104. client_args.signal = &ready;
  6105. start_thread(run_wolfssl_server, &server_args, &serverThread);
  6106. wait_tcp_ready(&server_args);
  6107. /* RUN Client side */
  6108. run_wolfssl_client(&client_args);
  6109. join_thread(serverThread);
  6110. FreeTcpReady(&ready);
  6111. #ifdef WOLFSSL_TIRTOS
  6112. fdCloseSession(Task_self());
  6113. #endif
  6114. client_callbacks->return_code = client_args.return_code;
  6115. server_callbacks->return_code = server_args.return_code;
  6116. }
  6117. #endif /* ENABLE_TLS_CALLBACK_TEST */
  6118. #ifdef HAVE_SNI
  6119. static void test_wolfSSL_UseSNI_params(void)
  6120. {
  6121. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  6122. WOLFSSL *ssl = wolfSSL_new(ctx);
  6123. AssertNotNull(ctx);
  6124. AssertNotNull(ssl);
  6125. /* invalid [ctx|ssl] */
  6126. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  6127. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  6128. /* invalid type */
  6129. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  6130. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  6131. /* invalid data */
  6132. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  6133. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  6134. /* success case */
  6135. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  6136. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  6137. wolfSSL_free(ssl);
  6138. wolfSSL_CTX_free(ctx);
  6139. }
  6140. /* BEGIN of connection tests callbacks */
  6141. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  6142. {
  6143. AssertIntEQ(WOLFSSL_SUCCESS,
  6144. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  6145. }
  6146. static void use_SNI_at_ssl(WOLFSSL* ssl)
  6147. {
  6148. AssertIntEQ(WOLFSSL_SUCCESS,
  6149. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  6150. }
  6151. static void different_SNI_at_ssl(WOLFSSL* ssl)
  6152. {
  6153. AssertIntEQ(WOLFSSL_SUCCESS,
  6154. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  6155. }
  6156. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  6157. {
  6158. use_SNI_at_ssl(ssl);
  6159. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  6160. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  6161. }
  6162. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  6163. {
  6164. use_SNI_at_ssl(ssl);
  6165. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  6166. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  6167. }
  6168. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  6169. {
  6170. use_SNI_at_ctx(ctx);
  6171. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  6172. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  6173. }
  6174. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  6175. {
  6176. use_SNI_at_ssl(ssl);
  6177. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  6178. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  6179. }
  6180. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  6181. {
  6182. use_SNI_at_ctx(ctx);
  6183. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  6184. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  6185. }
  6186. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  6187. {
  6188. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  6189. }
  6190. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  6191. {
  6192. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  6193. }
  6194. static void verify_SNI_no_matching(WOLFSSL* ssl)
  6195. {
  6196. byte type = WOLFSSL_SNI_HOST_NAME;
  6197. char* request = (char*) &type; /* to be overwritten */
  6198. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  6199. AssertNotNull(request);
  6200. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
  6201. AssertNull(request);
  6202. }
  6203. static void verify_SNI_real_matching(WOLFSSL* ssl)
  6204. {
  6205. byte type = WOLFSSL_SNI_HOST_NAME;
  6206. char* request = NULL;
  6207. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  6208. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
  6209. AssertNotNull(request);
  6210. AssertStrEQ("www.wolfssl.com", request);
  6211. }
  6212. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  6213. {
  6214. byte type = WOLFSSL_SNI_HOST_NAME;
  6215. char* request = NULL;
  6216. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  6217. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
  6218. AssertNotNull(request);
  6219. AssertStrEQ("ww2.wolfssl.com", request);
  6220. }
  6221. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  6222. {
  6223. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  6224. }
  6225. /* END of connection tests callbacks */
  6226. static void test_wolfSSL_UseSNI_connection(void)
  6227. {
  6228. callback_functions client_cb;
  6229. callback_functions server_cb;
  6230. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  6231. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  6232. client_cb.method = wolfSSLv23_client_method;
  6233. server_cb.method = wolfSSLv23_server_method;
  6234. client_cb.devId = devId;
  6235. server_cb.devId = devId;
  6236. /* success case at ctx */
  6237. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  6238. server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  6239. test_wolfSSL_client_server(&client_cb, &server_cb);
  6240. /* success case at ssl */
  6241. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
  6242. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
  6243. test_wolfSSL_client_server(&client_cb, &server_cb);
  6244. /* default mismatch behavior */
  6245. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
  6246. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
  6247. test_wolfSSL_client_server(&client_cb, &server_cb);
  6248. /* continue on mismatch */
  6249. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  6250. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  6251. test_wolfSSL_client_server(&client_cb, &server_cb);
  6252. /* fake answer on mismatch */
  6253. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  6254. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
  6255. test_wolfSSL_client_server(&client_cb, &server_cb);
  6256. /* sni abort - success */
  6257. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  6258. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  6259. test_wolfSSL_client_server(&client_cb, &server_cb);
  6260. /* sni abort - abort when absent (ctx) */
  6261. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  6262. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
  6263. test_wolfSSL_client_server(&client_cb, &server_cb);
  6264. /* sni abort - abort when absent (ssl) */
  6265. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  6266. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
  6267. test_wolfSSL_client_server(&client_cb, &server_cb);
  6268. /* sni abort - success when overwritten */
  6269. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  6270. 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;
  6271. test_wolfSSL_client_server(&client_cb, &server_cb);
  6272. /* sni abort - success when allowing mismatches */
  6273. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  6274. server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
  6275. test_wolfSSL_client_server(&client_cb, &server_cb);
  6276. }
  6277. static void test_wolfSSL_SNI_GetFromBuffer(void)
  6278. {
  6279. byte buff[] = { /* www.paypal.com */
  6280. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  6281. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  6282. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  6283. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  6284. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  6285. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  6286. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  6287. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  6288. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  6289. };
  6290. byte buff2[] = { /* api.textmate.org */
  6291. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  6292. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  6293. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  6294. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  6295. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  6296. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  6297. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  6298. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  6299. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  6300. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  6301. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  6302. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  6303. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  6304. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  6305. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  6306. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  6307. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  6308. };
  6309. byte buff3[] = { /* no sni extension */
  6310. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  6311. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  6312. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  6313. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  6314. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  6315. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  6316. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  6317. };
  6318. byte buff4[] = { /* last extension has zero size */
  6319. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  6320. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  6321. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  6322. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  6323. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  6324. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  6325. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  6326. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  6327. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  6328. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  6329. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  6330. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  6331. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  6332. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  6333. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  6334. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  6335. 0x12, 0x00, 0x00
  6336. };
  6337. byte buff5[] = { /* SSL v2.0 client hello */
  6338. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  6339. /* dummy bytes bellow, just to pass size check */
  6340. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  6341. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  6342. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  6343. };
  6344. byte result[32] = {0};
  6345. word32 length = 32;
  6346. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  6347. 0, result, &length));
  6348. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  6349. 0, result, &length));
  6350. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  6351. 1, result, &length));
  6352. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  6353. 0, result, &length));
  6354. buff[0] = 0x16;
  6355. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  6356. 0, result, &length));
  6357. buff[1] = 0x03;
  6358. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  6359. sizeof(buff), 0, result, &length));
  6360. buff[2] = 0x03;
  6361. AssertIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  6362. sizeof(buff), 0, result, &length));
  6363. buff[4] = 0x64;
  6364. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  6365. 0, result, &length));
  6366. result[length] = 0;
  6367. AssertStrEQ("www.paypal.com", (const char*) result);
  6368. length = 32;
  6369. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  6370. 0, result, &length));
  6371. result[length] = 0;
  6372. AssertStrEQ("api.textmate.org", (const char*) result);
  6373. /* SSL v2.0 tests */
  6374. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  6375. sizeof(buff5), 0, result, &length));
  6376. buff5[2] = 0x02;
  6377. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  6378. sizeof(buff5), 0, result, &length));
  6379. buff5[2] = 0x01; buff5[6] = 0x08;
  6380. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  6381. sizeof(buff5), 0, result, &length));
  6382. buff5[6] = 0x09; buff5[8] = 0x01;
  6383. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  6384. sizeof(buff5), 0, result, &length));
  6385. }
  6386. #endif /* HAVE_SNI */
  6387. static void test_wolfSSL_UseSNI(void)
  6388. {
  6389. #ifdef HAVE_SNI
  6390. test_wolfSSL_UseSNI_params();
  6391. test_wolfSSL_UseSNI_connection();
  6392. test_wolfSSL_SNI_GetFromBuffer();
  6393. #endif
  6394. }
  6395. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  6396. static void test_wolfSSL_UseTrustedCA(void)
  6397. {
  6398. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  6399. && !defined(NO_RSA)
  6400. WOLFSSL_CTX *ctx;
  6401. WOLFSSL *ssl;
  6402. byte id[20];
  6403. #ifndef NO_WOLFSSL_SERVER
  6404. AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  6405. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  6406. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  6407. #else
  6408. AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  6409. #endif
  6410. AssertNotNull((ssl = wolfSSL_new(ctx)));
  6411. XMEMSET(id, 0, sizeof(id));
  6412. /* error cases */
  6413. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  6414. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6415. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  6416. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6417. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  6418. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6419. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  6420. #ifdef NO_SHA
  6421. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6422. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  6423. #endif
  6424. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6425. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  6426. /* success cases */
  6427. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6428. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  6429. #ifndef NO_SHA
  6430. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6431. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  6432. #endif
  6433. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6434. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  6435. wolfSSL_free(ssl);
  6436. wolfSSL_CTX_free(ctx);
  6437. #endif /* HAVE_TRUSTED_CA */
  6438. }
  6439. static void test_wolfSSL_UseMaxFragment(void)
  6440. {
  6441. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  6442. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  6443. #ifndef NO_WOLFSSL_SERVER
  6444. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  6445. #else
  6446. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  6447. #endif
  6448. WOLFSSL *ssl;
  6449. #ifdef OPENSSL_EXTRA
  6450. int (*UseMaxFragment)(SSL *s, uint8_t mode);
  6451. int (*CTX_UseMaxFragment)(SSL_CTX *c, uint8_t mode);
  6452. #else
  6453. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  6454. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  6455. #endif
  6456. #ifndef NO_WOLFSSL_SERVER
  6457. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  6458. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  6459. #endif
  6460. AssertNotNull(ctx);
  6461. ssl = wolfSSL_new(ctx);
  6462. AssertNotNull(ssl);
  6463. #ifdef OPENSSL_EXTRA
  6464. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  6465. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  6466. #else
  6467. UseMaxFragment = wolfSSL_UseMaxFragment;
  6468. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  6469. #endif
  6470. /* error cases */
  6471. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  6472. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  6473. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  6474. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  6475. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  6476. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  6477. /* success case */
  6478. #ifdef OPENSSL_EXTRA
  6479. AssertIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  6480. #else
  6481. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  6482. #endif
  6483. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  6484. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  6485. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  6486. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  6487. #ifdef OPENSSL_EXTRA
  6488. AssertIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  6489. AssertIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  6490. #else
  6491. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  6492. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  6493. #endif
  6494. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  6495. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  6496. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  6497. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  6498. #ifdef OPENSSL_EXTRA
  6499. AssertIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  6500. #else
  6501. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  6502. #endif
  6503. wolfSSL_free(ssl);
  6504. wolfSSL_CTX_free(ctx);
  6505. #endif
  6506. }
  6507. static void test_wolfSSL_UseTruncatedHMAC(void)
  6508. {
  6509. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  6510. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  6511. #ifndef NO_WOLFSSL_SERVER
  6512. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  6513. #else
  6514. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  6515. #endif
  6516. WOLFSSL *ssl;
  6517. AssertNotNull(ctx);
  6518. #ifndef NO_WOLFSSL_SERVER
  6519. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  6520. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  6521. #endif
  6522. ssl = wolfSSL_new(ctx);
  6523. AssertNotNull(ssl);
  6524. /* error cases */
  6525. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  6526. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  6527. /* success case */
  6528. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  6529. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  6530. wolfSSL_free(ssl);
  6531. wolfSSL_CTX_free(ctx);
  6532. #endif
  6533. }
  6534. static void test_wolfSSL_UseSupportedCurve(void)
  6535. {
  6536. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && !defined(NO_TLS)
  6537. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  6538. WOLFSSL *ssl = wolfSSL_new(ctx);
  6539. AssertNotNull(ctx);
  6540. AssertNotNull(ssl);
  6541. /* error cases */
  6542. AssertIntNE(WOLFSSL_SUCCESS,
  6543. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  6544. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  6545. AssertIntNE(WOLFSSL_SUCCESS,
  6546. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  6547. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  6548. /* success case */
  6549. AssertIntEQ(WOLFSSL_SUCCESS,
  6550. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  6551. AssertIntEQ(WOLFSSL_SUCCESS,
  6552. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  6553. wolfSSL_free(ssl);
  6554. wolfSSL_CTX_free(ctx);
  6555. #endif
  6556. }
  6557. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  6558. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  6559. {
  6560. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  6561. }
  6562. static void use_ALPN_all(WOLFSSL* ssl)
  6563. {
  6564. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  6565. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  6566. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  6567. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  6568. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  6569. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  6570. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  6571. }
  6572. static void use_ALPN_all_continue(WOLFSSL* ssl)
  6573. {
  6574. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  6575. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  6576. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  6577. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  6578. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  6579. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  6580. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  6581. }
  6582. static void use_ALPN_one(WOLFSSL* ssl)
  6583. {
  6584. /* spdy/2 */
  6585. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  6586. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  6587. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  6588. }
  6589. static void use_ALPN_unknown(WOLFSSL* ssl)
  6590. {
  6591. /* http/2.0 */
  6592. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  6593. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  6594. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  6595. }
  6596. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  6597. {
  6598. /* http/2.0 */
  6599. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  6600. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  6601. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  6602. }
  6603. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  6604. {
  6605. /* spdy/3 */
  6606. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  6607. char *proto = NULL;
  6608. word16 protoSz = 0;
  6609. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  6610. /* check value */
  6611. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  6612. if (proto) {
  6613. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  6614. }
  6615. }
  6616. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  6617. {
  6618. char *proto = NULL;
  6619. word16 protoSz = 0;
  6620. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  6621. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  6622. /* check value */
  6623. AssertIntEQ(1, (0 == protoSz));
  6624. AssertIntEQ(1, (NULL == proto));
  6625. }
  6626. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  6627. {
  6628. /* http/1.1 */
  6629. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  6630. char *proto;
  6631. word16 protoSz = 0;
  6632. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  6633. /* check value */
  6634. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  6635. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  6636. }
  6637. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  6638. {
  6639. /* spdy/2 */
  6640. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  6641. char *proto;
  6642. word16 protoSz = 0;
  6643. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  6644. /* check value */
  6645. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  6646. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  6647. }
  6648. static void verify_ALPN_client_list(WOLFSSL* ssl)
  6649. {
  6650. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  6651. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  6652. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  6653. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  6654. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  6655. char *clist = NULL;
  6656. word16 clistSz = 0;
  6657. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  6658. &clistSz));
  6659. /* check value */
  6660. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  6661. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  6662. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  6663. }
  6664. static void test_wolfSSL_UseALPN_connection(void)
  6665. {
  6666. callback_functions client_cb;
  6667. callback_functions server_cb;
  6668. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  6669. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  6670. client_cb.method = wolfSSLv23_client_method;
  6671. server_cb.method = wolfSSLv23_server_method;
  6672. client_cb.devId = devId;
  6673. server_cb.devId = devId;
  6674. /* success case same list */
  6675. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  6676. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
  6677. test_wolfSSL_client_server(&client_cb, &server_cb);
  6678. /* success case only one for server */
  6679. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  6680. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
  6681. test_wolfSSL_client_server(&client_cb, &server_cb);
  6682. /* success case only one for client */
  6683. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
  6684. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
  6685. test_wolfSSL_client_server(&client_cb, &server_cb);
  6686. /* success case none for client */
  6687. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  6688. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
  6689. test_wolfSSL_client_server(&client_cb, &server_cb);
  6690. /* success case mismatch behavior but option 'continue' set */
  6691. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
  6692. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
  6693. test_wolfSSL_client_server(&client_cb, &server_cb);
  6694. /* success case read protocol send by client */
  6695. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  6696. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
  6697. test_wolfSSL_client_server(&client_cb, &server_cb);
  6698. /* mismatch behavior with same list
  6699. * the first and only this one must be taken */
  6700. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  6701. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
  6702. test_wolfSSL_client_server(&client_cb, &server_cb);
  6703. /* default mismatch behavior */
  6704. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  6705. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  6706. test_wolfSSL_client_server(&client_cb, &server_cb);
  6707. }
  6708. static void test_wolfSSL_UseALPN_params(void)
  6709. {
  6710. #ifndef NO_WOLFSSL_CLIENT
  6711. /* "http/1.1" */
  6712. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  6713. /* "spdy/1" */
  6714. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  6715. /* "spdy/2" */
  6716. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  6717. /* "spdy/3" */
  6718. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  6719. char buff[256];
  6720. word32 idx;
  6721. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  6722. WOLFSSL *ssl = wolfSSL_new(ctx);
  6723. AssertNotNull(ctx);
  6724. AssertNotNull(ssl);
  6725. /* error cases */
  6726. AssertIntNE(WOLFSSL_SUCCESS,
  6727. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  6728. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  6729. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  6730. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  6731. /* success case */
  6732. /* http1 only */
  6733. AssertIntEQ(WOLFSSL_SUCCESS,
  6734. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  6735. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  6736. /* http1, spdy1 */
  6737. XMEMCPY(buff, http1, sizeof(http1));
  6738. idx = sizeof(http1);
  6739. buff[idx++] = ',';
  6740. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  6741. idx += sizeof(spdy1);
  6742. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  6743. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  6744. /* http1, spdy2, spdy1 */
  6745. XMEMCPY(buff, http1, sizeof(http1));
  6746. idx = sizeof(http1);
  6747. buff[idx++] = ',';
  6748. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  6749. idx += sizeof(spdy2);
  6750. buff[idx++] = ',';
  6751. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  6752. idx += sizeof(spdy1);
  6753. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  6754. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  6755. /* spdy3, http1, spdy2, spdy1 */
  6756. XMEMCPY(buff, spdy3, sizeof(spdy3));
  6757. idx = sizeof(spdy3);
  6758. buff[idx++] = ',';
  6759. XMEMCPY(buff+idx, http1, sizeof(http1));
  6760. idx += sizeof(http1);
  6761. buff[idx++] = ',';
  6762. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  6763. idx += sizeof(spdy2);
  6764. buff[idx++] = ',';
  6765. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  6766. idx += sizeof(spdy1);
  6767. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  6768. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  6769. wolfSSL_free(ssl);
  6770. wolfSSL_CTX_free(ctx);
  6771. #endif
  6772. }
  6773. #endif /* HAVE_ALPN */
  6774. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  6775. static void CTX_set_alpn_protos(SSL_CTX *ctx)
  6776. {
  6777. unsigned char p[] = {
  6778. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  6779. 6, 's', 'p', 'd', 'y', '/', '2',
  6780. 6, 's', 'p', 'd', 'y', '/', '1',
  6781. };
  6782. unsigned char p_len = sizeof(p);
  6783. int ret;
  6784. ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
  6785. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  6786. AssertIntEQ(ret, 0);
  6787. #else
  6788. AssertIntEQ(ret, SSL_SUCCESS);
  6789. #endif
  6790. }
  6791. static void set_alpn_protos(SSL* ssl)
  6792. {
  6793. unsigned char p[] = {
  6794. 6, 's', 'p', 'd', 'y', '/', '3',
  6795. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  6796. 6, 's', 'p', 'd', 'y', '/', '2',
  6797. 6, 's', 'p', 'd', 'y', '/', '1',
  6798. };
  6799. unsigned char p_len = sizeof(p);
  6800. int ret;
  6801. ret = SSL_set_alpn_protos(ssl, p, p_len);
  6802. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  6803. AssertIntEQ(ret, 0);
  6804. #else
  6805. AssertIntEQ(ret, SSL_SUCCESS);
  6806. #endif
  6807. }
  6808. static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
  6809. {
  6810. /* "spdy/3" */
  6811. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  6812. const unsigned char *proto;
  6813. unsigned int protoSz = 0;
  6814. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  6815. /* check value */
  6816. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  6817. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  6818. }
  6819. static void verify_alpn_matching_http1(WOLFSSL* ssl)
  6820. {
  6821. /* "http/1.1" */
  6822. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  6823. const unsigned char *proto;
  6824. unsigned int protoSz = 0;
  6825. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  6826. /* check value */
  6827. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  6828. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  6829. }
  6830. static void test_wolfSSL_set_alpn_protos(void)
  6831. {
  6832. callback_functions client_cb;
  6833. callback_functions server_cb;
  6834. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  6835. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  6836. client_cb.method = wolfSSLv23_client_method;
  6837. server_cb.method = wolfSSLv23_server_method;
  6838. client_cb.devId = devId;
  6839. server_cb.devId = devId;
  6840. /* use CTX_alpn_protos */
  6841. client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  6842. server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
  6843. test_wolfSSL_client_server(&client_cb, &server_cb);
  6844. /* use set_alpn_protos */
  6845. client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
  6846. server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
  6847. test_wolfSSL_client_server(&client_cb, &server_cb);
  6848. }
  6849. #endif /* HAVE_ALPN_PROTOS_SUPPORT */
  6850. static void test_wolfSSL_UseALPN(void)
  6851. {
  6852. #if defined(HAVE_ALPN) && !defined(NO_WOLFSSL_SERVER) &&\
  6853. defined(HAVE_IO_TESTS_DEPENDENCIES)
  6854. test_wolfSSL_UseALPN_connection();
  6855. test_wolfSSL_UseALPN_params();
  6856. #endif
  6857. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  6858. test_wolfSSL_set_alpn_protos();
  6859. #endif
  6860. }
  6861. static void test_wolfSSL_DisableExtendedMasterSecret(void)
  6862. {
  6863. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  6864. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  6865. WOLFSSL *ssl = wolfSSL_new(ctx);
  6866. AssertNotNull(ctx);
  6867. AssertNotNull(ssl);
  6868. /* error cases */
  6869. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  6870. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  6871. /* success cases */
  6872. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  6873. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  6874. wolfSSL_free(ssl);
  6875. wolfSSL_CTX_free(ctx);
  6876. #endif
  6877. }
  6878. static void test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  6879. {
  6880. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  6881. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  6882. WOLFSSL *ssl = wolfSSL_new(ctx);
  6883. AssertNotNull(ctx);
  6884. AssertNotNull(ssl);
  6885. /* error cases */
  6886. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  6887. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  6888. /* success cases */
  6889. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  6890. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  6891. wolfSSL_free(ssl);
  6892. wolfSSL_CTX_free(ctx);
  6893. #endif
  6894. }
  6895. /*----------------------------------------------------------------------------*
  6896. | X509 Tests
  6897. *----------------------------------------------------------------------------*/
  6898. static void test_wolfSSL_X509_NAME_get_entry(void)
  6899. {
  6900. #if !defined(NO_CERTS) && !defined(NO_RSA)
  6901. #if defined(OPENSSL_ALL) || \
  6902. (defined(OPENSSL_EXTRA) && \
  6903. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  6904. printf(testingFmt, "wolfSSL_X509_NAME_get_entry()");
  6905. {
  6906. /* use openssl like name to test mapping */
  6907. X509_NAME_ENTRY* ne;
  6908. X509_NAME* name;
  6909. X509* x509;
  6910. #ifndef NO_FILESYSTEM
  6911. ASN1_STRING* asn;
  6912. char* subCN = NULL;
  6913. #endif
  6914. int idx;
  6915. ASN1_OBJECT *object = NULL;
  6916. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)
  6917. #ifndef NO_BIO
  6918. BIO* bio;
  6919. #endif
  6920. #endif
  6921. #ifndef NO_FILESYSTEM
  6922. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  6923. AssertNotNull(x509);
  6924. name = X509_get_subject_name(x509);
  6925. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  6926. AssertIntGE(idx, 0);
  6927. ne = X509_NAME_get_entry(name, idx);
  6928. AssertNotNull(ne);
  6929. asn = X509_NAME_ENTRY_get_data(ne);
  6930. AssertNotNull(asn);
  6931. subCN = (char*)ASN1_STRING_data(asn);
  6932. AssertNotNull(subCN);
  6933. wolfSSL_FreeX509(x509);
  6934. #endif
  6935. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  6936. AssertNotNull(x509);
  6937. name = X509_get_subject_name(x509);
  6938. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  6939. AssertIntGE(idx, 0);
  6940. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)
  6941. #ifndef NO_BIO
  6942. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  6943. AssertIntEQ(X509_NAME_print_ex(bio, name, 4,
  6944. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  6945. AssertIntEQ(X509_NAME_print_ex_fp(stdout, name, 4,
  6946. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  6947. BIO_free(bio);
  6948. #endif
  6949. #endif
  6950. ne = X509_NAME_get_entry(name, idx);
  6951. AssertNotNull(ne);
  6952. AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
  6953. wolfSSL_FreeX509(x509);
  6954. }
  6955. printf(resultFmt, passed);
  6956. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  6957. #endif /* !NO_CERTS && !NO_RSA */
  6958. }
  6959. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  6960. static void test_wolfSSL_PKCS12(void)
  6961. {
  6962. /* .p12 file is encrypted with DES3 */
  6963. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  6964. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  6965. * Password Key
  6966. */
  6967. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  6968. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  6969. !defined(NO_SHA) && defined(HAVE_PKCS12) && !defined(NO_BIO)
  6970. byte buf[6000];
  6971. char file[] = "./certs/test-servercert.p12";
  6972. char order[] = "./certs/ecc-rsa-server.p12";
  6973. #ifdef WC_RC2
  6974. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  6975. #endif
  6976. char pass[] = "a password";
  6977. const char goodPsw[] = "wolfSSL test";
  6978. const char badPsw[] = "bad";
  6979. #ifdef HAVE_ECC
  6980. WOLFSSL_X509_NAME* subject;
  6981. WOLFSSL_X509 *x509;
  6982. #endif
  6983. XFILE f;
  6984. int bytes, ret, goodPswLen, badPswLen;
  6985. WOLFSSL_BIO *bio;
  6986. WOLFSSL_EVP_PKEY *pkey;
  6987. WC_PKCS12 *pkcs12;
  6988. WC_PKCS12 *pkcs12_2;
  6989. WOLFSSL_X509 *cert;
  6990. WOLFSSL_X509 *tmp;
  6991. WOLF_STACK_OF(WOLFSSL_X509) *ca;
  6992. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  6993. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  6994. WOLFSSL_CTX *ctx;
  6995. WOLFSSL *ssl;
  6996. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  6997. #endif
  6998. printf(testingFmt, "wolfSSL_PKCS12()");
  6999. f = XFOPEN(file, "rb");
  7000. AssertTrue((f != XBADFILE));
  7001. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  7002. XFCLOSE(f);
  7003. goodPswLen = (int)XSTRLEN(goodPsw);
  7004. badPswLen = (int)XSTRLEN(badPsw);
  7005. bio = BIO_new_mem_buf((void*)buf, bytes);
  7006. AssertNotNull(bio);
  7007. pkcs12 = d2i_PKCS12_bio(bio, NULL);
  7008. AssertNotNull(pkcs12);
  7009. PKCS12_free(pkcs12);
  7010. AssertIntEQ(BIO_write(bio, buf, bytes), bytes); /* d2i consumes BIO */
  7011. d2i_PKCS12_bio(bio, &pkcs12);
  7012. AssertNotNull(pkcs12);
  7013. BIO_free(bio);
  7014. /* check verify MAC directly */
  7015. ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen);
  7016. AssertIntEQ(ret, 1);
  7017. /* check verify MAC fail case directly */
  7018. ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen);
  7019. AssertIntEQ(ret, 0);
  7020. /* check verify MAC fail case */
  7021. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  7022. AssertIntEQ(ret, 0);
  7023. AssertNull(pkey);
  7024. AssertNull(cert);
  7025. /* check parse with no extra certs kept */
  7026. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  7027. AssertIntEQ(ret, 1);
  7028. AssertNotNull(pkey);
  7029. AssertNotNull(cert);
  7030. wolfSSL_EVP_PKEY_free(pkey);
  7031. wolfSSL_X509_free(cert);
  7032. /* check parse with extra certs kept */
  7033. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  7034. AssertIntEQ(ret, 1);
  7035. AssertNotNull(pkey);
  7036. AssertNotNull(cert);
  7037. AssertNotNull(ca);
  7038. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  7039. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  7040. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  7041. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  7042. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  7043. #else
  7044. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  7045. #endif
  7046. /* Copy stack structure */
  7047. AssertNotNull(tmp_ca = X509_chain_up_ref(ca));
  7048. AssertIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  7049. /* CTX now owns the tmp_ca stack structure */
  7050. tmp_ca = NULL;
  7051. AssertIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  7052. AssertNotNull(tmp_ca);
  7053. AssertIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  7054. /* Check that the main cert is also set */
  7055. AssertNotNull(SSL_CTX_get0_certificate(ctx));
  7056. AssertNotNull(ssl = SSL_new(ctx));
  7057. AssertNotNull(SSL_get_certificate(ssl));
  7058. SSL_free(ssl);
  7059. SSL_CTX_free(ctx);
  7060. #endif
  7061. /* should be 2 other certs on stack */
  7062. tmp = sk_X509_pop(ca);
  7063. AssertNotNull(tmp);
  7064. X509_free(tmp);
  7065. tmp = sk_X509_pop(ca);
  7066. AssertNotNull(tmp);
  7067. X509_free(tmp);
  7068. AssertNull(sk_X509_pop(ca));
  7069. EVP_PKEY_free(pkey);
  7070. X509_free(cert);
  7071. sk_X509_pop_free(ca, X509_free);
  7072. /* check PKCS12_create */
  7073. AssertNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  7074. AssertIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  7075. SSL_SUCCESS);
  7076. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  7077. -1, -1, 100, -1, 0)));
  7078. EVP_PKEY_free(pkey);
  7079. X509_free(cert);
  7080. sk_X509_pop_free(ca, NULL);
  7081. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  7082. SSL_SUCCESS);
  7083. PKCS12_free(pkcs12_2);
  7084. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  7085. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  7086. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  7087. 2000, 1, 0)));
  7088. EVP_PKEY_free(pkey);
  7089. X509_free(cert);
  7090. sk_X509_pop_free(ca, NULL);
  7091. /* convert to DER then back and parse */
  7092. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  7093. AssertIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  7094. PKCS12_free(pkcs12_2);
  7095. AssertNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  7096. BIO_free(bio);
  7097. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  7098. SSL_SUCCESS);
  7099. /* should be 2 other certs on stack */
  7100. tmp = sk_X509_pop(ca);
  7101. AssertNotNull(tmp);
  7102. X509_free(tmp);
  7103. tmp = sk_X509_pop(ca);
  7104. AssertNotNull(tmp);
  7105. X509_free(tmp);
  7106. AssertNull(sk_X509_pop(ca));
  7107. #ifndef NO_RC4
  7108. PKCS12_free(pkcs12_2);
  7109. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  7110. NID_pbe_WithSHA1And128BitRC4,
  7111. NID_pbe_WithSHA1And128BitRC4,
  7112. 2000, 1, 0)));
  7113. EVP_PKEY_free(pkey);
  7114. X509_free(cert);
  7115. sk_X509_pop_free(ca, NULL);
  7116. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  7117. SSL_SUCCESS);
  7118. #endif /* NO_RC4 */
  7119. EVP_PKEY_free(pkey);
  7120. X509_free(cert);
  7121. PKCS12_free(pkcs12);
  7122. PKCS12_free(pkcs12_2);
  7123. sk_X509_pop_free(ca, NULL);
  7124. #ifdef HAVE_ECC
  7125. /* test order of parsing */
  7126. f = XFOPEN(order, "rb");
  7127. AssertTrue(f != XBADFILE);
  7128. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  7129. XFCLOSE(f);
  7130. AssertNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  7131. AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  7132. AssertIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  7133. WOLFSSL_SUCCESS);
  7134. /* check use of pkey after parse */
  7135. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  7136. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  7137. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  7138. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  7139. #else
  7140. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  7141. #endif
  7142. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  7143. SSL_CTX_free(ctx);
  7144. #endif
  7145. AssertNotNull(pkey);
  7146. AssertNotNull(cert);
  7147. AssertNotNull(ca);
  7148. /* compare subject lines of certificates */
  7149. AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  7150. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  7151. SSL_FILETYPE_PEM));
  7152. AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  7153. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  7154. X509_free(x509);
  7155. /* test expected fail case */
  7156. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  7157. SSL_FILETYPE_PEM));
  7158. AssertIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  7159. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  7160. X509_free(x509);
  7161. X509_free(cert);
  7162. /* get subject line from ca stack */
  7163. AssertNotNull(cert = sk_X509_pop(ca));
  7164. AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  7165. /* compare subject from certificate in ca to expected */
  7166. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  7167. SSL_FILETYPE_PEM));
  7168. AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  7169. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  7170. EVP_PKEY_free(pkey);
  7171. X509_free(x509);
  7172. X509_free(cert);
  7173. BIO_free(bio);
  7174. PKCS12_free(pkcs12);
  7175. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  7176. /* test order of parsing */
  7177. f = XFOPEN(file, "rb");
  7178. AssertTrue(f != XBADFILE);
  7179. AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  7180. XFCLOSE(f);
  7181. /* check verify MAC fail case */
  7182. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  7183. AssertIntEQ(ret, 0);
  7184. AssertNull(pkey);
  7185. AssertNull(cert);
  7186. /* check parse with no extra certs kept */
  7187. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  7188. AssertIntEQ(ret, 1);
  7189. AssertNotNull(pkey);
  7190. AssertNotNull(cert);
  7191. wolfSSL_EVP_PKEY_free(pkey);
  7192. wolfSSL_X509_free(cert);
  7193. /* check parse with extra certs kept */
  7194. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  7195. AssertIntEQ(ret, 1);
  7196. AssertNotNull(pkey);
  7197. AssertNotNull(cert);
  7198. AssertNotNull(ca);
  7199. wolfSSL_EVP_PKEY_free(pkey);
  7200. wolfSSL_X509_free(cert);
  7201. sk_X509_pop_free(ca, NULL);
  7202. PKCS12_free(pkcs12);
  7203. #endif /* HAVE_ECC */
  7204. #ifdef WC_RC2
  7205. /* test PKCS#12 with RC2 encryption */
  7206. f = XFOPEN(rc2p12, "rb");
  7207. AssertTrue(f != XBADFILE);
  7208. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  7209. XFCLOSE(f);
  7210. AssertNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  7211. AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  7212. /* check verify MAC fail case */
  7213. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  7214. AssertIntEQ(ret, 0);
  7215. AssertNull(pkey);
  7216. AssertNull(cert);
  7217. /* check parse iwth not extra certs kept */
  7218. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  7219. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  7220. AssertNotNull(pkey);
  7221. AssertNotNull(cert);
  7222. /* check parse with extra certs kept */
  7223. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  7224. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  7225. AssertNotNull(pkey);
  7226. AssertNotNull(cert);
  7227. AssertNotNull(ca);
  7228. wolfSSL_EVP_PKEY_free(pkey);
  7229. wolfSSL_X509_free(cert);
  7230. sk_X509_pop_free(ca, NULL);
  7231. BIO_free(bio);
  7232. PKCS12_free(pkcs12);
  7233. #endif /* WC_RC2 */
  7234. /* Test i2d_PKCS12_bio */
  7235. f = XFOPEN(file, "rb");
  7236. AssertTrue((f != XBADFILE));
  7237. AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  7238. XFCLOSE(f);
  7239. bio = BIO_new(BIO_s_mem());
  7240. AssertNotNull(bio);
  7241. ret = i2d_PKCS12_bio(bio, pkcs12);
  7242. AssertIntEQ(ret, 1);
  7243. ret = i2d_PKCS12_bio(NULL, pkcs12);
  7244. AssertIntEQ(ret, 0);
  7245. ret = i2d_PKCS12_bio(bio, NULL);
  7246. AssertIntEQ(ret, 0);
  7247. PKCS12_free(pkcs12);
  7248. BIO_free(bio);
  7249. (void)order;
  7250. printf(resultFmt, passed);
  7251. #endif /* OPENSSL_EXTRA */
  7252. #endif /* HAVE_FIPS */
  7253. }
  7254. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  7255. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  7256. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  7257. #define TEST_PKCS8_ENC
  7258. #endif
  7259. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  7260. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  7261. /* used to keep track if FailTestCallback was called */
  7262. static int failTestCallbackCalled = 0;
  7263. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  7264. {
  7265. (void)passwd;
  7266. (void)sz;
  7267. (void)rw;
  7268. (void)userdata;
  7269. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  7270. failTestCallbackCalled = 1;
  7271. return -1;
  7272. }
  7273. #endif
  7274. static void test_wolfSSL_no_password_cb(void)
  7275. {
  7276. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  7277. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  7278. WOLFSSL_CTX* ctx;
  7279. byte buff[FOURK_BUF];
  7280. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  7281. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  7282. XFILE f;
  7283. int bytes;
  7284. printf(testingFmt, "test_wolfSSL_no_password_cb()");
  7285. #ifndef NO_WOLFSSL_CLIENT
  7286. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  7287. #else
  7288. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  7289. #endif
  7290. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  7291. AssertTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  7292. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7293. XFCLOSE(f);
  7294. AssertIntLE(bytes, sizeof(buff));
  7295. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7296. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7297. AssertTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  7298. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7299. XFCLOSE(f);
  7300. AssertIntLE(bytes, sizeof(buff));
  7301. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7302. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  7303. wolfSSL_CTX_free(ctx);
  7304. if (failTestCallbackCalled != 0) {
  7305. Fail(("Password callback should not be called by default"),
  7306. ("Password callback was called without attempting "
  7307. "to first decipher private key without password."));
  7308. }
  7309. printf(resultFmt, passed);
  7310. #endif
  7311. }
  7312. #ifdef TEST_PKCS8_ENC
  7313. /* for PKCS8 test case */
  7314. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  7315. {
  7316. int flag = 0;
  7317. (void)rw;
  7318. if (userdata != NULL) {
  7319. flag = *((int*)userdata); /* user set data */
  7320. }
  7321. switch (flag) {
  7322. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  7323. * can be anything the user wishes to be passed to the callback
  7324. * associated with the WOLFSSL_CTX */
  7325. XSTRNCPY(passwd, "yassl123", sz);
  7326. return 8;
  7327. default:
  7328. return BAD_FUNC_ARG;
  7329. }
  7330. }
  7331. #endif /* TEST_PKCS8_ENC */
  7332. /* Testing functions dealing with PKCS8 */
  7333. static void test_wolfSSL_PKCS8(void)
  7334. {
  7335. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8)
  7336. byte buff[FOURK_BUF];
  7337. byte der[FOURK_BUF];
  7338. #ifndef NO_RSA
  7339. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  7340. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  7341. #endif
  7342. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  7343. #ifdef HAVE_ECC
  7344. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  7345. #endif
  7346. XFILE f;
  7347. int bytes;
  7348. WOLFSSL_CTX* ctx;
  7349. #if defined(HAVE_ECC) && !defined(NO_CODING)
  7350. int ret;
  7351. ecc_key key;
  7352. word32 x = 0;
  7353. #endif
  7354. #ifdef TEST_PKCS8_ENC
  7355. #if !defined(NO_RSA) && !defined(NO_SHA)
  7356. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  7357. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  7358. #endif
  7359. #if defined(HAVE_ECC) && !defined(NO_SHA)
  7360. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  7361. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  7362. #endif
  7363. int flag;
  7364. #endif
  7365. (void)der;
  7366. printf(testingFmt, "wolfSSL_PKCS8()");
  7367. #ifndef NO_WOLFSSL_CLIENT
  7368. #ifndef WOLFSSL_NO_TLS12
  7369. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  7370. #else
  7371. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  7372. #endif
  7373. #else
  7374. #ifndef WOLFSSL_NO_TLS12
  7375. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  7376. #else
  7377. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  7378. #endif
  7379. #endif
  7380. #ifdef TEST_PKCS8_ENC
  7381. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  7382. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  7383. flag = 1; /* used by password callback as return code */
  7384. #if !defined(NO_RSA) && !defined(NO_SHA)
  7385. /* test loading PEM PKCS8 encrypted file */
  7386. f = XFOPEN(serverKeyPkcs8EncPemFile, "rb");
  7387. AssertTrue((f != XBADFILE));
  7388. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7389. XFCLOSE(f);
  7390. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7391. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  7392. /* this next case should fail because of password callback return code */
  7393. flag = 0; /* used by password callback as return code */
  7394. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7395. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  7396. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  7397. AssertIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  7398. "yassl123"), 0);
  7399. /* test that error value is returned with a bad password */
  7400. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  7401. "bad"), 0);
  7402. /* test loading PEM PKCS8 encrypted file */
  7403. f = XFOPEN(serverKeyPkcs8EncDerFile, "rb");
  7404. AssertTrue((f != XBADFILE));
  7405. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7406. XFCLOSE(f);
  7407. flag = 1; /* used by password callback as return code */
  7408. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7409. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7410. /* this next case should fail because of password callback return code */
  7411. flag = 0; /* used by password callback as return code */
  7412. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7413. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7414. #endif /* !NO_RSA && !NO_SHA */
  7415. #if defined(HAVE_ECC) && !defined(NO_SHA)
  7416. /* test loading PEM PKCS8 encrypted ECC Key file */
  7417. f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb");
  7418. AssertTrue((f != XBADFILE));
  7419. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7420. XFCLOSE(f);
  7421. flag = 1; /* used by password callback as return code */
  7422. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7423. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  7424. /* this next case should fail because of password callback return code */
  7425. flag = 0; /* used by password callback as return code */
  7426. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7427. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  7428. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  7429. AssertIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  7430. "yassl123"), 0);
  7431. /* test that error value is returned with a bad password */
  7432. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  7433. "bad"), 0);
  7434. /* test loading DER PKCS8 encrypted ECC Key file */
  7435. f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb");
  7436. AssertTrue((f != XBADFILE));
  7437. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7438. XFCLOSE(f);
  7439. flag = 1; /* used by password callback as return code */
  7440. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7441. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7442. /* this next case should fail because of password callback return code */
  7443. flag = 0; /* used by password callback as return code */
  7444. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7445. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7446. /* leave flag as "okay" */
  7447. flag = 1;
  7448. #endif /* HAVE_ECC && !NO_SHA */
  7449. #endif /* TEST_PKCS8_ENC */
  7450. #ifndef NO_RSA
  7451. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  7452. f = XFOPEN(serverKeyPkcs8DerFile, "rb");
  7453. AssertTrue((f != XBADFILE));
  7454. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7455. XFCLOSE(f);
  7456. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7457. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7458. /* test loading PEM PKCS8 private key file (not encrypted) */
  7459. f = XFOPEN(serverKeyPkcs8PemFile, "rb");
  7460. AssertTrue((f != XBADFILE));
  7461. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7462. XFCLOSE(f);
  7463. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7464. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  7465. #endif /* !NO_RSA */
  7466. /* Test PKCS8 PEM ECC key no crypt */
  7467. f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb");
  7468. AssertTrue((f != XBADFILE));
  7469. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7470. XFCLOSE(f);
  7471. #ifdef HAVE_ECC
  7472. /* Test PKCS8 PEM ECC key no crypt */
  7473. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7474. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  7475. #ifndef NO_CODING
  7476. /* decrypt PKCS8 PEM to key in DER format */
  7477. AssertIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  7478. (word32)sizeof(der), NULL)), 0);
  7479. ret = wc_ecc_init(&key);
  7480. if (ret == 0) {
  7481. ret = wc_EccPrivateKeyDecode(der, &x, &key, bytes);
  7482. wc_ecc_free(&key);
  7483. }
  7484. AssertIntEQ(ret, 0);
  7485. #endif
  7486. /* Test PKCS8 DER ECC key no crypt */
  7487. f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb");
  7488. AssertTrue((f != XBADFILE));
  7489. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7490. XFCLOSE(f);
  7491. /* Test using a PKCS8 ECC PEM */
  7492. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7493. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7494. #else
  7495. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  7496. AssertIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  7497. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  7498. #endif /* HAVE_ECC */
  7499. wolfSSL_CTX_free(ctx);
  7500. printf(resultFmt, passed);
  7501. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  7502. }
  7503. static void test_wolfSSL_PKCS8_ED25519(void)
  7504. {
  7505. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  7506. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  7507. defined(HAVE_ED25519_KEY_IMPORT)
  7508. const byte encPrivKey[] = \
  7509. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  7510. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  7511. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  7512. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  7513. "2L9W4v7swXkV+X+a1ww=\n"
  7514. "-----END ENCRYPTED PRIVATE KEY-----\n";
  7515. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  7516. byte der[FOURK_BUF];
  7517. WOLFSSL_CTX* ctx;
  7518. int bytes;
  7519. XMEMSET(der, 0, sizeof(der));
  7520. AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  7521. (word32)sizeof(der), password)), 0);
  7522. #ifndef NO_WOLFSSL_SERVER
  7523. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  7524. #else
  7525. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  7526. #endif
  7527. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  7528. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7529. wolfSSL_CTX_free(ctx);
  7530. #endif
  7531. }
  7532. static void test_wolfSSL_PKCS8_ED448(void)
  7533. {
  7534. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  7535. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  7536. defined(HAVE_ED448_KEY_IMPORT)
  7537. const byte encPrivKey[] = \
  7538. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  7539. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  7540. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  7541. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  7542. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  7543. "-----END ENCRYPTED PRIVATE KEY-----\n";
  7544. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  7545. byte der[FOURK_BUF];
  7546. WOLFSSL_CTX* ctx;
  7547. int bytes;
  7548. XMEMSET(der, 0, sizeof(der));
  7549. AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  7550. (word32)sizeof(der), password)), 0);
  7551. #ifndef NO_WOLFSSL_SERVER
  7552. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  7553. #else
  7554. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  7555. #endif
  7556. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  7557. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7558. wolfSSL_CTX_free(ctx);
  7559. #endif
  7560. }
  7561. /* Testing functions dealing with PKCS5 */
  7562. static void test_wolfSSL_PKCS5(void)
  7563. {
  7564. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  7565. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  7566. const char* passwd = "myfipsPa$$W0rd";
  7567. #else
  7568. const char *passwd = "pass1234";
  7569. #endif
  7570. const unsigned char *salt = (unsigned char *)"salt1234";
  7571. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  7572. DYNAMIC_TYPE_TMP_BUFFER);
  7573. int ret = 0;
  7574. AssertNotNull(out);
  7575. ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  7576. (int)XSTRLEN((const char *) salt), 10,
  7577. WC_SHA_DIGEST_SIZE,out);
  7578. AssertIntEQ(ret, SSL_SUCCESS);
  7579. #ifdef WOLFSSL_SHA512
  7580. ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  7581. (int)XSTRLEN((const char *) salt), 10,
  7582. wolfSSL_EVP_sha512(), WC_SHA_DIGEST_SIZE, out);
  7583. AssertIntEQ(ret, SSL_SUCCESS);
  7584. #endif
  7585. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7586. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  7587. }
  7588. /* test parsing URI from certificate */
  7589. static void test_wolfSSL_URI(void)
  7590. {
  7591. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  7592. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  7593. defined(OPENSSL_EXTRA))
  7594. WOLFSSL_X509* x509;
  7595. const char uri[] = "./certs/client-uri-cert.pem";
  7596. const char badUri[] = "./certs/client-relative-uri.pem";
  7597. printf(testingFmt, "wolfSSL URI parse");
  7598. x509 = wolfSSL_X509_load_certificate_file(uri, WOLFSSL_FILETYPE_PEM);
  7599. AssertNotNull(x509);
  7600. wolfSSL_FreeX509(x509);
  7601. x509 = wolfSSL_X509_load_certificate_file(badUri, WOLFSSL_FILETYPE_PEM);
  7602. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT)
  7603. AssertNull(x509);
  7604. #else
  7605. AssertNotNull(x509);
  7606. #endif
  7607. printf(resultFmt, passed);
  7608. #endif
  7609. }
  7610. static void test_wolfSSL_TBS(void)
  7611. {
  7612. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  7613. && defined(OPENSSL_EXTRA)
  7614. WOLFSSL_X509* x509;
  7615. const unsigned char* tbs;
  7616. int tbsSz;
  7617. printf(testingFmt, "wolfSSL TBS");
  7618. AssertNotNull(x509 =
  7619. wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
  7620. AssertNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  7621. AssertNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  7622. AssertNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  7623. AssertIntEQ(tbsSz, 1003);
  7624. wolfSSL_FreeX509(x509);
  7625. printf(resultFmt, passed);
  7626. #endif
  7627. }
  7628. static void test_wolfSSL_X509_verify(void)
  7629. {
  7630. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  7631. && defined(OPENSSL_EXTRA)
  7632. WOLFSSL_X509* ca;
  7633. WOLFSSL_X509* serv;
  7634. WOLFSSL_EVP_PKEY* pkey;
  7635. unsigned char buf[2048];
  7636. const unsigned char* pt = NULL;
  7637. int bufSz;
  7638. printf(testingFmt, "wolfSSL X509 verify");
  7639. AssertNotNull(ca =
  7640. wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
  7641. AssertIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  7642. WOLFSSL_SUCCESS);
  7643. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  7644. WOLFSSL_SUCCESS);
  7645. AssertIntEQ(bufSz, 294);
  7646. bufSz = 2048;
  7647. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  7648. WOLFSSL_SUCCESS);
  7649. AssertIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  7650. AssertIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  7651. AssertNotNull(serv =
  7652. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM));
  7653. /* success case */
  7654. pt = buf;
  7655. AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  7656. AssertIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  7657. AssertIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  7658. wolfSSL_EVP_PKEY_free(pkey);
  7659. /* fail case */
  7660. bufSz = 2048;
  7661. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  7662. WOLFSSL_SUCCESS);
  7663. pt = buf;
  7664. AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  7665. AssertIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  7666. AssertIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  7667. AssertIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  7668. wolfSSL_EVP_PKEY_free(pkey);
  7669. wolfSSL_FreeX509(ca);
  7670. wolfSSL_FreeX509(serv);
  7671. printf(resultFmt, passed);
  7672. #endif
  7673. }
  7674. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  7675. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  7676. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  7677. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  7678. /* create certificate with version 2 */
  7679. static void test_set_x509_badversion(WOLFSSL_CTX* ctx)
  7680. {
  7681. WOLFSSL_X509 *x509, *x509v2;
  7682. WOLFSSL_EVP_PKEY *priv, *pub;
  7683. unsigned char *der = NULL, *key = NULL, *pt;
  7684. char *header, *name;
  7685. int derSz;
  7686. long keySz;
  7687. XFILE fp;
  7688. WOLFSSL_ASN1_TIME *notBefore, *notAfter;
  7689. time_t t;
  7690. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  7691. WOLFSSL_FILETYPE_PEM));
  7692. fp = XFOPEN(cliKeyFile, "rb");
  7693. AssertIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  7694. WOLFSSL_SUCCESS);
  7695. XFCLOSE(fp);
  7696. pt = key;
  7697. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  7698. (const unsigned char**)&pt, keySz));
  7699. /* create the version 2 certificate */
  7700. AssertNotNull(x509v2 = X509_new());
  7701. AssertIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  7702. AssertIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  7703. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  7704. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  7705. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  7706. AssertNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  7707. AssertIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  7708. t = time(NULL);
  7709. AssertNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  7710. AssertNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  7711. AssertTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  7712. AssertTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  7713. AssertIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  7714. derSz = wolfSSL_i2d_X509(x509v2, &der);
  7715. AssertIntGT(derSz, 0);
  7716. AssertIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  7717. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7718. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL); /* TODO: Replace with API call */
  7719. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7720. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7721. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7722. wolfSSL_X509_free(x509);
  7723. wolfSSL_X509_free(x509v2);
  7724. wolfSSL_EVP_PKEY_free(priv);
  7725. wolfSSL_EVP_PKEY_free(pub);
  7726. wolfSSL_ASN1_TIME_free(notBefore);
  7727. wolfSSL_ASN1_TIME_free(notAfter);
  7728. }
  7729. /* override certificate version error */
  7730. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  7731. {
  7732. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  7733. AssertIntEQ(store->error, ASN_VERSION_E);
  7734. #else
  7735. AssertIntEQ(store->error, 0);
  7736. #endif
  7737. AssertIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  7738. (void)preverify;
  7739. return 1;
  7740. }
  7741. /* set verify callback that will override bad certificate version */
  7742. static void test_set_override_x509(WOLFSSL_CTX* ctx)
  7743. {
  7744. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  7745. }
  7746. #endif
  7747. static void test_wolfSSL_X509_TLS_version(void)
  7748. {
  7749. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  7750. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  7751. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  7752. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  7753. tcp_ready ready;
  7754. func_args server_args;
  7755. func_args client_args;
  7756. THREAD_TYPE serverThread;
  7757. callback_functions func_cb_client;
  7758. callback_functions func_cb_server;
  7759. printf(testingFmt, "test_wolfSSL_X509_TLS_version");
  7760. /* test server rejects a client certificate that is not version 3 */
  7761. #ifdef WOLFSSL_TIRTOS
  7762. fdOpenSession(Task_self());
  7763. #endif
  7764. XMEMSET(&server_args, 0, sizeof(func_args));
  7765. XMEMSET(&client_args, 0, sizeof(func_args));
  7766. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  7767. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  7768. StartTCP();
  7769. InitTcpReady(&ready);
  7770. #if defined(USE_WINDOWS_API)
  7771. /* use RNG to get random port if using windows */
  7772. ready.port = GetRandomPort();
  7773. #endif
  7774. server_args.signal = &ready;
  7775. client_args.signal = &ready;
  7776. server_args.return_code = TEST_FAIL;
  7777. client_args.return_code = TEST_FAIL;
  7778. func_cb_client.ctx_ready = &test_set_x509_badversion;
  7779. #ifndef WOLFSSL_NO_TLS12
  7780. func_cb_client.method = wolfTLSv1_2_client_method;
  7781. #else
  7782. func_cb_client.method = wolfTLSv1_3_client_method;
  7783. #endif
  7784. client_args.callbacks = &func_cb_client;
  7785. #ifndef WOLFSSL_NO_TLS12
  7786. func_cb_server.method = wolfTLSv1_2_server_method;
  7787. #else
  7788. func_cb_server.method = wolfTLSv1_3_server_method;
  7789. #endif
  7790. server_args.callbacks = &func_cb_server;
  7791. start_thread(test_server_nofail, &server_args, &serverThread);
  7792. wait_tcp_ready(&server_args);
  7793. test_client_nofail(&client_args, NULL);
  7794. join_thread(serverThread);
  7795. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  7796. AssertIntEQ(client_args.return_code, TEST_FAIL);
  7797. AssertIntEQ(server_args.return_code, TEST_FAIL);
  7798. #else
  7799. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  7800. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  7801. #endif
  7802. FreeTcpReady(&ready);
  7803. #ifdef WOLFSSL_TIRTOS
  7804. fdCloseSession(Task_self());
  7805. #endif
  7806. /* Now re run but override the bad X509 version */
  7807. #ifdef WOLFSSL_TIRTOS
  7808. fdOpenSession(Task_self());
  7809. #endif
  7810. XMEMSET(&server_args, 0, sizeof(func_args));
  7811. XMEMSET(&client_args, 0, sizeof(func_args));
  7812. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  7813. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  7814. StartTCP();
  7815. InitTcpReady(&ready);
  7816. #if defined(USE_WINDOWS_API)
  7817. /* use RNG to get random port if using windows */
  7818. ready.port = GetRandomPort();
  7819. #endif
  7820. server_args.signal = &ready;
  7821. client_args.signal = &ready;
  7822. server_args.return_code = TEST_FAIL;
  7823. client_args.return_code = TEST_FAIL;
  7824. func_cb_client.ctx_ready = &test_set_x509_badversion;
  7825. func_cb_server.ctx_ready = &test_set_override_x509;
  7826. #ifndef WOLFSSL_NO_TLS12
  7827. func_cb_client.method = wolfTLSv1_2_client_method;
  7828. #else
  7829. func_cb_client.method = wolfTLSv1_3_client_method;
  7830. #endif
  7831. client_args.callbacks = &func_cb_client;
  7832. #ifndef WOLFSSL_NO_TLS12
  7833. func_cb_server.method = wolfTLSv1_2_server_method;
  7834. #else
  7835. func_cb_server.method = wolfTLSv1_3_server_method;
  7836. #endif
  7837. server_args.callbacks = &func_cb_server;
  7838. start_thread(test_server_nofail, &server_args, &serverThread);
  7839. wait_tcp_ready(&server_args);
  7840. test_client_nofail(&client_args, NULL);
  7841. join_thread(serverThread);
  7842. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  7843. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  7844. FreeTcpReady(&ready);
  7845. #ifdef WOLFSSL_TIRTOS
  7846. fdCloseSession(Task_self());
  7847. #endif
  7848. printf(resultFmt, passed);
  7849. #endif
  7850. }
  7851. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  7852. * version allowed.
  7853. * POST: 1 on success.
  7854. */
  7855. static int test_wolfSSL_CTX_SetMinVersion(void)
  7856. {
  7857. int failFlag = WOLFSSL_SUCCESS;
  7858. #ifndef NO_WOLFSSL_CLIENT
  7859. WOLFSSL_CTX* ctx;
  7860. int itr;
  7861. #ifndef NO_OLD_TLS
  7862. const int versions[] = {
  7863. #ifdef WOLFSSL_ALLOW_TLSV10
  7864. WOLFSSL_TLSV1,
  7865. #endif
  7866. WOLFSSL_TLSV1_1,
  7867. WOLFSSL_TLSV1_2 };
  7868. #elif !defined(WOLFSSL_NO_TLS12)
  7869. const int versions[] = { WOLFSSL_TLSV1_2 };
  7870. #elif defined(WOLFSSL_TLS13)
  7871. const int versions[] = { WOLFSSL_TLSV1_3 };
  7872. #else
  7873. const int versions[0];
  7874. #endif
  7875. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7876. printf(testingFmt, "wolfSSL_CTX_SetMinVersion()");
  7877. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++){
  7878. if(wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr)) != WOLFSSL_SUCCESS){
  7879. failFlag = WOLFSSL_FAILURE;
  7880. }
  7881. }
  7882. printf(resultFmt, failFlag == WOLFSSL_SUCCESS ? passed : failed);
  7883. wolfSSL_CTX_free(ctx);
  7884. #endif
  7885. return failFlag;
  7886. } /* END test_wolfSSL_CTX_SetMinVersion */
  7887. /*----------------------------------------------------------------------------*
  7888. | OCSP Stapling
  7889. *----------------------------------------------------------------------------*/
  7890. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  7891. * need to contact the CA, lowering the cost of cert revocation checking.
  7892. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  7893. * POST: 1 returned for success.
  7894. */
  7895. static int test_wolfSSL_UseOCSPStapling(void)
  7896. {
  7897. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  7898. !defined(NO_WOLFSSL_CLIENT)
  7899. int ret;
  7900. WOLFSSL_CTX* ctx;
  7901. WOLFSSL* ssl;
  7902. #ifndef NO_WOLFSSL_CLIENT
  7903. #ifndef WOLFSSL_NO_TLS12
  7904. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  7905. #else
  7906. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  7907. #endif
  7908. #else
  7909. #ifndef WOLFSSL_NO_TLS12
  7910. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  7911. #else
  7912. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  7913. #endif
  7914. #endif
  7915. ssl = wolfSSL_new(ctx);
  7916. printf(testingFmt, "wolfSSL_UseOCSPStapling()");
  7917. ret = wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  7918. WOLFSSL_CSR2_OCSP_USE_NONCE);
  7919. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  7920. wolfSSL_free(ssl);
  7921. wolfSSL_CTX_free(ctx);
  7922. return ret;
  7923. #else
  7924. return WOLFSSL_SUCCESS;
  7925. #endif
  7926. } /*END test_wolfSSL_UseOCSPStapling */
  7927. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  7928. * stapling eliminates the need to contact the CA and lowers cert revocation
  7929. * check.
  7930. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  7931. */
  7932. static int test_wolfSSL_UseOCSPStaplingV2 (void)
  7933. {
  7934. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  7935. !defined(NO_WOLFSSL_CLIENT)
  7936. int ret;
  7937. WOLFSSL_CTX* ctx;
  7938. WOLFSSL* ssl;
  7939. #ifndef NO_WOLFSSL_CLIENT
  7940. #ifndef WOLFSSL_NO_TLS12
  7941. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  7942. #else
  7943. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  7944. #endif
  7945. #else
  7946. #ifndef WOLFSSL_NO_TLS12
  7947. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  7948. #else
  7949. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  7950. #endif
  7951. #endif
  7952. ssl = wolfSSL_new(ctx);
  7953. printf(testingFmt, "wolfSSL_UseOCSPStaplingV2()");
  7954. ret = wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  7955. WOLFSSL_CSR2_OCSP_USE_NONCE );
  7956. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  7957. wolfSSL_free(ssl);
  7958. wolfSSL_CTX_free(ctx);
  7959. return ret;
  7960. #else
  7961. return WOLFSSL_SUCCESS;
  7962. #endif
  7963. } /*END test_wolfSSL_UseOCSPStaplingV2*/
  7964. /*----------------------------------------------------------------------------*
  7965. | Multicast Tests
  7966. *----------------------------------------------------------------------------*/
  7967. static void test_wolfSSL_mcast(void)
  7968. {
  7969. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  7970. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  7971. WOLFSSL_CTX* ctx;
  7972. WOLFSSL* ssl;
  7973. int result;
  7974. byte preMasterSecret[512];
  7975. byte clientRandom[32];
  7976. byte serverRandom[32];
  7977. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  7978. byte buf[256];
  7979. word16 newId;
  7980. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method());
  7981. AssertNotNull(ctx);
  7982. result = wolfSSL_CTX_mcast_set_member_id(ctx, 0);
  7983. AssertIntEQ(result, WOLFSSL_SUCCESS);
  7984. ssl = wolfSSL_new(ctx);
  7985. AssertNotNull(ssl);
  7986. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  7987. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  7988. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  7989. result = wolfSSL_set_secret(ssl, 23,
  7990. preMasterSecret, sizeof(preMasterSecret),
  7991. clientRandom, serverRandom, suite);
  7992. AssertIntEQ(result, WOLFSSL_SUCCESS);
  7993. result = wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf));
  7994. AssertIntLE(result, 0);
  7995. AssertIntLE(newId, 100);
  7996. wolfSSL_free(ssl);
  7997. wolfSSL_CTX_free(ctx);
  7998. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 || WOLFSSL_SNIFFER) */
  7999. }
  8000. /*----------------------------------------------------------------------------*
  8001. | Wolfcrypt
  8002. *----------------------------------------------------------------------------*/
  8003. /*
  8004. * Unit test for the wc_InitBlake2b()
  8005. */
  8006. static int test_wc_InitBlake2b (void)
  8007. {
  8008. int ret = 0;
  8009. #ifdef HAVE_BLAKE2
  8010. Blake2b blake;
  8011. printf(testingFmt, "wc_InitBlake2B()");
  8012. /* Test good arg. */
  8013. ret = wc_InitBlake2b(&blake, 64);
  8014. if (ret != 0) {
  8015. ret = WOLFSSL_FATAL_ERROR;
  8016. }
  8017. /* Test bad arg. */
  8018. if (!ret) {
  8019. ret = wc_InitBlake2b(NULL, 64);
  8020. if (ret == 0) {
  8021. ret = WOLFSSL_FATAL_ERROR;
  8022. } else {
  8023. ret = 0;
  8024. }
  8025. }
  8026. if (!ret) {
  8027. ret = wc_InitBlake2b(NULL, 128);
  8028. if (ret == 0) {
  8029. ret = WOLFSSL_FATAL_ERROR;
  8030. } else {
  8031. ret = 0;
  8032. }
  8033. }
  8034. if (!ret) {
  8035. ret = wc_InitBlake2b(&blake, 128);
  8036. if (ret == 0) {
  8037. ret = WOLFSSL_FATAL_ERROR;
  8038. } else {
  8039. ret = 0;
  8040. }
  8041. }
  8042. if (!ret) {
  8043. ret = wc_InitBlake2b(NULL, 0);
  8044. if (ret == 0) {
  8045. ret = WOLFSSL_FATAL_ERROR;
  8046. } else {
  8047. ret = 0;
  8048. }
  8049. }
  8050. if (!ret) {
  8051. ret = wc_InitBlake2b(&blake, 0);
  8052. if (ret == 0) {
  8053. ret = WOLFSSL_FATAL_ERROR;
  8054. } else {
  8055. ret = 0;
  8056. }
  8057. }
  8058. printf(resultFmt, ret == 0 ? passed : failed);
  8059. #endif
  8060. return ret;
  8061. } /*END test_wc_InitBlake2b*/
  8062. /*
  8063. * Unit test for the wc_InitBlake2b_WithKey()
  8064. */
  8065. static int test_wc_InitBlake2b_WithKey (void)
  8066. {
  8067. int ret = 0;
  8068. #ifdef HAVE_BLAKE2
  8069. Blake2b blake;
  8070. word32 digestSz = BLAKE2B_KEYBYTES;
  8071. byte key[BLAKE2B_KEYBYTES];
  8072. word32 keylen = BLAKE2B_KEYBYTES;
  8073. printf(testingFmt, "wc_InitBlake2b_WithKey()");
  8074. /* Test good arg. */
  8075. ret = wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen);
  8076. if (ret != 0) {
  8077. ret = WOLFSSL_FATAL_ERROR;
  8078. }
  8079. /* Test bad args. */
  8080. if (ret == 0) {
  8081. ret = wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen);
  8082. if (ret == BAD_FUNC_ARG) {
  8083. ret = 0;
  8084. }
  8085. }
  8086. if (ret == 0) {
  8087. ret = wc_InitBlake2b_WithKey(&blake, digestSz, key, 256);
  8088. if (ret == BAD_FUNC_ARG) {
  8089. ret = 0;
  8090. }
  8091. }
  8092. if (ret == 0) {
  8093. ret = wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen);
  8094. }
  8095. printf(resultFmt, ret == 0 ? passed : failed);
  8096. #endif
  8097. return ret;
  8098. } /*END wc_InitBlake2b_WithKey*/
  8099. /*
  8100. * Unit test for the wc_InitBlake2s_WithKey()
  8101. */
  8102. static int test_wc_InitBlake2s_WithKey (void)
  8103. {
  8104. int ret = 0;
  8105. #ifdef HAVE_BLAKE2S
  8106. Blake2s blake;
  8107. word32 digestSz = BLAKE2S_KEYBYTES;
  8108. byte *key = (byte*)"01234567890123456789012345678901";
  8109. word32 keylen = BLAKE2S_KEYBYTES;
  8110. printf(testingFmt, "wc_InitBlake2s_WithKey()");
  8111. /* Test good arg. */
  8112. ret = wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen);
  8113. if (ret != 0) {
  8114. ret = WOLFSSL_FATAL_ERROR;
  8115. }
  8116. /* Test bad args. */
  8117. if (ret == 0) {
  8118. ret = wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen);
  8119. if (ret == BAD_FUNC_ARG) {
  8120. ret = 0;
  8121. }
  8122. }
  8123. if (ret == 0) {
  8124. ret = wc_InitBlake2s_WithKey(&blake, digestSz, key, 256);
  8125. if (ret == BAD_FUNC_ARG) {
  8126. ret = 0;
  8127. }
  8128. }
  8129. if (ret == 0) {
  8130. ret = wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen);
  8131. }
  8132. printf(resultFmt, ret == 0 ? passed : failed);
  8133. #endif
  8134. return ret;
  8135. } /*END wc_InitBlake2s_WithKey*/
  8136. /*
  8137. * Unit test for the wc_InitMd5()
  8138. */
  8139. static int test_wc_InitMd5 (void)
  8140. {
  8141. int flag = 0;
  8142. #ifndef NO_MD5
  8143. wc_Md5 md5;
  8144. int ret;
  8145. printf(testingFmt, "wc_InitMd5()");
  8146. /* Test good arg. */
  8147. ret = wc_InitMd5(&md5);
  8148. if (ret != 0) {
  8149. flag = WOLFSSL_FATAL_ERROR;
  8150. }
  8151. /* Test bad arg. */
  8152. if (!flag) {
  8153. ret = wc_InitMd5(NULL);
  8154. if (ret != BAD_FUNC_ARG) {
  8155. flag = WOLFSSL_FATAL_ERROR;
  8156. }
  8157. }
  8158. wc_Md5Free(&md5);
  8159. printf(resultFmt, flag == 0 ? passed : failed);
  8160. #endif
  8161. return flag;
  8162. } /* END test_wc_InitMd5 */
  8163. /*
  8164. * Testing wc_UpdateMd5()
  8165. */
  8166. static int test_wc_Md5Update (void)
  8167. {
  8168. int flag = 0;
  8169. #ifndef NO_MD5
  8170. wc_Md5 md5;
  8171. byte hash[WC_MD5_DIGEST_SIZE];
  8172. testVector a, b, c;
  8173. int ret;
  8174. ret = wc_InitMd5(&md5);
  8175. if (ret != 0) {
  8176. flag = ret;
  8177. }
  8178. printf(testingFmt, "wc_Md5Update()");
  8179. /* Input */
  8180. if (!flag) {
  8181. a.input = "a";
  8182. a.inLen = XSTRLEN(a.input);
  8183. ret = wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen);
  8184. if (ret != 0) {
  8185. flag = ret;
  8186. }
  8187. }
  8188. if (!flag) {
  8189. ret = wc_Md5Final(&md5, hash);
  8190. if (ret != 0) {
  8191. flag = ret;
  8192. }
  8193. }
  8194. /* Update input. */
  8195. if (!flag) {
  8196. a.input = "abc";
  8197. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  8198. "\x72";
  8199. a.inLen = XSTRLEN(a.input);
  8200. a.outLen = XSTRLEN(a.output);
  8201. ret = wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen);
  8202. if (ret != 0) {
  8203. flag = ret;
  8204. }
  8205. }
  8206. if (!flag) {
  8207. ret = wc_Md5Final(&md5, hash);
  8208. if (ret != 0) {
  8209. flag = ret;
  8210. }
  8211. }
  8212. if (!flag) {
  8213. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  8214. flag = WOLFSSL_FATAL_ERROR;
  8215. }
  8216. }
  8217. /*Pass in bad values. */
  8218. if (!flag) {
  8219. b.input = NULL;
  8220. b.inLen = 0;
  8221. ret = wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen);
  8222. if (ret != 0) {
  8223. flag = ret;
  8224. }
  8225. }
  8226. if (!flag) {
  8227. c.input = NULL;
  8228. c.inLen = WC_MD5_DIGEST_SIZE;
  8229. ret = wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen);
  8230. if (ret != BAD_FUNC_ARG) {
  8231. flag = WOLFSSL_FATAL_ERROR;
  8232. }
  8233. }
  8234. if (!flag) {
  8235. ret = wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen);
  8236. if (ret != BAD_FUNC_ARG) {
  8237. flag = WOLFSSL_FATAL_ERROR;
  8238. }
  8239. }
  8240. wc_Md5Free(&md5);
  8241. printf(resultFmt, flag == 0 ? passed : failed);
  8242. #endif
  8243. return flag;
  8244. } /* END test_wc_Md5Update() */
  8245. /*
  8246. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  8247. */
  8248. static int test_wc_Md5Final (void)
  8249. {
  8250. int flag = 0;
  8251. #ifndef NO_MD5
  8252. /* Instantiate */
  8253. wc_Md5 md5;
  8254. byte* hash_test[3];
  8255. byte hash1[WC_MD5_DIGEST_SIZE];
  8256. byte hash2[2*WC_MD5_DIGEST_SIZE];
  8257. byte hash3[5*WC_MD5_DIGEST_SIZE];
  8258. int times, i, ret;
  8259. /* Initialize */
  8260. ret = wc_InitMd5(&md5);
  8261. if (ret != 0) {
  8262. flag = ret;
  8263. }
  8264. if (!flag) {
  8265. hash_test[0] = hash1;
  8266. hash_test[1] = hash2;
  8267. hash_test[2] = hash3;
  8268. }
  8269. times = sizeof(hash_test)/sizeof(byte*);
  8270. /* Test good args. */
  8271. printf(testingFmt, "wc_Md5Final()");
  8272. for (i = 0; i < times; i++) {
  8273. if (!flag) {
  8274. ret = wc_Md5Final(&md5, hash_test[i]);
  8275. if (ret != 0) {
  8276. flag = WOLFSSL_FATAL_ERROR;
  8277. }
  8278. }
  8279. }
  8280. /* Test bad args. */
  8281. if (!flag) {
  8282. ret = wc_Md5Final(NULL, NULL);
  8283. if (ret != BAD_FUNC_ARG) {
  8284. flag = WOLFSSL_FATAL_ERROR;
  8285. }
  8286. }
  8287. if (!flag) {
  8288. ret = wc_Md5Final(NULL, hash1);
  8289. if (ret != BAD_FUNC_ARG) {
  8290. flag = WOLFSSL_FATAL_ERROR;
  8291. }
  8292. }
  8293. if (!flag) {
  8294. ret = wc_Md5Final(&md5, NULL);
  8295. if (ret != BAD_FUNC_ARG) {
  8296. flag = WOLFSSL_FATAL_ERROR;
  8297. }
  8298. }
  8299. wc_Md5Free(&md5);
  8300. printf(resultFmt, flag == 0 ? passed : failed);
  8301. #endif
  8302. return flag;
  8303. }
  8304. /*
  8305. * Unit test for the wc_InitSha()
  8306. */
  8307. static int test_wc_InitSha(void)
  8308. {
  8309. int flag = 0;
  8310. #ifndef NO_SHA
  8311. wc_Sha sha;
  8312. int ret;
  8313. printf(testingFmt, "wc_InitSha()");
  8314. /* Test good arg. */
  8315. ret = wc_InitSha(&sha);
  8316. if (ret != 0) {
  8317. flag = WOLFSSL_FATAL_ERROR;
  8318. }
  8319. /* Test bad arg. */
  8320. if (!flag) {
  8321. ret = wc_InitSha(NULL);
  8322. if (ret != BAD_FUNC_ARG) {
  8323. flag = WOLFSSL_FATAL_ERROR;
  8324. }
  8325. }
  8326. wc_ShaFree(&sha);
  8327. printf(resultFmt, flag == 0 ? passed : failed);
  8328. #endif
  8329. return flag;
  8330. } /* END test_wc_InitSha */
  8331. /*
  8332. * Tesing wc_ShaUpdate()
  8333. */
  8334. static int test_wc_ShaUpdate (void)
  8335. {
  8336. int flag = 0;
  8337. #ifndef NO_SHA
  8338. wc_Sha sha;
  8339. byte hash[WC_SHA_DIGEST_SIZE];
  8340. testVector a, b, c;
  8341. int ret;
  8342. ret = wc_InitSha(&sha);
  8343. if (ret != 0) {
  8344. flag = ret;
  8345. }
  8346. printf(testingFmt, "wc_ShaUpdate()");
  8347. /* Input. */
  8348. if (!flag) {
  8349. a.input = "a";
  8350. a.inLen = XSTRLEN(a.input);
  8351. ret = wc_ShaUpdate(&sha, NULL, 0);
  8352. if (ret != 0) {
  8353. flag = ret;
  8354. }
  8355. ret = wc_ShaUpdate(&sha, (byte*)a.input, 0);
  8356. if (ret != 0) {
  8357. flag = ret;
  8358. }
  8359. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  8360. if (ret != 0) {
  8361. flag = ret;
  8362. }
  8363. }
  8364. if (!flag) {
  8365. ret = wc_ShaFinal(&sha, hash);
  8366. if (ret != 0) {
  8367. flag = ret;
  8368. }
  8369. }
  8370. /* Update input. */
  8371. if (!flag) {
  8372. a.input = "abc";
  8373. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  8374. "\x6C\x9C\xD0\xD8\x9D";
  8375. a.inLen = XSTRLEN(a.input);
  8376. a.outLen = XSTRLEN(a.output);
  8377. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  8378. if (ret != 0) {
  8379. flag = ret;
  8380. }
  8381. }
  8382. if (!flag) {
  8383. ret = wc_ShaFinal(&sha, hash);
  8384. if (ret !=0) {
  8385. flag = ret;
  8386. }
  8387. }
  8388. if (!flag) {
  8389. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  8390. flag = WOLFSSL_FATAL_ERROR;
  8391. }
  8392. }
  8393. /* Try passing in bad values. */
  8394. if (!flag) {
  8395. b.input = NULL;
  8396. b.inLen = 0;
  8397. ret = wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen);
  8398. if (ret != 0) {
  8399. flag = ret;
  8400. }
  8401. }
  8402. if (!flag) {
  8403. c.input = NULL;
  8404. c.inLen = WC_SHA_DIGEST_SIZE;
  8405. ret = wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen);
  8406. if (ret != BAD_FUNC_ARG) {
  8407. flag = WOLFSSL_FATAL_ERROR;
  8408. }
  8409. }
  8410. if (!flag) {
  8411. ret = wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  8412. if (ret != BAD_FUNC_ARG) {
  8413. flag = WOLFSSL_FATAL_ERROR;
  8414. }
  8415. }
  8416. wc_ShaFree(&sha);
  8417. /* If not returned then the unit test passed test vectors. */
  8418. printf(resultFmt, flag == 0 ? passed : failed);
  8419. #endif
  8420. return flag;
  8421. } /* END test_wc_ShaUpdate() */
  8422. /*
  8423. * Unit test on wc_ShaFinal
  8424. */
  8425. static int test_wc_ShaFinal (void)
  8426. {
  8427. int flag = 0;
  8428. #ifndef NO_SHA
  8429. wc_Sha sha;
  8430. byte* hash_test[3];
  8431. byte hash1[WC_SHA_DIGEST_SIZE];
  8432. byte hash2[2*WC_SHA_DIGEST_SIZE];
  8433. byte hash3[5*WC_SHA_DIGEST_SIZE];
  8434. int times, i, ret;
  8435. /*Initialize*/
  8436. ret = wc_InitSha(&sha);
  8437. if (ret) {
  8438. flag = ret;
  8439. }
  8440. if (!flag) {
  8441. hash_test[0] = hash1;
  8442. hash_test[1] = hash2;
  8443. hash_test[2] = hash3;
  8444. }
  8445. times = sizeof(hash_test)/sizeof(byte*);
  8446. /* Good test args. */
  8447. printf(testingFmt, "wc_ShaFinal()");
  8448. for (i = 0; i < times; i++) {
  8449. if (!flag) {
  8450. ret = wc_ShaFinal(&sha, hash_test[i]);
  8451. if (ret != 0) {
  8452. flag = WOLFSSL_FATAL_ERROR;
  8453. }
  8454. }
  8455. }
  8456. /* Test bad args. */
  8457. if (!flag) {
  8458. ret = wc_ShaFinal(NULL, NULL);
  8459. if (ret != BAD_FUNC_ARG) {
  8460. flag = WOLFSSL_FATAL_ERROR;
  8461. }
  8462. }
  8463. if (!flag) {
  8464. ret = wc_ShaFinal(NULL, hash1);
  8465. if (ret != BAD_FUNC_ARG) {
  8466. flag = WOLFSSL_FATAL_ERROR;
  8467. }
  8468. }
  8469. if (!flag) {
  8470. ret = wc_ShaFinal(&sha, NULL);
  8471. if (ret != BAD_FUNC_ARG) {
  8472. flag = WOLFSSL_FATAL_ERROR;
  8473. }
  8474. }
  8475. wc_ShaFree(&sha);
  8476. printf(resultFmt, flag == 0 ? passed : failed);
  8477. #endif
  8478. return flag;
  8479. } /* END test_wc_ShaFinal */
  8480. /*
  8481. * Unit test for wc_InitSha256()
  8482. */
  8483. static int test_wc_InitSha256 (void)
  8484. {
  8485. int flag = 0;
  8486. #ifndef NO_SHA256
  8487. wc_Sha256 sha256;
  8488. int ret;
  8489. printf(testingFmt, "wc_InitSha256()");
  8490. /* Test good arg. */
  8491. ret = wc_InitSha256(&sha256);
  8492. if (ret != 0) {
  8493. flag = WOLFSSL_FATAL_ERROR;
  8494. }
  8495. /* Test bad arg. */
  8496. if (!flag) {
  8497. ret = wc_InitSha256(NULL);
  8498. if (ret != BAD_FUNC_ARG) {
  8499. flag = WOLFSSL_FATAL_ERROR;
  8500. }
  8501. }
  8502. wc_Sha256Free(&sha256);
  8503. printf(resultFmt, flag == 0 ? passed : failed);
  8504. #endif
  8505. return flag;
  8506. } /* END test_wc_InitSha256 */
  8507. /*
  8508. * Unit test for wc_Sha256Update()
  8509. */
  8510. static int test_wc_Sha256Update (void)
  8511. {
  8512. int flag = 0;
  8513. #ifndef NO_SHA256
  8514. wc_Sha256 sha256;
  8515. byte hash[WC_SHA256_DIGEST_SIZE];
  8516. testVector a, b, c;
  8517. int ret;
  8518. ret = wc_InitSha256(&sha256);
  8519. if (ret != 0) {
  8520. flag = ret;
  8521. }
  8522. printf(testingFmt, "wc_Sha256Update()");
  8523. /* Input. */
  8524. if (!flag) {
  8525. a.input = "a";
  8526. a.inLen = XSTRLEN(a.input);
  8527. ret = wc_Sha256Update(&sha256, NULL, 0);
  8528. if (ret != 0) {
  8529. flag = ret;
  8530. }
  8531. ret = wc_Sha256Update(&sha256, (byte*)a.input, 0);
  8532. if (ret != 0) {
  8533. flag = ret;
  8534. }
  8535. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  8536. if (ret != 0) {
  8537. flag = ret;
  8538. }
  8539. }
  8540. if (!flag) {
  8541. ret = wc_Sha256Final(&sha256, hash);
  8542. if (ret != 0) {
  8543. flag = ret;
  8544. }
  8545. }
  8546. /* Update input. */
  8547. if (!flag) {
  8548. a.input = "abc";
  8549. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  8550. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  8551. "\x15\xAD";
  8552. a.inLen = XSTRLEN(a.input);
  8553. a.outLen = XSTRLEN(a.output);
  8554. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  8555. if (ret != 0) {
  8556. flag = ret;
  8557. }
  8558. }
  8559. if (!flag) {
  8560. ret = wc_Sha256Final(&sha256, hash);
  8561. if (ret != 0) {
  8562. flag = ret;
  8563. }
  8564. }
  8565. if (!flag) {
  8566. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  8567. flag = WOLFSSL_FATAL_ERROR;
  8568. }
  8569. }
  8570. /* Try passing in bad values */
  8571. if (!flag) {
  8572. b.input = NULL;
  8573. b.inLen = 0;
  8574. ret = wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen);
  8575. if (ret != 0) {
  8576. flag = ret;
  8577. }
  8578. }
  8579. if (!flag) {
  8580. c.input = NULL;
  8581. c.inLen = WC_SHA256_DIGEST_SIZE;
  8582. ret = wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen);
  8583. if (ret != BAD_FUNC_ARG) {
  8584. flag = WOLFSSL_FATAL_ERROR;
  8585. }
  8586. }
  8587. if (!flag) {
  8588. ret = wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen);
  8589. if (ret != BAD_FUNC_ARG) {
  8590. flag = WOLFSSL_FATAL_ERROR;
  8591. }
  8592. }
  8593. wc_Sha256Free(&sha256);
  8594. /* If not returned then the unit test passed. */
  8595. printf(resultFmt, flag == 0 ? passed : failed);
  8596. #endif
  8597. return flag;
  8598. } /* END test_wc_Sha256Update */
  8599. /*
  8600. * Unit test function for wc_Sha256Final()
  8601. */
  8602. static int test_wc_Sha256Final (void)
  8603. {
  8604. int flag = 0;
  8605. #ifndef NO_SHA256
  8606. wc_Sha256 sha256;
  8607. byte* hash_test[3];
  8608. byte hash1[WC_SHA256_DIGEST_SIZE];
  8609. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  8610. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  8611. int times, i, ret;
  8612. /* Initialize */
  8613. ret = wc_InitSha256(&sha256);
  8614. if (ret != 0) {
  8615. flag = ret;
  8616. }
  8617. if (!flag) {
  8618. hash_test[0] = hash1;
  8619. hash_test[1] = hash2;
  8620. hash_test[2] = hash3;
  8621. }
  8622. times = sizeof(hash_test) / sizeof(byte*);
  8623. /* Good test args. */
  8624. printf(testingFmt, "wc_Sha256Final()");
  8625. for (i = 0; i < times; i++) {
  8626. if (!flag) {
  8627. ret = wc_Sha256Final(&sha256, hash_test[i]);
  8628. if (ret != 0) {
  8629. flag = WOLFSSL_FATAL_ERROR;
  8630. }
  8631. }
  8632. }
  8633. /* Test bad args. */
  8634. if (!flag ) {
  8635. ret = wc_Sha256Final(NULL, NULL);
  8636. if (ret != BAD_FUNC_ARG) {
  8637. flag = WOLFSSL_FATAL_ERROR;
  8638. }
  8639. }
  8640. if (!flag) {
  8641. ret = wc_Sha256Final(NULL, hash1);
  8642. if (ret != BAD_FUNC_ARG) {
  8643. flag = WOLFSSL_FATAL_ERROR;
  8644. }
  8645. }
  8646. if (!flag) {
  8647. ret = wc_Sha256Final(&sha256, NULL);
  8648. if (ret != BAD_FUNC_ARG) {
  8649. flag = WOLFSSL_FATAL_ERROR;
  8650. }
  8651. }
  8652. wc_Sha256Free(&sha256);
  8653. printf(resultFmt, flag == 0 ? passed : failed);
  8654. #endif
  8655. return flag;
  8656. } /* END test_wc_Sha256Final */
  8657. /*
  8658. * Unit test function for wc_Sha256FinalRaw()
  8659. */
  8660. static int test_wc_Sha256FinalRaw (void)
  8661. {
  8662. int flag = 0;
  8663. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  8664. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  8665. !defined(WOLFSSL_NO_HASH_RAW)
  8666. wc_Sha256 sha256;
  8667. byte* hash_test[3];
  8668. byte hash1[WC_SHA256_DIGEST_SIZE];
  8669. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  8670. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  8671. int times, i, ret;
  8672. /* Initialize */
  8673. ret = wc_InitSha256(&sha256);
  8674. if (ret != 0) {
  8675. flag = ret;
  8676. }
  8677. if (!flag) {
  8678. hash_test[0] = hash1;
  8679. hash_test[1] = hash2;
  8680. hash_test[2] = hash3;
  8681. }
  8682. times = sizeof(hash_test) / sizeof(byte*);
  8683. /* Good test args. */
  8684. printf(testingFmt, "wc_Sha256FinalRaw()");
  8685. for (i = 0; i < times; i++) {
  8686. if (!flag) {
  8687. ret = wc_Sha256FinalRaw(&sha256, hash_test[i]);
  8688. if (ret != 0) {
  8689. flag = WOLFSSL_FATAL_ERROR;
  8690. }
  8691. }
  8692. }
  8693. /* Test bad args. */
  8694. if (!flag ) {
  8695. ret = wc_Sha256FinalRaw(NULL, NULL);
  8696. if (ret != BAD_FUNC_ARG) {
  8697. flag = WOLFSSL_FATAL_ERROR;
  8698. }
  8699. }
  8700. if (!flag) {
  8701. ret = wc_Sha256FinalRaw(NULL, hash1);
  8702. if (ret != BAD_FUNC_ARG) {
  8703. flag = WOLFSSL_FATAL_ERROR;
  8704. }
  8705. }
  8706. if (!flag) {
  8707. ret = wc_Sha256FinalRaw(&sha256, NULL);
  8708. if (ret != BAD_FUNC_ARG) {
  8709. flag = WOLFSSL_FATAL_ERROR;
  8710. }
  8711. }
  8712. wc_Sha256Free(&sha256);
  8713. printf(resultFmt, flag == 0 ? passed : failed);
  8714. #endif
  8715. return flag;
  8716. } /* END test_wc_Sha256FinalRaw */
  8717. /*
  8718. * Unit test function for wc_Sha256GetFlags()
  8719. */
  8720. static int test_wc_Sha256GetFlags (void)
  8721. {
  8722. int flag = 0;
  8723. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  8724. wc_Sha256 sha256;
  8725. word32 flags = 0;
  8726. printf(testingFmt, "wc_Sha256GetFlags()");
  8727. /* Initialize */
  8728. flag = wc_InitSha256(&sha256);
  8729. if (flag == 0) {
  8730. flag = wc_Sha256GetFlags(&sha256, &flags);
  8731. }
  8732. if (flag == 0) {
  8733. if (flags & WC_HASH_FLAG_ISCOPY) {
  8734. flag = 0;
  8735. }
  8736. }
  8737. wc_Sha256Free(&sha256);
  8738. printf(resultFmt, flag == 0 ? passed : failed);
  8739. #endif
  8740. return flag;
  8741. } /* END test_wc_Sha256GetFlags */
  8742. /*
  8743. * Unit test function for wc_Sha256Free()
  8744. */
  8745. static int test_wc_Sha256Free (void)
  8746. {
  8747. int flag = 0;
  8748. #ifndef NO_SHA256
  8749. printf(testingFmt, "wc_Sha256Free()");
  8750. wc_Sha256Free(NULL);
  8751. printf(resultFmt, flag == 0 ? passed : failed);
  8752. #endif
  8753. return flag;
  8754. } /* END test_wc_Sha256Free */
  8755. /*
  8756. * Unit test function for wc_Sha256GetHash()
  8757. */
  8758. static int test_wc_Sha256GetHash (void)
  8759. {
  8760. int flag = 0;
  8761. #ifndef NO_SHA256
  8762. wc_Sha256 sha256;
  8763. byte hash1[WC_SHA256_DIGEST_SIZE];
  8764. printf(testingFmt, "wc_Sha256GetHash()");
  8765. /* Initialize */
  8766. flag = wc_InitSha256(&sha256);
  8767. if (flag == 0) {
  8768. flag = wc_Sha256GetHash(&sha256, hash1);
  8769. }
  8770. /*test bad arguments*/
  8771. if (flag == 0) {
  8772. flag = wc_Sha256GetHash(NULL, NULL);
  8773. if (flag == BAD_FUNC_ARG) {
  8774. flag = 0;
  8775. }
  8776. }
  8777. if (flag == 0) {
  8778. flag = wc_Sha256GetHash(NULL, hash1);
  8779. if (flag == BAD_FUNC_ARG) {
  8780. flag = 0;
  8781. }
  8782. }
  8783. if (flag == 0) {
  8784. flag = wc_Sha256GetHash(&sha256, NULL);
  8785. if (flag == BAD_FUNC_ARG) {
  8786. flag = 0;
  8787. }
  8788. }
  8789. wc_Sha256Free(&sha256);
  8790. printf(resultFmt, flag == 0 ? passed : failed);
  8791. #endif
  8792. return flag;
  8793. } /* END test_wc_Sha256GetHash */
  8794. /*
  8795. * Unit test function for wc_Sha256Copy()
  8796. */
  8797. static int test_wc_Sha256Copy (void)
  8798. {
  8799. int flag = 0;
  8800. #ifndef NO_SHA256
  8801. wc_Sha256 sha256;
  8802. wc_Sha256 temp;
  8803. printf(testingFmt, "wc_Sha256Copy()");
  8804. /* Initialize */
  8805. flag = wc_InitSha256(&sha256);
  8806. if (flag == 0) {
  8807. flag = wc_InitSha256(&temp);
  8808. }
  8809. if (flag == 0) {
  8810. flag = wc_Sha256Copy(&sha256, &temp);
  8811. }
  8812. /*test bad arguments*/
  8813. if (flag == 0) {
  8814. flag = wc_Sha256Copy(NULL, NULL);
  8815. if (flag == BAD_FUNC_ARG) {
  8816. flag = 0;
  8817. }
  8818. }
  8819. if (flag == 0) {
  8820. flag = wc_Sha256Copy(NULL, &temp);
  8821. if (flag == BAD_FUNC_ARG) {
  8822. flag = 0;
  8823. }
  8824. }
  8825. if (flag == 0) {
  8826. flag = wc_Sha256Copy(&sha256, NULL);
  8827. if (flag == BAD_FUNC_ARG) {
  8828. flag = 0;
  8829. }
  8830. }
  8831. wc_Sha256Free(&sha256);
  8832. wc_Sha256Free(&temp);
  8833. printf(resultFmt, flag == 0 ? passed : failed);
  8834. #endif
  8835. return flag;
  8836. } /* END test_wc_Sha256Copy */
  8837. /*
  8838. * Testing wc_InitSha512()
  8839. */
  8840. static int test_wc_InitSha512 (void)
  8841. {
  8842. int flag = 0;
  8843. #ifdef WOLFSSL_SHA512
  8844. wc_Sha512 sha512;
  8845. int ret;
  8846. printf(testingFmt, "wc_InitSha512()");
  8847. /* Test good arg. */
  8848. ret = wc_InitSha512(&sha512);
  8849. if (ret != 0) {
  8850. flag = WOLFSSL_FATAL_ERROR;
  8851. }
  8852. /* Test bad arg. */
  8853. if (!flag) {
  8854. ret = wc_InitSha512(NULL);
  8855. if (ret != BAD_FUNC_ARG) {
  8856. flag = WOLFSSL_FATAL_ERROR;
  8857. }
  8858. }
  8859. wc_Sha512Free(&sha512);
  8860. printf(resultFmt, flag == 0 ? passed : failed);
  8861. #endif
  8862. return flag;
  8863. } /* END test_wc_InitSha512 */
  8864. /*
  8865. * wc_Sha512Update() test.
  8866. */
  8867. static int test_wc_Sha512Update (void)
  8868. {
  8869. int flag = 0;
  8870. #ifdef WOLFSSL_SHA512
  8871. wc_Sha512 sha512;
  8872. byte hash[WC_SHA512_DIGEST_SIZE];
  8873. testVector a, b, c;
  8874. int ret;
  8875. ret = wc_InitSha512(&sha512);
  8876. if (ret != 0) {
  8877. flag = ret;
  8878. }
  8879. printf(testingFmt, "wc_Sha512Update()");
  8880. /* Input. */
  8881. if (!flag) {
  8882. a.input = "a";
  8883. a.inLen = XSTRLEN(a.input);
  8884. ret = wc_Sha512Update(&sha512, NULL, 0);
  8885. if (ret != 0) {
  8886. flag = ret;
  8887. }
  8888. ret = wc_Sha512Update(&sha512,(byte*)a.input, 0);
  8889. if (ret != 0) {
  8890. flag = ret;
  8891. }
  8892. ret = wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen);
  8893. if (ret != 0) {
  8894. flag = ret;
  8895. }
  8896. ret = wc_Sha512Final(&sha512, hash);
  8897. if (ret != 0) {
  8898. flag = ret;
  8899. }
  8900. }
  8901. /* Update input. */
  8902. if (!flag) {
  8903. a.input = "abc";
  8904. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  8905. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  8906. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  8907. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  8908. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  8909. a.inLen = XSTRLEN(a.input);
  8910. a.outLen = XSTRLEN(a.output);
  8911. ret = wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen);
  8912. if (ret != 0) {
  8913. flag = ret;
  8914. }
  8915. }
  8916. if (!flag) {
  8917. ret = wc_Sha512Final(&sha512, hash);
  8918. if (ret != 0) {
  8919. flag = ret;
  8920. }
  8921. }
  8922. if (!flag) {
  8923. if (XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE) != 0) {
  8924. flag = WOLFSSL_FATAL_ERROR;
  8925. }
  8926. }
  8927. /* Try passing in bad values */
  8928. if (!flag) {
  8929. b.input = NULL;
  8930. b.inLen = 0;
  8931. ret = wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen);
  8932. if (ret != 0) {
  8933. flag = ret;
  8934. }
  8935. }
  8936. if (!flag) {
  8937. c.input = NULL;
  8938. c.inLen = WC_SHA512_DIGEST_SIZE;
  8939. ret = wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen);
  8940. if (ret != BAD_FUNC_ARG) {
  8941. flag = WOLFSSL_FATAL_ERROR;
  8942. }
  8943. }
  8944. if (!flag) {
  8945. ret = wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen);
  8946. if (ret != BAD_FUNC_ARG) {
  8947. flag = WOLFSSL_FATAL_ERROR;
  8948. }
  8949. }
  8950. wc_Sha512Free(&sha512);
  8951. /* If not returned then the unit test passed test vectors. */
  8952. printf(resultFmt, flag == 0 ? passed : failed);
  8953. #endif
  8954. return flag;
  8955. } /* END test_wc_Sha512Update */
  8956. #ifdef WOLFSSL_SHA512
  8957. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  8958. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  8959. /* Perfoms test for
  8960. * - wc_Sha512Final/wc_Sha512FinalRaw
  8961. * - wc_Sha512_224Final/wc_Sha512_224Final
  8962. * - wc_Sha512_256Final/wc_Sha512_256Final
  8963. * parameter:
  8964. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  8965. * WC_HASH_TYPE_SHA512_256
  8966. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  8967. *return 0 on success
  8968. */
  8969. static int test_Sha512_Family_Final(int type, int isRaw)
  8970. {
  8971. wc_Sha512 sha512;
  8972. byte* hash_test[3];
  8973. byte hash1[WC_SHA512_DIGEST_SIZE];
  8974. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  8975. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  8976. int times, i, ret;
  8977. int(*initFp)(wc_Sha512*);
  8978. int(*finalFp)(wc_Sha512*, byte*);
  8979. void(*freeFp)(wc_Sha512*);
  8980. if (type == WC_HASH_TYPE_SHA512) {
  8981. initFp = wc_InitSha512;
  8982. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  8983. !defined(WOLFSSL_NO_HASH_RAW)
  8984. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  8985. #else
  8986. finalFp = (isRaw)? NULL : wc_Sha512Final;
  8987. #endif
  8988. freeFp = wc_Sha512Free;
  8989. }
  8990. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  8991. #if !defined(WOLFSSL_NOSHA512_224)
  8992. else if (type == WC_HASH_TYPE_SHA512_224) {
  8993. initFp = wc_InitSha512_224;
  8994. #if !defined(WOLFSSL_NO_HASH_RAW)
  8995. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  8996. #else
  8997. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  8998. #endif
  8999. freeFp = wc_Sha512_224Free;
  9000. }
  9001. #endif
  9002. #if !defined(WOLFSSL_NOSHA512_256)
  9003. else if (type == WC_HASH_TYPE_SHA512_256) {
  9004. initFp = wc_InitSha512_256;
  9005. #if !defined(WOLFSSL_NO_HASH_RAW)
  9006. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  9007. #else
  9008. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  9009. #endif
  9010. freeFp = wc_Sha512_256Free;
  9011. }
  9012. #endif
  9013. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9014. else
  9015. return BAD_FUNC_ARG;
  9016. /* Initialize */
  9017. ret = initFp(&sha512);
  9018. if (!ret) {
  9019. hash_test[0] = hash1;
  9020. hash_test[1] = hash2;
  9021. hash_test[2] = hash3;
  9022. }
  9023. times = sizeof(hash_test) / sizeof(byte *);
  9024. /* Good test args. */
  9025. for (i = 0; i < times && ret == 0; i++) {
  9026. ret = finalFp(&sha512, hash_test[i]);
  9027. }
  9028. /* Test bad args. */
  9029. if (!ret) {
  9030. if (finalFp(NULL, NULL) != BAD_FUNC_ARG) {
  9031. ret = WOLFSSL_FATAL_ERROR;
  9032. }
  9033. }
  9034. if (!ret) {
  9035. if (finalFp(NULL, hash1) != BAD_FUNC_ARG) {
  9036. ret = WOLFSSL_FATAL_ERROR;
  9037. }
  9038. }
  9039. if (!ret) {
  9040. if (finalFp(&sha512, NULL) != BAD_FUNC_ARG) {
  9041. ret = WOLFSSL_FATAL_ERROR;
  9042. }
  9043. }
  9044. freeFp(&sha512);
  9045. return ret;
  9046. }
  9047. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  9048. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  9049. #endif /* WOLFSSL_SHA512 */
  9050. /*
  9051. * Unit test function for wc_Sha512Final()
  9052. */
  9053. static int test_wc_Sha512Final (void)
  9054. {
  9055. int flag = 0;
  9056. #ifdef WOLFSSL_SHA512
  9057. wc_Sha512 sha512;
  9058. byte* hash_test[3];
  9059. byte hash1[WC_SHA512_DIGEST_SIZE];
  9060. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  9061. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  9062. int times, i, ret;
  9063. /* Initialize */
  9064. ret = wc_InitSha512(&sha512);
  9065. if (ret != 0) {
  9066. flag = ret;
  9067. }
  9068. if (!flag) {
  9069. hash_test[0] = hash1;
  9070. hash_test[1] = hash2;
  9071. hash_test[2] = hash3;
  9072. }
  9073. times = sizeof(hash_test) / sizeof(byte *);
  9074. /* Good test args. */
  9075. printf(testingFmt, "wc_Sha512Final()");
  9076. for (i = 0; i < times; i++) {
  9077. if (!flag) {
  9078. ret = wc_Sha512Final(&sha512, hash_test[i]);
  9079. if (ret != 0) {
  9080. flag = WOLFSSL_FATAL_ERROR;
  9081. }
  9082. }
  9083. }
  9084. /* Test bad args. */
  9085. if (!flag) {
  9086. ret = wc_Sha512Final(NULL, NULL);
  9087. if (ret != BAD_FUNC_ARG) {
  9088. flag = WOLFSSL_FATAL_ERROR;
  9089. }
  9090. if (!flag) {}
  9091. ret = wc_Sha512Final(NULL, hash1);
  9092. if (ret != BAD_FUNC_ARG) {
  9093. flag = WOLFSSL_FATAL_ERROR;
  9094. }
  9095. }
  9096. if (!flag) {
  9097. ret = wc_Sha512Final(&sha512, NULL);
  9098. if (ret != BAD_FUNC_ARG) {
  9099. flag = WOLFSSL_FATAL_ERROR;
  9100. }
  9101. }
  9102. wc_Sha512Free(&sha512);
  9103. printf(resultFmt, flag == 0 ? passed : failed);
  9104. #endif
  9105. return flag;
  9106. } /* END test_wc_Sha512Final */
  9107. /*
  9108. * Unit test function for wc_Sha512GetFlags()
  9109. */
  9110. static int test_wc_Sha512GetFlags (void)
  9111. {
  9112. int flag = 0;
  9113. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  9114. wc_Sha512 sha512;
  9115. word32 flags = 0;
  9116. printf(testingFmt, "wc_Sha512GetFlags()");
  9117. /* Initialize */
  9118. flag = wc_InitSha512(&sha512);
  9119. if (flag == 0) {
  9120. flag = wc_Sha512GetFlags(&sha512, &flags);
  9121. }
  9122. if (flag == 0) {
  9123. if (flags & WC_HASH_FLAG_ISCOPY) {
  9124. flag = 0;
  9125. }
  9126. }
  9127. wc_Sha512Free(&sha512);
  9128. printf(resultFmt, flag == 0 ? passed : failed);
  9129. #endif
  9130. return flag;
  9131. } /* END test_wc_Sha512GetFlags */
  9132. /*
  9133. * Unit test function for wc_Sha512FinalRaw()
  9134. */
  9135. static int test_wc_Sha512FinalRaw (void)
  9136. {
  9137. int flag = 0;
  9138. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  9139. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  9140. !defined(WOLFSSL_NO_HASH_RAW)
  9141. wc_Sha512 sha512;
  9142. byte* hash_test[3];
  9143. byte hash1[WC_SHA512_DIGEST_SIZE];
  9144. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  9145. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  9146. int times, i, ret;
  9147. /* Initialize */
  9148. ret = wc_InitSha512(&sha512);
  9149. if (ret != 0) {
  9150. flag = ret;
  9151. }
  9152. if (!flag) {
  9153. hash_test[0] = hash1;
  9154. hash_test[1] = hash2;
  9155. hash_test[2] = hash3;
  9156. }
  9157. times = sizeof(hash_test) / sizeof(byte*);
  9158. /* Good test args. */
  9159. printf(testingFmt, "wc_Sha512FinalRaw()");
  9160. for (i = 0; i < times; i++) {
  9161. if (!flag) {
  9162. ret = wc_Sha512FinalRaw(&sha512, hash_test[i]);
  9163. if (ret != 0) {
  9164. flag = WOLFSSL_FATAL_ERROR;
  9165. }
  9166. }
  9167. }
  9168. /* Test bad args. */
  9169. if (!flag ) {
  9170. ret = wc_Sha512FinalRaw(NULL, NULL);
  9171. if (ret != BAD_FUNC_ARG) {
  9172. flag = WOLFSSL_FATAL_ERROR;
  9173. }
  9174. }
  9175. if (!flag) {
  9176. ret = wc_Sha512FinalRaw(NULL, hash1);
  9177. if (ret != BAD_FUNC_ARG) {
  9178. flag = WOLFSSL_FATAL_ERROR;
  9179. }
  9180. }
  9181. if (!flag) {
  9182. ret = wc_Sha512FinalRaw(&sha512, NULL);
  9183. if (ret != BAD_FUNC_ARG) {
  9184. flag = WOLFSSL_FATAL_ERROR;
  9185. }
  9186. }
  9187. wc_Sha512Free(&sha512);
  9188. printf(resultFmt, flag == 0 ? passed : failed);
  9189. #endif
  9190. return flag;
  9191. } /* END test_wc_Sha512FinalRaw */
  9192. /*
  9193. * Unit test function for wc_Sha512Free()
  9194. */
  9195. static int test_wc_Sha512Free (void)
  9196. {
  9197. int flag = 0;
  9198. #ifdef WOLFSSL_SHA512
  9199. printf(testingFmt, "wc_Sha512Free()");
  9200. wc_Sha512Free(NULL);
  9201. printf(resultFmt, flag == 0 ? passed : failed);
  9202. #endif
  9203. return flag;
  9204. } /* END test_wc_Sha512Free */
  9205. #ifdef WOLFSSL_SHA512
  9206. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  9207. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  9208. static int test_Sha512_Family_GetHash(int type )
  9209. {
  9210. int flag = 0;
  9211. int(*initFp)(wc_Sha512*);
  9212. int(*ghashFp)(wc_Sha512*, byte*);
  9213. wc_Sha512 sha512;
  9214. byte hash1[WC_SHA512_DIGEST_SIZE];
  9215. if (type == WC_HASH_TYPE_SHA512) {
  9216. initFp = wc_InitSha512;
  9217. ghashFp = wc_Sha512GetHash;
  9218. }
  9219. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9220. #if !defined(WOLFSSL_NOSHA512_224)
  9221. else if (type == WC_HASH_TYPE_SHA512_224) {
  9222. initFp = wc_InitSha512_224;
  9223. ghashFp = wc_Sha512_224GetHash;
  9224. }
  9225. #endif
  9226. #if !defined(WOLFSSL_NOSHA512_256)
  9227. else if (type == WC_HASH_TYPE_SHA512_256) {
  9228. initFp = wc_InitSha512_256;
  9229. ghashFp = wc_Sha512_256GetHash;
  9230. }
  9231. #endif
  9232. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9233. else {
  9234. initFp = NULL;
  9235. ghashFp = NULL;
  9236. }
  9237. if (initFp == NULL || ghashFp == NULL)
  9238. return WOLFSSL_FATAL_ERROR;
  9239. if (!flag) {
  9240. flag = initFp(&sha512);
  9241. }
  9242. if (!flag) {
  9243. flag = ghashFp(&sha512, hash1);
  9244. }
  9245. /*test bad arguments*/
  9246. if (!flag) {
  9247. if (ghashFp(NULL, NULL) != BAD_FUNC_ARG )
  9248. flag = WOLFSSL_FATAL_ERROR;
  9249. }
  9250. if (!flag) {
  9251. if (ghashFp(NULL, hash1) != BAD_FUNC_ARG )
  9252. flag = WOLFSSL_FATAL_ERROR;
  9253. }
  9254. if (!flag) {
  9255. if (ghashFp(&sha512, NULL) != BAD_FUNC_ARG )
  9256. flag = WOLFSSL_FATAL_ERROR;
  9257. }
  9258. wc_Sha512Free(&sha512);
  9259. return flag;
  9260. }
  9261. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  9262. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  9263. #endif /* WOLFSSL_SHA512 */
  9264. /*
  9265. * Unit test function for wc_Sha512GetHash()
  9266. */
  9267. static int test_wc_Sha512GetHash (void)
  9268. {
  9269. int flag = 0;
  9270. #ifdef WOLFSSL_SHA512
  9271. wc_Sha512 sha512;
  9272. byte hash1[WC_SHA512_DIGEST_SIZE];
  9273. printf(testingFmt, "wc_Sha512GetHash()");
  9274. /* Initialize */
  9275. flag = wc_InitSha512(&sha512);
  9276. if (flag == 0) {
  9277. flag = wc_Sha512GetHash(&sha512, hash1);
  9278. }
  9279. /*test bad arguments*/
  9280. if (flag == 0) {
  9281. flag = wc_Sha512GetHash(NULL, NULL);
  9282. if (flag == BAD_FUNC_ARG) {
  9283. flag = 0;
  9284. }
  9285. }
  9286. if (flag == 0) {
  9287. flag = wc_Sha512GetHash(NULL, hash1);
  9288. if (flag == BAD_FUNC_ARG) {
  9289. flag = 0;
  9290. }
  9291. }
  9292. if (flag == 0) {
  9293. flag = wc_Sha512GetHash(&sha512, NULL);
  9294. if (flag == BAD_FUNC_ARG) {
  9295. flag = 0;
  9296. }
  9297. }
  9298. wc_Sha512Free(&sha512);
  9299. printf(resultFmt, flag == 0 ? passed : failed);
  9300. #endif
  9301. return flag;
  9302. } /* END test_wc_Sha512GetHash */
  9303. /*
  9304. * Unit test function for wc_Sha512Copy()
  9305. */
  9306. static int test_wc_Sha512Copy (void)
  9307. {
  9308. int flag = 0;
  9309. #ifdef WOLFSSL_SHA512
  9310. wc_Sha512 sha512;
  9311. wc_Sha512 temp;
  9312. printf(testingFmt, "wc_Sha512Copy()");
  9313. /* Initialize */
  9314. flag = wc_InitSha512(&sha512);
  9315. if (flag == 0) {
  9316. flag = wc_InitSha512(&temp);
  9317. }
  9318. if (flag == 0) {
  9319. flag = wc_Sha512Copy(&sha512, &temp);
  9320. }
  9321. /*test bad arguments*/
  9322. if (flag == 0) {
  9323. flag = wc_Sha512Copy(NULL, NULL);
  9324. if (flag == BAD_FUNC_ARG) {
  9325. flag = 0;
  9326. }
  9327. }
  9328. if (flag == 0) {
  9329. flag = wc_Sha512Copy(NULL, &temp);
  9330. if (flag == BAD_FUNC_ARG) {
  9331. flag = 0;
  9332. }
  9333. }
  9334. if (flag == 0) {
  9335. flag = wc_Sha512Copy(&sha512, NULL);
  9336. if (flag == BAD_FUNC_ARG) {
  9337. flag = 0;
  9338. }
  9339. }
  9340. wc_Sha512Free(&sha512);
  9341. wc_Sha512Free(&temp);
  9342. printf(resultFmt, flag == 0 ? passed : failed);
  9343. #endif
  9344. return flag;
  9345. } /* END test_wc_Sha512Copy */
  9346. static int test_wc_InitSha512_224 (void)
  9347. {
  9348. int flag = 0;
  9349. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9350. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  9351. wc_Sha512 sha512;
  9352. int ret;
  9353. printf(testingFmt, "wc_InitSha512_224()");
  9354. /* Test good arg. */
  9355. ret = wc_InitSha512_224(&sha512);
  9356. if (ret != 0) {
  9357. flag = WOLFSSL_FATAL_ERROR;
  9358. }
  9359. /* Test bad arg. */
  9360. if (!flag) {
  9361. ret = wc_InitSha512_224(NULL);
  9362. if (ret != BAD_FUNC_ARG) {
  9363. flag = WOLFSSL_FATAL_ERROR;
  9364. }
  9365. }
  9366. wc_Sha512_224Free(&sha512);
  9367. printf(resultFmt, flag == 0 ? passed : failed);
  9368. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  9369. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9370. return flag;
  9371. }
  9372. static int test_wc_Sha512_224Update (void)
  9373. {
  9374. int flag = 0;
  9375. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9376. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  9377. wc_Sha512 sha512;
  9378. byte hash[WC_SHA512_DIGEST_SIZE];
  9379. testVector a, c;
  9380. int ret;
  9381. ret = wc_InitSha512_224(&sha512);
  9382. if (ret != 0) {
  9383. flag = ret;
  9384. }
  9385. printf(testingFmt, "wc_Sha512_224Update()");
  9386. /* Input. */
  9387. if (!flag) {
  9388. a.input = "a";
  9389. a.inLen = XSTRLEN(a.input);
  9390. ret = wc_Sha512_224Update(&sha512, NULL, 0);
  9391. if (ret != 0) {
  9392. flag = ret;
  9393. }
  9394. ret = wc_Sha512_224Update(&sha512,(byte*)a.input, 0);
  9395. if (ret != 0) {
  9396. flag = ret;
  9397. }
  9398. ret = wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen);
  9399. if (ret != 0) {
  9400. flag = ret;
  9401. }
  9402. ret = wc_Sha512_224Final(&sha512, hash);
  9403. if (ret != 0) {
  9404. flag = ret;
  9405. }
  9406. }
  9407. /* Update input. */
  9408. if (!flag) {
  9409. a.input = "abc";
  9410. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  9411. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  9412. a.inLen = XSTRLEN(a.input);
  9413. a.outLen = XSTRLEN(a.output);
  9414. ret = wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen);
  9415. if (ret != 0) {
  9416. flag = ret;
  9417. }
  9418. }
  9419. if (!flag) {
  9420. ret = wc_Sha512_224Final(&sha512, hash);
  9421. if (ret != 0) {
  9422. flag = ret;
  9423. }
  9424. }
  9425. if (!flag) {
  9426. if (XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE) != 0) {
  9427. flag = WOLFSSL_FATAL_ERROR;
  9428. }
  9429. }
  9430. if (!flag) {
  9431. c.input = NULL;
  9432. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  9433. ret = wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen);
  9434. if (ret != BAD_FUNC_ARG) {
  9435. flag = WOLFSSL_FATAL_ERROR;
  9436. }
  9437. }
  9438. if (!flag) {
  9439. ret = wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen);
  9440. if (ret != BAD_FUNC_ARG) {
  9441. flag = WOLFSSL_FATAL_ERROR;
  9442. }
  9443. }
  9444. wc_Sha512_224Free(&sha512);
  9445. /* If not returned then the unit test passed test vectors. */
  9446. printf(resultFmt, flag == 0 ? passed : failed);
  9447. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  9448. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9449. return flag;
  9450. }
  9451. static int test_wc_Sha512_224Final (void)
  9452. {
  9453. int flag = 0;
  9454. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9455. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  9456. printf(testingFmt, "wc_Sha512_224Final()");
  9457. flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0);
  9458. printf(resultFmt, flag == 0 ? passed : failed);
  9459. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  9460. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9461. return flag;
  9462. }
  9463. static int test_wc_Sha512_224GetFlags (void)
  9464. {
  9465. int flag = 0;
  9466. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9467. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  9468. wc_Sha512 sha512, copy;
  9469. word32 flags = 0;
  9470. printf(testingFmt, "wc_Sha512_224GetFlags()");
  9471. /* Initialize */
  9472. flag = wc_InitSha512_224(&sha512);
  9473. if (!flag) {
  9474. flag = wc_InitSha512_224(&copy);
  9475. }
  9476. if (!flag) {
  9477. flag = wc_Sha512_224Copy(&sha512, &copy);
  9478. }
  9479. if (!flag) {
  9480. flag = wc_Sha512_224GetFlags(&copy, &flags);
  9481. }
  9482. if (!flag) {
  9483. if (flags & WC_HASH_FLAG_ISCOPY)
  9484. flag = 0;
  9485. else
  9486. flag = WOLFSSL_FATAL_ERROR;
  9487. }
  9488. wc_Sha512_224Free(&copy);
  9489. wc_Sha512_224Free(&sha512);
  9490. printf(resultFmt, flag == 0 ? passed : failed);
  9491. #endif
  9492. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9493. return flag;
  9494. }
  9495. static int test_wc_Sha512_224FinalRaw (void)
  9496. {
  9497. int flag = 0;
  9498. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  9499. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  9500. !defined(WOLFSSL_NO_HASH_RAW)
  9501. printf(testingFmt, "wc_Sha512_224FinalRaw()");
  9502. flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1);
  9503. printf(resultFmt, flag == 0 ? passed : failed);
  9504. #endif
  9505. return flag;
  9506. }
  9507. static int test_wc_Sha512_224Free (void)
  9508. {
  9509. int flag = 0;
  9510. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9511. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  9512. printf(testingFmt, "wc_Sha512_224Free()");
  9513. wc_Sha512_224Free(NULL);
  9514. printf(resultFmt, passed);
  9515. #endif
  9516. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9517. return flag;
  9518. }
  9519. static int test_wc_Sha512_224GetHash (void)
  9520. {
  9521. int flag = 0;
  9522. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9523. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  9524. printf(testingFmt, "wc_Sha512_224GetHash()");
  9525. flag = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224);
  9526. printf(resultFmt, flag == 0 ? passed : failed);
  9527. #endif
  9528. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9529. return flag;
  9530. }
  9531. static int test_wc_Sha512_224Copy (void)
  9532. {
  9533. int flag = 0;
  9534. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9535. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  9536. wc_Sha512 sha512;
  9537. wc_Sha512 temp;
  9538. printf(testingFmt, "wc_Sha512_224Copy()");
  9539. /* Initialize */
  9540. flag = wc_InitSha512_224(&sha512);
  9541. if (flag == 0) {
  9542. flag = wc_InitSha512_224(&temp);
  9543. }
  9544. if (flag == 0) {
  9545. flag = wc_Sha512_224Copy(&sha512, &temp);
  9546. }
  9547. /*test bad arguments*/
  9548. if (flag == 0) {
  9549. if (wc_Sha512_224Copy(NULL, NULL) != BAD_FUNC_ARG)
  9550. flag = WOLFSSL_FATAL_ERROR;
  9551. }
  9552. if (flag == 0) {
  9553. if (wc_Sha512_224Copy(NULL, &temp) != BAD_FUNC_ARG)
  9554. flag = WOLFSSL_FATAL_ERROR;
  9555. }
  9556. if (flag == 0) {
  9557. if (wc_Sha512_224Copy(&sha512, NULL) != BAD_FUNC_ARG)
  9558. flag = WOLFSSL_FATAL_ERROR;
  9559. }
  9560. wc_Sha512_224Free(&sha512);
  9561. wc_Sha512_224Free(&temp);
  9562. printf(resultFmt, flag == 0 ? passed : failed);
  9563. #endif
  9564. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9565. return flag;
  9566. }
  9567. static int test_wc_InitSha512_256 (void)
  9568. {
  9569. int flag = 0;
  9570. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9571. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  9572. wc_Sha512 sha512;
  9573. int ret;
  9574. printf(testingFmt, "wc_InitSha512_256()");
  9575. /* Test good arg. */
  9576. ret = wc_InitSha512_256(&sha512);
  9577. if (ret != 0) {
  9578. flag = WOLFSSL_FATAL_ERROR;
  9579. }
  9580. /* Test bad arg. */
  9581. if (!flag) {
  9582. ret = wc_InitSha512_256(NULL);
  9583. if (ret != BAD_FUNC_ARG) {
  9584. flag = WOLFSSL_FATAL_ERROR;
  9585. }
  9586. }
  9587. wc_Sha512_256Free(&sha512);
  9588. printf(resultFmt, flag == 0 ? passed : failed);
  9589. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  9590. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9591. return flag;
  9592. }
  9593. static int test_wc_Sha512_256Update (void)
  9594. {
  9595. int flag = 0;
  9596. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9597. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  9598. wc_Sha512 sha512;
  9599. byte hash[WC_SHA512_DIGEST_SIZE];
  9600. testVector a, c;
  9601. int ret;
  9602. ret = wc_InitSha512_256(&sha512);
  9603. if (ret != 0) {
  9604. flag = ret;
  9605. }
  9606. printf(testingFmt, "wc_Sha512_256Update()");
  9607. /* Input. */
  9608. if (!flag) {
  9609. a.input = "a";
  9610. a.inLen = XSTRLEN(a.input);
  9611. ret = wc_Sha512_256Update(&sha512, NULL, 0);
  9612. if (ret != 0) {
  9613. flag = ret;
  9614. }
  9615. ret = wc_Sha512_256Update(&sha512,(byte*)a.input, 0);
  9616. if (ret != 0) {
  9617. flag = ret;
  9618. }
  9619. ret = wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen);
  9620. if (ret != 0) {
  9621. flag = ret;
  9622. }
  9623. ret = wc_Sha512_256Final(&sha512, hash);
  9624. if (ret != 0) {
  9625. flag = ret;
  9626. }
  9627. }
  9628. /* Update input. */
  9629. if (!flag) {
  9630. a.input = "abc";
  9631. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  9632. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  9633. "\x07\xe7\xaf\x23";
  9634. a.inLen = XSTRLEN(a.input);
  9635. a.outLen = XSTRLEN(a.output);
  9636. ret = wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen);
  9637. if (ret != 0) {
  9638. flag = ret;
  9639. }
  9640. }
  9641. if (!flag) {
  9642. ret = wc_Sha512_256Final(&sha512, hash);
  9643. if (ret != 0) {
  9644. flag = ret;
  9645. }
  9646. }
  9647. if (!flag) {
  9648. if (XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE) != 0) {
  9649. flag = WOLFSSL_FATAL_ERROR;
  9650. }
  9651. }
  9652. if (!flag) {
  9653. c.input = NULL;
  9654. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  9655. ret = wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen);
  9656. if (ret != BAD_FUNC_ARG) {
  9657. flag = WOLFSSL_FATAL_ERROR;
  9658. }
  9659. }
  9660. if (!flag) {
  9661. ret = wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen);
  9662. if (ret != BAD_FUNC_ARG) {
  9663. flag = WOLFSSL_FATAL_ERROR;
  9664. }
  9665. }
  9666. wc_Sha512_256Free(&sha512);
  9667. /* If not returned then the unit test passed test vectors. */
  9668. printf(resultFmt, flag == 0 ? passed : failed);
  9669. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  9670. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9671. return flag;
  9672. }
  9673. static int test_wc_Sha512_256Final (void)
  9674. {
  9675. int flag = 0;
  9676. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9677. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  9678. printf(testingFmt, "wc_Sha512_256Final()");
  9679. flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0);
  9680. printf(resultFmt, flag == 0 ? passed : failed);
  9681. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  9682. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9683. return flag;
  9684. }
  9685. static int test_wc_Sha512_256GetFlags (void)
  9686. {
  9687. int flag = 0;
  9688. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9689. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  9690. wc_Sha512 sha512, copy;
  9691. word32 flags = 0;
  9692. printf(testingFmt, "wc_Sha512_256GetFlags()");
  9693. /* Initialize */
  9694. flag = wc_InitSha512_256(&sha512);
  9695. if (!flag ) {
  9696. flag = wc_InitSha512_256(&copy);
  9697. }
  9698. if (!flag ) {
  9699. flag = wc_Sha512_256Copy(&sha512, &copy);
  9700. }
  9701. if (!flag ) {
  9702. flag = wc_Sha512_256GetFlags(&copy, &flags);
  9703. }
  9704. if (!flag) {
  9705. if (flags & WC_HASH_FLAG_ISCOPY)
  9706. flag = 0;
  9707. else
  9708. flag = WOLFSSL_FATAL_ERROR;
  9709. }
  9710. wc_Sha512_256Free(&sha512);
  9711. printf(resultFmt, flag == 0 ? passed : failed);
  9712. #endif
  9713. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9714. return flag;
  9715. }
  9716. static int test_wc_Sha512_256FinalRaw (void)
  9717. {
  9718. int flag = 0;
  9719. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  9720. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  9721. !defined(WOLFSSL_NO_HASH_RAW)
  9722. printf(testingFmt, "wc_Sha512_256FinalRaw()");
  9723. flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1);
  9724. printf(resultFmt, flag == 0 ? passed : failed);
  9725. #endif
  9726. return flag;
  9727. }
  9728. static int test_wc_Sha512_256Free (void)
  9729. {
  9730. int flag = 0;
  9731. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9732. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  9733. printf(testingFmt, "wc_Sha512_256Free()");
  9734. wc_Sha512_256Free(NULL);
  9735. printf(resultFmt, passed);
  9736. #endif
  9737. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9738. return flag;
  9739. }
  9740. static int test_wc_Sha512_256GetHash (void)
  9741. {
  9742. int flag = 0;
  9743. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9744. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  9745. printf(testingFmt, "wc_Sha512_256GetHash()");
  9746. flag = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256);
  9747. printf(resultFmt, flag == 0 ? passed : failed);
  9748. #endif
  9749. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9750. return flag;
  9751. }
  9752. static int test_wc_Sha512_256Copy (void)
  9753. {
  9754. int flag = 0;
  9755. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9756. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  9757. wc_Sha512 sha512;
  9758. wc_Sha512 temp;
  9759. printf(testingFmt, "wc_Sha512_256Copy()");
  9760. /* Initialize */
  9761. flag = wc_InitSha512_256(&sha512);
  9762. if (flag == 0) {
  9763. flag = wc_InitSha512_256(&temp);
  9764. }
  9765. if (flag == 0) {
  9766. flag = wc_Sha512_256Copy(&sha512, &temp);
  9767. }
  9768. /*test bad arguments*/
  9769. if (flag == 0) {
  9770. if (wc_Sha512_256Copy(NULL, NULL) != BAD_FUNC_ARG)
  9771. flag = WOLFSSL_FATAL_ERROR;
  9772. }
  9773. if (flag == 0) {
  9774. if (wc_Sha512_256Copy(NULL, &temp) != BAD_FUNC_ARG)
  9775. flag = WOLFSSL_FATAL_ERROR;
  9776. }
  9777. if (flag == 0) {
  9778. if (wc_Sha512_256Copy(&sha512, NULL) != BAD_FUNC_ARG)
  9779. flag = WOLFSSL_FATAL_ERROR;
  9780. }
  9781. wc_Sha512_256Free(&sha512);
  9782. wc_Sha512_256Free(&temp);
  9783. printf(resultFmt, flag == 0 ? passed : failed);
  9784. #endif
  9785. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9786. return flag;
  9787. }
  9788. /*
  9789. * Testing wc_InitSha384()
  9790. */
  9791. static int test_wc_InitSha384 (void)
  9792. {
  9793. int flag = 0;
  9794. #ifdef WOLFSSL_SHA384
  9795. wc_Sha384 sha384;
  9796. int ret;
  9797. printf(testingFmt, "wc_InitSha384()");
  9798. /* Test good arg. */
  9799. ret = wc_InitSha384(&sha384);
  9800. if (ret != 0) {
  9801. flag = WOLFSSL_FATAL_ERROR;
  9802. }
  9803. /* Test bad arg. */
  9804. if (!flag) {
  9805. ret = wc_InitSha384(NULL);
  9806. if (ret != BAD_FUNC_ARG) {
  9807. flag = WOLFSSL_FATAL_ERROR;
  9808. }
  9809. }
  9810. wc_Sha384Free(&sha384);
  9811. printf(resultFmt, flag == 0 ? passed : failed);
  9812. #endif
  9813. return flag;
  9814. } /* END test_wc_InitSha384 */
  9815. /*
  9816. * test wc_Sha384Update()
  9817. */
  9818. static int test_wc_Sha384Update (void)
  9819. {
  9820. int flag = 0;
  9821. #ifdef WOLFSSL_SHA384
  9822. wc_Sha384 sha384;
  9823. byte hash[WC_SHA384_DIGEST_SIZE];
  9824. testVector a, b, c;
  9825. int ret;
  9826. ret = wc_InitSha384(&sha384);
  9827. if (ret != 0) {
  9828. flag = ret;
  9829. }
  9830. printf(testingFmt, "wc_Sha384Update()");
  9831. /* Input */
  9832. if (!flag) {
  9833. a.input = "a";
  9834. a.inLen = XSTRLEN(a.input);
  9835. ret = wc_Sha384Update(&sha384, NULL, 0);
  9836. if (ret != 0) {
  9837. flag = ret;
  9838. }
  9839. ret = wc_Sha384Update(&sha384, (byte*)a.input, 0);
  9840. if (ret != 0) {
  9841. flag = ret;
  9842. }
  9843. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  9844. if (ret != 0) {
  9845. flag = ret;
  9846. }
  9847. }
  9848. if (!flag) {
  9849. ret = wc_Sha384Final(&sha384, hash);
  9850. if (ret != 0) {
  9851. flag = ret;
  9852. }
  9853. }
  9854. /* Update input. */
  9855. if (!flag) {
  9856. a.input = "abc";
  9857. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  9858. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  9859. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  9860. "\xc8\x25\xa7";
  9861. a.inLen = XSTRLEN(a.input);
  9862. a.outLen = XSTRLEN(a.output);
  9863. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  9864. if (ret != 0) {
  9865. flag = ret;
  9866. }
  9867. }
  9868. if (!flag) {
  9869. ret = wc_Sha384Final(&sha384, hash);
  9870. if (ret != 0) {
  9871. flag = ret;
  9872. }
  9873. }
  9874. if (!flag) {
  9875. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  9876. flag = WOLFSSL_FATAL_ERROR;
  9877. }
  9878. }
  9879. /* Pass in bad values. */
  9880. if (!flag) {
  9881. b.input = NULL;
  9882. b.inLen = 0;
  9883. ret = wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen);
  9884. if (ret != 0) {
  9885. flag = ret;
  9886. }
  9887. }
  9888. if (!flag) {
  9889. c.input = NULL;
  9890. c.inLen = WC_SHA384_DIGEST_SIZE;
  9891. ret = wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen);
  9892. if (ret != BAD_FUNC_ARG) {
  9893. flag = WOLFSSL_FATAL_ERROR;
  9894. }
  9895. }
  9896. if (!flag) {
  9897. ret = wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen);
  9898. if (ret != BAD_FUNC_ARG) {
  9899. flag = WOLFSSL_FATAL_ERROR;
  9900. }
  9901. }
  9902. wc_Sha384Free(&sha384);
  9903. /* If not returned then the unit test passed test vectors. */
  9904. printf(resultFmt, flag == 0 ? passed : failed);
  9905. #endif
  9906. return flag;
  9907. } /* END test_wc_Sha384Update */
  9908. /*
  9909. * Unit test function for wc_Sha384Final();
  9910. */
  9911. static int test_wc_Sha384Final (void)
  9912. {
  9913. int flag = 0;
  9914. #ifdef WOLFSSL_SHA384
  9915. wc_Sha384 sha384;
  9916. byte* hash_test[3];
  9917. byte hash1[WC_SHA384_DIGEST_SIZE];
  9918. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  9919. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  9920. int times, i, ret;
  9921. /* Initialize */
  9922. ret = wc_InitSha384(&sha384);
  9923. if (ret) {
  9924. flag = ret;
  9925. }
  9926. if (!flag) {
  9927. hash_test[0] = hash1;
  9928. hash_test[1] = hash2;
  9929. hash_test[2] = hash3;
  9930. }
  9931. times = sizeof(hash_test) / sizeof(byte*);
  9932. /* Good test args. */
  9933. printf(testingFmt, "wc_Sha384Final()");
  9934. for (i = 0; i < times; i++) {
  9935. if (!flag) {
  9936. ret = wc_Sha384Final(&sha384, hash_test[i]);
  9937. if (ret != 0) {
  9938. flag = WOLFSSL_FATAL_ERROR;
  9939. }
  9940. }
  9941. }
  9942. /* Test bad args. */
  9943. if (!flag) {
  9944. ret = wc_Sha384Final(NULL, NULL);
  9945. if (ret != BAD_FUNC_ARG) {
  9946. flag = WOLFSSL_FATAL_ERROR;
  9947. }
  9948. }
  9949. if (!flag) {
  9950. ret = wc_Sha384Final(NULL, hash1);
  9951. if (ret != BAD_FUNC_ARG) {
  9952. flag = WOLFSSL_FATAL_ERROR;
  9953. }
  9954. }
  9955. if (!flag) {
  9956. ret = wc_Sha384Final(&sha384, NULL);
  9957. if (ret != BAD_FUNC_ARG) {
  9958. flag = WOLFSSL_FATAL_ERROR;
  9959. }
  9960. }
  9961. wc_Sha384Free(&sha384);
  9962. printf(resultFmt, flag == 0 ? passed : failed);
  9963. #endif
  9964. return flag;
  9965. } /* END test_wc_Sha384Final */
  9966. /*
  9967. * Unit test function for wc_Sha384GetFlags()
  9968. */
  9969. static int test_wc_Sha384GetFlags (void)
  9970. {
  9971. int flag = 0;
  9972. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  9973. wc_Sha384 sha384;
  9974. word32 flags = 0;
  9975. printf(testingFmt, "wc_Sha384GetFlags()");
  9976. /* Initialize */
  9977. flag = wc_InitSha384(&sha384);
  9978. if (flag == 0) {
  9979. flag = wc_Sha384GetFlags(&sha384, &flags);
  9980. }
  9981. if (flag == 0) {
  9982. if (flags & WC_HASH_FLAG_ISCOPY) {
  9983. flag = 0;
  9984. }
  9985. }
  9986. wc_Sha384Free(&sha384);
  9987. printf(resultFmt, flag == 0 ? passed : failed);
  9988. #endif
  9989. return flag;
  9990. } /* END test_wc_Sha384GetFlags */
  9991. /*
  9992. * Unit test function for wc_Sha384FinalRaw()
  9993. */
  9994. static int test_wc_Sha384FinalRaw (void)
  9995. {
  9996. int flag = 0;
  9997. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  9998. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  9999. !defined(WOLFSSL_NO_HASH_RAW)
  10000. wc_Sha384 sha384;
  10001. byte* hash_test[3];
  10002. byte hash1[WC_SHA384_DIGEST_SIZE];
  10003. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  10004. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  10005. int times, i, ret;
  10006. /* Initialize */
  10007. ret = wc_InitSha384(&sha384);
  10008. if (ret != 0) {
  10009. flag = ret;
  10010. }
  10011. if (!flag) {
  10012. hash_test[0] = hash1;
  10013. hash_test[1] = hash2;
  10014. hash_test[2] = hash3;
  10015. }
  10016. times = sizeof(hash_test) / sizeof(byte*);
  10017. /* Good test args. */
  10018. printf(testingFmt, "wc_Sha384FinalRaw()");
  10019. for (i = 0; i < times; i++) {
  10020. if (!flag) {
  10021. ret = wc_Sha384FinalRaw(&sha384, hash_test[i]);
  10022. if (ret != 0) {
  10023. flag = WOLFSSL_FATAL_ERROR;
  10024. }
  10025. }
  10026. }
  10027. /* Test bad args. */
  10028. if (!flag ) {
  10029. ret = wc_Sha384FinalRaw(NULL, NULL);
  10030. if (ret != BAD_FUNC_ARG) {
  10031. flag = WOLFSSL_FATAL_ERROR;
  10032. }
  10033. }
  10034. if (!flag) {
  10035. ret = wc_Sha384FinalRaw(NULL, hash1);
  10036. if (ret != BAD_FUNC_ARG) {
  10037. flag = WOLFSSL_FATAL_ERROR;
  10038. }
  10039. }
  10040. if (!flag) {
  10041. ret = wc_Sha384FinalRaw(&sha384, NULL);
  10042. if (ret != BAD_FUNC_ARG) {
  10043. flag = WOLFSSL_FATAL_ERROR;
  10044. }
  10045. }
  10046. wc_Sha384Free(&sha384);
  10047. printf(resultFmt, flag == 0 ? passed : failed);
  10048. #endif
  10049. return flag;
  10050. } /* END test_wc_Sha384FinalRaw */
  10051. /*
  10052. * Unit test function for wc_Sha384Free()
  10053. */
  10054. static int test_wc_Sha384Free (void)
  10055. {
  10056. int flag = 0;
  10057. #ifdef WOLFSSL_SHA384
  10058. printf(testingFmt, "wc_Sha384Free()");
  10059. wc_Sha384Free(NULL);
  10060. printf(resultFmt, flag == 0 ? passed : failed);
  10061. #endif
  10062. return flag;
  10063. } /* END test_wc_Sha384Free */
  10064. /*
  10065. * Unit test function for wc_Sha384GetHash()
  10066. */
  10067. static int test_wc_Sha384GetHash (void)
  10068. {
  10069. int flag = 0;
  10070. #ifdef WOLFSSL_SHA384
  10071. wc_Sha384 sha384;
  10072. byte hash1[WC_SHA384_DIGEST_SIZE];
  10073. printf(testingFmt, "wc_Sha384GetHash()");
  10074. /* Initialize */
  10075. flag = wc_InitSha384(&sha384);
  10076. if (flag == 0) {
  10077. flag = wc_Sha384GetHash(&sha384, hash1);
  10078. }
  10079. /*test bad arguments*/
  10080. if (flag == 0) {
  10081. flag = wc_Sha384GetHash(NULL, NULL);
  10082. if (flag == BAD_FUNC_ARG) {
  10083. flag = 0;
  10084. }
  10085. }
  10086. if (flag == 0) {
  10087. flag = wc_Sha384GetHash(NULL, hash1);
  10088. if (flag == BAD_FUNC_ARG) {
  10089. flag = 0;
  10090. }
  10091. }
  10092. if (flag == 0) {
  10093. flag = wc_Sha384GetHash(&sha384, NULL);
  10094. if (flag == BAD_FUNC_ARG) {
  10095. flag = 0;
  10096. }
  10097. }
  10098. wc_Sha384Free(&sha384);
  10099. printf(resultFmt, flag == 0 ? passed : failed);
  10100. #endif
  10101. return flag;
  10102. } /* END test_wc_Sha384GetHash */
  10103. /*
  10104. * Unit test function for wc_Sha384Copy()
  10105. */
  10106. static int test_wc_Sha384Copy (void)
  10107. {
  10108. int flag = 0;
  10109. #ifdef WOLFSSL_SHA384
  10110. wc_Sha384 sha384;
  10111. wc_Sha384 temp;
  10112. printf(testingFmt, "wc_Sha384Copy()");
  10113. /* Initialize */
  10114. flag = wc_InitSha384(&sha384);
  10115. if (flag == 0) {
  10116. flag = wc_InitSha384(&temp);
  10117. }
  10118. if (flag == 0) {
  10119. flag = wc_Sha384Copy(&sha384, &temp);
  10120. }
  10121. /*test bad arguments*/
  10122. if (flag == 0) {
  10123. flag = wc_Sha384Copy(NULL, NULL);
  10124. if (flag == BAD_FUNC_ARG) {
  10125. flag = 0;
  10126. }
  10127. }
  10128. if (flag == 0) {
  10129. flag = wc_Sha384Copy(NULL, &temp);
  10130. if (flag == BAD_FUNC_ARG) {
  10131. flag = 0;
  10132. }
  10133. }
  10134. if (flag == 0) {
  10135. flag = wc_Sha384Copy(&sha384, NULL);
  10136. if (flag == BAD_FUNC_ARG) {
  10137. flag = 0;
  10138. }
  10139. }
  10140. wc_Sha384Free(&sha384);
  10141. wc_Sha384Free(&temp);
  10142. printf(resultFmt, flag == 0 ? passed : failed);
  10143. #endif
  10144. return flag;
  10145. } /* END test_wc_Sha384Copy */
  10146. /*
  10147. * Testing wc_InitSha224();
  10148. */
  10149. static int test_wc_InitSha224 (void)
  10150. {
  10151. int flag = 0;
  10152. #ifdef WOLFSSL_SHA224
  10153. wc_Sha224 sha224;
  10154. int ret;
  10155. printf(testingFmt, "wc_InitSha224()");
  10156. /* Test good arg. */
  10157. ret = wc_InitSha224(&sha224);
  10158. if (ret != 0) {
  10159. flag = WOLFSSL_FATAL_ERROR;
  10160. }
  10161. /* Test bad arg. */
  10162. if (!flag) {
  10163. ret = wc_InitSha224(NULL);
  10164. if (ret != BAD_FUNC_ARG) {
  10165. flag = WOLFSSL_FATAL_ERROR;
  10166. }
  10167. }
  10168. wc_Sha224Free(&sha224);
  10169. printf(resultFmt, flag == 0 ? passed : failed);
  10170. #endif
  10171. return flag;
  10172. } /* END test_wc_InitSha224 */
  10173. /*
  10174. * Unit test on wc_Sha224Update
  10175. */
  10176. static int test_wc_Sha224Update (void)
  10177. {
  10178. int flag = 0;
  10179. #ifdef WOLFSSL_SHA224
  10180. wc_Sha224 sha224;
  10181. byte hash[WC_SHA224_DIGEST_SIZE];
  10182. testVector a, b, c;
  10183. int ret;
  10184. ret = wc_InitSha224(&sha224);
  10185. if (ret != 0) {
  10186. flag = ret;
  10187. }
  10188. printf(testingFmt, "wc_Sha224Update()");
  10189. /* Input. */
  10190. if (!flag) {
  10191. a.input = "a";
  10192. a.inLen = XSTRLEN(a.input);
  10193. ret = wc_Sha224Update(&sha224, NULL, 0);
  10194. if (ret != 0) {
  10195. flag = ret;
  10196. }
  10197. ret = wc_Sha224Update(&sha224, (byte*)a.input, 0);
  10198. if (ret != 0) {
  10199. flag = ret;
  10200. }
  10201. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  10202. if (ret != 0) {
  10203. flag = ret;
  10204. }
  10205. }
  10206. if (!flag) {
  10207. ret = wc_Sha224Final(&sha224, hash);
  10208. if (ret != 0) {
  10209. flag = ret;
  10210. }
  10211. }
  10212. /* Update input. */
  10213. if (!flag) {
  10214. a.input = "abc";
  10215. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  10216. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  10217. a.inLen = XSTRLEN(a.input);
  10218. a.outLen = XSTRLEN(a.output);
  10219. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  10220. if (ret != 0) {
  10221. flag = ret;
  10222. }
  10223. }
  10224. if (!flag) {
  10225. ret = wc_Sha224Final(&sha224, hash);
  10226. if (ret != 0) {
  10227. flag = ret;
  10228. }
  10229. }
  10230. if (!flag) {
  10231. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  10232. flag = WOLFSSL_FATAL_ERROR;
  10233. }
  10234. }
  10235. /* Pass in bad values. */
  10236. if (!flag) {
  10237. b.input = NULL;
  10238. b.inLen = 0;
  10239. ret = wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen);
  10240. if (ret != 0) {
  10241. flag = ret;
  10242. }
  10243. }
  10244. if (!flag) {
  10245. c.input = NULL;
  10246. c.inLen = WC_SHA224_DIGEST_SIZE;
  10247. ret = wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen);
  10248. if (ret != BAD_FUNC_ARG) {
  10249. flag = WOLFSSL_FATAL_ERROR;
  10250. }
  10251. }
  10252. if (!flag) {
  10253. ret = wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen);
  10254. if (ret != BAD_FUNC_ARG) {
  10255. flag = WOLFSSL_FATAL_ERROR;
  10256. }
  10257. }
  10258. wc_Sha224Free(&sha224);
  10259. /* If not returned then the unit test passed test vectors. */
  10260. printf(resultFmt, flag == 0 ? passed : failed);
  10261. #endif
  10262. return flag;
  10263. } /* END test_wc_Sha224Update */
  10264. /*
  10265. * Unit test for wc_Sha224Final();
  10266. */
  10267. static int test_wc_Sha224Final (void)
  10268. {
  10269. int flag = 0;
  10270. #ifdef WOLFSSL_SHA224
  10271. wc_Sha224 sha224;
  10272. byte* hash_test[3];
  10273. byte hash1[WC_SHA224_DIGEST_SIZE];
  10274. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  10275. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  10276. int times, i, ret;
  10277. /* Initialize */
  10278. ret = wc_InitSha224(&sha224);
  10279. if (ret) {
  10280. flag = ret;
  10281. }
  10282. if (!flag) {
  10283. hash_test[0] = hash1;
  10284. hash_test[1] = hash2;
  10285. hash_test[2] = hash3;
  10286. }
  10287. times = sizeof(hash_test) / sizeof(byte*);
  10288. /* Good test args. */
  10289. printf(testingFmt, "wc_sha224Final()");
  10290. /* Testing oversized buffers. */
  10291. for (i = 0; i < times; i++) {
  10292. if (!flag) {
  10293. ret = wc_Sha224Final(&sha224, hash_test[i]);
  10294. if (ret != 0) {
  10295. flag = WOLFSSL_FATAL_ERROR;
  10296. }
  10297. }
  10298. }
  10299. /* Test bad args. */
  10300. if (!flag) {
  10301. ret = wc_Sha224Final(NULL, NULL);
  10302. if (ret != BAD_FUNC_ARG) {
  10303. flag = WOLFSSL_FATAL_ERROR;
  10304. }
  10305. }
  10306. if (!flag) {
  10307. ret = wc_Sha224Final(NULL, hash1);
  10308. if (ret != BAD_FUNC_ARG) {
  10309. flag = WOLFSSL_FATAL_ERROR;
  10310. }
  10311. }
  10312. if (!flag) {
  10313. ret = wc_Sha224Final(&sha224, NULL);
  10314. if (ret != BAD_FUNC_ARG) {
  10315. flag = WOLFSSL_FATAL_ERROR;
  10316. }
  10317. }
  10318. wc_Sha224Free(&sha224);
  10319. printf(resultFmt, flag == 0 ? passed : failed);
  10320. #endif
  10321. return flag;
  10322. } /* END test_wc_Sha224Final */
  10323. /*
  10324. * Unit test function for wc_Sha224SetFlags()
  10325. */
  10326. static int test_wc_Sha224SetFlags (void)
  10327. {
  10328. int flag = 0;
  10329. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  10330. wc_Sha224 sha224;
  10331. word32 flags = 0;
  10332. printf(testingFmt, "wc_Sha224SetFlags()");
  10333. /* Initialize */
  10334. flag = wc_InitSha224(&sha224);
  10335. if (flag == 0) {
  10336. flag = wc_Sha224SetFlags(&sha224, flags);
  10337. }
  10338. if (flag == 0) {
  10339. if (flags & WC_HASH_FLAG_ISCOPY) {
  10340. flag = 0;
  10341. }
  10342. }
  10343. wc_Sha224Free(&sha224);
  10344. printf(resultFmt, flag == 0 ? passed : failed);
  10345. #endif
  10346. return flag;
  10347. } /* END test_wc_Sha224SetFlags */
  10348. /*
  10349. * Unit test function for wc_Sha224GetFlags()
  10350. */
  10351. static int test_wc_Sha224GetFlags (void)
  10352. {
  10353. int flag = 0;
  10354. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  10355. wc_Sha224 sha224;
  10356. word32 flags = 0;
  10357. printf(testingFmt, "wc_Sha224GetFlags()");
  10358. /* Initialize */
  10359. flag = wc_InitSha224(&sha224);
  10360. if (flag == 0) {
  10361. flag = wc_Sha224GetFlags(&sha224, &flags);
  10362. }
  10363. if (flag == 0) {
  10364. if (flags & WC_HASH_FLAG_ISCOPY) {
  10365. flag = 0;
  10366. }
  10367. }
  10368. wc_Sha224Free(&sha224);
  10369. printf(resultFmt, flag == 0 ? passed : failed);
  10370. #endif
  10371. return flag;
  10372. } /* END test_wc_Sha224GetFlags */
  10373. /*
  10374. * Unit test function for wc_Sha224Free()
  10375. */
  10376. static int test_wc_Sha224Free (void)
  10377. {
  10378. int flag = 0;
  10379. #ifdef WOLFSSL_SHA224
  10380. printf(testingFmt, "wc_Sha224Free()");
  10381. wc_Sha224Free(NULL);
  10382. printf(resultFmt, flag == 0 ? passed : failed);
  10383. #endif
  10384. return flag;
  10385. } /* END test_wc_Sha224Free */
  10386. /*
  10387. * Unit test function for wc_Sha224GetHash()
  10388. */
  10389. static int test_wc_Sha224GetHash (void)
  10390. {
  10391. int flag = 0;
  10392. #ifdef WOLFSSL_SHA224
  10393. wc_Sha224 sha224;
  10394. byte hash1[WC_SHA224_DIGEST_SIZE];
  10395. printf(testingFmt, "wc_Sha224GetHash()");
  10396. /* Initialize */
  10397. flag = wc_InitSha224(&sha224);
  10398. if (flag == 0) {
  10399. flag = wc_Sha224GetHash(&sha224, hash1);
  10400. }
  10401. /*test bad arguments*/
  10402. if (flag == 0) {
  10403. flag = wc_Sha224GetHash(NULL, NULL);
  10404. if (flag == BAD_FUNC_ARG) {
  10405. flag = 0;
  10406. }
  10407. }
  10408. if (flag == 0) {
  10409. flag = wc_Sha224GetHash(NULL, hash1);
  10410. if (flag == BAD_FUNC_ARG) {
  10411. flag = 0;
  10412. }
  10413. }
  10414. if (flag == 0) {
  10415. flag = wc_Sha224GetHash(&sha224, NULL);
  10416. if (flag == BAD_FUNC_ARG) {
  10417. flag = 0;
  10418. }
  10419. }
  10420. wc_Sha224Free(&sha224);
  10421. printf(resultFmt, flag == 0 ? passed : failed);
  10422. #endif
  10423. return flag;
  10424. } /* END test_wc_Sha224GetHash */
  10425. /*
  10426. * Unit test function for wc_Sha224Copy()
  10427. */
  10428. static int test_wc_Sha224Copy (void)
  10429. {
  10430. int flag = 0;
  10431. #ifdef WOLFSSL_SHA224
  10432. wc_Sha224 sha224;
  10433. wc_Sha224 temp;
  10434. printf(testingFmt, "wc_Sha224Copy()");
  10435. /* Initialize */
  10436. flag = wc_InitSha224(&sha224);
  10437. if (flag == 0) {
  10438. flag = wc_InitSha224(&temp);
  10439. }
  10440. if (flag == 0) {
  10441. flag = wc_Sha224Copy(&sha224, &temp);
  10442. }
  10443. /*test bad arguments*/
  10444. if (flag == 0) {
  10445. flag = wc_Sha224Copy(NULL, NULL);
  10446. if (flag == BAD_FUNC_ARG) {
  10447. flag = 0;
  10448. }
  10449. }
  10450. if (flag == 0) {
  10451. flag = wc_Sha224Copy(NULL, &temp);
  10452. if (flag == BAD_FUNC_ARG) {
  10453. flag = 0;
  10454. }
  10455. }
  10456. if (flag == 0) {
  10457. flag = wc_Sha224Copy(&sha224, NULL);
  10458. if (flag == BAD_FUNC_ARG) {
  10459. flag = 0;
  10460. }
  10461. }
  10462. wc_Sha224Free(&sha224);
  10463. wc_Sha224Free(&temp);
  10464. printf(resultFmt, flag == 0 ? passed : failed);
  10465. #endif
  10466. return flag;
  10467. } /* END test_wc_Sha224Copy */
  10468. /*
  10469. * Testing wc_InitRipeMd()
  10470. */
  10471. static int test_wc_InitRipeMd (void)
  10472. {
  10473. int flag = 0;
  10474. #ifdef WOLFSSL_RIPEMD
  10475. RipeMd ripemd;
  10476. int ret;
  10477. printf(testingFmt, "wc_InitRipeMd()");
  10478. /* Test good arg. */
  10479. ret = wc_InitRipeMd(&ripemd);
  10480. if (ret != 0) {
  10481. flag = WOLFSSL_FATAL_ERROR;
  10482. }
  10483. /* Test bad arg. */
  10484. if (!flag) {
  10485. ret = wc_InitRipeMd(NULL);
  10486. if (ret != BAD_FUNC_ARG) {
  10487. flag = WOLFSSL_FATAL_ERROR;
  10488. }
  10489. }
  10490. printf(resultFmt, flag == 0 ? passed : failed);
  10491. #endif
  10492. return flag;
  10493. } /* END test_wc_InitRipeMd */
  10494. /*
  10495. * Testing wc_RipeMdUpdate()
  10496. */
  10497. static int test_wc_RipeMdUpdate (void)
  10498. {
  10499. int flag = 0;
  10500. #ifdef WOLFSSL_RIPEMD
  10501. RipeMd ripemd;
  10502. byte hash[RIPEMD_DIGEST_SIZE];
  10503. testVector a, b, c;
  10504. int ret;
  10505. ret = wc_InitRipeMd(&ripemd);
  10506. if (ret != 0) {
  10507. flag = ret;
  10508. }
  10509. printf(testingFmt, "wc_RipeMdUpdate()");
  10510. /* Input */
  10511. if (!flag) {
  10512. a.input = "a";
  10513. a.inLen = XSTRLEN(a.input);
  10514. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  10515. if (ret != 0) {
  10516. flag = ret;
  10517. }
  10518. }
  10519. if (!flag) {
  10520. ret = wc_RipeMdFinal(&ripemd, hash);
  10521. if (ret != 0) {
  10522. flag = ret;
  10523. }
  10524. }
  10525. /* Update input. */
  10526. if (!flag) {
  10527. a.input = "abc";
  10528. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  10529. "\xb0\x87\xf1\x5a\x0b\xfc";
  10530. a.inLen = XSTRLEN(a.input);
  10531. a.outLen = XSTRLEN(a.output);
  10532. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  10533. if (ret != 0) {
  10534. flag = ret;
  10535. }
  10536. }
  10537. if (!flag) {
  10538. ret = wc_RipeMdFinal(&ripemd, hash);
  10539. if (ret != 0) {
  10540. flag = ret;
  10541. }
  10542. }
  10543. if (!flag) {
  10544. if (XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE) != 0) {
  10545. flag = WOLFSSL_FATAL_ERROR;
  10546. }
  10547. }
  10548. /* Pass in bad values. */
  10549. if (!flag) {
  10550. b.input = NULL;
  10551. b.inLen = 0;
  10552. ret = wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen);
  10553. if (ret != 0) {
  10554. flag = ret;
  10555. }
  10556. }
  10557. if (!flag) {
  10558. c.input = NULL;
  10559. c.inLen = RIPEMD_DIGEST_SIZE;
  10560. ret = wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen);
  10561. if (ret != BAD_FUNC_ARG) {
  10562. flag = WOLFSSL_FATAL_ERROR;
  10563. }
  10564. }
  10565. if (!flag) {
  10566. ret = wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  10567. if (ret != BAD_FUNC_ARG) {
  10568. flag = WOLFSSL_FATAL_ERROR;
  10569. }
  10570. }
  10571. printf(resultFmt, flag == 0 ? passed : failed);
  10572. #endif
  10573. return flag;
  10574. } /* END test_wc_RipeMdUdpate */
  10575. /*
  10576. * Unit test function for wc_RipeMdFinal()
  10577. */
  10578. static int test_wc_RipeMdFinal (void)
  10579. {
  10580. int flag = 0;
  10581. #ifdef WOLFSSL_RIPEMD
  10582. RipeMd ripemd;
  10583. byte* hash_test[3];
  10584. byte hash1[RIPEMD_DIGEST_SIZE];
  10585. byte hash2[2*RIPEMD_DIGEST_SIZE];
  10586. byte hash3[5*RIPEMD_DIGEST_SIZE];
  10587. int times, i, ret;
  10588. /* Initialize */
  10589. ret = wc_InitRipeMd(&ripemd);
  10590. if (ret != 0) {
  10591. flag = ret;
  10592. }
  10593. if (!flag) {
  10594. hash_test[0] = hash1;
  10595. hash_test[1] = hash2;
  10596. hash_test[2] = hash3;
  10597. }
  10598. times = sizeof(hash_test) / sizeof(byte*);
  10599. /* Good test args. */
  10600. printf(testingFmt, "wc_RipeMdFinal()");
  10601. /* Testing oversized buffers. */
  10602. for (i = 0; i < times; i++) {
  10603. if (!flag) {
  10604. ret = wc_RipeMdFinal(&ripemd, hash_test[i]);
  10605. if (ret != 0) {
  10606. flag = WOLFSSL_FATAL_ERROR;
  10607. }
  10608. }
  10609. }
  10610. /* Test bad args. */
  10611. if (!flag) {
  10612. ret = wc_RipeMdFinal(NULL, NULL);
  10613. if (ret != BAD_FUNC_ARG) {
  10614. flag = WOLFSSL_FATAL_ERROR;
  10615. }
  10616. }
  10617. if (!flag) {
  10618. ret = wc_RipeMdFinal(NULL, hash1);
  10619. if (ret != BAD_FUNC_ARG) {
  10620. flag = WOLFSSL_FATAL_ERROR;
  10621. }
  10622. }
  10623. if (!flag) {
  10624. ret = wc_RipeMdFinal(&ripemd, NULL);
  10625. if (ret != BAD_FUNC_ARG) {
  10626. flag = WOLFSSL_FATAL_ERROR;
  10627. }
  10628. }
  10629. printf(resultFmt, flag == 0 ? passed : failed);
  10630. #endif
  10631. return flag;
  10632. } /* END test_wc_RipeMdFinal */
  10633. /*
  10634. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  10635. * wc_InitSha3_512
  10636. */
  10637. static int test_wc_InitSha3 (void)
  10638. {
  10639. int ret = 0;
  10640. #if defined(WOLFSSL_SHA3)
  10641. wc_Sha3 sha3;
  10642. (void)sha3;
  10643. #if !defined(WOLFSSL_NOSHA3_224)
  10644. printf(testingFmt, "wc_InitSha3_224()");
  10645. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  10646. /* Test bad args. */
  10647. if (ret == 0) {
  10648. ret = wc_InitSha3_224(NULL, HEAP_HINT, devId);
  10649. if (ret == BAD_FUNC_ARG) {
  10650. ret = 0;
  10651. } else if (ret == 0) {
  10652. ret = WOLFSSL_FATAL_ERROR;
  10653. }
  10654. }
  10655. wc_Sha3_224_Free(&sha3);
  10656. printf(resultFmt, ret == 0 ? passed : failed);
  10657. #endif /* NOSHA3_224 */
  10658. #if !defined(WOLFSSL_NOSHA3_256)
  10659. if (ret == 0) {
  10660. printf(testingFmt, "wc_InitSha3_256()");
  10661. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  10662. /* Test bad args. */
  10663. if (ret == 0) {
  10664. ret = wc_InitSha3_256(NULL, HEAP_HINT, devId);
  10665. if (ret == BAD_FUNC_ARG) {
  10666. ret = 0;
  10667. } else if (ret == 0) {
  10668. ret = WOLFSSL_FATAL_ERROR;
  10669. }
  10670. }
  10671. wc_Sha3_256_Free(&sha3);
  10672. printf(resultFmt, ret == 0 ? passed : failed);
  10673. } /* END sha3_256 */
  10674. #endif /* NOSHA3_256 */
  10675. #if !defined(WOLFSSL_NOSHA3_384)
  10676. if (ret == 0) {
  10677. printf(testingFmt, "wc_InitSha3_384()");
  10678. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  10679. /* Test bad args. */
  10680. if (ret == 0) {
  10681. ret = wc_InitSha3_384(NULL, HEAP_HINT, devId);
  10682. if (ret == BAD_FUNC_ARG) {
  10683. ret = 0;
  10684. } else if (ret == 0) {
  10685. ret = WOLFSSL_FATAL_ERROR;
  10686. }
  10687. }
  10688. wc_Sha3_384_Free(&sha3);
  10689. printf(resultFmt, ret == 0 ? passed : failed);
  10690. } /* END sha3_384 */
  10691. #endif /* NOSHA3_384 */
  10692. #if !defined(WOLFSSL_NOSHA3_512)
  10693. if (ret == 0) {
  10694. printf(testingFmt, "wc_InitSha3_512()");
  10695. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  10696. /* Test bad args. */
  10697. if (ret == 0) {
  10698. ret = wc_InitSha3_512(NULL, HEAP_HINT, devId);
  10699. if (ret == BAD_FUNC_ARG) {
  10700. ret = 0;
  10701. } else if (ret == 0) {
  10702. ret = WOLFSSL_FATAL_ERROR;
  10703. }
  10704. }
  10705. wc_Sha3_512_Free(&sha3);
  10706. printf(resultFmt, ret == 0 ? passed : failed);
  10707. } /* END sha3_512 */
  10708. #endif /* NOSHA3_512 */
  10709. #endif
  10710. return ret;
  10711. } /* END test_wc_InitSha3 */
  10712. /*
  10713. * Testing wc_Sha3_Update()
  10714. */
  10715. static int testing_wc_Sha3_Update (void)
  10716. {
  10717. int ret = 0;
  10718. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  10719. !defined(WOLFSSL_AFALG_XILINX)
  10720. wc_Sha3 sha3;
  10721. byte msg[] = "Everybody's working for the weekend.";
  10722. byte msg2[] = "Everybody gets Friday off.";
  10723. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  10724. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  10725. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  10726. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  10727. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  10728. word32 msglen = sizeof(msg) - 1;
  10729. word32 msg2len = sizeof(msg2);
  10730. word32 msgCmplen = sizeof(msgCmp);
  10731. #if !defined(WOLFSSL_NOSHA3_224)
  10732. printf(testingFmt, "wc_Sha3_224_Update()");
  10733. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  10734. if (ret != 0) {
  10735. return ret;
  10736. }
  10737. ret = wc_Sha3_224_Update(&sha3, msg, msglen);
  10738. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  10739. ret = WOLFSSL_FATAL_ERROR;
  10740. }
  10741. if (ret == 0) {
  10742. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  10743. if (ret == 0 && XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  10744. ret = WOLFSSL_FATAL_ERROR;
  10745. }
  10746. }
  10747. /* Pass bad args. */
  10748. if (ret == 0) {
  10749. ret = wc_Sha3_224_Update(NULL, msg2, msg2len);
  10750. if (ret == BAD_FUNC_ARG) {
  10751. ret = wc_Sha3_224_Update(&sha3, NULL, 5);
  10752. }
  10753. if (ret == BAD_FUNC_ARG) {
  10754. wc_Sha3_224_Free(&sha3);
  10755. if (wc_InitSha3_224(&sha3, HEAP_HINT, devId)) {
  10756. return ret;
  10757. }
  10758. ret = wc_Sha3_224_Update(&sha3, NULL, 0);
  10759. if (ret == 0) {
  10760. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  10761. }
  10762. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  10763. ret = WOLFSSL_FATAL_ERROR;
  10764. }
  10765. }
  10766. }
  10767. wc_Sha3_224_Free(&sha3);
  10768. printf(resultFmt, ret == 0 ? passed : failed);
  10769. #endif /* SHA3_224 */
  10770. #if !defined(WOLFSSL_NOSHA3_256)
  10771. if (ret == 0) {
  10772. printf(testingFmt, "wc_Sha3_256_Update()");
  10773. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  10774. if (ret != 0) {
  10775. return ret;
  10776. }
  10777. ret = wc_Sha3_256_Update(&sha3, msg, msglen);
  10778. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  10779. ret = WOLFSSL_FATAL_ERROR;
  10780. }
  10781. if (ret == 0) {
  10782. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  10783. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  10784. ret = WOLFSSL_FATAL_ERROR;
  10785. }
  10786. }
  10787. /* Pass bad args. */
  10788. if (ret == 0) {
  10789. ret = wc_Sha3_256_Update(NULL, msg2, msg2len);
  10790. if (ret == BAD_FUNC_ARG) {
  10791. ret = wc_Sha3_256_Update(&sha3, NULL, 5);
  10792. }
  10793. if (ret == BAD_FUNC_ARG) {
  10794. wc_Sha3_256_Free(&sha3);
  10795. if (wc_InitSha3_256(&sha3, HEAP_HINT, devId)) {
  10796. return ret;
  10797. }
  10798. ret = wc_Sha3_256_Update(&sha3, NULL, 0);
  10799. if (ret == 0) {
  10800. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  10801. }
  10802. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  10803. ret = WOLFSSL_FATAL_ERROR;
  10804. }
  10805. }
  10806. }
  10807. wc_Sha3_256_Free(&sha3);
  10808. printf(resultFmt, ret == 0 ? passed : failed);
  10809. }
  10810. #endif /* SHA3_256 */
  10811. #if !defined(WOLFSSL_NOSHA3_384)
  10812. if (ret == 0) {
  10813. printf(testingFmt, "wc_Sha3_384_Update()");
  10814. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  10815. if (ret != 0) {
  10816. return ret;
  10817. }
  10818. ret = wc_Sha3_384_Update(&sha3, msg, msglen);
  10819. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  10820. ret = WOLFSSL_FATAL_ERROR;
  10821. }
  10822. if (ret == 0) {
  10823. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  10824. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  10825. ret = WOLFSSL_FATAL_ERROR;
  10826. }
  10827. }
  10828. /* Pass bad args. */
  10829. if (ret == 0) {
  10830. ret = wc_Sha3_384_Update(NULL, msg2, msg2len);
  10831. if (ret == BAD_FUNC_ARG) {
  10832. ret = wc_Sha3_384_Update(&sha3, NULL, 5);
  10833. }
  10834. if (ret == BAD_FUNC_ARG) {
  10835. wc_Sha3_384_Free(&sha3);
  10836. if (wc_InitSha3_384(&sha3, HEAP_HINT, devId)) {
  10837. return ret;
  10838. }
  10839. ret = wc_Sha3_384_Update(&sha3, NULL, 0);
  10840. if (ret == 0) {
  10841. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  10842. }
  10843. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  10844. ret = WOLFSSL_FATAL_ERROR;
  10845. }
  10846. }
  10847. }
  10848. wc_Sha3_384_Free(&sha3);
  10849. printf(resultFmt, ret == 0 ? passed : failed);
  10850. }
  10851. #endif /* SHA3_384 */
  10852. #if !defined(WOLFSSL_NOSHA3_512)
  10853. if (ret == 0) {
  10854. printf(testingFmt, "wc_Sha3_512_Update()");
  10855. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  10856. if (ret != 0) {
  10857. return ret;
  10858. }
  10859. ret = wc_Sha3_512_Update(&sha3, msg, msglen);
  10860. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  10861. ret = WOLFSSL_FATAL_ERROR;
  10862. }
  10863. if (ret == 0) {
  10864. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  10865. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  10866. ret = WOLFSSL_FATAL_ERROR;
  10867. }
  10868. }
  10869. /* Pass bad args. */
  10870. if (ret == 0) {
  10871. ret = wc_Sha3_512_Update(NULL, msg2, msg2len);
  10872. if (ret == BAD_FUNC_ARG) {
  10873. ret = wc_Sha3_512_Update(&sha3, NULL, 5);
  10874. }
  10875. if (ret == BAD_FUNC_ARG) {
  10876. wc_Sha3_512_Free(&sha3);
  10877. if (wc_InitSha3_512(&sha3, HEAP_HINT, devId)) {
  10878. return ret;
  10879. }
  10880. ret = wc_Sha3_512_Update(&sha3, NULL, 0);
  10881. if (ret == 0) {
  10882. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  10883. }
  10884. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  10885. ret = WOLFSSL_FATAL_ERROR;
  10886. }
  10887. }
  10888. }
  10889. wc_Sha3_512_Free(&sha3);
  10890. printf(resultFmt, ret == 0 ? passed : failed);
  10891. }
  10892. #endif /* SHA3_512 */
  10893. #endif /* WOLFSSL_SHA3 */
  10894. return ret;
  10895. } /* END testing_wc_Sha3_Update */
  10896. /*
  10897. * Testing wc_Sha3_224_Final()
  10898. */
  10899. static int test_wc_Sha3_224_Final (void)
  10900. {
  10901. int ret = 0;
  10902. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  10903. wc_Sha3 sha3;
  10904. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  10905. "nopnopq";
  10906. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  10907. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  10908. "\x64\xea\xd0\xfc\xce\x33";
  10909. byte hash[WC_SHA3_224_DIGEST_SIZE];
  10910. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  10911. /* Init stack variables. */
  10912. XMEMSET(hash, 0, sizeof(hash));
  10913. printf(testingFmt, "wc_Sha3_224_Final()");
  10914. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  10915. if (ret != 0) {
  10916. return ret;
  10917. }
  10918. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  10919. if (ret == 0) {
  10920. ret = wc_Sha3_224_Final(&sha3, hash);
  10921. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE) != 0) {
  10922. ret = WOLFSSL_FATAL_ERROR;
  10923. }
  10924. }
  10925. /* Test bad args. */
  10926. if (ret == 0) {
  10927. ret = wc_Sha3_224_Final(NULL, hash);
  10928. if (ret == 0) {
  10929. ret = wc_Sha3_224_Final(&sha3, NULL);
  10930. }
  10931. if (ret == BAD_FUNC_ARG) {
  10932. ret = 0;
  10933. } else if (ret == 0) {
  10934. ret = WOLFSSL_FATAL_ERROR;
  10935. }
  10936. }
  10937. wc_Sha3_224_Free(&sha3);
  10938. printf(resultFmt, ret == 0 ? passed : failed);
  10939. if (ret == 0) {
  10940. printf(testingFmt, "wc_Sha3_224_GetHash()");
  10941. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  10942. if (ret != 0) {
  10943. return ret;
  10944. }
  10945. /* Init stack variables. */
  10946. XMEMSET(hash, 0, sizeof(hash));
  10947. XMEMSET(hashRet, 0, sizeof(hashRet));
  10948. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  10949. if (ret == 0) {
  10950. ret = wc_Sha3_224_GetHash(&sha3, hashRet);
  10951. }
  10952. if (ret == 0) {
  10953. ret = wc_Sha3_224_Final(&sha3, hash);
  10954. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE) != 0) {
  10955. ret = WOLFSSL_FATAL_ERROR;
  10956. }
  10957. }
  10958. if (ret == 0) {
  10959. /* Test bad args. */
  10960. ret = wc_Sha3_224_GetHash(NULL, hashRet);
  10961. if (ret == BAD_FUNC_ARG) {
  10962. ret = wc_Sha3_224_GetHash(&sha3, NULL);
  10963. }
  10964. if (ret == BAD_FUNC_ARG) {
  10965. ret = 0;
  10966. } else if (ret == 0) {
  10967. ret = WOLFSSL_FATAL_ERROR;
  10968. }
  10969. }
  10970. printf(resultFmt, ret == 0 ? passed : failed);
  10971. }
  10972. wc_Sha3_224_Free(&sha3);
  10973. #endif
  10974. return ret;
  10975. } /* END test_wc_Sha3_224_Final */
  10976. /*
  10977. * Testing wc_Sha3_256_Final()
  10978. */
  10979. static int test_wc_Sha3_256_Final (void)
  10980. {
  10981. int ret = 0;
  10982. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  10983. wc_Sha3 sha3;
  10984. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  10985. "nopnopq";
  10986. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  10987. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  10988. "\xdd\x97\x49\x6d\x33\x76";
  10989. byte hash[WC_SHA3_256_DIGEST_SIZE];
  10990. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  10991. /* Init stack variables. */
  10992. XMEMSET(hash, 0, sizeof(hash));
  10993. printf(testingFmt, "wc_Sha3_256_Final()");
  10994. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  10995. if (ret != 0) {
  10996. return ret;
  10997. }
  10998. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  10999. if (ret == 0) {
  11000. ret = wc_Sha3_256_Final(&sha3, hash);
  11001. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE) != 0) {
  11002. ret = WOLFSSL_FATAL_ERROR;
  11003. }
  11004. }
  11005. /* Test bad args. */
  11006. if (ret == 0) {
  11007. ret = wc_Sha3_256_Final(NULL, hash);
  11008. if (ret == 0) {
  11009. ret = wc_Sha3_256_Final(&sha3, NULL);
  11010. }
  11011. if (ret == BAD_FUNC_ARG) {
  11012. ret = 0;
  11013. } else if (ret == 0) {
  11014. ret = WOLFSSL_FATAL_ERROR;
  11015. }
  11016. }
  11017. wc_Sha3_256_Free(&sha3);
  11018. printf(resultFmt, ret == 0 ? passed : failed);
  11019. if (ret == 0) {
  11020. printf(testingFmt, "wc_Sha3_256_GetHash()");
  11021. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  11022. if (ret != 0) {
  11023. return ret;
  11024. }
  11025. /* Init stack variables. */
  11026. XMEMSET(hash, 0, sizeof(hash));
  11027. XMEMSET(hashRet, 0, sizeof(hashRet));
  11028. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11029. if (ret == 0) {
  11030. ret = wc_Sha3_256_GetHash(&sha3, hashRet);
  11031. }
  11032. if (ret == 0) {
  11033. ret = wc_Sha3_256_Final(&sha3, hash);
  11034. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE) != 0) {
  11035. ret = WOLFSSL_FATAL_ERROR;
  11036. }
  11037. }
  11038. if (ret == 0) {
  11039. /* Test bad args. */
  11040. ret = wc_Sha3_256_GetHash(NULL, hashRet);
  11041. if (ret == BAD_FUNC_ARG) {
  11042. ret = wc_Sha3_256_GetHash(&sha3, NULL);
  11043. }
  11044. if (ret == BAD_FUNC_ARG) {
  11045. ret = 0;
  11046. } else if (ret == 0) {
  11047. ret = WOLFSSL_FATAL_ERROR;
  11048. }
  11049. }
  11050. printf(resultFmt, ret == 0 ? passed : failed);
  11051. }
  11052. wc_Sha3_256_Free(&sha3);
  11053. #endif
  11054. return ret;
  11055. } /* END test_wc_Sha3_256_Final */
  11056. /*
  11057. * Testing wc_Sha3_384_Final()
  11058. */
  11059. static int test_wc_Sha3_384_Final (void)
  11060. {
  11061. int ret = 0;
  11062. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  11063. wc_Sha3 sha3;
  11064. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  11065. "nopnopq";
  11066. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  11067. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  11068. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  11069. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  11070. byte hash[WC_SHA3_384_DIGEST_SIZE];
  11071. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  11072. /* Init stack variables. */
  11073. XMEMSET(hash, 0, sizeof(hash));
  11074. printf(testingFmt, "wc_Sha3_384_Final()");
  11075. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  11076. if (ret != 0) {
  11077. return ret;
  11078. }
  11079. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11080. if (ret == 0) {
  11081. ret = wc_Sha3_384_Final(&sha3, hash);
  11082. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE) != 0) {
  11083. ret = WOLFSSL_FATAL_ERROR;
  11084. }
  11085. }
  11086. /* Test bad args. */
  11087. if (ret == 0) {
  11088. ret = wc_Sha3_384_Final(NULL, hash);
  11089. if (ret == 0) {
  11090. ret = wc_Sha3_384_Final(&sha3, NULL);
  11091. }
  11092. if (ret == BAD_FUNC_ARG) {
  11093. ret = 0;
  11094. } else if (ret == 0) {
  11095. ret = WOLFSSL_FATAL_ERROR;
  11096. }
  11097. }
  11098. wc_Sha3_384_Free(&sha3);
  11099. printf(resultFmt, ret == 0 ? passed : failed);
  11100. if (ret == 0) {
  11101. printf(testingFmt, "wc_Sha3_384_GetHash()");
  11102. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  11103. if (ret != 0) {
  11104. return ret;
  11105. }
  11106. /* Init stack variables. */
  11107. XMEMSET(hash, 0, sizeof(hash));
  11108. XMEMSET(hashRet, 0, sizeof(hashRet));
  11109. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11110. if (ret == 0) {
  11111. ret = wc_Sha3_384_GetHash(&sha3, hashRet);
  11112. }
  11113. if (ret == 0) {
  11114. ret = wc_Sha3_384_Final(&sha3, hash);
  11115. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE) != 0) {
  11116. ret = WOLFSSL_FATAL_ERROR;
  11117. }
  11118. }
  11119. if (ret == 0) {
  11120. /* Test bad args. */
  11121. ret = wc_Sha3_384_GetHash(NULL, hashRet);
  11122. if (ret == BAD_FUNC_ARG) {
  11123. ret = wc_Sha3_384_GetHash(&sha3, NULL);
  11124. }
  11125. if (ret == BAD_FUNC_ARG) {
  11126. ret = 0;
  11127. } else if (ret == 0) {
  11128. ret = WOLFSSL_FATAL_ERROR;
  11129. }
  11130. }
  11131. printf(resultFmt, ret == 0 ? passed : failed);
  11132. }
  11133. wc_Sha3_384_Free(&sha3);
  11134. #endif
  11135. return ret;
  11136. } /* END test_wc_Sha3_384_Final */
  11137. /*
  11138. * Testing wc_Sha3_512_Final()
  11139. */
  11140. static int test_wc_Sha3_512_Final (void)
  11141. {
  11142. int ret = 0;
  11143. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  11144. !defined(WOLFSSL_NOSHA3_384)
  11145. wc_Sha3 sha3;
  11146. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  11147. "nopnopq";
  11148. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  11149. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  11150. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  11151. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  11152. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  11153. byte hash[WC_SHA3_512_DIGEST_SIZE];
  11154. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  11155. /* Init stack variables. */
  11156. XMEMSET(hash, 0, sizeof(hash));
  11157. printf(testingFmt, "wc_Sha3_512_Final()");
  11158. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  11159. if (ret != 0) {
  11160. return ret;
  11161. }
  11162. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11163. if (ret == 0) {
  11164. ret = wc_Sha3_512_Final(&sha3, hash);
  11165. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE) != 0) {
  11166. ret = WOLFSSL_FATAL_ERROR;
  11167. }
  11168. }
  11169. /* Test bad args. */
  11170. if (ret == 0) {
  11171. ret = wc_Sha3_512_Final(NULL, hash);
  11172. if (ret == 0) {
  11173. ret = wc_Sha3_384_Final(&sha3, NULL);
  11174. }
  11175. if (ret == BAD_FUNC_ARG) {
  11176. ret = 0;
  11177. } else if (ret == 0) {
  11178. ret = WOLFSSL_FATAL_ERROR;
  11179. }
  11180. }
  11181. wc_Sha3_512_Free(&sha3);
  11182. printf(resultFmt, ret == 0 ? passed : failed);
  11183. if (ret == 0) {
  11184. printf(testingFmt, "wc_Sha3_512_GetHash()");
  11185. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  11186. if (ret != 0) {
  11187. return ret;
  11188. }
  11189. /* Init stack variables. */
  11190. XMEMSET(hash, 0, sizeof(hash));
  11191. XMEMSET(hashRet, 0, sizeof(hashRet));
  11192. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11193. if (ret == 0) {
  11194. ret = wc_Sha3_512_GetHash(&sha3, hashRet);
  11195. }
  11196. if (ret == 0) {
  11197. ret = wc_Sha3_512_Final(&sha3, hash);
  11198. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE) != 0) {
  11199. ret = WOLFSSL_FATAL_ERROR;
  11200. }
  11201. }
  11202. if (ret == 0) {
  11203. /* Test bad args. */
  11204. ret = wc_Sha3_512_GetHash(NULL, hashRet);
  11205. if (ret == BAD_FUNC_ARG) {
  11206. ret = wc_Sha3_512_GetHash(&sha3, NULL);
  11207. }
  11208. if (ret == BAD_FUNC_ARG) {
  11209. ret = 0;
  11210. } else if (ret == 0) {
  11211. ret = WOLFSSL_FATAL_ERROR;
  11212. }
  11213. }
  11214. printf(resultFmt, ret == 0 ? passed : failed);
  11215. }
  11216. wc_Sha3_512_Free(&sha3);
  11217. #endif
  11218. return ret;
  11219. } /* END test_wc_Sha3_512_Final */
  11220. /*
  11221. * Testing wc_Sha3_224_Copy()
  11222. */
  11223. static int test_wc_Sha3_224_Copy (void)
  11224. {
  11225. int ret = 0;
  11226. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  11227. wc_Sha3 sha3, sha3Cpy;
  11228. const char* msg = TEST_STRING;
  11229. word32 msglen = (word32)TEST_STRING_SZ;
  11230. byte hash[WC_SHA3_224_DIGEST_SIZE];
  11231. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  11232. XMEMSET(hash, 0, sizeof(hash));
  11233. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  11234. printf(testingFmt, "wc_Sha3_224_Copy()");
  11235. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  11236. if (ret != 0) {
  11237. return ret;
  11238. }
  11239. ret = wc_InitSha3_224(&sha3Cpy, HEAP_HINT, devId);
  11240. if (ret != 0) {
  11241. wc_Sha3_224_Free(&sha3);
  11242. return ret;
  11243. }
  11244. ret = wc_Sha3_224_Update(&sha3, (byte*)msg, msglen);
  11245. if (ret == 0) {
  11246. ret = wc_Sha3_224_Copy(&sha3Cpy, &sha3);
  11247. if (ret == 0) {
  11248. ret = wc_Sha3_224_Final(&sha3, hash);
  11249. if (ret == 0) {
  11250. ret = wc_Sha3_224_Final(&sha3Cpy, hashCpy);
  11251. }
  11252. }
  11253. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  11254. ret = WOLFSSL_FATAL_ERROR;
  11255. }
  11256. }
  11257. /* Test bad args. */
  11258. if (ret == 0) {
  11259. ret = wc_Sha3_224_Copy(NULL, &sha3);
  11260. if (ret == BAD_FUNC_ARG) {
  11261. ret = wc_Sha3_224_Copy(&sha3Cpy, NULL);
  11262. }
  11263. if (ret == BAD_FUNC_ARG) {
  11264. ret = 0;
  11265. } else if (ret == 0) {
  11266. ret = WOLFSSL_FATAL_ERROR;
  11267. }
  11268. }
  11269. printf(resultFmt, ret == 0 ? passed : failed);
  11270. #endif
  11271. return ret;
  11272. } /* END test_wc_Sha3_224_Copy */
  11273. /*
  11274. * Testing wc_Sha3_256_Copy()
  11275. */
  11276. static int test_wc_Sha3_256_Copy (void)
  11277. {
  11278. int ret = 0;
  11279. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  11280. wc_Sha3 sha3, sha3Cpy;
  11281. const char* msg = TEST_STRING;
  11282. word32 msglen = (word32)TEST_STRING_SZ;
  11283. byte hash[WC_SHA3_256_DIGEST_SIZE];
  11284. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  11285. XMEMSET(hash, 0, sizeof(hash));
  11286. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  11287. printf(testingFmt, "wc_Sha3_256_Copy()");
  11288. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  11289. if (ret != 0) {
  11290. return ret;
  11291. }
  11292. ret = wc_InitSha3_256(&sha3Cpy, HEAP_HINT, devId);
  11293. if (ret != 0) {
  11294. wc_Sha3_256_Free(&sha3);
  11295. return ret;
  11296. }
  11297. ret = wc_Sha3_256_Update(&sha3, (byte*)msg, msglen);
  11298. if (ret == 0) {
  11299. ret = wc_Sha3_256_Copy(&sha3Cpy, &sha3);
  11300. if (ret == 0) {
  11301. ret = wc_Sha3_256_Final(&sha3, hash);
  11302. if (ret == 0) {
  11303. ret = wc_Sha3_256_Final(&sha3Cpy, hashCpy);
  11304. }
  11305. }
  11306. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  11307. ret = WOLFSSL_FATAL_ERROR;
  11308. }
  11309. }
  11310. /* Test bad args. */
  11311. if (ret == 0) {
  11312. ret = wc_Sha3_256_Copy(NULL, &sha3);
  11313. if (ret == BAD_FUNC_ARG) {
  11314. ret = wc_Sha3_256_Copy(&sha3Cpy, NULL);
  11315. }
  11316. if (ret == BAD_FUNC_ARG) {
  11317. ret = 0;
  11318. } else if (ret == 0) {
  11319. ret = WOLFSSL_FATAL_ERROR;
  11320. }
  11321. }
  11322. printf(resultFmt, ret == 0 ? passed : failed);
  11323. #endif
  11324. return ret;
  11325. } /* END test_wc_Sha3_256_Copy */
  11326. /*
  11327. * Testing wc_Sha3_384_Copy()
  11328. */
  11329. static int test_wc_Sha3_384_Copy (void)
  11330. {
  11331. int ret = 0;
  11332. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  11333. wc_Sha3 sha3, sha3Cpy;
  11334. const char* msg = TEST_STRING;
  11335. word32 msglen = (word32)TEST_STRING_SZ;
  11336. byte hash[WC_SHA3_384_DIGEST_SIZE];
  11337. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  11338. XMEMSET(hash, 0, sizeof(hash));
  11339. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  11340. printf(testingFmt, "wc_Sha3_384_Copy()");
  11341. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  11342. if (ret != 0) {
  11343. return ret;
  11344. }
  11345. ret = wc_InitSha3_384(&sha3Cpy, HEAP_HINT, devId);
  11346. if (ret != 0) {
  11347. wc_Sha3_384_Free(&sha3);
  11348. return ret;
  11349. }
  11350. ret = wc_Sha3_384_Update(&sha3, (byte*)msg, msglen);
  11351. if (ret == 0) {
  11352. ret = wc_Sha3_384_Copy(&sha3Cpy, &sha3);
  11353. if (ret == 0) {
  11354. ret = wc_Sha3_384_Final(&sha3, hash);
  11355. if (ret == 0) {
  11356. ret = wc_Sha3_384_Final(&sha3Cpy, hashCpy);
  11357. }
  11358. }
  11359. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  11360. ret = WOLFSSL_FATAL_ERROR;
  11361. }
  11362. }
  11363. /* Test bad args. */
  11364. if (ret == 0) {
  11365. ret = wc_Sha3_384_Copy(NULL, &sha3);
  11366. if (ret == BAD_FUNC_ARG) {
  11367. ret = wc_Sha3_384_Copy(&sha3Cpy, NULL);
  11368. }
  11369. if (ret == BAD_FUNC_ARG) {
  11370. ret = 0;
  11371. } else if (ret == 0) {
  11372. ret = WOLFSSL_FATAL_ERROR;
  11373. }
  11374. }
  11375. printf(resultFmt, ret == 0 ? passed : failed);
  11376. #endif
  11377. return ret;
  11378. } /* END test_wc_Sha3_384_Copy */
  11379. /*
  11380. * Testing wc_Sha3_512_Copy()
  11381. */
  11382. static int test_wc_Sha3_512_Copy (void)
  11383. {
  11384. int ret = 0;
  11385. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  11386. wc_Sha3 sha3, sha3Cpy;
  11387. const char* msg = TEST_STRING;
  11388. word32 msglen = (word32)TEST_STRING_SZ;
  11389. byte hash[WC_SHA3_512_DIGEST_SIZE];
  11390. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  11391. XMEMSET(hash, 0, sizeof(hash));
  11392. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  11393. printf(testingFmt, "wc_Sha3_512_Copy()");
  11394. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  11395. if (ret != 0) {
  11396. return ret;
  11397. }
  11398. ret = wc_InitSha3_512(&sha3Cpy, HEAP_HINT, devId);
  11399. if (ret != 0) {
  11400. wc_Sha3_512_Free(&sha3);
  11401. return ret;
  11402. }
  11403. ret = wc_Sha3_512_Update(&sha3, (byte*)msg, msglen);
  11404. if (ret == 0) {
  11405. ret = wc_Sha3_512_Copy(&sha3Cpy, &sha3);
  11406. if (ret == 0) {
  11407. ret = wc_Sha3_512_Final(&sha3, hash);
  11408. if (ret == 0) {
  11409. ret = wc_Sha3_512_Final(&sha3Cpy, hashCpy);
  11410. }
  11411. }
  11412. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  11413. ret = WOLFSSL_FATAL_ERROR;
  11414. }
  11415. }
  11416. /* Test bad args. */
  11417. if (ret == 0) {
  11418. ret = wc_Sha3_512_Copy(NULL, &sha3);
  11419. if (ret == BAD_FUNC_ARG) {
  11420. ret = wc_Sha3_512_Copy(&sha3Cpy, NULL);
  11421. }
  11422. if (ret == BAD_FUNC_ARG) {
  11423. ret = 0;
  11424. } else if (ret == 0) {
  11425. ret = WOLFSSL_FATAL_ERROR;
  11426. }
  11427. }
  11428. printf(resultFmt, ret == 0 ? passed : failed);
  11429. #endif
  11430. return ret;
  11431. } /* END test_wc_Sha3_512_Copy */
  11432. /*
  11433. * Unit test function for wc_Sha3_GetFlags()
  11434. */
  11435. static int test_wc_Sha3_GetFlags (void)
  11436. {
  11437. int ret = 0;
  11438. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  11439. wc_Sha3 sha3;
  11440. word32 flags = 0;
  11441. printf(testingFmt, "wc_Sha3_GetFlags()");
  11442. /* Initialize */
  11443. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  11444. if (ret != 0) {
  11445. return ret;
  11446. }
  11447. if (ret == 0) {
  11448. ret = wc_Sha3_GetFlags(&sha3, &flags);
  11449. }
  11450. if (ret == 0) {
  11451. if (flags & WC_HASH_FLAG_ISCOPY) {
  11452. ret = 0;
  11453. }
  11454. }
  11455. wc_Sha3_224_Free(&sha3);
  11456. printf(resultFmt, ret == 0 ? passed : failed);
  11457. #endif
  11458. return ret;
  11459. } /* END test_wc_Sha3_GetFlags */
  11460. static int test_wc_InitShake256 (void)
  11461. {
  11462. int ret = 0;
  11463. #ifdef WOLFSSL_SHAKE256
  11464. wc_Shake shake;
  11465. printf(testingFmt, "wc_InitShake256()");
  11466. ret = wc_InitShake256(&shake, HEAP_HINT, devId);
  11467. /* Test bad args. */
  11468. if (ret == 0) {
  11469. ret = wc_InitShake256(NULL, HEAP_HINT, devId);
  11470. if (ret == BAD_FUNC_ARG) {
  11471. ret = 0;
  11472. } else if (ret == 0) {
  11473. ret = WOLFSSL_FATAL_ERROR;
  11474. }
  11475. }
  11476. wc_Shake256_Free(&shake);
  11477. printf(resultFmt, ret == 0 ? passed : failed);
  11478. #endif
  11479. return ret;
  11480. } /* END test_wc_InitSha3 */
  11481. static int testing_wc_Shake256_Update (void)
  11482. {
  11483. int ret = 0;
  11484. #ifdef WOLFSSL_SHAKE256
  11485. wc_Shake shake;
  11486. byte msg[] = "Everybody's working for the weekend.";
  11487. byte msg2[] = "Everybody gets Friday off.";
  11488. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  11489. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  11490. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  11491. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  11492. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  11493. word32 msglen = sizeof(msg) - 1;
  11494. word32 msg2len = sizeof(msg2);
  11495. word32 msgCmplen = sizeof(msgCmp);
  11496. printf(testingFmt, "wc_Shake256_Update()");
  11497. ret = wc_InitShake256(&shake, HEAP_HINT, devId);
  11498. if (ret != 0) {
  11499. return ret;
  11500. }
  11501. ret = wc_Shake256_Update(&shake, msg, msglen);
  11502. if (XMEMCMP(msg, shake.t, msglen) || shake.i != msglen) {
  11503. ret = WOLFSSL_FATAL_ERROR;
  11504. }
  11505. if (ret == 0) {
  11506. ret = wc_Shake256_Update(&shake, msg2, msg2len);
  11507. if (XMEMCMP(shake.t, msgCmp, msgCmplen) != 0) {
  11508. ret = WOLFSSL_FATAL_ERROR;
  11509. }
  11510. }
  11511. /* Pass bad args. */
  11512. if (ret == 0) {
  11513. ret = wc_Shake256_Update(NULL, msg2, msg2len);
  11514. if (ret == BAD_FUNC_ARG) {
  11515. ret = wc_Shake256_Update(&shake, NULL, 5);
  11516. }
  11517. if (ret == BAD_FUNC_ARG) {
  11518. wc_Shake256_Free(&shake);
  11519. if (wc_InitShake256(&shake, HEAP_HINT, devId)) {
  11520. return ret;
  11521. }
  11522. ret = wc_Shake256_Update(&shake, NULL, 0);
  11523. if (ret == 0) {
  11524. ret = wc_Shake256_Update(&shake, msg2, msg2len);
  11525. }
  11526. if (ret == 0 && XMEMCMP(msg2, shake.t, msg2len) != 0) {
  11527. ret = WOLFSSL_FATAL_ERROR;
  11528. }
  11529. }
  11530. }
  11531. wc_Shake256_Free(&shake);
  11532. printf(resultFmt, ret == 0 ? passed : failed);
  11533. #endif /* WOLFSSL_SHAKE256 */
  11534. return ret;
  11535. }
  11536. static int test_wc_Shake256_Final (void)
  11537. {
  11538. int ret = 0;
  11539. #ifdef WOLFSSL_SHAKE256
  11540. wc_Shake shake;
  11541. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  11542. "nopnopq";
  11543. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  11544. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  11545. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  11546. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  11547. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  11548. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  11549. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  11550. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  11551. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  11552. byte hash[114];
  11553. /* Init stack variables. */
  11554. XMEMSET(hash, 0, sizeof(hash));
  11555. printf(testingFmt, "wc_Shake256_Final()");
  11556. ret = wc_InitShake256(&shake, HEAP_HINT, devId);
  11557. if (ret != 0) {
  11558. return ret;
  11559. }
  11560. ret= wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg));
  11561. if (ret == 0) {
  11562. ret = wc_Shake256_Final(&shake, hash, (word32)sizeof(hash));
  11563. if (ret == 0 && XMEMCMP(expOut, hash, (word32)sizeof(hash)) != 0) {
  11564. ret = WOLFSSL_FATAL_ERROR;
  11565. }
  11566. }
  11567. /* Test bad args. */
  11568. if (ret == 0) {
  11569. ret = wc_Shake256_Final(NULL, hash, (word32)sizeof(hash));
  11570. if (ret == 0) {
  11571. ret = wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash));
  11572. }
  11573. if (ret == BAD_FUNC_ARG) {
  11574. ret = 0;
  11575. } else if (ret == 0) {
  11576. ret = WOLFSSL_FATAL_ERROR;
  11577. }
  11578. }
  11579. wc_Shake256_Free(&shake);
  11580. printf(resultFmt, ret == 0 ? passed : failed);
  11581. #endif
  11582. return ret;
  11583. }
  11584. /*
  11585. * Testing wc_Shake256_Copy()
  11586. */
  11587. static int test_wc_Shake256_Copy (void)
  11588. {
  11589. int ret = 0;
  11590. #ifdef WOLFSSL_SHAKE256
  11591. wc_Shake shake, shakeCpy;
  11592. const char* msg = TEST_STRING;
  11593. word32 msglen = (word32)TEST_STRING_SZ;
  11594. byte hash[144];
  11595. byte hashCpy[144];
  11596. word32 hashLen = sizeof(hash);
  11597. word32 hashLenCpy = sizeof(hashCpy);
  11598. XMEMSET(hash, 0, sizeof(hash));
  11599. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  11600. printf(testingFmt, "wc_Shake256_Copy()");
  11601. ret = wc_InitShake256(&shake, HEAP_HINT, devId);
  11602. if (ret != 0) {
  11603. return ret;
  11604. }
  11605. ret = wc_InitShake256(&shakeCpy, HEAP_HINT, devId);
  11606. if (ret != 0) {
  11607. wc_Shake256_Free(&shake);
  11608. return ret;
  11609. }
  11610. ret = wc_Shake256_Update(&shake, (byte*)msg, msglen);
  11611. if (ret == 0) {
  11612. ret = wc_Shake256_Copy(&shakeCpy, &shake);
  11613. if (ret == 0) {
  11614. ret = wc_Shake256_Final(&shake, hash, hashLen);
  11615. if (ret == 0) {
  11616. ret = wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy);
  11617. }
  11618. }
  11619. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  11620. ret = WOLFSSL_FATAL_ERROR;
  11621. }
  11622. }
  11623. /* Test bad args. */
  11624. if (ret == 0) {
  11625. ret = wc_Shake256_Copy(NULL, &shake);
  11626. if (ret == BAD_FUNC_ARG) {
  11627. ret = wc_Shake256_Copy(&shakeCpy, NULL);
  11628. }
  11629. if (ret == BAD_FUNC_ARG) {
  11630. ret = 0;
  11631. } else if (ret == 0) {
  11632. ret = WOLFSSL_FATAL_ERROR;
  11633. }
  11634. }
  11635. wc_Shake256_Free(&shake);
  11636. printf(resultFmt, ret == 0 ? passed : failed);
  11637. #endif
  11638. return ret;
  11639. } /* END test_wc_Shake256_Copy */
  11640. /*
  11641. * Unit test function for wc_Shake256Hash()
  11642. */
  11643. static int test_wc_Shake256Hash(void)
  11644. {
  11645. int ret = 0;
  11646. #ifdef WOLFSSL_SHAKE256
  11647. const byte data[] = { /* Hello World */
  11648. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  11649. 0x72,0x6c,0x64
  11650. };
  11651. word32 len = sizeof(data);
  11652. byte hash[144];
  11653. word32 hashLen = sizeof(hash);
  11654. printf(testingFmt, "wc_Shake256Hash()");
  11655. ret = wc_Shake256Hash(data, len, hash, hashLen);
  11656. printf(resultFmt, ret == 0 ? passed : failed);
  11657. #endif
  11658. return ret;
  11659. } /* END test_wc_Shake256Hash */
  11660. /*
  11661. * Test function for wc_HmacSetKey
  11662. */
  11663. static int test_wc_Md5HmacSetKey (void)
  11664. {
  11665. int flag = 0;
  11666. #if !defined(NO_HMAC) && !defined(NO_MD5)
  11667. Hmac hmac;
  11668. int ret, times, itr;
  11669. const char* keys[]=
  11670. {
  11671. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  11672. #ifndef HAVE_FIPS
  11673. "Jefe", /* smaller than minimum FIPS key size */
  11674. #endif
  11675. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  11676. };
  11677. times = sizeof(keys) / sizeof(char*);
  11678. flag = 0;
  11679. printf(testingFmt, "wc_HmacSetKey() with MD5");
  11680. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  11681. if (ret != 0)
  11682. return ret;
  11683. for (itr = 0; itr < times; itr++) {
  11684. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  11685. (word32)XSTRLEN(keys[itr]));
  11686. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  11687. wc_HmacFree(&hmac);
  11688. if (ret == BAD_FUNC_ARG)
  11689. return 0;
  11690. else {
  11691. return WOLFSSL_FATAL_ERROR;
  11692. }
  11693. #else
  11694. if (ret != 0) {
  11695. flag = ret;
  11696. }
  11697. #endif
  11698. }
  11699. /* Bad args. */
  11700. if (!flag) {
  11701. ret = wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  11702. (word32)XSTRLEN(keys[0]));
  11703. if (ret != BAD_FUNC_ARG) {
  11704. flag = WOLFSSL_FATAL_ERROR;
  11705. }
  11706. }
  11707. if (!flag) {
  11708. ret = wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0]));
  11709. if (ret != BAD_FUNC_ARG) {
  11710. flag = WOLFSSL_FATAL_ERROR;
  11711. }
  11712. }
  11713. if (!flag) {
  11714. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  11715. (word32)XSTRLEN(keys[0]));
  11716. if (ret != BAD_FUNC_ARG) {
  11717. flag = WOLFSSL_FATAL_ERROR;
  11718. }
  11719. }
  11720. if (!flag) {
  11721. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  11722. #ifdef HAVE_FIPS
  11723. if (ret != HMAC_MIN_KEYLEN_E) {
  11724. flag = WOLFSSL_FATAL_ERROR;
  11725. }
  11726. #else
  11727. if (ret != 0) {
  11728. flag = WOLFSSL_FATAL_ERROR;
  11729. }
  11730. #endif
  11731. }
  11732. wc_HmacFree(&hmac);
  11733. printf(resultFmt, flag == 0 ? passed : failed);
  11734. #endif
  11735. return flag;
  11736. } /* END test_wc_Md5HmacSetKey */
  11737. /*
  11738. * testing wc_HmacSetKey() on wc_Sha hash.
  11739. */
  11740. static int test_wc_ShaHmacSetKey (void)
  11741. {
  11742. int flag = 0;
  11743. #if !defined(NO_HMAC) && !defined(NO_SHA)
  11744. Hmac hmac;
  11745. int ret, times, itr;
  11746. const char* keys[]=
  11747. {
  11748. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  11749. "\x0b\x0b\x0b",
  11750. #ifndef HAVE_FIPS
  11751. "Jefe", /* smaller than minimum FIPS key size */
  11752. #endif
  11753. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  11754. "\xAA\xAA\xAA"
  11755. };
  11756. times = sizeof(keys) / sizeof(char*);
  11757. flag = 0;
  11758. printf(testingFmt, "wc_HmacSetKey() with SHA");
  11759. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  11760. if (ret != 0)
  11761. return ret;
  11762. for (itr = 0; itr < times; itr++) {
  11763. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  11764. (word32)XSTRLEN(keys[itr]));
  11765. if (ret != 0) {
  11766. flag = ret;
  11767. }
  11768. }
  11769. /* Bad args. */
  11770. if (!flag) {
  11771. ret = wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  11772. (word32)XSTRLEN(keys[0]));
  11773. if (ret != BAD_FUNC_ARG) {
  11774. flag = WOLFSSL_FATAL_ERROR;
  11775. }
  11776. }
  11777. if (!flag) {
  11778. ret = wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0]));
  11779. if (ret != BAD_FUNC_ARG) {
  11780. flag = WOLFSSL_FATAL_ERROR;
  11781. }
  11782. }
  11783. if (!flag) {
  11784. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  11785. (word32)XSTRLEN(keys[0]));
  11786. if (ret != BAD_FUNC_ARG) {
  11787. flag = WOLFSSL_FATAL_ERROR;
  11788. }
  11789. }
  11790. if (!flag) {
  11791. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  11792. #ifdef HAVE_FIPS
  11793. if (ret != HMAC_MIN_KEYLEN_E) {
  11794. flag = WOLFSSL_FATAL_ERROR;
  11795. }
  11796. #else
  11797. if (ret != 0) {
  11798. flag = WOLFSSL_FATAL_ERROR;
  11799. }
  11800. #endif
  11801. }
  11802. wc_HmacFree(&hmac);
  11803. printf(resultFmt, flag == 0 ? passed : failed);
  11804. #endif
  11805. return flag;
  11806. } /* END test_wc_ShaHmacSetKey() */
  11807. /*
  11808. * testing wc_HmacSetKey() on Sha224 hash.
  11809. */
  11810. static int test_wc_Sha224HmacSetKey (void)
  11811. {
  11812. int flag = 0;
  11813. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  11814. Hmac hmac;
  11815. int ret, times, itr;
  11816. const char* keys[]=
  11817. {
  11818. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  11819. "\x0b\x0b\x0b",
  11820. #ifndef HAVE_FIPS
  11821. "Jefe", /* smaller than minimum FIPS key size */
  11822. #endif
  11823. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  11824. "\xAA\xAA\xAA"
  11825. };
  11826. times = sizeof(keys) / sizeof(char*);
  11827. flag = 0;
  11828. printf(testingFmt, "wc_HmacSetKey() with SHA 224");
  11829. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  11830. if (ret != 0)
  11831. return ret;
  11832. for (itr = 0; itr < times; itr++) {
  11833. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  11834. (word32)XSTRLEN(keys[itr]));
  11835. if (ret != 0) {
  11836. flag = ret;
  11837. }
  11838. }
  11839. /* Bad args. */
  11840. if (!flag) {
  11841. ret = wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  11842. (word32)XSTRLEN(keys[0]));
  11843. if (ret != BAD_FUNC_ARG) {
  11844. flag = WOLFSSL_FATAL_ERROR;
  11845. }
  11846. }
  11847. if (!flag) {
  11848. ret = wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0]));
  11849. if (ret != BAD_FUNC_ARG) {
  11850. flag = WOLFSSL_FATAL_ERROR;
  11851. }
  11852. }
  11853. if (!flag) {
  11854. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  11855. (word32)XSTRLEN(keys[0]));
  11856. if (ret != BAD_FUNC_ARG) {
  11857. flag = WOLFSSL_FATAL_ERROR;
  11858. }
  11859. }
  11860. if (!flag) {
  11861. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  11862. #ifdef HAVE_FIPS
  11863. if (ret != HMAC_MIN_KEYLEN_E) {
  11864. flag = WOLFSSL_FATAL_ERROR;
  11865. }
  11866. #else
  11867. if (ret != 0) {
  11868. flag = WOLFSSL_FATAL_ERROR;
  11869. }
  11870. #endif
  11871. }
  11872. wc_HmacFree(&hmac);
  11873. printf(resultFmt, flag == 0 ? passed : failed);
  11874. #endif
  11875. return flag;
  11876. } /* END test_wc_Sha224HmacSetKey() */
  11877. /*
  11878. * testing wc_HmacSetKey() on Sha256 hash
  11879. */
  11880. static int test_wc_Sha256HmacSetKey (void)
  11881. {
  11882. int flag = 0;
  11883. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  11884. Hmac hmac;
  11885. int ret, times, itr;
  11886. const char* keys[]=
  11887. {
  11888. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  11889. "\x0b\x0b\x0b",
  11890. #ifndef HAVE_FIPS
  11891. "Jefe", /* smaller than minimum FIPS key size */
  11892. #endif
  11893. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  11894. "\xAA\xAA\xAA"
  11895. };
  11896. times = sizeof(keys) / sizeof(char*);
  11897. flag = 0;
  11898. printf(testingFmt, "wc_HmacSetKey() with SHA256");
  11899. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  11900. if (ret != 0)
  11901. return ret;
  11902. for (itr = 0; itr < times; itr++) {
  11903. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  11904. (word32)XSTRLEN(keys[itr]));
  11905. if (ret != 0) {
  11906. flag = ret;
  11907. }
  11908. }
  11909. /* Bad args. */
  11910. if (!flag) {
  11911. ret = wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  11912. (word32)XSTRLEN(keys[0]));
  11913. if (ret != BAD_FUNC_ARG) {
  11914. flag = WOLFSSL_FATAL_ERROR;
  11915. }
  11916. }
  11917. if (!flag) {
  11918. ret = wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0]));
  11919. if (ret != BAD_FUNC_ARG) {
  11920. flag = WOLFSSL_FATAL_ERROR;
  11921. }
  11922. }
  11923. if (!flag) {
  11924. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  11925. (word32)XSTRLEN(keys[0]));
  11926. if (ret != BAD_FUNC_ARG) {
  11927. flag = WOLFSSL_FATAL_ERROR;
  11928. }
  11929. }
  11930. if (!flag) {
  11931. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  11932. #ifdef HAVE_FIPS
  11933. if (ret != HMAC_MIN_KEYLEN_E) {
  11934. flag = WOLFSSL_FATAL_ERROR;
  11935. }
  11936. #else
  11937. if (ret != 0) {
  11938. flag = WOLFSSL_FATAL_ERROR;
  11939. }
  11940. #endif
  11941. }
  11942. wc_HmacFree(&hmac);
  11943. printf(resultFmt, flag == 0 ? passed : failed);
  11944. #endif
  11945. return flag;
  11946. } /* END test_wc_Sha256HmacSetKey() */
  11947. /*
  11948. * testing wc_HmacSetKey on Sha384 hash.
  11949. */
  11950. static int test_wc_Sha384HmacSetKey (void)
  11951. {
  11952. int flag = 0;
  11953. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  11954. Hmac hmac;
  11955. int ret, times, itr;
  11956. const char* keys[]=
  11957. {
  11958. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  11959. "\x0b\x0b\x0b",
  11960. #ifndef HAVE_FIPS
  11961. "Jefe", /* smaller than minimum FIPS key size */
  11962. #endif
  11963. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  11964. "\xAA\xAA\xAA"
  11965. };
  11966. times = sizeof(keys) / sizeof(char*);
  11967. flag = 0;
  11968. printf(testingFmt, "wc_HmacSetKey() with SHA384");
  11969. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  11970. if (ret != 0)
  11971. return ret;
  11972. for (itr = 0; itr < times; itr++) {
  11973. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  11974. (word32)XSTRLEN(keys[itr]));
  11975. if (ret != 0) {
  11976. flag = ret;
  11977. }
  11978. }
  11979. /* Bad args. */
  11980. if (!flag) {
  11981. ret = wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  11982. (word32)XSTRLEN(keys[0]));
  11983. if (ret != BAD_FUNC_ARG) {
  11984. flag = WOLFSSL_FATAL_ERROR;
  11985. }
  11986. }
  11987. if (!flag) {
  11988. ret = wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0]));
  11989. if (ret != BAD_FUNC_ARG) {
  11990. flag = WOLFSSL_FATAL_ERROR;
  11991. }
  11992. }
  11993. if (!flag) {
  11994. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  11995. (word32)XSTRLEN(keys[0]));
  11996. if (ret != BAD_FUNC_ARG) {
  11997. flag = WOLFSSL_FATAL_ERROR;
  11998. }
  11999. }
  12000. if (!flag) {
  12001. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  12002. #ifdef HAVE_FIPS
  12003. if (ret != HMAC_MIN_KEYLEN_E) {
  12004. flag = WOLFSSL_FATAL_ERROR;
  12005. }
  12006. #else
  12007. if (ret != 0) {
  12008. flag = WOLFSSL_FATAL_ERROR;
  12009. }
  12010. #endif
  12011. }
  12012. wc_HmacFree(&hmac);
  12013. printf(resultFmt, flag == 0 ? passed : failed);
  12014. #endif
  12015. return flag;
  12016. } /* END test_wc_Sha384HmacSetKey() */
  12017. /*
  12018. * testing wc_HmacUpdate on wc_Md5 hash.
  12019. */
  12020. static int test_wc_Md5HmacUpdate (void)
  12021. {
  12022. int flag = 0;
  12023. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  12024. Hmac hmac;
  12025. testVector a, b;
  12026. int ret;
  12027. #ifdef HAVE_FIPS
  12028. const char* keys =
  12029. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  12030. #else
  12031. const char* keys = "Jefe";
  12032. #endif
  12033. a.input = "what do ya want for nothing?";
  12034. a.inLen = XSTRLEN(a.input);
  12035. b.input = "Hi There";
  12036. b.inLen = XSTRLEN(b.input);
  12037. flag = 0;
  12038. printf(testingFmt, "wc_HmacUpdate() with MD5");
  12039. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12040. if (ret != 0)
  12041. return ret;
  12042. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys, (word32)XSTRLEN(keys));
  12043. if (ret != 0) {
  12044. flag = ret;
  12045. }
  12046. if (!flag) {
  12047. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  12048. if (ret != 0) {
  12049. flag = ret;
  12050. }
  12051. }
  12052. /* Update Hmac. */
  12053. if (!flag) {
  12054. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12055. if (ret != 0) {
  12056. flag = ret;
  12057. }
  12058. }
  12059. /* Test bad args. */
  12060. if (!flag) {
  12061. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  12062. if (ret != BAD_FUNC_ARG) {
  12063. flag = WOLFSSL_FATAL_ERROR;
  12064. }
  12065. }
  12066. if (!flag) {
  12067. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  12068. if (ret != BAD_FUNC_ARG) {
  12069. flag = WOLFSSL_FATAL_ERROR;
  12070. }
  12071. }
  12072. if (!flag) {
  12073. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  12074. if (ret != 0) {
  12075. flag = ret;
  12076. }
  12077. }
  12078. wc_HmacFree(&hmac);
  12079. printf(resultFmt, flag == 0 ? passed : failed);
  12080. #endif
  12081. return flag;
  12082. } /* END test_wc_Md5HmacUpdate */
  12083. /*
  12084. * testing wc_HmacUpdate on SHA hash.
  12085. */
  12086. static int test_wc_ShaHmacUpdate (void)
  12087. {
  12088. int flag = 0;
  12089. #if !defined(NO_HMAC) && !defined(NO_SHA)
  12090. Hmac hmac;
  12091. testVector a, b;
  12092. int ret;
  12093. #ifdef HAVE_FIPS
  12094. const char* keys =
  12095. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  12096. #else
  12097. const char* keys = "Jefe";
  12098. #endif
  12099. a.input = "what do ya want for nothing?";
  12100. a.inLen = XSTRLEN(a.input);
  12101. b.input = "Hi There";
  12102. b.inLen = XSTRLEN(b.input);
  12103. flag = 0;
  12104. printf(testingFmt, "wc_HmacUpdate() with SHA");
  12105. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12106. if (ret != 0)
  12107. return ret;
  12108. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys, (word32)XSTRLEN(keys));
  12109. if (ret != 0) {
  12110. flag = ret;
  12111. }
  12112. if (!flag) {
  12113. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  12114. if (ret != 0) {
  12115. flag = ret;
  12116. }
  12117. }
  12118. /* Update Hmac. */
  12119. if (!flag) {
  12120. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12121. if (ret != 0) {
  12122. flag = ret;
  12123. }
  12124. }
  12125. /* Test bad args. */
  12126. if (!flag) {
  12127. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  12128. if (ret != BAD_FUNC_ARG) {
  12129. flag = WOLFSSL_FATAL_ERROR;
  12130. }
  12131. }
  12132. if (!flag) {
  12133. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  12134. if (ret != BAD_FUNC_ARG) {
  12135. flag = WOLFSSL_FATAL_ERROR;
  12136. }
  12137. }
  12138. if (!flag) {
  12139. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  12140. if (ret != 0) {
  12141. flag = ret;
  12142. }
  12143. }
  12144. wc_HmacFree(&hmac);
  12145. printf(resultFmt, flag == 0 ? passed : failed);
  12146. #endif
  12147. return flag;
  12148. } /* END test_wc_ShaHmacUpdate */
  12149. /*
  12150. * testing wc_HmacUpdate on SHA224 hash.
  12151. */
  12152. static int test_wc_Sha224HmacUpdate (void)
  12153. {
  12154. int flag = 0;
  12155. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  12156. Hmac hmac;
  12157. testVector a, b;
  12158. int ret;
  12159. #ifdef HAVE_FIPS
  12160. const char* keys =
  12161. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  12162. #else
  12163. const char* keys = "Jefe";
  12164. #endif
  12165. a.input = "what do ya want for nothing?";
  12166. a.inLen = XSTRLEN(a.input);
  12167. b.input = "Hi There";
  12168. b.inLen = XSTRLEN(b.input);
  12169. flag = 0;
  12170. printf(testingFmt, "wc_HmacUpdate() with SHA224");
  12171. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12172. if (ret != 0)
  12173. return ret;
  12174. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys, (word32)XSTRLEN(keys));
  12175. if (ret != 0) {
  12176. flag = ret;
  12177. }
  12178. if (!flag) {
  12179. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  12180. if (ret != 0) {
  12181. flag = ret;
  12182. }
  12183. }
  12184. /* Update Hmac. */
  12185. if (!flag) {
  12186. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12187. if (ret != 0) {
  12188. flag = ret;
  12189. }
  12190. }
  12191. /* Test bad args. */
  12192. if (!flag) {
  12193. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  12194. if (ret != BAD_FUNC_ARG) {
  12195. flag = WOLFSSL_FATAL_ERROR;
  12196. }
  12197. }
  12198. if (!flag) {
  12199. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  12200. if (ret != BAD_FUNC_ARG) {
  12201. flag = WOLFSSL_FATAL_ERROR;
  12202. }
  12203. }
  12204. if (!flag) {
  12205. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  12206. if (ret != 0) {
  12207. flag = ret;
  12208. }
  12209. }
  12210. wc_HmacFree(&hmac);
  12211. printf(resultFmt, flag == 0 ? passed : failed);
  12212. #endif
  12213. return flag;
  12214. } /* END test_wc_Sha224HmacUpdate */
  12215. /*
  12216. * testing wc_HmacUpdate on SHA256 hash.
  12217. */
  12218. static int test_wc_Sha256HmacUpdate (void)
  12219. {
  12220. int flag = 0;
  12221. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  12222. Hmac hmac;
  12223. testVector a, b;
  12224. int ret;
  12225. #ifdef HAVE_FIPS
  12226. const char* keys =
  12227. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  12228. #else
  12229. const char* keys = "Jefe";
  12230. #endif
  12231. a.input = "what do ya want for nothing?";
  12232. a.inLen = XSTRLEN(a.input);
  12233. b.input = "Hi There";
  12234. b.inLen = XSTRLEN(b.input);
  12235. flag = 0;
  12236. printf(testingFmt, "wc_HmacUpdate() with WC_SHA256");
  12237. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12238. if (ret != 0)
  12239. return ret;
  12240. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys, (word32)XSTRLEN(keys));
  12241. if (ret != 0) {
  12242. flag = ret;
  12243. }
  12244. if (!flag) {
  12245. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  12246. if (ret != 0) {
  12247. flag = ret;
  12248. }
  12249. }
  12250. /* Update Hmac. */
  12251. if (!flag) {
  12252. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12253. if (ret != 0) {
  12254. flag = ret;
  12255. }
  12256. }
  12257. /* Test bad args. */
  12258. if (!flag) {
  12259. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  12260. if (ret != BAD_FUNC_ARG) {
  12261. flag = WOLFSSL_FATAL_ERROR;
  12262. }
  12263. }
  12264. if (!flag) {
  12265. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  12266. if (ret != BAD_FUNC_ARG) {
  12267. flag = WOLFSSL_FATAL_ERROR;
  12268. }
  12269. }
  12270. if (!flag) {
  12271. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  12272. if (ret != 0) {
  12273. flag = ret;
  12274. }
  12275. }
  12276. wc_HmacFree(&hmac);
  12277. printf(resultFmt, flag == 0 ? passed : failed);
  12278. #endif
  12279. return flag;
  12280. } /* END test_wc_Sha256HmacUpdate */
  12281. /*
  12282. * testing wc_HmacUpdate on SHA384 hash.
  12283. */
  12284. static int test_wc_Sha384HmacUpdate (void)
  12285. {
  12286. int flag = 0;
  12287. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  12288. Hmac hmac;
  12289. testVector a, b;
  12290. int ret;
  12291. #ifdef HAVE_FIPS
  12292. const char* keys =
  12293. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  12294. #else
  12295. const char* keys = "Jefe";
  12296. #endif
  12297. a.input = "what do ya want for nothing?";
  12298. a.inLen = XSTRLEN(a.input);
  12299. b.input = "Hi There";
  12300. b.inLen = XSTRLEN(b.input);
  12301. flag = 0;
  12302. printf(testingFmt, "wc_HmacUpdate() with SHA384");
  12303. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12304. if (ret != 0)
  12305. return ret;
  12306. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys, (word32)XSTRLEN(keys));
  12307. if (ret != 0) {
  12308. flag = ret;
  12309. }
  12310. if (!flag) {
  12311. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  12312. if (ret != 0) {
  12313. flag = ret;
  12314. }
  12315. }
  12316. /* Update Hmac. */
  12317. if (!flag) {
  12318. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12319. if (ret != 0) {
  12320. flag = ret;
  12321. }
  12322. }
  12323. /* Test bad args. */
  12324. if (!flag) {
  12325. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  12326. if (ret != BAD_FUNC_ARG) {
  12327. flag = WOLFSSL_FATAL_ERROR;
  12328. }
  12329. }
  12330. if (!flag) {
  12331. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  12332. if (ret != BAD_FUNC_ARG) {
  12333. flag = WOLFSSL_FATAL_ERROR;
  12334. }
  12335. }
  12336. if (!flag) {
  12337. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  12338. if (ret != 0) {
  12339. flag = ret;
  12340. }
  12341. }
  12342. wc_HmacFree(&hmac);
  12343. printf(resultFmt, flag == 0 ? passed : failed);
  12344. #endif
  12345. return flag;
  12346. } /* END test_wc_Sha384HmacUpdate */
  12347. /*
  12348. * Testing wc_HmacFinal() with MD5
  12349. */
  12350. static int test_wc_Md5HmacFinal (void)
  12351. {
  12352. int flag = 0;
  12353. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  12354. Hmac hmac;
  12355. byte hash[WC_MD5_DIGEST_SIZE];
  12356. testVector a;
  12357. int ret;
  12358. const char* key;
  12359. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  12360. a.input = "Hi There";
  12361. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  12362. "\x9d";
  12363. a.inLen = XSTRLEN(a.input);
  12364. a.outLen = XSTRLEN(a.output);
  12365. flag = 0;
  12366. printf(testingFmt, "wc_HmacFinal() with MD5");
  12367. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12368. if (ret != 0)
  12369. return ret;
  12370. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key));
  12371. if (ret != 0) {
  12372. flag = ret;
  12373. }
  12374. if (!flag) {
  12375. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12376. if (ret != 0) {
  12377. flag = ret;
  12378. }
  12379. }
  12380. if (!flag) {
  12381. ret = wc_HmacFinal(&hmac, hash);
  12382. if (ret != 0) {
  12383. flag = ret;
  12384. }
  12385. }
  12386. if (!flag) {
  12387. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  12388. flag = WOLFSSL_FATAL_ERROR;
  12389. }
  12390. }
  12391. /* Try bad parameters. */
  12392. if (!flag) {
  12393. ret = wc_HmacFinal(NULL, hash);
  12394. if (ret != BAD_FUNC_ARG) {
  12395. flag = WOLFSSL_FATAL_ERROR;
  12396. }
  12397. }
  12398. #ifndef HAVE_FIPS
  12399. if (!flag) {
  12400. ret = wc_HmacFinal(&hmac, NULL);
  12401. if (ret != BAD_FUNC_ARG) {
  12402. flag = WOLFSSL_FATAL_ERROR;
  12403. }
  12404. }
  12405. #endif
  12406. wc_HmacFree(&hmac);
  12407. printf(resultFmt, flag == 0 ? passed : failed);
  12408. #endif
  12409. return flag;
  12410. } /* END test_wc_Md5HmacFinal */
  12411. /*
  12412. * Testing wc_HmacFinal() with SHA
  12413. */
  12414. static int test_wc_ShaHmacFinal (void)
  12415. {
  12416. int flag = 0;
  12417. #if !defined(NO_HMAC) && !defined(NO_SHA)
  12418. Hmac hmac;
  12419. byte hash[WC_SHA_DIGEST_SIZE];
  12420. testVector a;
  12421. int ret;
  12422. const char* key;
  12423. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12424. "\x0b\x0b\x0b";
  12425. a.input = "Hi There";
  12426. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  12427. "\x8e\xf1\x46\xbe\x00";
  12428. a.inLen = XSTRLEN(a.input);
  12429. a.outLen = XSTRLEN(a.output);
  12430. flag = 0;
  12431. printf(testingFmt, "wc_HmacFinal() with SHA");
  12432. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12433. if (ret != 0)
  12434. return ret;
  12435. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key));
  12436. if (ret != 0) {
  12437. flag = ret;
  12438. }
  12439. if (!flag) {
  12440. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12441. if (ret != 0) {
  12442. flag = ret;
  12443. }
  12444. }
  12445. if (!flag) {
  12446. ret = wc_HmacFinal(&hmac, hash);
  12447. if (ret != 0) {
  12448. flag = ret;
  12449. }
  12450. }
  12451. if (!flag) {
  12452. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  12453. flag = WOLFSSL_FATAL_ERROR;
  12454. }
  12455. }
  12456. /* Try bad parameters. */
  12457. if (!flag) {
  12458. ret = wc_HmacFinal(NULL, hash);
  12459. if (ret != BAD_FUNC_ARG) {
  12460. flag = WOLFSSL_FATAL_ERROR;
  12461. }
  12462. }
  12463. #ifndef HAVE_FIPS
  12464. if (!flag) {
  12465. ret = wc_HmacFinal(&hmac, NULL);
  12466. if (ret != BAD_FUNC_ARG) {
  12467. flag = WOLFSSL_FATAL_ERROR;
  12468. }
  12469. }
  12470. #endif
  12471. wc_HmacFree(&hmac);
  12472. printf(resultFmt, flag == 0 ? passed : failed);
  12473. #endif
  12474. return flag;
  12475. } /* END test_wc_ShaHmacFinal */
  12476. /*
  12477. * Testing wc_HmacFinal() with SHA224
  12478. */
  12479. static int test_wc_Sha224HmacFinal (void)
  12480. {
  12481. int flag = 0;
  12482. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  12483. Hmac hmac;
  12484. byte hash[WC_SHA224_DIGEST_SIZE];
  12485. testVector a;
  12486. int ret;
  12487. const char* key;
  12488. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12489. "\x0b\x0b\x0b";
  12490. a.input = "Hi There";
  12491. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  12492. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  12493. a.inLen = XSTRLEN(a.input);
  12494. a.outLen = XSTRLEN(a.output);
  12495. flag = 0;
  12496. printf(testingFmt, "wc_HmacFinal() with SHA224");
  12497. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12498. if (ret != 0)
  12499. return ret;
  12500. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key, (word32)XSTRLEN(key));
  12501. if (ret != 0) {
  12502. flag = ret;
  12503. }
  12504. if (!flag) {
  12505. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12506. if (ret != 0) {
  12507. flag = ret;
  12508. }
  12509. }
  12510. if (!flag) {
  12511. ret = wc_HmacFinal(&hmac, hash);
  12512. if (ret != 0) {
  12513. flag = ret;
  12514. }
  12515. }
  12516. if (!flag) {
  12517. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  12518. flag = WOLFSSL_FATAL_ERROR;
  12519. }
  12520. }
  12521. /* Try bad parameters. */
  12522. if (!flag) {
  12523. ret = wc_HmacFinal(NULL, hash);
  12524. if (ret != BAD_FUNC_ARG) {
  12525. flag = WOLFSSL_FATAL_ERROR;
  12526. }
  12527. }
  12528. #ifndef HAVE_FIPS
  12529. if (!flag) {
  12530. ret = wc_HmacFinal(&hmac, NULL);
  12531. if (ret != BAD_FUNC_ARG) {
  12532. flag = WOLFSSL_FATAL_ERROR;
  12533. }
  12534. }
  12535. #endif
  12536. wc_HmacFree(&hmac);
  12537. printf(resultFmt, flag == 0 ? passed : failed);
  12538. #endif
  12539. return flag;
  12540. } /* END test_wc_Sha224HmacFinal */
  12541. /*
  12542. * Testing wc_HmacFinal() with SHA256
  12543. */
  12544. static int test_wc_Sha256HmacFinal (void)
  12545. {
  12546. int flag = 0;
  12547. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  12548. Hmac hmac;
  12549. byte hash[WC_SHA256_DIGEST_SIZE];
  12550. testVector a;
  12551. int ret;
  12552. const char* key;
  12553. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12554. "\x0b\x0b\x0b";
  12555. a.input = "Hi There";
  12556. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  12557. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  12558. "\xcf\xf7";
  12559. a.inLen = XSTRLEN(a.input);
  12560. a.outLen = XSTRLEN(a.output);
  12561. flag = 0;
  12562. printf(testingFmt, "wc_HmacFinal() with WC_SHA256");
  12563. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12564. if (ret != 0)
  12565. return ret;
  12566. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key, (word32)XSTRLEN(key));
  12567. if (ret != 0) {
  12568. flag = ret;
  12569. }
  12570. if (!flag) {
  12571. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12572. if (ret != 0) {
  12573. flag = ret;
  12574. }
  12575. }
  12576. if (!flag) {
  12577. ret = wc_HmacFinal(&hmac, hash);
  12578. if (ret != 0) {
  12579. flag = ret;
  12580. }
  12581. }
  12582. if (!flag) {
  12583. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  12584. flag = WOLFSSL_FATAL_ERROR;
  12585. }
  12586. }
  12587. /* Try bad parameters. */
  12588. if (!flag) {
  12589. ret = wc_HmacFinal(NULL, hash);
  12590. if (ret != BAD_FUNC_ARG) {
  12591. flag = WOLFSSL_FATAL_ERROR;
  12592. }
  12593. }
  12594. #ifndef HAVE_FIPS
  12595. if (!flag) {
  12596. ret = wc_HmacFinal(&hmac, NULL);
  12597. if (ret != BAD_FUNC_ARG) {
  12598. flag = WOLFSSL_FATAL_ERROR;
  12599. }
  12600. }
  12601. #endif
  12602. wc_HmacFree(&hmac);
  12603. printf(resultFmt, flag == 0 ? passed : failed);
  12604. #endif
  12605. return flag;
  12606. } /* END test_wc_Sha256HmacFinal */
  12607. /*
  12608. * Testing wc_HmacFinal() with SHA384
  12609. */
  12610. static int test_wc_Sha384HmacFinal (void)
  12611. {
  12612. int flag = 0;
  12613. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  12614. Hmac hmac;
  12615. byte hash[WC_SHA384_DIGEST_SIZE];
  12616. testVector a;
  12617. int ret;
  12618. const char* key;
  12619. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12620. "\x0b\x0b\x0b";
  12621. a.input = "Hi There";
  12622. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  12623. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  12624. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  12625. "\xfa\x9c\xb6";
  12626. a.inLen = XSTRLEN(a.input);
  12627. a.outLen = XSTRLEN(a.output);
  12628. flag = 0;
  12629. printf(testingFmt, "wc_HmacFinal() with SHA384");
  12630. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12631. if (ret != 0)
  12632. return ret;
  12633. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key, (word32)XSTRLEN(key));
  12634. if (ret != 0) {
  12635. flag = ret;
  12636. }
  12637. if (!flag) {
  12638. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12639. if (ret != 0) {
  12640. flag = ret;
  12641. }
  12642. }
  12643. if (!flag) {
  12644. ret = wc_HmacFinal(&hmac, hash);
  12645. if (ret != 0) {
  12646. flag = ret;
  12647. }
  12648. }
  12649. if (!flag) {
  12650. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  12651. flag = WOLFSSL_FATAL_ERROR;
  12652. }
  12653. }
  12654. /* Try bad parameters. */
  12655. if (!flag) {
  12656. ret = wc_HmacFinal(NULL, hash);
  12657. if (ret != BAD_FUNC_ARG) {
  12658. flag = WOLFSSL_FATAL_ERROR;
  12659. }
  12660. }
  12661. #ifndef HAVE_FIPS
  12662. if (!flag) {
  12663. ret = wc_HmacFinal(&hmac, NULL);
  12664. if (ret != BAD_FUNC_ARG) {
  12665. flag = WOLFSSL_FATAL_ERROR;
  12666. }
  12667. }
  12668. #endif
  12669. wc_HmacFree(&hmac);
  12670. printf(resultFmt, flag == 0 ? passed : failed);
  12671. #endif
  12672. return flag;
  12673. } /* END test_wc_Sha384HmacFinal */
  12674. /*
  12675. * Testing wc_InitCmac()
  12676. */
  12677. static int test_wc_InitCmac (void)
  12678. {
  12679. int ret = 0;
  12680. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  12681. Cmac cmac1, cmac2, cmac3;
  12682. /* AES 128 key. */
  12683. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  12684. "\x09\x10\x11\x12\x13\x14\x15\x16";
  12685. /* AES 192 key. */
  12686. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  12687. "\x09\x01\x11\x12\x13\x14\x15\x16"
  12688. "\x01\x02\x03\x04\x05\x06\x07\x08";
  12689. /* AES 256 key. */
  12690. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  12691. "\x09\x01\x11\x12\x13\x14\x15\x16"
  12692. "\x01\x02\x03\x04\x05\x06\x07\x08"
  12693. "\x09\x01\x11\x12\x13\x14\x15\x16";
  12694. word32 key1Sz = (word32)sizeof(key1) - 1;
  12695. word32 key2Sz = (word32)sizeof(key2) - 1;
  12696. word32 key3Sz = (word32)sizeof(key3) - 1;
  12697. int type = WC_CMAC_AES;
  12698. printf(testingFmt, "wc_InitCmac()");
  12699. #ifdef WOLFSSL_AES_128
  12700. ret = wc_InitCmac(&cmac1, key1, key1Sz, type, NULL);
  12701. #endif
  12702. #ifdef WOLFSSL_AES_192
  12703. if (ret == 0) {
  12704. wc_AesFree(&cmac1.aes);
  12705. ret = wc_InitCmac(&cmac2, key2, key2Sz, type, NULL);
  12706. }
  12707. #endif
  12708. #ifdef WOLFSSL_AES_256
  12709. if (ret == 0) {
  12710. wc_AesFree(&cmac2.aes);
  12711. ret = wc_InitCmac(&cmac3, key3, key3Sz, type, NULL);
  12712. }
  12713. #endif
  12714. /* Test bad args. */
  12715. if (ret == 0) {
  12716. wc_AesFree(&cmac3.aes);
  12717. ret = wc_InitCmac(NULL, key3, key3Sz, type, NULL);
  12718. if (ret == BAD_FUNC_ARG) {
  12719. ret = wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL);
  12720. }
  12721. if (ret == BAD_FUNC_ARG) {
  12722. ret = wc_InitCmac(&cmac3, key3, 0, type, NULL);
  12723. }
  12724. if (ret == BAD_FUNC_ARG) {
  12725. ret = wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL);
  12726. }
  12727. if (ret == BAD_FUNC_ARG) {
  12728. ret = 0;
  12729. } else {
  12730. ret = WOLFSSL_FATAL_ERROR;
  12731. }
  12732. }
  12733. (void)key1;
  12734. (void)key1Sz;
  12735. (void)key2;
  12736. (void)key2Sz;
  12737. (void)cmac1;
  12738. (void)cmac2;
  12739. printf(resultFmt, ret == 0 ? passed : failed);
  12740. #endif
  12741. return ret;
  12742. } /* END test_wc_InitCmac */
  12743. /*
  12744. * Testing wc_CmacUpdate()
  12745. */
  12746. static int test_wc_CmacUpdate (void)
  12747. {
  12748. int ret = 0;
  12749. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  12750. Cmac cmac;
  12751. byte key[] =
  12752. {
  12753. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  12754. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  12755. };
  12756. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  12757. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  12758. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  12759. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  12760. "\xf4";
  12761. word32 inSz = (word32)sizeof(in) - 1;
  12762. word32 keySz = (word32)sizeof(key);
  12763. int type = WC_CMAC_AES;
  12764. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  12765. if (ret != 0) {
  12766. return ret;
  12767. }
  12768. printf(testingFmt, "wc_CmacUpdate()");
  12769. ret = wc_CmacUpdate(&cmac, in, inSz);
  12770. /* Test bad args. */
  12771. if (ret == 0) {
  12772. ret = wc_CmacUpdate(NULL, in, inSz);
  12773. if (ret == BAD_FUNC_ARG) {
  12774. ret = wc_CmacUpdate(&cmac, NULL, 30);
  12775. }
  12776. if (ret == BAD_FUNC_ARG) {
  12777. ret = 0;
  12778. } else if (ret == 0) {
  12779. ret = WOLFSSL_FATAL_ERROR;
  12780. }
  12781. wc_AesFree(&cmac.aes);
  12782. }
  12783. printf(resultFmt, ret == 0 ? passed : failed);
  12784. #endif
  12785. return ret;
  12786. } /* END test_wc_CmacUpdate */
  12787. /*
  12788. * Testing wc_CmacFinal()
  12789. */
  12790. static int test_wc_CmacFinal (void)
  12791. {
  12792. int ret = 0;
  12793. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  12794. Cmac cmac;
  12795. byte key[] =
  12796. {
  12797. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  12798. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  12799. };
  12800. byte msg[] =
  12801. {
  12802. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  12803. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  12804. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  12805. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  12806. 0xf4
  12807. };
  12808. /* Test vectors from CMACGenAES128.rsp from
  12809. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  12810. * Per RFC4493 truncation of lsb is possible.
  12811. */
  12812. byte expMac[] =
  12813. {
  12814. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  12815. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  12816. };
  12817. byte mac[AES_BLOCK_SIZE];
  12818. word32 msgSz = (word32)sizeof(msg);
  12819. word32 keySz = (word32)sizeof(key);
  12820. word32 macSz = sizeof(mac);
  12821. word32 badMacSz = 17;
  12822. int expMacSz = sizeof(expMac);
  12823. int type = WC_CMAC_AES;
  12824. XMEMSET(mac, 0, macSz);
  12825. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  12826. if (ret != 0) {
  12827. return ret;
  12828. }
  12829. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  12830. printf(testingFmt, "wc_CmacFinal()");
  12831. if (ret == 0) {
  12832. ret = wc_CmacFinal(&cmac, mac, &macSz);
  12833. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  12834. ret = WOLFSSL_FATAL_ERROR;
  12835. }
  12836. /* Pass in bad args. */
  12837. if (ret == 0) {
  12838. ret = wc_CmacFinal(NULL, mac, &macSz);
  12839. if (ret == BAD_FUNC_ARG) {
  12840. ret = wc_CmacFinal(&cmac, NULL, &macSz);
  12841. }
  12842. if (ret == BAD_FUNC_ARG) {
  12843. ret = wc_CmacFinal(&cmac, mac, &badMacSz);
  12844. if (ret == BUFFER_E) {
  12845. ret = 0;
  12846. }
  12847. } else if (ret == 0) {
  12848. ret = WOLFSSL_FATAL_ERROR;
  12849. }
  12850. }
  12851. }
  12852. printf(resultFmt, ret == 0 ? passed : failed);
  12853. #endif
  12854. return ret;
  12855. } /* END test_wc_CmacFinal */
  12856. /*
  12857. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  12858. */
  12859. static int test_wc_AesCmacGenerate (void)
  12860. {
  12861. int ret = 0;
  12862. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  12863. Cmac cmac;
  12864. byte key[] =
  12865. {
  12866. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  12867. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  12868. };
  12869. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  12870. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  12871. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  12872. "\x3d\x32\x65\x4c\x66\x23\xc5";
  12873. byte mac[AES_BLOCK_SIZE];
  12874. word32 keySz = sizeof(key);
  12875. word32 macSz = sizeof(mac);
  12876. word32 msgSz = sizeof(msg) - 1;
  12877. word32 expMacSz = sizeof(expMac) - 1;
  12878. int type = WC_CMAC_AES;
  12879. XMEMSET(mac, 0, macSz);
  12880. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  12881. if (ret != 0) {
  12882. return ret;
  12883. }
  12884. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  12885. if (ret != 0) {
  12886. return ret;
  12887. } else {
  12888. wc_AesFree(&cmac.aes);
  12889. }
  12890. printf(testingFmt, "wc_AesCmacGenerate()");
  12891. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz);
  12892. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  12893. ret = WOLFSSL_FATAL_ERROR;
  12894. }
  12895. /* Pass in bad args. */
  12896. if (ret == 0) {
  12897. ret = wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz);
  12898. if (ret == BAD_FUNC_ARG) {
  12899. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz);
  12900. }
  12901. if (ret == BAD_FUNC_ARG) {
  12902. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0);
  12903. }
  12904. if (ret == BAD_FUNC_ARG) {
  12905. ret = wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz);
  12906. }
  12907. if (ret == BAD_FUNC_ARG) {
  12908. ret = 0;
  12909. } else if (ret == 0) {
  12910. ret = WOLFSSL_FATAL_ERROR;
  12911. }
  12912. }
  12913. printf(resultFmt, ret == 0 ? passed : failed);
  12914. if (ret == 0) {
  12915. printf(testingFmt, "wc_AesCmacVerify()");
  12916. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz);
  12917. /* Test bad args. */
  12918. if (ret == 0) {
  12919. ret = wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz);
  12920. if (ret == BAD_FUNC_ARG) {
  12921. ret = wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz);
  12922. }
  12923. if (ret == BAD_FUNC_ARG) {
  12924. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz);
  12925. }
  12926. if (ret == BAD_FUNC_ARG) {
  12927. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0);
  12928. }
  12929. if (ret == BAD_FUNC_ARG) {
  12930. ret = wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz);
  12931. }
  12932. if (ret == BAD_FUNC_ARG) {
  12933. ret = 0;
  12934. } else if (ret == 0) {
  12935. ret = WOLFSSL_FATAL_ERROR;
  12936. }
  12937. }
  12938. printf(resultFmt, ret == 0 ? passed : failed);
  12939. }
  12940. #endif
  12941. return ret;
  12942. } /* END test_wc_AesCmacGenerate */
  12943. /*
  12944. * Testing streaming AES-GCM API.
  12945. */
  12946. static int test_wc_AesGcmStream (void)
  12947. {
  12948. int ret = 0;
  12949. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  12950. defined(WOLFSSL_AESGCM_STREAM)
  12951. int i;
  12952. WC_RNG rng[1];
  12953. Aes aesEnc[1];
  12954. Aes aesDec[1];
  12955. byte tag[AES_BLOCK_SIZE];
  12956. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  12957. byte out[AES_BLOCK_SIZE * 3 + 2];
  12958. byte plain[AES_BLOCK_SIZE * 3 + 2];
  12959. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  12960. byte key[AES_128_KEY_SIZE] = { 0, };
  12961. byte iv[AES_IV_SIZE] = { 1, };
  12962. byte ivOut[AES_IV_SIZE];
  12963. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  12964. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  12965. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  12966. };
  12967. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  12968. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  12969. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  12970. };
  12971. static const byte expTag[AES_BLOCK_SIZE] = {
  12972. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  12973. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  12974. };
  12975. /* Create a random for generating IV/nonce. */
  12976. AssertIntEQ(wc_InitRng(rng), 0);
  12977. /* Initialize data structures. */
  12978. AssertIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  12979. AssertIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  12980. /* BadParameters to streaming init. */
  12981. AssertIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  12982. AssertIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  12983. AssertIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  12984. BAD_FUNC_ARG);
  12985. AssertIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  12986. BAD_FUNC_ARG);
  12987. /* Bad parameters to encrypt update. */
  12988. AssertIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  12989. BAD_FUNC_ARG);
  12990. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  12991. BAD_FUNC_ARG);
  12992. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  12993. BAD_FUNC_ARG);
  12994. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  12995. BAD_FUNC_ARG);
  12996. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  12997. BAD_FUNC_ARG);
  12998. /* Bad parameters to decrypt update. */
  12999. AssertIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  13000. BAD_FUNC_ARG);
  13001. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  13002. BAD_FUNC_ARG);
  13003. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  13004. BAD_FUNC_ARG);
  13005. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  13006. BAD_FUNC_ARG);
  13007. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  13008. BAD_FUNC_ARG);
  13009. /* Bad parameters to encrypt final. */
  13010. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  13011. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  13012. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  13013. BAD_FUNC_ARG);
  13014. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  13015. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  13016. BAD_FUNC_ARG);
  13017. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  13018. BAD_FUNC_ARG);
  13019. /* Bad parameters to decrypt final. */
  13020. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  13021. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  13022. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  13023. BAD_FUNC_ARG);
  13024. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  13025. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  13026. BAD_FUNC_ARG);
  13027. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  13028. BAD_FUNC_ARG);
  13029. /* Check calling final before setting key fails. */
  13030. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  13031. AssertIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  13032. /* Check calling update before setting key else fails. */
  13033. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  13034. MISSING_KEY);
  13035. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  13036. MISSING_KEY);
  13037. /* Set key but not IV. */
  13038. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  13039. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  13040. /* Check calling final before setting IV fails. */
  13041. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  13042. AssertIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  13043. /* Check calling update before setting IV else fails. */
  13044. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  13045. MISSING_IV);
  13046. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  13047. MISSING_IV);
  13048. /* Set IV using fixed part IV and external IV APIs. */
  13049. AssertIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  13050. rng), 0);
  13051. AssertIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  13052. GCM_NONCE_MID_SZ), 0);
  13053. AssertIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  13054. AssertIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  13055. /* Encrypt and decrypt data. */
  13056. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  13057. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  13058. AssertIntEQ(XMEMCMP(plain, in, 1), 0);
  13059. /* Finalize and check tag matches. */
  13060. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13061. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13062. /* Set key and IV through streaming init API. */
  13063. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13064. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13065. /* Encrypt/decrypt one block and AAD of one block. */
  13066. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  13067. AES_BLOCK_SIZE), 0);
  13068. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  13069. AES_BLOCK_SIZE), 0);
  13070. AssertIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  13071. /* Finalize and check tag matches. */
  13072. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13073. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13074. /* Set key and IV through streaming init API. */
  13075. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13076. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13077. /* No data to encrypt/decrypt one byte of AAD. */
  13078. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  13079. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  13080. /* Finalize and check tag matches. */
  13081. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13082. AssertIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  13083. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13084. /* Set key and IV through streaming init API. */
  13085. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13086. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13087. /* Encrypt/decrypt one byte and no AAD. */
  13088. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  13089. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  13090. AssertIntEQ(XMEMCMP(plain, in, 1), 0);
  13091. /* Finalize and check tag matches. */
  13092. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13093. AssertIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  13094. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13095. /* Set key and IV through streaming init API. */
  13096. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13097. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13098. /* Encryption AES is one byte at a time */
  13099. for (i = 0; i < (int)sizeof(aad); i++) {
  13100. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  13101. 0);
  13102. }
  13103. for (i = 0; i < (int)sizeof(in); i++) {
  13104. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  13105. 0);
  13106. }
  13107. /* Decryption AES is two bytes at a time */
  13108. for (i = 0; i < (int)sizeof(aad); i += 2) {
  13109. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  13110. 0);
  13111. }
  13112. for (i = 0; i < (int)sizeof(aad); i += 2) {
  13113. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  13114. 0), 0);
  13115. }
  13116. AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  13117. /* Finalize and check tag matches. */
  13118. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13119. AssertIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  13120. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13121. /* Check streaming encryption can be decrypted with one shot. */
  13122. AssertIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  13123. AssertIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  13124. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  13125. AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  13126. wc_AesFree(aesEnc);
  13127. wc_AesFree(aesDec);
  13128. wc_FreeRng(rng);
  13129. #endif
  13130. return ret;
  13131. } /* END test_wc_AesGcmStream */
  13132. /*
  13133. * unit test for wc_Des3_SetIV()
  13134. */
  13135. static int test_wc_Des3_SetIV (void)
  13136. {
  13137. int ret = 0;
  13138. #ifndef NO_DES3
  13139. Des3 des;
  13140. const byte key[] =
  13141. {
  13142. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  13143. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  13144. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  13145. };
  13146. const byte iv[] =
  13147. {
  13148. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  13149. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  13150. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  13151. };
  13152. printf(testingFmt, "wc_Des3_SetIV()");
  13153. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  13154. if (ret != 0)
  13155. return ret;
  13156. /* DES_ENCRYPTION or DES_DECRYPTION */
  13157. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  13158. if (ret == 0) {
  13159. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  13160. ret = WOLFSSL_FATAL_ERROR;
  13161. }
  13162. }
  13163. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  13164. /* Test explicitly wc_Des3_SetIV() */
  13165. if (ret == 0) {
  13166. ret = wc_Des3_SetIV(NULL, iv);
  13167. if (ret == BAD_FUNC_ARG) {
  13168. ret = wc_Des3_SetIV(&des, NULL);
  13169. } else if (ret == 0) {
  13170. ret = WOLFSSL_FATAL_ERROR;
  13171. }
  13172. }
  13173. #endif
  13174. wc_Des3Free(&des);
  13175. printf(resultFmt, ret == 0 ? passed : failed);
  13176. #endif
  13177. return ret;
  13178. } /* END test_wc_Des3_SetIV */
  13179. /*
  13180. * unit test for wc_Des3_SetKey()
  13181. */
  13182. static int test_wc_Des3_SetKey (void)
  13183. {
  13184. int ret = 0;
  13185. #ifndef NO_DES3
  13186. Des3 des;
  13187. const byte key[] =
  13188. {
  13189. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  13190. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  13191. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  13192. };
  13193. const byte iv[] =
  13194. {
  13195. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  13196. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  13197. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  13198. };
  13199. printf(testingFmt, "wc_Des3_SetKey()");
  13200. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  13201. if (ret != 0)
  13202. return ret;
  13203. /* DES_ENCRYPTION or DES_DECRYPTION */
  13204. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  13205. if (ret == 0) {
  13206. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  13207. ret = WOLFSSL_FATAL_ERROR;
  13208. }
  13209. }
  13210. /* Test bad args. */
  13211. if (ret == 0) {
  13212. ret = wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION);
  13213. if (ret == BAD_FUNC_ARG) {
  13214. ret = wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION);
  13215. }
  13216. if (ret == BAD_FUNC_ARG) {
  13217. ret = wc_Des3_SetKey(&des, key, iv, -1);
  13218. }
  13219. if (ret == BAD_FUNC_ARG) {
  13220. /* Default case. Should return 0. */
  13221. ret = wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION);
  13222. }
  13223. } /* END if ret != 0 */
  13224. wc_Des3Free(&des);
  13225. printf(resultFmt, ret == 0 ? passed : failed);
  13226. #endif
  13227. return ret;
  13228. } /* END test_wc_Des3_SetKey */
  13229. /*
  13230. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  13231. */
  13232. static int test_wc_Des3_CbcEncryptDecrypt (void)
  13233. {
  13234. int ret = 0;
  13235. #ifndef NO_DES3
  13236. Des3 des;
  13237. byte cipher[24];
  13238. byte plain[24];
  13239. const byte key[] =
  13240. {
  13241. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  13242. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  13243. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  13244. };
  13245. const byte iv[] =
  13246. {
  13247. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  13248. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  13249. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  13250. };
  13251. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  13252. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  13253. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  13254. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  13255. };
  13256. printf(testingFmt, "wc_Des3_CbcEncrypt()");
  13257. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  13258. if (ret != 0)
  13259. return ret;
  13260. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  13261. if (ret == 0) {
  13262. ret = wc_Des3_CbcEncrypt(&des, cipher, vector, 24);
  13263. if (ret == 0) {
  13264. ret = wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION);
  13265. }
  13266. if (ret == 0) {
  13267. ret = wc_Des3_CbcDecrypt(&des, plain, cipher, 24);
  13268. }
  13269. }
  13270. if (ret == 0) {
  13271. if (XMEMCMP(plain, vector, 24) != 0) {
  13272. ret = WOLFSSL_FATAL_ERROR;
  13273. }
  13274. }
  13275. /* Pass in bad args. */
  13276. if (ret == 0) {
  13277. ret = wc_Des3_CbcEncrypt(NULL, cipher, vector, 24);
  13278. if (ret == BAD_FUNC_ARG) {
  13279. ret = wc_Des3_CbcEncrypt(&des, NULL, vector, 24);
  13280. }
  13281. if (ret == BAD_FUNC_ARG) {
  13282. ret = wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector));
  13283. }
  13284. if (ret != BAD_FUNC_ARG) {
  13285. ret = WOLFSSL_FATAL_ERROR;
  13286. } else {
  13287. ret = 0;
  13288. }
  13289. }
  13290. if (ret == 0) {
  13291. ret = wc_Des3_CbcDecrypt(NULL, plain, cipher, 24);
  13292. if (ret == BAD_FUNC_ARG) {
  13293. ret = wc_Des3_CbcDecrypt(&des, NULL, cipher, 24);
  13294. }
  13295. if (ret == BAD_FUNC_ARG) {
  13296. ret = wc_Des3_CbcDecrypt(&des, plain, NULL, 24);
  13297. }
  13298. if (ret != BAD_FUNC_ARG) {
  13299. ret = WOLFSSL_FATAL_ERROR;
  13300. } else {
  13301. ret = 0;
  13302. }
  13303. }
  13304. wc_Des3Free(&des);
  13305. printf(resultFmt, ret == 0 ? passed : failed);
  13306. #endif
  13307. return ret;
  13308. } /* END wc_Des3_CbcEncrypt */
  13309. /*
  13310. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  13311. */
  13312. static int test_wc_Des3_CbcEncryptDecryptWithKey (void)
  13313. {
  13314. int ret = 0;
  13315. #ifndef NO_DES3
  13316. word32 vectorSz, cipherSz;
  13317. byte cipher[24];
  13318. byte plain[24];
  13319. byte vector[] = /* Now is the time for all w/o trailing 0 */
  13320. {
  13321. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  13322. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  13323. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  13324. };
  13325. byte key[] =
  13326. {
  13327. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  13328. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  13329. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  13330. };
  13331. byte iv[] =
  13332. {
  13333. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  13334. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  13335. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  13336. };
  13337. vectorSz = sizeof(byte) * 24;
  13338. cipherSz = sizeof(byte) * 24;
  13339. printf(testingFmt, "wc_Des3_CbcEncryptWithKey()");
  13340. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv);
  13341. if (ret == 0) {
  13342. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv);
  13343. if (ret == 0) {
  13344. if (XMEMCMP(plain, vector, 24) != 0) {
  13345. ret = WOLFSSL_FATAL_ERROR;
  13346. }
  13347. }
  13348. }
  13349. /* pass in bad args. */
  13350. if (ret == 0) {
  13351. ret = wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv);
  13352. if (ret == BAD_FUNC_ARG) {
  13353. ret = wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv);
  13354. }
  13355. if (ret == BAD_FUNC_ARG) {
  13356. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv);
  13357. }
  13358. if (ret == BAD_FUNC_ARG) {
  13359. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz,
  13360. key, NULL);
  13361. } else {
  13362. /* Return code catch. */
  13363. ret = WOLFSSL_FAILURE;
  13364. }
  13365. }
  13366. if (ret == 0) {
  13367. ret = wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv);
  13368. if (ret == BAD_FUNC_ARG) {
  13369. ret = wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv);
  13370. }
  13371. if (ret == BAD_FUNC_ARG) {
  13372. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv);
  13373. }
  13374. if (ret == BAD_FUNC_ARG) {
  13375. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL);
  13376. } else {
  13377. ret = WOLFSSL_FAILURE;
  13378. }
  13379. }
  13380. printf(resultFmt, ret == 0 ? passed : failed);
  13381. #endif
  13382. return ret;
  13383. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  13384. /*
  13385. * Unit test for wc_Des3_EcbEncrypt
  13386. */
  13387. static int test_wc_Des3_EcbEncrypt (void)
  13388. {
  13389. int ret = 0;
  13390. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  13391. Des3 des;
  13392. byte cipher[24];
  13393. word32 cipherSz = sizeof(cipher);
  13394. const byte key[] =
  13395. {
  13396. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  13397. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  13398. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  13399. };
  13400. const byte iv[] =
  13401. {
  13402. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  13403. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  13404. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  13405. };
  13406. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  13407. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  13408. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  13409. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  13410. };
  13411. printf(testingFmt, "wc_Des3_EcbEncrypt()");
  13412. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  13413. if (ret != 0) {
  13414. return ret;
  13415. }
  13416. if (ret == 0 ) {
  13417. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  13418. }
  13419. /* Bad Cases */
  13420. if (ret == 0) {
  13421. ret = wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz);
  13422. if (ret == BAD_FUNC_ARG) {
  13423. ret = 0;
  13424. }
  13425. }
  13426. if (ret == 0) {
  13427. ret = wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz);
  13428. if (ret == BAD_FUNC_ARG) {
  13429. ret = 0;
  13430. }
  13431. }
  13432. if (ret == 0) {
  13433. ret = wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz);
  13434. if (ret == BAD_FUNC_ARG) {
  13435. ret = 0;
  13436. }
  13437. }
  13438. if (ret == 0) {
  13439. ret = wc_Des3_EcbEncrypt(&des, cipher, vector, 0);
  13440. if (ret == BAD_FUNC_ARG) {
  13441. ret = 0;
  13442. }
  13443. }
  13444. if (ret == 0) {
  13445. ret = wc_Des3_EcbEncrypt(NULL, 0, NULL, 0);
  13446. if (ret == BAD_FUNC_ARG) {
  13447. ret = 0;
  13448. }
  13449. }
  13450. /* Good Cases */
  13451. if (ret == 0) {
  13452. ret = wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz);
  13453. }
  13454. wc_Des3Free(&des);
  13455. printf(resultFmt, ret == 0 ? passed : failed);
  13456. #endif
  13457. return ret;
  13458. } /* END test_wc_Des3_EcbEncrypt */
  13459. /*
  13460. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  13461. */
  13462. static int test_wc_Chacha_SetKey (void)
  13463. {
  13464. int ret = 0;
  13465. #ifdef HAVE_CHACHA
  13466. ChaCha ctx;
  13467. const byte key[] =
  13468. {
  13469. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13470. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13471. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13472. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  13473. };
  13474. byte cipher[128];
  13475. printf(testingFmt, "wc_Chacha_SetKey()");
  13476. ret = wc_Chacha_SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  13477. /* Test bad args. */
  13478. if (ret == 0) {
  13479. ret = wc_Chacha_SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  13480. if (ret == BAD_FUNC_ARG) {
  13481. ret = wc_Chacha_SetKey(&ctx, key, 18);
  13482. }
  13483. if (ret == BAD_FUNC_ARG) {
  13484. ret = 0;
  13485. } else {
  13486. ret = WOLFSSL_FATAL_ERROR;
  13487. }
  13488. }
  13489. printf(resultFmt, ret == 0 ? passed : failed);
  13490. if (ret != 0) {
  13491. return ret;
  13492. }
  13493. printf(testingFmt, "wc_Chacha_SetIV");
  13494. ret = wc_Chacha_SetIV(&ctx, cipher, 0);
  13495. if (ret == 0) {
  13496. /* Test bad args. */
  13497. ret = wc_Chacha_SetIV(NULL, cipher, 0);
  13498. if (ret == BAD_FUNC_ARG) {
  13499. ret = 0;
  13500. } else {
  13501. ret = WOLFSSL_FAILURE;
  13502. }
  13503. }
  13504. printf(resultFmt, ret == 0 ? passed : failed);
  13505. #endif
  13506. return ret;
  13507. } /* END test_wc_Chacha_SetKey */
  13508. /*
  13509. * unit test for wc_Poly1305SetKey()
  13510. */
  13511. static int test_wc_Poly1305SetKey(void)
  13512. {
  13513. int ret = 0;
  13514. #ifdef HAVE_POLY1305
  13515. Poly1305 ctx;
  13516. const byte key[] =
  13517. {
  13518. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13519. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13520. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13521. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  13522. };
  13523. printf(testingFmt, "wc_Poly1305_SetKey()");
  13524. ret = wc_Poly1305SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  13525. /* Test bad args. */
  13526. if (ret == 0) {
  13527. ret = wc_Poly1305SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  13528. if(ret == BAD_FUNC_ARG) {
  13529. ret = wc_Poly1305SetKey(&ctx, NULL, (word32)(sizeof(key)/sizeof(byte)));
  13530. }
  13531. if (ret == BAD_FUNC_ARG) {
  13532. ret = wc_Poly1305SetKey(&ctx, key, 18);
  13533. }
  13534. if (ret == BAD_FUNC_ARG) {
  13535. ret = 0;
  13536. } else {
  13537. ret = WOLFSSL_FATAL_ERROR;
  13538. }
  13539. }
  13540. printf(resultFmt, ret == 0 ? passed : failed);
  13541. #endif
  13542. return ret;
  13543. } /* END test_wc_Poly1305_SetKey() */
  13544. /*
  13545. * Testing wc_Chacha_Process()
  13546. */
  13547. static int test_wc_Chacha_Process (void)
  13548. {
  13549. int ret = 0;
  13550. #ifdef HAVE_CHACHA
  13551. ChaCha enc, dec;
  13552. byte cipher[128];
  13553. byte plain[128];
  13554. const byte key[] =
  13555. {
  13556. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13557. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13558. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13559. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  13560. };
  13561. const char* input = "Everybody gets Friday off.";
  13562. word32 keySz = sizeof(key)/sizeof(byte);
  13563. unsigned long int inlen = XSTRLEN(input);
  13564. /*Initialize stack varialbes.*/
  13565. XMEMSET(cipher, 0, 128);
  13566. XMEMSET(plain, 0, 128);
  13567. printf(testingFmt, "wc_Chacha_Process()");
  13568. ret = wc_Chacha_SetKey(&enc, key, keySz);
  13569. AssertIntEQ(ret, 0);
  13570. ret = wc_Chacha_SetKey(&dec, key, keySz);
  13571. AssertIntEQ(ret, 0);
  13572. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  13573. AssertIntEQ(ret, 0);
  13574. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  13575. AssertIntEQ(ret, 0);
  13576. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen);
  13577. AssertIntEQ(ret, 0);
  13578. ret = wc_Chacha_Process(&dec, plain, cipher, (word32)inlen);
  13579. AssertIntEQ(ret, 0);
  13580. ret = XMEMCMP(input, plain, (int)inlen);
  13581. AssertIntEQ(ret, 0);
  13582. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  13583. /* test checking and using leftovers, currently just in C code */
  13584. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  13585. AssertIntEQ(ret, 0);
  13586. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  13587. AssertIntEQ(ret, 0);
  13588. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen - 2);
  13589. AssertIntEQ(ret, 0);
  13590. ret = wc_Chacha_Process(&enc, cipher + (inlen - 2),
  13591. (byte*)input + (inlen - 2), 2);
  13592. AssertIntEQ(ret, 0);
  13593. ret = wc_Chacha_Process(&dec, plain, (byte*)cipher, (word32)inlen - 2);
  13594. AssertIntEQ(ret, 0);
  13595. ret = wc_Chacha_Process(&dec, cipher + (inlen - 2),
  13596. (byte*)input + (inlen - 2), 2);
  13597. AssertIntEQ(ret, 0);
  13598. ret = XMEMCMP(input, plain, (int)inlen);
  13599. AssertIntEQ(ret, 0);
  13600. /* check edge cases with counter increment */
  13601. {
  13602. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  13603. const byte expected[] = {
  13604. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  13605. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  13606. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  13607. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  13608. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  13609. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  13610. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  13611. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  13612. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  13613. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  13614. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  13615. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  13616. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  13617. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  13618. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  13619. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  13620. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  13621. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  13622. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  13623. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  13624. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  13625. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  13626. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  13627. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  13628. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  13629. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  13630. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  13631. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  13632. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  13633. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  13634. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  13635. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  13636. };
  13637. const byte iv2[] = {
  13638. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  13639. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  13640. };
  13641. byte input2[256];
  13642. int i;
  13643. for (i = 0; i < 256; i++)
  13644. input2[i] = i;
  13645. ret = wc_Chacha_SetIV(&enc, iv2, 0);
  13646. AssertIntEQ(ret, 0);
  13647. ret = wc_Chacha_Process(&enc, cipher, input2, 64);
  13648. AssertIntEQ(ret, 0);
  13649. AssertIntEQ(XMEMCMP(expected, cipher, 64), 0);
  13650. ret = wc_Chacha_Process(&enc, cipher, input2 + 64, 128);
  13651. AssertIntEQ(ret, 0);
  13652. AssertIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  13653. /* partial */
  13654. ret = wc_Chacha_Process(&enc, cipher, input2 + 192, 32);
  13655. AssertIntEQ(ret, 0);
  13656. AssertIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  13657. ret = wc_Chacha_Process(&enc, cipher, input2 + 224, 32);
  13658. AssertIntEQ(ret, 0);
  13659. AssertIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  13660. }
  13661. #endif
  13662. /* Test bad args. */
  13663. ret = wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen);
  13664. AssertIntEQ(ret, BAD_FUNC_ARG);
  13665. if (ret == BAD_FUNC_ARG) {
  13666. ret = 0;
  13667. }
  13668. printf(resultFmt, ret == 0 ? passed : failed);
  13669. #endif
  13670. return ret;
  13671. } /* END test_wc_Chacha_Process */
  13672. /*
  13673. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  13674. */
  13675. static int test_wc_ChaCha20Poly1305_aead (void)
  13676. {
  13677. int ret = 0;
  13678. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  13679. const byte key[] = {
  13680. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  13681. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  13682. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  13683. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  13684. };
  13685. const byte plaintext[] = {
  13686. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  13687. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  13688. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  13689. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  13690. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  13691. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  13692. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  13693. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  13694. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  13695. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  13696. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  13697. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  13698. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  13699. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  13700. 0x74, 0x2e
  13701. };
  13702. const byte iv[] = {
  13703. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  13704. 0x44, 0x45, 0x46, 0x47
  13705. };
  13706. const byte aad[] = { /* additional data */
  13707. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  13708. 0xc4, 0xc5, 0xc6, 0xc7
  13709. };
  13710. const byte cipher[] = { /* expected output from operation */
  13711. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  13712. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  13713. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  13714. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  13715. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  13716. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  13717. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  13718. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  13719. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  13720. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  13721. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  13722. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  13723. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  13724. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  13725. 0x61, 0x16
  13726. };
  13727. const byte authTag[] = { /* expected output from operation */
  13728. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  13729. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  13730. };
  13731. byte generatedCiphertext[272];
  13732. byte generatedPlaintext[272];
  13733. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  13734. /* Initialize stack variables. */
  13735. XMEMSET(generatedCiphertext, 0, 272);
  13736. XMEMSET(generatedPlaintext, 0, 272);
  13737. /* Test Encrypt */
  13738. printf(testingFmt, "wc_ChaCha20Poly1305_Encrypt()");
  13739. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), plaintext,
  13740. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  13741. AssertIntEQ(ret, 0);
  13742. ret = XMEMCMP(generatedCiphertext, cipher, sizeof(cipher)/sizeof(byte));
  13743. AssertIntEQ(ret, 0);
  13744. /* Test bad args. */
  13745. ret = wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad), plaintext,
  13746. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  13747. AssertIntEQ(ret, BAD_FUNC_ARG);
  13748. ret = wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  13749. plaintext, sizeof(plaintext),
  13750. generatedCiphertext, generatedAuthTag);
  13751. AssertIntEQ(ret, BAD_FUNC_ARG);
  13752. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  13753. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  13754. AssertIntEQ(ret, BAD_FUNC_ARG);
  13755. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  13756. NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  13757. AssertIntEQ(ret, BAD_FUNC_ARG);
  13758. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  13759. plaintext, sizeof(plaintext), NULL, generatedAuthTag);
  13760. AssertIntEQ(ret, BAD_FUNC_ARG);
  13761. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  13762. plaintext, sizeof(plaintext), generatedCiphertext, NULL);
  13763. if (ret == BAD_FUNC_ARG) {
  13764. ret = 0;
  13765. }
  13766. printf(resultFmt, ret == 0 ? passed : failed);
  13767. if (ret != 0) {
  13768. return ret;
  13769. }
  13770. printf(testingFmt, "wc_ChaCha20Poly1305_Decrypt()");
  13771. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  13772. sizeof(cipher), authTag, generatedPlaintext);
  13773. AssertIntEQ(ret, 0);
  13774. ret = XMEMCMP(generatedPlaintext, plaintext,
  13775. sizeof(plaintext)/sizeof(byte));
  13776. AssertIntEQ(ret, 0);
  13777. /* Test bad args. */
  13778. ret = wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  13779. sizeof(cipher), authTag, generatedPlaintext);
  13780. AssertIntEQ(ret, BAD_FUNC_ARG);
  13781. ret = wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  13782. cipher, sizeof(cipher), authTag, generatedPlaintext);
  13783. AssertIntEQ(ret, BAD_FUNC_ARG);
  13784. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  13785. sizeof(cipher), authTag, generatedPlaintext);
  13786. AssertIntEQ(ret, BAD_FUNC_ARG);
  13787. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  13788. sizeof(cipher), NULL, generatedPlaintext);
  13789. AssertIntEQ(ret, BAD_FUNC_ARG);
  13790. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  13791. sizeof(cipher), authTag, NULL);
  13792. AssertIntEQ(ret, BAD_FUNC_ARG);
  13793. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  13794. sizeof(cipher), authTag, generatedPlaintext);
  13795. AssertIntEQ(ret, BAD_FUNC_ARG);
  13796. if (ret == BAD_FUNC_ARG) {
  13797. ret = 0;
  13798. }
  13799. printf(resultFmt, ret == 0 ? passed : failed);
  13800. #endif
  13801. return ret;
  13802. } /* END test-wc_ChaCha20Poly1305_EncryptDecrypt */
  13803. /*
  13804. * Testing function for wc_Rc2SetKey().
  13805. */
  13806. static int test_wc_Rc2SetKey(void)
  13807. {
  13808. int ret = 0;
  13809. #ifdef WC_RC2
  13810. Rc2 rc2;
  13811. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  13812. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  13813. printf(testingFmt, "wc_Rc2SetKey()");
  13814. /* valid key and IV */
  13815. ret = wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  13816. iv, 40);
  13817. if (ret == 0) {
  13818. /* valid key, no IV */
  13819. ret = wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  13820. NULL, 40);
  13821. }
  13822. /* bad arguments */
  13823. if (ret == 0) {
  13824. /* null Rc2 struct */
  13825. ret = wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  13826. iv, 40);
  13827. if (ret == BAD_FUNC_ARG) {
  13828. ret = 0;
  13829. }
  13830. }
  13831. if (ret == 0) {
  13832. /* null key */
  13833. ret = wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  13834. iv, 40);
  13835. if (ret == BAD_FUNC_ARG) {
  13836. ret = 0;
  13837. }
  13838. }
  13839. if (ret == 0) {
  13840. /* key size == 0 */
  13841. ret = wc_Rc2SetKey(&rc2, key40, 0, iv, 40);
  13842. if (ret == WC_KEY_SIZE_E) {
  13843. ret = 0;
  13844. }
  13845. }
  13846. if (ret == 0) {
  13847. /* key size > 128 */
  13848. ret = wc_Rc2SetKey(&rc2, key40, 129, iv, 40);
  13849. if (ret == WC_KEY_SIZE_E) {
  13850. ret = 0;
  13851. }
  13852. }
  13853. if (ret == 0) {
  13854. /* effective bits == 0 */
  13855. ret = wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  13856. iv, 0);
  13857. if (ret == WC_KEY_SIZE_E) {
  13858. ret = 0;
  13859. }
  13860. }
  13861. if (ret == 0) {
  13862. /* effective bits > 1024 */
  13863. ret = wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  13864. iv, 1025);
  13865. if (ret == WC_KEY_SIZE_E) {
  13866. ret = 0;
  13867. }
  13868. }
  13869. printf(resultFmt, ret == 0 ? passed : failed);
  13870. #endif
  13871. return ret;
  13872. } /* END test_wc_Rc2SetKey */
  13873. /*
  13874. * Testing function for wc_Rc2SetIV().
  13875. */
  13876. static int test_wc_Rc2SetIV(void)
  13877. {
  13878. int ret = 0;
  13879. #ifdef WC_RC2
  13880. Rc2 rc2;
  13881. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  13882. printf(testingFmt, "wc_Rc2SetIV()");
  13883. /* valid IV */
  13884. ret = wc_Rc2SetIV(&rc2, iv);
  13885. if (ret == 0) {
  13886. /* valid NULL IV */
  13887. ret = wc_Rc2SetIV(&rc2, NULL);
  13888. }
  13889. /* bad arguments */
  13890. if (ret == 0) {
  13891. ret = wc_Rc2SetIV(NULL, iv);
  13892. if (ret == BAD_FUNC_ARG) {
  13893. ret = 0;
  13894. }
  13895. }
  13896. printf(resultFmt, ret == 0 ? passed : failed);
  13897. #endif
  13898. return ret;
  13899. } /* END test_wc_Rc2SetKey */
  13900. /*
  13901. * Testing function for wc_Rc2EcbEncrypt().
  13902. */
  13903. static int test_wc_Rc2EcbEncryptDecrypt(void)
  13904. {
  13905. int ret = 0;
  13906. #ifdef WC_RC2
  13907. Rc2 rc2;
  13908. int effectiveKeyBits = 63;
  13909. byte cipher[RC2_BLOCK_SIZE];
  13910. byte plain[RC2_BLOCK_SIZE];
  13911. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  13912. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  13913. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  13914. printf(testingFmt, "wc_Rc2EcbEncryptDecrypt()");
  13915. XMEMSET(cipher, 0, sizeof(cipher));
  13916. XMEMSET(plain, 0, sizeof(plain));
  13917. ret = wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  13918. NULL, effectiveKeyBits);
  13919. if (ret == 0) {
  13920. ret = wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE);
  13921. if (ret != 0 || XMEMCMP(cipher, output, RC2_BLOCK_SIZE) != 0) {
  13922. ret = WOLFSSL_FATAL_ERROR;
  13923. }
  13924. if (ret == 0) {
  13925. ret = wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE);
  13926. if (ret != 0 || XMEMCMP(plain, input, RC2_BLOCK_SIZE) != 0) {
  13927. ret = WOLFSSL_FATAL_ERROR;
  13928. }
  13929. }
  13930. }
  13931. /* Rc2EcbEncrypt bad arguments */
  13932. if (ret == 0) {
  13933. /* null Rc2 struct */
  13934. ret = wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE);
  13935. if (ret == BAD_FUNC_ARG) {
  13936. ret = 0;
  13937. }
  13938. }
  13939. if (ret == 0) {
  13940. /* null out buffer */
  13941. ret = wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE);
  13942. if (ret == BAD_FUNC_ARG) {
  13943. ret = 0;
  13944. }
  13945. }
  13946. if (ret == 0) {
  13947. /* null input buffer */
  13948. ret = wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE);
  13949. if (ret == BAD_FUNC_ARG) {
  13950. ret = 0;
  13951. }
  13952. }
  13953. if (ret == 0) {
  13954. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  13955. ret = wc_Rc2EcbEncrypt(&rc2, cipher, input, 7);
  13956. if (ret == BUFFER_E) {
  13957. ret = 0;
  13958. }
  13959. }
  13960. /* Rc2EcbDecrypt bad arguments */
  13961. if (ret == 0) {
  13962. /* null Rc2 struct */
  13963. ret = wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE);
  13964. if (ret == BAD_FUNC_ARG) {
  13965. ret = 0;
  13966. }
  13967. }
  13968. if (ret == 0) {
  13969. /* null out buffer */
  13970. ret = wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE);
  13971. if (ret == BAD_FUNC_ARG) {
  13972. ret = 0;
  13973. }
  13974. }
  13975. if (ret == 0) {
  13976. /* null input buffer */
  13977. ret = wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE);
  13978. if (ret == BAD_FUNC_ARG) {
  13979. ret = 0;
  13980. }
  13981. }
  13982. if (ret == 0) {
  13983. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  13984. ret = wc_Rc2EcbDecrypt(&rc2, plain, output, 7);
  13985. if (ret == BUFFER_E) {
  13986. ret = 0;
  13987. }
  13988. }
  13989. printf(resultFmt, ret == 0 ? passed : failed);
  13990. #endif
  13991. return ret;
  13992. } /* END test_wc_Rc2SetKey */
  13993. /*
  13994. * Testing function for wc_Rc2CbcEncrypt().
  13995. */
  13996. static int test_wc_Rc2CbcEncryptDecrypt(void)
  13997. {
  13998. int ret = 0;
  13999. #ifdef WC_RC2
  14000. Rc2 rc2;
  14001. int effectiveKeyBits = 63;
  14002. byte cipher[RC2_BLOCK_SIZE*2];
  14003. byte plain[RC2_BLOCK_SIZE*2];
  14004. /* vector taken from test.c */
  14005. byte key[] = {
  14006. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  14007. };
  14008. byte iv[] = {
  14009. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  14010. };
  14011. byte input[] = {
  14012. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  14013. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  14014. };
  14015. byte output[] = {
  14016. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  14017. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  14018. };
  14019. printf(testingFmt, "wc_Rc2CbcEncryptDecrypt()");
  14020. XMEMSET(cipher, 0, sizeof(cipher));
  14021. XMEMSET(plain, 0, sizeof(plain));
  14022. ret = wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  14023. iv, effectiveKeyBits);
  14024. if (ret == 0) {
  14025. ret = wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input));
  14026. if (ret != 0 || XMEMCMP(cipher, output, sizeof(output)) != 0) {
  14027. ret = WOLFSSL_FATAL_ERROR;
  14028. } else {
  14029. /* reset IV for decrypt */
  14030. ret = wc_Rc2SetIV(&rc2, iv);
  14031. }
  14032. if (ret == 0) {
  14033. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher));
  14034. if (ret != 0 || XMEMCMP(plain, input, sizeof(input)) != 0) {
  14035. ret = WOLFSSL_FATAL_ERROR;
  14036. }
  14037. }
  14038. }
  14039. /* Rc2CbcEncrypt bad arguments */
  14040. if (ret == 0) {
  14041. /* null Rc2 struct */
  14042. ret = wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input));
  14043. if (ret == BAD_FUNC_ARG) {
  14044. ret = 0;
  14045. }
  14046. }
  14047. if (ret == 0) {
  14048. /* null out buffer */
  14049. ret = wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input));
  14050. if (ret == BAD_FUNC_ARG) {
  14051. ret = 0;
  14052. }
  14053. }
  14054. if (ret == 0) {
  14055. /* null input buffer */
  14056. ret = wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input));
  14057. if (ret == BAD_FUNC_ARG) {
  14058. ret = 0;
  14059. }
  14060. }
  14061. /* Rc2CbcDecrypt bad arguments */
  14062. if (ret == 0) {
  14063. /* in size is 0 */
  14064. ret = wc_Rc2CbcDecrypt(&rc2, plain, output, 0);
  14065. if (ret != 0) {
  14066. ret = WOLFSSL_FATAL_ERROR;
  14067. }
  14068. }
  14069. if (ret == 0) {
  14070. /* null Rc2 struct */
  14071. ret = wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output));
  14072. if (ret == BAD_FUNC_ARG) {
  14073. ret = 0;
  14074. }
  14075. }
  14076. if (ret == 0) {
  14077. /* null out buffer */
  14078. ret = wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output));
  14079. if (ret == BAD_FUNC_ARG) {
  14080. ret = 0;
  14081. }
  14082. }
  14083. if (ret == 0) {
  14084. /* null input buffer */
  14085. ret = wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output));
  14086. if (ret == BAD_FUNC_ARG) {
  14087. ret = 0;
  14088. }
  14089. }
  14090. printf(resultFmt, ret == 0 ? passed : failed);
  14091. #endif
  14092. return ret;
  14093. } /* END test_wc_Rc2SetKey */
  14094. /*
  14095. * Testing function for wc_AesSetIV
  14096. */
  14097. static int test_wc_AesSetIV (void)
  14098. {
  14099. int ret = 0;
  14100. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14101. Aes aes;
  14102. byte key16[] =
  14103. {
  14104. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14105. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14106. };
  14107. byte iv1[] = "1234567890abcdef";
  14108. byte iv2[] = "0987654321fedcba";
  14109. printf(testingFmt, "wc_AesSetIV()");
  14110. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  14111. if (ret != 0)
  14112. return ret;
  14113. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  14114. iv1, AES_ENCRYPTION);
  14115. if(ret == 0) {
  14116. ret = wc_AesSetIV(&aes, iv2);
  14117. }
  14118. /* Test bad args. */
  14119. if(ret == 0) {
  14120. ret = wc_AesSetIV(NULL, iv1);
  14121. if(ret == BAD_FUNC_ARG) {
  14122. /* NULL iv should return 0. */
  14123. ret = wc_AesSetIV(&aes, NULL);
  14124. } else {
  14125. ret = WOLFSSL_FATAL_ERROR;
  14126. }
  14127. }
  14128. wc_AesFree(&aes);
  14129. printf(resultFmt, ret == 0 ? passed : failed);
  14130. #endif
  14131. return ret;
  14132. } /* test_wc_AesSetIV */
  14133. /*
  14134. * Testing function for wc_AesSetKey().
  14135. */
  14136. static int test_wc_AesSetKey (void)
  14137. {
  14138. int ret = 0;
  14139. #ifndef NO_AES
  14140. Aes aes;
  14141. byte key16[] =
  14142. {
  14143. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14144. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14145. };
  14146. #ifdef WOLFSSL_AES_192
  14147. byte key24[] =
  14148. {
  14149. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14150. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14151. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  14152. };
  14153. #endif
  14154. #ifdef WOLFSSL_AES_256
  14155. byte key32[] =
  14156. {
  14157. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14158. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14159. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14160. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14161. };
  14162. #endif
  14163. byte badKey16[] =
  14164. {
  14165. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14166. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  14167. };
  14168. byte iv[] = "1234567890abcdef";
  14169. printf(testingFmt, "wc_AesSetKey()");
  14170. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  14171. if (ret != 0)
  14172. return ret;
  14173. #ifdef WOLFSSL_AES_128
  14174. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  14175. iv, AES_ENCRYPTION);
  14176. #endif
  14177. #ifdef WOLFSSL_AES_192
  14178. if (ret == 0) {
  14179. ret = wc_AesSetKey (&aes, key24, (word32) sizeof(key24) / sizeof(byte),
  14180. iv, AES_ENCRYPTION);
  14181. }
  14182. #endif
  14183. #ifdef WOLFSSL_AES_256
  14184. if (ret == 0) {
  14185. ret = wc_AesSetKey (&aes, key32, (word32) sizeof(key32) / sizeof(byte),
  14186. iv, AES_ENCRYPTION);
  14187. }
  14188. #endif
  14189. /* Pass in bad args. */
  14190. if (ret == 0) {
  14191. ret = wc_AesSetKey (NULL, key16, (word32) sizeof(key16) / sizeof(byte),
  14192. iv, AES_ENCRYPTION);
  14193. if (ret == BAD_FUNC_ARG) {
  14194. ret = wc_AesSetKey(&aes, badKey16,
  14195. (word32) sizeof(badKey16) / sizeof(byte),
  14196. iv, AES_ENCRYPTION);
  14197. }
  14198. if (ret == BAD_FUNC_ARG) {
  14199. ret = 0;
  14200. } else {
  14201. ret = WOLFSSL_FATAL_ERROR;
  14202. }
  14203. }
  14204. wc_AesFree(&aes);
  14205. printf(resultFmt, ret == 0 ? passed : failed);
  14206. #endif
  14207. return ret;
  14208. } /* END test_wc_AesSetKey */
  14209. /*
  14210. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  14211. * and wc_AesCbcDecryptWithKey()
  14212. */
  14213. static int test_wc_AesCbcEncryptDecrypt (void)
  14214. {
  14215. int ret = 0;
  14216. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  14217. defined(WOLFSSL_AES_256)
  14218. Aes aes;
  14219. byte key32[] =
  14220. {
  14221. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14222. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14223. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14224. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14225. };
  14226. byte vector[] = /* Now is the time for all good men w/o trailing 0 */
  14227. {
  14228. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14229. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14230. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20,
  14231. 0x67,0x6f,0x6f,0x64,0x20,0x6d,0x65,0x6e
  14232. };
  14233. byte iv[] = "1234567890abcdef";
  14234. byte enc[sizeof(vector)];
  14235. byte dec[sizeof(vector)];
  14236. int cbcE = WOLFSSL_FATAL_ERROR;
  14237. int cbcD = WOLFSSL_FATAL_ERROR;
  14238. int cbcDWK = WOLFSSL_FATAL_ERROR;
  14239. byte dec2[sizeof(vector)];
  14240. /* Init stack variables. */
  14241. XMEMSET(enc, 0, sizeof(enc));
  14242. XMEMSET(dec, 0, sizeof(vector));
  14243. XMEMSET(dec2, 0, sizeof(vector));
  14244. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  14245. if (ret != 0)
  14246. return ret;
  14247. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv, AES_ENCRYPTION);
  14248. if (ret == 0) {
  14249. ret = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector));
  14250. if (ret == 0) {
  14251. /* Re init for decrypt and set flag. */
  14252. cbcE = 0;
  14253. wc_AesFree(&aes);
  14254. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2,
  14255. iv, AES_DECRYPTION);
  14256. }
  14257. if (ret == 0) {
  14258. ret = wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector));
  14259. if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector)) != 0) {
  14260. ret = WOLFSSL_FATAL_ERROR;
  14261. } else {
  14262. /* Set flag. */
  14263. cbcD = 0;
  14264. }
  14265. }
  14266. }
  14267. /* If encrypt succeeds but cbc decrypt fails, we can still test. */
  14268. if (ret == 0 || cbcE == 0) {
  14269. ret = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  14270. key32, sizeof(key32)/sizeof(byte), iv);
  14271. if (ret == 0 || XMEMCMP(vector, dec2, AES_BLOCK_SIZE) == 0) {
  14272. cbcDWK = 0;
  14273. }
  14274. }
  14275. printf(testingFmt, "wc_AesCbcEncrypt()");
  14276. /* Pass in bad args */
  14277. if (cbcE == 0) {
  14278. cbcE = wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector));
  14279. if (cbcE == BAD_FUNC_ARG) {
  14280. cbcE = wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector));
  14281. }
  14282. if (cbcE == BAD_FUNC_ARG) {
  14283. cbcE = wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector));
  14284. }
  14285. if (cbcE == BAD_FUNC_ARG) {
  14286. cbcE = 0;
  14287. } else {
  14288. cbcE = WOLFSSL_FATAL_ERROR;
  14289. }
  14290. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  14291. if (cbcE == 0) {
  14292. cbcE = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1);
  14293. }
  14294. if (cbcE == BAD_LENGTH_E) {
  14295. cbcE = 0;
  14296. } else {
  14297. cbcE = WOLFSSL_FATAL_ERROR;
  14298. }
  14299. #endif
  14300. }
  14301. if (cbcE == 0) {
  14302. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  14303. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  14304. printf("Zero length inputs not supported with AESNI in FIPS mode (v2),"
  14305. " skip test");
  14306. #else
  14307. /* Test passing in size of 0 */
  14308. XMEMSET(enc, 0, sizeof(enc));
  14309. cbcE = wc_AesCbcEncrypt(&aes, enc, vector, 0);
  14310. if (cbcE == 0) {
  14311. /* Check enc was not modified */
  14312. int i;
  14313. for (i = 0; i < (int)sizeof(enc); i++)
  14314. cbcE |= enc[i];
  14315. }
  14316. #endif
  14317. }
  14318. printf(resultFmt, cbcE == 0 ? passed : failed);
  14319. if (cbcE != 0) {
  14320. wc_AesFree(&aes);
  14321. return cbcE;
  14322. }
  14323. printf(testingFmt, "wc_AesCbcDecrypt()");
  14324. if (cbcD == 0) {
  14325. cbcD = wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE);
  14326. if (cbcD == BAD_FUNC_ARG) {
  14327. cbcD = wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE);
  14328. }
  14329. if (cbcD == BAD_FUNC_ARG) {
  14330. cbcD = wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE);
  14331. }
  14332. if (cbcD == BAD_FUNC_ARG) {
  14333. cbcD = wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1);
  14334. }
  14335. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  14336. if (cbcD == BAD_LENGTH_E) {
  14337. cbcD = 0;
  14338. } else {
  14339. cbcD = WOLFSSL_FATAL_ERROR;
  14340. }
  14341. #else
  14342. if (cbcD == BAD_FUNC_ARG) {
  14343. cbcD = 0;
  14344. } else {
  14345. cbcD = WOLFSSL_FATAL_ERROR;
  14346. }
  14347. #endif
  14348. }
  14349. if (cbcD == 0) {
  14350. /* Test passing in size of 0 */
  14351. XMEMSET(dec, 0, sizeof(dec));
  14352. cbcD = wc_AesCbcDecrypt(&aes, dec, enc, 0);
  14353. if (cbcD == 0) {
  14354. /* Check dec was not modified */
  14355. int i;
  14356. for (i = 0; i < (int)sizeof(dec); i++)
  14357. cbcD |= dec[i];
  14358. }
  14359. }
  14360. printf(resultFmt, cbcD == 0 ? passed : failed);
  14361. if (cbcD != 0) {
  14362. wc_AesFree(&aes);
  14363. return cbcD;
  14364. }
  14365. printf(testingFmt, "wc_AesCbcDecryptWithKey()");
  14366. if (cbcDWK == 0) {
  14367. cbcDWK = wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  14368. key32, sizeof(key32)/sizeof(byte), iv);
  14369. if (cbcDWK == BAD_FUNC_ARG) {
  14370. cbcDWK = wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  14371. key32, sizeof(key32)/sizeof(byte), iv);
  14372. }
  14373. if (cbcDWK == BAD_FUNC_ARG) {
  14374. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  14375. NULL, sizeof(key32)/sizeof(byte), iv);
  14376. }
  14377. if (cbcDWK == BAD_FUNC_ARG) {
  14378. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  14379. key32, sizeof(key32)/sizeof(byte), NULL);
  14380. }
  14381. if (cbcDWK == BAD_FUNC_ARG) {
  14382. cbcDWK = 0;
  14383. } else {
  14384. cbcDWK = WOLFSSL_FATAL_ERROR;
  14385. }
  14386. }
  14387. wc_AesFree(&aes);
  14388. printf(resultFmt, cbcDWK == 0 ? passed : failed);
  14389. if (cbcDWK != 0) {
  14390. return cbcDWK;
  14391. }
  14392. #endif
  14393. return ret;
  14394. } /* END test_wc_AesCbcEncryptDecrypt */
  14395. /*
  14396. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  14397. */
  14398. static int test_wc_AesCtrEncryptDecrypt (void)
  14399. {
  14400. int ret = 0;
  14401. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  14402. Aes aesEnc, aesDec;
  14403. byte key32[] =
  14404. {
  14405. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14406. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14407. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14408. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14409. };
  14410. byte vector[] = /* Now is the time for all w/o trailing 0 */
  14411. {
  14412. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14413. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14414. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14415. };
  14416. byte iv[] = "1234567890abcdef";
  14417. byte enc[AES_BLOCK_SIZE * 2];
  14418. byte dec[AES_BLOCK_SIZE * 2];
  14419. /* Init stack variables. */
  14420. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  14421. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  14422. printf(testingFmt, "wc_AesCtrEncrypt()");
  14423. ret = wc_AesInit(&aesEnc, NULL, INVALID_DEVID);
  14424. if (ret != 0)
  14425. return ret;
  14426. ret = wc_AesInit(&aesDec, NULL, INVALID_DEVID);
  14427. if (ret != 0) {
  14428. wc_AesFree(&aesEnc);
  14429. return ret;
  14430. }
  14431. ret = wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2,
  14432. iv, AES_ENCRYPTION);
  14433. if (ret == 0) {
  14434. ret = wc_AesCtrEncrypt(&aesEnc, enc, vector,
  14435. sizeof(vector)/sizeof(byte));
  14436. if (ret == 0) {
  14437. /* Decrypt with wc_AesCtrEncrypt() */
  14438. ret = wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2,
  14439. iv, AES_ENCRYPTION);
  14440. }
  14441. if (ret == 0) {
  14442. ret = wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte));
  14443. if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector))) {
  14444. ret = WOLFSSL_FATAL_ERROR;
  14445. }
  14446. }
  14447. }
  14448. /* Test bad args. */
  14449. if (ret == 0) {
  14450. ret = wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte));
  14451. if (ret == BAD_FUNC_ARG) {
  14452. ret = wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte));
  14453. }
  14454. if (ret == BAD_FUNC_ARG) {
  14455. ret = wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte));
  14456. }
  14457. if (ret == BAD_FUNC_ARG) {
  14458. ret = 0;
  14459. } else {
  14460. ret = WOLFSSL_FATAL_ERROR;
  14461. }
  14462. }
  14463. wc_AesFree(&aesEnc);
  14464. wc_AesFree(&aesDec);
  14465. printf(resultFmt, ret == 0 ? passed : failed);
  14466. #endif
  14467. return ret;
  14468. } /* END test_wc_AesCtrEncryptDecrypt */
  14469. /*
  14470. * test function for wc_AesGcmSetKey()
  14471. */
  14472. static int test_wc_AesGcmSetKey (void)
  14473. {
  14474. int ret = 0;
  14475. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  14476. Aes aes;
  14477. #ifdef WOLFSSL_AES_128
  14478. byte key16[] =
  14479. {
  14480. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14481. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14482. };
  14483. #endif
  14484. #ifdef WOLFSSL_AES_192
  14485. byte key24[] =
  14486. {
  14487. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14488. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14489. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  14490. };
  14491. #endif
  14492. #ifdef WOLFSSL_AES_256
  14493. byte key32[] =
  14494. {
  14495. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14496. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14497. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14498. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14499. };
  14500. #endif
  14501. byte badKey16[] =
  14502. {
  14503. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14504. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  14505. };
  14506. byte badKey24[] =
  14507. {
  14508. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14509. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14510. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  14511. };
  14512. byte badKey32[] =
  14513. {
  14514. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  14515. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14516. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14517. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  14518. };
  14519. printf(testingFmt, "wc_AesGcmSetKey()");
  14520. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  14521. if (ret != 0)
  14522. return ret;
  14523. #ifdef WOLFSSL_AES_128
  14524. ret = wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte));
  14525. #endif
  14526. #ifdef WOLFSSL_AES_192
  14527. if (ret == 0) {
  14528. ret = wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte));
  14529. }
  14530. #endif
  14531. #ifdef WOLFSSL_AES_256
  14532. if (ret == 0) {
  14533. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  14534. }
  14535. #endif
  14536. /* Pass in bad args. */
  14537. if (ret == 0) {
  14538. ret = wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte));
  14539. if (ret == BAD_FUNC_ARG) {
  14540. ret = wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte));
  14541. }
  14542. if (ret == BAD_FUNC_ARG) {
  14543. ret = wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte));
  14544. }
  14545. if (ret == BAD_FUNC_ARG) {
  14546. ret = 0;
  14547. } else {
  14548. ret = WOLFSSL_FATAL_ERROR;
  14549. }
  14550. }
  14551. wc_AesFree(&aes);
  14552. printf(resultFmt, ret == 0 ? passed : failed);
  14553. #endif
  14554. return ret;
  14555. } /* END test_wc_AesGcmSetKey */
  14556. /*
  14557. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  14558. */
  14559. static int test_wc_AesGcmEncryptDecrypt (void)
  14560. {
  14561. int ret = 0;
  14562. /* WOLFSSL_AFALG requires 12 byte IV */
  14563. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  14564. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  14565. Aes aes;
  14566. byte key32[] =
  14567. {
  14568. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14569. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14570. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14571. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14572. };
  14573. byte vector[] = /* Now is the time for all w/o trailing 0 */
  14574. {
  14575. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14576. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14577. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14578. };
  14579. const byte a[] =
  14580. {
  14581. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  14582. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  14583. 0xab, 0xad, 0xda, 0xd2
  14584. };
  14585. byte iv[] = "1234567890a";
  14586. byte longIV[] = "1234567890abcdefghij";
  14587. byte enc[sizeof(vector)];
  14588. byte resultT[AES_BLOCK_SIZE];
  14589. byte dec[sizeof(vector)];
  14590. int gcmD = WOLFSSL_FATAL_ERROR;
  14591. int gcmE = WOLFSSL_FATAL_ERROR;
  14592. /* Init stack variables. */
  14593. XMEMSET(enc, 0, sizeof(vector));
  14594. XMEMSET(dec, 0, sizeof(vector));
  14595. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  14596. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  14597. if (ret != 0)
  14598. return ret;
  14599. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  14600. if (ret == 0) {
  14601. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector),
  14602. iv, sizeof(iv)/sizeof(byte), resultT,
  14603. sizeof(resultT), a, sizeof(a));
  14604. }
  14605. if (gcmE == 0) { /* If encrypt fails, no decrypt. */
  14606. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector),
  14607. iv, sizeof(iv)/sizeof(byte), resultT,
  14608. sizeof(resultT), a, sizeof(a));
  14609. if(gcmD == 0 && (XMEMCMP(vector, dec, sizeof(vector)) != 0)) {
  14610. gcmD = WOLFSSL_FATAL_ERROR;
  14611. }
  14612. }
  14613. printf(testingFmt, "wc_AesGcmEncrypt()");
  14614. /*Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  14615. if (gcmE == 0) {
  14616. gcmE = wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector),
  14617. iv, sizeof(iv)/sizeof(byte), resultT, sizeof(resultT),
  14618. a, sizeof(a));
  14619. if (gcmE == BAD_FUNC_ARG) {
  14620. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  14621. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  14622. resultT, sizeof(resultT) + 1, a, sizeof(a));
  14623. }
  14624. if (gcmE == BAD_FUNC_ARG) {
  14625. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  14626. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  14627. resultT, sizeof(resultT) - 5, a, sizeof(a));
  14628. }
  14629. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  14630. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  14631. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  14632. /* FIPS does not check the lower bound of ivSz */
  14633. #else
  14634. if (gcmE == BAD_FUNC_ARG) {
  14635. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  14636. sizeof(vector), iv, 0,
  14637. resultT, sizeof(resultT), a, sizeof(a));
  14638. }
  14639. #endif
  14640. if (gcmE == BAD_FUNC_ARG) {
  14641. gcmE = 0;
  14642. } else {
  14643. gcmE = WOLFSSL_FATAL_ERROR;
  14644. }
  14645. }
  14646. /* This case is now considered good. Long IVs are now allowed.
  14647. * Except for the original FIPS release, it still has an upper
  14648. * bound on the IV length. */
  14649. #if (!defined(HAVE_FIPS) || \
  14650. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  14651. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  14652. if (gcmE == 0) {
  14653. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  14654. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT),
  14655. a, sizeof(a));
  14656. }
  14657. #else
  14658. (void)longIV;
  14659. #endif /* Old FIPS */
  14660. /* END wc_AesGcmEncrypt */
  14661. printf(resultFmt, gcmE == 0 ? passed : failed);
  14662. if (gcmE != 0) {
  14663. wc_AesFree(&aes);
  14664. return gcmE;
  14665. }
  14666. #ifdef HAVE_AES_DECRYPT
  14667. printf(testingFmt, "wc_AesGcmDecrypt()");
  14668. if (gcmD == 0) {
  14669. gcmD = wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte),
  14670. iv, sizeof(iv)/sizeof(byte), resultT,
  14671. sizeof(resultT), a, sizeof(a));
  14672. if (gcmD == BAD_FUNC_ARG) {
  14673. gcmD = wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte),
  14674. iv, sizeof(iv)/sizeof(byte), resultT,
  14675. sizeof(resultT), a, sizeof(a));
  14676. }
  14677. if (gcmD == BAD_FUNC_ARG) {
  14678. gcmD = wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte),
  14679. iv, sizeof(iv)/sizeof(byte), resultT,
  14680. sizeof(resultT), a, sizeof(a));
  14681. }
  14682. if (gcmD == BAD_FUNC_ARG) {
  14683. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  14684. NULL, sizeof(iv)/sizeof(byte), resultT,
  14685. sizeof(resultT), a, sizeof(a));
  14686. }
  14687. if (gcmD == BAD_FUNC_ARG) {
  14688. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  14689. iv, sizeof(iv)/sizeof(byte), NULL,
  14690. sizeof(resultT), a, sizeof(a));
  14691. }
  14692. if (gcmD == BAD_FUNC_ARG) {
  14693. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  14694. iv, sizeof(iv)/sizeof(byte), resultT,
  14695. sizeof(resultT) + 1, a, sizeof(a));
  14696. }
  14697. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  14698. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  14699. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  14700. /* FIPS does not check the lower bound of ivSz */
  14701. #else
  14702. if (gcmD == BAD_FUNC_ARG) {
  14703. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  14704. iv, 0, resultT,
  14705. sizeof(resultT), a, sizeof(a));
  14706. }
  14707. #endif
  14708. if (gcmD == BAD_FUNC_ARG) {
  14709. gcmD = 0;
  14710. } else {
  14711. gcmD = WOLFSSL_FATAL_ERROR;
  14712. }
  14713. } /* END wc_AesGcmDecrypt */
  14714. printf(resultFmt, gcmD == 0 ? passed : failed);
  14715. #endif /* HAVE_AES_DECRYPT */
  14716. wc_AesFree(&aes);
  14717. #endif
  14718. return ret;
  14719. } /* END test_wc_AesGcmEncryptDecrypt */
  14720. /*
  14721. * unit test for wc_GmacSetKey()
  14722. */
  14723. static int test_wc_GmacSetKey (void)
  14724. {
  14725. int ret = 0;
  14726. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  14727. Gmac gmac;
  14728. byte key16[] =
  14729. {
  14730. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14731. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14732. };
  14733. #ifdef WOLFSSL_AES_192
  14734. byte key24[] =
  14735. {
  14736. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14737. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14738. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  14739. };
  14740. #endif
  14741. #ifdef WOLFSSL_AES_256
  14742. byte key32[] =
  14743. {
  14744. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14745. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14746. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14747. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14748. };
  14749. #endif
  14750. byte badKey16[] =
  14751. {
  14752. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14753. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  14754. };
  14755. byte badKey24[] =
  14756. {
  14757. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  14758. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14759. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  14760. };
  14761. byte badKey32[] =
  14762. {
  14763. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14764. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  14765. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14766. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14767. };
  14768. printf(testingFmt, "wc_GmacSetKey()");
  14769. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  14770. if (ret != 0)
  14771. return ret;
  14772. #ifdef WOLFSSL_AES_128
  14773. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte));
  14774. #endif
  14775. #ifdef WOLFSSL_AES_192
  14776. if (ret == 0) {
  14777. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  14778. }
  14779. #endif
  14780. #ifdef WOLFSSL_AES_256
  14781. if (ret == 0) {
  14782. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  14783. }
  14784. #endif
  14785. /* Pass in bad args. */
  14786. if (ret == 0) {
  14787. ret = wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte));
  14788. if (ret == BAD_FUNC_ARG) {
  14789. ret = wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte));
  14790. }
  14791. if (ret == BAD_FUNC_ARG) {
  14792. ret = wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte));
  14793. }
  14794. if (ret == BAD_FUNC_ARG) {
  14795. ret = wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte));
  14796. }
  14797. if (ret == BAD_FUNC_ARG) {
  14798. ret = wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte));
  14799. }
  14800. if (ret == BAD_FUNC_ARG) {
  14801. ret = 0;
  14802. } else {
  14803. ret = WOLFSSL_FATAL_ERROR;
  14804. }
  14805. }
  14806. wc_AesFree(&gmac.aes);
  14807. printf(resultFmt, ret == 0 ? passed : failed);
  14808. #endif
  14809. return ret;
  14810. } /* END test_wc_GmacSetKey */
  14811. /*
  14812. * unit test for wc_GmacUpdate
  14813. */
  14814. static int test_wc_GmacUpdate (void)
  14815. {
  14816. int ret = 0;
  14817. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  14818. Gmac gmac;
  14819. #ifdef WOLFSSL_AES_128
  14820. const byte key16[] =
  14821. {
  14822. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  14823. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  14824. };
  14825. #endif
  14826. #ifdef WOLFSSL_AES_192
  14827. byte key24[] =
  14828. {
  14829. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  14830. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  14831. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  14832. };
  14833. #endif
  14834. #ifdef WOLFSSL_AES_256
  14835. byte key32[] =
  14836. {
  14837. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  14838. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  14839. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  14840. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  14841. };
  14842. #endif
  14843. #ifdef WOLFSSL_AES_128
  14844. const byte authIn[] =
  14845. {
  14846. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  14847. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  14848. };
  14849. #endif
  14850. #ifdef WOLFSSL_AES_192
  14851. const byte authIn2[] =
  14852. {
  14853. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  14854. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  14855. };
  14856. #endif
  14857. const byte authIn3[] =
  14858. {
  14859. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  14860. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  14861. };
  14862. #ifdef WOLFSSL_AES_128
  14863. const byte tag1[] = /* Known. */
  14864. {
  14865. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  14866. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  14867. };
  14868. #endif
  14869. #ifdef WOLFSSL_AES_192
  14870. const byte tag2[] = /* Known */
  14871. {
  14872. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  14873. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  14874. };
  14875. #endif
  14876. const byte tag3[] = /* Known */
  14877. {
  14878. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  14879. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  14880. };
  14881. #ifdef WOLFSSL_AES_128
  14882. const byte iv[] =
  14883. {
  14884. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  14885. 0xe2, 0x8c, 0x8f, 0x16
  14886. };
  14887. #endif
  14888. #ifdef WOLFSSL_AES_192
  14889. const byte iv2[] =
  14890. {
  14891. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  14892. 0x7e, 0x1a, 0x6f, 0xbc
  14893. };
  14894. #endif
  14895. const byte iv3[] =
  14896. {
  14897. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  14898. 0xc3, 0xfb, 0x6c, 0x8a
  14899. };
  14900. byte tagOut[16];
  14901. byte tagOut2[24];
  14902. byte tagOut3[32];
  14903. /* Init stack variables. */
  14904. XMEMSET(tagOut, 0, sizeof(tagOut));
  14905. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  14906. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  14907. printf(testingFmt, "wc_GmacUpdate()");
  14908. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  14909. if (ret != 0)
  14910. return ret;
  14911. #ifdef WOLFSSL_AES_128
  14912. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16));
  14913. if (ret == 0) {
  14914. ret = wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  14915. tagOut, sizeof(tag1));
  14916. if (ret == 0) {
  14917. ret = XMEMCMP(tag1, tagOut, sizeof(tag1));
  14918. }
  14919. wc_AesFree(&gmac.aes);
  14920. }
  14921. #endif
  14922. #ifdef WOLFSSL_AES_192
  14923. if (ret == 0) {
  14924. XMEMSET(&gmac, 0, sizeof(Gmac));
  14925. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  14926. }
  14927. if (ret == 0) {
  14928. ret = wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2,
  14929. sizeof(authIn2), tagOut2, sizeof(tag2));
  14930. }
  14931. if (ret == 0) {
  14932. ret = XMEMCMP(tagOut2, tag2, sizeof(tag2));
  14933. wc_AesFree(&gmac.aes);
  14934. }
  14935. #endif
  14936. #ifdef WOLFSSL_AES_256
  14937. if (ret == 0) {
  14938. XMEMSET(&gmac, 0, sizeof(Gmac));
  14939. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  14940. }
  14941. if (ret == 0) {
  14942. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  14943. sizeof(authIn3), tagOut3, sizeof(tag3));
  14944. }
  14945. if (ret == 0) {
  14946. ret = XMEMCMP(tag3, tagOut3, sizeof(tag3));
  14947. }
  14948. #endif
  14949. /*Pass bad args. */
  14950. if (ret == 0) {
  14951. ret = wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3,
  14952. sizeof(authIn3), tagOut3, sizeof(tag3));
  14953. if (ret == BAD_FUNC_ARG) {
  14954. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  14955. sizeof(authIn3), tagOut3, sizeof(tag3) - 5);
  14956. }
  14957. if (ret == BAD_FUNC_ARG) {
  14958. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  14959. sizeof(authIn3), tagOut3, sizeof(tag3) + 1);
  14960. }
  14961. if (ret == BAD_FUNC_ARG) {
  14962. ret = 0;
  14963. } else {
  14964. ret = WOLFSSL_FATAL_ERROR;
  14965. }
  14966. }
  14967. wc_AesFree(&gmac.aes);
  14968. printf(resultFmt, ret == 0 ? passed : failed);
  14969. #endif
  14970. return ret;
  14971. } /* END test_wc_GmacUpdate */
  14972. /*
  14973. * testing wc_CamelliaSetKey
  14974. */
  14975. static int test_wc_CamelliaSetKey (void)
  14976. {
  14977. int ret = 0;
  14978. #ifdef HAVE_CAMELLIA
  14979. Camellia camellia;
  14980. /*128-bit key*/
  14981. static const byte key16[] =
  14982. {
  14983. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  14984. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  14985. };
  14986. /* 192-bit key */
  14987. static const byte key24[] =
  14988. {
  14989. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  14990. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  14991. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  14992. };
  14993. /* 256-bit key */
  14994. static const byte key32[] =
  14995. {
  14996. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  14997. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  14998. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14999. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  15000. };
  15001. static const byte iv[] =
  15002. {
  15003. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15004. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  15005. };
  15006. printf(testingFmt, "wc_CamelliaSetKey()");
  15007. ret = wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv);
  15008. if (ret == 0) {
  15009. ret = wc_CamelliaSetKey(&camellia, key16,
  15010. (word32)sizeof(key16), NULL);
  15011. if (ret == 0) {
  15012. ret = wc_CamelliaSetKey(&camellia, key24,
  15013. (word32)sizeof(key24), iv);
  15014. }
  15015. if (ret == 0) {
  15016. ret = wc_CamelliaSetKey(&camellia, key24,
  15017. (word32)sizeof(key24), NULL);
  15018. }
  15019. if (ret == 0) {
  15020. ret = wc_CamelliaSetKey(&camellia, key32,
  15021. (word32)sizeof(key32), iv);
  15022. }
  15023. if (ret == 0) {
  15024. ret = wc_CamelliaSetKey(&camellia, key32,
  15025. (word32)sizeof(key32), NULL);
  15026. }
  15027. }
  15028. /* Bad args. */
  15029. if (ret == 0) {
  15030. ret = wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv);
  15031. if (ret != BAD_FUNC_ARG) {
  15032. ret = WOLFSSL_FATAL_ERROR;
  15033. } else {
  15034. ret = 0;
  15035. }
  15036. } /* END bad args. */
  15037. #endif
  15038. return ret;
  15039. } /* END test_wc_CammeliaSetKey */
  15040. /*
  15041. * Testing wc_CamelliaSetIV()
  15042. */
  15043. static int test_wc_CamelliaSetIV (void)
  15044. {
  15045. int ret = 0;
  15046. #ifdef HAVE_CAMELLIA
  15047. Camellia camellia;
  15048. static const byte iv[] =
  15049. {
  15050. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15051. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  15052. };
  15053. printf(testingFmt, "wc_CamelliaSetIV()");
  15054. ret = wc_CamelliaSetIV(&camellia, iv);
  15055. if (ret == 0) {
  15056. ret = wc_CamelliaSetIV(&camellia, NULL);
  15057. }
  15058. /* Bad args. */
  15059. if (ret == 0) {
  15060. ret = wc_CamelliaSetIV(NULL, NULL);
  15061. if (ret != BAD_FUNC_ARG) {
  15062. ret = WOLFSSL_FATAL_ERROR;
  15063. } else {
  15064. ret = 0;
  15065. }
  15066. }
  15067. printf(resultFmt, ret == 0 ? passed : failed);
  15068. #endif
  15069. return ret;
  15070. } /*END test_wc_CamelliaSetIV*/
  15071. /*
  15072. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  15073. */
  15074. static int test_wc_CamelliaEncryptDecryptDirect (void)
  15075. {
  15076. int ret = 0;
  15077. #ifdef HAVE_CAMELLIA
  15078. Camellia camellia;
  15079. static const byte key24[] =
  15080. {
  15081. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15082. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15083. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  15084. };
  15085. static const byte iv[] =
  15086. {
  15087. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15088. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  15089. };
  15090. static const byte plainT[] =
  15091. {
  15092. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  15093. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  15094. };
  15095. byte enc[sizeof(plainT)];
  15096. byte dec[sizeof(enc)];
  15097. int camE = WOLFSSL_FATAL_ERROR;
  15098. int camD = WOLFSSL_FATAL_ERROR;
  15099. /*Init stack variables.*/
  15100. XMEMSET(enc, 0, 16);
  15101. XMEMSET(enc, 0, 16);
  15102. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv);
  15103. if (ret == 0) {
  15104. ret = wc_CamelliaEncryptDirect(&camellia, enc, plainT);
  15105. if (ret == 0) {
  15106. ret = wc_CamelliaDecryptDirect(&camellia, dec, enc);
  15107. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  15108. ret = WOLFSSL_FATAL_ERROR;
  15109. }
  15110. }
  15111. }
  15112. printf(testingFmt, "wc_CamelliaEncryptDirect()");
  15113. /* Pass bad args. */
  15114. if (ret == 0) {
  15115. camE = wc_CamelliaEncryptDirect(NULL, enc, plainT);
  15116. if (camE == BAD_FUNC_ARG) {
  15117. camE = wc_CamelliaEncryptDirect(&camellia, NULL, plainT);
  15118. }
  15119. if (camE == BAD_FUNC_ARG) {
  15120. camE = wc_CamelliaEncryptDirect(&camellia, enc, NULL);
  15121. }
  15122. if (camE == BAD_FUNC_ARG) {
  15123. camE = 0;
  15124. } else {
  15125. camE = WOLFSSL_FATAL_ERROR;
  15126. }
  15127. }
  15128. printf(resultFmt, camE == 0 ? passed : failed);
  15129. if (camE != 0) {
  15130. return camE;
  15131. }
  15132. printf(testingFmt, "wc_CamelliaDecryptDirect()");
  15133. if (ret == 0) {
  15134. camD = wc_CamelliaDecryptDirect(NULL, dec, enc);
  15135. if (camD == BAD_FUNC_ARG) {
  15136. camD = wc_CamelliaDecryptDirect(&camellia, NULL, enc);
  15137. }
  15138. if (camD == BAD_FUNC_ARG) {
  15139. camD = wc_CamelliaDecryptDirect(&camellia, dec, NULL);
  15140. }
  15141. if (camD == BAD_FUNC_ARG) {
  15142. camD = 0;
  15143. } else {
  15144. camD = WOLFSSL_FATAL_ERROR;
  15145. }
  15146. }
  15147. printf(resultFmt, camD == 0 ? passed : failed);
  15148. if (camD != 0) {
  15149. return camD;
  15150. }
  15151. #endif
  15152. return ret;
  15153. } /* END test-wc_CamelliaEncryptDecryptDirect */
  15154. /*
  15155. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  15156. */
  15157. static int test_wc_CamelliaCbcEncryptDecrypt (void)
  15158. {
  15159. int ret = 0;
  15160. #ifdef HAVE_CAMELLIA
  15161. Camellia camellia;
  15162. static const byte key24[] =
  15163. {
  15164. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15165. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15166. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  15167. };
  15168. static const byte plainT[] =
  15169. {
  15170. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  15171. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  15172. };
  15173. byte enc[CAMELLIA_BLOCK_SIZE];
  15174. byte dec[CAMELLIA_BLOCK_SIZE];
  15175. int camCbcE = WOLFSSL_FATAL_ERROR;
  15176. int camCbcD = WOLFSSL_FATAL_ERROR;
  15177. /* Init stack variables. */
  15178. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  15179. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  15180. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  15181. if (ret == 0) {
  15182. ret = wc_CamelliaCbcEncrypt(&camellia, enc, plainT, CAMELLIA_BLOCK_SIZE);
  15183. if (ret != 0) {
  15184. ret = WOLFSSL_FATAL_ERROR;
  15185. }
  15186. }
  15187. if (ret == 0) {
  15188. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  15189. if (ret == 0) {
  15190. ret = wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE);
  15191. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  15192. ret = WOLFSSL_FATAL_ERROR;
  15193. }
  15194. }
  15195. }
  15196. printf(testingFmt, "wc_CamelliaCbcEncrypt");
  15197. /* Pass in bad args. */
  15198. if (ret == 0) {
  15199. camCbcE = wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE);
  15200. if (camCbcE == BAD_FUNC_ARG) {
  15201. camCbcE = wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  15202. CAMELLIA_BLOCK_SIZE);
  15203. }
  15204. if (camCbcE == BAD_FUNC_ARG) {
  15205. camCbcE = wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  15206. CAMELLIA_BLOCK_SIZE);
  15207. }
  15208. if (camCbcE == BAD_FUNC_ARG) {
  15209. camCbcE = 0;
  15210. } else {
  15211. camCbcE = WOLFSSL_FATAL_ERROR;
  15212. }
  15213. }
  15214. printf(resultFmt, camCbcE == 0 ? passed : failed);
  15215. if (camCbcE != 0) {
  15216. return camCbcE;
  15217. }
  15218. printf(testingFmt, "wc_CamelliaCbcDecrypt()");
  15219. if (ret == 0) {
  15220. camCbcD = wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE);
  15221. if (camCbcD == BAD_FUNC_ARG) {
  15222. camCbcD = wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  15223. CAMELLIA_BLOCK_SIZE);
  15224. }
  15225. if (camCbcD == BAD_FUNC_ARG) {
  15226. camCbcD = wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  15227. CAMELLIA_BLOCK_SIZE);
  15228. }
  15229. if (camCbcD == BAD_FUNC_ARG) {
  15230. camCbcD = 0;
  15231. } else {
  15232. camCbcD = WOLFSSL_FATAL_ERROR;
  15233. }
  15234. } /* END bad args. */
  15235. printf(resultFmt, camCbcD == 0 ? passed : failed);
  15236. if (camCbcD != 0) {
  15237. return camCbcD;
  15238. }
  15239. #endif
  15240. return ret;
  15241. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  15242. /*
  15243. * Testing wc_Arc4SetKey()
  15244. */
  15245. static int test_wc_Arc4SetKey (void)
  15246. {
  15247. int ret = 0;
  15248. #ifndef NO_RC4
  15249. Arc4 arc;
  15250. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  15251. int keyLen = 8;
  15252. printf(testingFmt, "wc_Arch4SetKey()");
  15253. ret = wc_Arc4SetKey(&arc, (byte*)key, keyLen);
  15254. /* Test bad args. */
  15255. if (ret == 0) {
  15256. ret = wc_Arc4SetKey(NULL, (byte*)key, keyLen);
  15257. if (ret == BAD_FUNC_ARG)
  15258. ret = wc_Arc4SetKey(&arc, NULL, keyLen); /* NULL key */
  15259. if (ret == BAD_FUNC_ARG)
  15260. ret = wc_Arc4SetKey(&arc, (byte*)key, 0); /* length == 0 */
  15261. if (ret == BAD_FUNC_ARG)
  15262. ret = WOLFSSL_ERROR_NONE;
  15263. else
  15264. ret = WOLFSSL_FATAL_ERROR;
  15265. } /* END test bad args. */
  15266. printf(resultFmt, ret == 0 ? passed : failed);
  15267. #endif
  15268. return ret;
  15269. } /* END test_wc_Arc4SetKey */
  15270. /*
  15271. * Testing wc_Arc4Process for ENC/DEC.
  15272. */
  15273. static int test_wc_Arc4Process (void)
  15274. {
  15275. int ret = 0;
  15276. #ifndef NO_RC4
  15277. Arc4 enc, dec;
  15278. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  15279. int keyLen = 8;
  15280. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  15281. byte cipher[8];
  15282. byte plain[8];
  15283. /* Init stack variables */
  15284. XMEMSET(cipher, 0, sizeof(cipher));
  15285. XMEMSET(plain, 0, sizeof(plain));
  15286. /* Use for async. */
  15287. ret = wc_Arc4Init(&enc, NULL, INVALID_DEVID);
  15288. if (ret == 0) {
  15289. ret = wc_Arc4Init(&dec, NULL, INVALID_DEVID);
  15290. }
  15291. printf(testingFmt, "wc_Arc4Process()");
  15292. if (ret == 0) {
  15293. ret = wc_Arc4SetKey(&enc, (byte*)key, keyLen);
  15294. }
  15295. if (ret == 0) {
  15296. ret = wc_Arc4SetKey(&dec, (byte*)key, keyLen);
  15297. }
  15298. if (ret == 0) {
  15299. ret = wc_Arc4Process(&enc, cipher, (byte*)input, keyLen);
  15300. }
  15301. if (ret == 0) {
  15302. ret = wc_Arc4Process(&dec, plain, cipher, keyLen);
  15303. if (ret != 0 || XMEMCMP(plain, input, keyLen)) {
  15304. ret = WOLFSSL_FATAL_ERROR;
  15305. } else {
  15306. ret = 0;
  15307. }
  15308. }
  15309. /* Bad args. */
  15310. if (ret == 0) {
  15311. ret = wc_Arc4Process(NULL, plain, cipher, keyLen);
  15312. if (ret == BAD_FUNC_ARG) {
  15313. ret = wc_Arc4Process(&dec, NULL, cipher, keyLen);
  15314. }
  15315. if (ret == BAD_FUNC_ARG) {
  15316. ret = wc_Arc4Process(&dec, plain, NULL, keyLen);
  15317. }
  15318. if (ret == BAD_FUNC_ARG) {
  15319. ret = 0;
  15320. } else {
  15321. ret = WOLFSSL_FATAL_ERROR;
  15322. }
  15323. }
  15324. printf(resultFmt, ret == 0 ? passed : failed);
  15325. wc_Arc4Free(&enc);
  15326. wc_Arc4Free(&dec);
  15327. #endif
  15328. return ret;
  15329. }/* END test_wc_Arc4Process */
  15330. /*
  15331. * Testing wc_Init RsaKey()
  15332. */
  15333. static int test_wc_InitRsaKey (void)
  15334. {
  15335. int ret = 0;
  15336. #ifndef NO_RSA
  15337. RsaKey key;
  15338. printf(testingFmt, "wc_InitRsaKey()");
  15339. ret = wc_InitRsaKey(&key, HEAP_HINT);
  15340. /* Test bad args. */
  15341. if (ret == 0) {
  15342. ret = wc_InitRsaKey(NULL, HEAP_HINT);
  15343. #ifndef HAVE_USER_RSA
  15344. if (ret == BAD_FUNC_ARG) {
  15345. ret = 0;
  15346. } else {
  15347. #else
  15348. if (ret == USER_CRYPTO_ERROR) {
  15349. ret = 0;
  15350. } else {
  15351. #endif
  15352. ret = WOLFSSL_FATAL_ERROR;
  15353. }
  15354. } /* end if */
  15355. if (wc_FreeRsaKey(&key) || ret != 0) {
  15356. ret = WOLFSSL_FATAL_ERROR;
  15357. }
  15358. printf(resultFmt, ret == 0 ? passed : failed);
  15359. #endif
  15360. return ret;
  15361. } /* END test_wc_InitRsaKey */
  15362. /*
  15363. * Testing wc_RsaPrivateKeyDecode()
  15364. */
  15365. static int test_wc_RsaPrivateKeyDecode (void)
  15366. {
  15367. int ret = 0;
  15368. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  15369. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  15370. RsaKey key;
  15371. byte* tmp;
  15372. word32 idx = 0;
  15373. int bytes = 0;
  15374. printf(testingFmt, "wc_RsaPrivateKeyDecode()");
  15375. tmp = (byte*)XMALLOC(FOURK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  15376. if (tmp == NULL) {
  15377. ret = WOLFSSL_FATAL_ERROR;
  15378. }
  15379. if (ret == 0) {
  15380. ret = wc_InitRsaKey(&key, HEAP_HINT);
  15381. }
  15382. if (ret == 0) {
  15383. #ifdef USE_CERT_BUFFERS_1024
  15384. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  15385. bytes = sizeof_client_key_der_1024;
  15386. #else
  15387. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  15388. bytes = sizeof_client_key_der_2048;
  15389. #endif /* Use cert buffers. */
  15390. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
  15391. }
  15392. #ifndef HAVE_USER_RSA
  15393. /* Test bad args. */
  15394. if (ret == 0) {
  15395. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  15396. if (ret == BAD_FUNC_ARG) {
  15397. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  15398. }
  15399. if (ret == BAD_FUNC_ARG) {
  15400. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  15401. }
  15402. if (ret == BAD_FUNC_ARG) {
  15403. ret = 0;
  15404. } else {
  15405. ret = WOLFSSL_FATAL_ERROR;
  15406. }
  15407. }
  15408. #else
  15409. /* Test bad args. User RSA. */
  15410. if (ret == 0) {
  15411. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  15412. if (ret == USER_CRYPTO_ERROR) {
  15413. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  15414. }
  15415. if (ret == USER_CRYPTO_ERROR) {
  15416. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  15417. }
  15418. if (ret == USER_CRYPTO_ERROR) {
  15419. ret = 0;
  15420. } else {
  15421. ret = WOLFSSL_FATAL_ERROR;
  15422. }
  15423. }
  15424. #endif
  15425. if (tmp != NULL) {
  15426. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  15427. }
  15428. if (wc_FreeRsaKey(&key) || ret != 0) {
  15429. ret = WOLFSSL_FATAL_ERROR;
  15430. }
  15431. printf(resultFmt, ret == 0 ? passed : failed);
  15432. #endif
  15433. return ret;
  15434. } /* END test_wc_RsaPrivateKeyDecode */
  15435. /*
  15436. * Testing wc_RsaPublicKeyDecode()
  15437. */
  15438. static int test_wc_RsaPublicKeyDecode (void)
  15439. {
  15440. int ret = 0;
  15441. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  15442. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  15443. RsaKey keyPub;
  15444. byte* tmp;
  15445. word32 idx = 0;
  15446. int bytes = 0;
  15447. word32 keySz = 0;
  15448. word32 tstKeySz = 0;
  15449. tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  15450. if (tmp == NULL) {
  15451. ret = WOLFSSL_FATAL_ERROR;
  15452. }
  15453. if (ret == 0) {
  15454. ret = wc_InitRsaKey(&keyPub, HEAP_HINT);
  15455. }
  15456. if (ret == 0) {
  15457. #ifdef USE_CERT_BUFFERS_1024
  15458. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  15459. bytes = sizeof_client_keypub_der_1024;
  15460. keySz = 1024;
  15461. #else
  15462. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  15463. bytes = sizeof_client_keypub_der_2048;
  15464. keySz = 2048;
  15465. #endif
  15466. printf(testingFmt, "wc_RsaPublicKeyDecode()");
  15467. ret = wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes);
  15468. }
  15469. #ifndef HAVE_USER_RSA
  15470. /* Pass in bad args. */
  15471. if (ret == 0) {
  15472. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  15473. if (ret == BAD_FUNC_ARG) {
  15474. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  15475. }
  15476. if (ret == BAD_FUNC_ARG) {
  15477. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  15478. }
  15479. if (ret == BAD_FUNC_ARG) {
  15480. ret = 0;
  15481. } else {
  15482. ret = WOLFSSL_FATAL_ERROR;
  15483. }
  15484. }
  15485. #else
  15486. /* Pass in bad args. */
  15487. if (ret == 0) {
  15488. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  15489. if (ret == USER_CRYPTO_ERROR) {
  15490. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  15491. }
  15492. if (ret == USER_CRYPTO_ERROR) {
  15493. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  15494. }
  15495. if (ret == USER_CRYPTO_ERROR) {
  15496. ret = 0;
  15497. } else {
  15498. ret = WOLFSSL_FATAL_ERROR;
  15499. }
  15500. }
  15501. #endif
  15502. if (wc_FreeRsaKey(&keyPub) || ret != 0) {
  15503. ret = WOLFSSL_FATAL_ERROR;
  15504. }
  15505. if (ret == 0) {
  15506. /* Test for getting modulus key size */
  15507. idx = 0;
  15508. ret = wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  15509. &tstKeySz, NULL, NULL);
  15510. ret = (ret == 0 && tstKeySz == keySz/8) ? 0 : WOLFSSL_FATAL_ERROR;
  15511. }
  15512. if (tmp != NULL) {
  15513. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  15514. }
  15515. printf(resultFmt, ret == 0 ? passed : failed);
  15516. #endif
  15517. return ret;
  15518. } /* END test_wc_RsaPublicKeyDecode */
  15519. /*
  15520. * Testing wc_RsaPublicKeyDecodeRaw()
  15521. */
  15522. static int test_wc_RsaPublicKeyDecodeRaw (void)
  15523. {
  15524. int ret = 0;
  15525. #if !defined(NO_RSA)
  15526. RsaKey key;
  15527. const byte n = 0x23;
  15528. const byte e = 0x03;
  15529. int nSz = sizeof(n);
  15530. int eSz = sizeof(e);
  15531. printf(testingFmt, "wc_RsaPublicKeyDecodeRaw()");
  15532. ret = wc_InitRsaKey(&key, HEAP_HINT);
  15533. if (ret == 0) {
  15534. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key);
  15535. }
  15536. #ifndef HAVE_USER_RSA
  15537. /* Pass in bad args. */
  15538. if (ret == 0) {
  15539. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  15540. if (ret == BAD_FUNC_ARG) {
  15541. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  15542. }
  15543. if (ret == BAD_FUNC_ARG) {
  15544. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  15545. }
  15546. if (ret == BAD_FUNC_ARG) {
  15547. ret = 0;
  15548. } else {
  15549. ret = WOLFSSL_FATAL_ERROR;
  15550. }
  15551. }
  15552. #else
  15553. /* Pass in bad args. User RSA. */
  15554. if (ret == 0) {
  15555. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  15556. if (ret == USER_CRYPTO_ERROR) {
  15557. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  15558. }
  15559. if (ret == USER_CRYPTO_ERROR) {
  15560. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  15561. }
  15562. if (ret == USER_CRYPTO_ERROR) {
  15563. ret = 0;
  15564. } else {
  15565. ret = WOLFSSL_FATAL_ERROR;
  15566. }
  15567. }
  15568. #endif
  15569. if (wc_FreeRsaKey(&key) || ret != 0) {
  15570. ret = WOLFSSL_FATAL_ERROR;
  15571. }
  15572. printf(resultFmt, ret == 0 ? passed : failed);
  15573. #endif
  15574. return ret;
  15575. } /* END test_wc_RsaPublicKeyDecodeRaw */
  15576. #if (!defined(NO_RSA) || !defined(HAVE_FAST_RSA)) && defined(WOLFSSL_KEY_GEN)
  15577. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  15578. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  15579. * trying until it gets a probable prime. */
  15580. #ifdef HAVE_FIPS
  15581. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  15582. {
  15583. int ret;
  15584. for (;;) {
  15585. ret = wc_MakeRsaKey(key, size, e, rng);
  15586. if (ret != PRIME_GEN_E) break;
  15587. printf("MakeRsaKey couldn't find prime; trying again.\n");
  15588. }
  15589. return ret;
  15590. }
  15591. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  15592. #else
  15593. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  15594. #endif
  15595. #endif
  15596. /*
  15597. * Testing wc_MakeRsaKey()
  15598. */
  15599. static int test_wc_MakeRsaKey (void)
  15600. {
  15601. int ret = 0;
  15602. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  15603. RsaKey genKey;
  15604. WC_RNG rng;
  15605. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  15606. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  15607. int bits = 1024;
  15608. #else
  15609. int bits = 2048;
  15610. #endif
  15611. printf(testingFmt, "wc_MakeRsaKey()");
  15612. ret = wc_InitRsaKey(&genKey, HEAP_HINT);
  15613. if (ret == 0) {
  15614. ret = wc_InitRng(&rng);
  15615. if (ret == 0) {
  15616. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
  15617. if (ret == 0 && wc_FreeRsaKey(&genKey) != 0) {
  15618. ret = WOLFSSL_FATAL_ERROR;
  15619. }
  15620. }
  15621. }
  15622. #ifndef HAVE_USER_RSA
  15623. /* Test bad args. */
  15624. if (ret == 0) {
  15625. ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
  15626. if (ret == BAD_FUNC_ARG) {
  15627. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
  15628. }
  15629. if (ret == BAD_FUNC_ARG) {
  15630. /* e < 3 */
  15631. ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
  15632. }
  15633. if (ret == BAD_FUNC_ARG) {
  15634. /* e & 1 == 0 */
  15635. ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
  15636. }
  15637. if (ret == BAD_FUNC_ARG) {
  15638. ret = 0;
  15639. } else {
  15640. ret = WOLFSSL_FATAL_ERROR;
  15641. }
  15642. }
  15643. #else
  15644. /* Test bad args. */
  15645. if (ret == 0) {
  15646. ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
  15647. if (ret == USER_CRYPTO_ERROR) {
  15648. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
  15649. }
  15650. if (ret == USER_CRYPTO_ERROR) {
  15651. /* e < 3 */
  15652. ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
  15653. }
  15654. if (ret == USER_CRYPTO_ERROR) {
  15655. /* e & 1 == 0 */
  15656. ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
  15657. }
  15658. if (ret == USER_CRYPTO_ERROR) {
  15659. ret = 0;
  15660. } else {
  15661. ret = WOLFSSL_FATAL_ERROR;
  15662. }
  15663. }
  15664. #endif
  15665. if (wc_FreeRng(&rng) || ret != 0) {
  15666. ret = WOLFSSL_FATAL_ERROR;
  15667. }
  15668. printf(resultFmt, ret == 0 ? passed : failed);
  15669. #endif
  15670. return ret;
  15671. } /* END test_wc_MakeRsaKey */
  15672. /*
  15673. * Test the bounds checking on the cipher text versus the key modulus.
  15674. * 1. Make a new RSA key.
  15675. * 2. Set c to 1.
  15676. * 3. Decrypt c into k. (error)
  15677. * 4. Copy the key modulus to c and sub 1 from the copy.
  15678. * 5. Decrypt c into k. (error)
  15679. * Valid bounds test cases are covered by all the other RSA tests.
  15680. */
  15681. static int test_RsaDecryptBoundsCheck(void)
  15682. {
  15683. int ret = 0;
  15684. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  15685. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  15686. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  15687. RsaKey key;
  15688. byte flatC[256];
  15689. word32 flatCSz;
  15690. byte out[256];
  15691. word32 outSz = sizeof(out);
  15692. WC_RNG rng;
  15693. printf(testingFmt, "RSA decrypt bounds check");
  15694. XMEMSET(&rng, 0, sizeof(rng));
  15695. ret = wc_InitRng(&rng);
  15696. if (ret == 0)
  15697. ret = wc_InitRsaKey(&key, HEAP_HINT);
  15698. if (ret == 0) {
  15699. const byte* derKey;
  15700. word32 derKeySz;
  15701. word32 idx = 0;
  15702. #ifdef USE_CERT_BUFFERS_1024
  15703. derKey = server_key_der_1024;
  15704. derKeySz = (word32)sizeof_server_key_der_1024;
  15705. flatCSz = 128;
  15706. #else
  15707. derKey = server_key_der_2048;
  15708. derKeySz = (word32)sizeof_server_key_der_2048;
  15709. flatCSz = 256;
  15710. #endif
  15711. ret = wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz);
  15712. }
  15713. if (ret == 0) {
  15714. XMEMSET(flatC, 0, flatCSz);
  15715. flatC[flatCSz-1] = 1;
  15716. ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  15717. RSA_PRIVATE_DECRYPT, &rng);
  15718. if (ret == RSA_OUT_OF_RANGE_E) {
  15719. mp_int c;
  15720. mp_init_copy(&c, &key.n);
  15721. mp_sub_d(&c, 1, &c);
  15722. mp_to_unsigned_bin(&c, flatC);
  15723. ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  15724. RSA_PRIVATE_DECRYPT, NULL);
  15725. mp_clear(&c);
  15726. }
  15727. if (ret == RSA_OUT_OF_RANGE_E)
  15728. ret = 0;
  15729. else
  15730. ret = WOLFSSL_FATAL_ERROR;
  15731. }
  15732. if (wc_FreeRsaKey(&key) || wc_FreeRng(&rng) || ret != 0)
  15733. ret = WOLFSSL_FATAL_ERROR;
  15734. printf(resultFmt, ret == 0 ? passed : failed);
  15735. #endif
  15736. return ret;
  15737. } /* END test_wc_RsaDecryptBoundsCheck */
  15738. /*
  15739. * Testing wc_SetKeyUsage()
  15740. */
  15741. static int test_wc_SetKeyUsage (void)
  15742. {
  15743. int ret = 0;
  15744. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  15745. Cert myCert;
  15746. ret = wc_InitCert(&myCert);
  15747. printf(testingFmt, "wc_SetKeyUsage()");
  15748. if (ret == 0) {
  15749. ret = wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement");
  15750. if (ret == 0) {
  15751. ret = wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation");
  15752. }
  15753. if (ret == 0) {
  15754. ret = wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly");
  15755. }
  15756. if (ret == 0) {
  15757. ret = wc_SetKeyUsage(&myCert, "decipherOnly");
  15758. }
  15759. if (ret == 0) {
  15760. ret = wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign");
  15761. }
  15762. }
  15763. /* Test bad args. */
  15764. if (ret == 0) {
  15765. ret = wc_SetKeyUsage(NULL, "decipherOnly");
  15766. if (ret == BAD_FUNC_ARG) {
  15767. ret = wc_SetKeyUsage(&myCert, NULL);
  15768. }
  15769. if (ret == BAD_FUNC_ARG) {
  15770. ret = wc_SetKeyUsage(&myCert, "");
  15771. }
  15772. if (ret == KEYUSAGE_E) {
  15773. ret = wc_SetKeyUsage(&myCert, ",");
  15774. }
  15775. if (ret == KEYUSAGE_E) {
  15776. ret = wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign");
  15777. }
  15778. if (ret == KEYUSAGE_E) {
  15779. ret = 0;
  15780. } else {
  15781. ret = WOLFSSL_FATAL_ERROR;
  15782. }
  15783. }
  15784. printf(resultFmt, ret == 0 ? passed : failed);
  15785. #endif
  15786. return ret;
  15787. } /* END test_wc_SetKeyUsage */
  15788. /*
  15789. * Testing wc_CheckProbablePrime()
  15790. */
  15791. static int test_wc_CheckProbablePrime (void)
  15792. {
  15793. int ret = 0;
  15794. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  15795. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  15796. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  15797. RsaKey key;
  15798. WC_RNG rng;
  15799. byte e[3];
  15800. word32 eSz = (word32)sizeof(e);
  15801. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  15802. word32 nSz = (word32)sizeof(n);
  15803. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  15804. word32 dSz = (word32)sizeof(d);
  15805. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  15806. word32 pSz = (word32)sizeof(p);
  15807. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  15808. word32 qSz = (word32)sizeof(q);
  15809. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  15810. int* isPrime;
  15811. int test[5];
  15812. isPrime = test;
  15813. printf(testingFmt, "wc_CheckProbablePrime()");
  15814. ret = wc_InitRsaKey(&key, HEAP_HINT);
  15815. if (ret == 0) {
  15816. ret = wc_InitRng(&rng);
  15817. }
  15818. if (ret == 0) {
  15819. ret = wc_RsaSetRNG(&key, &rng);
  15820. }
  15821. if (ret == 0) {
  15822. ret = wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS, WC_RSA_EXPONENT, &rng);
  15823. }
  15824. if (ret == 0) {
  15825. PRIVATE_KEY_UNLOCK();
  15826. ret = wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz,
  15827. p, &pSz, q, &qSz);
  15828. PRIVATE_KEY_LOCK();
  15829. }
  15830. /* Bad cases */
  15831. if (ret == 0) {
  15832. ret = wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz,
  15833. nlen, isPrime);
  15834. if (ret == BAD_FUNC_ARG) {
  15835. ret = 0;
  15836. }
  15837. }
  15838. if (ret == 0) {
  15839. ret = wc_CheckProbablePrime(p, 0, q, qSz, e, eSz,
  15840. nlen, isPrime);
  15841. if (ret == BAD_FUNC_ARG) {
  15842. ret = 0;
  15843. }
  15844. }
  15845. if (ret == 0) {
  15846. ret = wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz,
  15847. nlen, isPrime);
  15848. if (ret == BAD_FUNC_ARG) {
  15849. ret = 0;
  15850. }
  15851. }
  15852. if (ret == 0) {
  15853. ret = wc_CheckProbablePrime(p, pSz, q, 0, e, eSz,
  15854. nlen, isPrime);
  15855. if (ret == BAD_FUNC_ARG) {
  15856. ret = 0;
  15857. }
  15858. }
  15859. if (ret == 0) {
  15860. ret = wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz,
  15861. nlen, isPrime);
  15862. if (ret == BAD_FUNC_ARG) {
  15863. ret = 0;
  15864. }
  15865. }
  15866. if (ret == 0) {
  15867. ret = wc_CheckProbablePrime(p, pSz, q, qSz, e, 0,
  15868. nlen, isPrime);
  15869. if (ret == BAD_FUNC_ARG) {
  15870. ret = 0;
  15871. }
  15872. }
  15873. if (ret == 0) {
  15874. ret = wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0,
  15875. nlen, isPrime);
  15876. if (ret == BAD_FUNC_ARG) {
  15877. ret = 0;
  15878. }
  15879. }
  15880. /* Good case */
  15881. if (ret == 0) {
  15882. ret = wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz,
  15883. nlen, isPrime);
  15884. }
  15885. wc_FreeRsaKey(&key);
  15886. wc_FreeRng(&rng);
  15887. printf(resultFmt, ret == 0 ? passed : failed);
  15888. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  15889. #endif
  15890. return ret;
  15891. } /* END test_wc_CheckProbablePrime */
  15892. /*
  15893. * Testing wc_RsaPSS_Verify()
  15894. */
  15895. static int test_wc_RsaPSS_Verify (void)
  15896. {
  15897. int ret = 0;
  15898. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  15899. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  15900. RsaKey key;
  15901. WC_RNG rng;
  15902. int sz = 256;
  15903. byte* pt;
  15904. const char* szMessage = "This is the string to be signed";
  15905. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  15906. unsigned char pDecrypted[2048/8];
  15907. word32 outLen = sizeof(pDecrypted);
  15908. pt = pDecrypted;
  15909. printf(testingFmt, "wc_RsaPSS_Verify()");
  15910. ret = wc_InitRsaKey(&key, HEAP_HINT);
  15911. if (ret == 0) {
  15912. ret = wc_InitRng(&rng);
  15913. }
  15914. if (ret == 0) {
  15915. ret = wc_RsaSetRNG(&key, &rng);
  15916. }
  15917. if (ret == 0) {
  15918. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  15919. }
  15920. if (ret == 0) {
  15921. ret = wc_RsaPSS_Sign((byte*)szMessage, (word32)XSTRLEN(szMessage)+1,
  15922. pSignature, sizeof(pSignature),
  15923. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  15924. if (ret > 0 ){
  15925. sz = ret;
  15926. ret = 0;
  15927. }
  15928. }
  15929. /* Bad cases */
  15930. if (ret == 0) {
  15931. ret = wc_RsaPSS_Verify(NULL, sz, pt, outLen,
  15932. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  15933. if (ret == BAD_FUNC_ARG) {
  15934. ret = 0;
  15935. }
  15936. }
  15937. if (ret == 0) {
  15938. ret = wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  15939. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  15940. if (ret == BAD_FUNC_ARG) {
  15941. ret = 0;
  15942. }
  15943. }
  15944. if (ret == 0) {
  15945. ret = wc_RsaPSS_Verify(pSignature, sz, NULL, outLen,
  15946. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  15947. if (ret == BAD_FUNC_ARG) {
  15948. ret = 0;
  15949. }
  15950. }
  15951. if (ret == 0) {
  15952. ret = wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  15953. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  15954. if (ret == BAD_FUNC_ARG) {
  15955. ret = 0;
  15956. }
  15957. }
  15958. /* Good case */
  15959. if (ret == 0) {
  15960. ret = wc_RsaPSS_Verify(pSignature, sz, pt, outLen,
  15961. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  15962. if (ret > 0) {
  15963. ret = 0;
  15964. }
  15965. }
  15966. wc_FreeRsaKey(&key);
  15967. wc_FreeRng(&rng);
  15968. printf(resultFmt, ret == 0 ? passed : failed);
  15969. #endif
  15970. return ret;
  15971. } /* END test_wc_RsaPSS_Verify */
  15972. /*
  15973. * Testing wc_RsaPSS_VerifyCheck()
  15974. */
  15975. static int test_wc_RsaPSS_VerifyCheck (void)
  15976. {
  15977. int ret = 0;
  15978. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  15979. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  15980. RsaKey key;
  15981. WC_RNG rng;
  15982. int sz = 256; /* 2048/8 */
  15983. byte* pt;
  15984. byte digest[32];
  15985. word32 digestSz = sizeof(digest);
  15986. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  15987. word32 pSignatureSz = sizeof(pSignature);
  15988. unsigned char pDecrypted[2048/8];
  15989. word32 outLen = sizeof(pDecrypted);
  15990. pt = pDecrypted;
  15991. printf(testingFmt, "wc_RsaPSS_VerifyCheck()");
  15992. XMEMSET(digest, 0, sizeof(digest));
  15993. XMEMSET(pSignature, 0, sizeof(pSignature));
  15994. ret = wc_InitRsaKey(&key, HEAP_HINT);
  15995. if (ret == 0) {
  15996. ret = wc_InitRng(&rng);
  15997. }
  15998. if (ret == 0) {
  15999. ret = wc_RsaSetRNG(&key, &rng);
  16000. }
  16001. if (ret == 0) {
  16002. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  16003. }
  16004. if (ret == 0) {
  16005. digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256);
  16006. ret = wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz);
  16007. }
  16008. if (ret == 0) {
  16009. ret = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  16010. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  16011. if (ret > 0 ){
  16012. sz = ret;
  16013. ret = 0;
  16014. }
  16015. }
  16016. /* Bad cases */
  16017. if (ret == 0) {
  16018. ret = wc_RsaPSS_VerifyCheck(NULL, sz, pt, outLen,
  16019. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16020. if (ret == BAD_FUNC_ARG) {
  16021. ret = 0;
  16022. }
  16023. }
  16024. if (ret == 0) {
  16025. ret = wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen,
  16026. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16027. if (ret == BAD_FUNC_ARG) {
  16028. ret = 0;
  16029. }
  16030. }
  16031. if (ret == 0) {
  16032. ret = wc_RsaPSS_VerifyCheck(pSignature, sz, NULL, outLen,
  16033. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16034. if (ret == BAD_FUNC_ARG) {
  16035. ret = 0;
  16036. }
  16037. }
  16038. if (ret == 0) {
  16039. ret = wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen,
  16040. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16041. if (ret == BAD_FUNC_ARG) {
  16042. ret = 0;
  16043. }
  16044. }
  16045. /* Good case */
  16046. if (ret == 0) {
  16047. ret = wc_RsaPSS_VerifyCheck(pSignature, sz, pt, outLen,
  16048. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16049. if (ret > 0) {
  16050. ret = 0;
  16051. }
  16052. }
  16053. wc_FreeRsaKey(&key);
  16054. wc_FreeRng(&rng);
  16055. printf(resultFmt, ret == 0 ? passed : failed);
  16056. #endif
  16057. return ret;
  16058. } /* END test_wc_RsaPSS_VerifyCheck */
  16059. /*
  16060. * Testing wc_RsaPSS_VerifyCheckInline()
  16061. */
  16062. static int test_wc_RsaPSS_VerifyCheckInline (void)
  16063. {
  16064. int ret = 0;
  16065. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16066. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16067. RsaKey key;
  16068. WC_RNG rng;
  16069. int sz = 256;
  16070. byte* pt;
  16071. byte digest[32];
  16072. word32 digestSz = sizeof(digest);
  16073. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16074. unsigned char pDecrypted[2048/8];
  16075. pt = pDecrypted;
  16076. printf(testingFmt, "wc_RsaPSS_VerifyCheckInline()");
  16077. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16078. XMEMSET(digest, 0, sizeof(digest));
  16079. XMEMSET(pSignature, 0, sizeof(pSignature));
  16080. if (ret == 0) {
  16081. ret = wc_InitRng(&rng);
  16082. }
  16083. if (ret == 0) {
  16084. ret = wc_RsaSetRNG(&key, &rng);
  16085. }
  16086. if (ret == 0) {
  16087. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  16088. }
  16089. if (ret == 0) {
  16090. digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256);
  16091. ret = wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz);
  16092. }
  16093. if (ret == 0) {
  16094. ret = wc_RsaPSS_Sign(digest, digestSz, pSignature, sizeof(pSignature),
  16095. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  16096. if (ret > 0 ){
  16097. sz = ret;
  16098. ret = 0;
  16099. }
  16100. }
  16101. /* Bad Cases */
  16102. if (ret == 0) {
  16103. ret = wc_RsaPSS_VerifyCheckInline(NULL, sz, &pt,
  16104. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16105. if (ret == BAD_FUNC_ARG) {
  16106. ret = 0;
  16107. }
  16108. }
  16109. if (ret == 0) {
  16110. ret = wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL,
  16111. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16112. if (ret == BAD_FUNC_ARG) {
  16113. ret = 0;
  16114. }
  16115. }
  16116. if (ret == 0) {
  16117. ret = wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt,
  16118. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16119. if (ret == BAD_FUNC_ARG) {
  16120. ret = 0;
  16121. }
  16122. }
  16123. if (ret == 0) {
  16124. ret = wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt,
  16125. digest, digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key);
  16126. if (ret == BAD_FUNC_ARG) {
  16127. ret = 0;
  16128. }
  16129. }
  16130. /* Good case */
  16131. if (ret == 0) {
  16132. ret = wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt,
  16133. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16134. if (ret > 0) {
  16135. ret = 0;
  16136. }
  16137. }
  16138. wc_FreeRsaKey(&key);
  16139. wc_FreeRng(&rng);
  16140. printf(resultFmt, ret == 0 ? passed : failed);
  16141. #endif
  16142. return ret;
  16143. } /* END test_wc_RsaPSS_VerifyCheckInline */
  16144. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16145. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  16146. {
  16147. (void)flag;
  16148. (void)type;
  16149. (void)file;
  16150. (void)line;
  16151. }
  16152. #endif
  16153. /*
  16154. * Testing wc_LockMutex_ex
  16155. */
  16156. static int test_wc_LockMutex_ex (void)
  16157. {
  16158. int ret = 0;
  16159. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16160. int flag = CRYPTO_LOCK;
  16161. int type = 0;
  16162. const char* file = "./test-LockMutex_ex.txt";
  16163. int line = 0;
  16164. printf(testingFmt, "wc_LockMutex_ex()");
  16165. /*without SetMutexCb*/
  16166. ret = wc_LockMutex_ex(flag, type, file, line);
  16167. if (ret == BAD_STATE_E) {
  16168. ret = 0;
  16169. }
  16170. /*with SetMutexCb*/
  16171. if (ret == 0) {
  16172. ret = wc_SetMutexCb(sample_mutex_cb);
  16173. if (ret == 0) {
  16174. ret = wc_LockMutex_ex(flag, type, file, line);
  16175. }
  16176. }
  16177. printf(resultFmt, ret == 0 ? passed : failed);
  16178. #endif
  16179. return ret;
  16180. }/*End test_wc_LockMutex_ex*/
  16181. /*
  16182. * Testing wc_SetMutexCb
  16183. */
  16184. static int test_wc_SetMutexCb (void)
  16185. {
  16186. int ret = 0;
  16187. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16188. printf(testingFmt, "wc_SetMutexCb()");
  16189. ret = wc_SetMutexCb(sample_mutex_cb);
  16190. printf(resultFmt, ret == 0 ? passed : failed);
  16191. #endif
  16192. return ret;
  16193. }/*End test_wc_SetMutexCb*/
  16194. /*
  16195. * Testing wc_RsaKeyToDer()
  16196. */
  16197. static int test_wc_RsaKeyToDer (void)
  16198. {
  16199. int ret = 0;
  16200. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16201. RsaKey genKey;
  16202. WC_RNG rng;
  16203. byte* der;
  16204. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16205. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16206. int bits = 1024;
  16207. word32 derSz = 611;
  16208. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  16209. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  16210. #else
  16211. int bits = 2048;
  16212. word32 derSz = 1196;
  16213. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  16214. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  16215. #endif
  16216. XMEMSET(&rng, 0, sizeof(rng));
  16217. XMEMSET(&genKey, 0, sizeof(genKey));
  16218. der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16219. if (der == NULL) {
  16220. ret = WOLFSSL_FATAL_ERROR;
  16221. }
  16222. /* Init structures. */
  16223. if (ret == 0) {
  16224. ret = wc_InitRsaKey(&genKey, HEAP_HINT);
  16225. }
  16226. if (ret == 0) {
  16227. ret = wc_InitRng(&rng);
  16228. }
  16229. /* Make key. */
  16230. if (ret == 0) {
  16231. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
  16232. if (ret != 0) {
  16233. ret = WOLFSSL_FATAL_ERROR;
  16234. }
  16235. }
  16236. printf(testingFmt, "wc_RsaKeyToDer()");
  16237. if (ret == 0) {
  16238. ret = wc_RsaKeyToDer(&genKey, der, derSz);
  16239. if (ret > 0) {
  16240. ret = 0;
  16241. } else {
  16242. ret = WOLFSSL_FATAL_ERROR;
  16243. }
  16244. }
  16245. #ifndef HAVE_USER_RSA
  16246. /* Pass good/bad args. */
  16247. if (ret == 0) {
  16248. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  16249. if (ret == BAD_FUNC_ARG) {
  16250. /* Get just the output length */
  16251. ret = wc_RsaKeyToDer(&genKey, NULL, 0);
  16252. }
  16253. if (ret > 0) {
  16254. /* Try Public Key. */
  16255. genKey.type = 0;
  16256. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  16257. #ifdef WOLFSSL_CHECK_MEM_ZERO
  16258. /* Put back to Private Key */
  16259. genKey.type = 1;
  16260. #endif
  16261. }
  16262. if (ret == BAD_FUNC_ARG) {
  16263. ret = 0;
  16264. } else {
  16265. ret = WOLFSSL_FATAL_ERROR;
  16266. }
  16267. }
  16268. #else
  16269. /* Pass good/bad args. */
  16270. if (ret == 0) {
  16271. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  16272. if (ret == USER_CRYPTO_ERROR) {
  16273. /* Get just the output length */
  16274. ret = wc_RsaKeyToDer(&genKey, NULL, 0);
  16275. }
  16276. if (ret > 0) {
  16277. /* Try Public Key. */
  16278. genKey.type = 0;
  16279. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  16280. #ifdef WOLFSSL_CHECK_MEM_ZERO
  16281. /* Put back to Private Key */
  16282. genKey.type = 1;
  16283. #endif
  16284. }
  16285. if (ret == USER_CRYPTO_ERROR) {
  16286. ret = 0;
  16287. } else {
  16288. ret = WOLFSSL_FATAL_ERROR;
  16289. }
  16290. }
  16291. #endif
  16292. if (der != NULL) {
  16293. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16294. }
  16295. if (wc_FreeRsaKey(&genKey) || ret != 0) {
  16296. ret = WOLFSSL_FATAL_ERROR;
  16297. }
  16298. if (wc_FreeRng(&rng) || ret != 0) {
  16299. ret = WOLFSSL_FATAL_ERROR;
  16300. }
  16301. printf(resultFmt, ret == 0 ? passed : failed);
  16302. #endif
  16303. return ret;
  16304. } /* END test_wc_RsaKeyToDer */
  16305. /*
  16306. * Testing wc_RsaKeyToPublicDer()
  16307. */
  16308. static int test_wc_RsaKeyToPublicDer (void)
  16309. {
  16310. int ret = 0;
  16311. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16312. RsaKey key;
  16313. WC_RNG rng;
  16314. byte* der;
  16315. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16316. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16317. int bits = 1024;
  16318. word32 derLen = 162;
  16319. #else
  16320. int bits = 2048;
  16321. word32 derLen = 294;
  16322. #endif
  16323. XMEMSET(&rng, 0, sizeof(rng));
  16324. XMEMSET(&key, 0, sizeof(key));
  16325. der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16326. if (der == NULL) {
  16327. ret = WOLFSSL_FATAL_ERROR;
  16328. }
  16329. if (ret == 0) {
  16330. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16331. }
  16332. if (ret == 0) {
  16333. ret = wc_InitRng(&rng);
  16334. }
  16335. if (ret == 0) {
  16336. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  16337. }
  16338. printf(testingFmt, "wc_RsaKeyToPublicDer()");
  16339. if (ret == 0) {
  16340. /* test getting size only */
  16341. ret = wc_RsaKeyToPublicDer(&key, NULL, derLen);
  16342. if (ret >= 0)
  16343. ret = 0;
  16344. }
  16345. if (ret == 0) {
  16346. ret = wc_RsaKeyToPublicDer(&key, der, derLen);
  16347. if (ret >= 0) {
  16348. ret = 0;
  16349. } else {
  16350. ret = WOLFSSL_FATAL_ERROR;
  16351. }
  16352. }
  16353. if (ret == 0) {
  16354. /* test getting size only */
  16355. ret = wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0);
  16356. if (ret >= 0)
  16357. ret = 0;
  16358. }
  16359. if (ret == 0) {
  16360. ret = wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0);
  16361. if (ret >= 0) {
  16362. ret = 0;
  16363. } else {
  16364. ret = WOLFSSL_FATAL_ERROR;
  16365. }
  16366. }
  16367. #ifndef HAVE_USER_RSA
  16368. /* Pass in bad args. */
  16369. if (ret == 0) {
  16370. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  16371. if (ret == BAD_FUNC_ARG) {
  16372. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  16373. }
  16374. if (ret == BUFFER_E || ret == BAD_FUNC_ARG) {
  16375. ret = 0;
  16376. } else {
  16377. ret = WOLFSSL_FATAL_ERROR;
  16378. }
  16379. }
  16380. #else
  16381. /* Pass in bad args. */
  16382. if (ret == 0) {
  16383. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  16384. if (ret == USER_CRYPTO_ERROR) {
  16385. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  16386. }
  16387. if (ret == USER_CRYPTO_ERROR) {
  16388. ret = 0;
  16389. } else {
  16390. ret = WOLFSSL_FATAL_ERROR;
  16391. }
  16392. }
  16393. #endif
  16394. if (der != NULL) {
  16395. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16396. }
  16397. if (wc_FreeRsaKey(&key) || ret != 0) {
  16398. ret = WOLFSSL_FATAL_ERROR;
  16399. }
  16400. if (wc_FreeRng(&rng) || ret != 0) {
  16401. ret = WOLFSSL_FATAL_ERROR;
  16402. }
  16403. printf(resultFmt, ret == 0 ? passed : failed);
  16404. #endif
  16405. return ret;
  16406. } /* END test_wc_RsaKeyToPublicDer */
  16407. /*
  16408. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  16409. */
  16410. static int test_wc_RsaPublicEncryptDecrypt (void)
  16411. {
  16412. int ret = 0;
  16413. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16414. RsaKey key;
  16415. WC_RNG rng;
  16416. const char inStr[] = TEST_STRING;
  16417. const word32 plainLen = (word32)TEST_STRING_SZ;
  16418. const word32 inLen = (word32)TEST_STRING_SZ;
  16419. int bits = TEST_RSA_BITS;
  16420. const word32 cipherLen = TEST_RSA_BYTES;
  16421. word32 cipherLenResult = cipherLen;
  16422. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16423. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16424. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  16425. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16426. if (in == NULL || plain == NULL || cipher == NULL) {
  16427. printf("test_wc_RsaPublicEncryptDecrypt malloc failed\n");
  16428. return MEMORY_E;
  16429. }
  16430. #endif
  16431. XMEMCPY(in, inStr, inLen);
  16432. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16433. if (ret == 0) {
  16434. ret = wc_InitRng(&rng);
  16435. }
  16436. if (ret == 0) {
  16437. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  16438. }
  16439. /* Encrypt. */
  16440. printf(testingFmt, "wc_RsaPublicEncrypt()");
  16441. if (ret == 0) {
  16442. ret = wc_RsaPublicEncrypt(in, inLen, cipher, cipherLen, &key, &rng);
  16443. if (ret >= 0) {
  16444. cipherLenResult = ret;
  16445. ret = 0;
  16446. } else {
  16447. ret = WOLFSSL_FATAL_ERROR;
  16448. }
  16449. }
  16450. /* Pass bad args. */
  16451. /* Tests PsaPublicEncryptEx() which, is tested by another fn. No need dup.*/
  16452. printf(resultFmt, ret == 0 ? passed : failed);
  16453. if (ret != 0) {
  16454. return ret;
  16455. }
  16456. /* Decrypt */
  16457. printf(testingFmt, "wc_RsaPrivateDecrypt()");
  16458. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  16459. /* Bind rng */
  16460. if (ret == 0) {
  16461. ret = wc_RsaSetRNG(&key, &rng);
  16462. }
  16463. #endif
  16464. if (ret == 0) {
  16465. ret = wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen, &key);
  16466. }
  16467. if (ret >= 0) {
  16468. ret = XMEMCMP(plain, inStr, plainLen);
  16469. }
  16470. /* Pass in bad args. */
  16471. /* Tests RsaPrivateDecryptEx() which, is tested by another fn. No need dup.*/
  16472. WC_FREE_VAR(in, NULL);
  16473. WC_FREE_VAR(plain, NULL);
  16474. WC_FREE_VAR(cipher, NULL);
  16475. if (wc_FreeRsaKey(&key) || ret != 0) {
  16476. ret = WOLFSSL_FATAL_ERROR;
  16477. }
  16478. if (wc_FreeRng(&rng) || ret != 0) {
  16479. ret = WOLFSSL_FATAL_ERROR;
  16480. }
  16481. printf(resultFmt, ret == 0 ? passed : failed);
  16482. #endif
  16483. return ret;
  16484. } /* END test_wc_RsaPublicEncryptDecrypt */
  16485. /*
  16486. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  16487. */
  16488. static int test_wc_RsaPublicEncryptDecrypt_ex (void)
  16489. {
  16490. int ret = 0;
  16491. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  16492. && !defined(WC_NO_RSA_OAEP) && !defined(HAVE_USER_RSA)\
  16493. && !defined(NO_SHA)
  16494. RsaKey key;
  16495. WC_RNG rng;
  16496. const char inStr[] = TEST_STRING;
  16497. const word32 inLen = (word32)TEST_STRING_SZ;
  16498. const word32 plainSz = (word32)TEST_STRING_SZ;
  16499. byte* res = NULL;
  16500. int idx = 0;
  16501. int bits = TEST_RSA_BITS;
  16502. const word32 cipherSz = TEST_RSA_BYTES;
  16503. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16504. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16505. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  16506. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16507. if (in == NULL || plain == NULL || cipher == NULL) {
  16508. printf("test_wc_RsaPublicEncryptDecrypt_exmalloc failed\n");
  16509. return MEMORY_E;
  16510. }
  16511. #endif
  16512. XMEMCPY(in, inStr, inLen);
  16513. /* Initialize stack structures. */
  16514. XMEMSET(&rng, 0, sizeof(rng));
  16515. XMEMSET(&key, 0, sizeof(key));
  16516. ret = wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID);
  16517. if (ret == 0) {
  16518. ret = wc_InitRng(&rng);
  16519. }
  16520. if (ret == 0) {
  16521. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  16522. }
  16523. /* Encrypt */
  16524. printf(testingFmt, "wc_RsaPublicEncrypt_ex()");
  16525. if (ret == 0) {
  16526. ret = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key, &rng,
  16527. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  16528. if (ret >= 0) {
  16529. idx = ret;
  16530. ret = 0;
  16531. } else {
  16532. ret = WOLFSSL_FATAL_ERROR;
  16533. }
  16534. }
  16535. /*Pass bad args.*/
  16536. /* Tests RsaPublicEncryptEx again. No need duplicate. */
  16537. printf(resultFmt, ret == 0 ? passed : failed);
  16538. if (ret != 0) {
  16539. return ret;
  16540. }
  16541. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16542. /* Decrypt */
  16543. printf(testingFmt, "wc_RsaPrivateDecrypt_ex()");
  16544. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  16545. if (ret == 0) {
  16546. ret = wc_RsaSetRNG(&key, &rng);
  16547. }
  16548. #endif
  16549. if (ret == 0) {
  16550. ret = wc_RsaPrivateDecrypt_ex(cipher, (word32)idx,
  16551. plain, plainSz, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA,
  16552. WC_MGF1SHA1, NULL, 0);
  16553. }
  16554. if (ret >= 0) {
  16555. if (!XMEMCMP(plain, inStr, plainSz)) {
  16556. ret = 0;
  16557. } else {
  16558. ret = WOLFSSL_FATAL_ERROR;
  16559. }
  16560. }
  16561. /*Pass bad args.*/
  16562. /* Tests RsaPrivateDecryptEx() again. No need duplicate. */
  16563. printf(resultFmt, ret == 0 ? passed : failed);
  16564. if (ret != 0) {
  16565. return ret;
  16566. }
  16567. printf(testingFmt, "wc_RsaPrivateDecryptInline_ex()");
  16568. if (ret == 0) {
  16569. ret = wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx,
  16570. &res, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA,
  16571. WC_MGF1SHA1, NULL, 0);
  16572. if (ret >= 0) {
  16573. if (!XMEMCMP(inStr, res, plainSz)) {
  16574. ret = 0;
  16575. } else {
  16576. ret = WOLFSSL_FATAL_ERROR;
  16577. }
  16578. }
  16579. }
  16580. #endif
  16581. WC_FREE_VAR(in, NULL);
  16582. WC_FREE_VAR(plain, NULL);
  16583. WC_FREE_VAR(cipher, NULL);
  16584. if (wc_FreeRsaKey(&key) || ret != 0) {
  16585. ret = WOLFSSL_FATAL_ERROR;
  16586. }
  16587. if (wc_FreeRng(&rng) || ret != 0) {
  16588. ret = WOLFSSL_FATAL_ERROR;
  16589. }
  16590. printf(resultFmt, ret == 0 ? passed : failed);
  16591. #endif
  16592. return ret;
  16593. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  16594. /*
  16595. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  16596. */
  16597. static int test_wc_RsaSSL_SignVerify (void)
  16598. {
  16599. int ret = 0;
  16600. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16601. RsaKey key;
  16602. WC_RNG rng;
  16603. const char inStr[] = TEST_STRING;
  16604. const word32 plainSz = (word32)TEST_STRING_SZ;
  16605. const word32 inLen = (word32)TEST_STRING_SZ;
  16606. word32 idx = 0;
  16607. int bits = TEST_RSA_BITS;
  16608. const word32 outSz = TEST_RSA_BYTES;
  16609. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16610. WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  16611. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16612. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16613. if (in == NULL || out == NULL || plain == NULL) {
  16614. printf("test_wc_RsaSSL_SignVerify failed\n");
  16615. return MEMORY_E;
  16616. }
  16617. #endif
  16618. XMEMCPY(in, inStr, inLen);
  16619. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16620. if (ret == 0) {
  16621. ret = wc_InitRng(&rng);
  16622. }
  16623. if (ret == 0) {
  16624. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  16625. }
  16626. /* Sign. */
  16627. printf(testingFmt, "wc_RsaSSL_Sign()");
  16628. if (ret == 0) {
  16629. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng);
  16630. if (ret == (int)outSz) {
  16631. idx = ret;
  16632. ret = 0;
  16633. } else {
  16634. ret = WOLFSSL_FATAL_ERROR;
  16635. }
  16636. }
  16637. #ifndef HAVE_USER_RSA
  16638. /* Test bad args. */
  16639. if (ret == 0) {
  16640. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  16641. if (ret == BAD_FUNC_ARG) {
  16642. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  16643. }
  16644. if (ret == BAD_FUNC_ARG) {
  16645. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  16646. }
  16647. if (ret == BAD_FUNC_ARG) {
  16648. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  16649. }
  16650. if (ret == BAD_FUNC_ARG) {
  16651. ret = 0;
  16652. } else {
  16653. ret = WOLFSSL_FATAL_ERROR;
  16654. }
  16655. }
  16656. #else
  16657. /* Test bad args. */
  16658. if (ret == 0) {
  16659. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  16660. if (ret == USER_CRYPTO_ERROR) {
  16661. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  16662. }
  16663. if (ret == USER_CRYPTO_ERROR) {
  16664. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  16665. }
  16666. if (ret == USER_CRYPTO_ERROR) {
  16667. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  16668. }
  16669. if (ret == USER_CRYPTO_ERROR) {
  16670. ret = 0;
  16671. } else {
  16672. ret = WOLFSSL_FATAL_ERROR;
  16673. }
  16674. }
  16675. #endif
  16676. printf(resultFmt, ret == 0 ? passed : failed);
  16677. if (ret != 0) {
  16678. return ret;
  16679. }
  16680. /* Verify. */
  16681. printf(testingFmt, "wc_RsaSSL_Verify()");
  16682. if (ret == 0) {
  16683. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, &key);
  16684. if (ret == (int)inLen) {
  16685. ret = 0;
  16686. } else {
  16687. ret = WOLFSSL_FATAL_ERROR;
  16688. }
  16689. }
  16690. #ifndef HAVE_USER_RSA
  16691. /* Pass bad args. */
  16692. if (ret == 0) {
  16693. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  16694. if (ret == BAD_FUNC_ARG) {
  16695. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  16696. }
  16697. if (ret == BAD_FUNC_ARG) {
  16698. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  16699. }
  16700. if (ret == BAD_FUNC_ARG) {
  16701. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  16702. }
  16703. if (ret == BAD_FUNC_ARG) {
  16704. ret = 0;
  16705. } else {
  16706. ret = WOLFSSL_FATAL_ERROR;
  16707. }
  16708. }
  16709. #else
  16710. /* Pass bad args. */
  16711. if (ret == 0) {
  16712. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  16713. if (ret == USER_CRYPTO_ERROR) {
  16714. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  16715. }
  16716. if (ret == USER_CRYPTO_ERROR) {
  16717. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  16718. }
  16719. if (ret == USER_CRYPTO_ERROR) {
  16720. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  16721. }
  16722. if (ret == USER_CRYPTO_ERROR) {
  16723. ret = 0;
  16724. } else {
  16725. ret = WOLFSSL_FATAL_ERROR;
  16726. }
  16727. }
  16728. #endif
  16729. WC_FREE_VAR(in, NULL);
  16730. WC_FREE_VAR(out, NULL);
  16731. WC_FREE_VAR(plain, NULL);
  16732. if (wc_FreeRsaKey(&key) || ret != 0) {
  16733. ret = WOLFSSL_FATAL_ERROR;
  16734. }
  16735. if (wc_FreeRng(&rng) || ret != 0) {
  16736. ret = WOLFSSL_FATAL_ERROR;
  16737. }
  16738. printf(resultFmt, ret == 0 ? passed : failed);
  16739. #endif
  16740. return ret;
  16741. } /* END test_wc_RsaSSL_SignVerify */
  16742. /*
  16743. * Testing wc_RsaEncryptSize()
  16744. */
  16745. static int test_wc_RsaEncryptSize (void)
  16746. {
  16747. int ret = 0;
  16748. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16749. RsaKey key;
  16750. WC_RNG rng;
  16751. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16752. if (ret == 0) {
  16753. ret = wc_InitRng(&rng);
  16754. }
  16755. printf(testingFmt, "wc_RsaEncryptSize()");
  16756. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16757. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16758. if (ret == 0) {
  16759. ret = MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng);
  16760. if (ret == 0) {
  16761. ret = wc_RsaEncryptSize(&key);
  16762. }
  16763. if (ret == 128) {
  16764. ret = 0;
  16765. } else {
  16766. ret = WOLFSSL_FATAL_ERROR;
  16767. }
  16768. }
  16769. if (wc_FreeRsaKey(&key) || ret != 0) {
  16770. ret = WOLFSSL_FATAL_ERROR;
  16771. } else {
  16772. ret = 0;
  16773. }
  16774. #endif
  16775. if (ret == 0) {
  16776. ret = MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng);
  16777. if (ret == 0) {
  16778. ret = wc_RsaEncryptSize(&key);
  16779. }
  16780. if (ret == 256) {
  16781. ret = 0;
  16782. } else {
  16783. ret = WOLFSSL_FATAL_ERROR;
  16784. }
  16785. }
  16786. /* Pass in bad arg. */
  16787. if (ret == 0) {
  16788. ret = wc_RsaEncryptSize(NULL);
  16789. #ifndef HAVE_USER_RSA
  16790. if (ret == BAD_FUNC_ARG) {
  16791. ret = 0;
  16792. } else {
  16793. ret = WOLFSSL_FATAL_ERROR;
  16794. }
  16795. #endif
  16796. }
  16797. if (wc_FreeRsaKey(&key) || ret != 0) {
  16798. ret = WOLFSSL_FATAL_ERROR;
  16799. }
  16800. if (wc_FreeRng(&rng) || ret != 0) {
  16801. ret = WOLFSSL_FATAL_ERROR;
  16802. }
  16803. printf(resultFmt, ret == 0 ? passed : failed);
  16804. #endif
  16805. return ret;
  16806. } /* END test_wc_RsaEncryptSize*/
  16807. /*
  16808. * Testing wc_RsaFlattenPublicKey()
  16809. */
  16810. static int test_wc_RsaFlattenPublicKey (void)
  16811. {
  16812. int ret = 0;
  16813. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16814. RsaKey key;
  16815. WC_RNG rng;
  16816. byte e[256];
  16817. byte n[256];
  16818. word32 eSz = sizeof(e);
  16819. word32 nSz = sizeof(n);
  16820. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16821. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16822. int bits = 1024;
  16823. #else
  16824. int bits = 2048;
  16825. #endif
  16826. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16827. if (ret == 0) {
  16828. ret = wc_InitRng(&rng);
  16829. }
  16830. if (ret == 0) {
  16831. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  16832. if (ret >= 0) {
  16833. ret = 0;
  16834. } else {
  16835. ret = WOLFSSL_FATAL_ERROR;
  16836. }
  16837. }
  16838. printf(testingFmt, "wc_RsaFlattenPublicKey()");
  16839. if (ret == 0) {
  16840. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz);
  16841. }
  16842. #ifndef HAVE_USER_RSA
  16843. /* Pass bad args. */
  16844. if (ret == 0) {
  16845. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  16846. if (ret == BAD_FUNC_ARG) {
  16847. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  16848. }
  16849. if (ret == BAD_FUNC_ARG) {
  16850. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  16851. }
  16852. if (ret == BAD_FUNC_ARG) {
  16853. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  16854. }
  16855. if (ret == BAD_FUNC_ARG) {
  16856. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  16857. }
  16858. if (ret == BAD_FUNC_ARG) {
  16859. ret = 0;
  16860. } else {
  16861. ret = WOLFSSL_FATAL_ERROR;
  16862. }
  16863. }
  16864. #else
  16865. /* Pass bad args. */
  16866. if (ret == 0) {
  16867. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  16868. if (ret == USER_CRYPTO_ERROR) {
  16869. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  16870. }
  16871. if (ret == USER_CRYPTO_ERROR) {
  16872. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  16873. }
  16874. if (ret == USER_CRYPTO_ERROR) {
  16875. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  16876. }
  16877. if (ret == USER_CRYPTO_ERROR) {
  16878. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  16879. }
  16880. if (ret == USER_CRYPTO_ERROR) {
  16881. ret = 0;
  16882. } else {
  16883. ret = WOLFSSL_FATAL_ERROR;
  16884. }
  16885. }
  16886. #endif
  16887. if (wc_FreeRsaKey(&key) || ret != 0) {
  16888. ret = WOLFSSL_FATAL_ERROR;
  16889. }
  16890. if (wc_FreeRng(&rng) || ret != 0) {
  16891. ret = WOLFSSL_FATAL_ERROR;
  16892. }
  16893. printf(resultFmt, ret == 0 ? passed : failed);
  16894. #endif
  16895. return ret;
  16896. } /* END test_wc_RsaFlattenPublicKey */
  16897. /*
  16898. * unit test for wc_AesCcmSetKey
  16899. */
  16900. static int test_wc_AesCcmSetKey (void)
  16901. {
  16902. int ret = 0;
  16903. #ifdef HAVE_AESCCM
  16904. Aes aes;
  16905. const byte key16[] =
  16906. {
  16907. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  16908. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  16909. };
  16910. const byte key24[] =
  16911. {
  16912. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16913. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16914. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16915. };
  16916. const byte key32[] =
  16917. {
  16918. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16919. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16920. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16921. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16922. };
  16923. printf(testingFmt, "wc_AesCcmSetKey()");
  16924. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  16925. if (ret != 0)
  16926. return ret;
  16927. #ifdef WOLFSSL_AES_128
  16928. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  16929. #endif
  16930. #ifdef WOLFSSL_AES_192
  16931. if (ret == 0) {
  16932. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24));
  16933. }
  16934. #endif
  16935. #ifdef WOLFSSL_AES_256
  16936. if (ret == 0) {
  16937. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32));
  16938. }
  16939. #endif
  16940. /* Test bad args. */
  16941. if (ret == 0) {
  16942. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1);
  16943. if (ret == BAD_FUNC_ARG) {
  16944. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1);
  16945. }
  16946. if (ret == BAD_FUNC_ARG) {
  16947. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1);
  16948. }
  16949. if (ret != BAD_FUNC_ARG) {
  16950. ret = WOLFSSL_FATAL_ERROR;
  16951. } else {
  16952. ret = 0;
  16953. }
  16954. }
  16955. wc_AesFree(&aes);
  16956. printf(resultFmt, ret == 0 ? passed : failed);
  16957. #endif
  16958. return ret;
  16959. } /* END test_wc_AesCcmSetKey */
  16960. /*
  16961. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  16962. */
  16963. static int test_wc_AesCcmEncryptDecrypt (void)
  16964. {
  16965. int ret = 0;
  16966. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  16967. Aes aes;
  16968. const byte key16[] =
  16969. {
  16970. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  16971. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  16972. };
  16973. /* plaintext */
  16974. const byte plainT[] =
  16975. {
  16976. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  16977. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  16978. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  16979. };
  16980. /* nonce */
  16981. const byte iv[] =
  16982. {
  16983. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  16984. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  16985. };
  16986. const byte c[] = /* cipher text. */
  16987. {
  16988. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  16989. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  16990. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  16991. };
  16992. const byte t[] = /* Auth tag */
  16993. {
  16994. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  16995. };
  16996. const byte authIn[] =
  16997. {
  16998. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  16999. };
  17000. byte cipherOut[sizeof(plainT)];
  17001. byte authTag[sizeof(t)];
  17002. int ccmE = WOLFSSL_FATAL_ERROR;
  17003. #ifdef HAVE_AES_DECRYPT
  17004. int ccmD = WOLFSSL_FATAL_ERROR;
  17005. byte plainOut[sizeof(cipherOut)];
  17006. #endif
  17007. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  17008. if (ret != 0)
  17009. return ret;
  17010. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  17011. if (ret == 0) {
  17012. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17013. iv, sizeof(iv), authTag, sizeof(authTag),
  17014. authIn , sizeof(authIn));
  17015. if ((XMEMCMP(cipherOut, c, sizeof(c)) && ccmE == 0) ||
  17016. XMEMCMP(t, authTag, sizeof(t))) {
  17017. ccmE = WOLFSSL_FATAL_ERROR;
  17018. ret = WOLFSSL_FATAL_ERROR;
  17019. }
  17020. #ifdef HAVE_AES_DECRYPT
  17021. if (ret == 0) {
  17022. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  17023. sizeof(plainOut), iv, sizeof(iv),
  17024. authTag, sizeof(authTag),
  17025. authIn, sizeof(authIn));
  17026. if (XMEMCMP(plainOut, plainT, sizeof(plainT)) && ccmD == 0) {
  17027. ccmD = WOLFSSL_FATAL_ERROR;
  17028. }
  17029. }
  17030. #endif
  17031. }
  17032. printf(testingFmt, "wc_AesCcmEncrypt()");
  17033. /* Pass in bad args. Encrypt*/
  17034. if (ret == 0 && ccmE == 0) {
  17035. ccmE = wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  17036. iv, sizeof(iv), authTag, sizeof(authTag),
  17037. authIn , sizeof(authIn));
  17038. if (ccmE == BAD_FUNC_ARG) {
  17039. ccmE = wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  17040. iv, sizeof(iv), authTag, sizeof(authTag),
  17041. authIn , sizeof(authIn));
  17042. }
  17043. if (ccmE == BAD_FUNC_ARG) {
  17044. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  17045. iv, sizeof(iv), authTag, sizeof(authTag),
  17046. authIn , sizeof(authIn));
  17047. }
  17048. if (ccmE == BAD_FUNC_ARG) {
  17049. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17050. NULL, sizeof(iv), authTag, sizeof(authTag),
  17051. authIn , sizeof(authIn));
  17052. }
  17053. if (ccmE == BAD_FUNC_ARG) {
  17054. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17055. iv, sizeof(iv), NULL, sizeof(authTag),
  17056. authIn , sizeof(authIn));
  17057. }
  17058. if (ccmE == BAD_FUNC_ARG) {
  17059. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17060. iv, sizeof(iv) + 1, authTag, sizeof(authTag),
  17061. authIn , sizeof(authIn));
  17062. }
  17063. if (ccmE == BAD_FUNC_ARG) {
  17064. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17065. iv, sizeof(iv) - 7, authTag, sizeof(authTag),
  17066. authIn , sizeof(authIn));
  17067. }
  17068. if (ccmE != BAD_FUNC_ARG) {
  17069. ccmE = WOLFSSL_FATAL_ERROR;
  17070. } else {
  17071. ccmE = 0;
  17072. }
  17073. } /* End Encrypt */
  17074. printf(resultFmt, ccmE == 0 ? passed : failed);
  17075. if (ccmE != 0) {
  17076. wc_AesFree(&aes);
  17077. return ccmE;
  17078. }
  17079. #ifdef HAVE_AES_DECRYPT
  17080. printf(testingFmt, "wc_AesCcmDecrypt()");
  17081. /* Pass in bad args. Decrypt*/
  17082. if (ret == 0 && ccmD == 0) {
  17083. ccmD = wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  17084. iv, sizeof(iv), authTag, sizeof(authTag),
  17085. authIn, sizeof(authIn));
  17086. if (ccmD == BAD_FUNC_ARG) {
  17087. ccmD = wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  17088. iv, sizeof(iv), authTag, sizeof(authTag),
  17089. authIn, sizeof(authIn));
  17090. }
  17091. if (ccmD == BAD_FUNC_ARG) {
  17092. ccmD = wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  17093. iv, sizeof(iv), authTag, sizeof(authTag),
  17094. authIn, sizeof(authIn));
  17095. }
  17096. if (ccmD == BAD_FUNC_ARG) {
  17097. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  17098. sizeof(plainOut), NULL, sizeof(iv),
  17099. authTag, sizeof(authTag),
  17100. authIn, sizeof(authIn));
  17101. }
  17102. if (ccmD == BAD_FUNC_ARG) {
  17103. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  17104. sizeof(plainOut), iv, sizeof(iv), NULL,
  17105. sizeof(authTag), authIn, sizeof(authIn));
  17106. }
  17107. if (ccmD == BAD_FUNC_ARG) {
  17108. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  17109. sizeof(plainOut), iv, sizeof(iv) + 1,
  17110. authTag, sizeof(authTag),
  17111. authIn, sizeof(authIn));
  17112. }
  17113. if (ccmD == BAD_FUNC_ARG) {
  17114. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  17115. sizeof(plainOut), iv, sizeof(iv) - 7,
  17116. authTag, sizeof(authTag),
  17117. authIn, sizeof(authIn));
  17118. }
  17119. if (ccmD != BAD_FUNC_ARG) {
  17120. ccmD = WOLFSSL_FATAL_ERROR;
  17121. } else {
  17122. ccmD = 0;
  17123. }
  17124. } /* END Decrypt */
  17125. printf(resultFmt, ccmD == 0 ? passed : failed);
  17126. if (ccmD != 0) {
  17127. return ccmD;
  17128. }
  17129. #endif
  17130. wc_AesFree(&aes);
  17131. #endif /* HAVE_AESCCM */
  17132. return ret;
  17133. } /* END test_wc_AesCcmEncryptDecrypt */
  17134. /*
  17135. * Testing wc_InitDsaKey()
  17136. */
  17137. static int test_wc_InitDsaKey (void)
  17138. {
  17139. int ret = 0;
  17140. #ifndef NO_DSA
  17141. DsaKey key;
  17142. printf(testingFmt, "wc_InitDsaKey()");
  17143. ret = wc_InitDsaKey(&key);
  17144. /* Pass in bad args. */
  17145. if (ret == 0) {
  17146. ret = wc_InitDsaKey(NULL);
  17147. if (ret == BAD_FUNC_ARG) {
  17148. ret = 0;
  17149. } else {
  17150. ret = WOLFSSL_FATAL_ERROR;
  17151. }
  17152. }
  17153. printf(resultFmt, ret == 0 ? passed : failed);
  17154. wc_FreeDsaKey(&key);
  17155. #endif
  17156. return ret;
  17157. } /* END test_wc_InitDsaKey */
  17158. /*
  17159. * Testing wc_DsaSign() and wc_DsaVerify()
  17160. */
  17161. static int test_wc_DsaSignVerify (void)
  17162. {
  17163. int ret = 0;
  17164. #if !defined(NO_DSA)
  17165. DsaKey key;
  17166. WC_RNG rng;
  17167. wc_Sha sha;
  17168. byte signature[DSA_SIG_SIZE];
  17169. byte hash[WC_SHA_DIGEST_SIZE];
  17170. word32 idx = 0;
  17171. word32 bytes;
  17172. int answer;
  17173. #ifdef USE_CERT_BUFFERS_1024
  17174. byte tmp[ONEK_BUF];
  17175. XMEMSET(tmp, 0, sizeof(tmp));
  17176. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  17177. bytes = sizeof_dsa_key_der_1024;
  17178. #elif defined(USE_CERT_BUFFERS_2048)
  17179. byte tmp[TWOK_BUF];
  17180. XMEMSET(tmp, 0, sizeof(tmp));
  17181. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  17182. bytes = sizeof_dsa_key_der_2048;
  17183. #else
  17184. byte tmp[TWOK_BUF];
  17185. XMEMSET(tmp, 0, sizeof(tmp));
  17186. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  17187. if (fp == XBADFILE) {
  17188. return WOLFSSL_BAD_FILE;
  17189. }
  17190. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  17191. XFCLOSE(fp);
  17192. #endif /* END USE_CERT_BUFFERS_1024 */
  17193. ret = wc_InitSha(&sha);
  17194. if (ret == 0) {
  17195. ret = wc_ShaUpdate(&sha, tmp, bytes);
  17196. if (ret == 0) {
  17197. ret = wc_ShaFinal(&sha, hash);
  17198. }
  17199. if (ret == 0) {
  17200. ret = wc_InitDsaKey(&key);
  17201. }
  17202. if (ret == 0) {
  17203. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  17204. }
  17205. if (ret == 0) {
  17206. ret = wc_InitRng(&rng);
  17207. }
  17208. }
  17209. printf(testingFmt, "wc_DsaSign()");
  17210. /* Sign. */
  17211. if (ret == 0) {
  17212. ret = wc_DsaSign(hash, signature, &key, &rng);
  17213. }
  17214. /* Test bad args. */
  17215. if (ret == 0) {
  17216. ret = wc_DsaSign(NULL, signature, &key, &rng);
  17217. if (ret == BAD_FUNC_ARG) {
  17218. ret = wc_DsaSign(hash, NULL, &key, &rng);
  17219. }
  17220. if (ret == BAD_FUNC_ARG) {
  17221. ret = wc_DsaSign(hash, signature, NULL, &rng);
  17222. }
  17223. if (ret == BAD_FUNC_ARG) {
  17224. ret = wc_DsaSign(hash, signature, &key, NULL);
  17225. }
  17226. if (ret == BAD_FUNC_ARG) {
  17227. ret = 0;
  17228. } else {
  17229. ret = WOLFSSL_FATAL_ERROR;
  17230. }
  17231. }
  17232. printf(resultFmt, ret == 0 ? passed : failed);
  17233. if (ret != 0) {
  17234. return ret;
  17235. }
  17236. /* Verify. */
  17237. printf(testingFmt, "wc_DsaVerify()");
  17238. ret = wc_DsaVerify(hash, signature, &key, &answer);
  17239. if (ret != 0 || answer != 1) {
  17240. ret = WOLFSSL_FATAL_ERROR;
  17241. } else {
  17242. ret = 0;
  17243. }
  17244. /* Pass in bad args. */
  17245. if (ret == 0) {
  17246. ret = wc_DsaVerify(NULL, signature, &key, &answer);
  17247. if (ret == BAD_FUNC_ARG) {
  17248. ret = wc_DsaVerify(hash, NULL, &key, &answer);
  17249. }
  17250. if (ret == BAD_FUNC_ARG) {
  17251. ret = wc_DsaVerify(hash, signature, NULL, &answer);
  17252. }
  17253. if (ret == BAD_FUNC_ARG) {
  17254. ret = wc_DsaVerify(hash, signature, &key, NULL);
  17255. }
  17256. if (ret == BAD_FUNC_ARG) {
  17257. ret = 0;
  17258. } else {
  17259. ret = WOLFSSL_FATAL_ERROR;
  17260. }
  17261. }
  17262. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  17263. /* hard set q to 0 and test fail case */
  17264. mp_free(&key.q);
  17265. mp_init(&key.q);
  17266. AssertIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  17267. mp_set(&key.q, 1);
  17268. AssertIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  17269. #endif
  17270. if (wc_FreeRng(&rng) && ret == 0) {
  17271. ret = WOLFSSL_FATAL_ERROR;
  17272. }
  17273. printf(resultFmt, ret == 0 ? passed : failed);
  17274. wc_FreeDsaKey(&key);
  17275. wc_ShaFree(&sha);
  17276. #endif
  17277. return ret;
  17278. } /* END test_wc_DsaSign */
  17279. /*
  17280. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  17281. */
  17282. static int test_wc_DsaPublicPrivateKeyDecode (void)
  17283. {
  17284. int ret = 0;
  17285. #if !defined(NO_DSA)
  17286. DsaKey key;
  17287. word32 bytes;
  17288. word32 idx = 0;
  17289. int priv = WOLFSSL_FATAL_ERROR;
  17290. int pub = WOLFSSL_FATAL_ERROR;
  17291. #ifdef USE_CERT_BUFFERS_1024
  17292. byte tmp[ONEK_BUF];
  17293. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  17294. bytes = sizeof_dsa_key_der_1024;
  17295. #elif defined(USE_CERT_BUFFERS_2048)
  17296. byte tmp[TWOK_BUF];
  17297. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  17298. bytes = sizeof_dsa_key_der_2048;
  17299. #else
  17300. byte tmp[TWOK_BUF];
  17301. XMEMSET(tmp, 0, sizeof(tmp));
  17302. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  17303. if (fp == XBADFILE)
  17304. {
  17305. return WOLFSSL_BAD_FILE;
  17306. }
  17307. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  17308. XFCLOSE(fp);
  17309. #endif /* END USE_CERT_BUFFERS_1024 */
  17310. ret = wc_InitDsaKey(&key);
  17311. printf(testingFmt, "wc_DsaPrivateKeyDecode()");
  17312. if (ret == 0) {
  17313. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  17314. /* Test bad args. */
  17315. if (priv == 0) {
  17316. priv = wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes);
  17317. if (priv == BAD_FUNC_ARG) {
  17318. priv = wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes);
  17319. }
  17320. if (priv == BAD_FUNC_ARG) {
  17321. priv = wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes);
  17322. }
  17323. if (priv == BAD_FUNC_ARG) {
  17324. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  17325. }
  17326. if (priv == ASN_PARSE_E) {
  17327. priv = 0;
  17328. } else {
  17329. priv = WOLFSSL_FATAL_ERROR;
  17330. }
  17331. }
  17332. wc_FreeDsaKey(&key);
  17333. ret = wc_InitDsaKey(&key);
  17334. }
  17335. printf(resultFmt, priv == 0 ? passed : failed);
  17336. printf(testingFmt, "wc_DsaPublicKeyDecode()");
  17337. if (ret == 0) {
  17338. idx = 0; /* Reset */
  17339. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  17340. /* Test bad args. */
  17341. if (pub == 0) {
  17342. pub = wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes);
  17343. if (pub == BAD_FUNC_ARG) {
  17344. pub = wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes);
  17345. }
  17346. if (pub == BAD_FUNC_ARG) {
  17347. pub = wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes);
  17348. }
  17349. if (pub == BAD_FUNC_ARG) {
  17350. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  17351. }
  17352. if (pub == ASN_PARSE_E) {
  17353. pub = 0;
  17354. } else {
  17355. pub = WOLFSSL_FATAL_ERROR;
  17356. }
  17357. }
  17358. } /* END Public Key */
  17359. printf(resultFmt, pub == 0 ? passed : failed);
  17360. wc_FreeDsaKey(&key);
  17361. #endif
  17362. return ret;
  17363. } /* END test_wc_DsaPublicPrivateKeyDecode */
  17364. /*
  17365. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  17366. */
  17367. static int test_wc_MakeDsaKey (void)
  17368. {
  17369. int ret = 0;
  17370. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  17371. DsaKey genKey;
  17372. WC_RNG rng;
  17373. XMEMSET(&rng, 0, sizeof(rng));
  17374. XMEMSET(&genKey, 0, sizeof(genKey));
  17375. ret = wc_InitRng(&rng);
  17376. if (ret == 0) {
  17377. ret = wc_InitDsaKey(&genKey);
  17378. }
  17379. printf(testingFmt, "wc_MakeDsaParameters()");
  17380. if (ret == 0) {
  17381. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  17382. }
  17383. /* Test bad args. */
  17384. if (ret == 0) {
  17385. ret = wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey);
  17386. if (ret == BAD_FUNC_ARG) {
  17387. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, NULL);
  17388. }
  17389. if (ret == BAD_FUNC_ARG) {
  17390. ret = wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey);
  17391. }
  17392. if (ret == BAD_FUNC_ARG) {
  17393. ret = 0;
  17394. } else {
  17395. ret = WOLFSSL_FATAL_ERROR;
  17396. }
  17397. }
  17398. printf(resultFmt, ret == 0 ? passed : failed);
  17399. printf(testingFmt, "wc_MakeDsaKey()");
  17400. if (ret == 0) {
  17401. ret = wc_MakeDsaKey(&rng, &genKey);
  17402. }
  17403. /* Test bad args. */
  17404. if (ret == 0) {
  17405. ret = wc_MakeDsaKey(NULL, &genKey);
  17406. if (ret == BAD_FUNC_ARG) {
  17407. ret = wc_MakeDsaKey(&rng, NULL);
  17408. }
  17409. if (ret == BAD_FUNC_ARG) {
  17410. ret = 0;
  17411. } else {
  17412. ret = WOLFSSL_FATAL_ERROR;
  17413. }
  17414. }
  17415. if (wc_FreeRng(&rng) && ret == 0) {
  17416. ret = WOLFSSL_FAILURE;
  17417. }
  17418. printf(resultFmt, ret == 0 ? passed : failed);
  17419. wc_FreeDsaKey(&genKey);
  17420. #endif
  17421. return ret;
  17422. } /* END test_wc_MakeDsaKey */
  17423. /*
  17424. * Testing wc_DsaKeyToDer()
  17425. */
  17426. static int test_wc_DsaKeyToDer (void)
  17427. {
  17428. int ret = 0;
  17429. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  17430. DsaKey genKey;
  17431. WC_RNG rng;
  17432. word32 bytes;
  17433. word32 idx = 0;
  17434. #ifdef USE_CERT_BUFFERS_1024
  17435. byte tmp[ONEK_BUF];
  17436. byte der[ONEK_BUF];
  17437. XMEMSET(tmp, 0, sizeof(tmp));
  17438. XMEMSET(der, 0, sizeof(der));
  17439. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  17440. bytes = sizeof_dsa_key_der_1024;
  17441. #elif defined(USE_CERT_BUFFERS_2048)
  17442. byte tmp[TWOK_BUF];
  17443. byte der[TWOK_BUF];
  17444. XMEMSET(tmp, 0, sizeof(tmp));
  17445. XMEMSET(der, 0, sizeof(der));
  17446. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  17447. bytes = sizeof_dsa_key_der_2048;
  17448. #else
  17449. byte tmp[TWOK_BUF];
  17450. byte der[TWOK_BUF];
  17451. XMEMSET(tmp, 0, sizeof(tmp));
  17452. XMEMSET(der, 0, sizeof(der));
  17453. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  17454. if (fp == XBADFILE) {
  17455. return WOLFSSL_BAD_FILE;
  17456. }
  17457. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  17458. XFCLOSE(fp);
  17459. #endif /* END USE_CERT_BUFFERS_1024 */
  17460. XMEMSET(&rng, 0, sizeof(rng));
  17461. XMEMSET(&genKey, 0, sizeof(genKey));
  17462. ret = wc_InitRng(&rng);
  17463. if (ret == 0) {
  17464. ret = wc_InitDsaKey(&genKey);
  17465. }
  17466. if (ret == 0) {
  17467. ret = wc_MakeDsaParameters(&rng, sizeof(tmp), &genKey);
  17468. if (ret == 0) {
  17469. wc_FreeDsaKey(&genKey);
  17470. ret = wc_InitDsaKey(&genKey);
  17471. }
  17472. }
  17473. if (ret == 0) {
  17474. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &genKey, bytes);
  17475. }
  17476. printf(testingFmt, "wc_DsaKeyToDer()");
  17477. if (ret == 0) {
  17478. ret = wc_DsaKeyToDer(&genKey, der, bytes);
  17479. if ( ret >= 0 && ( ret = XMEMCMP(der, tmp, bytes) ) == 0 ) {
  17480. ret = 0;
  17481. }
  17482. }
  17483. /* Test bad args. */
  17484. if (ret == 0) {
  17485. ret = wc_DsaKeyToDer(NULL, der, FOURK_BUF);
  17486. if (ret == BAD_FUNC_ARG) {
  17487. ret = wc_DsaKeyToDer(&genKey, NULL, FOURK_BUF);
  17488. }
  17489. if (ret == BAD_FUNC_ARG) {
  17490. ret = 0;
  17491. } else {
  17492. ret = WOLFSSL_FATAL_ERROR;
  17493. }
  17494. }
  17495. if (wc_FreeRng(&rng) && ret == 0) {
  17496. ret = WOLFSSL_FATAL_ERROR;
  17497. }
  17498. printf(resultFmt, ret == 0 ? passed : failed);
  17499. wc_FreeDsaKey(&genKey);
  17500. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  17501. return ret;
  17502. } /* END test_wc_DsaKeyToDer */
  17503. /*
  17504. * Testing wc_DsaKeyToPublicDer()
  17505. * (indirectly testing setDsaPublicKey())
  17506. */
  17507. static int test_wc_DsaKeyToPublicDer(void)
  17508. {
  17509. int ret = 0;
  17510. #ifndef HAVE_SELFTEST
  17511. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  17512. DsaKey genKey;
  17513. WC_RNG rng;
  17514. byte* der;
  17515. word32 sz;
  17516. printf(testingFmt, "wc_DsaKeyToPublicDer()");
  17517. der = (byte*)XMALLOC(ONEK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17518. if (der == NULL) {
  17519. ret = WOLFSSL_FATAL_ERROR;
  17520. }
  17521. if (ret == 0) {
  17522. ret = wc_InitDsaKey(&genKey);
  17523. }
  17524. if (ret == 0) {
  17525. ret = wc_InitRng(&rng);
  17526. }
  17527. if (ret == 0) {
  17528. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  17529. }
  17530. if (ret == 0) {
  17531. ret = wc_MakeDsaKey(&rng, &genKey);
  17532. }
  17533. if (ret == 0) {
  17534. ret = wc_DsaKeyToPublicDer(&genKey, der, ONEK_BUF);
  17535. if (ret >= 0) {
  17536. sz = ret;
  17537. ret = 0;
  17538. } else {
  17539. ret = WOLFSSL_FATAL_ERROR;
  17540. }
  17541. }
  17542. if (ret == 0) {
  17543. word32 idx = 0;
  17544. wc_FreeDsaKey(&genKey);
  17545. ret = wc_DsaPublicKeyDecode(der, &idx, &genKey, sz);
  17546. }
  17547. /* Test without the SubjectPublicKeyInfo header */
  17548. if (ret == 0) {
  17549. ret = wc_SetDsaPublicKey(der, &genKey, ONEK_BUF, 0);
  17550. if (ret >= 0) {
  17551. sz = ret;
  17552. ret = 0;
  17553. } else {
  17554. ret = WOLFSSL_FATAL_ERROR;
  17555. }
  17556. }
  17557. if (ret == 0) {
  17558. word32 idx = 0;
  17559. wc_FreeDsaKey(&genKey);
  17560. ret = wc_DsaPublicKeyDecode(der, &idx, &genKey, sz);
  17561. }
  17562. /* Test bad args. */
  17563. if (ret == 0) {
  17564. ret = wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF);
  17565. if (ret == BAD_FUNC_ARG) {
  17566. ret = wc_DsaKeyToPublicDer(&genKey, NULL, FOURK_BUF);
  17567. }
  17568. if (ret == BAD_FUNC_ARG) {
  17569. ret = 0;
  17570. } else {
  17571. ret = WOLFSSL_FATAL_ERROR;
  17572. }
  17573. }
  17574. if (wc_FreeRng(&rng) && ret == 0) {
  17575. ret = WOLFSSL_FATAL_ERROR;
  17576. }
  17577. printf(resultFmt, ret == 0 ? passed : failed);
  17578. XFREE(der,NULL,DYNAMIC_TYPE_TMP_BUFFER);
  17579. wc_FreeDsaKey(&genKey);
  17580. #endif /* !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN) */
  17581. #endif /* HAVE_SELFTEST */
  17582. return ret;
  17583. } /* END test_wc_DsaKeyToPublicDer */
  17584. /*
  17585. * Testing wc_DsaImportParamsRaw()
  17586. */
  17587. static int test_wc_DsaImportParamsRaw (void)
  17588. {
  17589. int ret = 0;
  17590. #if !defined(NO_DSA)
  17591. DsaKey key;
  17592. /* [mod = L=1024, N=160], from CAVP KeyPair */
  17593. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  17594. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  17595. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  17596. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  17597. "47123188f8dc551054ee162b634d60f097f719076640e209"
  17598. "80a0093113a8bd73";
  17599. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  17600. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  17601. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  17602. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  17603. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  17604. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  17605. "76341a7e7d9";
  17606. /* invalid p and q parameters */
  17607. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  17608. const char* invalidQ = "96c5390a";
  17609. printf(testingFmt, "wc_DsaImportParamsRaw()");
  17610. ret = wc_InitDsaKey(&key);
  17611. if (ret == 0) {
  17612. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  17613. }
  17614. /* test bad args */
  17615. if (ret == 0) {
  17616. /* null key struct */
  17617. ret = wc_DsaImportParamsRaw(NULL, p, q, g);
  17618. if (ret == BAD_FUNC_ARG) {
  17619. /* null param pointers */
  17620. ret = wc_DsaImportParamsRaw(&key, NULL, NULL, NULL);
  17621. }
  17622. if (ret == BAD_FUNC_ARG) {
  17623. /* illegal p length */
  17624. ret = wc_DsaImportParamsRaw(&key, invalidP, q, g);
  17625. }
  17626. if (ret == BAD_FUNC_ARG) {
  17627. /* illegal q length */
  17628. ret = wc_DsaImportParamsRaw(&key, p, invalidQ, g);
  17629. if (ret == BAD_FUNC_ARG)
  17630. ret = 0;
  17631. }
  17632. }
  17633. printf(resultFmt, ret == 0 ? passed : failed);
  17634. wc_FreeDsaKey(&key);
  17635. #endif
  17636. return ret;
  17637. } /* END test_wc_DsaImportParamsRaw */
  17638. /*
  17639. * Testing wc_DsaImportParamsRawCheck()
  17640. */
  17641. static int test_wc_DsaImportParamsRawCheck (void)
  17642. {
  17643. int ret = 0;
  17644. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  17645. DsaKey key;
  17646. int trusted = 0;
  17647. /* [mod = L=1024, N=160], from CAVP KeyPair */
  17648. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  17649. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  17650. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  17651. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  17652. "47123188f8dc551054ee162b634d60f097f719076640e209"
  17653. "80a0093113a8bd73";
  17654. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  17655. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  17656. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  17657. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  17658. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  17659. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  17660. "76341a7e7d9";
  17661. /* invalid p and q parameters */
  17662. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  17663. const char* invalidQ = "96c5390a";
  17664. printf(testingFmt, "wc_DsaImportParamsRawCheck()");
  17665. ret = wc_InitDsaKey(&key);
  17666. if (ret == 0) {
  17667. ret = wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL);
  17668. }
  17669. /* test bad args */
  17670. if (ret == 0) {
  17671. /* null key struct */
  17672. ret = wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL);
  17673. if (ret == BAD_FUNC_ARG) {
  17674. /* null param pointers */
  17675. ret = wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted, NULL);
  17676. }
  17677. if (ret == BAD_FUNC_ARG) {
  17678. /* illegal p length */
  17679. ret = wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL);
  17680. }
  17681. if (ret == BAD_FUNC_ARG) {
  17682. /* illegal q length */
  17683. ret = wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL);
  17684. if (ret == BAD_FUNC_ARG)
  17685. ret = 0;
  17686. }
  17687. }
  17688. printf(resultFmt, ret == 0 ? passed : failed);
  17689. wc_FreeDsaKey(&key);
  17690. #endif
  17691. return ret;
  17692. } /* END test_wc_DsaImportParamsRawCheck */
  17693. /*
  17694. * Testing wc_DsaExportParamsRaw()
  17695. */
  17696. static int test_wc_DsaExportParamsRaw (void)
  17697. {
  17698. int ret = 0;
  17699. #if !defined(NO_DSA)
  17700. DsaKey key;
  17701. /* [mod = L=1024, N=160], from CAVP KeyPair */
  17702. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  17703. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  17704. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  17705. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  17706. "47123188f8dc551054ee162b634d60f097f719076640e209"
  17707. "80a0093113a8bd73";
  17708. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  17709. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  17710. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  17711. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  17712. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  17713. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  17714. "76341a7e7d9";
  17715. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  17716. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  17717. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  17718. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  17719. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  17720. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  17721. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  17722. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  17723. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  17724. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  17725. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  17726. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  17727. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  17728. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  17729. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  17730. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  17731. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  17732. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  17733. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  17734. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  17735. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  17736. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  17737. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  17738. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  17739. byte pOut[MAX_DSA_PARAM_SIZE];
  17740. byte qOut[MAX_DSA_PARAM_SIZE];
  17741. byte gOut[MAX_DSA_PARAM_SIZE];
  17742. word32 pOutSz, qOutSz, gOutSz;
  17743. printf(testingFmt, "wc_DsaExportParamsRaw()");
  17744. ret = wc_InitDsaKey(&key);
  17745. if (ret == 0) {
  17746. /* first test using imported raw parameters, for expected */
  17747. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  17748. }
  17749. if (ret == 0) {
  17750. pOutSz = sizeof(pOut);
  17751. qOutSz = sizeof(qOut);
  17752. gOutSz = sizeof(gOut);
  17753. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  17754. gOut, &gOutSz);
  17755. }
  17756. if (ret == 0) {
  17757. /* validate exported parameters are correct */
  17758. if ((XMEMCMP(pOut, pCompare, pOutSz) != 0) ||
  17759. (XMEMCMP(qOut, qCompare, qOutSz) != 0) ||
  17760. (XMEMCMP(gOut, gCompare, gOutSz) != 0) ) {
  17761. ret = -1;
  17762. }
  17763. }
  17764. /* test bad args */
  17765. if (ret == 0) {
  17766. /* null key struct */
  17767. ret = wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz,
  17768. gOut, &gOutSz);
  17769. if (ret == BAD_FUNC_ARG) {
  17770. /* null output pointers */
  17771. ret = wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz,
  17772. NULL, &gOutSz);
  17773. }
  17774. if (ret == LENGTH_ONLY_E) {
  17775. /* null output size pointers */
  17776. ret = wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL,
  17777. gOut, NULL);
  17778. }
  17779. if (ret == BAD_FUNC_ARG) {
  17780. /* p output buffer size too small */
  17781. pOutSz = 1;
  17782. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  17783. gOut, &gOutSz);
  17784. pOutSz = sizeof(pOut);
  17785. }
  17786. if (ret == BUFFER_E) {
  17787. /* q output buffer size too small */
  17788. qOutSz = 1;
  17789. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  17790. gOut, &gOutSz);
  17791. qOutSz = sizeof(qOut);
  17792. }
  17793. if (ret == BUFFER_E) {
  17794. /* g output buffer size too small */
  17795. gOutSz = 1;
  17796. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  17797. gOut, &gOutSz);
  17798. if (ret == BUFFER_E)
  17799. ret = 0;
  17800. }
  17801. }
  17802. printf(resultFmt, ret == 0 ? passed : failed);
  17803. wc_FreeDsaKey(&key);
  17804. #endif
  17805. return ret;
  17806. } /* END test_wc_DsaExportParamsRaw */
  17807. /*
  17808. * Testing wc_DsaExportKeyRaw()
  17809. */
  17810. static int test_wc_DsaExportKeyRaw (void)
  17811. {
  17812. int ret = 0;
  17813. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  17814. DsaKey key;
  17815. WC_RNG rng;
  17816. byte xOut[MAX_DSA_PARAM_SIZE];
  17817. byte yOut[MAX_DSA_PARAM_SIZE];
  17818. word32 xOutSz, yOutSz;
  17819. printf(testingFmt, "wc_DsaExportKeyRaw()");
  17820. XMEMSET(&rng, 0, sizeof(rng));
  17821. XMEMSET(&key, 0, sizeof(key));
  17822. ret = wc_InitRng(&rng);
  17823. if (ret == 0) {
  17824. ret = wc_InitDsaKey(&key);
  17825. }
  17826. if (ret == 0) {
  17827. ret = wc_MakeDsaParameters(&rng, 1024, &key);
  17828. if (ret == 0) {
  17829. ret = wc_MakeDsaKey(&rng, &key);
  17830. }
  17831. }
  17832. /* try successful export */
  17833. if (ret == 0) {
  17834. xOutSz = sizeof(xOut);
  17835. yOutSz = sizeof(yOut);
  17836. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  17837. }
  17838. /* test bad args */
  17839. if (ret == 0) {
  17840. /* null key struct */
  17841. ret = wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz);
  17842. if (ret == BAD_FUNC_ARG) {
  17843. /* null output pointers */
  17844. ret = wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz);
  17845. }
  17846. if (ret == LENGTH_ONLY_E) {
  17847. /* null output size pointers */
  17848. ret = wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL);
  17849. }
  17850. if (ret == BAD_FUNC_ARG) {
  17851. /* x output buffer size too small */
  17852. xOutSz = 1;
  17853. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  17854. xOutSz = sizeof(xOut);
  17855. }
  17856. if (ret == BUFFER_E) {
  17857. /* y output buffer size too small */
  17858. yOutSz = 1;
  17859. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  17860. if (ret == BUFFER_E)
  17861. ret = 0;
  17862. }
  17863. }
  17864. printf(resultFmt, ret == 0 ? passed : failed);
  17865. wc_FreeDsaKey(&key);
  17866. wc_FreeRng(&rng);
  17867. #endif
  17868. return ret;
  17869. } /* END test_wc_DsaExportParamsRaw */
  17870. /*
  17871. * Testing wc_ed25519_make_key().
  17872. */
  17873. static int test_wc_ed25519_make_key (void)
  17874. {
  17875. int ret = 0;
  17876. #if defined(HAVE_ED25519)
  17877. ed25519_key key;
  17878. WC_RNG rng;
  17879. ret = wc_InitRng(&rng);
  17880. if (ret == 0) {
  17881. ret = wc_ed25519_init(&key);
  17882. }
  17883. printf(testingFmt, "wc_ed25519_make_key()");
  17884. if (ret == 0) {
  17885. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  17886. }
  17887. /* Test bad args. */
  17888. if (ret == 0) {
  17889. ret = wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key);
  17890. if (ret == BAD_FUNC_ARG) {
  17891. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL);
  17892. }
  17893. if (ret == BAD_FUNC_ARG) {
  17894. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key);
  17895. }
  17896. if (ret == BAD_FUNC_ARG) {
  17897. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key);
  17898. }
  17899. if (ret == BAD_FUNC_ARG) {
  17900. ret = 0;
  17901. } else if (ret == 0) {
  17902. ret = WOLFSSL_FATAL_ERROR;
  17903. }
  17904. }
  17905. printf(resultFmt, ret == 0 ? passed : failed);
  17906. if (wc_FreeRng(&rng) && ret == 0) {
  17907. ret = WOLFSSL_FATAL_ERROR;
  17908. }
  17909. wc_ed25519_free(&key);
  17910. #endif
  17911. return ret;
  17912. } /* END test_wc_ed25519_make_key */
  17913. /*
  17914. * Testing wc_ed25519_init()
  17915. */
  17916. static int test_wc_ed25519_init (void)
  17917. {
  17918. int ret = 0;
  17919. #if defined(HAVE_ED25519)
  17920. ed25519_key key;
  17921. printf(testingFmt, "wc_ed25519_init()");
  17922. ret = wc_ed25519_init(&key);
  17923. /* Test bad args. */
  17924. if (ret == 0) {
  17925. ret = wc_ed25519_init(NULL);
  17926. if (ret == BAD_FUNC_ARG) {
  17927. ret = 0;
  17928. } else if (ret == 0) {
  17929. ret = WOLFSSL_FATAL_ERROR;
  17930. }
  17931. }
  17932. printf(resultFmt, ret == 0 ? passed : failed);
  17933. wc_ed25519_free(&key);
  17934. #endif
  17935. return ret;
  17936. } /* END test_wc_ed25519_init */
  17937. /*
  17938. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  17939. */
  17940. static int test_wc_ed25519_sign_msg (void)
  17941. {
  17942. int ret = 0;
  17943. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  17944. WC_RNG rng;
  17945. ed25519_key key;
  17946. byte msg[] = "Everybody gets Friday off.\n";
  17947. byte sig[ED25519_SIG_SIZE];
  17948. word32 msglen = sizeof(msg);
  17949. word32 siglen = sizeof(sig);
  17950. word32 badSigLen = sizeof(sig) - 1;
  17951. #ifdef HAVE_ED25519_VERIFY
  17952. int verify_ok = 0; /*1 = Verify success.*/
  17953. #endif
  17954. /* Initialize stack variables. */
  17955. XMEMSET(sig, 0, siglen);
  17956. /* Initialize key. */
  17957. ret = wc_InitRng(&rng);
  17958. if (ret == 0) {
  17959. ret = wc_ed25519_init(&key);
  17960. if (ret == 0) {
  17961. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  17962. }
  17963. }
  17964. printf(testingFmt, "wc_ed25519_sign_msg()");
  17965. if (ret == 0) {
  17966. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key);
  17967. }
  17968. /* Test bad args. */
  17969. if (ret == 0 && siglen == ED25519_SIG_SIZE) {
  17970. ret = wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key);
  17971. if (ret == BAD_FUNC_ARG) {
  17972. ret = wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key);
  17973. }
  17974. if (ret == BAD_FUNC_ARG) {
  17975. ret = wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key);
  17976. }
  17977. if (ret == BAD_FUNC_ARG) {
  17978. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL);
  17979. }
  17980. if (ret == BAD_FUNC_ARG) {
  17981. ret = wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key);
  17982. }
  17983. if (ret == BUFFER_E && badSigLen == ED25519_SIG_SIZE) {
  17984. badSigLen -= 1;
  17985. ret = 0;
  17986. } else if (ret == 0) {
  17987. ret = WOLFSSL_FATAL_ERROR;
  17988. }
  17989. } /* END sign */
  17990. printf(resultFmt, ret == 0 ? passed : failed);
  17991. #ifdef HAVE_ED25519_VERIFY
  17992. printf(testingFmt, "wc_ed25519_verify_msg()");
  17993. if (ret == 0) {
  17994. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key);
  17995. if (ret == 0 && verify_ok == 1) {
  17996. ret = 0;
  17997. } else if (ret == 0) {
  17998. ret = WOLFSSL_FATAL_ERROR;
  17999. }
  18000. /* Test bad args. */
  18001. if (ret == 0) {
  18002. AssertIntEQ(wc_ed25519_verify_msg(sig, siglen - 1, msg,
  18003. msglen, &verify_ok, &key),
  18004. BAD_FUNC_ARG);
  18005. AssertIntEQ(wc_ed25519_verify_msg(sig, siglen + 1, msg,
  18006. msglen, &verify_ok, &key),
  18007. BAD_FUNC_ARG);
  18008. ret = wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  18009. &key);
  18010. if (ret == BAD_FUNC_ARG) {
  18011. ret = wc_ed25519_verify_msg(sig, siglen, NULL, msglen,
  18012. &verify_ok, &key);
  18013. }
  18014. if (ret == BAD_FUNC_ARG) {
  18015. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  18016. NULL, &key);
  18017. }
  18018. if (ret == BAD_FUNC_ARG) {
  18019. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  18020. &verify_ok, NULL);
  18021. }
  18022. if (ret == BAD_FUNC_ARG) {
  18023. ret = wc_ed25519_verify_msg(sig, badSigLen, msg, msglen,
  18024. &verify_ok, &key);
  18025. }
  18026. if (ret == BAD_FUNC_ARG) {
  18027. ret = 0;
  18028. } else if (ret == 0) {
  18029. ret = WOLFSSL_FATAL_ERROR;
  18030. }
  18031. }
  18032. } /* END verify. */
  18033. printf(resultFmt, ret == 0 ? passed : failed);
  18034. #endif /* Verify. */
  18035. if (wc_FreeRng(&rng) && ret == 0) {
  18036. ret = WOLFSSL_FATAL_ERROR;
  18037. }
  18038. wc_ed25519_free(&key);
  18039. #endif
  18040. return ret;
  18041. } /* END test_wc_ed25519_sign_msg */
  18042. /*
  18043. * Testing wc_ed25519_import_public()
  18044. */
  18045. static int test_wc_ed25519_import_public (void)
  18046. {
  18047. int ret = 0;
  18048. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  18049. WC_RNG rng;
  18050. ed25519_key pubKey;
  18051. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  18052. word32 inlen = sizeof(in);
  18053. ret = wc_InitRng(&rng);
  18054. if (ret == 0) {
  18055. ret = wc_ed25519_init(&pubKey);
  18056. if (ret == 0) {
  18057. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey);
  18058. }
  18059. }
  18060. printf(testingFmt, "wc_ed25519_import_public()");
  18061. if (ret == 0) {
  18062. ret = wc_ed25519_import_public(in, inlen, &pubKey);
  18063. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  18064. ret = 0;
  18065. } else {
  18066. ret = WOLFSSL_FATAL_ERROR;
  18067. }
  18068. /* Test bad args. */
  18069. if (ret == 0) {
  18070. ret = wc_ed25519_import_public(NULL, inlen, &pubKey);
  18071. if (ret == BAD_FUNC_ARG) {
  18072. ret = wc_ed25519_import_public(in, inlen, NULL);
  18073. }
  18074. if (ret == BAD_FUNC_ARG) {
  18075. ret = wc_ed25519_import_public(in, inlen - 1, &pubKey);
  18076. }
  18077. if (ret == BAD_FUNC_ARG) {
  18078. ret = 0;
  18079. } else if (ret == 0) {
  18080. ret = WOLFSSL_FATAL_ERROR;
  18081. }
  18082. }
  18083. }
  18084. printf(resultFmt, ret == 0 ? passed : failed);
  18085. if (wc_FreeRng(&rng) && ret == 0) {
  18086. ret = WOLFSSL_FATAL_ERROR;
  18087. }
  18088. wc_ed25519_free(&pubKey);
  18089. #endif
  18090. return ret;
  18091. } /* END wc_ed25519_import_public */
  18092. /*
  18093. * Testing wc_ed25519_import_private_key()
  18094. */
  18095. static int test_wc_ed25519_import_private_key (void)
  18096. {
  18097. int ret = 0;
  18098. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  18099. WC_RNG rng;
  18100. ed25519_key key;
  18101. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  18102. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  18103. word32 privKeySz = sizeof(privKey);
  18104. word32 pubKeySz = sizeof(pubKey);
  18105. #ifdef HAVE_ED25519_KEY_EXPORT
  18106. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  18107. word32 bothKeysSz = sizeof(bothKeys);
  18108. #endif
  18109. ret = wc_InitRng(&rng);
  18110. if (ret != 0) {
  18111. return ret;
  18112. }
  18113. ret = wc_ed25519_init(&key);
  18114. if (ret != 0) {
  18115. wc_FreeRng(&rng);
  18116. return ret;
  18117. }
  18118. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18119. printf(testingFmt, "wc_ed25519_import_private_key()");
  18120. if (ret == 0) {
  18121. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  18122. pubKeySz, &key);
  18123. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
  18124. || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  18125. ret = WOLFSSL_FATAL_ERROR;
  18126. }
  18127. }
  18128. #ifdef HAVE_ED25519_KEY_EXPORT
  18129. if (ret == 0)
  18130. ret = wc_ed25519_export_private(&key, bothKeys, &bothKeysSz);
  18131. if (ret == 0) {
  18132. ret = wc_ed25519_import_private_key(bothKeys, bothKeysSz, NULL, 0, &key);
  18133. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
  18134. || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  18135. ret = WOLFSSL_FATAL_ERROR;
  18136. }
  18137. }
  18138. #endif
  18139. /* Test bad args. */
  18140. if (ret == 0) {
  18141. ret = wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  18142. &key);
  18143. if (ret == BAD_FUNC_ARG) {
  18144. ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  18145. pubKeySz, &key);
  18146. }
  18147. if (ret == BAD_FUNC_ARG) {
  18148. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  18149. pubKeySz, NULL);
  18150. }
  18151. if (ret == BAD_FUNC_ARG) {
  18152. ret = wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  18153. pubKeySz, &key);
  18154. }
  18155. if (ret == BAD_FUNC_ARG) {
  18156. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  18157. pubKeySz - 1, &key);
  18158. }
  18159. if (ret == BAD_FUNC_ARG) {
  18160. ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  18161. 0, &key);
  18162. }
  18163. if (ret == BAD_FUNC_ARG) {
  18164. ret = 0;
  18165. } else if (ret == 0) {
  18166. ret = WOLFSSL_FATAL_ERROR;
  18167. }
  18168. }
  18169. printf(resultFmt, ret == 0 ? passed : failed);
  18170. if (wc_FreeRng(&rng) && ret == 0) {
  18171. ret = WOLFSSL_FATAL_ERROR;
  18172. }
  18173. wc_ed25519_free(&key);
  18174. #endif
  18175. return ret;
  18176. } /* END test_wc_ed25519_import_private_key */
  18177. /*
  18178. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  18179. */
  18180. static int test_wc_ed25519_export (void)
  18181. {
  18182. int ret = 0;
  18183. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  18184. WC_RNG rng;
  18185. ed25519_key key;
  18186. byte priv[ED25519_PRV_KEY_SIZE];
  18187. byte pub[ED25519_PUB_KEY_SIZE];
  18188. word32 privSz = sizeof(priv);
  18189. word32 pubSz = sizeof(pub);
  18190. ret = wc_InitRng(&rng);
  18191. if (ret != 0) {
  18192. return ret;
  18193. }
  18194. ret = wc_ed25519_init(&key);
  18195. if (ret != 0) {
  18196. wc_FreeRng(&rng);
  18197. return ret;
  18198. }
  18199. if (ret == 0) {
  18200. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18201. }
  18202. printf(testingFmt, "wc_ed25519_export_public()");
  18203. if (ret == 0) {
  18204. ret = wc_ed25519_export_public(&key, pub, &pubSz);
  18205. if (ret == 0 && (pubSz != ED25519_KEY_SIZE
  18206. || XMEMCMP(key.p, pub, pubSz) != 0)) {
  18207. ret = WOLFSSL_FATAL_ERROR;
  18208. }
  18209. if (ret == 0) {
  18210. ret = wc_ed25519_export_public(NULL, pub, &pubSz);
  18211. if (ret == BAD_FUNC_ARG) {
  18212. ret = wc_ed25519_export_public(&key, NULL, &pubSz);
  18213. }
  18214. if (ret == BAD_FUNC_ARG) {
  18215. ret = wc_ed25519_export_public(&key, pub, NULL);
  18216. }
  18217. if (ret == BAD_FUNC_ARG) {
  18218. ret = 0;
  18219. } else if (ret == 0) {
  18220. ret = WOLFSSL_FATAL_ERROR;
  18221. }
  18222. }
  18223. }
  18224. printf(resultFmt, ret == 0 ? passed : failed);
  18225. printf(testingFmt, "wc_ed25519_export_private_only()");
  18226. if (ret == 0) {
  18227. ret = wc_ed25519_export_private_only(&key, priv, &privSz);
  18228. if (ret == 0 && (privSz != ED25519_KEY_SIZE
  18229. || XMEMCMP(key.k, priv, privSz) != 0)) {
  18230. ret = WOLFSSL_FATAL_ERROR;
  18231. }
  18232. if (ret == 0) {
  18233. ret = wc_ed25519_export_private_only(NULL, priv, &privSz);
  18234. if (ret == BAD_FUNC_ARG) {
  18235. ret = wc_ed25519_export_private_only(&key, NULL, &privSz);
  18236. }
  18237. if (ret == BAD_FUNC_ARG) {
  18238. ret = wc_ed25519_export_private_only(&key, priv, NULL);
  18239. }
  18240. if (ret == BAD_FUNC_ARG) {
  18241. ret = 0;
  18242. } else if (ret == 0) {
  18243. ret = WOLFSSL_FATAL_ERROR;
  18244. }
  18245. }
  18246. }
  18247. printf(resultFmt, ret == 0 ? passed : failed);
  18248. if (wc_FreeRng(&rng) && ret == 0) {
  18249. ret = WOLFSSL_FATAL_ERROR;
  18250. }
  18251. wc_ed25519_free(&key);
  18252. #endif
  18253. return ret;
  18254. } /* END test_wc_ed25519_export */
  18255. /*
  18256. * Testing wc_ed25519_size()
  18257. */
  18258. static int test_wc_ed25519_size (void)
  18259. {
  18260. int ret = 0;
  18261. #if defined(HAVE_ED25519)
  18262. WC_RNG rng;
  18263. ed25519_key key;
  18264. ret = wc_InitRng(&rng);
  18265. if (ret != 0) {
  18266. return ret;
  18267. }
  18268. ret = wc_ed25519_init(&key);
  18269. if (ret != 0) {
  18270. wc_FreeRng(&rng);
  18271. return ret;
  18272. }
  18273. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18274. if (ret != 0) {
  18275. wc_FreeRng(&rng);
  18276. wc_ed25519_free(&key);
  18277. return ret;
  18278. }
  18279. printf(testingFmt, "wc_ed25519_size()");
  18280. ret = wc_ed25519_size(&key);
  18281. /* Test bad args. */
  18282. if (ret == ED25519_KEY_SIZE) {
  18283. ret = wc_ed25519_size(NULL);
  18284. if (ret == BAD_FUNC_ARG) {
  18285. ret = 0;
  18286. }
  18287. }
  18288. printf(resultFmt, ret == 0 ? passed : failed);
  18289. if (ret == 0) {
  18290. printf(testingFmt, "wc_ed25519_sig_size()");
  18291. ret = wc_ed25519_sig_size(&key);
  18292. if (ret == ED25519_SIG_SIZE) {
  18293. ret = 0;
  18294. }
  18295. /* Test bad args. */
  18296. if (ret == 0) {
  18297. ret = wc_ed25519_sig_size(NULL);
  18298. if (ret == BAD_FUNC_ARG) {
  18299. ret = 0;
  18300. }
  18301. }
  18302. printf(resultFmt, ret == 0 ? passed : failed);
  18303. } /* END wc_ed25519_sig_size() */
  18304. if (ret == 0) {
  18305. printf(testingFmt, "wc_ed25519_pub_size");
  18306. ret = wc_ed25519_pub_size(&key);
  18307. if (ret == ED25519_PUB_KEY_SIZE) {
  18308. ret = 0;
  18309. }
  18310. if (ret == 0) {
  18311. ret = wc_ed25519_pub_size(NULL);
  18312. if (ret == BAD_FUNC_ARG) {
  18313. ret = 0;
  18314. }
  18315. }
  18316. printf(resultFmt, ret == 0 ? passed : failed);
  18317. } /* END wc_ed25519_pub_size */
  18318. if (ret == 0) {
  18319. printf(testingFmt, "wc_ed25519_priv_size");
  18320. ret = wc_ed25519_priv_size(&key);
  18321. if (ret == ED25519_PRV_KEY_SIZE) {
  18322. ret = 0;
  18323. }
  18324. if (ret == 0) {
  18325. ret = wc_ed25519_priv_size(NULL);
  18326. if (ret == BAD_FUNC_ARG) {
  18327. ret = 0;
  18328. }
  18329. }
  18330. printf(resultFmt, ret == 0 ? passed : failed);
  18331. } /* END wc_ed25519_pub_size */
  18332. if (wc_FreeRng(&rng) && ret == 0) {
  18333. ret = WOLFSSL_FATAL_ERROR;
  18334. }
  18335. wc_ed25519_free(&key);
  18336. #endif
  18337. return ret;
  18338. } /* END test_wc_ed25519_size */
  18339. /*
  18340. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  18341. */
  18342. static int test_wc_ed25519_exportKey (void)
  18343. {
  18344. int ret = 0;
  18345. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  18346. WC_RNG rng;
  18347. ed25519_key key;
  18348. byte priv[ED25519_PRV_KEY_SIZE];
  18349. byte pub[ED25519_PUB_KEY_SIZE];
  18350. byte privOnly[ED25519_PRV_KEY_SIZE];
  18351. word32 privSz = sizeof(priv);
  18352. word32 pubSz = sizeof(pub);
  18353. word32 privOnlySz = sizeof(privOnly);
  18354. ret = wc_InitRng(&rng);
  18355. if (ret != 0) {
  18356. return ret;
  18357. }
  18358. ret = wc_ed25519_init(&key);
  18359. if (ret != 0) {
  18360. wc_FreeRng(&rng);
  18361. return ret;
  18362. }
  18363. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18364. if (ret != 0) {
  18365. wc_FreeRng(&rng);
  18366. wc_ed25519_free(&key);
  18367. return ret;
  18368. }
  18369. printf(testingFmt, "wc_ed25519_export_private()");
  18370. ret = wc_ed25519_export_private(&key, privOnly, &privOnlySz);
  18371. if (ret == 0) {
  18372. ret = wc_ed25519_export_private(NULL, privOnly, &privOnlySz);
  18373. if (ret == BAD_FUNC_ARG) {
  18374. ret = wc_ed25519_export_private(&key, NULL, &privOnlySz);
  18375. }
  18376. if (ret == BAD_FUNC_ARG) {
  18377. ret = wc_ed25519_export_private(&key, privOnly, NULL);
  18378. }
  18379. if (ret == BAD_FUNC_ARG) {
  18380. ret = 0;
  18381. } else if (ret == 0) {
  18382. ret = WOLFSSL_FATAL_ERROR;
  18383. }
  18384. }
  18385. printf(resultFmt, ret == 0 ? passed : failed);
  18386. if (ret == 0) {
  18387. printf(testingFmt, "wc_ed25519_export_key()");
  18388. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz);
  18389. if (ret == 0) {
  18390. ret = wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz);
  18391. if (ret == BAD_FUNC_ARG) {
  18392. ret = wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz);
  18393. }
  18394. if (ret == BAD_FUNC_ARG) {
  18395. ret = wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz);
  18396. }
  18397. if (ret == BAD_FUNC_ARG) {
  18398. ret = wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz);
  18399. }
  18400. if (ret == BAD_FUNC_ARG) {
  18401. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, NULL);
  18402. }
  18403. if (ret == BAD_FUNC_ARG) {
  18404. ret = 0;
  18405. } else if (ret == 0) {
  18406. ret = WOLFSSL_FATAL_ERROR;
  18407. }
  18408. }
  18409. printf(resultFmt, ret == 0 ? passed : failed);
  18410. } /* END wc_ed25519_export_key() */
  18411. /* Cross check output. */
  18412. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  18413. ret = WOLFSSL_FATAL_ERROR;
  18414. }
  18415. if (wc_FreeRng(&rng) && ret == 0) {
  18416. ret = WOLFSSL_FATAL_ERROR;
  18417. }
  18418. wc_ed25519_free(&key);
  18419. #endif
  18420. return ret;
  18421. } /* END test_wc_ed25519_exportKey */
  18422. /*
  18423. * Testing wc_Ed25519PublicKeyToDer
  18424. */
  18425. static int test_wc_Ed25519PublicKeyToDer (void)
  18426. {
  18427. int ret = 0;
  18428. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  18429. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  18430. int tmp;
  18431. ed25519_key key;
  18432. byte derBuf[1024];
  18433. printf(testingFmt, "wc_Ed25519PublicKeyToDer()");
  18434. /* Test bad args */
  18435. tmp = wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0);
  18436. if (tmp != BAD_FUNC_ARG) {
  18437. ret = WOLFSSL_FATAL_ERROR;
  18438. }
  18439. if (ret == 0) {
  18440. wc_ed25519_init(&key);
  18441. tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0);
  18442. if (tmp != BUFFER_E) {
  18443. ret = WOLFSSL_FATAL_ERROR;
  18444. }
  18445. wc_ed25519_free(&key);
  18446. }
  18447. /* Test good args */
  18448. if (ret == 0) {
  18449. WC_RNG rng;
  18450. ret = wc_InitRng(&rng);
  18451. if (ret != 0) {
  18452. return ret;
  18453. }
  18454. ret = wc_ed25519_init(&key);
  18455. if (ret != 0) {
  18456. wc_FreeRng(&rng);
  18457. return ret;
  18458. }
  18459. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18460. if (ret != 0) {
  18461. wc_FreeRng(&rng);
  18462. wc_ed25519_free(&key);
  18463. return ret;
  18464. }
  18465. tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1);
  18466. if (tmp <= 0) {
  18467. ret = WOLFSSL_FATAL_ERROR;
  18468. }
  18469. wc_FreeRng(&rng);
  18470. wc_ed25519_free(&key);
  18471. }
  18472. printf(resultFmt, ret == 0 ? passed : failed);
  18473. #endif
  18474. return ret;
  18475. } /* END testing wc_Ed25519PublicKeyToDer */
  18476. /*
  18477. * Testing wc_curve25519_init and wc_curve25519_free.
  18478. */
  18479. static int test_wc_curve25519_init (void)
  18480. {
  18481. int ret = 0;
  18482. #if defined(HAVE_CURVE25519)
  18483. curve25519_key key;
  18484. printf(testingFmt, "wc_curve25519_init()");
  18485. ret = wc_curve25519_init(&key);
  18486. /* Test bad args for wc_curve25519_init */
  18487. if (ret == 0) {
  18488. ret = wc_curve25519_init(NULL);
  18489. if (ret == BAD_FUNC_ARG) {
  18490. ret = 0;
  18491. } else if (ret == 0) {
  18492. ret = WOLFSSL_FATAL_ERROR;
  18493. }
  18494. }
  18495. printf(resultFmt, ret == 0 ? passed : failed);
  18496. /* Test good args for wc_curve_25519_free */
  18497. wc_curve25519_free(&key);
  18498. wc_curve25519_free(NULL);
  18499. #endif
  18500. return ret;
  18501. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  18502. /*
  18503. * Testing test_wc_curve25519_size.
  18504. */
  18505. static int test_wc_curve25519_size (void)
  18506. {
  18507. int ret = 0;
  18508. #if defined(HAVE_CURVE25519)
  18509. curve25519_key key;
  18510. printf(testingFmt, "wc_curve25519_size()");
  18511. ret = wc_curve25519_init(&key);
  18512. /* Test good args for wc_curve25519_size */
  18513. if (ret == 0) {
  18514. ret = wc_curve25519_size(&key);
  18515. }
  18516. /* Test bad args for wc_curve25519_size */
  18517. if (ret != 0) {
  18518. ret = wc_curve25519_size(NULL);
  18519. }
  18520. printf(resultFmt, ret == 0 ? passed : failed);
  18521. wc_curve25519_free(&key);
  18522. #endif
  18523. return ret;
  18524. } /* END test_wc_curve25519_size*/
  18525. /*
  18526. * Testing test_wc_curve25519_export_key_raw().
  18527. */
  18528. static int test_wc_curve25519_export_key_raw (void)
  18529. {
  18530. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  18531. curve25519_key key;
  18532. WC_RNG rng;
  18533. byte privateKey[CURVE25519_KEYSIZE];
  18534. byte publicKey[CURVE25519_KEYSIZE];
  18535. word32 prvkSz;
  18536. word32 pubkSz;
  18537. byte prik[CURVE25519_KEYSIZE];
  18538. byte pubk[CURVE25519_KEYSIZE];
  18539. word32 prksz;
  18540. word32 pbksz;
  18541. printf(testingFmt, "wc_curve25519_export_key_raw()");
  18542. if(0 != wc_InitRng(&rng)){
  18543. printf(testingFmt, "failed due to wc_InitRng");
  18544. fflush( stdout );
  18545. return 1;
  18546. }
  18547. if(0 != wc_curve25519_init(&key)){
  18548. printf(testingFmt, "failed due to wc_curve25519_init");
  18549. fflush( stdout );
  18550. wc_FreeRng(&rng);
  18551. return 1;
  18552. }
  18553. if(0 != wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key)){
  18554. printf(testingFmt, "failed due to wc_curve25519_make_key");
  18555. fflush( stdout );
  18556. wc_curve25519_free(&key);
  18557. wc_FreeRng(&rng);
  18558. return 1;
  18559. }
  18560. /*
  18561. bad-argument-test cases
  18562. target function sould return BAD_FUNC_ARG
  18563. */
  18564. prvkSz = CURVE25519_KEYSIZE;
  18565. pubkSz = CURVE25519_KEYSIZE;
  18566. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  18567. NULL , privateKey, &prvkSz, publicKey, &pubkSz)){
  18568. printf(testingFmt,"failed at bad-arg-case-1.");
  18569. fflush( stdout );
  18570. wc_curve25519_free(&key);
  18571. wc_FreeRng(&rng);
  18572. return 1;
  18573. }
  18574. prvkSz = CURVE25519_KEYSIZE;
  18575. pubkSz = CURVE25519_KEYSIZE;
  18576. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  18577. &key , NULL, &prvkSz, publicKey, &pubkSz)){
  18578. printf(testingFmt,"failed at bad-arg-case-2.");
  18579. fflush( stdout );
  18580. wc_curve25519_free(&key);
  18581. wc_FreeRng(&rng);
  18582. return 1;
  18583. }
  18584. prvkSz = CURVE25519_KEYSIZE;
  18585. pubkSz = CURVE25519_KEYSIZE;
  18586. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  18587. &key , privateKey, NULL, publicKey, &pubkSz)){
  18588. printf(testingFmt,"failed at bad-arg-case-3.");
  18589. fflush( stdout );
  18590. wc_curve25519_free(&key);
  18591. wc_FreeRng(&rng);
  18592. return 1;
  18593. }
  18594. /* prvkSz = CURVE25519_KEYSIZE; */
  18595. pubkSz = CURVE25519_KEYSIZE;
  18596. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  18597. &key , privateKey, &prvkSz, NULL, &pubkSz)){
  18598. printf(testingFmt,"failed at bad-arg-case-4.");
  18599. fflush( stdout );
  18600. wc_curve25519_free(&key);
  18601. wc_FreeRng(&rng);
  18602. return 1;
  18603. }
  18604. prvkSz = CURVE25519_KEYSIZE;
  18605. pubkSz = CURVE25519_KEYSIZE;
  18606. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  18607. &key , privateKey, &prvkSz, publicKey, NULL )){
  18608. printf(testingFmt,"failed at bad-arg-case-5.");
  18609. fflush( stdout );
  18610. wc_curve25519_free(&key);
  18611. wc_FreeRng(&rng);
  18612. return 1;
  18613. }
  18614. /*
  18615. cross-testing
  18616. */
  18617. prksz = CURVE25519_KEYSIZE;
  18618. if( 0 != wc_curve25519_export_private_raw(&key, prik, &prksz)){
  18619. printf(testingFmt,"failed due to wc_curve25519_export_private_raw");
  18620. fflush( stdout );
  18621. wc_curve25519_free(&key);
  18622. wc_FreeRng(&rng);
  18623. return 1;
  18624. }
  18625. pbksz = CURVE25519_KEYSIZE;
  18626. if(0 != wc_curve25519_export_public(&key, pubk, &pbksz)){
  18627. printf(testingFmt,"failed due to wc_curve25519_export_public");
  18628. fflush( stdout );
  18629. wc_curve25519_free(&key);
  18630. wc_FreeRng(&rng);
  18631. return 1;
  18632. }
  18633. prvkSz = CURVE25519_KEYSIZE;
  18634. /* pubkSz = CURVE25519_KEYSIZE; */
  18635. if(0 != wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  18636. publicKey, &pubkSz)){
  18637. printf(testingFmt,"failed due to wc_curve25519_export_key_raw");
  18638. fflush( stdout );
  18639. wc_curve25519_free(&key);
  18640. wc_FreeRng(&rng);
  18641. return 1;
  18642. }
  18643. if((prksz == CURVE25519_KEYSIZE) &&
  18644. (pbksz == CURVE25519_KEYSIZE) &&
  18645. (prvkSz == CURVE25519_KEYSIZE) &&
  18646. (pubkSz == CURVE25519_KEYSIZE)){
  18647. if( 0 == XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE) &&
  18648. 0 == XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE)){
  18649. printf(resultFmt,passed);
  18650. fflush( stdout );
  18651. wc_curve25519_free(&key);
  18652. wc_FreeRng(&rng);
  18653. return 0;
  18654. }
  18655. else{
  18656. printf(testingFmt,"failed due to key-contents-inconsistency.");
  18657. fflush( stdout );
  18658. wc_curve25519_free(&key);
  18659. wc_FreeRng(&rng);
  18660. return 1;
  18661. }
  18662. }
  18663. else{
  18664. printf(testingFmt,"failed due to bad-key-size.");
  18665. fflush( stdout );
  18666. wc_curve25519_free(&key);
  18667. wc_FreeRng(&rng);
  18668. return 1;
  18669. }
  18670. #endif
  18671. fflush( stdout );
  18672. return 0;
  18673. } /* end of test_wc_curve25519_export_key_raw */
  18674. /*
  18675. * Testing test_wc_curve25519_export_key_raw_ex().
  18676. */
  18677. static int test_wc_curve25519_export_key_raw_ex (void)
  18678. {
  18679. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  18680. curve25519_key key;
  18681. WC_RNG rng;
  18682. byte privateKey[CURVE25519_KEYSIZE];
  18683. byte publicKey[CURVE25519_KEYSIZE];
  18684. word32 prvkSz;
  18685. word32 pubkSz;
  18686. byte prik[CURVE25519_KEYSIZE];
  18687. byte pubk[CURVE25519_KEYSIZE];
  18688. word32 prksz;
  18689. word32 pbksz;
  18690. printf(testingFmt, "wc_curve25519_export_key_raw_ex()");
  18691. if(0 != wc_InitRng(&rng)){
  18692. printf(testingFmt, "failed due to wc_InitRng");
  18693. fflush( stdout );
  18694. return 1;
  18695. }
  18696. if(0 != wc_curve25519_init(&key)){
  18697. printf(testingFmt, "failed due to wc_curve25519_init");
  18698. fflush( stdout );
  18699. wc_FreeRng(&rng);
  18700. return 1;
  18701. }
  18702. if(0 != wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key)){
  18703. printf(testingFmt, "failed due to wc_curve25519_make_key");
  18704. fflush( stdout );
  18705. wc_curve25519_free(&key);
  18706. wc_FreeRng(&rng);
  18707. return 1;
  18708. }
  18709. /*
  18710. bad-argument-test cases
  18711. target function sould return BAD_FUNC_ARG
  18712. */
  18713. prvkSz = CURVE25519_KEYSIZE;
  18714. pubkSz = CURVE25519_KEYSIZE;
  18715. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL , privateKey,
  18716. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
  18717. printf(testingFmt,"failed at bad-arg-case-1.");
  18718. fflush( stdout );
  18719. wc_curve25519_free(&key);
  18720. wc_FreeRng(&rng);
  18721. return 1;
  18722. }
  18723. prvkSz = CURVE25519_KEYSIZE;
  18724. pubkSz = CURVE25519_KEYSIZE;
  18725. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key , NULL,
  18726. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
  18727. printf(testingFmt,"failed at bad-arg-case-2.");
  18728. fflush( stdout );
  18729. wc_curve25519_free(&key);
  18730. wc_FreeRng(&rng);
  18731. return 1;
  18732. }
  18733. prvkSz = CURVE25519_KEYSIZE;
  18734. pubkSz = CURVE25519_KEYSIZE;
  18735. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key,privateKey,
  18736. NULL,publicKey, &pubkSz,EC25519_LITTLE_ENDIAN)){
  18737. printf(testingFmt,"failed at bad-arg-case-3.");
  18738. fflush( stdout );
  18739. wc_curve25519_free(&key);
  18740. wc_FreeRng(&rng);
  18741. return 1;
  18742. }
  18743. /* prvkSz = CURVE25519_KEYSIZE; */
  18744. pubkSz = CURVE25519_KEYSIZE;
  18745. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  18746. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN)){
  18747. printf(testingFmt,"failed at bad-arg-case-4.");
  18748. fflush( stdout );
  18749. wc_curve25519_free(&key);
  18750. wc_FreeRng(&rng);
  18751. return 1;
  18752. }
  18753. prvkSz = CURVE25519_KEYSIZE;
  18754. pubkSz = CURVE25519_KEYSIZE;
  18755. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  18756. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN)){
  18757. printf(testingFmt,"failed at bad-arg-case-5.");
  18758. fflush( stdout );
  18759. wc_curve25519_free(&key);
  18760. wc_FreeRng(&rng);
  18761. return 1;
  18762. }
  18763. prvkSz = CURVE25519_KEYSIZE;
  18764. /* pubkSz = CURVE25519_KEYSIZE; */
  18765. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL, privateKey,
  18766. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  18767. printf(testingFmt,"failed at bad-arg-case-6.");
  18768. fflush( stdout );
  18769. wc_curve25519_free(&key);
  18770. wc_FreeRng(&rng);
  18771. return 1;
  18772. }
  18773. prvkSz = CURVE25519_KEYSIZE;
  18774. pubkSz = CURVE25519_KEYSIZE;
  18775. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, NULL, &prvkSz,
  18776. publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  18777. printf(testingFmt,"failed at bad-arg-case-7.");
  18778. fflush( stdout );
  18779. wc_curve25519_free(&key);
  18780. wc_FreeRng(&rng);
  18781. return 1;
  18782. }
  18783. prvkSz = CURVE25519_KEYSIZE;
  18784. pubkSz = CURVE25519_KEYSIZE;
  18785. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  18786. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  18787. printf(testingFmt,"failed at bad-arg-case-8.");
  18788. fflush( stdout );
  18789. wc_curve25519_free(&key);
  18790. wc_FreeRng(&rng);
  18791. return 1;
  18792. }
  18793. /* prvkSz = CURVE25519_KEYSIZE; */
  18794. pubkSz = CURVE25519_KEYSIZE;
  18795. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  18796. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN)){
  18797. printf(testingFmt,"failed at bad-arg-case-9.");
  18798. fflush( stdout );
  18799. wc_curve25519_free(&key);
  18800. wc_FreeRng(&rng);
  18801. return 1;
  18802. }
  18803. prvkSz = CURVE25519_KEYSIZE;
  18804. pubkSz = CURVE25519_KEYSIZE;
  18805. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  18806. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN)){
  18807. printf(testingFmt,"failed at bad-arg-case-10.");
  18808. fflush( stdout );
  18809. wc_curve25519_free(&key);
  18810. wc_FreeRng(&rng);
  18811. return 1;
  18812. }
  18813. /* illegal value for endien */
  18814. prvkSz = CURVE25519_KEYSIZE;
  18815. /* pubkSz = CURVE25519_KEYSIZE; */
  18816. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  18817. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN + 10 )){
  18818. printf(testingFmt,"failed at bad-arg-case-11.");
  18819. fflush( stdout );
  18820. wc_curve25519_free(&key);
  18821. wc_FreeRng(&rng);
  18822. return 1;
  18823. }
  18824. /*
  18825. cross-testing
  18826. */
  18827. prksz = CURVE25519_KEYSIZE;
  18828. if(0 != wc_curve25519_export_private_raw( &key, prik, &prksz )){
  18829. printf(testingFmt,"failed due to wc_curve25519_export_private_raw");
  18830. fflush( stdout );
  18831. wc_curve25519_free(&key);
  18832. wc_FreeRng(&rng);
  18833. return 1;
  18834. }
  18835. pbksz = CURVE25519_KEYSIZE;
  18836. if(0 != wc_curve25519_export_public( &key, pubk, &pbksz )){
  18837. printf(testingFmt,"failed due to wc_curve25519_export_public");
  18838. fflush( stdout );
  18839. wc_curve25519_free(&key);
  18840. wc_FreeRng(&rng);
  18841. return 1;
  18842. }
  18843. prvkSz = CURVE25519_KEYSIZE;
  18844. /* pubkSz = CURVE25519_KEYSIZE; */
  18845. if(0 != wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  18846. publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
  18847. printf(testingFmt,"failed due to wc_curve25519_export_key_raw_ex");
  18848. fflush( stdout );
  18849. wc_curve25519_free(&key);
  18850. wc_FreeRng(&rng);
  18851. return 1;
  18852. }
  18853. if( prksz == CURVE25519_KEYSIZE &&
  18854. pbksz == CURVE25519_KEYSIZE &&
  18855. prvkSz == CURVE25519_KEYSIZE &&
  18856. pubkSz == CURVE25519_KEYSIZE ){
  18857. if( 0 == XMEMCMP( privateKey, prik, CURVE25519_KEYSIZE ) &&
  18858. 0 == XMEMCMP( publicKey, pubk, CURVE25519_KEYSIZE )){
  18859. if( 0 == wc_curve25519_export_key_raw_ex( &key, privateKey,
  18860. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
  18861. if( prvkSz == CURVE25519_KEYSIZE &&
  18862. pubkSz == CURVE25519_KEYSIZE ){
  18863. ; /* proceed to the next test */
  18864. }
  18865. else{
  18866. printf(testingFmt,"failed due to key-size-inconsistency");
  18867. fflush( stdout );
  18868. wc_curve25519_free(&key);
  18869. wc_FreeRng(&rng);
  18870. return 1;
  18871. }
  18872. }
  18873. else{
  18874. printf(testingFmt,
  18875. "failed due to wc_curve25519_export_key_raw_ex");
  18876. fflush( stdout );
  18877. wc_curve25519_free(&key);
  18878. wc_FreeRng(&rng);
  18879. return 1;
  18880. }
  18881. }
  18882. else{
  18883. printf(testingFmt,"failed due to key-contents-inconsistency");
  18884. fflush( stdout );
  18885. wc_curve25519_free(&key);
  18886. wc_FreeRng(&rng);
  18887. return 1;
  18888. }
  18889. }
  18890. else{
  18891. printf(testingFmt,"failed due to bad-key-size");
  18892. fflush( stdout );
  18893. wc_curve25519_free(&key);
  18894. wc_FreeRng(&rng);
  18895. return 1;
  18896. }
  18897. /*
  18898. try once with another endian
  18899. */
  18900. prvkSz = CURVE25519_KEYSIZE;
  18901. pubkSz = CURVE25519_KEYSIZE;
  18902. if( 0 == wc_curve25519_export_key_raw_ex( &key, privateKey,
  18903. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  18904. if( prvkSz == CURVE25519_KEYSIZE &&
  18905. pubkSz == CURVE25519_KEYSIZE ){
  18906. /* no more test*/
  18907. printf(resultFmt, passed );
  18908. fflush( stdout );
  18909. wc_curve25519_free(&key);
  18910. wc_FreeRng(&rng);
  18911. return 0;
  18912. }
  18913. else{
  18914. printf(testingFmt,"failed due to key-size-inconsistency");
  18915. fflush( stdout );
  18916. wc_curve25519_free(&key);
  18917. wc_FreeRng(&rng);
  18918. return 1;
  18919. }
  18920. }
  18921. else{
  18922. printf(testingFmt,
  18923. "failed due to wc_curve25519_export_key_raw_ex(BIGENDIAN)");
  18924. fflush( stdout );
  18925. wc_curve25519_free(&key);
  18926. wc_FreeRng(&rng);
  18927. return 1;
  18928. }
  18929. #else
  18930. return 0;
  18931. #endif
  18932. } /* end of test_wc_curve25519_export_key_raw_ex */
  18933. /*
  18934. * Testing wc_curve25519_make_key
  18935. */
  18936. static int test_wc_curve25519_make_key (void)
  18937. {
  18938. int ret = 0;
  18939. #if defined(HAVE_CURVE25519)
  18940. WC_RNG rng;
  18941. curve25519_key key;
  18942. int keysize;
  18943. printf(testingFmt, "wc_curve25519_make_key()");
  18944. ret = wc_curve25519_init(&key);
  18945. if (ret == 0) {
  18946. ret = wc_InitRng(&rng);
  18947. }
  18948. if (ret == 0) {
  18949. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  18950. if (ret == 0) {
  18951. keysize = wc_curve25519_size(&key);
  18952. if (keysize != CURVE25519_KEYSIZE) {
  18953. ret = WOLFSSL_FATAL_ERROR;
  18954. }
  18955. }
  18956. if (ret == 0) {
  18957. ret = wc_curve25519_make_key(&rng, keysize, &key);
  18958. }
  18959. }
  18960. /*test bad cases*/
  18961. if (ret == 0) {
  18962. ret = wc_curve25519_make_key(NULL, 0, NULL);
  18963. if (ret == BAD_FUNC_ARG) {
  18964. ret = 0;
  18965. }
  18966. }
  18967. if (ret == 0) {
  18968. ret = wc_curve25519_make_key(&rng, keysize, NULL);
  18969. if (ret == BAD_FUNC_ARG) {
  18970. ret = 0;
  18971. }
  18972. }
  18973. if (ret == 0) {
  18974. ret = wc_curve25519_make_key(NULL, keysize, &key);
  18975. if (ret == BAD_FUNC_ARG) {
  18976. ret = 0;
  18977. }
  18978. }
  18979. if (ret == 0) {
  18980. ret = wc_curve25519_make_key(&rng, 0, &key);
  18981. if (ret == ECC_BAD_ARG_E) {
  18982. ret = 0;
  18983. }
  18984. }
  18985. printf(resultFmt, ret == 0 ? passed : failed);
  18986. wc_curve25519_free(&key);
  18987. wc_FreeRng(&rng);
  18988. #endif
  18989. return ret;
  18990. } /*END test_wc_curve25519_make_key*/
  18991. /*
  18992. * Testing wc_curve25519_shared_secret_ex
  18993. */
  18994. static int test_wc_curve25519_shared_secret_ex(void)
  18995. {
  18996. int ret = 0;
  18997. #if defined(HAVE_CURVE25519)
  18998. WC_RNG rng;
  18999. curve25519_key private_key, public_key;
  19000. byte out[CURVE25519_KEYSIZE];
  19001. word32 outLen = sizeof(out);
  19002. int endian = EC25519_BIG_ENDIAN;
  19003. printf(testingFmt, "wc_curve25519_shared_secret_ex()");
  19004. ret = wc_curve25519_init(&private_key);
  19005. if (ret == 0) {
  19006. ret = wc_curve25519_init(&public_key);
  19007. }
  19008. if (ret == 0) {
  19009. ret = wc_InitRng(&rng);
  19010. }
  19011. if (ret == 0) {
  19012. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key);
  19013. }
  19014. if (ret == 0) {
  19015. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key);
  19016. }
  19017. if (ret == 0) {
  19018. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19019. &outLen, endian);
  19020. }
  19021. /*test bad cases*/
  19022. if (ret == 0) {
  19023. ret = wc_curve25519_shared_secret_ex(NULL, NULL, NULL,
  19024. 0, endian);
  19025. if (ret == 0) {
  19026. ret = -1;
  19027. }
  19028. if (ret == BAD_FUNC_ARG) {
  19029. ret = 0;
  19030. }
  19031. }
  19032. if (ret == 0) {
  19033. ret = wc_curve25519_shared_secret_ex(NULL, &public_key, out,
  19034. &outLen, endian);
  19035. if (ret == 0) {
  19036. ret = -1;
  19037. }
  19038. else if (ret == BAD_FUNC_ARG) {
  19039. ret = 0;
  19040. }
  19041. }
  19042. if (ret == 0) {
  19043. ret = wc_curve25519_shared_secret_ex(&private_key, NULL, out,
  19044. &outLen, endian);
  19045. if (ret == 0) {
  19046. ret = -1;
  19047. }
  19048. else if (ret == BAD_FUNC_ARG) {
  19049. ret = 0;
  19050. }
  19051. }
  19052. if (ret == 0) {
  19053. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  19054. &outLen, endian);
  19055. if (ret == 0) {
  19056. ret = -1;
  19057. }
  19058. else if (ret == BAD_FUNC_ARG) {
  19059. ret = 0;
  19060. }
  19061. }
  19062. if (ret == 0) {
  19063. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19064. NULL, endian);
  19065. if (ret == 0) {
  19066. ret = -1;
  19067. }
  19068. else if (ret == BAD_FUNC_ARG) {
  19069. ret = 0;
  19070. }
  19071. }
  19072. if (ret == 0) {
  19073. /*curve25519.c is checking for public_key size less than or equal to 0x7f,
  19074. *increasing to 0x8f checks for error being returned*/
  19075. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  19076. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19077. &outLen, endian);
  19078. if (ret == 0) {
  19079. ret = -1;
  19080. }
  19081. else if (ret == ECC_BAD_ARG_E) {
  19082. ret = 0;
  19083. }
  19084. }
  19085. outLen = outLen - 2;
  19086. if (ret == 0) {
  19087. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19088. &outLen, endian);
  19089. if (ret == 0) {
  19090. ret = -1;
  19091. }
  19092. else if (ret == BAD_FUNC_ARG) {
  19093. ret = 0;
  19094. }
  19095. }
  19096. printf(resultFmt, ret == 0 ? passed : failed);
  19097. wc_curve25519_free(&private_key);
  19098. wc_curve25519_free(&public_key);
  19099. wc_FreeRng(&rng);
  19100. #endif
  19101. return ret;
  19102. } /*END test_wc_curve25519_shared_secret_ex*/
  19103. /*
  19104. * Testing wc_curve25519_make_pub
  19105. */
  19106. static int test_wc_curve25519_make_pub(void)
  19107. {
  19108. int ret = 0;
  19109. #ifdef HAVE_CURVE25519
  19110. WC_RNG rng;
  19111. curve25519_key key;
  19112. byte out[CURVE25519_KEYSIZE];
  19113. printf(testingFmt, "wc_curve25519_make_pub()");
  19114. ret = wc_curve25519_init(&key);
  19115. if (ret == 0) {
  19116. ret = wc_InitRng(&rng);
  19117. if (ret == 0) {
  19118. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19119. }
  19120. }
  19121. if (ret == 0) {
  19122. ret = wc_curve25519_make_pub((int)sizeof(out), out, (int)sizeof(key.k), key.k);
  19123. }
  19124. /*test bad cases*/
  19125. if (ret == 0) {
  19126. ret = wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k, (int)sizeof out, out);
  19127. if (ret == ECC_BAD_ARG_E) {
  19128. ret = 0;
  19129. }
  19130. }
  19131. if (ret == 0) {
  19132. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), NULL);
  19133. if (ret == ECC_BAD_ARG_E) {
  19134. ret = 0;
  19135. }
  19136. }
  19137. if (ret == 0) {
  19138. ret = wc_curve25519_make_pub((int)sizeof out - 1, out, (int)sizeof(key.k), key.k);
  19139. if (ret == ECC_BAD_ARG_E) {
  19140. ret = 0;
  19141. }
  19142. }
  19143. if (ret == 0) {
  19144. ret = wc_curve25519_make_pub((int)sizeof out, NULL, (int)sizeof(key.k), key.k);
  19145. if (ret == ECC_BAD_ARG_E) {
  19146. ret = 0;
  19147. }
  19148. }
  19149. if (ret == 0) {
  19150. /* verify clamping test */
  19151. key.k[0] |= ~248;
  19152. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), key.k);
  19153. if (ret == ECC_BAD_ARG_E) {
  19154. ret = 0;
  19155. }
  19156. key.k[0] &= 248;
  19157. }
  19158. /* repeat the expected-to-succeed test. */
  19159. if (ret == 0) {
  19160. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), key.k);
  19161. }
  19162. printf(resultFmt, ret == 0 ? passed : failed);
  19163. wc_curve25519_free(&key);
  19164. wc_FreeRng(&rng);
  19165. #endif
  19166. return ret;
  19167. } /*END test_wc_curve25519_make_pub */
  19168. /*
  19169. * Testing test_wc_curve25519_export_public_ex
  19170. */
  19171. static int test_wc_curve25519_export_public_ex (void)
  19172. {
  19173. int ret = 0;
  19174. #if defined(HAVE_CURVE25519)
  19175. WC_RNG rng;
  19176. curve25519_key key;
  19177. byte out[CURVE25519_KEYSIZE];
  19178. word32 outLen = sizeof(out);
  19179. int endian = EC25519_BIG_ENDIAN;
  19180. printf(testingFmt, "wc_curve25519_export_public_ex()");
  19181. ret = wc_curve25519_init(&key);
  19182. if (ret == 0) {
  19183. ret = wc_InitRng(&rng);
  19184. }
  19185. if (ret == 0) {
  19186. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19187. if (ret == 0) {
  19188. ret = wc_curve25519_export_public(&key, out, &outLen);
  19189. }
  19190. if (ret == 0) {
  19191. ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
  19192. }
  19193. }
  19194. /*test bad cases*/
  19195. if (ret == 0) {
  19196. ret = wc_curve25519_export_public_ex(NULL, NULL, NULL, endian);
  19197. if (ret == BAD_FUNC_ARG) {
  19198. ret = 0;
  19199. }
  19200. }
  19201. if (ret == 0) {
  19202. ret = wc_curve25519_export_public_ex(NULL, out, &outLen, endian);
  19203. if (ret == BAD_FUNC_ARG) {
  19204. ret = 0;
  19205. }
  19206. }
  19207. if (ret == 0) {
  19208. ret = wc_curve25519_export_public_ex(&key, NULL, &outLen, endian);
  19209. if (ret == BAD_FUNC_ARG) {
  19210. ret = 0;
  19211. }
  19212. }
  19213. if (ret == 0) {
  19214. ret = wc_curve25519_export_public_ex(&key, out, NULL, endian);
  19215. if (ret == BAD_FUNC_ARG) {
  19216. ret = 0;
  19217. }
  19218. }
  19219. outLen = outLen - 2;
  19220. if (ret == 0) {
  19221. ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
  19222. if (ret == ECC_BAD_ARG_E) {
  19223. ret = 0;
  19224. }
  19225. }
  19226. printf(resultFmt, ret == 0 ? passed : failed);
  19227. wc_curve25519_free(&key);
  19228. wc_FreeRng(&rng);
  19229. #endif
  19230. return ret;
  19231. } /*END test_wc_curve25519_export_public_ex*/
  19232. /*
  19233. * Testing test_wc_curve25519_import_private_raw_ex
  19234. */
  19235. static int test_wc_curve25519_import_private_raw_ex (void)
  19236. {
  19237. int ret = 0;
  19238. #if defined(HAVE_CURVE25519)
  19239. WC_RNG rng;
  19240. curve25519_key key;
  19241. byte priv[CURVE25519_KEYSIZE];
  19242. byte pub[CURVE25519_KEYSIZE];
  19243. word32 privSz = sizeof(priv);
  19244. word32 pubSz = sizeof(pub);
  19245. int endian = EC25519_BIG_ENDIAN;
  19246. printf(testingFmt, "wc_curve25519_import_private_raw_ex()");
  19247. ret = wc_curve25519_init(&key);
  19248. if (ret == 0) {
  19249. ret = wc_InitRng(&rng);
  19250. }
  19251. if (ret == 0) {
  19252. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19253. if (ret == 0) {
  19254. ret = wc_curve25519_export_private_raw_ex(&key, priv, &privSz, endian);
  19255. }
  19256. if (ret == 0) {
  19257. ret = wc_curve25519_export_public(&key, pub, &pubSz);
  19258. }
  19259. if (ret == 0) {
  19260. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  19261. &key, endian);
  19262. }
  19263. }
  19264. /*test bad cases*/
  19265. if (ret == 0) {
  19266. ret = wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  19267. endian);
  19268. if (ret == BAD_FUNC_ARG) {
  19269. ret = 0;
  19270. }
  19271. }
  19272. if (ret == 0) {
  19273. ret = wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  19274. &key, endian);
  19275. if (ret == BAD_FUNC_ARG) {
  19276. ret = 0;
  19277. }
  19278. }
  19279. if (ret == 0) {
  19280. ret = wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  19281. &key, endian);
  19282. if (ret == BAD_FUNC_ARG) {
  19283. ret = 0;
  19284. }
  19285. }
  19286. if (ret == 0) {
  19287. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  19288. NULL, endian);
  19289. if (ret == BAD_FUNC_ARG) {
  19290. ret = 0;
  19291. }
  19292. }
  19293. if (ret == 0) {
  19294. ret = wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  19295. &key, endian);
  19296. if (ret == ECC_BAD_ARG_E) {
  19297. ret = 0;
  19298. }
  19299. }
  19300. if (ret == 0) {
  19301. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  19302. &key, endian);
  19303. if (ret == ECC_BAD_ARG_E) {
  19304. ret = 0;
  19305. }
  19306. }
  19307. if (ret == 0) {
  19308. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  19309. &key, EC25519_LITTLE_ENDIAN);
  19310. }
  19311. printf(resultFmt, ret == 0 ? passed : failed);
  19312. wc_curve25519_free(&key);
  19313. wc_FreeRng(&rng);
  19314. #endif
  19315. return ret;
  19316. } /*END test_wc_curve25519_import_private_raw_ex*/
  19317. /*
  19318. * Testing test_wc_curve25519_import_private
  19319. */
  19320. static int test_wc_curve25519_import_private (void)
  19321. {
  19322. int ret = 0;
  19323. #if defined(HAVE_CURVE25519)
  19324. curve25519_key key;
  19325. WC_RNG rng;
  19326. byte priv[CURVE25519_KEYSIZE];
  19327. word32 privSz = sizeof(priv);
  19328. printf(testingFmt, "wc_curve25519_import_private()");
  19329. ret = wc_curve25519_init(&key);
  19330. if (ret == 0) {
  19331. ret = wc_InitRng(&rng);
  19332. }
  19333. if (ret == 0) {
  19334. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19335. if (ret == 0) {
  19336. ret = wc_curve25519_export_private_raw(&key, priv, &privSz);
  19337. }
  19338. }
  19339. if (ret == 0) {
  19340. ret = wc_curve25519_import_private(priv, privSz, &key);
  19341. }
  19342. printf(resultFmt, ret == 0 ? passed : failed);
  19343. wc_curve25519_free(&key);
  19344. wc_FreeRng(&rng);
  19345. #endif
  19346. return ret;
  19347. } /*END test_wc_curve25519_import*/
  19348. /*
  19349. * Testing test_wc_curve25519_export_private_raw_ex
  19350. */
  19351. static int test_wc_curve25519_export_private_raw_ex (void)
  19352. {
  19353. int ret = 0;
  19354. #if defined(HAVE_CURVE25519)
  19355. curve25519_key key;
  19356. byte out[CURVE25519_KEYSIZE];
  19357. word32 outLen = sizeof(out);
  19358. int endian = EC25519_BIG_ENDIAN;
  19359. printf(testingFmt, "wc_curve25519_export_private_raw_ex()");
  19360. ret = wc_curve25519_init(&key);
  19361. if (ret == 0) {
  19362. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
  19363. }
  19364. /*test bad cases*/
  19365. if (ret == 0) {
  19366. ret = wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian);
  19367. if (ret == BAD_FUNC_ARG) {
  19368. ret = 0;
  19369. }
  19370. }
  19371. if (ret == 0) {
  19372. ret = wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian);
  19373. if (ret == BAD_FUNC_ARG) {
  19374. ret = 0;
  19375. }
  19376. }
  19377. if (ret == 0) {
  19378. ret = wc_curve25519_export_private_raw_ex(&key, NULL, &outLen, endian);
  19379. if (ret == BAD_FUNC_ARG) {
  19380. ret = 0;
  19381. }
  19382. }
  19383. if (ret == 0) {
  19384. ret = wc_curve25519_export_private_raw_ex(&key, out, NULL, endian);
  19385. if (ret == BAD_FUNC_ARG) {
  19386. ret = 0;
  19387. }
  19388. }
  19389. if (ret == 0) {
  19390. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  19391. EC25519_LITTLE_ENDIAN);
  19392. }
  19393. outLen = outLen - 2;
  19394. if (ret == 0) {
  19395. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
  19396. if (ret == ECC_BAD_ARG_E) {
  19397. ret = 0;
  19398. }
  19399. }
  19400. printf(resultFmt, ret == 0 ? passed : failed);
  19401. wc_curve25519_free(&key);
  19402. #endif
  19403. return ret;
  19404. }/*END test_wc_curve25519_export_private_raw_ex*/
  19405. /*
  19406. * Testing wc_ed448_make_key().
  19407. */
  19408. static int test_wc_ed448_make_key (void)
  19409. {
  19410. int ret = 0;
  19411. #if defined(HAVE_ED448)
  19412. ed448_key key;
  19413. WC_RNG rng;
  19414. ret = wc_InitRng(&rng);
  19415. if (ret == 0) {
  19416. ret = wc_ed448_init(&key);
  19417. }
  19418. printf(testingFmt, "wc_ed448_make_key()");
  19419. if (ret == 0) {
  19420. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  19421. }
  19422. /* Test bad args. */
  19423. if (ret == 0) {
  19424. ret = wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key);
  19425. if (ret == BAD_FUNC_ARG) {
  19426. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL);
  19427. }
  19428. if (ret == BAD_FUNC_ARG) {
  19429. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key);
  19430. }
  19431. if (ret == BAD_FUNC_ARG) {
  19432. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key);
  19433. }
  19434. if (ret == BAD_FUNC_ARG) {
  19435. ret = 0;
  19436. } else if (ret == 0) {
  19437. ret = WOLFSSL_FATAL_ERROR;
  19438. }
  19439. }
  19440. printf(resultFmt, ret == 0 ? passed : failed);
  19441. if (wc_FreeRng(&rng) && ret == 0) {
  19442. ret = WOLFSSL_FATAL_ERROR;
  19443. }
  19444. wc_ed448_free(&key);
  19445. #endif
  19446. return ret;
  19447. } /* END test_wc_ed448_make_key */
  19448. /*
  19449. * Testing wc_ed448_init()
  19450. */
  19451. static int test_wc_ed448_init (void)
  19452. {
  19453. int ret = 0;
  19454. #if defined(HAVE_ED448)
  19455. ed448_key key;
  19456. printf(testingFmt, "wc_ed448_init()");
  19457. ret = wc_ed448_init(&key);
  19458. /* Test bad args. */
  19459. if (ret == 0) {
  19460. ret = wc_ed448_init(NULL);
  19461. if (ret == BAD_FUNC_ARG) {
  19462. ret = 0;
  19463. } else if (ret == 0) {
  19464. ret = WOLFSSL_FATAL_ERROR;
  19465. }
  19466. }
  19467. printf(resultFmt, ret == 0 ? passed : failed);
  19468. wc_ed448_free(&key);
  19469. #endif
  19470. return ret;
  19471. } /* END test_wc_ed448_init */
  19472. /*
  19473. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  19474. */
  19475. static int test_wc_ed448_sign_msg (void)
  19476. {
  19477. int ret = 0;
  19478. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  19479. WC_RNG rng;
  19480. ed448_key key;
  19481. byte msg[] = "Everybody gets Friday off.\n";
  19482. byte sig[ED448_SIG_SIZE];
  19483. word32 msglen = sizeof(msg);
  19484. word32 siglen = sizeof(sig);
  19485. word32 badSigLen = sizeof(sig) - 1;
  19486. #ifdef HAVE_ED448_VERIFY
  19487. int verify_ok = 0; /*1 = Verify success.*/
  19488. #endif
  19489. /* Initialize stack variables. */
  19490. XMEMSET(sig, 0, siglen);
  19491. /* Initialize key. */
  19492. ret = wc_InitRng(&rng);
  19493. if (ret == 0) {
  19494. ret = wc_ed448_init(&key);
  19495. if (ret == 0) {
  19496. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  19497. }
  19498. }
  19499. printf(testingFmt, "wc_ed448_sign_msg()");
  19500. if (ret == 0) {
  19501. ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0);
  19502. }
  19503. /* Test bad args. */
  19504. if (ret == 0 && siglen == ED448_SIG_SIZE) {
  19505. ret = wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0);
  19506. if (ret == BAD_FUNC_ARG) {
  19507. ret = wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0);
  19508. }
  19509. if (ret == BAD_FUNC_ARG) {
  19510. ret = wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0);
  19511. }
  19512. if (ret == BAD_FUNC_ARG) {
  19513. ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0);
  19514. }
  19515. if (ret == BAD_FUNC_ARG) {
  19516. ret = wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key,
  19517. NULL, 0);
  19518. }
  19519. if (ret == BUFFER_E && badSigLen == ED448_SIG_SIZE) {
  19520. badSigLen -= 1;
  19521. ret = 0;
  19522. } else if (ret == 0) {
  19523. ret = WOLFSSL_FATAL_ERROR;
  19524. }
  19525. } /* END sign */
  19526. printf(resultFmt, ret == 0 ? passed : failed);
  19527. #ifdef HAVE_ED448_VERIFY
  19528. printf(testingFmt, "wc_ed448_verify_msg()");
  19529. if (ret == 0) {
  19530. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  19531. &key, NULL, 0);
  19532. if (ret == 0 && verify_ok == 1) {
  19533. ret = 0;
  19534. } else if (ret == 0) {
  19535. ret = WOLFSSL_FATAL_ERROR;
  19536. }
  19537. /* Test bad args. */
  19538. if (ret == 0) {
  19539. AssertIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg,
  19540. msglen, &verify_ok, &key,
  19541. NULL, 0),
  19542. BAD_FUNC_ARG);
  19543. AssertIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg,
  19544. msglen, &verify_ok, &key,
  19545. NULL, 0),
  19546. BAD_FUNC_ARG);
  19547. ret = wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  19548. &key, NULL, 0);
  19549. if (ret == BAD_FUNC_ARG) {
  19550. ret = wc_ed448_verify_msg(sig, siglen, NULL, msglen,
  19551. &verify_ok, &key, NULL, 0);
  19552. }
  19553. if (ret == BAD_FUNC_ARG) {
  19554. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
  19555. NULL, &key, NULL, 0);
  19556. }
  19557. if (ret == BAD_FUNC_ARG) {
  19558. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
  19559. &verify_ok, NULL, NULL, 0);
  19560. }
  19561. if (ret == BAD_FUNC_ARG) {
  19562. ret = wc_ed448_verify_msg(sig, badSigLen, msg, msglen,
  19563. &verify_ok, &key, NULL, 0);
  19564. }
  19565. if (ret == BAD_FUNC_ARG) {
  19566. ret = 0;
  19567. } else if (ret == 0) {
  19568. ret = WOLFSSL_FATAL_ERROR;
  19569. }
  19570. }
  19571. } /* END verify. */
  19572. printf(resultFmt, ret == 0 ? passed : failed);
  19573. #endif /* Verify. */
  19574. if (wc_FreeRng(&rng) && ret == 0) {
  19575. ret = WOLFSSL_FATAL_ERROR;
  19576. }
  19577. wc_ed448_free(&key);
  19578. #endif
  19579. return ret;
  19580. } /* END test_wc_ed448_sign_msg */
  19581. /*
  19582. * Testing wc_ed448_import_public()
  19583. */
  19584. static int test_wc_ed448_import_public (void)
  19585. {
  19586. int ret = 0;
  19587. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  19588. WC_RNG rng;
  19589. ed448_key pubKey;
  19590. const byte in[] =
  19591. "Ed448PublicKeyUnitTest.................................\n";
  19592. word32 inlen = sizeof(in);
  19593. ret = wc_InitRng(&rng);
  19594. if (ret == 0) {
  19595. ret = wc_ed448_init(&pubKey);
  19596. if (ret == 0) {
  19597. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey);
  19598. }
  19599. }
  19600. printf(testingFmt, "wc_ed448_import_public()");
  19601. if (ret == 0) {
  19602. ret = wc_ed448_import_public(in, inlen, &pubKey);
  19603. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  19604. ret = 0;
  19605. } else {
  19606. ret = WOLFSSL_FATAL_ERROR;
  19607. }
  19608. /* Test bad args. */
  19609. if (ret == 0) {
  19610. ret = wc_ed448_import_public(NULL, inlen, &pubKey);
  19611. if (ret == BAD_FUNC_ARG) {
  19612. ret = wc_ed448_import_public(in, inlen, NULL);
  19613. }
  19614. if (ret == BAD_FUNC_ARG) {
  19615. ret = wc_ed448_import_public(in, inlen - 1, &pubKey);
  19616. }
  19617. if (ret == BAD_FUNC_ARG) {
  19618. ret = 0;
  19619. } else if (ret == 0) {
  19620. ret = WOLFSSL_FATAL_ERROR;
  19621. }
  19622. }
  19623. }
  19624. printf(resultFmt, ret == 0 ? passed : failed);
  19625. if (wc_FreeRng(&rng) && ret == 0) {
  19626. ret = WOLFSSL_FATAL_ERROR;
  19627. }
  19628. wc_ed448_free(&pubKey);
  19629. #endif
  19630. return ret;
  19631. } /* END wc_ed448_import_public */
  19632. /*
  19633. * Testing wc_ed448_import_private_key()
  19634. */
  19635. static int test_wc_ed448_import_private_key (void)
  19636. {
  19637. int ret = 0;
  19638. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  19639. WC_RNG rng;
  19640. ed448_key key;
  19641. const byte privKey[] =
  19642. "Ed448PrivateKeyUnitTest................................\n";
  19643. const byte pubKey[] =
  19644. "Ed448PublicKeyUnitTest.................................\n";
  19645. word32 privKeySz = sizeof(privKey);
  19646. word32 pubKeySz = sizeof(pubKey);
  19647. #ifdef HAVE_ED448_KEY_EXPORT
  19648. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  19649. word32 bothKeysSz = sizeof(bothKeys);
  19650. #endif
  19651. ret = wc_InitRng(&rng);
  19652. if (ret != 0) {
  19653. return ret;
  19654. }
  19655. ret = wc_ed448_init(&key);
  19656. if (ret != 0) {
  19657. wc_FreeRng(&rng);
  19658. return ret;
  19659. }
  19660. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  19661. printf(testingFmt, "wc_ed448_import_private_key()");
  19662. if (ret == 0) {
  19663. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey, pubKeySz,
  19664. &key);
  19665. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
  19666. XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  19667. ret = WOLFSSL_FATAL_ERROR;
  19668. }
  19669. }
  19670. #ifdef HAVE_ED448_KEY_EXPORT
  19671. if (ret == 0)
  19672. ret = wc_ed448_export_private(&key, bothKeys, &bothKeysSz);
  19673. if (ret == 0) {
  19674. ret = wc_ed448_import_private_key(bothKeys, bothKeysSz, NULL, 0, &key);
  19675. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
  19676. XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  19677. ret = WOLFSSL_FATAL_ERROR;
  19678. }
  19679. }
  19680. #endif
  19681. /* Test bad args. */
  19682. if (ret == 0) {
  19683. ret = wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  19684. &key);
  19685. if (ret == BAD_FUNC_ARG) {
  19686. ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
  19687. pubKeySz, &key);
  19688. }
  19689. if (ret == BAD_FUNC_ARG) {
  19690. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  19691. pubKeySz, NULL);
  19692. }
  19693. if (ret == BAD_FUNC_ARG) {
  19694. ret = wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  19695. pubKeySz, &key);
  19696. }
  19697. if (ret == BAD_FUNC_ARG) {
  19698. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  19699. pubKeySz - 1, &key);
  19700. }
  19701. if (ret == BAD_FUNC_ARG) {
  19702. ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
  19703. 0, &key);
  19704. }
  19705. if (ret == BAD_FUNC_ARG) {
  19706. ret = 0;
  19707. } else if (ret == 0) {
  19708. ret = WOLFSSL_FATAL_ERROR;
  19709. }
  19710. }
  19711. printf(resultFmt, ret == 0 ? passed : failed);
  19712. if (wc_FreeRng(&rng) && ret == 0) {
  19713. ret = WOLFSSL_FATAL_ERROR;
  19714. }
  19715. wc_ed448_free(&key);
  19716. #endif
  19717. return ret;
  19718. } /* END test_wc_ed448_import_private_key */
  19719. /*
  19720. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  19721. */
  19722. static int test_wc_ed448_export (void)
  19723. {
  19724. int ret = 0;
  19725. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  19726. WC_RNG rng;
  19727. ed448_key key;
  19728. byte priv[ED448_PRV_KEY_SIZE];
  19729. byte pub[ED448_PUB_KEY_SIZE];
  19730. word32 privSz = sizeof(priv);
  19731. word32 pubSz = sizeof(pub);
  19732. ret = wc_InitRng(&rng);
  19733. if (ret != 0) {
  19734. return ret;
  19735. }
  19736. ret = wc_ed448_init(&key);
  19737. if (ret != 0) {
  19738. wc_FreeRng(&rng);
  19739. return ret;
  19740. }
  19741. if (ret == 0) {
  19742. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  19743. }
  19744. printf(testingFmt, "wc_ed448_export_public()");
  19745. if (ret == 0) {
  19746. ret = wc_ed448_export_public(&key, pub, &pubSz);
  19747. if (ret == 0 && (pubSz != ED448_KEY_SIZE ||
  19748. XMEMCMP(key.p, pub, pubSz) != 0)) {
  19749. ret = WOLFSSL_FATAL_ERROR;
  19750. }
  19751. if (ret == 0) {
  19752. ret = wc_ed448_export_public(NULL, pub, &pubSz);
  19753. if (ret == BAD_FUNC_ARG) {
  19754. ret = wc_ed448_export_public(&key, NULL, &pubSz);
  19755. }
  19756. if (ret == BAD_FUNC_ARG) {
  19757. ret = wc_ed448_export_public(&key, pub, NULL);
  19758. }
  19759. if (ret == BAD_FUNC_ARG) {
  19760. ret = 0;
  19761. } else if (ret == 0) {
  19762. ret = WOLFSSL_FATAL_ERROR;
  19763. }
  19764. }
  19765. }
  19766. printf(resultFmt, ret == 0 ? passed : failed);
  19767. printf(testingFmt, "wc_ed448_export_private_only()");
  19768. if (ret == 0) {
  19769. ret = wc_ed448_export_private_only(&key, priv, &privSz);
  19770. if (ret == 0 && (privSz != ED448_KEY_SIZE ||
  19771. XMEMCMP(key.k, priv, privSz) != 0)) {
  19772. ret = WOLFSSL_FATAL_ERROR;
  19773. }
  19774. if (ret == 0) {
  19775. ret = wc_ed448_export_private_only(NULL, priv, &privSz);
  19776. if (ret == BAD_FUNC_ARG) {
  19777. ret = wc_ed448_export_private_only(&key, NULL, &privSz);
  19778. }
  19779. if (ret == BAD_FUNC_ARG) {
  19780. ret = wc_ed448_export_private_only(&key, priv, NULL);
  19781. }
  19782. if (ret == BAD_FUNC_ARG) {
  19783. ret = 0;
  19784. } else if (ret == 0) {
  19785. ret = WOLFSSL_FATAL_ERROR;
  19786. }
  19787. }
  19788. }
  19789. printf(resultFmt, ret == 0 ? passed : failed);
  19790. if (wc_FreeRng(&rng) && ret == 0) {
  19791. ret = WOLFSSL_FATAL_ERROR;
  19792. }
  19793. wc_ed448_free(&key);
  19794. #endif
  19795. return ret;
  19796. } /* END test_wc_ed448_export */
  19797. /*
  19798. * Testing wc_ed448_size()
  19799. */
  19800. static int test_wc_ed448_size (void)
  19801. {
  19802. int ret = 0;
  19803. #if defined(HAVE_ED448)
  19804. WC_RNG rng;
  19805. ed448_key key;
  19806. ret = wc_InitRng(&rng);
  19807. if (ret != 0) {
  19808. return ret;
  19809. }
  19810. ret = wc_ed448_init(&key);
  19811. if (ret != 0) {
  19812. wc_FreeRng(&rng);
  19813. return ret;
  19814. }
  19815. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  19816. if (ret != 0) {
  19817. wc_FreeRng(&rng);
  19818. wc_ed448_free(&key);
  19819. return ret;
  19820. }
  19821. printf(testingFmt, "wc_ed448_size()");
  19822. ret = wc_ed448_size(&key);
  19823. /* Test bad args. */
  19824. if (ret == ED448_KEY_SIZE) {
  19825. ret = wc_ed448_size(NULL);
  19826. if (ret == BAD_FUNC_ARG) {
  19827. ret = 0;
  19828. }
  19829. }
  19830. printf(resultFmt, ret == 0 ? passed : failed);
  19831. if (ret == 0) {
  19832. printf(testingFmt, "wc_ed448_sig_size()");
  19833. ret = wc_ed448_sig_size(&key);
  19834. if (ret == ED448_SIG_SIZE) {
  19835. ret = 0;
  19836. }
  19837. /* Test bad args. */
  19838. if (ret == 0) {
  19839. ret = wc_ed448_sig_size(NULL);
  19840. if (ret == BAD_FUNC_ARG) {
  19841. ret = 0;
  19842. }
  19843. }
  19844. printf(resultFmt, ret == 0 ? passed : failed);
  19845. } /* END wc_ed448_sig_size() */
  19846. if (ret == 0) {
  19847. printf(testingFmt, "wc_ed448_pub_size");
  19848. ret = wc_ed448_pub_size(&key);
  19849. if (ret == ED448_PUB_KEY_SIZE) {
  19850. ret = 0;
  19851. }
  19852. if (ret == 0) {
  19853. ret = wc_ed448_pub_size(NULL);
  19854. if (ret == BAD_FUNC_ARG) {
  19855. ret = 0;
  19856. }
  19857. }
  19858. printf(resultFmt, ret == 0 ? passed : failed);
  19859. } /* END wc_ed448_pub_size */
  19860. if (ret == 0) {
  19861. printf(testingFmt, "wc_ed448_priv_size");
  19862. ret = wc_ed448_priv_size(&key);
  19863. if (ret == ED448_PRV_KEY_SIZE) {
  19864. ret = 0;
  19865. }
  19866. if (ret == 0) {
  19867. ret = wc_ed448_priv_size(NULL);
  19868. if (ret == BAD_FUNC_ARG) {
  19869. ret = 0;
  19870. }
  19871. }
  19872. printf(resultFmt, ret == 0 ? passed : failed);
  19873. } /* END wc_ed448_pub_size */
  19874. if (wc_FreeRng(&rng) && ret == 0) {
  19875. ret = WOLFSSL_FATAL_ERROR;
  19876. }
  19877. wc_ed448_free(&key);
  19878. #endif
  19879. return ret;
  19880. } /* END test_wc_ed448_size */
  19881. /*
  19882. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  19883. */
  19884. static int test_wc_ed448_exportKey (void)
  19885. {
  19886. int ret = 0;
  19887. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  19888. WC_RNG rng;
  19889. ed448_key key;
  19890. byte priv[ED448_PRV_KEY_SIZE];
  19891. byte pub[ED448_PUB_KEY_SIZE];
  19892. byte privOnly[ED448_PRV_KEY_SIZE];
  19893. word32 privSz = sizeof(priv);
  19894. word32 pubSz = sizeof(pub);
  19895. word32 privOnlySz = sizeof(privOnly);
  19896. ret = wc_InitRng(&rng);
  19897. if (ret != 0) {
  19898. return ret;
  19899. }
  19900. ret = wc_ed448_init(&key);
  19901. if (ret != 0) {
  19902. wc_FreeRng(&rng);
  19903. return ret;
  19904. }
  19905. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  19906. if (ret != 0) {
  19907. wc_FreeRng(&rng);
  19908. wc_ed448_free(&key);
  19909. return ret;
  19910. }
  19911. printf(testingFmt, "wc_ed448_export_private()");
  19912. ret = wc_ed448_export_private(&key, privOnly, &privOnlySz);
  19913. if (ret == 0) {
  19914. ret = wc_ed448_export_private(NULL, privOnly, &privOnlySz);
  19915. if (ret == BAD_FUNC_ARG) {
  19916. ret = wc_ed448_export_private(&key, NULL, &privOnlySz);
  19917. }
  19918. if (ret == BAD_FUNC_ARG) {
  19919. ret = wc_ed448_export_private(&key, privOnly, NULL);
  19920. }
  19921. if (ret == BAD_FUNC_ARG) {
  19922. ret = 0;
  19923. } else if (ret == 0) {
  19924. ret = WOLFSSL_FATAL_ERROR;
  19925. }
  19926. }
  19927. printf(resultFmt, ret == 0 ? passed : failed);
  19928. if (ret == 0) {
  19929. printf(testingFmt, "wc_ed448_export_key()");
  19930. ret = wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz);
  19931. if (ret == 0) {
  19932. ret = wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz);
  19933. if (ret == BAD_FUNC_ARG) {
  19934. ret = wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz);
  19935. }
  19936. if (ret == BAD_FUNC_ARG) {
  19937. ret = wc_ed448_export_key(&key, priv, NULL, pub, &pubSz);
  19938. }
  19939. if (ret == BAD_FUNC_ARG) {
  19940. ret = wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz);
  19941. }
  19942. if (ret == BAD_FUNC_ARG) {
  19943. ret = wc_ed448_export_key(&key, priv, &privSz, pub, NULL);
  19944. }
  19945. if (ret == BAD_FUNC_ARG) {
  19946. ret = 0;
  19947. } else if (ret == 0) {
  19948. ret = WOLFSSL_FATAL_ERROR;
  19949. }
  19950. }
  19951. printf(resultFmt, ret == 0 ? passed : failed);
  19952. } /* END wc_ed448_export_key() */
  19953. /* Cross check output. */
  19954. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  19955. ret = WOLFSSL_FATAL_ERROR;
  19956. }
  19957. if (wc_FreeRng(&rng) && ret == 0) {
  19958. ret = WOLFSSL_FATAL_ERROR;
  19959. }
  19960. wc_ed448_free(&key);
  19961. #endif
  19962. return ret;
  19963. } /* END test_wc_ed448_exportKey */
  19964. /*
  19965. * Testing wc_Ed448PublicKeyToDer
  19966. */
  19967. static int test_wc_Ed448PublicKeyToDer (void)
  19968. {
  19969. int ret = 0;
  19970. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  19971. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  19972. int tmp;
  19973. ed448_key key;
  19974. byte derBuf[1024];
  19975. printf(testingFmt, "wc_Ed448PublicKeyToDer()");
  19976. /* Test bad args */
  19977. tmp = wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0);
  19978. if (tmp != BAD_FUNC_ARG) {
  19979. ret = WOLFSSL_FATAL_ERROR;
  19980. }
  19981. if (ret == 0) {
  19982. wc_ed448_init(&key);
  19983. tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0);
  19984. if (tmp != BUFFER_E) {
  19985. ret = WOLFSSL_FATAL_ERROR;
  19986. }
  19987. wc_ed448_free(&key);
  19988. }
  19989. /* Test good args */
  19990. if (ret == 0) {
  19991. WC_RNG rng;
  19992. ret = wc_InitRng(&rng);
  19993. if (ret != 0) {
  19994. return ret;
  19995. }
  19996. ret = wc_ed448_init(&key);
  19997. if (ret != 0) {
  19998. wc_FreeRng(&rng);
  19999. return ret;
  20000. }
  20001. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20002. if (ret != 0) {
  20003. wc_FreeRng(&rng);
  20004. wc_ed448_free(&key);
  20005. return ret;
  20006. }
  20007. tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1);
  20008. if (tmp <= 0) {
  20009. ret = WOLFSSL_FATAL_ERROR;
  20010. }
  20011. wc_FreeRng(&rng);
  20012. wc_ed448_free(&key);
  20013. }
  20014. printf(resultFmt, ret == 0 ? passed : failed);
  20015. #endif
  20016. return ret;
  20017. } /* END testing wc_Ed448PublicKeyToDer */
  20018. /*
  20019. * Testing wc_curve448_init and wc_curve448_free.
  20020. */
  20021. static int test_wc_curve448_init (void)
  20022. {
  20023. int ret = 0;
  20024. #if defined(HAVE_CURVE448)
  20025. curve448_key key;
  20026. printf(testingFmt, "wc_curve448_init()");
  20027. ret = wc_curve448_init(&key);
  20028. /* Test bad args for wc_curve448_init */
  20029. if (ret == 0) {
  20030. ret = wc_curve448_init(NULL);
  20031. if (ret == BAD_FUNC_ARG) {
  20032. ret = 0;
  20033. } else if (ret == 0) {
  20034. ret = WOLFSSL_FATAL_ERROR;
  20035. }
  20036. }
  20037. printf(resultFmt, ret == 0 ? passed : failed);
  20038. /* Test good args for wc_curve_448_free */
  20039. wc_curve448_free(&key);
  20040. wc_curve448_free(NULL);
  20041. #endif
  20042. return ret;
  20043. } /* END test_wc_curve448_init and wc_curve_448_free*/
  20044. /*
  20045. * Testing wc_curve448_make_key
  20046. */
  20047. static int test_wc_curve448_make_key (void)
  20048. {
  20049. int ret = 0;
  20050. #if defined(HAVE_CURVE448)
  20051. WC_RNG rng;
  20052. curve448_key key;
  20053. int keysize;
  20054. printf(testingFmt, "wc_curve448_make_key()");
  20055. ret = wc_curve448_init(&key);
  20056. if (ret == 0) {
  20057. ret = wc_InitRng(&rng);
  20058. }
  20059. if (ret == 0) {
  20060. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  20061. if (ret == 0) {
  20062. keysize = wc_curve448_size(&key);
  20063. if (keysize != CURVE448_KEY_SIZE) {
  20064. ret = WOLFSSL_FATAL_ERROR;
  20065. }
  20066. }
  20067. if (ret == 0) {
  20068. ret = wc_curve448_make_key(&rng, keysize, &key);
  20069. }
  20070. }
  20071. /*test bad cases*/
  20072. if (ret == 0) {
  20073. ret = wc_curve448_make_key(NULL, 0, NULL);
  20074. if (ret == BAD_FUNC_ARG) {
  20075. ret = 0;
  20076. }
  20077. }
  20078. if (ret == 0) {
  20079. ret = wc_curve448_make_key(&rng, keysize, NULL);
  20080. if (ret == BAD_FUNC_ARG) {
  20081. ret = 0;
  20082. }
  20083. }
  20084. if (ret == 0) {
  20085. ret = wc_curve448_make_key(NULL, keysize, &key);
  20086. if (ret == BAD_FUNC_ARG) {
  20087. ret = 0;
  20088. }
  20089. }
  20090. if (ret == 0) {
  20091. ret = wc_curve448_make_key(&rng, 0, &key);
  20092. if (ret == ECC_BAD_ARG_E) {
  20093. ret = 0;
  20094. }
  20095. }
  20096. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  20097. ret = WOLFSSL_FATAL_ERROR;
  20098. }
  20099. printf(resultFmt, ret == 0 ? passed : failed);
  20100. wc_curve448_free(&key);
  20101. #endif
  20102. return ret;
  20103. } /*END test_wc_curve448_make_key*/
  20104. /*
  20105. * Testing test_wc_curve448_shared_secret_ex
  20106. */
  20107. static int test_wc_curve448_shared_secret_ex (void)
  20108. {
  20109. int ret = 0;
  20110. #if defined(HAVE_CURVE448)
  20111. WC_RNG rng;
  20112. curve448_key private_key, public_key;
  20113. byte out[CURVE448_KEY_SIZE];
  20114. word32 outLen = sizeof(out);
  20115. int endian = EC448_BIG_ENDIAN;
  20116. printf(testingFmt, "wc_curve448_shared_secret_ex()");
  20117. ret = wc_curve448_init(&private_key);
  20118. if (ret == 0) {
  20119. ret = wc_InitRng(&rng);
  20120. if (ret == 0) {
  20121. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key);
  20122. }
  20123. }
  20124. if (ret == 0) {
  20125. ret = wc_curve448_init(&public_key);
  20126. }
  20127. if (ret == 0) {
  20128. if (ret == 0) {
  20129. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key);
  20130. }
  20131. }
  20132. if (ret == 0) {
  20133. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20134. &outLen, endian);
  20135. }
  20136. /*test bad cases*/
  20137. if (ret == 0) {
  20138. ret = wc_curve448_shared_secret_ex(NULL, NULL, NULL,
  20139. 0, endian);
  20140. if (ret == BAD_FUNC_ARG) {
  20141. ret = 0;
  20142. }
  20143. }
  20144. if (ret == 0) {
  20145. ret = wc_curve448_shared_secret_ex(NULL, &public_key, out,
  20146. &outLen, endian);
  20147. if (ret == BAD_FUNC_ARG) {
  20148. ret = 0;
  20149. }
  20150. }
  20151. if (ret == 0) {
  20152. ret = wc_curve448_shared_secret_ex(&private_key, NULL, out,
  20153. &outLen, endian);
  20154. if (ret == BAD_FUNC_ARG) {
  20155. ret = 0;
  20156. }
  20157. }
  20158. if (ret == 0) {
  20159. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  20160. &outLen, endian);
  20161. if (ret == BAD_FUNC_ARG) {
  20162. ret = 0;
  20163. }
  20164. }
  20165. if (ret == 0) {
  20166. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20167. NULL, endian);
  20168. if (ret == BAD_FUNC_ARG) {
  20169. ret = 0;
  20170. }
  20171. }
  20172. outLen = outLen - 2;
  20173. if (ret == 0) {
  20174. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20175. &outLen, endian);
  20176. if (ret == BAD_FUNC_ARG) {
  20177. ret = 0;
  20178. }
  20179. }
  20180. printf(resultFmt, ret == 0 ? passed : failed);
  20181. wc_curve448_free(&private_key);
  20182. wc_curve448_free(&public_key);
  20183. wc_FreeRng(&rng);
  20184. #endif
  20185. return ret;
  20186. } /*END test_wc_curve448_shared_secret_ex*/
  20187. /*
  20188. * Testing test_wc_curve448_export_public_ex
  20189. */
  20190. static int test_wc_curve448_export_public_ex (void)
  20191. {
  20192. int ret = 0;
  20193. #if defined(HAVE_CURVE448)
  20194. WC_RNG rng;
  20195. curve448_key key;
  20196. byte out[CURVE448_KEY_SIZE];
  20197. word32 outLen = sizeof(out);
  20198. int endian = EC448_BIG_ENDIAN;
  20199. printf(testingFmt, "wc_curve448_export_public_ex()");
  20200. ret = wc_curve448_init(&key);
  20201. if (ret == 0) {
  20202. ret = wc_InitRng(&rng);
  20203. }
  20204. if (ret == 0) {
  20205. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  20206. if (ret == 0){
  20207. ret = wc_curve448_export_public(&key, out, &outLen);
  20208. }
  20209. if (ret == 0) {
  20210. ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
  20211. }
  20212. }
  20213. /*test bad cases*/
  20214. if (ret == 0) {
  20215. ret = wc_curve448_export_public_ex(NULL, NULL, NULL, endian);
  20216. if (ret == BAD_FUNC_ARG) {
  20217. ret = 0;
  20218. }
  20219. }
  20220. if (ret == 0) {
  20221. ret = wc_curve448_export_public_ex(NULL, out, &outLen, endian);
  20222. if (ret == BAD_FUNC_ARG) {
  20223. ret = 0;
  20224. }
  20225. }
  20226. if (ret == 0) {
  20227. ret = wc_curve448_export_public_ex(&key, NULL, &outLen, endian);
  20228. if (ret == BAD_FUNC_ARG) {
  20229. ret = 0;
  20230. }
  20231. }
  20232. if (ret == 0) {
  20233. ret = wc_curve448_export_public_ex(&key, out, NULL, endian);
  20234. if (ret == BAD_FUNC_ARG) {
  20235. ret = 0;
  20236. }
  20237. }
  20238. outLen = outLen - 2;
  20239. if (ret == 0) {
  20240. ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
  20241. if (ret == ECC_BAD_ARG_E) {
  20242. ret = 0;
  20243. }
  20244. }
  20245. printf(resultFmt, ret == 0 ? passed : failed);
  20246. wc_curve448_free(&key);
  20247. wc_FreeRng(&rng);
  20248. #endif
  20249. return ret;
  20250. } /*END test_wc_curve448_export_public_ex*/
  20251. /*
  20252. * Testing test_wc_curve448_export_private_raw_ex
  20253. */
  20254. static int test_wc_curve448_export_private_raw_ex (void)
  20255. {
  20256. int ret = 0;
  20257. #if defined(HAVE_CURVE448)
  20258. curve448_key key;
  20259. byte out[CURVE448_KEY_SIZE];
  20260. word32 outLen = sizeof(out);
  20261. int endian = EC448_BIG_ENDIAN;
  20262. printf(testingFmt, "wc_curve448_export_private_raw_ex()");
  20263. ret = wc_curve448_init(&key);
  20264. if (ret == 0) {
  20265. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
  20266. }
  20267. /*test bad cases*/
  20268. if (ret == 0) {
  20269. ret = wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian);
  20270. if (ret == BAD_FUNC_ARG) {
  20271. ret = 0;
  20272. }
  20273. }
  20274. if (ret == 0) {
  20275. ret = wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian);
  20276. if (ret == BAD_FUNC_ARG) {
  20277. ret = 0;
  20278. }
  20279. }
  20280. if (ret == 0) {
  20281. ret = wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian);
  20282. if (ret == BAD_FUNC_ARG) {
  20283. ret = 0;
  20284. }
  20285. }
  20286. if (ret == 0) {
  20287. ret = wc_curve448_export_private_raw_ex(&key, out, NULL, endian);
  20288. if (ret == BAD_FUNC_ARG) {
  20289. ret = 0;
  20290. }
  20291. }
  20292. if (ret == 0) {
  20293. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen,
  20294. EC448_LITTLE_ENDIAN);
  20295. }
  20296. outLen = outLen - 2;
  20297. if (ret == 0) {
  20298. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
  20299. if (ret == ECC_BAD_ARG_E) {
  20300. ret = 0;
  20301. }
  20302. }
  20303. printf(resultFmt, ret == 0 ? passed : failed);
  20304. wc_curve448_free(&key);
  20305. #endif
  20306. return ret;
  20307. }/*END test_wc_curve448_export_private_raw_ex*/
  20308. /*
  20309. * Testing test_wc_curve448_import_private_raw_ex
  20310. */
  20311. static int test_wc_curve448_import_private_raw_ex (void)
  20312. {
  20313. int ret = 0;
  20314. #if defined(HAVE_CURVE448)
  20315. WC_RNG rng;
  20316. curve448_key key;
  20317. byte priv[CURVE448_KEY_SIZE];
  20318. byte pub[CURVE448_KEY_SIZE];
  20319. word32 privSz = sizeof(priv);
  20320. word32 pubSz = sizeof(pub);
  20321. int endian = EC448_BIG_ENDIAN;
  20322. printf(testingFmt, "wc_curve448_import_private_raw_ex()");
  20323. ret = wc_curve448_init(&key);
  20324. if (ret == 0) {
  20325. ret = wc_InitRng(&rng);
  20326. }
  20327. if (ret == 0) {
  20328. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  20329. if (ret == 0){
  20330. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  20331. }
  20332. if (ret == 0){
  20333. ret = wc_curve448_export_public(&key, pub, &pubSz);
  20334. }
  20335. if (ret == 0) {
  20336. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20337. &key, endian);
  20338. }
  20339. }
  20340. /*test bad cases*/
  20341. if (ret == 0) {
  20342. ret = wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0);
  20343. if (ret == BAD_FUNC_ARG) {
  20344. ret = 0;
  20345. }
  20346. }
  20347. if (ret == 0) {
  20348. ret = wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  20349. &key, endian);
  20350. if (ret == BAD_FUNC_ARG) {
  20351. ret = 0;
  20352. }
  20353. }
  20354. if (ret == 0) {
  20355. ret = wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  20356. &key, endian);
  20357. if (ret == BAD_FUNC_ARG) {
  20358. ret = 0;
  20359. }
  20360. }
  20361. if (ret == 0) {
  20362. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20363. NULL, endian);
  20364. if (ret == BAD_FUNC_ARG) {
  20365. ret = 0;
  20366. }
  20367. }
  20368. if (ret == 0) {
  20369. ret = wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  20370. &key, endian);
  20371. if (ret == ECC_BAD_ARG_E) {
  20372. ret = 0;
  20373. }
  20374. }
  20375. if (ret == 0) {
  20376. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  20377. &key, endian);
  20378. if (ret == ECC_BAD_ARG_E) {
  20379. ret = 0;
  20380. }
  20381. }
  20382. if (ret == 0) {
  20383. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20384. &key, EC448_LITTLE_ENDIAN);
  20385. }
  20386. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  20387. ret = WOLFSSL_FATAL_ERROR;
  20388. }
  20389. printf(resultFmt, ret == 0 ? passed : failed);
  20390. wc_curve448_free(&key);
  20391. #endif
  20392. return ret;
  20393. } /*END test_wc_curve448_import_private_raw_ex*/
  20394. /*
  20395. * Testing test_curve448_export_key_raw
  20396. */
  20397. static int test_wc_curve448_export_key_raw (void)
  20398. {
  20399. int ret = 0;
  20400. #if defined(HAVE_CURVE448)
  20401. WC_RNG rng;
  20402. curve448_key key;
  20403. byte priv[CURVE448_KEY_SIZE];
  20404. byte pub[CURVE448_KEY_SIZE];
  20405. word32 privSz = sizeof(priv);
  20406. word32 pubSz = sizeof(pub);
  20407. printf(testingFmt, "wc_curve448_export_key_raw()");
  20408. ret = wc_curve448_init(&key);
  20409. if (ret == 0) {
  20410. ret = wc_InitRng(&rng);
  20411. }
  20412. if (ret == 0) {
  20413. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  20414. if (ret == 0) {
  20415. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  20416. }
  20417. if (ret == 0) {
  20418. ret = wc_curve448_export_public(&key, pub, &pubSz);
  20419. }
  20420. if (ret == 0) {
  20421. ret = wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz);
  20422. }
  20423. }
  20424. printf(resultFmt, ret == 0 ? passed : failed);
  20425. wc_curve448_free(&key);
  20426. wc_FreeRng(&rng);
  20427. #endif
  20428. return ret;
  20429. }/*END test_wc_curve448_import_private_raw_ex*/
  20430. /*
  20431. * Testing test_wc_curve448_import_private
  20432. */
  20433. static int test_wc_curve448_import_private (void)
  20434. {
  20435. int ret = 0;
  20436. #if defined(HAVE_CURVE448)
  20437. curve448_key key;
  20438. WC_RNG rng;
  20439. byte priv[CURVE448_KEY_SIZE];
  20440. word32 privSz = sizeof(priv);
  20441. printf(testingFmt, "wc_curve448_import_private()");
  20442. ret = wc_curve448_init(&key);
  20443. if (ret == 0) {
  20444. ret = wc_InitRng(&rng);
  20445. }
  20446. if (ret == 0) {
  20447. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  20448. if (ret == 0) {
  20449. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  20450. }
  20451. }
  20452. if (ret == 0) {
  20453. ret = wc_curve448_import_private(priv, privSz, &key);
  20454. }
  20455. printf(resultFmt, ret == 0 ? passed : failed);
  20456. wc_curve448_free(&key);
  20457. wc_FreeRng(&rng);
  20458. #endif
  20459. return ret;
  20460. } /*END test_wc_curve448_import*/
  20461. /*
  20462. * Testing test_wc_curve448_size.
  20463. */
  20464. static int test_wc_curve448_size (void)
  20465. {
  20466. int ret = 0;
  20467. #if defined(HAVE_CURVE448)
  20468. curve448_key key;
  20469. printf(testingFmt, "wc_curve448_size()");
  20470. ret = wc_curve448_init(&key);
  20471. /* Test good args for wc_curve448_size */
  20472. if (ret == 0) {
  20473. ret = wc_curve448_size(&key);
  20474. }
  20475. /* Test bad args for wc_curve448_size */
  20476. if (ret != 0) {
  20477. ret = wc_curve448_size(NULL);
  20478. }
  20479. printf(resultFmt, ret == 0 ? passed : failed);
  20480. wc_curve448_free(&key);
  20481. #endif
  20482. return ret;
  20483. } /* END test_wc_curve448_size*/
  20484. /*
  20485. * Testing wc_ecc_make_key.
  20486. */
  20487. static int test_wc_ecc_make_key (void)
  20488. {
  20489. int ret = 0;
  20490. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20491. WC_RNG rng;
  20492. ecc_key key;
  20493. printf(testingFmt, "wc_ecc_make_key()");
  20494. ret = wc_InitRng(&rng);
  20495. if (ret != 0)
  20496. return ret;
  20497. ret = wc_ecc_init(&key);
  20498. if (ret == 0) {
  20499. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20500. #if defined(WOLFSSL_ASYNC_CRYPT)
  20501. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20502. #endif
  20503. /* Pass in bad args. */
  20504. if (ret == 0) {
  20505. ret = wc_ecc_make_key(NULL, KEY14, &key);
  20506. if (ret == BAD_FUNC_ARG) {
  20507. ret = wc_ecc_make_key(&rng, KEY14, NULL);
  20508. }
  20509. if (ret == BAD_FUNC_ARG) {
  20510. ret = 0;
  20511. } else if (ret == 0) {
  20512. ret = WOLFSSL_FATAL_ERROR;
  20513. }
  20514. }
  20515. wc_ecc_free(&key);
  20516. }
  20517. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  20518. ret = WOLFSSL_FATAL_ERROR;
  20519. }
  20520. #ifdef FP_ECC
  20521. wc_ecc_fp_free();
  20522. #endif
  20523. printf(resultFmt, ret == 0 ? passed : failed);
  20524. #endif
  20525. return ret;
  20526. } /* END test_wc_ecc_make_key */
  20527. /*
  20528. * Testing wc_ecc_init()
  20529. */
  20530. static int test_wc_ecc_init (void)
  20531. {
  20532. int ret = 0;
  20533. #ifdef HAVE_ECC
  20534. ecc_key key;
  20535. printf(testingFmt, "wc_ecc_init()");
  20536. ret = wc_ecc_init(&key);
  20537. /* Pass in bad args. */
  20538. if (ret == 0) {
  20539. ret = wc_ecc_init(NULL);
  20540. if (ret == BAD_FUNC_ARG) {
  20541. ret = 0;
  20542. } else if (ret == 0) {
  20543. ret = WOLFSSL_FATAL_ERROR;
  20544. }
  20545. }
  20546. printf(resultFmt, ret == 0 ? passed : failed);
  20547. wc_ecc_free(&key);
  20548. #endif
  20549. return ret;
  20550. } /* END test_wc_ecc_init */
  20551. /*
  20552. * Testing wc_ecc_check_key()
  20553. */
  20554. static int test_wc_ecc_check_key (void)
  20555. {
  20556. int ret = 0;
  20557. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20558. WC_RNG rng;
  20559. ecc_key key;
  20560. XMEMSET(&rng, 0, sizeof(rng));
  20561. XMEMSET(&key, 0, sizeof(key));
  20562. ret = wc_InitRng(&rng);
  20563. if (ret == 0) {
  20564. ret = wc_ecc_init(&key);
  20565. if (ret == 0) {
  20566. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20567. #if defined(WOLFSSL_ASYNC_CRYPT)
  20568. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20569. #endif
  20570. }
  20571. }
  20572. printf(testingFmt, "wc_ecc_check_key()");
  20573. if (ret == 0) {
  20574. ret = wc_ecc_check_key(&key);
  20575. }
  20576. /* Pass in bad args. */
  20577. if (ret == 0) {
  20578. ret = wc_ecc_check_key(NULL);
  20579. if (ret == BAD_FUNC_ARG) {
  20580. ret = 0;
  20581. } else if (ret == 0) {
  20582. ret = WOLFSSL_FATAL_ERROR;
  20583. }
  20584. }
  20585. printf(resultFmt, ret == 0 ? passed : failed);
  20586. if (wc_FreeRng(&rng) && ret == 0) {
  20587. ret = WOLFSSL_FATAL_ERROR;
  20588. }
  20589. wc_ecc_free(&key);
  20590. #ifdef FP_ECC
  20591. wc_ecc_fp_free();
  20592. #endif
  20593. #endif
  20594. return ret;
  20595. } /* END test_wc_ecc_check_key */
  20596. /*
  20597. * Testing wc_ecc_get_generator()
  20598. */
  20599. static int test_wc_ecc_get_generator(void)
  20600. {
  20601. int ret = 0;
  20602. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  20603. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  20604. ecc_point* pt;
  20605. printf(testingFmt, "wc_ecc_new_point()");
  20606. pt = wc_ecc_new_point();
  20607. if (!pt) {
  20608. ret = WOLFSSL_FATAL_ERROR;
  20609. }
  20610. printf(testingFmt, "wc_ecc_get_generator()");
  20611. if (ret == 0) {
  20612. ret = wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1));
  20613. }
  20614. /* Test bad args. */
  20615. if (ret == MP_OKAY) {
  20616. /* Returns Zero for bad arg. */
  20617. ret = wc_ecc_get_generator(pt, -1);
  20618. if (ret != MP_OKAY)
  20619. wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1));
  20620. if (ret != MP_OKAY)
  20621. wc_ecc_get_generator(pt, 1000); /* If we ever get to 1000 curves
  20622. * increase this number */
  20623. if (ret != MP_OKAY)
  20624. wc_ecc_get_generator(NULL, -1);
  20625. ret = ret == MP_OKAY ? WOLFSSL_FATAL_ERROR : 0;
  20626. }
  20627. printf(resultFmt, ret == 0 ? passed : failed);
  20628. wc_ecc_del_point(pt);
  20629. #endif
  20630. return ret;
  20631. } /* END test_wc_ecc_get_generator */
  20632. /*
  20633. * Testing wc_ecc_size()
  20634. */
  20635. static int test_wc_ecc_size (void)
  20636. {
  20637. int ret = 0;
  20638. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20639. WC_RNG rng;
  20640. ecc_key key;
  20641. XMEMSET(&rng, 0, sizeof(rng));
  20642. XMEMSET(&key, 0, sizeof(key));
  20643. ret = wc_InitRng(&rng);
  20644. if (ret == 0) {
  20645. ret = wc_ecc_init(&key);
  20646. if (ret == 0) {
  20647. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20648. #if defined(WOLFSSL_ASYNC_CRYPT)
  20649. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20650. #endif
  20651. }
  20652. }
  20653. printf(testingFmt, "wc_ecc_size()");
  20654. if (ret == 0) {
  20655. ret = wc_ecc_size(&key);
  20656. if (ret == KEY14) {
  20657. ret = 0;
  20658. } else if (ret == 0){
  20659. ret = WOLFSSL_FATAL_ERROR;
  20660. }
  20661. }
  20662. /* Test bad args. */
  20663. if (ret == 0) {
  20664. /* Returns Zero for bad arg. */
  20665. ret = wc_ecc_size(NULL);
  20666. }
  20667. printf(resultFmt, ret == 0 ? passed : failed);
  20668. if (wc_FreeRng(&rng) && ret == 0) {
  20669. ret = WOLFSSL_FATAL_ERROR;
  20670. }
  20671. wc_ecc_free(&key);
  20672. #endif
  20673. return ret;
  20674. } /* END test_wc_ecc_size */
  20675. static void test_wc_ecc_params(void)
  20676. {
  20677. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  20678. It was added after certifications */
  20679. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  20680. const ecc_set_type* ecc_set;
  20681. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  20682. /* Test for SECP256R1 curve */
  20683. int curve_id = ECC_SECP256R1;
  20684. int curve_idx = wc_ecc_get_curve_idx(curve_id);
  20685. AssertIntNE(curve_idx, ECC_CURVE_INVALID);
  20686. ecc_set = wc_ecc_get_curve_params(curve_idx);
  20687. AssertNotNull(ecc_set);
  20688. AssertIntEQ(ecc_set->id, curve_id);
  20689. #endif
  20690. /* Test case when SECP256R1 is not enabled */
  20691. /* Test that we get curve params for index 0 */
  20692. ecc_set = wc_ecc_get_curve_params(0);
  20693. AssertNotNull(ecc_set);
  20694. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  20695. }
  20696. /*
  20697. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  20698. */
  20699. static int test_wc_ecc_signVerify_hash (void)
  20700. {
  20701. int ret = 0;
  20702. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  20703. WC_RNG rng;
  20704. ecc_key key;
  20705. int signH = WOLFSSL_FATAL_ERROR;
  20706. #ifdef HAVE_ECC_VERIFY
  20707. int verifyH = WOLFSSL_FATAL_ERROR;
  20708. int verify = 0;
  20709. #endif
  20710. word32 siglen = ECC_BUFSIZE;
  20711. byte sig[ECC_BUFSIZE];
  20712. byte adjustedSig[ECC_BUFSIZE+1];
  20713. byte digest[] = TEST_STRING;
  20714. word32 digestlen = (word32)TEST_STRING_SZ;
  20715. /* Init stack var */
  20716. XMEMSET(sig, 0, siglen);
  20717. XMEMSET(&key, 0, sizeof(key));
  20718. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  20719. /* Init structs. */
  20720. ret = wc_InitRng(&rng);
  20721. if (ret == 0) {
  20722. ret = wc_ecc_init(&key);
  20723. if (ret == 0) {
  20724. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20725. #if defined(WOLFSSL_ASYNC_CRYPT)
  20726. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20727. #endif
  20728. }
  20729. }
  20730. printf(testingFmt, "wc_ecc_sign_hash()");
  20731. if (ret == 0) {
  20732. ret = wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key);
  20733. }
  20734. /* Check bad args. */
  20735. if (ret == 0) {
  20736. signH = wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key);
  20737. if (signH == ECC_BAD_ARG_E) {
  20738. signH = wc_ecc_sign_hash(digest, digestlen, NULL, &siglen,
  20739. &rng, &key);
  20740. }
  20741. if (signH == ECC_BAD_ARG_E) {
  20742. signH = wc_ecc_sign_hash(digest, digestlen, sig, NULL,
  20743. &rng, &key);
  20744. }
  20745. if (signH == ECC_BAD_ARG_E) {
  20746. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  20747. NULL, &key);
  20748. }
  20749. if (signH == ECC_BAD_ARG_E) {
  20750. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  20751. &rng, NULL);
  20752. }
  20753. if (signH == ECC_BAD_ARG_E) {
  20754. signH = 0;
  20755. } else if (ret == 0) {
  20756. signH = WOLFSSL_FATAL_ERROR;
  20757. }
  20758. }
  20759. printf(resultFmt, signH == 0 ? passed : failed);
  20760. #ifdef HAVE_ECC_VERIFY
  20761. printf(testingFmt, "wc_ecc_verify_hash()");
  20762. ret = wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify, &key);
  20763. if (verify != 1 && ret == 0) {
  20764. ret = WOLFSSL_FATAL_ERROR;
  20765. }
  20766. /* test check on length of signature passed in */
  20767. XMEMCPY(adjustedSig, sig, siglen);
  20768. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  20769. #ifndef NO_STRICT_ECDSA_LEN
  20770. AssertIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  20771. &verify, &key), 0);
  20772. #else
  20773. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  20774. * is allowed */
  20775. AssertIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  20776. &verify, &key), 0);
  20777. #endif
  20778. /* Test bad args. */
  20779. if (ret == 0) {
  20780. verifyH = wc_ecc_verify_hash(NULL, siglen, digest, digestlen,
  20781. &verify, &key);
  20782. if (verifyH == ECC_BAD_ARG_E) {
  20783. verifyH = wc_ecc_verify_hash(sig, siglen, NULL, digestlen,
  20784. &verify, &key);
  20785. }
  20786. if (verifyH == ECC_BAD_ARG_E) {
  20787. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  20788. NULL, &key);
  20789. }
  20790. if (verifyH == ECC_BAD_ARG_E) {
  20791. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  20792. &verify, NULL);
  20793. }
  20794. if (verifyH == ECC_BAD_ARG_E) {
  20795. verifyH = 0;
  20796. } else if (ret == 0) {
  20797. verifyH = WOLFSSL_FATAL_ERROR;
  20798. }
  20799. }
  20800. printf(resultFmt, verifyH == 0 ? passed : failed);
  20801. #endif /* HAVE_ECC_VERIFY */
  20802. if (wc_FreeRng(&rng) && ret == 0) {
  20803. ret = WOLFSSL_FATAL_ERROR;
  20804. }
  20805. wc_ecc_free(&key);
  20806. #ifdef FP_ECC
  20807. wc_ecc_fp_free();
  20808. #endif
  20809. #endif
  20810. return ret;
  20811. } /* END test_wc_ecc_sign_hash */
  20812. /*
  20813. * Testing wc_ecc_shared_secret()
  20814. */
  20815. static int test_wc_ecc_shared_secret (void)
  20816. {
  20817. int ret = 0;
  20818. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  20819. ecc_key key, pubKey;
  20820. WC_RNG rng;
  20821. byte out[KEY32];
  20822. int keySz = sizeof(out);
  20823. word32 outlen = (word32)sizeof(out);
  20824. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  20825. const char* qx =
  20826. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  20827. const char* qy =
  20828. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  20829. const char* d =
  20830. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  20831. const char* curveName = "SECP256R1";
  20832. const byte expected_shared_secret[] =
  20833. {
  20834. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  20835. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  20836. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  20837. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  20838. };
  20839. #endif
  20840. /* Initialize variables. */
  20841. XMEMSET(out, 0, keySz);
  20842. XMEMSET(&rng, 0, sizeof(rng));
  20843. XMEMSET(&key, 0, sizeof(key));
  20844. XMEMSET(&pubKey, 0, sizeof(pubKey));
  20845. ret = wc_InitRng(&rng);
  20846. if (ret == 0) {
  20847. ret = wc_ecc_init(&key);
  20848. if (ret == 0) {
  20849. ret = wc_ecc_init(&pubKey);
  20850. }
  20851. }
  20852. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  20853. if (ret == 0) {
  20854. ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
  20855. }
  20856. if (ret == 0) {
  20857. ret = wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName);
  20858. }
  20859. #else
  20860. if (ret == 0) {
  20861. ret = wc_ecc_make_key(&rng, keySz, &key);
  20862. #if defined(WOLFSSL_ASYNC_CRYPT)
  20863. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20864. #endif
  20865. }
  20866. if (ret == 0) {
  20867. ret = wc_ecc_make_key(&rng, keySz, &pubKey);
  20868. #if defined(WOLFSSL_ASYNC_CRYPT)
  20869. ret = wc_AsyncWait(ret, &pubKey.asyncDev, WC_ASYNC_FLAG_NONE);
  20870. #endif
  20871. }
  20872. #endif
  20873. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  20874. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  20875. !defined(HAVE_SELFTEST)
  20876. if (ret == 0) {
  20877. ret = wc_ecc_set_rng(&key, &rng);
  20878. }
  20879. #endif
  20880. printf(testingFmt, "wc_ecc_shared_secret()");
  20881. if (ret == 0) {
  20882. ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
  20883. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  20884. if (ret == 0) {
  20885. if (0 != XMEMCMP(out, expected_shared_secret, outlen)) {
  20886. ret = WOLFSSL_FATAL_ERROR;
  20887. }
  20888. }
  20889. #endif
  20890. /* Test bad args. */
  20891. if (ret == 0) {
  20892. ret = wc_ecc_shared_secret(NULL, &pubKey, out, &outlen);
  20893. if (ret == BAD_FUNC_ARG) {
  20894. ret = wc_ecc_shared_secret(&key, NULL, out, &outlen);
  20895. }
  20896. if (ret == BAD_FUNC_ARG) {
  20897. ret = wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen);
  20898. }
  20899. if (ret == BAD_FUNC_ARG) {
  20900. ret = wc_ecc_shared_secret(&key, &pubKey, out, NULL);
  20901. }
  20902. if (ret == BAD_FUNC_ARG) {
  20903. /* Invalid length */
  20904. outlen = 1;
  20905. ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
  20906. }
  20907. if (ret == BUFFER_E) {
  20908. ret = 0;
  20909. } else if (ret == 0) {
  20910. ret = WOLFSSL_FATAL_ERROR;
  20911. }
  20912. }
  20913. }
  20914. printf(resultFmt, ret == 0 ? passed : failed);
  20915. if (wc_FreeRng(&rng) && ret == 0) {
  20916. ret = WOLFSSL_FATAL_ERROR;
  20917. }
  20918. wc_ecc_free(&key);
  20919. wc_ecc_free(&pubKey);
  20920. #ifdef FP_ECC
  20921. wc_ecc_fp_free();
  20922. #endif
  20923. #endif
  20924. return ret;
  20925. } /* END tests_wc_ecc_shared_secret */
  20926. /*
  20927. * testint wc_ecc_export_x963()
  20928. */
  20929. static int test_wc_ecc_export_x963 (void)
  20930. {
  20931. int ret = 0;
  20932. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  20933. ecc_key key;
  20934. WC_RNG rng;
  20935. byte out[ECC_ASN963_MAX_BUF_SZ];
  20936. word32 outlen = sizeof(out);
  20937. /* Initialize variables. */
  20938. XMEMSET(out, 0, outlen);
  20939. XMEMSET(&rng, 0, sizeof(rng));
  20940. XMEMSET(&key, 0, sizeof(key));
  20941. ret = wc_InitRng(&rng);
  20942. if (ret == 0) {
  20943. ret = wc_ecc_init(&key);
  20944. if (ret == 0) {
  20945. ret = wc_ecc_make_key(&rng, KEY20, &key);
  20946. #if defined(WOLFSSL_ASYNC_CRYPT)
  20947. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20948. #endif
  20949. }
  20950. }
  20951. printf(testingFmt, "wc_ecc_export_x963()");
  20952. if (ret == 0) {
  20953. ret = wc_ecc_export_x963(&key, out, &outlen);
  20954. }
  20955. /* Test bad args. */
  20956. if (ret == 0) {
  20957. ret = wc_ecc_export_x963(NULL, out, &outlen);
  20958. if (ret == ECC_BAD_ARG_E) {
  20959. ret = wc_ecc_export_x963(&key, NULL, &outlen);
  20960. }
  20961. if (ret == LENGTH_ONLY_E) {
  20962. ret = wc_ecc_export_x963(&key, out, NULL);
  20963. }
  20964. if (ret == ECC_BAD_ARG_E) {
  20965. key.idx = -4;
  20966. ret = wc_ecc_export_x963(&key, out, &outlen);
  20967. }
  20968. if (ret == ECC_BAD_ARG_E) {
  20969. ret = 0;
  20970. } else {
  20971. ret = WOLFSSL_FATAL_ERROR;
  20972. }
  20973. }
  20974. printf(resultFmt, ret == 0 ? passed : failed);
  20975. if (wc_FreeRng(&rng) && ret == 0) {
  20976. ret = WOLFSSL_FATAL_ERROR;
  20977. }
  20978. wc_ecc_free(&key);
  20979. #ifdef FP_ECC
  20980. wc_ecc_fp_free();
  20981. #endif
  20982. #endif
  20983. return ret;
  20984. } /* END test_wc_ecc_export_x963 */
  20985. /*
  20986. * Testing wc_ecc_export_x963_ex()
  20987. * compile with --enable-compkey will use compression.
  20988. */
  20989. static int test_wc_ecc_export_x963_ex (void)
  20990. {
  20991. int ret = 0;
  20992. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  20993. ecc_key key;
  20994. WC_RNG rng;
  20995. byte out[ECC_ASN963_MAX_BUF_SZ];
  20996. word32 outlen = sizeof(out);
  20997. #ifdef HAVE_COMP_KEY
  20998. word32 badOutLen = 5;
  20999. #endif
  21000. /* Init stack variables. */
  21001. XMEMSET(out, 0, outlen);
  21002. XMEMSET(&rng, 0, sizeof(rng));
  21003. XMEMSET(&key, 0, sizeof(key));
  21004. ret = wc_InitRng(&rng);
  21005. if (ret == 0) {
  21006. ret = wc_ecc_init(&key);
  21007. if (ret == 0) {
  21008. ret = wc_ecc_make_key(&rng, KEY64, &key);
  21009. #if defined(WOLFSSL_ASYNC_CRYPT)
  21010. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21011. #endif
  21012. }
  21013. }
  21014. printf(testingFmt, "wc_ecc_export_x963_ex()");
  21015. #ifdef HAVE_COMP_KEY
  21016. if (ret == 0) {
  21017. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  21018. }
  21019. #else
  21020. if (ret == 0) {
  21021. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  21022. }
  21023. #endif
  21024. /* Test bad args. */
  21025. #ifdef HAVE_COMP_KEY
  21026. if (ret == 0) {
  21027. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, COMP);
  21028. if (ret == BAD_FUNC_ARG) {
  21029. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP);
  21030. }
  21031. if (ret == BAD_FUNC_ARG) {
  21032. ret = wc_ecc_export_x963_ex(&key, out, NULL, COMP);
  21033. }
  21034. if (ret == BAD_FUNC_ARG) {
  21035. ret = wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP);
  21036. }
  21037. if (ret == BUFFER_E) {
  21038. key.idx = -4;
  21039. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  21040. }
  21041. if (ret == ECC_BAD_ARG_E) {
  21042. ret = 0;
  21043. } else {
  21044. ret = WOLFSSL_FATAL_ERROR;
  21045. }
  21046. }
  21047. #else
  21048. if (ret == 0) {
  21049. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP);
  21050. if (ret == BAD_FUNC_ARG) {
  21051. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP);
  21052. }
  21053. if (ret == BAD_FUNC_ARG) {
  21054. ret = wc_ecc_export_x963_ex(&key, out, &outlen, 1);
  21055. }
  21056. if (ret == NOT_COMPILED_IN) {
  21057. ret = wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP);
  21058. }
  21059. if (ret == BAD_FUNC_ARG) {
  21060. key.idx = -4;
  21061. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  21062. }
  21063. if (ret == ECC_BAD_ARG_E) {
  21064. ret = 0;
  21065. } else if (ret == 0) {
  21066. ret = WOLFSSL_FATAL_ERROR;
  21067. }
  21068. }
  21069. #endif
  21070. printf(resultFmt, ret == 0 ? passed : failed);
  21071. if (wc_FreeRng(&rng) && ret == 0) {
  21072. ret = WOLFSSL_FATAL_ERROR;
  21073. }
  21074. wc_ecc_free(&key);
  21075. #ifdef FP_ECC
  21076. wc_ecc_fp_free();
  21077. #endif
  21078. #endif
  21079. return ret;
  21080. } /* END test_wc_ecc_export_x963_ex */
  21081. /*
  21082. * testing wc_ecc_import_x963()
  21083. */
  21084. static int test_wc_ecc_import_x963 (void)
  21085. {
  21086. int ret = 0;
  21087. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21088. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21089. ecc_key pubKey, key;
  21090. WC_RNG rng;
  21091. byte x963[ECC_ASN963_MAX_BUF_SZ];
  21092. word32 x963Len = (word32)sizeof(x963);
  21093. /* Init stack variables. */
  21094. XMEMSET(x963, 0, x963Len);
  21095. XMEMSET(&rng, 0, sizeof(rng));
  21096. XMEMSET(&key, 0, sizeof(key));
  21097. XMEMSET(&pubKey, 0, sizeof(pubKey));
  21098. ret = wc_InitRng(&rng);
  21099. if (ret == 0) {
  21100. ret = wc_ecc_init(&pubKey);
  21101. if (ret == 0) {
  21102. ret = wc_ecc_init(&key);
  21103. }
  21104. if (ret == 0) {
  21105. ret = wc_ecc_make_key(&rng, KEY24, &key);
  21106. #if defined(WOLFSSL_ASYNC_CRYPT)
  21107. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21108. #endif
  21109. }
  21110. if (ret == 0) {
  21111. PRIVATE_KEY_UNLOCK();
  21112. ret = wc_ecc_export_x963(&key, x963, &x963Len);
  21113. PRIVATE_KEY_LOCK();
  21114. }
  21115. }
  21116. printf(testingFmt, "wc_ecc_import_x963()");
  21117. if (ret == 0) {
  21118. ret = wc_ecc_import_x963(x963, x963Len, &pubKey);
  21119. }
  21120. /* Test bad args. */
  21121. if (ret == 0) {
  21122. ret = wc_ecc_import_x963(NULL, x963Len, &pubKey);
  21123. if (ret == BAD_FUNC_ARG) {
  21124. ret = wc_ecc_import_x963(x963, x963Len, NULL);
  21125. }
  21126. if (ret == BAD_FUNC_ARG) {
  21127. ret = wc_ecc_import_x963(x963, x963Len + 1, &pubKey);
  21128. }
  21129. if (ret == ECC_BAD_ARG_E) {
  21130. ret = 0;
  21131. } else if (ret == 0) {
  21132. ret = WOLFSSL_FATAL_ERROR;
  21133. }
  21134. }
  21135. printf(resultFmt, ret == 0 ? passed : failed);
  21136. if (wc_FreeRng(&rng) && ret == 0) {
  21137. ret = WOLFSSL_FATAL_ERROR;
  21138. }
  21139. wc_ecc_free(&key);
  21140. wc_ecc_free(&pubKey);
  21141. #ifdef FP_ECC
  21142. wc_ecc_fp_free();
  21143. #endif
  21144. #endif
  21145. return ret;
  21146. } /* END wc_ecc_import_x963 */
  21147. /*
  21148. * testing wc_ecc_import_private_key()
  21149. */
  21150. static int ecc_import_private_key (void)
  21151. {
  21152. int ret = 0;
  21153. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21154. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21155. ecc_key key, keyImp;
  21156. WC_RNG rng;
  21157. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  21158. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  21159. word32 privKeySz = (word32)sizeof(privKey);
  21160. word32 x963KeySz = (word32)sizeof(x963Key);
  21161. /* Init stack variables. */
  21162. XMEMSET(privKey, 0, privKeySz);
  21163. XMEMSET(x963Key, 0, x963KeySz);
  21164. XMEMSET(&rng, 0, sizeof(rng));
  21165. XMEMSET(&key, 0, sizeof(key));
  21166. XMEMSET(&keyImp, 0, sizeof(keyImp));
  21167. ret = wc_InitRng(&rng);
  21168. if (ret == 0) {
  21169. ret = wc_ecc_init(&key);
  21170. if (ret == 0) {
  21171. ret = wc_ecc_init(&keyImp);
  21172. }
  21173. if (ret == 0) {
  21174. ret = wc_ecc_make_key(&rng, KEY48, &key);
  21175. #if defined(WOLFSSL_ASYNC_CRYPT)
  21176. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21177. #endif
  21178. }
  21179. if (ret == 0) {
  21180. PRIVATE_KEY_UNLOCK();
  21181. ret = wc_ecc_export_x963(&key, x963Key, &x963KeySz);
  21182. PRIVATE_KEY_LOCK();
  21183. }
  21184. if (ret == 0) {
  21185. ret = wc_ecc_export_private_only(&key, privKey, &privKeySz);
  21186. }
  21187. }
  21188. printf(testingFmt, "wc_ecc_import_private_key()");
  21189. if (ret == 0) {
  21190. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21191. x963KeySz, &keyImp);
  21192. }
  21193. /* Pass in bad args. */
  21194. if (ret == 0) {
  21195. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21196. x963KeySz, NULL);
  21197. if (ret == BAD_FUNC_ARG) {
  21198. ret = wc_ecc_import_private_key(NULL, privKeySz, x963Key,
  21199. x963KeySz, &keyImp);
  21200. }
  21201. if (ret == BAD_FUNC_ARG) {
  21202. ret = 0;
  21203. } else if (ret == 0) {
  21204. ret = WOLFSSL_FATAL_ERROR;
  21205. }
  21206. }
  21207. printf(resultFmt, ret == 0 ? passed : failed);
  21208. if (wc_FreeRng(&rng) && ret == 0) {
  21209. ret = WOLFSSL_FATAL_ERROR;
  21210. }
  21211. wc_ecc_free(&key);
  21212. wc_ecc_free(&keyImp);
  21213. #ifdef FP_ECC
  21214. wc_ecc_fp_free();
  21215. #endif
  21216. #endif
  21217. return ret;
  21218. } /* END wc_ecc_import_private_key */
  21219. /*
  21220. * Testing wc_ecc_export_private_only()
  21221. */
  21222. static int test_wc_ecc_export_private_only (void)
  21223. {
  21224. int ret = 0;
  21225. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21226. ecc_key key;
  21227. WC_RNG rng;
  21228. byte out[ECC_PRIV_KEY_BUF];
  21229. word32 outlen = sizeof(out);
  21230. /* Init stack variables. */
  21231. XMEMSET(out, 0, outlen);
  21232. XMEMSET(&rng, 0, sizeof(rng));
  21233. XMEMSET(&key, 0, sizeof(key));
  21234. ret = wc_InitRng(&rng);
  21235. if (ret == 0) {
  21236. ret = wc_ecc_init(&key);
  21237. if (ret == 0) {
  21238. ret = wc_ecc_make_key(&rng, KEY32, &key);
  21239. #if defined(WOLFSSL_ASYNC_CRYPT)
  21240. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21241. #endif
  21242. }
  21243. }
  21244. printf(testingFmt, "wc_ecc_export_private_only()");
  21245. if (ret == 0) {
  21246. ret = wc_ecc_export_private_only(&key, out, &outlen);
  21247. }
  21248. /* Pass in bad args. */
  21249. if (ret == 0) {
  21250. ret = wc_ecc_export_private_only(NULL, out, &outlen);
  21251. if (ret == BAD_FUNC_ARG) {
  21252. ret = wc_ecc_export_private_only(&key, NULL, &outlen);
  21253. }
  21254. if (ret == BAD_FUNC_ARG) {
  21255. ret = wc_ecc_export_private_only(&key, out, NULL);
  21256. }
  21257. if (ret == BAD_FUNC_ARG) {
  21258. ret = 0;
  21259. } else if (ret == 0) {
  21260. ret = WOLFSSL_FATAL_ERROR;
  21261. }
  21262. }
  21263. printf(resultFmt, ret == 0 ? passed : failed);
  21264. if (wc_FreeRng(&rng) && ret == 0) {
  21265. ret = WOLFSSL_FATAL_ERROR;
  21266. }
  21267. wc_ecc_free(&key);
  21268. #ifdef FP_ECC
  21269. wc_ecc_fp_free();
  21270. #endif
  21271. #endif
  21272. return ret;
  21273. } /* END test_wc_ecc_export_private_only */
  21274. /*
  21275. * Testing wc_ecc_rs_to_sig()
  21276. */
  21277. static int test_wc_ecc_rs_to_sig (void)
  21278. {
  21279. int ret = 0;
  21280. #if defined(HAVE_ECC) && !defined(NO_ASN)
  21281. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  21282. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  21283. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  21284. const char* zeroStr = "0";
  21285. byte sig[ECC_MAX_SIG_SIZE];
  21286. word32 siglen = (word32)sizeof(sig);
  21287. /*R and S max size is the order of curve. 2^192.*/
  21288. int keySz = KEY24;
  21289. byte r[KEY24];
  21290. byte s[KEY24];
  21291. word32 rlen = (word32)sizeof(r);
  21292. word32 slen = (word32)sizeof(s);
  21293. /* Init stack variables. */
  21294. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  21295. XMEMSET(r, 0, keySz);
  21296. XMEMSET(s, 0, keySz);
  21297. printf(testingFmt, "wc_ecc_rs_to_sig()");
  21298. ret = wc_ecc_rs_to_sig(R, S, sig, &siglen);
  21299. /* Test bad args. */
  21300. if (ret == 0) {
  21301. ret = wc_ecc_rs_to_sig(NULL, S, sig, &siglen);
  21302. if (ret == ECC_BAD_ARG_E) {
  21303. ret = wc_ecc_rs_to_sig(R, NULL, sig, &siglen);
  21304. }
  21305. if (ret == ECC_BAD_ARG_E) {
  21306. ret = wc_ecc_rs_to_sig(R, S, sig, NULL);
  21307. }
  21308. if (ret == ECC_BAD_ARG_E) {
  21309. ret = wc_ecc_rs_to_sig(R, S, NULL, &siglen);
  21310. }
  21311. if (ret == ECC_BAD_ARG_E) {
  21312. ret = wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen);
  21313. }
  21314. if (ret == MP_ZERO_E) {
  21315. ret = wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen);
  21316. }
  21317. if (ret == MP_ZERO_E) {
  21318. ret = 0;
  21319. } else {
  21320. ret = WOLFSSL_FATAL_ERROR;
  21321. }
  21322. }
  21323. printf(resultFmt, ret == 0 ? passed : failed);
  21324. printf(testingFmt, "wc_ecc_sig_to_rs()");
  21325. if (ret == 0) {
  21326. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen);
  21327. }
  21328. /* Test bad args. */
  21329. if (ret == 0) {
  21330. ret = wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen);
  21331. if (ret == ECC_BAD_ARG_E) {
  21332. ret = wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen);
  21333. }
  21334. if (ret == ECC_BAD_ARG_E) {
  21335. ret = wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen);
  21336. }
  21337. if (ret == ECC_BAD_ARG_E) {
  21338. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen);
  21339. }
  21340. if (ret == ECC_BAD_ARG_E) {
  21341. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL);
  21342. }
  21343. if (ret == ECC_BAD_ARG_E) {
  21344. ret = 0;
  21345. } else if (ret == 0) {
  21346. ret = WOLFSSL_FATAL_ERROR;
  21347. }
  21348. }
  21349. printf(resultFmt, ret == 0 ? passed : failed);
  21350. #endif
  21351. return ret;
  21352. } /* END test_wc_ecc_rs_to_sig */
  21353. static int test_wc_ecc_import_raw(void)
  21354. {
  21355. int ret = 0;
  21356. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21357. ecc_key key;
  21358. const char* qx =
  21359. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21360. const char* qy =
  21361. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21362. const char* d =
  21363. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21364. const char* curveName = "SECP256R1";
  21365. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21366. const char* kNullStr = "";
  21367. #endif
  21368. ret = wc_ecc_init(&key);
  21369. printf(testingFmt, "wc_ecc_import_raw()");
  21370. if (ret == 0) {
  21371. ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
  21372. }
  21373. /* Test bad args. */
  21374. if (ret == 0) {
  21375. ret = wc_ecc_import_raw(NULL, qx, qy, d, curveName);
  21376. if (ret == BAD_FUNC_ARG) {
  21377. ret = wc_ecc_import_raw(&key, NULL, qy, d, curveName);
  21378. }
  21379. if (ret == BAD_FUNC_ARG) {
  21380. ret = wc_ecc_import_raw(&key, qx, NULL, d, curveName);
  21381. }
  21382. if (ret == BAD_FUNC_ARG) {
  21383. ret = wc_ecc_import_raw(&key, qx, qy, d, NULL);
  21384. }
  21385. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21386. if (ret == BAD_FUNC_ARG) {
  21387. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21388. wc_ecc_free(&key);
  21389. #endif
  21390. ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr, curveName);
  21391. if (ret == ECC_INF_E)
  21392. ret = BAD_FUNC_ARG; /* This is expected by other tests */
  21393. }
  21394. #endif
  21395. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  21396. if (ret == BAD_FUNC_ARG) {
  21397. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21398. wc_ecc_free(&key);
  21399. #endif
  21400. ret = wc_ecc_import_raw(&key, "0", qy, d, curveName);
  21401. }
  21402. if (ret == BAD_FUNC_ARG) {
  21403. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21404. wc_ecc_free(&key);
  21405. #endif
  21406. ret = wc_ecc_import_raw(&key, qx, "0", d, curveName);
  21407. }
  21408. #endif
  21409. if (ret == BAD_FUNC_ARG) {
  21410. ret = 0;
  21411. }
  21412. }
  21413. printf(resultFmt, ret == 0 ? passed : failed);
  21414. wc_ecc_free(&key);
  21415. #endif
  21416. return ret;
  21417. } /* END test_wc_ecc_import_raw */
  21418. static int test_wc_ecc_import_unsigned(void)
  21419. {
  21420. int ret = 0;
  21421. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  21422. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  21423. ecc_key key;
  21424. const byte qx[] = {
  21425. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  21426. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  21427. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  21428. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  21429. };
  21430. const byte qy[] = {
  21431. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  21432. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  21433. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  21434. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  21435. };
  21436. const byte d[] = {
  21437. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  21438. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  21439. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  21440. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  21441. };
  21442. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21443. const byte nullBytes[32] = {0};
  21444. #endif
  21445. int curveId = ECC_SECP256R1;
  21446. ret = wc_ecc_init(&key);
  21447. printf(testingFmt, "wc_ecc_import_unsigned()");
  21448. if (ret == 0) {
  21449. ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  21450. curveId);
  21451. }
  21452. /* Test bad args. */
  21453. if (ret == 0) {
  21454. ret = wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  21455. curveId);
  21456. if (ret == BAD_FUNC_ARG) {
  21457. ret = wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  21458. curveId);
  21459. }
  21460. if (ret == BAD_FUNC_ARG) {
  21461. ret = wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  21462. curveId);
  21463. }
  21464. if (ret == BAD_FUNC_ARG) {
  21465. ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  21466. ECC_CURVE_INVALID);
  21467. }
  21468. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21469. if (ret == BAD_FUNC_ARG) {
  21470. ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  21471. (byte*)nullBytes, (byte*)nullBytes, curveId);
  21472. }
  21473. #endif
  21474. if (ret == BAD_FUNC_ARG || ret == ECC_INF_E) {
  21475. ret = 0;
  21476. }
  21477. }
  21478. printf(resultFmt, ret == 0 ? passed : failed);
  21479. wc_ecc_free(&key);
  21480. #endif
  21481. return ret;
  21482. } /* END test_wc_ecc_import_unsigned */
  21483. /*
  21484. * Testing wc_ecc_sig_size()
  21485. */
  21486. static int test_wc_ecc_sig_size (void)
  21487. {
  21488. int ret = 0;
  21489. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21490. ecc_key key;
  21491. WC_RNG rng;
  21492. int keySz = KEY16;
  21493. XMEMSET(&rng, 0, sizeof(rng));
  21494. XMEMSET(&key, 0, sizeof(key));
  21495. ret = wc_InitRng(&rng);
  21496. if (ret == 0) {
  21497. ret = wc_ecc_init(&key);
  21498. if (ret == 0) {
  21499. ret = wc_ecc_make_key(&rng, keySz, &key);
  21500. #if defined(WOLFSSL_ASYNC_CRYPT)
  21501. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21502. #endif
  21503. }
  21504. }
  21505. printf(testingFmt, "wc_ecc_sig_size()");
  21506. if (ret == 0) {
  21507. ret = wc_ecc_sig_size(&key);
  21508. if (ret <= (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ)) {
  21509. ret = 0;
  21510. }
  21511. }
  21512. printf(resultFmt, ret == 0 ? passed : failed);
  21513. if (wc_FreeRng(&rng) && ret == 0) {
  21514. ret = WOLFSSL_FATAL_ERROR;
  21515. }
  21516. wc_ecc_free(&key);
  21517. #endif
  21518. return ret;
  21519. } /* END test_wc_ecc_sig_size */
  21520. /*
  21521. * Testing wc_ecc_ctx_new()
  21522. */
  21523. static int test_wc_ecc_ctx_new (void)
  21524. {
  21525. int ret = 0;
  21526. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21527. WC_RNG rng;
  21528. ecEncCtx* cli = NULL;
  21529. ecEncCtx* srv = NULL;
  21530. ret = wc_InitRng(&rng);
  21531. printf(testingFmt, "wc_ecc_ctx_new()");
  21532. if (ret == 0) {
  21533. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
  21534. srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng);
  21535. }
  21536. if (ret == 0 && (cli == NULL || srv == NULL)) {
  21537. ret = WOLFSSL_FATAL_ERROR;
  21538. }
  21539. wc_ecc_ctx_free(cli);
  21540. wc_ecc_ctx_free(srv);
  21541. /* Test bad args. */
  21542. if (ret == 0) {
  21543. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  21544. cli = wc_ecc_ctx_new(0, &rng);
  21545. if (cli != NULL) {
  21546. ret = WOLFSSL_FATAL_ERROR;
  21547. }
  21548. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL);
  21549. if (cli != NULL) {
  21550. ret = WOLFSSL_FATAL_ERROR;
  21551. }
  21552. }
  21553. printf(resultFmt, ret == 0 ? passed : failed);
  21554. if (wc_FreeRng(&rng) && ret == 0) {
  21555. ret = WOLFSSL_FATAL_ERROR;
  21556. }
  21557. wc_ecc_ctx_free(cli);
  21558. #endif
  21559. return ret;
  21560. } /* END test_wc_ecc_ctx_new */
  21561. /*
  21562. * Tesing wc_ecc_reset()
  21563. */
  21564. static int test_wc_ecc_ctx_reset (void)
  21565. {
  21566. int ret = 0;
  21567. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21568. ecEncCtx* ctx = NULL;
  21569. WC_RNG rng;
  21570. ret = wc_InitRng(&rng);
  21571. if (ret == 0) {
  21572. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) {
  21573. ret = WOLFSSL_FATAL_ERROR;
  21574. }
  21575. }
  21576. printf(testingFmt, "wc_ecc_ctx_reset()");
  21577. if (ret == 0) {
  21578. ret = wc_ecc_ctx_reset(ctx, &rng);
  21579. }
  21580. /* Pass in bad args. */
  21581. if (ret == 0) {
  21582. ret = wc_ecc_ctx_reset(NULL, &rng);
  21583. if (ret == BAD_FUNC_ARG) {
  21584. ret = wc_ecc_ctx_reset(ctx, NULL);
  21585. }
  21586. if (ret == BAD_FUNC_ARG) {
  21587. ret = 0;
  21588. } else if (ret == 0) {
  21589. ret = WOLFSSL_FATAL_ERROR;
  21590. }
  21591. }
  21592. printf(resultFmt, ret == 0 ? passed : failed);
  21593. if (wc_FreeRng(&rng) && ret == 0) {
  21594. ret = WOLFSSL_FATAL_ERROR;
  21595. }
  21596. wc_ecc_ctx_free(ctx);
  21597. #endif
  21598. return ret;
  21599. } /* END test_wc_ecc_ctx_reset */
  21600. /*
  21601. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  21602. */
  21603. static int test_wc_ecc_ctx_set_peer_salt (void)
  21604. {
  21605. int ret = 0;
  21606. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21607. WC_RNG rng;
  21608. ecEncCtx* cliCtx = NULL;
  21609. ecEncCtx* servCtx = NULL;
  21610. const byte* cliSalt = NULL;
  21611. const byte* servSalt = NULL;
  21612. ret = wc_InitRng(&rng);
  21613. if (ret == 0) {
  21614. if ( ( (cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) ||
  21615. ( (servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng)) == NULL) ) {
  21616. ret = WOLFSSL_FATAL_ERROR;
  21617. }
  21618. }
  21619. printf(testingFmt, "wc_ecc_ctx_get_own_salt()");
  21620. /* Test bad args. */
  21621. if (ret == 0) {
  21622. cliSalt = wc_ecc_ctx_get_own_salt(NULL);
  21623. if (cliSalt != NULL) {
  21624. ret = WOLFSSL_FATAL_ERROR;
  21625. }
  21626. }
  21627. if (ret == 0) {
  21628. cliSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  21629. servSalt = wc_ecc_ctx_get_own_salt(servCtx);
  21630. if (cliSalt == NULL || servSalt == NULL) {
  21631. ret = WOLFSSL_FATAL_ERROR;
  21632. }
  21633. }
  21634. printf(resultFmt, ret == 0 ? passed : failed);
  21635. printf(testingFmt, "wc_ecc_ctx_set_peer_salt()");
  21636. if (ret == 0) {
  21637. ret = wc_ecc_ctx_set_peer_salt(cliCtx, servSalt);
  21638. }
  21639. /* Test bad args. */
  21640. if (ret == 0) {
  21641. ret = wc_ecc_ctx_set_peer_salt(NULL, servSalt);
  21642. if (ret == BAD_FUNC_ARG) {
  21643. ret = wc_ecc_ctx_set_peer_salt(cliCtx, NULL);
  21644. }
  21645. if (ret == BAD_FUNC_ARG) {
  21646. ret = 0;
  21647. } else if (ret == 0) {
  21648. ret = WOLFSSL_FATAL_ERROR;
  21649. }
  21650. }
  21651. printf(resultFmt, ret == 0 ? passed : failed);
  21652. if (wc_FreeRng(&rng) && ret == 0) {
  21653. ret = WOLFSSL_FATAL_ERROR;
  21654. }
  21655. wc_ecc_ctx_free(cliCtx);
  21656. wc_ecc_ctx_free(servCtx);
  21657. #endif
  21658. return ret;
  21659. } /* END test_wc_ecc_ctx_set_peer_salt */
  21660. /*
  21661. * Testing wc_ecc_ctx_set_info()
  21662. */
  21663. static int test_wc_ecc_ctx_set_info (void)
  21664. {
  21665. int ret = 0;
  21666. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21667. ecEncCtx* ctx = NULL;
  21668. WC_RNG rng;
  21669. const char* optInfo = "Optional Test Info.";
  21670. int optInfoSz = (int)XSTRLEN(optInfo);
  21671. const char* badOptInfo = NULL;
  21672. ret = wc_InitRng(&rng);
  21673. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL || ret != 0 ) {
  21674. ret = WOLFSSL_FATAL_ERROR;
  21675. }
  21676. printf(testingFmt, "wc_ecc_ctx_set_info()");
  21677. if (ret == 0) {
  21678. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz);
  21679. }
  21680. /* Test bad args. */
  21681. if (ret == 0) {
  21682. ret = wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz);
  21683. if (ret == BAD_FUNC_ARG) {
  21684. ret = wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz);
  21685. }
  21686. if (ret == BAD_FUNC_ARG) {
  21687. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1);
  21688. }
  21689. if (ret == BAD_FUNC_ARG) {
  21690. ret = 0;
  21691. } else if (ret == 0) {
  21692. ret = WOLFSSL_FATAL_ERROR;
  21693. }
  21694. }
  21695. printf(resultFmt, ret == 0 ? passed : failed);
  21696. if (wc_FreeRng(&rng) && ret == 0) {
  21697. ret = WOLFSSL_FATAL_ERROR;
  21698. }
  21699. wc_ecc_ctx_free(ctx);
  21700. #endif
  21701. return ret;
  21702. } /* END test_wc_ecc_ctx_set_info */
  21703. /*
  21704. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  21705. */
  21706. static int test_wc_ecc_encryptDecrypt (void)
  21707. {
  21708. int ret = 0;
  21709. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  21710. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  21711. ecc_key srvKey, cliKey, tmpKey;
  21712. WC_RNG rng;
  21713. const char* msg = "EccBlock Size 16";
  21714. word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
  21715. #ifdef WOLFSSL_ECIES_OLD
  21716. byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  21717. #else
  21718. byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  21719. #endif
  21720. word32 outSz = (word32)sizeof(out);
  21721. byte plain[sizeof("EccBlock Size 16")];
  21722. word32 plainSz = (word32)sizeof(plain);
  21723. int keySz = KEY20;
  21724. /* Init stack variables. */
  21725. XMEMSET(out, 0, outSz);
  21726. XMEMSET(plain, 0, plainSz);
  21727. XMEMSET(&rng, 0, sizeof(rng));
  21728. XMEMSET(&srvKey, 0, sizeof(srvKey));
  21729. XMEMSET(&cliKey, 0, sizeof(cliKey));
  21730. ret = wc_InitRng(&rng);
  21731. if (ret == 0) {
  21732. ret = wc_ecc_init(&cliKey);
  21733. if (ret == 0) {
  21734. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  21735. #if defined(WOLFSSL_ASYNC_CRYPT)
  21736. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
  21737. #endif
  21738. }
  21739. if (ret == 0) {
  21740. ret = wc_ecc_init(&srvKey);
  21741. }
  21742. if (ret == 0) {
  21743. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  21744. #if defined(WOLFSSL_ASYNC_CRYPT)
  21745. ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
  21746. #endif
  21747. }
  21748. if (ret == 0) {
  21749. ret = wc_ecc_init(&tmpKey);
  21750. }
  21751. }
  21752. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21753. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21754. !defined(HAVE_SELFTEST)
  21755. if (ret == 0) {
  21756. ret = wc_ecc_set_rng(&srvKey, &rng);
  21757. }
  21758. if (ret == 0) {
  21759. ret = wc_ecc_set_rng(&cliKey, &rng);
  21760. }
  21761. #endif
  21762. printf(testingFmt, "wc_ecc_encrypt()");
  21763. if (ret == 0) {
  21764. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  21765. &outSz, NULL);
  21766. }
  21767. if (ret == 0) {
  21768. ret = wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out,
  21769. &outSz, NULL);
  21770. if (ret == BAD_FUNC_ARG) {
  21771. ret = wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out,
  21772. &outSz, NULL);
  21773. }
  21774. if (ret == BAD_FUNC_ARG) {
  21775. ret = wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out,
  21776. &outSz, NULL);
  21777. }
  21778. if (ret == BAD_FUNC_ARG) {
  21779. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  21780. &outSz, NULL);
  21781. }
  21782. if (ret == BAD_FUNC_ARG) {
  21783. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  21784. NULL, NULL);
  21785. }
  21786. if (ret == BAD_FUNC_ARG) {
  21787. ret = 0;
  21788. } else if (ret == 0) {
  21789. ret = WOLFSSL_FATAL_ERROR;
  21790. }
  21791. }
  21792. printf(resultFmt, ret == 0 ? passed : failed);
  21793. printf(testingFmt, "wc_ecc_decrypt()");
  21794. #ifdef WOLFSSL_ECIES_OLD
  21795. if (ret == 0) {
  21796. tmpKey.dp = cliKey.dp;
  21797. ret = wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey);
  21798. }
  21799. #endif
  21800. if (ret == 0) {
  21801. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain,
  21802. &plainSz, NULL);
  21803. }
  21804. if (ret == 0) {
  21805. ret = wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain,
  21806. &plainSz, NULL);
  21807. #ifdef WOLFSSL_ECIES_OLD
  21808. /* NULL parameter allowed in new implementations - public key comes from
  21809. * the message. */
  21810. if (ret == BAD_FUNC_ARG) {
  21811. ret = wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain,
  21812. &plainSz, NULL);
  21813. }
  21814. #endif
  21815. if (ret == BAD_FUNC_ARG) {
  21816. ret = wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain,
  21817. &plainSz, NULL);
  21818. }
  21819. if (ret == BAD_FUNC_ARG) {
  21820. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL,
  21821. &plainSz, NULL);
  21822. }
  21823. if (ret == BAD_FUNC_ARG) {
  21824. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz,
  21825. plain, NULL, NULL);
  21826. }
  21827. if (ret == BAD_FUNC_ARG) {
  21828. ret = 0;
  21829. } else if (ret == 0) {
  21830. ret = WOLFSSL_FATAL_ERROR;
  21831. }
  21832. }
  21833. if (XMEMCMP(msg, plain, msgSz) != 0) {
  21834. ret = WOLFSSL_FATAL_ERROR;
  21835. }
  21836. printf(resultFmt, ret == 0 ? passed : failed);
  21837. if (wc_FreeRng(&rng) && ret == 0) {
  21838. ret = WOLFSSL_FATAL_ERROR;
  21839. }
  21840. wc_ecc_free(&tmpKey);
  21841. wc_ecc_free(&cliKey);
  21842. wc_ecc_free(&srvKey);
  21843. #endif
  21844. return ret;
  21845. } /* END test_wc_ecc_encryptDecrypt */
  21846. /*
  21847. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  21848. */
  21849. static int test_wc_ecc_del_point (void)
  21850. {
  21851. int ret = 0;
  21852. #if defined(HAVE_ECC)
  21853. ecc_point* pt;
  21854. printf(testingFmt, "wc_ecc_new_point()");
  21855. pt = wc_ecc_new_point();
  21856. if (!pt) {
  21857. ret = WOLFSSL_FATAL_ERROR;
  21858. }
  21859. printf(resultFmt, ret == 0 ? passed : failed);
  21860. wc_ecc_del_point(pt);
  21861. #endif
  21862. return ret;
  21863. } /* END test_wc_ecc_del_point */
  21864. /*
  21865. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  21866. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  21867. * and wc_ecc_cmp_point()
  21868. */
  21869. static int test_wc_ecc_pointFns (void)
  21870. {
  21871. int ret = 0;
  21872. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  21873. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  21874. !defined(WOLFSSL_ATECC608A)
  21875. ecc_key key;
  21876. WC_RNG rng;
  21877. ecc_point* point = NULL;
  21878. ecc_point* cpypt = NULL;
  21879. int idx = 0;
  21880. int keySz = KEY32;
  21881. byte der[DER_SZ(KEY32)];
  21882. word32 derlenChk = 0;
  21883. word32 derSz = DER_SZ(KEY32);
  21884. /* Init stack variables. */
  21885. XMEMSET(der, 0, derSz);
  21886. XMEMSET(&rng, 0, sizeof(rng));
  21887. XMEMSET(&key, 0, sizeof(key));
  21888. ret = wc_InitRng(&rng);
  21889. if (ret == 0) {
  21890. ret = wc_ecc_init(&key);
  21891. if (ret == 0) {
  21892. ret = wc_ecc_make_key(&rng, keySz, &key);
  21893. #if defined(WOLFSSL_ASYNC_CRYPT)
  21894. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21895. #endif
  21896. }
  21897. }
  21898. if (ret == 0) {
  21899. point = wc_ecc_new_point();
  21900. if (!point) {
  21901. ret = WOLFSSL_FATAL_ERROR;
  21902. }
  21903. }
  21904. if (ret == 0) {
  21905. cpypt = wc_ecc_new_point();
  21906. if (!cpypt) {
  21907. ret = WOLFSSL_FATAL_ERROR;
  21908. }
  21909. }
  21910. /* Export */
  21911. printf(testingFmt, "wc_ecc_export_point_der()");
  21912. if (ret == 0) {
  21913. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  21914. NULL, &derlenChk);
  21915. /* Check length value. */
  21916. if (derSz == derlenChk && ret == LENGTH_ONLY_E) {
  21917. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  21918. der, &derSz);
  21919. }
  21920. }
  21921. /* Test bad args. */
  21922. if (ret == 0) {
  21923. ret = wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz);
  21924. if (ret == ECC_BAD_ARG_E) {
  21925. ret = wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz);
  21926. }
  21927. if (ret == ECC_BAD_ARG_E) {
  21928. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  21929. der, NULL);
  21930. }
  21931. if (ret == ECC_BAD_ARG_E) {
  21932. ret = 0;
  21933. } else if (ret == 0) {
  21934. ret = WOLFSSL_FATAL_ERROR;
  21935. }
  21936. }
  21937. printf(resultFmt, ret == 0 ? passed : failed);
  21938. /* Import */
  21939. printf(testingFmt, "wc_ecc_import_point_der()");
  21940. if (ret == 0) {
  21941. ret = wc_ecc_import_point_der(der, derSz, idx, point);
  21942. /* Condition double checks wc_ecc_cmp_point(). */
  21943. if (ret == 0 &&
  21944. XMEMCMP((void *)&key.pubkey, (void *)point, sizeof(key.pubkey))) {
  21945. ret = wc_ecc_cmp_point(&key.pubkey, point);
  21946. }
  21947. }
  21948. /* Test bad args. */
  21949. if (ret == 0) {
  21950. ret = wc_ecc_import_point_der(NULL, derSz, idx, point);
  21951. if (ret == ECC_BAD_ARG_E) {
  21952. ret = wc_ecc_import_point_der(der, derSz, idx, NULL);
  21953. }
  21954. if (ret == ECC_BAD_ARG_E) {
  21955. ret = wc_ecc_import_point_der(der, derSz, -1, point);
  21956. }
  21957. if (ret == ECC_BAD_ARG_E) {
  21958. ret = wc_ecc_import_point_der(der, derSz + 1, idx, point);
  21959. }
  21960. if (ret == ECC_BAD_ARG_E) {
  21961. ret = 0;
  21962. } else if (ret == 0) {
  21963. ret = WOLFSSL_FATAL_ERROR;
  21964. }
  21965. }
  21966. printf(resultFmt, ret == 0 ? passed : failed);
  21967. /* Copy */
  21968. printf(testingFmt, "wc_ecc_copy_point()");
  21969. if (ret == 0) {
  21970. ret = wc_ecc_copy_point(point, cpypt);
  21971. }
  21972. /* Test bad args. */
  21973. if (ret == 0) {
  21974. ret = wc_ecc_copy_point(NULL, cpypt);
  21975. if (ret == ECC_BAD_ARG_E) {
  21976. ret = wc_ecc_copy_point(point, NULL);
  21977. }
  21978. if (ret == ECC_BAD_ARG_E) {
  21979. ret = 0;
  21980. } else if (ret == 0) {
  21981. ret = WOLFSSL_FATAL_ERROR;
  21982. }
  21983. }
  21984. printf(resultFmt, ret == 0 ? passed : failed);
  21985. printf(testingFmt, "wc_ecc_cmp_point()");
  21986. /* Compare point */
  21987. if (ret == 0) {
  21988. ret = wc_ecc_cmp_point(point, cpypt);
  21989. }
  21990. /* Test bad args. */
  21991. if (ret == 0) {
  21992. ret = wc_ecc_cmp_point(NULL, cpypt);
  21993. if (ret == BAD_FUNC_ARG) {
  21994. ret = wc_ecc_cmp_point(point, NULL);
  21995. }
  21996. if (ret == BAD_FUNC_ARG) {
  21997. ret = 0;
  21998. } else if (ret == 0) {
  21999. ret = WOLFSSL_FATAL_ERROR;
  22000. }
  22001. }
  22002. printf(resultFmt, ret == 0 ? passed : failed);
  22003. printf(testingFmt, "wc_ecc_point_is_at_infinity()");
  22004. /* At infinity if return == 1, otherwise return == 0. */
  22005. if (ret == 0) {
  22006. ret = wc_ecc_point_is_at_infinity(point);
  22007. }
  22008. /* Test bad args. */
  22009. if (ret == 0) {
  22010. ret = wc_ecc_point_is_at_infinity(NULL);
  22011. if (ret == BAD_FUNC_ARG) {
  22012. ret = 0;
  22013. } else if (ret == 0) {
  22014. ret = WOLFSSL_FATAL_ERROR;
  22015. }
  22016. }
  22017. printf(resultFmt, ret == 0 ? passed : failed);
  22018. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  22019. #ifdef USE_ECC_B_PARAM
  22020. printf(testingFmt, "wc_ecc_point_is_on_curve()");
  22021. /* On curve if ret == 0 */
  22022. if (ret == 0) {
  22023. ret = wc_ecc_point_is_on_curve(point, idx);
  22024. }
  22025. /* Test bad args. */
  22026. if (ret == 0) {
  22027. ret = wc_ecc_point_is_on_curve(NULL, idx);
  22028. if (ret == BAD_FUNC_ARG) {
  22029. ret = wc_ecc_point_is_on_curve(point, 1000);
  22030. }
  22031. if (ret == ECC_BAD_ARG_E) {
  22032. ret = 0;
  22033. } else if (ret == 0) {
  22034. ret = WOLFSSL_FATAL_ERROR;
  22035. }
  22036. }
  22037. printf(resultFmt, ret == 0 ? passed : failed);
  22038. #endif /* USE_ECC_B_PARAM */
  22039. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  22040. /* Free */
  22041. wc_ecc_del_point(point);
  22042. wc_ecc_del_point(cpypt);
  22043. wc_ecc_free(&key);
  22044. if (wc_FreeRng(&rng) && ret == 0) {
  22045. ret = WOLFSSL_FATAL_ERROR;
  22046. }
  22047. #endif
  22048. return ret;
  22049. } /* END test_wc_ecc_pointFns */
  22050. /*
  22051. * Testing wc_ecc_sahred_secret_ssh()
  22052. */
  22053. static int test_wc_ecc_shared_secret_ssh (void)
  22054. {
  22055. int ret = 0;
  22056. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  22057. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22058. !defined(WOLFSSL_ATECC608A)
  22059. ecc_key key, key2;
  22060. WC_RNG rng;
  22061. int keySz = KEY32;
  22062. int key2Sz = KEY24;
  22063. byte secret[KEY32];
  22064. word32 secretLen = keySz;
  22065. /* Init stack variables. */
  22066. XMEMSET(secret, 0, secretLen);
  22067. XMEMSET(&rng, 0, sizeof(rng));
  22068. XMEMSET(&key, 0, sizeof(key));
  22069. XMEMSET(&key2, 0, sizeof(key2));
  22070. /* Make keys */
  22071. ret = wc_InitRng(&rng);
  22072. if (ret == 0) {
  22073. ret = wc_ecc_init(&key);
  22074. if (ret == 0) {
  22075. ret = wc_ecc_make_key(&rng, keySz, &key);
  22076. #if defined(WOLFSSL_ASYNC_CRYPT)
  22077. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22078. #endif
  22079. }
  22080. if (wc_FreeRng(&rng) && ret == 0) {
  22081. ret = WOLFSSL_FATAL_ERROR;
  22082. }
  22083. }
  22084. if (ret == 0) {
  22085. ret = wc_InitRng(&rng);
  22086. if (ret == 0) {
  22087. ret = wc_ecc_init(&key2);
  22088. }
  22089. if (ret == 0) {
  22090. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  22091. #if defined(WOLFSSL_ASYNC_CRYPT)
  22092. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  22093. #endif
  22094. }
  22095. }
  22096. printf(testingFmt, "ecc_shared_secret_ssh()");
  22097. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22098. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22099. !defined(HAVE_SELFTEST)
  22100. if (ret == 0) {
  22101. ret = wc_ecc_set_rng(&key, &rng);
  22102. }
  22103. #endif
  22104. if (ret == 0) {
  22105. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  22106. }
  22107. /* Pass in bad args. */
  22108. if (ret == 0) {
  22109. ret = wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret, &secretLen);
  22110. if (ret == BAD_FUNC_ARG) {
  22111. ret = wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen);
  22112. }
  22113. if (ret == BAD_FUNC_ARG) {
  22114. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen);
  22115. }
  22116. if (ret == BAD_FUNC_ARG) {
  22117. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL);
  22118. }
  22119. if (ret == BAD_FUNC_ARG) {
  22120. key.type = ECC_PUBLICKEY;
  22121. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  22122. if (ret == ECC_BAD_ARG_E) {
  22123. ret = 0;
  22124. } else if (ret == 0) {
  22125. ret = WOLFSSL_FATAL_ERROR;
  22126. }
  22127. } else if (ret == 0) {
  22128. ret = WOLFSSL_FATAL_ERROR;
  22129. }
  22130. }
  22131. printf(resultFmt, ret == 0 ? passed : failed);
  22132. if (wc_FreeRng(&rng) && ret == 0) {
  22133. ret = WOLFSSL_FATAL_ERROR;
  22134. }
  22135. wc_ecc_free(&key);
  22136. wc_ecc_free(&key2);
  22137. #ifdef FP_ECC
  22138. wc_ecc_fp_free();
  22139. #endif
  22140. #endif
  22141. return ret;
  22142. } /* END test_wc_ecc_shared_secret_ssh */
  22143. /*
  22144. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  22145. */
  22146. static int test_wc_ecc_verify_hash_ex (void)
  22147. {
  22148. int ret = 0;
  22149. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  22150. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22151. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
  22152. ecc_key key;
  22153. WC_RNG rng;
  22154. mp_int r;
  22155. mp_int s;
  22156. mp_int z;
  22157. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  22158. unsigned char iHash[] = "Everyone gets Friday off.......";
  22159. unsigned char shortHash[] = TEST_STRING;
  22160. word32 hashlen = sizeof(hash);
  22161. word32 iHashLen = sizeof(iHash);
  22162. word32 shortHashLen = sizeof(shortHash);
  22163. int keySz = KEY32;
  22164. int sig = WOLFSSL_FATAL_ERROR;
  22165. int ver = WOLFSSL_FATAL_ERROR;
  22166. int verify_ok = 0;
  22167. /* Initialize r and s. */
  22168. ret = mp_init_multi(&r, &s, &z, NULL, NULL, NULL);
  22169. if (ret != MP_OKAY) {
  22170. return MP_INIT_E;
  22171. }
  22172. ret = wc_InitRng(&rng);
  22173. if (ret == 0) {
  22174. ret = wc_ecc_init(&key);
  22175. if (ret == 0) {
  22176. ret = wc_ecc_make_key(&rng, keySz, &key);
  22177. #if defined(WOLFSSL_ASYNC_CRYPT)
  22178. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22179. #endif
  22180. }
  22181. }
  22182. if (ret == 0) {
  22183. ret = wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s);
  22184. if (ret == 0) {
  22185. /* verify_ok should be 1. */
  22186. ret = wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key);
  22187. if (verify_ok != 1 && ret == 0) {
  22188. ret = WOLFSSL_FATAL_ERROR;
  22189. }
  22190. }
  22191. if (ret == 0) {
  22192. /* verify_ok should be 0 */
  22193. ret = wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen,
  22194. &verify_ok, &key);
  22195. if (verify_ok != 0 && ret == 0) {
  22196. ret = WOLFSSL_FATAL_ERROR;
  22197. }
  22198. }
  22199. if (ret == 0) {
  22200. /* verify_ok should be 0. */
  22201. ret = wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22202. &verify_ok, &key);
  22203. if (verify_ok != 0 && ret == 0) {
  22204. ret = WOLFSSL_FATAL_ERROR;
  22205. }
  22206. }
  22207. }
  22208. printf(testingFmt, "wc_ecc_sign_hash_ex()");
  22209. /* Test bad args. */
  22210. if (ret == 0) {
  22211. if (wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s)
  22212. == ECC_BAD_ARG_E) {
  22213. sig = 0;
  22214. }
  22215. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s)
  22216. != ECC_BAD_ARG_E) {
  22217. sig = WOLFSSL_FATAL_ERROR;
  22218. }
  22219. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s)
  22220. != ECC_BAD_ARG_E) {
  22221. sig = WOLFSSL_FATAL_ERROR;
  22222. }
  22223. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s)
  22224. != ECC_BAD_ARG_E) {
  22225. sig = WOLFSSL_FATAL_ERROR;
  22226. }
  22227. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL)
  22228. != ECC_BAD_ARG_E) {
  22229. sig = WOLFSSL_FATAL_ERROR;
  22230. }
  22231. }
  22232. printf(resultFmt, sig == 0 ? passed : failed);
  22233. printf(testingFmt, "wc_ecc_verify_hash_ex()");
  22234. /* Test bad args. */
  22235. if (ret == 0) {
  22236. if (wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen, &verify_ok, &key)
  22237. == ECC_BAD_ARG_E) {
  22238. ver = 0;
  22239. }
  22240. if (ver == 0 && wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  22241. &verify_ok, &key) != ECC_BAD_ARG_E) {
  22242. ver = WOLFSSL_FATAL_ERROR;
  22243. }
  22244. if (wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen, &verify_ok, &key)
  22245. != MP_ZERO_E) {
  22246. ver = WOLFSSL_FATAL_ERROR;
  22247. }
  22248. if (wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen, &verify_ok, &key)
  22249. != MP_ZERO_E) {
  22250. ver = WOLFSSL_FATAL_ERROR;
  22251. }
  22252. if (wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen, &verify_ok, &key)
  22253. != MP_ZERO_E) {
  22254. ver = WOLFSSL_FATAL_ERROR;
  22255. }
  22256. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  22257. &key) != ECC_BAD_ARG_E) {
  22258. ver = WOLFSSL_FATAL_ERROR;
  22259. }
  22260. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22261. NULL, &key) != ECC_BAD_ARG_E) {
  22262. ver = WOLFSSL_FATAL_ERROR;
  22263. }
  22264. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22265. &verify_ok, NULL) != ECC_BAD_ARG_E) {
  22266. ver = WOLFSSL_FATAL_ERROR;
  22267. }
  22268. }
  22269. printf(resultFmt, ver == 0 ? passed : failed);
  22270. wc_ecc_free(&key);
  22271. mp_free(&r);
  22272. mp_free(&s);
  22273. if (wc_FreeRng(&rng)) {
  22274. return WOLFSSL_FATAL_ERROR;
  22275. }
  22276. if (ret == 0 && (sig != 0 || ver != 0)) {
  22277. ret = WOLFSSL_FATAL_ERROR;
  22278. }
  22279. #endif
  22280. return ret;
  22281. } /* END test_wc_ecc_verify_hash_ex */
  22282. /*
  22283. * Testing wc_ecc_mulmod()
  22284. */
  22285. static int test_wc_ecc_mulmod (void)
  22286. {
  22287. int ret = 0;
  22288. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  22289. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  22290. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  22291. ecc_key key1, key2, key3;
  22292. WC_RNG rng;
  22293. ret = wc_InitRng(&rng);
  22294. if (ret == 0) {
  22295. ret = wc_ecc_init(&key1);
  22296. if (ret == 0) {
  22297. ret = wc_ecc_init(&key2);
  22298. }
  22299. if (ret == 0) {
  22300. ret = wc_ecc_init(&key3);
  22301. }
  22302. if (ret == 0) {
  22303. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  22304. #if defined(WOLFSSL_ASYNC_CRYPT)
  22305. ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
  22306. #endif
  22307. }
  22308. wc_FreeRng(&rng);
  22309. }
  22310. if (ret == 0) {
  22311. ret = wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy, key1.dp->Af,
  22312. ECC_SECP256R1);
  22313. if (ret == 0) {
  22314. ret = wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  22315. key1.dp->prime, ECC_SECP256R1);
  22316. }
  22317. }
  22318. printf(testingFmt, "wc_ecc_mulmod()");
  22319. if (ret == 0) {
  22320. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey, &key2.k,
  22321. &key3.k, 1);
  22322. }
  22323. /* Test bad args. */
  22324. if (ret == 0) {
  22325. ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey, &key2.k,
  22326. &key3.k, 1);
  22327. if (ret == ECC_BAD_ARG_E) {
  22328. ret = wc_ecc_mulmod(&key1.k, NULL, &key3.pubkey, &key2.k,
  22329. &key3.k, 1);
  22330. }
  22331. if (ret == ECC_BAD_ARG_E) {
  22332. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, NULL, &key2.k,
  22333. &key3.k, 1);
  22334. }
  22335. if (ret == ECC_BAD_ARG_E) {
  22336. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey,
  22337. &key2.k, NULL, 1);
  22338. }
  22339. if (ret == ECC_BAD_ARG_E) {
  22340. ret = 0;
  22341. } else if (ret == 0) {
  22342. ret = WOLFSSL_FATAL_ERROR;
  22343. }
  22344. }
  22345. printf(resultFmt, ret == 0 ? passed : failed);
  22346. wc_ecc_free(&key1);
  22347. wc_ecc_free(&key2);
  22348. wc_ecc_free(&key3);
  22349. #ifdef FP_ECC
  22350. wc_ecc_fp_free();
  22351. #endif
  22352. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  22353. return ret;
  22354. } /* END test_wc_ecc_mulmod */
  22355. /*
  22356. * Testing wc_ecc_is_valid_idx()
  22357. */
  22358. static int test_wc_ecc_is_valid_idx (void)
  22359. {
  22360. int ret = 0;
  22361. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22362. ecc_key key;
  22363. WC_RNG rng;
  22364. int iVal = -2;
  22365. int iVal2 = 3000;
  22366. XMEMSET(&rng, 0, sizeof(rng));
  22367. XMEMSET(&key, 0, sizeof(key));
  22368. ret = wc_InitRng(&rng);
  22369. if (ret == 0) {
  22370. ret = wc_ecc_init(&key);
  22371. if (ret == 0) {
  22372. ret = wc_ecc_make_key(&rng, 32, &key);
  22373. #if defined(WOLFSSL_ASYNC_CRYPT)
  22374. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22375. #endif
  22376. }
  22377. }
  22378. printf(testingFmt, "wc_ecc_is_valid_idx()");
  22379. if (ret == 0) {
  22380. ret = wc_ecc_is_valid_idx(key.idx);
  22381. if (ret == 1) {
  22382. ret = 0;
  22383. } else {
  22384. ret = WOLFSSL_FATAL_ERROR;
  22385. }
  22386. }
  22387. /* Test bad args. */
  22388. if (ret == 0) {
  22389. ret = wc_ecc_is_valid_idx(iVal); /* should return 0 */
  22390. if (ret == 0) {
  22391. ret = wc_ecc_is_valid_idx(iVal2);
  22392. }
  22393. if (ret != 0) {
  22394. ret = WOLFSSL_FATAL_ERROR;
  22395. }
  22396. }
  22397. printf(resultFmt, ret == 0 ? passed : failed);
  22398. if (wc_FreeRng(&rng) && ret == 0) {
  22399. ret = WOLFSSL_FATAL_ERROR;
  22400. }
  22401. wc_ecc_free(&key);
  22402. #ifdef FP_ECC
  22403. wc_ecc_fp_free();
  22404. #endif
  22405. #endif
  22406. return ret;
  22407. } /* END test_wc_ecc_is_valid_idx */
  22408. /*
  22409. * Testing wc_ecc_get_curve_id_from_oid()
  22410. */
  22411. static int test_wc_ecc_get_curve_id_from_oid (void)
  22412. {
  22413. int ret = 0;
  22414. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  22415. !defined(HAVE_FIPS)
  22416. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  22417. word32 len = sizeof(oid);
  22418. printf(testingFmt, "wc_ecc_get_curve_id_from_oid()");
  22419. /* Bad Cases */
  22420. ret = wc_ecc_get_curve_id_from_oid(NULL, len);
  22421. if (ret == BAD_FUNC_ARG) {
  22422. ret = 0;
  22423. }
  22424. if (ret == 0) {
  22425. ret = wc_ecc_get_curve_id_from_oid(oid, 0);
  22426. if (ret == ECC_CURVE_INVALID) {
  22427. ret = 0;
  22428. }
  22429. }
  22430. /* Good Case */
  22431. if (ret == 0) {
  22432. ret = wc_ecc_get_curve_id_from_oid(oid, len);
  22433. if (ret == 7) {
  22434. ret = 0;
  22435. }
  22436. }
  22437. printf(resultFmt, ret == 0 ? passed : failed);
  22438. #endif
  22439. return ret;
  22440. }/* END test_wc_ecc_get_curve_id_from_oid */
  22441. /*
  22442. * Testing wc_ecc_sig_size_calc()
  22443. */
  22444. static int test_wc_ecc_sig_size_calc (void)
  22445. {
  22446. int ret = 0;
  22447. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  22448. ecc_key key;
  22449. WC_RNG rng;
  22450. int sz = 0;
  22451. printf(testingFmt, "wc_ecc_sig_size_calc()");
  22452. ret = wc_InitRng(&rng);
  22453. if (ret == 0) {
  22454. ret = wc_ecc_init(&key);
  22455. if (ret == 0) {
  22456. ret = wc_ecc_make_key(&rng, 16, &key);
  22457. #if defined(WOLFSSL_ASYNC_CRYPT)
  22458. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22459. #endif
  22460. }
  22461. sz = key.dp->size;
  22462. }
  22463. if (ret == 0) {
  22464. ret = wc_ecc_sig_size_calc(sz);
  22465. if (ret > 0) {
  22466. ret = 0;
  22467. }
  22468. }
  22469. printf(resultFmt, ret == 0 ? passed : failed);
  22470. wc_ecc_free(&key);
  22471. wc_FreeRng(&rng);
  22472. #endif
  22473. return ret;
  22474. } /* END test_wc_ecc_sig_size_calc */
  22475. /*
  22476. * Testing ToTraditional
  22477. */
  22478. static int test_ToTraditional (void)
  22479. {
  22480. int ret = 0;
  22481. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  22482. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  22483. defined(OPENSSL_EXTRA_X509_SMALL))
  22484. XFILE f;
  22485. byte input[TWOK_BUF];
  22486. word32 sz;
  22487. printf(testingFmt, "ToTraditional()");
  22488. f = XFOPEN("./certs/server-keyPkcs8.der", "rb");
  22489. AssertTrue((f != XBADFILE));
  22490. sz = (word32)XFREAD(input, 1, sizeof(input), f);
  22491. XFCLOSE(f);
  22492. /* Good case */
  22493. ret = ToTraditional(input, sz);
  22494. if (ret > 0) {
  22495. ret = 0;
  22496. }
  22497. /* Bad cases */
  22498. if (ret == 0) {
  22499. ret = ToTraditional(NULL, 0);
  22500. if (ret == BAD_FUNC_ARG) {
  22501. ret = 0;
  22502. }
  22503. }
  22504. if (ret == 0) {
  22505. ret = ToTraditional(NULL, sz);
  22506. if (ret == BAD_FUNC_ARG) {
  22507. ret = 0;
  22508. }
  22509. }
  22510. if (ret == 0) {
  22511. ret = ToTraditional(input, 0);
  22512. if (ret == ASN_PARSE_E || ret == BUFFER_E) {
  22513. ret = 0;
  22514. }
  22515. }
  22516. printf(resultFmt, ret == 0 ? passed : failed);
  22517. #endif
  22518. return ret;
  22519. }/* End test_ToTraditional*/
  22520. /*
  22521. * Testing wc_EccPrivateKeyToDer
  22522. */
  22523. static int test_wc_EccPrivateKeyToDer (void)
  22524. {
  22525. int ret = 0;
  22526. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22527. byte output[ONEK_BUF];
  22528. ecc_key eccKey;
  22529. WC_RNG rng;
  22530. word32 inLen;
  22531. printf(testingFmt, "wc_EccPrivateKeyToDer()");
  22532. ret = wc_InitRng(&rng);
  22533. if (ret == 0) {
  22534. ret = wc_ecc_init(&eccKey);
  22535. if (ret == 0) {
  22536. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  22537. #if defined(WOLFSSL_ASYNC_CRYPT)
  22538. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22539. #endif
  22540. }
  22541. inLen = (word32)sizeof(output);
  22542. /* Bad Cases */
  22543. if (ret == 0) {
  22544. ret = wc_EccPrivateKeyToDer(NULL, NULL, 0);
  22545. if (ret == BAD_FUNC_ARG) {
  22546. ret = 0;
  22547. }
  22548. }
  22549. if (ret == 0) {
  22550. ret = wc_EccPrivateKeyToDer(NULL, output, inLen);
  22551. if (ret == BAD_FUNC_ARG) {
  22552. ret = 0;
  22553. }
  22554. }
  22555. if (ret == 0) {
  22556. ret = wc_EccPrivateKeyToDer(&eccKey, NULL, inLen);
  22557. if (ret == LENGTH_ONLY_E) {
  22558. ret = 0;
  22559. }
  22560. }
  22561. if (ret == 0) {
  22562. ret = wc_EccPrivateKeyToDer(&eccKey, output, 0);
  22563. if (ret == BAD_FUNC_ARG) {
  22564. ret = 0;
  22565. }
  22566. }
  22567. /*Good Case */
  22568. if (ret == 0) {
  22569. ret = wc_EccPrivateKeyToDer(&eccKey, output, inLen);
  22570. if (ret > 0) {
  22571. ret = 0;
  22572. }
  22573. }
  22574. wc_ecc_free(&eccKey);
  22575. }
  22576. wc_FreeRng(&rng);
  22577. printf(resultFmt, ret == 0 ? passed : failed);
  22578. #endif
  22579. return ret;
  22580. }/* End test_wc_EccPrivateKeyToDer*/
  22581. /*
  22582. * Testing wc_DhPublicKeyDecode
  22583. */
  22584. static int test_wc_DhPublicKeyDecode(void)
  22585. {
  22586. int ret = 0;
  22587. #ifndef NO_DH
  22588. word32 inOutIdx;
  22589. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  22590. DhKey key;
  22591. AssertIntEQ(wc_InitDhKey(&key), 0);
  22592. printf(testingFmt, "wc_DhPublicKeyDecode()");
  22593. AssertIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0),
  22594. BAD_FUNC_ARG);
  22595. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  22596. BAD_FUNC_ARG);
  22597. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  22598. BAD_FUNC_ARG);
  22599. inOutIdx = 0;
  22600. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  22601. BAD_FUNC_ARG);
  22602. inOutIdx = 0;
  22603. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  22604. BAD_FUNC_ARG);
  22605. inOutIdx = 0;
  22606. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  22607. sizeof_dh_pub_key_der_2048), 0);
  22608. AssertTrue(key.p.used != 0 && key.g.used != 0 && key.q.used == 0 &&
  22609. key.pub.used != 0 && key.priv.used == 0);
  22610. wc_FreeDhKey(&key);
  22611. printf(resultFmt, passed);
  22612. #endif
  22613. (void)inOutIdx;
  22614. #endif /* !NO_DH */
  22615. return ret;
  22616. }
  22617. /*
  22618. * Testing wc_Ed25519KeyToDer
  22619. */
  22620. static int test_wc_Ed25519KeyToDer (void)
  22621. {
  22622. int ret = 0;
  22623. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  22624. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22625. byte output[ONEK_BUF];
  22626. ed25519_key ed25519Key;
  22627. WC_RNG rng;
  22628. word32 inLen;
  22629. printf(testingFmt, "wc_Ed25519KeyToDer()");
  22630. ret = wc_InitRng(&rng);
  22631. if (ret == 0) {
  22632. ret = wc_ed25519_init(&ed25519Key);
  22633. if (ret == 0) {
  22634. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  22635. }
  22636. inLen = (word32)sizeof(output);
  22637. /* Bad Cases */
  22638. if (ret == 0) {
  22639. ret = wc_Ed25519KeyToDer(NULL, NULL, 0);
  22640. if (ret == BAD_FUNC_ARG) {
  22641. ret = 0;
  22642. }
  22643. }
  22644. if (ret == 0) {
  22645. ret = wc_Ed25519KeyToDer(NULL, output, inLen);
  22646. if (ret == BAD_FUNC_ARG) {
  22647. ret = 0;
  22648. }
  22649. }
  22650. if (ret == 0) {
  22651. ret = wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen);
  22652. if (ret == BAD_FUNC_ARG) {
  22653. ret = 0;
  22654. }
  22655. }
  22656. if (ret == 0) {
  22657. ret = wc_Ed25519KeyToDer(&ed25519Key, output, 0);
  22658. if (ret == BAD_FUNC_ARG) {
  22659. ret = 0;
  22660. }
  22661. }
  22662. /* Good Case */
  22663. if (ret == 0) {
  22664. ret = wc_Ed25519KeyToDer(&ed25519Key, output, inLen);
  22665. if (ret > 0) {
  22666. ret = 0;
  22667. }
  22668. }
  22669. wc_ed25519_free(&ed25519Key);
  22670. }
  22671. wc_FreeRng(&rng);
  22672. printf(resultFmt, ret == 0 ? passed : failed);
  22673. #endif
  22674. return ret;
  22675. }/* End test_wc_Ed25519KeyToDer*/
  22676. /*
  22677. * Testing wc_Ed25519PrivateKeyToDer
  22678. */
  22679. static int test_wc_Ed25519PrivateKeyToDer (void)
  22680. {
  22681. int ret = 0;
  22682. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  22683. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22684. byte output[ONEK_BUF];
  22685. ed25519_key ed25519PrivKey;
  22686. WC_RNG rng;
  22687. word32 inLen;
  22688. printf(testingFmt, "wc_Ed25519PrivateKeyToDer()");
  22689. ret = wc_InitRng(&rng);
  22690. if (ret == 0) {
  22691. ret = wc_ed25519_init(&ed25519PrivKey);
  22692. if (ret == 0) {
  22693. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey);
  22694. }
  22695. inLen = (word32)sizeof(output);
  22696. /* Bad Cases */
  22697. if (ret == 0) {
  22698. ret = wc_Ed25519PrivateKeyToDer(NULL, NULL, 0);
  22699. if (ret == BAD_FUNC_ARG) {
  22700. ret = 0;
  22701. }
  22702. }
  22703. if (ret == 0) {
  22704. ret = wc_Ed25519PrivateKeyToDer(NULL, output, inLen);
  22705. if (ret == BAD_FUNC_ARG) {
  22706. ret = 0;
  22707. }
  22708. }
  22709. if (ret == 0) {
  22710. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen);
  22711. if (ret == BAD_FUNC_ARG) {
  22712. ret = 0;
  22713. }
  22714. }
  22715. if (ret == 0) {
  22716. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0);
  22717. if (ret == BAD_FUNC_ARG) {
  22718. ret = 0;
  22719. }
  22720. }
  22721. /* Good Case */
  22722. if (ret == 0) {
  22723. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen);
  22724. if (ret > 0) {
  22725. ret = 0;
  22726. }
  22727. }
  22728. wc_ed25519_free(&ed25519PrivKey);
  22729. }
  22730. wc_FreeRng(&rng);
  22731. printf(resultFmt, ret == 0 ? passed : failed);
  22732. #endif
  22733. return ret;
  22734. }/* End test_wc_Ed25519PrivateKeyToDer*/
  22735. /*
  22736. * Testing wc_Ed448KeyToDer
  22737. */
  22738. static int test_wc_Ed448KeyToDer (void)
  22739. {
  22740. int ret = 0;
  22741. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  22742. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22743. byte output[ONEK_BUF];
  22744. ed448_key ed448Key;
  22745. WC_RNG rng;
  22746. word32 inLen;
  22747. printf(testingFmt, "wc_Ed448KeyToDer()");
  22748. ret = wc_InitRng(&rng);
  22749. if (ret == 0) {
  22750. ret = wc_ed448_init(&ed448Key);
  22751. if (ret == 0) {
  22752. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  22753. }
  22754. inLen = sizeof(output);
  22755. /* Bad Cases */
  22756. if (ret == 0) {
  22757. ret = wc_Ed448KeyToDer(NULL, NULL, 0);
  22758. if (ret == BAD_FUNC_ARG) {
  22759. ret = 0;
  22760. }
  22761. }
  22762. if (ret == 0) {
  22763. ret = wc_Ed448KeyToDer(NULL, output, inLen);
  22764. if (ret == BAD_FUNC_ARG) {
  22765. ret = 0;
  22766. }
  22767. }
  22768. if (ret == 0) {
  22769. ret = wc_Ed448KeyToDer(&ed448Key, NULL, inLen);
  22770. if (ret == BAD_FUNC_ARG) {
  22771. ret = 0;
  22772. }
  22773. }
  22774. if (ret == 0) {
  22775. ret = wc_Ed448KeyToDer(&ed448Key, output, 0);
  22776. if (ret == BAD_FUNC_ARG) {
  22777. ret = 0;
  22778. }
  22779. }
  22780. /* Good Case */
  22781. if (ret == 0) {
  22782. ret = wc_Ed448KeyToDer(&ed448Key, output, inLen);
  22783. if (ret > 0) {
  22784. ret = 0;
  22785. }
  22786. }
  22787. wc_ed448_free(&ed448Key);
  22788. }
  22789. wc_FreeRng(&rng);
  22790. printf(resultFmt, ret == 0 ? passed : failed);
  22791. #endif
  22792. return ret;
  22793. }/* End test_wc_Ed448KeyToDer*/
  22794. /*
  22795. * Testing wc_Ed448PrivateKeyToDer
  22796. */
  22797. static int test_wc_Ed448PrivateKeyToDer (void)
  22798. {
  22799. int ret = 0;
  22800. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  22801. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22802. byte output[ONEK_BUF];
  22803. ed448_key ed448PrivKey;
  22804. WC_RNG rng;
  22805. word32 inLen;
  22806. printf(testingFmt, "wc_Ed448PrivateKeyToDer()");
  22807. ret = wc_InitRng(&rng);
  22808. if (ret == 0) {
  22809. ret = wc_ed448_init(&ed448PrivKey);
  22810. if (ret == 0) {
  22811. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey);
  22812. }
  22813. inLen = sizeof(output);
  22814. /* Bad Cases */
  22815. if (ret == 0) {
  22816. ret = wc_Ed448PrivateKeyToDer(NULL, NULL, 0);
  22817. if (ret == BAD_FUNC_ARG) {
  22818. ret = 0;
  22819. }
  22820. }
  22821. if (ret == 0) {
  22822. ret = wc_Ed448PrivateKeyToDer(NULL, output, inLen);
  22823. if (ret == BAD_FUNC_ARG) {
  22824. ret = 0;
  22825. }
  22826. }
  22827. if (ret == 0) {
  22828. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen);
  22829. if (ret == BAD_FUNC_ARG) {
  22830. ret = 0;
  22831. }
  22832. }
  22833. if (ret == 0) {
  22834. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0);
  22835. if (ret == BAD_FUNC_ARG) {
  22836. ret = 0;
  22837. }
  22838. }
  22839. /* Good case */
  22840. if (ret == 0) {
  22841. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen);
  22842. if (ret > 0) {
  22843. ret = 0;
  22844. }
  22845. }
  22846. wc_ed448_free(&ed448PrivKey);
  22847. }
  22848. wc_FreeRng(&rng);
  22849. printf(resultFmt, ret == 0 ? passed : failed);
  22850. #endif
  22851. return ret;
  22852. }/* End test_wc_Ed448PrivateKeyToDer*/
  22853. /*
  22854. * Testing wc_SetSubjectBuffer
  22855. */
  22856. static int test_wc_SetSubjectBuffer (void)
  22857. {
  22858. int ret = 0;
  22859. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  22860. Cert cert;
  22861. FILE* file;
  22862. byte* der;
  22863. word32 derSz;
  22864. printf(testingFmt, "wc_SetSubjectBuffer()");
  22865. derSz = FOURK_BUF;
  22866. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22867. if (der == NULL) {
  22868. ret = -1;
  22869. }
  22870. if (ret == 0) {
  22871. file = XFOPEN("./certs/ca-cert.der", "rb");
  22872. if (file != NULL) {
  22873. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  22874. XFCLOSE(file);
  22875. }
  22876. else {
  22877. ret = -1;
  22878. }
  22879. }
  22880. if (ret == 0) {
  22881. ret = wc_InitCert(&cert);
  22882. }
  22883. if (ret == 0) {
  22884. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  22885. }
  22886. if (ret == 0) {
  22887. ret = wc_SetSubjectBuffer(NULL, der, derSz);
  22888. if (ret == BAD_FUNC_ARG) {
  22889. ret = 0;
  22890. }
  22891. }
  22892. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22893. printf(resultFmt, ret == 0 ? passed : failed);
  22894. #endif
  22895. return ret;
  22896. }/* End test_wc_SetSubjectBuffer*/
  22897. /*
  22898. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  22899. */
  22900. static int test_wc_SetSubjectKeyIdFromPublicKey_ex (void)
  22901. {
  22902. int ret = 0;
  22903. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  22904. WC_RNG rng;
  22905. Cert cert;
  22906. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  22907. ed25519_key ed25519Key;
  22908. #endif
  22909. #if !defined(NO_RSA) && defined(HAVE_RSA)
  22910. RsaKey rsaKey;
  22911. int bits = 2048;
  22912. #endif
  22913. #if defined(HAVE_ECC)
  22914. ecc_key eccKey;
  22915. #endif
  22916. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  22917. ed448_key ed448Key;
  22918. #endif
  22919. printf(testingFmt, "wc_SetSubjectKeyIdFromPublicKey_ex()");
  22920. #ifndef HAVE_FIPS
  22921. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  22922. #else
  22923. ret = wc_InitRng(&rng);
  22924. #endif
  22925. wc_InitCert(&cert);
  22926. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  22927. if (ret == 0) { /*ED25519*/
  22928. ret = wc_ed25519_init(&ed25519Key);
  22929. if (ret == 0) {
  22930. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  22931. }
  22932. if (ret == 0) {
  22933. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  22934. &ed25519Key);
  22935. }
  22936. wc_ed25519_free(&ed25519Key);
  22937. }
  22938. #endif
  22939. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  22940. if (ret == 0) { /*RSA*/
  22941. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  22942. if (ret == 0) {
  22943. MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
  22944. }
  22945. if (ret == 0) {
  22946. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
  22947. }
  22948. wc_FreeRsaKey(&rsaKey);
  22949. }
  22950. #endif
  22951. #if defined(HAVE_ECC)
  22952. if (ret == 0) { /*ECC*/
  22953. ret = wc_ecc_init(&eccKey);
  22954. if (ret == 0) {
  22955. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  22956. #if defined(WOLFSSL_ASYNC_CRYPT)
  22957. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22958. #endif
  22959. }
  22960. if (ret == 0) {
  22961. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
  22962. }
  22963. wc_ecc_free(&eccKey);
  22964. }
  22965. #endif
  22966. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  22967. if (ret == 0) { /*ED448*/
  22968. ret = wc_ed448_init(&ed448Key);
  22969. if (ret == 0) {
  22970. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  22971. }
  22972. if (ret == 0) {
  22973. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  22974. &ed448Key);
  22975. }
  22976. wc_ed448_free(&ed448Key);
  22977. }
  22978. #endif
  22979. printf(resultFmt, ret == 0 ? passed : failed);
  22980. wc_FreeRng(&rng);
  22981. #endif
  22982. return ret;
  22983. }/* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  22984. /*
  22985. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  22986. */
  22987. static int test_wc_SetAuthKeyIdFromPublicKey_ex (void)
  22988. {
  22989. int ret = 0;
  22990. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  22991. WC_RNG rng;
  22992. Cert cert;
  22993. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  22994. ed25519_key ed25519Key;
  22995. #endif
  22996. #if !defined(NO_RSA) && defined(HAVE_RSA)
  22997. RsaKey rsaKey;
  22998. int bits = 2048;
  22999. #endif
  23000. #if defined(HAVE_ECC)
  23001. ecc_key eccKey;
  23002. #endif
  23003. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23004. ed448_key ed448Key;
  23005. #endif
  23006. printf(testingFmt, "wc_SetAuthKeyIdFromPublicKey_ex()");
  23007. #ifndef HAVE_FIPS
  23008. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  23009. #else
  23010. ret = wc_InitRng(&rng);
  23011. #endif
  23012. wc_InitCert(&cert);
  23013. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23014. if (ret == 0) { /*ED25519*/
  23015. ret = wc_ed25519_init(&ed25519Key);
  23016. if (ret == 0) {
  23017. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  23018. }
  23019. if (ret == 0) {
  23020. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  23021. &ed25519Key);
  23022. }
  23023. wc_ed25519_free(&ed25519Key);
  23024. }
  23025. #endif
  23026. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  23027. if (ret == 0) { /*RSA*/
  23028. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  23029. if (ret == 0) {
  23030. MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
  23031. }
  23032. if (ret == 0) {
  23033. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
  23034. }
  23035. wc_FreeRsaKey(&rsaKey);
  23036. }
  23037. #endif
  23038. #if defined(HAVE_ECC)
  23039. if (ret == 0) { /*ECC*/
  23040. ret = wc_ecc_init(&eccKey);
  23041. if (ret == 0) {
  23042. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23043. #if defined(WOLFSSL_ASYNC_CRYPT)
  23044. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23045. #endif
  23046. }
  23047. if (ret == 0) {
  23048. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
  23049. }
  23050. wc_ecc_free(&eccKey);
  23051. }
  23052. #endif
  23053. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23054. if (ret == 0) { /*ED448*/
  23055. ret = wc_ed448_init(&ed448Key);
  23056. if (ret == 0) {
  23057. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  23058. }
  23059. if (ret == 0) {
  23060. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  23061. &ed448Key);
  23062. }
  23063. wc_ed448_free(&ed448Key);
  23064. }
  23065. #endif
  23066. printf(resultFmt, ret == 0 ? passed : failed);
  23067. wc_FreeRng(&rng);
  23068. #endif /*defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  23069. return ret;
  23070. }/* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  23071. /*
  23072. * Testing wc_PKCS7_New()
  23073. */
  23074. static void test_wc_PKCS7_New (void)
  23075. {
  23076. #if defined(HAVE_PKCS7)
  23077. PKCS7* pkcs7;
  23078. void* heap = NULL;
  23079. printf(testingFmt, "wc_PKCS7_New()");
  23080. pkcs7 = wc_PKCS7_New(heap, devId);
  23081. AssertNotNull(pkcs7);
  23082. printf(resultFmt, passed);
  23083. wc_PKCS7_Free(pkcs7);
  23084. #endif
  23085. } /* END test-wc_PKCS7_New */
  23086. /*
  23087. * Testing wc_PKCS7_Init()
  23088. */
  23089. static void test_wc_PKCS7_Init (void)
  23090. {
  23091. #if defined(HAVE_PKCS7)
  23092. PKCS7* pkcs7;
  23093. void* heap = NULL;
  23094. printf(testingFmt, "wc_PKCS7_Init()");
  23095. pkcs7 = wc_PKCS7_New(heap, devId);
  23096. AssertNotNull(pkcs7);
  23097. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, devId), 0);
  23098. /* Pass in bad args. */
  23099. AssertIntEQ(wc_PKCS7_Init(NULL, heap, devId), BAD_FUNC_ARG);
  23100. printf(resultFmt, passed);
  23101. wc_PKCS7_Free(pkcs7);
  23102. #endif
  23103. } /* END test-wc_PKCS7_Init */
  23104. /*
  23105. * Testing wc_PKCS7_InitWithCert()
  23106. */
  23107. static void test_wc_PKCS7_InitWithCert (void)
  23108. {
  23109. #if defined(HAVE_PKCS7)
  23110. PKCS7* pkcs7;
  23111. #ifndef NO_RSA
  23112. #if defined(USE_CERT_BUFFERS_2048)
  23113. unsigned char cert[sizeof(client_cert_der_2048)];
  23114. int certSz = (int)sizeof(cert);
  23115. XMEMSET(cert, 0, certSz);
  23116. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  23117. #elif defined(USE_CERT_BUFFERS_1024)
  23118. unsigned char cert[sizeof(client_cert_der_1024)];
  23119. int certSz = (int)sizeof(cert);
  23120. XMEMSET(cert, 0, certSz);
  23121. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  23122. #else
  23123. unsigned char cert[ONEK_BUF];
  23124. XFILE fp;
  23125. int certSz;
  23126. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  23127. AssertTrue(fp != XBADFILE);
  23128. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  23129. XFCLOSE(fp);
  23130. #endif
  23131. #elif defined(HAVE_ECC)
  23132. #if defined(USE_CERT_BUFFERS_256)
  23133. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23134. int certSz = (int)sizeof(cert);
  23135. XMEMSET(cert, 0, certSz);
  23136. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  23137. #else
  23138. unsigned char cert[ONEK_BUF];
  23139. XFILE fp;
  23140. int certSz;
  23141. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  23142. AssertTrue(fp != XBADFILE);
  23143. certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256), fp);
  23144. XFCLOSE(fp);
  23145. #endif
  23146. #else
  23147. #error PKCS7 requires ECC or RSA
  23148. #endif
  23149. #ifdef HAVE_ECC
  23150. {
  23151. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  23152. static unsigned char certWithInvalidEccKey[] = {
  23153. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  23154. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  23155. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  23156. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  23157. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  23158. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  23159. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  23160. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  23161. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  23162. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  23163. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  23164. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  23165. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23166. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23167. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23168. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  23169. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  23170. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  23171. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  23172. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  23173. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  23174. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  23175. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  23176. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  23177. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  23178. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  23179. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  23180. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  23181. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  23182. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  23183. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  23184. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  23185. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  23186. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  23187. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  23188. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  23189. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  23190. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  23191. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  23192. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  23193. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  23194. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  23195. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  23196. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  23197. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  23198. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  23199. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  23200. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  23201. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  23202. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  23203. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  23204. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  23205. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  23206. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23207. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23208. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23209. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  23210. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  23211. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  23212. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  23213. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  23214. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  23215. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  23216. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  23217. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  23218. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  23219. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  23220. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  23221. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  23222. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  23223. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  23224. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  23225. 0x08, 0xA8, 0xB4};
  23226. #endif
  23227. printf(testingFmt, "wc_PKCS7_InitWithCert()");
  23228. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23229. /* If initialization is not successful, it's free'd in init func. */
  23230. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  23231. wc_PKCS7_Free(pkcs7);
  23232. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23233. /* Valid initialization usage. */
  23234. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23235. /* Pass in bad args. No need free for null checks, free at end.*/
  23236. AssertIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  23237. BAD_FUNC_ARG);
  23238. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  23239. BAD_FUNC_ARG);
  23240. #ifdef HAVE_ECC
  23241. AssertIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  23242. sizeof(certWithInvalidEccKey)), 0);
  23243. #endif
  23244. }
  23245. printf(resultFmt, passed);
  23246. wc_PKCS7_Free(pkcs7);
  23247. #endif
  23248. } /* END test_wc_PKCS7_InitWithCert */
  23249. /*
  23250. * Testing wc_PKCS7_EncodeData()
  23251. */
  23252. static void test_wc_PKCS7_EncodeData (void)
  23253. {
  23254. #if defined(HAVE_PKCS7)
  23255. PKCS7* pkcs7;
  23256. byte output[FOURK_BUF];
  23257. byte data[] = "My encoded DER cert.";
  23258. #ifndef NO_RSA
  23259. #if defined(USE_CERT_BUFFERS_2048)
  23260. unsigned char cert[sizeof(client_cert_der_2048)];
  23261. unsigned char key[sizeof(client_key_der_2048)];
  23262. int certSz = (int)sizeof(cert);
  23263. int keySz = (int)sizeof(key);
  23264. XMEMSET(cert, 0, certSz);
  23265. XMEMSET(key, 0, keySz);
  23266. XMEMCPY(cert, client_cert_der_2048, certSz);
  23267. XMEMCPY(key, client_key_der_2048, keySz);
  23268. #elif defined(USE_CERT_BUFFERS_1024)
  23269. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  23270. unsigned char key[sizeof_client_key_der_1024];
  23271. int certSz = (int)sizeof(cert);
  23272. int keySz = (int)sizeof(key);
  23273. XMEMSET(cert, 0, certSz);
  23274. XMEMSET(key, 0, keySz);
  23275. XMEMCPY(cert, client_cert_der_1024, certSz);
  23276. XMEMCPY(key, client_key_der_1024, keySz);
  23277. #else
  23278. unsigned char cert[ONEK_BUF];
  23279. unsigned char key[ONEK_BUF];
  23280. XFILE fp;
  23281. int certSz;
  23282. int keySz;
  23283. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  23284. AssertTrue(fp != XBADFILE);
  23285. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  23286. XFCLOSE(fp);
  23287. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  23288. AssertTrue(fp != XBADFILE);
  23289. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  23290. XFCLOSE(fp);
  23291. #endif
  23292. #elif defined(HAVE_ECC)
  23293. #if defined(USE_CERT_BUFFERS_256)
  23294. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23295. unsigned char key[sizeof(ecc_clikey_der_256)];
  23296. int certSz = (int)sizeof(cert);
  23297. int keySz = (int)sizeof(key);
  23298. XMEMSET(cert, 0, certSz);
  23299. XMEMSET(key, 0, keySz);
  23300. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  23301. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  23302. #else
  23303. unsigned char cert[ONEK_BUF];
  23304. unsigned char key[ONEK_BUF];
  23305. XFILE fp;
  23306. int certSz, keySz;
  23307. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  23308. AssertTrue(fp != XBADFILE);
  23309. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  23310. XFCLOSE(fp);
  23311. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  23312. AssertTrue(fp != XBADFILE);
  23313. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  23314. XFCLOSE(fp);
  23315. #endif
  23316. #endif
  23317. XMEMSET(output, 0, sizeof(output));
  23318. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23319. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23320. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, certSz), 0);
  23321. printf(testingFmt, "wc_PKCS7_EncodeData()");
  23322. pkcs7->content = data;
  23323. pkcs7->contentSz = sizeof(data);
  23324. pkcs7->privateKey = key;
  23325. pkcs7->privateKeySz = keySz;
  23326. AssertIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  23327. /* Test bad args. */
  23328. AssertIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  23329. BAD_FUNC_ARG);
  23330. AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  23331. BAD_FUNC_ARG);
  23332. AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  23333. printf(resultFmt, passed);
  23334. wc_PKCS7_Free(pkcs7);
  23335. #endif
  23336. } /* END test_wc_PKCS7_EncodeData */
  23337. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  23338. !defined(NO_RSA) && !defined(NO_SHA256)
  23339. /* RSA sign raw digest callback */
  23340. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  23341. byte* out, word32 outSz, byte* privateKey,
  23342. word32 privateKeySz, int devid, int hashOID)
  23343. {
  23344. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  23345. byte digInfoEncoding[] = {
  23346. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  23347. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  23348. 0x00, 0x04, 0x20
  23349. };
  23350. int ret;
  23351. byte digestInfo[ONEK_BUF];
  23352. byte sig[FOURK_BUF];
  23353. word32 digestInfoSz = 0;
  23354. word32 idx = 0;
  23355. RsaKey rsa;
  23356. /* SHA-256 required only for this example callback due to above
  23357. * digInfoEncoding[] */
  23358. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  23359. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  23360. (hashOID != SHA256h)) {
  23361. return -1;
  23362. }
  23363. /* build DigestInfo */
  23364. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  23365. digestInfoSz += sizeof(digInfoEncoding);
  23366. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  23367. digestInfoSz += digestSz;
  23368. /* set up RSA key */
  23369. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  23370. if (ret != 0) {
  23371. return ret;
  23372. }
  23373. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  23374. /* sign DigestInfo */
  23375. if (ret == 0) {
  23376. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  23377. &rsa, pkcs7->rng);
  23378. if (ret > 0) {
  23379. if (ret > (int)outSz) {
  23380. /* output buffer too small */
  23381. ret = -1;
  23382. } else {
  23383. /* success, ret holds sig size */
  23384. XMEMCPY(out, sig, ret);
  23385. }
  23386. }
  23387. }
  23388. wc_FreeRsaKey(&rsa);
  23389. return ret;
  23390. }
  23391. #endif
  23392. /*
  23393. * Testing wc_PKCS7_EncodeSignedData()
  23394. */
  23395. static void test_wc_PKCS7_EncodeSignedData(void)
  23396. {
  23397. #if defined(HAVE_PKCS7)
  23398. PKCS7* pkcs7;
  23399. WC_RNG rng;
  23400. byte output[FOURK_BUF];
  23401. byte badOut[1];
  23402. word32 outputSz = (word32)sizeof(output);
  23403. word32 badOutSz = 0;
  23404. byte data[] = "Test data to encode.";
  23405. #ifndef NO_RSA
  23406. #if defined(USE_CERT_BUFFERS_2048)
  23407. byte key[sizeof(client_key_der_2048)];
  23408. byte cert[sizeof(client_cert_der_2048)];
  23409. word32 keySz = (word32)sizeof(key);
  23410. word32 certSz = (word32)sizeof(cert);
  23411. XMEMSET(key, 0, keySz);
  23412. XMEMSET(cert, 0, certSz);
  23413. XMEMCPY(key, client_key_der_2048, keySz);
  23414. XMEMCPY(cert, client_cert_der_2048, certSz);
  23415. #elif defined(USE_CERT_BUFFERS_1024)
  23416. byte key[sizeof_client_key_der_1024];
  23417. byte cert[sizeof(sizeof_client_cert_der_1024)];
  23418. word32 keySz = (word32)sizeof(key);
  23419. word32 certSz = (word32)sizeof(cert);
  23420. XMEMSET(key, 0, keySz);
  23421. XMEMSET(cert, 0, certSz);
  23422. XMEMCPY(key, client_key_der_1024, keySz);
  23423. XMEMCPY(cert, client_cert_der_1024, certSz);
  23424. #else
  23425. unsigned char cert[ONEK_BUF];
  23426. unsigned char key[ONEK_BUF];
  23427. XFILE fp;
  23428. int certSz;
  23429. int keySz;
  23430. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  23431. AssertTrue(fp != XBADFILE);
  23432. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  23433. XFCLOSE(fp);
  23434. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  23435. AssertTrue(fp != XBADFILE);
  23436. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  23437. XFCLOSE(fp);
  23438. #endif
  23439. #elif defined(HAVE_ECC)
  23440. #if defined(USE_CERT_BUFFERS_256)
  23441. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23442. unsigned char key[sizeof(ecc_clikey_der_256)];
  23443. int certSz = (int)sizeof(cert);
  23444. int keySz = (int)sizeof(key);
  23445. XMEMSET(cert, 0, certSz);
  23446. XMEMSET(key, 0, keySz);
  23447. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  23448. XMEMCPY(key, ecc_clikey_der_256, keySz);
  23449. #else
  23450. unsigned char cert[ONEK_BUF];
  23451. unsigned char key[ONEK_BUF];
  23452. XFILE fp;
  23453. int certSz, keySz;
  23454. fp = XOPEN("./certs/client-ecc-cert.der", "rb");
  23455. AssertTrue(fp != XBADFILE);
  23456. certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp);
  23457. XFCLOSE(fp);
  23458. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  23459. AssertTrue(fp != XBADFILE);
  23460. keySz = (int)XFREAD(key, 1, ONEK_BUF, fp);
  23461. XFCLOSE(fp);
  23462. #endif
  23463. #endif
  23464. XMEMSET(output, 0, outputSz);
  23465. AssertIntEQ(wc_InitRng(&rng), 0);
  23466. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23467. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23468. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23469. printf(testingFmt, "wc_PKCS7_EncodeSignedData()");
  23470. pkcs7->content = data;
  23471. pkcs7->contentSz = (word32)sizeof(data);
  23472. pkcs7->privateKey = key;
  23473. pkcs7->privateKeySz = (word32)sizeof(key);
  23474. pkcs7->encryptOID = RSAk;
  23475. pkcs7->hashOID = SHAh;
  23476. pkcs7->rng = &rng;
  23477. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  23478. wc_PKCS7_Free(pkcs7);
  23479. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23480. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23481. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23482. /* Pass in bad args. */
  23483. AssertIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz), BAD_FUNC_ARG);
  23484. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  23485. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  23486. badOutSz), BAD_FUNC_ARG);
  23487. pkcs7->hashOID = 0; /* bad hashOID */
  23488. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), BAD_FUNC_ARG);
  23489. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  23490. !defined(NO_RSA) && !defined(NO_SHA256)
  23491. /* test RSA sign raw digest callback, if using RSA and compiled in.
  23492. * Example callback assumes SHA-256, so only run test if compiled in. */
  23493. wc_PKCS7_Free(pkcs7);
  23494. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23495. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23496. pkcs7->content = data;
  23497. pkcs7->contentSz = (word32)sizeof(data);
  23498. pkcs7->privateKey = key;
  23499. pkcs7->privateKeySz = (word32)sizeof(key);
  23500. pkcs7->encryptOID = RSAk;
  23501. pkcs7->hashOID = SHA256h;
  23502. pkcs7->rng = &rng;
  23503. AssertIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  23504. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  23505. #endif
  23506. printf(resultFmt, passed);
  23507. wc_PKCS7_Free(pkcs7);
  23508. wc_FreeRng(&rng);
  23509. #endif
  23510. } /* END test_wc_PKCS7_EncodeSignedData */
  23511. /*
  23512. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  23513. */
  23514. static void test_wc_PKCS7_EncodeSignedData_ex(void)
  23515. {
  23516. #if defined(HAVE_PKCS7)
  23517. int ret, i;
  23518. PKCS7* pkcs7;
  23519. WC_RNG rng;
  23520. byte outputHead[FOURK_BUF/2];
  23521. byte outputFoot[FOURK_BUF/2];
  23522. word32 outputHeadSz = (word32)sizeof(outputHead);
  23523. word32 outputFootSz = (word32)sizeof(outputFoot);
  23524. byte data[FOURK_BUF];
  23525. wc_HashAlg hash;
  23526. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  23527. byte hashBuf[WC_MAX_DIGEST_SIZE];
  23528. word32 hashSz = wc_HashGetDigestSize(hashType);
  23529. #ifndef NO_RSA
  23530. #if defined(USE_CERT_BUFFERS_2048)
  23531. byte key[sizeof(client_key_der_2048)];
  23532. byte cert[sizeof(client_cert_der_2048)];
  23533. word32 keySz = (word32)sizeof(key);
  23534. word32 certSz = (word32)sizeof(cert);
  23535. XMEMSET(key, 0, keySz);
  23536. XMEMSET(cert, 0, certSz);
  23537. XMEMCPY(key, client_key_der_2048, keySz);
  23538. XMEMCPY(cert, client_cert_der_2048, certSz);
  23539. #elif defined(USE_CERT_BUFFERS_1024)
  23540. byte key[sizeof_client_key_der_1024];
  23541. byte cert[sizeof(sizeof_client_cert_der_1024)];
  23542. word32 keySz = (word32)sizeof(key);
  23543. word32 certSz = (word32)sizeof(cert);
  23544. XMEMSET(key, 0, keySz);
  23545. XMEMSET(cert, 0, certSz);
  23546. XMEMCPY(key, client_key_der_1024, keySz);
  23547. XMEMCPY(cert, client_cert_der_1024, certSz);
  23548. #else
  23549. unsigned char cert[ONEK_BUF];
  23550. unsigned char key[ONEK_BUF];
  23551. XFILE fp;
  23552. int certSz;
  23553. int keySz;
  23554. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  23555. AssertTrue((fp != XBADFILE));
  23556. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  23557. XFCLOSE(fp);
  23558. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  23559. AssertTrue(fp != XBADFILE);
  23560. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  23561. XFCLOSE(fp);
  23562. #endif
  23563. #elif defined(HAVE_ECC)
  23564. #if defined(USE_CERT_BUFFERS_256)
  23565. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23566. unsigned char key[sizeof(ecc_clikey_der_256)];
  23567. int certSz = (int)sizeof(cert);
  23568. int keySz = (int)sizeof(key);
  23569. XMEMSET(cert, 0, certSz);
  23570. XMEMSET(key, 0, keySz);
  23571. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  23572. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  23573. #else
  23574. unsigned char cert[ONEK_BUF];
  23575. unsigned char key[ONEK_BUF];
  23576. XFILE fp;
  23577. int certSz, keySz;
  23578. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  23579. AssertTrue(fp != XBADFILE);
  23580. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  23581. XFCLOSE(fp);
  23582. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  23583. AssertTrue(fp != XBADFILE);
  23584. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  23585. XFCLOSE(fp);
  23586. #endif
  23587. #endif
  23588. /* initialize large data with sequence */
  23589. for (i=0; i<(int)sizeof(data); i++)
  23590. data[i] = i & 0xff;
  23591. XMEMSET(outputHead, 0, outputHeadSz);
  23592. XMEMSET(outputFoot, 0, outputFootSz);
  23593. AssertIntEQ(wc_InitRng(&rng), 0);
  23594. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23595. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23596. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23597. printf(testingFmt, "wc_PKCS7_EncodeSignedData()");
  23598. pkcs7->content = NULL; /* not used for ex */
  23599. pkcs7->contentSz = (word32)sizeof(data);
  23600. pkcs7->privateKey = key;
  23601. pkcs7->privateKeySz = (word32)sizeof(key);
  23602. pkcs7->encryptOID = RSAk;
  23603. pkcs7->hashOID = SHAh;
  23604. pkcs7->rng = &rng;
  23605. /* calculate hash for content */
  23606. ret = wc_HashInit(&hash, hashType);
  23607. if (ret == 0) {
  23608. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  23609. if (ret == 0) {
  23610. ret = wc_HashFinal(&hash, hashType, hashBuf);
  23611. }
  23612. wc_HashFree(&hash, hashType);
  23613. }
  23614. AssertIntEQ(ret, 0);
  23615. /* Perform PKCS7 sign using hash directly */
  23616. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23617. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  23618. AssertIntGT(outputHeadSz, 0);
  23619. AssertIntGT(outputFootSz, 0);
  23620. wc_PKCS7_Free(pkcs7);
  23621. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23622. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23623. /* required parameter even on verify when using _ex, if using outputHead
  23624. * and outputFoot */
  23625. pkcs7->contentSz = (word32)sizeof(data);
  23626. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23627. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  23628. wc_PKCS7_Free(pkcs7);
  23629. /* assembly complete PKCS7 sign and use normal verify */
  23630. {
  23631. byte* output = (byte*)XMALLOC(outputHeadSz + sizeof(data) + outputFootSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23632. word32 outputSz = 0;
  23633. AssertNotNull(output);
  23634. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  23635. outputSz += outputHeadSz;
  23636. XMEMCPY(&output[outputSz], data, sizeof(data));
  23637. outputSz += sizeof(data);
  23638. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  23639. outputSz += outputFootSz;
  23640. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23641. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23642. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23643. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23644. }
  23645. /* Pass in bad args. */
  23646. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  23647. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23648. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  23649. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23650. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  23651. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23652. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  23653. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23654. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23655. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23656. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23657. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  23658. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23659. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  23660. pkcs7->hashOID = 0; /* bad hashOID */
  23661. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23662. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23663. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  23664. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23665. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  23666. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23667. #ifndef NO_PKCS7_STREAM
  23668. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  23669. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  23670. #else
  23671. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  23672. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  23673. #endif
  23674. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  23675. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23676. #ifndef NO_PKCS7_STREAM
  23677. /* can pass in 0 buffer length with streaming API */
  23678. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23679. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  23680. #else
  23681. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23682. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23683. #endif
  23684. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23685. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  23686. #ifndef NO_PKCS7_STREAM
  23687. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23688. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  23689. #else
  23690. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23691. outputHead, outputHeadSz, outputFoot, 0), ASN_PARSE_E);
  23692. #endif
  23693. printf(resultFmt, passed);
  23694. wc_PKCS7_Free(pkcs7);
  23695. wc_FreeRng(&rng);
  23696. #endif
  23697. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  23698. #if defined(HAVE_PKCS7)
  23699. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  23700. byte* data, word32 dataSz,
  23701. int withAttribs, int detachedSig)
  23702. {
  23703. PKCS7* pkcs7;
  23704. WC_RNG rng;
  23705. static byte messageTypeOid[] =
  23706. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  23707. 0x09, 0x02 };
  23708. static byte messageType[] = { 0x13, 2, '1', '9' };
  23709. PKCS7Attrib attribs[] =
  23710. {
  23711. { messageTypeOid, sizeof(messageTypeOid), messageType,
  23712. sizeof(messageType) }
  23713. };
  23714. #ifndef NO_RSA
  23715. #if defined(USE_CERT_BUFFERS_2048)
  23716. byte key[sizeof(client_key_der_2048)];
  23717. byte cert[sizeof(client_cert_der_2048)];
  23718. word32 keySz = (word32)sizeof(key);
  23719. word32 certSz = (word32)sizeof(cert);
  23720. XMEMSET(key, 0, keySz);
  23721. XMEMSET(cert, 0, certSz);
  23722. XMEMCPY(key, client_key_der_2048, keySz);
  23723. XMEMCPY(cert, client_cert_der_2048, certSz);
  23724. #elif defined(USE_CERT_BUFFERS_1024)
  23725. byte key[sizeof_client_key_der_1024];
  23726. byte cert[sizeof(sizeof_client_cert_der_1024)];
  23727. word32 keySz = (word32)sizeof(key);
  23728. word32 certSz = (word32)sizeof(cert);
  23729. XMEMSET(key, 0, keySz);
  23730. XMEMSET(cert, 0, certSz);
  23731. XMEMCPY(key, client_key_der_1024, keySz);
  23732. XMEMCPY(cert, client_cert_der_1024, certSz);
  23733. #else
  23734. unsigned char cert[ONEK_BUF];
  23735. unsigned char key[ONEK_BUF];
  23736. FILE* fp;
  23737. int certSz;
  23738. int keySz;
  23739. fp = fopen("./certs/1024/client-cert.der", "rb");
  23740. AssertNotNull(fp);
  23741. certSz = fread(cert, 1, sizeof_client_cert_der_1024, fp);
  23742. fclose(fp);
  23743. fp = fopen("./certs/1024/client-key.der", "rb");
  23744. AssertNotNull(fp);
  23745. keySz = fread(key, 1, sizeof_client_key_der_1024, fp);
  23746. fclose(fp);
  23747. #endif
  23748. #elif defined(HAVE_ECC)
  23749. #if defined(USE_CERT_BUFFERS_256)
  23750. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23751. unsigned char key[sizeof(ecc_clikey_der_256)];
  23752. int certSz = (int)sizeof(cert);
  23753. int keySz = (int)sizeof(key);
  23754. XMEMSET(cert, 0, certSz);
  23755. XMEMSET(key, 0, keySz);
  23756. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  23757. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  23758. #else
  23759. unsigned char cert[ONEK_BUF];
  23760. unsigned char key[ONEK_BUF];
  23761. FILE* fp;
  23762. int certSz, keySz;
  23763. fp = fopen("./certs/client-ecc-cert.der", "rb");
  23764. AssertNotNull(fp);
  23765. certSz = fread(cert, 1, sizeof_cliecc_cert_der_256, fp);
  23766. fclose(fp);
  23767. fp = fopen("./certs/client-ecc-key.der", "rb");
  23768. AssertNotNull(fp);
  23769. keySz = fread(key, 1, sizeof_ecc_clikey_der_256, fp);
  23770. fclose(fp);
  23771. #endif
  23772. #endif
  23773. XMEMSET(output, 0, outputSz);
  23774. AssertIntEQ(wc_InitRng(&rng), 0);
  23775. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23776. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23777. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23778. printf(testingFmt, "wc_PKCS7_VerifySignedData()");
  23779. pkcs7->content = data;
  23780. pkcs7->contentSz = dataSz;
  23781. pkcs7->privateKey = key;
  23782. pkcs7->privateKeySz = (word32)sizeof(key);
  23783. pkcs7->encryptOID = RSAk;
  23784. pkcs7->hashOID = SHAh;
  23785. pkcs7->rng = &rng;
  23786. if (withAttribs) {
  23787. /* include a signed attribute */
  23788. pkcs7->signedAttribs = attribs;
  23789. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  23790. }
  23791. if (detachedSig) {
  23792. AssertIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  23793. }
  23794. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  23795. wc_PKCS7_Free(pkcs7);
  23796. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23797. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23798. if (detachedSig) {
  23799. pkcs7->content = data;
  23800. pkcs7->contentSz = dataSz;
  23801. }
  23802. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23803. wc_PKCS7_Free(pkcs7);
  23804. wc_FreeRng(&rng);
  23805. return outputSz;
  23806. }
  23807. #endif
  23808. /*
  23809. * Testing wc_PKCS_VerifySignedData()
  23810. */
  23811. static void test_wc_PKCS7_VerifySignedData(void)
  23812. {
  23813. #if defined(HAVE_PKCS7)
  23814. PKCS7* pkcs7;
  23815. byte output[FOURK_BUF];
  23816. word32 outputSz = sizeof(output);
  23817. byte data[] = "Test data to encode.";
  23818. byte badOut[1];
  23819. word32 badOutSz = 0;
  23820. byte badContent[] = "This is different content than was signed";
  23821. int ret;
  23822. wc_HashAlg hash;
  23823. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  23824. byte hashBuf[WC_MAX_DIGEST_SIZE];
  23825. word32 hashSz = wc_HashGetDigestSize(hashType);
  23826. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  23827. (word32)sizeof(data),
  23828. 0, 0)), 0);
  23829. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23830. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23831. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23832. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23833. /* Test bad args. */
  23834. AssertIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz), BAD_FUNC_ARG);
  23835. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  23836. #ifndef NO_PKCS7_STREAM
  23837. /* can pass in 0 buffer length with streaming API */
  23838. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  23839. badOutSz), WC_PKCS7_WANT_READ_E);
  23840. #else
  23841. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  23842. badOutSz), BAD_FUNC_ARG);
  23843. #endif
  23844. wc_PKCS7_Free(pkcs7);
  23845. /* Invalid content should error, use detached signature so we can
  23846. * easily change content */
  23847. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  23848. (word32)sizeof(data),
  23849. 1, 1)), 0);
  23850. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23851. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23852. pkcs7->content = badContent;
  23853. pkcs7->contentSz = sizeof(badContent);
  23854. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), SIG_VERIFY_E);
  23855. wc_PKCS7_Free(pkcs7);
  23856. /* Test success case with detached signature and valid content */
  23857. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23858. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23859. pkcs7->content = data;
  23860. pkcs7->contentSz = sizeof(data);
  23861. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23862. wc_PKCS7_Free(pkcs7);
  23863. /* verify using pre-computed content digest only (no content) */
  23864. {
  23865. /* calculate hash for content */
  23866. ret = wc_HashInit(&hash, hashType);
  23867. if (ret == 0) {
  23868. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  23869. if (ret == 0) {
  23870. ret = wc_HashFinal(&hash, hashType, hashBuf);
  23871. }
  23872. wc_HashFree(&hash, hashType);
  23873. }
  23874. AssertIntEQ(ret, 0);
  23875. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23876. AssertIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  23877. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23878. output, outputSz,
  23879. NULL, 0), 0);
  23880. wc_PKCS7_Free(pkcs7);
  23881. }
  23882. printf(resultFmt, passed);
  23883. #endif
  23884. } /* END test_wc_PKCS7_VerifySignedData() */
  23885. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  23886. !defined(NO_AES_256)
  23887. static const byte defKey[] = {
  23888. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  23889. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  23890. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  23891. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  23892. };
  23893. static byte aesHandle[32]; /* simulated hardware key handle */
  23894. /* return 0 on success */
  23895. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  23896. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  23897. byte* in, int inSz, byte* out, void* usrCtx)
  23898. {
  23899. int ret;
  23900. Aes aes;
  23901. if (usrCtx == NULL) {
  23902. /* no simulated handle passed in */
  23903. return -1;
  23904. }
  23905. switch (encryptOID) {
  23906. case AES256CBCb:
  23907. if (ivSz != AES_BLOCK_SIZE)
  23908. return BAD_FUNC_ARG;
  23909. break;
  23910. default:
  23911. WOLFSSL_MSG("Unsupported content cipher type for test");
  23912. return ALGO_ID_E;
  23913. };
  23914. /* simulate using handle to get key */
  23915. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  23916. if (ret == 0) {
  23917. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  23918. if (ret == 0)
  23919. ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
  23920. wc_AesFree(&aes);
  23921. }
  23922. (void)aad;
  23923. (void)aadSz;
  23924. (void)authTag;
  23925. (void)authTagSz;
  23926. (void)pkcs7;
  23927. return ret;
  23928. }
  23929. /* returns key size on success */
  23930. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  23931. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  23932. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  23933. {
  23934. int ret = -1;
  23935. if (out == NULL)
  23936. return BAD_FUNC_ARG;
  23937. if (keyId[0] != 0x00) {
  23938. return -1;
  23939. }
  23940. if (type != (int)PKCS7_KEKRI) {
  23941. return -1;
  23942. }
  23943. switch (keyWrapAlgo) {
  23944. case AES256_WRAP:
  23945. /* simulate setting a handle for later decryption but use key
  23946. * as handle in the test case here */
  23947. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  23948. aesHandle, sizeof(aesHandle), NULL);
  23949. if (ret < 0)
  23950. return ret;
  23951. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  23952. if (ret < 0)
  23953. return ret;
  23954. /* return key size on success */
  23955. return sizeof(defKey);
  23956. default:
  23957. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  23958. return BAD_KEYWRAP_ALG_E;
  23959. };
  23960. (void)cekSz;
  23961. (void)cek;
  23962. (void)outSz;
  23963. (void)keyIdSz;
  23964. (void)direction;
  23965. (void)orginKey; /* used with KAKRI */
  23966. (void)orginKeySz;
  23967. return ret;
  23968. }
  23969. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  23970. /*
  23971. * Testing wc_PKCS7_EncodeEnvelopedData()
  23972. */
  23973. static void test_wc_PKCS7_EncodeDecodeEnvelopedData (void)
  23974. {
  23975. #if defined(HAVE_PKCS7)
  23976. PKCS7* pkcs7;
  23977. #ifdef ECC_TIMING_RESISTANT
  23978. WC_RNG rng;
  23979. #endif
  23980. word32 tempWrd32 = 0;
  23981. byte* tmpBytePtr = NULL;
  23982. const char input[] = "Test data to encode.";
  23983. int i;
  23984. int testSz = 0;
  23985. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  23986. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  23987. byte* rsaCert = NULL;
  23988. byte* rsaPrivKey = NULL;
  23989. word32 rsaCertSz;
  23990. word32 rsaPrivKeySz;
  23991. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  23992. !defined(USE_CERT_BUFFERS_2048) )
  23993. static const char* rsaClientCert = "./certs/client-cert.der";
  23994. static const char* rsaClientKey = "./certs/client-key.der";
  23995. rsaCertSz = (word32)sizeof(rsaClientCert);
  23996. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  23997. #endif
  23998. #endif
  23999. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  24000. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24001. byte* eccCert = NULL;
  24002. byte* eccPrivKey = NULL;
  24003. word32 eccCertSz;
  24004. word32 eccPrivKeySz;
  24005. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  24006. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  24007. static const char* eccClientKey = "./certs/ecc-client-key.der";
  24008. #endif
  24009. #endif
  24010. /* Generic buffer size. */
  24011. byte output[ONEK_BUF];
  24012. byte decoded[sizeof(input)/sizeof(char)];
  24013. int decodedSz = 0;
  24014. #ifndef NO_FILESYSTEM
  24015. XFILE certFile;
  24016. XFILE keyFile;
  24017. #endif
  24018. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  24019. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24020. /* RSA certs and keys. */
  24021. #if defined(USE_CERT_BUFFERS_1024)
  24022. /* Allocate buffer space. */
  24023. AssertNotNull(rsaCert =
  24024. (byte*)XMALLOC(ONEK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  24025. /* Init buffer. */
  24026. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  24027. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  24028. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(ONEK_BUF, HEAP_HINT,
  24029. DYNAMIC_TYPE_TMP_BUFFER));
  24030. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  24031. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  24032. #elif defined(USE_CERT_BUFFERS_2048)
  24033. /* Allocate buffer */
  24034. AssertNotNull(rsaCert =
  24035. (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  24036. /* Init buffer. */
  24037. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  24038. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  24039. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  24040. DYNAMIC_TYPE_TMP_BUFFER));
  24041. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  24042. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  24043. #else
  24044. /* File system. */
  24045. certFile = XFOPEN(rsaClientCert, "rb");
  24046. AssertTrue(certFile != XBADFILE);
  24047. rsaCertSz = (word32)FOURK_BUF;
  24048. AssertNotNull(rsaCert =
  24049. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  24050. rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz, certFile);
  24051. XFCLOSE(certFile);
  24052. keyFile = XFOPEN(rsaClientKey, "rb");
  24053. AssertTrue(keyFile != XBADFILE);
  24054. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24055. DYNAMIC_TYPE_TMP_BUFFER));
  24056. rsaPrivKeySz = (word32)FOURK_BUF;
  24057. rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz, keyFile);
  24058. XFCLOSE(keyFile);
  24059. #endif /* USE_CERT_BUFFERS */
  24060. #endif /* NO_RSA */
  24061. /* ECC */
  24062. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  24063. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24064. #ifdef USE_CERT_BUFFERS_256
  24065. AssertNotNull(eccCert =
  24066. (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  24067. /* Init buffer. */
  24068. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  24069. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  24070. AssertNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  24071. DYNAMIC_TYPE_TMP_BUFFER));
  24072. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  24073. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  24074. #else /* File system. */
  24075. certFile = XFOPEN(eccClientCert, "rb");
  24076. AssertTrue(certFile != XBADFILE);
  24077. eccCertSz = (word32)FOURK_BUF;
  24078. AssertNotNull(eccCert =
  24079. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  24080. eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz, certFile);
  24081. XFCLOSE(certFile);
  24082. keyFile = XFOPEN(eccClientKey, "rb");
  24083. AssertTrue(keyFile != XBADFILE);
  24084. eccPrivKeySz = (word32)FOURK_BUF;
  24085. AssertNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24086. DYNAMIC_TYPE_TMP_BUFFER));
  24087. eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz, keyFile);
  24088. XFCLOSE(keyFile);
  24089. #endif /* USE_CERT_BUFFERS_256 */
  24090. #endif /* END HAVE_ECC */
  24091. /* Silence. */
  24092. (void)keyFile;
  24093. (void)certFile;
  24094. {
  24095. const pkcs7EnvelopedVector testVectors[] = {
  24096. /* DATA is a global variable defined in the makefile. */
  24097. #if !defined(NO_RSA)
  24098. #ifndef NO_DES3
  24099. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  24100. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24101. #endif /* NO_DES3 */
  24102. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24103. #ifndef NO_AES_128
  24104. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  24105. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24106. #endif
  24107. #ifndef NO_AES_192
  24108. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  24109. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24110. #endif
  24111. #ifndef NO_AES_256
  24112. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  24113. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24114. #endif
  24115. #endif /* NO_AES && HAVE_AES_CBC */
  24116. #endif /* NO_RSA */
  24117. #if defined(HAVE_ECC)
  24118. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24119. #if !defined(NO_SHA) && !defined(NO_AES_128)
  24120. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  24121. AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme, eccCert,
  24122. eccCertSz, eccPrivKey, eccPrivKeySz},
  24123. #endif
  24124. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  24125. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  24126. AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme, eccCert,
  24127. eccCertSz, eccPrivKey, eccPrivKeySz},
  24128. #endif
  24129. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  24130. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  24131. AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme, eccCert,
  24132. eccCertSz, eccPrivKey, eccPrivKeySz},
  24133. #endif
  24134. #endif /* NO_AES && HAVE_AES_CBC*/
  24135. #endif /* END HAVE_ECC */
  24136. }; /* END pkcs7EnvelopedVector */
  24137. #ifdef ECC_TIMING_RESISTANT
  24138. AssertIntEQ(wc_InitRng(&rng), 0);
  24139. #endif
  24140. printf(testingFmt, "wc_PKCS7_EncodeEnvelopedData()");
  24141. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24142. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, devId), 0);
  24143. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  24144. for (i = 0; i < testSz; i++) {
  24145. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  24146. (word32)(testVectors + i)->certSz), 0);
  24147. #ifdef ECC_TIMING_RESISTANT
  24148. pkcs7->rng = &rng;
  24149. #endif
  24150. pkcs7->content = (byte*)(testVectors + i)->content;
  24151. pkcs7->contentSz = (testVectors + i)->contentSz;
  24152. pkcs7->contentOID = (testVectors + i)->contentOID;
  24153. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  24154. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  24155. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  24156. pkcs7->privateKey = (testVectors + i)->privateKey;
  24157. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  24158. AssertIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  24159. (word32)sizeof(output)), 0);
  24160. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24161. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  24162. AssertIntGE(decodedSz, 0);
  24163. /* Verify the size of each buffer. */
  24164. AssertIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  24165. /* Don't free the last time through the loop. */
  24166. if (i < testSz - 1 ){
  24167. wc_PKCS7_Free(pkcs7);
  24168. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24169. }
  24170. } /* END test loop. */
  24171. }
  24172. /* Test bad args. */
  24173. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  24174. (word32)sizeof(output)), BAD_FUNC_ARG);
  24175. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  24176. (word32)sizeof(output)), BAD_FUNC_ARG);
  24177. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  24178. printf(resultFmt, passed);
  24179. /* Decode. */
  24180. printf(testingFmt, "wc_PKCS7_DecodeEnvelopedData()");
  24181. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  24182. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24183. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24184. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24185. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24186. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  24187. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  24188. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24189. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  24190. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24191. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  24192. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  24193. /* only a failure for KARI test cases */
  24194. tempWrd32 = pkcs7->singleCertSz;
  24195. pkcs7->singleCertSz = 0;
  24196. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24197. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24198. pkcs7->singleCertSz = tempWrd32;
  24199. tmpBytePtr = pkcs7->singleCert;
  24200. pkcs7->singleCert = NULL;
  24201. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24202. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24203. pkcs7->singleCert = tmpBytePtr;
  24204. #endif
  24205. tempWrd32 = pkcs7->privateKeySz;
  24206. pkcs7->privateKeySz = 0;
  24207. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24208. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24209. pkcs7->privateKeySz = tempWrd32;
  24210. tmpBytePtr = pkcs7->privateKey;
  24211. pkcs7->privateKey = NULL;
  24212. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24213. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24214. pkcs7->privateKey = tmpBytePtr;
  24215. wc_PKCS7_Free(pkcs7);
  24216. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  24217. /* test of decrypt callback with KEKRI enveloped data */
  24218. {
  24219. int envelopedSz;
  24220. const byte keyId[] = { 0x00 };
  24221. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24222. pkcs7->content = (byte*)input;
  24223. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  24224. pkcs7->contentOID = DATA;
  24225. pkcs7->encryptOID = AES256CBCb;
  24226. AssertIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  24227. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  24228. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  24229. AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  24230. AssertIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  24231. (word32)sizeof(output))), 0);
  24232. wc_PKCS7_Free(pkcs7);
  24233. /* decode envelopedData */
  24234. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24235. AssertIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  24236. AssertIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  24237. AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24238. envelopedSz, decoded, sizeof(decoded))), 0);
  24239. wc_PKCS7_Free(pkcs7);
  24240. }
  24241. #endif /* !NO_AES && !NO_AES_256 */
  24242. printf(resultFmt, passed);
  24243. #ifndef NO_RSA
  24244. if (rsaCert) {
  24245. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24246. }
  24247. if (rsaPrivKey) {
  24248. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24249. }
  24250. #endif /*NO_RSA */
  24251. #ifdef HAVE_ECC
  24252. if (eccCert) {
  24253. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24254. }
  24255. if (eccPrivKey) {
  24256. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24257. }
  24258. #endif /* HAVE_ECC */
  24259. #ifdef ECC_TIMING_RESISTANT
  24260. wc_FreeRng(&rng);
  24261. #endif
  24262. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3)
  24263. {
  24264. byte out[7];
  24265. byte *cms;
  24266. word32 cmsSz;
  24267. XFILE cmsFile;
  24268. XMEMSET(out, 0, sizeof(out));
  24269. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24270. cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb");
  24271. AssertTrue(cmsFile != XBADFILE);
  24272. cmsSz = (word32)FOURK_BUF;
  24273. AssertNotNull(cms =
  24274. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  24275. cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile);
  24276. XFCLOSE(cmsFile);
  24277. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  24278. sizeof_client_cert_der_2048), 0);
  24279. pkcs7->privateKey = (byte*)client_key_der_2048;
  24280. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  24281. AssertIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  24282. 2), 0);
  24283. AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  24284. sizeof(out)), 0);
  24285. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24286. AssertIntEQ(XMEMCMP(out, "test", 4), 0);
  24287. wc_PKCS7_Free(pkcs7);
  24288. }
  24289. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 */
  24290. #endif /* HAVE_PKCS7 */
  24291. } /* END test_wc_PKCS7_EncodeEnvelopedData() */
  24292. /*
  24293. * Testing wc_PKCS7_EncodeEncryptedData()
  24294. */
  24295. static void test_wc_PKCS7_EncodeEncryptedData (void)
  24296. {
  24297. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  24298. PKCS7* pkcs7 = NULL;
  24299. byte* tmpBytePtr = NULL;
  24300. byte encrypted[TWOK_BUF];
  24301. byte decoded[TWOK_BUF];
  24302. word32 tmpWrd32 = 0;
  24303. int tmpInt = 0;
  24304. int decodedSz;
  24305. int encryptedSz;
  24306. int testSz;
  24307. int i;
  24308. const byte data[] = { /* Hello World */
  24309. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  24310. 0x72,0x6c,0x64
  24311. };
  24312. #ifndef NO_DES3
  24313. byte desKey[] = {
  24314. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  24315. };
  24316. byte des3Key[] = {
  24317. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  24318. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  24319. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  24320. };
  24321. #endif
  24322. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24323. #ifndef NO_AES_128
  24324. byte aes128Key[] = {
  24325. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24326. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24327. };
  24328. #endif
  24329. #ifndef NO_AES_192
  24330. byte aes192Key[] = {
  24331. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24332. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24333. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24334. };
  24335. #endif
  24336. #ifndef NO_AES_256
  24337. byte aes256Key[] = {
  24338. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24339. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24340. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24341. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24342. };
  24343. #endif
  24344. #endif /* !NO_AES && HAVE_AES_CBC */
  24345. const pkcs7EncryptedVector testVectors[] =
  24346. {
  24347. #ifndef NO_DES3
  24348. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  24349. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  24350. #endif /* !NO_DES3 */
  24351. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24352. #ifndef NO_AES_128
  24353. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  24354. sizeof(aes128Key)},
  24355. #endif
  24356. #ifndef NO_AES_192
  24357. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  24358. sizeof(aes192Key)},
  24359. #endif
  24360. #ifndef NO_AES_256
  24361. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  24362. sizeof(aes256Key)},
  24363. #endif
  24364. #endif /* !NO_AES && HAVE_AES_CBC */
  24365. };
  24366. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  24367. for (i = 0; i < testSz; i++) {
  24368. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24369. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, devId), 0);
  24370. pkcs7->content = (byte*)testVectors[i].content;
  24371. pkcs7->contentSz = testVectors[i].contentSz;
  24372. pkcs7->contentOID = testVectors[i].contentOID;
  24373. pkcs7->encryptOID = testVectors[i].encryptOID;
  24374. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  24375. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  24376. pkcs7->heap = HEAP_HINT;
  24377. /* encode encryptedData */
  24378. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24379. sizeof(encrypted));
  24380. AssertIntGT(encryptedSz, 0);
  24381. /* Decode encryptedData */
  24382. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24383. decoded, sizeof(decoded));
  24384. AssertIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  24385. /* Keep values for last itr. */
  24386. if (i < testSz - 1) {
  24387. wc_PKCS7_Free(pkcs7);
  24388. }
  24389. }
  24390. if (pkcs7 == NULL || testSz == 0) {
  24391. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24392. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, devId), 0);
  24393. }
  24394. printf(testingFmt, "wc_PKCS7_EncodeEncryptedData()");
  24395. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  24396. sizeof(encrypted)),BAD_FUNC_ARG);
  24397. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  24398. sizeof(encrypted)), BAD_FUNC_ARG);
  24399. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24400. 0), BAD_FUNC_ARG);
  24401. /* Testing the struct. */
  24402. tmpBytePtr = pkcs7->content;
  24403. pkcs7->content = NULL;
  24404. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24405. sizeof(encrypted)), BAD_FUNC_ARG);
  24406. pkcs7->content = tmpBytePtr;
  24407. tmpWrd32 = pkcs7->contentSz;
  24408. pkcs7->contentSz = 0;
  24409. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24410. sizeof(encrypted)), BAD_FUNC_ARG);
  24411. pkcs7->contentSz = tmpWrd32;
  24412. tmpInt = pkcs7->encryptOID;
  24413. pkcs7->encryptOID = 0;
  24414. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24415. sizeof(encrypted)), BAD_FUNC_ARG);
  24416. pkcs7->encryptOID = tmpInt;
  24417. tmpBytePtr = pkcs7->encryptionKey;
  24418. pkcs7->encryptionKey = NULL;
  24419. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24420. sizeof(encrypted)), BAD_FUNC_ARG);
  24421. pkcs7->encryptionKey = tmpBytePtr;
  24422. tmpWrd32 = pkcs7->encryptionKeySz;
  24423. pkcs7->encryptionKeySz = 0;
  24424. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24425. sizeof(encrypted)), BAD_FUNC_ARG);
  24426. pkcs7->encryptionKeySz = tmpWrd32;
  24427. printf(resultFmt, passed);
  24428. printf(testingFmt, "wc_PKCS7_EncodeEncryptedData()");
  24429. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  24430. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24431. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
  24432. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24433. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  24434. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24435. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24436. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  24437. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24438. decoded, 0), BAD_FUNC_ARG);
  24439. /* Test struct fields */
  24440. tmpBytePtr = pkcs7->encryptionKey;
  24441. pkcs7->encryptionKey = NULL;
  24442. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24443. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24444. pkcs7->encryptionKey = tmpBytePtr;
  24445. pkcs7->encryptionKeySz = 0;
  24446. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24447. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24448. printf(resultFmt, passed);
  24449. wc_PKCS7_Free(pkcs7);
  24450. #endif
  24451. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  24452. /*
  24453. * Testing wc_PKCS7_Degenerate()
  24454. */
  24455. static void test_wc_PKCS7_Degenerate(void)
  24456. {
  24457. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  24458. PKCS7* pkcs7;
  24459. char fName[] = "./certs/test-degenerate.p7b";
  24460. XFILE f;
  24461. byte der[4096];
  24462. word32 derSz;
  24463. int ret;
  24464. printf(testingFmt, "wc_PKCS7_Degenerate()");
  24465. AssertNotNull(f = XFOPEN(fName, "rb"));
  24466. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  24467. derSz = (word32)ret;
  24468. XFCLOSE(f);
  24469. /* test degenerate success */
  24470. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24471. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24472. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24473. #ifndef NO_RSA
  24474. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  24475. #else
  24476. AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  24477. #endif
  24478. wc_PKCS7_Free(pkcs7);
  24479. /* test with turning off degenerate cases */
  24480. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24481. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24482. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24483. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  24484. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), PKCS7_NO_SIGNER_E);
  24485. wc_PKCS7_Free(pkcs7);
  24486. printf(resultFmt, passed);
  24487. #endif
  24488. } /* END test_wc_PKCS7_Degenerate() */
  24489. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  24490. defined(ASN_BER_TO_DER) && !defined(NO_DES3)
  24491. static byte berContent[] = {
  24492. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  24493. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  24494. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  24495. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  24496. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  24497. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  24498. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  24499. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  24500. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  24501. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  24502. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  24503. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  24504. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  24505. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  24506. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  24507. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  24508. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  24509. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  24510. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  24511. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  24512. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  24513. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  24514. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  24515. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  24516. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  24517. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  24518. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  24519. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  24520. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  24521. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  24522. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  24523. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  24524. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  24525. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  24526. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  24527. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  24528. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  24529. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  24530. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  24531. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  24532. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  24533. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  24534. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  24535. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  24536. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  24537. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  24538. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  24539. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  24540. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  24541. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  24542. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  24543. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  24544. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  24545. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  24546. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  24547. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  24548. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  24549. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  24550. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  24551. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  24552. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  24553. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  24554. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  24555. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  24556. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  24557. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  24558. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  24559. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  24560. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  24561. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  24562. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  24563. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  24564. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  24565. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  24566. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  24567. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  24568. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  24569. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  24570. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  24571. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  24572. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  24573. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  24574. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  24575. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  24576. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  24577. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  24578. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  24579. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  24580. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  24581. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  24582. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  24583. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  24584. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  24585. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  24586. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  24587. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  24588. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  24589. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  24590. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  24591. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  24592. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  24593. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  24594. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  24595. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  24596. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  24597. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  24598. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  24599. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  24600. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  24601. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  24602. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  24603. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  24604. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  24605. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  24606. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  24607. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  24608. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  24609. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  24610. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  24611. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  24612. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  24613. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  24614. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  24615. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  24616. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  24617. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  24618. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  24619. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  24620. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  24621. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  24622. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  24623. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  24624. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  24625. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  24626. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  24627. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  24628. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  24629. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  24630. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  24631. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  24632. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  24633. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  24634. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  24635. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  24636. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  24637. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  24638. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  24639. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  24640. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  24641. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  24642. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  24643. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  24644. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  24645. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  24646. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  24647. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  24648. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  24649. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  24650. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  24651. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  24652. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  24653. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  24654. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  24655. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  24656. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  24657. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  24658. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  24659. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  24660. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  24661. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  24662. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  24663. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  24664. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  24665. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  24666. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  24667. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  24668. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  24669. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  24670. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  24671. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  24672. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  24673. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  24674. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  24675. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  24676. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  24677. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  24678. 0x00, 0x00, 0x00, 0x00, 0x00
  24679. };
  24680. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER && !NO_DES3 */
  24681. /*
  24682. * Testing wc_PKCS7_BER()
  24683. */
  24684. static void test_wc_PKCS7_BER(void)
  24685. {
  24686. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  24687. defined(ASN_BER_TO_DER)
  24688. PKCS7* pkcs7;
  24689. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  24690. XFILE f;
  24691. byte der[4096];
  24692. #ifndef NO_DES3
  24693. byte decoded[2048];
  24694. #endif
  24695. word32 derSz;
  24696. int ret;
  24697. printf(testingFmt, "wc_PKCS7_BER()");
  24698. AssertNotNull(f = XFOPEN(fName, "rb"));
  24699. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  24700. derSz = (word32)ret;
  24701. XFCLOSE(f);
  24702. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24703. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24704. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24705. #ifndef NO_RSA
  24706. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  24707. #else
  24708. AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  24709. #endif
  24710. wc_PKCS7_Free(pkcs7);
  24711. #ifndef NO_DES3
  24712. /* decode BER content */
  24713. AssertNotNull(f = XFOPEN("./certs/1024/client-cert.der", "rb"));
  24714. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  24715. derSz = (word32)ret;
  24716. XFCLOSE(f);
  24717. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24718. #ifndef NO_RSA
  24719. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  24720. #else
  24721. AssertIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  24722. #endif
  24723. AssertNotNull(f = XFOPEN("./certs/1024/client-key.der", "rb"));
  24724. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  24725. derSz = (word32)ret;
  24726. XFCLOSE(f);
  24727. pkcs7->privateKey = der;
  24728. pkcs7->privateKeySz = derSz;
  24729. #ifndef NO_RSA
  24730. #ifdef WOLFSSL_SP_MATH
  24731. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  24732. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  24733. #else
  24734. AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  24735. sizeof(berContent), decoded, sizeof(decoded)), 0);
  24736. #endif
  24737. #else
  24738. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  24739. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  24740. #endif
  24741. wc_PKCS7_Free(pkcs7);
  24742. #endif /* !NO_DES3 */
  24743. printf(resultFmt, passed);
  24744. #endif
  24745. } /* END test_wc_PKCS7_BER() */
  24746. static void test_PKCS7_signed_enveloped(void)
  24747. {
  24748. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  24749. !defined(NO_FILESYSTEM)
  24750. XFILE f;
  24751. PKCS7* pkcs7;
  24752. #ifdef HAVE_AES_CBC
  24753. PKCS7* inner;
  24754. #endif
  24755. void* pt;
  24756. WC_RNG rng;
  24757. unsigned char key[FOURK_BUF/2];
  24758. unsigned char cert[FOURK_BUF/2];
  24759. unsigned char env[FOURK_BUF];
  24760. int envSz = FOURK_BUF;
  24761. int keySz;
  24762. int certSz;
  24763. unsigned char sig[FOURK_BUF * 2];
  24764. int sigSz = FOURK_BUF * 2;
  24765. #ifdef HAVE_AES_CBC
  24766. unsigned char decoded[FOURK_BUF];
  24767. int decodedSz = FOURK_BUF;
  24768. #endif
  24769. printf(testingFmt, "PKCS7_signed_enveloped");
  24770. /* load cert */
  24771. AssertNotNull(f = XFOPEN(cliCertDerFile, "rb"));
  24772. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  24773. XFCLOSE(f);
  24774. /* load key */
  24775. AssertNotNull(f = XFOPEN(cliKeyFile, "rb"));
  24776. AssertIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  24777. XFCLOSE(f);
  24778. keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL);
  24779. /* sign cert for envelope */
  24780. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  24781. AssertIntEQ(wc_InitRng(&rng), 0);
  24782. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24783. pkcs7->content = cert;
  24784. pkcs7->contentSz = certSz;
  24785. pkcs7->contentOID = DATA;
  24786. pkcs7->privateKey = key;
  24787. pkcs7->privateKeySz = keySz;
  24788. pkcs7->encryptOID = RSAk;
  24789. pkcs7->hashOID = SHA256h;
  24790. pkcs7->rng = &rng;
  24791. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  24792. wc_PKCS7_Free(pkcs7);
  24793. wc_FreeRng(&rng);
  24794. #ifdef HAVE_AES_CBC
  24795. /* create envelope */
  24796. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  24797. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24798. pkcs7->content = sig;
  24799. pkcs7->contentSz = sigSz;
  24800. pkcs7->contentOID = DATA;
  24801. pkcs7->encryptOID = AES256CBCb;
  24802. pkcs7->privateKey = key;
  24803. pkcs7->privateKeySz = keySz;
  24804. AssertIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
  24805. AssertIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  24806. wc_PKCS7_Free(pkcs7);
  24807. #endif
  24808. /* create bad signed enveloped data */
  24809. sigSz = FOURK_BUF * 2;
  24810. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  24811. AssertIntEQ(wc_InitRng(&rng), 0);
  24812. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24813. pkcs7->content = env;
  24814. pkcs7->contentSz = envSz;
  24815. pkcs7->contentOID = DATA;
  24816. pkcs7->privateKey = key;
  24817. pkcs7->privateKeySz = keySz;
  24818. pkcs7->encryptOID = RSAk;
  24819. pkcs7->hashOID = SHA256h;
  24820. pkcs7->rng = &rng;
  24821. /* Set no certs in bundle for this test. Hang on to the pointer though to
  24822. * free it later. */
  24823. pt = (void*)pkcs7->certList;
  24824. pkcs7->certList = NULL; /* no certs in bundle */
  24825. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  24826. pkcs7->certList = (Pkcs7Cert*)pt; /* restore pointer for PKCS7 free call */
  24827. wc_PKCS7_Free(pkcs7);
  24828. /* check verify fails */
  24829. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  24830. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24831. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
  24832. PKCS7_SIGNEEDS_CHECK);
  24833. /* try verifying the signature manually */
  24834. {
  24835. RsaKey rKey;
  24836. word32 idx = 0;
  24837. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  24838. WC_MAX_DIGEST_SIZE];
  24839. int digestSz;
  24840. AssertIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  24841. AssertIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
  24842. digestSz = wc_RsaSSL_Verify(pkcs7->signature, pkcs7->signatureSz,
  24843. digest, sizeof(digest), &rKey);
  24844. AssertIntGT(digestSz, 0);
  24845. AssertIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  24846. AssertIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  24847. AssertIntEQ(wc_FreeRsaKey(&rKey), 0);
  24848. /* verify was success */
  24849. }
  24850. wc_PKCS7_Free(pkcs7);
  24851. /* initializing the PKCS7 struct with the signing certificate should pass */
  24852. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  24853. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24854. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  24855. wc_PKCS7_Free(pkcs7);
  24856. /* create valid degenerate bundle */
  24857. sigSz = FOURK_BUF * 2;
  24858. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  24859. pkcs7->content = env;
  24860. pkcs7->contentSz = envSz;
  24861. pkcs7->contentOID = DATA;
  24862. pkcs7->privateKey = key;
  24863. pkcs7->privateKeySz = keySz;
  24864. pkcs7->encryptOID = RSAk;
  24865. pkcs7->hashOID = SHA256h;
  24866. pkcs7->rng = &rng;
  24867. AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  24868. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  24869. wc_PKCS7_Free(pkcs7);
  24870. wc_FreeRng(&rng);
  24871. /* check verify */
  24872. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  24873. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, devId), 0);
  24874. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  24875. AssertNotNull(pkcs7->content);
  24876. #ifdef HAVE_AES_CBC
  24877. /* check decode */
  24878. AssertNotNull(inner = wc_PKCS7_New(NULL, 0));
  24879. AssertIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
  24880. inner->privateKey = key;
  24881. inner->privateKeySz = keySz;
  24882. AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  24883. pkcs7->contentSz, decoded, decodedSz)), 0);
  24884. wc_PKCS7_Free(inner);
  24885. #endif
  24886. wc_PKCS7_Free(pkcs7);
  24887. #ifdef HAVE_AES_CBC
  24888. /* check cert set */
  24889. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  24890. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24891. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
  24892. AssertNotNull(pkcs7->singleCert);
  24893. AssertIntNE(pkcs7->singleCertSz, 0);
  24894. wc_PKCS7_Free(pkcs7);
  24895. #endif
  24896. printf(resultFmt, passed);
  24897. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  24898. }
  24899. static void test_wc_PKCS7_NoDefaultSignedAttribs (void)
  24900. {
  24901. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  24902. && !defined(NO_AES)
  24903. PKCS7* pkcs7;
  24904. void* heap = NULL;
  24905. printf(testingFmt, "wc_PKCS7_NoDefaultSignedAttribs()");
  24906. pkcs7 = wc_PKCS7_New(heap, devId);
  24907. AssertNotNull(pkcs7);
  24908. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, devId), 0);
  24909. AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  24910. AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  24911. wc_PKCS7_Free(pkcs7);
  24912. printf(resultFmt, passed);
  24913. #endif
  24914. }
  24915. static void test_wc_PKCS7_SetOriEncryptCtx (void)
  24916. {
  24917. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  24918. && !defined(NO_AES)
  24919. PKCS7* pkcs7;
  24920. void* heap = NULL;
  24921. WOLFSSL_CTX* ctx;
  24922. ctx = NULL;
  24923. printf(testingFmt, "wc_PKCS7_SetOriEncryptCtx()");
  24924. pkcs7 = wc_PKCS7_New(heap, devId);
  24925. AssertNotNull(pkcs7);
  24926. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, devId), 0);
  24927. AssertIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  24928. AssertIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  24929. wc_PKCS7_Free(pkcs7);
  24930. printf(resultFmt, passed);
  24931. #endif
  24932. }
  24933. static void test_wc_PKCS7_SetOriDecryptCtx (void)
  24934. {
  24935. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  24936. && !defined(NO_AES)
  24937. PKCS7* pkcs7;
  24938. void* heap = NULL;
  24939. WOLFSSL_CTX* ctx;
  24940. ctx = NULL;
  24941. printf(testingFmt, "wc_PKCS7_SetOriDecryptCtx()");
  24942. pkcs7 = wc_PKCS7_New(heap, devId);
  24943. AssertNotNull(pkcs7);
  24944. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, devId), 0);
  24945. AssertIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  24946. AssertIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  24947. wc_PKCS7_Free(pkcs7);
  24948. printf(resultFmt, passed);
  24949. #endif
  24950. }
  24951. static void test_wc_PKCS7_DecodeCompressedData(void)
  24952. {
  24953. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  24954. && !defined(NO_AES) && defined(HAVE_LIBZ)
  24955. PKCS7* pkcs7;
  24956. void* heap = NULL;
  24957. byte out[4096];
  24958. byte *decompressed;
  24959. int outSz, decompressedSz;
  24960. const char* cert = "./certs/client-cert.pem";
  24961. byte* cert_buf = NULL;
  24962. size_t cert_sz = 0;
  24963. printf(testingFmt, "wc_PKCS7_DecodeCompressedData()");
  24964. AssertIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  24965. AssertNotNull((decompressed =
  24966. (byte*)XMALLOC(cert_sz, heap, DYNAMIC_TYPE_TMP_BUFFER)));
  24967. decompressedSz = (int)cert_sz;
  24968. AssertNotNull((pkcs7 = wc_PKCS7_New(heap, devId)));
  24969. pkcs7->content = (byte*)cert_buf;
  24970. pkcs7->contentSz = (word32)cert_sz;
  24971. pkcs7->contentOID = DATA;
  24972. AssertIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  24973. sizeof(out))), 0);
  24974. wc_PKCS7_Free(pkcs7);
  24975. /* compressed key should be smaller than when started */
  24976. AssertIntLT(outSz, cert_sz);
  24977. /* test decompression */
  24978. AssertNotNull((pkcs7 = wc_PKCS7_New(heap, devId)));
  24979. AssertIntEQ(pkcs7->contentOID, 0);
  24980. /* fail case with out buffer too small */
  24981. AssertIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  24982. decompressed, outSz), 0);
  24983. /* success case */
  24984. AssertIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  24985. decompressed, decompressedSz), cert_sz);
  24986. AssertIntEQ(pkcs7->contentOID, DATA);
  24987. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  24988. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  24989. decompressed = NULL;
  24990. /* test decompression function with different 'max' inputs */
  24991. outSz = sizeof(out);
  24992. AssertIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  24993. 0);
  24994. AssertIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  24995. out, outSz, 0, heap), 0);
  24996. AssertNull(decompressed);
  24997. AssertIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  24998. out, outSz, 0, heap), 0);
  24999. AssertNotNull(decompressed);
  25000. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  25001. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  25002. decompressed = NULL;
  25003. AssertIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  25004. out, outSz, 0, heap), 0);
  25005. AssertNotNull(decompressed);
  25006. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  25007. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  25008. if (cert_buf)
  25009. free(cert_buf);
  25010. wc_PKCS7_Free(pkcs7);
  25011. printf(resultFmt, passed);
  25012. #endif
  25013. }
  25014. static void test_wc_i2d_PKCS12(void)
  25015. {
  25016. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  25017. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25018. && !defined(NO_AES) && !defined(NO_DES3) && !defined(NO_SHA)
  25019. WC_PKCS12* pkcs12 = NULL;
  25020. unsigned char der[FOURK_BUF * 2];
  25021. unsigned char* pt;
  25022. int derSz;
  25023. unsigned char out[FOURK_BUF * 2];
  25024. int outSz = FOURK_BUF * 2;
  25025. const char p12_f[] = "./certs/test-servercert.p12";
  25026. XFILE f;
  25027. printf(testingFmt, "wc_i2d_PKCS12");
  25028. f = XFOPEN(p12_f, "rb");
  25029. AssertNotNull(f);
  25030. derSz = (int)XFREAD(der, 1, sizeof(der), f);
  25031. AssertIntGT(derSz, 0);
  25032. XFCLOSE(f);
  25033. AssertNotNull(pkcs12 = wc_PKCS12_new());
  25034. AssertIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
  25035. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  25036. AssertIntEQ(outSz, derSz);
  25037. outSz = derSz - 1;
  25038. pt = out;
  25039. AssertIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  25040. outSz = derSz;
  25041. AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  25042. AssertIntEQ((pt == out), 0);
  25043. pt = NULL;
  25044. AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  25045. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  25046. wc_PKCS12_free(pkcs12);
  25047. /* Run the same test but use wc_d2i_PKCS12_fp. */
  25048. AssertNotNull(pkcs12 = wc_PKCS12_new());
  25049. AssertIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  25050. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  25051. AssertIntEQ(outSz, derSz);
  25052. wc_PKCS12_free(pkcs12);
  25053. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  25054. pkcs12 = NULL;
  25055. AssertIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  25056. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  25057. AssertIntEQ(outSz, derSz);
  25058. wc_PKCS12_free(pkcs12);
  25059. printf(resultFmt, passed);
  25060. #endif
  25061. }
  25062. /* Testing wc_SignatureGetSize() for signature type ECC */
  25063. static int test_wc_SignatureGetSize_ecc(void)
  25064. {
  25065. int ret = 0;
  25066. #ifndef NO_SIG_WRAPPER
  25067. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  25068. enum wc_SignatureType sig_type;
  25069. word32 key_len;
  25070. /* Initialize ECC Key */
  25071. ecc_key ecc;
  25072. const char* qx =
  25073. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  25074. const char* qy =
  25075. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  25076. const char* d =
  25077. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  25078. ret = wc_ecc_init(&ecc);
  25079. if (ret == 0) {
  25080. ret = wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1");
  25081. }
  25082. printf(testingFmt, "wc_SigntureGetSize_ecc()");
  25083. if (ret == 0) {
  25084. /* Input for signature type ECC */
  25085. sig_type = WC_SIGNATURE_TYPE_ECC;
  25086. key_len = sizeof(ecc_key);
  25087. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  25088. /* Test bad args */
  25089. if (ret > 0) {
  25090. sig_type = (enum wc_SignatureType) 100;
  25091. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  25092. if (ret == BAD_FUNC_ARG) {
  25093. sig_type = WC_SIGNATURE_TYPE_ECC;
  25094. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  25095. }
  25096. if (ret >= 0) {
  25097. key_len = (word32) 0;
  25098. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  25099. }
  25100. if (ret == BAD_FUNC_ARG) {
  25101. ret = SIG_TYPE_E;
  25102. }
  25103. }
  25104. } else {
  25105. ret = WOLFSSL_FATAL_ERROR;
  25106. }
  25107. wc_ecc_free(&ecc);
  25108. #else
  25109. ret = SIG_TYPE_E;
  25110. #endif
  25111. if (ret == SIG_TYPE_E) {
  25112. ret = 0;
  25113. }
  25114. else {
  25115. ret = WOLFSSL_FATAL_ERROR;
  25116. }
  25117. printf(resultFmt, ret == 0 ? passed : failed);
  25118. #endif /* NO_SIG_WRAPPER */
  25119. return ret;
  25120. }/* END test_wc_SignatureGetSize_ecc() */
  25121. /* Testing wc_SignatureGetSize() for signature type rsa */
  25122. static int test_wc_SignatureGetSize_rsa(void)
  25123. {
  25124. int ret = 0;
  25125. #ifndef NO_SIG_WRAPPER
  25126. #ifndef NO_RSA
  25127. enum wc_SignatureType sig_type;
  25128. word32 key_len;
  25129. word32 idx = 0;
  25130. /* Initialize RSA Key */
  25131. RsaKey rsa_key;
  25132. byte* tmp = NULL;
  25133. size_t bytes;
  25134. #ifdef USE_CERT_BUFFERS_1024
  25135. bytes = (size_t)sizeof_client_key_der_1024;
  25136. if (bytes < (size_t)sizeof_client_key_der_1024)
  25137. bytes = (size_t)sizeof_client_cert_der_1024;
  25138. #elif defined(USE_CERT_BUFFERS_2048)
  25139. bytes = (size_t)sizeof_client_key_der_2048;
  25140. if (bytes < (size_t)sizeof_client_cert_der_2048)
  25141. bytes = (size_t)sizeof_client_cert_der_2048;
  25142. #else
  25143. bytes = FOURK_BUF;
  25144. #endif
  25145. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25146. if (tmp != NULL) {
  25147. #ifdef USE_CERT_BUFFERS_1024
  25148. XMEMCPY(tmp, client_key_der_1024,
  25149. (size_t)sizeof_client_key_der_1024);
  25150. #elif defined(USE_CERT_BUFFERS_2048)
  25151. XMEMCPY(tmp, client_key_der_2048,
  25152. (size_t)sizeof_client_key_der_2048);
  25153. #elif !defined(NO_FILESYSTEM)
  25154. file = XFOPEN(clientKey, "rb");
  25155. if (file != XBADFILE) {
  25156. bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file);
  25157. XFCLOSE(file);
  25158. }
  25159. else {
  25160. ret = WOLFSSL_FATAL_ERROR;
  25161. }
  25162. #else
  25163. ret = WOLFSSL_FATAL_ERROR;
  25164. #endif
  25165. } else {
  25166. ret = WOLFSSL_FATAL_ERROR;
  25167. }
  25168. if (ret == 0) {
  25169. ret = wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, devId);
  25170. }
  25171. if (ret == 0) {
  25172. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes);
  25173. }
  25174. printf(testingFmt, "wc_SigntureGetSize_rsa()");
  25175. if (ret == 0) {
  25176. /* Input for signature type RSA */
  25177. sig_type = WC_SIGNATURE_TYPE_RSA;
  25178. key_len = sizeof(RsaKey);
  25179. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  25180. /* Test bad args */
  25181. if (ret > 0) {
  25182. sig_type = (enum wc_SignatureType) 100;
  25183. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  25184. if (ret == BAD_FUNC_ARG) {
  25185. sig_type = WC_SIGNATURE_TYPE_RSA;
  25186. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  25187. }
  25188. #ifndef HAVE_USER_RSA
  25189. if (ret == BAD_FUNC_ARG) {
  25190. #else
  25191. if (ret == 0) {
  25192. #endif
  25193. key_len = (word32)0;
  25194. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  25195. }
  25196. if (ret == BAD_FUNC_ARG) {
  25197. ret = SIG_TYPE_E;
  25198. }
  25199. }
  25200. } else {
  25201. ret = WOLFSSL_FATAL_ERROR;
  25202. }
  25203. wc_FreeRsaKey(&rsa_key);
  25204. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25205. #else
  25206. ret = SIG_TYPE_E;
  25207. #endif
  25208. if (ret == SIG_TYPE_E) {
  25209. ret = 0;
  25210. }else {
  25211. ret = WOLFSSL_FATAL_ERROR;
  25212. }
  25213. printf(resultFmt, ret == 0 ? passed : failed);
  25214. #endif /* NO_SIG_WRAPPER */
  25215. return ret;
  25216. }/* END test_wc_SignatureGetSize_rsa(void) */
  25217. /*----------------------------------------------------------------------------*
  25218. | hash.h Tests
  25219. *----------------------------------------------------------------------------*/
  25220. static int test_wc_HashInit(void)
  25221. {
  25222. int ret = 0, i; /* 0 indicates tests passed, 1 indicates failure */
  25223. wc_HashAlg hash;
  25224. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  25225. enum wc_HashType enumArray[] = {
  25226. #ifndef NO_MD5
  25227. WC_HASH_TYPE_MD5,
  25228. #endif
  25229. #ifndef NO_SHA
  25230. WC_HASH_TYPE_SHA,
  25231. #endif
  25232. #ifndef WOLFSSL_SHA224
  25233. WC_HASH_TYPE_SHA224,
  25234. #endif
  25235. #ifndef NO_SHA256
  25236. WC_HASH_TYPE_SHA256,
  25237. #endif
  25238. #ifndef WOLFSSL_SHA384
  25239. WC_HASH_TYPE_SHA384,
  25240. #endif
  25241. #ifndef WOLFSSL_SHA512
  25242. WC_HASH_TYPE_SHA512,
  25243. #endif
  25244. };
  25245. /* dynamically finds the length */
  25246. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  25247. /* For loop to test various arguments... */
  25248. for (i = 0; i < enumlen; i++) {
  25249. /* check for bad args */
  25250. if (wc_HashInit(&hash, enumArray[i]) == BAD_FUNC_ARG) {
  25251. ret = 1;
  25252. break;
  25253. }
  25254. wc_HashFree(&hash, enumArray[i]);
  25255. /* check for null ptr */
  25256. if (wc_HashInit(NULL, enumArray[i]) != BAD_FUNC_ARG) {
  25257. ret = 1;
  25258. break;
  25259. }
  25260. } /* end of for loop */
  25261. printf(testingFmt, "wc_HashInit()");
  25262. if (ret==0) { /* all tests have passed */
  25263. printf(resultFmt, passed);
  25264. }
  25265. else { /* a test has failed */
  25266. printf(resultFmt, failed);
  25267. }
  25268. return ret;
  25269. } /* end of test_wc_HashInit */
  25270. /*
  25271. * Unit test function for wc_HashSetFlags()
  25272. */
  25273. static int test_wc_HashSetFlags(void)
  25274. {
  25275. int ret = 0;
  25276. #ifdef WOLFSSL_HASH_FLAGS
  25277. wc_HashAlg hash;
  25278. word32 flags = 0;
  25279. int i, j;
  25280. int notSupportedLen;
  25281. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  25282. enum wc_HashType enumArray[] = {
  25283. #ifndef NO_MD5
  25284. WC_HASH_TYPE_MD5,
  25285. #endif
  25286. #ifndef NO_SHA
  25287. WC_HASH_TYPE_SHA,
  25288. #endif
  25289. #ifdef WOLFSSL_SHA224
  25290. WC_HASH_TYPE_SHA224,
  25291. #endif
  25292. #ifndef NO_SHA256
  25293. WC_HASH_TYPE_SHA256,
  25294. #endif
  25295. #ifdef WOLFSSL_SHA384
  25296. WC_HASH_TYPE_SHA384,
  25297. #endif
  25298. #ifdef WOLFSSL_SHA512
  25299. WC_HASH_TYPE_SHA512,
  25300. #endif
  25301. #ifdef WOLFSSL_SHA3
  25302. WC_HASH_TYPE_SHA3_224,
  25303. #endif
  25304. };
  25305. enum wc_HashType notSupported[] = {
  25306. WC_HASH_TYPE_MD5_SHA,
  25307. WC_HASH_TYPE_MD2,
  25308. WC_HASH_TYPE_MD4,
  25309. WC_HASH_TYPE_BLAKE2B,
  25310. WC_HASH_TYPE_BLAKE2S,
  25311. WC_HASH_TYPE_NONE,
  25312. };
  25313. /* dynamically finds the length */
  25314. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  25315. printf(testingFmt, "wc_HashSetFlags()");
  25316. /* For loop to test various arguments... */
  25317. for (i = 0; i < enumlen; i++) {
  25318. ret = wc_HashInit(&hash, enumArray[i]);
  25319. if (ret == 0) {
  25320. ret = wc_HashSetFlags(&hash, enumArray[i], flags);
  25321. }
  25322. if (ret == 0) {
  25323. if (flags & WC_HASH_FLAG_ISCOPY) {
  25324. ret = 0;
  25325. }
  25326. }
  25327. if (ret == 0) {
  25328. ret = wc_HashSetFlags(NULL, enumArray[i], flags);
  25329. if (ret == BAD_FUNC_ARG) {
  25330. ret = 0;
  25331. }
  25332. }
  25333. wc_HashFree(&hash, enumArray[i]);
  25334. }
  25335. /* For loop to test not supported cases */
  25336. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  25337. for (j = 0; ret == 0 && j < notSupportedLen; j++){
  25338. ret = wc_HashInit(&hash, notSupported[j]);
  25339. if (ret == 0) {
  25340. ret = -1;
  25341. }
  25342. else if (ret == BAD_FUNC_ARG){
  25343. ret = wc_HashSetFlags(&hash, notSupported[j], flags);
  25344. if (ret == 0) {
  25345. ret = -1;
  25346. }
  25347. else if (ret == BAD_FUNC_ARG) {
  25348. ret = 0;
  25349. }
  25350. }
  25351. if (ret == 0) {
  25352. ret = wc_HashFree(&hash, notSupported[j]);
  25353. if (ret == 0) {
  25354. ret = -1;
  25355. }
  25356. else if (ret == BAD_FUNC_ARG) {
  25357. ret = 0;
  25358. }
  25359. }
  25360. }
  25361. printf(resultFmt, ret == 0 ? passed : failed);
  25362. #endif
  25363. return ret;
  25364. } /* END test_wc_HashSetFlags */
  25365. /*
  25366. * Unit test function for wc_HashGetFlags()
  25367. */
  25368. static int test_wc_HashGetFlags(void)
  25369. {
  25370. int ret = 0;
  25371. #ifdef WOLFSSL_HASH_FLAGS
  25372. wc_HashAlg hash;
  25373. word32 flags = 0;
  25374. int i, j;
  25375. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  25376. enum wc_HashType enumArray[] = {
  25377. #ifndef NO_MD5
  25378. WC_HASH_TYPE_MD5,
  25379. #endif
  25380. #ifndef NO_SHA
  25381. WC_HASH_TYPE_SHA,
  25382. #endif
  25383. #ifdef WOLFSSL_SHA224
  25384. WC_HASH_TYPE_SHA224,
  25385. #endif
  25386. #ifndef NO_SHA256
  25387. WC_HASH_TYPE_SHA256,
  25388. #endif
  25389. #ifdef WOLFSSL_SHA384
  25390. WC_HASH_TYPE_SHA384,
  25391. #endif
  25392. #ifdef WOLFSSL_SHA512
  25393. WC_HASH_TYPE_SHA512,
  25394. #endif
  25395. #ifdef WOLFSSL_SHA3
  25396. WC_HASH_TYPE_SHA3_224,
  25397. #endif
  25398. };
  25399. enum wc_HashType notSupported[] = {
  25400. WC_HASH_TYPE_MD5_SHA,
  25401. WC_HASH_TYPE_MD2,
  25402. WC_HASH_TYPE_MD4,
  25403. WC_HASH_TYPE_BLAKE2B,
  25404. WC_HASH_TYPE_BLAKE2S,
  25405. WC_HASH_TYPE_NONE,
  25406. };
  25407. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  25408. int notSupportedLen;
  25409. printf(testingFmt, "wc_HashGetFlags()");
  25410. /* For loop to test various arguments... */
  25411. for (i = 0; i < enumlen; i++) {
  25412. ret = wc_HashInit(&hash, enumArray[i]);
  25413. if (ret == 0) {
  25414. ret = wc_HashGetFlags(&hash, enumArray[i], &flags);
  25415. }
  25416. if (ret == 0) {
  25417. if (flags & WC_HASH_FLAG_ISCOPY) {
  25418. ret = 0;
  25419. }
  25420. }
  25421. if (ret == 0) {
  25422. ret = wc_HashGetFlags(NULL, enumArray[i], &flags);
  25423. if (ret == BAD_FUNC_ARG) {
  25424. ret = 0;
  25425. }
  25426. }
  25427. wc_HashFree(&hash, enumArray[i]);
  25428. if (ret != 0) {
  25429. break;
  25430. }
  25431. }
  25432. /* For loop to test not supported cases */
  25433. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  25434. for (j = 0; ret == 0 && j < notSupportedLen; j++){
  25435. ret = wc_HashInit(&hash, notSupported[j]);
  25436. if (ret == 0) {
  25437. ret = -1;
  25438. }
  25439. else if (ret == BAD_FUNC_ARG){
  25440. ret = wc_HashGetFlags(&hash, notSupported[j], &flags);
  25441. if (ret == 0) {
  25442. ret = -1;
  25443. }
  25444. else if (ret == BAD_FUNC_ARG) {
  25445. ret = 0;
  25446. }
  25447. }
  25448. if (ret == 0) {
  25449. ret = wc_HashFree(&hash, notSupported[j]);
  25450. if (ret == 0) {
  25451. ret = -1;
  25452. }
  25453. if (ret == BAD_FUNC_ARG) {
  25454. ret = 0;
  25455. }
  25456. }
  25457. }
  25458. printf(resultFmt, ret == 0 ? passed : failed);
  25459. #endif
  25460. return ret;
  25461. } /* END test_wc_HashGetFlags */
  25462. /*----------------------------------------------------------------------------*
  25463. | Compatibility Tests
  25464. *----------------------------------------------------------------------------*/
  25465. static void test_wolfSSL_lhash(void)
  25466. {
  25467. #ifdef OPENSSL_ALL
  25468. const char testStr[] = "Like a true nature's child\n"
  25469. "We were born\n"
  25470. "Born to be wild";
  25471. printf(testingFmt, "wolfSSL_LH_strhash()");
  25472. AssertIntEQ(lh_strhash(testStr), 0x5b7541dc);
  25473. printf(resultFmt, passed);
  25474. #endif
  25475. }
  25476. static void test_wolfSSL_X509_NAME(void)
  25477. {
  25478. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  25479. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  25480. && !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  25481. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  25482. defined(OPENSSL_EXTRA))
  25483. X509* x509;
  25484. const unsigned char* c;
  25485. unsigned char buf[4096];
  25486. int bytes;
  25487. XFILE f;
  25488. const X509_NAME* a;
  25489. const X509_NAME* b;
  25490. X509_NAME* d2i_name = NULL;
  25491. int sz;
  25492. unsigned char* tmp;
  25493. char file[] = "./certs/ca-cert.der";
  25494. #ifndef OPENSSL_EXTRA_X509_SMALL
  25495. byte empty[] = { /* CN=empty emailAddress= */
  25496. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  25497. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  25498. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  25499. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  25500. 0x01, 0x16, 0x00
  25501. };
  25502. #endif
  25503. printf(testingFmt, "wolfSSL_X509_NAME()");
  25504. #ifndef OPENSSL_EXTRA_X509_SMALL
  25505. /* test compile of deprecated function, returns 0 */
  25506. AssertIntEQ(CRYPTO_thread_id(), 0);
  25507. #endif
  25508. AssertNotNull(a = X509_NAME_new());
  25509. X509_NAME_free((X509_NAME*)a);
  25510. f = XFOPEN(file, "rb");
  25511. AssertTrue(f != XBADFILE);
  25512. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  25513. XFCLOSE(f);
  25514. c = buf;
  25515. AssertNotNull(x509 = wolfSSL_X509_d2i(NULL, c, bytes));
  25516. /* test cmp function */
  25517. AssertNotNull(a = X509_get_issuer_name(x509));
  25518. AssertNotNull(b = X509_get_subject_name(x509));
  25519. #ifndef OPENSSL_EXTRA_X509_SMALL
  25520. AssertIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  25521. #endif
  25522. tmp = buf;
  25523. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  25524. if (sz > 0 && tmp == buf) {
  25525. printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__);
  25526. printf(" Expected pointer to be incremented\n");
  25527. abort();
  25528. }
  25529. #ifndef OPENSSL_EXTRA_X509_SMALL
  25530. tmp = buf;
  25531. AssertNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  25532. #endif
  25533. /* if output parameter is NULL, should still return required size. */
  25534. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  25535. /* retry but with the function creating a buffer */
  25536. tmp = NULL;
  25537. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  25538. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  25539. AssertNotNull(b = X509_NAME_dup((X509_NAME*)a));
  25540. #ifndef OPENSSL_EXTRA_X509_SMALL
  25541. AssertIntEQ(X509_NAME_cmp(a, b), 0);
  25542. #endif
  25543. X509_NAME_free((X509_NAME*)b);
  25544. X509_NAME_free(d2i_name);
  25545. X509_free(x509);
  25546. #ifndef OPENSSL_EXTRA_X509_SMALL
  25547. /* test with an empty domain component */
  25548. tmp = empty;
  25549. sz = sizeof(empty);
  25550. AssertNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  25551. AssertIntEQ(X509_NAME_entry_count(d2i_name), 2);
  25552. /* size of empty emailAddress will be 0 */
  25553. tmp = buf;
  25554. AssertIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  25555. (char*)tmp, sizeof(buf)), 0);
  25556. /* should contain no organization name */
  25557. tmp = buf;
  25558. AssertIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  25559. (char*)tmp, sizeof(buf)), -1);
  25560. X509_NAME_free(d2i_name);
  25561. #endif
  25562. printf(resultFmt, passed);
  25563. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  25564. }
  25565. static void test_wolfSSL_X509_NAME_hash(void)
  25566. {
  25567. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) \
  25568. && !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  25569. BIO* bio;
  25570. X509* x509 = NULL;
  25571. printf(testingFmt, "wolfSSL_X509_NAME_hash");
  25572. AssertNotNull(bio = BIO_new(BIO_s_file()));
  25573. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  25574. AssertNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  25575. AssertIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  25576. AssertIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  25577. X509_free(x509);
  25578. BIO_free(bio);
  25579. printf(resultFmt, passed);
  25580. #endif
  25581. }
  25582. static void test_wolfSSL_X509_NAME_print_ex(void)
  25583. {
  25584. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  25585. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  25586. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  25587. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  25588. !defined(NO_BIO) && !defined(NO_RSA)
  25589. int memSz;
  25590. byte* mem = NULL;
  25591. BIO* bio = NULL;
  25592. BIO* membio = NULL;
  25593. X509* x509 = NULL;
  25594. X509_NAME* name = NULL;
  25595. const char* expNormal = "C=US,CN=wolfssl.com";
  25596. const char* expReverse = "CN=wolfssl.com,C=US";
  25597. const char* expNotEscaped = "C= US,+\"\\ ,CN=#wolfssl.com<>;";
  25598. const char* expNotEscapedRev = "CN=#wolfssl.com<>;,C= US,+\"\\ ";
  25599. const char* expRFC5523 =
  25600. "CN=\\#wolfssl.com\\<\\>\\;,C=\\ US\\,\\+\\\"\\\\\\ ";
  25601. printf(testingFmt, "wolfSSL_X509_NAME_print_ex");
  25602. /* Test with real cert (svrCertFile) first */
  25603. AssertNotNull(bio = BIO_new(BIO_s_file()));
  25604. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  25605. AssertNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  25606. AssertNotNull(name = X509_get_subject_name(x509));
  25607. /* Test without flags */
  25608. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  25609. AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  25610. BIO_free(membio);
  25611. /* Test flag: XN_FLAG_RFC2253 */
  25612. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  25613. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  25614. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  25615. BIO_free(membio);
  25616. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  25617. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  25618. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  25619. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  25620. BIO_free(membio);
  25621. X509_free(x509);
  25622. BIO_free(bio);
  25623. /* Test normal case without escaped characters */
  25624. {
  25625. /* Create name: "/C=US/CN=wolfssl.com" */
  25626. AssertNotNull(name = X509_NAME_new());
  25627. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  25628. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  25629. WOLFSSL_SUCCESS);
  25630. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  25631. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  25632. WOLFSSL_SUCCESS);
  25633. /* Test without flags */
  25634. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  25635. AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  25636. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  25637. AssertIntEQ(memSz, XSTRLEN(expNormal)+1);
  25638. AssertIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  25639. BIO_free(membio);
  25640. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  25641. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  25642. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  25643. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  25644. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  25645. AssertIntEQ(memSz, XSTRLEN(expReverse)+1);
  25646. BIO_free(membio);
  25647. /* Test flags: XN_FLAG_DN_REV - reversed */
  25648. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  25649. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  25650. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  25651. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  25652. AssertIntEQ(memSz, XSTRLEN(expReverse)+1);
  25653. AssertIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  25654. BIO_free(membio);
  25655. X509_NAME_free(name);
  25656. }
  25657. /* Test RFC2253 characters are escaped with backslashes */
  25658. {
  25659. AssertNotNull(name = X509_NAME_new());
  25660. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  25661. /* space at beginning and end, and: ,+"\ */
  25662. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  25663. WOLFSSL_SUCCESS);
  25664. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  25665. /* # at beginning, and: <>;*/
  25666. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  25667. WOLFSSL_SUCCESS);
  25668. /* Test without flags */
  25669. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  25670. AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  25671. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  25672. AssertIntEQ(memSz, XSTRLEN(expNotEscaped)+1);
  25673. AssertIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  25674. XSTRLEN(expNotEscaped)), 0);
  25675. BIO_free(membio);
  25676. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  25677. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  25678. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  25679. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  25680. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  25681. AssertIntEQ(memSz, XSTRLEN(expRFC5523)+1);
  25682. AssertIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  25683. BIO_free(membio);
  25684. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  25685. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  25686. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  25687. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  25688. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  25689. AssertIntEQ(memSz, XSTRLEN(expNotEscapedRev)+1);
  25690. AssertIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  25691. XSTRLEN(expNotEscapedRev)), 0);
  25692. BIO_free(membio);
  25693. X509_NAME_free(name);
  25694. }
  25695. printf(resultFmt, passed);
  25696. #endif
  25697. }
  25698. #ifndef NO_BIO
  25699. static void test_wolfSSL_X509_INFO_multiple_info(void)
  25700. {
  25701. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  25702. STACK_OF(X509_INFO) *info_stack;
  25703. X509_INFO *info;
  25704. int len;
  25705. int i;
  25706. const char* files[] = {
  25707. cliCertFile,
  25708. cliKeyFile,
  25709. /* This needs to be the order as svrCertFile contains the
  25710. * intermediate cert as well. */
  25711. svrKeyFile,
  25712. svrCertFile,
  25713. NULL,
  25714. };
  25715. const char** curFile;
  25716. BIO *fileBIO;
  25717. BIO *concatBIO = NULL;
  25718. byte tmp[FOURK_BUF];
  25719. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  25720. * to group objects together. */
  25721. AssertNotNull(concatBIO = BIO_new(BIO_s_mem()));
  25722. for (curFile = files; *curFile != NULL; curFile++) {
  25723. int fileLen;
  25724. AssertNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  25725. fileLen = wolfSSL_BIO_get_len(fileBIO);
  25726. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  25727. AssertIntEQ(BIO_write(concatBIO, tmp, len), len);
  25728. fileLen -= len;
  25729. }
  25730. /* Make sure we read the entire file */
  25731. AssertIntEQ(fileLen, 0);
  25732. BIO_free(fileBIO);
  25733. }
  25734. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  25735. NULL));
  25736. AssertIntEQ(sk_X509_INFO_num(info_stack), 3);
  25737. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  25738. AssertNotNull(info = sk_X509_INFO_value(info_stack, i));
  25739. AssertNotNull(info->x509);
  25740. AssertNull(info->crl);
  25741. if (i != 0) {
  25742. AssertNotNull(info->x_pkey);
  25743. AssertIntEQ(X509_check_private_key(info->x509,
  25744. info->x_pkey->dec_pkey), 1);
  25745. }
  25746. else {
  25747. AssertNull(info->x_pkey);
  25748. }
  25749. }
  25750. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  25751. BIO_free(concatBIO);
  25752. #endif
  25753. }
  25754. #endif
  25755. #ifndef NO_BIO
  25756. static void test_wolfSSL_X509_INFO(void)
  25757. {
  25758. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  25759. STACK_OF(X509_INFO) *info_stack;
  25760. X509_INFO *info;
  25761. BIO *cert;
  25762. int i;
  25763. /* PEM in hex format to avoid null terminator */
  25764. byte data[] = {
  25765. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  25766. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  25767. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  25768. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  25769. 0x2d, 0x2d, 0x2d
  25770. };
  25771. /* PEM in hex format to avoid null terminator */
  25772. byte data2[] = {
  25773. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  25774. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  25775. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  25776. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  25777. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  25778. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  25779. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  25780. };
  25781. printf(testingFmt, "wolfSSL_X509_INFO");
  25782. AssertNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  25783. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  25784. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  25785. AssertNotNull(info = sk_X509_INFO_value(info_stack, i));
  25786. AssertNotNull(info->x509);
  25787. AssertNull(info->crl);
  25788. AssertNull(info->x_pkey);
  25789. }
  25790. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  25791. BIO_free(cert);
  25792. AssertNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  25793. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  25794. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  25795. BIO_free(cert);
  25796. /* This case should fail due to invalid input. */
  25797. AssertNotNull(cert = BIO_new(BIO_s_mem()));
  25798. AssertIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  25799. AssertNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  25800. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  25801. BIO_free(cert);
  25802. AssertNotNull(cert = BIO_new(BIO_s_mem()));
  25803. AssertIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  25804. AssertNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  25805. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  25806. BIO_free(cert);
  25807. printf(resultFmt, passed);
  25808. #endif
  25809. }
  25810. #endif
  25811. static void test_wolfSSL_X509_subject_name_hash(void)
  25812. {
  25813. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  25814. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  25815. X509* x509;
  25816. X509_NAME* subjectName = NULL;
  25817. unsigned long ret = 0;
  25818. printf(testingFmt, "wolfSSL_X509_subject_name_hash()");
  25819. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  25820. SSL_FILETYPE_PEM));
  25821. AssertNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  25822. ret = X509_subject_name_hash(x509);
  25823. AssertIntNE(ret, 0);
  25824. X509_free(x509);
  25825. printf(resultFmt, passed);
  25826. #endif
  25827. }
  25828. static void test_wolfSSL_X509_issuer_name_hash(void)
  25829. {
  25830. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  25831. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  25832. X509* x509;
  25833. X509_NAME* issuertName = NULL;
  25834. unsigned long ret = 0;
  25835. printf(testingFmt, "wolfSSL_X509_issuer_name_hash()");
  25836. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  25837. SSL_FILETYPE_PEM));
  25838. AssertNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  25839. ret = X509_issuer_name_hash(x509);
  25840. AssertIntNE(ret, 0);
  25841. X509_free(x509);
  25842. printf(resultFmt, passed);
  25843. #endif
  25844. }
  25845. static void test_wolfSSL_X509_check_host(void)
  25846. {
  25847. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  25848. && !defined(NO_SHA) && !defined(NO_RSA)
  25849. X509* x509;
  25850. const char altName[] = "example.com";
  25851. printf(testingFmt, "wolfSSL_X509_check_host()");
  25852. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  25853. SSL_FILETYPE_PEM));
  25854. AssertIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  25855. WOLFSSL_SUCCESS);
  25856. AssertIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  25857. WOLFSSL_FAILURE);
  25858. X509_free(x509);
  25859. AssertIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  25860. WOLFSSL_FAILURE);
  25861. printf(resultFmt, passed);
  25862. #endif
  25863. }
  25864. static void test_wolfSSL_X509_check_email(void)
  25865. {
  25866. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  25867. X509* x509;
  25868. const char goodEmail[] = "info@wolfssl.com";
  25869. const char badEmail[] = "disinfo@wolfssl.com";
  25870. printf(testingFmt, "wolfSSL_X509_check_email()");
  25871. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  25872. SSL_FILETYPE_PEM));
  25873. /* Should fail on non-matching email address */
  25874. AssertIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  25875. WOLFSSL_FAILURE);
  25876. /* Should succeed on matching email address */
  25877. AssertIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  25878. WOLFSSL_SUCCESS);
  25879. /* Should compute length internally when not provided */
  25880. AssertIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  25881. WOLFSSL_SUCCESS);
  25882. /* Should fail when email address is NULL */
  25883. AssertIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  25884. WOLFSSL_FAILURE);
  25885. X509_free(x509);
  25886. /* Should fail when x509 is NULL */
  25887. AssertIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  25888. WOLFSSL_FAILURE);
  25889. printf(resultFmt, passed);
  25890. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  25891. }
  25892. static void test_wolfSSL_DES(void)
  25893. {
  25894. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  25895. const_DES_cblock myDes;
  25896. DES_cblock iv;
  25897. DES_key_schedule key;
  25898. word32 i;
  25899. DES_LONG dl;
  25900. unsigned char msg[] = "hello wolfssl";
  25901. printf(testingFmt, "wolfSSL_DES()");
  25902. DES_check_key(1);
  25903. DES_set_key(&myDes, &key);
  25904. /* check, check of odd parity */
  25905. XMEMSET(myDes, 4, sizeof(const_DES_cblock)); myDes[0] = 6; /*set even parity*/
  25906. XMEMSET(key, 5, sizeof(DES_key_schedule));
  25907. AssertIntEQ(DES_set_key_checked(&myDes, &key), -1);
  25908. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  25909. /* set odd parity for success case */
  25910. DES_set_odd_parity(&myDes);
  25911. AssertIntEQ(DES_check_key_parity(&myDes), 1);
  25912. printf("%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2], myDes[3]);
  25913. AssertIntEQ(DES_set_key_checked(&myDes, &key), 0);
  25914. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  25915. AssertIntEQ(key[i], myDes[i]);
  25916. }
  25917. AssertIntEQ(DES_is_weak_key(&myDes), 0);
  25918. /* check weak key */
  25919. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  25920. XMEMSET(key, 5, sizeof(DES_key_schedule));
  25921. AssertIntEQ(DES_set_key_checked(&myDes, &key), -2);
  25922. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  25923. /* now do unchecked copy of a weak key over */
  25924. DES_set_key_unchecked(&myDes, &key);
  25925. /* compare arrays, should be the same */
  25926. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  25927. AssertIntEQ(key[i], myDes[i]);
  25928. }
  25929. AssertIntEQ(DES_is_weak_key(&myDes), 1);
  25930. /* check DES_key_sched API */
  25931. XMEMSET(key, 1, sizeof(DES_key_schedule));
  25932. AssertIntEQ(DES_key_sched(&myDes, NULL), 0);
  25933. AssertIntEQ(DES_key_sched(NULL, &key), 0);
  25934. AssertIntEQ(DES_key_sched(&myDes, &key), 0);
  25935. /* compare arrays, should be the same */
  25936. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  25937. AssertIntEQ(key[i], myDes[i]);
  25938. }
  25939. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  25940. * DES_cbc_encrypt on the input */
  25941. XMEMSET(iv, 0, sizeof(DES_cblock));
  25942. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  25943. AssertIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  25944. AssertIntEQ(dl, 480052723);
  25945. printf(resultFmt, passed);
  25946. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  25947. }
  25948. static void test_wc_PemToDer(void)
  25949. {
  25950. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER)
  25951. int ret;
  25952. DerBuffer* pDer = NULL;
  25953. const char* ca_cert = "./certs/server-cert.pem";
  25954. byte* cert_buf = NULL;
  25955. size_t cert_sz = 0;
  25956. int eccKey = 0;
  25957. EncryptedInfo info;
  25958. printf(testingFmt, "wc_PemToDer()");
  25959. XMEMSET(&info, 0, sizeof(info));
  25960. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  25961. if (ret == 0) {
  25962. ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE,
  25963. &pDer, NULL, &info, &eccKey);
  25964. AssertIntEQ(ret, 0);
  25965. wc_FreeDer(&pDer);
  25966. }
  25967. if (cert_buf)
  25968. free(cert_buf);
  25969. #ifdef HAVE_ECC
  25970. {
  25971. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  25972. byte key_buf[256] = {0};
  25973. /* Test fail of loading a key with cert type */
  25974. AssertIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  25975. key_buf[0] = '\n';
  25976. XMEMCPY(key_buf + 1, cert_buf, cert_sz);
  25977. AssertIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  25978. &pDer, NULL, &info, &eccKey)), 0);
  25979. #ifdef OPENSSL_EXTRA
  25980. AssertIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  25981. &pDer, NULL, &info, &eccKey)), 0);
  25982. #endif
  25983. wc_FreeDer(&pDer);
  25984. if (cert_buf)
  25985. free(cert_buf);
  25986. }
  25987. #endif
  25988. printf(resultFmt, passed);
  25989. #endif
  25990. }
  25991. static void test_wc_AllocDer(void)
  25992. {
  25993. #if !defined(NO_CERTS)
  25994. int ret;
  25995. DerBuffer* pDer = NULL;
  25996. word32 testSize = 1024;
  25997. printf(testingFmt, "wc_AllocDer()");
  25998. ret = wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT);
  25999. AssertIntEQ(ret, 0);
  26000. AssertNotNull(pDer);
  26001. wc_FreeDer(&pDer);
  26002. printf(resultFmt, passed);
  26003. #endif
  26004. }
  26005. static void test_wc_CertPemToDer(void)
  26006. {
  26007. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER)
  26008. int ret;
  26009. const char* ca_cert = "./certs/ca-cert.pem";
  26010. byte* cert_buf = NULL;
  26011. size_t cert_sz = 0, cert_dersz = 0;
  26012. byte* cert_der = NULL;
  26013. printf(testingFmt, "wc_CertPemToDer()");
  26014. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  26015. if (ret == 0) {
  26016. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  26017. cert_der = (byte*)malloc(cert_dersz);
  26018. if (cert_der) {
  26019. ret = wc_CertPemToDer(cert_buf, (int)cert_sz,
  26020. cert_der, (int)cert_dersz, CERT_TYPE);
  26021. AssertIntGE(ret, 0);
  26022. }
  26023. }
  26024. if (cert_der)
  26025. free(cert_der);
  26026. if (cert_buf)
  26027. free(cert_buf);
  26028. printf(resultFmt, passed);
  26029. #endif
  26030. }
  26031. static void test_wc_PubKeyPemToDer(void)
  26032. {
  26033. #ifdef WOLFSSL_PEM_TO_DER
  26034. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER)
  26035. int ret;
  26036. const char* key = "./certs/ecc-client-keyPub.pem";
  26037. byte* cert_buf = NULL;
  26038. size_t cert_sz = 0, cert_dersz = 0;
  26039. byte* cert_der = NULL;
  26040. printf(testingFmt, "wc_PubKeyPemToDer()");
  26041. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  26042. cert_der, (int)cert_dersz);
  26043. AssertIntGE(ret, BAD_FUNC_ARG);
  26044. ret = load_file(key, &cert_buf, &cert_sz);
  26045. if (ret == 0) {
  26046. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  26047. cert_der = (byte*)malloc(cert_dersz);
  26048. if (cert_der) {
  26049. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  26050. cert_der, (int)cert_dersz);
  26051. AssertIntGE(ret, 0);
  26052. }
  26053. }
  26054. if (cert_der)
  26055. free(cert_der);
  26056. if (cert_buf)
  26057. free(cert_buf);
  26058. printf(resultFmt, passed);
  26059. #endif
  26060. #endif
  26061. }
  26062. static void test_wc_PemPubKeyToDer(void)
  26063. {
  26064. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER)
  26065. int ret;
  26066. const char* key = "./certs/ecc-client-keyPub.pem";
  26067. size_t cert_dersz = 1024;
  26068. byte* cert_der = (byte*)malloc(cert_dersz);
  26069. printf(testingFmt, "wc_PemPubKeyToDer()");
  26070. ret = wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz);
  26071. AssertIntGE(ret, BAD_FUNC_ARG);
  26072. if (cert_der) {
  26073. ret = wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz);
  26074. AssertIntGE(ret, 0);
  26075. free(cert_der);
  26076. }
  26077. printf(resultFmt, passed);
  26078. #endif
  26079. }
  26080. static void test_wc_GetPubKeyDerFromCert(void)
  26081. {
  26082. #if !defined(NO_RSA) || defined(HAVE_ECC)
  26083. int ret;
  26084. word32 idx = 0;
  26085. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  26086. word32 keyDerSz = (word32)sizeof(keyDer);
  26087. DecodedCert decoded;
  26088. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ)
  26089. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  26090. word32 certBufSz = sizeof(certBuf);
  26091. #endif
  26092. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  26093. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA)
  26094. XFILE fp;
  26095. #endif
  26096. #ifndef NO_RSA
  26097. RsaKey rsaKey;
  26098. #if defined(USE_CERT_BUFFERS_2048)
  26099. byte* rsaCertDer = (byte*)client_cert_der_2048;
  26100. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  26101. #elif defined(USE_CERT_BUFFERS_1024)
  26102. byte* rsaCertDer = (byte*)client_cert_der_1024;
  26103. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  26104. #else
  26105. unsigned char rsaCertDer[TWOK_BUF];
  26106. word32 rsaCertDerSz;
  26107. #endif
  26108. #endif
  26109. #ifdef HAVE_ECC
  26110. ecc_key eccKey;
  26111. #if defined(USE_CERT_BUFFERS_256)
  26112. byte* eccCert = (byte*)cliecc_cert_der_256;
  26113. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  26114. #else
  26115. unsigned char eccCert[ONEK_BUF];
  26116. word32 eccCertSz;
  26117. XFILE fp2;
  26118. #endif
  26119. #endif
  26120. printf(testingFmt, "wc_GetPubKeyDerFromCert()");
  26121. #ifndef NO_RSA
  26122. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  26123. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  26124. AssertTrue((fp != XBADFILE));
  26125. rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer), fp);
  26126. XFCLOSE(fp);
  26127. #endif
  26128. /* good test case - RSA DER cert */
  26129. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  26130. ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
  26131. AssertIntEQ(ret, 0);
  26132. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  26133. AssertIntEQ(ret, 0);
  26134. AssertIntGT(keyDerSz, 0);
  26135. /* sanity check, verify we can import DER public key */
  26136. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  26137. AssertIntEQ(ret, 0);
  26138. ret = wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz);
  26139. AssertIntEQ(ret, 0);
  26140. wc_FreeRsaKey(&rsaKey);
  26141. /* test LENGTH_ONLY_E case */
  26142. keyDerSz = 0;
  26143. ret = wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz);
  26144. AssertIntEQ(ret, LENGTH_ONLY_E);
  26145. AssertIntGT(keyDerSz, 0);
  26146. /* bad args: DecodedCert NULL */
  26147. ret = wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz);
  26148. AssertIntEQ(ret, BAD_FUNC_ARG);
  26149. /* bad args: output key buff size */
  26150. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL);
  26151. AssertIntEQ(ret, BAD_FUNC_ARG);
  26152. /* bad args: zero size output key buffer */
  26153. keyDerSz = 0;
  26154. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  26155. AssertIntEQ(ret, BAD_FUNC_ARG);
  26156. wc_FreeDecodedCert(&decoded);
  26157. /* Certificate Request Tests */
  26158. #ifdef WOLFSSL_CERT_REQ
  26159. {
  26160. XMEMSET(certBuf, 0, sizeof(certBuf));
  26161. fp = XFOPEN("./certs/csr.signed.der", "rb");
  26162. AssertTrue((fp != XBADFILE));
  26163. certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp);
  26164. XFCLOSE(fp);
  26165. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  26166. ret = wc_ParseCert(&decoded, CERTREQ_TYPE, NO_VERIFY, NULL);
  26167. AssertIntEQ(ret, 0);
  26168. /* good test case - RSA DER certificate request */
  26169. keyDerSz = sizeof(keyDer);
  26170. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  26171. AssertIntEQ(ret, 0);
  26172. AssertIntGT(keyDerSz, 0);
  26173. /* sanity check, verify we can import DER public key */
  26174. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  26175. AssertIntEQ(ret, 0);
  26176. idx = 0;
  26177. ret = wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz);
  26178. AssertIntEQ(ret, 0);
  26179. wc_FreeRsaKey(&rsaKey);
  26180. wc_FreeDecodedCert(&decoded);
  26181. }
  26182. #endif /* WOLFSSL_CERT_REQ */
  26183. #endif /* NO_RSA */
  26184. #ifdef HAVE_ECC
  26185. #ifndef USE_CERT_BUFFERS_256
  26186. fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb");
  26187. AssertTrue((fp2 != XBADFILE));
  26188. eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2);
  26189. XFCLOSE(fp2);
  26190. #endif
  26191. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  26192. ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
  26193. AssertIntEQ(ret, 0);
  26194. /* good test case - ECC */
  26195. XMEMSET(keyDer, 0, sizeof(keyDer));
  26196. keyDerSz = sizeof(keyDer);
  26197. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  26198. AssertIntEQ(ret, 0);
  26199. AssertIntGT(keyDerSz, 0);
  26200. /* sanity check, verify we can import DER public key */
  26201. ret = wc_ecc_init(&eccKey);
  26202. AssertIntEQ(ret, 0);
  26203. idx = 0; /* reset idx to 0, used above in RSA case */
  26204. ret = wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz);
  26205. AssertIntEQ(ret, 0);
  26206. wc_ecc_free(&eccKey);
  26207. /* test LENGTH_ONLY_E case */
  26208. keyDerSz = 0;
  26209. ret = wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz);
  26210. AssertIntEQ(ret, LENGTH_ONLY_E);
  26211. AssertIntGT(keyDerSz, 0);
  26212. wc_FreeDecodedCert(&decoded);
  26213. #endif
  26214. printf(resultFmt, passed);
  26215. #endif /* !NO_RSA || HAVE_ECC */
  26216. }
  26217. static void test_wc_CheckCertSigPubKey(void)
  26218. {
  26219. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  26220. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  26221. int ret;
  26222. const char* ca_cert = "./certs/ca-cert.pem";
  26223. byte* cert_buf = NULL;
  26224. size_t cert_sz = 0;
  26225. byte* cert_der = NULL;
  26226. word32 cert_dersz = 0;
  26227. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  26228. word32 keyDerSz = (word32)sizeof(keyDer);
  26229. DecodedCert decoded;
  26230. printf(testingFmt, "wc_CheckCertSigPubKey()");
  26231. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  26232. if (ret == 0) {
  26233. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  26234. cert_der = (byte*)malloc(cert_dersz);
  26235. if (cert_der) {
  26236. ret = wc_CertPemToDer(cert_buf, (int)cert_sz,
  26237. cert_der, (int)cert_dersz, CERT_TYPE);
  26238. AssertIntGE(ret, 0);
  26239. }
  26240. }
  26241. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  26242. ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
  26243. AssertIntEQ(ret, 0);
  26244. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  26245. AssertIntEQ(ret, 0);
  26246. AssertIntGT(keyDerSz, 0);
  26247. /* Good test case. */
  26248. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, keyDerSz,
  26249. RSAk);
  26250. AssertIntEQ(ret, 0);
  26251. /* No certificate. */
  26252. ret = wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  26253. ECDSAk);
  26254. AssertIntEQ(ret, BAD_FUNC_ARG);
  26255. /* Bad cert size. */
  26256. ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  26257. RSAk);
  26258. AssertTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  26259. /* No public key. */
  26260. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL, keyDerSz,
  26261. RSAk);
  26262. AssertIntEQ(ret, ASN_NO_SIGNER_E);
  26263. /* Bad public key size. */
  26264. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  26265. RSAk);
  26266. AssertIntEQ(ret, BAD_FUNC_ARG);
  26267. /* Wrong aglo. */
  26268. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, keyDerSz,
  26269. ECDSAk);
  26270. AssertIntEQ(ret, ASN_PARSE_E);
  26271. wc_FreeDecodedCert(&decoded);
  26272. if (cert_der)
  26273. free(cert_der);
  26274. if (cert_buf)
  26275. free(cert_buf);
  26276. printf(resultFmt, passed);
  26277. #endif
  26278. }
  26279. static void test_wolfSSL_certs(void)
  26280. {
  26281. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  26282. !defined(NO_RSA)
  26283. X509* x509ext;
  26284. #ifdef OPENSSL_ALL
  26285. X509* x509;
  26286. WOLFSSL_X509_EXTENSION* ext;
  26287. ASN1_OBJECT* obj;
  26288. #endif
  26289. WOLFSSL* ssl;
  26290. WOLFSSL_CTX* ctx;
  26291. STACK_OF(ASN1_OBJECT)* sk;
  26292. ASN1_STRING* asn1_str;
  26293. AUTHORITY_KEYID* akey;
  26294. BASIC_CONSTRAINTS* bc;
  26295. int crit;
  26296. printf(testingFmt, "wolfSSL_certs()");
  26297. #ifndef NO_WOLFSSL_SERVER
  26298. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  26299. #else
  26300. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  26301. #endif
  26302. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  26303. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  26304. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  26305. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26306. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  26307. #endif
  26308. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  26309. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26310. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  26311. #endif
  26312. AssertNotNull(ssl = SSL_new(ctx));
  26313. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26314. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  26315. #endif
  26316. #ifdef HAVE_PK_CALLBACKS
  26317. AssertIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  26318. #endif /* HAVE_PK_CALLBACKS */
  26319. /* create and use x509 */
  26320. #ifdef OPENSSL_ALL
  26321. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  26322. AssertNotNull(x509);
  26323. #endif
  26324. x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt, WOLFSSL_FILETYPE_PEM);
  26325. AssertNotNull(x509ext);
  26326. AssertIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  26327. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26328. /* with loading in a new cert the check on private key should now fail */
  26329. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  26330. #endif
  26331. #if defined(USE_CERT_BUFFERS_2048)
  26332. AssertIntEQ(SSL_use_certificate_ASN1(ssl,
  26333. (unsigned char*)server_cert_der_2048,
  26334. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  26335. #endif
  26336. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  26337. /************* Get Digest of Certificate ******************/
  26338. {
  26339. byte digest[64]; /* max digest size */
  26340. word32 digestSz;
  26341. XMEMSET(digest, 0, sizeof(digest));
  26342. AssertIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  26343. WOLFSSL_SUCCESS);
  26344. AssertIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  26345. WOLFSSL_SUCCESS);
  26346. AssertIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  26347. WOLFSSL_FAILURE);
  26348. }
  26349. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  26350. /* test and checkout X509 extensions */
  26351. bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext, NID_basic_constraints,
  26352. &crit, NULL);
  26353. AssertNotNull(bc);
  26354. AssertIntEQ(crit, 0);
  26355. #ifdef OPENSSL_ALL
  26356. ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc);
  26357. AssertNotNull(ext);
  26358. X509_EXTENSION_free(ext);
  26359. AssertNotNull(ext = X509_EXTENSION_new());
  26360. X509_EXTENSION_set_critical(ext, 1);
  26361. AssertNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  26362. AssertIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  26363. ASN1_OBJECT_free(obj);
  26364. X509_EXTENSION_free(ext);
  26365. AssertNotNull(ext = X509_EXTENSION_new());
  26366. X509_EXTENSION_set_critical(ext, 0);
  26367. AssertIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  26368. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  26369. NULL);
  26370. AssertIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  26371. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  26372. * and X509_get_ext_d2i has created new */
  26373. X509_EXTENSION_free(ext);
  26374. #endif
  26375. BASIC_CONSTRAINTS_free(bc);
  26376. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit, NULL);
  26377. AssertNotNull(asn1_str);
  26378. AssertIntEQ(crit, 1);
  26379. AssertIntEQ(asn1_str->type, NID_key_usage);
  26380. #ifdef OPENSSL_ALL
  26381. ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str);
  26382. AssertNotNull(ext);
  26383. X509_EXTENSION_free(ext);
  26384. #endif
  26385. ASN1_STRING_free(asn1_str);
  26386. #ifdef OPENSSL_ALL
  26387. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_ext_key_usage,
  26388. &crit, NULL);
  26389. AssertNotNull(sk);
  26390. ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk);
  26391. AssertNotNull(ext);
  26392. X509_EXTENSION_free(ext);
  26393. sk_ASN1_OBJECT_pop_free(sk, NULL);
  26394. #else
  26395. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  26396. &crit, NULL);
  26397. AssertNull(sk);
  26398. #endif
  26399. akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  26400. NID_authority_key_identifier, &crit, NULL);
  26401. AssertNotNull(akey);
  26402. #ifdef OPENSSL_ALL
  26403. ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit, akey);
  26404. AssertNotNull(ext);
  26405. X509_EXTENSION_free(ext);
  26406. #endif
  26407. wolfSSL_AUTHORITY_KEYID_free(akey);
  26408. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  26409. NID_private_key_usage_period, &crit, NULL);
  26410. /* AssertNotNull(sk); NID not yet supported */
  26411. AssertIntEQ(crit, -1);
  26412. sk_ASN1_OBJECT_free(sk);
  26413. sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext, NID_subject_alt_name,
  26414. &crit, NULL);
  26415. /* AssertNotNull(sk); no alt names set */
  26416. sk_GENERAL_NAME_free(sk);
  26417. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_issuer_alt_name,
  26418. &crit, NULL);
  26419. /* AssertNotNull(sk); NID not yet supported */
  26420. AssertIntEQ(crit, -1);
  26421. sk_ASN1_OBJECT_free(sk);
  26422. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_info_access, &crit,
  26423. NULL);
  26424. /* AssertNotNull(sk); no auth info set */
  26425. sk_ASN1_OBJECT_free(sk);
  26426. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_sinfo_access,
  26427. &crit, NULL);
  26428. /* AssertNotNull(sk); NID not yet supported */
  26429. AssertIntEQ(crit, -1);
  26430. sk_ASN1_OBJECT_free(sk);
  26431. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_name_constraints,
  26432. &crit, NULL);
  26433. /* AssertNotNull(sk); NID not yet supported */
  26434. AssertIntEQ(crit, -1);
  26435. sk_ASN1_OBJECT_free(sk);
  26436. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  26437. NID_certificate_policies, &crit, NULL);
  26438. #if !defined(WOLFSSL_SEP) && !defined(WOLFSSL_CERT_EXT)
  26439. AssertNull(sk);
  26440. #else
  26441. /* AssertNotNull(sk); no cert policy set */
  26442. #endif
  26443. sk_ASN1_OBJECT_free(sk);
  26444. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_policy_mappings,
  26445. &crit, NULL);
  26446. /* AssertNotNull(sk); NID not yet supported */
  26447. AssertIntEQ(crit, -1);
  26448. sk_ASN1_OBJECT_free(sk);
  26449. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_policy_constraints,
  26450. &crit, NULL);
  26451. /* AssertNotNull(sk); NID not yet supported */
  26452. AssertIntEQ(crit, -1);
  26453. sk_ASN1_OBJECT_free(sk);
  26454. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_inhibit_any_policy,
  26455. &crit, NULL);
  26456. /* AssertNotNull(sk); NID not yet supported */
  26457. AssertIntEQ(crit, -1);
  26458. sk_ASN1_OBJECT_free(sk);
  26459. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_tlsfeature, &crit,
  26460. NULL);
  26461. /* AssertNotNull(sk); NID not yet supported */
  26462. AssertIntEQ(crit, -1);
  26463. sk_ASN1_OBJECT_free(sk);
  26464. /* test invalid cases */
  26465. crit = 0;
  26466. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit, NULL);
  26467. AssertNull(sk);
  26468. AssertIntEQ(crit, -1);
  26469. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL, NID_tlsfeature,
  26470. NULL, NULL);
  26471. AssertNull(sk);
  26472. AssertIntEQ(SSL_get_hit(ssl), 0);
  26473. #ifdef OPENSSL_ALL
  26474. X509_free(x509);
  26475. #endif
  26476. X509_free(x509ext);
  26477. SSL_free(ssl);
  26478. SSL_CTX_free(ctx);
  26479. printf(resultFmt, passed);
  26480. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  26481. }
  26482. static void test_wolfSSL_X509_check_private_key(void)
  26483. {
  26484. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  26485. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  26486. X509* x509;
  26487. EVP_PKEY* pkey = NULL;
  26488. const byte* key;
  26489. printf(testingFmt, "wolfSSL_X509_check_private_key()");
  26490. /* Check with correct key */
  26491. AssertNotNull((x509 = X509_load_certificate_file(cliCertFile,
  26492. SSL_FILETYPE_PEM)));
  26493. key = client_key_der_2048;
  26494. AssertNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  26495. &key, (long)sizeof_client_key_der_2048));
  26496. AssertIntEQ(X509_check_private_key(x509, pkey), 1);
  26497. EVP_PKEY_free(pkey);
  26498. pkey = NULL;
  26499. /* Check with wrong key */
  26500. key = server_key_der_2048;
  26501. AssertNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  26502. &key, (long)sizeof_server_key_der_2048));
  26503. AssertIntEQ(X509_check_private_key(x509, pkey), 0);
  26504. /* test for incorrect parameter */
  26505. AssertIntEQ(X509_check_private_key(NULL, pkey), 0);
  26506. AssertIntEQ(X509_check_private_key(x509, NULL), 0);
  26507. AssertIntEQ(X509_check_private_key(NULL, NULL), 0);
  26508. EVP_PKEY_free(pkey);
  26509. X509_free(x509);
  26510. printf(resultFmt, passed);
  26511. #endif
  26512. }
  26513. static void test_wolfSSL_ASN1_TIME_print(void)
  26514. {
  26515. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) \
  26516. && (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  26517. defined(WOLFSSL_HAPROXY)) && defined(USE_CERT_BUFFERS_2048) && \
  26518. !defined(NO_BIO)
  26519. BIO* bio;
  26520. X509* x509;
  26521. const unsigned char* der = client_cert_der_2048;
  26522. ASN1_TIME* t;
  26523. unsigned char buf[25];
  26524. printf(testingFmt, "wolfSSL_ASN1_TIME_print()");
  26525. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  26526. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  26527. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  26528. AssertIntEQ(ASN1_TIME_print(bio, X509_get_notBefore(x509)), 1);
  26529. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  26530. AssertIntEQ(XMEMCMP(buf, "Feb 15 12:50:24 2022 GMT", sizeof(buf) - 1), 0);
  26531. /* create a bad time and test results */
  26532. AssertNotNull(t = X509_get_notAfter(x509));
  26533. AssertIntEQ(ASN1_TIME_check(t), WOLFSSL_SUCCESS);
  26534. t->data[8] = 0;
  26535. t->data[3] = 0;
  26536. AssertIntNE(ASN1_TIME_print(bio, t), 1);
  26537. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  26538. AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  26539. AssertIntEQ(ASN1_TIME_check(t), WOLFSSL_FAILURE);
  26540. BIO_free(bio);
  26541. X509_free(x509);
  26542. printf(resultFmt, passed);
  26543. #endif
  26544. }
  26545. static void test_wolfSSL_ASN1_UTCTIME_print(void)
  26546. {
  26547. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  26548. BIO* bio;
  26549. ASN1_UTCTIME* utc = NULL;
  26550. unsigned char buf[25];
  26551. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  26552. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  26553. printf(testingFmt, "ASN1_UTCTIME_print()");
  26554. /* NULL parameter check */
  26555. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  26556. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  26557. BIO_free(bio);
  26558. /* Valid date */
  26559. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  26560. AssertNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  26561. DYNAMIC_TYPE_ASN1));
  26562. utc->type = ASN_UTC_TIME;
  26563. utc->length = ASN_UTC_TIME_SIZE;
  26564. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  26565. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  26566. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  26567. AssertIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  26568. XMEMSET(buf, 0, sizeof(buf));
  26569. BIO_free(bio);
  26570. /* Invalid format */
  26571. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  26572. utc->type = ASN_UTC_TIME;
  26573. utc->length = ASN_UTC_TIME_SIZE;
  26574. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  26575. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  26576. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  26577. AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  26578. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  26579. BIO_free(bio);
  26580. printf(resultFmt, passed);
  26581. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  26582. }
  26583. static void test_wolfSSL_ASN1_TIME_diff_compare(void)
  26584. {
  26585. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  26586. ASN1_TIME* fromTime;
  26587. ASN1_TIME* toTime;
  26588. int daysDiff;
  26589. int secsDiff;
  26590. printf(testingFmt, "test_wolfSSL_ASN1_TIME_diff");
  26591. AssertNotNull((fromTime = ASN1_TIME_new()));
  26592. /* Feb 22, 2003, 21:15:15 */
  26593. AssertIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), WOLFSSL_SUCCESS);
  26594. AssertNotNull((toTime = ASN1_TIME_new()));
  26595. /* Dec 19, 2010, 18:10:11 */
  26596. AssertIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), WOLFSSL_SUCCESS);
  26597. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), WOLFSSL_SUCCESS);
  26598. /* Error conditions. */
  26599. AssertIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime),
  26600. WOLFSSL_FAILURE);
  26601. AssertIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime),
  26602. WOLFSSL_FAILURE);
  26603. /* If both times are NULL, difference is 0. */
  26604. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL),
  26605. WOLFSSL_SUCCESS);
  26606. AssertIntEQ(daysDiff, 0);
  26607. AssertIntEQ(secsDiff, 0);
  26608. /* If one time is NULL, it defaults to the current time. */
  26609. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime),
  26610. WOLFSSL_SUCCESS);
  26611. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL),
  26612. WOLFSSL_SUCCESS);
  26613. /* Normal operation. Both times non-NULL. */
  26614. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime),
  26615. WOLFSSL_SUCCESS);
  26616. AssertIntEQ(daysDiff, 2856);
  26617. AssertIntEQ(secsDiff, 75296);
  26618. /* Swapping the times should return negative values. */
  26619. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime),
  26620. WOLFSSL_SUCCESS);
  26621. AssertIntEQ(daysDiff, -2856);
  26622. AssertIntEQ(secsDiff, -75296);
  26623. AssertIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  26624. AssertIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  26625. AssertIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  26626. /* Compare regression test: No seconds difference, just difference in days.
  26627. */
  26628. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  26629. ASN1_TIME_set_string(toTime, "19800101000000Z");
  26630. AssertIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  26631. AssertIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  26632. AssertIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  26633. /* Edge case with Unix epoch. */
  26634. AssertNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  26635. AssertNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  26636. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime),
  26637. WOLFSSL_SUCCESS);
  26638. AssertIntEQ(daysDiff, 3652);
  26639. AssertIntEQ(secsDiff, 0);
  26640. /* Edge case with year > 2038 (year 2038 problem). */
  26641. AssertNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  26642. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime),
  26643. WOLFSSL_SUCCESS);
  26644. AssertIntEQ(daysDiff, 2932896);
  26645. AssertIntEQ(secsDiff, 86399);
  26646. ASN1_TIME_free(fromTime);
  26647. ASN1_TIME_free(toTime);
  26648. printf(resultFmt, passed);
  26649. #endif
  26650. }
  26651. static void test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  26652. {
  26653. #if defined(OPENSSL_EXTRA)
  26654. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime;
  26655. unsigned char nullstr[32];
  26656. printf(testingFmt, "test_wolfSSL_ASN1_GENERALIZEDTIME_free");
  26657. XMEMSET(nullstr, 0, 32);
  26658. asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  26659. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL,
  26660. DYNAMIC_TYPE_TMP_BUFFER);
  26661. if (asn1_gtime) {
  26662. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  26663. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  26664. AssertIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  26665. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26666. }
  26667. printf(resultFmt, passed);
  26668. #endif /* OPENSSL_EXTRA */
  26669. }
  26670. static void test_wolfSSL_private_keys(void)
  26671. {
  26672. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  26673. !defined(NO_FILESYSTEM)
  26674. WOLFSSL* ssl;
  26675. WOLFSSL_CTX* ctx;
  26676. EVP_PKEY* pkey = NULL;
  26677. printf(testingFmt, "wolfSSL_private_keys()");
  26678. OpenSSL_add_all_digests();
  26679. OpenSSL_add_all_algorithms();
  26680. #ifndef NO_RSA
  26681. #ifndef NO_WOLFSSL_SERVER
  26682. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  26683. #else
  26684. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  26685. #endif
  26686. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  26687. /* Have to load a cert before you can check the private key against that
  26688. * certificates public key! */
  26689. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26690. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  26691. #endif
  26692. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  26693. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26694. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  26695. #endif
  26696. AssertNotNull(ssl = SSL_new(ctx));
  26697. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26698. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  26699. #endif
  26700. #ifdef USE_CERT_BUFFERS_2048
  26701. {
  26702. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  26703. unsigned char buf[FOURK_BUF];
  26704. word32 bufSz;
  26705. AssertIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  26706. (unsigned char*)client_key_der_2048,
  26707. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  26708. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26709. /* Should mismatch now that a different private key loaded */
  26710. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  26711. #endif
  26712. AssertIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  26713. (unsigned char*)server_key,
  26714. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  26715. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26716. /* After loading back in DER format of original key, should match */
  26717. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  26718. #endif
  26719. /* test loading private key to the WOLFSSL_CTX */
  26720. AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  26721. (unsigned char*)client_key_der_2048,
  26722. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  26723. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26724. /* Should mismatch now that a different private key loaded */
  26725. AssertIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  26726. #endif
  26727. AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  26728. (unsigned char*)server_key,
  26729. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  26730. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26731. /* After loading back in DER format of original key, should match */
  26732. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  26733. #endif
  26734. /* pkey not set yet, expecting to fail */
  26735. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  26736. /* set PKEY and test again */
  26737. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  26738. &server_key, (long)sizeof_server_key_der_2048));
  26739. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  26740. /* reuse PKEY structure and test
  26741. * this should be checked with a memory management sanity checker */
  26742. AssertFalse(server_key == (const unsigned char*)server_key_der_2048);
  26743. server_key = (const unsigned char*)server_key_der_2048;
  26744. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  26745. &server_key, (long)sizeof_server_key_der_2048));
  26746. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  26747. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  26748. bufSz = FOURK_BUF;
  26749. AssertIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  26750. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  26751. RSAk, NULL, 0)), 0);
  26752. server_key = (const unsigned char*)buf;
  26753. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  26754. (long)bufSz));
  26755. }
  26756. #endif
  26757. EVP_PKEY_free(pkey);
  26758. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  26759. SSL_CTX_free(ctx);
  26760. #endif /* end of RSA private key match tests */
  26761. #ifdef HAVE_ECC
  26762. #ifndef NO_WOLFSSL_SERVER
  26763. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  26764. #else
  26765. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  26766. #endif
  26767. AssertTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  26768. WOLFSSL_FILETYPE_PEM));
  26769. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  26770. WOLFSSL_FILETYPE_PEM));
  26771. AssertNotNull(ssl = SSL_new(ctx));
  26772. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26773. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  26774. #endif
  26775. SSL_free(ssl);
  26776. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  26777. WOLFSSL_FILETYPE_PEM));
  26778. AssertNotNull(ssl = SSL_new(ctx));
  26779. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  26780. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  26781. #endif
  26782. SSL_free(ssl);
  26783. SSL_CTX_free(ctx);
  26784. #endif /* end of ECC private key match tests */
  26785. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  26786. #ifndef NO_WOLFSSL_SERVER
  26787. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  26788. #else
  26789. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  26790. #endif
  26791. AssertTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  26792. WOLFSSL_FILETYPE_PEM));
  26793. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  26794. WOLFSSL_FILETYPE_PEM));
  26795. AssertNotNull(ssl = SSL_new(ctx));
  26796. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26797. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  26798. #endif
  26799. SSL_free(ssl);
  26800. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  26801. WOLFSSL_FILETYPE_PEM));
  26802. AssertNotNull(ssl = SSL_new(ctx));
  26803. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26804. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  26805. #endif
  26806. SSL_free(ssl);
  26807. SSL_CTX_free(ctx);
  26808. #endif /* end of Ed25519 private key match tests */
  26809. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  26810. #ifndef NO_WOLFSSL_SERVER
  26811. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  26812. #else
  26813. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  26814. #endif
  26815. AssertTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  26816. WOLFSSL_FILETYPE_PEM));
  26817. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  26818. WOLFSSL_FILETYPE_PEM));
  26819. AssertNotNull(ssl = SSL_new(ctx));
  26820. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26821. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  26822. #endif
  26823. SSL_free(ssl);
  26824. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  26825. WOLFSSL_FILETYPE_PEM));
  26826. AssertNotNull(ssl = SSL_new(ctx));
  26827. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26828. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  26829. #endif
  26830. SSL_free(ssl);
  26831. SSL_CTX_free(ctx);
  26832. #endif /* end of Ed448 private key match tests */
  26833. EVP_cleanup();
  26834. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  26835. CONF_modules_free();
  26836. ENGINE_cleanup();
  26837. CONF_modules_unload();
  26838. (void)ssl;
  26839. (void)ctx;
  26840. (void)pkey;
  26841. printf(resultFmt, passed);
  26842. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  26843. }
  26844. static void test_wolfSSL_PEM_read_PrivateKey(void)
  26845. {
  26846. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  26847. && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  26848. XFILE file;
  26849. const char* fname = "./certs/server-key.pem";
  26850. EVP_PKEY* pkey;
  26851. RSA* rsa;
  26852. WOLFSSL_EVP_PKEY_CTX* ctx;
  26853. unsigned char* sig;
  26854. size_t sigLen;
  26855. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  26856. size_t tbsLen = sizeof(tbs);
  26857. printf(testingFmt, "test_wolfSSL_PEM_read_PrivateKey()");
  26858. /* Check error case. */
  26859. AssertNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  26860. /* Read in an RSA key. */
  26861. file = XFOPEN(fname, "rb");
  26862. AssertTrue(file != XBADFILE);
  26863. AssertNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  26864. XFCLOSE(file);
  26865. /* Make sure the key is usable by signing some data with it. */
  26866. AssertNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  26867. AssertIntGT((sigLen = RSA_size(rsa)), 0);
  26868. AssertNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  26869. DYNAMIC_TYPE_TMP_BUFFER));
  26870. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  26871. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  26872. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  26873. WOLFSSL_SUCCESS);
  26874. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26875. EVP_PKEY_CTX_free(ctx);
  26876. EVP_PKEY_free(pkey);
  26877. printf(resultFmt, passed);
  26878. #endif
  26879. }
  26880. static void test_wolfSSL_PEM_PrivateKey(void)
  26881. {
  26882. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  26883. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  26884. #ifndef NO_BIO
  26885. BIO* bio = NULL;
  26886. #endif
  26887. EVP_PKEY* pkey = NULL;
  26888. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  26889. #ifndef NO_BIO
  26890. /* test creating new EVP_PKEY with bad arg */
  26891. AssertNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  26892. /* test loading RSA key using BIO */
  26893. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  26894. {
  26895. XFILE file;
  26896. const char* fname = "./certs/server-key.pem";
  26897. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  26898. size_t sz;
  26899. byte* buf;
  26900. EVP_PKEY* pkey2;
  26901. EVP_PKEY* pkey3;
  26902. RSA* rsa_key = NULL;
  26903. file = XFOPEN(fname, "rb");
  26904. AssertTrue((file != XBADFILE));
  26905. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  26906. sz = XFTELL(file);
  26907. XREWIND(file);
  26908. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  26909. if (buf) {
  26910. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  26911. }
  26912. XFCLOSE(file);
  26913. /* Test using BIO new mem and loading PEM private key */
  26914. bio = BIO_new_mem_buf(buf, (int)sz);
  26915. AssertNotNull(bio);
  26916. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  26917. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  26918. BIO_free(bio);
  26919. bio = NULL;
  26920. AssertNotNull(pkey2 = EVP_PKEY_new());
  26921. pkey2->type = EVP_PKEY_RSA;
  26922. /* Test parameter copy */
  26923. AssertIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  26924. EVP_PKEY_free(pkey2);
  26925. EVP_PKEY_free(pkey);
  26926. pkey = NULL;
  26927. /* Qt unit test case : rsa pkcs8 key */
  26928. file = XFOPEN(fname_rsa_p8, "rb");
  26929. AssertTrue((file != XBADFILE));
  26930. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  26931. sz = XFTELL(file);
  26932. XREWIND(file);
  26933. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  26934. if (buf)
  26935. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  26936. XFCLOSE(file);
  26937. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  26938. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  26939. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  26940. BIO_free(bio);
  26941. bio = NULL;
  26942. AssertNotNull(pkey3 = EVP_PKEY_new());
  26943. AssertNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  26944. AssertIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  26945. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  26946. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  26947. #else
  26948. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  26949. #endif
  26950. RSA_free(rsa_key);
  26951. EVP_PKEY_free(pkey3);
  26952. EVP_PKEY_free(pkey);
  26953. pkey = NULL;
  26954. }
  26955. #endif
  26956. /* test loading ECC key using BIO */
  26957. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  26958. {
  26959. XFILE file;
  26960. const char* fname = "./certs/ecc-key.pem";
  26961. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  26962. size_t sz;
  26963. byte* buf;
  26964. EVP_PKEY* pkey2;
  26965. EVP_PKEY* pkey3;
  26966. EC_KEY* ec_key;
  26967. int nid = 0;
  26968. file = XFOPEN(fname, "rb");
  26969. AssertTrue((file != XBADFILE));
  26970. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  26971. sz = XFTELL(file);
  26972. XREWIND(file);
  26973. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  26974. if (buf)
  26975. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  26976. XFCLOSE(file);
  26977. /* Test using BIO new mem and loading PEM private key */
  26978. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  26979. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  26980. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  26981. BIO_free(bio);
  26982. bio = NULL;
  26983. AssertNotNull(pkey2 = EVP_PKEY_new());
  26984. AssertNotNull(pkey3 = EVP_PKEY_new());
  26985. pkey2->type = EVP_PKEY_EC;
  26986. /* Test parameter copy */
  26987. AssertIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  26988. /* Qt unit test case 1*/
  26989. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  26990. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  26991. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  26992. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  26993. #else
  26994. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  26995. #endif
  26996. /* Test default digest */
  26997. AssertIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  26998. AssertIntEQ(nid, NID_sha256);
  26999. EC_KEY_free(ec_key);
  27000. EVP_PKEY_free(pkey3);
  27001. EVP_PKEY_free(pkey2);
  27002. EVP_PKEY_free(pkey);
  27003. pkey = NULL;
  27004. /* Qt unit test case ec pkcs8 key */
  27005. file = XFOPEN(fname_ecc_p8, "rb");
  27006. AssertTrue((file != XBADFILE));
  27007. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  27008. sz = XFTELL(file);
  27009. XREWIND(file);
  27010. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  27011. if (buf)
  27012. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  27013. XFCLOSE(file);
  27014. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  27015. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  27016. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  27017. BIO_free(bio);
  27018. bio = NULL;
  27019. AssertNotNull(pkey3 = EVP_PKEY_new());
  27020. /* Qt unit test case */
  27021. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  27022. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  27023. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  27024. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  27025. #else
  27026. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  27027. #endif
  27028. EC_KEY_free(ec_key);
  27029. EVP_PKEY_free(pkey3);
  27030. EVP_PKEY_free(pkey);
  27031. pkey = NULL;
  27032. }
  27033. #endif
  27034. #if !defined(NO_BIO) && !defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || \
  27035. defined(WOLFSSL_CERT_GEN))
  27036. {
  27037. #define BIO_PEM_TEST_CHAR 'a'
  27038. EVP_PKEY* pkey2 = NULL;
  27039. unsigned char extra[10];
  27040. int i;
  27041. BIO* pub_bio = NULL;
  27042. printf(testingFmt, "wolfSSL_PEM_PrivateKey()");
  27043. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  27044. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27045. AssertIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  27046. AssertNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27047. AssertIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  27048. AssertNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
  27049. &server_key, (long)sizeof_server_key_der_2048));
  27050. AssertNull(pkey);
  27051. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  27052. &server_key, (long)sizeof_server_key_der_2048));
  27053. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  27054. WOLFSSL_SUCCESS);
  27055. AssertIntGT(BIO_pending(bio), 0);
  27056. AssertIntEQ(BIO_pending(bio), 1679);
  27057. /* Check if the pubkey API writes only the public key */
  27058. #ifdef WOLFSSL_KEY_GEN
  27059. AssertIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  27060. AssertIntGT(BIO_pending(pub_bio), 0);
  27061. /* Previously both the private key and the pubkey calls would write
  27062. * out the private key and the PEM header was the only difference.
  27063. * The public PEM should be significantly shorter than the
  27064. * private key versison. */
  27065. AssertIntEQ(BIO_pending(pub_bio), 451);
  27066. #endif
  27067. /* test creating new EVP_PKEY with good args */
  27068. AssertNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  27069. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr)
  27070. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz), 0);
  27071. /* test of reuse of EVP_PKEY */
  27072. AssertNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  27073. AssertIntEQ(BIO_pending(bio), 0);
  27074. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  27075. SSL_SUCCESS);
  27076. AssertIntEQ(BIO_write(bio, extra, 10), 10); /* add 10 extra bytes after PEM */
  27077. AssertNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  27078. AssertNotNull(pkey);
  27079. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  27080. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
  27081. }
  27082. AssertIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
  27083. AssertIntEQ(BIO_read(bio, extra, 10), 10);
  27084. for (i = 0; i < 10; i++) {
  27085. AssertIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  27086. }
  27087. BIO_free(pub_bio);
  27088. BIO_free(bio);
  27089. bio = NULL;
  27090. EVP_PKEY_free(pkey);
  27091. pkey = NULL;
  27092. EVP_PKEY_free(pkey2);
  27093. }
  27094. #endif
  27095. /* key is DES encrypted */
  27096. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  27097. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  27098. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && \
  27099. !defined(HAVE_USER_RSA) && !defined(NO_RSA)
  27100. {
  27101. XFILE f;
  27102. wc_pem_password_cb* passwd_cb;
  27103. void* passwd_cb_userdata;
  27104. SSL_CTX* ctx;
  27105. char passwd[] = "bad password";
  27106. #ifndef WOLFSSL_NO_TLS12
  27107. #ifndef NO_WOLFSSL_SERVER
  27108. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  27109. #else
  27110. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  27111. #endif
  27112. #else
  27113. #ifndef NO_WOLFSSL_SERVER
  27114. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  27115. #else
  27116. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  27117. #endif
  27118. #endif
  27119. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  27120. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  27121. AssertNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  27122. AssertNull(passwd_cb_userdata =
  27123. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  27124. /* fail case with password call back */
  27125. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  27126. (void*)passwd));
  27127. BIO_free(bio);
  27128. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  27129. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  27130. (void*)passwd));
  27131. BIO_free(bio);
  27132. f = XFOPEN("./certs/server-keyEnc.pem", "rb");
  27133. AssertNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  27134. /* use callback that works */
  27135. AssertNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  27136. (void*)"yassl123"));
  27137. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  27138. EVP_PKEY_free(pkey);
  27139. pkey = NULL;
  27140. BIO_free(bio);
  27141. bio = NULL;
  27142. SSL_CTX_free(ctx);
  27143. }
  27144. #endif /* !defined(NO_DES3) */
  27145. #endif /* !NO_BIO */
  27146. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  27147. {
  27148. unsigned char buf[2048];
  27149. size_t bytes;
  27150. XFILE f;
  27151. SSL_CTX* ctx;
  27152. #ifndef WOLFSSL_NO_TLS12
  27153. #ifndef NO_WOLFSSL_SERVER
  27154. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  27155. #else
  27156. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  27157. #endif
  27158. #else
  27159. #ifndef NO_WOLFSSL_SERVER
  27160. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  27161. #else
  27162. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  27163. #endif
  27164. #endif
  27165. f = XFOPEN("./certs/ecc-key.der", "rb");
  27166. AssertTrue((f != XBADFILE));
  27167. bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f);
  27168. XFCLOSE(f);
  27169. server_key = buf;
  27170. pkey = NULL;
  27171. AssertNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  27172. AssertNull(pkey);
  27173. AssertNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  27174. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  27175. EVP_PKEY_free(pkey);
  27176. pkey = NULL;
  27177. SSL_CTX_free(ctx);
  27178. }
  27179. #endif
  27180. printf(resultFmt, passed);
  27181. #ifndef NO_BIO
  27182. (void)bio;
  27183. #endif
  27184. (void)pkey;
  27185. (void)server_key;
  27186. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  27187. }
  27188. #ifndef NO_BIO
  27189. static void test_wolfSSL_PEM_bio_RSAKey(void)
  27190. {
  27191. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  27192. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  27193. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  27194. RSA* rsa = NULL;
  27195. BIO* bio = NULL;
  27196. printf(testingFmt, "wolfSSL_PEM_bio_RSAKey");
  27197. /* PrivateKey */
  27198. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  27199. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  27200. AssertNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  27201. AssertNotNull(rsa);
  27202. AssertIntEQ(RSA_size(rsa), 256);
  27203. AssertIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  27204. NULL), WOLFSSL_FAILURE);
  27205. BIO_free(bio);
  27206. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27207. AssertIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  27208. NULL), WOLFSSL_SUCCESS);
  27209. BIO_free(bio);
  27210. RSA_free(rsa);
  27211. /* PUBKEY */
  27212. AssertNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  27213. AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  27214. AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  27215. AssertIntEQ(RSA_size(rsa), 256);
  27216. AssertIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  27217. BIO_free(bio);
  27218. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27219. AssertIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  27220. BIO_free(bio);
  27221. /* Same test as above, but with a file pointer rather than a BIO. */
  27222. AssertIntEQ(PEM_write_RSAPublicKey(NULL, rsa), WOLFSSL_FAILURE);
  27223. AssertIntEQ(PEM_write_RSAPublicKey(stdout, NULL), WOLFSSL_FAILURE);
  27224. AssertIntEQ(PEM_write_RSAPublicKey(stdout, rsa), WOLFSSL_SUCCESS);
  27225. RSA_free(rsa);
  27226. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  27227. AssertNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  27228. AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  27229. BIO_free(bio);
  27230. RSA_free(rsa);
  27231. #ifdef HAVE_ECC
  27232. /* ensure that non-rsa keys do not work */
  27233. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  27234. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  27235. AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  27236. BIO_free(bio);
  27237. RSA_free(rsa);
  27238. #endif /* HAVE_ECC */
  27239. printf(resultFmt, passed);
  27240. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  27241. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  27242. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  27243. }
  27244. static void test_wolfSSL_PEM_RSAPrivateKey(void)
  27245. {
  27246. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  27247. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  27248. RSA* rsa = NULL;
  27249. RSA* rsa_dup = NULL;
  27250. BIO* bio = NULL;
  27251. printf(testingFmt, "wolfSSL_PEM_RSAPrivateKey()");
  27252. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  27253. AssertNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  27254. AssertIntEQ(RSA_size(rsa), 256);
  27255. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  27256. AssertNull(rsa_dup = RSAPublicKey_dup(NULL));
  27257. /* Test duplicating empty key. */
  27258. rsa_dup = RSA_new();
  27259. AssertNull(RSAPublicKey_dup(rsa_dup));
  27260. RSA_free(rsa_dup);
  27261. AssertNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  27262. AssertPtrNE(rsa_dup, rsa);
  27263. #endif
  27264. /* test if valgrind complains about unreleased memory */
  27265. RSA_up_ref(rsa);
  27266. RSA_free(rsa);
  27267. BIO_free(bio);
  27268. RSA_free(rsa);
  27269. RSA_free(rsa_dup);
  27270. #ifdef HAVE_ECC
  27271. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  27272. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  27273. BIO_free(bio);
  27274. #endif /* HAVE_ECC */
  27275. printf(resultFmt, passed);
  27276. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  27277. }
  27278. static void test_wolfSSL_PEM_bio_DSAKey(void)
  27279. {
  27280. #ifndef HAVE_SELFTEST
  27281. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  27282. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  27283. DSA* dsa = NULL;
  27284. BIO* bio = NULL;
  27285. printf(testingFmt, "wolfSSL_PEM_bio_DSAKey");
  27286. /* PrivateKey */
  27287. AssertNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  27288. AssertNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  27289. AssertNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  27290. AssertIntEQ(BN_num_bytes(dsa->g), 128);
  27291. AssertIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, NULL),
  27292. WOLFSSL_FAILURE);
  27293. BIO_free(bio);
  27294. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27295. AssertIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL, NULL),
  27296. WOLFSSL_SUCCESS);
  27297. BIO_free(bio);
  27298. DSA_free(dsa);
  27299. /* PUBKEY */
  27300. AssertNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  27301. AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  27302. AssertNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  27303. AssertIntEQ(BN_num_bytes(dsa->g), 128);
  27304. AssertIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  27305. BIO_free(bio);
  27306. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27307. AssertIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  27308. BIO_free(bio);
  27309. DSA_free(dsa);
  27310. #ifdef HAVE_ECC
  27311. /* ensure that non-dsa keys do not work */
  27312. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  27313. AssertNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  27314. AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  27315. BIO_free(bio);
  27316. DSA_free(dsa);
  27317. #endif /* HAVE_ECC */
  27318. printf(resultFmt, passed);
  27319. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  27320. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  27321. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  27322. #endif /* HAVE_SELFTEST */
  27323. }
  27324. static void test_wolfSSL_PEM_bio_ECKey(void)
  27325. {
  27326. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  27327. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  27328. EC_KEY* ec = NULL;
  27329. BIO* bio = NULL;
  27330. printf(testingFmt, "wolfSSL_PEM_bio_ECKey");
  27331. /* PrivateKey */
  27332. AssertNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  27333. AssertNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  27334. AssertNotNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  27335. AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  27336. AssertIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  27337. NULL),WOLFSSL_FAILURE);
  27338. BIO_free(bio);
  27339. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27340. AssertIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  27341. NULL), WOLFSSL_SUCCESS);
  27342. BIO_free(bio);
  27343. EC_KEY_free(ec);
  27344. /* PUBKEY */
  27345. AssertNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  27346. AssertNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  27347. AssertNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  27348. AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  27349. AssertIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  27350. BIO_free(bio);
  27351. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27352. AssertIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  27353. BIO_free(bio);
  27354. /* Same test as above, but with a file pointer rather than a BIO. */
  27355. AssertIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  27356. AssertIntEQ(PEM_write_EC_PUBKEY(stdout, NULL), WOLFSSL_FAILURE);
  27357. AssertIntEQ(PEM_write_EC_PUBKEY(stdout, ec), WOLFSSL_SUCCESS);
  27358. EC_KEY_free(ec);
  27359. #ifndef NO_RSA
  27360. /* ensure that non-ec keys do not work */
  27361. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  27362. AssertNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  27363. AssertNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  27364. BIO_free(bio);
  27365. EC_KEY_free(ec);
  27366. #endif /* HAVE_ECC */
  27367. printf(resultFmt, passed);
  27368. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  27369. }
  27370. static void test_wolfSSL_PEM_PUBKEY(void)
  27371. {
  27372. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  27373. BIO* bio = NULL;
  27374. EVP_PKEY* pkey = NULL;
  27375. /* test creating new EVP_PKEY with bad arg */
  27376. AssertNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  27377. /* test loading ECC key using BIO */
  27378. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  27379. {
  27380. XFILE file;
  27381. const char* fname = "./certs/ecc-client-keyPub.pem";
  27382. size_t sz;
  27383. byte* buf;
  27384. EVP_PKEY* pkey2;
  27385. EC_KEY* ec_key;
  27386. file = XFOPEN(fname, "rb");
  27387. AssertTrue((file != XBADFILE));
  27388. XFSEEK(file, 0, XSEEK_END);
  27389. sz = XFTELL(file);
  27390. XREWIND(file);
  27391. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  27392. if (buf)
  27393. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  27394. XFCLOSE(file);
  27395. /* Test using BIO new mem and loading PEM private key */
  27396. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  27397. AssertNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  27398. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  27399. BIO_free(bio);
  27400. bio = NULL;
  27401. /* Qt unit test case*/
  27402. AssertNotNull(pkey2 = EVP_PKEY_new());
  27403. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  27404. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  27405. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  27406. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  27407. #else
  27408. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  27409. #endif
  27410. EC_KEY_free(ec_key);
  27411. EVP_PKEY_free(pkey2);
  27412. EVP_PKEY_free(pkey);
  27413. pkey = NULL;
  27414. }
  27415. #endif
  27416. (void)bio;
  27417. (void)pkey;
  27418. #endif
  27419. }
  27420. #endif /* !NO_BIO */
  27421. static void test_DSA_do_sign_verify(void)
  27422. {
  27423. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  27424. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  27425. !defined(NO_DSA)
  27426. unsigned char digest[WC_SHA_DIGEST_SIZE];
  27427. DSA_SIG* sig;
  27428. DSA* dsa;
  27429. word32 bytes;
  27430. byte sigBin[DSA_SIG_SIZE];
  27431. int dsacheck;
  27432. #ifdef USE_CERT_BUFFERS_1024
  27433. byte tmp[ONEK_BUF];
  27434. XMEMSET(tmp, 0, sizeof(tmp));
  27435. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  27436. bytes = sizeof_dsa_key_der_1024;
  27437. #elif defined(USE_CERT_BUFFERS_2048)
  27438. byte tmp[TWOK_BUF];
  27439. XMEMSET(tmp, 0, sizeof(tmp));
  27440. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  27441. bytes = sizeof_dsa_key_der_2048;
  27442. #else
  27443. byte tmp[TWOK_BUF];
  27444. XMEMSET(tmp, 0, sizeof(tmp));
  27445. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  27446. if (fp == XBADFILE) {
  27447. return WOLFSSL_BAD_FILE;
  27448. }
  27449. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  27450. XFCLOSE(fp);
  27451. #endif /* END USE_CERT_BUFFERS_1024 */
  27452. printf(testingFmt, "DSA_do_sign_verify()");
  27453. XMEMSET(digest, 202, sizeof(digest));
  27454. AssertNotNull(dsa = DSA_new());
  27455. AssertIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
  27456. AssertIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  27457. AssertIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  27458. AssertNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  27459. AssertIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  27460. DSA_SIG_free(sig);
  27461. DSA_free(dsa);
  27462. #endif
  27463. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  27464. }
  27465. static void test_wolfSSL_tmp_dh(void)
  27466. {
  27467. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  27468. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  27469. byte buff[6000];
  27470. char file[] = "./certs/dsaparams.pem";
  27471. XFILE f;
  27472. int bytes;
  27473. DSA* dsa;
  27474. DH* dh;
  27475. #if defined(WOLFSSL_DH_EXTRA) && \
  27476. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  27477. DH* dh2;
  27478. #endif
  27479. BIO* bio;
  27480. SSL* ssl;
  27481. SSL_CTX* ctx;
  27482. printf(testingFmt, "wolfSSL_tmp_dh()");
  27483. #ifndef NO_WOLFSSL_SERVER
  27484. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  27485. #else
  27486. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  27487. #endif
  27488. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  27489. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  27490. AssertNotNull(ssl = SSL_new(ctx));
  27491. f = XFOPEN(file, "rb");
  27492. AssertTrue((f != XBADFILE));
  27493. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  27494. XFCLOSE(f);
  27495. bio = BIO_new_mem_buf((void*)buff, bytes);
  27496. AssertNotNull(bio);
  27497. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  27498. AssertNotNull(dsa);
  27499. dh = wolfSSL_DSA_dup_DH(dsa);
  27500. AssertNotNull(dh);
  27501. #if defined(WOLFSSL_DH_EXTRA) && \
  27502. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  27503. AssertNotNull(dh2 = wolfSSL_DH_dup(dh));
  27504. #endif
  27505. AssertIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  27506. #ifndef NO_WOLFSSL_SERVER
  27507. AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  27508. #else
  27509. AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  27510. #endif
  27511. BIO_free(bio);
  27512. DSA_free(dsa);
  27513. DH_free(dh);
  27514. #if defined(WOLFSSL_DH_EXTRA) && \
  27515. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  27516. DH_free(dh2);
  27517. #endif
  27518. SSL_free(ssl);
  27519. SSL_CTX_free(ctx);
  27520. printf(resultFmt, passed);
  27521. #endif
  27522. }
  27523. static void test_wolfSSL_ctrl(void)
  27524. {
  27525. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  27526. byte buff[6000];
  27527. BIO* bio;
  27528. int bytes;
  27529. BUF_MEM* ptr = NULL;
  27530. printf(testingFmt, "wolfSSL_crtl()");
  27531. bytes = sizeof(buff);
  27532. bio = BIO_new_mem_buf((void*)buff, bytes);
  27533. AssertNotNull(bio);
  27534. AssertNotNull(BIO_s_socket());
  27535. AssertIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  27536. /* needs tested after stubs filled out @TODO
  27537. SSL_ctrl
  27538. SSL_CTX_ctrl
  27539. */
  27540. BIO_free(bio);
  27541. printf(resultFmt, passed);
  27542. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  27543. }
  27544. static void test_wolfSSL_EVP_PKEY_new_mac_key(void)
  27545. {
  27546. #ifdef OPENSSL_EXTRA
  27547. static const unsigned char pw[] = "password";
  27548. static const int pwSz = sizeof(pw) - 1;
  27549. size_t checkPwSz = 0;
  27550. const unsigned char* checkPw = NULL;
  27551. WOLFSSL_EVP_PKEY* key = NULL;
  27552. printf(testingFmt, "wolfSSL_EVP_PKEY_new_mac_key()");
  27553. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  27554. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  27555. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, pwSz));
  27556. if (key) {
  27557. AssertIntEQ(key->type, EVP_PKEY_HMAC);
  27558. AssertIntEQ(key->save_type, EVP_PKEY_HMAC);
  27559. AssertIntEQ(key->pkey_sz, pwSz);
  27560. AssertIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  27561. }
  27562. AssertNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  27563. AssertIntEQ((int)checkPwSz, pwSz);
  27564. if (checkPw) {
  27565. AssertIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  27566. }
  27567. wolfSSL_EVP_PKEY_free(key);
  27568. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, 0));
  27569. if (key) {
  27570. AssertIntEQ(key->pkey_sz, 0);
  27571. }
  27572. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  27573. (void)checkPw;
  27574. AssertIntEQ((int)checkPwSz, 0);
  27575. wolfSSL_EVP_PKEY_free(key);
  27576. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL, 0));
  27577. if (key) {
  27578. AssertIntEQ(key->pkey_sz, 0);
  27579. }
  27580. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  27581. (void)checkPw;
  27582. AssertIntEQ((int)checkPwSz, 0);
  27583. wolfSSL_EVP_PKEY_free(key);
  27584. printf(resultFmt, passed);
  27585. #endif /* OPENSSL_EXTRA */
  27586. }
  27587. static void test_wolfSSL_EVP_Digest(void)
  27588. {
  27589. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  27590. const char* in = "abc";
  27591. int inLen = (int)XSTRLEN(in);
  27592. byte out[WC_SHA256_DIGEST_SIZE];
  27593. unsigned int outLen;
  27594. const char* expOut = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  27595. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  27596. "\x15\xAD";
  27597. printf(testingFmt, "wolfSSL_EVP_Digest()");
  27598. AssertIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen, "SHA256", NULL), 1);
  27599. AssertIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  27600. AssertIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  27601. printf(resultFmt, passed);
  27602. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  27603. }
  27604. static void test_wolfSSL_EVP_Digest_all(void)
  27605. {
  27606. #ifdef OPENSSL_EXTRA
  27607. const char* digests[] = {
  27608. #ifndef NO_MD5
  27609. "MD5",
  27610. #endif
  27611. #ifndef NO_SHA
  27612. "SHA",
  27613. #endif
  27614. #ifdef WOLFSSL_SHA224
  27615. "SHA224",
  27616. #endif
  27617. #ifndef NO_SHA256
  27618. "SHA256",
  27619. #endif
  27620. #ifdef WOLFSSL_SHA384
  27621. "SHA384",
  27622. #endif
  27623. #ifdef WOLFSSL_SHA512
  27624. "SHA512",
  27625. #endif
  27626. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  27627. "SHA512_224",
  27628. #endif
  27629. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  27630. "SHA512_256",
  27631. #endif
  27632. #ifdef WOLFSSL_SHA3
  27633. #ifndef WOLFSSL_NOSHA3_224
  27634. "SHA3_224",
  27635. #endif
  27636. #ifndef WOLFSSL_NOSHA3_256
  27637. "SHA3_256",
  27638. #endif
  27639. "SHA3_384",
  27640. #ifndef WOLFSSL_NOSHA3_512
  27641. "SHA3_512",
  27642. #endif
  27643. #endif /* WOLFSSL_SHA3 */
  27644. NULL
  27645. };
  27646. const char** d;
  27647. const unsigned char in[] = "abc";
  27648. int inLen = XSTR_SIZEOF(in);
  27649. byte out[WC_MAX_DIGEST_SIZE];
  27650. unsigned int outLen;
  27651. printf(testingFmt, "wolfSSL_EVP_Digest_all");
  27652. for (d = digests; *d != NULL; d++) {
  27653. AssertIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  27654. AssertIntGT(outLen, 0);
  27655. AssertIntEQ(EVP_MD_size(*d), outLen);
  27656. }
  27657. printf(resultFmt, passed);
  27658. #endif
  27659. }
  27660. static void test_wolfSSL_EVP_MD_size(void)
  27661. {
  27662. #ifdef OPENSSL_EXTRA
  27663. WOLFSSL_EVP_MD_CTX mdCtx;
  27664. printf(testingFmt, "wolfSSL_EVP_MD_size()");
  27665. #ifdef WOLFSSL_SHA3
  27666. #ifndef WOLFSSL_NOSHA3_224
  27667. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27668. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  27669. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  27670. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  27671. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27672. #endif
  27673. #ifndef WOLFSSL_NOSHA3_256
  27674. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27675. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  27676. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  27677. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  27678. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27679. #endif
  27680. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27681. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  27682. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  27683. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  27684. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27685. #ifndef WOLFSSL_NOSHA3_512
  27686. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27687. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  27688. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  27689. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  27690. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27691. #endif
  27692. #endif /* WOLFSSL_SHA3 */
  27693. #ifndef NO_SHA256
  27694. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27695. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  27696. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA256_DIGEST_SIZE);
  27697. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA256_BLOCK_SIZE);
  27698. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  27699. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  27700. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27701. #endif
  27702. #ifndef NO_MD5
  27703. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27704. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  27705. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_MD5_DIGEST_SIZE);
  27706. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_MD5_BLOCK_SIZE);
  27707. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  27708. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  27709. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27710. #endif
  27711. #ifdef WOLFSSL_SHA224
  27712. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27713. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  27714. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA224_DIGEST_SIZE);
  27715. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA224_BLOCK_SIZE);
  27716. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  27717. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  27718. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27719. #endif
  27720. #ifdef WOLFSSL_SHA384
  27721. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27722. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  27723. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA384_DIGEST_SIZE);
  27724. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA384_BLOCK_SIZE);
  27725. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  27726. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  27727. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27728. #endif
  27729. #ifdef WOLFSSL_SHA512
  27730. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27731. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  27732. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA512_DIGEST_SIZE);
  27733. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA512_BLOCK_SIZE);
  27734. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  27735. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  27736. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27737. #endif
  27738. #ifndef NO_SHA
  27739. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27740. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  27741. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_DIGEST_SIZE);
  27742. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_BLOCK_SIZE);
  27743. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  27744. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  27745. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27746. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27747. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  27748. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_DIGEST_SIZE);
  27749. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_BLOCK_SIZE);
  27750. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  27751. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  27752. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27753. #endif
  27754. /* error case */
  27755. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27756. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  27757. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), BAD_FUNC_ARG);
  27758. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  27759. /* Cleanup is valid on uninit'ed struct */
  27760. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27761. printf(resultFmt, passed);
  27762. #endif /* OPENSSL_EXTRA */
  27763. }
  27764. static void test_wolfSSL_EVP_MD_pkey_type(void)
  27765. {
  27766. #ifdef OPENSSL_EXTRA
  27767. const WOLFSSL_EVP_MD* md;
  27768. printf(testingFmt, "test_wolfSSL_EVP_MD_pkey_type()");
  27769. #ifndef NO_MD5
  27770. AssertNotNull(md = EVP_md5());
  27771. AssertIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  27772. #endif
  27773. #ifndef NO_SHA
  27774. AssertNotNull(md = EVP_sha1());
  27775. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  27776. #endif
  27777. #ifdef WOLFSSL_SHA224
  27778. AssertNotNull(md = EVP_sha224());
  27779. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  27780. #endif
  27781. AssertNotNull(md = EVP_sha256());
  27782. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  27783. #ifdef WOLFSSL_SHA384
  27784. AssertNotNull(md = EVP_sha384());
  27785. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  27786. #endif
  27787. #ifdef WOLFSSL_SHA512
  27788. AssertNotNull(md = EVP_sha512());
  27789. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  27790. #endif
  27791. printf(resultFmt, passed);
  27792. #endif
  27793. }
  27794. #ifdef OPENSSL_EXTRA
  27795. static void test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  27796. size_t testKeySz, const char* testData, size_t testDataSz,
  27797. const byte* testResult, size_t testResultSz)
  27798. {
  27799. unsigned char check[WC_MAX_DIGEST_SIZE];
  27800. size_t checkSz = -1;
  27801. WOLFSSL_EVP_PKEY* key;
  27802. WOLFSSL_EVP_MD_CTX mdCtx;
  27803. printf(testingFmt, "wolfSSL_EVP_MD_hmac_signing()");
  27804. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  27805. testKey, (int)testKeySz));
  27806. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27807. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  27808. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  27809. (unsigned int)testDataSz), 1);
  27810. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  27811. AssertIntEQ((int)checkSz, (int)testResultSz);
  27812. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  27813. AssertIntEQ((int)checkSz,(int)testResultSz);
  27814. AssertIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  27815. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27816. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  27817. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  27818. (unsigned int)testDataSz), 1);
  27819. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  27820. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27821. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27822. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  27823. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  27824. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  27825. AssertIntEQ((int)checkSz, (int)testResultSz);
  27826. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  27827. AssertIntEQ((int)checkSz,(int)testResultSz);
  27828. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  27829. (unsigned int)testDataSz - 4), 1);
  27830. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  27831. AssertIntEQ((int)checkSz,(int)testResultSz);
  27832. AssertIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  27833. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27834. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  27835. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  27836. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  27837. (unsigned int)testDataSz - 4), 1);
  27838. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  27839. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27840. wolfSSL_EVP_PKEY_free(key);
  27841. }
  27842. #endif
  27843. static void test_wolfSSL_EVP_MD_hmac_signing(void)
  27844. {
  27845. #ifdef OPENSSL_EXTRA
  27846. static const unsigned char testKey[] =
  27847. {
  27848. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  27849. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  27850. 0x0b, 0x0b, 0x0b, 0x0b
  27851. };
  27852. static const char testData[] = "Hi There";
  27853. #ifdef WOLFSSL_SHA224
  27854. static const unsigned char testResultSha224[] =
  27855. {
  27856. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  27857. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  27858. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  27859. 0x53, 0x68, 0x4b, 0x22
  27860. };
  27861. #endif
  27862. #ifndef NO_SHA256
  27863. static const unsigned char testResultSha256[] =
  27864. {
  27865. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  27866. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  27867. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  27868. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  27869. };
  27870. #endif
  27871. #ifdef WOLFSSL_SHA384
  27872. static const unsigned char testResultSha384[] =
  27873. {
  27874. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  27875. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  27876. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  27877. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  27878. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  27879. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  27880. };
  27881. #endif
  27882. #ifdef WOLFSSL_SHA512
  27883. static const unsigned char testResultSha512[] =
  27884. {
  27885. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  27886. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  27887. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  27888. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  27889. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  27890. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  27891. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  27892. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  27893. };
  27894. #endif
  27895. #ifdef WOLFSSL_SHA3
  27896. #ifndef WOLFSSL_NOSHA3_224
  27897. static const unsigned char testResultSha3_224[] =
  27898. {
  27899. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  27900. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  27901. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  27902. 0xf3, 0xc8, 0x60, 0xf7
  27903. };
  27904. #endif
  27905. #ifndef WOLFSSL_NOSHA3_256
  27906. static const unsigned char testResultSha3_256[] =
  27907. {
  27908. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  27909. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  27910. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  27911. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  27912. };
  27913. #endif
  27914. #ifndef WOLFSSL_NOSHA3_384
  27915. static const unsigned char testResultSha3_384[] =
  27916. {
  27917. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  27918. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  27919. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  27920. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  27921. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  27922. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  27923. };
  27924. #endif
  27925. #ifndef WOLFSSL_NOSHA3_512
  27926. static const unsigned char testResultSha3_512[] =
  27927. {
  27928. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  27929. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  27930. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  27931. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  27932. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  27933. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  27934. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  27935. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  27936. };
  27937. #endif
  27938. #endif
  27939. #ifndef NO_SHA256
  27940. test_hmac_signing(wolfSSL_EVP_sha256(), testKey, sizeof(testKey), testData,
  27941. XSTRLEN(testData), testResultSha256, sizeof(testResultSha256));
  27942. #endif
  27943. #ifdef WOLFSSL_SHA224
  27944. test_hmac_signing(wolfSSL_EVP_sha224(), testKey, sizeof(testKey), testData,
  27945. XSTRLEN(testData), testResultSha224, sizeof(testResultSha224));
  27946. #endif
  27947. #ifdef WOLFSSL_SHA384
  27948. test_hmac_signing(wolfSSL_EVP_sha384(), testKey, sizeof(testKey), testData,
  27949. XSTRLEN(testData), testResultSha384, sizeof(testResultSha384));
  27950. #endif
  27951. #ifdef WOLFSSL_SHA512
  27952. test_hmac_signing(wolfSSL_EVP_sha512(), testKey, sizeof(testKey), testData,
  27953. XSTRLEN(testData), testResultSha512, sizeof(testResultSha512));
  27954. #endif
  27955. #ifdef WOLFSSL_SHA3
  27956. #ifndef WOLFSSL_NOSHA3_224
  27957. test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey, sizeof(testKey),
  27958. testData, XSTRLEN(testData), testResultSha3_224,
  27959. sizeof(testResultSha3_224));
  27960. #endif
  27961. #ifndef WOLFSSL_NOSHA3_256
  27962. test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey, sizeof(testKey),
  27963. testData, XSTRLEN(testData), testResultSha3_256,
  27964. sizeof(testResultSha3_256));
  27965. #endif
  27966. #ifndef WOLFSSL_NOSHA3_384
  27967. test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey, sizeof(testKey),
  27968. testData, XSTRLEN(testData), testResultSha3_384,
  27969. sizeof(testResultSha3_384));
  27970. #endif
  27971. #ifndef WOLFSSL_NOSHA3_512
  27972. test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey, sizeof(testKey),
  27973. testData, XSTRLEN(testData), testResultSha3_512,
  27974. sizeof(testResultSha3_512));
  27975. #endif
  27976. #endif
  27977. printf(resultFmt, passed);
  27978. #endif /* OPENSSL_EXTRA */
  27979. }
  27980. static void test_wolfSSL_EVP_MD_rsa_signing(void)
  27981. {
  27982. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  27983. defined(USE_CERT_BUFFERS_2048)
  27984. WOLFSSL_EVP_PKEY* privKey;
  27985. WOLFSSL_EVP_PKEY* pubKey;
  27986. WOLFSSL_EVP_PKEY_CTX* keyCtx;
  27987. const char testData[] = "Hi There";
  27988. WOLFSSL_EVP_MD_CTX mdCtx;
  27989. size_t checkSz = -1;
  27990. int sz = 2048 / 8;
  27991. const unsigned char* cp;
  27992. const unsigned char* p;
  27993. unsigned char check[2048/8];
  27994. size_t i;
  27995. int paddings[] = {
  27996. RSA_PKCS1_PADDING,
  27997. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  27998. RSA_PKCS1_PSS_PADDING,
  27999. #endif
  28000. };
  28001. printf(testingFmt, "wolfSSL_EVP_MD_rsa_signing()");
  28002. cp = client_key_der_2048;
  28003. AssertNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  28004. sizeof_client_key_der_2048)));
  28005. p = client_keypub_der_2048;
  28006. AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  28007. sizeof_client_keypub_der_2048)));
  28008. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28009. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28010. NULL, privKey), 1);
  28011. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  28012. (unsigned int)XSTRLEN(testData)), 1);
  28013. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  28014. AssertIntEQ((int)checkSz, sz);
  28015. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28016. AssertIntEQ((int)checkSz,sz);
  28017. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28018. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28019. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28020. NULL, pubKey), 1);
  28021. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  28022. (unsigned int)XSTRLEN(testData)),
  28023. 1);
  28024. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  28025. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28026. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28027. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28028. NULL, privKey), 1);
  28029. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  28030. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  28031. AssertIntEQ((int)checkSz, sz);
  28032. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28033. AssertIntEQ((int)checkSz, sz);
  28034. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  28035. (unsigned int)XSTRLEN(testData) - 4), 1);
  28036. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28037. AssertIntEQ((int)checkSz, sz);
  28038. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28039. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28040. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28041. NULL, pubKey), 1);
  28042. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  28043. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  28044. (unsigned int)XSTRLEN(testData) - 4),
  28045. 1);
  28046. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  28047. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28048. /* Check all signing padding types */
  28049. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  28050. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28051. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  28052. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  28053. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  28054. paddings[i]), 1);
  28055. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  28056. (unsigned int)XSTRLEN(testData)), 1);
  28057. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  28058. AssertIntEQ((int)checkSz, sz);
  28059. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28060. AssertIntEQ((int)checkSz,sz);
  28061. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28062. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28063. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  28064. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  28065. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  28066. paddings[i]), 1);
  28067. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  28068. (unsigned int)XSTRLEN(testData)), 1);
  28069. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  28070. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28071. }
  28072. wolfSSL_EVP_PKEY_free(pubKey);
  28073. wolfSSL_EVP_PKEY_free(privKey);
  28074. printf(resultFmt, passed);
  28075. #endif
  28076. }
  28077. static void test_wolfSSL_EVP_MD_ecc_signing(void)
  28078. {
  28079. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  28080. WOLFSSL_EVP_PKEY* privKey;
  28081. WOLFSSL_EVP_PKEY* pubKey;
  28082. const char testData[] = "Hi There";
  28083. WOLFSSL_EVP_MD_CTX mdCtx;
  28084. size_t checkSz = -1;
  28085. const unsigned char* cp;
  28086. const unsigned char* p;
  28087. unsigned char check[2048/8];
  28088. printf(testingFmt, "wolfSSL_EVP_MD_ecc_signing()");
  28089. cp = ecc_clikey_der_256;
  28090. privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  28091. sizeof_ecc_clikey_der_256);
  28092. AssertNotNull(privKey);
  28093. p = ecc_clikeypub_der_256;
  28094. AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  28095. sizeof_ecc_clikeypub_der_256)));
  28096. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28097. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28098. NULL, privKey), 1);
  28099. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  28100. (unsigned int)XSTRLEN(testData)), 1);
  28101. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  28102. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28103. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28104. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28105. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28106. NULL, pubKey), 1);
  28107. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  28108. (unsigned int)XSTRLEN(testData)),
  28109. 1);
  28110. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  28111. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28112. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28113. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28114. NULL, privKey), 1);
  28115. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  28116. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  28117. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28118. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  28119. (unsigned int)XSTRLEN(testData) - 4), 1);
  28120. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28121. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28122. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28123. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28124. NULL, pubKey), 1);
  28125. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  28126. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  28127. (unsigned int)XSTRLEN(testData) - 4),
  28128. 1);
  28129. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  28130. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28131. wolfSSL_EVP_PKEY_free(pubKey);
  28132. wolfSSL_EVP_PKEY_free(privKey);
  28133. printf(resultFmt, passed);
  28134. #endif
  28135. }
  28136. static void test_wolfSSL_CTX_add_extra_chain_cert(void)
  28137. {
  28138. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28139. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  28140. char caFile[] = "./certs/client-ca.pem";
  28141. char clientFile[] = "./certs/client-cert.pem";
  28142. SSL_CTX* ctx;
  28143. X509* x509;
  28144. BIO *bio = NULL;
  28145. X509 *cert = NULL;
  28146. X509 *ca;
  28147. STACK_OF(X509) *chain = NULL;
  28148. STACK_OF(X509) *chain2 = NULL;
  28149. printf(testingFmt, "wolfSSL_CTX_add_extra_chain_cert()");
  28150. #ifndef NO_WOLFSSL_SERVER
  28151. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28152. #else
  28153. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28154. #endif
  28155. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  28156. AssertNotNull(x509);
  28157. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  28158. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  28159. AssertNotNull(x509);
  28160. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  28161. /* additional test of getting EVP_PKEY key size from X509
  28162. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  28163. * allowed with user RSA */
  28164. {
  28165. EVP_PKEY* pkey;
  28166. #if defined(HAVE_ECC)
  28167. X509* ecX509;
  28168. #endif /* HAVE_ECC */
  28169. AssertNotNull(pkey = X509_get_pubkey(x509));
  28170. /* current RSA key is 2048 bit (256 bytes) */
  28171. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  28172. EVP_PKEY_free(pkey);
  28173. #if defined(HAVE_ECC)
  28174. #if defined(USE_CERT_BUFFERS_256)
  28175. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  28176. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  28177. SSL_FILETYPE_ASN1));
  28178. #else
  28179. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(cliEccCertFile,
  28180. SSL_FILETYPE_PEM));
  28181. #endif
  28182. pkey = X509_get_pubkey(ecX509);
  28183. AssertNotNull(pkey);
  28184. /* current ECC key is 256 bit (32 bytes) */
  28185. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  28186. X509_free(ecX509);
  28187. EVP_PKEY_free(pkey);
  28188. #endif /* HAVE_ECC */
  28189. }
  28190. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  28191. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  28192. #ifdef WOLFSSL_ENCRYPTED_KEYS
  28193. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  28194. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  28195. #endif
  28196. SSL_CTX_free(ctx);
  28197. #ifndef NO_WOLFSSL_SERVER
  28198. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28199. #else
  28200. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28201. #endif
  28202. /* Test haproxy use case */
  28203. AssertNotNull(bio = BIO_new_file(svrCertFile, "r"));
  28204. /* Read Certificate */
  28205. AssertNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  28206. AssertNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  28207. AssertNotNull(chain = sk_X509_new_null());
  28208. AssertIntEQ(sk_X509_push(chain, ca), 1);
  28209. AssertNotNull(chain2 = X509_chain_up_ref(chain));
  28210. AssertNotNull(ca = sk_X509_shift(chain2));
  28211. AssertIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  28212. AssertIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  28213. BIO_free(bio);
  28214. X509_free(cert);
  28215. sk_X509_pop_free(chain, X509_free);
  28216. sk_X509_pop_free(chain2, X509_free);
  28217. SSL_CTX_free(ctx);
  28218. printf(resultFmt, passed);
  28219. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28220. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  28221. }
  28222. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  28223. static void test_wolfSSL_ERR_peek_last_error_line(void)
  28224. {
  28225. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28226. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  28227. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  28228. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  28229. tcp_ready ready;
  28230. func_args client_args;
  28231. func_args server_args;
  28232. #ifndef SINGLE_THREADED
  28233. THREAD_TYPE serverThread;
  28234. #endif
  28235. callback_functions client_cb;
  28236. callback_functions server_cb;
  28237. int line = 0;
  28238. int flag = ERR_TXT_STRING;
  28239. const char* file = NULL;
  28240. const char* data = NULL;
  28241. printf(testingFmt, "wolfSSL_ERR_peek_last_error_line()");
  28242. /* create a failed connection and inspect the error */
  28243. #ifdef WOLFSSL_TIRTOS
  28244. fdOpenSession(Task_self());
  28245. #endif
  28246. XMEMSET(&client_args, 0, sizeof(func_args));
  28247. XMEMSET(&server_args, 0, sizeof(func_args));
  28248. StartTCP();
  28249. InitTcpReady(&ready);
  28250. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  28251. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  28252. client_cb.method = wolfTLSv1_1_client_method;
  28253. server_cb.method = wolfTLSv1_2_server_method;
  28254. server_args.signal = &ready;
  28255. server_args.callbacks = &server_cb;
  28256. client_args.signal = &ready;
  28257. client_args.callbacks = &client_cb;
  28258. #ifndef SINGLE_THREADED
  28259. start_thread(test_server_nofail, &server_args, &serverThread);
  28260. wait_tcp_ready(&server_args);
  28261. test_client_nofail(&client_args, NULL);
  28262. join_thread(serverThread);
  28263. #endif
  28264. FreeTcpReady(&ready);
  28265. AssertIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  28266. AssertNotNull(data);
  28267. /* check clearing error state */
  28268. ERR_remove_state(0);
  28269. AssertIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  28270. ERR_peek_last_error_line(NULL, &line);
  28271. AssertIntEQ(line, 0);
  28272. ERR_peek_last_error_line(&file, NULL);
  28273. AssertNull(file);
  28274. /* retry connection to fill error queue */
  28275. XMEMSET(&client_args, 0, sizeof(func_args));
  28276. XMEMSET(&server_args, 0, sizeof(func_args));
  28277. StartTCP();
  28278. InitTcpReady(&ready);
  28279. client_cb.method = wolfTLSv1_1_client_method;
  28280. server_cb.method = wolfTLSv1_2_server_method;
  28281. server_args.signal = &ready;
  28282. server_args.callbacks = &server_cb;
  28283. client_args.signal = &ready;
  28284. client_args.callbacks = &client_cb;
  28285. start_thread(test_server_nofail, &server_args, &serverThread);
  28286. wait_tcp_ready(&server_args);
  28287. test_client_nofail(&client_args, NULL);
  28288. join_thread(serverThread);
  28289. FreeTcpReady(&ready);
  28290. /* check that error code was stored */
  28291. AssertIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  28292. ERR_peek_last_error_line(NULL, &line);
  28293. AssertIntNE(line, 0);
  28294. ERR_peek_last_error_line(&file, NULL);
  28295. AssertNotNull(file);
  28296. #ifdef WOLFSSL_TIRTOS
  28297. fdOpenSession(Task_self());
  28298. #endif
  28299. printf(resultFmt, passed);
  28300. printf("\nTesting error print out\n");
  28301. ERR_print_errors_fp(stdout);
  28302. printf("Done testing print out\n\n");
  28303. fflush(stdout);
  28304. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28305. !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  28306. }
  28307. #endif
  28308. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28309. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  28310. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  28311. {
  28312. (void) ok;
  28313. (void) ctx;
  28314. printf("ENTER verify_cb\n");
  28315. return SSL_SUCCESS;
  28316. }
  28317. #endif
  28318. static void test_wolfSSL_X509_Name_canon(void)
  28319. {
  28320. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  28321. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  28322. defined(WOLFSSL_CERT_GEN) && \
  28323. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  28324. const long ex_hash1 = 0x0fdb2da4;
  28325. const long ex_hash2 = 0x9f3e8c9e;
  28326. X509_NAME *name = NULL;
  28327. X509 *x509 = NULL;
  28328. FILE* file = NULL;
  28329. unsigned long hash = 0;
  28330. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  28331. byte *pbuf = NULL;
  28332. word32 len = 0;
  28333. (void) ex_hash2;
  28334. printf(testingFmt, "test_wolfSSL_X509_Name_canon()");
  28335. file = XFOPEN(caCertFile, "rb");
  28336. AssertNotNull(file);
  28337. AssertNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  28338. AssertNotNull(name = X509_get_issuer_name(x509));
  28339. /* When output buffer is NULL, should return necessary output buffer
  28340. * length.*/
  28341. AssertIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  28342. AssertIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  28343. AssertIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  28344. hash = (((unsigned long)digest[3] << 24) |
  28345. ((unsigned long)digest[2] << 16) |
  28346. ((unsigned long)digest[1] << 8) |
  28347. ((unsigned long)digest[0]));
  28348. AssertIntEQ(hash, ex_hash1);
  28349. XFCLOSE(file);
  28350. X509_free(x509);
  28351. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  28352. pbuf = NULL;
  28353. file = XFOPEN(cliCertFile, "rb");
  28354. AssertNotNull(file);
  28355. AssertNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  28356. AssertNotNull(name = X509_get_issuer_name(x509));
  28357. AssertIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  28358. AssertIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  28359. hash = (((unsigned long)digest[3] << 24) |
  28360. ((unsigned long)digest[2] << 16) |
  28361. ((unsigned long)digest[1] << 8) |
  28362. ((unsigned long)digest[0]));
  28363. AssertIntEQ(hash, ex_hash2);
  28364. XFCLOSE(file);
  28365. X509_free(x509);
  28366. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  28367. printf(resultFmt, passed);
  28368. #endif
  28369. }
  28370. static void test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  28371. {
  28372. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  28373. const int MAX_DIR = 4;
  28374. const char paths[][32] = {
  28375. "./certs/ed25519",
  28376. "./certs/ecc",
  28377. "./certs/crl",
  28378. "./certs/",
  28379. };
  28380. char CertCrl_path[MAX_FILENAME_SZ];
  28381. char *p;
  28382. X509_STORE* str;
  28383. X509_LOOKUP* lookup;
  28384. WOLFSSL_STACK* sk = NULL;
  28385. int len, total_len, i;
  28386. (void) sk;
  28387. printf(testingFmt, "test_wolfSSL_X509_LOOKUP_ctrl_hash_dir()");
  28388. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  28389. /* illegal string */
  28390. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  28391. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  28392. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  28393. SSL_FILETYPE_PEM,NULL), 0);
  28394. /* free store */
  28395. X509_STORE_free(str);
  28396. /* short folder string */
  28397. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  28398. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  28399. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  28400. SSL_FILETYPE_PEM,NULL), 1);
  28401. #if defined(WOLFSSL_INT_H)
  28402. /* only available when including internal.h */
  28403. AssertNotNull(sk = lookup->dirs->dir_entry);
  28404. #endif
  28405. /* free store */
  28406. X509_STORE_free(str);
  28407. /* typical function check */
  28408. p = &CertCrl_path[0];
  28409. total_len = 0;
  28410. for(i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  28411. len = (int)XSTRLEN((const char*)&paths[i]);
  28412. total_len += len;
  28413. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  28414. p += len;
  28415. if (i != 0) *(p++) = SEPARATOR_CHAR;
  28416. }
  28417. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  28418. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  28419. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  28420. SSL_FILETYPE_PEM,NULL), 1);
  28421. #if defined(WOLFSSL_INT_H)
  28422. /* only available when including internal.h */
  28423. AssertNotNull(sk = lookup->dirs->dir_entry);
  28424. #endif
  28425. X509_STORE_free(str);
  28426. printf(resultFmt, passed);
  28427. #endif
  28428. }
  28429. static void test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  28430. {
  28431. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  28432. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  28433. defined(WOLFSSL_SIGNER_DER_CERT)
  28434. X509_STORE_CTX* ctx;
  28435. X509_STORE* str;
  28436. X509_LOOKUP* lookup;
  28437. X509* cert1;
  28438. X509* x509Ca;
  28439. X509* x509Svr;
  28440. X509* issuer;
  28441. WOLFSSL_STACK* sk = NULL;
  28442. X509_NAME* caName;
  28443. X509_NAME* issuerName;
  28444. FILE* file1 = NULL;
  28445. int i, cert_count, cmp;
  28446. char der[] = "certs/ca-cert.der";
  28447. #ifdef HAVE_CRL
  28448. char pem[][100] = {
  28449. "./certs/crl/crl.pem",
  28450. "./certs/crl/crl2.pem",
  28451. "./certs/crl/caEccCrl.pem",
  28452. "./certs/crl/eccCliCRL.pem",
  28453. "./certs/crl/eccSrvCRL.pem",
  28454. ""
  28455. };
  28456. #endif
  28457. printf(testingFmt, "test_wolfSSL_X509_LOOKUP_ctrl_file()");
  28458. AssertNotNull(file1=fopen("./certs/ca-cert.pem", "rb"));
  28459. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  28460. fclose(file1);
  28461. AssertNotNull(ctx = X509_STORE_CTX_new());
  28462. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  28463. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  28464. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  28465. SSL_FILETYPE_PEM,NULL), 1);
  28466. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  28467. AssertIntEQ((cert_count = sk_X509_num(sk)), 1);
  28468. /* check if CA cert is loaded into the store */
  28469. for (i = 0; i < cert_count; i++) {
  28470. x509Ca = sk_X509_value(sk, i);
  28471. AssertIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  28472. }
  28473. AssertNotNull((x509Svr =
  28474. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  28475. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  28476. AssertNull(X509_STORE_CTX_get0_current_issuer(NULL));
  28477. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  28478. AssertNotNull(issuer);
  28479. caName = X509_get_subject_name(x509Ca);
  28480. AssertNotNull(caName);
  28481. issuerName = X509_get_subject_name(issuer);
  28482. AssertNotNull(issuerName);
  28483. cmp = X509_NAME_cmp(caName, issuerName);
  28484. AssertIntEQ(cmp, 0);
  28485. /* load der format */
  28486. X509_free(issuer);
  28487. X509_STORE_CTX_free(ctx);
  28488. X509_STORE_free(str);
  28489. sk_X509_pop_free(sk, NULL);
  28490. X509_free(x509Svr);
  28491. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  28492. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  28493. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  28494. SSL_FILETYPE_ASN1,NULL), 1);
  28495. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  28496. AssertIntEQ((cert_count = sk_X509_num(sk)), 1);
  28497. /* check if CA cert is loaded into the store */
  28498. for (i = 0; i < cert_count; i++) {
  28499. x509Ca = sk_X509_value(sk, i);
  28500. AssertIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  28501. }
  28502. X509_STORE_free(str);
  28503. sk_X509_pop_free(sk, NULL);
  28504. X509_free(cert1);
  28505. #ifdef HAVE_CRL
  28506. AssertNotNull(str = wolfSSL_X509_STORE_new());
  28507. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  28508. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  28509. SSL_FILETYPE_PEM,NULL), 1);
  28510. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  28511. "certs/server-revoked-cert.pem",
  28512. SSL_FILETYPE_PEM,NULL), 1);
  28513. if (str) {
  28514. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  28515. WOLFSSL_FILETYPE_PEM), 1);
  28516. /* since store hasn't yet known the revoked cert*/
  28517. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm,
  28518. "certs/server-revoked-cert.pem",
  28519. WOLFSSL_FILETYPE_PEM), 1);
  28520. }
  28521. for (i = 0; pem[i][0] != '\0'; i++)
  28522. {
  28523. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  28524. SSL_FILETYPE_PEM, NULL), 1);
  28525. }
  28526. if (str) {
  28527. /* since store knows crl list */
  28528. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm,
  28529. "certs/server-revoked-cert.pem",
  28530. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  28531. }
  28532. AssertIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  28533. X509_STORE_free(str);
  28534. #endif
  28535. printf(resultFmt, passed);
  28536. #endif
  28537. }
  28538. static void test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  28539. {
  28540. #if defined(OPENSSL_EXTRA)
  28541. printf(testingFmt, "test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup()");
  28542. X509_STORE_CTX_cleanup(NULL);
  28543. X509_STORE_CTX_trusted_stack(NULL, NULL);
  28544. AssertTrue(1); /* to confirm previous call gives no harm */
  28545. printf(resultFmt, passed);
  28546. #endif
  28547. }
  28548. static void test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  28549. {
  28550. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  28551. #ifdef WOLFSSL_SIGNER_DER_CERT
  28552. int cmp;
  28553. #endif
  28554. X509_STORE_CTX* ctx;
  28555. X509_STORE* str;
  28556. X509* x509Ca;
  28557. X509* x509Svr;
  28558. X509* issuer;
  28559. X509_NAME* caName;
  28560. X509_NAME* issuerName;
  28561. printf(testingFmt, "wolfSSL_X509_STORE_CTX_get0_current_issuer()");
  28562. AssertNotNull(ctx = X509_STORE_CTX_new());
  28563. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  28564. AssertNotNull((x509Ca =
  28565. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  28566. AssertIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  28567. AssertNotNull((x509Svr =
  28568. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  28569. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  28570. AssertNull(X509_STORE_CTX_get0_current_issuer(NULL));
  28571. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  28572. AssertNotNull(issuer);
  28573. caName = X509_get_subject_name(x509Ca);
  28574. AssertNotNull(caName);
  28575. issuerName = X509_get_subject_name(issuer);
  28576. AssertNotNull(issuerName);
  28577. #ifdef WOLFSSL_SIGNER_DER_CERT
  28578. cmp = X509_NAME_cmp(caName, issuerName);
  28579. AssertIntEQ(cmp, 0);
  28580. #endif
  28581. X509_free(issuer);
  28582. X509_STORE_CTX_free(ctx);
  28583. X509_free(x509Svr);
  28584. X509_STORE_free(str);
  28585. X509_free(x509Ca);
  28586. printf(resultFmt, passed);
  28587. #endif
  28588. }
  28589. static void test_wolfSSL_PKCS7_certs(void)
  28590. {
  28591. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  28592. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  28593. STACK_OF(X509)* sk = NULL;
  28594. STACK_OF(X509_INFO)* info_sk = NULL;
  28595. PKCS7 *p7 = NULL;
  28596. BIO* bio;
  28597. const byte* p = NULL;
  28598. int buflen = 0;
  28599. int i;
  28600. printf(testingFmt, "wolfSSL_PKCS7_certs()");
  28601. /* Test twice. Once with d2i and once without to test
  28602. * that everything is free'd correctly. */
  28603. for (i = 0; i < 2; i++) {
  28604. AssertNotNull(p7 = PKCS7_new());
  28605. p7->version = 1;
  28606. p7->hashOID = SHAh;
  28607. AssertNotNull(bio = BIO_new(BIO_s_file()));
  28608. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  28609. AssertNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  28610. AssertIntEQ(sk_X509_INFO_num(info_sk), 2);
  28611. AssertNotNull(sk = sk_X509_new_null());
  28612. while (sk_X509_INFO_num(info_sk)) {
  28613. X509_INFO* info;
  28614. AssertNotNull(info = sk_X509_INFO_shift(info_sk));
  28615. AssertIntEQ(sk_X509_push(sk, info->x509), 1);
  28616. info->x509 = NULL;
  28617. X509_INFO_free(info);
  28618. }
  28619. sk_X509_INFO_free(info_sk);
  28620. BIO_free(bio);
  28621. bio = BIO_new(BIO_s_mem());
  28622. AssertIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  28623. AssertIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  28624. if (i == 0) {
  28625. PKCS7_free(p7);
  28626. AssertNotNull(d2i_PKCS7(&p7, &p, buflen));
  28627. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate them */
  28628. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  28629. /* PKCS7_free free's the certs */
  28630. AssertNotNull(wolfSSL_PKCS7_to_stack(p7));
  28631. }
  28632. BIO_free(bio);
  28633. PKCS7_free(p7);
  28634. }
  28635. printf(resultFmt, passed);
  28636. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  28637. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  28638. }
  28639. static void test_wolfSSL_X509_STORE_CTX(void)
  28640. {
  28641. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28642. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  28643. X509_STORE_CTX* ctx;
  28644. X509_STORE* str;
  28645. X509* x509;
  28646. #ifdef OPENSSL_ALL
  28647. X509* x5092;
  28648. STACK_OF(X509) *sk, *sk2, *sk3;
  28649. #endif
  28650. printf(testingFmt, "wolfSSL_X509_STORE_CTX()");
  28651. AssertNotNull(ctx = X509_STORE_CTX_new());
  28652. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  28653. AssertNotNull((x509 =
  28654. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  28655. AssertIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  28656. #ifdef OPENSSL_ALL
  28657. /* sk_X509_new only in OPENSSL_ALL */
  28658. sk = sk_X509_new();
  28659. AssertNotNull(sk);
  28660. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  28661. #else
  28662. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  28663. #endif
  28664. AssertIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  28665. X509_STORE_CTX_set_error(ctx, -5);
  28666. X509_STORE_CTX_set_error(NULL, -5);
  28667. X509_STORE_CTX_free(ctx);
  28668. #ifdef OPENSSL_ALL
  28669. sk_X509_pop_free(sk, NULL);
  28670. #endif
  28671. X509_STORE_free(str);
  28672. X509_free(x509);
  28673. AssertNotNull(ctx = X509_STORE_CTX_new());
  28674. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  28675. X509_STORE_CTX_free(ctx);
  28676. #ifdef OPENSSL_ALL
  28677. /* test X509_STORE_CTX_get(1)_chain */
  28678. AssertNotNull((x509 = X509_load_certificate_file(svrCertFile,
  28679. SSL_FILETYPE_PEM)));
  28680. AssertNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  28681. SSL_FILETYPE_PEM)));
  28682. AssertNotNull((sk = sk_X509_new()));
  28683. AssertIntEQ(sk_X509_push(sk, x509), 1);
  28684. AssertNotNull((str = X509_STORE_new()));
  28685. AssertNotNull((ctx = X509_STORE_CTX_new()));
  28686. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  28687. AssertNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  28688. AssertNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  28689. AssertIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  28690. AssertNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  28691. AssertNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  28692. AssertIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  28693. X509_STORE_CTX_free(ctx);
  28694. X509_STORE_free(str);
  28695. /* CTX certs not freed yet */
  28696. X509_free(x5092);
  28697. sk_X509_pop_free(sk, NULL);
  28698. /* sk3 is dup so free here */
  28699. sk_X509_pop_free(sk3, NULL);
  28700. #endif
  28701. /* test X509_STORE_CTX_get/set_ex_data */
  28702. {
  28703. int i = 0, tmpData = 5;
  28704. void* tmpDataRet;
  28705. AssertNotNull(ctx = X509_STORE_CTX_new());
  28706. #ifdef HAVE_EX_DATA
  28707. for (i = 0; i < MAX_EX_DATA; i++) {
  28708. AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  28709. WOLFSSL_SUCCESS);
  28710. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  28711. AssertNotNull(tmpDataRet);
  28712. AssertIntEQ(tmpData, *(int*)tmpDataRet);
  28713. }
  28714. #else
  28715. AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  28716. WOLFSSL_FAILURE);
  28717. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  28718. AssertNull(tmpDataRet);
  28719. #endif
  28720. X509_STORE_CTX_free(ctx);
  28721. }
  28722. /* test X509_STORE_get/set_ex_data */
  28723. {
  28724. int i = 0, tmpData = 99;
  28725. void* tmpDataRet;
  28726. AssertNotNull(str = X509_STORE_new());
  28727. #ifdef HAVE_EX_DATA
  28728. for (i = 0; i < MAX_EX_DATA; i++) {
  28729. AssertIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  28730. WOLFSSL_SUCCESS);
  28731. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  28732. AssertNotNull(tmpDataRet);
  28733. AssertIntEQ(tmpData, *(int*)tmpDataRet);
  28734. }
  28735. #else
  28736. AssertIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  28737. WOLFSSL_FAILURE);
  28738. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  28739. AssertNull(tmpDataRet);
  28740. #endif
  28741. X509_STORE_free(str);
  28742. }
  28743. printf(resultFmt, passed);
  28744. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28745. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  28746. }
  28747. static void test_wolfSSL_X509_STORE_set_flags(void)
  28748. {
  28749. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28750. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  28751. X509_STORE* store;
  28752. X509* x509;
  28753. printf(testingFmt, "wolfSSL_X509_STORE_set_flags()");
  28754. AssertNotNull((store = wolfSSL_X509_STORE_new()));
  28755. AssertNotNull((x509 =
  28756. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM)));
  28757. AssertIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  28758. #ifdef HAVE_CRL
  28759. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL), WOLFSSL_SUCCESS);
  28760. #else
  28761. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  28762. NOT_COMPILED_IN);
  28763. #endif
  28764. wolfSSL_X509_free(x509);
  28765. wolfSSL_X509_STORE_free(store);
  28766. printf(resultFmt, passed);
  28767. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28768. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  28769. }
  28770. static void test_wolfSSL_X509_LOOKUP_load_file(void)
  28771. {
  28772. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  28773. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  28774. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  28775. WOLFSSL_X509_STORE* store;
  28776. WOLFSSL_X509_LOOKUP* lookup;
  28777. printf(testingFmt, "wolfSSL_X509_LOOKUP_load_file()");
  28778. AssertNotNull(store = wolfSSL_X509_STORE_new());
  28779. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  28780. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  28781. X509_FILETYPE_PEM), 1);
  28782. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  28783. X509_FILETYPE_PEM), 1);
  28784. if (store) {
  28785. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  28786. WOLFSSL_FILETYPE_PEM), 1);
  28787. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  28788. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  28789. }
  28790. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  28791. X509_FILETYPE_PEM), 1);
  28792. if (store) {
  28793. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  28794. WOLFSSL_FILETYPE_PEM), 1);
  28795. }
  28796. wolfSSL_X509_STORE_free(store);
  28797. printf(resultFmt, passed);
  28798. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  28799. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  28800. }
  28801. static void test_wolfSSL_X509_STORE_CTX_set_time(void)
  28802. {
  28803. #if defined(OPENSSL_EXTRA)
  28804. WOLFSSL_X509_STORE_CTX* ctx;
  28805. time_t c_time;
  28806. printf(testingFmt, "wolfSSL_X509_set_time()");
  28807. AssertNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  28808. c_time = 365*24*60*60;
  28809. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  28810. AssertTrue(
  28811. (ctx->param->flags & WOLFSSL_USE_CHECK_TIME) == WOLFSSL_USE_CHECK_TIME);
  28812. AssertTrue(ctx->param->check_time == c_time);
  28813. wolfSSL_X509_STORE_CTX_free(ctx);
  28814. printf(resultFmt, passed);
  28815. #endif /* OPENSSL_EXTRA */
  28816. }
  28817. static void test_wolfSSL_CTX_get0_set1_param(void)
  28818. {
  28819. #if defined(OPENSSL_EXTRA)
  28820. int ret;
  28821. SSL_CTX* ctx;
  28822. WOLFSSL_X509_VERIFY_PARAM* pParam;
  28823. WOLFSSL_X509_VERIFY_PARAM* pvpm;
  28824. char testIPv4[] = "127.0.0.1";
  28825. char testhostName[] = "foo.hoge.com";
  28826. printf(testingFmt, "wolfSSL_CTX_get0_set1_param()");
  28827. #ifndef NO_WOLFSSL_SERVER
  28828. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28829. #else
  28830. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28831. #endif
  28832. AssertNull(SSL_CTX_get0_param(NULL));
  28833. AssertNotNull(pParam = SSL_CTX_get0_param(ctx));
  28834. pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  28835. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL);
  28836. AssertNotNull(pvpm);
  28837. XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  28838. wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  28839. (int)XSTRLEN(testhostName));
  28840. wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4);
  28841. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  28842. ret = SSL_CTX_set1_param(ctx, pvpm);
  28843. AssertIntEQ(1, ret);
  28844. AssertIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  28845. (int)XSTRLEN(testhostName)));
  28846. AssertIntEQ(0x01, pParam->hostFlags);
  28847. AssertIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  28848. /* test for incorrect patameter */
  28849. AssertIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  28850. AssertIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  28851. AssertIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  28852. SSL_CTX_free(ctx);
  28853. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  28854. printf(resultFmt, passed);
  28855. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  28856. }
  28857. static void test_wolfSSL_get0_param(void)
  28858. {
  28859. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  28860. SSL_CTX* ctx;
  28861. SSL* ssl;
  28862. WOLFSSL_X509_VERIFY_PARAM* pParam;
  28863. printf(testingFmt, "wolfSSL_get0_param()");
  28864. #ifndef NO_WOLFSSL_SERVER
  28865. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28866. #else
  28867. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28868. #endif
  28869. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  28870. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  28871. AssertNotNull(ssl = SSL_new(ctx));
  28872. pParam = SSL_get0_param(ssl);
  28873. (void)pParam;
  28874. SSL_free(ssl);
  28875. SSL_CTX_free(ctx);
  28876. printf(resultFmt, passed);
  28877. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  28878. }
  28879. static void test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  28880. {
  28881. #if defined(OPENSSL_EXTRA)
  28882. const char host[] = "www.example.com";
  28883. WOLFSSL_X509_VERIFY_PARAM* pParam;
  28884. printf(testingFmt, "wolfSSL_X509_VERIFY_PARAM_set1_host()");
  28885. AssertNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  28886. sizeof(WOLFSSL_X509_VERIFY_PARAM),
  28887. HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  28888. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  28889. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  28890. AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  28891. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  28892. AssertIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  28893. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  28894. printf(resultFmt, passed);
  28895. #endif /* OPENSSL_EXTRA */
  28896. }
  28897. static void test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  28898. {
  28899. #if defined(OPENSSL_EXTRA)
  28900. unsigned char buf[16] = {0};
  28901. WOLFSSL_X509_VERIFY_PARAM* param;
  28902. printf(testingFmt, "test_wolfSSL_X509_VERIFY_PARAM_set1_ip()");
  28903. AssertNotNull(param = X509_VERIFY_PARAM_new());
  28904. /* test 127.0.0.1 */
  28905. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  28906. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  28907. AssertIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  28908. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  28909. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  28910. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  28911. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  28912. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  28913. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  28914. AssertIntEQ(XSTRNCMP(param->ipasc,
  28915. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  28916. /* test 2001:db8:: */
  28917. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  28918. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  28919. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  28920. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  28921. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  28922. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  28923. /* test ::1234:5678 */
  28924. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  28925. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  28926. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  28927. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  28928. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  28929. AssertIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  28930. /* test 2001:db8::1234:5678 */
  28931. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  28932. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  28933. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  28934. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  28935. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  28936. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  28937. sizeof(param->ipasc)), 0);
  28938. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  28939. /* 2001:db8:1::ab9:c0a8:102 */
  28940. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  28941. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  28942. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  28943. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  28944. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  28945. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  28946. sizeof(param->ipasc)), 0);
  28947. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  28948. printf(resultFmt, passed);
  28949. #endif /* OPENSSL_EXTRA */
  28950. }
  28951. static void test_wolfSSL_X509_STORE_CTX_get0_store(void)
  28952. {
  28953. #if defined(OPENSSL_EXTRA)
  28954. X509_STORE* store;
  28955. X509_STORE_CTX* ctx;
  28956. X509_STORE_CTX* ctx_no_init;
  28957. printf(testingFmt, "wolfSSL_X509_STORE_CTX_get0_store()");
  28958. AssertNotNull((store = X509_STORE_new()));
  28959. AssertNotNull(ctx = X509_STORE_CTX_new());
  28960. AssertNotNull(ctx_no_init = X509_STORE_CTX_new());
  28961. AssertIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  28962. AssertNull(X509_STORE_CTX_get0_store(NULL));
  28963. /* should return NULL if ctx has not bee initialized */
  28964. AssertNull(X509_STORE_CTX_get0_store(ctx_no_init));
  28965. AssertNotNull(X509_STORE_CTX_get0_store(ctx));
  28966. wolfSSL_X509_STORE_CTX_free(ctx);
  28967. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  28968. X509_STORE_free(store);
  28969. printf(resultFmt, passed);
  28970. #endif /* OPENSSL_EXTRA */
  28971. }
  28972. static void test_wolfSSL_CTX_set_client_CA_list(void)
  28973. {
  28974. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  28975. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  28976. WOLFSSL_CTX* ctx;
  28977. WOLFSSL* ssl;
  28978. X509_NAME* name = NULL;
  28979. STACK_OF(X509_NAME)* names = NULL;
  28980. STACK_OF(X509_NAME)* ca_list = NULL;
  28981. int i, names_len;
  28982. printf(testingFmt, "wolfSSL_CTX_set_client_CA_list()");
  28983. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  28984. /* Send two X501 names in cert request */
  28985. names = SSL_load_client_CA_file(cliCertFile);
  28986. AssertNotNull(names);
  28987. ca_list = SSL_load_client_CA_file(caCertFile);
  28988. AssertNotNull(ca_list);
  28989. AssertIntEQ(sk_X509_NAME_push(names, sk_X509_NAME_value(ca_list, 0)), 1);
  28990. SSL_CTX_set_client_CA_list(ctx, names);
  28991. /* This should only free the stack structure */
  28992. sk_X509_NAME_free(ca_list);
  28993. AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  28994. AssertIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  28995. AssertIntGT((names_len = sk_X509_NAME_num(names)), 0);
  28996. for (i=0; i<names_len; i++) {
  28997. AssertNotNull(name = sk_X509_NAME_value(names, i));
  28998. AssertIntEQ(sk_X509_NAME_find(names, name), i);
  28999. }
  29000. /* Needed to be able to create ssl object */
  29001. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  29002. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  29003. AssertNotNull(ssl = wolfSSL_new(ctx));
  29004. /* load again as old names are responsibility of ctx to free*/
  29005. names = SSL_load_client_CA_file(cliCertFile);
  29006. AssertNotNull(names);
  29007. SSL_set_client_CA_list(ssl, names);
  29008. AssertNotNull(ca_list = SSL_get_client_CA_list(ssl));
  29009. AssertIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  29010. AssertIntGT((names_len = sk_X509_NAME_num(names)), 0);
  29011. for (i=0; i<names_len; i++) {
  29012. AssertNotNull(name = sk_X509_NAME_value(names, i));
  29013. AssertIntEQ(sk_X509_NAME_find(names, name), i);
  29014. }
  29015. printf(resultFmt, passed);
  29016. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  29017. {
  29018. tcp_ready ready;
  29019. func_args server_args;
  29020. callback_functions server_cb;
  29021. THREAD_TYPE serverThread;
  29022. WOLFSSL* ssl_client;
  29023. WOLFSSL_CTX* ctx_client;
  29024. SOCKET_T sockfd = 0;
  29025. printf(testingFmt, "wolfSSL_get_client_CA_list() with handshake");
  29026. StartTCP();
  29027. InitTcpReady(&ready);
  29028. XMEMSET(&server_args, 0, sizeof(func_args));
  29029. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  29030. server_args.signal = &ready;
  29031. server_args.callbacks = &server_cb;
  29032. /* we are responsible for free'ing WOLFSSL_CTX */
  29033. server_cb.ctx = ctx;
  29034. server_cb.isSharedCtx = 1;
  29035. AssertIntEQ(WOLFSSL_SUCCESS,
  29036. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  29037. start_thread(test_server_nofail, &server_args, &serverThread);
  29038. wait_tcp_ready(&server_args);
  29039. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  29040. AssertNotNull(ctx_client = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  29041. AssertIntEQ(WOLFSSL_SUCCESS,
  29042. wolfSSL_CTX_load_verify_locations(ctx_client, caCertFile, 0));
  29043. AssertIntEQ(WOLFSSL_SUCCESS,
  29044. wolfSSL_CTX_use_certificate_file(ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  29045. AssertIntEQ(WOLFSSL_SUCCESS,
  29046. wolfSSL_CTX_use_PrivateKey_file(ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  29047. AssertNotNull(ssl_client = wolfSSL_new(ctx_client));
  29048. AssertIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  29049. AssertIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  29050. AssertNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  29051. /* We are expecting two cert names to be sent */
  29052. AssertIntEQ(sk_X509_NAME_num(ca_list), 2);
  29053. AssertNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  29054. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  29055. AssertNotNull(name = sk_X509_NAME_value(ca_list, i));
  29056. AssertIntGE(sk_X509_NAME_find(names, name), 0);
  29057. }
  29058. wolfSSL_shutdown(ssl_client);
  29059. wolfSSL_free(ssl_client);
  29060. wolfSSL_CTX_free(ctx_client);
  29061. join_thread(serverThread);
  29062. FreeTcpReady(&ready);
  29063. printf(resultFmt, passed);
  29064. }
  29065. #endif
  29066. wolfSSL_free(ssl);
  29067. wolfSSL_CTX_free(ctx);
  29068. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT && !NO_BIO */
  29069. }
  29070. static void test_wolfSSL_CTX_add_client_CA(void)
  29071. {
  29072. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  29073. !defined(NO_WOLFSSL_CLIENT)
  29074. WOLFSSL_CTX* ctx;
  29075. WOLFSSL_X509* x509;
  29076. WOLFSSL_X509* x509_a;
  29077. STACK_OF(X509_NAME)* ca_list;
  29078. int ret = 0;
  29079. printf(testingFmt, "wolfSSL_CTX_add_client_CA()");
  29080. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29081. /* Add client cert */
  29082. x509 = X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
  29083. AssertNotNull(x509);
  29084. ret = SSL_CTX_add_client_CA(ctx, x509);
  29085. AssertIntEQ(ret, SSL_SUCCESS);
  29086. AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  29087. /* Add another client cert */
  29088. AssertNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  29089. SSL_FILETYPE_PEM));
  29090. AssertIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  29091. /* test for incorrect parameter */
  29092. AssertIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  29093. AssertIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  29094. AssertIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  29095. X509_free(x509);
  29096. X509_free(x509_a);
  29097. SSL_CTX_free(ctx);
  29098. printf(resultFmt, passed);
  29099. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  29100. }
  29101. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  29102. static THREAD_RETURN WOLFSSL_THREAD server_task(void* args)
  29103. {
  29104. callback_functions* callbacks = ((func_args*)args)->callbacks;
  29105. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(callbacks->method());
  29106. WOLFSSL* ssl = NULL;
  29107. SOCKET_T sfd = 0;
  29108. SOCKET_T cfd = 0;
  29109. word16 port;
  29110. char msg[] = "I hear you fa shizzle!";
  29111. int len = (int) XSTRLEN(msg);
  29112. char input[1024];
  29113. int idx;
  29114. int ret, err = 0;
  29115. #ifdef WOLFSSL_TIRTOS
  29116. fdOpenSession(Task_self());
  29117. #endif
  29118. ((func_args*)args)->return_code = TEST_FAIL;
  29119. port = ((func_args*)args)->signal->port;
  29120. AssertIntEQ(WOLFSSL_SUCCESS,
  29121. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  29122. AssertIntEQ(WOLFSSL_SUCCESS,
  29123. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  29124. WOLFSSL_FILETYPE_PEM));
  29125. AssertIntEQ(WOLFSSL_SUCCESS,
  29126. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  29127. WOLFSSL_FILETYPE_PEM));
  29128. if (callbacks->ctx_ready)
  29129. callbacks->ctx_ready(ctx);
  29130. ssl = wolfSSL_new(ctx);
  29131. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  29132. CloseSocket(sfd);
  29133. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  29134. if (callbacks->ssl_ready)
  29135. callbacks->ssl_ready(ssl);
  29136. do {
  29137. err = 0; /* Reset error */
  29138. ret = wolfSSL_accept(ssl);
  29139. if (ret != WOLFSSL_SUCCESS) {
  29140. err = wolfSSL_get_error(ssl, 0);
  29141. }
  29142. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  29143. if (ret != WOLFSSL_SUCCESS) {
  29144. char buff[WOLFSSL_MAX_ERROR_SZ];
  29145. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  29146. }
  29147. else {
  29148. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  29149. input[idx] = 0;
  29150. printf("Client message: %s\n", input);
  29151. }
  29152. AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
  29153. #ifdef WOLFSSL_TIRTOS
  29154. Task_yield();
  29155. #endif
  29156. ((func_args*)args)->return_code = TEST_SUCCESS;
  29157. }
  29158. if (callbacks->on_result)
  29159. callbacks->on_result(ssl);
  29160. wolfSSL_shutdown(ssl);
  29161. wolfSSL_free(ssl);
  29162. wolfSSL_CTX_free(ctx);
  29163. CloseSocket(cfd);
  29164. #ifdef WOLFSSL_TIRTOS
  29165. fdCloseSession(Task_self());
  29166. #endif
  29167. #ifndef WOLFSSL_TIRTOS
  29168. return 0;
  29169. #endif
  29170. }
  29171. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  29172. {
  29173. AssertNotNull(ssl);
  29174. AssertNotNull(line);
  29175. XFILE fp;
  29176. const byte lf = '\n';
  29177. fp = XFOPEN("./MyKeyLog.txt", "a");
  29178. XFWRITE( line, 1, strlen(line),fp);
  29179. XFWRITE( (void*)&lf,1,1,fp);
  29180. XFCLOSE(fp);
  29181. }
  29182. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  29183. static void test_wolfSSL_CTX_set_keylog_callback(void)
  29184. {
  29185. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  29186. SSL_CTX* ctx;
  29187. printf( testingFmt, "wolfSSL_CTX_set_keylog_callback()");
  29188. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29189. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  29190. SSL_CTX_free(ctx);
  29191. SSL_CTX_set_keylog_callback(NULL, NULL);
  29192. printf(resultFmt, passed);
  29193. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  29194. }
  29195. static void test_wolfSSL_CTX_get_keylog_callback(void)
  29196. {
  29197. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  29198. SSL_CTX* ctx;
  29199. printf( testingFmt, "wolfSSL_CTX_get_keylog_callback()");
  29200. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29201. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  29202. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  29203. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  29204. SSL_CTX_set_keylog_callback(ctx, NULL );
  29205. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  29206. SSL_CTX_free(ctx);
  29207. printf(resultFmt, passed);
  29208. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  29209. }
  29210. static void test_wolfSSL_Tls12_Key_Logging_test(void)
  29211. {
  29212. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  29213. /* This test is intended for checking whether keylog callback is called
  29214. * in client during TLS handshake between the client and a server.
  29215. */
  29216. tcp_ready ready;
  29217. func_args client_args;
  29218. func_args server_args;
  29219. THREAD_TYPE serverThread;
  29220. callback_functions server_cbf;
  29221. callback_functions client_cbf;
  29222. SOCKET_T sockfd = 0;
  29223. WOLFSSL_CTX* ctx;
  29224. WOLFSSL* ssl;
  29225. XFILE fp;
  29226. char msg[64] = "hello wolfssl!";
  29227. char reply[1024];
  29228. int msgSz = (int)XSTRLEN(msg);
  29229. printf(testingFmt, "wolfSSL_Tls12_Key_Logging_test()");
  29230. #ifdef WOLFSSL_TIRTOS
  29231. fdOpenSession(Task_self());
  29232. #endif
  29233. InitTcpReady(&ready);
  29234. ready.port = 22222;
  29235. XMEMSET(&client_args, 0, sizeof(func_args));
  29236. XMEMSET(&server_args, 0, sizeof(func_args));
  29237. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  29238. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  29239. server_cbf.method = wolfTLSv1_2_server_method;
  29240. server_args.callbacks = &server_cbf;
  29241. server_args.signal = &ready;
  29242. /* clean up keylog file */
  29243. fp = XFOPEN("./MyKeyLog.txt", "w");
  29244. XFCLOSE(fp);
  29245. /* start server task */
  29246. start_thread(server_task, &server_args, &serverThread);
  29247. wait_tcp_ready(&server_args);
  29248. /* run as a TLS1.2 client */
  29249. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  29250. AssertIntEQ(WOLFSSL_SUCCESS,
  29251. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  29252. AssertIntEQ(WOLFSSL_SUCCESS,
  29253. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  29254. AssertIntEQ(WOLFSSL_SUCCESS,
  29255. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  29256. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  29257. /* set keylog callback */
  29258. wolfSSL_CTX_set_keylog_callback(ctx,keyLog_callback);
  29259. /* get connected the server task */
  29260. AssertNotNull(ssl = wolfSSL_new(ctx));
  29261. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  29262. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  29263. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  29264. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  29265. wolfSSL_shutdown(ssl);
  29266. wolfSSL_free(ssl);
  29267. wolfSSL_CTX_free(ctx);
  29268. CloseSocket(sockfd);
  29269. join_thread(serverThread);
  29270. FreeTcpReady(&ready);
  29271. #ifdef WOLFSSL_TIRTOS
  29272. fdOpenSession(Task_self());
  29273. #endif
  29274. /* check if the keylog file exists */
  29275. char buff[300] = {0};
  29276. int found = 0;
  29277. fp = XFOPEN("./MyKeyLog.txt", "r");
  29278. AssertNotNull(fp);
  29279. while(XFGETS( buff, (int)sizeof(buff),fp) != NULL ) {
  29280. if(0 == strncmp(buff,"CLIENT_RANDOM ",
  29281. sizeof("CLIENT_RANDOM ")-1)) {
  29282. found = 1;
  29283. break;
  29284. }
  29285. }
  29286. XFCLOSE(fp);
  29287. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  29288. AssertNotNull( found );
  29289. printf(resultFmt, passed);
  29290. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  29291. }
  29292. static void test_wolfSSL_Tls13_Key_Logging_test(void)
  29293. {
  29294. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  29295. defined(HAVE_SECRET_CALLBACK)
  29296. /* This test is intended for checking whether keylog callback is called
  29297. * in client during TLS handshake between the client and a server.
  29298. */
  29299. tcp_ready ready;
  29300. func_args client_args;
  29301. func_args server_args;
  29302. THREAD_TYPE serverThread;
  29303. callback_functions server_cbf;
  29304. callback_functions client_cbf;
  29305. SOCKET_T sockfd = 0;
  29306. WOLFSSL_CTX* ctx;
  29307. WOLFSSL* ssl;
  29308. XFILE fp;
  29309. char msg[64] = "hello wolfssl!";
  29310. char reply[1024];
  29311. int msgSz = (int)XSTRLEN(msg);
  29312. printf(testingFmt, "wolfSSL_Tls13_Key_Logging_test()");
  29313. #ifdef WOLFSSL_TIRTOS
  29314. fdOpenSession(Task_self());
  29315. #endif
  29316. InitTcpReady(&ready);
  29317. ready.port = 22222;
  29318. XMEMSET(&client_args, 0, sizeof(func_args));
  29319. XMEMSET(&server_args, 0, sizeof(func_args));
  29320. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  29321. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  29322. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  29323. server_args.callbacks = &server_cbf;
  29324. server_args.signal = &ready;
  29325. /* clean up keylog file */
  29326. fp = XFOPEN("./MyKeyLog.txt", "w");
  29327. XFCLOSE(fp);
  29328. /* start server task */
  29329. start_thread(server_task, &server_args, &serverThread);
  29330. wait_tcp_ready(&server_args);
  29331. /* run as a TLS1.3 client */
  29332. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  29333. AssertIntEQ(WOLFSSL_SUCCESS,
  29334. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  29335. AssertIntEQ(WOLFSSL_SUCCESS,
  29336. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  29337. AssertIntEQ(WOLFSSL_SUCCESS,
  29338. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  29339. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  29340. /* set keylog callback */
  29341. wolfSSL_CTX_set_keylog_callback(ctx,keyLog_callback);
  29342. /* get connected the server task */
  29343. AssertNotNull(ssl = wolfSSL_new(ctx));
  29344. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  29345. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  29346. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  29347. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  29348. wolfSSL_free(ssl);
  29349. wolfSSL_CTX_free(ctx);
  29350. join_thread(serverThread);
  29351. FreeTcpReady(&ready);
  29352. #ifdef WOLFSSL_TIRTOS
  29353. fdOpenSession(Task_self());
  29354. #endif
  29355. /* check if the keylog file exists */
  29356. {
  29357. char buff[300] = {0};
  29358. int found[4] = {0};
  29359. int numfnd = 0;
  29360. int i;
  29361. fp = XFOPEN("./MyKeyLog.txt", "r");
  29362. AssertNotNull(fp);
  29363. while (XFGETS( buff, (int)sizeof(buff),fp) != NULL ) {
  29364. if (0 == strncmp(buff,"CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  29365. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  29366. found[0] = 1;
  29367. continue;
  29368. }
  29369. else if (0 == strncmp(buff,"SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  29370. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  29371. found[1] = 1;
  29372. continue;
  29373. }
  29374. else if (0 == strncmp(buff,"CLIENT_TRAFFIC_SECRET_0 ",
  29375. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  29376. found[2] = 1;
  29377. continue;
  29378. }
  29379. else if (0 == strncmp(buff,"SERVER_TRAFFIC_SECRET_0 ",
  29380. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  29381. found[3] = 1;
  29382. continue;
  29383. }
  29384. }
  29385. XFCLOSE(fp);
  29386. for (i = 0; i < 4; i++) {
  29387. if( found[i] != 0)
  29388. numfnd++;
  29389. }
  29390. AssertIntEQ(numfnd, 4);
  29391. }
  29392. printf(resultFmt, passed);
  29393. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  29394. }
  29395. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  29396. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29397. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  29398. static void post_auth_version_cb(WOLFSSL* ssl)
  29399. {
  29400. /* do handshake and then test version error */
  29401. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  29402. AssertStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  29403. AssertIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  29404. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  29405. /* check was added to error queue */
  29406. AssertIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  29407. /* check the string matches expected string */
  29408. AssertStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  29409. "WRONG_SSL_VERSION");
  29410. #endif
  29411. }
  29412. static void post_auth_cb(WOLFSSL* ssl)
  29413. {
  29414. WOLFSSL_X509* x509;
  29415. /* do handshake and then test version error */
  29416. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  29417. AssertStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  29418. AssertNull(x509 = wolfSSL_get_peer_certificate(ssl));
  29419. wolfSSL_X509_free(x509);
  29420. AssertIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  29421. }
  29422. static void set_post_auth_cb(WOLFSSL* ssl)
  29423. {
  29424. if (!wolfSSL_is_server(ssl)) {
  29425. AssertIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  29426. }
  29427. else {
  29428. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  29429. }
  29430. }
  29431. #endif
  29432. static void test_wolfSSL_Tls13_postauth(void)
  29433. {
  29434. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  29435. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29436. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  29437. tcp_ready ready;
  29438. func_args client_args;
  29439. func_args server_args;
  29440. callback_functions server_cbf;
  29441. callback_functions client_cbf;
  29442. THREAD_TYPE serverThread;
  29443. printf(testingFmt, "wolfSSL_Tls13_postauth()");
  29444. XMEMSET(&client_args, 0, sizeof(func_args));
  29445. XMEMSET(&server_args, 0, sizeof(func_args));
  29446. StartTCP();
  29447. InitTcpReady(&ready);
  29448. #if defined(USE_WINDOWS_API)
  29449. /* use RNG to get random port if using windows */
  29450. ready.port = GetRandomPort();
  29451. #endif
  29452. server_args.signal = &ready;
  29453. client_args.signal = &ready;
  29454. /* test version failure doing post auth with TLS 1.2 connection */
  29455. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  29456. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  29457. server_cbf.method = wolfTLSv1_2_server_method;
  29458. server_cbf.ssl_ready = set_post_auth_cb;
  29459. client_cbf.ssl_ready = set_post_auth_cb;
  29460. server_cbf.on_result = post_auth_version_cb;
  29461. server_args.callbacks = &server_cbf;
  29462. client_args.callbacks = &client_cbf;
  29463. start_thread(test_server_nofail, &server_args, &serverThread);
  29464. wait_tcp_ready(&server_args);
  29465. test_client_nofail(&client_args, NULL);
  29466. join_thread(serverThread);
  29467. /* tests on post auth with TLS 1.3 */
  29468. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  29469. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  29470. server_cbf.method = wolfTLSv1_3_server_method;
  29471. server_cbf.ssl_ready = set_post_auth_cb;
  29472. client_cbf.ssl_ready = set_post_auth_cb;
  29473. server_cbf.on_result = post_auth_cb;
  29474. server_args.callbacks = &server_cbf;
  29475. client_args.callbacks = &client_cbf;
  29476. start_thread(test_server_nofail, &server_args, &serverThread);
  29477. wait_tcp_ready(&server_args);
  29478. test_client_nofail(&client_args, NULL);
  29479. join_thread(serverThread);
  29480. FreeTcpReady(&ready);
  29481. printf(resultFmt, passed);
  29482. #endif
  29483. }
  29484. static void test_wolfSSL_X509_NID(void)
  29485. {
  29486. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  29487. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  29488. int sigType;
  29489. int nameSz;
  29490. X509* cert;
  29491. EVP_PKEY* pubKeyTmp;
  29492. X509_NAME* name;
  29493. char commonName[80];
  29494. char countryName[80];
  29495. char localityName[80];
  29496. char stateName[80];
  29497. char orgName[80];
  29498. char orgUnit[80];
  29499. printf(testingFmt, "wolfSSL_X509_NID()");
  29500. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  29501. /* convert cert from DER to internal WOLFSSL_X509 struct */
  29502. AssertNotNull(cert = wolfSSL_X509_d2i(&cert, client_cert_der_2048,
  29503. sizeof_client_cert_der_2048));
  29504. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  29505. /* extract PUBLIC KEY from cert */
  29506. AssertNotNull(pubKeyTmp = X509_get_pubkey(cert));
  29507. /* extract signatureType */
  29508. AssertIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  29509. /* extract subjectName info */
  29510. AssertNotNull(name = X509_get_subject_name(cert));
  29511. AssertIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  29512. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  29513. NULL, 0)), 0);
  29514. AssertIntEQ(nameSz, 15);
  29515. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  29516. commonName, sizeof(commonName))), 0);
  29517. AssertIntEQ(nameSz, 15);
  29518. AssertIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  29519. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  29520. commonName, 9)), 0);
  29521. AssertIntEQ(nameSz, 8);
  29522. AssertIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  29523. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  29524. countryName, sizeof(countryName))), 0);
  29525. AssertIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  29526. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  29527. localityName, sizeof(localityName))), 0);
  29528. AssertIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  29529. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_stateOrProvinceName,
  29530. stateName, sizeof(stateName))), 0);
  29531. AssertIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  29532. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  29533. orgName, sizeof(orgName))), 0);
  29534. AssertIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  29535. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationalUnitName,
  29536. orgUnit, sizeof(orgUnit))), 0);
  29537. AssertIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  29538. EVP_PKEY_free(pubKeyTmp);
  29539. X509_free(cert);
  29540. printf(resultFmt, passed);
  29541. #endif
  29542. }
  29543. static void test_wolfSSL_CTX_set_srp_username(void)
  29544. {
  29545. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  29546. && !defined(NO_SHA256) && !defined(WC_NO_RNG)
  29547. WOLFSSL_CTX* ctx;
  29548. WOLFSSL* ssl;
  29549. const char *username = "TESTUSER";
  29550. const char *password = "TESTPASSWORD";
  29551. int r;
  29552. printf(testingFmt, "wolfSSL_CTX_set_srp_username()");
  29553. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  29554. AssertNotNull(ctx);
  29555. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  29556. AssertIntEQ(r,SSL_SUCCESS);
  29557. wolfSSL_CTX_free(ctx);
  29558. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  29559. AssertNotNull(ctx);
  29560. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  29561. AssertIntEQ(r,SSL_SUCCESS);
  29562. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  29563. AssertIntEQ(r,SSL_SUCCESS);
  29564. AssertNotNull(ssl = SSL_new(ctx));
  29565. AssertNotNull(SSL_get_srp_username(ssl));
  29566. AssertStrEQ(SSL_get_srp_username(ssl), username);
  29567. wolfSSL_free(ssl);
  29568. wolfSSL_CTX_free(ctx);
  29569. printf(resultFmt, passed);
  29570. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  29571. /* && !NO_SHA256 && !WC_NO_RNG */
  29572. }
  29573. static void test_wolfSSL_CTX_set_srp_password(void)
  29574. {
  29575. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  29576. && !defined(NO_SHA256) && !defined(WC_NO_RNG)
  29577. WOLFSSL_CTX* ctx;
  29578. const char *username = "TESTUSER";
  29579. const char *password = "TESTPASSWORD";
  29580. int r;
  29581. printf(testingFmt, "wolfSSL_CTX_set_srp_password()");
  29582. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  29583. AssertNotNull(ctx);
  29584. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  29585. AssertIntEQ(r,SSL_SUCCESS);
  29586. wolfSSL_CTX_free(ctx);
  29587. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  29588. AssertNotNull(ctx);
  29589. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  29590. AssertIntEQ(r,SSL_SUCCESS);
  29591. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  29592. AssertIntEQ(r,SSL_SUCCESS);
  29593. wolfSSL_CTX_free(ctx);
  29594. printf(resultFmt, passed);
  29595. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  29596. /* && !NO_SHA256 && !WC_NO_RNG */
  29597. }
  29598. static void test_wolfSSL_X509_STORE(void)
  29599. {
  29600. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  29601. X509_STORE *store;
  29602. #ifdef HAVE_CRL
  29603. X509_STORE_CTX *storeCtx;
  29604. X509_CRL *crl;
  29605. X509 *ca, *cert;
  29606. const char crlPem[] = "./certs/crl/crl.revoked";
  29607. const char srvCert[] = "./certs/server-revoked-cert.pem";
  29608. const char caCert[] = "./certs/ca-cert.pem";
  29609. XFILE fp;
  29610. printf(testingFmt, "test_wolfSSL_X509_STORE");
  29611. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  29612. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  29613. SSL_FILETYPE_PEM)));
  29614. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  29615. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  29616. SSL_FILETYPE_PEM)));
  29617. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  29618. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  29619. AssertIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  29620. X509_STORE_free(store);
  29621. X509_STORE_CTX_free(storeCtx);
  29622. X509_free(cert);
  29623. X509_free(ca);
  29624. /* should fail to verify now after adding in CRL */
  29625. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  29626. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  29627. SSL_FILETYPE_PEM)));
  29628. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  29629. fp = XFOPEN(crlPem, "rb");
  29630. AssertTrue((fp != XBADFILE));
  29631. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  29632. NULL, NULL));
  29633. XFCLOSE(fp);
  29634. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  29635. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  29636. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  29637. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  29638. SSL_FILETYPE_PEM)));
  29639. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  29640. AssertIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  29641. AssertIntEQ(X509_STORE_CTX_get_error(storeCtx), CRL_CERT_REVOKED);
  29642. X509_CRL_free(crl);
  29643. X509_STORE_free(store);
  29644. X509_STORE_CTX_free(storeCtx);
  29645. X509_free(cert);
  29646. X509_free(ca);
  29647. #endif /* HAVE_CRL */
  29648. #ifndef WOLFCRYPT_ONLY
  29649. {
  29650. SSL_CTX* ctx;
  29651. SSL* ssl;
  29652. int i;
  29653. for (i = 0; i < 2; i++) {
  29654. #ifndef NO_WOLFSSL_SERVER
  29655. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29656. #else
  29657. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29658. #endif
  29659. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  29660. SSL_CTX_set_cert_store(ctx, store);
  29661. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  29662. SSL_CTX_set_cert_store(ctx, store);
  29663. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  29664. AssertIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  29665. SSL_FILETYPE_PEM), SSL_SUCCESS);
  29666. AssertIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  29667. SSL_FILETYPE_PEM), SSL_SUCCESS);
  29668. AssertNotNull(ssl = SSL_new(ctx));
  29669. if (i == 0) {
  29670. AssertIntEQ(SSL_set0_verify_cert_store(ssl, store), SSL_SUCCESS);
  29671. }
  29672. else {
  29673. AssertIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  29674. X509_STORE_free(store);
  29675. }
  29676. SSL_free(ssl);
  29677. SSL_CTX_free(ctx);
  29678. }
  29679. }
  29680. #endif
  29681. printf(resultFmt, passed);
  29682. #endif
  29683. return;
  29684. }
  29685. static void test_wolfSSL_X509_STORE_load_locations(void)
  29686. {
  29687. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  29688. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  29689. SSL_CTX *ctx;
  29690. X509_STORE *store;
  29691. const char ca_file[] = "./certs/ca-cert.pem";
  29692. const char client_pem_file[] = "./certs/client-cert.pem";
  29693. const char client_der_file[] = "./certs/client-cert.der";
  29694. const char ecc_file[] = "./certs/ecc-key.pem";
  29695. const char certs_path[] = "./certs/";
  29696. const char bad_path[] = "./bad-path/";
  29697. #ifdef HAVE_CRL
  29698. const char crl_path[] = "./certs/crl/";
  29699. const char crl_file[] = "./certs/crl/crl.pem";
  29700. #endif
  29701. printf(testingFmt, "wolfSSL_X509_STORE_load_locations");
  29702. #ifndef NO_WOLFSSL_SERVER
  29703. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  29704. #else
  29705. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  29706. #endif
  29707. AssertNotNull(store = SSL_CTX_get_cert_store(ctx));
  29708. AssertIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL), WOLFSSL_SUCCESS);
  29709. /* Test bad arguments */
  29710. AssertIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL), WOLFSSL_FAILURE);
  29711. AssertIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  29712. AssertIntEQ(X509_STORE_load_locations(store, client_der_file, NULL), WOLFSSL_FAILURE);
  29713. AssertIntEQ(X509_STORE_load_locations(store, ecc_file, NULL), WOLFSSL_FAILURE);
  29714. AssertIntEQ(X509_STORE_load_locations(store, NULL, bad_path), WOLFSSL_FAILURE);
  29715. #ifdef HAVE_CRL
  29716. /* Test with CRL */
  29717. AssertIntEQ(X509_STORE_load_locations(store, crl_file, NULL), WOLFSSL_SUCCESS);
  29718. AssertIntEQ(X509_STORE_load_locations(store, NULL, crl_path), WOLFSSL_SUCCESS);
  29719. #endif
  29720. /* Test with CA */
  29721. AssertIntEQ(X509_STORE_load_locations(store, ca_file, NULL), WOLFSSL_SUCCESS);
  29722. /* Test with client_cert and certs path */
  29723. AssertIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL), WOLFSSL_SUCCESS);
  29724. AssertIntEQ(X509_STORE_load_locations(store, NULL, certs_path), WOLFSSL_SUCCESS);
  29725. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  29726. /* Clear nodes */
  29727. ERR_clear_error();
  29728. #endif
  29729. SSL_CTX_free(ctx);
  29730. printf(resultFmt, passed);
  29731. #endif
  29732. }
  29733. static void test_X509_STORE_get0_objects(void)
  29734. {
  29735. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  29736. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  29737. X509_STORE *store;
  29738. X509_STORE *store_cpy;
  29739. SSL_CTX *ctx;
  29740. X509_OBJECT *obj;
  29741. STACK_OF(X509_OBJECT) *objs;
  29742. int i;
  29743. printf(testingFmt, "wolfSSL_X509_STORE_get0_objects");
  29744. /* Setup store */
  29745. #ifndef NO_WOLFSSL_SERVER
  29746. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  29747. #else
  29748. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  29749. #endif
  29750. AssertNotNull(store_cpy = X509_STORE_new());
  29751. AssertNotNull(store = SSL_CTX_get_cert_store(ctx));
  29752. AssertIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL), WOLFSSL_SUCCESS);
  29753. AssertIntEQ(X509_STORE_load_locations(store, caCertFile, NULL), WOLFSSL_SUCCESS);
  29754. AssertIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL), WOLFSSL_SUCCESS);
  29755. #ifdef HAVE_CRL
  29756. AssertIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir), WOLFSSL_SUCCESS);
  29757. #endif
  29758. /* Store ready */
  29759. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  29760. AssertNotNull(objs = X509_STORE_get0_objects(store));
  29761. #ifdef HAVE_CRL
  29762. #ifdef WOLFSSL_SIGNER_DER_CERT
  29763. AssertIntEQ(sk_X509_OBJECT_num(objs), 4);
  29764. #else
  29765. AssertIntEQ(sk_X509_OBJECT_num(objs), 1);
  29766. #endif
  29767. #else
  29768. #ifdef WOLFSSL_SIGNER_DER_CERT
  29769. AssertIntEQ(sk_X509_OBJECT_num(objs), 3);
  29770. #else
  29771. AssertIntEQ(sk_X509_OBJECT_num(objs), 0);
  29772. #endif
  29773. #endif
  29774. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  29775. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  29776. switch (X509_OBJECT_get_type(obj)) {
  29777. case X509_LU_X509:
  29778. AssertNotNull(X509_OBJECT_get0_X509(obj));
  29779. AssertIntEQ(X509_STORE_add_cert(store_cpy,
  29780. X509_OBJECT_get0_X509(obj)), WOLFSSL_SUCCESS);
  29781. break;
  29782. case X509_LU_CRL:
  29783. #ifdef HAVE_CRL
  29784. AssertNotNull(X509_OBJECT_get0_X509_CRL(obj));
  29785. AssertIntEQ(X509_STORE_add_crl(store_cpy,
  29786. X509_OBJECT_get0_X509_CRL(obj)), WOLFSSL_SUCCESS);
  29787. break;
  29788. #endif
  29789. case X509_LU_NONE:
  29790. default:
  29791. Fail(("X509_OBJECT_get_type should return x509 or crl "
  29792. "(when built with crl support)"),
  29793. ("Unrecognized X509_OBJECT type or none"));
  29794. }
  29795. }
  29796. X509_STORE_free(store_cpy);
  29797. SSL_CTX_free(ctx);
  29798. printf(resultFmt, passed);
  29799. #endif
  29800. }
  29801. static void test_wolfSSL_BN(void)
  29802. {
  29803. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  29804. BIGNUM* a;
  29805. BIGNUM* b;
  29806. BIGNUM* c;
  29807. BIGNUM* d;
  29808. ASN1_INTEGER* ai;
  29809. printf(testingFmt, "wolfSSL_BN()");
  29810. AssertNotNull(b = BN_new());
  29811. AssertNotNull(c = BN_new());
  29812. AssertNotNull(d = BN_new());
  29813. ai = ASN1_INTEGER_new();
  29814. AssertNotNull(ai);
  29815. /* at the moment hard setting since no set function */
  29816. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  29817. ai->data[1] = 0x01; /* length of integer */
  29818. ai->data[2] = 0x03;
  29819. AssertNotNull(a = ASN1_INTEGER_to_BN(ai, NULL));
  29820. ASN1_INTEGER_free(ai);
  29821. AssertIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  29822. AssertIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  29823. /* a + 3 = */
  29824. AssertIntEQ(BN_add_word(NULL, 3), WOLFSSL_FAILURE);
  29825. AssertIntEQ(BN_add_word(a, 3), WOLFSSL_SUCCESS);
  29826. /* check result 3 + 3*/
  29827. AssertIntEQ(BN_get_word(a), 6);
  29828. /* set a back to 3 */
  29829. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  29830. /* a - 3 = */
  29831. AssertIntEQ(BN_sub_word(NULL, 3), WOLFSSL_FAILURE);
  29832. AssertIntEQ(BN_sub_word(a, 3), WOLFSSL_SUCCESS);
  29833. /* check result 3 - 3*/
  29834. AssertIntEQ(BN_get_word(a), 0);
  29835. /* set a back to 3 */
  29836. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  29837. /* a^b mod c = */
  29838. AssertIntEQ(BN_mod_exp(d, NULL, b, c, NULL), WOLFSSL_FAILURE);
  29839. AssertIntEQ(BN_mod_exp(d, a, b, c, NULL), WOLFSSL_SUCCESS);
  29840. /* check result 3^2 mod 5 */
  29841. AssertIntEQ(BN_get_word(d), 4);
  29842. /* a*b = */
  29843. AssertIntEQ(BN_mul(d, NULL, b, NULL), WOLFSSL_FAILURE);
  29844. AssertIntEQ(BN_mul(d, a, b, NULL), WOLFSSL_SUCCESS);
  29845. /* check result 3*2 */
  29846. AssertIntEQ(BN_get_word(d), 6);
  29847. /* c/b => db + a */
  29848. AssertIntEQ(BN_div(d, NULL, c, b, NULL), WOLFSSL_FAILURE);
  29849. AssertIntEQ(BN_div(d, a, c, b, NULL), WOLFSSL_SUCCESS);
  29850. /* check result 5/2 */
  29851. AssertIntEQ(BN_get_word(d), 2); /* check quotient */
  29852. AssertIntEQ(BN_get_word(a), 1); /* check remainder */
  29853. /* set a back to 3 */
  29854. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  29855. /* a*b mod c = */
  29856. AssertIntEQ(BN_mod_mul(d, NULL, b, c, NULL), SSL_FAILURE);
  29857. AssertIntEQ(BN_mod_mul(d, a, b, c, NULL), SSL_SUCCESS);
  29858. /* check result 3*2 mod 5 */
  29859. AssertIntEQ(BN_get_word(d), 1);
  29860. AssertIntEQ(BN_set_word(a, 16), SSL_SUCCESS);
  29861. AssertIntEQ(BN_set_word(b, 24), SSL_SUCCESS);
  29862. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  29863. /* gcd of a and b */
  29864. AssertIntEQ(BN_gcd(d, NULL, b, NULL), SSL_FAILURE);
  29865. AssertIntEQ(BN_gcd(d, a, b, NULL), SSL_SUCCESS);
  29866. /* check result gcd(16, 24) */
  29867. AssertIntEQ(BN_get_word(d), 8);
  29868. #endif /* !NO_RSA && WOLFSSL_KEY_GEN */
  29869. AssertIntEQ(BN_set_word(a, 1 << 6), SSL_SUCCESS);
  29870. AssertIntEQ(BN_rshift(b, a, 6), SSL_SUCCESS);
  29871. AssertIntEQ(BN_is_zero(b), 0);
  29872. AssertIntEQ(BN_rshift(b, a, 7), SSL_SUCCESS);
  29873. AssertIntEQ(BN_is_zero(b), 1);
  29874. AssertIntEQ(BN_rshift1(b, a), SSL_SUCCESS);
  29875. AssertIntEQ(BN_is_zero(b), 0);
  29876. /* set b back to 2 */
  29877. AssertIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  29878. {
  29879. /* BN_mod_inverse test */
  29880. BIGNUM *r = BN_new();
  29881. BIGNUM *val = BN_mod_inverse(r,b,c,NULL);
  29882. AssertIntEQ((int)(BN_get_word(r) & 0x03), 3);
  29883. BN_free(val);
  29884. }
  29885. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  29886. defined(WOLFSSL_SP_INT_NEGATIVE))
  29887. AssertIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  29888. AssertIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  29889. AssertIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  29890. AssertIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  29891. AssertIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  29892. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  29893. {
  29894. char* ret;
  29895. AssertNotNull(ret = BN_bn2dec(c));
  29896. AssertIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  29897. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  29898. }
  29899. #endif
  29900. AssertIntEQ(BN_get_word(c), 4);
  29901. #endif
  29902. BN_free(a);
  29903. BN_free(b);
  29904. BN_free(c);
  29905. BN_clear_free(d);
  29906. /* check that converting NULL and the null string returns an error */
  29907. a = NULL;
  29908. AssertIntLE(BN_hex2bn(&a, NULL), 0);
  29909. AssertIntLE(BN_hex2bn(&a, ""), 0);
  29910. AssertNull(a);
  29911. /* check that getting a string and a bin of the same number are equal,
  29912. * and that the comparison works EQ, LT and GT */
  29913. AssertIntGT(BN_hex2bn(&a, "03"), 0);
  29914. AssertNotNull(b = BN_new());
  29915. AssertIntEQ(BN_set_word(b, 3), SSL_SUCCESS);
  29916. AssertNotNull(c = BN_new());
  29917. AssertIntEQ(BN_set_word(c, 4), SSL_SUCCESS);
  29918. AssertIntEQ(BN_cmp(a, b), 0);
  29919. AssertIntLT(BN_cmp(a, c), 0);
  29920. AssertIntGT(BN_cmp(c, b), 0);
  29921. AssertIntEQ(BN_set_word(a, 0), 1);
  29922. AssertIntEQ(BN_is_zero(a), 1);
  29923. AssertIntEQ(BN_set_bit(a, 0x45), 1);
  29924. AssertIntEQ(BN_is_zero(a), 0);
  29925. AssertIntEQ(BN_is_bit_set(a, 0x45), 1);
  29926. AssertIntEQ(BN_clear_bit(a, 0x45), 1);
  29927. AssertIntEQ(BN_is_bit_set(a, 0x45), 0);
  29928. AssertIntEQ(BN_is_zero(a), 1);
  29929. BN_free(a);
  29930. BN_free(b);
  29931. BN_free(c);
  29932. #if defined(USE_FAST_MATH) && !defined(HAVE_WOLF_BIGINT)
  29933. {
  29934. BIGNUM *ap;
  29935. BIGNUM bv;
  29936. BIGNUM cv;
  29937. BIGNUM dv;
  29938. AssertNotNull(ap = BN_new());
  29939. BN_init(&bv);
  29940. BN_init(&cv);
  29941. BN_init(&dv);
  29942. AssertIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  29943. AssertIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  29944. AssertIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  29945. /* a^b mod c = */
  29946. AssertIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  29947. AssertIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  29948. /* check result 3^2 mod 5 */
  29949. AssertIntEQ(BN_get_word(&dv), 4);
  29950. /* a*b mod c = */
  29951. AssertIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  29952. AssertIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  29953. /* check result 3*2 mod 5 */
  29954. AssertIntEQ(BN_get_word(&dv), 1);
  29955. BN_free(ap);
  29956. }
  29957. #endif
  29958. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  29959. AssertNotNull(a = BN_new());
  29960. AssertIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL),
  29961. SSL_SUCCESS);
  29962. AssertIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), SSL_SUCCESS);
  29963. BN_free(a);
  29964. #endif
  29965. printf(resultFmt, passed);
  29966. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  29967. }
  29968. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29969. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29970. #define TEST_ARG 0x1234
  29971. static void msg_cb(int write_p, int version, int content_type,
  29972. const void *buf, size_t len, SSL *ssl, void *arg)
  29973. {
  29974. (void)write_p;
  29975. (void)version;
  29976. (void)content_type;
  29977. (void)buf;
  29978. (void)len;
  29979. (void)ssl;
  29980. AssertTrue(arg == (void*)TEST_ARG);
  29981. }
  29982. #endif
  29983. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29984. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  29985. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
  29986. !defined(NO_WOLFSSL_SERVER)
  29987. #ifndef SINGLE_THREADED
  29988. #if defined(SESSION_CERTS)
  29989. #include "wolfssl/internal.h"
  29990. #endif
  29991. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  29992. {
  29993. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  29994. STACK_OF(X509)* sk;
  29995. X509* x509;
  29996. int i, num;
  29997. BIO* bio;
  29998. #endif
  29999. (void) ctx;
  30000. printf("\n===== msgcb called ====\n");
  30001. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  30002. AssertTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  30003. AssertIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  30004. AssertNotNull(SSL_get0_verified_chain(ssl));
  30005. #else
  30006. (void) ssl;
  30007. #endif
  30008. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  30009. bio = BIO_new(BIO_s_file());
  30010. BIO_set_fp(bio, stdout, BIO_NOCLOSE);
  30011. sk = SSL_get_peer_cert_chain(ssl);
  30012. AssertNotNull(sk);
  30013. if (!sk) {
  30014. BIO_free(bio);
  30015. return SSL_FAILURE;
  30016. }
  30017. num = sk_X509_num(sk);
  30018. AssertTrue(num > 0);
  30019. for (i = 0; i < num; i++) {
  30020. x509 = sk_X509_value(sk,i);
  30021. AssertNotNull(x509);
  30022. if (!x509)
  30023. break;
  30024. printf("Certificate at index [%d] = :\n",i);
  30025. X509_print(bio,x509);
  30026. printf("\n\n");
  30027. }
  30028. BIO_free(bio);
  30029. #endif
  30030. return SSL_SUCCESS;
  30031. }
  30032. #endif
  30033. #endif
  30034. static void test_wolfSSL_msgCb(void)
  30035. {
  30036. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30037. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  30038. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
  30039. !defined(NO_WOLFSSL_SERVER)
  30040. tcp_ready ready;
  30041. func_args client_args;
  30042. func_args server_args;
  30043. #ifndef SINGLE_THREADED
  30044. THREAD_TYPE serverThread;
  30045. #endif
  30046. callback_functions client_cb;
  30047. callback_functions server_cb;
  30048. printf(testingFmt, "test_wolfSSL_msgCb");
  30049. /* create a failed connection and inspect the error */
  30050. #ifdef WOLFSSL_TIRTOS
  30051. fdOpenSession(Task_self());
  30052. #endif
  30053. XMEMSET(&client_args, 0, sizeof(func_args));
  30054. XMEMSET(&server_args, 0, sizeof(func_args));
  30055. StartTCP();
  30056. InitTcpReady(&ready);
  30057. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  30058. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  30059. #ifndef WOLFSSL_NO_TLS12
  30060. client_cb.method = wolfTLSv1_2_client_method;
  30061. server_cb.method = wolfTLSv1_2_server_method;
  30062. #else
  30063. client_cb.method = wolfTLSv1_3_client_method;
  30064. server_cb.method = wolfTLSv1_3_server_method;
  30065. #endif
  30066. server_args.signal = &ready;
  30067. server_args.callbacks = &server_cb;
  30068. client_args.signal = &ready;
  30069. client_args.callbacks = &client_cb;
  30070. client_args.return_code = TEST_FAIL;
  30071. #ifndef SINGLE_THREADED
  30072. start_thread(test_server_nofail, &server_args, &serverThread);
  30073. wait_tcp_ready(&server_args);
  30074. test_client_nofail(&client_args, msgCb);
  30075. join_thread(serverThread);
  30076. #endif
  30077. FreeTcpReady(&ready);
  30078. #ifndef SINGLE_THREADED
  30079. AssertTrue(client_args.return_code);
  30080. AssertTrue(server_args.return_code);
  30081. #endif
  30082. #ifdef WOLFSSL_TIRTOS
  30083. fdOpenSession(Task_self());
  30084. #endif
  30085. printf(resultFmt, passed);
  30086. #endif
  30087. }
  30088. static void test_wolfSSL_either_side(void)
  30089. {
  30090. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  30091. !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  30092. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  30093. tcp_ready ready;
  30094. func_args client_args;
  30095. func_args server_args;
  30096. #ifndef SINGLE_THREADED
  30097. THREAD_TYPE serverThread;
  30098. #endif
  30099. callback_functions client_cb;
  30100. callback_functions server_cb;
  30101. printf(testingFmt, "test_wolfSSL_either_side");
  30102. /* create a failed connection and inspect the error */
  30103. #ifdef WOLFSSL_TIRTOS
  30104. fdOpenSession(Task_self());
  30105. #endif
  30106. XMEMSET(&client_args, 0, sizeof(func_args));
  30107. XMEMSET(&server_args, 0, sizeof(func_args));
  30108. StartTCP();
  30109. InitTcpReady(&ready);
  30110. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  30111. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  30112. /* Use different CTX for client and server */
  30113. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  30114. AssertNotNull(client_cb.ctx);
  30115. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  30116. AssertNotNull(server_cb.ctx);
  30117. /* we are responsible for free'ing WOLFSSL_CTX */
  30118. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  30119. server_args.signal = &ready;
  30120. server_args.callbacks = &server_cb;
  30121. client_args.signal = &ready;
  30122. client_args.callbacks = &client_cb;
  30123. client_args.return_code = TEST_FAIL;
  30124. #ifndef SINGLE_THREADED
  30125. start_thread(test_server_nofail, &server_args, &serverThread);
  30126. wait_tcp_ready(&server_args);
  30127. test_client_nofail(&client_args, NULL);
  30128. join_thread(serverThread);
  30129. #endif
  30130. wolfSSL_CTX_free(client_cb.ctx);
  30131. wolfSSL_CTX_free(server_cb.ctx);
  30132. FreeTcpReady(&ready);
  30133. #ifndef SINGLE_THREADED
  30134. AssertTrue(client_args.return_code);
  30135. AssertTrue(server_args.return_code);
  30136. #endif
  30137. #ifdef WOLFSSL_TIRTOS
  30138. fdOpenSession(Task_self());
  30139. #endif
  30140. printf(resultFmt, passed);
  30141. #endif
  30142. }
  30143. static void test_wolfSSL_DTLS_either_side(void)
  30144. {
  30145. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  30146. !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  30147. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  30148. defined(WOLFSSL_DTLS)
  30149. tcp_ready ready;
  30150. func_args client_args;
  30151. func_args server_args;
  30152. #ifndef SINGLE_THREADED
  30153. THREAD_TYPE serverThread;
  30154. #endif
  30155. callback_functions client_cb;
  30156. callback_functions server_cb;
  30157. printf(testingFmt, "test_wolfSSL_DTLS_either_side");
  30158. /* create a failed connection and inspect the error */
  30159. #ifdef WOLFSSL_TIRTOS
  30160. fdOpenSession(Task_self());
  30161. #endif
  30162. XMEMSET(&client_args, 0, sizeof(func_args));
  30163. XMEMSET(&server_args, 0, sizeof(func_args));
  30164. StartTCP();
  30165. InitTcpReady(&ready);
  30166. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  30167. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  30168. /* Use different CTX for client and server */
  30169. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  30170. AssertNotNull(client_cb.ctx);
  30171. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  30172. AssertNotNull(server_cb.ctx);
  30173. /* we are responsible for free'ing WOLFSSL_CTX */
  30174. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  30175. server_args.signal = &ready;
  30176. server_args.callbacks = &server_cb;
  30177. client_args.signal = &ready;
  30178. client_args.callbacks = &client_cb;
  30179. client_args.return_code = TEST_FAIL;
  30180. #ifndef SINGLE_THREADED
  30181. start_thread(test_server_nofail, &server_args, &serverThread);
  30182. wait_tcp_ready(&server_args);
  30183. test_client_nofail(&client_args, NULL);
  30184. join_thread(serverThread);
  30185. #endif
  30186. wolfSSL_CTX_free(client_cb.ctx);
  30187. wolfSSL_CTX_free(server_cb.ctx);
  30188. FreeTcpReady(&ready);
  30189. #ifndef SINGLE_THREADED
  30190. AssertTrue(client_args.return_code);
  30191. AssertTrue(server_args.return_code);
  30192. #endif
  30193. #ifdef WOLFSSL_TIRTOS
  30194. fdOpenSession(Task_self());
  30195. #endif
  30196. printf(resultFmt, passed);
  30197. #endif
  30198. }
  30199. static void test_generate_cookie(void)
  30200. {
  30201. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  30202. SSL_CTX* ctx;
  30203. SSL* ssl;
  30204. byte buf[FOURK_BUF] = {0};
  30205. printf(testingFmt, "test_generate_cookie");
  30206. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  30207. AssertNotNull(ssl = SSL_new(ctx));
  30208. /* Test unconnected */
  30209. AssertIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  30210. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  30211. wolfSSL_SetCookieCtx(ssl, ctx);
  30212. AssertNotNull(wolfSSL_GetCookieCtx(ssl));
  30213. AssertNull(wolfSSL_GetCookieCtx(NULL));
  30214. SSL_free(ssl);
  30215. SSL_CTX_free(ctx);
  30216. printf(resultFmt, passed);
  30217. #endif
  30218. }
  30219. static void test_wolfSSL_set_options(void)
  30220. {
  30221. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30222. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30223. SSL* ssl;
  30224. SSL_CTX* ctx;
  30225. char appData[] = "extra msg";
  30226. unsigned char protos[] = {
  30227. 7, 't', 'l', 's', '/', '1', '.', '2',
  30228. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  30229. };
  30230. unsigned int len = sizeof(protos);
  30231. void *arg = (void *)TEST_ARG;
  30232. printf(testingFmt, "wolfSSL_set_options()");
  30233. #ifndef NO_WOLFSSL_SERVER
  30234. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30235. #else
  30236. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30237. #endif
  30238. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  30239. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30240. AssertTrue(SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1) == SSL_OP_NO_TLSv1);
  30241. AssertTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  30242. AssertIntGT((int)SSL_CTX_set_options(ctx, (SSL_OP_COOKIE_EXCHANGE |
  30243. SSL_OP_NO_SSLv2)), 0);
  30244. AssertTrue((SSL_CTX_set_options(ctx, SSL_OP_COOKIE_EXCHANGE) &
  30245. SSL_OP_COOKIE_EXCHANGE) == SSL_OP_COOKIE_EXCHANGE);
  30246. AssertTrue((SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_2) &
  30247. SSL_OP_NO_TLSv1_2) == SSL_OP_NO_TLSv1_2);
  30248. AssertTrue((SSL_CTX_set_options(ctx, SSL_OP_NO_COMPRESSION) &
  30249. SSL_OP_NO_COMPRESSION) == SSL_OP_NO_COMPRESSION);
  30250. AssertNull((SSL_CTX_clear_options(ctx, SSL_OP_NO_COMPRESSION) &
  30251. SSL_OP_NO_COMPRESSION));
  30252. SSL_CTX_free(ctx);
  30253. #ifndef NO_WOLFSSL_SERVER
  30254. ctx = SSL_CTX_new(wolfSSLv23_server_method());
  30255. AssertNotNull(ctx);
  30256. #else
  30257. ctx = SSL_CTX_new(wolfSSLv23_client_method());
  30258. AssertNotNull(ctx);
  30259. #endif
  30260. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  30261. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30262. AssertTrue(SSL_CTX_set_msg_callback(ctx, msg_cb) == SSL_SUCCESS);
  30263. AssertNotNull(ssl = SSL_new(ctx));
  30264. #ifdef HAVE_EX_DATA
  30265. AssertIntEQ(SSL_set_app_data(ssl, (void*)appData), SSL_SUCCESS);
  30266. AssertNotNull(SSL_get_app_data((const WOLFSSL*)ssl));
  30267. if (ssl) {
  30268. AssertIntEQ(XMEMCMP(SSL_get_app_data((const WOLFSSL*)ssl),
  30269. appData, sizeof(appData)), 0);
  30270. }
  30271. #else
  30272. AssertIntEQ(SSL_set_app_data(ssl, (void*)appData), SSL_FAILURE);
  30273. AssertNull(SSL_get_app_data((const WOLFSSL*)ssl));
  30274. #endif
  30275. AssertTrue(SSL_set_options(ssl, SSL_OP_NO_TLSv1) == SSL_OP_NO_TLSv1);
  30276. AssertTrue(SSL_get_options(ssl) == SSL_OP_NO_TLSv1);
  30277. AssertIntGT((int)SSL_set_options(ssl, (SSL_OP_COOKIE_EXCHANGE |
  30278. WOLFSSL_OP_NO_SSLv2)), 0);
  30279. AssertTrue((SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE) &
  30280. SSL_OP_COOKIE_EXCHANGE) == SSL_OP_COOKIE_EXCHANGE);
  30281. AssertTrue((SSL_set_options(ssl, SSL_OP_NO_TLSv1_2) &
  30282. SSL_OP_NO_TLSv1_2) == SSL_OP_NO_TLSv1_2);
  30283. AssertTrue((SSL_set_options(ssl, SSL_OP_NO_COMPRESSION) &
  30284. SSL_OP_NO_COMPRESSION) == SSL_OP_NO_COMPRESSION);
  30285. AssertNull((SSL_clear_options(ssl, SSL_OP_NO_COMPRESSION) &
  30286. SSL_OP_NO_COMPRESSION));
  30287. AssertTrue(SSL_set_msg_callback(ssl, msg_cb) == SSL_SUCCESS);
  30288. SSL_set_msg_callback_arg(ssl, arg);
  30289. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30290. AssertTrue(SSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  30291. #else
  30292. AssertTrue(SSL_CTX_set_alpn_protos(ctx, protos, len) == SSL_SUCCESS);
  30293. #endif
  30294. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  30295. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  30296. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  30297. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  30298. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30299. AssertTrue(SSL_set_alpn_protos(ssl, protos, len) == 0);
  30300. #else
  30301. AssertTrue(SSL_set_alpn_protos(ssl, protos, len) == SSL_SUCCESS);
  30302. #endif
  30303. #endif /* HAVE_ALPN && !NO_BIO */
  30304. #endif
  30305. SSL_free(ssl);
  30306. SSL_CTX_free(ctx);
  30307. printf(resultFmt, passed);
  30308. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30309. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  30310. }
  30311. static void test_wolfSSL_sk_SSL_CIPHER(void)
  30312. {
  30313. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  30314. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30315. SSL* ssl;
  30316. SSL_CTX* ctx;
  30317. STACK_OF(SSL_CIPHER) *sk, *dupSk;
  30318. printf(testingFmt, "wolfSSL_sk_SSL_CIPHER_*()");
  30319. #ifndef NO_WOLFSSL_SERVER
  30320. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30321. #else
  30322. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30323. #endif
  30324. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  30325. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30326. AssertNotNull(ssl = SSL_new(ctx));
  30327. AssertNotNull(sk = SSL_get_ciphers(ssl));
  30328. AssertNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  30329. AssertIntGT(sk_SSL_CIPHER_num(sk), 0);
  30330. AssertIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  30331. /* error case because connection has not been established yet */
  30332. AssertIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  30333. sk_SSL_CIPHER_free(dupSk);
  30334. /* sk is pointer to internal struct that should be free'd in SSL_free */
  30335. SSL_free(ssl);
  30336. SSL_CTX_free(ctx);
  30337. printf(resultFmt, passed);
  30338. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30339. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  30340. }
  30341. static void test_wolfSSL_set1_curves_list(void)
  30342. {
  30343. #if defined(OPENSSL_EXTRA) && defined(HAVE_RSA)
  30344. SSL* ssl = NULL;
  30345. SSL_CTX* ctx = NULL;
  30346. #ifndef NO_WOLFSSL_SERVER
  30347. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30348. #else
  30349. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30350. #endif
  30351. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  30352. SSL_FILETYPE_PEM));
  30353. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30354. AssertNotNull(ssl = SSL_new(ctx));
  30355. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  30356. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  30357. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  30358. AssertIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  30359. AssertIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  30360. AssertIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  30361. SSL_free(ssl);
  30362. SSL_CTX_free(ctx);
  30363. printf(resultFmt, passed);
  30364. #endif
  30365. }
  30366. static void test_wolfSSL_set1_sigalgs_list(void)
  30367. {
  30368. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  30369. SSL* ssl;
  30370. SSL_CTX* ctx;
  30371. #ifndef NO_WOLFSSL_SERVER
  30372. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30373. #else
  30374. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30375. #endif
  30376. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  30377. SSL_FILETYPE_PEM));
  30378. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30379. AssertNotNull(ssl = SSL_new(ctx));
  30380. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  30381. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  30382. AssertIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  30383. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  30384. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  30385. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  30386. #ifndef NO_RSA
  30387. #ifndef NO_SHA256
  30388. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  30389. WOLFSSL_FAILURE);
  30390. AssertIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  30391. WOLFSSL_FAILURE);
  30392. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  30393. WOLFSSL_SUCCESS);
  30394. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  30395. WOLFSSL_SUCCESS);
  30396. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  30397. WOLFSSL_FAILURE);
  30398. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  30399. WOLFSSL_FAILURE);
  30400. #ifdef WC_RSA_PSS
  30401. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  30402. WOLFSSL_SUCCESS);
  30403. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  30404. WOLFSSL_SUCCESS);
  30405. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  30406. WOLFSSL_SUCCESS);
  30407. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  30408. WOLFSSL_SUCCESS);
  30409. #endif
  30410. #ifdef WOLFSSL_SHA512
  30411. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  30412. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  30413. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  30414. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  30415. #elif defined(WOLFSSL_SHA384)
  30416. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  30417. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  30418. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  30419. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  30420. #endif
  30421. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  30422. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  30423. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  30424. WOLFSSL_FAILURE);
  30425. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  30426. WOLFSSL_FAILURE);
  30427. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  30428. WOLFSSL_FAILURE);
  30429. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  30430. WOLFSSL_FAILURE);
  30431. #endif
  30432. #endif
  30433. #ifdef HAVE_ECC
  30434. #ifndef NO_SHA256
  30435. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  30436. WOLFSSL_SUCCESS);
  30437. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"), WOLFSSL_SUCCESS);
  30438. #ifdef WOLFSSL_SHA512
  30439. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  30440. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  30441. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  30442. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  30443. #elif defined(WOLFSSL_SHA384)
  30444. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  30445. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  30446. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  30447. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  30448. #endif
  30449. #endif
  30450. #endif
  30451. #ifdef HAVE_ED25519
  30452. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  30453. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  30454. #endif
  30455. #ifdef HAVE_ED448
  30456. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  30457. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  30458. #endif
  30459. #ifndef NO_DSA
  30460. #ifndef NO_SHA256
  30461. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  30462. WOLFSSL_SUCCESS);
  30463. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  30464. WOLFSSL_SUCCESS);
  30465. #endif
  30466. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  30467. defined(WOLFSSL_ALLOW_TLS_SHA1))
  30468. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  30469. WOLFSSL_SUCCESS);
  30470. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  30471. WOLFSSL_SUCCESS);
  30472. #endif
  30473. #endif
  30474. SSL_free(ssl);
  30475. SSL_CTX_free(ctx);
  30476. printf(resultFmt, passed);
  30477. #endif
  30478. }
  30479. /* Testing wolfSSL_set_tlsext_status_type function.
  30480. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  30481. */
  30482. static void test_wolfSSL_set_tlsext_status_type(void){
  30483. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  30484. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  30485. SSL* ssl;
  30486. SSL_CTX* ctx;
  30487. printf(testingFmt, "wolfSSL_set_tlsext_status_type()");
  30488. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30489. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  30490. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30491. AssertNotNull(ssl = SSL_new(ctx));
  30492. AssertIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  30493. SSL_SUCCESS);
  30494. AssertIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  30495. SSL_free(ssl);
  30496. SSL_CTX_free(ctx);
  30497. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  30498. }
  30499. #ifndef NO_BIO
  30500. static void test_wolfSSL_PEM_read_bio(void)
  30501. {
  30502. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30503. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30504. byte buff[6000];
  30505. XFILE f;
  30506. int bytes;
  30507. X509* x509;
  30508. BIO* bio = NULL;
  30509. BUF_MEM* buf;
  30510. printf(testingFmt, "wolfSSL_PEM_read_bio()");
  30511. f = XFOPEN(cliCertFile, "rb");
  30512. AssertTrue((f != XBADFILE));
  30513. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  30514. XFCLOSE(f);
  30515. AssertNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  30516. AssertNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  30517. AssertIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  30518. AssertNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  30519. AssertIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  30520. /* BIO should return the set EOF value */
  30521. AssertIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  30522. AssertIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  30523. AssertIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  30524. AssertIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  30525. BIO_free(bio);
  30526. BUF_MEM_free(buf);
  30527. X509_free(x509);
  30528. printf(resultFmt, passed);
  30529. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30530. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  30531. }
  30532. #if defined(OPENSSL_EXTRA)
  30533. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  30534. long argl, long ret)
  30535. {
  30536. (void)bio;
  30537. (void)cmd;
  30538. (void)argp;
  30539. (void)argi;
  30540. (void)argl;
  30541. return ret;
  30542. }
  30543. #endif
  30544. static void test_wolfSSL_BIO(void)
  30545. {
  30546. #if defined(OPENSSL_EXTRA)
  30547. const unsigned char* p;
  30548. byte buff[20];
  30549. BIO* bio1;
  30550. BIO* bio2;
  30551. BIO* bio3;
  30552. char* bufPt;
  30553. int i;
  30554. printf(testingFmt, "wolfSSL_BIO()");
  30555. for (i = 0; i < 20; i++) {
  30556. buff[i] = i;
  30557. }
  30558. /* test BIO_free with NULL */
  30559. AssertIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  30560. /* Creating and testing type BIO_s_bio */
  30561. AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
  30562. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  30563. AssertNotNull(bio3 = BIO_new(BIO_s_bio()));
  30564. /* read/write before set up */
  30565. AssertIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  30566. AssertIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  30567. AssertIntEQ(BIO_set_nbio(bio1, 1), 1);
  30568. AssertIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  30569. AssertIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  30570. AssertIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  30571. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  30572. XMEMCPY(bufPt, buff, 10);
  30573. AssertIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  30574. /* write buffer full */
  30575. AssertIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  30576. AssertIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  30577. AssertIntEQ((int)BIO_ctrl_pending(bio1), 0);
  30578. /* write the other direction with pair */
  30579. AssertIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  30580. XMEMCPY(bufPt, buff, 8);
  30581. AssertIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  30582. /* try read */
  30583. AssertIntEQ((int)BIO_ctrl_pending(bio1), 8);
  30584. AssertIntEQ((int)BIO_ctrl_pending(bio2), 20);
  30585. /* try read using ctrl function */
  30586. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  30587. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  30588. AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  30589. AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  30590. AssertIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  30591. for (i = 0; i < 20; i++) {
  30592. AssertIntEQ((int)bufPt[i], i);
  30593. }
  30594. AssertIntEQ(BIO_nread(bio2, &bufPt, 1), WOLFSSL_BIO_ERROR);
  30595. AssertIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  30596. for (i = 0; i < 8; i++) {
  30597. AssertIntEQ((int)bufPt[i], i);
  30598. }
  30599. AssertIntEQ(BIO_nread(bio1, &bufPt, 1), WOLFSSL_BIO_ERROR);
  30600. AssertIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  30601. /* new pair */
  30602. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  30603. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  30604. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  30605. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  30606. AssertIntEQ(BIO_nread(bio3, &bufPt, 10), WOLFSSL_BIO_ERROR);
  30607. /* test wrap around... */
  30608. AssertIntEQ(BIO_reset(bio1), 0);
  30609. AssertIntEQ(BIO_reset(bio3), 0);
  30610. /* fill write buffer, read only small amount then write again */
  30611. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  30612. XMEMCPY(bufPt, buff, 20);
  30613. AssertIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  30614. for (i = 0; i < 4; i++) {
  30615. AssertIntEQ(bufPt[i], i);
  30616. }
  30617. /* try writing over read index */
  30618. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  30619. XMEMSET(bufPt, 0, 4);
  30620. AssertIntEQ((int)BIO_ctrl_pending(bio3), 20);
  30621. /* read and write 0 bytes */
  30622. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  30623. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  30624. /* should read only to end of write buffer then need to read again */
  30625. AssertIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  30626. for (i = 0; i < 16; i++) {
  30627. AssertIntEQ(bufPt[i], buff[4 + i]);
  30628. }
  30629. AssertIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  30630. AssertIntEQ(BIO_nread0(bio3, &bufPt), 4);
  30631. for (i = 0; i < 4; i++) {
  30632. AssertIntEQ(bufPt[i], 0);
  30633. }
  30634. /* read index should not have advanced with nread0 */
  30635. AssertIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  30636. for (i = 0; i < 4; i++) {
  30637. AssertIntEQ(bufPt[i], 0);
  30638. }
  30639. /* write and fill up buffer checking reset of index state */
  30640. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  30641. XMEMCPY(bufPt, buff, 20);
  30642. /* test reset on data in bio1 write buffer */
  30643. AssertIntEQ(BIO_reset(bio1), 0);
  30644. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  30645. AssertIntEQ(BIO_nread(bio3, &bufPt, 3), WOLFSSL_BIO_ERROR);
  30646. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  30647. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  30648. AssertNotNull(p);
  30649. XMEMCPY(bufPt, buff, 20);
  30650. AssertIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  30651. for (i = 0; i < 6; i++) {
  30652. AssertIntEQ(bufPt[i], i);
  30653. }
  30654. /* test case of writing twice with offset read index */
  30655. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  30656. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  30657. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  30658. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  30659. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  30660. AssertIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  30661. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  30662. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  30663. BIO_free(bio1);
  30664. BIO_free(bio3);
  30665. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  30666. {
  30667. BIO* bioA = NULL;
  30668. BIO* bioB = NULL;
  30669. AssertIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  30670. AssertIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  30671. BIO_free(bioA);
  30672. bioA = NULL;
  30673. BIO_free(bioB);
  30674. bioB = NULL;
  30675. }
  30676. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  30677. /* BIOs with file pointers */
  30678. #if !defined(NO_FILESYSTEM)
  30679. {
  30680. XFILE f1;
  30681. XFILE f2;
  30682. BIO* f_bio1;
  30683. BIO* f_bio2;
  30684. unsigned char cert[300];
  30685. char testFile[] = "tests/bio_write_test.txt";
  30686. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  30687. AssertNotNull(f_bio1 = BIO_new(BIO_s_file()));
  30688. AssertNotNull(f_bio2 = BIO_new(BIO_s_file()));
  30689. /* Failure due to wrong BIO type */
  30690. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  30691. AssertIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  30692. f1 = XFOPEN(svrCertFile, "rwb");
  30693. AssertTrue((f1 != XBADFILE));
  30694. AssertIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  30695. AssertIntEQ(BIO_write_filename(f_bio2, testFile),
  30696. WOLFSSL_SUCCESS);
  30697. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  30698. AssertIntEQ(BIO_tell(f_bio1),sizeof(cert));
  30699. AssertIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  30700. AssertIntEQ(BIO_tell(f_bio2),sizeof(msg));
  30701. AssertIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  30702. AssertIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  30703. AssertIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  30704. AssertIntEQ(BIO_reset(f_bio2), 0);
  30705. AssertIntEQ(BIO_tell(NULL),-1);
  30706. AssertIntEQ(BIO_tell(f_bio2),0);
  30707. AssertIntEQ(BIO_seek(f_bio2, 4), 0);
  30708. AssertIntEQ(BIO_tell(f_bio2),4);
  30709. BIO_free(f_bio1);
  30710. BIO_free(f_bio2);
  30711. AssertNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  30712. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  30713. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  30714. BIO_free(f_bio1);
  30715. }
  30716. #endif /* !defined(NO_FILESYSTEM) */
  30717. /* BIO info callback */
  30718. {
  30719. const char* testArg = "test";
  30720. BIO* cb_bio;
  30721. AssertNotNull(cb_bio = BIO_new(BIO_s_mem()));
  30722. BIO_set_callback(cb_bio, bioCallback);
  30723. AssertNotNull(BIO_get_callback(cb_bio));
  30724. BIO_set_callback(cb_bio, NULL);
  30725. AssertNull(BIO_get_callback(cb_bio));
  30726. BIO_set_callback_arg(cb_bio, (char*)testArg);
  30727. AssertStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  30728. AssertNull(BIO_get_callback_arg(NULL));
  30729. BIO_free(cb_bio);
  30730. }
  30731. /* BIO_vfree */
  30732. AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
  30733. BIO_vfree(NULL);
  30734. BIO_vfree(bio1);
  30735. printf(resultFmt, passed);
  30736. #endif
  30737. }
  30738. #endif /* !NO_BIO */
  30739. static void test_wolfSSL_ASN1_STRING(void)
  30740. {
  30741. #if defined(OPENSSL_EXTRA)
  30742. ASN1_STRING* str = NULL;
  30743. const char data[] = "hello wolfSSL";
  30744. printf(testingFmt, "wolfSSL_ASN1_STRING()");
  30745. AssertNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  30746. AssertIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  30747. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  30748. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  30749. AssertIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  30750. ASN1_STRING_free(str);
  30751. printf(resultFmt, passed);
  30752. #endif
  30753. }
  30754. static void test_wolfSSL_ASN1_BIT_STRING(void)
  30755. {
  30756. #ifdef OPENSSL_ALL
  30757. ASN1_BIT_STRING* str;
  30758. printf(testingFmt, "test_wolfSSL_ASN1_BIT_STRING()");
  30759. AssertNotNull(str = ASN1_BIT_STRING_new());
  30760. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  30761. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  30762. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  30763. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  30764. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  30765. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  30766. ASN1_BIT_STRING_free(str);
  30767. printf(resultFmt, passed);
  30768. #endif
  30769. }
  30770. static void test_wolfSSL_a2i_ASN1_INTEGER(void)
  30771. {
  30772. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  30773. BIO *bio, *out;
  30774. ASN1_INTEGER* ai;
  30775. char buf[] = "123456\n12345\n112345678912345678901234567890\n";
  30776. char tmp[1024];
  30777. int tmpSz;
  30778. const char expected1[] = "123456";
  30779. const char expected2[] = "112345678912345678901234567890";
  30780. printf(testingFmt, "test_wolfSSL_a2i_ASN1_INTEGER()");
  30781. AssertNotNull(bio = BIO_new_mem_buf(buf, -1));
  30782. AssertNotNull(out = BIO_new(BIO_s_mem()));
  30783. AssertNotNull(ai = ASN1_INTEGER_new());
  30784. /* read first line */
  30785. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
  30786. AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  30787. XMEMSET(tmp, 0, 1024);
  30788. tmpSz = BIO_read(out, tmp, 1024);
  30789. AssertIntEQ(tmpSz, 6);
  30790. AssertIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  30791. /* fail on second line (not % 2) */
  30792. AssertIntNE(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
  30793. /* read 3rd long line */
  30794. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
  30795. AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  30796. XMEMSET(tmp, 0, 1024);
  30797. tmpSz = BIO_read(out, tmp, 1024);
  30798. AssertIntEQ(tmpSz, 30);
  30799. AssertIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  30800. BIO_free(out);
  30801. BIO_free(bio);
  30802. ASN1_INTEGER_free(ai);
  30803. printf(resultFmt, passed);
  30804. #endif
  30805. }
  30806. static void test_wolfSSL_a2i_IPADDRESS(void)
  30807. {
  30808. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  30809. const unsigned char* data;
  30810. int dataSz = 0;
  30811. ASN1_OCTET_STRING *st;
  30812. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  30813. const unsigned char ipv6_exp[] = {
  30814. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  30815. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  30816. };
  30817. const unsigned char ipv6_home[] = {
  30818. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  30819. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  30820. };
  30821. printf(testingFmt, "test_wolfSSL_a2i_IPADDRESS()");
  30822. AssertNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  30823. AssertNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  30824. data = ASN1_STRING_get0_data(st);
  30825. dataSz = ASN1_STRING_length(st);
  30826. AssertIntEQ(dataSz, WOLFSSL_IP4_ADDR_LEN);
  30827. AssertIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  30828. ASN1_STRING_free(st);
  30829. AssertNotNull(st = a2i_IPADDRESS("::1"));
  30830. data = ASN1_STRING_get0_data(st);
  30831. dataSz = ASN1_STRING_length(st);
  30832. AssertIntEQ(dataSz, WOLFSSL_IP6_ADDR_LEN);
  30833. AssertIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  30834. ASN1_STRING_free(st);
  30835. AssertNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  30836. data = ASN1_STRING_get0_data(st);
  30837. dataSz = ASN1_STRING_length(st);
  30838. AssertIntEQ(dataSz, WOLFSSL_IP6_ADDR_LEN);
  30839. AssertIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  30840. ASN1_STRING_free(st);
  30841. printf(resultFmt, passed);
  30842. #endif
  30843. }
  30844. static void test_wolfSSL_DES_ecb_encrypt(void)
  30845. {
  30846. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  30847. WOLFSSL_DES_cblock input1,input2,output1,output2,back1,back2;
  30848. WOLFSSL_DES_key_schedule key;
  30849. printf(testingFmt, "wolfSSL_DES_ecb_encrypt()");
  30850. XMEMCPY(key,"12345678",sizeof(WOLFSSL_DES_key_schedule));
  30851. XMEMCPY(input1, "Iamhuman",sizeof(WOLFSSL_DES_cblock));
  30852. XMEMCPY(input2, "Whoisit?",sizeof(WOLFSSL_DES_cblock));
  30853. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  30854. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  30855. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  30856. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  30857. /* Encrypt messages */
  30858. wolfSSL_DES_ecb_encrypt(&input1,&output1,&key,DES_ENCRYPT);
  30859. wolfSSL_DES_ecb_encrypt(&input2,&output2,&key,DES_ENCRYPT);
  30860. {
  30861. /* Decrypt messages */
  30862. int ret1 = 0;
  30863. int ret2 = 0;
  30864. wolfSSL_DES_ecb_encrypt(&output1,&back1,&key,DES_DECRYPT);
  30865. ret1 = XMEMCMP((unsigned char *) back1,(unsigned char *) input1,sizeof(WOLFSSL_DES_cblock));
  30866. AssertIntEQ(ret1,0);
  30867. wolfSSL_DES_ecb_encrypt(&output2,&back2,&key,DES_DECRYPT);
  30868. ret2 = XMEMCMP((unsigned char *) back2,(unsigned char *) input2,sizeof(WOLFSSL_DES_cblock));
  30869. AssertIntEQ(ret2,0);
  30870. }
  30871. printf(resultFmt, passed);
  30872. #endif
  30873. }
  30874. static void test_wolfSSL_ASN1_TIME_adj(void)
  30875. {
  30876. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  30877. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  30878. const int year = 365*24*60*60;
  30879. const int day = 24*60*60;
  30880. const int hour = 60*60;
  30881. const int mini = 60;
  30882. const byte asn_utc_time = ASN_UTC_TIME;
  30883. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  30884. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  30885. #endif
  30886. WOLFSSL_ASN1_TIME *asn_time, *s;
  30887. int offset_day;
  30888. long offset_sec;
  30889. char date_str[CTC_DATE_SIZE + 1];
  30890. time_t t;
  30891. printf(testingFmt, "wolfSSL_ASN1_TIME_adj()");
  30892. AssertNotNull(s = wolfSSL_ASN1_TIME_new());
  30893. /* UTC notation test */
  30894. /* 2000/2/15 20:30:00 */
  30895. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  30896. offset_day = 7;
  30897. offset_sec = 45 * mini;
  30898. /* offset_sec = -45 * min;*/
  30899. AssertNotNull(asn_time =
  30900. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  30901. AssertTrue(asn_time->type == asn_utc_time);
  30902. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  30903. date_str[CTC_DATE_SIZE] = '\0';
  30904. AssertIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  30905. /* negative offset */
  30906. offset_sec = -45 * mini;
  30907. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  30908. AssertTrue(asn_time->type == asn_utc_time);
  30909. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  30910. date_str[CTC_DATE_SIZE] = '\0';
  30911. AssertIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  30912. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  30913. XMEMSET(date_str, 0, sizeof(date_str));
  30914. /* Generalized time will overflow time_t if not long */
  30915. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  30916. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  30917. DYNAMIC_TYPE_OPENSSL);
  30918. /* GeneralizedTime notation test */
  30919. /* 2055/03/01 09:00:00 */
  30920. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  30921. offset_day = 12;
  30922. offset_sec = 10 * mini;
  30923. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  30924. AssertTrue(asn_time->type == asn_gen_time);
  30925. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  30926. date_str[CTC_DATE_SIZE] = '\0';
  30927. AssertIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  30928. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  30929. XMEMSET(date_str, 0, sizeof(date_str));
  30930. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  30931. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  30932. s = NULL;
  30933. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  30934. offset_day = 7;
  30935. offset_sec = 45 * mini;
  30936. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  30937. AssertTrue(asn_time->type == asn_utc_time);
  30938. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  30939. date_str[CTC_DATE_SIZE] = '\0';
  30940. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  30941. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  30942. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, offset_sec);
  30943. AssertTrue(asn_time->type == asn_utc_time);
  30944. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  30945. date_str[CTC_DATE_SIZE] = '\0';
  30946. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  30947. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  30948. printf(resultFmt, passed);
  30949. #endif
  30950. }
  30951. static void test_wolfSSL_ASN1_TIME_to_tm(void)
  30952. {
  30953. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  30954. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) \
  30955. && !defined(NO_ASN_TIME)
  30956. ASN1_TIME asnTime;
  30957. struct tm tm;
  30958. printf(testingFmt, "wolfSSL_ASN1_TIME_to_tm()");
  30959. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  30960. AssertIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  30961. AssertIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  30962. AssertIntEQ(tm.tm_sec, 15);
  30963. AssertIntEQ(tm.tm_min, 15);
  30964. AssertIntEQ(tm.tm_hour, 21);
  30965. AssertIntEQ(tm.tm_mday, 22);
  30966. AssertIntEQ(tm.tm_mon, 1);
  30967. AssertIntEQ(tm.tm_year, 100);
  30968. AssertIntEQ(tm.tm_isdst, 0);
  30969. #ifdef XMKTIME
  30970. AssertIntEQ(tm.tm_wday, 2);
  30971. AssertIntEQ(tm.tm_yday, 52);
  30972. #endif
  30973. printf(resultFmt, passed);
  30974. #endif
  30975. }
  30976. static void test_wolfSSL_X509_cmp_time(void)
  30977. {
  30978. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  30979. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  30980. WOLFSSL_ASN1_TIME asn_time;
  30981. time_t t;
  30982. printf(testingFmt, "wolfSSL_X509_cmp_time()");
  30983. AssertIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  30984. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  30985. AssertIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  30986. AssertIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  30987. AssertIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  30988. printf(resultFmt, passed);
  30989. #endif
  30990. }
  30991. static void test_wolfSSL_X509_time_adj(void)
  30992. {
  30993. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  30994. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  30995. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  30996. !defined(NO_ASN_TIME)
  30997. X509* x509;
  30998. time_t t, not_before, not_after;
  30999. printf(testingFmt, "wolfSSL_X509_time_adj()");
  31000. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  31001. client_cert_der_2048, sizeof_client_cert_der_2048,
  31002. WOLFSSL_FILETYPE_ASN1));
  31003. t = 0;
  31004. not_before = wc_Time(0);
  31005. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  31006. AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  31007. AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  31008. /* Check X509_gmtime_adj, too. */
  31009. AssertNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  31010. X509_free(x509);
  31011. printf(resultFmt, passed);
  31012. #endif
  31013. }
  31014. static void test_wolfSSL_X509(void)
  31015. {
  31016. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)\
  31017. && !defined(NO_RSA)
  31018. X509* x509;
  31019. #ifndef NO_BIO
  31020. BIO* bio;
  31021. X509_STORE_CTX* ctx;
  31022. X509_STORE* store;
  31023. #endif
  31024. char der[] = "certs/ca-cert.der";
  31025. XFILE fp;
  31026. printf(testingFmt, "wolfSSL_X509()");
  31027. AssertNotNull(x509 = X509_new());
  31028. X509_free(x509);
  31029. #ifndef NO_BIO
  31030. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
  31031. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  31032. #ifdef WOLFSSL_CERT_GEN
  31033. AssertIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  31034. #endif
  31035. AssertNotNull(ctx = X509_STORE_CTX_new());
  31036. AssertIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  31037. AssertNotNull(store = X509_STORE_new());
  31038. AssertIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  31039. AssertIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  31040. AssertIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  31041. X509_STORE_CTX_free(ctx);
  31042. X509_STORE_free(store);
  31043. X509_free(x509);
  31044. BIO_free(bio);
  31045. #endif
  31046. /** d2i_X509_fp test **/
  31047. fp = XFOPEN(der, "rb");
  31048. AssertTrue((fp != XBADFILE));
  31049. AssertNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  31050. AssertNotNull(x509);
  31051. X509_free(x509);
  31052. XFCLOSE(fp);
  31053. fp = XFOPEN(der, "rb");
  31054. AssertTrue((fp != XBADFILE));
  31055. AssertNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  31056. AssertNotNull(x509);
  31057. X509_free(x509);
  31058. XFCLOSE(fp);
  31059. /* X509_up_ref test */
  31060. AssertIntEQ(X509_up_ref(NULL), 0);
  31061. AssertNotNull(x509 = X509_new()); /* refCount = 1 */
  31062. AssertIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  31063. AssertIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  31064. X509_free(x509); /* refCount = 2 */
  31065. X509_free(x509); /* refCount = 1 */
  31066. X509_free(x509); /* refCount = 0, free */
  31067. printf(resultFmt, passed);
  31068. #endif
  31069. }
  31070. static void test_wolfSSL_X509_get_ext_count(void)
  31071. {
  31072. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  31073. !defined(NO_RSA)
  31074. int ret = 0;
  31075. WOLFSSL_X509* x509;
  31076. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  31077. FILE* f;
  31078. printf(testingFmt, "wolfSSL_X509_get_ext_count()");
  31079. /* NULL parameter check */
  31080. AssertIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  31081. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  31082. SSL_FILETYPE_PEM));
  31083. AssertIntEQ(X509_get_ext_count(x509), 5);
  31084. wolfSSL_X509_free(x509);
  31085. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  31086. SSL_FILETYPE_PEM));
  31087. AssertIntEQ(X509_get_ext_count(x509), 5);
  31088. wolfSSL_X509_free(x509);
  31089. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  31090. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  31091. fclose(f);
  31092. printf(testingFmt, "wolfSSL_X509_get_ext_count() valid input");
  31093. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  31094. printf(resultFmt, ret == 4 ? passed : failed);
  31095. printf(testingFmt, "wolfSSL_X509_get_ext_count() NULL argument");
  31096. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  31097. printf(resultFmt, ret == WOLFSSL_FAILURE ? passed : failed);
  31098. wolfSSL_X509_free(x509);
  31099. printf(resultFmt, passed);
  31100. #endif
  31101. }
  31102. static void test_wolfSSL_X509_sign2(void)
  31103. {
  31104. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  31105. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  31106. defined(WOLFSSL_CERT_EXT) && \
  31107. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  31108. WOLFSSL_X509 *x509, *ca;
  31109. const unsigned char *der;
  31110. const unsigned char *pt;
  31111. WOLFSSL_EVP_PKEY *priv;
  31112. WOLFSSL_X509_NAME *name;
  31113. WOLFSSL_ASN1_TIME *notBefore, *notAfter;
  31114. int derSz;
  31115. const int year = 365*24*60*60;
  31116. const int day = 24*60*60;
  31117. const int hour = 60*60;
  31118. const int mini = 60;
  31119. time_t t;
  31120. const unsigned char expected[] = {
  31121. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xfb, 0xa0, 0x03, 0x02, 0x01,
  31122. 0x02, 0x02, 0x14, 0x01, 0x1a, 0xeb, 0x56, 0xab, 0xdc, 0x8b, 0xf3, 0xa6,
  31123. 0x1e, 0xf4, 0x93, 0x60, 0x89, 0xb7, 0x05, 0x07, 0x29, 0x01, 0x2c, 0x30,
  31124. 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
  31125. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55,
  31126. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03,
  31127. 0x55, 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61,
  31128. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42,
  31129. 0x6f, 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x11, 0x30, 0x0f, 0x06, 0x03,
  31130. 0x55, 0x04, 0x0a, 0x0c, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6f, 0x6f, 0x74,
  31131. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0a,
  31132. 0x43, 0x6f, 0x6e, 0x73, 0x75, 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x31, 0x18,
  31133. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77, 0x77,
  31134. 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  31135. 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  31136. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f,
  31137. 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17,
  31138. 0x0d, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  31139. 0x30, 0x5a, 0x17, 0x0d, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  31140. 0x33, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09,
  31141. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  31142. 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74,
  31143. 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07,
  31144. 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x15, 0x30,
  31145. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0c, 0x77, 0x6f, 0x6c, 0x66,
  31146. 0x53, 0x53, 0x4c, 0x5f, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  31147. 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x10, 0x50, 0x72, 0x6f, 0x67, 0x72,
  31148. 0x61, 0x6d, 0x6d, 0x69, 0x6e, 0x67, 0x2d, 0x32, 0x30, 0x34, 0x38, 0x31,
  31149. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77,
  31150. 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f,
  31151. 0x6d, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
  31152. 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77,
  31153. 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x82,
  31154. 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  31155. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82,
  31156. 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xc3, 0x03, 0xd1, 0x2b, 0xfe,
  31157. 0x39, 0xa4, 0x32, 0x45, 0x3b, 0x53, 0xc8, 0x84, 0x2b, 0x2a, 0x7c, 0x74,
  31158. 0x9a, 0xbd, 0xaa, 0x2a, 0x52, 0x07, 0x47, 0xd6, 0xa6, 0x36, 0xb2, 0x07,
  31159. 0x32, 0x8e, 0xd0, 0xba, 0x69, 0x7b, 0xc6, 0xc3, 0x44, 0x9e, 0xd4, 0x81,
  31160. 0x48, 0xfd, 0x2d, 0x68, 0xa2, 0x8b, 0x67, 0xbb, 0xa1, 0x75, 0xc8, 0x36,
  31161. 0x2c, 0x4a, 0xd2, 0x1b, 0xf7, 0x8b, 0xba, 0xcf, 0x0d, 0xf9, 0xef, 0xec,
  31162. 0xf1, 0x81, 0x1e, 0x7b, 0x9b, 0x03, 0x47, 0x9a, 0xbf, 0x65, 0xcc, 0x7f,
  31163. 0x65, 0x24, 0x69, 0xa6, 0xe8, 0x14, 0x89, 0x5b, 0xe4, 0x34, 0xf7, 0xc5,
  31164. 0xb0, 0x14, 0x93, 0xf5, 0x67, 0x7b, 0x3a, 0x7a, 0x78, 0xe1, 0x01, 0x56,
  31165. 0x56, 0x91, 0xa6, 0x13, 0x42, 0x8d, 0xd2, 0x3c, 0x40, 0x9c, 0x4c, 0xef,
  31166. 0xd1, 0x86, 0xdf, 0x37, 0x51, 0x1b, 0x0c, 0xa1, 0x3b, 0xf5, 0xf1, 0xa3,
  31167. 0x4a, 0x35, 0xe4, 0xe1, 0xce, 0x96, 0xdf, 0x1b, 0x7e, 0xbf, 0x4e, 0x97,
  31168. 0xd0, 0x10, 0xe8, 0xa8, 0x08, 0x30, 0x81, 0xaf, 0x20, 0x0b, 0x43, 0x14,
  31169. 0xc5, 0x74, 0x67, 0xb4, 0x32, 0x82, 0x6f, 0x8d, 0x86, 0xc2, 0x88, 0x40,
  31170. 0x99, 0x36, 0x83, 0xba, 0x1e, 0x40, 0x72, 0x22, 0x17, 0xd7, 0x52, 0x65,
  31171. 0x24, 0x73, 0xb0, 0xce, 0xef, 0x19, 0xcd, 0xae, 0xff, 0x78, 0x6c, 0x7b,
  31172. 0xc0, 0x12, 0x03, 0xd4, 0x4e, 0x72, 0x0d, 0x50, 0x6d, 0x3b, 0xa3, 0x3b,
  31173. 0xa3, 0x99, 0x5e, 0x9d, 0xc8, 0xd9, 0x0c, 0x85, 0xb3, 0xd9, 0x8a, 0xd9,
  31174. 0x54, 0x26, 0xdb, 0x6d, 0xfa, 0xac, 0xbb, 0xff, 0x25, 0x4c, 0xc4, 0xd1,
  31175. 0x79, 0xf4, 0x71, 0xd3, 0x86, 0x40, 0x18, 0x13, 0xb0, 0x63, 0xb5, 0x72,
  31176. 0x4e, 0x30, 0xc4, 0x97, 0x84, 0x86, 0x2d, 0x56, 0x2f, 0xd7, 0x15, 0xf7,
  31177. 0x7f, 0xc0, 0xae, 0xf5, 0xfc, 0x5b, 0xe5, 0xfb, 0xa1, 0xba, 0xd3, 0x02,
  31178. 0x03, 0x01, 0x00, 0x01, 0xa3, 0x82, 0x01, 0x4f, 0x30, 0x82, 0x01, 0x4b,
  31179. 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  31180. 0x01, 0xff, 0x30, 0x1c, 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x15, 0x30,
  31181. 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63,
  31182. 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01, 0x30, 0x1d, 0x06, 0x03,
  31183. 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x33, 0xd8, 0x45, 0x66, 0xd7,
  31184. 0x68, 0x87, 0x18, 0x7e, 0x54, 0x0d, 0x70, 0x27, 0x91, 0xc7, 0x26, 0xd7,
  31185. 0x85, 0x65, 0xc0, 0x30, 0x81, 0xde, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04,
  31186. 0x81, 0xd6, 0x30, 0x81, 0xd3, 0x80, 0x14, 0x33, 0xd8, 0x45, 0x66, 0xd7,
  31187. 0x68, 0x87, 0x18, 0x7e, 0x54, 0x0d, 0x70, 0x27, 0x91, 0xc7, 0x26, 0xd7,
  31188. 0x85, 0x65, 0xc0, 0xa1, 0x81, 0xa4, 0xa4, 0x81, 0xa1, 0x30, 0x81, 0x9e,
  31189. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  31190. 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x07,
  31191. 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06,
  31192. 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d, 0x61,
  31193. 0x6e, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0c,
  31194. 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x5f, 0x32, 0x30, 0x34, 0x38,
  31195. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x10, 0x50,
  31196. 0x72, 0x6f, 0x67, 0x72, 0x61, 0x6d, 0x6d, 0x69, 0x6e, 0x67, 0x2d, 0x32,
  31197. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  31198. 0x0c, 0x0f, 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73,
  31199. 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a,
  31200. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e,
  31201. 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63,
  31202. 0x6f, 0x6d, 0x82, 0x14, 0x01, 0x1a, 0xeb, 0x56, 0xab, 0xdc, 0x8b, 0xf3,
  31203. 0xa6, 0x1e, 0xf4, 0x93, 0x60, 0x89, 0xb7, 0x05, 0x07, 0x29, 0x01, 0x2c,
  31204. 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  31205. 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2b,
  31206. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0d, 0x06, 0x09, 0x2a,
  31207. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82,
  31208. 0x01, 0x01, 0x00, 0xa3, 0x41, 0x43, 0x93, 0x30, 0x92, 0x98, 0xfe, 0x57,
  31209. 0xd0, 0x39, 0x7c, 0x50, 0x06, 0x50, 0x20, 0x80, 0x0e, 0x28, 0x95, 0x79,
  31210. 0xb4, 0xf1, 0x6b, 0x6a, 0xab, 0x78, 0x30, 0x93, 0x49, 0x0a, 0x6a, 0x19,
  31211. 0x09, 0xae, 0x31, 0xc6, 0x8e, 0xcc, 0x69, 0x26, 0x89, 0x37, 0xc1, 0x57,
  31212. 0x58, 0x75, 0xae, 0xbf, 0x13, 0xc8, 0xd6, 0xad, 0xd0, 0x0f, 0x57, 0xcd,
  31213. 0x32, 0xa8, 0xda, 0xa8, 0x1b, 0xbf, 0xb5, 0xcd, 0x16, 0x14, 0x56, 0x86,
  31214. 0x84, 0xb4, 0xab, 0x93, 0x52, 0x74, 0xfd, 0x96, 0x9f, 0x6d, 0xbe, 0xdb,
  31215. 0x75, 0x5e, 0x76, 0xfe, 0xa6, 0x37, 0xe5, 0x5f, 0xcb, 0x62, 0x77, 0xc7,
  31216. 0xd6, 0xcb, 0xb4, 0xf6, 0x43, 0xc8, 0x47, 0xdf, 0x12, 0x16, 0x28, 0x29,
  31217. 0x61, 0xd1, 0xdc, 0x9d, 0x37, 0x9f, 0xe5, 0x71, 0x52, 0xae, 0xb8, 0x12,
  31218. 0xec, 0x32, 0x9f, 0x03, 0x1a, 0x66, 0x98, 0xd8, 0xb0, 0x40, 0x71, 0x4c,
  31219. 0xee, 0x64, 0x15, 0x48, 0x0c, 0x5c, 0x8a, 0x47, 0x20, 0xbd, 0x07, 0xc0,
  31220. 0x30, 0xf8, 0x84, 0xe6, 0x29, 0x6d, 0xa9, 0x32, 0x53, 0x02, 0x4d, 0x3c,
  31221. 0x99, 0x6e, 0x63, 0xfe, 0x39, 0x9c, 0x05, 0xa6, 0xa0, 0x0c, 0x1e, 0x11,
  31222. 0xa4, 0x86, 0x6a, 0x89, 0x76, 0x54, 0x17, 0x68, 0x5d, 0x35, 0x9a, 0xd7,
  31223. 0x5e, 0x27, 0x0e, 0xbb, 0xba, 0x67, 0x4d, 0x62, 0x12, 0xa8, 0x46, 0x1f,
  31224. 0x0e, 0xd8, 0x7d, 0xc0, 0xae, 0x30, 0xc2, 0x45, 0x71, 0xab, 0xb1, 0xc1,
  31225. 0xfb, 0xdc, 0x03, 0x7a, 0x52, 0xe6, 0x57, 0xf9, 0x7f, 0x65, 0x6b, 0x4e,
  31226. 0x44, 0x64, 0xe8, 0x77, 0x82, 0x1c, 0xc8, 0xfa, 0x09, 0xc7, 0x2f, 0xa9,
  31227. 0x40, 0x87, 0x8e, 0x0e, 0x49, 0xc2, 0x7d, 0x97, 0x27, 0x79, 0x90, 0xc2,
  31228. 0x90, 0x13, 0xa7, 0x49, 0xb7, 0xd7, 0xc5, 0x02, 0x32, 0x4f, 0x1e, 0x34,
  31229. 0x4a, 0xa6, 0xe4, 0xbd, 0xa5, 0xc6, 0xec
  31230. };
  31231. printf(testingFmt, "wolfSSL_X509_sign2");
  31232. pt = ca_key_der_2048;
  31233. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  31234. sizeof_ca_key_der_2048));
  31235. pt = client_cert_der_2048;
  31236. AssertNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  31237. sizeof_client_cert_der_2048));
  31238. pt = ca_cert_der_2048;
  31239. AssertNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  31240. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  31241. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  31242. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  31243. AssertNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  31244. AssertNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  31245. AssertIntEQ(notAfter->length, 13);
  31246. AssertTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  31247. AssertTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  31248. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  31249. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  31250. AssertIntEQ(derSz, sizeof(expected));
  31251. AssertIntEQ(XMEMCMP(der, expected, derSz), 0);
  31252. wolfSSL_X509_free(ca);
  31253. wolfSSL_X509_free(x509);
  31254. wolfSSL_EVP_PKEY_free(priv);
  31255. wolfSSL_ASN1_TIME_free(notBefore);
  31256. wolfSSL_ASN1_TIME_free(notAfter);
  31257. printf(resultFmt, passed);
  31258. #endif
  31259. }
  31260. static void test_wolfSSL_X509_sign(void)
  31261. {
  31262. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31263. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  31264. int ret;
  31265. char *cn;
  31266. word32 cnSz;
  31267. X509_NAME *name;
  31268. X509 *x509, *ca;
  31269. DecodedCert dCert;
  31270. EVP_PKEY *pub;
  31271. EVP_PKEY *priv;
  31272. EVP_MD_CTX *mctx;
  31273. #if defined(USE_CERT_BUFFERS_1024)
  31274. const unsigned char* rsaPriv = client_key_der_1024;
  31275. const unsigned char* rsaPub = client_keypub_der_1024;
  31276. const unsigned char* certIssuer = client_cert_der_1024;
  31277. long clientKeySz = (long)sizeof_client_key_der_1024;
  31278. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  31279. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  31280. #elif defined(USE_CERT_BUFFERS_2048)
  31281. const unsigned char* rsaPriv = client_key_der_2048;
  31282. const unsigned char* rsaPub = client_keypub_der_2048;
  31283. const unsigned char* certIssuer = client_cert_der_2048;
  31284. long clientKeySz = (long)sizeof_client_key_der_2048;
  31285. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  31286. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  31287. #endif
  31288. byte sn[16];
  31289. int snSz = sizeof(sn);
  31290. printf(testingFmt, "wolfSSL_X509_sign");
  31291. /* Set X509_NAME fields */
  31292. AssertNotNull(name = X509_NAME_new());
  31293. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  31294. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  31295. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  31296. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  31297. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  31298. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  31299. /* Get private and public keys */
  31300. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  31301. clientKeySz));
  31302. AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  31303. AssertNotNull(x509 = X509_new());
  31304. /* Set version 3 */
  31305. AssertIntNE(X509_set_version(x509, 2L), 0);
  31306. /* Set subject name, add pubkey, and sign certificate */
  31307. AssertIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  31308. X509_NAME_free(name);
  31309. AssertIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  31310. #ifdef WOLFSSL_ALT_NAMES
  31311. /* Add some subject alt names */
  31312. AssertIntNE(wolfSSL_X509_add_altname(NULL,
  31313. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  31314. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  31315. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  31316. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  31317. "sphygmomanometer",
  31318. ASN_DNS_TYPE), SSL_SUCCESS);
  31319. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  31320. "supercalifragilisticexpialidocious",
  31321. ASN_DNS_TYPE), SSL_SUCCESS);
  31322. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  31323. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  31324. ASN_DNS_TYPE), SSL_SUCCESS);
  31325. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  31326. {
  31327. unsigned char ip4_type[] = {127,128,0,255};
  31328. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  31329. 0xff, 0xee, 0x99, 0x88,
  31330. 0x77, 0x66, 0x55, 0x44,
  31331. 0x00, 0x33, 0x22, 0x11};
  31332. AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  31333. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  31334. AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  31335. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  31336. }
  31337. #endif
  31338. #endif /* WOLFSSL_ALT_NAMES */
  31339. /* test valid sign case */
  31340. ret = X509_sign(x509, priv, EVP_sha256());
  31341. /* test valid X509_sign_ctx case */
  31342. AssertNotNull(mctx = EVP_MD_CTX_new());
  31343. AssertIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  31344. AssertIntGT(X509_sign_ctx(x509, mctx), 0);
  31345. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  31346. AssertIntEQ(X509_get_ext_count(x509), 1);
  31347. #endif
  31348. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  31349. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  31350. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  31351. #endif
  31352. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  31353. WOLFSSL_SUCCESS);
  31354. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  31355. /* Variation in size depends on ASN.1 encoding when MSB is set.
  31356. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  31357. * with the MSB set. See GenerateInteger in asn.c */
  31358. #ifndef USE_CERT_BUFFERS_1024
  31359. #ifndef WOLFSSL_ALT_NAMES
  31360. /* Valid case - size should be 798-797 with 16 byte serial number */
  31361. AssertTrue((ret == 781 + snSz) || (ret == 782 + snSz));
  31362. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  31363. /* Valid case - size should be 955-956 with 16 byte serial number */
  31364. AssertTrue((ret == 939 + snSz) || (ret == 940 + snSz));
  31365. #else
  31366. /* Valid case - size should be 926-927 with 16 byte serial number */
  31367. AssertTrue((ret == 910 + snSz) || (ret == 911 + snSz));
  31368. #endif
  31369. #else
  31370. #ifndef WOLFSSL_ALT_NAMES
  31371. /* Valid case - size should be 537-538 with 16 byte serial number */
  31372. AssertTrue((ret == 521 + snSz) || (ret == 522 + snSz));
  31373. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  31374. /* Valid case - size should be 695-696 with 16 byte serial number */
  31375. AssertTrue((ret == 679 + snSz) || (ret == 680 + snSz));
  31376. #else
  31377. /* Valid case - size should be 666-667 with 16 byte serial number */
  31378. AssertTrue((ret == 650 + snSz) || (ret == 651 + snSz));
  31379. #endif
  31380. #endif
  31381. /* check that issuer name is as expected after signature */
  31382. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  31383. AssertIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  31384. AssertNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  31385. AssertNotNull(name = X509_get_subject_name(ca));
  31386. cnSz = X509_NAME_get_sz(name);
  31387. AssertNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  31388. AssertNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  31389. AssertIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  31390. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  31391. #ifdef WOLFSSL_MULTI_ATTRIB
  31392. /* test adding multiple OU's to the signer */
  31393. AssertNotNull(name = X509_get_subject_name(ca));
  31394. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  31395. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  31396. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  31397. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  31398. AssertIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  31399. #endif
  31400. AssertNotNull(name = X509_get_subject_name(ca));
  31401. AssertIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  31402. AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  31403. AssertNotNull(name = X509_get_issuer_name(x509));
  31404. cnSz = X509_NAME_get_sz(name);
  31405. AssertNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  31406. AssertNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  31407. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  31408. AssertIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  31409. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  31410. FreeDecodedCert(&dCert);
  31411. /* Test invalid parameters */
  31412. AssertIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  31413. AssertIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  31414. AssertIntEQ(X509_sign(x509, priv, NULL), 0);
  31415. AssertIntEQ(X509_sign_ctx(NULL, mctx), 0);
  31416. EVP_MD_CTX_free(mctx);
  31417. AssertNotNull(mctx = EVP_MD_CTX_new());
  31418. AssertIntEQ(X509_sign_ctx(x509, mctx), 0);
  31419. AssertIntEQ(X509_sign_ctx(x509, NULL), 0);
  31420. /* test invalid version number */
  31421. #if defined(OPENSSL_ALL)
  31422. AssertIntNE(X509_set_version(x509, 6L), 0);
  31423. AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  31424. /* uses ParseCert which fails on bad version number */
  31425. AssertIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  31426. #endif
  31427. EVP_MD_CTX_free(mctx);
  31428. EVP_PKEY_free(priv);
  31429. EVP_PKEY_free(pub);
  31430. X509_free(x509);
  31431. X509_free(ca);
  31432. printf(resultFmt, passed);
  31433. #endif
  31434. }
  31435. static void test_wolfSSL_X509_get0_tbs_sigalg(void)
  31436. {
  31437. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  31438. X509* x509 = NULL;
  31439. const X509_ALGOR* alg;
  31440. printf(testingFmt, "wolfSSL_X509_get0_tbs_sigalg");
  31441. AssertNotNull(x509 = X509_new());
  31442. AssertNull(alg = X509_get0_tbs_sigalg(NULL));
  31443. AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
  31444. X509_free(x509);
  31445. printf(resultFmt, passed);
  31446. #endif
  31447. }
  31448. static void test_wolfSSL_X509_ALGOR_get0(void)
  31449. {
  31450. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  31451. !defined(NO_SHA256) && !defined(NO_RSA)
  31452. X509* x509 = NULL;
  31453. const ASN1_OBJECT* obj = NULL;
  31454. const X509_ALGOR* alg;
  31455. int pptype = 0;
  31456. const void *ppval = NULL;
  31457. printf(testingFmt, "wolfSSL_X509_ALGOR_get0");
  31458. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  31459. SSL_FILETYPE_PEM));
  31460. AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
  31461. /* Invalid case */
  31462. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  31463. AssertNull(obj);
  31464. /* Valid case */
  31465. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  31466. AssertNotNull(obj);
  31467. AssertNull(ppval);
  31468. AssertIntNE(pptype, 0);
  31469. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  31470. AssertIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  31471. X509_free(x509);
  31472. printf(resultFmt, passed);
  31473. #endif
  31474. }
  31475. static void test_wolfSSL_X509_VERIFY_PARAM(void)
  31476. {
  31477. #if defined(OPENSSL_EXTRA)
  31478. X509_VERIFY_PARAM *paramTo;
  31479. X509_VERIFY_PARAM *paramFrom;
  31480. int ret;
  31481. char testIPv4[] = "127.0.0.1";
  31482. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  31483. char testhostName1[] = "foo.hoge.com";
  31484. char testhostName2[] = "foobar.hoge.com";
  31485. printf(testingFmt, "wolfSSL_X509()");
  31486. paramTo = X509_VERIFY_PARAM_new();
  31487. AssertNotNull(paramTo);
  31488. XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM ));
  31489. paramFrom = X509_VERIFY_PARAM_new();
  31490. AssertNotNull(paramFrom);
  31491. XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM ));
  31492. ret = X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  31493. (int)XSTRLEN(testhostName1));
  31494. AssertIntEQ(1, ret);
  31495. AssertIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  31496. (int)XSTRLEN(testhostName1)));
  31497. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  31498. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  31499. AssertIntEQ(0x01, paramFrom->hostFlags);
  31500. ret = X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4);
  31501. AssertIntEQ(0, ret);
  31502. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4);
  31503. AssertIntEQ(1, ret);
  31504. AssertIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  31505. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL);
  31506. AssertIntEQ(1, ret);
  31507. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6);
  31508. AssertIntEQ(1, ret);
  31509. AssertIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  31510. /* null pointer */
  31511. ret = X509_VERIFY_PARAM_set1(NULL, paramFrom);
  31512. AssertIntEQ(WOLFSSL_FAILURE, ret);
  31513. /* in the case of "from" null, returns success */
  31514. ret = X509_VERIFY_PARAM_set1(paramTo, NULL);
  31515. AssertIntEQ(WOLFSSL_SUCCESS, ret);
  31516. ret = X509_VERIFY_PARAM_set1(NULL, NULL);
  31517. AssertIntEQ(WOLFSSL_FAILURE, ret);
  31518. /* inherit flags test : VPARAM_DEFAULT */
  31519. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  31520. AssertIntEQ(1, ret);
  31521. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  31522. (int)XSTRLEN(testhostName1)));
  31523. AssertIntEQ(0x01, paramTo->hostFlags);
  31524. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  31525. /* inherit flags test : VPARAM OVERWRITE */
  31526. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  31527. (int)XSTRLEN(testhostName2));
  31528. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  31529. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  31530. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  31531. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  31532. AssertIntEQ(1, ret);
  31533. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  31534. (int)XSTRLEN(testhostName1)));
  31535. AssertIntEQ(0x01, paramTo->hostFlags);
  31536. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  31537. /* inherit flags test : VPARAM_RESET_FLAGS */
  31538. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  31539. (int)XSTRLEN(testhostName2));
  31540. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  31541. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  31542. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  31543. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  31544. AssertIntEQ(1, ret);
  31545. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  31546. (int)XSTRLEN(testhostName1)));
  31547. AssertIntEQ(0x01, paramTo->hostFlags);
  31548. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  31549. /* inherit flags test : VPARAM_LOCKED */
  31550. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  31551. (int)XSTRLEN(testhostName2));
  31552. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  31553. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  31554. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  31555. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  31556. AssertIntEQ(1, ret);
  31557. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  31558. (int)XSTRLEN(testhostName2)));
  31559. AssertIntEQ(0x00, paramTo->hostFlags);
  31560. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  31561. /* test for incorrect parameters */
  31562. ret = X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL );
  31563. AssertIntEQ(0, ret);
  31564. ret = X509_VERIFY_PARAM_set_flags(NULL, 0 );
  31565. AssertIntEQ(0, ret);
  31566. /* inherit flags test : VPARAM_ONCE, not testable yet */
  31567. ret = X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL);
  31568. AssertIntEQ(1, ret);
  31569. ret = X509_VERIFY_PARAM_get_flags(paramTo);
  31570. AssertIntEQ(X509_V_FLAG_CRL_CHECK_ALL, ret);
  31571. ret = X509_VERIFY_PARAM_clear_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL);
  31572. AssertIntEQ(1, ret);
  31573. ret = X509_VERIFY_PARAM_get_flags(paramTo);
  31574. AssertIntEQ(0, ret);
  31575. X509_VERIFY_PARAM_free(paramTo);
  31576. X509_VERIFY_PARAM_free(paramFrom);
  31577. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  31578. printf(resultFmt, passed);
  31579. #endif
  31580. }
  31581. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  31582. static int test_wolfSSL_check_domain_verify_count = 0;
  31583. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  31584. WOLFSSL_X509_STORE_CTX* store)
  31585. {
  31586. AssertIntEQ(X509_STORE_CTX_get_error(store), 0);
  31587. AssertIntEQ(preverify, 1);
  31588. test_wolfSSL_check_domain_verify_count++;
  31589. return 1;
  31590. }
  31591. static void test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  31592. {
  31593. X509_VERIFY_PARAM *param = SSL_get0_param(ssl);
  31594. /* Domain check should only be done on the leaf cert */
  31595. X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  31596. AssertIntEQ(X509_VERIFY_PARAM_set1_host(param,
  31597. "wolfSSL Server Chain", 0), 1);
  31598. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  31599. test_wolfSSL_check_domain_verify_cb);
  31600. }
  31601. static void test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  31602. {
  31603. /* Use a cert with different domains in chain */
  31604. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  31605. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  31606. }
  31607. static void test_wolfSSL_check_domain(void)
  31608. {
  31609. tcp_ready ready;
  31610. func_args client_args;
  31611. func_args server_args;
  31612. THREAD_TYPE serverThread;
  31613. callback_functions func_cb_client;
  31614. callback_functions func_cb_server;
  31615. printf(testingFmt, "wolfSSL_check_domain");
  31616. XMEMSET(&client_args, 0, sizeof(func_args));
  31617. XMEMSET(&server_args, 0, sizeof(func_args));
  31618. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  31619. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  31620. #ifdef WOLFSSL_TIRTOS
  31621. fdOpenSession(Task_self());
  31622. #endif
  31623. StartTCP();
  31624. InitTcpReady(&ready);
  31625. #if defined(USE_WINDOWS_API)
  31626. /* use RNG to get random port if using windows */
  31627. ready.port = GetRandomPort();
  31628. #endif
  31629. server_args.signal = &ready;
  31630. client_args.signal = &ready;
  31631. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  31632. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  31633. client_args.callbacks = &func_cb_client;
  31634. server_args.callbacks = &func_cb_server;
  31635. start_thread(test_server_nofail, &server_args, &serverThread);
  31636. wait_tcp_ready(&server_args);
  31637. test_client_nofail(&client_args, NULL);
  31638. join_thread(serverThread);
  31639. AssertTrue(client_args.return_code);
  31640. AssertTrue(server_args.return_code);
  31641. FreeTcpReady(&ready);
  31642. /* Should have been called once for each cert in sent chain */
  31643. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  31644. AssertIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  31645. #else
  31646. AssertIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  31647. #endif
  31648. printf(resultFmt, passed);
  31649. }
  31650. #endif /* OPENSSL_EXTRA && HAVE_IO_TESTS_DEPENDENCIES */
  31651. static void test_wolfSSL_X509_get_X509_PUBKEY(void)
  31652. {
  31653. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  31654. X509* x509 = NULL;
  31655. X509_PUBKEY* pubKey;
  31656. printf(testingFmt, "wolfSSL_X509_get_X509_PUBKEY");
  31657. AssertNotNull(x509 = X509_new());
  31658. AssertNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  31659. AssertNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  31660. X509_free(x509);
  31661. printf(resultFmt, passed);
  31662. #endif
  31663. }
  31664. static void test_wolfSSL_X509_PUBKEY_RSA(void)
  31665. {
  31666. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  31667. !defined(NO_SHA256) && !defined(NO_RSA)
  31668. X509* x509 = NULL;
  31669. ASN1_OBJECT* obj = NULL;
  31670. const ASN1_OBJECT* pa_oid = NULL;
  31671. X509_PUBKEY* pubKey;
  31672. X509_PUBKEY* pubKey2;
  31673. EVP_PKEY* evpKey;
  31674. const unsigned char *pk;
  31675. int ppklen, pptype;
  31676. X509_ALGOR *pa;
  31677. const void *pval;
  31678. printf(testingFmt, "wolfSSL_X509_PUBKEY_RSA");
  31679. AssertNotNull(x509 = X509_load_certificate_file(cliCertFile,
  31680. SSL_FILETYPE_PEM));
  31681. AssertNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  31682. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  31683. AssertNotNull(pk);
  31684. AssertNotNull(pa);
  31685. AssertNotNull(pubKey);
  31686. AssertIntGT(ppklen, 0);
  31687. AssertIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  31688. AssertNotNull(evpKey = X509_PUBKEY_get(pubKey));
  31689. AssertNotNull(pubKey2 = X509_PUBKEY_new());
  31690. AssertIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  31691. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  31692. AssertNotNull(pk);
  31693. AssertNotNull(pa);
  31694. AssertIntGT(ppklen, 0);
  31695. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  31696. AssertNotNull(pa_oid);
  31697. AssertNull(pval);
  31698. AssertIntEQ(pptype, V_ASN1_NULL);
  31699. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  31700. X509_PUBKEY_free(pubKey2);
  31701. X509_free(x509);
  31702. EVP_PKEY_free(evpKey);
  31703. printf(resultFmt, passed);
  31704. #endif
  31705. }
  31706. static void test_wolfSSL_X509_PUBKEY_EC(void)
  31707. {
  31708. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  31709. X509* x509 = NULL;
  31710. ASN1_OBJECT* obj = NULL;
  31711. ASN1_OBJECT* poid;
  31712. const ASN1_OBJECT* pa_oid = NULL;
  31713. X509_PUBKEY* pubKey;
  31714. X509_PUBKEY* pubKey2;
  31715. EVP_PKEY* evpKey;
  31716. const unsigned char *pk;
  31717. int ppklen, pptype;
  31718. X509_ALGOR *pa;
  31719. const void *pval;
  31720. char buf[50];
  31721. printf(testingFmt, "wolfSSL_X509_PUBKEY_EC");
  31722. AssertNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  31723. SSL_FILETYPE_PEM));
  31724. AssertNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  31725. AssertNotNull(evpKey = X509_PUBKEY_get(pubKey));
  31726. AssertNotNull(pubKey2 = X509_PUBKEY_new());
  31727. AssertIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  31728. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  31729. AssertNotNull(pk);
  31730. AssertNotNull(pa);
  31731. AssertIntGT(ppklen, 0);
  31732. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  31733. AssertNotNull(pa_oid);
  31734. AssertNotNull(pval);
  31735. AssertIntEQ(pptype, V_ASN1_OBJECT);
  31736. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  31737. poid = (ASN1_OBJECT *)pval;
  31738. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  31739. AssertIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  31740. X509_PUBKEY_free(pubKey2);
  31741. X509_free(x509);
  31742. EVP_PKEY_free(evpKey);
  31743. printf(resultFmt, passed);
  31744. #endif
  31745. }
  31746. static void test_wolfSSL_X509_PUBKEY_DSA(void)
  31747. {
  31748. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  31749. word32 bytes;
  31750. #ifdef USE_CERT_BUFFERS_1024
  31751. byte tmp[ONEK_BUF];
  31752. #elif defined(USE_CERT_BUFFERS_2048)
  31753. byte tmp[TWOK_BUF];
  31754. #else
  31755. byte tmp[TWOK_BUF];
  31756. #endif /* END USE_CERT_BUFFERS_1024 */
  31757. const unsigned char* dsaKeyDer = tmp;
  31758. ASN1_OBJECT* obj = NULL;
  31759. ASN1_STRING* str;
  31760. const ASN1_OBJECT* pa_oid = NULL;
  31761. X509_PUBKEY* pubKey = NULL;
  31762. EVP_PKEY* evpKey = NULL;
  31763. const unsigned char *pk;
  31764. int ppklen, pptype;
  31765. X509_ALGOR *pa;
  31766. const void *pval;
  31767. printf(testingFmt, "wolfSSL_X509_PUBKEY_DSA");
  31768. #ifdef USE_CERT_BUFFERS_1024
  31769. XMEMSET(tmp, 0, sizeof(tmp));
  31770. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  31771. bytes = sizeof_dsa_key_der_1024;
  31772. #elif defined(USE_CERT_BUFFERS_2048)
  31773. XMEMSET(tmp, 0, sizeof(tmp));
  31774. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  31775. bytes = sizeof_dsa_key_der_2048;
  31776. #else
  31777. {
  31778. XFILE fp;
  31779. XMEMSET(tmp, 0, sizeof(tmp));
  31780. fp = XFOPEN("./certs/dsa2048.der", "rb");
  31781. if (fp == XBADFILE) {
  31782. return WOLFSSL_BAD_FILE;
  31783. }
  31784. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  31785. XFCLOSE(fp);
  31786. }
  31787. #endif
  31788. /* Initialize pkey with der format dsa key */
  31789. AssertNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  31790. AssertNotNull(pubKey = X509_PUBKEY_new());
  31791. AssertIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  31792. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  31793. AssertNotNull(pk);
  31794. AssertNotNull(pa);
  31795. AssertIntGT(ppklen, 0);
  31796. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  31797. AssertNotNull(pa_oid);
  31798. AssertNotNull(pval);
  31799. AssertIntEQ(pptype, V_ASN1_SEQUENCE);
  31800. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  31801. str = (ASN1_STRING *)pval;
  31802. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  31803. #ifdef USE_CERT_BUFFERS_1024
  31804. AssertIntEQ(ASN1_STRING_length(str), 291);
  31805. #else
  31806. AssertIntEQ(ASN1_STRING_length(str), 549);
  31807. #endif /* END USE_CERT_BUFFERS_1024 */
  31808. X509_PUBKEY_free(pubKey);
  31809. EVP_PKEY_free(evpKey);
  31810. printf(resultFmt, passed);
  31811. #endif
  31812. }
  31813. static void test_wolfSSL_RAND(void)
  31814. {
  31815. #if defined(OPENSSL_EXTRA)
  31816. byte seed[16];
  31817. printf(testingFmt, "wolfSSL_RAND()");
  31818. RAND_seed(seed, sizeof(seed));
  31819. AssertIntEQ(RAND_poll(), 1);
  31820. RAND_cleanup();
  31821. AssertIntEQ(RAND_egd(NULL), -1);
  31822. #ifndef NO_FILESYSTEM
  31823. {
  31824. char fname[100];
  31825. AssertNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  31826. AssertIntEQ(RAND_write_file(NULL), 0);
  31827. }
  31828. #endif
  31829. printf(resultFmt, passed);
  31830. #endif
  31831. }
  31832. static void test_wolfSSL_BUF(void)
  31833. {
  31834. #if defined(OPENSSL_EXTRA)
  31835. BUF_MEM* buf;
  31836. AssertNotNull(buf = BUF_MEM_new());
  31837. AssertIntEQ(BUF_MEM_grow(buf, 10), 10);
  31838. AssertIntEQ(BUF_MEM_grow(buf, -1), 0);
  31839. BUF_MEM_free(buf);
  31840. #endif /* OPENSSL_EXTRA */
  31841. }
  31842. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  31843. static int stub_rand_seed(const void *buf, int num)
  31844. {
  31845. (void)buf;
  31846. (void)num;
  31847. return 123;
  31848. }
  31849. static int stub_rand_bytes(unsigned char *buf, int num)
  31850. {
  31851. (void)buf;
  31852. (void)num;
  31853. return 456;
  31854. }
  31855. static byte* was_stub_rand_cleanup_called(void)
  31856. {
  31857. static byte was_called = 0;
  31858. return &was_called;
  31859. }
  31860. static void stub_rand_cleanup(void)
  31861. {
  31862. byte* was_called = was_stub_rand_cleanup_called();
  31863. *was_called = 1;
  31864. return;
  31865. }
  31866. static byte* was_stub_rand_add_called(void)
  31867. {
  31868. static byte was_called = 0;
  31869. return &was_called;
  31870. }
  31871. static int stub_rand_add(const void *buf, int num, double entropy)
  31872. {
  31873. byte* was_called = was_stub_rand_add_called();
  31874. (void)buf;
  31875. (void)num;
  31876. (void)entropy;
  31877. *was_called = 1;
  31878. return 0;
  31879. }
  31880. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  31881. {
  31882. (void)buf;
  31883. (void)num;
  31884. return 9876;
  31885. }
  31886. static int stub_rand_status(void)
  31887. {
  31888. return 5432;
  31889. }
  31890. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  31891. static void test_wolfSSL_RAND_set_rand_method(void)
  31892. {
  31893. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  31894. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  31895. unsigned char* buf = NULL;
  31896. int num = 0;
  31897. double entropy = 0;
  31898. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  31899. byte* was_add_called = was_stub_rand_add_called();
  31900. printf(testingFmt, "wolfSSL_RAND_set_rand_method()");
  31901. buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  31902. DYNAMIC_TYPE_TMP_BUFFER);
  31903. AssertIntNE(wolfSSL_RAND_status(), 5432);
  31904. AssertIntEQ(*was_cleanup_called, 0);
  31905. RAND_cleanup();
  31906. AssertIntEQ(*was_cleanup_called, 0);
  31907. rand_methods.seed = &stub_rand_seed;
  31908. rand_methods.bytes = &stub_rand_bytes;
  31909. rand_methods.cleanup = &stub_rand_cleanup;
  31910. rand_methods.add = &stub_rand_add;
  31911. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  31912. rand_methods.status = &stub_rand_status;
  31913. AssertIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  31914. AssertIntEQ(RAND_seed(buf, num), 123);
  31915. AssertIntEQ(RAND_bytes(buf, num), 456);
  31916. AssertIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  31917. AssertIntEQ(RAND_status(), 5432);
  31918. AssertIntEQ(*was_add_called, 0);
  31919. /* The function pointer for RAND_add returns int, but RAND_add itself returns void. */
  31920. RAND_add(buf, num, entropy);
  31921. AssertIntEQ(*was_add_called, 1);
  31922. was_add_called = 0;
  31923. AssertIntEQ(*was_cleanup_called, 0);
  31924. RAND_cleanup();
  31925. AssertIntEQ(*was_cleanup_called, 1);
  31926. *was_cleanup_called = 0;
  31927. AssertIntEQ(RAND_set_rand_method(NULL), WOLFSSL_SUCCESS);
  31928. AssertIntNE(RAND_status(), 5432);
  31929. AssertIntEQ(*was_cleanup_called, 0);
  31930. RAND_cleanup();
  31931. AssertIntEQ(*was_cleanup_called, 0);
  31932. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  31933. printf(resultFmt, passed);
  31934. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  31935. }
  31936. static void test_wolfSSL_RAND_bytes(void)
  31937. {
  31938. #if defined(OPENSSL_EXTRA)
  31939. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  31940. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  31941. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  31942. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  31943. int max_bufsize;
  31944. byte *my_buf;
  31945. printf(testingFmt, "test_wolfSSL_RAND_bytes()");
  31946. /* sanity check */
  31947. AssertIntEQ(RAND_bytes(NULL, 16), 0);
  31948. AssertIntEQ(RAND_bytes(NULL, 0), 0);
  31949. max_bufsize = size4;
  31950. my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  31951. DYNAMIC_TYPE_TMP_BUFFER);
  31952. AssertIntEQ(RAND_bytes(my_buf, 0), 1);
  31953. AssertIntEQ(RAND_bytes(my_buf, -1), 0);
  31954. AssertNotNull(my_buf);
  31955. XMEMSET(my_buf, 0, max_bufsize);
  31956. AssertIntEQ(RAND_bytes(my_buf, size1), 1);
  31957. AssertIntEQ(RAND_bytes(my_buf, size2), 1);
  31958. AssertIntEQ(RAND_bytes(my_buf, size3), 1);
  31959. AssertIntEQ(RAND_bytes(my_buf, size4), 1);
  31960. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  31961. printf(resultFmt, passed);
  31962. #endif
  31963. }
  31964. static void test_wolfSSL_BN_rand(void)
  31965. {
  31966. #if defined(OPENSSL_EXTRA)
  31967. BIGNUM* bn;
  31968. BIGNUM* range;
  31969. printf(testingFmt, "wolfSSL_BN_rand()");
  31970. /* Error conditions. */
  31971. /* NULL BN. */
  31972. AssertIntEQ(BN_rand(NULL, 0, 0, 0), SSL_FAILURE);
  31973. AssertNotNull(bn = BN_new());
  31974. /* Negative bits. */
  31975. AssertIntEQ(BN_rand(bn, -2, 0, 0), SSL_FAILURE);
  31976. /* 0 bits and top is not -1. */
  31977. AssertIntEQ(BN_rand(bn, 0, 1, 0), SSL_FAILURE);
  31978. /* 0 bits and bottom is not 0. */
  31979. AssertIntEQ(BN_rand(bn, 0, 0, 1), SSL_FAILURE);
  31980. /* 1 bit and top is 1. */
  31981. AssertIntEQ(BN_rand(bn, 1, 1, 0), SSL_FAILURE);
  31982. AssertIntEQ(BN_rand(bn, 0, -1, 0), SSL_SUCCESS);
  31983. AssertIntEQ(BN_num_bits(bn), 0);
  31984. AssertIntEQ(BN_rand(bn, 8, 0, 0), SSL_SUCCESS);
  31985. AssertIntEQ(BN_num_bits(bn), 8);
  31986. /* When top is 0, top bit should be 1. */
  31987. AssertIntEQ(BN_is_bit_set(bn, 7), SSL_SUCCESS);
  31988. AssertIntEQ(BN_rand(bn, 8, 1, 0), SSL_SUCCESS);
  31989. /* When top is 1, top 2 bits should be 1. */
  31990. AssertIntEQ(BN_is_bit_set(bn, 7), SSL_SUCCESS);
  31991. AssertIntEQ(BN_is_bit_set(bn, 6), SSL_SUCCESS);
  31992. AssertIntEQ(BN_rand(bn, 8, 0, 1), SSL_SUCCESS);
  31993. /* When bottom is 1, bottom bit should be 1. */
  31994. AssertIntEQ(BN_is_bit_set(bn, 0), SSL_SUCCESS);
  31995. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  31996. * requested number of bits up to the nearest multiple of 8. E.g. in this
  31997. * case, requesting a 13-bit random number would actually return a 16-bit
  31998. * random number. */
  31999. AssertIntEQ(BN_rand(bn, 13, 0, 0), SSL_SUCCESS);
  32000. AssertIntEQ(BN_num_bits(bn), 13);
  32001. AssertNotNull(range = BN_new());
  32002. AssertIntEQ(BN_rand(range, 64, 0, 0), SSL_SUCCESS);
  32003. AssertIntEQ(BN_rand_range(bn, range), SSL_SUCCESS);
  32004. BN_free(bn);
  32005. BN_free(range);
  32006. printf(resultFmt, passed);
  32007. #endif
  32008. }
  32009. static void test_wolfSSL_pseudo_rand(void)
  32010. {
  32011. #if defined(OPENSSL_EXTRA)
  32012. BIGNUM* bn;
  32013. unsigned char bin[8];
  32014. int i;
  32015. printf(testingFmt, "wolfSSL_pseudo_rand()");
  32016. /* BN_pseudo_rand returns 1 on success 0 on failure
  32017. * int BN_pseudo_rand(BIGNUM* bn, int bits, int top, int bottom) */
  32018. for (i = 0; i < 10; i++) {
  32019. AssertNotNull(bn = BN_new());
  32020. AssertIntEQ(BN_pseudo_rand(bn, 8, 0, 0), SSL_SUCCESS);
  32021. AssertIntGT(BN_bn2bin(bn, bin),0);
  32022. AssertIntEQ((bin[0] & 0x80), 0x80); /* top bit should be set */
  32023. BN_free(bn);
  32024. }
  32025. for (i = 0; i < 10; i++) {
  32026. AssertNotNull(bn = BN_new());
  32027. AssertIntEQ(BN_pseudo_rand(bn, 8, 1, 1), SSL_SUCCESS);
  32028. AssertIntGT(BN_bn2bin(bn, bin),0);
  32029. AssertIntEQ((bin[0] & 0xc1), 0xc1); /* top bit should be set */
  32030. BN_free(bn);
  32031. }
  32032. printf(resultFmt, passed);
  32033. #endif
  32034. }
  32035. static void test_wolfSSL_PKCS8_Compat(void)
  32036. {
  32037. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  32038. #ifndef NO_BIO
  32039. PKCS8_PRIV_KEY_INFO* pt;
  32040. BIO* bio;
  32041. XFILE f;
  32042. int bytes;
  32043. char pkcs8_buffer[512];
  32044. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  32045. EVP_PKEY *pkey = NULL;
  32046. #endif
  32047. printf(testingFmt, "wolfSSL_pkcs8()");
  32048. /* file from wolfssl/certs/ directory */
  32049. f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb");
  32050. AssertTrue(f != XBADFILE);
  32051. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)), 0);
  32052. XFCLOSE(f);
  32053. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  32054. AssertNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  32055. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  32056. AssertNotNull(pkey = EVP_PKCS82PKEY(pt));
  32057. AssertIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  32058. /* gets PKCS8 pointer to pkey */
  32059. AssertNotNull(EVP_PKEY2PKCS8(pkey));
  32060. EVP_PKEY_free(pkey);
  32061. #endif
  32062. BIO_free(bio);
  32063. PKCS8_PRIV_KEY_INFO_free(pt);
  32064. printf(resultFmt, passed);
  32065. #endif
  32066. #endif
  32067. }
  32068. static void test_wolfSSL_PKCS8_d2i(void)
  32069. {
  32070. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  32071. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  32072. * requires a 12 byte password in FIPS mode. This test ends up
  32073. * trying to use an 8 byte password. */
  32074. #ifndef NO_FILESYSTEM
  32075. unsigned char pkcs8_buffer[2048];
  32076. const unsigned char* p;
  32077. int bytes;
  32078. XFILE file;
  32079. WOLFSSL_EVP_PKEY* pkey = NULL;
  32080. #ifndef NO_BIO
  32081. BIO* bio;
  32082. #if defined(OPENSSL_ALL) && \
  32083. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  32084. defined(HAVE_ECC)) && \
  32085. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  32086. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  32087. #endif
  32088. #endif
  32089. #ifndef NO_RSA
  32090. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  32091. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  32092. #ifndef NO_DES3
  32093. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  32094. #endif
  32095. #endif /* NO_RSA */
  32096. #ifdef HAVE_ECC
  32097. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  32098. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  32099. #ifndef NO_DES3
  32100. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  32101. #endif
  32102. #endif /* HAVE_ECC */
  32103. #endif /* !NO_FILESYSTEM */
  32104. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  32105. #ifndef NO_RSA
  32106. #ifdef USE_CERT_BUFFERS_1024
  32107. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  32108. int rsaSz = sizeof_server_key_der_1024;
  32109. #else
  32110. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  32111. int rsaSz = sizeof_server_key_der_2048;
  32112. #endif
  32113. #endif
  32114. #ifdef HAVE_ECC
  32115. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  32116. int ecSz = sizeof_ecc_key_der_256;
  32117. #endif
  32118. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  32119. #ifndef NO_FILESYSTEM
  32120. (void)pkcs8_buffer;
  32121. (void)p;
  32122. (void)bytes;
  32123. (void)file;
  32124. #ifndef NO_BIO
  32125. (void)bio;
  32126. #endif
  32127. #endif
  32128. #ifdef OPENSSL_ALL
  32129. #ifndef NO_RSA
  32130. /* Try to auto-detect normal RSA private key */
  32131. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  32132. EVP_PKEY_free(pkey);
  32133. #endif
  32134. #ifdef HAVE_ECC
  32135. /* Try to auto-detect normal EC private key */
  32136. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  32137. EVP_PKEY_free(pkey);
  32138. #endif
  32139. #endif /* OPENSSL_ALL */
  32140. #ifndef NO_FILESYSTEM
  32141. #ifndef NO_RSA
  32142. /* Get DER encoded RSA PKCS#8 data. */
  32143. file = XFOPEN(rsaDerPkcs8File, "rb");
  32144. AssertTrue(file != XBADFILE);
  32145. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  32146. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  32147. file)), 0);
  32148. XFCLOSE(file);
  32149. p = pkcs8_buffer;
  32150. #ifdef OPENSSL_ALL
  32151. /* Try to decode - auto-detect key type. */
  32152. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  32153. #else
  32154. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  32155. #endif
  32156. /* Get PEM encoded RSA PKCS#8 data. */
  32157. file = XFOPEN(rsaPemPkcs8File, "rb");
  32158. AssertTrue(file != XBADFILE);
  32159. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  32160. file)), 0);
  32161. XFCLOSE(file);
  32162. #if defined(OPENSSL_ALL) && \
  32163. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  32164. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  32165. /* Write PKCS#8 PEM to BIO. */
  32166. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  32167. NULL), bytes);
  32168. /* Compare file and written data */
  32169. AssertIntEQ(BIO_get_mem_data(bio, &p), bytes);
  32170. AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  32171. BIO_free(bio);
  32172. #ifndef NO_DES3
  32173. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  32174. /* Write Encrypted PKCS#8 PEM to BIO. */
  32175. bytes = 1834;
  32176. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  32177. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  32178. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  32179. (void*)"yassl123"));
  32180. EVP_PKEY_free(evpPkey);
  32181. BIO_free(bio);
  32182. #endif /* !NO_DES3 */
  32183. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  32184. EVP_PKEY_free(pkey);
  32185. /* PKCS#8 encrypted RSA key */
  32186. #ifndef NO_DES3
  32187. file = XFOPEN(rsaDerPkcs8EncFile, "rb");
  32188. AssertTrue(file != XBADFILE);
  32189. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  32190. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  32191. file)), 0);
  32192. XFCLOSE(file);
  32193. #if defined(OPENSSL_ALL) && \
  32194. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  32195. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  32196. AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  32197. (void*)"yassl123"));
  32198. EVP_PKEY_free(pkey);
  32199. BIO_free(bio);
  32200. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  32201. #endif /* !NO_DES3 */
  32202. #endif /* NO_RSA */
  32203. #ifdef HAVE_ECC
  32204. /* PKCS#8 encode EC key */
  32205. file = XFOPEN(ecDerPkcs8File, "rb");
  32206. AssertTrue(file != XBADFILE);
  32207. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  32208. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  32209. file)), 0);
  32210. XFCLOSE(file);
  32211. p = pkcs8_buffer;
  32212. #ifdef OPENSSL_ALL
  32213. /* Try to decode - auto-detect key type. */
  32214. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  32215. #else
  32216. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  32217. #endif
  32218. /* Get PEM encoded RSA PKCS#8 data. */
  32219. file = XFOPEN(ecPemPkcs8File, "rb");
  32220. AssertTrue(file != XBADFILE);
  32221. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  32222. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  32223. file)), 0);
  32224. XFCLOSE(file);
  32225. #if defined(OPENSSL_ALL) && \
  32226. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  32227. defined(HAVE_AES_CBC)
  32228. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  32229. /* Write PKCS#8 PEM to BIO. */
  32230. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  32231. NULL), bytes);
  32232. /* Compare file and written data */
  32233. AssertIntEQ(BIO_get_mem_data(bio, &p), bytes);
  32234. AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  32235. BIO_free(bio);
  32236. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  32237. /* Write Encrypted PKCS#8 PEM to BIO. */
  32238. bytes = 379;
  32239. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  32240. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  32241. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  32242. (void*)"yassl123"));
  32243. EVP_PKEY_free(evpPkey);
  32244. BIO_free(bio);
  32245. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  32246. EVP_PKEY_free(pkey);
  32247. /* PKCS#8 encrypted EC key */
  32248. #ifndef NO_DES3
  32249. file = XFOPEN(ecDerPkcs8EncFile, "rb");
  32250. AssertTrue(file != XBADFILE);
  32251. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  32252. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  32253. file)), 0);
  32254. XFCLOSE(file);
  32255. #if defined(OPENSSL_ALL) && \
  32256. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  32257. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  32258. AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  32259. (void*)"yassl123"));
  32260. EVP_PKEY_free(pkey);
  32261. BIO_free(bio);
  32262. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  32263. #endif /* !NO_DES3 */
  32264. #endif /* HAVE_ECC */
  32265. #endif /* !NO_FILESYSTEM */
  32266. printf(resultFmt, passed);
  32267. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  32268. }
  32269. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  32270. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  32271. #define LOGGING_THREADS 5
  32272. #define ERROR_COUNT 10
  32273. static volatile int loggingThreadsReady;
  32274. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  32275. {
  32276. const char* file;
  32277. int line;
  32278. int err;
  32279. int errorCount = 0;
  32280. int i;
  32281. (void)args;
  32282. while (!loggingThreadsReady);
  32283. for (i = 0; i < ERROR_COUNT; i++)
  32284. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  32285. while ((err = ERR_get_error_line(&file, &line))) {
  32286. AssertIntEQ(err, 990 + errorCount);
  32287. errorCount++;
  32288. }
  32289. AssertIntEQ(errorCount, ERROR_COUNT);
  32290. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  32291. errorCount = 0;
  32292. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  32293. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  32294. while ((err = ERR_get_error_line(&file, &line))) {
  32295. AssertIntEQ(err, 990 + errorCount);
  32296. errorCount++;
  32297. }
  32298. /* test that the 3 errors over the max were dropped */
  32299. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  32300. return 0;
  32301. }
  32302. #endif
  32303. static void test_error_queue_per_thread(void)
  32304. {
  32305. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  32306. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  32307. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  32308. int i;
  32309. printf(testingFmt, "error_queue_per_thread()");
  32310. ERR_clear_error(); /* clear out any error nodes */
  32311. loggingThreadsReady = 0;
  32312. for (i = 0; i < LOGGING_THREADS; i++)
  32313. start_thread(test_logging, NULL, &loggingThreads[i]);
  32314. loggingThreadsReady = 1;
  32315. for (i = 0; i < LOGGING_THREADS; i++)
  32316. join_thread(loggingThreads[i]);
  32317. printf(resultFmt, passed);
  32318. #endif
  32319. }
  32320. static void test_wolfSSL_ERR_put_error(void)
  32321. {
  32322. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  32323. defined(DEBUG_WOLFSSL)
  32324. const char* file;
  32325. int line;
  32326. printf(testingFmt, "wolfSSL_ERR_put_error()");
  32327. ERR_clear_error(); /* clear out any error nodes */
  32328. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  32329. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  32330. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  32331. AssertIntEQ(ERR_get_error_line(&file, &line), 1);
  32332. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  32333. AssertIntEQ(ERR_get_error_line(&file, &line), 2);
  32334. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  32335. AssertIntEQ(ERR_get_error_line(&file, &line), 3);
  32336. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  32337. AssertIntEQ(ERR_get_error_line(&file, &line), 4);
  32338. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  32339. AssertIntEQ(ERR_get_error_line(&file, &line), 5);
  32340. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  32341. AssertIntEQ(ERR_get_error_line(&file, &line), 6);
  32342. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  32343. AssertIntEQ(ERR_get_error_line(&file, &line), 7);
  32344. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  32345. AssertIntEQ(ERR_get_error_line(&file, &line), 8);
  32346. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  32347. AssertIntEQ(ERR_get_error_line(&file, &line), 9);
  32348. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  32349. AssertIntEQ(ERR_get_error_line(&file, &line), 10);
  32350. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  32351. AssertIntEQ(ERR_get_error_line(&file, &line), 11);
  32352. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  32353. AssertIntEQ(ERR_get_error_line(&file, &line), 12);
  32354. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  32355. AssertIntEQ(ERR_get_error_line(&file, &line), 13);
  32356. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  32357. AssertIntEQ(ERR_get_error_line(&file, &line), 14);
  32358. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  32359. AssertIntEQ(ERR_get_error_line(&file, &line), 15);
  32360. #ifdef WOLFSSL_PYTHON
  32361. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  32362. "this file", 100);
  32363. AssertIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  32364. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  32365. AssertIntEQ(line, 100);
  32366. AssertIntEQ(wolfSSL_ERR_peek_error(),
  32367. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  32368. AssertIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  32369. #endif
  32370. /* try reading past end of error queue */
  32371. file = NULL;
  32372. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  32373. AssertNull(file);
  32374. AssertIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  32375. PEMerr(4,4);
  32376. AssertIntEQ(ERR_get_error(), 4);
  32377. /* Empty and free up all error nodes */
  32378. ERR_clear_error();
  32379. /* Verify all nodes are cleared */
  32380. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  32381. ERR_clear_error();
  32382. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  32383. printf(resultFmt, passed);
  32384. #endif
  32385. }
  32386. #ifndef NO_BIO
  32387. static void test_wolfSSL_ERR_print_errors(void)
  32388. {
  32389. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  32390. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  32391. BIO* bio;
  32392. char buf[1024];
  32393. printf(testingFmt, "wolfSSL_ERR_print_errors()");
  32394. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  32395. ERR_clear_error(); /* clear out any error nodes */
  32396. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  32397. /* Choosing -299 as an unused errno between MIN_CODE_E < x < WC_LAST_E. */
  32398. ERR_put_error(0,SYS_F_BIND, -299, "asn.c", 100);
  32399. ERR_print_errors(bio);
  32400. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  32401. AssertIntEQ(XSTRNCMP("error:173:wolfSSL library:Bad function argument:ssl.c:0",
  32402. buf, 55), 0);
  32403. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  32404. AssertIntEQ(XSTRNCMP("error:299:wolfSSL library:unknown error number:asn.c:100",
  32405. buf, 56), 0);
  32406. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  32407. AssertIntEQ(buf[0], '\0');
  32408. AssertIntEQ(ERR_get_error_line(NULL, NULL), 0);
  32409. BIO_free(bio);
  32410. printf(resultFmt, passed);
  32411. #endif
  32412. }
  32413. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  32414. defined(DEBUG_WOLFSSL)
  32415. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  32416. {
  32417. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  32418. return 0;
  32419. }
  32420. #endif
  32421. static void test_wolfSSL_ERR_print_errors_cb(void)
  32422. {
  32423. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  32424. defined(DEBUG_WOLFSSL)
  32425. BIO* bio;
  32426. char buf[1024];
  32427. printf(testingFmt, "wolfSSL_ERR_print_errors_cb()");
  32428. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  32429. ERR_clear_error(); /* clear out any error nodes */
  32430. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  32431. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  32432. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  32433. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  32434. AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  32435. buf, 53), 0);
  32436. AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 275 line:100 file:asn.c",
  32437. buf + 53, 55), 0);
  32438. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  32439. BIO_free(bio);
  32440. printf(resultFmt, passed);
  32441. #endif
  32442. }
  32443. /*
  32444. * Testing WOLFSSL_ERROR_MSG
  32445. */
  32446. static int test_WOLFSSL_ERROR_MSG (void)
  32447. {
  32448. int ret = 0;
  32449. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  32450. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  32451. const char* msg = TEST_STRING;
  32452. printf(testingFmt, "WOLFSSL_ERROR_MSG()");
  32453. WOLFSSL_ERROR_MSG(msg);
  32454. printf(resultFmt, ret == 0 ? passed : failed);
  32455. #endif
  32456. return ret;
  32457. }/*End test_WOLFSSL_ERROR_MSG*/
  32458. /*
  32459. * Testing wc_ERR_remove_state
  32460. */
  32461. static int test_wc_ERR_remove_state (void)
  32462. {
  32463. int ret = 0;
  32464. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  32465. printf(testingFmt, "wc_ERR_remove_state()");
  32466. wc_ERR_remove_state();
  32467. printf(resultFmt, ret == 0 ? passed : failed);
  32468. #endif
  32469. return ret;
  32470. }/*End test_wc_ERR_remove_state*/
  32471. /*
  32472. * Testing wc_ERR_print_errors_fp
  32473. */
  32474. static int test_wc_ERR_print_errors_fp (void)
  32475. {
  32476. int ret = 0;
  32477. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  32478. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  32479. long sz;
  32480. XFILE fp;
  32481. printf(testingFmt, "wc_ERR_print_errors_fp()");
  32482. WOLFSSL_ERROR(BAD_FUNC_ARG);
  32483. fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar");
  32484. wc_ERR_print_errors_fp(fp);
  32485. #if defined(DEBUG_WOLFSSL)
  32486. AssertTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  32487. sz = XFTELL(fp);
  32488. #ifdef NO_ERROR_QUEUE
  32489. /* File should be empty when NO_ERROR_QUEUE is defined */
  32490. if (sz != 0) {
  32491. ret = BAD_FUNC_ARG;
  32492. }
  32493. #else
  32494. if (sz == 0) {
  32495. ret = BAD_FUNC_ARG;
  32496. }
  32497. #endif
  32498. #endif
  32499. printf(resultFmt, ret == 0 ? passed : failed);
  32500. XFCLOSE(fp);
  32501. (void)sz;
  32502. #endif
  32503. return ret;
  32504. }/*End test_wc_ERR_print_errors_fp*/
  32505. #ifdef DEBUG_WOLFSSL
  32506. static void Logging_cb(const int logLevel, const char *const logMessage)
  32507. {
  32508. (void)logLevel;
  32509. (void)logMessage;
  32510. }
  32511. #endif
  32512. /*
  32513. * Testing wolfSSL_GetLoggingCb
  32514. */
  32515. static int test_wolfSSL_GetLoggingCb (void)
  32516. {
  32517. int ret = 0;
  32518. printf(testingFmt, "wolfSSL_GetLoggingCb()");
  32519. #ifdef DEBUG_WOLFSSL
  32520. /* Testing without wolfSSL_SetLoggingCb() */
  32521. if (ret == 0) {
  32522. if (wolfSSL_GetLoggingCb() == NULL) { /* Should be true */
  32523. ret = 0;
  32524. }
  32525. if (wolfSSL_GetLoggingCb() != NULL) { /* Should not be true */
  32526. ret = -1;
  32527. }
  32528. }
  32529. /* Testing with wolfSSL_SetLoggingCb() */
  32530. if (ret == 0) {
  32531. ret = wolfSSL_SetLoggingCb(Logging_cb);
  32532. if (ret == 0){
  32533. if (wolfSSL_GetLoggingCb() == NULL) { /* Should not be true */
  32534. ret = -1;
  32535. }
  32536. if (ret == 0) {
  32537. if (wolfSSL_GetLoggingCb() == Logging_cb) { /* Should be true */
  32538. ret = 0;
  32539. }
  32540. }
  32541. /* reset logging callback */
  32542. wolfSSL_SetLoggingCb(NULL);
  32543. }
  32544. }
  32545. #endif
  32546. if (ret == 0) {
  32547. if (wolfSSL_GetLoggingCb() != NULL) {
  32548. ret = -1;
  32549. }
  32550. }
  32551. printf(resultFmt, ret == 0 ? passed : failed);
  32552. return ret;
  32553. }/*End test_wolfSSL_GetLoggingCb*/
  32554. #endif /* !NO_BIO */
  32555. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  32556. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  32557. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  32558. static void test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  32559. {
  32560. static const unsigned char key[] = "simple test key";
  32561. HMAC_CTX* hmac;
  32562. ENGINE* e = NULL;
  32563. unsigned char hash[WC_MAX_DIGEST_SIZE];
  32564. unsigned int len;
  32565. AssertNotNull(hmac = HMAC_CTX_new());
  32566. HMAC_CTX_init(hmac);
  32567. AssertIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e),
  32568. SSL_SUCCESS);
  32569. /* re-using test key as data to hash */
  32570. AssertIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), SSL_SUCCESS);
  32571. AssertIntEQ(HMAC_Update(hmac, NULL, 0), SSL_SUCCESS);
  32572. AssertIntEQ(HMAC_Final(hmac, hash, &len), SSL_SUCCESS);
  32573. AssertIntEQ(len, md_len);
  32574. AssertIntEQ(HMAC_size(hmac), md_len);
  32575. AssertStrEQ(HMAC_CTX_get_md(hmac), md);
  32576. HMAC_cleanup(hmac);
  32577. HMAC_CTX_free(hmac);
  32578. len = 0;
  32579. AssertNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  32580. AssertIntEQ(len, md_len);
  32581. }
  32582. #endif
  32583. static void test_wolfSSL_HMAC(void)
  32584. {
  32585. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  32586. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  32587. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  32588. printf(testingFmt, "wolfSSL_HMAC()");
  32589. #ifndef NO_SHA256
  32590. test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE);
  32591. #endif
  32592. #ifdef WOLFSSL_SHA224
  32593. test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE);
  32594. #endif
  32595. #ifdef WOLFSSL_SHA384
  32596. test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE);
  32597. #endif
  32598. #ifdef WOLFSSL_SHA512
  32599. test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE);
  32600. #endif
  32601. #ifdef WOLFSSL_SHA3
  32602. #ifndef WOLFSSL_NOSHA3_224
  32603. test_openssl_hmac(EVP_sha3_224(), (int)WC_SHA3_224_DIGEST_SIZE);
  32604. #endif
  32605. #ifndef WOLFSSL_NOSHA3_256
  32606. test_openssl_hmac(EVP_sha3_256(), (int)WC_SHA3_256_DIGEST_SIZE);
  32607. #endif
  32608. #ifndef WOLFSSL_NOSHA3_384
  32609. test_openssl_hmac(EVP_sha3_384(), (int)WC_SHA3_384_DIGEST_SIZE);
  32610. #endif
  32611. #ifndef WOLFSSL_NOSHA3_512
  32612. test_openssl_hmac(EVP_sha3_512(), (int)WC_SHA3_512_DIGEST_SIZE);
  32613. #endif
  32614. #endif
  32615. printf(resultFmt, passed);
  32616. #endif
  32617. }
  32618. static void test_wolfSSL_CMAC(void)
  32619. {
  32620. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  32621. defined(WOLFSSL_AES_DIRECT)
  32622. int i;
  32623. byte key[AES_128_KEY_SIZE];
  32624. CMAC_CTX* cmacCtx = NULL;
  32625. byte out[AES_BLOCK_SIZE];
  32626. size_t outLen = AES_BLOCK_SIZE;
  32627. printf(testingFmt, "test_wolfSSL_CMAC()");
  32628. for (i=0; i < AES_128_KEY_SIZE; ++i) {
  32629. key[i] = i;
  32630. }
  32631. AssertNotNull(cmacCtx = CMAC_CTX_new());
  32632. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  32633. AssertNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  32634. AssertIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  32635. NULL), SSL_SUCCESS);
  32636. /* re-using test key as data to hash */
  32637. AssertIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), SSL_SUCCESS);
  32638. AssertIntEQ(CMAC_Update(cmacCtx, NULL, 0), SSL_SUCCESS);
  32639. AssertIntEQ(CMAC_Final(cmacCtx, out, &outLen), SSL_SUCCESS);
  32640. AssertIntEQ(outLen, AES_BLOCK_SIZE);
  32641. CMAC_CTX_free(cmacCtx);
  32642. printf(resultFmt, passed);
  32643. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  32644. }
  32645. static void test_wolfSSL_OBJ(void)
  32646. {
  32647. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  32648. * mode
  32649. */
  32650. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  32651. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  32652. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  32653. ASN1_OBJECT *obj = NULL;
  32654. ASN1_OBJECT *obj2 = NULL;
  32655. char buf[50];
  32656. XFILE fp;
  32657. X509 *x509 = NULL;
  32658. X509_NAME *x509Name;
  32659. X509_NAME_ENTRY *x509NameEntry;
  32660. ASN1_OBJECT *asn1Name = NULL;
  32661. int numNames;
  32662. BIO *bio = NULL;
  32663. int nid;
  32664. int i, j;
  32665. const char *f[] = {
  32666. #ifndef NO_RSA
  32667. "./certs/ca-cert.der",
  32668. #endif
  32669. #ifdef HAVE_ECC
  32670. "./certs/ca-ecc-cert.der",
  32671. "./certs/ca-ecc384-cert.der",
  32672. #endif
  32673. NULL};
  32674. ASN1_OBJECT *field_name_obj = NULL;
  32675. int lastpos = -1;
  32676. int tmp = -1;
  32677. ASN1_STRING *asn1 = NULL;
  32678. unsigned char *buf_dyn = NULL;
  32679. printf(testingFmt, "wolfSSL_OBJ()");
  32680. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  32681. AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
  32682. AssertIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  32683. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  32684. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  32685. ASN1_OBJECT_free(obj);
  32686. AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
  32687. AssertIntEQ(OBJ_obj2nid(obj), NID_sha256);
  32688. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  32689. #ifdef WOLFSSL_CERT_EXT
  32690. AssertIntEQ(OBJ_txt2nid(buf), NID_sha256);
  32691. #endif
  32692. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  32693. AssertNotNull(obj2 = OBJ_dup(obj));
  32694. AssertIntEQ(OBJ_cmp(obj, obj2), 0);
  32695. ASN1_OBJECT_free(obj);
  32696. ASN1_OBJECT_free(obj2);
  32697. for (i = 0; f[i] != NULL; i++)
  32698. {
  32699. AssertTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  32700. AssertNotNull(x509 = d2i_X509_fp(fp, NULL));
  32701. XFCLOSE(fp);
  32702. AssertNotNull(x509Name = X509_get_issuer_name(x509));
  32703. AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  32704. /* Get the Common Name by using OBJ_txt2obj */
  32705. AssertNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  32706. do
  32707. {
  32708. lastpos = tmp;
  32709. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  32710. } while (tmp > -1);
  32711. AssertIntNE(lastpos, -1);
  32712. ASN1_OBJECT_free(field_name_obj);
  32713. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  32714. AssertNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  32715. AssertIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  32716. /*
  32717. * All Common Names should be www.wolfssl.com
  32718. * This makes testing easier as we can test for the expected value.
  32719. */
  32720. AssertStrEQ((char*)buf_dyn, "www.wolfssl.com");
  32721. OPENSSL_free(buf_dyn);
  32722. bio = BIO_new(BIO_s_mem());
  32723. AssertTrue(bio != NULL);
  32724. for (j = 0; j < numNames; j++)
  32725. {
  32726. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  32727. AssertNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  32728. AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  32729. }
  32730. BIO_free(bio);
  32731. X509_free(x509);
  32732. }
  32733. #ifdef HAVE_PKCS12
  32734. {
  32735. PKCS12 *p12;
  32736. int boolRet;
  32737. EVP_PKEY *pkey = NULL;
  32738. const char *p12_f[] = {
  32739. #if !defined(NO_DES3) && !defined(NO_RSA)
  32740. "./certs/test-servercert.p12",
  32741. #endif
  32742. NULL};
  32743. for (i = 0; p12_f[i] != NULL; i++)
  32744. {
  32745. AssertTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  32746. AssertNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  32747. XFCLOSE(fp);
  32748. AssertTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  32749. &pkey, &x509, NULL)) > 0);
  32750. wc_PKCS12_free(p12);
  32751. EVP_PKEY_free(pkey);
  32752. x509Name = X509_get_issuer_name(x509);
  32753. AssertNotNull(x509Name);
  32754. AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  32755. AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  32756. for (j = 0; j < numNames; j++)
  32757. {
  32758. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  32759. AssertNotNull(asn1Name =
  32760. X509_NAME_ENTRY_get_object(x509NameEntry));
  32761. AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  32762. }
  32763. BIO_free(bio);
  32764. X509_free(x509);
  32765. }
  32766. }
  32767. #endif /* HAVE_PKCS12 */
  32768. printf(resultFmt, passed);
  32769. #endif
  32770. }
  32771. static void test_wolfSSL_i2a_ASN1_OBJECT(void)
  32772. {
  32773. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  32774. ASN1_OBJECT *obj = NULL;
  32775. BIO *bio = NULL;
  32776. AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
  32777. AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  32778. AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  32779. AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  32780. AssertIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  32781. BIO_free(bio);
  32782. ASN1_OBJECT_free(obj);
  32783. #endif
  32784. }
  32785. static void test_wolfSSL_OBJ_cmp(void)
  32786. {
  32787. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  32788. ASN1_OBJECT *obj = NULL;
  32789. ASN1_OBJECT *obj2 = NULL;
  32790. printf(testingFmt, "wolfSSL_OBJ_cmp()");
  32791. AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
  32792. AssertNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  32793. AssertIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  32794. AssertIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  32795. AssertIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  32796. AssertIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  32797. AssertIntEQ(OBJ_cmp(obj, obj), 0);
  32798. AssertIntEQ(OBJ_cmp(obj2, obj2), 0);
  32799. ASN1_OBJECT_free(obj);
  32800. ASN1_OBJECT_free(obj2);
  32801. printf(resultFmt, passed);
  32802. #endif
  32803. }
  32804. static void test_wolfSSL_OBJ_txt2nid(void)
  32805. {
  32806. #if !defined(NO_WOLFSSL_STUB) && defined(WOLFSSL_APACHE_HTTPD)
  32807. int i;
  32808. static const struct {
  32809. const char* sn;
  32810. const char* ln;
  32811. const char* oid;
  32812. int nid;
  32813. } testVals[] = {
  32814. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  32815. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  32816. NID_id_on_dnsSRV },
  32817. { "msUPN", "Microsoft User Principal Name",
  32818. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  32819. { NULL, NULL, NULL, NID_undef }
  32820. };
  32821. printf(testingFmt, "wolfSSL_OBJ_txt2nid()");
  32822. /* Invalid cases */
  32823. AssertIntEQ(OBJ_txt2nid(NULL), NID_undef);
  32824. AssertIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  32825. /* Valid cases */
  32826. for (i = 0; testVals[i].sn != NULL; i++) {
  32827. AssertIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  32828. AssertIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  32829. AssertIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  32830. }
  32831. printf(resultFmt, passed);
  32832. #endif
  32833. }
  32834. static void test_wolfSSL_OBJ_txt2obj(void)
  32835. {
  32836. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  32837. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  32838. int i;
  32839. char buf[50];
  32840. ASN1_OBJECT* obj;
  32841. static const struct {
  32842. const char* oidStr;
  32843. const char* sn;
  32844. const char* ln;
  32845. } objs_list[] = {
  32846. #if defined(WOLFSSL_APACHE_HTTPD)
  32847. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  32848. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  32849. #endif
  32850. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  32851. { NULL, NULL, NULL }
  32852. };
  32853. static const struct {
  32854. const char* numeric;
  32855. const char* name;
  32856. } objs_named[] = {
  32857. /* In dictionary but not in normal list. */
  32858. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  32859. /* Made up OID. */
  32860. { "1.3.5.7", "1.3.5.7" },
  32861. { NULL, NULL }
  32862. };
  32863. printf(testingFmt, "wolfSSL_OBJ_txt2obj()");
  32864. AssertNull(obj = OBJ_txt2obj("Bad name", 0));
  32865. AssertNull(obj = OBJ_txt2obj(NULL, 0));
  32866. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  32867. /* Test numerical value of oid (oidStr) */
  32868. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  32869. /* Convert object back to text to confirm oid is correct */
  32870. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  32871. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  32872. ASN1_OBJECT_free(obj);
  32873. XMEMSET(buf, 0, sizeof(buf));
  32874. /* Test short name (sn) */
  32875. AssertNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  32876. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  32877. /* Convert object back to text to confirm oid is correct */
  32878. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  32879. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  32880. ASN1_OBJECT_free(obj);
  32881. XMEMSET(buf, 0, sizeof(buf));
  32882. /* Test long name (ln) - should fail when no_name = 1 */
  32883. AssertNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  32884. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  32885. /* Convert object back to text to confirm oid is correct */
  32886. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  32887. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  32888. ASN1_OBJECT_free(obj);
  32889. XMEMSET(buf, 0, sizeof(buf));
  32890. }
  32891. for (i = 0; objs_named[i].numeric != NULL; i++) {
  32892. AssertNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  32893. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  32894. AssertIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  32895. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  32896. AssertIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  32897. ASN1_OBJECT_free(obj);
  32898. }
  32899. printf(resultFmt, passed);
  32900. #endif
  32901. }
  32902. static void test_wolfSSL_i2t_ASN1_OBJECT(void)
  32903. {
  32904. #if defined(OPENSSL_EXTRA) && \
  32905. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  32906. char buf[50] = {0};
  32907. ASN1_OBJECT* obj;
  32908. const char* oid = "2.5.29.19";
  32909. const char* ln = "X509v3 Basic Constraints";
  32910. printf(testingFmt, "test_wolfSSL_i2t_ASN1_OBJECT()");
  32911. obj = NULL;
  32912. AssertIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), WOLFSSL_FAILURE);
  32913. AssertIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), WOLFSSL_FAILURE);
  32914. AssertIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), WOLFSSL_FAILURE);
  32915. AssertNotNull(obj = OBJ_txt2obj(oid, 0));
  32916. XMEMSET(buf, 0, sizeof(buf));
  32917. AssertIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  32918. AssertIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  32919. ASN1_OBJECT_free(obj);
  32920. printf(resultFmt, passed);
  32921. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  32922. }
  32923. static void test_wolfSSL_PEM_write_bio_X509(void)
  32924. {
  32925. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_AKID_NAME) && \
  32926. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && \
  32927. !defined(NO_BIO)
  32928. /* This test contains the hard coded expected
  32929. * lengths. Update if necessary */
  32930. BIO* input;
  32931. BIO* output;
  32932. X509* x509a = NULL;
  32933. X509* x509b = NULL;
  32934. ASN1_TIME* notBeforeA = NULL;
  32935. ASN1_TIME* notAfterA = NULL;
  32936. ASN1_TIME* notBeforeB = NULL;
  32937. ASN1_TIME* notAfterB = NULL;
  32938. int expectedLen;
  32939. printf(testingFmt, "wolfSSL_PEM_write_bio_X509()");
  32940. AssertNotNull(input = BIO_new_file(
  32941. "certs/test/cert-ext-multiple.pem", "rb"));
  32942. AssertIntEQ(wolfSSL_BIO_get_len(input), 2000);
  32943. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  32944. AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  32945. AssertNotNull(notBeforeA = X509_get_notBefore(x509a));
  32946. AssertNotNull(notAfterA = X509_get_notAfter(x509a));
  32947. /* write X509 back to PEM BIO */
  32948. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  32949. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  32950. /* compare length against expected */
  32951. expectedLen = 2000;
  32952. AssertIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  32953. /* read exported X509 PEM back into struct, sanity check on export,
  32954. * make sure notBefore/notAfter are the same. */
  32955. AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  32956. AssertNotNull(notBeforeB = X509_get_notBefore(x509b));
  32957. AssertNotNull(notAfterB = X509_get_notAfter(x509b));
  32958. AssertIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  32959. AssertIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  32960. X509_free(x509b);
  32961. /* Reset output buffer */
  32962. BIO_free(output);
  32963. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  32964. /* Test forcing the AKID to be generated just from KeyIdentifier */
  32965. if (x509a->authKeyIdSrc != NULL) {
  32966. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  32967. x509a->authKeyId = x509a->authKeyIdSrc;
  32968. x509a->authKeyIdSrc = NULL;
  32969. x509a->authKeyIdSrcSz = 0;
  32970. }
  32971. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  32972. /* Check that we generate a smaller output since the AKID will
  32973. * only contain the KeyIdentifier without any additional
  32974. * information */
  32975. /* Here we copy the validity struct from the original */
  32976. expectedLen = 1688;
  32977. AssertIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  32978. /* Reset buffers and x509 */
  32979. BIO_free(input);
  32980. BIO_free(output);
  32981. X509_free(x509a);
  32982. /* test CA and basicConstSet values are encoded when
  32983. * the cert is a CA */
  32984. AssertNotNull(input = BIO_new_file(
  32985. "certs/server-cert.pem", "rb"));
  32986. /* read PEM into X509 struct */
  32987. AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  32988. /* write X509 back to PEM BIO */
  32989. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  32990. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  32991. /* read exported X509 PEM back into struct, ensure isCa and
  32992. * basicConstSet values are maintained */
  32993. AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  32994. AssertIntEQ(x509b->isCa, 1);
  32995. AssertIntEQ(x509b->basicConstSet, 1);
  32996. X509_free(x509a);
  32997. X509_free(x509b);
  32998. BIO_free(input);
  32999. BIO_free(output);
  33000. /* test CA and basicConstSet values are encoded when
  33001. * the cert is not CA */
  33002. AssertNotNull(input = BIO_new_file(
  33003. "certs/client-uri-cert.pem", "rb"));
  33004. /* read PEM into X509 struct */
  33005. AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  33006. /* write X509 back to PEM BIO */
  33007. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  33008. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  33009. /* read exported X509 PEM back into struct, ensure isCa and
  33010. * basicConstSet values are maintained */
  33011. AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  33012. AssertIntEQ(x509b->isCa, 0);
  33013. AssertIntEQ(x509b->basicConstSet, 1);
  33014. X509_free(x509a);
  33015. X509_free(x509b);
  33016. BIO_free(input);
  33017. BIO_free(output);
  33018. printf(resultFmt, passed);
  33019. #endif
  33020. }
  33021. static void test_wolfSSL_X509_NAME_ENTRY(void)
  33022. {
  33023. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  33024. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  33025. X509* x509;
  33026. #ifndef NO_BIO
  33027. BIO* bio;
  33028. #endif
  33029. X509_NAME* nm;
  33030. X509_NAME_ENTRY* entry;
  33031. unsigned char cn[] = "another name to add";
  33032. #ifdef OPENSSL_ALL
  33033. int i, names_len;
  33034. #endif
  33035. printf(testingFmt, "wolfSSL_X509_NAME_ENTRY()");
  33036. AssertNotNull(x509 =
  33037. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  33038. #ifndef NO_BIO
  33039. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33040. AssertIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  33041. #endif
  33042. #ifdef WOLFSSL_CERT_REQ
  33043. {
  33044. X509_REQ* req;
  33045. #ifndef NO_BIO
  33046. BIO* bReq;
  33047. #endif
  33048. AssertNotNull(req =
  33049. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  33050. #ifndef NO_BIO
  33051. AssertNotNull(bReq = BIO_new(BIO_s_mem()));
  33052. AssertIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  33053. BIO_free(bReq);
  33054. #endif
  33055. X509_free(req);
  33056. }
  33057. #endif
  33058. AssertNotNull(nm = X509_get_subject_name(x509));
  33059. /* Test add entry */
  33060. AssertNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  33061. 0x0c, cn, (int)sizeof(cn)));
  33062. AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  33063. #ifdef WOLFSSL_CERT_EXT
  33064. AssertIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  33065. (byte*)"support@wolfssl.com", 19, -1,
  33066. 1), WOLFSSL_SUCCESS);
  33067. #endif
  33068. X509_NAME_ENTRY_free(entry);
  33069. #ifdef WOLFSSL_CERT_REQ
  33070. {
  33071. unsigned char srv_pkcs9p[] = "Server";
  33072. char* subject;
  33073. AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  33074. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  33075. subject = X509_NAME_oneline(nm, 0, 0);
  33076. #ifdef DEBUG_WOLFSSL
  33077. printf("\n\t%s\n", subject);
  33078. #endif
  33079. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  33080. }
  33081. #endif
  33082. /* Test add entry by text */
  33083. AssertNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  33084. 0x0c, cn, (int)sizeof(cn)));
  33085. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  33086. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  33087. AssertNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  33088. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  33089. #endif
  33090. AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  33091. X509_NAME_ENTRY_free(entry);
  33092. /* Test add entry by NID */
  33093. AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  33094. cn, -1, -1, 0), SSL_SUCCESS);
  33095. #ifdef OPENSSL_ALL
  33096. /* stack of name entry */
  33097. AssertIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  33098. for (i=0; i<names_len; i++) {
  33099. AssertNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  33100. }
  33101. #endif
  33102. #ifndef NO_BIO
  33103. BIO_free(bio);
  33104. #endif
  33105. X509_free(x509); /* free's nm */
  33106. printf(resultFmt, passed);
  33107. #endif
  33108. }
  33109. static void test_wolfSSL_X509_set_name(void)
  33110. {
  33111. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33112. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  33113. X509* x509;
  33114. X509_NAME* name;
  33115. printf(testingFmt, "wolfSSL_X509_set_name()");
  33116. AssertNotNull(name = X509_NAME_new());
  33117. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  33118. (byte*)"wolfssl.com", 11, 0, 1),
  33119. WOLFSSL_SUCCESS);
  33120. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  33121. (byte*)"support@wolfssl.com", 19, -1,
  33122. 1), WOLFSSL_SUCCESS);
  33123. AssertNotNull(x509 = X509_new());
  33124. AssertIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  33125. AssertIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  33126. AssertIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  33127. AssertIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  33128. AssertIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  33129. AssertIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  33130. AssertIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  33131. AssertIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  33132. X509_free(x509);
  33133. X509_NAME_free(name);
  33134. printf(resultFmt, passed);
  33135. #endif /* OPENSSL_ALL && !NO_CERTS */
  33136. }
  33137. static void test_wolfSSL_X509_set_notAfter(void)
  33138. {
  33139. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  33140. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  33141. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  33142. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  33143. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  33144. /* Generalized time will overflow time_t if not long */
  33145. X509* x;
  33146. BIO* bio;
  33147. ASN1_TIME *asn_time, *time_check;
  33148. const int year = 365*24*60*60;
  33149. const int day = 24*60*60;
  33150. const int hour = 60*60;
  33151. const int mini = 60;
  33152. int offset_day;
  33153. unsigned char buf[25];
  33154. time_t t;
  33155. printf(testingFmt, "wolfSSL_X509_set_notAfter()");
  33156. /*
  33157. * Setup asn_time. APACHE HTTPD uses time(NULL)
  33158. */
  33159. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  33160. offset_day = 7;
  33161. /*
  33162. * Free these.
  33163. */
  33164. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  33165. AssertNotNull(asn_time);
  33166. AssertNotNull(x = X509_new());
  33167. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33168. /*
  33169. * Tests
  33170. */
  33171. AssertTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  33172. /* time_check is simply (ANS1_TIME*)x->notAfter */
  33173. AssertNotNull(time_check = X509_get_notAfter(x));
  33174. /* ANS1_TIME_check validates by checking if argument can be parsed */
  33175. AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  33176. /* Convert to human readable format and compare to intended date */
  33177. AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
  33178. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  33179. AssertIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  33180. /*
  33181. * Cleanup
  33182. */
  33183. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  33184. X509_free(x);
  33185. BIO_free(bio);
  33186. printf(resultFmt, passed);
  33187. #endif
  33188. }
  33189. static void test_wolfSSL_X509_set_notBefore(void)
  33190. {
  33191. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  33192. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  33193. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  33194. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  33195. X509* x;
  33196. BIO* bio;
  33197. ASN1_TIME *asn_time, *time_check;
  33198. const int year = 365*24*60*60;
  33199. const int day = 24*60*60;
  33200. const int hour = 60*60;
  33201. const int mini = 60;
  33202. int offset_day;
  33203. unsigned char buf[25];
  33204. time_t t;
  33205. printf(testingFmt, "wolfSSL_X509_set_notBefore()");
  33206. /*
  33207. * Setup asn_time. APACHE HTTPD uses time(NULL)
  33208. */
  33209. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  33210. offset_day = 7;
  33211. /*
  33212. * Free these.
  33213. */
  33214. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  33215. AssertNotNull(asn_time);
  33216. AssertNotNull(x = X509_new());
  33217. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33218. AssertIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  33219. /*
  33220. * Main Tests
  33221. */
  33222. AssertTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  33223. /* time_check == (ANS1_TIME*)x->notBefore */
  33224. AssertNotNull(time_check = X509_get_notBefore(x));
  33225. /* ANS1_TIME_check validates by checking if argument can be parsed */
  33226. AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  33227. /* Convert to human readable format and compare to intended date */
  33228. AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
  33229. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  33230. AssertIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  33231. /*
  33232. * Cleanup
  33233. */
  33234. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  33235. X509_free(x);
  33236. BIO_free(bio);
  33237. printf(resultFmt, passed);
  33238. #endif
  33239. }
  33240. static void test_wolfSSL_X509_set_version(void)
  33241. {
  33242. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  33243. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  33244. X509* x509;
  33245. long v = 2L;
  33246. long maxInt = INT_MAX;
  33247. AssertNotNull(x509 = X509_new());
  33248. /* These should pass. */
  33249. AssertTrue(wolfSSL_X509_set_version(x509, v));
  33250. AssertIntEQ(v, wolfSSL_X509_get_version(x509));
  33251. /* Fail Case: When v(long) is greater than x509->version(int). */
  33252. v = maxInt+1;
  33253. AssertFalse(wolfSSL_X509_set_version(x509, v));
  33254. /* Cleanup */
  33255. X509_free(x509);
  33256. printf(resultFmt, passed);
  33257. #endif
  33258. }
  33259. #ifndef NO_BIO
  33260. static void test_wolfSSL_BIO_gets(void)
  33261. {
  33262. #if defined(OPENSSL_EXTRA)
  33263. BIO* bio;
  33264. BIO* bio2;
  33265. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  33266. char emp[] = "";
  33267. char bio_buffer[20];
  33268. int bufferSz = 20;
  33269. printf(testingFmt, "wolfSSL_BIO_gets()");
  33270. /* try with bad args */
  33271. AssertNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  33272. /* try with real msg */
  33273. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  33274. XMEMSET(bio_buffer, 0, bufferSz);
  33275. AssertNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  33276. AssertNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  33277. AssertNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  33278. AssertFalse(bio2 != BIO_next(bio));
  33279. /* make buffer filled with no terminating characters */
  33280. XMEMSET(bio_buffer, 1, bufferSz);
  33281. /* BIO_gets reads a line of data */
  33282. AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  33283. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  33284. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  33285. AssertStrEQ(bio_buffer, "hello wolfSSL\n");
  33286. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  33287. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  33288. AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  33289. /* check not null terminated string */
  33290. BIO_free(bio);
  33291. msg[0] = 0x33;
  33292. msg[1] = 0x33;
  33293. msg[2] = 0x33;
  33294. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  33295. AssertIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  33296. AssertIntEQ(bio_buffer[0], msg[0]);
  33297. AssertIntEQ(bio_buffer[1], msg[1]);
  33298. AssertIntNE(bio_buffer[2], msg[2]);
  33299. BIO_free(bio);
  33300. msg[3] = 0x33;
  33301. bio_buffer[3] = 0x33;
  33302. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  33303. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  33304. AssertIntEQ(bio_buffer[0], msg[0]);
  33305. AssertIntEQ(bio_buffer[1], msg[1]);
  33306. AssertIntEQ(bio_buffer[2], msg[2]);
  33307. AssertIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  33308. /* check reading an empty string */
  33309. BIO_free(bio);
  33310. AssertNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  33311. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  33312. AssertStrEQ(emp, bio_buffer);
  33313. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  33314. /* check error cases */
  33315. BIO_free(bio);
  33316. AssertIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  33317. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33318. AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  33319. #if !defined(NO_FILESYSTEM)
  33320. {
  33321. BIO* f_bio;
  33322. XFILE f;
  33323. AssertNotNull(f_bio = BIO_new(BIO_s_file()));
  33324. AssertIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  33325. f = XFOPEN(svrCertFile, "rb");
  33326. AssertTrue((f != XBADFILE));
  33327. AssertIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  33328. AssertIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  33329. BIO_free(f_bio);
  33330. }
  33331. #endif /* NO_FILESYSTEM */
  33332. BIO_free(bio);
  33333. BIO_free(bio2);
  33334. /* try with type BIO */
  33335. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  33336. sizeof(msg));
  33337. AssertNotNull(bio = BIO_new(BIO_s_bio()));
  33338. AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  33339. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  33340. AssertIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  33341. AssertIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  33342. AssertIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  33343. AssertIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  33344. AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  33345. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  33346. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  33347. AssertStrEQ(bio_buffer, "hello wolfSSL\n");
  33348. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  33349. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  33350. AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  33351. BIO_free(bio);
  33352. BIO_free(bio2);
  33353. /* check reading an empty string */
  33354. AssertNotNull(bio = BIO_new(BIO_s_bio()));
  33355. AssertIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  33356. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  33357. AssertStrEQ(emp, bio_buffer);
  33358. BIO_free(bio);
  33359. printf(resultFmt, passed);
  33360. #endif
  33361. }
  33362. static void test_wolfSSL_BIO_puts(void)
  33363. {
  33364. #if defined(OPENSSL_EXTRA)
  33365. BIO* bio;
  33366. char input[] = "hello\0world\n.....ok\n\0";
  33367. char output[128];
  33368. printf(testingFmt, "wolfSSL_BIO_puts()");
  33369. XMEMSET(output, 0, sizeof(output));
  33370. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33371. AssertIntEQ(BIO_puts(bio, input), 5);
  33372. AssertIntEQ(BIO_pending(bio), 5);
  33373. AssertIntEQ(BIO_puts(bio, input + 6), 14);
  33374. AssertIntEQ(BIO_pending(bio), 19);
  33375. AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  33376. AssertStrEQ(output, "helloworld\n");
  33377. AssertIntEQ(BIO_pending(bio), 8);
  33378. AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  33379. AssertStrEQ(output, ".....ok\n");
  33380. AssertIntEQ(BIO_pending(bio), 0);
  33381. AssertIntEQ(BIO_puts(bio, ""), -1);
  33382. BIO_free(bio);
  33383. printf(resultFmt, passed);
  33384. #endif
  33385. }
  33386. static void test_wolfSSL_BIO_dump(void)
  33387. {
  33388. #if defined(OPENSSL_EXTRA)
  33389. BIO* bio;
  33390. static const unsigned char data[] = {
  33391. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  33392. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  33393. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  33394. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  33395. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  33396. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  33397. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  33398. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  33399. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  33400. 0xB4
  33401. };
  33402. /* Generated with OpenSSL. */
  33403. static const char expected[] =
  33404. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  33405. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  33406. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  33407. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  33408. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  33409. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  33410. static const char expectedAll[] =
  33411. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  33412. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  33413. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  33414. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  33415. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  33416. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  33417. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  33418. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  33419. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  33420. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  33421. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  33422. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  33423. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  33424. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  33425. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  33426. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  33427. char output[16 * 80];
  33428. int i;
  33429. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33430. /* Example key dumped. */
  33431. AssertIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  33432. sizeof(expected) - 1);
  33433. AssertIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  33434. AssertIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  33435. /* Try every possible value for a character. */
  33436. for (i = 0; i < 256; i++)
  33437. output[i] = i;
  33438. AssertIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  33439. AssertIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  33440. AssertIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  33441. BIO_free(bio);
  33442. printf(resultFmt, passed);
  33443. #endif
  33444. }
  33445. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  33446. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  33447. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  33448. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  33449. {
  33450. (void)ssl;
  33451. (void)buf;
  33452. (void)sz;
  33453. (void)ctx;
  33454. return WOLFSSL_CBIO_ERR_WANT_READ;
  33455. }
  33456. #endif
  33457. static void test_wolfSSL_BIO_should_retry(void)
  33458. {
  33459. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  33460. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  33461. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  33462. tcp_ready ready;
  33463. func_args server_args;
  33464. THREAD_TYPE serverThread;
  33465. SOCKET_T sockfd = 0;
  33466. WOLFSSL_CTX* ctx;
  33467. WOLFSSL* ssl;
  33468. char msg[64] = "hello wolfssl!";
  33469. char reply[1024];
  33470. int msgSz = (int)XSTRLEN(msg);
  33471. int ret;
  33472. BIO* bio;
  33473. printf(testingFmt, "wolfSSL_BIO_should_retry()");
  33474. XMEMSET(&server_args, 0, sizeof(func_args));
  33475. #ifdef WOLFSSL_TIRTOS
  33476. fdOpenSession(Task_self());
  33477. #endif
  33478. StartTCP();
  33479. InitTcpReady(&ready);
  33480. #if defined(USE_WINDOWS_API)
  33481. /* use RNG to get random port if using windows */
  33482. ready.port = GetRandomPort();
  33483. #endif
  33484. server_args.signal = &ready;
  33485. start_thread(test_server_nofail, &server_args, &serverThread);
  33486. wait_tcp_ready(&server_args);
  33487. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  33488. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  33489. AssertIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  33490. #endif
  33491. AssertIntEQ(WOLFSSL_SUCCESS,
  33492. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  33493. AssertIntEQ(WOLFSSL_SUCCESS,
  33494. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  33495. AssertIntEQ(WOLFSSL_SUCCESS,
  33496. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  33497. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  33498. /* force retry */
  33499. ssl = wolfSSL_new(ctx);
  33500. AssertNotNull(ssl);
  33501. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  33502. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  33503. AssertNotNull(bio = BIO_new(BIO_f_ssl()));
  33504. BIO_set_ssl(bio, ssl, BIO_CLOSE);
  33505. AssertIntLE(BIO_write(bio, msg, msgSz), 0);
  33506. AssertIntNE(BIO_should_retry(bio), 0);
  33507. /* now perform successful connection */
  33508. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  33509. AssertIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  33510. BIO_read(bio, reply, sizeof(reply));
  33511. ret = wolfSSL_get_error(ssl, -1);
  33512. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  33513. AssertIntNE(BIO_should_retry(bio), 0);
  33514. }
  33515. else {
  33516. AssertIntEQ(BIO_should_retry(bio), 0);
  33517. }
  33518. AssertIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  33519. XSTRLEN("I hear you fa shizzle!")), 0);
  33520. BIO_free(bio);
  33521. wolfSSL_CTX_free(ctx);
  33522. join_thread(serverThread);
  33523. FreeTcpReady(&ready);
  33524. #ifdef WOLFSSL_TIRTOS
  33525. fdOpenSession(Task_self());
  33526. #endif
  33527. printf(resultFmt, passed);
  33528. #endif
  33529. }
  33530. static void test_wolfSSL_BIO_connect(void)
  33531. {
  33532. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
  33533. tcp_ready ready;
  33534. func_args server_args;
  33535. THREAD_TYPE serverThread;
  33536. BIO *tcpBio;
  33537. BIO *sslBio;
  33538. SSL_CTX* ctx;
  33539. SSL *ssl;
  33540. SSL *sslPtr;
  33541. char msg[] = "hello wolfssl!";
  33542. char reply[30];
  33543. char buff[10] = {0};
  33544. printf(testingFmt, "wolfSSL_BIO_new_connect()");
  33545. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  33546. AssertIntEQ(WOLFSSL_SUCCESS,
  33547. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  33548. AssertIntEQ(WOLFSSL_SUCCESS,
  33549. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  33550. AssertIntEQ(WOLFSSL_SUCCESS,
  33551. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  33552. /* Setup server */
  33553. XMEMSET(&server_args, 0, sizeof(func_args));
  33554. StartTCP();
  33555. InitTcpReady(&ready);
  33556. #if defined(USE_WINDOWS_API)
  33557. /* use RNG to get random port if using windows */
  33558. ready.port = GetRandomPort();
  33559. #endif
  33560. server_args.signal = &ready;
  33561. start_thread(test_server_nofail, &server_args, &serverThread);
  33562. wait_tcp_ready(&server_args);
  33563. AssertIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  33564. /* Start the test proper */
  33565. /* Setup the TCP BIO */
  33566. AssertNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  33567. AssertIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  33568. /* Setup the SSL object */
  33569. AssertNotNull(ssl = SSL_new(ctx));
  33570. SSL_set_connect_state(ssl);
  33571. /* Setup the SSL BIO */
  33572. AssertNotNull(sslBio = BIO_new(BIO_f_ssl()));
  33573. AssertIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  33574. /* Verify that BIO_get_ssl works. */
  33575. AssertIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  33576. AssertPtrEq(ssl, sslPtr);
  33577. /* Link BIO's so that sslBio uses tcpBio for IO */
  33578. AssertPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  33579. /* Do TCP connect */
  33580. AssertIntEQ(BIO_do_connect(sslBio), 1);
  33581. /* Do TLS handshake */
  33582. AssertIntEQ(BIO_do_handshake(sslBio), 1);
  33583. /* Test writing */
  33584. AssertIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  33585. /* Expect length of default wolfSSL reply */
  33586. AssertIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  33587. /* Clean it all up */
  33588. BIO_free_all(sslBio);
  33589. /* Server clean up */
  33590. join_thread(serverThread);
  33591. FreeTcpReady(&ready);
  33592. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  33593. * after. */
  33594. XMEMSET(&server_args, 0, sizeof(func_args));
  33595. StartTCP();
  33596. InitTcpReady(&ready);
  33597. #if defined(USE_WINDOWS_API)
  33598. /* use RNG to get random port if using windows */
  33599. ready.port = GetRandomPort();
  33600. #endif
  33601. server_args.signal = &ready;
  33602. start_thread(test_server_nofail, &server_args, &serverThread);
  33603. wait_tcp_ready(&server_args);
  33604. AssertIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  33605. AssertNotNull(sslBio = BIO_new_ssl_connect(ctx));
  33606. AssertIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  33607. AssertIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  33608. AssertIntEQ(BIO_do_connect(sslBio), 1);
  33609. AssertIntEQ(BIO_do_handshake(sslBio), 1);
  33610. AssertIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  33611. AssertIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  33612. /* Attempt to close the TLS connection gracefully. */
  33613. BIO_ssl_shutdown(sslBio);
  33614. BIO_free_all(sslBio);
  33615. join_thread(serverThread);
  33616. FreeTcpReady(&ready);
  33617. SSL_CTX_free(ctx);
  33618. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  33619. wc_ecc_fp_free(); /* free per thread cache */
  33620. #endif
  33621. printf(resultFmt, passed);
  33622. #endif
  33623. }
  33624. static void test_wolfSSL_BIO_tls(void)
  33625. {
  33626. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  33627. SSL_CTX* ctx;
  33628. SSL *ssl;
  33629. BIO *readBio;
  33630. BIO *writeBio;
  33631. int ret, err = 0;
  33632. printf(testingFmt, "test_wolfSSL_BIO_tls()");
  33633. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  33634. AssertNotNull(ssl = SSL_new(ctx));
  33635. AssertNotNull(readBio = BIO_new(BIO_s_mem()));
  33636. AssertNotNull(writeBio = BIO_new(BIO_s_mem()));
  33637. /* Qt reads data from write-bio,
  33638. * then writes the read data into plain packet.
  33639. * Qt reads data from plain packet,
  33640. * then writes the read data into read-bio.
  33641. */
  33642. SSL_set_bio(ssl, readBio, writeBio);
  33643. do {
  33644. #ifdef WOLFSSL_ASYNC_CRYPT
  33645. if (err == WC_PENDING_E) {
  33646. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  33647. if (ret < 0) { break; } else if (ret == 0) { continue; }
  33648. }
  33649. #endif
  33650. ret = SSL_connect(ssl);
  33651. err = SSL_get_error(ssl, 0);
  33652. } while (err == WC_PENDING_E);
  33653. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  33654. /* in this use case, should return WANT READ
  33655. * so that Qt will read the data from plain packet for next state.
  33656. */
  33657. AssertIntEQ(err, SSL_ERROR_WANT_READ);
  33658. SSL_free(ssl);
  33659. SSL_CTX_free(ctx);
  33660. printf(resultFmt, passed);
  33661. #endif
  33662. }
  33663. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
  33664. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  33665. {
  33666. BIO* clientBio;
  33667. SSL* sslClient;
  33668. SSL_CTX* ctx;
  33669. char connectAddr[20]; /* IP + port */;
  33670. (void)args;
  33671. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  33672. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  33673. AssertIntEQ(BIO_do_connect(clientBio), 1);
  33674. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  33675. AssertNotNull(sslClient = SSL_new(ctx));
  33676. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  33677. SSL_set_bio(sslClient, clientBio, clientBio);
  33678. AssertIntEQ(SSL_connect(sslClient), 1);
  33679. SSL_free(sslClient);
  33680. SSL_CTX_free(ctx);
  33681. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  33682. wc_ecc_fp_free(); /* free per thread cache */
  33683. #endif
  33684. return 0;
  33685. }
  33686. #endif
  33687. static void test_wolfSSL_BIO_accept(void)
  33688. {
  33689. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
  33690. BIO* serverBindBio;
  33691. BIO* serverAcceptBio;
  33692. SSL* sslServer;
  33693. SSL_CTX* ctx;
  33694. func_args args;
  33695. THREAD_TYPE thread;
  33696. char port[10]; /* 10 bytes should be enough to store the string
  33697. * representation of the port */
  33698. printf(testingFmt, "wolfSSL_BIO_new_accept()");
  33699. AssertIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  33700. AssertNotNull(serverBindBio = BIO_new_accept(port));
  33701. /* First BIO_do_accept binds the port */
  33702. AssertIntEQ(BIO_do_accept(serverBindBio), 1);
  33703. XMEMSET(&args, 0, sizeof(func_args));
  33704. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  33705. AssertIntEQ(BIO_do_accept(serverBindBio), 1);
  33706. /* Let's plug it into SSL to test */
  33707. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  33708. AssertIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  33709. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  33710. AssertNotNull(sslServer = SSL_new(ctx));
  33711. AssertNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  33712. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  33713. AssertIntEQ(SSL_accept(sslServer), 1);
  33714. join_thread(thread);
  33715. BIO_free(serverBindBio);
  33716. SSL_free(sslServer);
  33717. SSL_CTX_free(ctx);
  33718. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  33719. wc_ecc_fp_free(); /* free per thread cache */
  33720. #endif
  33721. printf(resultFmt, passed);
  33722. #endif
  33723. }
  33724. static void test_wolfSSL_BIO_write(void)
  33725. {
  33726. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  33727. BIO* bio;
  33728. BIO* bio64;
  33729. BIO* ptr;
  33730. int sz;
  33731. char msg[] = "conversion test";
  33732. char out[40];
  33733. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  33734. void* bufPtr = NULL;
  33735. BUF_MEM* buf = NULL;
  33736. printf(testingFmt, "wolfSSL_BIO_write()");
  33737. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  33738. AssertNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  33739. /* now should convert to base64 then write to memory */
  33740. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  33741. BIO_flush(bio);
  33742. /* test BIO chain */
  33743. AssertIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  33744. AssertNotNull(buf);
  33745. AssertIntEQ(buf->length, 25);
  33746. AssertIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  33747. AssertPtrEq(buf->data, bufPtr);
  33748. AssertNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  33749. sz = sizeof(out);
  33750. XMEMSET(out, 0, sz);
  33751. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  33752. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  33753. /* write then read should return the same message */
  33754. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  33755. sz = sizeof(out);
  33756. XMEMSET(out, 0, sz);
  33757. AssertIntEQ(BIO_read(bio, out, sz), 16);
  33758. AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  33759. /* now try encoding with no line ending */
  33760. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  33761. #ifdef HAVE_EX_DATA
  33762. BIO_set_ex_data(bio64, 0, (void*) "data");
  33763. AssertIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  33764. #endif
  33765. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  33766. BIO_flush(bio);
  33767. sz = sizeof(out);
  33768. XMEMSET(out, 0, sz);
  33769. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  33770. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  33771. BIO_free_all(bio); /* frees bio64 also */
  33772. /* test with more than one bio64 in list */
  33773. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  33774. AssertNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  33775. AssertNotNull(BIO_push(bio64, BIO_new(BIO_s_mem())));
  33776. /* now should convert to base64 when stored and then decode with read */
  33777. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  33778. BIO_flush(bio);
  33779. sz = sizeof(out);
  33780. XMEMSET(out, 0, sz);
  33781. AssertIntEQ((sz = BIO_read(bio, out, sz)), 16);
  33782. AssertIntEQ(XMEMCMP(out, msg, sz), 0);
  33783. BIO_clear_flags(bio64, ~0);
  33784. BIO_set_retry_read(bio);
  33785. BIO_free_all(bio); /* frees bio64s also */
  33786. AssertNotNull(bio = BIO_new_mem_buf(out, 0));
  33787. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  33788. BIO_free(bio);
  33789. printf(resultFmt, passed);
  33790. #endif
  33791. }
  33792. static void test_wolfSSL_BIO_printf(void)
  33793. {
  33794. #if defined(OPENSSL_ALL)
  33795. BIO* bio;
  33796. int sz = 7;
  33797. char msg[] = "TLS 1.3 for the world";
  33798. char out[60];
  33799. char expected[] = "TLS 1.3 for the world : sz = 7";
  33800. printf(testingFmt, "wolfSSL_BIO_printf()");
  33801. XMEMSET(out, 0, sizeof(out));
  33802. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33803. AssertIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  33804. AssertIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  33805. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  33806. AssertIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  33807. BIO_free(bio);
  33808. printf(resultFmt, passed);
  33809. #endif
  33810. }
  33811. static void test_wolfSSL_BIO_f_md(void)
  33812. {
  33813. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  33814. BIO *bio, *mem;
  33815. char msg[] = "message to hash";
  33816. char out[60];
  33817. EVP_MD_CTX* ctx;
  33818. const unsigned char testKey[] =
  33819. {
  33820. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  33821. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  33822. 0x0b, 0x0b, 0x0b, 0x0b
  33823. };
  33824. const char testData[] = "Hi There";
  33825. const unsigned char testResult[] =
  33826. {
  33827. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  33828. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  33829. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  33830. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  33831. };
  33832. const unsigned char expectedHash[] =
  33833. {
  33834. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  33835. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  33836. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  33837. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  33838. };
  33839. const unsigned char emptyHash[] =
  33840. {
  33841. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  33842. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  33843. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  33844. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  33845. };
  33846. unsigned char check[sizeof(testResult) + 1];
  33847. size_t checkSz = -1;
  33848. EVP_PKEY* key;
  33849. printf(testingFmt, "wolfSSL_BIO_f_md()");
  33850. XMEMSET(out, 0, sizeof(out));
  33851. AssertNotNull(bio = BIO_new(BIO_f_md()));
  33852. AssertNotNull(mem = BIO_new(BIO_s_mem()));
  33853. AssertIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  33854. AssertIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  33855. /* should not be able to write/read yet since just digest wrapper and no
  33856. * data is passing through the bio */
  33857. AssertIntEQ(BIO_write(bio, msg, 0), 0);
  33858. AssertIntEQ(BIO_pending(bio), 0);
  33859. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  33860. AssertIntEQ(BIO_gets(bio, out, 3), 0);
  33861. AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  33862. AssertIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  33863. BIO_reset(bio);
  33864. /* append BIO mem to bio in order to read/write */
  33865. AssertNotNull(bio = BIO_push(bio, mem));
  33866. XMEMSET(out, 0, sizeof(out));
  33867. AssertIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  33868. AssertIntEQ(BIO_pending(bio), 16);
  33869. /* this just reads the message and does not hash it (gets calls final) */
  33870. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  33871. AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  33872. /* create a message digest using BIO */
  33873. XMEMSET(out, 0, sizeof(out));
  33874. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  33875. AssertIntEQ(BIO_pending(mem), 16);
  33876. AssertIntEQ(BIO_pending(bio), 16);
  33877. AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  33878. AssertIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  33879. BIO_free(bio);
  33880. BIO_free(mem);
  33881. /* test with HMAC */
  33882. XMEMSET(out, 0, sizeof(out));
  33883. AssertNotNull(bio = BIO_new(BIO_f_md()));
  33884. AssertNotNull(mem = BIO_new(BIO_s_mem()));
  33885. BIO_get_md_ctx(bio, &ctx);
  33886. AssertNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  33887. testKey, (int)sizeof(testKey)));
  33888. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  33889. AssertNotNull(bio = BIO_push(bio, mem));
  33890. BIO_write(bio, testData, (int)strlen(testData));
  33891. EVP_DigestSignFinal(ctx, NULL, &checkSz);
  33892. EVP_DigestSignFinal(ctx, check, &checkSz);
  33893. AssertIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  33894. EVP_PKEY_free(key);
  33895. BIO_free(bio);
  33896. BIO_free(mem);
  33897. printf(resultFmt, passed);
  33898. #endif
  33899. }
  33900. static void test_wolfSSL_BIO_up_ref(void)
  33901. {
  33902. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  33903. BIO* bio;
  33904. printf(testingFmt, "wolfSSL_BIO_up_ref()");
  33905. AssertNotNull(bio = BIO_new(BIO_f_md()));
  33906. AssertIntEQ(BIO_up_ref(NULL), 0);
  33907. AssertIntEQ(BIO_up_ref(bio), 1);
  33908. BIO_free(bio);
  33909. AssertIntEQ(BIO_up_ref(bio), 1);
  33910. BIO_free(bio);
  33911. BIO_free(bio);
  33912. printf(resultFmt, "passed");
  33913. #endif
  33914. }
  33915. #endif /* !NO_BIO */
  33916. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  33917. /* test that the callback arg is correct */
  33918. static int certCbArg = 0;
  33919. static int clientCertCb(WOLFSSL* ssl, void* arg)
  33920. {
  33921. if (ssl == NULL || arg != &certCbArg)
  33922. return 0;
  33923. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  33924. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  33925. return 0;
  33926. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  33927. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  33928. return 0;
  33929. return 1;
  33930. }
  33931. static void clientCertSetupCb(WOLFSSL_CTX* ctx)
  33932. {
  33933. SSL_CTX_set_cert_cb(ctx, clientCertCb, &certCbArg);
  33934. }
  33935. /**
  33936. * This is only done because test_client_nofail has no way to stop
  33937. * certificate and key loading
  33938. */
  33939. static void clientCertClearCb(WOLFSSL* ssl)
  33940. {
  33941. /* Clear the loaded certs to force the callbacks to set them up */
  33942. SSL_certs_clear(ssl);
  33943. }
  33944. static int serverCertCb(WOLFSSL* ssl, void* arg)
  33945. {
  33946. if (ssl == NULL || arg != &certCbArg)
  33947. return 0;
  33948. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  33949. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  33950. return 0;
  33951. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  33952. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  33953. return 0;
  33954. return 1;
  33955. }
  33956. static void serverCertSetupCb(WOLFSSL_CTX* ctx)
  33957. {
  33958. SSL_CTX_set_cert_cb(ctx, serverCertCb, &certCbArg);
  33959. }
  33960. /**
  33961. * This is only done because test_server_nofail has no way to stop
  33962. * certificate and key loading
  33963. */
  33964. static void serverCertClearCb(WOLFSSL* ssl)
  33965. {
  33966. /* Clear the loaded certs to force the callbacks to set them up */
  33967. SSL_certs_clear(ssl);
  33968. }
  33969. #endif
  33970. static void test_wolfSSL_cert_cb(void)
  33971. {
  33972. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  33973. callback_functions func_cb_client;
  33974. callback_functions func_cb_server;
  33975. tcp_ready ready;
  33976. func_args client_args;
  33977. func_args server_args;
  33978. THREAD_TYPE serverThread;
  33979. XMEMSET(&client_args, 0, sizeof(func_args));
  33980. XMEMSET(&server_args, 0, sizeof(func_args));
  33981. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  33982. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  33983. #ifdef WOLFSSL_TIRTOS
  33984. fdOpenSession(Task_self());
  33985. #endif
  33986. StartTCP();
  33987. InitTcpReady(&ready);
  33988. #if defined(USE_WINDOWS_API)
  33989. /* use RNG to get random port if using windows */
  33990. ready.port = GetRandomPort();
  33991. #endif
  33992. server_args.signal = &ready;
  33993. client_args.signal = &ready;
  33994. client_args.callbacks = &func_cb_client;
  33995. server_args.callbacks = &func_cb_server;
  33996. func_cb_client.ctx_ready = clientCertSetupCb;
  33997. func_cb_client.ssl_ready = clientCertClearCb;
  33998. func_cb_server.ctx_ready = serverCertSetupCb;
  33999. func_cb_server.ssl_ready = serverCertClearCb;
  34000. start_thread(test_server_nofail, &server_args, &serverThread);
  34001. wait_tcp_ready(&server_args);
  34002. test_client_nofail(&client_args, NULL);
  34003. join_thread(serverThread);
  34004. AssertTrue(client_args.return_code);
  34005. AssertTrue(server_args.return_code);
  34006. FreeTcpReady(&ready);
  34007. #ifdef WOLFSSL_TIRTOS
  34008. fdOpenSession(Task_self());
  34009. #endif
  34010. #endif
  34011. }
  34012. static void test_wolfSSL_SESSION(void)
  34013. {
  34014. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  34015. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  34016. !defined(NO_SESSION_CACHE)
  34017. WOLFSSL* ssl;
  34018. WOLFSSL_CTX* ctx;
  34019. WOLFSSL_SESSION* sess;
  34020. WOLFSSL_SESSION* sess_copy;
  34021. #ifdef OPENSSL_EXTRA
  34022. unsigned char* sessDer = NULL;
  34023. unsigned char* ptr = NULL;
  34024. const unsigned char context[] = "user app context";
  34025. unsigned int contextSz = (unsigned int)sizeof(context);
  34026. int sz;
  34027. #endif
  34028. int ret, err;
  34029. SOCKET_T sockfd;
  34030. tcp_ready ready;
  34031. func_args server_args;
  34032. THREAD_TYPE serverThread;
  34033. char msg[80];
  34034. const char* sendGET = "GET";
  34035. printf(testingFmt, "wolfSSL_SESSION()");
  34036. /* TLS v1.3 requires session tickets */
  34037. /* CHACHA and POLY1305 required for myTicketEncCb */
  34038. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  34039. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  34040. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  34041. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  34042. #else
  34043. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34044. #endif
  34045. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  34046. WOLFSSL_FILETYPE_PEM));
  34047. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  34048. WOLFSSL_FILETYPE_PEM));
  34049. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  34050. WOLFSSL_SUCCESS);
  34051. #ifdef WOLFSSL_ENCRYPTED_KEYS
  34052. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  34053. #endif
  34054. #ifdef HAVE_SESSION_TICKET
  34055. /* Use session tickets, for ticket tests below */
  34056. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  34057. #endif
  34058. XMEMSET(&server_args, 0, sizeof(func_args));
  34059. #ifdef WOLFSSL_TIRTOS
  34060. fdOpenSession(Task_self());
  34061. #endif
  34062. StartTCP();
  34063. InitTcpReady(&ready);
  34064. #if defined(USE_WINDOWS_API)
  34065. /* use RNG to get random port if using windows */
  34066. ready.port = GetRandomPort();
  34067. #endif
  34068. server_args.signal = &ready;
  34069. start_thread(test_server_nofail, &server_args, &serverThread);
  34070. wait_tcp_ready(&server_args);
  34071. /* client connection */
  34072. ssl = wolfSSL_new(ctx);
  34073. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  34074. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  34075. #ifdef WOLFSSL_ASYNC_CRYPT
  34076. err = 0; /* Reset error */
  34077. #endif
  34078. do {
  34079. #ifdef WOLFSSL_ASYNC_CRYPT
  34080. if (err == WC_PENDING_E) {
  34081. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  34082. if (ret < 0) { break; } else if (ret == 0) { continue; }
  34083. }
  34084. #endif
  34085. ret = wolfSSL_connect(ssl);
  34086. err = wolfSSL_get_error(ssl, 0);
  34087. } while (err == WC_PENDING_E);
  34088. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  34089. #ifdef WOLFSSL_ASYNC_CRYPT
  34090. err = 0; /* Reset error */
  34091. #endif
  34092. do {
  34093. #ifdef WOLFSSL_ASYNC_CRYPT
  34094. if (err == WC_PENDING_E) {
  34095. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  34096. if (ret < 0) { break; } else if (ret == 0) { continue; }
  34097. }
  34098. #endif
  34099. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  34100. err = wolfSSL_get_error(ssl, 0);
  34101. } while (err == WC_PENDING_E);
  34102. AssertIntEQ(ret, (int)XSTRLEN(sendGET));
  34103. #ifdef WOLFSSL_ASYNC_CRYPT
  34104. err = 0; /* Reset error */
  34105. #endif
  34106. do {
  34107. #ifdef WOLFSSL_ASYNC_CRYPT
  34108. if (err == WC_PENDING_E) {
  34109. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  34110. if (ret < 0) { break; } else if (ret == 0) { continue; }
  34111. }
  34112. #endif
  34113. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  34114. err = wolfSSL_get_error(ssl, 0);
  34115. } while (err == WC_PENDING_E);
  34116. AssertIntEQ(ret, 23);
  34117. AssertPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  34118. AssertPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  34119. #ifdef HAVE_EXT_CACHE
  34120. AssertPtrEq(sess, sess_copy); /* they should be the same pointer but without
  34121. * HAVE_EXT_CACHE we get new objects each time */
  34122. #endif
  34123. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  34124. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  34125. sess = wolfSSL_get_session(ssl);
  34126. #ifdef OPENSSL_EXTRA
  34127. AssertIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  34128. AssertIntEQ(SSL_SESSION_is_resumable(sess), 1);
  34129. AssertIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  34130. AssertIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  34131. #ifdef HAVE_SESSION_TICKET
  34132. AssertIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  34133. AssertIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  34134. SESSION_TICKET_HINT_DEFAULT);
  34135. #else
  34136. AssertIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  34137. #endif
  34138. #else
  34139. (void)sess;
  34140. #endif /* OPENSSL_EXTRA */
  34141. /* Retain copy of the session for later testing */
  34142. AssertNotNull(sess = wolfSSL_get1_session(ssl));
  34143. wolfSSL_shutdown(ssl);
  34144. wolfSSL_free(ssl);
  34145. join_thread(serverThread);
  34146. FreeTcpReady(&ready);
  34147. #ifdef WOLFSSL_TIRTOS
  34148. fdOpenSession(Task_self());
  34149. #endif
  34150. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  34151. {
  34152. X509 *x509;
  34153. char buf[30];
  34154. int bufSz;
  34155. AssertNotNull(x509 = SSL_SESSION_get0_peer(sess));
  34156. AssertIntGT((bufSz = X509_NAME_get_text_by_NID(
  34157. X509_get_subject_name(x509), NID_organizationalUnitName,
  34158. buf, sizeof(buf))), 0);
  34159. AssertIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  34160. if (bufSz == 7) {
  34161. AssertIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  34162. }
  34163. if (bufSz == 16) {
  34164. AssertIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  34165. }
  34166. }
  34167. #endif
  34168. #ifdef HAVE_EXT_CACHE
  34169. AssertNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  34170. wolfSSL_SESSION_free(sess_copy);
  34171. sess_copy = NULL;
  34172. #endif
  34173. #ifdef OPENSSL_EXTRA
  34174. /* get session from DER and update the timeout */
  34175. AssertIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  34176. AssertIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  34177. wolfSSL_SESSION_free(sess);
  34178. sess = NULL;
  34179. ptr = sessDer;
  34180. AssertNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  34181. AssertNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  34182. (const unsigned char**)&ptr, sz));
  34183. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  34184. sessDer = NULL;
  34185. AssertIntGT(wolfSSL_SESSION_get_time(sess), 0);
  34186. AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  34187. #endif
  34188. /* successful set session test */
  34189. AssertNotNull(ssl = wolfSSL_new(ctx));
  34190. AssertIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  34191. #ifdef HAVE_SESSION_TICKET
  34192. /* Test set/get session ticket */
  34193. {
  34194. const char* ticket = "This is a session ticket";
  34195. char buf[64] = {0};
  34196. word32 bufSz = (word32)sizeof(buf);
  34197. AssertIntEQ(SSL_SUCCESS,
  34198. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  34199. (word32)XSTRLEN(ticket)));
  34200. AssertIntEQ(SSL_SUCCESS,
  34201. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  34202. AssertStrEQ(ticket, buf);
  34203. }
  34204. #endif
  34205. #ifdef OPENSSL_EXTRA
  34206. /* session timeout case */
  34207. /* make the session to be expired */
  34208. AssertIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  34209. XSLEEP_MS(1200);
  34210. /* SSL_set_session should reject specified session but return success
  34211. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  34212. */
  34213. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  34214. AssertIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  34215. #else
  34216. AssertIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  34217. #endif
  34218. AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  34219. /* fail case with miss match session context IDs (use compatibility API) */
  34220. AssertIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  34221. SSL_SUCCESS);
  34222. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  34223. wolfSSL_free(ssl);
  34224. AssertIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  34225. SSL_FAILURE);
  34226. AssertIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  34227. SSL_SUCCESS);
  34228. AssertNotNull(ssl = wolfSSL_new(ctx));
  34229. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  34230. #endif /* OPENSSL_EXTRA */
  34231. wolfSSL_free(ssl);
  34232. wolfSSL_SESSION_free(sess);
  34233. wolfSSL_CTX_free(ctx);
  34234. printf(resultFmt, passed);
  34235. #endif
  34236. }
  34237. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  34238. defined(HAVE_EX_DATA)
  34239. static int clientSessRemCountMalloc = 0;
  34240. static int serverSessRemCountMalloc = 0;
  34241. static int clientSessRemCountFree = 0;
  34242. static int serverSessRemCountFree = 0;
  34243. static WOLFSSL_CTX* serverSessCtx = NULL;
  34244. static WOLFSSL_SESSION* serverSess = NULL;
  34245. #ifndef NO_SESSION_CACHE_REF
  34246. static WOLFSSL_CTX* clientSessCtx = NULL;
  34247. static WOLFSSL_SESSION* clientSess = NULL;
  34248. #endif
  34249. static int serverSessRemIdx = 3;
  34250. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  34251. {
  34252. int* mallocedData = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  34253. (void)ctx;
  34254. AssertNotNull(mallocedData);
  34255. if (!*mallocedData)
  34256. clientSessRemCountFree++;
  34257. else
  34258. serverSessRemCountFree++;
  34259. XFREE(mallocedData, NULL, DYNAMIC_TYPE_SESSION);
  34260. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  34261. }
  34262. static void SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  34263. {
  34264. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  34265. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  34266. !defined(NO_SESSION_CACHE_REF)
  34267. /* Allow downgrade, set min version, and disable TLS 1.3.
  34268. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  34269. * reference to the session cache. But with WOLFSSL_TLS13 and without
  34270. * HAVE_SESSION_TICKET we won't have a session ID to be able to place the
  34271. * session in the cache. In this case we need to downgrade to previous
  34272. * versions to just use the legacy session ID field. */
  34273. AssertIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION), SSL_SUCCESS);
  34274. AssertIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION), SSL_SUCCESS);
  34275. #endif
  34276. }
  34277. static void SessRemSslSetupCb(WOLFSSL* ssl)
  34278. {
  34279. int* mallocedData = (int*)XMALLOC(sizeof(int), NULL, DYNAMIC_TYPE_SESSION);
  34280. AssertNotNull(mallocedData);
  34281. *mallocedData = SSL_is_server(ssl);
  34282. if (!*mallocedData) {
  34283. clientSessRemCountMalloc++;
  34284. #ifndef NO_SESSION_CACHE_REF
  34285. AssertNotNull(clientSess = SSL_get1_session(ssl));
  34286. AssertIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  34287. SSL_SUCCESS);
  34288. #endif
  34289. }
  34290. else {
  34291. serverSessRemCountMalloc++;
  34292. AssertNotNull(serverSess = SSL_get1_session(ssl));
  34293. AssertIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  34294. SSL_SUCCESS);
  34295. }
  34296. AssertIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl), serverSessRemIdx,
  34297. mallocedData), SSL_SUCCESS);
  34298. }
  34299. #endif
  34300. static void test_wolfSSL_CTX_sess_set_remove_cb(void)
  34301. {
  34302. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  34303. defined(HAVE_EX_DATA)
  34304. /* Check that the remove callback gets called for external data in a
  34305. * session object */
  34306. callback_functions func_cb;
  34307. tcp_ready ready;
  34308. func_args client_args;
  34309. func_args server_args;
  34310. THREAD_TYPE serverThread;
  34311. printf(testingFmt, "wolfSSL_CTX_sess_set_remove_cb()");
  34312. XMEMSET(&client_args, 0, sizeof(func_args));
  34313. XMEMSET(&server_args, 0, sizeof(func_args));
  34314. XMEMSET(&func_cb, 0, sizeof(callback_functions));
  34315. #ifdef WOLFSSL_TIRTOS
  34316. fdOpenSession(Task_self());
  34317. #endif
  34318. StartTCP();
  34319. InitTcpReady(&ready);
  34320. #if defined(USE_WINDOWS_API)
  34321. /* use RNG to get random port if using windows */
  34322. ready.port = GetRandomPort();
  34323. #endif
  34324. server_args.signal = &ready;
  34325. client_args.signal = &ready;
  34326. client_args.callbacks = &func_cb;
  34327. server_args.callbacks = &func_cb;
  34328. func_cb.ctx_ready = SessRemCtxSetupCb;
  34329. func_cb.on_result = SessRemSslSetupCb;
  34330. start_thread(test_server_nofail, &server_args, &serverThread);
  34331. wait_tcp_ready(&server_args);
  34332. test_client_nofail(&client_args, NULL);
  34333. join_thread(serverThread);
  34334. AssertTrue(client_args.return_code);
  34335. AssertTrue(server_args.return_code);
  34336. FreeTcpReady(&ready);
  34337. #ifdef WOLFSSL_TIRTOS
  34338. fdOpenSession(Task_self());
  34339. #endif
  34340. /* Both should have been allocated */
  34341. AssertIntEQ(clientSessRemCountMalloc, 1);
  34342. AssertIntEQ(serverSessRemCountMalloc, 1);
  34343. #ifdef NO_SESSION_CACHE_REF
  34344. /* Client session should not be added to cache so this should be free'd when
  34345. * the SSL object was being free'd */
  34346. AssertIntEQ(clientSessRemCountFree, 1);
  34347. #else
  34348. /* Client session is in cache due to requiring a persistent reference */
  34349. AssertIntEQ(clientSessRemCountFree, 0);
  34350. /* Force a cache lookup */
  34351. AssertNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  34352. /* Force a cache update */
  34353. AssertNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  34354. /* This should set the timeout to 0 and call the remove callback from within
  34355. * the session cache. */
  34356. AssertIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  34357. AssertNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  34358. AssertIntEQ(clientSessRemCountFree, 1);
  34359. #endif
  34360. /* Server session is in the cache so ex_data isn't free'd with the SSL
  34361. * object */
  34362. AssertIntEQ(serverSessRemCountFree, 0);
  34363. /* Force a cache lookup */
  34364. AssertNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  34365. /* Force a cache update */
  34366. AssertNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  34367. /* This should set the timeout to 0 and call the remove callback from within
  34368. * the session cache. */
  34369. AssertIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  34370. AssertNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  34371. AssertIntEQ(serverSessRemCountFree, 1);
  34372. /* Need to free the references that we kept */
  34373. SSL_CTX_free(serverSessCtx);
  34374. SSL_SESSION_free(serverSess);
  34375. #ifndef NO_SESSION_CACHE_REF
  34376. SSL_CTX_free(clientSessCtx);
  34377. SSL_SESSION_free(clientSess);
  34378. #endif
  34379. printf(resultFmt, passed);
  34380. #endif
  34381. }
  34382. static void test_wolfSSL_ticket_keys(void)
  34383. {
  34384. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  34385. !defined(NO_WOLFSSL_SERVER)
  34386. WOLFSSL_CTX* ctx;
  34387. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  34388. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34389. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  34390. WOLFSSL_FAILURE);
  34391. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  34392. WOLFSSL_FAILURE);
  34393. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  34394. WOLFSSL_FAILURE);
  34395. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  34396. WOLFSSL_FAILURE);
  34397. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  34398. WOLFSSL_FAILURE);
  34399. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  34400. WOLFSSL_FAILURE);
  34401. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  34402. WOLFSSL_FAILURE);
  34403. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  34404. WOLFSSL_FAILURE);
  34405. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  34406. WOLFSSL_FAILURE);
  34407. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  34408. WOLFSSL_FAILURE);
  34409. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  34410. WOLFSSL_FAILURE);
  34411. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  34412. WOLFSSL_FAILURE);
  34413. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  34414. WOLFSSL_FAILURE);
  34415. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  34416. WOLFSSL_FAILURE);
  34417. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  34418. WOLFSSL_SUCCESS);
  34419. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  34420. WOLFSSL_SUCCESS);
  34421. wolfSSL_CTX_free(ctx);
  34422. #endif
  34423. }
  34424. #ifndef NO_BIO
  34425. static void test_wolfSSL_d2i_PUBKEY(void)
  34426. {
  34427. #if defined(OPENSSL_EXTRA)
  34428. BIO* bio;
  34429. EVP_PKEY* pkey;
  34430. printf(testingFmt, "wolfSSL_d2i_PUBKEY()");
  34431. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34432. AssertNull(d2i_PUBKEY_bio(NULL, NULL));
  34433. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  34434. /* RSA PUBKEY test */
  34435. AssertIntGT(BIO_write(bio, client_keypub_der_2048,
  34436. sizeof_client_keypub_der_2048), 0);
  34437. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  34438. EVP_PKEY_free(pkey);
  34439. #endif
  34440. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  34441. /* ECC PUBKEY test */
  34442. AssertIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  34443. sizeof_ecc_clikeypub_der_256), 0);
  34444. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  34445. EVP_PKEY_free(pkey);
  34446. #endif
  34447. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  34448. /* DSA PUBKEY test */
  34449. AssertIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  34450. sizeof_dsa_pub_key_der_2048), 0);
  34451. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  34452. EVP_PKEY_free(pkey);
  34453. #endif
  34454. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  34455. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  34456. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  34457. (HAVE_FIPS_VERSION > 2))
  34458. /* DH PUBKEY test */
  34459. AssertIntGT(BIO_write(bio, dh_pub_key_der_2048,
  34460. sizeof_dh_pub_key_der_2048), 0);
  34461. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  34462. EVP_PKEY_free(pkey);
  34463. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  34464. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  34465. BIO_free(bio);
  34466. (void)pkey;
  34467. printf(resultFmt, passed);
  34468. #endif
  34469. }
  34470. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  34471. static void test_wolfSSL_d2i_PrivateKeys_bio(void)
  34472. {
  34473. BIO* bio = NULL;
  34474. EVP_PKEY* pkey = NULL;
  34475. #ifndef NO_RSA
  34476. #endif
  34477. WOLFSSL_CTX* ctx;
  34478. #if defined(WOLFSSL_KEY_GEN)
  34479. unsigned char buff[4096];
  34480. unsigned char* bufPtr = buff;
  34481. #endif
  34482. printf(testingFmt, "wolfSSL_d2i_PrivateKeys_bio()");
  34483. /* test creating new EVP_PKEY with bad arg */
  34484. AssertNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  34485. /* test loading RSA key using BIO */
  34486. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  34487. {
  34488. XFILE file;
  34489. const char* fname = "./certs/server-key.der";
  34490. size_t sz;
  34491. byte* buf;
  34492. file = XFOPEN(fname, "rb");
  34493. AssertTrue((file != XBADFILE));
  34494. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  34495. sz = XFTELL(file);
  34496. XREWIND(file);
  34497. AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  34498. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  34499. XFCLOSE(file);
  34500. /* Test using BIO new mem and loading DER private key */
  34501. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  34502. AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  34503. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  34504. BIO_free(bio);
  34505. bio = NULL;
  34506. EVP_PKEY_free(pkey);
  34507. pkey = NULL;
  34508. }
  34509. #endif
  34510. /* test loading ECC key using BIO */
  34511. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  34512. {
  34513. XFILE file;
  34514. const char* fname = "./certs/ecc-key.der";
  34515. size_t sz;
  34516. byte* buf;
  34517. file = XFOPEN(fname, "rb");
  34518. AssertTrue((file != XBADFILE));
  34519. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  34520. sz = XFTELL(file);
  34521. XREWIND(file);
  34522. AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  34523. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  34524. XFCLOSE(file);
  34525. /* Test using BIO new mem and loading DER private key */
  34526. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  34527. AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  34528. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  34529. BIO_free(bio);
  34530. bio = NULL;
  34531. EVP_PKEY_free(pkey);
  34532. pkey = NULL;
  34533. }
  34534. #endif
  34535. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34536. #ifndef NO_WOLFSSL_SERVER
  34537. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34538. #else
  34539. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34540. #endif
  34541. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  34542. !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  34543. {
  34544. RSA* rsa = NULL;
  34545. /* Tests bad parameters */
  34546. AssertNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  34547. /* RSA not set yet, expecting to fail*/
  34548. AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  34549. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  34550. /* set RSA using bio*/
  34551. AssertIntGT(BIO_write(bio, client_key_der_2048,
  34552. sizeof_client_key_der_2048), 0);
  34553. AssertNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  34554. AssertNotNull(rsa);
  34555. AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  34556. /*i2d RSAprivate key tests */
  34557. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  34558. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  34559. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  34560. sizeof_client_key_der_2048);
  34561. bufPtr -= sizeof_client_key_der_2048;
  34562. AssertIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  34563. sizeof_client_key_der_2048), 0);
  34564. bufPtr = NULL;
  34565. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  34566. sizeof_client_key_der_2048);
  34567. AssertNotNull(bufPtr);
  34568. AssertIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  34569. sizeof_client_key_der_2048), 0);
  34570. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  34571. RSA_free(rsa);
  34572. rsa = RSA_new();
  34573. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  34574. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  34575. RSA_free(rsa);
  34576. }
  34577. #endif /* !HAVE_FAST_RSA && WOLFSSL_KEY_GEN && !NO_RSA && !HAVE_USER_RSA*/
  34578. SSL_CTX_free(ctx);
  34579. ctx = NULL;
  34580. BIO_free(bio);
  34581. bio = NULL;
  34582. printf(resultFmt, passed);
  34583. }
  34584. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  34585. #endif /* !NO_BIO */
  34586. static void test_wolfSSL_sk_GENERAL_NAME(void)
  34587. {
  34588. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  34589. !defined(NO_RSA)
  34590. X509* x509;
  34591. GENERAL_NAME* gn;
  34592. unsigned char buf[4096];
  34593. const unsigned char* bufPt;
  34594. int bytes, i;
  34595. XFILE f;
  34596. STACK_OF(GENERAL_NAME)* sk;
  34597. printf(testingFmt, "wolfSSL_sk_GENERAL_NAME()");
  34598. f = XFOPEN(cliCertDerFileExt, "rb");
  34599. AssertTrue((f != XBADFILE));
  34600. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  34601. XFCLOSE(f);
  34602. bufPt = buf;
  34603. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  34604. AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  34605. NID_subject_alt_name, NULL, NULL));
  34606. AssertIntEQ(sk_GENERAL_NAME_num(sk), 1);
  34607. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  34608. AssertNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  34609. switch (gn->type) {
  34610. case GEN_DNS:
  34611. printf("found type GEN_DNS\n");
  34612. break;
  34613. case GEN_EMAIL:
  34614. printf("found type GEN_EMAIL\n");
  34615. break;
  34616. case GEN_URI:
  34617. printf("found type GEN_URI\n");
  34618. break;
  34619. }
  34620. }
  34621. X509_free(x509);
  34622. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  34623. printf(resultFmt, passed);
  34624. #endif
  34625. }
  34626. static void test_wolfSSL_GENERAL_NAME_print(void)
  34627. {
  34628. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  34629. X509* x509;
  34630. GENERAL_NAME* gn;
  34631. unsigned char buf[4096];
  34632. const unsigned char* bufPt;
  34633. int bytes;
  34634. XFILE f;
  34635. STACK_OF(GENERAL_NAME)* sk;
  34636. BIO* out;
  34637. unsigned char outbuf[128];
  34638. X509_EXTENSION* ext;
  34639. AUTHORITY_INFO_ACCESS* aia;
  34640. ACCESS_DESCRIPTION* ad;
  34641. const unsigned char v4Addr[] = {192,168,53,1};
  34642. const unsigned char v6Addr[] =
  34643. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  34644. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  34645. const unsigned char email[] =
  34646. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  34647. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  34648. const char* dnsStr = "DNS:example.com";
  34649. const char* uriStr = "URI:http://127.0.0.1:22220";
  34650. const char* v4addStr = "IP Address:192.168.53.1";
  34651. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  34652. const char* emailStr = "email:info@wolfssl.com";
  34653. const char* othrStr = "othername:<unsupported>";
  34654. const char* x400Str = "X400Name:<unsupported>";
  34655. const char* ediStr = "EdiPartyName:<unsupported>";
  34656. printf(testingFmt, "test_wolfSSL_GENERAL_NAME_print()");
  34657. /* BIO to output */
  34658. AssertNotNull(out = BIO_new(BIO_s_mem()));
  34659. /* test for NULL param */
  34660. gn = NULL;
  34661. AssertIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  34662. AssertIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  34663. AssertIntEQ(GENERAL_NAME_print(out, NULL), 0);
  34664. /* test for GEN_DNS */
  34665. f = XFOPEN(cliCertDerFileExt, "rb");
  34666. AssertTrue((f != XBADFILE));
  34667. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  34668. XFCLOSE(f);
  34669. bufPt = buf;
  34670. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  34671. AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  34672. NID_subject_alt_name, NULL, NULL));
  34673. AssertNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  34674. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  34675. XMEMSET(outbuf,0,sizeof(outbuf));
  34676. BIO_read(out, outbuf, sizeof(outbuf));
  34677. AssertIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  34678. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  34679. X509_free(x509);
  34680. /* test for GEN_URI */
  34681. f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb");
  34682. AssertTrue((f != XBADFILE));
  34683. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  34684. XFCLOSE(f);
  34685. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  34686. aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  34687. AssertNotNull(aia);
  34688. ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0);
  34689. gn = ad->location;
  34690. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  34691. XMEMSET(outbuf,0,sizeof(outbuf));
  34692. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  34693. AssertIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  34694. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  34695. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  34696. AssertNotNull(aia);
  34697. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  34698. X509_free(x509);
  34699. /* test for GEN_IPADD */
  34700. /* ip v4 address */
  34701. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  34702. gn->type = GEN_IPADD;
  34703. gn->d.iPAddress->length = sizeof(v4Addr);
  34704. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  34705. sizeof(v4Addr)), 1);
  34706. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  34707. XMEMSET(outbuf,0,sizeof(outbuf));
  34708. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  34709. AssertIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  34710. GENERAL_NAME_free(gn);
  34711. /* ip v6 address */
  34712. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  34713. gn->type = GEN_IPADD;
  34714. gn->d.iPAddress->length = sizeof(v6Addr);
  34715. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  34716. sizeof(v6Addr)), 1);
  34717. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  34718. XMEMSET(outbuf,0,sizeof(outbuf));
  34719. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  34720. AssertIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  34721. GENERAL_NAME_free(gn);
  34722. /* test for GEN_EMAIL */
  34723. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  34724. gn->type = GEN_EMAIL;
  34725. gn->d.rfc822Name->length = sizeof(email);
  34726. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email,
  34727. sizeof(email)), 1);
  34728. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  34729. XMEMSET(outbuf,0,sizeof(outbuf));
  34730. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  34731. AssertIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  34732. GENERAL_NAME_free(gn);
  34733. /* test for GEN_OTHERNAME */
  34734. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  34735. gn->type = GEN_OTHERNAME;
  34736. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  34737. XMEMSET(outbuf,0,sizeof(outbuf));
  34738. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  34739. AssertIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  34740. GENERAL_NAME_free(gn);
  34741. /* test for GEN_X400 */
  34742. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  34743. gn->type = GEN_X400;
  34744. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  34745. XMEMSET(outbuf,0,sizeof(outbuf));
  34746. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  34747. AssertIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  34748. GENERAL_NAME_free(gn);
  34749. /* test for GEN_EDIPARTY */
  34750. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  34751. gn->type = GEN_EDIPARTY;
  34752. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  34753. XMEMSET(outbuf,0,sizeof(outbuf));
  34754. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  34755. AssertIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  34756. GENERAL_NAME_free(gn);
  34757. BIO_free(out);
  34758. printf(resultFmt, passed);
  34759. #endif /* OPENSSL_ALL */
  34760. }
  34761. static void test_wolfSSL_sk_DIST_POINT(void)
  34762. {
  34763. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  34764. !defined(NO_RSA)
  34765. X509* x509;
  34766. unsigned char buf[4096];
  34767. const unsigned char* bufPt;
  34768. int bytes, i, j;
  34769. XFILE f;
  34770. DIST_POINT* dp;
  34771. GENERAL_NAME* gn;
  34772. ASN1_IA5STRING* uri;
  34773. STACK_OF(DIST_POINT)* dps;
  34774. STACK_OF(GENERAL_NAME)* gns;
  34775. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  34776. printf(testingFmt, "wolfSSL_sk_DIST_POINT()");
  34777. f = XFOPEN(cliCertDerCrlDistPoint, "rb");
  34778. AssertTrue((f != XBADFILE));
  34779. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  34780. XFCLOSE(f);
  34781. bufPt = buf;
  34782. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  34783. AssertNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  34784. NID_crl_distribution_points, NULL, NULL));
  34785. AssertIntEQ(sk_DIST_POINT_num(dps), 1);
  34786. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  34787. AssertNotNull(dp = sk_DIST_POINT_value(dps, i));
  34788. gns = dp->distpoint->name.fullname;
  34789. AssertNotNull(gns);
  34790. AssertIntEQ(sk_GENERAL_NAME_num(gns), 1);
  34791. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  34792. gn = sk_GENERAL_NAME_value(gns, j);
  34793. AssertIntEQ(gn->type, GEN_URI);
  34794. AssertNotNull(uri = gn->d.uniformResourceIdentifier);
  34795. AssertNotNull(uri->data);
  34796. AssertIntGT(uri->length, 0);
  34797. }
  34798. }
  34799. X509_free(x509);
  34800. CRL_DIST_POINTS_free(dps);
  34801. printf(resultFmt, passed);
  34802. #endif
  34803. }
  34804. static void test_wolfSSL_MD4(void)
  34805. {
  34806. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  34807. MD4_CTX md4;
  34808. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  34809. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  34810. "789012345678901234567890";
  34811. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  34812. "\xcc\x05\x36";
  34813. int msgSz = (int)XSTRLEN(msg);
  34814. printf(testingFmt, "wolfSSL_MD4()");
  34815. XMEMSET(out, 0, sizeof(out));
  34816. MD4_Init(&md4);
  34817. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  34818. MD4_Final(out, &md4);
  34819. AssertIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  34820. printf(resultFmt, passed);
  34821. #endif
  34822. }
  34823. static void test_wolfSSL_verify_mode(void)
  34824. {
  34825. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  34826. WOLFSSL* ssl;
  34827. WOLFSSL_CTX* ctx;
  34828. printf(testingFmt, "test_wolfSSL_verify()");
  34829. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34830. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  34831. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  34832. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  34833. AssertNotNull(ssl = SSL_new(ctx));
  34834. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  34835. SSL_free(ssl);
  34836. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  34837. AssertNotNull(ssl = SSL_new(ctx));
  34838. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  34839. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  34840. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  34841. AssertIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  34842. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  34843. SSL_free(ssl);
  34844. wolfSSL_CTX_set_verify(ctx,
  34845. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  34846. AssertNotNull(ssl = SSL_new(ctx));
  34847. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  34848. AssertIntEQ(SSL_get_verify_mode(ssl),
  34849. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  34850. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  34851. AssertIntEQ(SSL_CTX_get_verify_mode(ctx),
  34852. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  34853. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  34854. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  34855. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  34856. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  34857. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  34858. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  34859. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  34860. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  34861. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  34862. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  34863. #endif
  34864. AssertIntEQ(SSL_CTX_get_verify_mode(ctx),
  34865. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  34866. SSL_free(ssl);
  34867. SSL_CTX_free(ctx);
  34868. printf(resultFmt, passed);
  34869. #endif
  34870. }
  34871. static void test_wolfSSL_verify_depth(void)
  34872. {
  34873. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  34874. WOLFSSL* ssl;
  34875. WOLFSSL_CTX* ctx;
  34876. long depth;
  34877. printf(testingFmt, "test_wolfSSL_verify_depth()");
  34878. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34879. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  34880. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  34881. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  34882. AssertIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  34883. AssertNotNull(ssl = SSL_new(ctx));
  34884. AssertIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  34885. SSL_free(ssl);
  34886. SSL_CTX_set_verify_depth(ctx, -1);
  34887. AssertIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  34888. SSL_CTX_set_verify_depth(ctx, 2);
  34889. AssertIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  34890. AssertNotNull(ssl = SSL_new(ctx));
  34891. AssertIntEQ(2, SSL_get_verify_depth(ssl));
  34892. SSL_free(ssl);
  34893. SSL_CTX_free(ctx);
  34894. printf(resultFmt, passed);
  34895. #endif
  34896. }
  34897. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  34898. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  34899. * buffer of 64 bytes.
  34900. *
  34901. * returns the size of the digest buffer on success and a negative value on
  34902. * failure.
  34903. */
  34904. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest)
  34905. {
  34906. HMAC_CTX ctx1;
  34907. HMAC_CTX ctx2;
  34908. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  34909. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  34910. unsigned char long_key[] =
  34911. "0123456789012345678901234567890123456789"
  34912. "0123456789012345678901234567890123456789"
  34913. "0123456789012345678901234567890123456789"
  34914. "0123456789012345678901234567890123456789";
  34915. unsigned char msg[] = "message to hash";
  34916. unsigned int digestSz = 64;
  34917. int keySz = sizeof(key);
  34918. int long_keySz = sizeof(long_key);
  34919. int msgSz = sizeof(msg);
  34920. unsigned char digest2[64];
  34921. unsigned int digestSz2 = 64;
  34922. HMAC_CTX_init(&ctx1);
  34923. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  34924. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  34925. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  34926. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  34927. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  34928. HMAC_CTX_cleanup(&ctx1);
  34929. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  34930. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  34931. HMAC_CTX_cleanup(&ctx2);
  34932. AssertIntEQ(digestSz, digestSz2);
  34933. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  34934. /* test HMAC_Init with NULL key */
  34935. /* init after copy */
  34936. printf("test HMAC_Init with NULL key (0)\n");
  34937. HMAC_CTX_init(&ctx1);
  34938. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  34939. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  34940. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  34941. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  34942. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  34943. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  34944. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  34945. HMAC_CTX_cleanup(&ctx1);
  34946. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  34947. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  34948. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  34949. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  34950. HMAC_CTX_cleanup(&ctx2);
  34951. AssertIntEQ(digestSz, digestSz2);
  34952. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  34953. /* long key */
  34954. printf("test HMAC_Init with NULL key (1)\n");
  34955. HMAC_CTX_init(&ctx1);
  34956. AssertIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type), SSL_SUCCESS);
  34957. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  34958. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  34959. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  34960. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  34961. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  34962. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  34963. HMAC_CTX_cleanup(&ctx1);
  34964. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  34965. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  34966. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  34967. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  34968. HMAC_CTX_cleanup(&ctx2);
  34969. AssertIntEQ(digestSz, digestSz2);
  34970. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  34971. /* init before copy */
  34972. printf("test HMAC_Init with NULL key (2)\n");
  34973. HMAC_CTX_init(&ctx1);
  34974. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  34975. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  34976. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  34977. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  34978. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  34979. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  34980. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  34981. HMAC_CTX_cleanup(&ctx1);
  34982. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  34983. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  34984. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  34985. HMAC_CTX_cleanup(&ctx2);
  34986. AssertIntEQ(digestSz, digestSz2);
  34987. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  34988. return digestSz;
  34989. }
  34990. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  34991. static void test_wolfSSL_HMAC_CTX(void)
  34992. {
  34993. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  34994. unsigned char digest[64];
  34995. int digestSz;
  34996. printf(testingFmt, "wolfSSL_HMAC_CTX()");
  34997. #ifndef NO_SHA
  34998. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha1(), digest)), 20);
  34999. AssertIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  35000. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  35001. #endif /* !NO_SHA */
  35002. #ifdef WOLFSSL_SHA224
  35003. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha224(), digest)), 28);
  35004. AssertIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  35005. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  35006. "\x02\x0E", digest, digestSz), 0);
  35007. #endif /* WOLFSSL_SHA224 */
  35008. #ifndef NO_SHA256
  35009. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha256(), digest)), 32);
  35010. AssertIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  35011. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  35012. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  35013. #endif /* !NO_SHA256 */
  35014. #ifdef WOLFSSL_SHA384
  35015. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha384(), digest)), 48);
  35016. AssertIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  35017. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  35018. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  35019. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  35020. digest, digestSz), 0);
  35021. #endif /* WOLFSSL_SHA384 */
  35022. #ifdef WOLFSSL_SHA512
  35023. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha512(), digest)), 64);
  35024. AssertIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  35025. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  35026. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  35027. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  35028. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  35029. digest, digestSz), 0);
  35030. #endif /* WOLFSSL_SHA512 */
  35031. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || HAVE_FIPS_VERSION <= 2)
  35032. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_md5(), digest)), 16);
  35033. AssertIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  35034. "\xE4\x98\xDD", digest, digestSz), 0);
  35035. #endif /* !NO_MD5 */
  35036. printf(resultFmt, passed);
  35037. #endif
  35038. }
  35039. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  35040. static void sslMsgCb(int w, int version, int type, const void* buf,
  35041. size_t sz, SSL* ssl, void* arg)
  35042. {
  35043. int i;
  35044. unsigned char* pt = (unsigned char*)buf;
  35045. printf("%s %d bytes of version %d , type %d : ", (w)?"Writing":"Reading",
  35046. (int)sz, version, type);
  35047. for (i = 0; i < (int)sz; i++) printf("%02X", pt[i]);
  35048. printf("\n");
  35049. (void)ssl;
  35050. (void)arg;
  35051. }
  35052. #endif /* OPENSSL_EXTRA */
  35053. static void test_wolfSSL_msg_callback(void)
  35054. {
  35055. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  35056. WOLFSSL* ssl;
  35057. WOLFSSL_CTX* ctx;
  35058. printf(testingFmt, "wolfSSL_msg_callback()");
  35059. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35060. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  35061. SSL_FILETYPE_PEM));
  35062. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  35063. SSL_FILETYPE_PEM));
  35064. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  35065. SSL_SUCCESS);
  35066. AssertNotNull(ssl = SSL_new(ctx));
  35067. AssertIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  35068. AssertIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  35069. AssertIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  35070. SSL_free(ssl);
  35071. SSL_CTX_free(ctx);
  35072. printf(resultFmt, passed);
  35073. #endif
  35074. }
  35075. static void test_wolfSSL_SHA(void)
  35076. {
  35077. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  35078. printf(testingFmt, "wolfSSL_SHA()");
  35079. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  35080. (!defined(HAVE_FIPS) || \
  35081. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  35082. {
  35083. const unsigned char in[] = "abc";
  35084. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  35085. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  35086. unsigned char out[WC_SHA_DIGEST_SIZE];
  35087. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  35088. AssertNotNull(SHA1(in, XSTRLEN((char*)in), out));
  35089. AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  35090. /* SHA interface test */
  35091. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  35092. AssertNull(SHA(NULL, XSTRLEN((char*)in), out));
  35093. AssertNotNull(SHA(in, 0, out));
  35094. AssertNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  35095. AssertNotNull(SHA(NULL, 0, out));
  35096. AssertNotNull(SHA(NULL, 0, NULL));
  35097. AssertNotNull(SHA(in, XSTRLEN((char*)in), out));
  35098. AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  35099. }
  35100. #endif
  35101. #if !defined(NO_SHA256)
  35102. {
  35103. const unsigned char in[] = "abc";
  35104. unsigned char expected[] = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  35105. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  35106. "\x15\xAD";
  35107. unsigned char out[WC_SHA256_DIGEST_SIZE];
  35108. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  35109. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  35110. AssertNotNull(SHA256(in, XSTRLEN((char*)in), out));
  35111. #else
  35112. AssertNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  35113. #endif
  35114. AssertIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  35115. }
  35116. #endif
  35117. #if defined(WOLFSSL_SHA384)
  35118. {
  35119. const unsigned char in[] = "abc";
  35120. unsigned char expected[] = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  35121. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  35122. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  35123. "\xc8\x25\xa7";
  35124. unsigned char out[WC_SHA384_DIGEST_SIZE];
  35125. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  35126. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  35127. AssertNotNull(SHA384(in, XSTRLEN((char*)in), out));
  35128. #else
  35129. AssertNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  35130. #endif
  35131. AssertIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  35132. }
  35133. #endif
  35134. #if defined(WOLFSSL_SHA512)
  35135. {
  35136. const unsigned char in[] = "abc";
  35137. unsigned char expected[] = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  35138. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  35139. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  35140. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  35141. "\xa5\x4c\xa4\x9f";
  35142. unsigned char out[WC_SHA512_DIGEST_SIZE];
  35143. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  35144. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  35145. AssertNotNull(SHA512(in, XSTRLEN((char*)in), out));
  35146. #else
  35147. AssertNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  35148. #endif
  35149. AssertIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  35150. }
  35151. #endif
  35152. printf(resultFmt, passed);
  35153. #endif
  35154. }
  35155. static void test_wolfSSL_DH_1536_prime(void)
  35156. {
  35157. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  35158. BIGNUM* bn;
  35159. unsigned char bits[200];
  35160. int sz = 192; /* known binary size */
  35161. const byte expected[] = {
  35162. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  35163. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  35164. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  35165. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  35166. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  35167. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  35168. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  35169. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  35170. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  35171. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  35172. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  35173. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  35174. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  35175. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  35176. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  35177. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  35178. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  35179. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  35180. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  35181. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  35182. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  35183. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  35184. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  35185. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  35186. };
  35187. printf(testingFmt, "wolfSSL_DH_1536_prime()");
  35188. bn = get_rfc3526_prime_1536(NULL);
  35189. AssertNotNull(bn);
  35190. AssertIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  35191. AssertIntEQ(0, XMEMCMP(expected, bits, sz));
  35192. BN_free(bn);
  35193. printf(resultFmt, passed);
  35194. #endif
  35195. }
  35196. static void test_wolfSSL_DH_get_2048_256(void)
  35197. {
  35198. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  35199. WOLFSSL_DH* dh;
  35200. const WOLFSSL_BIGNUM* pBn;
  35201. const WOLFSSL_BIGNUM* gBn;
  35202. const WOLFSSL_BIGNUM* qBn;
  35203. const byte pExpected[] = {
  35204. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  35205. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  35206. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  35207. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  35208. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  35209. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  35210. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  35211. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  35212. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  35213. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  35214. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  35215. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  35216. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  35217. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  35218. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  35219. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  35220. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  35221. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  35222. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  35223. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  35224. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  35225. 0x1E, 0x1A, 0x15, 0x97
  35226. };
  35227. const byte gExpected[] = {
  35228. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  35229. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  35230. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  35231. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  35232. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  35233. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  35234. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  35235. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  35236. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  35237. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  35238. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  35239. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  35240. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  35241. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  35242. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  35243. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  35244. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  35245. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  35246. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  35247. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  35248. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  35249. 0x6C, 0xC4, 0x16, 0x59
  35250. };
  35251. const byte qExpected[] = {
  35252. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  35253. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  35254. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  35255. };
  35256. int pSz;
  35257. int qSz;
  35258. int gSz;
  35259. byte* pReturned;
  35260. byte* qReturned;
  35261. byte* gReturned;
  35262. printf(testingFmt, "wolfSSL_DH_get_2048_256()");
  35263. AssertNotNull((dh = wolfSSL_DH_get_2048_256()));
  35264. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  35265. AssertIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  35266. AssertNotNull(pReturned = (byte*)XMALLOC(pSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  35267. AssertIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  35268. AssertIntEQ(pSz, sizeof(pExpected));
  35269. AssertIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  35270. AssertIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  35271. AssertNotNull(qReturned = (byte*)XMALLOC(qSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  35272. AssertIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  35273. AssertIntEQ(qSz, sizeof(qExpected));
  35274. AssertIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  35275. AssertIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  35276. AssertNotNull(gReturned = (byte*)XMALLOC(gSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  35277. AssertIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  35278. AssertIntEQ(gSz, sizeof(gExpected));
  35279. AssertIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  35280. wolfSSL_DH_free(dh);
  35281. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  35282. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  35283. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  35284. printf(resultFmt, passed);
  35285. #endif
  35286. }
  35287. static void test_wolfSSL_PEM_write_DHparams(void)
  35288. {
  35289. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  35290. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  35291. DH* dh;
  35292. BIO* bio;
  35293. XFILE fp;
  35294. byte pem[2048];
  35295. int pemSz;
  35296. const char expected[] =
  35297. "-----BEGIN DH PARAMETERS-----\n\
  35298. MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n\
  35299. v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n\
  35300. nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n\
  35301. joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n\
  35302. wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n\
  35303. tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n\
  35304. -----END DH PARAMETERS-----\n";
  35305. printf(testingFmt, "wolfSSL_PEM_write_DHparams()");
  35306. AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
  35307. AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  35308. XFCLOSE(fp);
  35309. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  35310. AssertIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  35311. AssertNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  35312. BIO_free(bio);
  35313. AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  35314. AssertIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  35315. AssertIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  35316. XFCLOSE(fp);
  35317. DH_free(dh);
  35318. /* check results */
  35319. XMEMSET(pem, 0, sizeof(pem));
  35320. AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "rb"));
  35321. AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  35322. AssertIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  35323. XFCLOSE(fp);
  35324. printf(resultFmt, passed);
  35325. #endif
  35326. }
  35327. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  35328. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  35329. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  35330. static void binary_dump(void *ptr, int size)
  35331. {
  35332. #ifdef WOLFSSL_EVP_PRINT
  35333. int i = 0;
  35334. unsigned char *p = (unsigned char *) ptr;
  35335. printf("{");
  35336. while((p != NULL) && (i < size)) {
  35337. if((i % 8) == 0) {
  35338. printf("\n");
  35339. printf(" ");
  35340. }
  35341. printf("0x%02x, ", p[i]);
  35342. i++;
  35343. }
  35344. printf("\n};\n");
  35345. #else
  35346. (void) ptr;
  35347. (void) size;
  35348. #endif
  35349. }
  35350. static int last_val = 0x0f;
  35351. static int check_result(unsigned char *data, int len)
  35352. {
  35353. int i;
  35354. for( ; len; ) {
  35355. last_val = (last_val + 1) % 16;
  35356. for(i = 0; i < 16; len--, i++, data++)
  35357. if(*data != last_val) {
  35358. return -1;
  35359. }
  35360. }
  35361. return 0;
  35362. }
  35363. static int r_offset;
  35364. static int w_offset;
  35365. static void init_offset(void)
  35366. {
  35367. r_offset = 0;
  35368. w_offset = 0;
  35369. }
  35370. static void get_record(unsigned char *data, unsigned char *buf, int len)
  35371. {
  35372. XMEMCPY(buf, data+r_offset, len);
  35373. r_offset += len;
  35374. }
  35375. static void set_record(unsigned char *data, unsigned char *buf, int len)
  35376. {
  35377. XMEMCPY(data+w_offset, buf, len);
  35378. w_offset += len;
  35379. }
  35380. static void set_plain(unsigned char *plain, int rec)
  35381. {
  35382. int i, j;
  35383. unsigned char *p = plain;
  35384. #define BLOCKSZ 16
  35385. for(i=0; i<(rec/BLOCKSZ); i++){
  35386. for(j=0; j<BLOCKSZ; j++)
  35387. *p++ = (i % 16);
  35388. }
  35389. }
  35390. #endif
  35391. static int test_wolfSSL_EVP_Cipher_extra(void)
  35392. {
  35393. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  35394. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  35395. /* aes128-cbc, keylen=16, ivlen=16 */
  35396. byte aes128_cbc_key[] = {
  35397. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  35398. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  35399. };
  35400. byte aes128_cbc_iv[] = {
  35401. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  35402. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  35403. };
  35404. /* teset data size table */
  35405. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  35406. int test_drive2[] = {8, 3, 8, 512, 0};
  35407. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  35408. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  35409. int test_drive_len[100];
  35410. int ret = 0;
  35411. EVP_CIPHER_CTX *evp = NULL;
  35412. int ilen = 0;
  35413. int klen = 0;
  35414. int i, j;
  35415. const EVP_CIPHER *type;
  35416. byte *iv;
  35417. byte *key;
  35418. int ivlen;
  35419. int keylen;
  35420. #define RECORDS 16
  35421. #define BUFFSZ 512
  35422. byte plain [BUFFSZ * RECORDS];
  35423. byte cipher[BUFFSZ * RECORDS];
  35424. byte inb[BUFFSZ];
  35425. byte outb[BUFFSZ+16];
  35426. int outl, inl;
  35427. iv = aes128_cbc_iv;
  35428. ivlen = sizeof(aes128_cbc_iv);
  35429. key = aes128_cbc_key;
  35430. keylen = sizeof(aes128_cbc_key);
  35431. type = EVP_aes_128_cbc();
  35432. set_plain(plain, BUFFSZ * RECORDS);
  35433. SSL_library_init();
  35434. AssertNotNull(evp = EVP_CIPHER_CTX_new());
  35435. AssertIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  35436. klen = EVP_CIPHER_CTX_key_length(evp);
  35437. if (klen > 0 && keylen != klen) {
  35438. AssertIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  35439. }
  35440. ilen = EVP_CIPHER_CTX_iv_length(evp);
  35441. if (ilen > 0 && ivlen != ilen) {
  35442. AssertIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  35443. }
  35444. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  35445. for (j = 0; j<RECORDS; j++)
  35446. {
  35447. inl = BUFFSZ;
  35448. get_record(plain, inb, inl);
  35449. AssertIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  35450. set_record(cipher, outb, outl);
  35451. }
  35452. for (i = 0; test_drive[i]; i++) {
  35453. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  35454. init_offset();
  35455. test_drive_len[i] = 0;
  35456. for (j = 0; test_drive[i][j]; j++)
  35457. {
  35458. inl = test_drive[i][j];
  35459. test_drive_len[i] += inl;
  35460. get_record(plain, inb, inl);
  35461. AssertIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)), 0);
  35462. /* output to cipher buffer, so that following Dec test can detect
  35463. if any error */
  35464. set_record(cipher, outb, outl);
  35465. }
  35466. EVP_CipherFinal(evp, outb, &outl);
  35467. if(outl > 0)
  35468. set_record(cipher, outb, outl);
  35469. }
  35470. for (i = 0; test_drive[i]; i++) {
  35471. last_val = 0x0f;
  35472. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  35473. init_offset();
  35474. for (j = 0; test_drive[i][j]; j++){
  35475. inl = test_drive[i][j];
  35476. get_record(cipher, inb, inl);
  35477. AssertIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)), 0);
  35478. binary_dump(outb, outl);
  35479. AssertIntEQ((ret = check_result(outb, outl)), 0);
  35480. AssertFalse(outl > ((inl/16+1)*16) && outl > 16);
  35481. }
  35482. ret = EVP_CipherFinal(evp, outb, &outl);
  35483. binary_dump(outb, outl);
  35484. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  35485. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  35486. AssertTrue(ret);
  35487. }
  35488. EVP_CIPHER_CTX_free(evp);
  35489. #endif /* test_EVP_Cipher */
  35490. return 0;
  35491. }
  35492. static void test_wolfSSL_PEM_read_DHparams(void)
  35493. {
  35494. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  35495. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  35496. DH* dh;
  35497. XFILE fp;
  35498. unsigned char derOut[300];
  35499. unsigned char* derOutBuf = derOut;
  35500. int derOutSz = 0;
  35501. unsigned char derExpected[300];
  35502. int derExpectedSz = 0;
  35503. printf(testingFmt, "wolfSSL_PEM_read_DHparams()");
  35504. XMEMSET(derOut, 0, sizeof(derOut));
  35505. XMEMSET(derExpected, 0, sizeof(derExpected));
  35506. /* open DH param file, read into DH struct */
  35507. AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
  35508. /* bad args */
  35509. AssertNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  35510. AssertNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  35511. /* good args */
  35512. AssertNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  35513. XFCLOSE(fp);
  35514. /* read in certs/dh2048.der for comparison against exported params */
  35515. fp = XFOPEN("./certs/dh2048.der", "rb");
  35516. AssertTrue(fp != XBADFILE);
  35517. derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected), fp);
  35518. XFCLOSE(fp);
  35519. /* export DH back to DER and compare */
  35520. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  35521. AssertIntEQ(derOutSz, derExpectedSz);
  35522. AssertIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  35523. /* Test parsing with X9.42 header */
  35524. fp = XFOPEN("./certs/x942dh2048.pem", "rb");
  35525. AssertNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  35526. XFCLOSE(fp);
  35527. DH_free(dh);
  35528. printf(resultFmt, passed);
  35529. #endif
  35530. }
  35531. static void test_wolfSSL_AES_ecb_encrypt(void)
  35532. {
  35533. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB)
  35534. AES_KEY aes;
  35535. const byte msg[] =
  35536. {
  35537. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  35538. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  35539. };
  35540. const byte verify[] =
  35541. {
  35542. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  35543. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  35544. };
  35545. const byte key[] =
  35546. {
  35547. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  35548. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  35549. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  35550. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  35551. };
  35552. byte out[AES_BLOCK_SIZE];
  35553. printf(testingFmt, "wolfSSL_AES_ecb_encrypt()");
  35554. AssertIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  35555. XMEMSET(out, 0, AES_BLOCK_SIZE);
  35556. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  35557. AssertIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  35558. #ifdef HAVE_AES_DECRYPT
  35559. AssertIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  35560. XMEMSET(out, 0, AES_BLOCK_SIZE);
  35561. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  35562. AssertIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  35563. #endif
  35564. /* test bad arguments */
  35565. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  35566. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  35567. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  35568. printf(resultFmt, passed);
  35569. #endif
  35570. }
  35571. static void test_wolfSSL_MD5(void)
  35572. {
  35573. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  35574. byte input1[] = "";
  35575. byte input2[] = "message digest";
  35576. byte hash[WC_MD5_DIGEST_SIZE];
  35577. unsigned char output1[] =
  35578. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  35579. unsigned char output2[] =
  35580. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  35581. WOLFSSL_MD5_CTX md5;
  35582. printf(testingFmt, "wolfSSL_MD5()");
  35583. XMEMSET(&md5, 0, sizeof(md5));
  35584. /* Test cases for illegal parameters */
  35585. AssertIntEQ(MD5_Init(NULL), 0);
  35586. AssertIntEQ(MD5_Init(&md5), 1);
  35587. AssertIntEQ(MD5_Update(NULL, input1, 0), 0);
  35588. AssertIntEQ(MD5_Update(NULL, NULL, 0), 0);
  35589. AssertIntEQ(MD5_Update(&md5, NULL, 1), 0);
  35590. AssertIntEQ(MD5_Final(NULL, &md5), 0);
  35591. AssertIntEQ(MD5_Final(hash, NULL), 0);
  35592. AssertIntEQ(MD5_Final(NULL, NULL), 0);
  35593. /* Init MD5 CTX */
  35594. AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
  35595. AssertIntEQ(wolfSSL_MD5_Update(&md5, input1,
  35596. XSTRLEN((const char*)&input1)), 1);
  35597. AssertIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  35598. AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  35599. /* Init MD5 CTX */
  35600. AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
  35601. AssertIntEQ(wolfSSL_MD5_Update(&md5, input2,
  35602. (int)XSTRLEN((const char*)input2)), 1);
  35603. AssertIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  35604. AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  35605. #if !defined(NO_OLD_NAMES) && \
  35606. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  35607. AssertPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  35608. AssertPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  35609. AssertPtrNE(MD5(input1, 1, NULL), NULL);
  35610. AssertPtrNE(MD5(NULL, 0, NULL), NULL);
  35611. AssertPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash), &hash);
  35612. AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  35613. AssertPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash), &hash);
  35614. AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  35615. {
  35616. byte data[] = "Data to be hashed.";
  35617. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  35618. AssertNotNull(MD5(data, sizeof(data), NULL));
  35619. AssertNotNull(MD5(data, sizeof(data), hash));
  35620. AssertNotNull(MD5(NULL, 0, hash));
  35621. AssertNull(MD5(NULL, sizeof(data), hash));
  35622. }
  35623. #endif
  35624. printf(resultFmt, passed);
  35625. #endif
  35626. }
  35627. static void test_wolfSSL_MD5_Transform(void)
  35628. {
  35629. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  35630. byte input1[] = "";
  35631. byte input2[] = "abc";
  35632. byte local[WC_MD5_BLOCK_SIZE];
  35633. word32 sLen = 0;
  35634. #ifdef BIG_ENDIAN_ORDER
  35635. unsigned char output1[] =
  35636. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  35637. unsigned char output2[] =
  35638. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  35639. #else
  35640. unsigned char output1[] =
  35641. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  35642. unsigned char output2[] =
  35643. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  35644. #endif
  35645. union {
  35646. wc_Md5 native;
  35647. MD5_CTX compat;
  35648. } md5;
  35649. printf(testingFmt, "wolfSSL_MD5_Transform()");
  35650. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  35651. XMEMSET(&local, 0, sizeof(local));
  35652. /* sanity check */
  35653. AssertIntEQ(MD5_Transform(NULL, NULL), 0);
  35654. AssertIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  35655. AssertIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  35656. AssertIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  35657. AssertIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  35658. AssertIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  35659. /* Init MD5 CTX */
  35660. AssertIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  35661. /* Do Transform*/
  35662. sLen = (word32)XSTRLEN((char*)input1);
  35663. XMEMCPY(local, input1, sLen);
  35664. AssertIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  35665. AssertIntEQ(XMEMCMP(md5.native.digest, output1,
  35666. WC_MD5_DIGEST_SIZE), 0);
  35667. /* Init MD5 CTX */
  35668. AssertIntEQ(MD5_Init(&md5.compat), 1);
  35669. sLen = (word32)XSTRLEN((char*)input2);
  35670. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  35671. XMEMCPY(local, input2, sLen);
  35672. AssertIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  35673. AssertIntEQ(XMEMCMP(md5.native.digest, output2,
  35674. WC_MD5_DIGEST_SIZE), 0);
  35675. printf(resultFmt, passed);
  35676. #endif
  35677. }
  35678. static void test_wolfSSL_SHA224(void)
  35679. {
  35680. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  35681. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  35682. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  35683. unsigned char input[] =
  35684. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  35685. unsigned char output[] =
  35686. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  35687. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  35688. size_t inLen;
  35689. byte hash[WC_SHA224_DIGEST_SIZE];
  35690. printf(testingFmt, "wolfSSL_SHA224()");
  35691. inLen = XSTRLEN((char*)input);
  35692. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  35693. AssertNull(SHA224(NULL, inLen, hash));
  35694. AssertNotNull(SHA224(input, 0, hash));
  35695. AssertNotNull(SHA224(input, inLen, NULL));
  35696. AssertNotNull(SHA224(NULL, 0, hash));
  35697. AssertNotNull(SHA224(NULL, 0, NULL));
  35698. AssertNotNull(SHA224(input, inLen, hash));
  35699. AssertIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  35700. printf(resultFmt, passed);
  35701. #endif
  35702. }
  35703. static void test_wolfSSL_SHA_Transform(void)
  35704. {
  35705. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  35706. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  35707. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  35708. byte input1[] = "";
  35709. byte input2[] = "abc";
  35710. byte local[WC_SHA_BLOCK_SIZE];
  35711. word32 sLen = 0;
  35712. #ifdef BIG_ENDIAN_ORDER
  35713. unsigned char output1[] =
  35714. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  35715. "\x09\xf7\x3a\x93";
  35716. unsigned char output2[] =
  35717. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  35718. "\x7c\x6e\x08\xb8";
  35719. #else
  35720. unsigned char output1[] =
  35721. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  35722. "\x93\x3a\xf7\x09";
  35723. unsigned char output2[] =
  35724. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  35725. "\xb8\x08\x6e\x7c";
  35726. #endif
  35727. union {
  35728. wc_Sha native;
  35729. SHA_CTX compat;
  35730. } sha;
  35731. union {
  35732. wc_Sha native;
  35733. SHA_CTX compat;
  35734. } sha1;
  35735. printf(testingFmt, "wolfSSL_SHA_Transform()");
  35736. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  35737. XMEMSET(&local, 0, sizeof(local));
  35738. /* sanity check */
  35739. AssertIntEQ(SHA_Transform(NULL, NULL), 0);
  35740. AssertIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  35741. AssertIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  35742. AssertIntEQ(SHA1_Transform(NULL, NULL), 0);
  35743. AssertIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  35744. AssertIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  35745. AssertIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  35746. AssertIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  35747. AssertIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  35748. /* Init SHA CTX */
  35749. AssertIntEQ(SHA_Init(&sha.compat), 1);
  35750. /* Do Transform*/
  35751. sLen = (word32)XSTRLEN((char*)input1);
  35752. XMEMCPY(local, input1, sLen);
  35753. AssertIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  35754. AssertIntEQ(XMEMCMP(sha.native.digest, output1,
  35755. WC_SHA_DIGEST_SIZE), 0);
  35756. AssertIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  35757. /* Init SHA CTX */
  35758. AssertIntEQ(SHA_Init(&sha.compat), 1);
  35759. sLen = (word32)XSTRLEN((char*)input2);
  35760. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  35761. XMEMCPY(local, input2, sLen);
  35762. AssertIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  35763. AssertIntEQ(XMEMCMP(sha.native.digest, output2,
  35764. WC_SHA_DIGEST_SIZE), 0);
  35765. AssertIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  35766. /* SHA1 */
  35767. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  35768. /* Init SHA CTX */
  35769. AssertIntEQ(SHA1_Init(&sha1.compat), 1);
  35770. /* Do Transform*/
  35771. sLen = (word32)XSTRLEN((char*)input1);
  35772. XMEMCPY(local, input1, sLen);
  35773. AssertIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  35774. AssertIntEQ(XMEMCMP(sha1.native.digest, output1,
  35775. WC_SHA_DIGEST_SIZE), 0);
  35776. AssertIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  35777. /* Init SHA CTX */
  35778. AssertIntEQ(SHA1_Init(&sha1.compat), 1);
  35779. sLen = (word32)XSTRLEN((char*)input2);
  35780. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  35781. XMEMCPY(local, input2, sLen);
  35782. AssertIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  35783. AssertIntEQ(XMEMCMP(sha1.native.digest, output2,
  35784. WC_SHA_DIGEST_SIZE), 0);
  35785. AssertIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  35786. printf(resultFmt, passed);
  35787. #endif
  35788. #endif
  35789. }
  35790. static void test_wolfSSL_SHA256_Transform(void)
  35791. {
  35792. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  35793. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  35794. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  35795. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH)
  35796. byte input1[] = "";
  35797. byte input2[] = "abc";
  35798. byte local[WC_SHA256_BLOCK_SIZE];
  35799. word32 sLen = 0;
  35800. #ifdef BIG_ENDIAN_ORDER
  35801. unsigned char output1[] =
  35802. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  35803. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  35804. unsigned char output2[] =
  35805. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  35806. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  35807. #else
  35808. unsigned char output1[] =
  35809. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  35810. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  35811. unsigned char output2[] =
  35812. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  35813. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  35814. #endif
  35815. union {
  35816. wc_Sha256 native;
  35817. SHA256_CTX compat;
  35818. } sha256;
  35819. printf(testingFmt, "wolfSSL_SHA256_Transform()");
  35820. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  35821. XMEMSET(&local, 0, sizeof(local));
  35822. /* sanity check */
  35823. AssertIntEQ(SHA256_Transform(NULL, NULL), 0);
  35824. AssertIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  35825. AssertIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  35826. AssertIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  35827. AssertIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  35828. AssertIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  35829. /* Init SHA256 CTX */
  35830. AssertIntEQ(SHA256_Init(&sha256.compat), 1);
  35831. /* Do Transform*/
  35832. sLen = (word32)XSTRLEN((char*)input1);
  35833. XMEMCPY(local, input1, sLen);
  35834. AssertIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  35835. AssertIntEQ(XMEMCMP(sha256.native.digest, output1,
  35836. WC_SHA256_DIGEST_SIZE), 0);
  35837. AssertIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  35838. /* Init SHA256 CTX */
  35839. AssertIntEQ(SHA256_Init(&sha256.compat), 1);
  35840. sLen = (word32)XSTRLEN((char*)input2);
  35841. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  35842. XMEMCPY(local, input2, sLen);
  35843. AssertIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  35844. AssertIntEQ(XMEMCMP(sha256.native.digest, output2,
  35845. WC_SHA256_DIGEST_SIZE), 0);
  35846. AssertIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  35847. printf(resultFmt, passed);
  35848. #endif
  35849. #endif
  35850. }
  35851. static void test_wolfSSL_SHA256(void)
  35852. {
  35853. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  35854. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  35855. unsigned char input[] =
  35856. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  35857. unsigned char output[] =
  35858. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  35859. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  35860. "\x06\xC1";
  35861. size_t inLen;
  35862. byte hash[WC_SHA256_DIGEST_SIZE];
  35863. printf(testingFmt, "wolfSSL_SHA256()");
  35864. inLen = XSTRLEN((char*)input);
  35865. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  35866. AssertNotNull(SHA256(input, inLen, hash));
  35867. AssertIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  35868. printf(resultFmt, passed);
  35869. #endif
  35870. }
  35871. static void test_wolfSSL_SHA512_Transform(void)
  35872. {
  35873. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  35874. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  35875. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  35876. byte input1[] = "";
  35877. byte input2[] = "abc";
  35878. byte local[WC_SHA512_BLOCK_SIZE];
  35879. word32 sLen = 0;
  35880. #ifdef BIG_ENDIAN_ORDER
  35881. unsigned char output1[] =
  35882. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  35883. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  35884. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  35885. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  35886. unsigned char output2[] =
  35887. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  35888. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  35889. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  35890. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  35891. #else
  35892. unsigned char output1[] =
  35893. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  35894. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  35895. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  35896. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  35897. unsigned char output2[] =
  35898. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  35899. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  35900. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  35901. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  35902. #endif
  35903. union {
  35904. wc_Sha512 native;
  35905. SHA512_CTX compat;
  35906. } sha512;
  35907. printf(testingFmt, "wolfSSL_SHA512_Transform()");
  35908. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  35909. XMEMSET(&local, 0, sizeof(local));
  35910. /* sanity check */
  35911. AssertIntEQ(SHA512_Transform(NULL, NULL), 0);
  35912. AssertIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  35913. AssertIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  35914. AssertIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  35915. AssertIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  35916. AssertIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  35917. /* Init SHA512 CTX */
  35918. AssertIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  35919. /* Do Transform*/
  35920. sLen = (word32)XSTRLEN((char*)input1);
  35921. XMEMCPY(local, input1, sLen);
  35922. AssertIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  35923. AssertIntEQ(XMEMCMP(sha512.native.digest, output1,
  35924. WC_SHA512_DIGEST_SIZE), 0);
  35925. AssertIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  35926. /* Init SHA512 CTX */
  35927. AssertIntEQ(SHA512_Init(&sha512.compat), 1);
  35928. sLen = (word32)XSTRLEN((char*)input2);
  35929. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  35930. XMEMCPY(local, input2, sLen);
  35931. AssertIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  35932. AssertIntEQ(XMEMCMP(sha512.native.digest, output2,
  35933. WC_SHA512_DIGEST_SIZE), 0);
  35934. AssertIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  35935. (void)input1;
  35936. printf(resultFmt, passed);
  35937. #endif
  35938. #endif
  35939. }
  35940. static void test_wolfSSL_X509_get_serialNumber(void)
  35941. {
  35942. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  35943. ASN1_INTEGER* a;
  35944. BIGNUM* bn;
  35945. X509* x509;
  35946. char *serialHex;
  35947. byte serial[3];
  35948. int serialSz;
  35949. printf(testingFmt, "wolfSSL_X509_get_serialNumber()");
  35950. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  35951. SSL_FILETYPE_PEM));
  35952. AssertNotNull(a = X509_get_serialNumber(x509));
  35953. /* check on value of ASN1 Integer */
  35954. AssertNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  35955. /* test setting serial number and then retrieving it */
  35956. AssertNotNull(a = ASN1_INTEGER_new());
  35957. ASN1_INTEGER_set(a, 3);
  35958. AssertIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  35959. serialSz = sizeof(serial);
  35960. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  35961. WOLFSSL_SUCCESS);
  35962. AssertIntEQ(serialSz, 1);
  35963. AssertIntEQ(serial[0], 3);
  35964. ASN1_INTEGER_free(a);
  35965. /* test setting serial number with 0's in it */
  35966. serial[0] = 0x01;
  35967. serial[1] = 0x00;
  35968. serial[2] = 0x02;
  35969. AssertNotNull(a = wolfSSL_ASN1_INTEGER_new());
  35970. a->data[0] = ASN_INTEGER;
  35971. a->data[1] = sizeof(serial);
  35972. XMEMCPY(&a->data[2], serial, sizeof(serial));
  35973. a->length = sizeof(serial) + 2;
  35974. AssertIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  35975. XMEMSET(serial, 0, sizeof(serial));
  35976. serialSz = sizeof(serial);
  35977. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  35978. WOLFSSL_SUCCESS);
  35979. AssertIntEQ(serialSz, 3);
  35980. AssertIntEQ(serial[0], 0x01);
  35981. AssertIntEQ(serial[1], 0x00);
  35982. AssertIntEQ(serial[2], 0x02);
  35983. ASN1_INTEGER_free(a);
  35984. X509_free(x509); /* free's a */
  35985. AssertNotNull(serialHex = BN_bn2hex(bn));
  35986. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  35987. AssertStrEQ(serialHex, "01");
  35988. #else
  35989. AssertStrEQ(serialHex, "1");
  35990. #endif
  35991. OPENSSL_free(serialHex);
  35992. AssertIntEQ(BN_get_word(bn), 1);
  35993. BN_free(bn);
  35994. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  35995. a = ASN1_INTEGER_new();
  35996. if (a) {
  35997. AssertNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  35998. DYNAMIC_TYPE_OPENSSL));
  35999. a->isDynamic = 1;
  36000. ASN1_INTEGER_free(a);
  36001. }
  36002. printf(resultFmt, passed);
  36003. #endif
  36004. }
  36005. static void test_wolfSSL_OpenSSL_add_all_algorithms(void){
  36006. #if defined(OPENSSL_EXTRA)
  36007. printf(testingFmt, "wolfSSL_OpenSSL_add_all_algorithms()");
  36008. AssertIntEQ(wolfSSL_add_all_algorithms(),WOLFSSL_SUCCESS);
  36009. AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(),WOLFSSL_SUCCESS);
  36010. AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(),WOLFSSL_SUCCESS);
  36011. printf(resultFmt, passed);
  36012. #endif
  36013. }
  36014. static void test_wolfSSL_OPENSSL_hexstr2buf(void)
  36015. {
  36016. #if defined(OPENSSL_EXTRA)
  36017. #define MAX_HEXSTR_BUFSZ 9
  36018. #define NUM_CASES 5
  36019. struct Output {
  36020. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  36021. long ret;
  36022. };
  36023. int i;
  36024. int j;
  36025. const char* inputs[NUM_CASES] = {
  36026. "aabcd1357e",
  36027. "01:12:23:34:a5:b6:c7:d8:e9",
  36028. ":01:02",
  36029. "012",
  36030. ":ab:ac:d"
  36031. };
  36032. struct Output expectedOutputs[NUM_CASES] = {
  36033. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  36034. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  36035. {{0x01, 0x02}, 2},
  36036. {{0x00}, 0},
  36037. {{0x00}, 0}
  36038. };
  36039. long len = 0;
  36040. unsigned char* returnedBuf = NULL;
  36041. printf(testingFmt, "test_wolfSSL_OPENSSL_hexstr2buf()");
  36042. for (i = 0; i < NUM_CASES; ++i) {
  36043. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  36044. if (returnedBuf == NULL) {
  36045. AssertIntEQ(expectedOutputs[i].ret, 0);
  36046. continue;
  36047. }
  36048. AssertIntEQ(expectedOutputs[i].ret, len);
  36049. for (j = 0; j < len; ++j) {
  36050. AssertIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  36051. }
  36052. OPENSSL_free(returnedBuf);
  36053. }
  36054. printf(resultFmt, passed);
  36055. #endif
  36056. }
  36057. static void test_wolfSSL_ASN1_STRING_print_ex(void){
  36058. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  36059. #ifndef NO_BIO
  36060. ASN1_STRING* asn_str;
  36061. const char data[] = "Hello wolfSSL!";
  36062. ASN1_STRING* esc_str;
  36063. const char esc_data[] = "a+;<>";
  36064. BIO *bio;
  36065. unsigned long flags;
  36066. int p_len;
  36067. unsigned char rbuf[255];
  36068. printf(testingFmt, "wolfSSL_ASN1_STRING_print_ex()");
  36069. /* setup */
  36070. XMEMSET(rbuf, 0, 255);
  36071. bio = BIO_new(BIO_s_mem());
  36072. BIO_set_write_buf_size(bio,255);
  36073. asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  36074. ASN1_STRING_set(asn_str, (const void*)data, sizeof(data));
  36075. esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  36076. ASN1_STRING_set(esc_str, (const void*)esc_data, sizeof(esc_data));
  36077. /* no flags */
  36078. XMEMSET(rbuf, 0, 255);
  36079. flags = 0;
  36080. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  36081. AssertIntEQ(p_len, 15);
  36082. BIO_read(bio, (void*)rbuf, 15);
  36083. AssertStrEQ((char*)rbuf, "Hello wolfSSL!");
  36084. /* RFC2253 Escape */
  36085. XMEMSET(rbuf, 0, 255);
  36086. flags = ASN1_STRFLGS_ESC_2253;
  36087. p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags);
  36088. AssertIntEQ(p_len, 9);
  36089. BIO_read(bio, (void*)rbuf, 9);
  36090. AssertStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  36091. /* Show type */
  36092. XMEMSET(rbuf, 0, 255);
  36093. flags = ASN1_STRFLGS_SHOW_TYPE;
  36094. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  36095. AssertIntEQ(p_len, 28);
  36096. BIO_read(bio, (void*)rbuf, 28);
  36097. AssertStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  36098. /* Dump All */
  36099. XMEMSET(rbuf, 0, 255);
  36100. flags = ASN1_STRFLGS_DUMP_ALL;
  36101. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  36102. AssertIntEQ(p_len, 31);
  36103. BIO_read(bio, (void*)rbuf, 31);
  36104. AssertStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  36105. /* Dump Der */
  36106. XMEMSET(rbuf, 0, 255);
  36107. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  36108. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  36109. AssertIntEQ(p_len, 35);
  36110. BIO_read(bio, (void*)rbuf, 35);
  36111. AssertStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  36112. /* Dump All + Show type */
  36113. XMEMSET(rbuf, 0, 255);
  36114. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  36115. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  36116. AssertIntEQ(p_len, 44);
  36117. BIO_read(bio, (void*)rbuf, 44);
  36118. AssertStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  36119. BIO_free(bio);
  36120. ASN1_STRING_free(asn_str);
  36121. ASN1_STRING_free(esc_str);
  36122. printf(resultFmt, passed);
  36123. #endif /* !NO_BIO */
  36124. #endif
  36125. }
  36126. static void test_wolfSSL_ASN1_TIME_to_generalizedtime(void){
  36127. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  36128. WOLFSSL_ASN1_TIME *t;
  36129. WOLFSSL_ASN1_TIME *out;
  36130. WOLFSSL_ASN1_TIME *gtime;
  36131. int tlen = 0;
  36132. unsigned char *data;
  36133. printf(testingFmt, "wolfSSL_ASN1_TIME_to_generalizedtime()");
  36134. /* UTC Time test */
  36135. AssertNotNull(t = wolfSSL_ASN1_TIME_new());
  36136. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  36137. AssertNotNull(out = wolfSSL_ASN1_TIME_new());
  36138. t->type = ASN_UTC_TIME;
  36139. t->length = ASN_UTC_TIME_SIZE;
  36140. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  36141. tlen = wolfSSL_ASN1_TIME_get_length(t);
  36142. AssertIntEQ(tlen, ASN_UTC_TIME_SIZE);
  36143. data = wolfSSL_ASN1_TIME_get_data(t);
  36144. AssertStrEQ((char*)data, "050727123456Z");
  36145. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  36146. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  36147. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  36148. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  36149. /* Generalized Time test */
  36150. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  36151. XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE);
  36152. XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE);
  36153. t->type = ASN_GENERALIZED_TIME;
  36154. t->length = ASN_GENERALIZED_TIME_SIZE;
  36155. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  36156. tlen = wolfSSL_ASN1_TIME_get_length(t);
  36157. AssertIntEQ(tlen, ASN_GENERALIZED_TIME_SIZE);
  36158. data = wolfSSL_ASN1_TIME_get_data(t);
  36159. AssertStrEQ((char*)data, "20050727123456Z");
  36160. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  36161. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  36162. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  36163. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  36164. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  36165. /* Null parameter test */
  36166. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  36167. gtime = NULL;
  36168. out = NULL;
  36169. t->type = ASN_UTC_TIME;
  36170. t->length = ASN_UTC_TIME_SIZE;
  36171. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  36172. AssertNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  36173. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  36174. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  36175. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  36176. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  36177. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  36178. printf(resultFmt, passed);
  36179. #endif
  36180. }
  36181. static void test_wolfSSL_X509_CA_num(void){
  36182. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  36183. defined(HAVE_ECC) && !defined(NO_RSA)
  36184. WOLFSSL_X509_STORE *store;
  36185. WOLFSSL_X509 *x509_1, *x509_2;
  36186. int ca_num = 0;
  36187. printf(testingFmt, "wolfSSL_X509_CA_num()");
  36188. store = wolfSSL_X509_STORE_new();
  36189. x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  36190. wolfSSL_X509_STORE_add_cert(store, x509_1);
  36191. ca_num = wolfSSL_X509_CA_num(store);
  36192. AssertIntEQ(ca_num, 1);
  36193. x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile, WOLFSSL_FILETYPE_PEM);
  36194. wolfSSL_X509_STORE_add_cert(store, x509_2);
  36195. ca_num = wolfSSL_X509_CA_num(store);
  36196. AssertIntEQ(ca_num, 2);
  36197. wolfSSL_X509_free(x509_1);
  36198. wolfSSL_X509_free(x509_2);
  36199. wolfSSL_X509_STORE_free(store);
  36200. printf(resultFmt, passed);
  36201. #endif
  36202. }
  36203. static void test_wolfSSL_X509_check_ca(void){
  36204. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  36205. WOLFSSL_X509 *x509;
  36206. printf(testingFmt, "wolfSSL_X509_check_ca()");
  36207. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  36208. AssertIntEQ(wolfSSL_X509_check_ca(x509), 1);
  36209. wolfSSL_X509_free(x509);
  36210. printf(resultFmt, passed);
  36211. #endif
  36212. }
  36213. static void test_wolfSSL_X509_check_ip_asc(void){
  36214. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  36215. WOLFSSL_X509 *x509;
  36216. printf(testingFmt, "wolfSSL_X509_check_ip_asc()");
  36217. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  36218. #if 0
  36219. /* TODO: add cert gen for testing positive case */
  36220. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  36221. #endif
  36222. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  36223. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  36224. wolfSSL_X509_free(x509);
  36225. printf(resultFmt, passed);
  36226. #endif
  36227. }
  36228. static void test_wolfSSL_make_cert(void)
  36229. {
  36230. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  36231. int ret;
  36232. Cert cert;
  36233. CertName name;
  36234. RsaKey key;
  36235. WC_RNG rng;
  36236. byte der[FOURK_BUF];
  36237. word32 idx;
  36238. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  36239. #ifdef OPENSSL_EXTRA
  36240. const unsigned char* pt;
  36241. int certSz;
  36242. X509* x509;
  36243. X509_NAME* x509name;
  36244. X509_NAME_ENTRY* entry;
  36245. ASN1_STRING* entryValue;
  36246. #endif
  36247. printf(testingFmt, "wolfSSL Make Certs");
  36248. XMEMSET(&name, 0, sizeof(CertName));
  36249. /* set up cert name */
  36250. XMEMCPY(name.country, "US", sizeof("US"));
  36251. name.countryEnc = CTC_PRINTABLE;
  36252. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  36253. name.stateEnc = CTC_UTF8;
  36254. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  36255. name.localityEnc = CTC_UTF8;
  36256. XMEMCPY(name.sur, "Test", sizeof("Test"));
  36257. name.surEnc = CTC_UTF8;
  36258. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  36259. name.orgEnc = CTC_UTF8;
  36260. XMEMCPY(name.unit, "Development", sizeof("Development"));
  36261. name.unitEnc = CTC_UTF8;
  36262. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  36263. name.commonNameEnc = CTC_UTF8;
  36264. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  36265. name.serialDevEnc = CTC_PRINTABLE;
  36266. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  36267. name.userIdEnc = CTC_PRINTABLE;
  36268. #ifdef WOLFSSL_MULTI_ATTRIB
  36269. #if CTC_MAX_ATTRIB > 2
  36270. {
  36271. NameAttrib* n;
  36272. n = &name.name[0];
  36273. n->id = ASN_DOMAIN_COMPONENT;
  36274. n->type = CTC_UTF8;
  36275. n->sz = sizeof("com");
  36276. XMEMCPY(n->value, "com", sizeof("com"));
  36277. n = &name.name[1];
  36278. n->id = ASN_DOMAIN_COMPONENT;
  36279. n->type = CTC_UTF8;
  36280. n->sz = sizeof("wolfssl");
  36281. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  36282. }
  36283. #endif
  36284. #endif /* WOLFSSL_MULTI_ATTRIB */
  36285. AssertIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  36286. #ifndef HAVE_FIPS
  36287. AssertIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, devId), 0);
  36288. #else
  36289. AssertIntEQ(wc_InitRng(&rng), 0);
  36290. #endif
  36291. /* load test RSA key */
  36292. idx = 0;
  36293. #if defined(USE_CERT_BUFFERS_1024)
  36294. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  36295. sizeof_server_key_der_1024), 0);
  36296. #elif defined(USE_CERT_BUFFERS_2048)
  36297. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  36298. sizeof_server_key_der_2048), 0);
  36299. #else
  36300. /* error case, no RSA key loaded, happens later */
  36301. (void)idx;
  36302. #endif
  36303. XMEMSET(&cert, 0 , sizeof(Cert));
  36304. AssertIntEQ(wc_InitCert(&cert), 0);
  36305. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  36306. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  36307. cert.serialSz = (int)sizeof(mySerial);
  36308. cert.isCA = 1;
  36309. #ifndef NO_SHA256
  36310. cert.sigType = CTC_SHA256wRSA;
  36311. #else
  36312. cert.sigType = CTC_SHAwRSA;
  36313. #endif
  36314. /* add SKID from the Public Key */
  36315. AssertIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  36316. /* add AKID from the Public Key */
  36317. AssertIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  36318. ret = 0;
  36319. do {
  36320. #if defined(WOLFSSL_ASYNC_CRYPT)
  36321. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  36322. #endif
  36323. if (ret >= 0) {
  36324. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  36325. }
  36326. } while (ret == WC_PENDING_E);
  36327. AssertIntGT(ret, 0);
  36328. #ifdef OPENSSL_EXTRA
  36329. /* der holds a certificate with DC's now check X509 parsing of it */
  36330. certSz = ret;
  36331. pt = der;
  36332. AssertNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  36333. AssertNotNull(x509name = X509_get_subject_name(x509));
  36334. #ifdef WOLFSSL_MULTI_ATTRIB
  36335. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  36336. -1)), 5);
  36337. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  36338. idx)), 6);
  36339. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  36340. idx)), -1);
  36341. #endif /* WOLFSSL_MULTI_ATTRIB */
  36342. /* compare DN at index 0 */
  36343. AssertNotNull(entry = X509_NAME_get_entry(x509name, 0));
  36344. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  36345. AssertIntEQ(ASN1_STRING_length(entryValue), 2);
  36346. AssertStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  36347. #ifdef WOLFSSL_MULTI_ATTRIB
  36348. /* get first and second DC and compare result */
  36349. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  36350. -1)), 5);
  36351. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  36352. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  36353. AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  36354. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  36355. idx)), 6);
  36356. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  36357. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  36358. AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  36359. #endif /* WOLFSSL_MULTI_ATTRIB */
  36360. /* try invalid index locations for regression test and sanity check */
  36361. AssertNull(entry = X509_NAME_get_entry(x509name, 11));
  36362. AssertNull(entry = X509_NAME_get_entry(x509name, 20));
  36363. X509_free(x509);
  36364. #endif /* OPENSSL_EXTRA */
  36365. wc_FreeRsaKey(&key);
  36366. wc_FreeRng(&rng);
  36367. printf(resultFmt, passed);
  36368. #endif
  36369. }
  36370. static void test_wolfSSL_X509_get_version(void){
  36371. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  36372. WOLFSSL_X509 *x509;
  36373. printf(testingFmt, "wolfSSL_X509_get_version()");
  36374. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  36375. AssertNotNull(x509);
  36376. AssertIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  36377. wolfSSL_X509_free(x509);
  36378. printf(resultFmt, passed);
  36379. #endif
  36380. }
  36381. static void test_wolfSSL_DES_ncbc(void){
  36382. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  36383. const_DES_cblock myDes;
  36384. DES_cblock iv = {1};
  36385. DES_key_schedule key = {0};
  36386. unsigned char msg[] = "hello wolfssl";
  36387. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  36388. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  36389. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  36390. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  36391. printf(testingFmt, "wolfSSL_DES_ncbc()");
  36392. /* partial block test */
  36393. DES_set_key(&key, &myDes);
  36394. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  36395. AssertIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  36396. AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  36397. DES_set_key(&key, &myDes);
  36398. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  36399. *((byte*)&iv) = 1;
  36400. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  36401. AssertIntEQ(XMEMCMP(msg, pln, 3), 0);
  36402. AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  36403. /* full block test */
  36404. DES_set_key(&key, &myDes);
  36405. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  36406. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  36407. *((byte*)&iv) = 1;
  36408. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  36409. AssertIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  36410. AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  36411. DES_set_key(&key, &myDes);
  36412. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  36413. *((byte*)&iv) = 1;
  36414. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  36415. AssertIntEQ(XMEMCMP(msg, pln, 8), 0);
  36416. AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  36417. printf(resultFmt, passed);
  36418. #endif
  36419. }
  36420. static void test_wolfSSL_AES_cbc_encrypt(void)
  36421. {
  36422. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA)
  36423. AES_KEY aes;
  36424. AES_KEY* aesN = NULL;
  36425. size_t len = 0;
  36426. size_t lenB = 0;
  36427. int keySz0 = 0;
  36428. int keySzN = -1;
  36429. byte out[AES_BLOCK_SIZE] = {0};
  36430. byte* outN = NULL;
  36431. /* Test vectors retrieved from:
  36432. * <begin URL>
  36433. * https://csrc.nist.gov/
  36434. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  36435. * documents/aes/KAT_AES.zip
  36436. * </end URL>
  36437. */
  36438. const byte* pt128N = NULL;
  36439. byte* key128N = NULL;
  36440. byte* iv128N = NULL;
  36441. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  36442. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  36443. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  36444. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  36445. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  36446. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  36447. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  36448. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  36449. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  36450. len = sizeof(pt128);
  36451. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  36452. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  36453. AssertIntNE(XMEMCMP(b, g, h), i)
  36454. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  36455. printf(testingFmt, "Stressing wolfSSL_AES_cbc_encrypt()");
  36456. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  36457. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  36458. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  36459. AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  36460. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  36461. AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  36462. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  36463. printf(resultFmt, "Stress Tests: passed");
  36464. printf(testingFmt, "Stressing wolfSSL_AES_set_encrypt_key");
  36465. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  36466. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  36467. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  36468. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  36469. printf(resultFmt, "Stress Tests: passed");
  36470. printf(testingFmt, "Stressing wolfSSL_AES_set_decrypt_key");
  36471. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  36472. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  36473. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  36474. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  36475. printf(resultFmt, "Stress Tests: passed");
  36476. #ifdef WOLFSSL_AES_128
  36477. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 128-bit");
  36478. XMEMSET(out, 0, AES_BLOCK_SIZE);
  36479. RESET_IV(iv128tmp, iv128);
  36480. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  36481. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  36482. AssertIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  36483. printf(resultFmt, "passed");
  36484. #ifdef HAVE_AES_DECRYPT
  36485. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode");
  36486. XMEMSET(out, 0, AES_BLOCK_SIZE);
  36487. RESET_IV(iv128tmp, iv128);
  36488. len = sizeof(ct128);
  36489. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  36490. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  36491. AssertIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  36492. printf(resultFmt, "passed");
  36493. #endif
  36494. #endif /* WOLFSSL_AES_128 */
  36495. #ifdef WOLFSSL_AES_192
  36496. {
  36497. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  36498. * Appendix F.2.3 */
  36499. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  36500. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  36501. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  36502. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  36503. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  36504. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  36505. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  36506. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  36507. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  36508. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  36509. len = sizeof(pt192);
  36510. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 192-bit");
  36511. XMEMSET(out, 0, AES_BLOCK_SIZE);
  36512. RESET_IV(iv192tmp, iv192);
  36513. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  36514. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  36515. AssertIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  36516. printf(resultFmt, "passed");
  36517. #ifdef HAVE_AES_DECRYPT
  36518. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode");
  36519. len = sizeof(ct192);
  36520. RESET_IV(iv192tmp, iv192);
  36521. XMEMSET(out, 0, AES_BLOCK_SIZE);
  36522. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  36523. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  36524. AssertIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  36525. printf(resultFmt, "passed");
  36526. #endif
  36527. }
  36528. #endif /* WOLFSSL_AES_192 */
  36529. #ifdef WOLFSSL_AES_256
  36530. {
  36531. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  36532. * Appendix F.2.5 */
  36533. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  36534. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  36535. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  36536. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  36537. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  36538. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  36539. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  36540. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  36541. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  36542. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  36543. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  36544. len = sizeof(pt256);
  36545. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 256-bit");
  36546. XMEMSET(out, 0, AES_BLOCK_SIZE);
  36547. RESET_IV(iv256tmp, iv256);
  36548. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  36549. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  36550. AssertIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  36551. printf(resultFmt, "passed");
  36552. #ifdef HAVE_AES_DECRYPT
  36553. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode");
  36554. len = sizeof(ct256);
  36555. RESET_IV(iv256tmp, iv256);
  36556. XMEMSET(out, 0, AES_BLOCK_SIZE);
  36557. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  36558. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  36559. AssertIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  36560. printf(resultFmt, "passed");
  36561. #endif
  36562. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  36563. {
  36564. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  36565. byte wrapPlain[sizeof(key256)] = { 0 };
  36566. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  36567. printf(testingFmt, "wolfSSL_AES_wrap_key() 256-bit NULL iv");
  36568. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  36569. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  36570. 15), WOLFSSL_FAILURE);
  36571. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  36572. sizeof(key256)), sizeof(wrapCipher));
  36573. printf(resultFmt, "passed");
  36574. printf(testingFmt, "wolfSSL_AES_unwrap_key() 256-bit NULL iv");
  36575. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  36576. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  36577. 23), WOLFSSL_FAILURE);
  36578. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  36579. sizeof(wrapCipher)), sizeof(wrapPlain));
  36580. AssertIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  36581. printf(resultFmt, "passed");
  36582. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  36583. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  36584. printf(testingFmt, "wolfSSL_AES_wrap_key() 256-bit custom iv");
  36585. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  36586. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  36587. sizeof(key256)), sizeof(wrapCipher));
  36588. printf(resultFmt, "passed");
  36589. printf(testingFmt, "wolfSSL_AES_unwrap_key() 256-bit custom iv");
  36590. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  36591. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  36592. sizeof(wrapCipher)), sizeof(wrapPlain));
  36593. AssertIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  36594. printf(resultFmt, "passed");
  36595. }
  36596. #endif /* HAVE_AES_KEYWRAP */
  36597. }
  36598. #endif /* WOLFSSL_AES_256 */
  36599. #endif
  36600. }
  36601. static void test_wolfSSL_CRYPTO_cts128(void)
  36602. {
  36603. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) \
  36604. && defined(HAVE_CTS)
  36605. byte tmp[64]; /* Largest vector size */
  36606. /* Test vectors taken form RFC3962 Appendix B */
  36607. const testVector vects[] = {
  36608. {
  36609. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  36610. "\x20",
  36611. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  36612. "\x97",
  36613. 17, 17
  36614. },
  36615. {
  36616. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  36617. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  36618. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  36619. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  36620. 31, 31
  36621. },
  36622. {
  36623. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  36624. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  36625. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  36626. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  36627. 32, 32
  36628. },
  36629. {
  36630. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  36631. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  36632. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  36633. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  36634. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  36635. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  36636. 47, 47
  36637. },
  36638. {
  36639. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  36640. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  36641. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  36642. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  36643. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  36644. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  36645. 48, 48
  36646. },
  36647. {
  36648. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  36649. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  36650. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  36651. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  36652. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  36653. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  36654. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  36655. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  36656. 64, 64
  36657. }
  36658. };
  36659. byte keyBytes[AES_128_KEY_SIZE] = {
  36660. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  36661. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  36662. };
  36663. size_t i;
  36664. XMEMSET(tmp, 0, sizeof(tmp));
  36665. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  36666. AES_KEY encKey;
  36667. AES_KEY decKey;
  36668. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  36669. XMEMSET(iv, 0, sizeof(iv));
  36670. AssertIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8, &encKey), 0);
  36671. AssertIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8, &decKey), 0);
  36672. AssertIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  36673. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  36674. vects[i].outLen);
  36675. AssertIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  36676. XMEMSET(iv, 0, sizeof(iv));
  36677. AssertIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  36678. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  36679. vects[i].inLen);
  36680. AssertIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  36681. }
  36682. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  36683. }
  36684. #if defined(OPENSSL_ALL)
  36685. #if !defined(NO_ASN)
  36686. static void test_wolfSSL_ASN1_STRING_to_UTF8(void)
  36687. {
  36688. #if !defined(NO_RSA)
  36689. WOLFSSL_X509* x509;
  36690. WOLFSSL_X509_NAME* subject;
  36691. WOLFSSL_X509_NAME_ENTRY* e;
  36692. WOLFSSL_ASN1_STRING* a;
  36693. FILE* file;
  36694. int idx = 0;
  36695. char targetOutput[16] = "www.wolfssl.com";
  36696. unsigned char* actual_output;
  36697. int len = 0;
  36698. int result = 0;
  36699. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  36700. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  36701. fclose(file);
  36702. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName");
  36703. AssertNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  36704. AssertIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  36705. NID_commonName, -1)), 5);
  36706. AssertNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  36707. AssertNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  36708. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  36709. result = strncmp((const char*)actual_output, targetOutput, len);
  36710. AssertIntEQ(result, 0);
  36711. printf(resultFmt, result == 0 ? passed : failed);
  36712. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(NULL, valid): ");
  36713. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)),
  36714. WOLFSSL_FATAL_ERROR);
  36715. printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
  36716. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(valid, NULL): ");
  36717. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)),
  36718. WOLFSSL_FATAL_ERROR);
  36719. printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
  36720. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL): ");
  36721. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)),
  36722. WOLFSSL_FATAL_ERROR);
  36723. printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
  36724. wolfSSL_X509_free(x509);
  36725. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  36726. #endif
  36727. }
  36728. static void test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  36729. {
  36730. ASN1_STRING* asn1str_test;
  36731. ASN1_STRING* asn1str_answer;
  36732. /* Each character is encoded using 4 bytes */
  36733. char input[] = {
  36734. 0, 0, 0, 'T',
  36735. 0, 0, 0, 'e',
  36736. 0, 0, 0, 's',
  36737. 0, 0, 0, 't',
  36738. };
  36739. char output[] = "Test";
  36740. printf(testingFmt, "test_wolfSSL_ASN1_UNIVERSALSTRING_to_string()");
  36741. AssertNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  36742. AssertIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  36743. AssertIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  36744. AssertNotNull(asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  36745. AssertIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  36746. AssertIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  36747. ASN1_STRING_free(asn1str_test);
  36748. ASN1_STRING_free(asn1str_answer);
  36749. printf(resultFmt, "passed");
  36750. }
  36751. #endif /* !defined(NO_ASN) */
  36752. static void test_wolfSSL_sk_CIPHER_description(void)
  36753. {
  36754. #if !defined(NO_RSA)
  36755. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  36756. int i,j,k;
  36757. int numCiphers = 0;
  36758. const SSL_METHOD *method = NULL;
  36759. const SSL_CIPHER *cipher = NULL;
  36760. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  36761. SSL_CTX *ctx = NULL;
  36762. SSL *ssl = NULL;
  36763. char buf[256];
  36764. char test_str[9] = "0000000";
  36765. const char badStr[] = "unknown";
  36766. const char certPath[] = "./certs/client-cert.pem";
  36767. XMEMSET(buf, 0, sizeof(buf));
  36768. printf(testingFmt, "wolfSSL_sk_CIPHER_description");
  36769. AssertNotNull(method = TLSv1_2_client_method());
  36770. AssertNotNull(ctx = SSL_CTX_new(method));
  36771. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  36772. SSL_CTX_set_verify_depth(ctx, 4);
  36773. SSL_CTX_set_options(ctx, flags);
  36774. AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  36775. WOLFSSL_SUCCESS);
  36776. AssertNotNull(ssl = SSL_new(ctx));
  36777. /* SSL_get_ciphers returns a stack of all configured ciphers
  36778. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  36779. */
  36780. AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  36781. /* loop through the amount of supportedCiphers */
  36782. numCiphers = sk_num(supportedCiphers);
  36783. for (i = 0; i < numCiphers; ++i) {
  36784. /* sk_value increments "sk->data.cipher->cipherOffset".
  36785. * wolfSSL_sk_CIPHER_description sets the description for
  36786. * the cipher based on the provided offset.
  36787. */
  36788. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  36789. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  36790. }
  36791. /* Search cipher description string for "unknown" descriptor */
  36792. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  36793. k = 0;
  36794. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  36795. test_str[k] = badStr[k];
  36796. j++;
  36797. k++;
  36798. }
  36799. }
  36800. /* Fail if test_str == badStr == "unknown" */
  36801. AssertStrNE(test_str,badStr);
  36802. }
  36803. SSL_free(ssl);
  36804. SSL_CTX_free(ctx);
  36805. printf(resultFmt, passed);
  36806. #endif
  36807. }
  36808. static void test_wolfSSL_get_ciphers_compat(void)
  36809. {
  36810. #if !defined(NO_RSA)
  36811. const SSL_METHOD *method = NULL;
  36812. const char certPath[] = "./certs/client-cert.pem";
  36813. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  36814. SSL_CTX *ctx = NULL;
  36815. WOLFSSL *ssl = NULL;
  36816. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  36817. printf(testingFmt, "wolfSSL_get_ciphers_compat");
  36818. method = SSLv23_client_method();
  36819. AssertNotNull(method);
  36820. ctx = SSL_CTX_new(method);
  36821. AssertNotNull(ctx);
  36822. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  36823. SSL_CTX_set_verify_depth(ctx, 4);
  36824. SSL_CTX_set_options(ctx, flags);
  36825. AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  36826. WOLFSSL_SUCCESS);
  36827. AssertNotNull(ssl = SSL_new(ctx));
  36828. /* Test Bad NULL input */
  36829. AssertNull(supportedCiphers = SSL_get_ciphers(NULL));
  36830. /* Test for Good input */
  36831. AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  36832. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  36833. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  36834. SSL_free(ssl);
  36835. SSL_CTX_free(ctx);
  36836. printf(resultFmt, passed);
  36837. #endif
  36838. }
  36839. static void test_wolfSSL_X509_PUBKEY_get(void)
  36840. {
  36841. WOLFSSL_X509_PUBKEY pubkey;
  36842. WOLFSSL_X509_PUBKEY* key;
  36843. WOLFSSL_EVP_PKEY evpkey ;
  36844. WOLFSSL_EVP_PKEY* evpPkey;
  36845. WOLFSSL_EVP_PKEY* retEvpPkey;
  36846. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  36847. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  36848. key = &pubkey;
  36849. evpPkey = &evpkey;
  36850. evpPkey->type = WOLFSSL_SUCCESS;
  36851. key->pkey = evpPkey;
  36852. printf(testingFmt, "wolfSSL_X509_PUBKEY_get()");
  36853. AssertNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  36854. AssertIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  36855. AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  36856. key->pkey = NULL;
  36857. AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  36858. printf(resultFmt,retEvpPkey == NULL ? passed : failed);
  36859. }
  36860. static void test_wolfSSL_d2i_DHparams(void)
  36861. {
  36862. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  36863. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  36864. FILE* f = NULL;
  36865. unsigned char buf[4096];
  36866. const unsigned char* pt = buf;
  36867. #ifdef HAVE_FFDHE_2048
  36868. const char* params1 = "./certs/dh2048.der";
  36869. #endif
  36870. #ifdef HAVE_FFDHE_3072
  36871. const char* params2 = "./certs/dh3072.der";
  36872. #endif
  36873. long len = 0;
  36874. WOLFSSL_DH* dh = NULL;
  36875. XMEMSET(buf, 0, sizeof(buf));
  36876. /* Test 2048 bit parameters */
  36877. #ifdef HAVE_FFDHE_2048
  36878. printf(testingFmt, "wolfSSL_d2i_DHparams() 2048-bit");
  36879. f = XFOPEN(params1, "rb");
  36880. AssertTrue(f != XBADFILE);
  36881. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  36882. XFCLOSE(f);
  36883. /* Valid case */
  36884. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  36885. AssertNotNull(dh->p);
  36886. AssertNotNull(dh->g);
  36887. AssertTrue(pt != buf);
  36888. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  36889. AssertIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), WOLFSSL_SUCCESS);
  36890. #endif
  36891. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  36892. /* Invalid cases */
  36893. AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  36894. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  36895. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  36896. DH_free(dh);
  36897. printf(resultFmt, passed);
  36898. *buf = 0;
  36899. pt = buf;
  36900. #endif /* HAVE_FFDHE_2048 */
  36901. /* Test 3072 bit parameters */
  36902. #ifdef HAVE_FFDHE_3072
  36903. printf(testingFmt, "wolfSSL_d2i_DHparams() 3072-bit");
  36904. f = XFOPEN(params2, "rb");
  36905. AssertTrue(f != XBADFILE);
  36906. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  36907. XFCLOSE(f);
  36908. /* Valid case */
  36909. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  36910. AssertNotNull(dh->p);
  36911. AssertNotNull(dh->g);
  36912. AssertTrue(pt != buf);
  36913. AssertIntEQ(DH_generate_key(dh), 1);
  36914. /* Invalid cases */
  36915. AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  36916. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  36917. DH_free(dh);
  36918. printf(resultFmt, passed);
  36919. #endif /* HAVE_FFDHE_3072 */
  36920. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  36921. #endif /* !NO_DH */
  36922. }
  36923. static void test_wolfSSL_i2d_DHparams(void)
  36924. {
  36925. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  36926. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  36927. FILE* f;
  36928. unsigned char buf[4096];
  36929. const unsigned char* pt = buf;
  36930. unsigned char* pt2 = buf;
  36931. #ifdef HAVE_FFDHE_2048
  36932. const char* params1 = "./certs/dh2048.der";
  36933. #endif
  36934. #ifdef HAVE_FFDHE_3072
  36935. const char* params2 = "./certs/dh3072.der";
  36936. #endif
  36937. long len;
  36938. WOLFSSL_DH* dh;
  36939. /* Test 2048 bit parameters */
  36940. #ifdef HAVE_FFDHE_2048
  36941. printf(testingFmt, "wolfSSL_i2d_DHparams() 2048-bit");
  36942. f = XFOPEN(params1, "rb");
  36943. AssertTrue(f != XBADFILE);
  36944. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  36945. XFCLOSE(f);
  36946. /* Valid case */
  36947. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  36948. AssertTrue(pt != buf);
  36949. AssertIntEQ(DH_generate_key(dh), 1);
  36950. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  36951. /* Invalid case */
  36952. AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  36953. /* Return length only */
  36954. AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  36955. DH_free(dh);
  36956. printf(resultFmt, passed);
  36957. *buf = 0;
  36958. pt = buf;
  36959. pt2 = buf;
  36960. #endif
  36961. /* Test 3072 bit parameters */
  36962. #ifdef HAVE_FFDHE_3072
  36963. printf(testingFmt, "wolfSSL_i2d_DHparams() 3072-bit");
  36964. f = XFOPEN(params2, "rb");
  36965. AssertTrue(f != XBADFILE);
  36966. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  36967. XFCLOSE(f);
  36968. /* Valid case */
  36969. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  36970. AssertTrue(pt != buf);
  36971. AssertIntEQ(DH_generate_key(dh), 1);
  36972. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  36973. /* Invalid case */
  36974. AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  36975. /* Return length only */
  36976. AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  36977. DH_free(dh);
  36978. printf(resultFmt, passed);
  36979. #endif
  36980. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  36981. #endif
  36982. }
  36983. static void test_wolfSSL_EC_KEY_dup(void)
  36984. {
  36985. #if defined(HAVE_ECC) && (defined(OPENSSL_EXTRA) || \
  36986. defined(OPENSSL_EXTRA_X509_SMALL))
  36987. WOLFSSL_EC_KEY* ecKey;
  36988. WOLFSSL_EC_KEY* dupKey;
  36989. ecc_key* srcKey;
  36990. ecc_key* destKey;
  36991. printf(testingFmt, "wolfSSL_EC_KEY_dup()");
  36992. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  36993. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  36994. /* Valid cases */
  36995. AssertNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  36996. AssertIntEQ(EC_KEY_check_key(dupKey), 1);
  36997. /* Compare pubkey */
  36998. srcKey = (ecc_key*)ecKey->internal;
  36999. destKey = (ecc_key*)dupKey->internal;
  37000. AssertIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  37001. /* compare EC_GROUP */
  37002. AssertIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  37003. /* compare EC_POINT */
  37004. AssertIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  37005. dupKey->pub_key, NULL), MP_EQ);
  37006. /* compare BIGNUM */
  37007. AssertIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  37008. wolfSSL_EC_KEY_free(dupKey);
  37009. /* Invalid cases */
  37010. /* NULL key */
  37011. AssertNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  37012. /* NULL ecc_key */
  37013. wc_ecc_free((ecc_key*)ecKey->internal);
  37014. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  37015. ecKey->internal = NULL; /* Set ecc_key to NULL */
  37016. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  37017. wolfSSL_EC_KEY_free(ecKey);
  37018. wolfSSL_EC_KEY_free(dupKey);
  37019. /* NULL Group */
  37020. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  37021. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  37022. wolfSSL_EC_GROUP_free(ecKey->group);
  37023. ecKey->group = NULL; /* Set group to NULL */
  37024. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  37025. wolfSSL_EC_KEY_free(ecKey);
  37026. wolfSSL_EC_KEY_free(dupKey);
  37027. /* NULL public key */
  37028. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  37029. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  37030. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  37031. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  37032. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  37033. wolfSSL_EC_POINT_free(ecKey->pub_key);
  37034. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  37035. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  37036. wolfSSL_EC_KEY_free(ecKey);
  37037. wolfSSL_EC_KEY_free(dupKey);
  37038. /* NULL private key */
  37039. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  37040. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  37041. wolfSSL_BN_free(ecKey->priv_key);
  37042. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  37043. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  37044. wolfSSL_EC_KEY_free(ecKey);
  37045. wolfSSL_EC_KEY_free(dupKey);
  37046. /* Test EC_KEY_up_ref */
  37047. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  37048. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  37049. AssertIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  37050. AssertIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  37051. /* reference count doesn't follow duplicate */
  37052. AssertNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  37053. AssertIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  37054. AssertIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  37055. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  37056. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  37057. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  37058. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  37059. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  37060. printf(resultFmt, passed);
  37061. #endif
  37062. }
  37063. static void test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  37064. {
  37065. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  37066. DSA *dsa = NULL;
  37067. DSA *setDsa = NULL;
  37068. EVP_PKEY *pkey = NULL;
  37069. EVP_PKEY *set1Pkey = NULL;
  37070. SHA_CTX sha;
  37071. byte signature[DSA_SIG_SIZE];
  37072. byte hash[WC_SHA_DIGEST_SIZE];
  37073. word32 bytes;
  37074. int answer;
  37075. #ifdef USE_CERT_BUFFERS_1024
  37076. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  37077. int dsaKeySz = sizeof_dsa_key_der_1024;
  37078. byte tmp[ONEK_BUF];
  37079. XMEMSET(tmp, 0, sizeof(tmp));
  37080. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  37081. bytes = dsaKeySz;
  37082. #elif defined(USE_CERT_BUFFERS_2048)
  37083. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  37084. int dsaKeySz = sizeof_dsa_key_der_2048;
  37085. byte tmp[TWOK_BUF];
  37086. XMEMSET(tmp, 0, sizeof(tmp));
  37087. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  37088. bytes = dsaKeySz;
  37089. #else
  37090. byte tmp[TWOK_BUF];
  37091. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  37092. int dsaKeySz;
  37093. XMEMSET(tmp, 0, sizeof(tmp));
  37094. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  37095. if (fp == XBADFILE) {
  37096. return WOLFSSL_BAD_FILE;
  37097. }
  37098. dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  37099. XFCLOSE(fp);
  37100. #endif /* END USE_CERT_BUFFERS_1024 */
  37101. printf(testingFmt,
  37102. "wolfSSL_EVP_PKEY_set1_DSA and wolfSSL_EVP_PKEY_get1_DSA");
  37103. /* Create hash to later Sign and Verify */
  37104. AssertIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  37105. AssertIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  37106. AssertIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  37107. /* Initialize pkey with der format dsa key */
  37108. AssertNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey,
  37109. &dsaKeyDer ,(long)dsaKeySz));
  37110. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  37111. /* Should Fail: NULL argument */
  37112. AssertNull(dsa = EVP_PKEY_get0_DSA(NULL));
  37113. AssertNull(dsa = EVP_PKEY_get1_DSA(NULL));
  37114. /* Should Pass: Initialized pkey argument */
  37115. AssertNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  37116. AssertNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  37117. #ifdef USE_CERT_BUFFERS_1024
  37118. AssertIntEQ(DSA_bits(dsa), 1024);
  37119. #else
  37120. AssertIntEQ(DSA_bits(dsa), 2048);
  37121. #endif
  37122. /* Sign */
  37123. AssertIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  37124. /* Verify. */
  37125. AssertIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  37126. WOLFSSL_SUCCESS);
  37127. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  37128. /* Should Fail: set1Pkey not initialized */
  37129. AssertIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  37130. /* Initialize set1Pkey */
  37131. set1Pkey = EVP_PKEY_new();
  37132. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  37133. AssertIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  37134. WOLFSSL_SUCCESS);
  37135. /* Should Pass: set dsa into set1Pkey */
  37136. AssertIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  37137. printf(resultFmt, passed);
  37138. DSA_free(dsa);
  37139. DSA_free(setDsa);
  37140. EVP_PKEY_free(pkey);
  37141. EVP_PKEY_free(set1Pkey);
  37142. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  37143. } /* END test_EVP_PKEY_set1_get1_DSA */
  37144. static void test_wolfSSL_DSA_SIG(void)
  37145. {
  37146. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  37147. !defined(HAVE_FIPS)
  37148. DSA *dsa = NULL;
  37149. DSA *dsa2 = NULL;
  37150. DSA_SIG *sig = NULL;
  37151. const BIGNUM *p = NULL;
  37152. const BIGNUM *q = NULL;
  37153. const BIGNUM *g = NULL;
  37154. const BIGNUM *pub = NULL;
  37155. const BIGNUM *priv = NULL;
  37156. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  37157. printf(testingFmt, "wolfSSL_DSA_SIG");
  37158. AssertNotNull(dsa = DSA_generate_parameters(2048,
  37159. NULL, 0, NULL, NULL, NULL, NULL));
  37160. DSA_free(dsa);
  37161. AssertNotNull(dsa = DSA_new());
  37162. AssertIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  37163. NULL, 0, NULL, NULL, NULL), 1);
  37164. AssertIntEQ(DSA_generate_key(dsa), 1);
  37165. DSA_get0_pqg(dsa, &p, &q, &g);
  37166. DSA_get0_key(dsa, &pub, &priv);
  37167. AssertNotNull(p = BN_dup(p));
  37168. AssertNotNull(q = BN_dup(q));
  37169. AssertNotNull(g = BN_dup(g));
  37170. AssertNotNull(pub = BN_dup(pub));
  37171. AssertNotNull(priv = BN_dup(priv));
  37172. AssertNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  37173. AssertNotNull(dsa2 = DSA_new());
  37174. AssertIntEQ(DSA_set0_pqg(dsa2, (BIGNUM*)p, (BIGNUM*)q, (BIGNUM*)g), 1);
  37175. AssertIntEQ(DSA_set0_key(dsa2, (BIGNUM*)pub, (BIGNUM*)priv), 1);
  37176. AssertIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  37177. printf(resultFmt, passed);
  37178. DSA_free(dsa);
  37179. DSA_free(dsa2);
  37180. DSA_SIG_free(sig);
  37181. #endif
  37182. }
  37183. static void test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  37184. {
  37185. #ifdef HAVE_ECC
  37186. WOLFSSL_EC_KEY *ecKey = NULL;
  37187. WOLFSSL_EC_KEY *ecGet1 = NULL;
  37188. EVP_PKEY *pkey = NULL;
  37189. printf(testingFmt,
  37190. "wolfSSL_EVP_PKEY_set1_EC_KEY and wolfSSL_EVP_PKEY_get1_EC_KEY");
  37191. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  37192. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  37193. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  37194. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  37195. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  37196. /* Should fail since ecKey is empty */
  37197. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  37198. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  37199. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  37200. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  37201. AssertNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  37202. AssertNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  37203. wolfSSL_EC_KEY_free(ecKey);
  37204. wolfSSL_EC_KEY_free(ecGet1);
  37205. EVP_PKEY_free(pkey);
  37206. /* PASSED */
  37207. printf(resultFmt, passed);
  37208. #endif /* HAVE_ECC */
  37209. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  37210. static void test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  37211. {
  37212. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  37213. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  37214. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  37215. DH *dh = NULL;
  37216. DH *setDh = NULL;
  37217. EVP_PKEY *pkey = NULL;
  37218. FILE* f = NULL;
  37219. unsigned char buf[4096];
  37220. const unsigned char* pt = buf;
  37221. const char* dh2048 = "./certs/dh2048.der";
  37222. long len = 0;
  37223. int code = -1;
  37224. printf(testingFmt,"wolfSSL_EVP_PKEY_set1_DH and wolfSSL_EVP_PKEY_get1_DH");
  37225. XMEMSET(buf, 0, sizeof(buf));
  37226. f = XFOPEN(dh2048, "rb");
  37227. AssertTrue(f != XBADFILE);
  37228. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  37229. XFCLOSE(f);
  37230. /* Load dh2048.der into DH with internal format */
  37231. AssertNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  37232. AssertIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  37233. AssertIntEQ(code, 0);
  37234. code = -1;
  37235. pkey = wolfSSL_EVP_PKEY_new();
  37236. /* Set DH into PKEY */
  37237. AssertIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  37238. /* Get DH from PKEY */
  37239. AssertNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  37240. AssertIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  37241. AssertIntEQ(code, 0);
  37242. EVP_PKEY_free(pkey);
  37243. DH_free(setDh);
  37244. DH_free(dh);
  37245. printf(resultFmt, passed);
  37246. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  37247. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  37248. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  37249. } /* END test_EVP_PKEY_set1_get1_DH */
  37250. static void test_wolfSSL_CTX_ctrl(void)
  37251. {
  37252. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  37253. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  37254. char caFile[] = "./certs/client-ca.pem";
  37255. char clientFile[] = "./certs/client-cert.pem";
  37256. SSL_CTX* ctx;
  37257. X509* x509 = NULL;
  37258. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  37259. byte buf[6000];
  37260. char file[] = "./certs/dsaparams.pem";
  37261. XFILE f;
  37262. int bytes;
  37263. BIO* bio;
  37264. DSA* dsa;
  37265. DH* dh;
  37266. #endif
  37267. #ifdef HAVE_ECC
  37268. WOLFSSL_EC_KEY* ecKey;
  37269. #endif
  37270. printf(testingFmt, "wolfSSL_CTX_ctrl");
  37271. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  37272. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  37273. AssertNotNull(x509);
  37274. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  37275. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  37276. AssertNotNull(x509);
  37277. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  37278. /* Initialize DH */
  37279. f = XFOPEN(file, "rb");
  37280. AssertTrue((f != XBADFILE));
  37281. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  37282. XFCLOSE(f);
  37283. bio = BIO_new_mem_buf((void*)buf, bytes);
  37284. AssertNotNull(bio);
  37285. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  37286. AssertNotNull(dsa);
  37287. dh = wolfSSL_DSA_dup_DH(dsa);
  37288. AssertNotNull(dh);
  37289. #endif
  37290. #ifdef HAVE_ECC
  37291. /* Initialize WOLFSSL_EC_KEY */
  37292. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  37293. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey),1);
  37294. #endif
  37295. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  37296. /* additional test of getting EVP_PKEY key size from X509
  37297. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  37298. * allowed with user RSA */
  37299. {
  37300. EVP_PKEY* pkey;
  37301. #if defined(HAVE_ECC)
  37302. X509* ecX509;
  37303. #endif /* HAVE_ECC */
  37304. AssertNotNull(pkey = X509_get_pubkey(x509));
  37305. /* current RSA key is 2048 bit (256 bytes) */
  37306. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  37307. EVP_PKEY_free(pkey);
  37308. #if defined(HAVE_ECC)
  37309. #if defined(USE_CERT_BUFFERS_256)
  37310. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  37311. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  37312. SSL_FILETYPE_ASN1));
  37313. #else
  37314. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  37315. cliEccCertFile, SSL_FILETYPE_PEM));
  37316. #endif
  37317. AssertNotNull(pkey = X509_get_pubkey(ecX509));
  37318. /* current ECC key is 256 bit (32 bytes) */
  37319. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  37320. X509_free(ecX509);
  37321. EVP_PKEY_free(pkey);
  37322. #endif /* HAVE_ECC */
  37323. }
  37324. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  37325. /* Tests should fail with passed in NULL pointer */
  37326. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,NULL),
  37327. SSL_FAILURE);
  37328. #if !defined(NO_DH) && !defined(NO_DSA)
  37329. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,NULL),
  37330. SSL_FAILURE);
  37331. #endif
  37332. #ifdef HAVE_ECC
  37333. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,NULL),
  37334. SSL_FAILURE);
  37335. #endif
  37336. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  37337. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  37338. */
  37339. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,x509),
  37340. SSL_SUCCESS);
  37341. /* Test with SSL_CTRL_OPTIONS
  37342. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  37343. */
  37344. AssertTrue(wolfSSL_CTX_ctrl(ctx,SSL_CTRL_OPTIONS,SSL_OP_NO_TLSv1,NULL)
  37345. == SSL_OP_NO_TLSv1);
  37346. AssertTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  37347. /* Test with SSL_CTRL_SET_TMP_DH
  37348. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  37349. */
  37350. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  37351. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,dh),
  37352. SSL_SUCCESS);
  37353. #endif
  37354. /* Test with SSL_CTRL_SET_TMP_ECDH
  37355. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  37356. */
  37357. #ifdef HAVE_ECC
  37358. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,ecKey),
  37359. SSL_SUCCESS);
  37360. #endif
  37361. #ifdef WOLFSSL_ENCRYPTED_KEYS
  37362. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  37363. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  37364. #endif
  37365. /* Test for min/max proto */
  37366. #ifndef WOLFSSL_NO_TLS12
  37367. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  37368. 0, NULL), SSL_SUCCESS);
  37369. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  37370. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  37371. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  37372. #endif
  37373. #ifdef WOLFSSL_TLS13
  37374. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  37375. 0, NULL), SSL_SUCCESS);
  37376. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  37377. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  37378. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  37379. #ifndef WOLFSSL_NO_TLS12
  37380. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  37381. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  37382. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  37383. #endif
  37384. #endif
  37385. /* Cleanup and Pass */
  37386. #if !defined(NO_DH) && !defined(NO_DSA)
  37387. #ifndef NO_BIO
  37388. BIO_free(bio);
  37389. DSA_free(dsa);
  37390. DH_free(dh);
  37391. #endif
  37392. #endif
  37393. #ifdef HAVE_ECC
  37394. wolfSSL_EC_KEY_free(ecKey);
  37395. #endif
  37396. SSL_CTX_free(ctx);
  37397. printf(resultFmt, passed);
  37398. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  37399. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  37400. }
  37401. static void test_wolfSSL_DH_check(void)
  37402. {
  37403. #if !defined(NO_DH) && !defined(NO_DSA)
  37404. #ifndef NO_BIO
  37405. byte buf[6000];
  37406. char file[] = "./certs/dsaparams.pem";
  37407. XFILE f;
  37408. int bytes;
  37409. BIO* bio;
  37410. DSA* dsa;
  37411. DH* dh = NULL;
  37412. WOLFSSL_BIGNUM* pTmp = NULL;
  37413. WOLFSSL_BIGNUM* gTmp = NULL;
  37414. int codes = -1;
  37415. printf(testingFmt, "wolfSSL_DH_check");
  37416. /* Initialize DH */
  37417. f = XFOPEN(file, "rb");
  37418. AssertTrue((f != XBADFILE));
  37419. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  37420. XFCLOSE(f);
  37421. bio = BIO_new_mem_buf((void*)buf, bytes);
  37422. AssertNotNull(bio);
  37423. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  37424. AssertNotNull(dsa);
  37425. dh = wolfSSL_DSA_dup_DH(dsa);
  37426. AssertNotNull(dh);
  37427. /* Test assumed to be valid dh.
  37428. * Should return WOLFSSL_SUCCESS
  37429. * codes should be 0
  37430. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  37431. */
  37432. AssertIntEQ(wolfSSL_DH_check(dh, &codes), WOLFSSL_SUCCESS);
  37433. AssertIntEQ(codes, 0);
  37434. /* Test NULL dh: expected BAD_FUNC_ARG */
  37435. AssertIntEQ(wolfSSL_DH_check(NULL, &codes), WOLFSSL_FAILURE);
  37436. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  37437. pTmp = dh->p;
  37438. dh->p = NULL;
  37439. AssertIntEQ(wolfSSL_DH_check(dh, &codes), WOLFSSL_FAILURE);
  37440. AssertIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  37441. /* set dh->p back to normal so it wont fail on next tests */
  37442. dh->p = pTmp;
  37443. pTmp = NULL;
  37444. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  37445. gTmp = dh->g;
  37446. dh->g = NULL;
  37447. AssertIntEQ(wolfSSL_DH_check(dh, &codes), WOLFSSL_FAILURE);
  37448. AssertIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  37449. dh->g = gTmp;
  37450. gTmp = NULL;
  37451. /* Cleanup and Pass Test */
  37452. BIO_free(bio);
  37453. DSA_free(dsa);
  37454. DH_free(dh);
  37455. printf(resultFmt, passed);
  37456. #endif
  37457. #endif /* !NO_DH && !NO_DSA */
  37458. }
  37459. static void test_wolfSSL_EVP_PKEY_assign(void)
  37460. {
  37461. int type;
  37462. WOLFSSL_EVP_PKEY* pkey;
  37463. #ifndef NO_RSA
  37464. WOLFSSL_RSA* rsa;
  37465. #endif
  37466. #ifndef NO_DSA
  37467. WOLFSSL_DSA* dsa;
  37468. #endif
  37469. #ifdef HAVE_ECC
  37470. WOLFSSL_EC_KEY* ecKey;
  37471. #endif
  37472. (void)pkey;
  37473. printf(testingFmt, "wolfSSL_EVP_PKEY_assign");
  37474. #ifndef NO_RSA
  37475. type = EVP_PKEY_RSA;
  37476. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  37477. AssertNotNull(rsa = wolfSSL_RSA_new());
  37478. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,rsa), WOLFSSL_FAILURE);
  37479. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  37480. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,rsa), WOLFSSL_FAILURE);
  37481. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,rsa), WOLFSSL_SUCCESS);
  37482. wolfSSL_EVP_PKEY_free(pkey);
  37483. #endif /* NO_RSA */
  37484. #ifndef NO_DSA
  37485. type = EVP_PKEY_DSA;
  37486. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  37487. AssertNotNull(dsa = wolfSSL_DSA_new());
  37488. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,dsa), WOLFSSL_FAILURE);
  37489. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  37490. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,dsa), WOLFSSL_FAILURE);
  37491. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,dsa), WOLFSSL_SUCCESS);
  37492. wolfSSL_EVP_PKEY_free(pkey);
  37493. #endif /* NO_DSA */
  37494. #ifdef HAVE_ECC
  37495. type = EVP_PKEY_EC;
  37496. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  37497. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  37498. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,ecKey), WOLFSSL_FAILURE);
  37499. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  37500. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,ecKey), WOLFSSL_FAILURE);
  37501. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_FAILURE);
  37502. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  37503. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_SUCCESS);
  37504. wolfSSL_EVP_PKEY_free(pkey);
  37505. #endif /* HAVE_ECC */
  37506. (void)type;
  37507. printf(resultFmt, passed);
  37508. }
  37509. static void test_wolfSSL_EVP_PKEY_base_id(void)
  37510. {
  37511. WOLFSSL_EVP_PKEY* pkey;
  37512. printf(testingFmt, "wolfSSL_EVP_PKEY_base_id");
  37513. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  37514. AssertIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  37515. AssertIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  37516. EVP_PKEY_free(pkey);
  37517. printf(resultFmt, passed);
  37518. }
  37519. static void test_wolfSSL_EVP_PKEY_id(void)
  37520. {
  37521. WOLFSSL_EVP_PKEY* pkey;
  37522. printf(testingFmt, "wolfSSL_EVP_PKEY_id");
  37523. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  37524. AssertIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  37525. AssertIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  37526. EVP_PKEY_free(pkey);
  37527. printf(resultFmt, passed);
  37528. }
  37529. static void test_wolfSSL_EVP_PKEY_paramgen(void)
  37530. {
  37531. #if defined(OPENSSL_ALL) && \
  37532. !defined(NO_ECC_SECP) && \
  37533. /* This last bit is taken from ecc.c. It is the condition that
  37534. * defines ECC256 */ \
  37535. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  37536. ECC_MIN_KEY_SZ <= 256)
  37537. EVP_PKEY_CTX* ctx;
  37538. EVP_PKEY* pkey = NULL;
  37539. printf(testingFmt, "wolfSSL_EVP_PKEY_paramgen");
  37540. /* Test error conditions. */
  37541. AssertIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  37542. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  37543. AssertIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  37544. #ifndef NO_RSA
  37545. EVP_PKEY_CTX_free(ctx);
  37546. /* Parameter generation for RSA not supported yet. */
  37547. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  37548. AssertIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  37549. #endif
  37550. #ifdef HAVE_ECC
  37551. EVP_PKEY_CTX_free(ctx);
  37552. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  37553. AssertIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  37554. AssertIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  37555. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  37556. AssertIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  37557. AssertIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  37558. WOLFSSL_SUCCESS);
  37559. AssertIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  37560. AssertIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  37561. #endif
  37562. EVP_PKEY_CTX_free(ctx);
  37563. EVP_PKEY_free(pkey);
  37564. printf(resultFmt, passed);
  37565. #endif
  37566. }
  37567. static void test_wolfSSL_EVP_PKEY_keygen(void)
  37568. {
  37569. WOLFSSL_EVP_PKEY* pkey = NULL;
  37570. EVP_PKEY_CTX* ctx = NULL;
  37571. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  37572. WOLFSSL_EVP_PKEY* params = NULL;
  37573. DH* dh = NULL;
  37574. const BIGNUM* pubkey = NULL;
  37575. const BIGNUM* privkey = NULL;
  37576. ASN1_INTEGER* asn1int = NULL;
  37577. unsigned int length = 0;
  37578. byte* derBuffer = NULL;
  37579. #endif
  37580. printf(testingFmt, "wolfSSL_EVP_PKEY_keygen");
  37581. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  37582. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  37583. /* Bad cases */
  37584. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  37585. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  37586. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  37587. /* Good case */
  37588. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  37589. EVP_PKEY_CTX_free(ctx);
  37590. EVP_PKEY_free(pkey);
  37591. pkey = NULL;
  37592. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  37593. /* Test DH keygen */
  37594. {
  37595. AssertNotNull(params = wolfSSL_EVP_PKEY_new());
  37596. AssertNotNull(dh = DH_get_2048_256());
  37597. AssertIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  37598. AssertNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  37599. AssertIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  37600. AssertIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  37601. DH_free(dh);
  37602. EVP_PKEY_CTX_free(ctx);
  37603. EVP_PKEY_free(params);
  37604. /* try exporting generated key to DER, to verify */
  37605. AssertNotNull(dh = EVP_PKEY_get1_DH(pkey));
  37606. DH_get0_key(dh, &pubkey, &privkey);
  37607. AssertNotNull(pubkey);
  37608. AssertNotNull(privkey);
  37609. AssertNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  37610. AssertIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  37611. ASN1_INTEGER_free(asn1int);
  37612. DH_free(dh);
  37613. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  37614. EVP_PKEY_free(pkey);
  37615. }
  37616. #endif
  37617. printf(resultFmt, passed);
  37618. }
  37619. static void test_wolfSSL_EVP_PKEY_keygen_init(void)
  37620. {
  37621. WOLFSSL_EVP_PKEY* pkey;
  37622. EVP_PKEY_CTX *ctx;
  37623. printf(testingFmt, "wolfSSL_EVP_PKEY_keygen_init");
  37624. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  37625. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  37626. AssertIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  37627. EVP_PKEY_CTX_free(ctx);
  37628. EVP_PKEY_free(pkey);
  37629. printf(resultFmt, passed);
  37630. }
  37631. static void test_wolfSSL_EVP_PKEY_missing_parameters(void)
  37632. {
  37633. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  37634. WOLFSSL_EVP_PKEY* pkey;
  37635. printf(testingFmt, "wolfSSL_EVP_PKEY_missing_parameters");
  37636. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  37637. AssertIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  37638. EVP_PKEY_free(pkey);
  37639. printf(resultFmt, passed);
  37640. #endif
  37641. }
  37642. static void test_wolfSSL_EVP_PKEY_copy_parameters(void)
  37643. {
  37644. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  37645. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  37646. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  37647. !defined(NO_FILESYSTEM)
  37648. WOLFSSL_EVP_PKEY* params = NULL;
  37649. WOLFSSL_EVP_PKEY* copy = NULL;
  37650. DH* dh = NULL;
  37651. BIGNUM* p1;
  37652. BIGNUM* g1;
  37653. BIGNUM* q1;
  37654. BIGNUM* p2;
  37655. BIGNUM* g2;
  37656. BIGNUM* q2;
  37657. printf(testingFmt, "wolfSSL_EVP_PKEY_copy_parameters");
  37658. /* create DH with DH_get_2048_256 params */
  37659. AssertNotNull(params = wolfSSL_EVP_PKEY_new());
  37660. AssertNotNull(dh = DH_get_2048_256());
  37661. AssertIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  37662. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  37663. (const BIGNUM**)&q1,
  37664. (const BIGNUM**)&g1);
  37665. DH_free(dh);
  37666. /* create DH with random generated DH params */
  37667. AssertNotNull(copy = wolfSSL_EVP_PKEY_new());
  37668. AssertNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  37669. AssertIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  37670. DH_free(dh);
  37671. AssertIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  37672. AssertNotNull(dh = EVP_PKEY_get1_DH(copy));
  37673. AssertNotNull(dh->p);
  37674. AssertNotNull(dh->g);
  37675. AssertNotNull(dh->q);
  37676. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  37677. (const BIGNUM**)&q2,
  37678. (const BIGNUM**)&g2);
  37679. AssertIntEQ(BN_cmp(p1, p2), 0);
  37680. AssertIntEQ(BN_cmp(q1, q2), 0);
  37681. AssertIntEQ(BN_cmp(g1, g2), 0);
  37682. DH_free(dh);
  37683. EVP_PKEY_free(copy);
  37684. EVP_PKEY_free(params);
  37685. printf(resultFmt, passed);
  37686. #endif
  37687. }
  37688. static void test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  37689. {
  37690. WOLFSSL_EVP_PKEY* pkey;
  37691. EVP_PKEY_CTX *ctx;
  37692. int bits = 2048;
  37693. printf(testingFmt, "wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits");
  37694. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  37695. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  37696. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  37697. WOLFSSL_SUCCESS);
  37698. EVP_PKEY_CTX_free(ctx);
  37699. EVP_PKEY_free(pkey);
  37700. printf(resultFmt, passed);
  37701. }
  37702. static void test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  37703. {
  37704. /* This is large enough to be used for all key sizes */
  37705. byte key[AES_256_KEY_SIZE] = {0};
  37706. byte iv[AES_BLOCK_SIZE] = {0};
  37707. int i, enumlen;
  37708. EVP_CIPHER_CTX *ctx;
  37709. const EVP_CIPHER *init;
  37710. int enumArray[] = {
  37711. #ifdef HAVE_AES_CBC
  37712. NID_aes_128_cbc,
  37713. #endif
  37714. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  37715. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  37716. #ifdef HAVE_AESGCM
  37717. NID_aes_128_gcm,
  37718. #endif
  37719. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  37720. #ifdef WOLFSSL_AES_COUNTER
  37721. NID_aes_128_ctr,
  37722. #endif
  37723. #ifndef NO_DES3
  37724. NID_des_cbc,
  37725. NID_des_ede3_cbc,
  37726. #endif
  37727. };
  37728. int iv_lengths[] = {
  37729. #ifdef HAVE_AES_CBC
  37730. AES_BLOCK_SIZE,
  37731. #endif
  37732. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  37733. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  37734. #ifdef HAVE_AESGCM
  37735. GCM_NONCE_MID_SZ,
  37736. #endif
  37737. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  37738. #ifdef WOLFSSL_AES_COUNTER
  37739. AES_BLOCK_SIZE,
  37740. #endif
  37741. #ifndef NO_DES3
  37742. DES_BLOCK_SIZE,
  37743. DES_BLOCK_SIZE,
  37744. #endif
  37745. };
  37746. printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_iv_length");
  37747. enumlen = (sizeof(enumArray)/sizeof(int));
  37748. for(i = 0; i < enumlen; i++)
  37749. {
  37750. ctx = EVP_CIPHER_CTX_new();
  37751. init = wolfSSL_EVP_get_cipherbynid(enumArray[i]);
  37752. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  37753. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  37754. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  37755. EVP_CIPHER_CTX_free(ctx);
  37756. }
  37757. printf(resultFmt, passed);
  37758. }
  37759. static void test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  37760. {
  37761. #if !defined(NO_DES3)
  37762. byte key[AES_256_KEY_SIZE] = {0};
  37763. byte iv[AES_BLOCK_SIZE] = {0};
  37764. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  37765. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  37766. printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_key_length");
  37767. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  37768. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  37769. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), 24);
  37770. EVP_CIPHER_CTX_free(ctx);
  37771. printf(resultFmt, passed);
  37772. #endif
  37773. }
  37774. static void test_wolfSSL_EVP_CIPHER_CTX_set_key_length(void)
  37775. {
  37776. #if !defined(NO_DES3)
  37777. byte key[AES_256_KEY_SIZE] = {0};
  37778. byte iv[AES_BLOCK_SIZE] = {0};
  37779. int keylen;
  37780. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  37781. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  37782. printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_set_key_length");
  37783. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  37784. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  37785. keylen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  37786. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, keylen),
  37787. WOLFSSL_SUCCESS);
  37788. EVP_CIPHER_CTX_free(ctx);
  37789. printf(resultFmt, passed);
  37790. #endif
  37791. }
  37792. static void test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  37793. {
  37794. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  37795. byte key[DES3_KEY_SIZE] = {0};
  37796. byte iv[DES_BLOCK_SIZE] = {0};
  37797. int ivLen, keyLen;
  37798. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  37799. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  37800. printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_set_iv");
  37801. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  37802. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  37803. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  37804. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  37805. /* Bad cases */
  37806. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen), WOLFSSL_FAILURE);
  37807. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen), WOLFSSL_FAILURE);
  37808. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  37809. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  37810. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen), WOLFSSL_FAILURE);
  37811. /* Good case */
  37812. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  37813. EVP_CIPHER_CTX_free(ctx);
  37814. printf(resultFmt, passed);
  37815. #endif
  37816. }
  37817. static void test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  37818. {
  37819. WOLFSSL_ENGINE* e = NULL;
  37820. int id = 0;
  37821. EVP_PKEY_CTX *ctx;
  37822. printf(testingFmt, "wolfSSL_EVP_PKEY_CTX_new_id");
  37823. AssertNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  37824. EVP_PKEY_CTX_free(ctx);
  37825. printf(resultFmt, passed);
  37826. }
  37827. static void test_wolfSSL_EVP_rc4(void)
  37828. {
  37829. #if !defined(NO_RC4)
  37830. printf(testingFmt, "wolfSSL_EVP_rc4");
  37831. AssertNotNull(wolfSSL_EVP_rc4());
  37832. printf(resultFmt, passed);
  37833. #endif
  37834. }
  37835. static void test_wolfSSL_EVP_enc_null(void)
  37836. {
  37837. printf(testingFmt, "wolfSSL_EVP_enc_null");
  37838. AssertNotNull(wolfSSL_EVP_enc_null());
  37839. printf(resultFmt, passed);
  37840. }
  37841. static void test_wolfSSL_EVP_rc2_cbc(void)
  37842. {
  37843. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  37844. printf(testingFmt, "wolfSSL_EVP_rc2_cbc");
  37845. AssertNull(wolfSSL_EVP_rc2_cbc());
  37846. printf(resultFmt, passed);
  37847. #endif
  37848. }
  37849. static void test_wolfSSL_EVP_mdc2(void)
  37850. {
  37851. #if !defined(NO_WOLFSSL_STUB)
  37852. printf(testingFmt, "wolfSSL_EVP_mdc2");
  37853. AssertNull(wolfSSL_EVP_mdc2());
  37854. printf(resultFmt, passed);
  37855. #endif
  37856. }
  37857. static void test_wolfSSL_EVP_md4(void)
  37858. {
  37859. #if !defined(NO_MD4)
  37860. printf(testingFmt, "wolfSSL_EVP_md4");
  37861. AssertNotNull(wolfSSL_EVP_md4());
  37862. printf(resultFmt, passed);
  37863. #endif
  37864. }
  37865. static void test_wolfSSL_EVP_aes_256_gcm(void)
  37866. {
  37867. printf(testingFmt, "wolfSSL_EVP_aes_256_gcm");
  37868. AssertNotNull(wolfSSL_EVP_aes_256_gcm());
  37869. printf(resultFmt, passed);
  37870. }
  37871. static void test_wolfSSL_EVP_aes_192_gcm(void)
  37872. {
  37873. printf(testingFmt, "wolfSSL_EVP_aes_192_gcm");
  37874. AssertNotNull(wolfSSL_EVP_aes_192_gcm());
  37875. printf(resultFmt, passed);
  37876. }
  37877. static void test_wolfSSL_EVP_ripemd160(void)
  37878. {
  37879. #if !defined(NO_WOLFSSL_STUB)
  37880. printf(testingFmt, "wolfSSL_EVP_ripemd160");
  37881. AssertNull(wolfSSL_EVP_ripemd160());
  37882. printf(resultFmt, passed);
  37883. #endif
  37884. }
  37885. static void test_wolfSSL_EVP_get_digestbynid(void)
  37886. {
  37887. printf(testingFmt, "wolfSSL_EVP_get_digestbynid");
  37888. #ifndef NO_MD5
  37889. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  37890. #endif
  37891. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  37892. AssertNull(wolfSSL_EVP_get_digestbynid(0));
  37893. printf(resultFmt, passed);
  37894. }
  37895. static void test_wolfSSL_EVP_MD_nid(void)
  37896. {
  37897. printf(testingFmt, "wolfSSL_EVP_MD_nid");
  37898. #ifndef NO_MD5
  37899. AssertIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  37900. #endif
  37901. #ifndef NO_SHA
  37902. AssertIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  37903. #endif
  37904. #ifndef NO_SHA256
  37905. AssertIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  37906. #endif
  37907. AssertIntEQ(EVP_MD_nid(NULL), NID_undef);
  37908. printf(resultFmt, passed);
  37909. }
  37910. static void test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  37911. {
  37912. #if defined(HAVE_ECC)
  37913. WOLFSSL_EVP_PKEY* pkey;
  37914. printf(testingFmt, "wolfSSL_EVP_PKEY_get0_EC_KEY");
  37915. AssertNotNull(pkey = EVP_PKEY_new());
  37916. AssertNull(EVP_PKEY_get0_EC_KEY(pkey));
  37917. EVP_PKEY_free(pkey);
  37918. printf(resultFmt, passed);
  37919. #endif
  37920. }
  37921. static void test_wolfSSL_EVP_X_STATE(void)
  37922. {
  37923. #if !defined(NO_DES3) && !defined(NO_RC4)
  37924. byte key[DES3_KEY_SIZE] = {0};
  37925. byte iv[DES_IV_SIZE] = {0};
  37926. EVP_CIPHER_CTX *ctx;
  37927. const EVP_CIPHER *init;
  37928. printf(testingFmt, "wolfSSL_EVP_X_STATE");
  37929. /* Bad test cases */
  37930. ctx = EVP_CIPHER_CTX_new();
  37931. init = EVP_des_ede3_cbc();
  37932. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  37933. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  37934. AssertNull(wolfSSL_EVP_X_STATE(NULL));
  37935. AssertNull(wolfSSL_EVP_X_STATE(ctx));
  37936. EVP_CIPHER_CTX_free(ctx);
  37937. /* Good test case */
  37938. ctx = EVP_CIPHER_CTX_new();
  37939. init = wolfSSL_EVP_rc4();
  37940. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  37941. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  37942. AssertNotNull(wolfSSL_EVP_X_STATE(ctx));
  37943. EVP_CIPHER_CTX_free(ctx);
  37944. printf(resultFmt, passed);
  37945. #endif
  37946. }
  37947. static void test_wolfSSL_EVP_X_STATE_LEN(void)
  37948. {
  37949. #if !defined(NO_DES3) && !defined(NO_RC4)
  37950. byte key[DES3_KEY_SIZE] = {0};
  37951. byte iv[DES_IV_SIZE] = {0};
  37952. EVP_CIPHER_CTX *ctx;
  37953. const EVP_CIPHER *init;
  37954. printf(testingFmt, "wolfSSL_EVP_X_STATE_LEN");
  37955. /* Bad test cases */
  37956. ctx = EVP_CIPHER_CTX_new();
  37957. init = EVP_des_ede3_cbc();
  37958. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  37959. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  37960. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  37961. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  37962. EVP_CIPHER_CTX_free(ctx);
  37963. /* Good test case */
  37964. ctx = EVP_CIPHER_CTX_new();
  37965. init = wolfSSL_EVP_rc4();
  37966. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  37967. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  37968. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  37969. EVP_CIPHER_CTX_free(ctx);
  37970. printf(resultFmt, passed);
  37971. #endif
  37972. }
  37973. static void test_wolfSSL_EVP_CIPHER_block_size(void)
  37974. {
  37975. #ifdef HAVE_AES_CBC
  37976. #ifdef WOLFSSL_AES_128
  37977. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  37978. #endif
  37979. #ifdef WOLFSSL_AES_192
  37980. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  37981. #endif
  37982. #ifdef WOLFSSL_AES_256
  37983. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  37984. #endif
  37985. #endif
  37986. #ifdef HAVE_AESGCM
  37987. #ifdef WOLFSSL_AES_128
  37988. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  37989. #endif
  37990. #ifdef WOLFSSL_AES_192
  37991. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  37992. #endif
  37993. #ifdef WOLFSSL_AES_256
  37994. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  37995. #endif
  37996. #endif
  37997. #ifdef WOLFSSL_AES_COUNTER
  37998. #ifdef WOLFSSL_AES_128
  37999. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  38000. #endif
  38001. #ifdef WOLFSSL_AES_192
  38002. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  38003. #endif
  38004. #ifdef WOLFSSL_AES_256
  38005. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  38006. #endif
  38007. #endif
  38008. #ifdef HAVE_AES_ECB
  38009. #ifdef WOLFSSL_AES_128
  38010. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  38011. #endif
  38012. #ifdef WOLFSSL_AES_192
  38013. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  38014. #endif
  38015. #ifdef WOLFSSL_AES_256
  38016. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  38017. #endif
  38018. #endif
  38019. #ifdef WOLFSSL_AES_OFB
  38020. #ifdef WOLFSSL_AES_128
  38021. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  38022. #endif
  38023. #ifdef WOLFSSL_AES_192
  38024. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  38025. #endif
  38026. #ifdef WOLFSSL_AES_256
  38027. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  38028. #endif
  38029. #endif
  38030. #ifndef NO_RC4
  38031. AssertIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  38032. #endif
  38033. }
  38034. static void test_wolfSSL_EVP_CIPHER_iv_length(void)
  38035. {
  38036. int i, enumlen;
  38037. int enumArray[] = {
  38038. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  38039. #ifdef WOLFSSL_AES_128
  38040. NID_aes_128_cbc,
  38041. #endif
  38042. #ifdef WOLFSSL_AES_192
  38043. NID_aes_192_cbc,
  38044. #endif
  38045. #ifdef WOLFSSL_AES_256
  38046. NID_aes_256_cbc,
  38047. #endif
  38048. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  38049. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  38050. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  38051. #ifdef HAVE_AESGCM
  38052. #ifdef WOLFSSL_AES_128
  38053. NID_aes_128_gcm,
  38054. #endif
  38055. #ifdef WOLFSSL_AES_192
  38056. NID_aes_192_gcm,
  38057. #endif
  38058. #ifdef WOLFSSL_AES_256
  38059. NID_aes_256_gcm,
  38060. #endif
  38061. #endif /* HAVE_AESGCM */
  38062. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  38063. #ifdef WOLFSSL_AES_COUNTER
  38064. #ifdef WOLFSSL_AES_128
  38065. NID_aes_128_ctr,
  38066. #endif
  38067. #ifdef WOLFSSL_AES_192
  38068. NID_aes_192_ctr,
  38069. #endif
  38070. #ifdef WOLFSSL_AES_256
  38071. NID_aes_256_ctr,
  38072. #endif
  38073. #endif
  38074. #ifndef NO_DES3
  38075. NID_des_cbc,
  38076. NID_des_ede3_cbc,
  38077. #endif
  38078. };
  38079. int iv_lengths[] = {
  38080. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  38081. #ifdef WOLFSSL_AES_128
  38082. AES_BLOCK_SIZE,
  38083. #endif
  38084. #ifdef WOLFSSL_AES_192
  38085. AES_BLOCK_SIZE,
  38086. #endif
  38087. #ifdef WOLFSSL_AES_256
  38088. AES_BLOCK_SIZE,
  38089. #endif
  38090. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  38091. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  38092. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  38093. #ifdef HAVE_AESGCM
  38094. #ifdef WOLFSSL_AES_128
  38095. GCM_NONCE_MID_SZ,
  38096. #endif
  38097. #ifdef WOLFSSL_AES_192
  38098. GCM_NONCE_MID_SZ,
  38099. #endif
  38100. #ifdef WOLFSSL_AES_256
  38101. GCM_NONCE_MID_SZ,
  38102. #endif
  38103. #endif /* HAVE_AESGCM */
  38104. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  38105. #ifdef WOLFSSL_AES_COUNTER
  38106. #ifdef WOLFSSL_AES_128
  38107. AES_BLOCK_SIZE,
  38108. #endif
  38109. #ifdef WOLFSSL_AES_192
  38110. AES_BLOCK_SIZE,
  38111. #endif
  38112. #ifdef WOLFSSL_AES_256
  38113. AES_BLOCK_SIZE,
  38114. #endif
  38115. #endif
  38116. #ifndef NO_DES3
  38117. DES_BLOCK_SIZE,
  38118. DES_BLOCK_SIZE,
  38119. #endif
  38120. };
  38121. printf(testingFmt, "wolfSSL_EVP_CIPHER_iv_length");
  38122. enumlen = (sizeof(enumArray)/sizeof(int));
  38123. for(i = 0; i < enumlen; i++)
  38124. {
  38125. const EVP_CIPHER *c = EVP_get_cipherbynid(enumArray[i]);
  38126. AssertIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  38127. }
  38128. printf(resultFmt, passed);
  38129. }
  38130. static void test_wolfSSL_EVP_SignInit_ex(void)
  38131. {
  38132. WOLFSSL_EVP_MD_CTX mdCtx;
  38133. WOLFSSL_ENGINE* e = 0;
  38134. const EVP_MD* md;
  38135. md = "SHA256";
  38136. printf(testingFmt, "wolfSSL_EVP_SignInit_ex");
  38137. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  38138. AssertIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  38139. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  38140. printf(resultFmt, passed);
  38141. }
  38142. static void test_wolfSSL_EVP_DigestFinal_ex(void)
  38143. {
  38144. #if !defined(NO_SHA256)
  38145. WOLFSSL_EVP_MD_CTX mdCtx;
  38146. unsigned int s = 0;
  38147. unsigned char md[WC_SHA256_DIGEST_SIZE];
  38148. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  38149. printf(testingFmt, "wolfSSL_EVP_DigestFinal_ex");
  38150. /* Bad Case */
  38151. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  38152. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  38153. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  38154. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  38155. #else
  38156. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  38157. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  38158. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  38159. #endif
  38160. /* Good Case */
  38161. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  38162. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), WOLFSSL_SUCCESS);
  38163. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  38164. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  38165. printf(resultFmt, passed);
  38166. #endif
  38167. }
  38168. static void test_wolfSSL_EVP_PKEY_assign_DH(void)
  38169. {
  38170. #if !defined(NO_DH) && \
  38171. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  38172. FILE* f = NULL;
  38173. unsigned char buf[4096];
  38174. const unsigned char* pt = buf;
  38175. const char* params1 = "./certs/dh2048.der";
  38176. long len = 0;
  38177. WOLFSSL_DH* dh = NULL;
  38178. WOLFSSL_EVP_PKEY* pkey;
  38179. XMEMSET(buf, 0, sizeof(buf));
  38180. f = XFOPEN(params1, "rb");
  38181. AssertTrue(f != XBADFILE);
  38182. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  38183. XFCLOSE(f);
  38184. printf(testingFmt, "wolfSSL_EVP_PKEY_assign_DH");
  38185. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  38186. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  38187. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38188. /* Bad cases */
  38189. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  38190. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  38191. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  38192. /* Good case */
  38193. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  38194. EVP_PKEY_free(pkey);
  38195. printf(resultFmt, passed);
  38196. #endif
  38197. }
  38198. static void test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  38199. {
  38200. #if defined(OPENSSL_EXTRA)
  38201. EVP_PKEY* pkey;
  38202. EVP_PKEY_CTX* ctx;
  38203. printf(testingFmt, "test_wolfSSL_QT_EVP_PKEY_CTX_free");
  38204. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38205. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  38206. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  38207. /* void */
  38208. EVP_PKEY_CTX_free(ctx);
  38209. AssertTrue(1);
  38210. #else
  38211. /* int */
  38212. AssertIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  38213. #endif
  38214. EVP_PKEY_free(pkey);
  38215. printf(resultFmt, passed);
  38216. #endif
  38217. }
  38218. static void test_wolfSSL_EVP_PKEY_param_check(void)
  38219. {
  38220. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  38221. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  38222. DH *dh = NULL;
  38223. DH *setDh = NULL;
  38224. EVP_PKEY *pkey = NULL;
  38225. EVP_PKEY_CTX* ctx = NULL;
  38226. FILE* f = NULL;
  38227. unsigned char buf[512];
  38228. const unsigned char* pt = buf;
  38229. const char* dh2048 = "./certs/dh2048.der";
  38230. long len = 0;
  38231. int code = -1;
  38232. printf(testingFmt, "test_wolfSSL_EVP_PKEY_param_check");
  38233. XMEMSET(buf, 0, sizeof(buf));
  38234. f = XFOPEN(dh2048, "rb");
  38235. AssertTrue(f != XBADFILE);
  38236. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  38237. XFCLOSE(f);
  38238. /* Load dh2048.der into DH with internal format */
  38239. AssertNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  38240. AssertIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  38241. AssertIntEQ(code, 0);
  38242. code = -1;
  38243. pkey = wolfSSL_EVP_PKEY_new();
  38244. /* Set DH into PKEY */
  38245. AssertIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  38246. /* create ctx from pkey */
  38247. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  38248. AssertIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  38249. /* */
  38250. /* TO DO invlaid case */
  38251. /* */
  38252. EVP_PKEY_CTX_free(ctx);
  38253. EVP_PKEY_free(pkey);
  38254. DH_free(setDh);
  38255. DH_free(dh);
  38256. printf(resultFmt, passed);
  38257. #endif
  38258. #endif
  38259. }
  38260. static void test_wolfSSL_EVP_BytesToKey(void)
  38261. {
  38262. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  38263. byte key[AES_BLOCK_SIZE] = {0};
  38264. byte iv[AES_BLOCK_SIZE] = {0};
  38265. int sz = 5;
  38266. int count = 0;
  38267. const EVP_MD* md = "SHA256";
  38268. const EVP_CIPHER *type;
  38269. const unsigned char *salt = (unsigned char *)"salt1234";
  38270. const byte data[] = {
  38271. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  38272. 0x72,0x6c,0x64
  38273. };
  38274. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  38275. printf(testingFmt, "EVP_BytesToKey");
  38276. /* Bad cases */
  38277. AssertIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  38278. 0);
  38279. AssertIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  38280. 16);
  38281. md = "2";
  38282. AssertIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  38283. WOLFSSL_FAILURE);
  38284. /* Good case */
  38285. md = "SHA256";
  38286. AssertIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  38287. 16);
  38288. printf(resultFmt, passed);
  38289. #endif
  38290. }
  38291. static void test_evp_cipher_aes_gcm(void)
  38292. {
  38293. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  38294. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  38295. (HAVE_FIPS_VERSION >= 2)))
  38296. /*
  38297. * This test checks data at various points in the encrypt/decrypt process
  38298. * against known values produced using the same test with OpenSSL. This
  38299. * interop testing is critical for verifying the correctness of our
  38300. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  38301. * a flow supported by OpenSSL that uses the control command
  38302. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  38303. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  38304. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  38305. * wolfSSL OpenSSH clients because there was a bug in this flow that
  38306. * happened to "cancel out" if both sides of the connection had the bug.
  38307. */
  38308. enum {
  38309. NUM_ENCRYPTIONS = 3,
  38310. AAD_SIZE = 4
  38311. };
  38312. byte plainText1[] = {
  38313. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  38314. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  38315. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  38316. };
  38317. byte plainText2[] = {
  38318. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  38319. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  38320. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  38321. };
  38322. byte plainText3[] = {
  38323. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  38324. 0x5b, 0x57, 0x10
  38325. };
  38326. byte* plainTexts[NUM_ENCRYPTIONS] = {
  38327. plainText1,
  38328. plainText2,
  38329. plainText3
  38330. };
  38331. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  38332. sizeof(plainText1),
  38333. sizeof(plainText2),
  38334. sizeof(plainText3)
  38335. };
  38336. byte aad1[AAD_SIZE] = {
  38337. 0x00, 0x00, 0x00, 0x01
  38338. };
  38339. byte aad2[AAD_SIZE] = {
  38340. 0x00, 0x00, 0x00, 0x10
  38341. };
  38342. byte aad3[AAD_SIZE] = {
  38343. 0x00, 0x00, 0x01, 0x00
  38344. };
  38345. byte* aads[NUM_ENCRYPTIONS] = {
  38346. aad1,
  38347. aad2,
  38348. aad3
  38349. };
  38350. const byte iv[GCM_NONCE_MID_SZ] = {
  38351. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  38352. };
  38353. byte currentIv[GCM_NONCE_MID_SZ];
  38354. const byte key[] = {
  38355. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  38356. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  38357. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  38358. };
  38359. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  38360. {
  38361. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  38362. 0xEF
  38363. },
  38364. {
  38365. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  38366. 0xF0
  38367. },
  38368. {
  38369. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  38370. 0xF1
  38371. }
  38372. };
  38373. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  38374. {
  38375. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  38376. 0x3D, 0x32, 0x18, 0x34, 0xA9
  38377. },
  38378. {
  38379. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  38380. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  38381. },
  38382. {
  38383. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  38384. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  38385. }
  38386. };
  38387. const byte expCipherText1[] = {
  38388. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  38389. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  38390. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  38391. };
  38392. const byte expCipherText2[] = {
  38393. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  38394. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  38395. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  38396. };
  38397. const byte expCipherText3[] = {
  38398. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  38399. 0x80, 0x5E, 0x6B,
  38400. };
  38401. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  38402. expCipherText1,
  38403. expCipherText2,
  38404. expCipherText3
  38405. };
  38406. byte* cipherText;
  38407. byte* calcPlainText;
  38408. byte tag[AES_BLOCK_SIZE];
  38409. EVP_CIPHER_CTX* encCtx = NULL;
  38410. EVP_CIPHER_CTX* decCtx = NULL;
  38411. int i, j, outl;
  38412. printf(testingFmt, "test_evp_cipher_aes_gcm");
  38413. /****************************************************/
  38414. for (i = 0; i < 3; ++i) {
  38415. AssertNotNull(encCtx = EVP_CIPHER_CTX_new());
  38416. AssertNotNull(decCtx = EVP_CIPHER_CTX_new());
  38417. /* First iteration, set key before IV. */
  38418. if (i == 0) {
  38419. AssertIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  38420. SSL_SUCCESS);
  38421. /*
  38422. * The call to EVP_CipherInit below (with NULL key) should clear the
  38423. * gcmIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  38424. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  38425. * behavior.
  38426. */
  38427. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  38428. (void*)iv), SSL_SUCCESS);
  38429. AssertIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  38430. SSL_SUCCESS);
  38431. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  38432. currentIv), SSL_FAILURE);
  38433. AssertIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  38434. SSL_SUCCESS);
  38435. AssertIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  38436. SSL_SUCCESS);
  38437. }
  38438. /* Second iteration, IV before key. */
  38439. else {
  38440. AssertIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  38441. SSL_SUCCESS);
  38442. AssertIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  38443. SSL_SUCCESS);
  38444. AssertIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  38445. SSL_SUCCESS);
  38446. AssertIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  38447. SSL_SUCCESS);
  38448. }
  38449. /*
  38450. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  38451. * been issued first.
  38452. */
  38453. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  38454. currentIv), SSL_FAILURE);
  38455. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  38456. (void*)iv), SSL_SUCCESS);
  38457. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  38458. (void*)iv), SSL_SUCCESS);
  38459. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  38460. /*************** Encrypt ***************/
  38461. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  38462. currentIv), SSL_SUCCESS);
  38463. /* Check current IV against expected. */
  38464. AssertIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  38465. /* Add AAD. */
  38466. if (i == 2) {
  38467. /* Test streaming API. */
  38468. AssertIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  38469. AAD_SIZE), SSL_SUCCESS);
  38470. }
  38471. else {
  38472. AssertIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  38473. AAD_SIZE);
  38474. }
  38475. AssertNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  38476. DYNAMIC_TYPE_TMP_BUFFER));
  38477. /* Encrypt plaintext. */
  38478. if (i == 2){
  38479. AssertIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  38480. plainTexts[j], plainTextSzs[j]),
  38481. SSL_SUCCESS);
  38482. }
  38483. else {
  38484. AssertIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  38485. plainTextSzs[j]), plainTextSzs[j]);
  38486. }
  38487. if (i == 2) {
  38488. AssertIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  38489. SSL_SUCCESS);
  38490. }
  38491. else {
  38492. /*
  38493. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  38494. * akin to calling EVP_CipherFinal.
  38495. */
  38496. AssertIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  38497. }
  38498. /* Check ciphertext against expected. */
  38499. AssertIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  38500. 0);
  38501. /* Get and check tag against expected. */
  38502. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  38503. sizeof(tag), tag), SSL_SUCCESS);
  38504. AssertIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  38505. /*************** Decrypt ***************/
  38506. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  38507. currentIv), SSL_SUCCESS);
  38508. /* Check current IV against expected. */
  38509. AssertIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  38510. /* Add AAD. */
  38511. if (i == 2) {
  38512. /* Test streaming API. */
  38513. AssertIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  38514. AAD_SIZE), SSL_SUCCESS);
  38515. }
  38516. else {
  38517. AssertIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  38518. AAD_SIZE);
  38519. }
  38520. /* Set expected tag. */
  38521. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  38522. sizeof(tag), tag), SSL_SUCCESS);
  38523. /* Decrypt ciphertext. */
  38524. AssertNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  38525. DYNAMIC_TYPE_TMP_BUFFER));
  38526. if (i == 2){
  38527. AssertIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  38528. cipherText, plainTextSzs[j]),
  38529. SSL_SUCCESS);
  38530. }
  38531. else {
  38532. /* This first EVP_Cipher call will check the tag, too. */
  38533. AssertIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  38534. plainTextSzs[j]), plainTextSzs[j]);
  38535. }
  38536. if (i == 2) {
  38537. AssertIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  38538. SSL_SUCCESS);
  38539. }
  38540. else {
  38541. AssertIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  38542. }
  38543. /* Check plaintext against expected. */
  38544. AssertIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  38545. 0);
  38546. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38547. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38548. }
  38549. EVP_CIPHER_CTX_free(encCtx);
  38550. EVP_CIPHER_CTX_free(decCtx);
  38551. }
  38552. printf(resultFmt, passed);
  38553. #endif
  38554. }
  38555. static void test_wolfSSL_OBJ_ln(void)
  38556. {
  38557. const int nid_set[] = {
  38558. NID_commonName,
  38559. NID_serialNumber,
  38560. NID_countryName,
  38561. NID_localityName,
  38562. NID_stateOrProvinceName,
  38563. NID_organizationName,
  38564. NID_organizationalUnitName,
  38565. NID_domainComponent,
  38566. NID_businessCategory,
  38567. NID_jurisdictionCountryName,
  38568. NID_jurisdictionStateOrProvinceName,
  38569. NID_emailAddress
  38570. };
  38571. const char* ln_set[] = {
  38572. "commonName",
  38573. "serialNumber",
  38574. "countryName",
  38575. "localityName",
  38576. "stateOrProvinceName",
  38577. "organizationName",
  38578. "organizationalUnitName",
  38579. "domainComponent",
  38580. "businessCategory",
  38581. "jurisdictionCountryName",
  38582. "jurisdictionStateOrProvinceName",
  38583. "emailAddress",
  38584. };
  38585. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  38586. printf(testingFmt, "wolfSSL_OBJ_ln");
  38587. AssertIntEQ(OBJ_ln2nid(NULL), NID_undef);
  38588. #ifdef HAVE_ECC
  38589. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  38590. {
  38591. EC_builtin_curve r[27];
  38592. size_t nCurves = sizeof(r) / sizeof(r[0]);
  38593. nCurves = EC_get_builtin_curves(r,nCurves);
  38594. for (i = 0; i < nCurves; i++) {
  38595. /* skip ECC_CURVE_INVALID */
  38596. if (r[i].nid != ECC_CURVE_INVALID) {
  38597. AssertIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  38598. AssertStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  38599. }
  38600. }
  38601. }
  38602. #endif
  38603. #endif
  38604. for (i = 0; i < maxIdx; i++) {
  38605. AssertIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  38606. AssertStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  38607. }
  38608. printf(resultFmt, passed);
  38609. }
  38610. static void test_wolfSSL_OBJ_sn(void)
  38611. {
  38612. int i = 0, maxIdx = 7;
  38613. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  38614. NID_stateOrProvinceName,NID_organizationName,
  38615. NID_organizationalUnitName,NID_emailAddress};
  38616. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  38617. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  38618. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  38619. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  38620. WOLFSSL_EMAIL_ADDR};
  38621. printf(testingFmt, "wolfSSL_OBJ_sn");
  38622. AssertIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  38623. for (i = 0; i < maxIdx; i++) {
  38624. AssertIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  38625. AssertStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  38626. }
  38627. printf(resultFmt, passed);
  38628. }
  38629. #if !defined(NO_BIO)
  38630. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  38631. {
  38632. return lh_strhash(s[3]);
  38633. }
  38634. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  38635. {
  38636. return XSTRCMP(a[3], b[3]);
  38637. }
  38638. #endif
  38639. static void test_wolfSSL_TXT_DB(void)
  38640. {
  38641. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  38642. BIO *bio;
  38643. TXT_DB *db = NULL;
  38644. const int columns = 6;
  38645. const char *fields[6] = {
  38646. "V",
  38647. "320926161116Z",
  38648. "",
  38649. "12BD",
  38650. "unknown",
  38651. "/CN=rsa doe",
  38652. };
  38653. char** fields_copy;
  38654. printf(testingFmt, "wolfSSL_TXT_DB");
  38655. /* Test read */
  38656. AssertNotNull(bio = BIO_new(BIO_s_file()));
  38657. AssertIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  38658. AssertNotNull(db = TXT_DB_read(bio, columns));
  38659. AssertNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  38660. DYNAMIC_TYPE_OPENSSL));
  38661. XMEMCPY(fields_copy, fields, sizeof(fields));
  38662. AssertIntEQ(TXT_DB_insert(db, fields_copy), 1);
  38663. BIO_free(bio);
  38664. /* Test write */
  38665. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  38666. AssertIntEQ(TXT_DB_write(bio, db), 1484);
  38667. BIO_free(bio);
  38668. /* Test index */
  38669. AssertIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  38670. (wolf_sk_compare_cb)TXT_DB_cmp), 1);
  38671. AssertNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  38672. fields[3] = "12DA";
  38673. AssertNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  38674. fields[3] = "FFFF";
  38675. AssertNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  38676. fields[3] = "";
  38677. AssertNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  38678. TXT_DB_free(db);
  38679. printf(resultFmt, passed);
  38680. #endif
  38681. }
  38682. static void test_wolfSSL_NCONF(void)
  38683. {
  38684. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  38685. const char* confFile = "./tests/NCONF_test.cnf";
  38686. CONF* conf = NULL;
  38687. long eline = 0;
  38688. long num = 0;
  38689. printf(testingFmt, "wolfSSL_NCONF");
  38690. AssertNotNull(conf = NCONF_new(NULL));
  38691. AssertIntEQ(NCONF_load(conf, confFile, &eline), 1);
  38692. AssertIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  38693. AssertIntEQ(num, 1234);
  38694. AssertIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  38695. AssertIntEQ(num, 4321);
  38696. AssertStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  38697. AssertStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  38698. "./test-dir/file1");
  38699. AssertStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  38700. "./test-dir/file1:./test-dir/file2:./section1:file2");
  38701. NCONF_free(conf);
  38702. printf(resultFmt, passed);
  38703. #endif
  38704. }
  38705. #endif /* OPENSSL_ALL */
  38706. static void test_wolfSSL_EC_KEY_set_group(void)
  38707. {
  38708. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  38709. defined(OPENSSL_EXTRA)
  38710. EC_KEY *key = NULL;
  38711. EC_GROUP *group = NULL;
  38712. const EC_GROUP *group2 = NULL;
  38713. printf(testingFmt, "wolfSSL_EC_KEY_dup()");
  38714. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  38715. AssertNotNull(key = EC_KEY_new());
  38716. AssertIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  38717. AssertNotNull(group2 = EC_KEY_get0_group(key));
  38718. AssertIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  38719. EC_GROUP_free(group);
  38720. EC_KEY_free(key);
  38721. printf(resultFmt, passed);
  38722. #endif
  38723. }
  38724. static void test_wolfSSL_EC_KEY_set_conv_form(void)
  38725. {
  38726. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA)
  38727. BIO* bio;
  38728. EC_KEY* key;
  38729. printf(testingFmt, "test_wolfSSL_EC_KEY_set_conv_form");
  38730. /* Error condition: NULL key. */
  38731. AssertIntLT(EC_KEY_get_conv_form(NULL), 0);
  38732. AssertNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  38733. AssertNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  38734. /* Conversion form defaults to uncompressed. */
  38735. AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  38736. #ifdef HAVE_COMP_KEY
  38737. /* Explicitly set to compressed. */
  38738. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  38739. AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  38740. #endif
  38741. BIO_free(bio);
  38742. EC_KEY_free(key);
  38743. printf(resultFmt, passed);
  38744. #endif
  38745. }
  38746. static void test_wolfSSL_EC_KEY_print_fp(void)
  38747. {
  38748. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  38749. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256 && \
  38750. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  38751. !defined(NO_STDIO_FILESYSTEM)
  38752. EC_KEY* key = NULL;
  38753. printf(testingFmt, "test_wolfSSL_EC_KEY_print_fp");
  38754. /* Bad file pointer. */
  38755. AssertIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  38756. /* NULL key. */
  38757. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stdout, NULL, 0), WOLFSSL_FAILURE);
  38758. AssertNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  38759. /* Negative indent. */
  38760. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stdout, key, -1), WOLFSSL_FAILURE);
  38761. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stdout, key, 4), WOLFSSL_SUCCESS);
  38762. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  38763. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stdout, key, 4), WOLFSSL_SUCCESS);
  38764. wolfSSL_EC_KEY_free(key);
  38765. AssertNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  38766. NID_X9_62_prime256v1)));
  38767. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  38768. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stdout, key, 4), WOLFSSL_SUCCESS);
  38769. wolfSSL_EC_KEY_free(key);
  38770. printf(resultFmt, passed);
  38771. #endif
  38772. }
  38773. static void test_wolfSSL_X509V3_EXT_get(void) {
  38774. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  38775. FILE* f;
  38776. int numOfExt =0;
  38777. int extNid = 0;
  38778. int i = 0;
  38779. WOLFSSL_X509* x509;
  38780. WOLFSSL_X509_EXTENSION* ext;
  38781. const WOLFSSL_v3_ext_method* method;
  38782. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  38783. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  38784. fclose(f);
  38785. printf(testingFmt, "wolfSSL_X509V3_EXT_get() return struct and nid test");
  38786. AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  38787. for (i = 0; i < numOfExt; i++) {
  38788. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  38789. AssertIntNE((extNid = ext->obj->nid), NID_undef);
  38790. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  38791. AssertIntEQ(method->ext_nid, extNid);
  38792. }
  38793. printf(resultFmt, "passed");
  38794. printf(testingFmt, "wolfSSL_X509V3_EXT_get() NULL argument test");
  38795. AssertNull(method = wolfSSL_X509V3_EXT_get(NULL));
  38796. printf(resultFmt, "passed");
  38797. wolfSSL_X509_free(x509);
  38798. #endif
  38799. }
  38800. static void test_wolfSSL_X509V3_EXT_nconf(void)
  38801. {
  38802. #ifdef OPENSSL_ALL
  38803. const char *ext_names[] = {
  38804. "subjectKeyIdentifier",
  38805. "authorityKeyIdentifier",
  38806. "subjectAltName",
  38807. "keyUsage",
  38808. };
  38809. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  38810. int ext_nids[] = {
  38811. NID_subject_key_identifier,
  38812. NID_authority_key_identifier,
  38813. NID_subject_alt_name,
  38814. NID_key_usage,
  38815. };
  38816. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  38817. const char *ext_values[] = {
  38818. "hash",
  38819. "hash",
  38820. "DNS:example.com, IP:127.0.0.1",
  38821. "digitalSignature,keyEncipherment,dataEncipherment",
  38822. };
  38823. size_t i;
  38824. X509_EXTENSION* ext;
  38825. X509* x509 = X509_new();
  38826. printf(testingFmt, "wolfSSL_X509V3_EXT_nconf()");
  38827. for (i = 0; i < ext_names_count; i++) {
  38828. ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i], ext_values[i]);
  38829. AssertNotNull(ext);
  38830. X509_EXTENSION_free(ext);
  38831. }
  38832. for (i = 0; i < ext_nids_count; i++) {
  38833. ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i], ext_values[i]);
  38834. AssertNotNull(ext);
  38835. X509_EXTENSION_free(ext);
  38836. }
  38837. /* Test adding extension to X509 */
  38838. for (i = 0; i < ext_nids_count; i++) {
  38839. ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i], ext_values[i]);
  38840. AssertIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  38841. X509_EXTENSION_free(ext);
  38842. }
  38843. X509_free(x509);
  38844. printf(resultFmt, "passed");
  38845. #endif
  38846. }
  38847. static void test_wolfSSL_X509V3_EXT(void) {
  38848. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  38849. FILE* f;
  38850. int numOfExt = 0, nid = 0, i = 0, expected, actual;
  38851. char* str;
  38852. unsigned char* data;
  38853. const WOLFSSL_v3_ext_method* method;
  38854. WOLFSSL_X509* x509;
  38855. WOLFSSL_X509_EXTENSION* ext;
  38856. WOLFSSL_X509_EXTENSION* ext2;
  38857. WOLFSSL_ASN1_OBJECT *obj, *adObj;
  38858. WOLFSSL_ASN1_STRING* asn1str;
  38859. WOLFSSL_AUTHORITY_KEYID* aKeyId;
  38860. WOLFSSL_AUTHORITY_INFO_ACCESS* aia;
  38861. WOLFSSL_BASIC_CONSTRAINTS* bc;
  38862. WOLFSSL_ACCESS_DESCRIPTION* ad;
  38863. WOLFSSL_GENERAL_NAME* gn;
  38864. printf(testingFmt, "wolfSSL_X509V3_EXT_d2i()");
  38865. /* Check NULL argument */
  38866. AssertNull(wolfSSL_X509V3_EXT_d2i(NULL));
  38867. /* Using OCSP cert with X509V3 extensions */
  38868. AssertNotNull(f = fopen("./certs/ocsp/root-ca-cert.pem", "rb"));
  38869. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  38870. fclose(f);
  38871. AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  38872. /* Basic Constraints */
  38873. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  38874. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  38875. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  38876. AssertNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  38877. AssertIntEQ(bc->ca, 1);
  38878. AssertNull(bc->pathlen);
  38879. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  38880. i++;
  38881. /* Subject Key Identifier */
  38882. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  38883. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  38884. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  38885. AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  38886. AssertNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  38887. asn1str));
  38888. X509_EXTENSION_free(ext2);
  38889. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  38890. AssertNotNull(method->i2s);
  38891. AssertNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  38892. wolfSSL_ASN1_STRING_free(asn1str);
  38893. actual = strcmp(str,
  38894. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  38895. AssertIntEQ(actual, 0);
  38896. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38897. i++;
  38898. /* Authority Key Identifier */
  38899. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  38900. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  38901. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  38902. AssertNotNull(aKeyId =
  38903. (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(ext));
  38904. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  38905. AssertNotNull(asn1str = aKeyId->keyid);
  38906. AssertNotNull(str =
  38907. wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method, asn1str));
  38908. actual = strcmp(str,
  38909. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  38910. AssertIntEQ(actual, 0);
  38911. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38912. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  38913. i++;
  38914. /* Key Usage */
  38915. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  38916. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  38917. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  38918. AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  38919. #if defined(WOLFSSL_QT)
  38920. AssertNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  38921. #else
  38922. AssertNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  38923. #endif
  38924. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  38925. #ifdef BIG_ENDIAN_ORDER
  38926. actual = data[1];
  38927. #else
  38928. actual = data[0];
  38929. #endif
  38930. AssertIntEQ(actual, expected);
  38931. wolfSSL_ASN1_STRING_free(asn1str);
  38932. #if 1
  38933. i++;
  38934. /* Authority Info Access */
  38935. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  38936. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  38937. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  38938. AssertNotNull(aia =
  38939. (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext));
  38940. #if defined(WOLFSSL_QT)
  38941. AssertIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  38942. #else
  38943. AssertIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  38944. #endif
  38945. /* URI entry is an ACCESS_DESCRIPTION type */
  38946. #if defined(WOLFSSL_QT)
  38947. AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  38948. #else
  38949. AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  38950. #endif
  38951. AssertNotNull(adObj = ad->method);
  38952. /* Make sure nid is OCSP */
  38953. AssertIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  38954. /* GENERAL_NAME stores URI as an ASN1_STRING */
  38955. AssertNotNull(gn = ad->location);
  38956. AssertIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  38957. AssertNotNull(asn1str = gn->d.uniformResourceIdentifier);
  38958. AssertIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  38959. #if defined(WOLFSSL_QT)
  38960. str = (char*)ASN1_STRING_get0_data(asn1str);
  38961. #else
  38962. str = (char*)wolfSSL_ASN1_STRING_data(asn1str);
  38963. #endif
  38964. actual = strcmp(str, "http://127.0.0.1:22220");
  38965. AssertIntEQ(actual, 0);
  38966. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  38967. #else
  38968. (void) aia; (void) ad; (void) adObj; (void) gn;
  38969. #endif
  38970. wolfSSL_X509_free(x509);
  38971. printf(resultFmt, "passed");
  38972. #endif
  38973. }
  38974. static void test_wolfSSL_X509_get_extension_flags(void)
  38975. {
  38976. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  38977. XFILE f;
  38978. X509* x509;
  38979. unsigned int extFlags;
  38980. unsigned int keyUsageFlags;
  38981. unsigned int extKeyUsageFlags;
  38982. printf(testingFmt, "test_wolfSSL_X509_get_extension_flags");
  38983. /* client-int-cert.pem has the following extension flags. */
  38984. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  38985. /* and the following key usage flags. */
  38986. keyUsageFlags = KU_DIGITAL_SIGNATURE
  38987. | KU_NON_REPUDIATION
  38988. | KU_KEY_ENCIPHERMENT;
  38989. /* and the following extended key usage flags. */
  38990. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  38991. f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb");
  38992. AssertTrue(f != XBADFILE);
  38993. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  38994. XFCLOSE(f);
  38995. AssertIntEQ(X509_get_extension_flags(x509), extFlags);
  38996. AssertIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  38997. AssertIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  38998. X509_free(x509);
  38999. /* client-cert-ext.pem has the following extension flags. */
  39000. extFlags = EXFLAG_KUSAGE;
  39001. /* and the following key usage flags. */
  39002. keyUsageFlags = KU_DIGITAL_SIGNATURE
  39003. | KU_KEY_CERT_SIGN
  39004. | KU_CRL_SIGN;
  39005. AssertNotNull(f = fopen("./certs/client-cert-ext.pem", "rb"));
  39006. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  39007. XFCLOSE(f);
  39008. AssertIntEQ(X509_get_extension_flags(x509), extFlags);
  39009. AssertIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  39010. X509_free(x509);
  39011. printf(resultFmt, passed);
  39012. #endif /* OPENSSL_ALL */
  39013. }
  39014. static void test_wolfSSL_X509_get_ext(void){
  39015. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  39016. int ret = 0;
  39017. FILE* f;
  39018. WOLFSSL_X509* x509;
  39019. WOLFSSL_X509_EXTENSION* foundExtension;
  39020. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  39021. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  39022. fclose(f);
  39023. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  39024. printf(testingFmt, "wolfSSL_X509_get_ext() valid input");
  39025. AssertNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  39026. printf(resultFmt, "passed");
  39027. printf(testingFmt, "wolfSSL_X509_get_ext() valid x509, idx out of bounds");
  39028. AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  39029. AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  39030. printf(resultFmt, "passed");
  39031. printf(testingFmt, "wolfSSL_X509_get_ext() NULL x509, idx out of bounds");
  39032. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  39033. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  39034. printf(resultFmt, "passed");
  39035. printf(testingFmt, "wolfSSL_X509_get_ext() NULL x509, valid idx");
  39036. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  39037. printf(resultFmt, "passed");
  39038. wolfSSL_X509_free(x509);
  39039. #endif
  39040. }
  39041. static void test_wolfSSL_X509_get_ext_by_NID(void)
  39042. {
  39043. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  39044. int rc;
  39045. FILE* f;
  39046. WOLFSSL_X509* x509;
  39047. ASN1_OBJECT* obj = NULL;
  39048. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  39049. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  39050. fclose(f);
  39051. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
  39052. AssertIntGE(rc, 0);
  39053. /* Start search from last location (should fail) */
  39054. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, rc);
  39055. AssertIntGE(rc, -1);
  39056. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -2);
  39057. AssertIntGE(rc, -1);
  39058. rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints, -1);
  39059. AssertIntEQ(rc, -1);
  39060. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1);
  39061. AssertIntEQ(rc, -1);
  39062. /* NID_ext_key_usage, check also its nid and oid */
  39063. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1);
  39064. AssertIntGT(rc, -1);
  39065. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(x509, rc)));
  39066. AssertIntEQ(obj->nid, NID_ext_key_usage);
  39067. AssertIntEQ(obj->type, EXT_KEY_USAGE_OID);
  39068. wolfSSL_X509_free(x509);
  39069. #endif
  39070. }
  39071. static void test_wolfSSL_X509_get_ext_subj_alt_name(void)
  39072. {
  39073. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  39074. int rc;
  39075. XFILE f;
  39076. WOLFSSL_X509* x509;
  39077. WOLFSSL_X509_EXTENSION* ext;
  39078. WOLFSSL_ASN1_STRING* sanString;
  39079. byte* sanDer;
  39080. const byte expectedDer[] = {
  39081. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  39082. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  39083. printf(testingFmt, "test_wolfSSL_X509_get_ext_subj_alt_name");
  39084. f = XFOPEN("./certs/server-cert.pem", "rb");
  39085. AssertTrue(f != XBADFILE);
  39086. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  39087. fclose(f);
  39088. rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1);
  39089. AssertIntNE(rc, -1);
  39090. AssertNotNull(ext = X509_get_ext(x509, rc));
  39091. AssertNotNull(sanString = X509_EXTENSION_get_data(ext));
  39092. AssertIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  39093. AssertNotNull(sanDer = ASN1_STRING_data(sanString));
  39094. AssertIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  39095. X509_free(x509);
  39096. printf(resultFmt, passed);
  39097. #endif
  39098. }
  39099. static void test_wolfSSL_X509_EXTENSION_new(void)
  39100. {
  39101. #if defined (OPENSSL_ALL)
  39102. WOLFSSL_X509_EXTENSION* ext;
  39103. AssertNotNull(ext = wolfSSL_X509_EXTENSION_new());
  39104. AssertNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  39105. ext->obj->nid = WOLFSSL_SUCCESS;
  39106. AssertIntEQ(WOLFSSL_SUCCESS, ext->obj->nid);
  39107. wolfSSL_X509_EXTENSION_free(ext);
  39108. #endif
  39109. }
  39110. static void test_wolfSSL_X509_EXTENSION_get_object(void)
  39111. {
  39112. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  39113. WOLFSSL_X509* x509;
  39114. WOLFSSL_X509_EXTENSION* ext;
  39115. WOLFSSL_ASN1_OBJECT* o;
  39116. FILE* file;
  39117. int nid = 0;
  39118. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  39119. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  39120. fclose(file);
  39121. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_object() testing ext idx 0");
  39122. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  39123. AssertNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  39124. AssertIntEQ(o->nid, 128);
  39125. nid = o->nid;
  39126. printf(resultFmt, nid == 128 ? passed : failed);
  39127. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_object() NULL argument");
  39128. AssertNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  39129. printf(resultFmt, passed);
  39130. wolfSSL_X509_free(x509);
  39131. #endif
  39132. }
  39133. static void test_wolfSSL_X509_EXTENSION_get_data(void)
  39134. {
  39135. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  39136. WOLFSSL_X509* x509;
  39137. WOLFSSL_X509_EXTENSION* ext;
  39138. WOLFSSL_ASN1_STRING* str;
  39139. FILE* file;
  39140. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_data");
  39141. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  39142. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  39143. fclose(file);
  39144. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  39145. AssertNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  39146. printf(resultFmt, passed);
  39147. wolfSSL_X509_free(x509);
  39148. #endif
  39149. }
  39150. static void test_wolfSSL_X509_EXTENSION_get_critical(void)
  39151. {
  39152. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  39153. WOLFSSL_X509* x509;
  39154. WOLFSSL_X509_EXTENSION* ext;
  39155. FILE* file;
  39156. int crit;
  39157. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_critical");
  39158. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  39159. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  39160. fclose(file);
  39161. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  39162. crit = wolfSSL_X509_EXTENSION_get_critical(ext);
  39163. AssertIntEQ(crit, 0);
  39164. printf(resultFmt, passed);
  39165. wolfSSL_X509_free(x509);
  39166. #endif
  39167. }
  39168. static void test_wolfSSL_X509V3_EXT_print(void)
  39169. {
  39170. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  39171. !defined(NO_RSA)
  39172. printf(testingFmt, "wolfSSL_X509V3_EXT_print");
  39173. {
  39174. FILE* f;
  39175. WOLFSSL_X509* x509;
  39176. X509_EXTENSION * ext = NULL;
  39177. int loc;
  39178. BIO *bio = NULL;
  39179. AssertNotNull(f = fopen(svrCertFile, "rb"));
  39180. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  39181. fclose(f);
  39182. AssertNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  39183. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
  39184. AssertIntGT(loc, -1);
  39185. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  39186. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  39187. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_subject_key_identifier, -1);
  39188. AssertIntGT(loc, -1);
  39189. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  39190. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  39191. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_authority_key_identifier, -1);
  39192. AssertIntGT(loc, -1);
  39193. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  39194. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  39195. wolfSSL_BIO_free(bio);
  39196. wolfSSL_X509_free(x509);
  39197. }
  39198. {
  39199. X509 *x509;
  39200. BIO *bio;
  39201. X509_EXTENSION *ext;
  39202. unsigned int i;
  39203. unsigned int idx;
  39204. /* Some NIDs to test with */
  39205. int nids[] = {
  39206. /* NID_key_usage, currently X509_get_ext returns this as a bit
  39207. * string, which messes up X509V3_EXT_print */
  39208. /* NID_ext_key_usage, */
  39209. NID_subject_alt_name,
  39210. };
  39211. int* n;
  39212. AssertNotNull(bio = BIO_new_fp(stdout, BIO_NOCLOSE));
  39213. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  39214. WOLFSSL_FILETYPE_PEM));
  39215. printf("\nPrinting extension values:\n");
  39216. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  39217. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  39218. * update the index */
  39219. AssertIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  39220. AssertNotNull(ext = X509_get_ext(x509, idx));
  39221. AssertIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  39222. printf("\n");
  39223. }
  39224. BIO_free(bio);
  39225. X509_free(x509);
  39226. }
  39227. printf(resultFmt, passed);
  39228. #endif
  39229. }
  39230. static void test_wolfSSL_X509_cmp(void)
  39231. {
  39232. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  39233. FILE* file1;
  39234. FILE* file2;
  39235. WOLFSSL_X509* cert1;
  39236. WOLFSSL_X509* cert2;
  39237. int ret = 0;
  39238. AssertNotNull(file1=fopen("./certs/server-cert.pem", "rb"));
  39239. AssertNotNull(file2=fopen("./certs/3072/client-cert.pem", "rb"));
  39240. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  39241. AssertNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  39242. fclose(file1);
  39243. fclose(file2);
  39244. printf(testingFmt, "wolfSSL_X509_cmp() testing matching certs");
  39245. ret = wolfSSL_X509_cmp(cert1, cert1);
  39246. AssertIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  39247. printf(resultFmt, ret == 0 ? passed : failed);
  39248. printf(testingFmt, "wolfSSL_X509_cmp() testing mismatched certs");
  39249. ret = wolfSSL_X509_cmp(cert1, cert2);
  39250. AssertIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  39251. printf(resultFmt, ret == -1 ? passed : failed);
  39252. printf(testingFmt, "wolfSSL_X509_cmp() testing NULL, valid args");
  39253. ret = wolfSSL_X509_cmp(NULL, cert2);
  39254. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  39255. printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
  39256. printf(testingFmt, "wolfSSL_X509_cmp() testing valid, NULL args");
  39257. ret = wolfSSL_X509_cmp(cert1, NULL);
  39258. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  39259. printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
  39260. printf(testingFmt, "wolfSSL_X509_cmp() testing NULL, NULL args");
  39261. ret = wolfSSL_X509_cmp(NULL, NULL);
  39262. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  39263. printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
  39264. wolfSSL_X509_free(cert1);
  39265. wolfSSL_X509_free(cert2);
  39266. #endif
  39267. }
  39268. static void test_wolfSSL_PKEY_up_ref(void)
  39269. {
  39270. #if defined(OPENSSL_ALL)
  39271. EVP_PKEY* pkey;
  39272. printf(testingFmt, "wolfSSL_PKEY_up_ref()");
  39273. pkey = EVP_PKEY_new();
  39274. AssertIntEQ(EVP_PKEY_up_ref(NULL), 0);
  39275. AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
  39276. EVP_PKEY_free(pkey);
  39277. AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
  39278. EVP_PKEY_free(pkey);
  39279. EVP_PKEY_free(pkey);
  39280. printf(resultFmt, "passed");
  39281. #endif
  39282. }
  39283. static void test_wolfSSL_d2i_and_i2d_PublicKey(void)
  39284. {
  39285. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  39286. EVP_PKEY* pkey;
  39287. const unsigned char* p;
  39288. unsigned char* der = NULL;
  39289. int derLen;
  39290. printf(testingFmt, "test_wolfSSL_d2i_and_i2d_PublicKey()");
  39291. p = client_keypub_der_2048;
  39292. /* Check that key can be successfully decoded. */
  39293. AssertNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  39294. sizeof_client_keypub_der_2048));
  39295. /* Check that key can be successfully encoded. */
  39296. AssertIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  39297. /* Ensure that the encoded version matches the original. */
  39298. AssertIntEQ(derLen, sizeof_client_keypub_der_2048);
  39299. AssertIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  39300. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  39301. EVP_PKEY_free(pkey);
  39302. printf(resultFmt, passed);
  39303. #endif
  39304. }
  39305. static void test_wolfSSL_d2i_and_i2d_DSAparams(void)
  39306. {
  39307. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  39308. DSA* dsa;
  39309. char file[] = "./certs/dsaparams.der";
  39310. XFILE f;
  39311. int derInLen;
  39312. byte* derIn;
  39313. int derOutLen;
  39314. byte* derOut = NULL;
  39315. printf(testingFmt, "test_wolfSSL_d2i_and_i2d_DSAparams()");
  39316. f = XFOPEN(file, "rb");
  39317. AssertTrue(f != XBADFILE);
  39318. AssertTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  39319. derInLen = (int)XFTELL(f);
  39320. XREWIND(f);
  39321. AssertNotNull(derIn = (byte*)XMALLOC(derInLen, HEAP_HINT,
  39322. DYNAMIC_TYPE_TMP_BUFFER));
  39323. AssertIntEQ(XFREAD(derIn, 1, derInLen, f), derInLen);
  39324. XFCLOSE(f);
  39325. /* Check that params can be successfully decoded. */
  39326. AssertNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&derIn, derInLen));
  39327. /* Check that params can be successfully encoded. */
  39328. AssertIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  39329. /* Ensure that the encoded version matches the original. */
  39330. AssertIntEQ(derInLen, derOutLen);
  39331. AssertIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  39332. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39333. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  39334. DSA_free(dsa);
  39335. printf(resultFmt, passed);
  39336. #endif
  39337. }
  39338. static void test_wolfSSL_i2d_PrivateKey(void)
  39339. {
  39340. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_PWDBASED)
  39341. printf(testingFmt, "wolfSSL_i2d_PrivateKey()");
  39342. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  39343. {
  39344. EVP_PKEY* pkey;
  39345. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  39346. unsigned char buf[FOURK_BUF];
  39347. unsigned char* pt = NULL;
  39348. int bufSz;
  39349. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  39350. (long)sizeof_server_key_der_2048));
  39351. AssertIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  39352. pt = buf;
  39353. AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  39354. AssertIntNE((pt - buf), 0);
  39355. AssertIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  39356. EVP_PKEY_free(pkey);
  39357. }
  39358. #endif
  39359. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  39360. {
  39361. EVP_PKEY* pkey;
  39362. const unsigned char* client_key =
  39363. (const unsigned char*)ecc_clikey_der_256;
  39364. unsigned char buf[FOURK_BUF];
  39365. unsigned char* pt = NULL;
  39366. int bufSz;
  39367. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  39368. sizeof_ecc_clikey_der_256)));
  39369. AssertIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  39370. pt = buf;
  39371. AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  39372. AssertIntNE((pt - buf), 0);
  39373. AssertIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  39374. EVP_PKEY_free(pkey);
  39375. }
  39376. #endif
  39377. printf(resultFmt, "passed");
  39378. #endif
  39379. }
  39380. static void test_wolfSSL_OCSP_id_get0_info(void)
  39381. {
  39382. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP) && \
  39383. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  39384. X509* cert;
  39385. X509* issuer;
  39386. OCSP_CERTID* id;
  39387. OCSP_CERTID* id2;
  39388. ASN1_STRING* name = NULL;
  39389. ASN1_OBJECT* pmd = NULL;
  39390. ASN1_STRING* keyHash = NULL;
  39391. ASN1_INTEGER* serial = NULL;
  39392. ASN1_INTEGER* x509Int;
  39393. printf(testingFmt, "wolfSSL_OCSP_id_get0_info()");
  39394. AssertNotNull(cert =
  39395. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM));
  39396. AssertNotNull(issuer =
  39397. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
  39398. id = OCSP_cert_to_id(NULL, cert, issuer);
  39399. AssertNotNull(id);
  39400. id2 = OCSP_cert_to_id(NULL, cert, issuer);
  39401. AssertNotNull(id2);
  39402. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  39403. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  39404. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  39405. AssertIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  39406. AssertIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  39407. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  39408. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  39409. AssertNotNull(serial);
  39410. /* compare serial number to one in cert, should be equal */
  39411. x509Int = X509_get_serialNumber(cert);
  39412. AssertNotNull(x509Int);
  39413. AssertIntEQ(x509Int->length, serial->length);
  39414. AssertIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  39415. /* test OCSP_id_cmp */
  39416. AssertIntNE(OCSP_id_cmp(NULL, NULL), 0);
  39417. AssertIntNE(OCSP_id_cmp(id, NULL), 0);
  39418. AssertIntNE(OCSP_id_cmp(NULL, id2), 0);
  39419. AssertIntEQ(OCSP_id_cmp(id, id2), 0);
  39420. id->issuerHash[0] = ~id->issuerHash[0];
  39421. AssertIntNE(OCSP_id_cmp(id, id2), 0);
  39422. OCSP_CERTID_free(id);
  39423. OCSP_CERTID_free(id2);
  39424. X509_free(cert); /* free's x509Int */
  39425. X509_free(issuer);
  39426. printf(resultFmt, "passed");
  39427. #endif
  39428. }
  39429. static void test_wolfSSL_i2d_OCSP_CERTID(void)
  39430. {
  39431. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  39432. WOLFSSL_OCSP_CERTID certId;
  39433. byte* targetBuffer;
  39434. byte* beginTargetBuffer;
  39435. /* OCSP CertID bytes taken from PCAP */
  39436. byte rawCertId[] = {
  39437. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  39438. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  39439. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  39440. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  39441. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  39442. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  39443. 0xcf, 0xbc, 0x91
  39444. };
  39445. int ret, i;
  39446. printf(testingFmt, "wolfSSL_i2d_OCSP_CERTID()");
  39447. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  39448. certId.rawCertId = rawCertId;
  39449. certId.rawCertIdSize = sizeof(rawCertId);
  39450. targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39451. beginTargetBuffer = targetBuffer;
  39452. ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer);
  39453. /* If target buffer is not null, function increments targetBuffer to point
  39454. just past the end of the encoded data. */
  39455. AssertPtrEq(targetBuffer, (beginTargetBuffer + sizeof(rawCertId)));
  39456. /* Function returns the size of the encoded data. */
  39457. AssertIntEQ(ret, sizeof(rawCertId));
  39458. for (i = 0; i < ret; ++i)
  39459. {
  39460. AssertIntEQ(beginTargetBuffer[i], rawCertId[i]);
  39461. }
  39462. XFREE(beginTargetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39463. targetBuffer = NULL;
  39464. ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer);
  39465. /* If target buffer is null, function allocates memory for a buffer and
  39466. copies the encoded data into it. targetBuffer then points to the start of
  39467. this newly allocate buffer. */
  39468. AssertIntEQ(ret, sizeof(rawCertId));
  39469. for (i = 0; i < ret; ++i)
  39470. {
  39471. AssertIntEQ(targetBuffer[i], rawCertId[i]);
  39472. }
  39473. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  39474. printf(resultFmt, passed);
  39475. #endif
  39476. }
  39477. static void test_wolfSSL_OCSP_id_cmp(void)
  39478. {
  39479. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  39480. OCSP_CERTID id1;
  39481. OCSP_CERTID id2;
  39482. printf(testingFmt, "wolfSSL_OCSP_id_cmp()");
  39483. XMEMSET(&id1, 0, sizeof(id1));
  39484. XMEMSET(&id2, 0, sizeof(id2));
  39485. AssertIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  39486. printf(resultFmt, passed);
  39487. #endif
  39488. }
  39489. static void test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  39490. {
  39491. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  39492. WOLFSSL_OCSP_SINGLERESP single;
  39493. const WOLFSSL_OCSP_CERTID* certId;
  39494. XMEMSET(&single, 0, sizeof(single));
  39495. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  39496. printf(testingFmt, "wolfSSL_OCSP_SINGLERESP_get0_id()");
  39497. AssertPtrEq(&single, certId);
  39498. printf(resultFmt, passed);
  39499. #endif
  39500. }
  39501. static void test_wolfSSL_OCSP_single_get0_status(void)
  39502. {
  39503. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  39504. WOLFSSL_OCSP_SINGLERESP single;
  39505. CertStatus certStatus;
  39506. WOLFSSL_ASN1_TIME* thisDate;
  39507. WOLFSSL_ASN1_TIME* nextDate;
  39508. int ret, i;
  39509. printf(testingFmt, "wolfSSL_OCSP_single_get0_status()");
  39510. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  39511. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  39512. /* Fill the date fields with some dummy data. */
  39513. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  39514. certStatus.thisDateParsed.data[i] = i;
  39515. certStatus.nextDateParsed.data[i] = i;
  39516. }
  39517. certStatus.status = CERT_GOOD;
  39518. single.status = &certStatus;
  39519. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  39520. &nextDate);
  39521. AssertIntEQ(ret, CERT_GOOD);
  39522. AssertPtrEq(thisDate, &certStatus.thisDateParsed);
  39523. AssertPtrEq(nextDate, &certStatus.nextDateParsed);
  39524. printf(resultFmt, passed);
  39525. #endif
  39526. }
  39527. static void test_wolfSSL_OCSP_resp_count(void)
  39528. {
  39529. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  39530. WOLFSSL_OCSP_BASICRESP basicResp;
  39531. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  39532. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  39533. int count;
  39534. printf(testingFmt, "wolfSSL_OCSP_resp_count()");
  39535. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  39536. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  39537. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  39538. count = wolfSSL_OCSP_resp_count(&basicResp);
  39539. AssertIntEQ(count, 0);
  39540. basicResp.single = &singleRespOne;
  39541. count = wolfSSL_OCSP_resp_count(&basicResp);
  39542. AssertIntEQ(count, 1);
  39543. singleRespOne.next = &singleRespTwo;
  39544. count = wolfSSL_OCSP_resp_count(&basicResp);
  39545. AssertIntEQ(count, 2);
  39546. printf(resultFmt, passed);
  39547. #endif
  39548. }
  39549. static void test_wolfSSL_OCSP_resp_get0(void)
  39550. {
  39551. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  39552. WOLFSSL_OCSP_BASICRESP basicResp;
  39553. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  39554. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  39555. WOLFSSL_OCSP_SINGLERESP* ret;
  39556. printf(testingFmt, "wolfSSL_OCSP_resp_get0()");
  39557. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  39558. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  39559. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  39560. basicResp.single = &singleRespOne;
  39561. singleRespOne.next = &singleRespTwo;
  39562. ret = wolfSSL_OCSP_resp_get0(&basicResp, 0);
  39563. AssertPtrEq(ret, &singleRespOne);
  39564. ret = wolfSSL_OCSP_resp_get0(&basicResp, 1);
  39565. AssertPtrEq(ret, &singleRespTwo);
  39566. printf(resultFmt, passed);
  39567. #endif
  39568. }
  39569. static void test_wolfSSL_EVP_PKEY_derive(void)
  39570. {
  39571. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  39572. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  39573. EVP_PKEY_CTX *ctx;
  39574. unsigned char *skey;
  39575. size_t skeylen;
  39576. EVP_PKEY *pkey, *peerkey;
  39577. const unsigned char* key;
  39578. printf(testingFmt, "wolfSSL_EVP_PKEY_derive()");
  39579. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  39580. /* DH */
  39581. key = dh_key_der_2048;
  39582. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  39583. sizeof_dh_key_der_2048)));
  39584. AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  39585. key = dh_key_der_2048;
  39586. AssertNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  39587. sizeof_dh_key_der_2048)));
  39588. AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  39589. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39590. AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
  39591. AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  39592. AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  39593. AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
  39594. AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  39595. EVP_PKEY_CTX_free(ctx);
  39596. EVP_PKEY_free(peerkey);
  39597. EVP_PKEY_free(pkey);
  39598. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  39599. #endif
  39600. #ifdef HAVE_ECC
  39601. /* ECDH */
  39602. key = ecc_clikey_der_256;
  39603. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  39604. sizeof_ecc_clikey_der_256)));
  39605. key = ecc_clikeypub_der_256;
  39606. AssertNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  39607. sizeof_ecc_clikeypub_der_256)));
  39608. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39609. AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
  39610. AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  39611. AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  39612. AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
  39613. AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  39614. EVP_PKEY_CTX_free(ctx);
  39615. EVP_PKEY_free(peerkey);
  39616. EVP_PKEY_free(pkey);
  39617. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  39618. #endif /* HAVE_ECC */
  39619. printf(resultFmt, "passed");
  39620. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  39621. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  39622. }
  39623. static void test_wolfSSL_EVP_PBE_scrypt(void)
  39624. {
  39625. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  39626. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  39627. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  39628. int ret;
  39629. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  39630. int pwdlen = sizeof(pwd);
  39631. const byte salt[] = {'N','a','C','l'};
  39632. int saltlen = sizeof(salt);
  39633. byte key[80];
  39634. word64 numOvr32 = (word64)INT32_MAX + 1;
  39635. /* expected derived key for N:16, r:1, p:1 */
  39636. const byte expectedKey[] = {
  39637. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  39638. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  39639. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  39640. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  39641. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  39642. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  39643. 0xE7, 0xE9, 0xC0, 0x9A};
  39644. printf(testingFmt, "wolfSSL_EVP_PBE_scrypt()");
  39645. /* N r p mx key keylen */
  39646. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  39647. AssertIntEQ(ret, 0); /* N must be greater than 1 */
  39648. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  39649. AssertIntEQ(ret, 0); /* N must be power of 2 */
  39650. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  39651. AssertIntEQ(ret, 0); /* r must be greater than 0 */
  39652. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  39653. AssertIntEQ(ret, 0); /* p must be greater than 0 */
  39654. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  39655. AssertIntEQ(ret, 0); /* keylen must be greater than 0 */
  39656. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  39657. AssertIntEQ(ret, 0); /* r must be smaller than 9 */
  39658. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  39659. AssertIntEQ(ret, 1); /* should succeed if key is NULL */
  39660. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  39661. AssertIntEQ(ret, 1); /* should succeed */
  39662. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  39663. key, 64);
  39664. AssertIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  39665. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  39666. key, 64);
  39667. AssertIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  39668. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  39669. AssertIntEQ(ret, 1); /* should succeed even if salt is NULL */
  39670. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  39671. AssertIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  39672. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  39673. AssertIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  39674. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  39675. AssertIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  39676. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  39677. AssertIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  39678. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  39679. AssertIntEQ(ret, 1);
  39680. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  39681. AssertIntEQ(ret, 0); /* derived key must be the same as expected-key */
  39682. printf(resultFmt, "passed");
  39683. #endif /* !NO_PWDBASED && !NO_SHA256 */
  39684. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  39685. }
  39686. static void test_wolfSSL_EC_get_builtin_curves(void)
  39687. {
  39688. #if defined(HAVE_ECC) && (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL))
  39689. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  39690. EC_builtin_curve* curves = NULL;
  39691. size_t crv_len = 0;
  39692. size_t i = 0;
  39693. printf(testingFmt, "wolfSSL_EC_get_builtin_curves");
  39694. AssertIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  39695. AssertNotNull(curves = (EC_builtin_curve*)
  39696. XMALLOC(sizeof(EC_builtin_curve)*crv_len, NULL,
  39697. DYNAMIC_TYPE_TMP_BUFFER));
  39698. AssertIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  39699. for (i = 0; i < crv_len; i++)
  39700. {
  39701. if (curves[i].comment != NULL)
  39702. AssertStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  39703. }
  39704. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39705. printf(resultFmt, passed);
  39706. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  39707. #endif /* defined(HAVE_ECC) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  39708. }
  39709. static void test_no_op_functions(void)
  39710. {
  39711. #if defined(OPENSSL_EXTRA)
  39712. printf(testingFmt, "no_op_functions()");
  39713. /* this makes sure wolfSSL can compile and run these no-op functions */
  39714. SSL_load_error_strings();
  39715. ENGINE_load_builtin_engines();
  39716. OpenSSL_add_all_ciphers();
  39717. AssertIntEQ(CRYPTO_malloc_init(), 0);
  39718. printf(resultFmt, passed);
  39719. #endif
  39720. }
  39721. static void test_wolfSSL_CRYPTO_memcmp(void)
  39722. {
  39723. #ifdef OPENSSL_EXTRA
  39724. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  39725. "implementation of TLS/SSL for embedded devices to the cloud.";
  39726. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  39727. "implementation of TLS/SSL for embedded devices to the cloud.";
  39728. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  39729. "implementation of TLS/SSL for embedded devices to the cloud!";
  39730. AssertIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  39731. AssertIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  39732. #endif
  39733. }
  39734. /*----------------------------------------------------------------------------*
  39735. | wolfCrypt ASN
  39736. *----------------------------------------------------------------------------*/
  39737. static void test_wc_CreateEncryptedPKCS8Key(void)
  39738. {
  39739. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  39740. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  39741. WC_RNG rng;
  39742. byte* encKey = NULL;
  39743. word32 encKeySz = 0;
  39744. word32 decKeySz = 0;
  39745. const char password[] = "Lorem ipsum dolor sit amet";
  39746. word32 passwordSz = (word32)XSTRLEN(password);
  39747. word32 tradIdx = 0;
  39748. printf(testingFmt, "test_wc_CreateEncryptedPKCS8Key");
  39749. AssertIntEQ(wc_InitRng(&rng), 0);
  39750. /* Call with NULL for out buffer to get necessary length. */
  39751. AssertIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  39752. sizeof_server_key_der_2048, NULL, &encKeySz, password, passwordSz,
  39753. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  39754. LENGTH_ONLY_E);
  39755. AssertNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  39756. DYNAMIC_TYPE_TMP_BUFFER));
  39757. /* Call with the allocated out buffer. */
  39758. AssertIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  39759. sizeof_server_key_der_2048, encKey, &encKeySz, password, passwordSz,
  39760. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  39761. 0);
  39762. /* Decrypt the encrypted PKCS8 key we just made. */
  39763. AssertIntGT((decKeySz = wc_DecryptPKCS8Key(encKey, encKeySz, password,
  39764. passwordSz)), 0);
  39765. /* encKey now holds the decrypted key (decrypted in place). */
  39766. AssertIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  39767. /* Check that the decrypted key matches the key prior to encryption. */
  39768. AssertIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  39769. sizeof_server_key_der_2048), 0);
  39770. if (encKey != NULL)
  39771. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39772. wc_FreeRng(&rng);
  39773. printf(resultFmt, passed);
  39774. #endif
  39775. }
  39776. static void test_wc_GetPkcs8TraditionalOffset(void)
  39777. {
  39778. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  39779. int length, derSz;
  39780. word32 inOutIdx;
  39781. const char* path = "./certs/server-keyPkcs8.der";
  39782. XFILE file;
  39783. byte der[2048];
  39784. printf(testingFmt, "wc_GetPkcs8TraditionalOffset");
  39785. file = XFOPEN(path, "rb");
  39786. AssertTrue(file != XBADFILE);
  39787. derSz = (int)XFREAD(der, 1, sizeof(der), file);
  39788. XFCLOSE(file);
  39789. /* valid case */
  39790. inOutIdx = 0;
  39791. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  39792. AssertIntGT(length, 0);
  39793. /* inOutIdx > sz */
  39794. inOutIdx = 4000;
  39795. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  39796. AssertIntEQ(length, BAD_FUNC_ARG);
  39797. /* null input */
  39798. inOutIdx = 0;
  39799. length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0);
  39800. AssertIntEQ(length, BAD_FUNC_ARG);
  39801. /* invalid input, fill buffer with 1's */
  39802. XMEMSET(der, 1, sizeof(der));
  39803. inOutIdx = 0;
  39804. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  39805. AssertIntEQ(length, ASN_PARSE_E);
  39806. printf(resultFmt, passed);
  39807. #endif /* NO_ASN */
  39808. }
  39809. static void test_wc_SetSubjectRaw(void)
  39810. {
  39811. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  39812. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  39813. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  39814. WOLFSSL_X509* x509;
  39815. int peerCertSz;
  39816. const byte* peerCertBuf;
  39817. Cert forgedCert;
  39818. printf(testingFmt, "test_wc_SetSubjectRaw()");
  39819. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  39820. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  39821. AssertIntEQ(0, wc_InitCert(&forgedCert));
  39822. AssertIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  39823. wolfSSL_FreeX509(x509);
  39824. printf(resultFmt, passed);
  39825. #endif
  39826. }
  39827. static void test_wc_GetSubjectRaw(void)
  39828. {
  39829. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  39830. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  39831. Cert cert;
  39832. byte *subjectRaw;
  39833. printf(testingFmt, "test_wc_GetSubjectRaw()");
  39834. AssertIntEQ(0, wc_InitCert(&cert));
  39835. AssertIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  39836. printf(resultFmt, passed);
  39837. #endif
  39838. }
  39839. static void test_wc_SetIssuerRaw(void)
  39840. {
  39841. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  39842. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  39843. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  39844. WOLFSSL_X509* x509;
  39845. int peerCertSz;
  39846. const byte* peerCertBuf;
  39847. Cert forgedCert;
  39848. printf(testingFmt, "test_wc_SetIssuerRaw()");
  39849. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  39850. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  39851. AssertIntEQ(0, wc_InitCert(&forgedCert));
  39852. AssertIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  39853. wolfSSL_FreeX509(x509);
  39854. printf(resultFmt, passed);
  39855. #endif
  39856. }
  39857. static void test_wc_SetIssueBuffer(void)
  39858. {
  39859. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  39860. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  39861. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  39862. WOLFSSL_X509* x509;
  39863. int peerCertSz;
  39864. const byte* peerCertBuf;
  39865. Cert forgedCert;
  39866. printf(testingFmt, "test_wc_SetIssuerBuffer()");
  39867. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  39868. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  39869. AssertIntEQ(0, wc_InitCert(&forgedCert));
  39870. AssertIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  39871. wolfSSL_FreeX509(x509);
  39872. printf(resultFmt, passed);
  39873. #endif
  39874. }
  39875. /*
  39876. * Testing wc_SetSubjectKeyId
  39877. */
  39878. static void test_wc_SetSubjectKeyId(void)
  39879. {
  39880. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  39881. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  39882. Cert cert;
  39883. const char* file = "certs/ecc-client-keyPub.pem";
  39884. printf(testingFmt, "wc_SetSubjectKeyId()");
  39885. AssertIntEQ(0, wc_InitCert(&cert));
  39886. AssertIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  39887. AssertIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  39888. AssertIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  39889. printf(resultFmt, passed);
  39890. #endif
  39891. } /* END test_wc_SetSubjectKeyId */
  39892. /*
  39893. * Testing wc_SetSubject
  39894. */
  39895. static void test_wc_SetSubject(void)
  39896. {
  39897. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  39898. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  39899. Cert cert;
  39900. const char* file = "./certs/ca-ecc-cert.pem";
  39901. printf(testingFmt, "wc_SetSubject()");
  39902. AssertIntEQ(0, wc_InitCert(&cert));
  39903. AssertIntEQ(0, wc_SetSubject(&cert, file));
  39904. AssertIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  39905. AssertIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  39906. printf(resultFmt, passed);
  39907. #endif
  39908. } /* END test_wc_SetSubject */
  39909. static void test_CheckCertSignature(void)
  39910. {
  39911. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  39912. WOLFSSL_CERT_MANAGER* cm = NULL;
  39913. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  39914. FILE* fp;
  39915. byte cert[4096];
  39916. int certSz;
  39917. #endif
  39918. AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
  39919. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  39920. AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
  39921. #ifndef NO_RSA
  39922. #ifdef USE_CERT_BUFFERS_1024
  39923. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
  39924. sizeof_server_cert_der_1024, NULL, cm));
  39925. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  39926. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  39927. WOLFSSL_FILETYPE_ASN1));
  39928. AssertIntEQ(0, CheckCertSignature(server_cert_der_1024,
  39929. sizeof_server_cert_der_1024, NULL, cm));
  39930. #elif defined(USE_CERT_BUFFERS_2048)
  39931. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
  39932. sizeof_server_cert_der_2048, NULL, cm));
  39933. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  39934. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  39935. WOLFSSL_FILETYPE_ASN1));
  39936. AssertIntEQ(0, CheckCertSignature(server_cert_der_2048,
  39937. sizeof_server_cert_der_2048, NULL, cm));
  39938. #endif
  39939. #endif
  39940. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  39941. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
  39942. sizeof_serv_ecc_der_256, NULL, cm));
  39943. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  39944. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  39945. WOLFSSL_FILETYPE_ASN1));
  39946. AssertIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  39947. NULL, cm));
  39948. #endif
  39949. #if !defined(NO_FILESYSTEM)
  39950. wolfSSL_CertManagerFree(cm);
  39951. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  39952. #ifndef NO_RSA
  39953. AssertNotNull(fp = XFOPEN("./certs/server-cert.der", "rb"));
  39954. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  39955. XFCLOSE(fp);
  39956. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  39957. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  39958. "./certs/ca-cert.pem", NULL));
  39959. AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  39960. #endif
  39961. #ifdef HAVE_ECC
  39962. AssertNotNull(fp = XFOPEN("./certs/server-ecc.der", "rb"));
  39963. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  39964. XFCLOSE(fp);
  39965. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  39966. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  39967. "./certs/ca-ecc-cert.pem", NULL));
  39968. AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  39969. #endif
  39970. #endif
  39971. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  39972. (void)fp;
  39973. (void)cert;
  39974. (void)certSz;
  39975. #endif
  39976. wolfSSL_CertManagerFree(cm);
  39977. #endif
  39978. }
  39979. static void test_wc_ParseCert(void)
  39980. {
  39981. #if !defined(NO_CERTS) && !defined(NO_RSA)
  39982. DecodedCert decodedCert;
  39983. const byte* rawCert = client_cert_der_2048;
  39984. const int rawCertSize = sizeof_client_cert_der_2048;
  39985. printf(testingFmt, "wc_ParseCert");
  39986. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  39987. AssertIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  39988. wc_FreeDecodedCert(&decodedCert);
  39989. printf(resultFmt, passed);
  39990. #endif
  39991. }
  39992. /*----------------------------------------------------------------------------*
  39993. | wolfCrypt ECC
  39994. *----------------------------------------------------------------------------*/
  39995. static void test_wc_ecc_get_curve_size_from_name(void)
  39996. {
  39997. #ifdef HAVE_ECC
  39998. int ret;
  39999. printf(testingFmt, "wc_ecc_get_curve_size_from_name");
  40000. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  40001. ret = wc_ecc_get_curve_size_from_name("SECP256R1");
  40002. AssertIntEQ(ret, 32);
  40003. #endif
  40004. /* invalid case */
  40005. ret = wc_ecc_get_curve_size_from_name("BADCURVE");
  40006. AssertIntEQ(ret, -1);
  40007. /* NULL input */
  40008. ret = wc_ecc_get_curve_size_from_name(NULL);
  40009. AssertIntEQ(ret, BAD_FUNC_ARG);
  40010. printf(resultFmt, passed);
  40011. #endif /* HAVE_ECC */
  40012. }
  40013. static void test_wc_ecc_get_curve_id_from_name(void)
  40014. {
  40015. #ifdef HAVE_ECC
  40016. int id;
  40017. printf(testingFmt, "wc_ecc_get_curve_id_from_name");
  40018. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  40019. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  40020. AssertIntEQ(id, ECC_SECP256R1);
  40021. #endif
  40022. /* invalid case */
  40023. id = wc_ecc_get_curve_id_from_name("BADCURVE");
  40024. AssertIntEQ(id, -1);
  40025. /* NULL input */
  40026. id = wc_ecc_get_curve_id_from_name(NULL);
  40027. AssertIntEQ(id, BAD_FUNC_ARG);
  40028. printf(resultFmt, passed);
  40029. #endif /* HAVE_ECC */
  40030. }
  40031. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  40032. !defined(HAVE_SELFTEST) && \
  40033. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  40034. static void test_wc_ecc_get_curve_id_from_dp_params(void)
  40035. {
  40036. int id;
  40037. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  40038. int curve_id;
  40039. ecc_key* key;
  40040. const ecc_set_type* params;
  40041. int ret;
  40042. #endif
  40043. WOLFSSL_EC_KEY *ecKey = NULL;
  40044. printf(testingFmt, "wc_ecc_get_curve_id_from_dp_params");
  40045. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  40046. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  40047. AssertIntEQ(id, ECC_SECP256R1);
  40048. ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  40049. AssertNotNull(ecKey);
  40050. ret = EC_KEY_generate_key(ecKey);
  40051. if (ret == 0) {
  40052. /* normal test */
  40053. key = (ecc_key*)ecKey->internal;
  40054. params = key->dp;
  40055. curve_id = wc_ecc_get_curve_id_from_dp_params(params);
  40056. AssertIntEQ(curve_id, id);
  40057. }
  40058. #endif
  40059. /* invalid case, NULL input*/
  40060. id = wc_ecc_get_curve_id_from_dp_params(NULL);
  40061. AssertIntEQ(id, BAD_FUNC_ARG);
  40062. wolfSSL_EC_KEY_free(ecKey);
  40063. printf(resultFmt, passed);
  40064. }
  40065. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  40066. static void test_wc_ecc_get_curve_id_from_params(void)
  40067. {
  40068. #ifdef HAVE_ECC
  40069. int id;
  40070. const byte prime[] =
  40071. {
  40072. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  40073. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  40074. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  40075. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  40076. };
  40077. const byte primeInvalid[] =
  40078. {
  40079. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  40080. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  40081. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  40082. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  40083. };
  40084. const byte Af[] =
  40085. {
  40086. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  40087. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  40088. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  40089. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  40090. };
  40091. const byte Bf[] =
  40092. {
  40093. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  40094. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  40095. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  40096. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  40097. };
  40098. const byte order[] =
  40099. {
  40100. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  40101. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  40102. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  40103. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  40104. };
  40105. const byte Gx[] =
  40106. {
  40107. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  40108. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  40109. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  40110. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  40111. };
  40112. const byte Gy[] =
  40113. {
  40114. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  40115. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  40116. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  40117. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  40118. };
  40119. int cofactor = 1;
  40120. int fieldSize = 256;
  40121. printf(testingFmt, "wc_ecc_get_curve_id_from_params");
  40122. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  40123. id = wc_ecc_get_curve_id_from_params(fieldSize, prime, sizeof(prime),
  40124. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  40125. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  40126. AssertIntEQ(id, ECC_SECP256R1);
  40127. #endif
  40128. /* invalid case, fieldSize = 0 */
  40129. id = wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  40130. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  40131. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  40132. AssertIntEQ(id, ECC_CURVE_INVALID);
  40133. /* invalid case, NULL prime */
  40134. id = wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  40135. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  40136. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  40137. AssertIntEQ(id, BAD_FUNC_ARG);
  40138. /* invalid case, invalid prime */
  40139. id = wc_ecc_get_curve_id_from_params(fieldSize,
  40140. primeInvalid, sizeof(primeInvalid),
  40141. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  40142. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  40143. AssertIntEQ(id, ECC_CURVE_INVALID);
  40144. printf(resultFmt, passed);
  40145. #endif
  40146. }
  40147. static void test_wolfSSL_EVP_PKEY_encrypt(void)
  40148. {
  40149. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  40150. !defined(HAVE_FAST_RSA)
  40151. WOLFSSL_RSA* rsa = NULL;
  40152. WOLFSSL_EVP_PKEY* pkey = NULL;
  40153. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  40154. const char* in = "What is easy to do is easy not to do.";
  40155. size_t inlen = XSTRLEN(in);
  40156. size_t outEncLen = 0;
  40157. byte* outEnc = NULL;
  40158. byte* outDec = NULL;
  40159. size_t outDecLen = 0;
  40160. size_t rsaKeySz = 2048/8; /* Bytes */
  40161. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  40162. byte* inTmp = NULL;
  40163. byte* outEncTmp = NULL;
  40164. byte* outDecTmp = NULL;
  40165. #endif
  40166. printf(testingFmt, "wolfSSL_EVP_PKEY_encrypt()");
  40167. AssertNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  40168. XMEMSET(outEnc, 0, rsaKeySz);
  40169. AssertNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  40170. XMEMSET(outDec, 0, rsaKeySz);
  40171. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  40172. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  40173. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  40174. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  40175. AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  40176. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  40177. WOLFSSL_SUCCESS);
  40178. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  40179. since ctx uses it.*/
  40180. AssertIntEQ(pkey->references, 2);
  40181. EVP_PKEY_free(pkey);
  40182. AssertIntEQ(pkey->references, 1);
  40183. /* Encrypt data */
  40184. /* Check that we can get the required output buffer length by passing in a
  40185. * NULL output buffer. */
  40186. AssertIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  40187. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  40188. AssertIntEQ(rsaKeySz, outEncLen);
  40189. /* Now do the actual encryption. */
  40190. AssertIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  40191. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  40192. /* Decrypt data */
  40193. AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  40194. /* Check that we can get the required output buffer length by passing in a
  40195. * NULL output buffer. */
  40196. AssertIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  40197. WOLFSSL_SUCCESS);
  40198. AssertIntEQ(rsaKeySz, outDecLen);
  40199. /* Now do the actual decryption. */
  40200. AssertIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  40201. WOLFSSL_SUCCESS);
  40202. AssertIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  40203. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  40204. /* The input length must be the same size as the RSA key.*/
  40205. AssertNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  40206. XMEMSET(inTmp, 9, rsaKeySz);
  40207. AssertNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  40208. XMEMSET(outEncTmp, 0, rsaKeySz);
  40209. AssertNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  40210. XMEMSET(outDecTmp, 0, rsaKeySz);
  40211. AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  40212. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  40213. WOLFSSL_SUCCESS);
  40214. AssertIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  40215. WOLFSSL_SUCCESS);
  40216. AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  40217. AssertIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp, outEncLen),
  40218. WOLFSSL_SUCCESS);
  40219. AssertIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  40220. #endif
  40221. EVP_PKEY_CTX_free(ctx);
  40222. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40223. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40224. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  40225. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40226. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40227. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40228. #endif
  40229. printf(resultFmt, passed);
  40230. #endif
  40231. }
  40232. static void test_wolfSSL_EVP_PKEY_sign_verify(void)
  40233. {
  40234. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  40235. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  40236. WOLFSSL_DSA* dsa = NULL;
  40237. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  40238. WOLFSSL_EVP_PKEY* pkey = NULL;
  40239. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  40240. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  40241. const char* in = "What is easy to do is easy not to do.";
  40242. size_t inlen = XSTRLEN(in);
  40243. byte hash[SHA256_DIGEST_LENGTH] = {0};
  40244. byte zero[SHA256_DIGEST_LENGTH] = {0};
  40245. SHA256_CTX c;
  40246. byte* sig = NULL;
  40247. byte* sigVerify = NULL;
  40248. size_t siglen;
  40249. size_t siglenOnlyLen;
  40250. size_t keySz = 2048/8; /* Bytes */
  40251. int i;
  40252. int encs[3] = {0};
  40253. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  40254. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  40255. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  40256. encs[0] = EVP_PKEY_RSA;
  40257. #endif
  40258. #endif
  40259. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  40260. encs[1] = EVP_PKEY_DSA;
  40261. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  40262. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  40263. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  40264. encs[2] = EVP_PKEY_EC;
  40265. #endif
  40266. #endif
  40267. printf(testingFmt, "wolfSSL_EVP_PKEY_sign_verify()");
  40268. AssertNotNull(sig =
  40269. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  40270. AssertNotNull(sigVerify =
  40271. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  40272. for (i = 0; i < 3; i++) {
  40273. if (encs[i] == 0)
  40274. continue;
  40275. siglen = keySz;
  40276. XMEMSET(sig, 0, keySz);
  40277. XMEMSET(sigVerify, 0, keySz);
  40278. /* Generate hash */
  40279. SHA256_Init(&c);
  40280. SHA256_Update(&c, in, inlen);
  40281. SHA256_Final(hash, &c);
  40282. #ifdef WOLFSSL_SMALL_STACK_CACHE
  40283. /* workaround for small stack cache case */
  40284. wc_Sha256Free((wc_Sha256*)&c);
  40285. #endif
  40286. /* Generate key */
  40287. AssertNotNull(pkey = EVP_PKEY_new());
  40288. switch (encs[i]) {
  40289. case EVP_PKEY_RSA:
  40290. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  40291. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  40292. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  40293. {
  40294. WOLFSSL_RSA* rsa = NULL;
  40295. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  40296. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  40297. }
  40298. #endif
  40299. #endif
  40300. break;
  40301. case EVP_PKEY_DSA:
  40302. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  40303. AssertNotNull(dsa = DSA_new());
  40304. AssertIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  40305. NULL, 0, NULL, NULL, NULL), 1);
  40306. AssertIntEQ(DSA_generate_key(dsa), 1);
  40307. AssertIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  40308. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  40309. break;
  40310. case EVP_PKEY_EC:
  40311. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  40312. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  40313. {
  40314. WOLFSSL_EC_KEY* ecKey = NULL;
  40315. AssertNotNull(ecKey = EC_KEY_new());
  40316. AssertIntEQ(EC_KEY_generate_key(ecKey), 1);
  40317. AssertIntEQ(
  40318. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  40319. }
  40320. #endif
  40321. #endif
  40322. break;
  40323. }
  40324. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  40325. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  40326. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  40327. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  40328. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  40329. if (encs[i] == EVP_PKEY_RSA)
  40330. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  40331. WOLFSSL_SUCCESS);
  40332. #endif
  40333. #endif
  40334. /* Check returning only length */
  40335. AssertIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  40336. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  40337. AssertIntGT(siglenOnlyLen, 0);
  40338. /* Sign data */
  40339. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  40340. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  40341. AssertIntGE(siglenOnlyLen, siglen);
  40342. /* Verify signature */
  40343. AssertNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  40344. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  40345. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  40346. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  40347. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  40348. if (encs[i] == EVP_PKEY_RSA)
  40349. AssertIntEQ(
  40350. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  40351. WOLFSSL_SUCCESS);
  40352. #endif
  40353. #endif
  40354. AssertIntEQ(EVP_PKEY_verify(
  40355. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  40356. WOLFSSL_SUCCESS);
  40357. AssertIntEQ(EVP_PKEY_verify(
  40358. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  40359. WOLFSSL_FAILURE);
  40360. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  40361. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  40362. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  40363. if (encs[i] == EVP_PKEY_RSA) {
  40364. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  40365. /* Try RSA sign/verify with no padding. */
  40366. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  40367. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  40368. WOLFSSL_SUCCESS);
  40369. AssertIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  40370. siglen), WOLFSSL_SUCCESS);
  40371. AssertIntGE(siglenOnlyLen, siglen);
  40372. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  40373. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  40374. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  40375. AssertIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  40376. siglen), WOLFSSL_SUCCESS);
  40377. #endif
  40378. /* Wrong padding schemes. */
  40379. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  40380. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  40381. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  40382. AssertIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  40383. siglen), WOLFSSL_SUCCESS);
  40384. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  40385. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  40386. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  40387. AssertIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  40388. siglen), WOLFSSL_SUCCESS);
  40389. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  40390. WOLFSSL_SUCCESS);
  40391. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  40392. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  40393. }
  40394. #endif
  40395. #endif
  40396. /* error cases */
  40397. siglen = keySz; /* Reset because sig size may vary slightly */
  40398. AssertIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  40399. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  40400. AssertIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  40401. WOLFSSL_SUCCESS);
  40402. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  40403. WOLFSSL_SUCCESS);
  40404. EVP_PKEY_free(pkey);
  40405. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  40406. DSA_free(dsa);
  40407. dsa = NULL;
  40408. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  40409. EVP_PKEY_CTX_free(ctx_verify);
  40410. EVP_PKEY_CTX_free(ctx);
  40411. }
  40412. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40413. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40414. printf(resultFmt, passed);
  40415. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  40416. }
  40417. static void test_EVP_PKEY_rsa(void)
  40418. {
  40419. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  40420. WOLFSSL_RSA* rsa;
  40421. WOLFSSL_EVP_PKEY* pkey;
  40422. AssertNotNull(rsa = wolfSSL_RSA_new());
  40423. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  40424. AssertIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  40425. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  40426. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  40427. AssertPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  40428. wolfSSL_EVP_PKEY_free(pkey);
  40429. printf(resultFmt, passed);
  40430. #endif
  40431. }
  40432. static void test_EVP_PKEY_ec(void)
  40433. {
  40434. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  40435. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  40436. WOLFSSL_EC_KEY* ecKey;
  40437. WOLFSSL_EVP_PKEY* pkey;
  40438. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  40439. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  40440. AssertIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  40441. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  40442. /* Should fail since ecKey is empty */
  40443. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  40444. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  40445. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  40446. wolfSSL_EVP_PKEY_free(pkey);
  40447. printf(resultFmt, passed);
  40448. #endif
  40449. #endif
  40450. }
  40451. static void test_EVP_PKEY_cmp(void)
  40452. {
  40453. #if defined(OPENSSL_EXTRA)
  40454. EVP_PKEY *a, *b;
  40455. const unsigned char *in;
  40456. printf(testingFmt, "wolfSSL_EVP_PKEY_cmp()");
  40457. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  40458. in = client_key_der_2048;
  40459. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  40460. &in, (long)sizeof_client_key_der_2048));
  40461. in = client_key_der_2048;
  40462. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  40463. &in, (long)sizeof_client_key_der_2048));
  40464. /* Test success case RSA */
  40465. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  40466. AssertIntEQ(EVP_PKEY_cmp(a, b), 1);
  40467. #else
  40468. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  40469. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  40470. EVP_PKEY_free(b);
  40471. EVP_PKEY_free(a);
  40472. #endif
  40473. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  40474. in = ecc_clikey_der_256;
  40475. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  40476. &in, (long)sizeof_ecc_clikey_der_256));
  40477. in = ecc_clikey_der_256;
  40478. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  40479. &in, (long)sizeof_ecc_clikey_der_256));
  40480. /* Test success case ECC */
  40481. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  40482. AssertIntEQ(EVP_PKEY_cmp(a, b), 1);
  40483. #else
  40484. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  40485. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  40486. EVP_PKEY_free(b);
  40487. EVP_PKEY_free(a);
  40488. #endif
  40489. /* Test failure cases */
  40490. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  40491. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  40492. in = client_key_der_2048;
  40493. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  40494. &in, (long)sizeof_client_key_der_2048));
  40495. in = ecc_clikey_der_256;
  40496. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  40497. &in, (long)sizeof_ecc_clikey_der_256));
  40498. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  40499. AssertIntEQ(EVP_PKEY_cmp(a, b), -1);
  40500. #else
  40501. AssertIntNE(EVP_PKEY_cmp(a, b), 0);
  40502. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  40503. EVP_PKEY_free(b);
  40504. EVP_PKEY_free(a);
  40505. #endif
  40506. /* invalid or empty failure cases */
  40507. a = EVP_PKEY_new();
  40508. b = EVP_PKEY_new();
  40509. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  40510. AssertIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  40511. AssertIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  40512. AssertIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  40513. #ifdef NO_RSA
  40514. /* Type check will fail since RSA is the default EVP key type */
  40515. AssertIntEQ(EVP_PKEY_cmp(a, b), -2);
  40516. #else
  40517. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  40518. #endif
  40519. #else
  40520. AssertIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  40521. AssertIntNE(EVP_PKEY_cmp(a, NULL), 0);
  40522. AssertIntNE(EVP_PKEY_cmp(NULL, b), 0);
  40523. AssertIntNE(EVP_PKEY_cmp(a, b), 0);
  40524. #endif
  40525. EVP_PKEY_free(b);
  40526. EVP_PKEY_free(a);
  40527. (void)in;
  40528. printf(resultFmt, passed);
  40529. #endif
  40530. }
  40531. static void test_ERR_load_crypto_strings(void)
  40532. {
  40533. #if defined(OPENSSL_ALL)
  40534. ERR_load_crypto_strings();
  40535. printf(resultFmt, passed);
  40536. #endif
  40537. }
  40538. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  40539. static void free_x509(X509* x)
  40540. {
  40541. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  40542. }
  40543. #endif
  40544. static void test_sk_X509(void)
  40545. {
  40546. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  40547. STACK_OF(X509)* s;
  40548. AssertNotNull(s = sk_X509_new());
  40549. AssertIntEQ(sk_X509_num(s), 0);
  40550. sk_X509_pop_free(s, NULL);
  40551. AssertNotNull(s = sk_X509_new_null());
  40552. AssertIntEQ(sk_X509_num(s), 0);
  40553. sk_X509_pop_free(s, NULL);
  40554. AssertNotNull(s = sk_X509_new());
  40555. sk_X509_push(s, (X509*)1);
  40556. AssertIntEQ(sk_X509_num(s), 1);
  40557. AssertIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  40558. sk_X509_push(s, (X509*)2);
  40559. AssertIntEQ(sk_X509_num(s), 2);
  40560. AssertIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  40561. AssertIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  40562. sk_X509_push(s, (X509*)2);
  40563. sk_X509_pop_free(s, free_x509);
  40564. printf(resultFmt, passed);
  40565. #endif
  40566. }
  40567. static void test_sk_X509_CRL(void)
  40568. {
  40569. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  40570. X509_CRL* crl;
  40571. XFILE fp;
  40572. STACK_OF(X509_CRL)* s;
  40573. printf(testingFmt, "test_sk_X509_CRL");
  40574. fp = XFOPEN("./certs/crl/crl.pem", "rb");
  40575. AssertTrue((fp != XBADFILE));
  40576. AssertNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
  40577. XFCLOSE(fp);
  40578. AssertNotNull(s = sk_X509_CRL_new());
  40579. AssertIntEQ(sk_X509_CRL_num(s), 0);
  40580. AssertIntEQ(sk_X509_CRL_push(s, crl), 1);
  40581. AssertIntEQ(sk_X509_CRL_num(s), 1);
  40582. AssertPtrEq(sk_X509_CRL_value(s, 0), crl);
  40583. sk_X509_CRL_free(s);
  40584. printf(resultFmt, passed);
  40585. #endif
  40586. }
  40587. static void test_X509_get_signature_nid(void)
  40588. {
  40589. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  40590. X509* x509;
  40591. AssertIntEQ(X509_get_signature_nid(NULL), 0);
  40592. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  40593. SSL_FILETYPE_PEM));
  40594. AssertIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  40595. X509_free(x509);
  40596. printf(resultFmt, passed);
  40597. #endif
  40598. }
  40599. static void test_X509_REQ(void)
  40600. {
  40601. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  40602. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  40603. X509_NAME* name;
  40604. X509_NAME* subject;
  40605. #if !defined(NO_RSA) || defined(HAVE_ECC)
  40606. X509_REQ* req;
  40607. EVP_PKEY* priv;
  40608. EVP_PKEY* pub;
  40609. unsigned char* der = NULL;
  40610. int len;
  40611. #endif
  40612. #ifndef NO_RSA
  40613. EVP_MD_CTX *mctx = NULL;
  40614. EVP_PKEY_CTX *pkctx = NULL;
  40615. #ifdef USE_CERT_BUFFERS_1024
  40616. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  40617. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  40618. #elif defined(USE_CERT_BUFFERS_2048)
  40619. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  40620. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  40621. #endif
  40622. #endif
  40623. #ifdef HAVE_ECC
  40624. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  40625. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  40626. #endif
  40627. AssertNotNull(name = X509_NAME_new());
  40628. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  40629. (byte*)"wolfssl.com", 11, 0, 1),
  40630. WOLFSSL_SUCCESS);
  40631. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  40632. (byte*)"support@wolfssl.com", 19, -1,
  40633. 1), WOLFSSL_SUCCESS);
  40634. #ifndef NO_RSA
  40635. AssertNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  40636. (long)sizeof_client_key_der_2048));
  40637. AssertNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  40638. (long)sizeof_client_keypub_der_2048));
  40639. AssertNotNull(req = X509_REQ_new());
  40640. AssertIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  40641. AssertIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  40642. AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  40643. AssertIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  40644. AssertIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  40645. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  40646. AssertIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  40647. AssertIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  40648. AssertIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  40649. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  40650. len = i2d_X509_REQ(req, &der);
  40651. DEBUG_WRITE_DER(der, len, "req.der");
  40652. #ifdef USE_CERT_BUFFERS_1024
  40653. AssertIntEQ(len, 381);
  40654. #else
  40655. AssertIntEQ(len, 643);
  40656. #endif
  40657. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  40658. der = NULL;
  40659. mctx = EVP_MD_CTX_new();
  40660. AssertIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv), WOLFSSL_SUCCESS);
  40661. AssertIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  40662. EVP_MD_CTX_free(mctx);
  40663. X509_REQ_free(NULL);
  40664. X509_REQ_free(req);
  40665. /* Test getting the subject from a newly created X509_REQ */
  40666. AssertNotNull(req = X509_REQ_new());
  40667. AssertNotNull(subject = X509_REQ_get_subject_name(req));
  40668. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  40669. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  40670. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  40671. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  40672. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  40673. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  40674. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  40675. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  40676. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  40677. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  40678. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  40679. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  40680. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  40681. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  40682. len = i2d_X509_REQ(req, &der);
  40683. DEBUG_WRITE_DER(der, len, "req2.der");
  40684. #ifdef USE_CERT_BUFFERS_1024
  40685. AssertIntEQ(len, 435);
  40686. #else
  40687. AssertIntEQ(len, 696);
  40688. #endif
  40689. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  40690. der = NULL;
  40691. EVP_PKEY_free(pub);
  40692. EVP_PKEY_free(priv);
  40693. X509_REQ_free(req);
  40694. #endif
  40695. #ifdef HAVE_ECC
  40696. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  40697. sizeof_ecc_clikey_der_256));
  40698. AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  40699. sizeof_ecc_clikeypub_der_256));
  40700. AssertNotNull(req = X509_REQ_new());
  40701. AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  40702. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  40703. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  40704. /* Signature is random and may be shorter or longer. */
  40705. AssertIntGE((len = i2d_X509_REQ(req, &der)), 245);
  40706. AssertIntLE(len, 253);
  40707. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  40708. X509_REQ_free(req);
  40709. EVP_PKEY_free(pub);
  40710. EVP_PKEY_free(priv);
  40711. #ifdef FP_ECC
  40712. wc_ecc_fp_free();
  40713. #endif
  40714. #endif /* HAVE_ECC */
  40715. X509_NAME_free(name);
  40716. printf(resultFmt, passed);
  40717. #endif
  40718. }
  40719. static void test_wolfssl_PKCS7(void)
  40720. {
  40721. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO)
  40722. PKCS7* pkcs7;
  40723. byte data[FOURK_BUF];
  40724. word32 len = sizeof(data);
  40725. const byte* p = data;
  40726. byte content[] = "Test data to encode.";
  40727. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  40728. BIO* bio;
  40729. byte key[sizeof(client_key_der_2048)];
  40730. word32 keySz = (word32)sizeof(key);
  40731. byte* out = NULL;
  40732. #endif
  40733. AssertIntGT((len = CreatePKCS7SignedData(data, len, content,
  40734. (word32)sizeof(content),
  40735. 0, 0)), 0);
  40736. AssertNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
  40737. AssertNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  40738. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  40739. AssertIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  40740. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  40741. PKCS7_free(pkcs7);
  40742. /* fail case, without PKCS7_NOVERIFY */
  40743. p = data;
  40744. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  40745. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  40746. 0), WOLFSSL_FAILURE);
  40747. PKCS7_free(pkcs7);
  40748. /* success case, with PKCS7_NOVERIFY */
  40749. p = data;
  40750. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  40751. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  40752. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  40753. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  40754. /* test i2d */
  40755. XMEMCPY(key, client_key_der_2048, keySz);
  40756. pkcs7->privateKey = key;
  40757. pkcs7->privateKeySz = (word32)sizeof(key);
  40758. pkcs7->encryptOID = RSAk;
  40759. pkcs7->hashOID = SHAh;
  40760. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  40761. AssertIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  40762. AssertIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  40763. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40764. BIO_free(bio);
  40765. #endif
  40766. PKCS7_free(NULL);
  40767. PKCS7_free(pkcs7);
  40768. printf(resultFmt, passed);
  40769. #endif
  40770. }
  40771. static void test_wolfSSL_PKCS7_sign(void)
  40772. {
  40773. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  40774. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  40775. PKCS7* p7 = NULL;
  40776. PKCS7* p7Ver = NULL;
  40777. byte* out = NULL;
  40778. byte* tmpPtr = NULL;
  40779. int outLen = 0;
  40780. int flags = 0;
  40781. byte data[] = "Test data to encode.";
  40782. const char* cert = "./certs/server-cert.pem";
  40783. const char* key = "./certs/server-key.pem";
  40784. const char* ca = "./certs/ca-cert.pem";
  40785. WOLFSSL_BIO* certBio = NULL;
  40786. WOLFSSL_BIO* keyBio = NULL;
  40787. WOLFSSL_BIO* caBio = NULL;
  40788. WOLFSSL_BIO* inBio = NULL;
  40789. X509* signCert = NULL;
  40790. EVP_PKEY* signKey = NULL;
  40791. X509* caCert = NULL;
  40792. X509_STORE* store = NULL;
  40793. printf(testingFmt, "wolfSSL_PKCS7_sign()");
  40794. /* read signer cert/key into BIO */
  40795. AssertNotNull(certBio = BIO_new_file(cert, "r"));
  40796. AssertNotNull(keyBio = BIO_new_file(key, "r"));
  40797. AssertNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  40798. AssertNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  40799. /* read CA cert into store (for verify) */
  40800. AssertNotNull(caBio = BIO_new_file(ca, "r"));
  40801. AssertNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  40802. AssertNotNull(store = X509_STORE_new());
  40803. AssertIntEQ(X509_STORE_add_cert(store, caCert), 1);
  40804. /* data to be signed into BIO */
  40805. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  40806. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  40807. /* PKCS7_sign, bad args: signer NULL */
  40808. AssertNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  40809. /* PKCS7_sign, bad args: signer key NULL */
  40810. AssertNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  40811. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  40812. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  40813. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  40814. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  40815. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  40816. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  40817. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  40818. {
  40819. flags = PKCS7_BINARY;
  40820. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  40821. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  40822. /* verify with d2i_PKCS7 */
  40823. tmpPtr = out;
  40824. AssertNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  40825. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  40826. PKCS7_free(p7Ver);
  40827. /* verify with wc_PKCS7_VerifySignedData */
  40828. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, devId));
  40829. AssertIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  40830. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  40831. /* compare the signer found to expected signer */
  40832. AssertIntNE(p7Ver->verifyCertSz, 0);
  40833. tmpPtr = NULL;
  40834. AssertIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  40835. AssertIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  40836. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  40837. tmpPtr = NULL;
  40838. wc_PKCS7_Free(p7Ver);
  40839. AssertNotNull(out);
  40840. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40841. out = NULL;
  40842. PKCS7_free(p7);
  40843. }
  40844. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  40845. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  40846. {
  40847. /* re-populate input BIO, may have been consumed */
  40848. BIO_free(inBio);
  40849. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  40850. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  40851. flags = PKCS7_BINARY | PKCS7_STREAM;
  40852. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  40853. AssertIntEQ(PKCS7_final(p7, inBio, flags), 1);
  40854. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  40855. /* PKCS7_final, bad args: PKCS7 null */
  40856. AssertIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  40857. /* PKCS7_final, bad args: PKCS7 null */
  40858. AssertIntEQ(PKCS7_final(p7, NULL, 0), 0);
  40859. tmpPtr = out;
  40860. AssertNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  40861. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  40862. PKCS7_free(p7Ver);
  40863. AssertNotNull(out);
  40864. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40865. out = NULL;
  40866. PKCS7_free(p7);
  40867. }
  40868. /* TEST SUCCESS: Detached, not streaming, not MIME */
  40869. {
  40870. /* re-populate input BIO, may have been consumed */
  40871. BIO_free(inBio);
  40872. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  40873. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  40874. flags = PKCS7_BINARY | PKCS7_DETACHED;
  40875. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  40876. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  40877. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  40878. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, devId));
  40879. p7Ver->content = data;
  40880. p7Ver->contentSz = sizeof(data);
  40881. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  40882. wc_PKCS7_Free(p7Ver);
  40883. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  40884. * yet support detached content */
  40885. tmpPtr = out;
  40886. AssertNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  40887. PKCS7_free(p7Ver);
  40888. AssertNotNull(out);
  40889. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40890. out = NULL;
  40891. PKCS7_free(p7);
  40892. }
  40893. /* TEST SUCCESS: Detached, streaming, not MIME */
  40894. {
  40895. /* re-populate input BIO, may have been consumed */
  40896. BIO_free(inBio);
  40897. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  40898. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  40899. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  40900. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  40901. AssertIntEQ(PKCS7_final(p7, inBio, flags), 1);
  40902. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  40903. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  40904. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, devId));
  40905. p7Ver->content = data;
  40906. p7Ver->contentSz = sizeof(data);
  40907. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  40908. wc_PKCS7_Free(p7Ver);
  40909. AssertNotNull(out);
  40910. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40911. PKCS7_free(p7);
  40912. }
  40913. X509_STORE_free(store);
  40914. X509_free(caCert);
  40915. X509_free(signCert);
  40916. EVP_PKEY_free(signKey);
  40917. BIO_free(inBio);
  40918. BIO_free(keyBio);
  40919. BIO_free(certBio);
  40920. BIO_free(caBio);
  40921. printf(resultFmt, passed);
  40922. #endif
  40923. }
  40924. static void test_wolfSSL_PKCS7_SIGNED_new(void)
  40925. {
  40926. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  40927. PKCS7_SIGNED* pkcs7;
  40928. printf(testingFmt, "wolfSSL_PKCS7_SIGNED_new()");
  40929. pkcs7 = PKCS7_SIGNED_new();
  40930. AssertNotNull(pkcs7);
  40931. AssertIntEQ(pkcs7->contentOID, SIGNED_DATA);
  40932. PKCS7_SIGNED_free(pkcs7);
  40933. printf(resultFmt, passed);
  40934. #endif
  40935. }
  40936. #ifndef NO_BIO
  40937. static void test_wolfSSL_PEM_write_bio_PKCS7(void)
  40938. {
  40939. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  40940. PKCS7* pkcs7 = NULL;
  40941. BIO* bio = NULL;
  40942. const byte* cert_buf = NULL;
  40943. int ret = 0;
  40944. WC_RNG rng;
  40945. const byte data[] = { /* Hello World */
  40946. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  40947. 0x72,0x6c,0x64
  40948. };
  40949. #ifndef NO_RSA
  40950. #if defined(USE_CERT_BUFFERS_2048)
  40951. byte key[sizeof(client_key_der_2048)];
  40952. byte cert[sizeof(client_cert_der_2048)];
  40953. word32 keySz = (word32)sizeof(key);
  40954. word32 certSz = (word32)sizeof(cert);
  40955. XMEMSET(key, 0, keySz);
  40956. XMEMSET(cert, 0, certSz);
  40957. XMEMCPY(key, client_key_der_2048, keySz);
  40958. XMEMCPY(cert, client_cert_der_2048, certSz);
  40959. #elif defined(USE_CERT_BUFFERS_1024)
  40960. byte key[sizeof_client_key_der_1024];
  40961. byte cert[sizeof(sizeof_client_cert_der_1024)];
  40962. word32 keySz = (word32)sizeof(key);
  40963. word32 certSz = (word32)sizeof(cert);
  40964. XMEMSET(key, 0, keySz);
  40965. XMEMSET(cert, 0, certSz);
  40966. XMEMCPY(key, client_key_der_1024, keySz);
  40967. XMEMCPY(cert, client_cert_der_1024, certSz);
  40968. #else
  40969. unsigned char cert[ONEK_BUF];
  40970. unsigned char key[ONEK_BUF];
  40971. XFILE fp;
  40972. int certSz;
  40973. int keySz;
  40974. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  40975. AssertTrue((fp != XBADFILE));
  40976. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  40977. XFCLOSE(fp);
  40978. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  40979. AssertTrue(fp != XBADFILE);
  40980. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  40981. XFCLOSE(fp);
  40982. #endif
  40983. #elif defined(HAVE_ECC)
  40984. #if defined(USE_CERT_BUFFERS_256)
  40985. unsigned char cert[sizeof(cliecc_cert_der_256)];
  40986. unsigned char key[sizeof(ecc_clikey_der_256)];
  40987. int certSz = (int)sizeof(cert);
  40988. int keySz = (int)sizeof(key);
  40989. XMEMSET(cert, 0, certSz);
  40990. XMEMSET(key, 0, keySz);
  40991. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  40992. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  40993. #else
  40994. unsigned char cert[ONEK_BUF];
  40995. unsigned char key[ONEK_BUF];
  40996. XFILE fp;
  40997. int certSz, keySz;
  40998. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  40999. AssertTrue(fp != XBADFILE);
  41000. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  41001. XFCLOSE(fp);
  41002. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  41003. AssertTrue(fp != XBADFILE);
  41004. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  41005. XFCLOSE(fp);
  41006. #endif
  41007. #else
  41008. #error PKCS7 requires ECC or RSA
  41009. #endif
  41010. printf(testingFmt, "wolfSSL_PEM_write_bio_PKCS7()");
  41011. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  41012. /* initialize with DER encoded cert */
  41013. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  41014. /* init rng */
  41015. AssertIntEQ(wc_InitRng(&rng), 0);
  41016. pkcs7->rng = &rng;
  41017. pkcs7->content = (byte*)data; /* not used for ex */
  41018. pkcs7->contentSz = (word32)sizeof(data);
  41019. pkcs7->contentOID = SIGNED_DATA;
  41020. pkcs7->privateKey = key;
  41021. pkcs7->privateKeySz = (word32)sizeof(key);
  41022. pkcs7->encryptOID = RSAk;
  41023. pkcs7->hashOID = SHAh;
  41024. pkcs7->signedAttribs = NULL;
  41025. pkcs7->signedAttribsSz = 0;
  41026. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  41027. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  41028. AssertIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  41029. /* Read PKCS#7 PEM from BIO */
  41030. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  41031. AssertIntGE(ret, 0);
  41032. BIO_free(bio);
  41033. wc_PKCS7_Free(pkcs7);
  41034. wc_FreeRng(&rng);
  41035. printf(resultFmt, passed);
  41036. #endif
  41037. }
  41038. #ifdef HAVE_SMIME
  41039. static void test_wolfSSL_SMIME_read_PKCS7(void)
  41040. {
  41041. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  41042. !defined(NO_RSA)
  41043. PKCS7* pkcs7 = NULL;
  41044. BIO* bio = NULL;
  41045. BIO* bcont = NULL;
  41046. BIO* out = NULL;
  41047. const byte* outBuf = NULL;
  41048. int outBufLen = 0;
  41049. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  41050. XFILE smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r");
  41051. printf(testingFmt, "wolfSSL_SMIME_read_PKCS7()");
  41052. /* smime-test.p7s */
  41053. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  41054. AssertNotNull(bio);
  41055. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  41056. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  41057. AssertNotNull(pkcs7);
  41058. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  41059. PKCS7_NOVERIFY), SSL_SUCCESS);
  41060. XFCLOSE(smimeTestFile);
  41061. if (bcont) BIO_free(bcont);
  41062. wolfSSL_PKCS7_free(pkcs7);
  41063. /* smime-test-multipart.p7s */
  41064. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  41065. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  41066. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  41067. AssertNotNull(pkcs7);
  41068. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  41069. PKCS7_NOVERIFY), SSL_SUCCESS);
  41070. XFCLOSE(smimeTestFile);
  41071. if (bcont) BIO_free(bcont);
  41072. wolfSSL_PKCS7_free(pkcs7);
  41073. /* smime-test-multipart-badsig.p7s */
  41074. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  41075. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  41076. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  41077. AssertNull(pkcs7);
  41078. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  41079. PKCS7_NOVERIFY), SSL_FAILURE);
  41080. XFCLOSE(smimeTestFile);
  41081. if (bcont) BIO_free(bcont);
  41082. wolfSSL_PKCS7_free(pkcs7);
  41083. /* smime-test-canon.p7s */
  41084. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  41085. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  41086. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  41087. AssertNotNull(pkcs7);
  41088. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  41089. PKCS7_NOVERIFY), SSL_SUCCESS);
  41090. XFCLOSE(smimeTestFile);
  41091. if (bcont) BIO_free(bcont);
  41092. wolfSSL_PKCS7_free(pkcs7);
  41093. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  41094. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  41095. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  41096. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  41097. AssertNotNull(pkcs7);
  41098. out = wolfSSL_BIO_new(BIO_s_mem());
  41099. AssertNotNull(out);
  41100. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  41101. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  41102. AssertIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  41103. /* Content-Type should not show up at beginning of output buffer */
  41104. AssertIntGT(outBufLen, XSTRLEN(contTypeText));
  41105. AssertIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  41106. BIO_free(out);
  41107. BIO_free(bio);
  41108. if (bcont) BIO_free(bcont);
  41109. wolfSSL_PKCS7_free(pkcs7);
  41110. printf(resultFmt, passed);
  41111. #endif
  41112. }
  41113. static void test_wolfSSL_SMIME_write_PKCS7(void)
  41114. {
  41115. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  41116. PKCS7* p7 = NULL;
  41117. PKCS7* p7Ver = NULL;
  41118. int flags = 0;
  41119. byte data[] = "Test data to encode.";
  41120. const char* cert = "./certs/server-cert.pem";
  41121. const char* key = "./certs/server-key.pem";
  41122. const char* ca = "./certs/ca-cert.pem";
  41123. WOLFSSL_BIO* certBio = NULL;
  41124. WOLFSSL_BIO* keyBio = NULL;
  41125. WOLFSSL_BIO* caBio = NULL;
  41126. WOLFSSL_BIO* inBio = NULL;
  41127. WOLFSSL_BIO* outBio = NULL;
  41128. WOLFSSL_BIO* content = NULL;
  41129. X509* signCert = NULL;
  41130. EVP_PKEY* signKey = NULL;
  41131. X509* caCert = NULL;
  41132. X509_STORE* store = NULL;
  41133. printf(testingFmt, "wolfSSL_SMIME_write_PKCS7()");
  41134. /* read signer cert/key into BIO */
  41135. AssertNotNull(certBio = BIO_new_file(cert, "r"));
  41136. AssertNotNull(keyBio = BIO_new_file(key, "r"));
  41137. AssertNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  41138. AssertNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  41139. /* read CA cert into store (for verify) */
  41140. AssertNotNull(caBio = BIO_new_file(ca, "r"));
  41141. AssertNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  41142. AssertNotNull(store = X509_STORE_new());
  41143. AssertIntEQ(X509_STORE_add_cert(store, caCert), 1);
  41144. /* generate and verify SMIME: not detached */
  41145. {
  41146. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  41147. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  41148. flags = PKCS7_STREAM;
  41149. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  41150. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  41151. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  41152. /* bad arg: out NULL */
  41153. AssertIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  41154. /* bad arg: pkcs7 NULL */
  41155. AssertIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  41156. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  41157. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  41158. BIO_free(content);
  41159. BIO_free(inBio);
  41160. BIO_free(outBio);
  41161. PKCS7_free(p7Ver);
  41162. PKCS7_free(p7);
  41163. }
  41164. /* generate and verify SMIME: not detached, add Content-Type */
  41165. {
  41166. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  41167. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  41168. flags = PKCS7_STREAM | PKCS7_TEXT;
  41169. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  41170. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  41171. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  41172. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  41173. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  41174. BIO_free(content);
  41175. BIO_free(inBio);
  41176. BIO_free(outBio);
  41177. PKCS7_free(p7Ver);
  41178. PKCS7_free(p7);
  41179. }
  41180. /* generate and verify SMIME: detached */
  41181. {
  41182. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  41183. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  41184. flags = PKCS7_DETACHED | PKCS7_STREAM;
  41185. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  41186. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  41187. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  41188. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  41189. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  41190. BIO_free(content);
  41191. BIO_free(inBio);
  41192. BIO_free(outBio);
  41193. PKCS7_free(p7Ver);
  41194. PKCS7_free(p7);
  41195. }
  41196. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  41197. {
  41198. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  41199. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  41200. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  41201. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  41202. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  41203. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  41204. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  41205. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  41206. BIO_free(content);
  41207. BIO_free(inBio);
  41208. BIO_free(outBio);
  41209. PKCS7_free(p7Ver);
  41210. PKCS7_free(p7);
  41211. }
  41212. X509_STORE_free(store);
  41213. X509_free(caCert);
  41214. X509_free(signCert);
  41215. EVP_PKEY_free(signKey);
  41216. BIO_free(keyBio);
  41217. BIO_free(certBio);
  41218. BIO_free(caBio);
  41219. printf(resultFmt, passed);
  41220. #endif
  41221. }
  41222. #endif /* HAVE_SMIME*/
  41223. #endif /* !NO_BIO */
  41224. /*----------------------------------------------------------------------------*
  41225. | Certificate Failure Checks
  41226. *----------------------------------------------------------------------------*/
  41227. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  41228. !defined(WOLFSSL_NO_CLIENT_AUTH))
  41229. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  41230. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  41231. int type)
  41232. {
  41233. int ret;
  41234. WOLFSSL_CERT_MANAGER* cm = NULL;
  41235. switch (type) {
  41236. case TESTING_RSA:
  41237. #ifdef NO_RSA
  41238. printf("RSA disabled, skipping test\n");
  41239. return ASN_SIG_CONFIRM_E;
  41240. #else
  41241. break;
  41242. #endif
  41243. case TESTING_ECC:
  41244. #ifndef HAVE_ECC
  41245. printf("ECC disabled, skipping test\n");
  41246. return ASN_SIG_CONFIRM_E;
  41247. #else
  41248. break;
  41249. #endif
  41250. default:
  41251. printf("Bad function argument\n");
  41252. return BAD_FUNC_ARG;
  41253. }
  41254. cm = wolfSSL_CertManagerNew();
  41255. if (cm == NULL) {
  41256. printf("wolfSSL_CertManagerNew failed\n");
  41257. return -1;
  41258. }
  41259. #ifndef NO_FILESYSTEM
  41260. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  41261. if (ret != WOLFSSL_SUCCESS) {
  41262. printf("wolfSSL_CertManagerLoadCA failed\n");
  41263. wolfSSL_CertManagerFree(cm);
  41264. return ret;
  41265. }
  41266. #else
  41267. (void)ca;
  41268. #endif
  41269. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz, WOLFSSL_FILETYPE_ASN1);
  41270. /* Let AssertIntEQ handle return code */
  41271. wolfSSL_CertManagerFree(cm);
  41272. return ret;
  41273. }
  41274. static int test_RsaSigFailure_cm(void)
  41275. {
  41276. int ret = 0;
  41277. const char* ca_cert = "./certs/ca-cert.pem";
  41278. const char* server_cert = "./certs/server-cert.der";
  41279. byte* cert_buf = NULL;
  41280. size_t cert_sz = 0;
  41281. ret = load_file(server_cert, &cert_buf, &cert_sz);
  41282. if (ret == 0) {
  41283. /* corrupt DER - invert last byte, which is signature */
  41284. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  41285. /* test bad cert */
  41286. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA);
  41287. }
  41288. printf("Signature failure test: RSA: Ret %d\n", ret);
  41289. if (cert_buf)
  41290. free(cert_buf);
  41291. return ret;
  41292. }
  41293. static int test_EccSigFailure_cm(void)
  41294. {
  41295. int ret = 0;
  41296. /* self-signed ECC cert, so use server cert as CA */
  41297. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  41298. const char* server_cert = "./certs/server-ecc.der";
  41299. byte* cert_buf = NULL;
  41300. size_t cert_sz = 0;
  41301. ret = load_file(server_cert, &cert_buf, &cert_sz);
  41302. if (ret == 0) {
  41303. /* corrupt DER - invert last byte, which is signature */
  41304. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  41305. /* test bad cert */
  41306. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC);
  41307. }
  41308. printf("Signature failure test: ECC: Ret %d\n", ret);
  41309. if (cert_buf)
  41310. free(cert_buf);
  41311. #ifdef FP_ECC
  41312. wc_ecc_fp_free();
  41313. #endif
  41314. return ret;
  41315. }
  41316. #endif /* NO_CERTS */
  41317. #ifdef WOLFSSL_TLS13
  41318. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  41319. #ifdef WC_SHA384_DIGEST_SIZE
  41320. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  41321. #else
  41322. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  41323. #endif
  41324. #endif
  41325. #ifdef WOLFSSL_EARLY_DATA
  41326. static const char earlyData[] = "Early Data";
  41327. static char earlyDataBuffer[1];
  41328. #endif
  41329. static int test_tls13_apis(void)
  41330. {
  41331. int ret = 0;
  41332. #ifndef WOLFSSL_NO_TLS12
  41333. #ifndef NO_WOLFSSL_CLIENT
  41334. WOLFSSL_CTX* clientTls12Ctx;
  41335. WOLFSSL* clientTls12Ssl;
  41336. #endif
  41337. #ifndef NO_WOLFSSL_SERVER
  41338. WOLFSSL_CTX* serverTls12Ctx;
  41339. WOLFSSL* serverTls12Ssl;
  41340. #endif
  41341. #endif
  41342. #ifndef NO_WOLFSSL_CLIENT
  41343. WOLFSSL_CTX* clientCtx;
  41344. WOLFSSL* clientSsl;
  41345. #endif
  41346. #ifndef NO_WOLFSSL_SERVER
  41347. WOLFSSL_CTX* serverCtx;
  41348. WOLFSSL* serverSsl;
  41349. #ifndef NO_CERTS
  41350. const char* ourCert = svrCertFile;
  41351. const char* ourKey = svrKeyFile;
  41352. #endif
  41353. #endif
  41354. int required;
  41355. #ifdef WOLFSSL_EARLY_DATA
  41356. int outSz;
  41357. #endif
  41358. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  41359. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  41360. #ifdef HAVE_PQC
  41361. WOLFSSL_SABER_LEVEL3
  41362. #else
  41363. WOLFSSL_ECC_SECP256R1
  41364. #endif
  41365. };
  41366. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  41367. int numGroups = 2;
  41368. #endif
  41369. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  41370. char groupList[] =
  41371. #ifndef NO_ECC_SECP
  41372. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  41373. "P-521:"
  41374. #endif
  41375. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  41376. "P-384:"
  41377. #endif
  41378. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  41379. "P-256"
  41380. #ifdef HAVE_PQC
  41381. ":P256_SABER_LEVEL1"
  41382. #endif
  41383. #endif
  41384. #ifdef HAVE_PQC
  41385. ":KYBER_LEVEL1"
  41386. #endif
  41387. "";
  41388. #endif /* !defined(NO_ECC_SECP) */
  41389. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  41390. #ifndef WOLFSSL_NO_TLS12
  41391. #ifndef NO_WOLFSSL_CLIENT
  41392. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  41393. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  41394. #endif
  41395. #ifndef NO_WOLFSSL_SERVER
  41396. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  41397. #ifndef NO_CERTS
  41398. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  41399. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey, WOLFSSL_FILETYPE_PEM);
  41400. #endif
  41401. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  41402. #endif
  41403. #endif
  41404. #ifndef NO_WOLFSSL_CLIENT
  41405. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  41406. clientSsl = wolfSSL_new(clientCtx);
  41407. #endif
  41408. #ifndef NO_WOLFSSL_SERVER
  41409. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  41410. #ifndef NO_CERTS
  41411. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  41412. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  41413. #endif
  41414. serverSsl = wolfSSL_new(serverCtx);
  41415. #endif
  41416. #ifdef WOLFSSL_SEND_HRR_COOKIE
  41417. AssertIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  41418. #ifndef NO_WOLFSSL_CLIENT
  41419. AssertIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  41420. #endif
  41421. #ifndef NO_WOLFSSL_SERVER
  41422. #ifndef WOLFSSL_NO_TLS12
  41423. AssertIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0), BAD_FUNC_ARG);
  41424. #endif
  41425. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  41426. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  41427. WOLFSSL_SUCCESS);
  41428. #endif
  41429. #endif
  41430. #ifdef HAVE_SUPPORTED_CURVES
  41431. #ifdef HAVE_ECC
  41432. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  41433. #ifndef NO_WOLFSSL_SERVER
  41434. do {
  41435. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  41436. #ifdef WOLFSSL_ASYNC_CRYPT
  41437. if (ret == WC_PENDING_E)
  41438. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  41439. #endif
  41440. } while (ret == WC_PENDING_E);
  41441. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  41442. #endif
  41443. #ifndef NO_WOLFSSL_CLIENT
  41444. #ifndef WOLFSSL_NO_TLS12
  41445. do {
  41446. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  41447. #ifdef WOLFSSL_ASYNC_CRYPT
  41448. if (ret == WC_PENDING_E)
  41449. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  41450. #endif
  41451. } while (ret == WC_PENDING_E);
  41452. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  41453. #endif
  41454. do {
  41455. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  41456. #ifdef WOLFSSL_ASYNC_CRYPT
  41457. if (ret == WC_PENDING_E)
  41458. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  41459. #endif
  41460. } while (ret == WC_PENDING_E);
  41461. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  41462. #endif
  41463. #elif defined(HAVE_CURVE25519)
  41464. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  41465. #ifndef NO_WOLFSSL_SERVER
  41466. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  41467. WOLFSSL_SUCCESS);
  41468. #endif
  41469. #ifndef NO_WOLFSSL_CLIENT
  41470. #ifndef WOLFSSL_NO_TLS12
  41471. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  41472. WOLFSSL_SUCCESS);
  41473. #endif
  41474. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  41475. WOLFSSL_SUCCESS);
  41476. #endif
  41477. #elif defined(HAVE_CURVE448)
  41478. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  41479. #ifndef NO_WOLFSSL_SERVER
  41480. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  41481. WOLFSSL_SUCCESS);
  41482. #endif
  41483. #ifndef NO_WOLFSSL_CLIENT
  41484. #ifndef WOLFSSL_NO_TLS12
  41485. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  41486. WOLFSSL_SUCCESS);
  41487. #endif
  41488. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  41489. WOLFSSL_SUCCESS);
  41490. #endif
  41491. #else
  41492. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  41493. #ifndef NO_WOLFSSL_CLIENT
  41494. #ifndef WOLFSSL_NO_TLS12
  41495. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  41496. NOT_COMPILED_IN);
  41497. #endif
  41498. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  41499. NOT_COMPILED_IN);
  41500. #endif
  41501. #endif
  41502. #if defined(HAVE_PQC)
  41503. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  41504. #ifndef NO_WOLFSSL_SERVER
  41505. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  41506. WOLFSSL_SUCCESS);
  41507. #endif
  41508. #ifndef NO_WOLFSSL_CLIENT
  41509. #ifndef WOLFSSL_NO_TLS12
  41510. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  41511. BAD_FUNC_ARG);
  41512. #endif
  41513. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  41514. WOLFSSL_SUCCESS);
  41515. #endif
  41516. #endif
  41517. AssertIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  41518. #ifndef NO_WOLFSSL_SERVER
  41519. AssertIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  41520. #endif
  41521. #ifndef NO_WOLFSSL_CLIENT
  41522. #ifndef WOLFSSL_NO_TLS12
  41523. AssertIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  41524. #endif
  41525. AssertIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  41526. #endif
  41527. #endif /* HAVE_SUPPORTED_CURVES */
  41528. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  41529. #ifndef NO_WOLFSSL_CLIENT
  41530. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  41531. #endif
  41532. #ifndef NO_WOLFSSL_SERVER
  41533. #ifndef WOLFSSL_NO_TLS12
  41534. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  41535. #endif
  41536. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  41537. #endif
  41538. AssertIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  41539. #ifndef NO_WOLFSSL_CLIENT
  41540. AssertIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  41541. #endif
  41542. #ifndef NO_WOLFSSL_SERVER
  41543. #ifndef WOLFSSL_NO_TLS12
  41544. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  41545. #endif
  41546. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  41547. #endif
  41548. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  41549. #ifndef NO_WOLFSSL_CLIENT
  41550. #ifndef WOLFSSL_NO_TLS12
  41551. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  41552. #endif
  41553. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  41554. #endif
  41555. #ifndef NO_WOLFSSL_SERVER
  41556. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  41557. #endif
  41558. AssertIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  41559. #ifndef NO_WOLFSSL_CLIENT
  41560. #ifndef WOLFSSL_NO_TLS12
  41561. AssertIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  41562. #endif
  41563. AssertIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  41564. #endif
  41565. #ifndef NO_WOLFSSL_SERVER
  41566. AssertIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  41567. #endif
  41568. AssertIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  41569. #ifndef NO_WOLFSSL_CLIENT
  41570. #ifndef WOLFSSL_NO_TLS12
  41571. AssertIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  41572. #endif
  41573. AssertIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  41574. #endif
  41575. #ifndef NO_WOLFSSL_SERVER
  41576. AssertIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  41577. #endif
  41578. AssertIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  41579. AssertIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  41580. #ifndef NO_WOLFSSL_CLIENT
  41581. #ifndef WOLFSSL_NO_TLS12
  41582. AssertIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  41583. BAD_FUNC_ARG);
  41584. #endif
  41585. AssertIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  41586. #endif
  41587. #ifndef NO_WOLFSSL_SERVER
  41588. AssertIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  41589. #endif
  41590. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  41591. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  41592. #ifndef NO_WOLFSSL_SERVER
  41593. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  41594. #endif
  41595. #ifndef NO_WOLFSSL_CLIENT
  41596. #ifndef WOLFSSL_NO_TLS12
  41597. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  41598. BAD_FUNC_ARG);
  41599. #endif
  41600. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  41601. #endif
  41602. AssertIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  41603. #ifndef NO_WOLFSSL_SERVER
  41604. AssertIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  41605. #endif
  41606. #ifndef NO_WOLFSSL_CLIENT
  41607. #ifndef WOLFSSL_NO_TLS12
  41608. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  41609. BAD_FUNC_ARG);
  41610. #endif
  41611. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  41612. #endif
  41613. AssertIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  41614. #ifndef NO_WOLFSSL_CLIENT
  41615. AssertIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  41616. #endif
  41617. #ifndef NO_WOLFSSL_SERVER
  41618. #ifndef WOLFSSL_NO_TLS12
  41619. AssertIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  41620. BAD_FUNC_ARG);
  41621. #endif
  41622. AssertIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  41623. #endif
  41624. #endif
  41625. #ifdef HAVE_ECC
  41626. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  41627. AssertIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  41628. #ifndef NO_WOLFSSL_SERVER
  41629. AssertIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  41630. #endif
  41631. #ifndef NO_WOLFSSL_CLIENT
  41632. #ifndef WOLFSSL_NO_TLS12
  41633. AssertIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  41634. #endif
  41635. AssertIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  41636. #endif
  41637. #endif
  41638. #ifdef HAVE_SUPPORTED_CURVES
  41639. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  41640. #ifndef NO_WOLFSSL_CLIENT
  41641. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  41642. #endif
  41643. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  41644. #ifndef NO_WOLFSSL_CLIENT
  41645. #ifndef WOLFSSL_NO_TLS12
  41646. AssertIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  41647. BAD_FUNC_ARG);
  41648. #endif
  41649. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  41650. WOLFSSL_MAX_GROUP_COUNT + 1),
  41651. BAD_FUNC_ARG);
  41652. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  41653. WOLFSSL_SUCCESS);
  41654. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  41655. BAD_FUNC_ARG);
  41656. #endif
  41657. #ifndef NO_WOLFSSL_SERVER
  41658. AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  41659. WOLFSSL_SUCCESS);
  41660. AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  41661. BAD_FUNC_ARG);
  41662. #endif
  41663. AssertIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  41664. #ifndef NO_WOLFSSL_CLIENT
  41665. AssertIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  41666. #endif
  41667. AssertIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  41668. #ifndef NO_WOLFSSL_CLIENT
  41669. #ifndef WOLFSSL_NO_TLS12
  41670. AssertIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  41671. BAD_FUNC_ARG);
  41672. #endif
  41673. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups,
  41674. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  41675. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  41676. WOLFSSL_SUCCESS);
  41677. AssertIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  41678. BAD_FUNC_ARG);
  41679. #endif
  41680. #ifndef NO_WOLFSSL_SERVER
  41681. AssertIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  41682. WOLFSSL_SUCCESS);
  41683. AssertIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  41684. BAD_FUNC_ARG);
  41685. #endif
  41686. #ifdef OPENSSL_EXTRA
  41687. AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  41688. #ifndef NO_WOLFSSL_CLIENT
  41689. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL), WOLFSSL_FAILURE);
  41690. #endif
  41691. AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  41692. #ifndef NO_WOLFSSL_CLIENT
  41693. #ifndef WOLFSSL_NO_TLS12
  41694. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  41695. WOLFSSL_FAILURE);
  41696. #endif
  41697. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  41698. WOLFSSL_SUCCESS);
  41699. #endif
  41700. #ifndef NO_WOLFSSL_SERVER
  41701. AssertIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  41702. WOLFSSL_SUCCESS);
  41703. #endif
  41704. AssertIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  41705. #ifndef NO_WOLFSSL_CLIENT
  41706. AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  41707. #endif
  41708. AssertIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  41709. #ifndef NO_WOLFSSL_CLIENT
  41710. #ifndef WOLFSSL_NO_TLS12
  41711. AssertIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  41712. WOLFSSL_FAILURE);
  41713. #endif
  41714. AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  41715. WOLFSSL_SUCCESS);
  41716. #endif
  41717. #ifndef NO_WOLFSSL_SERVER
  41718. AssertIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  41719. WOLFSSL_SUCCESS);
  41720. #endif
  41721. #endif /* OPENSSL_EXTRA */
  41722. #endif /* HAVE_SUPPORTED_CURVES */
  41723. #endif /* HAVE_ECC */
  41724. #ifdef WOLFSSL_EARLY_DATA
  41725. #ifndef OPENSSL_EXTRA
  41726. AssertIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  41727. AssertIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  41728. #else
  41729. AssertIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  41730. AssertIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  41731. #endif
  41732. #ifndef NO_WOLFSSL_CLIENT
  41733. #ifndef OPENSSL_EXTRA
  41734. AssertIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  41735. AssertIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  41736. #else
  41737. AssertIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  41738. AssertIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  41739. #endif
  41740. #endif
  41741. #ifndef NO_WOLFSSL_SERVER
  41742. #ifndef WOLFSSL_NO_TLS12
  41743. #ifndef OPENSSL_EXTRA
  41744. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  41745. BAD_FUNC_ARG);
  41746. AssertIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  41747. #else
  41748. AssertIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  41749. BAD_FUNC_ARG);
  41750. AssertIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  41751. #endif
  41752. #endif
  41753. #ifndef OPENSSL_EXTRA
  41754. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  41755. AssertIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  41756. #else
  41757. AssertIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  41758. AssertIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  41759. #endif
  41760. #endif
  41761. #ifndef OPENSSL_EXTRA
  41762. AssertIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  41763. AssertIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  41764. #else
  41765. AssertIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  41766. AssertIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  41767. #endif
  41768. #ifndef NO_WOLFSSL_CLIENT
  41769. #ifndef OPENSSL_EXTRA
  41770. AssertIntEQ(wolfSSL_set_max_early_data(clientSsl, 0), SIDE_ERROR);
  41771. AssertIntEQ(wolfSSL_get_max_early_data(clientSsl), SIDE_ERROR);
  41772. #else
  41773. AssertIntEQ(SSL_set_max_early_data(clientSsl, 0), SIDE_ERROR);
  41774. AssertIntEQ(SSL_get_max_early_data(clientSsl), SIDE_ERROR);
  41775. #endif
  41776. #endif
  41777. #ifndef NO_WOLFSSL_SERVER
  41778. #ifndef WOLFSSL_NO_TLS12
  41779. #ifndef OPENSSL_EXTRA
  41780. AssertIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  41781. AssertIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  41782. #else
  41783. AssertIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  41784. AssertIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  41785. #endif
  41786. #endif
  41787. #ifndef OPENSSL_EXTRA
  41788. AssertIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  41789. AssertIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  41790. #else
  41791. AssertIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  41792. AssertIntEQ(SSL_get_max_early_data(serverSsl), 16);
  41793. #endif
  41794. #endif
  41795. AssertIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  41796. &outSz), BAD_FUNC_ARG);
  41797. #ifndef NO_WOLFSSL_CLIENT
  41798. AssertIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  41799. &outSz), BAD_FUNC_ARG);
  41800. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  41801. BAD_FUNC_ARG);
  41802. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  41803. sizeof(earlyData), NULL),
  41804. BAD_FUNC_ARG);
  41805. #endif
  41806. #ifndef NO_WOLFSSL_SERVER
  41807. AssertIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  41808. sizeof(earlyData), &outSz),
  41809. SIDE_ERROR);
  41810. #endif
  41811. #ifndef NO_WOLFSSL_CLIENT
  41812. #ifndef WOLFSSL_NO_TLS12
  41813. AssertIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  41814. sizeof(earlyData), &outSz),
  41815. BAD_FUNC_ARG);
  41816. #endif
  41817. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  41818. sizeof(earlyData), &outSz),
  41819. WOLFSSL_FATAL_ERROR);
  41820. #endif
  41821. AssertIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  41822. sizeof(earlyDataBuffer), &outSz),
  41823. BAD_FUNC_ARG);
  41824. #ifndef NO_WOLFSSL_SERVER
  41825. AssertIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  41826. sizeof(earlyDataBuffer), &outSz),
  41827. BAD_FUNC_ARG);
  41828. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1, &outSz),
  41829. BAD_FUNC_ARG);
  41830. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  41831. sizeof(earlyDataBuffer), NULL),
  41832. BAD_FUNC_ARG);
  41833. #endif
  41834. #ifndef NO_WOLFSSL_CLIENT
  41835. AssertIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  41836. sizeof(earlyDataBuffer), &outSz),
  41837. SIDE_ERROR);
  41838. #endif
  41839. #ifndef NO_WOLFSSL_SERVER
  41840. #ifndef WOLFSSL_NO_TLS12
  41841. AssertIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  41842. sizeof(earlyDataBuffer), &outSz),
  41843. BAD_FUNC_ARG);
  41844. #endif
  41845. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  41846. sizeof(earlyDataBuffer), &outSz),
  41847. WOLFSSL_FATAL_ERROR);
  41848. #endif
  41849. #endif
  41850. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  41851. AssertIntLT(SSL_get_early_data_status(NULL), 0);
  41852. #endif
  41853. #ifndef NO_WOLFSSL_SERVER
  41854. wolfSSL_free(serverSsl);
  41855. wolfSSL_CTX_free(serverCtx);
  41856. #endif
  41857. #ifndef NO_WOLFSSL_CLIENT
  41858. wolfSSL_free(clientSsl);
  41859. wolfSSL_CTX_free(clientCtx);
  41860. #endif
  41861. #ifndef WOLFSSL_NO_TLS12
  41862. #ifndef NO_WOLFSSL_SERVER
  41863. wolfSSL_free(serverTls12Ssl);
  41864. wolfSSL_CTX_free(serverTls12Ctx);
  41865. #endif
  41866. #ifndef NO_WOLFSSL_CLIENT
  41867. wolfSSL_free(clientTls12Ssl);
  41868. wolfSSL_CTX_free(clientTls12Ctx);
  41869. #endif
  41870. #endif
  41871. return ret;
  41872. }
  41873. #endif
  41874. #if defined(HAVE_PK_CALLBACKS) && (!defined(WOLFSSL_NO_TLS12) || \
  41875. !defined(NO_OLD_TLS))
  41876. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  41877. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && \
  41878. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  41879. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  41880. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  41881. const unsigned char* priv, unsigned int privSz,
  41882. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  41883. unsigned char* out, unsigned int* outlen,
  41884. void* ctx)
  41885. {
  41886. int result;
  41887. /* Test fail when context associated with WOLFSSL is NULL */
  41888. if (ctx == NULL) {
  41889. return -1;
  41890. }
  41891. (void)ssl;
  41892. /* return 0 on success */
  41893. PRIVATE_KEY_UNLOCK();
  41894. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  41895. PRIVATE_KEY_LOCK();
  41896. return result;
  41897. }
  41898. static void test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  41899. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  41900. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  41901. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  41902. WOLFSSL_SUCCESS);
  41903. #endif
  41904. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  41905. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  41906. WOLFSSL_SUCCESS);
  41907. #endif
  41908. }
  41909. static void test_dh_ssl_setup(WOLFSSL* ssl)
  41910. {
  41911. static int dh_test_ctx = 1;
  41912. int ret;
  41913. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  41914. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  41915. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  41916. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  41917. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  41918. }
  41919. }
  41920. static void test_dh_ssl_setup_fail(WOLFSSL* ssl)
  41921. {
  41922. int ret;
  41923. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  41924. AssertNull(wolfSSL_GetDhAgreeCtx(ssl));
  41925. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  41926. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  41927. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  41928. }
  41929. }
  41930. #endif
  41931. static void test_DhCallbacks(void)
  41932. {
  41933. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  41934. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && \
  41935. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  41936. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  41937. WOLFSSL_CTX *ctx;
  41938. WOLFSSL *ssl;
  41939. tcp_ready ready;
  41940. func_args server_args;
  41941. func_args client_args;
  41942. THREAD_TYPE serverThread;
  41943. callback_functions func_cb_client;
  41944. callback_functions func_cb_server;
  41945. int test;
  41946. printf(testingFmt, "test_DhCallbacks");
  41947. #ifndef NO_WOLFSSL_CLIENT
  41948. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  41949. #else
  41950. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  41951. #endif
  41952. AssertIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  41953. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  41954. /* load client ca cert */
  41955. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  41956. WOLFSSL_SUCCESS);
  41957. /* test with NULL arguments */
  41958. wolfSSL_SetDhAgreeCtx(NULL, &test);
  41959. AssertNull(wolfSSL_GetDhAgreeCtx(NULL));
  41960. /* test success case */
  41961. test = 1;
  41962. AssertNotNull(ssl = wolfSSL_new(ctx));
  41963. wolfSSL_SetDhAgreeCtx(ssl, &test);
  41964. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  41965. wolfSSL_free(ssl);
  41966. wolfSSL_CTX_free(ctx);
  41967. /* test a connection where callback is used */
  41968. #ifdef WOLFSSL_TIRTOS
  41969. fdOpenSession(Task_self());
  41970. #endif
  41971. XMEMSET(&server_args, 0, sizeof(func_args));
  41972. XMEMSET(&client_args, 0, sizeof(func_args));
  41973. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  41974. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  41975. StartTCP();
  41976. InitTcpReady(&ready);
  41977. #if defined(USE_WINDOWS_API)
  41978. /* use RNG to get random port if using windows */
  41979. ready.port = GetRandomPort();
  41980. #endif
  41981. server_args.signal = &ready;
  41982. client_args.signal = &ready;
  41983. server_args.return_code = TEST_FAIL;
  41984. client_args.return_code = TEST_FAIL;
  41985. /* set callbacks to use DH functions */
  41986. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  41987. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  41988. #ifndef WOLFSSL_NO_TLS12
  41989. func_cb_client.method = wolfTLSv1_2_client_method;
  41990. #else
  41991. func_cb_client.method = wolfTLSv1_3_client_method;
  41992. #endif
  41993. client_args.callbacks = &func_cb_client;
  41994. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  41995. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  41996. #ifndef WOLFSSL_NO_TLS12
  41997. func_cb_server.method = wolfTLSv1_2_server_method;
  41998. #else
  41999. func_cb_server.method = wolfTLSv1_3_server_method;
  42000. #endif
  42001. server_args.callbacks = &func_cb_server;
  42002. start_thread(test_server_nofail, &server_args, &serverThread);
  42003. wait_tcp_ready(&server_args);
  42004. test_client_nofail(&client_args, NULL);
  42005. join_thread(serverThread);
  42006. AssertTrue(client_args.return_code);
  42007. AssertTrue(server_args.return_code);
  42008. FreeTcpReady(&ready);
  42009. #ifdef WOLFSSL_TIRTOS
  42010. fdOpenSession(Task_self());
  42011. #endif
  42012. /* now set user ctx to not be 1 so that the callback returns fail case */
  42013. #ifdef WOLFSSL_TIRTOS
  42014. fdOpenSession(Task_self());
  42015. #endif
  42016. XMEMSET(&server_args, 0, sizeof(func_args));
  42017. XMEMSET(&client_args, 0, sizeof(func_args));
  42018. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  42019. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  42020. StartTCP();
  42021. InitTcpReady(&ready);
  42022. #if defined(USE_WINDOWS_API)
  42023. /* use RNG to get random port if using windows */
  42024. ready.port = GetRandomPort();
  42025. #endif
  42026. server_args.signal = &ready;
  42027. client_args.signal = &ready;
  42028. server_args.return_code = TEST_FAIL;
  42029. client_args.return_code = TEST_FAIL;
  42030. /* set callbacks to use DH functions */
  42031. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  42032. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  42033. #ifndef WOLFSSL_NO_TLS12
  42034. func_cb_client.method = wolfTLSv1_2_client_method;
  42035. #else
  42036. func_cb_client.method = wolfTLSv1_3_client_method;
  42037. #endif
  42038. client_args.callbacks = &func_cb_client;
  42039. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  42040. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  42041. #ifndef WOLFSSL_NO_TLS12
  42042. func_cb_server.method = wolfTLSv1_2_server_method;
  42043. #else
  42044. func_cb_server.method = wolfTLSv1_3_server_method;
  42045. #endif
  42046. server_args.callbacks = &func_cb_server;
  42047. start_thread(test_server_nofail, &server_args, &serverThread);
  42048. wait_tcp_ready(&server_args);
  42049. test_client_nofail(&client_args, NULL);
  42050. join_thread(serverThread);
  42051. AssertIntEQ(client_args.return_code, TEST_FAIL);
  42052. AssertIntEQ(server_args.return_code, TEST_FAIL);
  42053. FreeTcpReady(&ready);
  42054. #ifdef WOLFSSL_TIRTOS
  42055. fdOpenSession(Task_self());
  42056. #endif
  42057. printf(resultFmt, passed);
  42058. #endif
  42059. }
  42060. #endif /* HAVE_PK_CALLBACKS */
  42061. #ifdef HAVE_HASHDRBG
  42062. #ifdef TEST_RESEED_INTERVAL
  42063. static int test_wc_RNG_GenerateBlock_Reseed(void)
  42064. {
  42065. int i, ret;
  42066. WC_RNG rng;
  42067. byte key[32];
  42068. ret = wc_InitRng(&rng);
  42069. if (ret == 0) {
  42070. for(i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  42071. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  42072. if (ret != 0) {
  42073. break;
  42074. }
  42075. }
  42076. }
  42077. wc_FreeRng(&rng);
  42078. return ret;
  42079. }
  42080. #endif /* TEST_RESEED_INTERVAL */
  42081. static int test_wc_RNG_GenerateBlock(void)
  42082. {
  42083. int i, ret;
  42084. WC_RNG rng;
  42085. byte key[32];
  42086. ret = wc_InitRng(&rng);
  42087. if (ret == 0) {
  42088. for(i = 0; i < 10; i++) {
  42089. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  42090. if (ret != 0) {
  42091. break;
  42092. }
  42093. }
  42094. }
  42095. wc_FreeRng(&rng);
  42096. (void)rng; /* for WC_NO_RNG case */
  42097. (void)key;
  42098. return ret;
  42099. }
  42100. #endif
  42101. /*
  42102. * Testing get_rand_digit
  42103. */
  42104. static int test_get_rand_digit (void)
  42105. {
  42106. int ret = 0;
  42107. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  42108. WC_RNG rng;
  42109. mp_digit d;
  42110. printf(testingFmt, "get_rand_digit()");
  42111. ret = wc_InitRng(&rng);
  42112. if (ret == 0) {
  42113. ret = get_rand_digit(&rng, &d);
  42114. }
  42115. if (ret == 0) {
  42116. ret = get_rand_digit(NULL, NULL);
  42117. if (ret == BAD_FUNC_ARG) {
  42118. ret = 0;
  42119. }
  42120. }
  42121. if (ret == 0) {
  42122. ret = get_rand_digit(NULL, &d);
  42123. if (ret == BAD_FUNC_ARG) {
  42124. ret = 0;
  42125. }
  42126. }
  42127. if (ret == 0) {
  42128. ret = get_rand_digit(&rng, NULL);
  42129. if (ret == BAD_FUNC_ARG) {
  42130. ret = 0;
  42131. }
  42132. }
  42133. if (ret == 0) {
  42134. ret = wc_FreeRng(&rng);
  42135. }
  42136. printf(resultFmt, ret == 0 ? passed : failed);
  42137. #endif
  42138. return ret;
  42139. }/* End test_get_rand_digit*/
  42140. /*
  42141. * Testing get_digit_count
  42142. */
  42143. static int test_get_digit_count (void)
  42144. {
  42145. int ret = 0;
  42146. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  42147. mp_int a;
  42148. printf(testingFmt, "get_digit_count()");
  42149. if (mp_init(&a) != MP_OKAY) {
  42150. ret = -1;
  42151. }
  42152. if (ret == 0) {
  42153. ret = get_digit_count(NULL);
  42154. }
  42155. if (ret == 0) {
  42156. ret = get_digit_count(&a);
  42157. }
  42158. printf(resultFmt, ret == 0 ? passed : failed);
  42159. mp_clear(&a);
  42160. #endif
  42161. return ret;
  42162. }/* End test_get_digit_count*/
  42163. /*
  42164. * Testing mp_cond_copy
  42165. */
  42166. static int test_mp_cond_copy (void)
  42167. {
  42168. int ret = 0;
  42169. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  42170. defined(WOLFSSL_PUBLIC_MP)
  42171. mp_int a;
  42172. mp_int b;
  42173. int copy = 0;
  42174. printf(testingFmt, "mp_cond_copy()");
  42175. if (mp_init(&a) != MP_OKAY) {
  42176. ret = -1;
  42177. }
  42178. if (ret == 0) {
  42179. if (mp_init(&b) != MP_OKAY) {
  42180. ret = -1;
  42181. }
  42182. }
  42183. if (ret == 0) {
  42184. ret = mp_cond_copy(NULL, copy, NULL);
  42185. if (ret == BAD_FUNC_ARG) {
  42186. ret = 0;
  42187. }
  42188. }
  42189. if (ret == 0) {
  42190. ret = mp_cond_copy(NULL, copy, &b);
  42191. if (ret == BAD_FUNC_ARG) {
  42192. ret = 0;
  42193. }
  42194. }
  42195. if (ret == 0) {
  42196. ret = mp_cond_copy(&a, copy, NULL);
  42197. if (ret == BAD_FUNC_ARG) {
  42198. ret = 0;
  42199. }
  42200. }
  42201. if (ret == 0) {
  42202. ret = mp_cond_copy(&a, copy, &b);
  42203. }
  42204. printf(resultFmt, ret == 0 ? passed : failed);
  42205. mp_clear(&a);
  42206. mp_clear(&b);
  42207. #endif
  42208. return ret;
  42209. }/* End test_mp_cond_copy*/
  42210. /*
  42211. * Testing mp_rand
  42212. */
  42213. static int test_mp_rand (void)
  42214. {
  42215. int ret = 0;
  42216. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  42217. mp_int a;
  42218. int digits = 1;
  42219. WC_RNG rng;
  42220. printf(testingFmt, "mp_rand()");
  42221. if (mp_init(&a) != MP_OKAY) {
  42222. ret = -1;
  42223. }
  42224. if (ret == 0) {
  42225. ret = wc_InitRng(&rng);
  42226. }
  42227. if (ret == 0) {
  42228. ret = mp_rand(&a, digits, NULL);
  42229. if (ret == MISSING_RNG_E) {
  42230. ret = 0;
  42231. }
  42232. }
  42233. if (ret == 0) {
  42234. ret = mp_rand(NULL, digits, &rng);
  42235. if (ret == BAD_FUNC_ARG) {
  42236. ret = 0;
  42237. }
  42238. }
  42239. if (ret == 0) {
  42240. ret = mp_rand(&a, 0, &rng);
  42241. if (ret == BAD_FUNC_ARG) {
  42242. ret = 0;
  42243. }
  42244. }
  42245. if (ret == 0) {
  42246. ret = mp_rand(&a, digits, &rng);
  42247. }
  42248. printf(resultFmt, ret == 0 ? passed : failed);
  42249. mp_clear(&a);
  42250. wc_FreeRng(&rng);
  42251. #endif
  42252. return ret;
  42253. }/* End test_mp_rand*/
  42254. /*
  42255. * Testing get_digit
  42256. */
  42257. static int test_get_digit (void)
  42258. {
  42259. int ret = 0;
  42260. #if defined(WOLFSSL_PUBLIC_MP)
  42261. mp_int a;
  42262. int n = 0;
  42263. printf(testingFmt, "get_digit()");
  42264. if (mp_init(&a) != MP_OKAY) {
  42265. ret = -1;
  42266. }
  42267. if (ret == 0) {
  42268. if (get_digit(NULL, n) != 0) { /* Should not hit this */
  42269. ret = -1;
  42270. }
  42271. }
  42272. if (ret == 0) {
  42273. if (get_digit(NULL, n) == 0) { /* Should hit this */
  42274. ret = 0;
  42275. }
  42276. }
  42277. if (ret == 0) {
  42278. if (get_digit(&a, n) != 0) { /* Should not hit this */
  42279. ret = -1;
  42280. }
  42281. }
  42282. if (ret == 0) {
  42283. if (get_digit(&a, n) == 0) { /* Should hit this */
  42284. ret = 0;
  42285. }
  42286. }
  42287. printf(resultFmt, ret == 0 ? passed : failed);
  42288. mp_clear(&a);
  42289. #endif
  42290. return ret;
  42291. }/* End test_get_digit*/
  42292. /*
  42293. * Testing wc_export_int
  42294. */
  42295. static int test_wc_export_int(void)
  42296. {
  42297. int ret = 0;
  42298. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  42299. defined(WOLFSSL_PUBLIC_MP)
  42300. mp_int mp;
  42301. byte buf[32];
  42302. word32 keySz = (word32)sizeof(buf);
  42303. word32 len = (word32)sizeof(buf);
  42304. printf(testingFmt, "wc_export_int()");
  42305. if (mp_init(&mp) != MP_OKAY) {
  42306. ret = -1;
  42307. }
  42308. if (ret == 0) {
  42309. ret = mp_set(&mp, 1234);
  42310. }
  42311. if (ret == 0) {
  42312. ret = wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  42313. if (ret == BAD_FUNC_ARG) {
  42314. ret = 0;
  42315. }
  42316. }
  42317. if (ret == 0) {
  42318. len = sizeof(buf)-1;
  42319. ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  42320. if (ret == BUFFER_E) {
  42321. ret = 0;
  42322. }
  42323. }
  42324. if (ret == 0) {
  42325. len = sizeof(buf);
  42326. ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  42327. }
  42328. if (ret == 0) {
  42329. len = 4; /* test input too small */
  42330. ret = wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR);
  42331. if (ret == BUFFER_E) {
  42332. ret = 0;
  42333. }
  42334. }
  42335. if (ret == 0) {
  42336. len = sizeof(buf);
  42337. ret = wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR);
  42338. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  42339. if (ret == 0 && len != 5) {
  42340. ret = BAD_FUNC_ARG;
  42341. }
  42342. }
  42343. printf(resultFmt, ret == 0 ? passed : failed);
  42344. mp_clear(&mp);
  42345. #endif
  42346. return ret;
  42347. }/* End test_wc_export_int*/
  42348. static int test_wc_InitRngNonce(void)
  42349. {
  42350. int ret=0;
  42351. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  42352. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  42353. WC_RNG rng;
  42354. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  42355. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  42356. word32 nonceSz = sizeof(nonce);
  42357. printf(testingFmt, "wc_InitRngNonce()");
  42358. if (ret == 0){
  42359. ret = wc_InitRngNonce(&rng, nonce, nonceSz);
  42360. }
  42361. wc_FreeRng(&rng);
  42362. printf(resultFmt, ret == 0 ? passed : failed);
  42363. #endif
  42364. return ret;
  42365. }/* End test_wc_InitRngNonce*/
  42366. /*
  42367. * Testing wc_InitRngNonce_ex
  42368. */
  42369. static int test_wc_InitRngNonce_ex(void)
  42370. {
  42371. int ret=0;
  42372. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  42373. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  42374. WC_RNG rng;
  42375. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  42376. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  42377. word32 nonceSz = sizeof(nonce);
  42378. printf(testingFmt, "wc_InitRngNonce_ex()");
  42379. if (ret == 0){
  42380. ret = wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, devId);
  42381. }
  42382. wc_FreeRng(&rng);
  42383. printf(resultFmt, ret == 0 ? passed : failed);
  42384. #endif
  42385. return ret;
  42386. }/*End test_wc_InitRngNonce_ex*/
  42387. static void test_wolfSSL_X509_CRL(void)
  42388. {
  42389. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  42390. X509_CRL *crl;
  42391. char pem[][100] = {
  42392. "./certs/crl/crl.pem",
  42393. "./certs/crl/crl2.pem",
  42394. "./certs/crl/caEccCrl.pem",
  42395. "./certs/crl/eccCliCRL.pem",
  42396. "./certs/crl/eccSrvCRL.pem",
  42397. ""
  42398. };
  42399. #ifndef NO_BIO
  42400. BIO *bio;
  42401. #endif
  42402. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  42403. char der[][100] = {
  42404. "./certs/crl/crl.der",
  42405. "./certs/crl/crl2.der",
  42406. ""};
  42407. #endif
  42408. XFILE fp;
  42409. int i;
  42410. printf(testingFmt, "test_wolfSSL_X509_CRL");
  42411. for (i = 0; pem[i][0] != '\0'; i++)
  42412. {
  42413. fp = XFOPEN(pem[i], "rb");
  42414. AssertTrue((fp != XBADFILE));
  42415. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
  42416. AssertNotNull(crl);
  42417. X509_CRL_free(crl);
  42418. XFCLOSE(fp);
  42419. fp = XFOPEN(pem[i], "rb");
  42420. AssertTrue((fp != XBADFILE));
  42421. AssertNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL, NULL));
  42422. AssertNotNull(crl);
  42423. X509_CRL_free(crl);
  42424. XFCLOSE(fp);
  42425. }
  42426. #ifndef NO_BIO
  42427. for (i = 0; pem[i][0] != '\0'; i++)
  42428. {
  42429. AssertNotNull(bio = BIO_new_file(pem[i], "rb"));
  42430. AssertNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  42431. X509_CRL_free(crl);
  42432. BIO_free(bio);
  42433. }
  42434. #endif
  42435. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  42436. for(i = 0; der[i][0] != '\0'; i++){
  42437. fp = XFOPEN(der[i], "rb");
  42438. AssertTrue((fp != XBADFILE));
  42439. AssertNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  42440. AssertNotNull(crl);
  42441. X509_CRL_free(crl);
  42442. XFCLOSE(fp);
  42443. fp = XFOPEN(der[i], "rb");
  42444. AssertTrue((fp != XBADFILE));
  42445. AssertNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  42446. AssertNotNull(crl);
  42447. X509_CRL_free(crl);
  42448. XFCLOSE(fp);
  42449. }
  42450. #endif
  42451. printf(resultFmt, passed);
  42452. #endif
  42453. return;
  42454. }
  42455. static void test_wolfSSL_X509_load_crl_file(void)
  42456. {
  42457. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  42458. !defined(NO_RSA) && !defined(NO_BIO)
  42459. int i;
  42460. char pem[][100] = {
  42461. "./certs/crl/crl.pem",
  42462. "./certs/crl/crl2.pem",
  42463. "./certs/crl/caEccCrl.pem",
  42464. "./certs/crl/eccCliCRL.pem",
  42465. "./certs/crl/eccSrvCRL.pem",
  42466. ""
  42467. };
  42468. char der[][100] = {
  42469. "./certs/crl/crl.der",
  42470. "./certs/crl/crl2.der",
  42471. ""
  42472. };
  42473. WOLFSSL_X509_STORE* store;
  42474. WOLFSSL_X509_LOOKUP* lookup;
  42475. printf(testingFmt, "wolfSSL_X509_load_crl_file");
  42476. AssertNotNull(store = wolfSSL_X509_STORE_new());
  42477. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  42478. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  42479. X509_FILETYPE_PEM), 1);
  42480. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  42481. X509_FILETYPE_PEM), 1);
  42482. if (store) {
  42483. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  42484. WOLFSSL_FILETYPE_PEM), 1);
  42485. /* since store hasn't yet known the revoked cert*/
  42486. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  42487. WOLFSSL_FILETYPE_PEM), 1);
  42488. }
  42489. for (i = 0; pem[i][0] != '\0'; i++)
  42490. {
  42491. AssertIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM), 1);
  42492. }
  42493. if (store) {
  42494. /* since store knows crl list */
  42495. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  42496. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  42497. }
  42498. /* once feeing store */
  42499. X509_STORE_free(store);
  42500. store = NULL;
  42501. AssertNotNull(store = wolfSSL_X509_STORE_new());
  42502. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  42503. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  42504. X509_FILETYPE_PEM), 1);
  42505. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  42506. X509_FILETYPE_PEM), 1);
  42507. if (store) {
  42508. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  42509. WOLFSSL_FILETYPE_PEM), 1);
  42510. /* since store hasn't yet known the revoked cert*/
  42511. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  42512. WOLFSSL_FILETYPE_PEM), 1);
  42513. }
  42514. for (i = 0; der[i][0] != '\0'; i++)
  42515. {
  42516. AssertIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1), 1);
  42517. }
  42518. if (store) {
  42519. /* since store knows crl list */
  42520. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  42521. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  42522. }
  42523. /* test for incorrect parameter */
  42524. AssertIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  42525. AssertIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  42526. AssertIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  42527. X509_STORE_free(store);
  42528. store = NULL;
  42529. printf(resultFmt, passed);
  42530. #endif
  42531. }
  42532. static void test_wolfSSL_d2i_X509_REQ(void)
  42533. {
  42534. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  42535. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA))
  42536. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der were
  42537. * generated by libest
  42538. * ./certs/csr.attr.der contains sample attributes
  42539. * ./certs/csr.ext.der contains sample extensions */
  42540. const char* csrFile = "./certs/csr.signed.der";
  42541. const char* csrPopFile = "./certs/csr.attr.der";
  42542. const char* csrExtFile = "./certs/csr.ext.der";
  42543. /* ./certs/csr.dsa.pem is generated using
  42544. * openssl req -newkey dsa:certs/dsaparams.pem \
  42545. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  42546. * -outform PEM
  42547. * with the passphrase "wolfSSL"
  42548. */
  42549. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  42550. const char* csrDsaFile = "./certs/csr.dsa.pem";
  42551. XFILE f;
  42552. #endif
  42553. BIO* bio = NULL;
  42554. X509* req = NULL;
  42555. EVP_PKEY *pub_key = NULL;
  42556. {
  42557. AssertNotNull(bio = BIO_new_file(csrFile, "rb"));
  42558. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  42559. /*
  42560. * Extract the public key from the CSR
  42561. */
  42562. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  42563. /*
  42564. * Verify the signature in the CSR
  42565. */
  42566. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  42567. X509_free(req);
  42568. BIO_free(bio);
  42569. EVP_PKEY_free(pub_key);
  42570. }
  42571. {
  42572. #ifdef OPENSSL_ALL
  42573. X509_ATTRIBUTE* attr;
  42574. ASN1_TYPE *at;
  42575. #endif
  42576. AssertNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  42577. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  42578. /*
  42579. * Extract the public key from the CSR
  42580. */
  42581. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  42582. /*
  42583. * Verify the signature in the CSR
  42584. */
  42585. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  42586. #ifdef OPENSSL_ALL
  42587. /*
  42588. * Obtain the challenge password from the CSR
  42589. */
  42590. AssertIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword, -1),
  42591. 1);
  42592. AssertNotNull(attr = X509_REQ_get_attr(req, 1));
  42593. AssertNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  42594. AssertNotNull(at->value.asn1_string);
  42595. AssertStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "2xIE+qqp/rhyTXP+");
  42596. AssertIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  42597. #endif
  42598. X509_free(req);
  42599. BIO_free(bio);
  42600. EVP_PKEY_free(pub_key);
  42601. }
  42602. {
  42603. #ifdef OPENSSL_ALL
  42604. X509_ATTRIBUTE* attr;
  42605. ASN1_TYPE *at;
  42606. STACK_OF(X509_EXTENSION) *exts = NULL;
  42607. #endif
  42608. AssertNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  42609. /* This CSR contains an Extension Request attribute so
  42610. * we test extension parsing in a CSR attribute here. */
  42611. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  42612. /*
  42613. * Extract the public key from the CSR
  42614. */
  42615. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  42616. /*
  42617. * Verify the signature in the CSR
  42618. */
  42619. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  42620. #ifdef OPENSSL_ALL
  42621. AssertNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(req));
  42622. AssertIntEQ(sk_X509_EXTENSION_num(exts), 2);
  42623. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  42624. /*
  42625. * Obtain the challenge password from the CSR
  42626. */
  42627. AssertIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword, -1),
  42628. 0);
  42629. AssertNotNull(attr = X509_REQ_get_attr(req, 0));
  42630. AssertNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  42631. AssertNotNull(at->value.asn1_string);
  42632. AssertStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  42633. AssertIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  42634. AssertIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  42635. #endif
  42636. X509_free(req);
  42637. BIO_free(bio);
  42638. EVP_PKEY_free(pub_key);
  42639. }
  42640. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  42641. {
  42642. AssertNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  42643. AssertNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  42644. /*
  42645. * Extract the public key from the CSR
  42646. */
  42647. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  42648. /*
  42649. * Verify the signature in the CSR
  42650. */
  42651. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  42652. X509_free(req);
  42653. BIO_free(bio);
  42654. /* Run the same test, but with a file pointer instead of a BIO.
  42655. * (PEM_read_X509_REQ)*/
  42656. AssertTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  42657. AssertNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  42658. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  42659. X509_free(req);
  42660. EVP_PKEY_free(pub_key);
  42661. }
  42662. #endif /* !NO_DSA && !HAVE_SELFTEST */
  42663. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  42664. }
  42665. static void test_wolfSSL_PEM_read_X509(void)
  42666. {
  42667. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  42668. !defined(NO_RSA)
  42669. X509 *x509 = NULL;
  42670. XFILE fp;
  42671. printf(testingFmt, "wolfSSL_PEM_read_X509");
  42672. fp = XFOPEN(svrCertFile, "rb");
  42673. AssertTrue((fp != XBADFILE));
  42674. AssertNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  42675. X509_free(x509);
  42676. XFCLOSE(fp);
  42677. printf(resultFmt, passed);
  42678. #endif
  42679. }
  42680. static void test_wolfSSL_PEM_read(void)
  42681. {
  42682. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  42683. const char* filename = "./certs/server-keyEnc.pem";
  42684. XFILE fp;
  42685. char* name = NULL;
  42686. char* header = NULL;
  42687. byte* data = NULL;
  42688. long len;
  42689. EVP_CIPHER_INFO cipher;
  42690. WOLFSSL_BIO* bio;
  42691. byte* fileData;
  42692. size_t fileDataSz;
  42693. byte* out;
  42694. printf(testingFmt, "wolfSSL_PEM_read");
  42695. fp = XFOPEN(filename, "rb");
  42696. AssertTrue((fp != XBADFILE));
  42697. /* Fail cases. */
  42698. AssertIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  42699. AssertIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  42700. AssertIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  42701. AssertIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  42702. AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  42703. AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  42704. AssertIntGT(XSTRLEN(header), 0);
  42705. AssertIntGT(len, 0);
  42706. AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  42707. AssertIntGT((fileDataSz = XFTELL(fp)), 0);
  42708. AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  42709. AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  42710. DYNAMIC_TYPE_TMP_BUFFER));
  42711. AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  42712. XFCLOSE(fp);
  42713. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  42714. /* Fail cases. */
  42715. AssertIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  42716. AssertIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  42717. AssertIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  42718. AssertIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  42719. AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  42720. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  42721. AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  42722. /* Fail cases. */
  42723. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  42724. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  42725. AssertIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  42726. #ifndef NO_DES3
  42727. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  42728. #endif
  42729. /* Fail cases. */
  42730. AssertIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  42731. (void*)"yassl123"), WOLFSSL_FAILURE);
  42732. AssertIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  42733. (void*)"yassl123"), WOLFSSL_FAILURE);
  42734. AssertIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  42735. (void*)"yassl123"), WOLFSSL_FAILURE);
  42736. #if !defined(NO_DES3) && !defined(NO_MD5)
  42737. AssertIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  42738. (void*)"yassl123"), WOLFSSL_SUCCESS);
  42739. #endif
  42740. BIO_free(bio);
  42741. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42742. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42743. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42744. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42745. name = NULL;
  42746. header = NULL;
  42747. data = NULL;
  42748. fp = XFOPEN(svrKeyFile, "rb");
  42749. AssertTrue((fp != XBADFILE));
  42750. AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  42751. AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  42752. AssertIntEQ(XSTRLEN(header), 0);
  42753. AssertIntGT(len, 0);
  42754. AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  42755. AssertIntGT((fileDataSz = XFTELL(fp)), 0);
  42756. AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  42757. AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  42758. DYNAMIC_TYPE_TMP_BUFFER));
  42759. AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  42760. XFCLOSE(fp);
  42761. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  42762. AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  42763. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  42764. AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  42765. BIO_free(bio);
  42766. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42767. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42768. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42769. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42770. printf(resultFmt, passed);
  42771. #endif
  42772. }
  42773. static void test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  42774. {
  42775. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  42776. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  42777. const byte iv[12] = { 0 };
  42778. const byte key[16] = { 0 };
  42779. const byte cleartext[16] = { 0 };
  42780. const byte aad[] = {
  42781. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  42782. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  42783. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  42784. 0x4f
  42785. };
  42786. byte out1Part[16];
  42787. byte outTag1Part[16];
  42788. byte out2Part[16];
  42789. byte outTag2Part[16];
  42790. byte decryptBuf[16];
  42791. int len;
  42792. int tlen;
  42793. EVP_CIPHER_CTX* ctx = NULL;
  42794. printf(testingFmt, "wolfssl_EVP_aes_gcm_AAD_2_parts");
  42795. /* ENCRYPT */
  42796. /* Send AAD and data in 1 part */
  42797. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  42798. tlen = 0;
  42799. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  42800. 1);
  42801. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  42802. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  42803. AssertIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  42804. sizeof(cleartext)), 1);
  42805. tlen += len;
  42806. AssertIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  42807. tlen += len;
  42808. AssertIntEQ(tlen, sizeof(cleartext));
  42809. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  42810. outTag1Part), 1);
  42811. EVP_CIPHER_CTX_free(ctx);
  42812. /* DECRYPT */
  42813. /* Send AAD and data in 1 part */
  42814. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  42815. tlen = 0;
  42816. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  42817. 1);
  42818. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  42819. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  42820. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  42821. sizeof(cleartext)), 1);
  42822. tlen += len;
  42823. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  42824. outTag1Part), 1);
  42825. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  42826. tlen += len;
  42827. AssertIntEQ(tlen, sizeof(cleartext));
  42828. EVP_CIPHER_CTX_free(ctx);
  42829. AssertIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  42830. /* ENCRYPT */
  42831. /* Send AAD and data in 2 parts */
  42832. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  42833. tlen = 0;
  42834. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  42835. 1);
  42836. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  42837. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  42838. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  42839. 1);
  42840. AssertIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  42841. tlen += len;
  42842. AssertIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  42843. sizeof(cleartext) - 1), 1);
  42844. tlen += len;
  42845. AssertIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  42846. tlen += len;
  42847. AssertIntEQ(tlen, sizeof(cleartext));
  42848. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  42849. outTag2Part), 1);
  42850. AssertIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  42851. AssertIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  42852. EVP_CIPHER_CTX_free(ctx);
  42853. /* DECRYPT */
  42854. /* Send AAD and data in 2 parts */
  42855. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  42856. tlen = 0;
  42857. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  42858. 1);
  42859. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  42860. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  42861. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  42862. 1);
  42863. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  42864. tlen += len;
  42865. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  42866. sizeof(cleartext) - 1), 1);
  42867. tlen += len;
  42868. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  42869. outTag1Part), 1);
  42870. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  42871. tlen += len;
  42872. AssertIntEQ(tlen, sizeof(cleartext));
  42873. AssertIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  42874. /* Test AAD re-use */
  42875. EVP_CIPHER_CTX_free(ctx);
  42876. printf(resultFmt, passed);
  42877. #endif
  42878. }
  42879. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  42880. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  42881. static void test_wolfssl_EVP_aes_gcm_zeroLen(void)
  42882. {
  42883. /* Zero length plain text */
  42884. byte key[] = {
  42885. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  42886. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  42887. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  42888. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  42889. }; /* align */
  42890. byte iv[] = {
  42891. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  42892. }; /* align */
  42893. byte plaintxt[1];
  42894. int ivSz = 12;
  42895. int plaintxtSz = 0;
  42896. unsigned char tag[16];
  42897. unsigned char tag_kat[] =
  42898. {0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  42899. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b};
  42900. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  42901. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  42902. int ciphertxtSz = 0;
  42903. int decryptedtxtSz = 0;
  42904. int len = 0;
  42905. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  42906. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  42907. AssertIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  42908. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  42909. AssertIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  42910. plaintxtSz));
  42911. AssertIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  42912. ciphertxtSz += len;
  42913. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  42914. AssertIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  42915. AssertIntEQ(0, ciphertxtSz);
  42916. AssertIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  42917. EVP_CIPHER_CTX_init(de);
  42918. AssertIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  42919. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  42920. AssertIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  42921. decryptedtxtSz = len;
  42922. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  42923. AssertIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  42924. decryptedtxtSz += len;
  42925. AssertIntEQ(0, decryptedtxtSz);
  42926. EVP_CIPHER_CTX_free(en);
  42927. EVP_CIPHER_CTX_free(de);
  42928. }
  42929. #endif
  42930. static void test_wolfssl_EVP_aes_gcm(void)
  42931. {
  42932. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  42933. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  42934. /* A 256 bit key, AES_128 will use the first 128 bit*/
  42935. byte *key = (byte*)"01234567890123456789012345678901";
  42936. /* A 128 bit IV */
  42937. byte *iv = (byte*)"0123456789012345";
  42938. int ivSz = AES_BLOCK_SIZE;
  42939. /* Message to be encrypted */
  42940. byte *plaintxt = (byte*)"for things to change you have to change";
  42941. /* Additional non-confidential data */
  42942. byte *aad = (byte*)"Don't spend major time on minor things.";
  42943. unsigned char tag[AES_BLOCK_SIZE] = {0};
  42944. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  42945. int aadSz = (int)XSTRLEN((char*)aad);
  42946. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  42947. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  42948. int ciphertxtSz = 0;
  42949. int decryptedtxtSz = 0;
  42950. int len = 0;
  42951. int i = 0;
  42952. EVP_CIPHER_CTX en[2];
  42953. EVP_CIPHER_CTX de[2];
  42954. printf(testingFmt, "wolfssl_EVP_aes_gcm");
  42955. for (i = 0; i < 2; i++) {
  42956. EVP_CIPHER_CTX_init(&en[i]);
  42957. if (i == 0) {
  42958. /* Default uses 96-bits IV length */
  42959. #ifdef WOLFSSL_AES_128
  42960. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, key, iv));
  42961. #elif defined(WOLFSSL_AES_192)
  42962. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, key, iv));
  42963. #elif defined(WOLFSSL_AES_256)
  42964. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, key, iv));
  42965. #endif
  42966. }
  42967. else {
  42968. #ifdef WOLFSSL_AES_128
  42969. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
  42970. #elif defined(WOLFSSL_AES_192)
  42971. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
  42972. #elif defined(WOLFSSL_AES_256)
  42973. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
  42974. #endif
  42975. /* non-default must to set the IV length first */
  42976. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  42977. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  42978. }
  42979. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  42980. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  42981. ciphertxtSz = len;
  42982. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  42983. ciphertxtSz += len;
  42984. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, AES_BLOCK_SIZE, tag));
  42985. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  42986. EVP_CIPHER_CTX_init(&de[i]);
  42987. if (i == 0) {
  42988. /* Default uses 96-bits IV length */
  42989. #ifdef WOLFSSL_AES_128
  42990. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, key, iv));
  42991. #elif defined(WOLFSSL_AES_192)
  42992. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, key, iv));
  42993. #elif defined(WOLFSSL_AES_256)
  42994. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, key, iv));
  42995. #endif
  42996. }
  42997. else {
  42998. #ifdef WOLFSSL_AES_128
  42999. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
  43000. #elif defined(WOLFSSL_AES_192)
  43001. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
  43002. #elif defined(WOLFSSL_AES_256)
  43003. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
  43004. #endif
  43005. /* non-default must to set the IV length first */
  43006. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  43007. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  43008. }
  43009. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  43010. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  43011. decryptedtxtSz = len;
  43012. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
  43013. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  43014. decryptedtxtSz += len;
  43015. AssertIntEQ(ciphertxtSz, decryptedtxtSz);
  43016. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  43017. /* modify tag*/
  43018. tag[AES_BLOCK_SIZE-1]+=0xBB;
  43019. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  43020. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
  43021. /* fail due to wrong tag */
  43022. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  43023. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  43024. AssertIntEQ(0, len);
  43025. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  43026. }
  43027. test_wolfssl_EVP_aes_gcm_zeroLen();
  43028. printf(resultFmt, passed);
  43029. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  43030. }
  43031. static void test_wolfssl_EVP_chacha20_poly1305(void)
  43032. {
  43033. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  43034. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  43035. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  43036. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  43037. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  43038. byte cipherText[sizeof(plainText)];
  43039. byte decryptedText[sizeof(plainText)];
  43040. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  43041. EVP_CIPHER_CTX* ctx;
  43042. int outSz;
  43043. printf(testingFmt, "test_wolfssl_EVP_chacha20_poly1305");
  43044. /* Encrypt. */
  43045. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  43046. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  43047. NULL), WOLFSSL_SUCCESS);
  43048. /* Invalid IV length. */
  43049. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  43050. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  43051. /* Valid IV length. */
  43052. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  43053. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  43054. /* Invalid tag length. */
  43055. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  43056. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  43057. /* Valid tag length. */
  43058. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  43059. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  43060. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  43061. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  43062. WOLFSSL_SUCCESS);
  43063. AssertIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  43064. sizeof(plainText)), WOLFSSL_SUCCESS);
  43065. AssertIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  43066. /* Invalid tag length. */
  43067. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  43068. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  43069. /* Valid tag length. */
  43070. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  43071. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  43072. EVP_CIPHER_CTX_free(ctx);
  43073. /* Decrypt. */
  43074. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  43075. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  43076. NULL), WOLFSSL_SUCCESS);
  43077. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  43078. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  43079. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  43080. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  43081. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  43082. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  43083. WOLFSSL_SUCCESS);
  43084. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  43085. sizeof(cipherText)), WOLFSSL_SUCCESS);
  43086. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  43087. WOLFSSL_SUCCESS);
  43088. EVP_CIPHER_CTX_free(ctx);
  43089. printf(resultFmt, passed);
  43090. #endif
  43091. }
  43092. static void test_wolfSSL_EVP_PKEY_hkdf(void)
  43093. {
  43094. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  43095. EVP_PKEY_CTX* ctx;
  43096. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  43097. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  43098. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  43099. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  43100. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  43101. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  43102. byte outKey[34];
  43103. size_t outKeySz = sizeof(outKey);
  43104. /* These expected outputs were gathered by running the same test below using
  43105. * OpenSSL. */
  43106. const byte extractAndExpand[] = {
  43107. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  43108. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  43109. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  43110. };
  43111. const byte extractOnly[] = {
  43112. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  43113. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  43114. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  43115. };
  43116. const byte expandOnly[] = {
  43117. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  43118. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  43119. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  43120. };
  43121. const byte extractAndExpandAddInfo[] = {
  43122. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  43123. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  43124. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  43125. };
  43126. printf(testingFmt, "test_wolfSSL_EVP_PKEY_hkdf");
  43127. AssertNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  43128. AssertIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  43129. /* NULL ctx. */
  43130. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  43131. /* NULL md. */
  43132. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  43133. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  43134. /* NULL ctx. */
  43135. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  43136. WOLFSSL_FAILURE);
  43137. /* NULL salt is ok. */
  43138. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  43139. WOLFSSL_SUCCESS);
  43140. /* Salt length <= 0. */
  43141. /* Length 0 salt is ok. */
  43142. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  43143. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  43144. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  43145. WOLFSSL_SUCCESS);
  43146. /* NULL ctx. */
  43147. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  43148. WOLFSSL_FAILURE);
  43149. /* NULL key. */
  43150. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  43151. WOLFSSL_FAILURE);
  43152. /* Key length <= 0 */
  43153. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  43154. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  43155. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  43156. WOLFSSL_SUCCESS);
  43157. /* NULL ctx. */
  43158. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  43159. WOLFSSL_FAILURE);
  43160. /* NULL info is ok. */
  43161. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  43162. WOLFSSL_SUCCESS);
  43163. /* Info length <= 0 */
  43164. /* Length 0 info is ok. */
  43165. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  43166. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  43167. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  43168. WOLFSSL_SUCCESS);
  43169. /* NULL ctx. */
  43170. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  43171. WOLFSSL_FAILURE);
  43172. /* Extract and expand (default). */
  43173. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  43174. AssertIntEQ(outKeySz, sizeof(extractAndExpand));
  43175. AssertIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  43176. /* Extract only. */
  43177. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  43178. WOLFSSL_SUCCESS);
  43179. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  43180. AssertIntEQ(outKeySz, sizeof(extractOnly));
  43181. AssertIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  43182. outKeySz = sizeof(outKey);
  43183. /* Expand only. */
  43184. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  43185. WOLFSSL_SUCCESS);
  43186. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  43187. AssertIntEQ(outKeySz, sizeof(expandOnly));
  43188. AssertIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  43189. outKeySz = sizeof(outKey);
  43190. /* Extract and expand with appended additional info. */
  43191. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  43192. WOLFSSL_SUCCESS);
  43193. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  43194. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  43195. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  43196. AssertIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  43197. AssertIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  43198. EVP_PKEY_CTX_free(ctx);
  43199. printf(resultFmt, passed);
  43200. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  43201. }
  43202. #ifndef NO_BIO
  43203. static void test_wolfSSL_PEM_X509_INFO_read_bio(void)
  43204. {
  43205. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  43206. BIO* bio;
  43207. X509_INFO* info;
  43208. STACK_OF(X509_INFO)* sk;
  43209. char* subject;
  43210. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  43211. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  43212. printf(testingFmt, "wolfSSL_PEM_X509_INFO_read_bio");
  43213. AssertNotNull(bio = BIO_new(BIO_s_file()));
  43214. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  43215. AssertNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  43216. AssertIntEQ(sk_X509_INFO_num(sk), 2);
  43217. /* using dereference to maintain testing for Apache port*/
  43218. AssertNotNull(info = sk_X509_INFO_pop(sk));
  43219. AssertNotNull(subject =
  43220. X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
  43221. AssertIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  43222. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  43223. X509_INFO_free(info);
  43224. AssertNotNull(info = sk_X509_INFO_pop(sk));
  43225. AssertNotNull(subject =
  43226. X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
  43227. AssertIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  43228. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  43229. X509_INFO_free(info);
  43230. AssertNull(info = sk_X509_INFO_pop(sk));
  43231. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  43232. BIO_free(bio);
  43233. printf(resultFmt, passed);
  43234. #endif
  43235. }
  43236. #endif /* !NO_BIO */
  43237. static void test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  43238. {
  43239. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  43240. X509 *x509;
  43241. X509_NAME* name;
  43242. int idx = 0;
  43243. X509_NAME_ENTRY *ne;
  43244. ASN1_OBJECT *object = NULL;
  43245. printf(testingFmt, "wolfSSL_X509_NAME_ENTRY_get_object");
  43246. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  43247. AssertNotNull(x509);
  43248. name = X509_get_subject_name(x509);
  43249. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  43250. AssertIntGE(idx, 0);
  43251. ne = X509_NAME_get_entry(name, idx);
  43252. AssertNotNull(ne);
  43253. AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
  43254. X509_free(x509);
  43255. printf(resultFmt, passed);
  43256. #endif
  43257. }
  43258. static void test_wolfSSL_ASN1_INTEGER_get_set(void)
  43259. {
  43260. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  43261. ASN1_INTEGER *a;
  43262. long val;
  43263. int ret;
  43264. printf(testingFmt, "test_wolfSSL_ASN1_INTEGER_get_set");
  43265. a = ASN1_INTEGER_new();
  43266. val = 0;
  43267. ret = ASN1_INTEGER_set(NULL, val);
  43268. AssertIntEQ(ret, 0);
  43269. ASN1_INTEGER_free(a);
  43270. /* 0 */
  43271. a = ASN1_INTEGER_new();
  43272. val = 0;
  43273. ret = ASN1_INTEGER_set(a, val);
  43274. AssertIntEQ(ret, 1);
  43275. AssertIntEQ(ASN1_INTEGER_get(a), val);
  43276. ASN1_INTEGER_free(a);
  43277. /* 40 */
  43278. a = ASN1_INTEGER_new();
  43279. val = 40;
  43280. ret = ASN1_INTEGER_set(a, val);
  43281. AssertIntEQ(ret, 1);
  43282. AssertIntEQ(ASN1_INTEGER_get(a), val);
  43283. ASN1_INTEGER_free(a);
  43284. /* -40 */
  43285. a = ASN1_INTEGER_new();
  43286. val = -40;
  43287. ret = ASN1_INTEGER_set(a, val);
  43288. AssertIntEQ(ret, 1);
  43289. AssertIntEQ(ASN1_INTEGER_get(a), val);
  43290. ASN1_INTEGER_free(a);
  43291. /* 128 */
  43292. a = ASN1_INTEGER_new();
  43293. val = 128;
  43294. ret = ASN1_INTEGER_set(a, val);
  43295. AssertIntEQ(ret, 1);
  43296. AssertIntEQ(ASN1_INTEGER_get(a), val);
  43297. ASN1_INTEGER_free(a);
  43298. /* -128 */
  43299. a = ASN1_INTEGER_new();
  43300. val = -128;
  43301. ret = ASN1_INTEGER_set(a, val);
  43302. AssertIntEQ(ret, 1);
  43303. AssertIntEQ(ASN1_INTEGER_get(a), val);
  43304. ASN1_INTEGER_free(a);
  43305. /* 200 */
  43306. a = ASN1_INTEGER_new();
  43307. val = 200;
  43308. ret = ASN1_INTEGER_set(a, val);
  43309. AssertIntEQ(ret, 1);
  43310. AssertIntEQ(ASN1_INTEGER_get(a), val);
  43311. ASN1_INTEGER_free(a);
  43312. /* int max (2147483647) */
  43313. a = ASN1_INTEGER_new();
  43314. val = 2147483647;
  43315. ret = ASN1_INTEGER_set(a, val);
  43316. AssertIntEQ(ret, 1);
  43317. AssertIntEQ(ASN1_INTEGER_get(a), val);
  43318. ASN1_INTEGER_free(a);
  43319. /* int min (-2147483648) */
  43320. a = ASN1_INTEGER_new();
  43321. val = -2147483647 - 1;
  43322. ret = ASN1_INTEGER_set(a, val);
  43323. AssertIntEQ(ret, 1);
  43324. AssertIntEQ(ASN1_INTEGER_get(a), val);
  43325. ASN1_INTEGER_free(a);
  43326. printf(resultFmt, passed);
  43327. #endif
  43328. }
  43329. #if defined(OPENSSL_EXTRA)
  43330. typedef struct ASN1IntTestVector {
  43331. const byte* der;
  43332. const size_t derSz;
  43333. const long value;
  43334. } ASN1IntTestVector;
  43335. #endif
  43336. static void test_wolfSSL_d2i_ASN1_INTEGER(void)
  43337. {
  43338. #if defined(OPENSSL_EXTRA)
  43339. size_t i;
  43340. WOLFSSL_ASN1_INTEGER* a = NULL;
  43341. WOLFSSL_ASN1_INTEGER* b = NULL;
  43342. WOLFSSL_ASN1_INTEGER* c = NULL;
  43343. const byte* p = NULL;
  43344. byte* reEncoded = NULL;
  43345. int reEncodedSz;
  43346. static const byte zeroDer[] = {
  43347. 0x02, 0x01, 0x00
  43348. };
  43349. static const byte oneDer[] = {
  43350. 0x02, 0x01, 0x01
  43351. };
  43352. static const byte negativeDer[] = {
  43353. 0x02, 0x03, 0xC1, 0x16, 0x0D
  43354. };
  43355. static const byte positiveDer[] = {
  43356. 0x02, 0x03, 0x01, 0x00, 0x01
  43357. };
  43358. static const byte primeDer[] = {
  43359. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  43360. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  43361. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  43362. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  43363. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  43364. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  43365. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  43366. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  43367. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  43368. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  43369. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  43370. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  43371. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  43372. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  43373. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  43374. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  43375. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  43376. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  43377. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  43378. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  43379. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  43380. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  43381. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  43382. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  43383. };
  43384. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  43385. static const ASN1IntTestVector testVectors[] = {
  43386. {zeroDer, sizeof(zeroDer), 0},
  43387. {oneDer, sizeof(oneDer), 1},
  43388. {negativeDer, sizeof(negativeDer), -4123123},
  43389. {positiveDer, sizeof(positiveDer), 65537},
  43390. {primeDer, sizeof(primeDer), 0}
  43391. };
  43392. static const size_t NUM_TEST_VECTORS = sizeof(testVectors)/sizeof(testVectors[0]);
  43393. printf(testingFmt, "test_wolfSSL_d2i_ASN1_INTEGER");
  43394. /* Check d2i error conditions */
  43395. /* NULL pointer to input. */
  43396. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  43397. AssertNull(b);
  43398. /* NULL input. */
  43399. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  43400. AssertNull(b);
  43401. /* 0 length. */
  43402. p = testVectors[0].der;
  43403. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  43404. AssertNull(b);
  43405. /* Negative length. */
  43406. p = testVectors[0].der;
  43407. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  43408. AssertNull(b);
  43409. /* Garbage DER input. */
  43410. p = garbageDer;
  43411. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  43412. AssertNull(b);
  43413. {
  43414. /* Check i2d error conditions */
  43415. /* NULL input. */
  43416. byte* p2 = NULL;
  43417. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  43418. /* 0 length input data buffer (a->length == 0). */
  43419. AssertNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  43420. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  43421. a->data = NULL;
  43422. /* NULL input data buffer. */
  43423. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  43424. /* Reset a->data. */
  43425. a->data = a->intData;
  43426. /* Set a to valid value. */
  43427. AssertIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  43428. /* NULL output buffer. */
  43429. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  43430. wolfSSL_ASN1_INTEGER_free(a);
  43431. }
  43432. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  43433. p = testVectors[i].der;
  43434. a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, testVectors[i].derSz);
  43435. AssertIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  43436. if (testVectors[i].derSz <= sizeof(long)) {
  43437. c = wolfSSL_ASN1_INTEGER_new();
  43438. wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value);
  43439. AssertIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  43440. wolfSSL_ASN1_INTEGER_free(c);
  43441. }
  43442. /* Convert to DER without a pre-allocated output buffer. */
  43443. AssertIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  43444. AssertIntEQ(reEncodedSz, testVectors[i].derSz);
  43445. AssertIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  43446. /* Convert to DER with a pre-allocated output buffer. In this case, the
  43447. * output buffer pointer should be incremented just past the end of the
  43448. * encoded data. */
  43449. p = reEncoded;
  43450. AssertIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  43451. AssertIntEQ(reEncodedSz, testVectors[i].derSz);
  43452. AssertPtrEq(p, reEncoded - reEncodedSz);
  43453. AssertIntEQ(XMEMCMP(p, testVectors[i].der, reEncodedSz), 0);
  43454. XFREE(reEncoded - reEncodedSz, NULL, DYNAMIC_TYPE_ASN1);
  43455. reEncoded = NULL;
  43456. wolfSSL_ASN1_INTEGER_free(a);
  43457. }
  43458. printf(resultFmt, passed);
  43459. #endif /* OPENSSL_EXTRA */
  43460. }
  43461. static void test_wolfSSL_X509_STORE_get1_certs(void)
  43462. {
  43463. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  43464. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  43465. X509_STORE_CTX *storeCtx;
  43466. X509_STORE *store;
  43467. X509 *caX509;
  43468. X509 *svrX509;
  43469. X509_NAME *subject;
  43470. WOLF_STACK_OF(WOLFSSL_X509) *certs;
  43471. printf(testingFmt, "wolfSSL_X509_STORE_get1_certs()");
  43472. AssertNotNull(caX509 =
  43473. X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
  43474. AssertNotNull((svrX509 =
  43475. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  43476. AssertNotNull(storeCtx = X509_STORE_CTX_new());
  43477. AssertNotNull(store = X509_STORE_new());
  43478. AssertNotNull(subject = X509_get_subject_name(caX509));
  43479. /* Errors */
  43480. AssertNull(X509_STORE_get1_certs(storeCtx, subject));
  43481. AssertNull(X509_STORE_get1_certs(NULL, subject));
  43482. AssertNull(X509_STORE_get1_certs(storeCtx, NULL));
  43483. AssertIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  43484. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL), SSL_SUCCESS);
  43485. /* Should find the cert */
  43486. AssertNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  43487. AssertIntEQ(1, wolfSSL_sk_X509_num(certs));
  43488. sk_X509_pop_free(certs, NULL);
  43489. /* Should not find the cert */
  43490. AssertNotNull(subject = X509_get_subject_name(svrX509));
  43491. AssertNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  43492. AssertIntEQ(0, wolfSSL_sk_X509_num(certs));
  43493. sk_X509_pop_free(certs, NULL);
  43494. X509_STORE_free(store);
  43495. X509_STORE_CTX_free(storeCtx);
  43496. X509_free(svrX509);
  43497. X509_free(caX509);
  43498. printf(resultFmt, passed);
  43499. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  43500. }
  43501. /* Testing code used in dpp.c in hostap */
  43502. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  43503. typedef struct {
  43504. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  43505. * as an OID identifying the curve */
  43506. X509_ALGOR *alg;
  43507. /* Compressed format public key per ANSI X9.63 */
  43508. ASN1_BIT_STRING *pub_key;
  43509. } DPP_BOOTSTRAPPING_KEY;
  43510. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  43511. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  43512. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  43513. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  43514. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  43515. #endif
  43516. static void test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  43517. {
  43518. /* Testing code used in dpp.c in hostap */
  43519. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  43520. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  43521. EC_KEY *eckey;
  43522. EVP_PKEY *key;
  43523. size_t len;
  43524. unsigned char *der = NULL;
  43525. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  43526. const unsigned char *in = ecc_clikey_der_256;
  43527. const EC_GROUP *group;
  43528. const EC_POINT *point;
  43529. int nid;
  43530. AssertNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  43531. AssertNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  43532. (long)sizeof_ecc_clikey_der_256));
  43533. AssertNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  43534. AssertNotNull(group = EC_KEY_get0_group(eckey));
  43535. AssertNotNull(point = EC_KEY_get0_public_key(eckey));
  43536. nid = EC_GROUP_get_curve_name(group);
  43537. AssertIntEQ(X509_ALGOR_set0(bootstrap->alg, OBJ_nid2obj(EVP_PKEY_EC),
  43538. V_ASN1_OBJECT, OBJ_nid2obj(nid)), 1);
  43539. #ifdef HAVE_COMP_KEY
  43540. AssertIntGT((len = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  43541. NULL, 0, NULL)), 0);
  43542. #else
  43543. AssertIntGT((len = EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  43544. NULL, 0, NULL)), 0);
  43545. #endif
  43546. AssertNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  43547. #ifdef HAVE_COMP_KEY
  43548. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  43549. der, len, NULL), len);
  43550. #else
  43551. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  43552. der, len, NULL), len);
  43553. #endif
  43554. bootstrap->pub_key->data = der;
  43555. bootstrap->pub_key->length = (int)len;
  43556. /* Not actually used */
  43557. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  43558. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  43559. der = NULL;
  43560. AssertIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  43561. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  43562. EVP_PKEY_free(key);
  43563. EC_KEY_free(eckey);
  43564. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  43565. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  43566. #endif /* WOLFSSL_WPAS && HAVE_ECC && USE_CERT_BUFFERS_256 */
  43567. }
  43568. static void test_wolfSSL_i2c_ASN1_INTEGER(void)
  43569. {
  43570. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  43571. ASN1_INTEGER *a;
  43572. unsigned char *pp,*tpp;
  43573. int ret;
  43574. printf(testingFmt, "wolfSSL_i2c_ASN1_INTEGER");
  43575. a = wolfSSL_ASN1_INTEGER_new();
  43576. /* 40 */
  43577. a->intData[0] = ASN_INTEGER;
  43578. a->intData[1] = 1;
  43579. a->intData[2] = 40;
  43580. ret = i2c_ASN1_INTEGER(a, NULL);
  43581. AssertIntEQ(ret, 1);
  43582. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  43583. DYNAMIC_TYPE_TMP_BUFFER));
  43584. tpp = pp;
  43585. XMEMSET(pp, 0, ret + 1);
  43586. i2c_ASN1_INTEGER(a, &pp);
  43587. pp--;
  43588. AssertIntEQ(*pp, 40);
  43589. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43590. /* 128 */
  43591. a->intData[0] = ASN_INTEGER;
  43592. a->intData[1] = 1;
  43593. a->intData[2] = 128;
  43594. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  43595. AssertIntEQ(ret, 2);
  43596. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  43597. DYNAMIC_TYPE_TMP_BUFFER));
  43598. tpp = pp;
  43599. XMEMSET(pp, 0, ret + 1);
  43600. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  43601. pp--;
  43602. AssertIntEQ(*(pp--), 128);
  43603. AssertIntEQ(*pp, 0);
  43604. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43605. /* -40 */
  43606. a->intData[0] = ASN_INTEGER;
  43607. a->intData[1] = 1;
  43608. a->intData[2] = 40;
  43609. a->negative = 1;
  43610. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  43611. AssertIntEQ(ret, 1);
  43612. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  43613. DYNAMIC_TYPE_TMP_BUFFER));
  43614. tpp = pp;
  43615. XMEMSET(pp, 0, ret + 1);
  43616. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  43617. pp--;
  43618. AssertIntEQ(*pp, 216);
  43619. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43620. /* -128 */
  43621. a->intData[0] = ASN_INTEGER;
  43622. a->intData[1] = 1;
  43623. a->intData[2] = 128;
  43624. a->negative = 1;
  43625. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  43626. AssertIntEQ(ret, 1);
  43627. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  43628. DYNAMIC_TYPE_TMP_BUFFER));
  43629. tpp = pp;
  43630. XMEMSET(pp, 0, ret + 1);
  43631. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  43632. pp--;
  43633. AssertIntEQ(*pp, 128);
  43634. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43635. /* -200 */
  43636. a->intData[0] = ASN_INTEGER;
  43637. a->intData[1] = 1;
  43638. a->intData[2] = 200;
  43639. a->negative = 1;
  43640. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  43641. AssertIntEQ(ret, 2);
  43642. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  43643. DYNAMIC_TYPE_TMP_BUFFER));
  43644. tpp = pp;
  43645. XMEMSET(pp, 0, ret + 1);
  43646. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  43647. pp--;
  43648. AssertIntEQ(*(pp--), 56);
  43649. AssertIntEQ(*pp, 255);
  43650. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43651. wolfSSL_ASN1_INTEGER_free(a);
  43652. printf(resultFmt, passed);
  43653. #endif /* OPENSSL_EXTRA && !NO_ASN */
  43654. }
  43655. #ifndef NO_INLINE
  43656. #define WOLFSSL_MISC_INCLUDED
  43657. #include <wolfcrypt/src/misc.c>
  43658. #else
  43659. #include <wolfssl/wolfcrypt/misc.h>
  43660. #endif
  43661. static int test_ForceZero(void)
  43662. {
  43663. unsigned char data[32];
  43664. unsigned int i, j, len;
  43665. /* Test case with 0 length */
  43666. ForceZero(data, 0);
  43667. /* Test ForceZero */
  43668. for (i = 0; i < sizeof(data); i++) {
  43669. for (len = 1; len < sizeof(data) - i; len++) {
  43670. for (j = 0; j < sizeof(data); j++)
  43671. data[j] = j + 1;
  43672. ForceZero(data + i, len);
  43673. for (j = 0; j < sizeof(data); j++) {
  43674. if (j < i || j >= i + len) {
  43675. if (data[j] == 0x00)
  43676. return -10200;
  43677. }
  43678. else if (data[j] != 0x00)
  43679. return -10201;
  43680. }
  43681. }
  43682. }
  43683. return 0;
  43684. }
  43685. #ifndef NO_BIO
  43686. static void test_wolfSSL_X509_print(void)
  43687. {
  43688. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  43689. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(XSNPRINTF)
  43690. X509 *x509;
  43691. BIO *bio;
  43692. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  43693. const X509_ALGOR *cert_sig_alg;
  43694. #endif
  43695. printf(testingFmt, "wolfSSL_X509_print");
  43696. x509 = X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  43697. AssertNotNull(x509);
  43698. /* print to memory */
  43699. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  43700. AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  43701. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  43702. /* Will print IP address subject alt name. */
  43703. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3329);
  43704. #else
  43705. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3307);
  43706. #endif
  43707. BIO_free(bio);
  43708. AssertNotNull(bio = BIO_new_fd(STDOUT_FILENO, BIO_NOCLOSE));
  43709. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  43710. /* Print signature */
  43711. AssertNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  43712. AssertIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  43713. #endif
  43714. /* print to stdout */
  43715. #if !defined(NO_WOLFSSL_DIR)
  43716. AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  43717. #endif
  43718. /* print again */
  43719. AssertIntEQ(X509_print_fp(stdout, x509), SSL_SUCCESS);
  43720. X509_free(x509);
  43721. BIO_free(bio);
  43722. printf(resultFmt, passed);
  43723. #endif
  43724. }
  43725. static void test_wolfSSL_BIO_get_len(void)
  43726. {
  43727. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  43728. BIO *bio = NULL;
  43729. const char txt[] = "Some example text to push to the BIO.";
  43730. printf(testingFmt, "wolfSSL_BIO_get_len");
  43731. AssertIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  43732. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  43733. AssertIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  43734. AssertIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  43735. BIO_free(bio);
  43736. AssertNotNull(bio = BIO_new_fd(STDOUT_FILENO, BIO_NOCLOSE));
  43737. AssertIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  43738. BIO_free(bio);
  43739. printf(resultFmt, passed);
  43740. #endif
  43741. }
  43742. static void test_wolfSSL_ASN1_STRING_print(void){
  43743. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS)
  43744. ASN1_STRING* asnStr = NULL;
  43745. const char HELLO_DATA[]= \
  43746. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  43747. #define MAX_UNPRINTABLE_CHAR 32
  43748. #define MAX_BUF 255
  43749. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  43750. unsigned char expected[sizeof(unprintableData)+1];
  43751. unsigned char rbuf[MAX_BUF];
  43752. BIO *bio;
  43753. int p_len, i;
  43754. printf(testingFmt, "wolfSSL_ASN1_STRING_print()");
  43755. /* setup */
  43756. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  43757. unprintableData[i] = HELLO_DATA[i];
  43758. expected[i] = HELLO_DATA[i];
  43759. }
  43760. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  43761. unprintableData[sizeof(HELLO_DATA)+i] = i;
  43762. if (i == (int)'\n' || i == (int)'\r')
  43763. expected[sizeof(HELLO_DATA)+i] = i;
  43764. else
  43765. expected[sizeof(HELLO_DATA)+i] = '.';
  43766. }
  43767. unprintableData[sizeof(unprintableData)-1] = '\0';
  43768. expected[sizeof(expected)-1] = '\0';
  43769. XMEMSET(rbuf, 0, MAX_BUF);
  43770. bio = BIO_new(BIO_s_mem());
  43771. BIO_set_write_buf_size(bio, MAX_BUF);
  43772. asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  43773. ASN1_STRING_set(asnStr,(const void*)unprintableData,
  43774. (int)sizeof(unprintableData));
  43775. /* test */
  43776. p_len = wolfSSL_ASN1_STRING_print(bio, asnStr);
  43777. AssertIntEQ(p_len, 46);
  43778. BIO_read(bio, (void*)rbuf, 46);
  43779. AssertStrEQ((char*)rbuf, (const char*)expected);
  43780. BIO_free(bio);
  43781. ASN1_STRING_free(asnStr);
  43782. printf(resultFmt, passed);
  43783. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS */
  43784. }
  43785. #endif /* !NO_BIO */
  43786. static void test_wolfSSL_ASN1_get_object(void)
  43787. {
  43788. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  43789. const unsigned char* derBuf = cliecc_cert_der_256;
  43790. int len = sizeof_cliecc_cert_der_256;
  43791. long asnLen = 0;
  43792. int tag = 0, cls = 0;
  43793. ASN1_OBJECT *a;
  43794. printf(testingFmt, "wolfSSL_ASN1_get_object()");
  43795. /* Read a couple TLV triplets and make sure they match the expected values */
  43796. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  43797. AssertIntEQ(asnLen, 862);
  43798. AssertIntEQ(tag, 0x10);
  43799. AssertIntEQ(cls, 0);
  43800. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  43801. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  43802. AssertIntEQ(asnLen, 772);
  43803. AssertIntEQ(tag, 0x10);
  43804. AssertIntEQ(cls, 0);
  43805. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  43806. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  43807. AssertIntEQ(asnLen, 3);
  43808. AssertIntEQ(tag, 0);
  43809. AssertIntEQ(cls, 0x80);
  43810. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  43811. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  43812. AssertIntEQ(asnLen, 1);
  43813. AssertIntEQ(tag, 0x2);
  43814. AssertIntEQ(cls, 0);
  43815. derBuf += asnLen;
  43816. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  43817. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  43818. AssertIntEQ(asnLen, 20);
  43819. AssertIntEQ(tag, 0x2);
  43820. AssertIntEQ(cls, 0);
  43821. derBuf += asnLen;
  43822. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  43823. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  43824. AssertIntEQ(asnLen, 10);
  43825. AssertIntEQ(tag, 0x10);
  43826. AssertIntEQ(cls, 0);
  43827. /* Read an ASN OBJECT */
  43828. AssertNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  43829. ASN1_OBJECT_free(a);
  43830. printf(resultFmt, passed);
  43831. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  43832. }
  43833. static void test_wolfSSL_RSA(void)
  43834. {
  43835. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  43836. defined(WOLFSSL_KEY_GEN)
  43837. RSA* rsa;
  43838. const BIGNUM *n;
  43839. const BIGNUM *e;
  43840. const BIGNUM *d;
  43841. const BIGNUM *p;
  43842. const BIGNUM *q;
  43843. const BIGNUM *dmp1;
  43844. const BIGNUM *dmq1;
  43845. const BIGNUM *iqmp;
  43846. printf(testingFmt, "wolfSSL_RSA()");
  43847. AssertNotNull(rsa = RSA_new());
  43848. AssertIntEQ(RSA_size(NULL), 0);
  43849. AssertIntEQ(RSA_size(rsa), 0);
  43850. AssertIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  43851. AssertIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  43852. AssertIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  43853. #ifdef WOLFSSL_RSA_KEY_CHECK
  43854. AssertIntEQ(RSA_check_key(rsa), 0);
  43855. #endif
  43856. RSA_free(rsa);
  43857. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  43858. AssertIntEQ(RSA_size(rsa), 256);
  43859. #ifdef WOLFSSL_RSA_KEY_CHECK
  43860. AssertIntEQ(RSA_check_key(NULL), 0);
  43861. AssertIntEQ(RSA_check_key(rsa), 1);
  43862. #endif
  43863. /* sanity check */
  43864. AssertIntEQ(RSA_bits(NULL), 0);
  43865. /* key */
  43866. AssertIntEQ(RSA_bits(rsa), 2048);
  43867. RSA_get0_key(rsa, &n, &e, &d);
  43868. AssertPtrEq(rsa->n, n);
  43869. AssertPtrEq(rsa->e, e);
  43870. AssertPtrEq(rsa->d, d);
  43871. AssertNotNull(n = BN_new());
  43872. AssertNotNull(e = BN_new());
  43873. AssertNotNull(d = BN_new());
  43874. AssertIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  43875. AssertPtrEq(rsa->n, n);
  43876. AssertPtrEq(rsa->e, e);
  43877. AssertPtrEq(rsa->d, d);
  43878. AssertIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  43879. AssertIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  43880. /* crt_params */
  43881. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  43882. AssertPtrEq(rsa->dmp1, dmp1);
  43883. AssertPtrEq(rsa->dmq1, dmq1);
  43884. AssertPtrEq(rsa->iqmp, iqmp);
  43885. AssertNotNull(dmp1 = BN_new());
  43886. AssertNotNull(dmq1 = BN_new());
  43887. AssertNotNull(iqmp = BN_new());
  43888. AssertIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  43889. (BIGNUM*)iqmp), 1);
  43890. AssertPtrEq(rsa->dmp1, dmp1);
  43891. AssertPtrEq(rsa->dmq1, dmq1);
  43892. AssertPtrEq(rsa->iqmp, iqmp);
  43893. AssertIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  43894. AssertIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  43895. (BIGNUM*)iqmp), 0);
  43896. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  43897. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  43898. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  43899. AssertNull(dmp1);
  43900. AssertNull(dmq1);
  43901. AssertNull(iqmp);
  43902. /* factors */
  43903. RSA_get0_factors(rsa, NULL, NULL);
  43904. RSA_get0_factors(rsa, &p, &q);
  43905. AssertPtrEq(rsa->p, p);
  43906. AssertPtrEq(rsa->q, q);
  43907. AssertNotNull(p = BN_new());
  43908. AssertNotNull(q = BN_new());
  43909. AssertIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  43910. AssertPtrEq(rsa->p, p);
  43911. AssertPtrEq(rsa->q, q);
  43912. AssertIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  43913. AssertIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  43914. RSA_get0_factors(NULL, NULL, NULL);
  43915. RSA_get0_factors(NULL, &p, &q);
  43916. AssertNull(p);
  43917. AssertNull(q);
  43918. AssertIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  43919. AssertIntEQ(RSA_bits(rsa), 21);
  43920. RSA_free(rsa);
  43921. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  43922. AssertNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  43923. AssertIntEQ(RSA_size(rsa), 384);
  43924. AssertIntEQ(RSA_bits(rsa), 3072);
  43925. RSA_free(rsa);
  43926. #endif
  43927. /* remove for now with odd key size until adjusting rsa key size check with
  43928. wc_MakeRsaKey()
  43929. AssertNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  43930. RSA_free(rsa);
  43931. */
  43932. AssertNull(RSA_generate_key(-1, 3, NULL, NULL));
  43933. AssertNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  43934. AssertNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  43935. AssertNull(RSA_generate_key(2048, 0, NULL, NULL));
  43936. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  43937. {
  43938. byte buff[FOURK_BUF];
  43939. byte der[FOURK_BUF];
  43940. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  43941. XFILE f;
  43942. int bytes;
  43943. /* test loading encrypted RSA private pem w/o password */
  43944. f = XFOPEN(PrivKeyPemFile, "rb");
  43945. AssertTrue((f != XBADFILE));
  43946. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  43947. XFCLOSE(f);
  43948. XMEMSET(der, 0, sizeof(der));
  43949. /* test that error value is returned with no password */
  43950. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""), 0);
  43951. }
  43952. #endif
  43953. printf(resultFmt, passed);
  43954. #endif
  43955. }
  43956. static void test_wolfSSL_RSA_DER(void)
  43957. {
  43958. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  43959. !defined(NO_RSA) && !defined(HAVE_USER_RSA) && defined(OPENSSL_EXTRA)
  43960. RSA *rsa;
  43961. int i;
  43962. const unsigned char *buff = NULL;
  43963. unsigned char *newBuff = NULL;
  43964. struct tbl_s
  43965. {
  43966. const unsigned char *der;
  43967. int sz;
  43968. } tbl[] = {
  43969. #ifdef USE_CERT_BUFFERS_1024
  43970. {client_key_der_1024, sizeof_client_key_der_1024},
  43971. {server_key_der_1024, sizeof_server_key_der_1024},
  43972. #endif
  43973. #ifdef USE_CERT_BUFFERS_2048
  43974. {client_key_der_2048, sizeof_client_key_der_2048},
  43975. {server_key_der_2048, sizeof_server_key_der_2048},
  43976. #endif
  43977. {NULL, 0}
  43978. };
  43979. /* Public Key DER */
  43980. struct tbl_s pub[] = {
  43981. #ifdef USE_CERT_BUFFERS_1024
  43982. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  43983. #endif
  43984. #ifdef USE_CERT_BUFFERS_2048
  43985. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  43986. #endif
  43987. {NULL, 0}
  43988. };
  43989. printf(testingFmt, "test_wolfSSL_RSA_DER()");
  43990. AssertNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  43991. buff = pub[0].der;
  43992. AssertNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  43993. AssertNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  43994. buff = tbl[0].der;
  43995. AssertNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  43996. AssertIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  43997. rsa = RSA_new();
  43998. AssertIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  43999. RSA_free(rsa);
  44000. for (i = 0; tbl[i].der != NULL; i++)
  44001. {
  44002. /* Passing in pointer results in pointer moving. */
  44003. buff = tbl[i].der;
  44004. AssertNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  44005. AssertNotNull(rsa);
  44006. RSA_free(rsa);
  44007. }
  44008. for (i = 0; tbl[i].der != NULL; i++)
  44009. {
  44010. /* Passing in pointer results in pointer moving. */
  44011. buff = tbl[i].der;
  44012. AssertNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  44013. AssertNotNull(rsa);
  44014. RSA_free(rsa);
  44015. }
  44016. for (i = 0; pub[i].der != NULL; i++)
  44017. {
  44018. buff = pub[i].der;
  44019. AssertNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  44020. AssertNotNull(rsa);
  44021. AssertIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  44022. newBuff = NULL;
  44023. AssertIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  44024. AssertNotNull(newBuff);
  44025. AssertIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  44026. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44027. RSA_free(rsa);
  44028. }
  44029. printf(resultFmt, passed);
  44030. #endif
  44031. }
  44032. static void test_wolfSSL_RSA_print(void)
  44033. {
  44034. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  44035. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  44036. !defined(HAVE_FAST_RSA) && !defined(NO_BIO)
  44037. BIO *bio;
  44038. WOLFSSL_RSA* rsa = NULL;
  44039. printf(testingFmt, "wolfSSL_RSA_print");
  44040. AssertNotNull(bio = BIO_new_fd(STDOUT_FILENO, BIO_NOCLOSE));
  44041. AssertNotNull(rsa = RSA_new());
  44042. AssertIntEQ(RSA_print(NULL, rsa, 0), -1);
  44043. AssertIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  44044. AssertIntEQ(RSA_print(bio, NULL, 0), -1);
  44045. AssertIntEQ(RSA_print_fp(stdout, NULL, 0), 0);
  44046. /* Some very large number of indent spaces. */
  44047. AssertIntEQ(RSA_print(bio, rsa, 128), -1);
  44048. /* RSA is empty. */
  44049. AssertIntEQ(RSA_print(bio, rsa, 0), 0);
  44050. AssertIntEQ(RSA_print_fp(stdout, rsa, 0), 0);
  44051. RSA_free(rsa);
  44052. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  44053. AssertIntEQ(RSA_print(bio, rsa, 0), 1);
  44054. AssertIntEQ(RSA_print(bio, rsa, 4), 1);
  44055. AssertIntEQ(RSA_print(bio, rsa, -1), 1);
  44056. AssertIntEQ(RSA_print_fp(stdout, rsa, 0), 1);
  44057. AssertIntEQ(RSA_print_fp(stdout, rsa, 4), 1);
  44058. AssertIntEQ(RSA_print_fp(stdout, rsa, -1), 1);
  44059. BIO_free(bio);
  44060. RSA_free(rsa);
  44061. printf(resultFmt, passed);
  44062. #endif
  44063. }
  44064. #ifndef NO_RSA
  44065. static void test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  44066. {
  44067. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  44068. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44069. RSA *rsa;
  44070. const unsigned char *derBuf = client_key_der_2048;
  44071. unsigned char em[256] = {0}; /* len = 2048/8 */
  44072. /* Random data simulating a hash */
  44073. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  44074. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  44075. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  44076. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  44077. };
  44078. AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  44079. AssertIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  44080. RSA_PSS_SALTLEN_DIGEST), 0);
  44081. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  44082. RSA_PSS_SALTLEN_DIGEST), 0);
  44083. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  44084. RSA_PSS_SALTLEN_DIGEST), 0);
  44085. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  44086. RSA_PSS_SALTLEN_DIGEST), 0);
  44087. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  44088. AssertIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  44089. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  44090. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  44091. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  44092. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  44093. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  44094. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  44095. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  44096. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  44097. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  44098. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  44099. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  44100. RSA_PSS_SALTLEN_DIGEST), 1);
  44101. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  44102. RSA_PSS_SALTLEN_DIGEST), 1);
  44103. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  44104. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  44105. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  44106. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  44107. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  44108. RSA_PSS_SALTLEN_MAX), 1);
  44109. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  44110. RSA_PSS_SALTLEN_MAX), 1);
  44111. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  44112. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  44113. RSA_free(rsa);
  44114. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  44115. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  44116. }
  44117. #endif
  44118. static void test_wolfSSL_RSA_sign_sha3(void)
  44119. {
  44120. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  44121. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  44122. RSA *rsa;
  44123. const unsigned char *derBuf = client_key_der_2048;
  44124. unsigned char sigRet[256] = {0};
  44125. unsigned int sigLen = sizeof(sigRet);
  44126. /* Random data simulating a hash */
  44127. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  44128. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  44129. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  44130. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  44131. };
  44132. printf(testingFmt, "wolfSSL_RSA_sign_sha3");
  44133. AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  44134. AssertIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet,
  44135. &sigLen, rsa), 1);
  44136. RSA_free(rsa);
  44137. printf(resultFmt, passed);
  44138. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  44139. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  44140. }
  44141. static void test_wolfSSL_RSA_get0_key(void)
  44142. {
  44143. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  44144. RSA *rsa = NULL;
  44145. const BIGNUM* n = NULL;
  44146. const BIGNUM* e = NULL;
  44147. const BIGNUM* d = NULL;
  44148. const unsigned char* der;
  44149. int derSz;
  44150. #ifdef USE_CERT_BUFFERS_1024
  44151. der = client_key_der_1024;
  44152. derSz = sizeof_client_key_der_1024;
  44153. #elif defined(USE_CERT_BUFFERS_2048)
  44154. der = client_key_der_2048;
  44155. derSz = sizeof_client_key_der_2048;
  44156. #else
  44157. der = NULL;
  44158. derSz = 0;
  44159. #endif
  44160. printf(testingFmt, "test_wolfSSL_RSA_get0_key()");
  44161. if (der != NULL) {
  44162. RSA_get0_key(NULL, NULL, NULL, NULL);
  44163. RSA_get0_key(rsa, NULL, NULL, NULL);
  44164. RSA_get0_key(NULL, &n, &e, &d);
  44165. AssertNull(n);
  44166. AssertNull(e);
  44167. AssertNull(d);
  44168. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  44169. AssertNotNull(rsa);
  44170. RSA_get0_key(rsa, NULL, NULL, NULL);
  44171. RSA_get0_key(rsa, &n, NULL, NULL);
  44172. AssertNotNull(n);
  44173. RSA_get0_key(rsa, NULL, &e, NULL);
  44174. AssertNotNull(e);
  44175. RSA_get0_key(rsa, NULL, NULL, &d);
  44176. AssertNotNull(d);
  44177. RSA_get0_key(rsa, &n, &e, &d);
  44178. AssertNotNull(n);
  44179. AssertNotNull(e);
  44180. AssertNotNull(d);
  44181. RSA_free(rsa);
  44182. }
  44183. printf(resultFmt, passed);
  44184. #endif
  44185. }
  44186. static void test_wolfSSL_RSA_meth(void)
  44187. {
  44188. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  44189. RSA *rsa;
  44190. RSA_METHOD *rsa_meth;
  44191. printf(testingFmt, "test_wolfSSL_RSA_meth");
  44192. #ifdef WOLFSSL_KEY_GEN
  44193. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  44194. RSA_free(rsa);
  44195. #else
  44196. AssertNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  44197. #endif
  44198. AssertNotNull(RSA_get_default_method());
  44199. wolfSSL_RSA_meth_free(NULL);
  44200. AssertNull(wolfSSL_RSA_meth_new(NULL, 0));
  44201. AssertNotNull(rsa_meth =
  44202. RSA_meth_new("placeholder RSA method", RSA_METHOD_FLAG_NO_CHECK));
  44203. #ifndef NO_WOLFSSL_STUB
  44204. AssertIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  44205. AssertIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  44206. AssertIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  44207. AssertIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  44208. AssertIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  44209. AssertIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  44210. AssertIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  44211. #endif
  44212. AssertIntEQ(RSA_flags(NULL), 0);
  44213. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  44214. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  44215. AssertIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  44216. AssertNotNull(rsa = RSA_new());
  44217. /* No method set. */
  44218. AssertIntEQ(RSA_flags(rsa), 0);
  44219. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  44220. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  44221. AssertIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  44222. AssertIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  44223. AssertIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  44224. AssertNull(RSA_get_method(NULL));
  44225. AssertPtrEq(RSA_get_method(rsa), rsa_meth);
  44226. AssertIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  44227. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  44228. AssertIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  44229. AssertIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  44230. RSA_METHOD_FLAG_NO_CHECK);
  44231. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  44232. AssertIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  44233. AssertIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  44234. /* rsa_meth is freed here */
  44235. RSA_free(rsa);
  44236. printf(resultFmt, passed);
  44237. #endif
  44238. }
  44239. static void test_wolfSSL_RSA_verify(void)
  44240. {
  44241. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && \
  44242. !defined(NO_FILESYSTEM)
  44243. #ifndef NO_BIO
  44244. XFILE fp;
  44245. RSA *pKey, *pubKey;
  44246. X509 *cert;
  44247. const char *text = "Hello wolfSSL !";
  44248. unsigned char hash[SHA256_DIGEST_LENGTH];
  44249. unsigned char signature[2048/8];
  44250. unsigned int signatureLength;
  44251. byte *buf;
  44252. BIO *bio;
  44253. SHA256_CTX c;
  44254. EVP_PKEY *evpPkey, *evpPubkey;
  44255. size_t sz;
  44256. printf(testingFmt, "wolfSSL_RSA_verify");
  44257. /* generate hash */
  44258. SHA256_Init(&c);
  44259. SHA256_Update(&c, text, strlen(text));
  44260. SHA256_Final(hash, &c);
  44261. #ifdef WOLFSSL_SMALL_STACK_CACHE
  44262. /* workaround for small stack cache case */
  44263. wc_Sha256Free((wc_Sha256*)&c);
  44264. #endif
  44265. /* read privete key file */
  44266. fp = XFOPEN(svrKeyFile, "rb");
  44267. AssertTrue((fp != XBADFILE));
  44268. XFSEEK(fp, 0, XSEEK_END);
  44269. sz = XFTELL(fp);
  44270. XREWIND(fp);
  44271. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  44272. AssertIntEQ(XFREAD(buf, 1, sz, fp), sz);
  44273. XFCLOSE(fp);
  44274. /* read private key and sign hash data */
  44275. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  44276. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  44277. AssertNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  44278. AssertIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  44279. signature, &signatureLength, pKey), SSL_SUCCESS);
  44280. /* read public key and verify signed data */
  44281. fp = XFOPEN(svrCertFile,"rb");
  44282. AssertTrue((fp != XBADFILE));
  44283. cert = PEM_read_X509(fp, 0, 0, 0 );
  44284. XFCLOSE(fp);
  44285. evpPubkey = X509_get_pubkey(cert);
  44286. pubKey = EVP_PKEY_get1_RSA(evpPubkey);
  44287. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  44288. signatureLength, pubKey), SSL_SUCCESS);
  44289. AssertIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  44290. signatureLength, NULL), SSL_FAILURE);
  44291. AssertIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  44292. signatureLength, pubKey), SSL_FAILURE);
  44293. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  44294. signatureLength, pubKey), SSL_FAILURE);
  44295. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  44296. signatureLength, NULL), SSL_FAILURE);
  44297. RSA_free(pKey);
  44298. EVP_PKEY_free(evpPkey);
  44299. RSA_free(pubKey);
  44300. EVP_PKEY_free(evpPubkey);
  44301. X509_free(cert);
  44302. BIO_free(bio);
  44303. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  44304. printf(resultFmt, passed);
  44305. #endif
  44306. #endif
  44307. }
  44308. static void test_wolfSSL_RSA_sign(void)
  44309. {
  44310. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  44311. RSA *rsa;
  44312. unsigned char hash[SHA256_DIGEST_LENGTH];
  44313. #ifdef USE_CERT_BUFFERS_1024
  44314. const unsigned char* privDer = client_key_der_1024;
  44315. size_t privDerSz = sizeof_client_key_der_1024;
  44316. const unsigned char* pubDer = client_keypub_der_1024;
  44317. size_t pubDerSz = sizeof_client_keypub_der_1024;
  44318. unsigned char signature[1024/8];
  44319. #else
  44320. const unsigned char* privDer = client_key_der_2048;
  44321. size_t privDerSz = sizeof_client_key_der_2048;
  44322. const unsigned char* pubDer = client_keypub_der_2048;
  44323. size_t pubDerSz = sizeof_client_keypub_der_2048;
  44324. unsigned char signature[2048/8];
  44325. #endif
  44326. unsigned int signatureLen;
  44327. const unsigned char* der;
  44328. printf(testingFmt, "wolfSSL_RSA_sign");
  44329. XMEMSET(hash, 0, sizeof(hash));
  44330. der = privDer;
  44331. rsa = NULL;
  44332. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  44333. AssertIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  44334. AssertIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  44335. &signatureLen, rsa), 0);
  44336. AssertIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  44337. &signatureLen, rsa), 0);
  44338. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  44339. &signatureLen, rsa), 0);
  44340. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  44341. NULL, rsa), 0);
  44342. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  44343. &signatureLen, NULL), 0);
  44344. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  44345. &signatureLen, rsa), 1);
  44346. RSA_free(rsa);
  44347. der = pubDer;
  44348. rsa = NULL;
  44349. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  44350. AssertIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  44351. signatureLen, rsa), 1);
  44352. RSA_free(rsa);
  44353. printf(resultFmt, passed);
  44354. #endif
  44355. }
  44356. static void test_wolfSSL_RSA_sign_ex(void)
  44357. {
  44358. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  44359. RSA *rsa;
  44360. unsigned char hash[SHA256_DIGEST_LENGTH];
  44361. #ifdef USE_CERT_BUFFERS_1024
  44362. const unsigned char* privDer = client_key_der_1024;
  44363. size_t privDerSz = sizeof_client_key_der_1024;
  44364. const unsigned char* pubDer = client_keypub_der_1024;
  44365. size_t pubDerSz = sizeof_client_keypub_der_1024;
  44366. unsigned char signature[1024/8];
  44367. #else
  44368. const unsigned char* privDer = client_key_der_2048;
  44369. size_t privDerSz = sizeof_client_key_der_2048;
  44370. const unsigned char* pubDer = client_keypub_der_2048;
  44371. size_t pubDerSz = sizeof_client_keypub_der_2048;
  44372. unsigned char signature[2048/8];
  44373. #endif
  44374. unsigned int signatureLen;
  44375. const unsigned char* der;
  44376. unsigned char encodedHash[51];
  44377. unsigned int encodedHashLen;
  44378. const unsigned char expEncHash[] = {
  44379. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  44380. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  44381. 0x00, 0x04, 0x20,
  44382. /* Hash data */
  44383. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44384. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44385. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44386. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44387. };
  44388. printf(testingFmt, "wolfSSL_RSA_sign_ex");
  44389. XMEMSET(hash, 0, sizeof(hash));
  44390. AssertNotNull(rsa = wolfSSL_RSA_new());
  44391. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  44392. &signatureLen, rsa, 1), 0);
  44393. wolfSSL_RSA_free(rsa);
  44394. der = privDer;
  44395. rsa = NULL;
  44396. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  44397. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  44398. -1), 0);
  44399. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  44400. signature, &signatureLen, rsa, 1), 0);
  44401. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  44402. &signatureLen, rsa, 1), 0);
  44403. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  44404. &signatureLen, rsa, 1), 0);
  44405. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  44406. NULL, rsa, 1), 0);
  44407. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  44408. &signatureLen, NULL, 1), 0);
  44409. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  44410. &signatureLen, rsa, -1), 0);
  44411. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  44412. &signatureLen, rsa, 0), 0);
  44413. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  44414. &signatureLen, rsa, 0), 0);
  44415. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  44416. NULL, rsa, 0), 0);
  44417. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  44418. &signatureLen, rsa, 1), 1);
  44419. /* Test returning encoded hash. */
  44420. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  44421. &encodedHashLen, rsa, 0), 1);
  44422. AssertIntEQ(encodedHashLen, sizeof(expEncHash));
  44423. AssertIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  44424. RSA_free(rsa);
  44425. der = pubDer;
  44426. rsa = NULL;
  44427. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  44428. AssertIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  44429. signatureLen, rsa), 1);
  44430. RSA_free(rsa);
  44431. printf(resultFmt, passed);
  44432. #endif
  44433. }
  44434. static void test_wolfSSL_RSA_public_decrypt(void)
  44435. {
  44436. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  44437. RSA *rsa;
  44438. unsigned char msg[SHA256_DIGEST_LENGTH];
  44439. #ifdef USE_CERT_BUFFERS_1024
  44440. const unsigned char* pubDer = client_keypub_der_1024;
  44441. size_t pubDerSz = sizeof_client_keypub_der_1024;
  44442. unsigned char decMsg[1024/8];
  44443. const unsigned char encMsg[] = {
  44444. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  44445. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  44446. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  44447. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  44448. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  44449. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  44450. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  44451. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  44452. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  44453. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  44454. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  44455. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  44456. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  44457. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  44458. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  44459. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  44460. };
  44461. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  44462. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  44463. defined(WC_RSA_NO_PADDING)
  44464. const unsigned char encMsgNoPad[] = {
  44465. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  44466. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  44467. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  44468. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  44469. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  44470. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  44471. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  44472. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  44473. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  44474. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  44475. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  44476. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  44477. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  44478. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  44479. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  44480. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  44481. };
  44482. #endif
  44483. #else
  44484. const unsigned char* pubDer = client_keypub_der_2048;
  44485. size_t pubDerSz = sizeof_client_keypub_der_2048;
  44486. unsigned char decMsg[2048/8];
  44487. const unsigned char encMsg[] = {
  44488. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  44489. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  44490. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  44491. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  44492. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  44493. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  44494. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  44495. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  44496. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  44497. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  44498. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  44499. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  44500. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  44501. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  44502. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  44503. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  44504. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  44505. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  44506. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  44507. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  44508. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  44509. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  44510. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  44511. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  44512. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  44513. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  44514. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  44515. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  44516. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  44517. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  44518. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  44519. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  44520. };
  44521. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  44522. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  44523. defined(WC_RSA_NO_PADDING)
  44524. const unsigned char encMsgNoPad[] = {
  44525. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  44526. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  44527. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  44528. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  44529. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  44530. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  44531. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  44532. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  44533. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  44534. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  44535. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  44536. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  44537. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  44538. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  44539. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  44540. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  44541. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  44542. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  44543. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  44544. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  44545. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  44546. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  44547. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  44548. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  44549. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  44550. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  44551. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  44552. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  44553. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  44554. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  44555. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  44556. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  44557. };
  44558. #endif
  44559. #endif
  44560. const unsigned char* der;
  44561. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  44562. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  44563. defined(WC_RSA_NO_PADDING)
  44564. int i;
  44565. #endif
  44566. printf(testingFmt, "wolfSSL_RSA_public_decrypt");
  44567. XMEMSET(msg, 0, sizeof(msg));
  44568. der = pubDer;
  44569. rsa = NULL;
  44570. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  44571. AssertIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  44572. AssertIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  44573. RSA_PKCS1_PADDING), -1);
  44574. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  44575. RSA_PKCS1_PADDING), -1);
  44576. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  44577. RSA_PKCS1_PADDING), -1);
  44578. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  44579. RSA_PKCS1_PADDING), -1);
  44580. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  44581. RSA_PKCS1_PSS_PADDING), -1);
  44582. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  44583. RSA_PKCS1_PADDING), 32);
  44584. AssertIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  44585. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  44586. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  44587. defined(WC_RSA_NO_PADDING)
  44588. AssertIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  44589. rsa, RSA_NO_PADDING), sizeof(decMsg));
  44590. /* Zeros before actual data. */
  44591. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  44592. AssertIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  44593. }
  44594. /* Check actual data. */
  44595. XMEMSET(msg, 0x01, sizeof(msg));
  44596. AssertIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  44597. #endif
  44598. RSA_free(rsa);
  44599. printf(resultFmt, passed);
  44600. #endif
  44601. }
  44602. static void test_wolfSSL_RSA_private_encrypt(void)
  44603. {
  44604. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  44605. RSA *rsa;
  44606. unsigned char msg[SHA256_DIGEST_LENGTH];
  44607. #ifdef USE_CERT_BUFFERS_1024
  44608. const unsigned char* privDer = client_key_der_1024;
  44609. size_t privDerSz = sizeof_client_key_der_1024;
  44610. unsigned char encMsg[1024/8];
  44611. const unsigned char expEncMsg[] = {
  44612. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  44613. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  44614. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  44615. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  44616. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  44617. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  44618. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  44619. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  44620. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  44621. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  44622. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  44623. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  44624. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  44625. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  44626. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  44627. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  44628. };
  44629. #ifdef WC_RSA_NO_PADDING
  44630. const unsigned char expEncMsgNoPad[] = {
  44631. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  44632. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  44633. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  44634. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  44635. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  44636. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  44637. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  44638. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  44639. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  44640. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  44641. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  44642. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  44643. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  44644. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  44645. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  44646. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  44647. };
  44648. #endif
  44649. #else
  44650. const unsigned char* privDer = client_key_der_2048;
  44651. size_t privDerSz = sizeof_client_key_der_2048;
  44652. unsigned char encMsg[2048/8];
  44653. const unsigned char expEncMsg[] = {
  44654. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  44655. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  44656. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  44657. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  44658. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  44659. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  44660. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  44661. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  44662. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  44663. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  44664. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  44665. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  44666. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  44667. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  44668. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  44669. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  44670. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  44671. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  44672. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  44673. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  44674. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  44675. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  44676. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  44677. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  44678. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  44679. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  44680. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  44681. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  44682. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  44683. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  44684. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  44685. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  44686. };
  44687. #ifdef WC_RSA_NO_PADDING
  44688. const unsigned char expEncMsgNoPad[] = {
  44689. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  44690. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  44691. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  44692. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  44693. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  44694. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  44695. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  44696. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  44697. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  44698. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  44699. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  44700. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  44701. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  44702. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  44703. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  44704. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  44705. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  44706. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  44707. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  44708. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  44709. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  44710. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  44711. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  44712. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  44713. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  44714. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  44715. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  44716. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  44717. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  44718. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  44719. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  44720. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  44721. };
  44722. #endif
  44723. #endif
  44724. const unsigned char* der;
  44725. printf(testingFmt, "wolfSSL_RSA_private_encrypt");
  44726. XMEMSET(msg, 0x00, sizeof(msg));
  44727. der = privDer;
  44728. rsa = NULL;
  44729. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  44730. AssertIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  44731. AssertIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  44732. -1);
  44733. AssertIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  44734. RSA_PKCS1_PADDING), -1);
  44735. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  44736. RSA_PKCS1_PADDING), -1);
  44737. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  44738. RSA_PKCS1_PADDING), -1);
  44739. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  44740. RSA_PKCS1_PSS_PADDING), -1);
  44741. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  44742. RSA_PKCS1_PADDING), sizeof(encMsg));
  44743. AssertIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  44744. #ifdef WC_RSA_NO_PADDING
  44745. /* Non-zero message. */
  44746. XMEMSET(msg, 0x01, sizeof(msg));
  44747. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  44748. RSA_NO_PADDING), sizeof(encMsg));
  44749. AssertIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  44750. #endif
  44751. RSA_free(rsa);
  44752. printf(resultFmt, passed);
  44753. #endif
  44754. }
  44755. static void test_wolfSSL_RSA_public_encrypt(void)
  44756. {
  44757. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  44758. RSA* rsa;
  44759. const unsigned char msg[2048/8] = { 0 };
  44760. unsigned char encMsg[2048/8];
  44761. printf(testingFmt, "wolfSSL_RSA_public_decrypt");
  44762. AssertNotNull(rsa = RSA_new());
  44763. AssertIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  44764. RSA_PKCS1_PADDING), -1);
  44765. AssertIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  44766. RSA_PKCS1_PADDING), -1);
  44767. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  44768. RSA_PKCS1_PADDING), -1);
  44769. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  44770. RSA_PKCS1_PADDING), -1);
  44771. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  44772. RSA_PKCS1_PSS_PADDING), -1);
  44773. /* Empty RSA key. */
  44774. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  44775. RSA_PKCS1_PADDING), -1);
  44776. RSA_free(rsa);
  44777. printf(resultFmt, passed);
  44778. #endif
  44779. }
  44780. static void test_wolfSSL_RSA_private_decrypt(void)
  44781. {
  44782. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  44783. RSA* rsa;
  44784. unsigned char msg[2048/8];
  44785. const unsigned char encMsg[2048/8] = { 0 };
  44786. printf(testingFmt, "wolfSSL_RSA_private_decrypt");
  44787. AssertNotNull(rsa = RSA_new());
  44788. AssertIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  44789. RSA_PKCS1_PADDING), -1);
  44790. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  44791. RSA_PKCS1_PADDING), -1);
  44792. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  44793. RSA_PKCS1_PADDING), -1);
  44794. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  44795. RSA_PKCS1_PADDING), -1);
  44796. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  44797. RSA_PKCS1_PSS_PADDING), -1);
  44798. /* Empty RSA key. */
  44799. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  44800. RSA_PKCS1_PADDING), -1);
  44801. RSA_free(rsa);
  44802. printf(resultFmt, passed);
  44803. #endif
  44804. }
  44805. static void test_wolfSSL_RSA_GenAdd(void)
  44806. {
  44807. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  44808. RSA *rsa;
  44809. #ifdef USE_CERT_BUFFERS_1024
  44810. const unsigned char* privDer = client_key_der_1024;
  44811. size_t privDerSz = sizeof_client_key_der_1024;
  44812. const unsigned char* pubDer = client_keypub_der_1024;
  44813. size_t pubDerSz = sizeof_client_keypub_der_1024;
  44814. #else
  44815. const unsigned char* privDer = client_key_der_2048;
  44816. size_t privDerSz = sizeof_client_key_der_2048;
  44817. const unsigned char* pubDer = client_keypub_der_2048;
  44818. size_t pubDerSz = sizeof_client_keypub_der_2048;
  44819. #endif
  44820. const unsigned char* der;
  44821. printf(testingFmt, "wolfSSL_RSA_GenAdd");
  44822. der = privDer;
  44823. rsa = NULL;
  44824. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  44825. AssertIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  44826. #ifndef RSA_LOW_MEM
  44827. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  44828. #else
  44829. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  44830. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  44831. #endif
  44832. RSA_free(rsa);
  44833. der = pubDer;
  44834. rsa = NULL;
  44835. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  44836. /* Need private values. */
  44837. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  44838. RSA_free(rsa);
  44839. printf(resultFmt, passed);
  44840. #endif
  44841. }
  44842. static void test_wolfSSL_RSA_blinding_on(void)
  44843. {
  44844. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  44845. RSA *rsa;
  44846. WOLFSSL_BN_CTX *bnCtx;
  44847. #ifdef USE_CERT_BUFFERS_1024
  44848. const unsigned char* privDer = client_key_der_1024;
  44849. size_t privDerSz = sizeof_client_key_der_1024;
  44850. #else
  44851. const unsigned char* privDer = client_key_der_2048;
  44852. size_t privDerSz = sizeof_client_key_der_2048;
  44853. #endif
  44854. const unsigned char* der;
  44855. printf(testingFmt, "wolfSSL_RSA_blinding_on");
  44856. der = privDer;
  44857. rsa = NULL;
  44858. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  44859. AssertNotNull(bnCtx = wolfSSL_BN_CTX_new());
  44860. /* Does nothing so all parameters are valid. */
  44861. AssertIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  44862. AssertIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  44863. AssertIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  44864. AssertIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  44865. wolfSSL_BN_CTX_free(bnCtx);
  44866. RSA_free(rsa);
  44867. printf(resultFmt, passed);
  44868. #endif
  44869. }
  44870. static void test_wolfSSL_RSA_ex_data(void)
  44871. {
  44872. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  44873. RSA* rsa;
  44874. unsigned char data[1];
  44875. printf(testingFmt, "wolfSSL_RSA_ex_data");
  44876. rsa = RSA_new();
  44877. AssertNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  44878. AssertNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  44879. #ifdef MAX_EX_DATA
  44880. AssertNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  44881. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  44882. #endif
  44883. AssertIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  44884. AssertIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  44885. #ifdef HAVE_EX_DATA
  44886. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  44887. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  44888. AssertPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  44889. #else
  44890. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  44891. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  44892. AssertNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  44893. #endif
  44894. RSA_free(rsa);
  44895. printf(resultFmt, passed);
  44896. #endif /* !NO_RSA && OPENSSL_EXTRA */
  44897. }
  44898. static void test_wolfSSL_RSA_LoadDer(void)
  44899. {
  44900. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  44901. defined(OPENSSL_EXTRA_X509_SMALL))
  44902. RSA *rsa;
  44903. #ifdef USE_CERT_BUFFERS_1024
  44904. const unsigned char* privDer = client_key_der_1024;
  44905. size_t privDerSz = sizeof_client_key_der_1024;
  44906. #else
  44907. const unsigned char* privDer = client_key_der_2048;
  44908. size_t privDerSz = sizeof_client_key_der_2048;
  44909. #endif
  44910. printf(testingFmt, "wolfSSL_RSA_LoadDer");
  44911. AssertNotNull(rsa = RSA_new());
  44912. AssertIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  44913. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  44914. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  44915. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  44916. RSA_free(rsa);
  44917. printf(resultFmt, passed);
  44918. #endif
  44919. }
  44920. /* Local API. */
  44921. static void test_wolfSSL_RSA_To_Der(void)
  44922. {
  44923. #ifdef WOLFSSL_TEST_STATIC_BUILD
  44924. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA) && \
  44925. defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  44926. RSA* rsa;
  44927. #ifdef USE_CERT_BUFFERS_1024
  44928. const unsigned char* privDer = client_key_der_1024;
  44929. size_t privDerSz = sizeof_client_key_der_1024;
  44930. const unsigned char* pubDer = client_keypub_der_1024;
  44931. size_t pubDerSz = sizeof_client_keypub_der_1024;
  44932. unsigned char out[sizeof(client_key_der_1024)];
  44933. #else
  44934. const unsigned char* privDer = client_key_der_2048;
  44935. size_t privDerSz = sizeof_client_key_der_2048;
  44936. const unsigned char* pubDer = client_keypub_der_2048;
  44937. size_t pubDerSz = sizeof_client_keypub_der_2048;
  44938. unsigned char out[sizeof(client_key_der_2048)];
  44939. #endif
  44940. const unsigned char* der;
  44941. unsigned char* outDer = NULL;
  44942. printf(testingFmt, "wolfSSL_RSA_To_Der");
  44943. der = privDer;
  44944. rsa = NULL;
  44945. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  44946. AssertIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  44947. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  44948. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  44949. outDer = out;
  44950. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  44951. AssertIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  44952. outDer = NULL;
  44953. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  44954. AssertNotNull(outDer);
  44955. AssertIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  44956. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER.);
  44957. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  44958. outDer = out;
  44959. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  44960. AssertIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  44961. RSA_free(rsa);
  44962. AssertNotNull(rsa = RSA_new());
  44963. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  44964. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  44965. RSA_free(rsa);
  44966. der = pubDer;
  44967. rsa = NULL;
  44968. AssertNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  44969. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  44970. RSA_free(rsa);
  44971. printf(resultFmt, passed);
  44972. #endif
  44973. #endif
  44974. }
  44975. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  44976. static void test_wolfSSL_PEM_read_RSAPublicKey(void)
  44977. {
  44978. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  44979. !defined(NO_WOLFSSL_STUB)
  44980. RSA* rsa = NULL;
  44981. XFILE fp;
  44982. printf(testingFmt, "wolfSSL_PEM_read_RSAPublicKey");
  44983. fp = XFOPEN("./certs/client-keyPub.pem", "rb");
  44984. AssertNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  44985. /* Valid but stub so returns NULL. */
  44986. AssertNull(wolfSSL_PEM_read_RSAPublicKey(fp, NULL, NULL, NULL));
  44987. /* Valid but stub so returns NULL. */
  44988. AssertNull(wolfSSL_PEM_read_RSAPublicKey(fp, &rsa, NULL, NULL));
  44989. XFCLOSE(fp);
  44990. printf(resultFmt, passed);
  44991. #endif
  44992. }
  44993. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  44994. static void test_wolfSSL_PEM_write_RSA_PUBKEY(void)
  44995. {
  44996. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  44997. !defined(NO_WOLFSSL_STUB)
  44998. RSA* rsa = NULL;
  44999. printf(testingFmt, "wolfSSL_PEM_write_RSA_PUBKEY");
  45000. AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
  45001. AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stdout, NULL), 0);
  45002. /* Valid but stub so returns 0. */
  45003. AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stdout, rsa), 0);
  45004. printf(resultFmt, passed);
  45005. #endif
  45006. }
  45007. static void test_wolfSSL_PEM_write_RSAPrivateKey(void)
  45008. {
  45009. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  45010. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  45011. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  45012. RSA* rsa;
  45013. #ifdef USE_CERT_BUFFERS_1024
  45014. const unsigned char* privDer = client_key_der_1024;
  45015. size_t privDerSz = sizeof_client_key_der_1024;
  45016. #else
  45017. const unsigned char* privDer = client_key_der_2048;
  45018. size_t privDerSz = sizeof_client_key_der_2048;
  45019. #endif
  45020. const unsigned char* der;
  45021. #ifndef NO_AES
  45022. unsigned char passwd[] = "password";
  45023. #endif
  45024. printf(testingFmt, "wolfSSL_PEM_write_RSAPrivateKey");
  45025. AssertNotNull(rsa = RSA_new());
  45026. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stdout, rsa, NULL, NULL, 0,
  45027. NULL, NULL), 0);
  45028. RSA_free(rsa);
  45029. der = privDer;
  45030. rsa = NULL;
  45031. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  45032. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  45033. NULL, NULL), 0);
  45034. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stdout, NULL, NULL, NULL, 0,
  45035. NULL, NULL), 0);
  45036. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stdout, rsa, NULL, NULL, 0,
  45037. NULL, NULL), 1);
  45038. #ifndef NO_AES
  45039. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stdout, rsa, EVP_aes_128_cbc(),
  45040. NULL, 0, NULL, NULL), 1);
  45041. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stdout, rsa, EVP_aes_128_cbc(),
  45042. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  45043. #endif
  45044. RSA_free(rsa);
  45045. printf(resultFmt, passed);
  45046. #endif
  45047. }
  45048. static void test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  45049. {
  45050. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  45051. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  45052. defined(WOLFSSL_DER_TO_PEM))
  45053. RSA* rsa;
  45054. #ifdef USE_CERT_BUFFERS_1024
  45055. const unsigned char* privDer = client_key_der_1024;
  45056. size_t privDerSz = sizeof_client_key_der_1024;
  45057. #else
  45058. const unsigned char* privDer = client_key_der_2048;
  45059. size_t privDerSz = sizeof_client_key_der_2048;
  45060. #endif
  45061. const unsigned char* der;
  45062. #ifndef NO_AES
  45063. unsigned char passwd[] = "password";
  45064. #endif
  45065. unsigned char* pem;
  45066. int plen;
  45067. printf(testingFmt, "wolfSSL_PEM_write_mem_RSAPrivateKey");
  45068. AssertNotNull(rsa = RSA_new());
  45069. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  45070. &plen), 0);
  45071. RSA_free(rsa);
  45072. der = privDer;
  45073. rsa = NULL;
  45074. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  45075. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  45076. &plen), 0);
  45077. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  45078. &plen), 0);
  45079. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  45080. NULL), 0);
  45081. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  45082. &plen), 1);
  45083. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  45084. #ifndef NO_AES
  45085. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  45086. NULL, 0, &pem, &plen), 1);
  45087. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  45088. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  45089. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  45090. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  45091. #endif
  45092. RSA_free(rsa);
  45093. printf(resultFmt, passed);
  45094. #endif
  45095. }
  45096. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  45097. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  45098. static void test_openssl_make_self_signed_certificate(EVP_PKEY* pkey)
  45099. {
  45100. X509* x509 = NULL;
  45101. BIGNUM* serial_number = NULL;
  45102. X509_NAME* name = NULL;
  45103. time_t epoch_off = 0;
  45104. ASN1_INTEGER* asn1_serial_number;
  45105. long not_before, not_after;
  45106. AssertNotNull(x509 = X509_new());
  45107. AssertIntNE(X509_set_pubkey(x509, pkey), 0);
  45108. AssertNotNull(serial_number = BN_new());
  45109. AssertIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  45110. AssertNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  45111. AssertNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  45112. /* version 3 */
  45113. AssertIntNE(X509_set_version(x509, 2L), 0);
  45114. AssertNotNull(name = X509_NAME_new());
  45115. AssertIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  45116. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  45117. AssertIntNE(X509_set_subject_name(x509, name), 0);
  45118. AssertIntNE(X509_set_issuer_name(x509, name), 0);
  45119. not_before = (long)wc_Time(NULL);
  45120. not_after = not_before + (365 * 24 * 60 * 60);
  45121. AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &epoch_off));
  45122. AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &epoch_off));
  45123. AssertIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  45124. BN_free(serial_number);
  45125. X509_NAME_free(name);
  45126. X509_free(x509);
  45127. }
  45128. #endif
  45129. static void test_openssl_generate_key_and_cert(void)
  45130. {
  45131. #if defined(OPENSSL_EXTRA)
  45132. #if !defined(NO_RSA)
  45133. {
  45134. EVP_PKEY* pkey = EVP_PKEY_new();
  45135. int key_length = 2048;
  45136. BIGNUM* exponent = BN_new();
  45137. RSA* rsa = RSA_new();
  45138. AssertNotNull(pkey);
  45139. AssertNotNull(exponent);
  45140. AssertNotNull(rsa);
  45141. AssertIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  45142. #ifndef WOLFSSL_KEY_GEN
  45143. AssertIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  45144. #if defined(USE_CERT_BUFFERS_1024)
  45145. AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  45146. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  45147. key_length = 1024;
  45148. #elif defined(USE_CERT_BUFFERS_2048)
  45149. AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  45150. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  45151. #else
  45152. RSA_free(rsa);
  45153. rsa = NULL;
  45154. #endif
  45155. #else
  45156. AssertIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  45157. AssertIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  45158. AssertIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  45159. AssertIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  45160. #endif
  45161. if (rsa) {
  45162. AssertIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  45163. BN_free(exponent);
  45164. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  45165. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  45166. test_openssl_make_self_signed_certificate(pkey);
  45167. #endif
  45168. }
  45169. EVP_PKEY_free(pkey);
  45170. }
  45171. #endif /* !NO_RSA */
  45172. #ifdef HAVE_ECC
  45173. {
  45174. EVP_PKEY* pkey = EVP_PKEY_new();
  45175. EC_KEY* ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  45176. AssertNotNull(pkey);
  45177. AssertNotNull(ec_key);
  45178. #ifndef NO_WOLFSSL_STUB
  45179. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  45180. #endif
  45181. AssertIntNE(EC_KEY_generate_key(ec_key), 0);
  45182. AssertIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  45183. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  45184. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  45185. test_openssl_make_self_signed_certificate(pkey);
  45186. #endif
  45187. EVP_PKEY_free(pkey);
  45188. }
  45189. #endif /* HAVE_ECC */
  45190. #endif /* OPENSSL_EXTRA */
  45191. }
  45192. static void test_stubs_are_stubs(void)
  45193. {
  45194. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB)
  45195. WOLFSSL_CTX* ctx = NULL;
  45196. WOLFSSL_CTX* ctxN = NULL;
  45197. #ifndef NO_WOLFSSL_CLIENT
  45198. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  45199. AssertNotNull(ctx);
  45200. #elif !defined(NO_WOLFSSL_SERVER)
  45201. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  45202. AssertNotNull(ctx);
  45203. #else
  45204. return;
  45205. #endif
  45206. #define CHECKZERO_RET(x, y, z) AssertIntEQ((int) x(y), 0); \
  45207. AssertIntEQ((int) x(z), 0)
  45208. /* test logic, all stubs return same result regardless of ctx being NULL
  45209. * as there are no sanity checks, it's just a stub! If at some
  45210. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  45211. * if invalid inputs are supplied. Test calling both
  45212. * with and without valid inputs, if a stub functionality remains unchanged.
  45213. */
  45214. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  45215. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  45216. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  45217. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  45218. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  45219. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  45220. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  45221. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  45222. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  45223. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  45224. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  45225. wolfSSL_CTX_free(ctx);
  45226. ctx = NULL;
  45227. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB */
  45228. }
  45229. static void test_CONF_modules_xxx(void)
  45230. {
  45231. #if defined(OPENSSL_EXTRA)
  45232. CONF_modules_free();
  45233. AssertTrue(1); /* to confirm previous call gives no harm */
  45234. CONF_modules_unload(0);
  45235. AssertTrue(1);
  45236. CONF_modules_unload(1);
  45237. AssertTrue(1);
  45238. CONF_modules_unload(-1);
  45239. AssertTrue(1);
  45240. #endif /* OPENSSL_EXTRA */
  45241. }
  45242. static void test_CRYPTO_set_dynlock_xxx(void)
  45243. {
  45244. #if defined(OPENSSL_EXTRA)
  45245. printf(testingFmt, "CRYPTO_set_dynlock_xxx()");
  45246. CRYPTO_set_dynlock_create_callback(
  45247. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  45248. CRYPTO_set_dynlock_create_callback(
  45249. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  45250. CRYPTO_set_dynlock_destroy_callback(
  45251. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  45252. CRYPTO_set_dynlock_destroy_callback(
  45253. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  45254. CRYPTO_set_dynlock_lock_callback(
  45255. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  45256. CRYPTO_set_dynlock_lock_callback(
  45257. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  45258. AssertTrue(1); /* to confirm previous call gives no harm */
  45259. printf(resultFmt, passed);
  45260. #endif /* OPENSSL_EXTRA */
  45261. }
  45262. static void test_CRYPTO_THREADID_xxx(void)
  45263. {
  45264. #if defined(OPENSSL_EXTRA)
  45265. printf(testingFmt, "CRYPTO_THREADID_xxx()");
  45266. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  45267. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  45268. AssertIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  45269. printf(resultFmt, passed);
  45270. #endif /* OPENSSL_EXTRA */
  45271. }
  45272. static void test_ENGINE_cleanup(void)
  45273. {
  45274. #if defined(OPENSSL_EXTRA)
  45275. printf(testingFmt, "ENGINE_cleanup()");
  45276. ENGINE_cleanup();
  45277. AssertTrue(1); /* to confirm previous call gives no harm */
  45278. printf(resultFmt, passed);
  45279. #endif /* OPENSSL_EXTRA */
  45280. }
  45281. static void test_wolfSSL_CTX_LoadCRL(void)
  45282. {
  45283. #ifdef HAVE_CRL
  45284. WOLFSSL_CTX* ctx = NULL;
  45285. WOLFSSL* ssl = NULL;
  45286. const char* badPath = "dummypath";
  45287. const char* validPath = "./certs/crl";
  45288. const char* validFilePath = "./certs/crl/cliCrl.pem";
  45289. const char* issuerCert = "./certs/client-cert.pem";
  45290. int derType = WOLFSSL_FILETYPE_ASN1;
  45291. int pemType = WOLFSSL_FILETYPE_PEM;
  45292. int monitor = WOLFSSL_CRL_MONITOR;
  45293. WOLFSSL_CERT_MANAGER* cm = NULL;
  45294. #define FAIL_T1(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
  45295. BAD_FUNC_ARG)
  45296. #define SUCC_T(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
  45297. WOLFSSL_SUCCESS)
  45298. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  45299. #ifndef NO_WOLFSSL_CLIENT
  45300. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  45301. #elif !defined(NO_WOLFSSL_SERVER)
  45302. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  45303. #else
  45304. return;
  45305. #endif
  45306. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  45307. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  45308. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  45309. wolfSSL_CTX_free(ctx);
  45310. #ifndef NO_WOLFSSL_CLIENT
  45311. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  45312. #elif !defined(NO_WOLFSSL_SERVER)
  45313. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  45314. #else
  45315. return;
  45316. #endif
  45317. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  45318. WOLFSSL_SUCCESS);
  45319. AssertIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  45320. wolfSSL_CTX_free(ctx);
  45321. #ifndef NO_WOLFSSL_CLIENT
  45322. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  45323. #elif !defined(NO_WOLFSSL_SERVER)
  45324. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  45325. #else
  45326. return;
  45327. #endif
  45328. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  45329. WOLFSSL_SUCCESS);
  45330. AssertNotNull(ssl = wolfSSL_new(ctx));
  45331. AssertIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  45332. wolfSSL_free(ssl);
  45333. wolfSSL_CTX_free(ctx);
  45334. AssertNotNull(cm = wolfSSL_CertManagerNew());
  45335. AssertIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  45336. WOLFSSL_SUCCESS);
  45337. AssertIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType), WOLFSSL_SUCCESS);
  45338. wolfSSL_CertManagerFree(cm);
  45339. #endif
  45340. }
  45341. static void test_SetTmpEC_DHE_Sz(void)
  45342. {
  45343. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  45344. WOLFSSL_CTX *ctx;
  45345. WOLFSSL *ssl;
  45346. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  45347. AssertNotNull(ctx);
  45348. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  45349. ssl = wolfSSL_new(ctx);
  45350. AssertNotNull(ssl);
  45351. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  45352. wolfSSL_free(ssl);
  45353. wolfSSL_CTX_free(ctx);
  45354. #endif
  45355. }
  45356. static void test_wolfSSL_CTX_get0_privatekey(void)
  45357. {
  45358. #ifdef OPENSSL_ALL
  45359. WOLFSSL_CTX* ctx = NULL;
  45360. printf(testingFmt, "wolfSSL_CTX_get0_privatekey()");
  45361. #ifndef NO_RSA
  45362. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  45363. AssertNull(SSL_CTX_get0_privatekey(ctx));
  45364. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  45365. WOLFSSL_FILETYPE_PEM));
  45366. AssertNull(SSL_CTX_get0_privatekey(ctx));
  45367. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  45368. WOLFSSL_FILETYPE_PEM));
  45369. AssertNotNull(SSL_CTX_get0_privatekey(ctx));
  45370. wolfSSL_CTX_free(ctx);
  45371. #endif
  45372. #ifdef HAVE_ECC
  45373. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  45374. AssertNull(SSL_CTX_get0_privatekey(ctx));
  45375. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  45376. WOLFSSL_FILETYPE_PEM));
  45377. AssertNull(SSL_CTX_get0_privatekey(ctx));
  45378. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  45379. WOLFSSL_FILETYPE_PEM));
  45380. AssertNotNull(SSL_CTX_get0_privatekey(ctx));
  45381. wolfSSL_CTX_free(ctx);
  45382. #endif
  45383. printf(resultFmt, passed);
  45384. #endif
  45385. }
  45386. static void test_wolfSSL_dtls_set_mtu(void)
  45387. {
  45388. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  45389. defined(WOLFSSL_DTLS)
  45390. WOLFSSL_CTX* ctx = NULL;
  45391. WOLFSSL* ssl = NULL;
  45392. const char* testCertFile;
  45393. const char* testKeyFile;
  45394. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  45395. #ifndef NO_RSA
  45396. testCertFile = svrCertFile;
  45397. testKeyFile = svrKeyFile;
  45398. #elif defined(HAVE_ECC)
  45399. testCertFile = eccCertFile;
  45400. testKeyFile = eccKeyFile;
  45401. #endif
  45402. if (testCertFile != NULL && testKeyFile != NULL) {
  45403. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  45404. WOLFSSL_FILETYPE_PEM));
  45405. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  45406. WOLFSSL_FILETYPE_PEM));
  45407. }
  45408. AssertNotNull(ssl = wolfSSL_new(ctx));
  45409. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  45410. AssertIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  45411. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  45412. AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  45413. AssertIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  45414. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  45415. AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  45416. wolfSSL_free(ssl);
  45417. wolfSSL_CTX_free(ctx);
  45418. printf(testingFmt, "wolfSSL_dtls_set_mtu()");
  45419. printf(resultFmt, passed);
  45420. #endif
  45421. }
  45422. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  45423. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  45424. !defined(WOLFSSL_NO_CLIENT_AUTH))
  45425. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  45426. {
  45427. int ret;
  45428. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  45429. printf("loading cert %s failed\n", certA);
  45430. printf("Error: (%d): %s\n", ret, wolfSSL_ERR_reason_error_string(ret));
  45431. return -1;
  45432. }
  45433. return 0;
  45434. }
  45435. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  45436. {
  45437. int ret;
  45438. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  45439. != WOLFSSL_SUCCESS) {
  45440. printf("could not verify the cert: %s\n", certA);
  45441. printf("Error: (%d): %s\n", ret, wolfSSL_ERR_reason_error_string(ret));
  45442. return -1;
  45443. } else {
  45444. printf("successfully verified: %s\n", certA);
  45445. }
  45446. return 0;
  45447. }
  45448. #define LOAD_ONE_CA(a, b, c, d) \
  45449. do { \
  45450. (a) = load_ca_into_cm(c, d); \
  45451. if ((a) != 0) \
  45452. return (b); \
  45453. else \
  45454. (b)--; \
  45455. } while(0)
  45456. #define VERIFY_ONE_CERT(a, b, c, d) \
  45457. do { \
  45458. (a) = verify_cert_with_cm(c, d); \
  45459. if ((a) != 0) \
  45460. return (b); \
  45461. else \
  45462. (b)--; \
  45463. } while(0)
  45464. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  45465. {
  45466. int ret;
  45467. int i = -1;
  45468. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  45469. char chainGArr[9][50] = {"certs/ca-cert.pem",
  45470. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  45471. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  45472. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  45473. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  45474. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  45475. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  45476. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  45477. "certs/test-pathlen/chainG-entity.pem"};
  45478. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  45479. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  45480. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  45481. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  45482. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  45483. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  45484. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  45485. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  45486. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  45487. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  45488. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  45489. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  45490. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  45491. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  45492. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  45493. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  45494. /* test validating the entity twice, should have no effect on pathLen since
  45495. * entity/leaf cert */
  45496. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  45497. return ret;
  45498. }
  45499. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  45500. {
  45501. int ret;
  45502. int i = -1;
  45503. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  45504. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  45505. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  45506. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  45507. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  45508. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  45509. */
  45510. char chainHArr[6][50] = {"certs/ca-cert.pem",
  45511. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  45512. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  45513. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  45514. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  45515. "certs/test-pathlen/chainH-entity.pem"};
  45516. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  45517. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  45518. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  45519. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  45520. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  45521. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  45522. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  45523. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  45524. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  45525. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  45526. return ret;
  45527. }
  45528. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  45529. {
  45530. int ret;
  45531. int i = -1;
  45532. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  45533. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  45534. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  45535. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  45536. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  45537. */
  45538. char chainIArr[5][50] = {"certs/ca-cert.pem",
  45539. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  45540. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  45541. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  45542. "certs/test-pathlen/chainI-entity.pem"};
  45543. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  45544. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  45545. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  45546. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  45547. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  45548. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  45549. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  45550. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  45551. return ret;
  45552. }
  45553. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  45554. {
  45555. int ret;
  45556. int i = -1;
  45557. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  45558. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  45559. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  45560. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  45561. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  45562. */
  45563. char chainJArr[6][50] = {"certs/ca-cert.pem",
  45564. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  45565. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  45566. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  45567. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  45568. "certs/test-pathlen/chainJ-entity.pem"};
  45569. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  45570. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  45571. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  45572. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  45573. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  45574. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  45575. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  45576. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  45577. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  45578. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  45579. return ret;
  45580. }
  45581. static int test_various_pathlen_chains(void)
  45582. {
  45583. int ret;
  45584. WOLFSSL_CERT_MANAGER* cm;
  45585. /* Test chain G (large chain with varying pathLens) */
  45586. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  45587. printf("cert manager new failed\n");
  45588. return -1;
  45589. }
  45590. AssertIntEQ(test_chainG(cm), 0);
  45591. ret = wolfSSL_CertManagerUnloadCAs(cm);
  45592. if (ret != WOLFSSL_SUCCESS)
  45593. return -1;
  45594. wolfSSL_CertManagerFree(cm);
  45595. /* end test chain G */
  45596. /* Test chain H (5 chain with same pathLens) */
  45597. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  45598. printf("cert manager new failed\n");
  45599. return -1;
  45600. }
  45601. AssertIntLT(test_chainH(cm), 0);
  45602. wolfSSL_CertManagerUnloadCAs(cm);
  45603. wolfSSL_CertManagerFree(cm);
  45604. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  45605. printf("cert manager new failed\n");
  45606. return -1;
  45607. }
  45608. ret = wolfSSL_CertManagerUnloadCAs(cm);
  45609. if (ret != WOLFSSL_SUCCESS)
  45610. return -1;
  45611. wolfSSL_CertManagerFree(cm);
  45612. /* end test chain H */
  45613. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  45614. * followed by 2 ICA's, should pass) */
  45615. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  45616. printf("cert manager new failed\n");
  45617. return -1;
  45618. }
  45619. AssertIntEQ(test_chainI(cm), 0);
  45620. wolfSSL_CertManagerUnloadCAs(cm);
  45621. wolfSSL_CertManagerFree(cm);
  45622. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  45623. printf("cert manager new failed\n");
  45624. return -1;
  45625. }
  45626. ret = wolfSSL_CertManagerUnloadCAs(cm);
  45627. if (ret != WOLFSSL_SUCCESS)
  45628. return -1;
  45629. wolfSSL_CertManagerFree(cm);
  45630. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  45631. * this time followed by 3 ICA's, should fail */
  45632. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  45633. printf("cert manager new failed\n");
  45634. return -1;
  45635. }
  45636. AssertIntLT(test_chainJ(cm), 0);
  45637. wolfSSL_CertManagerUnloadCAs(cm);
  45638. wolfSSL_CertManagerFree(cm);
  45639. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  45640. printf("cert manager new failed\n");
  45641. return -1;
  45642. }
  45643. ret = wolfSSL_CertManagerUnloadCAs(cm);
  45644. wolfSSL_CertManagerFree(cm);
  45645. return ret;
  45646. }
  45647. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  45648. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  45649. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  45650. {
  45651. byte ekm[100] = {0};
  45652. (void)ctx;
  45653. /* Succes Cases */
  45654. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  45655. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  45656. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  45657. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  45658. /* Use some random context */
  45659. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  45660. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  45661. /* Failure cases */
  45662. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  45663. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  45664. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  45665. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  45666. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  45667. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  45668. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  45669. "extended master secret", XSTR_SIZEOF("extended master secret"), NULL, 0, 0), 0);
  45670. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  45671. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  45672. return 0;
  45673. }
  45674. static void test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  45675. {
  45676. wolfSSL_KeepArrays(ssl);
  45677. }
  45678. static void test_export_keying_material(void)
  45679. {
  45680. #ifndef SINGLE_THREADED
  45681. tcp_ready ready;
  45682. callback_functions clientCb;
  45683. func_args client_args;
  45684. func_args server_args;
  45685. THREAD_TYPE serverThread;
  45686. XMEMSET(&client_args, 0, sizeof(func_args));
  45687. XMEMSET(&server_args, 0, sizeof(func_args));
  45688. XMEMSET(&clientCb, 0, sizeof(callback_functions));
  45689. #ifdef WOLFSSL_TIRTOS
  45690. fdOpenSession(Task_self());
  45691. #endif
  45692. StartTCP();
  45693. InitTcpReady(&ready);
  45694. #if defined(USE_WINDOWS_API)
  45695. /* use RNG to get random port if using windows */
  45696. ready.port = GetRandomPort();
  45697. #endif
  45698. server_args.signal = &ready;
  45699. client_args.signal = &ready;
  45700. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  45701. client_args.callbacks = &clientCb;
  45702. start_thread(test_server_nofail, &server_args, &serverThread);
  45703. wait_tcp_ready(&server_args);
  45704. test_client_nofail(&client_args, test_export_keying_material_cb);
  45705. join_thread(serverThread);
  45706. AssertTrue(client_args.return_code);
  45707. AssertTrue(server_args.return_code);
  45708. FreeTcpReady(&ready);
  45709. #ifdef WOLFSSL_TIRTOS
  45710. fdOpenSession(Task_self());
  45711. #endif
  45712. #endif /* !SINGLE_THREADED */
  45713. }
  45714. #endif /* HAVE_KEYING_MATERIAL */
  45715. static int test_wolfSSL_THREADID_hash(void)
  45716. {
  45717. int ret = 0;
  45718. unsigned long res;
  45719. #if defined(OPENSSL_EXTRA)
  45720. CRYPTO_THREADID id;
  45721. printf(testingFmt, "wolfSSL_THREADID_hash");
  45722. CRYPTO_THREADID_current(NULL);
  45723. AssertTrue(1);
  45724. res = CRYPTO_THREADID_hash(NULL);
  45725. AssertTrue( res == 0UL);
  45726. XMEMSET(&id, 0, sizeof(id));
  45727. res = CRYPTO_THREADID_hash(&id);
  45728. AssertTrue( res == 0UL);
  45729. printf(resultFmt, passed);
  45730. #endif /* OPENSSL_EXTRA */
  45731. (void)res;
  45732. return ret;
  45733. }
  45734. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  45735. {
  45736. int ret = 0;
  45737. WOLFSSL_CTX* ctx = NULL;
  45738. #if defined(OPENSSL_EXTRA)
  45739. printf(testingFmt, "SSL_CTX_set_ecdh_auto");
  45740. AssertIntEQ( SSL_CTX_set_ecdh_auto(NULL,0),1);
  45741. AssertIntEQ( SSL_CTX_set_ecdh_auto(NULL,1),1);
  45742. AssertIntEQ( SSL_CTX_set_ecdh_auto(ctx,0),1);
  45743. AssertIntEQ( SSL_CTX_set_ecdh_auto(ctx,1),1);
  45744. printf(resultFmt, passed);
  45745. #endif /* OPENSSL_EXTRA */
  45746. (void)ctx;
  45747. return ret;
  45748. }
  45749. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  45750. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  45751. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  45752. {
  45753. callback_functions* callbacks = NULL;
  45754. WOLFSSL_CTX* ctx = NULL;
  45755. WOLFSSL* ssl = NULL;
  45756. SOCKET_T sfd = 0;
  45757. SOCKET_T cfd = 0;
  45758. word16 port;
  45759. char msg[] = "I hear you fa shizzle!";
  45760. int len = (int) XSTRLEN(msg);
  45761. char input[1024];
  45762. int ret, err;
  45763. if (!args)
  45764. return 0;
  45765. ((func_args*)args)->return_code = TEST_FAIL;
  45766. callbacks = ((func_args*)args)->callbacks;
  45767. ctx = wolfSSL_CTX_new(callbacks->method());
  45768. #if defined(USE_WINDOWS_API)
  45769. port = ((func_args*)args)->signal->port;
  45770. #else
  45771. /* Let tcp_listen assign port */
  45772. port = 0;
  45773. #endif
  45774. #ifdef WOLFSSL_TIRTOS
  45775. fdOpenSession(Task_self());
  45776. #endif
  45777. AssertIntEQ(WOLFSSL_SUCCESS,
  45778. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  45779. AssertIntEQ(WOLFSSL_SUCCESS,
  45780. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  45781. WOLFSSL_FILETYPE_PEM));
  45782. AssertIntEQ(WOLFSSL_SUCCESS,
  45783. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  45784. WOLFSSL_FILETYPE_PEM));
  45785. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  45786. AssertIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  45787. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  45788. #elif !defined(NO_DH)
  45789. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  45790. #endif
  45791. if (callbacks->ctx_ready)
  45792. callbacks->ctx_ready(ctx);
  45793. ssl = wolfSSL_new(ctx);
  45794. AssertNotNull(ssl);
  45795. /* listen and accept */
  45796. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  45797. CloseSocket(sfd);
  45798. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  45799. if (callbacks->ssl_ready)
  45800. callbacks->ssl_ready(ssl);
  45801. do {
  45802. err = 0; /* Reset error */
  45803. ret = wolfSSL_accept(ssl);
  45804. if (ret != WOLFSSL_SUCCESS) {
  45805. err = wolfSSL_get_error(ssl, 0);
  45806. }
  45807. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  45808. if (ret != WOLFSSL_SUCCESS) {
  45809. wolfSSL_free(ssl);
  45810. wolfSSL_CTX_free(ctx);
  45811. CloseSocket(cfd);
  45812. ((func_args*)args)->return_code = TEST_FAIL;
  45813. return 0;
  45814. }
  45815. /* read and write data */
  45816. XMEMSET( input, 0, sizeof(input));
  45817. while (1) {
  45818. ret = wolfSSL_read(ssl, input, sizeof(input));
  45819. if (ret > 0) {
  45820. break;
  45821. }
  45822. else {
  45823. err = wolfSSL_get_error(ssl,ret);
  45824. if (err == WOLFSSL_ERROR_WANT_READ) {
  45825. continue;
  45826. }
  45827. break;
  45828. }
  45829. }
  45830. if (err == WOLFSSL_ERROR_ZERO_RETURN) {
  45831. do {
  45832. ret = wolfSSL_write(ssl, msg, len);
  45833. if (ret > 0) {
  45834. break;
  45835. }
  45836. } while (ret < 0);
  45837. }
  45838. /* bidirectional shutdown */
  45839. while (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  45840. continue;
  45841. }
  45842. /* wait for the peer to disconnect the tcp connection */
  45843. do {
  45844. ret = wolfSSL_read(ssl, input, sizeof(input));
  45845. err = wolfSSL_get_error(ssl, ret);
  45846. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  45847. /* detect TCP disconnect */
  45848. AssertIntLE(ret,WOLFSSL_FAILURE);
  45849. AssertIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  45850. ((func_args*)args)->return_code = TEST_SUCCESS;
  45851. wolfSSL_free(ssl);
  45852. wolfSSL_CTX_free(ctx);
  45853. CloseSocket(cfd);
  45854. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  45855. wc_ecc_fp_free(); /* free per thread cache */
  45856. #endif
  45857. return 0;
  45858. }
  45859. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  45860. {
  45861. callback_functions* callbacks = NULL;
  45862. WOLFSSL_CTX* ctx = NULL;
  45863. WOLFSSL* ssl = NULL;
  45864. SOCKET_T sfd = 0;
  45865. char msg[] = "hello wolfssl server!";
  45866. int len = (int) XSTRLEN(msg);
  45867. char input[1024];
  45868. int idx;
  45869. int ret, err;
  45870. if (!args)
  45871. return 0;
  45872. ((func_args*)args)->return_code = TEST_FAIL;
  45873. callbacks = ((func_args*)args)->callbacks;
  45874. ctx = wolfSSL_CTX_new(callbacks->method());
  45875. #ifdef WOLFSSL_TIRTOS
  45876. fdOpenSession(Task_self());
  45877. #endif
  45878. AssertIntEQ(WOLFSSL_SUCCESS,
  45879. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  45880. AssertIntEQ(WOLFSSL_SUCCESS,
  45881. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  45882. WOLFSSL_FILETYPE_PEM));
  45883. AssertIntEQ(WOLFSSL_SUCCESS,
  45884. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  45885. WOLFSSL_FILETYPE_PEM));
  45886. AssertNotNull((ssl = wolfSSL_new(ctx)));
  45887. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  45888. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  45889. do {
  45890. err = 0; /* Reset error */
  45891. ret = wolfSSL_connect(ssl);
  45892. if (ret != WOLFSSL_SUCCESS) {
  45893. err = wolfSSL_get_error(ssl, 0);
  45894. }
  45895. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  45896. AssertIntGE(wolfSSL_write(ssl, msg, len), 0);
  45897. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  45898. input[idx] = 0;
  45899. }
  45900. ret = wolfSSL_shutdown(ssl);
  45901. if ( ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  45902. ret = wolfSSL_shutdown(ssl);
  45903. }
  45904. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  45905. ((func_args*)args)->return_code = TEST_SUCCESS;
  45906. wolfSSL_free(ssl);
  45907. wolfSSL_CTX_free(ctx);
  45908. CloseSocket(sfd);
  45909. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  45910. wc_ecc_fp_free(); /* free per thread cache */
  45911. #endif
  45912. return 0;
  45913. }
  45914. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  45915. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  45916. /* This test is to check wolfSSL_read behaves as same as
  45917. * openSSL when it is called after SSL_shutdown completes.
  45918. */
  45919. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  45920. {
  45921. int ret = 0;
  45922. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  45923. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  45924. tcp_ready ready;
  45925. func_args client_args;
  45926. func_args server_args;
  45927. THREAD_TYPE serverThread;
  45928. THREAD_TYPE clientThread;
  45929. callback_functions server_cbf;
  45930. callback_functions client_cbf;
  45931. printf(testingFmt, "wolfSSL_read_detect_TCP_disconnect()");
  45932. #ifdef WOLFSSL_TIRTOS
  45933. fdOpenSession(Task_self());
  45934. #endif
  45935. StartTCP();
  45936. InitTcpReady(&ready);
  45937. #if defined(USE_WINDOWS_API)
  45938. /* use RNG to get random port if using windows */
  45939. ready.port = GetRandomPort();
  45940. #endif
  45941. XMEMSET(&client_args, 0, sizeof(func_args));
  45942. XMEMSET(&server_args, 0, sizeof(func_args));
  45943. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  45944. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  45945. server_cbf.method = wolfTLSv1_2_server_method;
  45946. client_cbf.method = wolfTLSv1_2_client_method;
  45947. server_args.callbacks = &server_cbf;
  45948. client_args.callbacks = &client_cbf;
  45949. server_args.signal = &ready;
  45950. client_args.signal = &ready;
  45951. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  45952. wait_tcp_ready(&server_args);
  45953. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  45954. join_thread(clientThread);
  45955. join_thread(serverThread);
  45956. AssertTrue(client_args.return_code);
  45957. AssertTrue(server_args.return_code);
  45958. FreeTcpReady(&ready);
  45959. printf(resultFmt, passed);
  45960. #endif
  45961. return ret;
  45962. }
  45963. static void test_wolfSSL_CTX_get_min_proto_version(void)
  45964. {
  45965. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  45966. WOLFSSL_CTX *ctx;
  45967. (void)ctx;
  45968. printf(testingFmt, "wolfSSL_CTX_get_min_proto_version()");
  45969. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  45970. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION), WOLFSSL_SUCCESS);
  45971. #ifdef WOLFSSL_ALLOW_SSLV3
  45972. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  45973. #else
  45974. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  45975. #endif
  45976. wolfSSL_CTX_free(ctx);
  45977. #ifdef WOLFSSL_ALLOW_TLSV10
  45978. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  45979. #else
  45980. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  45981. #endif
  45982. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION), WOLFSSL_SUCCESS);
  45983. #ifdef WOLFSSL_ALLOW_TLSV10
  45984. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  45985. #else
  45986. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  45987. #endif
  45988. wolfSSL_CTX_free(ctx);
  45989. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  45990. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION), WOLFSSL_SUCCESS);
  45991. #ifndef NO_OLD_TLS
  45992. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  45993. #else
  45994. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  45995. #endif
  45996. wolfSSL_CTX_free(ctx);
  45997. #ifndef WOLFSSL_NO_TLS12
  45998. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  45999. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION), WOLFSSL_SUCCESS);
  46000. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  46001. wolfSSL_CTX_free(ctx);
  46002. #endif
  46003. #ifdef WOLFSSL_TLS13
  46004. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  46005. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION), WOLFSSL_SUCCESS);
  46006. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  46007. wolfSSL_CTX_free(ctx);
  46008. #endif
  46009. printf(resultFmt, passed);
  46010. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  46011. }
  46012. static void test_wolfSSL_security_level(void)
  46013. {
  46014. #if defined(OPENSSL_EXTRA)
  46015. SSL_CTX *ctx;
  46016. printf(testingFmt, "test_wolfSSL_security_level()");
  46017. #ifdef WOLFSSL_TLS13
  46018. #ifdef NO_WOLFSSL_SERVER
  46019. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  46020. #else
  46021. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  46022. #endif
  46023. SSL_CTX_set_security_level(ctx, 1);
  46024. AssertTrue(1);
  46025. AssertIntEQ(SSL_CTX_get_security_level(ctx), 0);
  46026. SSL_CTX_free(ctx);
  46027. #else
  46028. (void)ctx;
  46029. #endif
  46030. printf(resultFmt, passed);
  46031. #endif
  46032. }
  46033. static void test_wolfSSL_SSL_in_init(void)
  46034. {
  46035. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  46036. SSL_CTX* ctx;
  46037. SSL* ssl;
  46038. const char* testCertFile;
  46039. const char* testKeyFile;
  46040. printf(testingFmt, "test_wolfSSL_SSL_in_init()");
  46041. #ifdef WOLFSSL_TLS13
  46042. #ifdef NO_WOLFSSL_SERVER
  46043. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  46044. #else
  46045. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  46046. #endif
  46047. #ifndef NO_RSA
  46048. testCertFile = svrCertFile;
  46049. testKeyFile = svrKeyFile;
  46050. #elif defined(HAVE_ECC)
  46051. testCertFile = eccCertFile;
  46052. testKeyFile = eccKeyFile;
  46053. #else
  46054. testCertFile = NULL;
  46055. testKeyFile = NULL;
  46056. #endif
  46057. if (testCertFile != NULL && testKeyFile != NULL) {
  46058. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  46059. SSL_FILETYPE_PEM));
  46060. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  46061. SSL_FILETYPE_PEM));
  46062. }
  46063. ssl = SSL_new(ctx);
  46064. AssertNotNull(ssl);
  46065. AssertIntEQ(SSL_in_init(ssl), 1);
  46066. SSL_CTX_free(ctx);
  46067. SSL_free(ssl);
  46068. #else
  46069. (void)ctx;
  46070. (void)ssl;
  46071. (void)testCertFile;
  46072. (void)testKeyFile;
  46073. #endif
  46074. printf(resultFmt, passed);
  46075. #endif
  46076. }
  46077. static void test_wolfSSL_EC_curve(void)
  46078. {
  46079. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46080. int nid = NID_secp160k1;
  46081. const char* nid_name;
  46082. printf(testingFmt, "test_wolfSSL_EC_curve()");
  46083. AssertNotNull(nid_name = EC_curve_nid2nist(nid));
  46084. AssertIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  46085. AssertIntEQ(EC_curve_nist2nid(nid_name), nid);
  46086. printf(resultFmt, passed);
  46087. #endif
  46088. }
  46089. static void test_wolfSSL_CTX_set_timeout(void)
  46090. {
  46091. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  46092. int timeout;
  46093. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  46094. (void)timeout;
  46095. printf(testingFmt, "test_wolfSSL_CTX_set_timeout()");
  46096. AssertNotNull(ctx);
  46097. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  46098. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  46099. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  46100. */
  46101. AssertIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  46102. /* giving 0 as timeout value sets default timeout */
  46103. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  46104. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  46105. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  46106. #else
  46107. AssertIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  46108. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  46109. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  46110. #endif
  46111. wolfSSL_CTX_free(ctx);
  46112. printf(resultFmt, passed);
  46113. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  46114. }
  46115. static void test_wolfSSL_OpenSSL_version(void)
  46116. {
  46117. #if defined(OPENSSL_EXTRA)
  46118. const char* ver;
  46119. printf(testingFmt, "test_wolfSSL_OpenSSL_version()");
  46120. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  46121. AssertNotNull(ver = OpenSSL_version(0));
  46122. #else
  46123. AssertNotNull(ver = OpenSSL_version());
  46124. #endif
  46125. AssertIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  46126. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  46127. printf(resultFmt, passed);
  46128. #endif
  46129. }
  46130. static void test_CONF_CTX_CMDLINE(void)
  46131. {
  46132. #if defined(OPENSSL_ALL)
  46133. SSL_CTX* ctx = NULL;
  46134. SSL_CONF_CTX* cctx = NULL;
  46135. printf(testingFmt, "test_CONF_CTX_CMDLINE");
  46136. AssertNotNull(cctx = SSL_CONF_CTX_new());
  46137. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  46138. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  46139. AssertTrue(1);
  46140. /* set flags */
  46141. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  46142. WOLFSSL_CONF_FLAG_CMDLINE);
  46143. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  46144. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  46145. /* cmd invalid command */
  46146. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  46147. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  46148. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  46149. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  46150. AssertIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  46151. /* cmd Certificate and Private Key*/
  46152. {
  46153. #if !defined(NO_CERTS) && !defined(NO_RSA)
  46154. const char* ourCert = svrCertFile;
  46155. const char* ourKey = svrKeyFile;
  46156. AssertIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  46157. AssertIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert),
  46158. WOLFSSL_SUCCESS);
  46159. AssertIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  46160. AssertIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  46161. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  46162. #endif
  46163. }
  46164. /* cmd curves */
  46165. {
  46166. #if defined(HAVE_ECC)
  46167. const char* curve = "secp256r1";
  46168. AssertIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  46169. AssertIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  46170. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  46171. #endif
  46172. }
  46173. /* cmd CipherString */
  46174. {
  46175. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  46176. AssertIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  46177. AssertIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  46178. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  46179. }
  46180. /* cmd DH parameter */
  46181. {
  46182. #if !defined(NO_DH) && !defined(NO_BIO)
  46183. const char* ourdhcert = "./certs/dh2048.pem";
  46184. AssertIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL),
  46185. -3);
  46186. AssertIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert),
  46187. WOLFSSL_SUCCESS);
  46188. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  46189. #endif
  46190. }
  46191. SSL_CTX_free(ctx);
  46192. SSL_CONF_CTX_free(cctx);
  46193. printf(resultFmt, passed);
  46194. #endif /* OPENSSL_EXTRA */
  46195. }
  46196. static void test_CONF_CTX_FILE(void)
  46197. {
  46198. #if defined(OPENSSL_ALL)
  46199. SSL_CTX* ctx = NULL;
  46200. SSL_CONF_CTX* cctx = NULL;
  46201. printf(testingFmt, "test_CONF_CTX_FILE");
  46202. AssertNotNull(cctx = SSL_CONF_CTX_new());
  46203. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  46204. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  46205. AssertTrue(1);
  46206. /* set flags */
  46207. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  46208. WOLFSSL_CONF_FLAG_FILE);
  46209. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  46210. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  46211. /* sanity check */
  46212. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  46213. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  46214. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  46215. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  46216. AssertIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  46217. /* cmd Certificate and Private Key*/
  46218. {
  46219. #if !defined(NO_CERTS) && !defined(NO_RSA)
  46220. const char* ourCert = svrCertFile;
  46221. const char* ourKey = svrKeyFile;
  46222. AssertIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  46223. AssertIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  46224. AssertIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  46225. WOLFSSL_SUCCESS);
  46226. AssertIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  46227. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  46228. #endif
  46229. }
  46230. /* cmd curves */
  46231. {
  46232. #if defined(HAVE_ECC)
  46233. const char* curve = "secp256r1";
  46234. AssertIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  46235. AssertIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  46236. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  46237. #endif
  46238. }
  46239. /* cmd CipherString */
  46240. {
  46241. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  46242. AssertIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  46243. AssertIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher), WOLFSSL_SUCCESS);
  46244. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  46245. }
  46246. /* cmd DH parameter */
  46247. {
  46248. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  46249. const char* ourdhcert = "./certs/dh3072.pem";
  46250. AssertIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  46251. AssertIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  46252. WOLFSSL_SUCCESS);
  46253. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  46254. #endif
  46255. }
  46256. SSL_CTX_free(ctx);
  46257. SSL_CONF_CTX_free(cctx);
  46258. printf(resultFmt, passed);
  46259. #endif /* OPENSSL_EXTRA */
  46260. }
  46261. static void test_wolfSSL_CRYPTO_get_ex_new_index(void)
  46262. {
  46263. #ifdef HAVE_EX_DATA
  46264. int idx1,idx2;
  46265. printf(testingFmt, "test_wolfSSL_CRYPTO_get_ex_new_index()");
  46266. /* test for unsupported class index */
  46267. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  46268. 0,NULL, NULL, NULL, NULL ), -1);
  46269. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  46270. 0,NULL, NULL, NULL, NULL ), -1);
  46271. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  46272. 0,NULL, NULL, NULL, NULL ), -1);
  46273. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  46274. 0,NULL, NULL, NULL, NULL ), -1);
  46275. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  46276. 0,NULL, NULL, NULL, NULL ), -1);
  46277. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  46278. 0,NULL, NULL, NULL, NULL ), -1);
  46279. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  46280. 0,NULL, NULL, NULL, NULL ), -1);
  46281. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  46282. 0,NULL, NULL, NULL, NULL ), -1);
  46283. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  46284. 0,NULL, NULL, NULL, NULL ), -1);
  46285. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  46286. 0,NULL, NULL, NULL, NULL ), -1);
  46287. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  46288. 0,NULL, NULL, NULL, NULL ), -1);
  46289. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  46290. 0,NULL, NULL, NULL, NULL ), -1);
  46291. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20, 0,NULL, NULL, NULL, NULL ), -1);
  46292. /* test for supported class index */
  46293. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  46294. 0,NULL, NULL, NULL, NULL );
  46295. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  46296. 0,NULL, NULL, NULL, NULL );
  46297. AssertIntNE(idx1, -1);
  46298. AssertIntNE(idx2, -1);
  46299. AssertIntNE(idx1, idx2);
  46300. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  46301. 0,NULL, NULL, NULL, NULL );
  46302. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  46303. 0,NULL, NULL, NULL, NULL );
  46304. AssertIntNE(idx1, -1);
  46305. AssertIntNE(idx2, -1);
  46306. AssertIntNE(idx1, idx2);
  46307. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  46308. 0,NULL, NULL, NULL, NULL );
  46309. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  46310. 0,NULL, NULL, NULL, NULL );
  46311. AssertIntNE(idx1, -1);
  46312. AssertIntNE(idx2, -1);
  46313. AssertIntNE(idx1, idx2);
  46314. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  46315. 0,NULL, NULL, NULL, NULL );
  46316. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  46317. 0,NULL, NULL, NULL, NULL );
  46318. AssertIntNE(idx1, -1);
  46319. AssertIntNE(idx2, -1);
  46320. AssertIntNE(idx1, idx2);
  46321. printf(resultFmt, "passed");
  46322. #endif /* HAVE_EX_DATA */
  46323. }
  46324. static void test_wolfSSL_set_psk_use_session_callback(void)
  46325. {
  46326. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  46327. SSL_CTX* ctx;
  46328. SSL* ssl;
  46329. const char* testCertFile;
  46330. const char* testKeyFile;
  46331. printf(testingFmt, "test_wolfSSL_set_psk_use_session_callback()");
  46332. #ifdef WOLFSSL_TLS13
  46333. #ifdef NO_WOLFSSL_SERVER
  46334. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  46335. #else
  46336. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  46337. #endif
  46338. #ifndef NO_RSA
  46339. testCertFile = svrCertFile;
  46340. testKeyFile = svrKeyFile;
  46341. #elif defined(HAVE_ECC)
  46342. testCertFile = eccCertFile;
  46343. testKeyFile = eccKeyFile;
  46344. #else
  46345. testCertFile = NULL;
  46346. testKeyFile = NULL;
  46347. #endif
  46348. if (testCertFile != NULL && testKeyFile != NULL) {
  46349. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  46350. SSL_FILETYPE_PEM));
  46351. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  46352. SSL_FILETYPE_PEM));
  46353. }
  46354. ssl = SSL_new(ctx);
  46355. AssertNotNull(ssl);
  46356. SSL_set_psk_use_session_callback(ssl,
  46357. my_psk_use_session_cb);
  46358. AssertTrue(1);
  46359. SSL_CTX_free(ctx);
  46360. SSL_free(ssl);
  46361. #else
  46362. (void)ctx;
  46363. (void)ssl;
  46364. (void)testCertFile;
  46365. (void)testKeyFile;
  46366. #endif
  46367. printf(resultFmt, passed);
  46368. #endif
  46369. }
  46370. static void test_wolfSSL_DH(void)
  46371. {
  46372. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  46373. DH *dh = NULL;
  46374. BIGNUM* p;
  46375. BIGNUM* q;
  46376. BIGNUM* g;
  46377. BIGNUM* pub;
  46378. BIGNUM* priv;
  46379. #if defined(OPENSSL_ALL) && defined(WOLFSSL_KEY_GEN)
  46380. #if !defined(HAVE_FIPS) || \
  46381. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  46382. FILE* f = NULL;
  46383. unsigned char buf[268];
  46384. const unsigned char* pt = buf;
  46385. long len = 0;
  46386. dh = NULL;
  46387. XMEMSET(buf, 0, sizeof(buf));
  46388. /* Test 2048 bit parameters */
  46389. f = XFOPEN("./certs/dh2048.der", "rb");
  46390. AssertTrue(f != XBADFILE);
  46391. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  46392. XFCLOSE(f);
  46393. AssertNotNull(dh = d2i_DHparams(NULL, &pt, len));
  46394. AssertNotNull(dh->p);
  46395. AssertNotNull(dh->g);
  46396. AssertTrue(pt != buf);
  46397. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  46398. DH_get0_pqg(dh, (const BIGNUM**)&p,
  46399. (const BIGNUM**)&q,
  46400. (const BIGNUM**) &g);
  46401. AssertPtrEq(p, dh->p);
  46402. AssertPtrEq(q, dh->q);
  46403. AssertPtrEq(g, dh->g);
  46404. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  46405. AssertPtrEq(pub, dh->pub_key);
  46406. AssertPtrEq(priv, dh->priv_key);
  46407. AssertNotNull(pub = BN_new());
  46408. AssertNotNull(priv = BN_new());
  46409. AssertIntEQ(DH_set0_key(dh, pub, priv), 1);
  46410. AssertPtrEq(pub, dh->pub_key);
  46411. AssertPtrEq(priv, dh->priv_key);
  46412. DH_free(dh);
  46413. AssertNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  46414. DH_free(dh);
  46415. #endif
  46416. #endif
  46417. (void)dh;
  46418. (void)p;
  46419. (void)q;
  46420. (void)g;
  46421. (void)pub;
  46422. (void)priv;
  46423. printf(testingFmt, "test_wolfSSL_DH");
  46424. dh = wolfSSL_DH_new();
  46425. AssertNotNull(dh);
  46426. /* invalid parameters test */
  46427. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  46428. (const BIGNUM**)&q,
  46429. (const BIGNUM**)&g);
  46430. DH_get0_pqg(dh, NULL,
  46431. (const BIGNUM**)&q,
  46432. (const BIGNUM**)&g);
  46433. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  46434. DH_get0_pqg(dh, NULL, NULL, NULL);
  46435. AssertTrue(1);
  46436. DH_get0_pqg(dh, (const BIGNUM**)&p,
  46437. (const BIGNUM**)&q,
  46438. (const BIGNUM**)&g);
  46439. AssertPtrEq(p, NULL);
  46440. AssertPtrEq(q, NULL);
  46441. AssertPtrEq(g, NULL);
  46442. DH_free(dh);
  46443. /* Test DH_up_ref() */
  46444. dh = wolfSSL_DH_new();
  46445. AssertNotNull(dh);
  46446. AssertIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  46447. AssertIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  46448. DH_free(dh); /* decrease ref count */
  46449. DH_free(dh); /* free WOLFSSL_DH */
  46450. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  46451. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  46452. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  46453. #ifdef HAVE_FFDHE_2048
  46454. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  46455. DH_free(dh);
  46456. #endif
  46457. #ifdef HAVE_FFDHE_3072
  46458. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  46459. DH_free(dh);
  46460. #endif
  46461. #ifdef HAVE_FFDHE_4096
  46462. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  46463. DH_free(dh);
  46464. #endif
  46465. #else
  46466. AssertNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  46467. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  46468. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  46469. printf(resultFmt, passed);
  46470. #endif /* OPENSSL_EXTRA && !NO_DH */
  46471. }
  46472. static void test_wolfSSL_ERR_strings(void)
  46473. {
  46474. const char* err1 = "unsupported cipher suite";
  46475. const char* err2 = "wolfSSL PEM routines";
  46476. const char* err = NULL;
  46477. (void)err;
  46478. (void)err1;
  46479. (void)err2;
  46480. #if !defined(NO_ERROR_STRINGS)
  46481. printf(testingFmt, "test_wolfSSL_ERR_strings");
  46482. #if defined(OPENSSL_EXTRA)
  46483. err = ERR_reason_error_string(UNSUPPORTED_SUITE);
  46484. AssertTrue(err != NULL);
  46485. AssertIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  46486. err = ERR_func_error_string(UNSUPPORTED_SUITE);
  46487. AssertTrue(err != NULL);
  46488. AssertIntEQ((*err == '\0'), 1);
  46489. err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD);
  46490. AssertTrue(err != NULL);
  46491. AssertIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  46492. #else
  46493. err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE);
  46494. AssertTrue(err != NULL);
  46495. AssertIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  46496. err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE);
  46497. AssertTrue(err != NULL);
  46498. AssertIntEQ((*err == '\0'), 1);
  46499. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  46500. err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2);
  46501. AssertTrue(err != NULL);
  46502. AssertIntEQ((*err == '\0'), 1);
  46503. #endif
  46504. printf(resultFmt, passed);
  46505. #endif
  46506. }
  46507. static void test_wolfSSL_EVP_shake128(void)
  46508. {
  46509. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  46510. defined(WOLFSSL_SHAKE128)
  46511. printf(testingFmt, "test_wolfSSL_EVP_shake128");
  46512. const EVP_MD* md = NULL;
  46513. md = EVP_shake128();
  46514. AssertTrue(md != NULL);
  46515. AssertIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  46516. printf(resultFmt, passed);
  46517. #endif
  46518. }
  46519. static void test_wolfSSL_EVP_shake256(void)
  46520. {
  46521. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  46522. defined(WOLFSSL_SHAKE256)
  46523. const EVP_MD* md = NULL;
  46524. printf(testingFmt, "test_wolfSSL_EVP_shake256");
  46525. md = EVP_shake256();
  46526. AssertTrue(md != NULL);
  46527. AssertIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  46528. printf(resultFmt, passed);
  46529. #endif
  46530. }
  46531. static void test_EVP_blake2(void)
  46532. {
  46533. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  46534. const EVP_MD* md = NULL;
  46535. (void)md;
  46536. printf(testingFmt, "test_EVP_blake2");
  46537. #if defined(HAVE_BLAKE2)
  46538. md = EVP_blake2b512();
  46539. AssertTrue(md != NULL);
  46540. AssertIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  46541. #endif
  46542. #if defined(HAVE_BLAKE2S)
  46543. md = EVP_blake2s256();
  46544. AssertTrue(md != NULL);
  46545. AssertIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  46546. #endif
  46547. printf(resultFmt, passed);
  46548. #endif
  46549. }
  46550. #if defined(OPENSSL_EXTRA)
  46551. static void list_md_fn(const EVP_MD* m, const char* from,
  46552. const char* to, void* arg)
  46553. {
  46554. const char* mn;
  46555. BIO *bio;
  46556. (void) from;
  46557. (void) to;
  46558. (void) arg;
  46559. (void) mn;
  46560. (void) bio;
  46561. if (!m) {
  46562. /* alias */
  46563. AssertNull(m);
  46564. AssertNotNull(to);
  46565. }
  46566. else {
  46567. AssertNotNull(m);
  46568. AssertNull(to);
  46569. }
  46570. AssertNotNull(from);
  46571. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  46572. mn = EVP_get_digestbyname(from);
  46573. /* print to stdout */
  46574. AssertNotNull(arg);
  46575. bio = BIO_new(BIO_s_file());
  46576. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  46577. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  46578. BIO_free(bio);
  46579. #endif
  46580. }
  46581. #endif
  46582. static void test_EVP_MD_do_all(void)
  46583. {
  46584. #if defined(OPENSSL_EXTRA)
  46585. printf(testingFmt, "test_EVP_MD_do_all");
  46586. EVP_MD_do_all(NULL, stdout);
  46587. /* to confirm previous call gives no harm */
  46588. AssertTrue(1);
  46589. EVP_MD_do_all(list_md_fn, stdout);
  46590. /* to confirm previous call gives no harm */
  46591. AssertTrue(1);
  46592. printf(resultFmt, passed);
  46593. #endif
  46594. }
  46595. #if defined(OPENSSL_EXTRA)
  46596. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  46597. {
  46598. (void)arg;
  46599. (void)nm;
  46600. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  46601. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  46602. /* print to stdout */
  46603. AssertNotNull(arg);
  46604. bio = BIO_new(BIO_s_file());
  46605. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  46606. BIO_printf(bio, "%s\n", mn);
  46607. BIO_free(bio);
  46608. #endif
  46609. }
  46610. #endif
  46611. static void test_OBJ_NAME_do_all(void)
  46612. {
  46613. #if defined(OPENSSL_EXTRA)
  46614. printf(testingFmt, "test_OBJ_NAME_do_all");
  46615. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  46616. /* to confirm previous call gives no harm */
  46617. AssertTrue(1);
  46618. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stdout);
  46619. /* to confirm previous call gives no harm */
  46620. AssertTrue(1);
  46621. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stdout);
  46622. AssertTrue(1);
  46623. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stdout);
  46624. AssertTrue(1);
  46625. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stdout);
  46626. AssertTrue(1);
  46627. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stdout);
  46628. AssertTrue(1);
  46629. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stdout);
  46630. AssertTrue(1);
  46631. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stdout);
  46632. AssertTrue(1);
  46633. OBJ_NAME_do_all(-1, obj_name_t, stdout);
  46634. AssertTrue(1);
  46635. printf(resultFmt, passed);
  46636. #endif
  46637. }
  46638. static void test_SSL_CIPHER_get_xxx(void)
  46639. {
  46640. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  46641. !defined(NO_FILESYSTEM)
  46642. const SSL_CIPHER* cipher = NULL;
  46643. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  46644. int i, numCiphers = 0;
  46645. SSL_CTX* ctx = NULL;
  46646. SSL* ssl = NULL;
  46647. const char* testCertFile;
  46648. const char* testKeyFile;
  46649. char buf[256] = {0};
  46650. const char* cipher_id = NULL;
  46651. int expect_nid1 = NID_undef;
  46652. int expect_nid2 = NID_undef;
  46653. int expect_nid3 = NID_undef;
  46654. int expect_nid4 = NID_undef;
  46655. int expect_nid5 = 0;
  46656. const char* cipher_id2 = NULL;
  46657. int expect_nid21 = NID_undef;
  46658. int expect_nid22 = NID_undef;
  46659. int expect_nid23 = NID_undef;
  46660. int expect_nid24 = NID_undef;
  46661. int expect_nid25 = 0;
  46662. (void)cipher;
  46663. (void)supportedCiphers;
  46664. (void)i;
  46665. (void)numCiphers;
  46666. (void)ctx;
  46667. (void)ssl;
  46668. (void)testCertFile;
  46669. (void)testKeyFile;
  46670. printf(testingFmt, "test_SSL_CIPHER_get_xxx");
  46671. #if defined(WOLFSSL_TLS13)
  46672. cipher_id = "TLS13-AES128-GCM-SHA256";
  46673. expect_nid1 = NID_auth_rsa;
  46674. expect_nid2 = NID_aes_128_gcm;
  46675. expect_nid3 = NID_sha256;
  46676. expect_nid4 = NID_kx_any;
  46677. expect_nid5 = 1;
  46678. #if !defined(WOLFSSL_NO_TLS12)
  46679. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  46680. expect_nid21 = NID_auth_rsa;
  46681. expect_nid22 = NID_aes_256_gcm;
  46682. expect_nid23 = NID_sha384;
  46683. expect_nid24 = NID_kx_ecdhe;
  46684. expect_nid25 = 1;
  46685. #endif
  46686. #endif
  46687. #ifdef NO_WOLFSSL_SERVER
  46688. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  46689. #else
  46690. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  46691. #endif
  46692. if (cipher_id) {
  46693. #ifndef NO_RSA
  46694. testCertFile = svrCertFile;
  46695. testKeyFile = svrKeyFile;
  46696. #elif defined(HAVE_ECC)
  46697. testCertFile = eccCertFile;
  46698. testKeyFile = eccKeyFile;
  46699. #else
  46700. testCertFile = NULL;
  46701. testKeyFile = NULL;
  46702. #endif
  46703. if (testCertFile != NULL && testKeyFile != NULL) {
  46704. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  46705. SSL_FILETYPE_PEM));
  46706. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  46707. SSL_FILETYPE_PEM));
  46708. }
  46709. ssl = SSL_new(ctx);
  46710. AssertNotNull(ssl);
  46711. AssertIntEQ(SSL_in_init(ssl), 1);
  46712. supportedCiphers = SSL_get_ciphers(ssl);
  46713. numCiphers = sk_num(supportedCiphers);
  46714. for (i = 0; i < numCiphers; ++i) {
  46715. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  46716. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  46717. }
  46718. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  46719. break;
  46720. }
  46721. }
  46722. /* test case for */
  46723. if (i != numCiphers) {
  46724. AssertIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  46725. AssertIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  46726. AssertIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  46727. AssertIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  46728. AssertIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  46729. }
  46730. if (cipher_id2) {
  46731. for (i = 0; i < numCiphers; ++i) {
  46732. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  46733. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  46734. }
  46735. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  46736. break;
  46737. }
  46738. }
  46739. /* test case for */
  46740. if (i != numCiphers) {
  46741. AssertIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  46742. AssertIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  46743. AssertIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  46744. AssertIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  46745. AssertIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  46746. }
  46747. }
  46748. }
  46749. if (ctx)
  46750. SSL_CTX_free(ctx);
  46751. if(ssl)
  46752. SSL_free(ssl);
  46753. printf(resultFmt, passed);
  46754. #endif
  46755. }
  46756. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  46757. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  46758. int* keyFormat)
  46759. {
  46760. int ret;
  46761. byte* key_buf = NULL;
  46762. size_t key_sz = 0;
  46763. EncryptedInfo encInfo;
  46764. XMEMSET(&encInfo, 0, sizeof(encInfo));
  46765. ret = load_file(privKeyFile, &key_buf, &key_sz);
  46766. if (ret == 0) {
  46767. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  46768. NULL, &encInfo, keyFormat);
  46769. }
  46770. if (key_buf != NULL) {
  46771. free(key_buf); key_buf = NULL;
  46772. }
  46773. (void)encInfo; /* not used in this test */
  46774. #ifdef DEBUG_WOLFSSL
  46775. printf("%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  46776. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  46777. (*pDer)->length);
  46778. #endif
  46779. return ret;
  46780. }
  46781. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  46782. {
  46783. int ret = CRYPTOCB_UNAVAILABLE;
  46784. const char* privKeyFile = (const char*)ctx;
  46785. DerBuffer* pDer = NULL;
  46786. int keyFormat = 0;
  46787. if (info->algo_type == WC_ALGO_TYPE_PK) {
  46788. #ifdef DEBUG_WOLFSSL
  46789. printf("test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  46790. #endif
  46791. #ifndef NO_RSA
  46792. if (info->pk.type == WC_PK_TYPE_RSA) {
  46793. switch (info->pk.rsa.type) {
  46794. case RSA_PUBLIC_ENCRYPT:
  46795. case RSA_PUBLIC_DECRYPT:
  46796. /* perform software based RSA public op */
  46797. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  46798. break;
  46799. case RSA_PRIVATE_ENCRYPT:
  46800. case RSA_PRIVATE_DECRYPT:
  46801. {
  46802. RsaKey key;
  46803. /* perform software based RSA private op */
  46804. #ifdef DEBUG_WOLFSSL
  46805. printf("test_CryptoCb_Func: RSA Priv\n");
  46806. #endif
  46807. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  46808. &keyFormat);
  46809. if (ret != 0) {
  46810. return ret;
  46811. }
  46812. ret = wc_InitRsaKey(&key, HEAP_HINT);
  46813. if (ret == 0) {
  46814. word32 keyIdx = 0;
  46815. /* load RSA private key and perform private transform */
  46816. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  46817. &key, pDer->length);
  46818. if (ret == 0) {
  46819. ret = wc_RsaFunction(
  46820. info->pk.rsa.in, info->pk.rsa.inLen,
  46821. info->pk.rsa.out, info->pk.rsa.outLen,
  46822. info->pk.rsa.type, &key, info->pk.rsa.rng);
  46823. }
  46824. else {
  46825. /* if decode fails, then fall-back to software based crypto */
  46826. printf("test_CryptoCb_Func: RSA private key decode "
  46827. "failed %d, falling back to software\n", ret);
  46828. ret = CRYPTOCB_UNAVAILABLE;
  46829. }
  46830. wc_FreeRsaKey(&key);
  46831. }
  46832. wc_FreeDer(&pDer); pDer = NULL;
  46833. break;
  46834. }
  46835. }
  46836. #ifdef DEBUG_WOLFSSL
  46837. printf("test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  46838. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  46839. #endif
  46840. }
  46841. #endif /* !NO_RSA */
  46842. #ifdef HAVE_ECC
  46843. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  46844. /* mark this key as ephemeral */
  46845. if (info->pk.eckg.key != NULL) {
  46846. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  46847. sizeof(info->pk.eckg.key->label));
  46848. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  46849. }
  46850. }
  46851. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  46852. ecc_key key;
  46853. /* perform software based ECC sign */
  46854. #ifdef DEBUG_WOLFSSL
  46855. printf("test_CryptoCb_Func: ECC Sign\n");
  46856. #endif
  46857. if (info->pk.eccsign.key != NULL &&
  46858. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  46859. /* this is an empheral key */
  46860. #ifdef DEBUG_WOLFSSL
  46861. printf("test_CryptoCb_Func: skipping signing op on ephemeral key\n");
  46862. #endif
  46863. return CRYPTOCB_UNAVAILABLE;
  46864. }
  46865. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  46866. if (ret != 0) {
  46867. return ret;
  46868. }
  46869. ret = wc_ecc_init(&key);
  46870. if (ret == 0) {
  46871. word32 keyIdx = 0;
  46872. /* load ECC private key and perform private transform */
  46873. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  46874. &key, pDer->length);
  46875. if (ret == 0) {
  46876. ret = wc_ecc_sign_hash(
  46877. info->pk.eccsign.in, info->pk.eccsign.inlen,
  46878. info->pk.eccsign.out, info->pk.eccsign.outlen,
  46879. info->pk.eccsign.rng, &key);
  46880. }
  46881. else {
  46882. /* if decode fails, then fall-back to software based crypto */
  46883. printf("test_CryptoCb_Func: ECC private key decode "
  46884. "failed %d, falling back to software\n", ret);
  46885. ret = CRYPTOCB_UNAVAILABLE;
  46886. }
  46887. wc_ecc_free(&key);
  46888. }
  46889. wc_FreeDer(&pDer); pDer = NULL;
  46890. #ifdef DEBUG_WOLFSSL
  46891. printf("test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  46892. ret, *info->pk.eccsign.outlen);
  46893. #endif
  46894. }
  46895. #endif /* HAVE_ECC */
  46896. #ifdef HAVE_ED25519
  46897. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  46898. ed25519_key key;
  46899. /* perform software based ED25519 sign */
  46900. #ifdef DEBUG_WOLFSSL
  46901. printf("test_CryptoCb_Func: ED25519 Sign\n");
  46902. #endif
  46903. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  46904. if (ret != 0) {
  46905. return ret;
  46906. }
  46907. ret = wc_ed25519_init(&key);
  46908. if (ret == 0) {
  46909. word32 keyIdx = 0;
  46910. /* load ED25519 private key and perform private transform */
  46911. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  46912. &key, pDer->length);
  46913. if (ret == 0) {
  46914. /* calculate public key */
  46915. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  46916. if (ret == 0) {
  46917. key.pubKeySet = 1;
  46918. ret = wc_ed25519_sign_msg_ex(
  46919. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  46920. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  46921. &key, info->pk.ed25519sign.type,
  46922. info->pk.ed25519sign.context,
  46923. info->pk.ed25519sign.contextLen);
  46924. }
  46925. }
  46926. else {
  46927. /* if decode fails, then fall-back to software based crypto */
  46928. printf("test_CryptoCb_Func: ED25519 private key decode "
  46929. "failed %d, falling back to software\n", ret);
  46930. ret = CRYPTOCB_UNAVAILABLE;
  46931. }
  46932. wc_ed25519_free(&key);
  46933. }
  46934. wc_FreeDer(&pDer); pDer = NULL;
  46935. #ifdef DEBUG_WOLFSSL
  46936. printf("test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  46937. ret, *info->pk.ed25519sign.outLen);
  46938. #endif
  46939. }
  46940. #endif /* HAVE_ED25519 */
  46941. }
  46942. (void)thisDevId;
  46943. (void)keyFormat;
  46944. return ret;
  46945. }
  46946. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  46947. static void test_wc_CryptoCb_TLS(int tlsVer,
  46948. const char* cliCaPemFile, const char* cliCertPemFile,
  46949. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  46950. const char* svrCaPemFile, const char* svrCertPemFile,
  46951. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  46952. {
  46953. callback_functions client_cbf;
  46954. callback_functions server_cbf;
  46955. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  46956. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  46957. if (tlsVer == WOLFSSL_TLSV1_3) {
  46958. #ifdef WOLFSSL_TLS13
  46959. server_cbf.method = wolfTLSv1_3_server_method;
  46960. client_cbf.method = wolfTLSv1_3_client_method;
  46961. #endif
  46962. }
  46963. else if (tlsVer == WOLFSSL_TLSV1_2) {
  46964. #ifndef WOLFSSL_NO_TLS12
  46965. server_cbf.method = wolfTLSv1_2_server_method;
  46966. client_cbf.method = wolfTLSv1_2_client_method;
  46967. #endif
  46968. }
  46969. else if (tlsVer == WOLFSSL_TLSV1_1) {
  46970. #ifndef NO_OLD_TLS
  46971. server_cbf.method = wolfTLSv1_1_server_method;
  46972. client_cbf.method = wolfTLSv1_1_client_method;
  46973. #endif
  46974. }
  46975. else if (tlsVer == WOLFSSL_TLSV1) {
  46976. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  46977. server_cbf.method = wolfTLSv1_server_method;
  46978. client_cbf.method = wolfTLSv1_client_method;
  46979. #endif
  46980. }
  46981. else if (tlsVer == WOLFSSL_SSLV3) {
  46982. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  46983. defined(WOLFSSL_STATIC_RSA)
  46984. server_cbf.method = wolfSSLv3_server_method;
  46985. client_cbf.method = wolfSSLv3_client_method;
  46986. #endif
  46987. }
  46988. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  46989. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  46990. server_cbf.method = wolfDTLSv1_2_server_method;
  46991. client_cbf.method = wolfDTLSv1_2_client_method;
  46992. #endif
  46993. }
  46994. else if (tlsVer == WOLFSSL_DTLSV1) {
  46995. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  46996. server_cbf.method = wolfDTLSv1_server_method;
  46997. client_cbf.method = wolfDTLSv1_client_method;
  46998. #endif
  46999. }
  47000. if (server_cbf.method == NULL) {
  47001. /* not enabled */
  47002. return;
  47003. }
  47004. /* Setup the keys for the TLS test */
  47005. client_cbf.certPemFile = cliCertPemFile;
  47006. client_cbf.keyPemFile = cliPubKeyPemFile;
  47007. client_cbf.caPemFile = cliCaPemFile;
  47008. server_cbf.certPemFile = svrCertPemFile;
  47009. server_cbf.keyPemFile = svrPubKeyPemFile;
  47010. server_cbf.caPemFile = svrCaPemFile;
  47011. /* Setup a crypto callback with pointer to private key file for testing */
  47012. client_cbf.devId = 1;
  47013. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  47014. (void*)cliPrivKeyPemFile);
  47015. server_cbf.devId = 2;
  47016. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  47017. (void*)svrPrivKeyPemFile);
  47018. /* Perform TLS server and client test */
  47019. /* First test is at WOLFSSL_CTX level */
  47020. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  47021. /* Check for success */
  47022. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  47023. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  47024. /* Second test is a WOLFSSL object level */
  47025. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  47026. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  47027. /* Check for success */
  47028. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  47029. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  47030. /* Un register the devId's */
  47031. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  47032. client_cbf.devId = INVALID_DEVID;
  47033. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  47034. server_cbf.devId = INVALID_DEVID;
  47035. }
  47036. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  47037. static void test_wc_CryptoCb(void)
  47038. {
  47039. #ifdef WOLF_CRYPTO_CB
  47040. /* TODO: Add crypto callback API tests */
  47041. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  47042. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  47043. int tlsVer;
  47044. #endif
  47045. #ifndef NO_RSA
  47046. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  47047. test_wc_CryptoCb_TLS(tlsVer,
  47048. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  47049. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile);
  47050. }
  47051. #endif
  47052. #ifdef HAVE_ECC
  47053. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  47054. test_wc_CryptoCb_TLS(tlsVer,
  47055. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  47056. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile);
  47057. }
  47058. #endif
  47059. #ifdef HAVE_ED25519
  47060. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  47061. if (tlsVer == WOLFSSL_DTLSV1) continue;
  47062. test_wc_CryptoCb_TLS(tlsVer,
  47063. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  47064. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile);
  47065. }
  47066. #endif
  47067. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  47068. #endif /* WOLF_CRYPTO_CB */
  47069. }
  47070. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  47071. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  47072. static void test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  47073. const char* cliCaPemFile, const char* cliCertPemFile,
  47074. const char* cliPrivKeyPemFile,
  47075. const char* svrCaPemFile, const char* svrCertPemFile,
  47076. const char* svrPrivKeyPemFile,
  47077. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  47078. {
  47079. callback_functions client_cbf;
  47080. callback_functions server_cbf;
  47081. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  47082. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  47083. if (tlsVer == WOLFSSL_TLSV1_3) {
  47084. #ifdef WOLFSSL_TLS13
  47085. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  47086. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  47087. #endif
  47088. }
  47089. else if (tlsVer == WOLFSSL_TLSV1_2) {
  47090. #ifndef WOLFSSL_NO_TLS12
  47091. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  47092. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  47093. #endif
  47094. }
  47095. else if (tlsVer == WOLFSSL_TLSV1_1) {
  47096. #ifndef NO_OLD_TLS
  47097. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  47098. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  47099. #endif
  47100. }
  47101. else if (tlsVer == WOLFSSL_TLSV1) {
  47102. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  47103. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  47104. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  47105. #endif
  47106. }
  47107. else if (tlsVer == WOLFSSL_SSLV3) {
  47108. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  47109. defined(WOLFSSL_STATIC_RSA)
  47110. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  47111. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  47112. #endif
  47113. }
  47114. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  47115. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  47116. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  47117. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  47118. #endif
  47119. }
  47120. else if (tlsVer == WOLFSSL_DTLSV1) {
  47121. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  47122. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  47123. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  47124. #endif
  47125. }
  47126. if (server_cbf.method_ex == NULL) {
  47127. /* not enabled */
  47128. return;
  47129. }
  47130. /* Setup the keys for the TLS test */
  47131. client_cbf.certPemFile = cliCertPemFile;
  47132. client_cbf.keyPemFile = cliPrivKeyPemFile;
  47133. client_cbf.caPemFile = cliCaPemFile;
  47134. server_cbf.certPemFile = svrCertPemFile;
  47135. server_cbf.keyPemFile = svrPrivKeyPemFile;
  47136. server_cbf.caPemFile = svrCaPemFile;
  47137. client_cbf.mem = cliMem;
  47138. client_cbf.memSz = cliMemSz;
  47139. server_cbf.mem = svrMem;
  47140. server_cbf.memSz = svrMemSz;
  47141. client_cbf.devId = INVALID_DEVID;
  47142. server_cbf.devId = INVALID_DEVID;
  47143. /* Perform TLS server and client test */
  47144. /* First test is at WOLFSSL_CTX level */
  47145. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  47146. /* Check for success */
  47147. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  47148. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  47149. /* Second test is a WOLFSSL object level */
  47150. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  47151. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  47152. /* Check for success */
  47153. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  47154. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  47155. }
  47156. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  47157. #ifdef WOLFSSL_STATIC_MEMORY
  47158. #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) || \
  47159. defined(SESSION_CERTS)
  47160. #ifdef OPENSSL_EXTRA
  47161. #define TEST_TLS_STATIC_MEMSZ (400000)
  47162. #else
  47163. #define TEST_TLS_STATIC_MEMSZ (320000)
  47164. #endif
  47165. #else
  47166. #define TEST_TLS_STATIC_MEMSZ (80000)
  47167. #endif
  47168. static void test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  47169. {
  47170. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  47171. WOLFSSL_MEM_STATS mem_stats;
  47172. WOLFSSL_MEM_CONN_STATS ssl_stats;
  47173. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  47174. AssertIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  47175. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  47176. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  47177. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  47178. #endif
  47179. AssertNotNull((ssl1 = wolfSSL_new(ctx)));
  47180. AssertNotNull((ssl2 = wolfSSL_new(ctx)));
  47181. /* this should fail because kMaxCtxClients == 2 */
  47182. AssertNull((ssl3 = wolfSSL_new(ctx)));
  47183. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  47184. #ifdef DEBUG_WOLFSSL
  47185. wolfSSL_PrintStatsConn(&ssl_stats);
  47186. #endif
  47187. (void)ssl_stats;
  47188. }
  47189. /* display collected statistics */
  47190. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  47191. #ifdef DEBUG_WOLFSSL
  47192. wolfSSL_PrintStats(&mem_stats);
  47193. #endif
  47194. (void)mem_stats;
  47195. }
  47196. wolfSSL_free(ssl1);
  47197. wolfSSL_free(ssl2);
  47198. }
  47199. #endif /* WOLFSSL_STATIC_MEMORY */
  47200. static void test_wolfSSL_CTX_StaticMemory(void)
  47201. {
  47202. #ifdef WOLFSSL_STATIC_MEMORY
  47203. wolfSSL_method_func method_func;
  47204. WOLFSSL_CTX* ctx;
  47205. const int kMaxCtxClients = 2;
  47206. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  47207. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  47208. int tlsVer;
  47209. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  47210. #endif
  47211. #endif
  47212. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  47213. printf(testingFmt, "test_wolfSSL_CTX_StaticMemory()");
  47214. #ifndef NO_WOLFSSL_SERVER
  47215. #ifndef WOLFSSL_NO_TLS12
  47216. method_func = wolfTLSv1_2_server_method_ex;
  47217. #else
  47218. method_func = wolfTLSv1_3_server_method_ex;
  47219. #endif
  47220. #else
  47221. #ifndef WOLFSSL_NO_TLS12
  47222. method_func = wolfTLSv1_2_client_method_ex;
  47223. #else
  47224. method_func = wolfTLSv1_3_client_method_ex;
  47225. #endif
  47226. #endif
  47227. /* Test creating CTX directly from static memory pool */
  47228. ctx = NULL;
  47229. AssertIntEQ(wolfSSL_CTX_load_static_memory(
  47230. &ctx, method_func, svrMem, sizeof(svrMem),
  47231. 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  47232. test_wolfSSL_CTX_StaticMemory_SSL(ctx);
  47233. wolfSSL_CTX_free(ctx);
  47234. ctx = NULL;
  47235. /* Test for heap allocated CTX, then assigning static pool to it */
  47236. AssertNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  47237. AssertIntEQ(wolfSSL_CTX_load_static_memory(&ctx,
  47238. NULL, svrMem, sizeof(svrMem),
  47239. 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  47240. test_wolfSSL_CTX_StaticMemory_SSL(ctx);
  47241. wolfSSL_CTX_free(ctx);
  47242. /* TLS Level Tests using static memory */
  47243. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  47244. #ifndef NO_RSA
  47245. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  47246. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  47247. svrCertFile, cliCertFile, cliKeyFile,
  47248. cliCertFile, svrCertFile, svrKeyFile,
  47249. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  47250. }
  47251. #endif
  47252. #ifdef HAVE_ECC
  47253. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  47254. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  47255. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  47256. cliEccCertFile, eccCertFile, eccKeyFile,
  47257. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  47258. }
  47259. #endif
  47260. #ifdef HAVE_ED25519
  47261. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  47262. if (tlsVer == WOLFSSL_DTLSV1) continue;
  47263. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  47264. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  47265. cliEdCertFile, edCertFile, edKeyFile,
  47266. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  47267. }
  47268. #endif
  47269. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  47270. printf(resultFmt, passed);
  47271. #endif
  47272. }
  47273. static void test_openssl_FIPS_drbg(void)
  47274. {
  47275. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  47276. DRBG_CTX* dctx;
  47277. byte data1[32], data2[32], zeroData[32];
  47278. byte testSeed[16];
  47279. size_t dlen = sizeof(data1);
  47280. int i;
  47281. XMEMSET(data1, 0, dlen);
  47282. XMEMSET(data2, 0, dlen);
  47283. XMEMSET(zeroData, 0, sizeof(zeroData));
  47284. for (i=0; i<(int)sizeof(testSeed); i++) {
  47285. testSeed[i] = (byte)i;
  47286. }
  47287. printf(testingFmt, "test_openssl_FIPS_drbg()");
  47288. AssertNotNull(dctx = FIPS_get_default_drbg());
  47289. AssertIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  47290. AssertIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  47291. WOLFSSL_SUCCESS);
  47292. AssertIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  47293. AssertIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  47294. WOLFSSL_SUCCESS);
  47295. AssertIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  47296. AssertIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  47297. WOLFSSL_SUCCESS);
  47298. AssertIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  47299. WOLFSSL_SUCCESS);
  47300. AssertIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  47301. AssertIntNE(XMEMCMP(data1, data2, dlen), 0);
  47302. AssertIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  47303. printf(resultFmt, passed);
  47304. #endif
  47305. }
  47306. static void test_wolfSSL_FIPS_mode(void)
  47307. {
  47308. #if defined(OPENSSL_ALL)
  47309. printf(testingFmt, "test_wolfSSL_FIPS_mode()");
  47310. #ifdef HAVE_FIPS
  47311. AssertIntEQ(wolfSSL_FIPS_mode(), 1);
  47312. AssertIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  47313. AssertIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  47314. #else
  47315. AssertIntEQ(wolfSSL_FIPS_mode(), 0);
  47316. AssertIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  47317. AssertIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  47318. #endif
  47319. printf(resultFmt, passed);
  47320. #endif
  47321. }
  47322. /*----------------------------------------------------------------------------*
  47323. | Main
  47324. *----------------------------------------------------------------------------*/
  47325. void ApiTest(void)
  47326. {
  47327. printf("\n-----------------Porting tests------------------\n");
  47328. AssertTrue(test_fileAccess());
  47329. printf(" Begin API Tests\n");
  47330. AssertIntEQ(test_wolfSSL_Init(), WOLFSSL_SUCCESS);
  47331. /* wolfcrypt initialization tests */
  47332. test_wolfSSL_Method_Allocators();
  47333. #ifndef NO_WOLFSSL_SERVER
  47334. test_wolfSSL_CTX_new(wolfSSLv23_server_method());
  47335. #endif
  47336. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  47337. (!defined(NO_RSA) || defined(HAVE_ECC))
  47338. test_for_double_Free();
  47339. #endif
  47340. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  47341. test_wolfSSL_get_finished();
  47342. test_wolfSSL_CTX_add_session();
  47343. #endif
  47344. test_SSL_CIPHER_get_xxx();
  47345. test_wolfSSL_ERR_strings();
  47346. test_wolfSSL_EVP_shake128();
  47347. test_wolfSSL_EVP_shake256();
  47348. test_EVP_blake2();
  47349. test_EVP_MD_do_all();
  47350. test_OBJ_NAME_do_all();
  47351. test_wolfSSL_CTX_use_certificate_file();
  47352. AssertIntEQ(test_wolfSSL_CTX_use_certificate_buffer(), WOLFSSL_SUCCESS);
  47353. test_wolfSSL_CTX_use_PrivateKey_file();
  47354. test_wolfSSL_CTX_load_verify_locations();
  47355. test_wolfSSL_CertManagerCheckOCSPResponse();
  47356. test_wolfSSL_CertManagerLoadCABuffer();
  47357. test_wolfSSL_CertManagerGetCerts();
  47358. test_wolfSSL_CertManagerSetVerify();
  47359. test_wolfSSL_CertManagerNameConstraint();
  47360. test_wolfSSL_CertManagerNameConstraint2();
  47361. test_wolfSSL_CertManagerNameConstraint3();
  47362. test_wolfSSL_CertManagerNameConstraint4();
  47363. test_wolfSSL_CertManagerNameConstraint5();
  47364. test_wolfSSL_CertManagerCRL();
  47365. test_wolfSSL_CTX_load_verify_locations_ex();
  47366. test_wolfSSL_CTX_load_verify_buffer_ex();
  47367. test_wolfSSL_CTX_load_verify_chain_buffer_format();
  47368. test_wolfSSL_CTX_add1_chain_cert();
  47369. test_wolfSSL_CTX_use_certificate_chain_file_format();
  47370. test_wolfSSL_CTX_trust_peer_cert();
  47371. test_wolfSSL_CTX_SetTmpDH_file();
  47372. test_wolfSSL_CTX_SetTmpDH_buffer();
  47373. test_wolfSSL_CTX_SetMinMaxDhKey_Sz();
  47374. test_wolfSSL_CTX_der_load_verify_locations();
  47375. test_wolfSSL_CTX_enable_disable();
  47376. test_wolfSSL_CTX_ticket_API();
  47377. test_server_wolfSSL_new();
  47378. test_client_wolfSSL_new();
  47379. test_wolfSSL_SetTmpDH_file();
  47380. test_wolfSSL_SetTmpDH_buffer();
  47381. test_wolfSSL_SetMinMaxDhKey_Sz();
  47382. test_SetTmpEC_DHE_Sz();
  47383. test_wolfSSL_CTX_get0_privatekey();
  47384. test_wolfSSL_dtls_set_mtu();
  47385. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  47386. defined(HAVE_IO_TESTS_DEPENDENCIES)
  47387. test_wolfSSL_read_write();
  47388. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  47389. test_wolfSSL_reuse_WOLFSSLobj();
  47390. #endif
  47391. test_wolfSSL_CTX_verifyDepth_ServerClient();
  47392. test_wolfSSL_dtls_export();
  47393. test_wolfSSL_tls_export();
  47394. #endif
  47395. AssertIntEQ(test_wolfSSL_SetMinVersion(), WOLFSSL_SUCCESS);
  47396. AssertIntEQ(test_wolfSSL_CTX_SetMinVersion(), WOLFSSL_SUCCESS);
  47397. /* TLS extensions tests */
  47398. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  47399. test_wolfSSL_UseSNI();
  47400. #endif
  47401. test_wolfSSL_UseTrustedCA();
  47402. test_wolfSSL_UseMaxFragment();
  47403. test_wolfSSL_UseTruncatedHMAC();
  47404. test_wolfSSL_UseSupportedCurve();
  47405. test_wolfSSL_UseALPN();
  47406. test_wolfSSL_DisableExtendedMasterSecret();
  47407. test_wolfSSL_wolfSSL_UseSecureRenegotiation();
  47408. /* X509 tests */
  47409. test_wolfSSL_X509_NAME_get_entry();
  47410. test_wolfSSL_PKCS12();
  47411. test_wolfSSL_no_password_cb();
  47412. test_wolfSSL_PKCS8();
  47413. test_wolfSSL_PKCS8_ED25519();
  47414. test_wolfSSL_PKCS8_ED448();
  47415. test_wolfSSL_PKCS5();
  47416. test_wolfSSL_URI();
  47417. test_wolfSSL_TBS();
  47418. test_wolfSSL_X509_verify();
  47419. test_wolfSSL_X509_TLS_version();
  47420. test_wc_PemToDer();
  47421. test_wc_AllocDer();
  47422. test_wc_CertPemToDer();
  47423. test_wc_PubKeyPemToDer();
  47424. test_wc_PemPubKeyToDer();
  47425. test_wc_GetPubKeyDerFromCert();
  47426. test_wc_CheckCertSigPubKey();
  47427. /*OCSP Stapling. */
  47428. AssertIntEQ(test_wolfSSL_UseOCSPStapling(), WOLFSSL_SUCCESS);
  47429. AssertIntEQ(test_wolfSSL_UseOCSPStaplingV2(), WOLFSSL_SUCCESS);
  47430. /* Multicast */
  47431. test_wolfSSL_mcast();
  47432. /* compatibility tests */
  47433. test_wolfSSL_lhash();
  47434. test_wolfSSL_X509_NAME();
  47435. test_wolfSSL_X509_NAME_hash();
  47436. test_wolfSSL_X509_NAME_print_ex();
  47437. #ifndef NO_BIO
  47438. test_wolfSSL_X509_INFO_multiple_info();
  47439. test_wolfSSL_X509_INFO();
  47440. #endif
  47441. test_wolfSSL_X509_subject_name_hash();
  47442. test_wolfSSL_X509_issuer_name_hash();
  47443. test_wolfSSL_X509_check_host();
  47444. test_wolfSSL_X509_check_email();
  47445. test_wolfSSL_DES();
  47446. test_wolfSSL_certs();
  47447. test_wolfSSL_X509_check_private_key();
  47448. test_wolfSSL_ASN1_TIME_print();
  47449. test_wolfSSL_ASN1_UTCTIME_print();
  47450. test_wolfSSL_ASN1_TIME_diff_compare();
  47451. test_wolfSSL_ASN1_GENERALIZEDTIME_free();
  47452. test_wolfSSL_private_keys();
  47453. test_wolfSSL_PEM_read_PrivateKey();
  47454. test_wolfSSL_PEM_PrivateKey();
  47455. #ifndef NO_BIO
  47456. test_wolfSSL_PEM_bio_RSAKey();
  47457. test_wolfSSL_PEM_bio_DSAKey();
  47458. test_wolfSSL_PEM_bio_ECKey();
  47459. test_wolfSSL_PEM_RSAPrivateKey();
  47460. test_wolfSSL_PEM_PUBKEY();
  47461. #endif
  47462. test_DSA_do_sign_verify();
  47463. test_wolfSSL_tmp_dh();
  47464. test_wolfSSL_ctrl();
  47465. test_wolfSSL_EVP_MD_size();
  47466. test_wolfSSL_EVP_MD_pkey_type();
  47467. test_wolfSSL_EVP_Digest();
  47468. test_wolfSSL_EVP_Digest_all();
  47469. test_wolfSSL_EVP_PKEY_new_mac_key();
  47470. test_wolfSSL_EVP_MD_hmac_signing();
  47471. test_wolfSSL_EVP_MD_rsa_signing();
  47472. test_wolfSSL_EVP_MD_ecc_signing();
  47473. test_wolfSSL_EVP_PKEY_print_public();
  47474. test_wolfSSL_EVP_ENCODE_CTX_new();
  47475. test_wolfSSL_EVP_ENCODE_CTX_free();
  47476. test_wolfSSL_EVP_EncodeInit();
  47477. test_wolfSSL_EVP_EncodeUpdate();
  47478. test_wolfSSL_EVP_EncodeFinal();
  47479. test_wolfSSL_EVP_DecodeInit();
  47480. test_wolfSSL_EVP_DecodeUpdate();
  47481. test_wolfSSL_EVP_DecodeFinal();
  47482. test_wolfSSL_CTX_add_extra_chain_cert();
  47483. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  47484. test_wolfSSL_ERR_peek_last_error_line();
  47485. #endif
  47486. #ifndef NO_BIO
  47487. test_wolfSSL_ERR_print_errors_cb();
  47488. AssertFalse(test_wolfSSL_GetLoggingCb());
  47489. AssertFalse(test_WOLFSSL_ERROR_MSG());
  47490. AssertFalse(test_wc_ERR_remove_state());
  47491. AssertFalse(test_wc_ERR_print_errors_fp());
  47492. #endif
  47493. test_wolfSSL_set_options();
  47494. test_wolfSSL_sk_SSL_CIPHER();
  47495. test_wolfSSL_set1_curves_list();
  47496. test_wolfSSL_set1_sigalgs_list();
  47497. test_wolfSSL_PKCS7_certs();
  47498. test_wolfSSL_X509_STORE_CTX();
  47499. test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup();
  47500. test_wolfSSL_X509_STORE_CTX_get0_current_issuer();
  47501. test_wolfSSL_msgCb();
  47502. test_wolfSSL_either_side();
  47503. test_wolfSSL_DTLS_either_side();
  47504. test_generate_cookie();
  47505. test_wolfSSL_X509_STORE_set_flags();
  47506. test_wolfSSL_X509_LOOKUP_load_file();
  47507. test_wolfSSL_X509_Name_canon();
  47508. test_wolfSSL_X509_LOOKUP_ctrl_file();
  47509. test_wolfSSL_X509_LOOKUP_ctrl_hash_dir();
  47510. test_wolfSSL_X509_NID();
  47511. test_wolfSSL_X509_STORE_CTX_set_time();
  47512. test_wolfSSL_get0_param();
  47513. test_wolfSSL_X509_VERIFY_PARAM_set1_host();
  47514. test_wolfSSL_X509_VERIFY_PARAM_set1_ip();
  47515. test_wolfSSL_X509_STORE_CTX_get0_store();
  47516. test_wolfSSL_X509_STORE();
  47517. test_wolfSSL_X509_STORE_load_locations();
  47518. test_X509_STORE_get0_objects();
  47519. test_wolfSSL_X509_load_crl_file();
  47520. test_wolfSSL_BN();
  47521. test_wolfSSL_CTX_get0_set1_param();
  47522. #ifndef NO_BIO
  47523. test_wolfSSL_PEM_read_bio();
  47524. test_wolfSSL_BIO();
  47525. #endif
  47526. test_wolfSSL_ASN1_STRING();
  47527. test_wolfSSL_ASN1_BIT_STRING();
  47528. test_wolfSSL_a2i_ASN1_INTEGER();
  47529. test_wolfSSL_a2i_IPADDRESS();
  47530. test_wolfSSL_X509();
  47531. test_wolfSSL_X509_VERIFY_PARAM();
  47532. test_wolfSSL_X509_sign();
  47533. test_wolfSSL_X509_sign2();
  47534. test_wolfSSL_X509_get0_tbs_sigalg();
  47535. test_wolfSSL_X509_ALGOR_get0();
  47536. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  47537. test_wolfSSL_check_domain();
  47538. #endif
  47539. test_wolfSSL_X509_get_X509_PUBKEY();
  47540. test_wolfSSL_X509_PUBKEY_RSA();
  47541. test_wolfSSL_X509_PUBKEY_EC();
  47542. test_wolfSSL_X509_PUBKEY_DSA();
  47543. test_wolfSSL_RAND();
  47544. test_wolfSSL_BUF();
  47545. test_wolfSSL_set_tlsext_status_type();
  47546. test_wolfSSL_ASN1_TIME_adj();
  47547. test_wolfSSL_ASN1_TIME_to_tm();
  47548. test_wolfSSL_X509_cmp_time();
  47549. test_wolfSSL_X509_time_adj();
  47550. test_wolfSSL_CTX_set_client_CA_list();
  47551. test_wolfSSL_CTX_add_client_CA();
  47552. test_wolfSSL_CTX_set_srp_username();
  47553. test_wolfSSL_CTX_set_srp_password();
  47554. test_wolfSSL_CTX_set_keylog_callback();
  47555. test_wolfSSL_CTX_get_keylog_callback();
  47556. test_wolfSSL_Tls12_Key_Logging_test();
  47557. test_wolfSSL_Tls13_Key_Logging_test();
  47558. test_wolfSSL_Tls13_postauth();
  47559. test_wolfSSL_CTX_set_ecdh_auto();
  47560. test_wolfSSL_set_minmax_proto_version();
  47561. test_wolfSSL_THREADID_hash();
  47562. test_wolfSSL_RAND_set_rand_method();
  47563. test_wolfSSL_RAND_bytes();
  47564. test_wolfSSL_BN_rand();
  47565. test_wolfSSL_pseudo_rand();
  47566. test_wolfSSL_PKCS8_Compat();
  47567. test_wolfSSL_PKCS8_d2i();
  47568. test_error_queue_per_thread();
  47569. test_wolfSSL_ERR_put_error();
  47570. #ifndef NO_BIO
  47571. test_wolfSSL_ERR_print_errors();
  47572. #endif
  47573. test_wolfSSL_HMAC();
  47574. test_wolfSSL_CMAC();
  47575. test_wolfSSL_OBJ();
  47576. test_wolfSSL_i2a_ASN1_OBJECT();
  47577. test_wolfSSL_OBJ_cmp();
  47578. test_wolfSSL_OBJ_txt2nid();
  47579. test_wolfSSL_OBJ_txt2obj();
  47580. test_wolfSSL_i2t_ASN1_OBJECT();
  47581. test_wolfSSL_PEM_write_bio_X509();
  47582. test_wolfSSL_X509_NAME_ENTRY();
  47583. test_wolfSSL_X509_set_name();
  47584. test_wolfSSL_X509_set_notAfter();
  47585. test_wolfSSL_X509_set_notBefore();
  47586. test_wolfSSL_X509_set_version();
  47587. #ifndef NO_BIO
  47588. test_wolfSSL_BIO_gets();
  47589. test_wolfSSL_BIO_puts();
  47590. test_wolfSSL_BIO_dump();
  47591. test_wolfSSL_BIO_should_retry();
  47592. test_wolfSSL_d2i_PUBKEY();
  47593. test_wolfSSL_BIO_write();
  47594. test_wolfSSL_BIO_connect();
  47595. test_wolfSSL_BIO_accept();
  47596. test_wolfSSL_BIO_printf();
  47597. test_wolfSSL_BIO_f_md();
  47598. test_wolfSSL_BIO_up_ref();
  47599. test_wolfSSL_BIO_tls();
  47600. #endif
  47601. test_wolfSSL_cert_cb();
  47602. test_wolfSSL_SESSION();
  47603. test_wolfSSL_CTX_sess_set_remove_cb();
  47604. test_wolfSSL_ticket_keys();
  47605. test_wolfSSL_DES_ecb_encrypt();
  47606. test_wolfSSL_sk_GENERAL_NAME();
  47607. test_wolfSSL_GENERAL_NAME_print();
  47608. test_wolfSSL_sk_DIST_POINT();
  47609. test_wolfSSL_MD4();
  47610. test_wolfSSL_verify_mode();
  47611. test_wolfSSL_verify_depth();
  47612. test_wolfSSL_HMAC_CTX();
  47613. test_wolfSSL_msg_callback();
  47614. test_wolfSSL_SHA();
  47615. test_wolfSSL_DH_1536_prime();
  47616. test_wolfSSL_DH_get_2048_256();
  47617. test_wolfSSL_PEM_write_DHparams();
  47618. test_wolfSSL_PEM_read_DHparams();
  47619. test_wolfSSL_AES_ecb_encrypt();
  47620. test_wolfSSL_MD5();
  47621. test_wolfSSL_MD5_Transform();
  47622. test_wolfSSL_SHA_Transform();
  47623. test_wolfSSL_SHA256();
  47624. test_wolfSSL_SHA256_Transform();
  47625. test_wolfSSL_SHA224();
  47626. test_wolfSSL_SHA512_Transform();
  47627. test_wolfSSL_X509_get_serialNumber();
  47628. test_wolfSSL_X509_CRL();
  47629. test_wolfSSL_d2i_X509_REQ();
  47630. test_wolfSSL_PEM_read_X509();
  47631. test_wolfSSL_PEM_read();
  47632. #ifndef NO_BIO
  47633. test_wolfSSL_PEM_X509_INFO_read_bio();
  47634. test_wolfSSL_PEM_read_bio_ECPKParameters();
  47635. #endif
  47636. test_wolfSSL_X509_STORE_get1_certs();
  47637. test_wolfSSL_X509_NAME_ENTRY_get_object();
  47638. test_wolfSSL_OpenSSL_add_all_algorithms();
  47639. test_wolfSSL_OPENSSL_hexstr2buf();
  47640. test_wolfSSL_ASN1_STRING_print_ex();
  47641. test_wolfSSL_ASN1_TIME_to_generalizedtime();
  47642. test_wolfSSL_ASN1_INTEGER_get_set();
  47643. test_wolfSSL_d2i_ASN1_INTEGER();
  47644. test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS();
  47645. test_wolfSSL_i2c_ASN1_INTEGER();
  47646. test_wolfSSL_X509_check_ca();
  47647. test_wolfSSL_X509_check_ip_asc();
  47648. test_wolfSSL_make_cert();
  47649. test_wolfSSL_DES_ncbc();
  47650. test_wolfSSL_AES_cbc_encrypt();
  47651. test_wolfSSL_CRYPTO_cts128();
  47652. test_wolfssl_EVP_aes_gcm_AAD_2_parts();
  47653. test_wolfssl_EVP_aes_gcm();
  47654. test_wolfssl_EVP_chacha20_poly1305();
  47655. test_wolfSSL_EVP_PKEY_hkdf();
  47656. test_wolfSSL_PKEY_up_ref();
  47657. test_wolfSSL_EVP_Cipher_extra();
  47658. test_wolfSSL_d2i_and_i2d_PublicKey();
  47659. test_wolfSSL_d2i_and_i2d_DSAparams();
  47660. test_wolfSSL_i2d_PrivateKey();
  47661. test_wolfSSL_OCSP_id_get0_info();
  47662. test_wolfSSL_i2d_OCSP_CERTID();
  47663. test_wolfSSL_OCSP_id_cmp();
  47664. test_wolfSSL_OCSP_SINGLERESP_get0_id();
  47665. test_wolfSSL_OCSP_single_get0_status();
  47666. test_wolfSSL_OCSP_resp_count();
  47667. test_wolfSSL_OCSP_resp_get0();
  47668. test_wolfSSL_EVP_PKEY_derive();
  47669. test_wolfSSL_EVP_PBE_scrypt();
  47670. test_CONF_modules_xxx();
  47671. test_CRYPTO_set_dynlock_xxx();
  47672. test_CRYPTO_THREADID_xxx();
  47673. test_ENGINE_cleanup();
  47674. test_wolfSSL_EC_KEY_set_group();
  47675. test_wolfSSL_EC_KEY_set_conv_form();
  47676. test_wolfSSL_EC_KEY_print_fp();
  47677. #if defined(OPENSSL_ALL)
  47678. test_wolfSSL_X509_PUBKEY_get();
  47679. test_wolfSSL_sk_CIPHER_description();
  47680. test_wolfSSL_get_ciphers_compat();
  47681. test_wolfSSL_d2i_DHparams();
  47682. test_wolfSSL_i2d_DHparams();
  47683. test_wolfSSL_ASN1_STRING_to_UTF8();
  47684. test_wolfSSL_ASN1_UNIVERSALSTRING_to_string();
  47685. test_wolfSSL_EC_KEY_dup();
  47686. test_wolfSSL_EVP_PKEY_set1_get1_DSA();
  47687. test_wolfSSL_DSA_SIG();
  47688. test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY();
  47689. test_wolfSSL_EVP_PKEY_set1_get1_DH();
  47690. test_wolfSSL_CTX_ctrl();
  47691. test_wolfSSL_DH_check();
  47692. test_wolfSSL_EVP_PKEY_assign();
  47693. test_wolfSSL_EVP_PKEY_base_id();
  47694. test_wolfSSL_EVP_PKEY_id();
  47695. test_wolfSSL_EVP_PKEY_paramgen();
  47696. test_wolfSSL_EVP_PKEY_keygen();
  47697. test_wolfSSL_EVP_PKEY_keygen_init();
  47698. test_wolfSSL_EVP_PKEY_missing_parameters();
  47699. test_wolfSSL_EVP_PKEY_copy_parameters();
  47700. test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits();
  47701. test_wolfSSL_EVP_CIPHER_CTX_iv_length();
  47702. test_wolfSSL_EVP_CIPHER_CTX_key_length();
  47703. test_wolfSSL_EVP_CIPHER_CTX_set_key_length();
  47704. test_wolfSSL_EVP_CIPHER_CTX_set_iv();
  47705. test_wolfSSL_EVP_PKEY_CTX_new_id();
  47706. test_wolfSSL_EVP_rc4();
  47707. test_wolfSSL_EVP_enc_null();
  47708. test_wolfSSL_EVP_rc2_cbc();
  47709. test_wolfSSL_EVP_mdc2();
  47710. test_wolfSSL_EVP_md4();
  47711. test_wolfSSL_EVP_aes_256_gcm();
  47712. test_wolfSSL_EVP_aes_192_gcm();
  47713. test_wolfSSL_EVP_ripemd160();
  47714. test_wolfSSL_EVP_get_digestbynid();
  47715. test_wolfSSL_EVP_MD_nid();
  47716. test_wolfSSL_EVP_PKEY_get0_EC_KEY();
  47717. test_wolfSSL_EVP_X_STATE();
  47718. test_wolfSSL_EVP_X_STATE_LEN();
  47719. test_wolfSSL_EVP_CIPHER_block_size();
  47720. test_wolfSSL_EVP_CIPHER_iv_length();
  47721. test_wolfSSL_EVP_SignInit_ex();
  47722. test_wolfSSL_EVP_DigestFinal_ex();
  47723. test_wolfSSL_EVP_PKEY_assign_DH();
  47724. test_wolfSSL_EVP_BytesToKey();
  47725. test_wolfSSL_EVP_PKEY_param_check();
  47726. test_wolfSSL_QT_EVP_PKEY_CTX_free();
  47727. test_evp_cipher_aes_gcm();
  47728. test_wolfSSL_OBJ_ln();
  47729. test_wolfSSL_OBJ_sn();
  47730. test_wolfSSL_TXT_DB();
  47731. test_wolfSSL_NCONF();
  47732. #endif /* OPENSSL_ALL */
  47733. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  47734. AssertIntEQ(test_wolfSSL_CTX_use_certificate_ASN1(), WOLFSSL_SUCCESS);
  47735. #ifndef NO_BIO
  47736. test_wolfSSL_d2i_PrivateKeys_bio();
  47737. #endif
  47738. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  47739. test_wolfSSL_X509_CA_num();
  47740. test_wolfSSL_X509_get_version();
  47741. #ifndef NO_BIO
  47742. test_wolfSSL_X509_print();
  47743. test_wolfSSL_BIO_get_len();
  47744. #endif
  47745. test_wolfSSL_RSA();
  47746. test_wolfSSL_RSA_DER();
  47747. test_wolfSSL_RSA_print();
  47748. #ifndef NO_RSA
  47749. test_wolfSSL_RSA_padding_add_PKCS1_PSS();
  47750. #endif
  47751. test_wolfSSL_RSA_sign_sha3();
  47752. test_wolfSSL_RSA_get0_key();
  47753. test_wolfSSL_RSA_meth();
  47754. test_wolfSSL_RSA_verify();
  47755. test_wolfSSL_RSA_sign();
  47756. test_wolfSSL_RSA_sign_ex();
  47757. test_wolfSSL_RSA_public_decrypt();
  47758. test_wolfSSL_RSA_private_encrypt();
  47759. test_wolfSSL_RSA_public_encrypt();
  47760. test_wolfSSL_RSA_private_decrypt();
  47761. test_wolfSSL_RSA_GenAdd();
  47762. test_wolfSSL_RSA_blinding_on();
  47763. test_wolfSSL_RSA_ex_data();
  47764. test_wolfSSL_RSA_LoadDer();
  47765. test_wolfSSL_RSA_To_Der();
  47766. test_wolfSSL_PEM_read_RSAPublicKey();
  47767. test_wolfSSL_PEM_write_RSA_PUBKEY();
  47768. test_wolfSSL_PEM_write_RSAPrivateKey();
  47769. test_wolfSSL_PEM_write_mem_RSAPrivateKey();
  47770. test_wolfSSL_X509V3_EXT_get();
  47771. test_wolfSSL_X509V3_EXT_nconf();
  47772. test_wolfSSL_X509V3_EXT();
  47773. test_wolfSSL_X509_get_extension_flags();
  47774. test_wolfSSL_X509_get_ext();
  47775. test_wolfSSL_X509_get_ext_by_NID();
  47776. test_wolfSSL_X509_get_ext_subj_alt_name();
  47777. test_wolfSSL_X509_get_ext_count();
  47778. test_wolfSSL_X509_EXTENSION_new();
  47779. test_wolfSSL_X509_EXTENSION_get_object();
  47780. test_wolfSSL_X509_EXTENSION_get_data();
  47781. test_wolfSSL_X509_EXTENSION_get_critical();
  47782. test_wolfSSL_X509V3_EXT_print();
  47783. test_wolfSSL_X509_cmp();
  47784. #ifndef NO_BIO
  47785. test_wolfSSL_ASN1_STRING_print();
  47786. #endif
  47787. test_wolfSSL_ASN1_get_object();
  47788. test_openssl_generate_key_and_cert();
  47789. test_wolfSSL_EC_get_builtin_curves();
  47790. test_wolfSSL_CRYPTO_memcmp();
  47791. test_wolfSSL_read_detect_TCP_disconnect();
  47792. /* test the no op functions for compatibility */
  47793. test_no_op_functions();
  47794. /* OpenSSL EVP_PKEY API tests */
  47795. test_EVP_PKEY_rsa();
  47796. test_wolfSSL_EVP_PKEY_encrypt();
  47797. test_wolfSSL_EVP_PKEY_sign_verify();
  47798. test_EVP_PKEY_ec();
  47799. test_EVP_PKEY_cmp();
  47800. /* OpenSSL error API tests */
  47801. test_ERR_load_crypto_strings();
  47802. /* OpenSSL sk_X509 API test */
  47803. test_sk_X509();
  47804. /* OpenSSL sk_X509_CRL API test */
  47805. test_sk_X509_CRL();
  47806. /* OpenSSL X509 API test */
  47807. test_X509_get_signature_nid();
  47808. /* OpenSSL X509 REQ API test */
  47809. test_X509_REQ();
  47810. /* OpenSSL PKCS7 API test */
  47811. test_wolfssl_PKCS7();
  47812. test_wolfSSL_PKCS7_sign();
  47813. test_wolfSSL_PKCS7_SIGNED_new();
  47814. #ifndef NO_BIO
  47815. test_wolfSSL_PEM_write_bio_PKCS7();
  47816. #ifdef HAVE_SMIME
  47817. test_wolfSSL_SMIME_read_PKCS7();
  47818. test_wolfSSL_SMIME_write_PKCS7();
  47819. #endif
  47820. #endif
  47821. /* wolfCrypt ASN tests */
  47822. test_wc_CreateEncryptedPKCS8Key();
  47823. test_wc_GetPkcs8TraditionalOffset();
  47824. test_wc_SetSubjectRaw();
  47825. test_wc_GetSubjectRaw();
  47826. test_wc_SetIssuerRaw();
  47827. test_wc_SetIssueBuffer();
  47828. test_wc_SetSubjectKeyId();
  47829. test_wc_SetSubject();
  47830. test_CheckCertSignature();
  47831. test_wc_ParseCert();
  47832. /* wolfCrypt ECC tests */
  47833. test_wc_ecc_get_curve_size_from_name();
  47834. test_wc_ecc_get_curve_id_from_name();
  47835. test_wc_ecc_get_curve_id_from_params();
  47836. #ifdef WOLFSSL_TLS13
  47837. /* TLS v1.3 API tests */
  47838. test_tls13_apis();
  47839. #endif
  47840. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  47841. !defined(WOLFSSL_NO_CLIENT_AUTH))
  47842. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  47843. /* Bad certificate signature tests */
  47844. AssertIntEQ(test_EccSigFailure_cm(), ASN_SIG_CONFIRM_E);
  47845. AssertIntEQ(test_RsaSigFailure_cm(), ASN_SIG_CONFIRM_E);
  47846. #endif /* NO_CERTS */
  47847. #if defined(HAVE_PK_CALLBACKS) && (!defined(WOLFSSL_NO_TLS12) || \
  47848. !defined(NO_OLD_TLS))
  47849. /* public key callback tests */
  47850. test_DhCallbacks();
  47851. #endif
  47852. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  47853. test_export_keying_material();
  47854. #endif /* HAVE_KEYING_MATERIAL */
  47855. test_wolfSSL_CTX_get_min_proto_version();
  47856. test_wolfSSL_security_level();
  47857. test_wolfSSL_SSL_in_init();
  47858. test_wolfSSL_EC_curve();
  47859. test_wolfSSL_CTX_set_timeout();
  47860. test_wolfSSL_OpenSSL_version();
  47861. test_wolfSSL_set_psk_use_session_callback();
  47862. test_CONF_CTX_FILE();
  47863. test_CONF_CTX_CMDLINE();
  47864. test_wolfSSL_CRYPTO_get_ex_new_index();
  47865. test_wolfSSL_DH();
  47866. /*wolfcrypt */
  47867. printf("\n-----------------wolfcrypt unit tests------------------\n");
  47868. AssertFalse(test_wolfCrypt_Init());
  47869. AssertFalse(test_wc_InitMd5());
  47870. AssertFalse(test_wc_Md5Update());
  47871. AssertFalse(test_wc_Md5Final());
  47872. AssertFalse(test_wc_InitSha());
  47873. AssertFalse(test_wc_ShaUpdate());
  47874. AssertFalse(test_wc_ShaFinal());
  47875. AssertFalse(test_wc_InitSha256());
  47876. AssertFalse(test_wc_Sha256Update());
  47877. AssertFalse(test_wc_Sha256Final());
  47878. AssertFalse(test_wc_Sha256FinalRaw());
  47879. AssertFalse(test_wc_Sha256GetFlags());
  47880. AssertFalse(test_wc_Sha256Free());
  47881. AssertFalse(test_wc_Sha256GetHash());
  47882. AssertFalse(test_wc_Sha256Copy());
  47883. AssertFalse(test_wc_InitSha512());
  47884. AssertFalse(test_wc_Sha512Update());
  47885. AssertFalse(test_wc_Sha512Final());
  47886. AssertFalse(test_wc_Sha512GetFlags());
  47887. AssertFalse(test_wc_Sha512FinalRaw());
  47888. AssertFalse(test_wc_Sha512Free());
  47889. AssertFalse(test_wc_Sha512GetHash());
  47890. AssertFalse(test_wc_Sha512Copy());
  47891. AssertFalse(test_wc_InitSha512_224());
  47892. AssertFalse(test_wc_Sha512_224Update());
  47893. AssertFalse(test_wc_Sha512_224Final());
  47894. AssertFalse(test_wc_Sha512_224GetFlags());
  47895. AssertFalse(test_wc_Sha512_224FinalRaw());
  47896. AssertFalse(test_wc_Sha512_224Free());
  47897. AssertFalse(test_wc_Sha512_224GetHash());
  47898. AssertFalse(test_wc_Sha512_224Copy());
  47899. AssertFalse(test_wc_InitSha512_256());
  47900. AssertFalse(test_wc_Sha512_256Update());
  47901. AssertFalse(test_wc_Sha512_256Final());
  47902. AssertFalse(test_wc_Sha512_256GetFlags());
  47903. AssertFalse(test_wc_Sha512_256FinalRaw());
  47904. AssertFalse(test_wc_Sha512_256Free());
  47905. AssertFalse(test_wc_Sha512_256GetHash());
  47906. AssertFalse(test_wc_Sha512_256Copy());
  47907. AssertFalse(test_wc_InitSha384());
  47908. AssertFalse(test_wc_Sha384Update());
  47909. AssertFalse(test_wc_Sha384Final());
  47910. AssertFalse(test_wc_Sha384GetFlags());
  47911. AssertFalse(test_wc_Sha384FinalRaw());
  47912. AssertFalse(test_wc_Sha384Free());
  47913. AssertFalse(test_wc_Sha384GetHash());
  47914. AssertFalse(test_wc_Sha384Copy());
  47915. AssertFalse(test_wc_InitSha224());
  47916. AssertFalse(test_wc_Sha224Update());
  47917. AssertFalse(test_wc_Sha224Final());
  47918. AssertFalse(test_wc_Sha224SetFlags());
  47919. AssertFalse(test_wc_Sha224GetFlags());
  47920. AssertFalse(test_wc_Sha224Free());
  47921. AssertFalse(test_wc_Sha224GetHash());
  47922. AssertFalse(test_wc_Sha224Copy());
  47923. AssertFalse(test_wc_InitBlake2b());
  47924. AssertFalse(test_wc_InitBlake2b_WithKey());
  47925. AssertFalse(test_wc_InitBlake2s_WithKey());
  47926. AssertFalse(test_wc_InitRipeMd());
  47927. AssertFalse(test_wc_RipeMdUpdate());
  47928. AssertFalse(test_wc_RipeMdFinal());
  47929. AssertIntEQ(test_wc_InitSha3(), 0);
  47930. AssertIntEQ(testing_wc_Sha3_Update(), 0);
  47931. AssertIntEQ(test_wc_Sha3_224_Final(), 0);
  47932. AssertIntEQ(test_wc_Sha3_256_Final(), 0);
  47933. AssertIntEQ(test_wc_Sha3_384_Final(), 0);
  47934. AssertIntEQ(test_wc_Sha3_512_Final(), 0);
  47935. AssertIntEQ(test_wc_Sha3_224_Copy(), 0);
  47936. AssertIntEQ(test_wc_Sha3_256_Copy(), 0);
  47937. AssertIntEQ(test_wc_Sha3_384_Copy(), 0);
  47938. AssertIntEQ(test_wc_Sha3_512_Copy(), 0);
  47939. AssertIntEQ(test_wc_Sha3_GetFlags(), 0);
  47940. AssertIntEQ(test_wc_InitShake256(), 0);
  47941. AssertIntEQ(testing_wc_Shake256_Update(), 0);
  47942. AssertIntEQ(test_wc_Shake256_Final(), 0);
  47943. AssertIntEQ(test_wc_Shake256_Copy(), 0);
  47944. AssertIntEQ(test_wc_Shake256Hash(), 0);
  47945. AssertFalse(test_wc_Md5HmacSetKey());
  47946. AssertFalse(test_wc_Md5HmacUpdate());
  47947. AssertFalse(test_wc_Md5HmacFinal());
  47948. AssertFalse(test_wc_ShaHmacSetKey());
  47949. AssertFalse(test_wc_ShaHmacUpdate());
  47950. AssertFalse(test_wc_ShaHmacFinal());
  47951. AssertFalse(test_wc_Sha224HmacSetKey());
  47952. AssertFalse(test_wc_Sha224HmacUpdate());
  47953. AssertFalse(test_wc_Sha224HmacFinal());
  47954. AssertFalse(test_wc_Sha256HmacSetKey());
  47955. AssertFalse(test_wc_Sha256HmacUpdate());
  47956. AssertFalse(test_wc_Sha256HmacFinal());
  47957. AssertFalse(test_wc_Sha384HmacSetKey());
  47958. AssertFalse(test_wc_Sha384HmacUpdate());
  47959. AssertFalse(test_wc_Sha384HmacFinal());
  47960. AssertIntEQ(test_wc_HashInit(), 0);
  47961. AssertIntEQ(test_wc_HashSetFlags(), 0);
  47962. AssertIntEQ(test_wc_HashGetFlags(), 0);
  47963. AssertIntEQ(test_wc_InitCmac(), 0);
  47964. AssertIntEQ(test_wc_CmacUpdate(), 0);
  47965. AssertIntEQ(test_wc_CmacFinal(), 0);
  47966. AssertIntEQ(test_wc_AesCmacGenerate(), 0);
  47967. AssertIntEQ(test_wc_AesGcmStream(), 0);
  47968. AssertIntEQ(test_wc_Des3_SetIV(), 0);
  47969. AssertIntEQ(test_wc_Des3_SetKey(), 0);
  47970. AssertIntEQ(test_wc_Des3_CbcEncryptDecrypt(), 0);
  47971. AssertIntEQ(test_wc_Des3_CbcEncryptDecryptWithKey(), 0);
  47972. AssertIntEQ(test_wc_Des3_EcbEncrypt(), 0);
  47973. AssertIntEQ(test_wc_Chacha_SetKey(), 0);
  47974. AssertIntEQ(test_wc_Chacha_Process(), 0);
  47975. AssertIntEQ(test_wc_ChaCha20Poly1305_aead(), 0);
  47976. AssertIntEQ(test_wc_Poly1305SetKey(), 0);
  47977. AssertIntEQ(test_wc_CamelliaSetKey(), 0);
  47978. AssertIntEQ(test_wc_CamelliaSetIV(), 0);
  47979. AssertIntEQ(test_wc_CamelliaEncryptDecryptDirect(), 0);
  47980. AssertIntEQ(test_wc_CamelliaCbcEncryptDecrypt(), 0);
  47981. AssertIntEQ(test_wc_Arc4SetKey(), 0);
  47982. AssertIntEQ(test_wc_Arc4Process(), 0);
  47983. AssertIntEQ(test_wc_Rc2SetKey(), 0);
  47984. AssertIntEQ(test_wc_Rc2SetIV(), 0);
  47985. AssertIntEQ(test_wc_Rc2EcbEncryptDecrypt(), 0);
  47986. AssertIntEQ(test_wc_Rc2CbcEncryptDecrypt(), 0);
  47987. AssertIntEQ(test_wc_AesSetKey(), 0);
  47988. AssertIntEQ(test_wc_AesSetIV(), 0);
  47989. AssertIntEQ(test_wc_AesCbcEncryptDecrypt(), 0);
  47990. AssertIntEQ(test_wc_AesCtrEncryptDecrypt(), 0);
  47991. AssertIntEQ(test_wc_AesGcmSetKey(), 0);
  47992. AssertIntEQ(test_wc_AesGcmEncryptDecrypt(), 0);
  47993. AssertIntEQ(test_wc_GmacSetKey(), 0);
  47994. AssertIntEQ(test_wc_GmacUpdate(), 0);
  47995. AssertIntEQ(test_wc_InitRsaKey(), 0);
  47996. AssertIntEQ(test_wc_RsaPrivateKeyDecode(), 0);
  47997. AssertIntEQ(test_wc_RsaPublicKeyDecode(), 0);
  47998. AssertIntEQ(test_wc_RsaPublicKeyDecodeRaw(), 0);
  47999. AssertIntEQ(test_wc_MakeRsaKey(), 0);
  48000. AssertIntEQ(test_wc_SetKeyUsage (), 0);
  48001. AssertIntEQ(test_wc_CheckProbablePrime (), 0);
  48002. AssertIntEQ(test_wc_RsaPSS_Verify (), 0);
  48003. AssertIntEQ(test_wc_RsaPSS_VerifyCheck (), 0);
  48004. AssertIntEQ(test_wc_RsaPSS_VerifyCheckInline (), 0);
  48005. AssertIntEQ(test_wc_SetMutexCb(), 0);
  48006. AssertIntEQ(test_wc_LockMutex_ex(), 0);
  48007. AssertIntEQ(test_wc_RsaKeyToDer(), 0);
  48008. AssertIntEQ(test_wc_RsaKeyToPublicDer(), 0);
  48009. AssertIntEQ(test_wc_RsaPublicEncryptDecrypt(), 0);
  48010. AssertIntEQ(test_wc_RsaPublicEncryptDecrypt_ex(), 0);
  48011. AssertIntEQ(test_wc_RsaEncryptSize(), 0);
  48012. AssertIntEQ(test_wc_RsaSSL_SignVerify(), 0);
  48013. AssertIntEQ(test_wc_RsaFlattenPublicKey(), 0);
  48014. AssertIntEQ(test_RsaDecryptBoundsCheck(), 0);
  48015. AssertIntEQ(test_wc_AesCcmSetKey(), 0);
  48016. AssertIntEQ(test_wc_AesCcmEncryptDecrypt(), 0);
  48017. AssertIntEQ(test_wc_InitDsaKey(), 0);
  48018. AssertIntEQ(test_wc_DsaSignVerify(), 0);
  48019. AssertIntEQ(test_wc_DsaPublicPrivateKeyDecode(), 0);
  48020. AssertIntEQ(test_wc_MakeDsaKey(), 0);
  48021. AssertIntEQ(test_wc_DsaKeyToDer(), 0);
  48022. AssertIntEQ(test_wc_DsaKeyToPublicDer(), 0);
  48023. AssertIntEQ(test_wc_DsaImportParamsRaw(), 0);
  48024. AssertIntEQ(test_wc_DsaImportParamsRawCheck(), 0);
  48025. AssertIntEQ(test_wc_DsaExportParamsRaw(), 0);
  48026. AssertIntEQ(test_wc_DsaExportKeyRaw(), 0);
  48027. AssertIntEQ(test_wc_SignatureGetSize_ecc(), 0);
  48028. AssertIntEQ(test_wc_SignatureGetSize_rsa(), 0);
  48029. wolfCrypt_Cleanup();
  48030. #ifdef OPENSSL_EXTRA
  48031. /*wolfSSL_EVP_get_cipherbynid test*/
  48032. test_wolfSSL_EVP_get_cipherbynid();
  48033. test_wolfSSL_EVP_CIPHER_CTX();
  48034. test_wolfSSL_EC();
  48035. test_wolfSSL_ECDSA_SIG();
  48036. test_ECDSA_size_sign();
  48037. test_ED25519();
  48038. test_ED448();
  48039. test_EC_i2d();
  48040. #endif
  48041. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  48042. !defined(HAVE_SELFTEST) && \
  48043. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  48044. test_wc_ecc_get_curve_id_from_dp_params();
  48045. #endif
  48046. #ifdef HAVE_HASHDRBG
  48047. #ifdef TEST_RESEED_INTERVAL
  48048. AssertIntEQ(test_wc_RNG_GenerateBlock_Reseed(), 0);
  48049. #endif
  48050. AssertIntEQ(test_wc_RNG_GenerateBlock(), 0);
  48051. #endif
  48052. AssertIntEQ(test_get_rand_digit(), 0);
  48053. AssertIntEQ(test_get_digit_count(), 0);
  48054. AssertIntEQ(test_mp_cond_copy(), 0);
  48055. AssertIntEQ(test_mp_rand(), 0);
  48056. AssertIntEQ(test_get_digit(), 0);
  48057. AssertIntEQ(test_wc_export_int(), 0);
  48058. AssertIntEQ(test_wc_InitRngNonce(), 0);
  48059. AssertIntEQ(test_wc_InitRngNonce_ex(), 0);
  48060. AssertIntEQ(test_wc_ed25519_make_key(), 0);
  48061. AssertIntEQ(test_wc_ed25519_init(), 0);
  48062. AssertIntEQ(test_wc_ed25519_sign_msg(), 0);
  48063. AssertIntEQ(test_wc_ed25519_import_public(), 0);
  48064. AssertIntEQ(test_wc_ed25519_import_private_key(), 0);
  48065. AssertIntEQ(test_wc_ed25519_export(), 0);
  48066. AssertIntEQ(test_wc_ed25519_size(), 0);
  48067. AssertIntEQ(test_wc_ed25519_exportKey(), 0);
  48068. AssertIntEQ(test_wc_Ed25519PublicKeyToDer(), 0);
  48069. AssertIntEQ(test_wc_curve25519_init(), 0);
  48070. AssertIntEQ(test_wc_curve25519_size(), 0);
  48071. AssertIntEQ(test_wc_curve25519_export_key_raw(), 0);
  48072. AssertIntEQ(test_wc_curve25519_export_key_raw_ex(), 0);
  48073. AssertIntEQ(test_wc_curve25519_size (), 0);
  48074. AssertIntEQ(test_wc_curve25519_make_key (), 0);
  48075. AssertIntEQ(test_wc_curve25519_shared_secret_ex (), 0);
  48076. AssertIntEQ(test_wc_curve25519_make_pub (), 0);
  48077. AssertIntEQ(test_wc_curve25519_export_public_ex (), 0);
  48078. AssertIntEQ(test_wc_curve25519_export_private_raw_ex (), 0);
  48079. AssertIntEQ(test_wc_curve25519_import_private_raw_ex (), 0);
  48080. AssertIntEQ(test_wc_curve25519_import_private (), 0);
  48081. AssertIntEQ(test_wc_ed448_make_key(), 0);
  48082. AssertIntEQ(test_wc_ed448_init(), 0);
  48083. AssertIntEQ(test_wc_ed448_sign_msg(), 0);
  48084. AssertIntEQ(test_wc_ed448_import_public(), 0);
  48085. AssertIntEQ(test_wc_ed448_import_private_key(), 0);
  48086. AssertIntEQ(test_wc_ed448_export(), 0);
  48087. AssertIntEQ(test_wc_ed448_size(), 0);
  48088. AssertIntEQ(test_wc_ed448_exportKey(), 0);
  48089. AssertIntEQ(test_wc_Ed448PublicKeyToDer(), 0);
  48090. AssertIntEQ(test_wc_curve448_make_key (), 0);
  48091. AssertIntEQ(test_wc_curve448_shared_secret_ex (), 0);
  48092. AssertIntEQ(test_wc_curve448_export_public_ex (), 0);
  48093. AssertIntEQ(test_wc_curve448_export_private_raw_ex (), 0);
  48094. AssertIntEQ(test_wc_curve448_export_key_raw (), 0);
  48095. AssertIntEQ(test_wc_curve448_import_private_raw_ex (), 0);
  48096. AssertIntEQ(test_wc_curve448_import_private (), 0);
  48097. AssertIntEQ(test_wc_curve448_init(), 0);
  48098. AssertIntEQ(test_wc_curve448_size (), 0);
  48099. AssertIntEQ(test_wc_ecc_make_key(), 0);
  48100. AssertIntEQ(test_wc_ecc_init(), 0);
  48101. AssertIntEQ(test_wc_ecc_check_key(), 0);
  48102. AssertIntEQ(test_wc_ecc_get_generator(), 0);
  48103. AssertIntEQ(test_wc_ecc_size(), 0);
  48104. test_wc_ecc_params();
  48105. AssertIntEQ(test_wc_ecc_signVerify_hash(), 0);
  48106. PRIVATE_KEY_UNLOCK();
  48107. AssertIntEQ(test_wc_ecc_shared_secret(), 0);
  48108. AssertIntEQ(test_wc_ecc_export_x963(), 0);
  48109. PRIVATE_KEY_LOCK();
  48110. AssertIntEQ(test_wc_ecc_export_x963_ex(), 0);
  48111. AssertIntEQ(test_wc_ecc_import_x963(), 0);
  48112. AssertIntEQ(ecc_import_private_key(), 0);
  48113. AssertIntEQ(test_wc_ecc_export_private_only(), 0);
  48114. AssertIntEQ(test_wc_ecc_rs_to_sig(), 0);
  48115. AssertIntEQ(test_wc_ecc_import_raw(), 0);
  48116. AssertIntEQ(test_wc_ecc_import_unsigned(), 0);
  48117. AssertIntEQ(test_wc_ecc_sig_size(), 0);
  48118. AssertIntEQ(test_wc_ecc_ctx_new(), 0);
  48119. AssertIntEQ(test_wc_ecc_ctx_reset(), 0);
  48120. AssertIntEQ(test_wc_ecc_ctx_set_peer_salt(), 0);
  48121. AssertIntEQ(test_wc_ecc_ctx_set_info(), 0);
  48122. AssertIntEQ(test_wc_ecc_encryptDecrypt(), 0);
  48123. AssertIntEQ(test_wc_ecc_del_point(), 0);
  48124. AssertIntEQ(test_wc_ecc_pointFns(), 0);
  48125. AssertIntEQ(test_wc_ecc_shared_secret_ssh(), 0);
  48126. AssertIntEQ(test_wc_ecc_verify_hash_ex(), 0);
  48127. AssertIntEQ(test_wc_ecc_mulmod(), 0);
  48128. AssertIntEQ(test_wc_ecc_is_valid_idx(), 0);
  48129. AssertIntEQ(test_wc_ecc_get_curve_id_from_oid(), 0);
  48130. AssertIntEQ(test_wc_ecc_sig_size_calc(), 0);
  48131. AssertIntEQ(test_ToTraditional(), 0);
  48132. AssertIntEQ(test_wc_EccPrivateKeyToDer(), 0);
  48133. AssertIntEQ(test_wc_DhPublicKeyDecode(), 0);
  48134. AssertIntEQ(test_wc_Ed25519KeyToDer(), 0);
  48135. AssertIntEQ(test_wc_Ed25519PrivateKeyToDer(), 0);
  48136. AssertIntEQ(test_wc_Ed448KeyToDer(), 0);
  48137. AssertIntEQ(test_wc_Ed448PrivateKeyToDer(), 0);
  48138. AssertIntEQ(test_wc_SetAuthKeyIdFromPublicKey_ex(), 0);
  48139. AssertIntEQ(test_wc_SetSubjectBuffer(), 0);
  48140. AssertIntEQ(test_wc_SetSubjectKeyIdFromPublicKey_ex(), 0);
  48141. test_wc_PKCS7_New();
  48142. test_wc_PKCS7_Init();
  48143. test_wc_PKCS7_InitWithCert();
  48144. test_wc_PKCS7_EncodeData();
  48145. test_wc_PKCS7_EncodeSignedData();
  48146. test_wc_PKCS7_EncodeSignedData_ex();
  48147. test_wc_PKCS7_VerifySignedData();
  48148. test_wc_PKCS7_EncodeDecodeEnvelopedData();
  48149. test_wc_PKCS7_EncodeEncryptedData();
  48150. test_wc_PKCS7_Degenerate();
  48151. test_wc_PKCS7_BER();
  48152. test_PKCS7_signed_enveloped();
  48153. test_wc_PKCS7_NoDefaultSignedAttribs();
  48154. test_wc_PKCS7_SetOriEncryptCtx();
  48155. test_wc_PKCS7_SetOriDecryptCtx();
  48156. test_wc_PKCS7_DecodeCompressedData();
  48157. test_wc_i2d_PKCS12();
  48158. test_wolfSSL_CTX_LoadCRL();
  48159. test_openssl_FIPS_drbg();
  48160. test_wc_CryptoCb();
  48161. test_wolfSSL_CTX_StaticMemory();
  48162. test_wolfSSL_FIPS_mode();
  48163. AssertIntEQ(test_ForceZero(), 0);
  48164. AssertIntEQ(test_wolfSSL_Cleanup(), WOLFSSL_SUCCESS);
  48165. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  48166. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  48167. !defined(WOLFSSL_NO_CLIENT_AUTH))
  48168. AssertIntEQ(test_various_pathlen_chains(), WOLFSSL_SUCCESS);
  48169. #endif
  48170. /* If at some point a stub get implemented this test should fail indicating
  48171. * a need to implement a new test case
  48172. */
  48173. test_stubs_are_stubs();
  48174. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  48175. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  48176. wc_ecc_fp_free(); /* free per thread cache */
  48177. #endif
  48178. wolfSSL_Cleanup();
  48179. (void)devId;
  48180. printf(" End API Tests\n");
  48181. }