api.c 1.1 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947369483694936950369513695236953
  1. /* api.c API unit tests
  2. *
  3. * Copyright (C) 2006-2020 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. /*----------------------------------------------------------------------------*
  22. | Includes
  23. *----------------------------------------------------------------------------*/
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #include <wolfssl/wolfcrypt/settings.h>
  28. #ifndef FOURK_BUF
  29. #define FOURK_BUF 4096
  30. #endif
  31. #ifndef TWOK_BUF
  32. #define TWOK_BUF 2048
  33. #endif
  34. #ifndef ONEK_BUF
  35. #define ONEK_BUF 1024
  36. #endif
  37. #if defined(WOLFSSL_STATIC_MEMORY)
  38. #include <wolfssl/wolfcrypt/memory.h>
  39. #endif /* WOLFSSL_STATIC_MEMORY */
  40. #ifndef HEAP_HINT
  41. #define HEAP_HINT NULL
  42. #endif /* WOLFSSL_STAIC_MEMORY */
  43. #ifdef WOLFSSL_ASNC_CRYPT
  44. #include <wolfssl/wolfcrypt/async.h>
  45. #endif
  46. #ifdef HAVE_ECC
  47. #include <wolfssl/wolfcrypt/ecc.h> /* wc_ecc_fp_free */
  48. #ifndef ECC_ASN963_MAX_BUF_SZ
  49. #define ECC_ASN963_MAX_BUF_SZ 133
  50. #endif
  51. #ifndef ECC_PRIV_KEY_BUF
  52. #define ECC_PRIV_KEY_BUF 66 /* For non user defined curves. */
  53. #endif
  54. #ifdef HAVE_ALL_CURVES
  55. /* ecc key sizes: 14, 16, 20, 24, 28, 30, 32, 40, 48, 64*/
  56. #ifndef KEY14
  57. #define KEY14 14
  58. #endif
  59. #if !defined(KEY16)
  60. #define KEY16 16
  61. #endif
  62. #if !defined(KEY20)
  63. #define KEY20 20
  64. #endif
  65. #if !defined(KEY24)
  66. #define KEY24 24
  67. #endif
  68. #if !defined(KEY28)
  69. #define KEY28 28
  70. #endif
  71. #if !defined(KEY30)
  72. #define KEY30 30
  73. #endif
  74. #if !defined(KEY32)
  75. #define KEY32 32
  76. #endif
  77. #if !defined(KEY40)
  78. #define KEY40 40
  79. #endif
  80. #if !defined(KEY48)
  81. #define KEY48 48
  82. #endif
  83. #if !defined(KEY64)
  84. #define KEY64 64
  85. #endif
  86. #else
  87. /* ecc key sizes: 14, 16, 20, 24, 28, 30, 32, 40, 48, 64*/
  88. #ifndef KEY14
  89. #define KEY14 32
  90. #endif
  91. #if !defined(KEY16)
  92. #define KEY16 32
  93. #endif
  94. #if !defined(KEY20)
  95. #define KEY20 32
  96. #endif
  97. #if !defined(KEY24)
  98. #define KEY24 32
  99. #endif
  100. #if !defined(KEY28)
  101. #define KEY28 32
  102. #endif
  103. #if !defined(KEY30)
  104. #define KEY30 32
  105. #endif
  106. #if !defined(KEY32)
  107. #define KEY32 32
  108. #endif
  109. #if !defined(KEY40)
  110. #define KEY40 32
  111. #endif
  112. #if !defined(KEY48)
  113. #define KEY48 32
  114. #endif
  115. #if !defined(KEY64)
  116. #define KEY64 32
  117. #endif
  118. #endif
  119. #if !defined(HAVE_COMP_KEY)
  120. #if !defined(NOCOMP)
  121. #define NOCOMP 0
  122. #endif
  123. #else
  124. #if !defined(COMP)
  125. #define COMP 1
  126. #endif
  127. #endif
  128. #if !defined(DER_SZ)
  129. #define DER_SZ(ks) (ks * 2 + 1)
  130. #endif
  131. #endif
  132. #ifndef NO_ASN
  133. #include <wolfssl/wolfcrypt/asn_public.h>
  134. #endif
  135. #include <wolfssl/error-ssl.h>
  136. #include <stdlib.h>
  137. #include <wolfssl/ssl.h> /* compatibility layer */
  138. #include <wolfssl/test.h>
  139. #include <tests/unit.h>
  140. #include "examples/server/server.h"
  141. /* for testing compatibility layer callbacks */
  142. #ifndef NO_MD5
  143. #include <wolfssl/wolfcrypt/md5.h>
  144. #endif
  145. #ifndef NO_SHA
  146. #include <wolfssl/wolfcrypt/sha.h>
  147. #endif
  148. #ifndef NO_SHA256
  149. #include <wolfssl/wolfcrypt/sha256.h>
  150. #endif
  151. #ifdef WOLFSSL_SHA512
  152. #include <wolfssl/wolfcrypt/sha512.h>
  153. #endif
  154. #ifdef WOLFSSL_SHA384
  155. #include <wolfssl/wolfcrypt/sha512.h>
  156. #endif
  157. #ifdef WOLFSSL_SHA3
  158. #include <wolfssl/wolfcrypt/sha3.h>
  159. #ifndef HEAP_HINT
  160. #define HEAP_HINT NULL
  161. #endif
  162. #endif
  163. #ifndef NO_AES
  164. #include <wolfssl/wolfcrypt/aes.h>
  165. #ifdef HAVE_AES_DECRYPT
  166. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  167. #endif
  168. #endif
  169. #ifdef WOLFSSL_RIPEMD
  170. #include <wolfssl/wolfcrypt/ripemd.h>
  171. #endif
  172. #ifdef HAVE_IDEA
  173. #include <wolfssl/wolfcrypt/idea.h>
  174. #endif
  175. #ifndef NO_DES3
  176. #include <wolfssl/wolfcrypt/des3.h>
  177. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  178. #endif
  179. #ifndef NO_HMAC
  180. #include <wolfssl/wolfcrypt/hmac.h>
  181. #endif
  182. #ifdef HAVE_CHACHA
  183. #include <wolfssl/wolfcrypt/chacha.h>
  184. #endif
  185. #ifdef HAVE_POLY1305
  186. #include <wolfssl/wolfcrypt/poly1305.h>
  187. #endif
  188. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  189. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  190. #endif
  191. #ifdef HAVE_CAMELLIA
  192. #include <wolfssl/wolfcrypt/camellia.h>
  193. #endif
  194. #ifndef NO_RABBIT
  195. #include <wolfssl/wolfcrypt/rabbit.h>
  196. #endif
  197. #ifndef NO_RC4
  198. #include <wolfssl/wolfcrypt/arc4.h>
  199. #endif
  200. #ifdef HAVE_BLAKE2
  201. #include <wolfssl/wolfcrypt/blake2.h>
  202. #endif
  203. #include <wolfssl/wolfcrypt/hash.h>
  204. #ifndef NO_RSA
  205. #include <wolfssl/wolfcrypt/rsa.h>
  206. #define FOURK_BUF 4096
  207. #define GEN_BUF 294
  208. #ifndef USER_CRYPTO_ERROR
  209. #define USER_CRYPTO_ERROR -101 /* error returned by IPP lib. */
  210. #endif
  211. #endif
  212. #ifndef NO_SIG_WRAPPER
  213. #include <wolfssl/wolfcrypt/signature.h>
  214. #endif
  215. #ifdef HAVE_AESCCM
  216. #include <wolfssl/wolfcrypt/aes.h>
  217. #endif
  218. #ifdef HAVE_HC128
  219. #include <wolfssl/wolfcrypt/hc128.h>
  220. #endif
  221. #ifdef HAVE_PKCS7
  222. #include <wolfssl/wolfcrypt/pkcs7.h>
  223. #include <wolfssl/wolfcrypt/asn.h>
  224. #endif
  225. #ifdef WOLFSSL_SMALL_CERT_VERIFY
  226. #include <wolfssl/wolfcrypt/asn.h>
  227. #endif
  228. #ifndef NO_DSA
  229. #include <wolfssl/wolfcrypt/dsa.h>
  230. #ifndef ONEK_BUF
  231. #define ONEK_BUF 1024
  232. #endif
  233. #ifndef TWOK_BUF
  234. #define TWOK_BUF 2048
  235. #endif
  236. #ifndef FOURK_BUF
  237. #define FOURK_BUF 4096
  238. #endif
  239. #ifndef DSA_SIG_SIZE
  240. #define DSA_SIG_SIZE 40
  241. #endif
  242. #ifndef MAX_DSA_PARAM_SIZE
  243. #define MAX_DSA_PARAM_SIZE 256
  244. #endif
  245. #endif
  246. #ifdef WOLFSSL_CMAC
  247. #include <wolfssl/wolfcrypt/cmac.h>
  248. #endif
  249. #ifdef HAVE_ED25519
  250. #include <wolfssl/wolfcrypt/ed25519.h>
  251. #endif
  252. #ifdef HAVE_CURVE25519
  253. #include <wolfssl/wolfcrypt/curve25519.h>
  254. #endif
  255. #ifdef HAVE_ED448
  256. #include <wolfssl/wolfcrypt/ed448.h>
  257. #endif
  258. #ifdef HAVE_CURVE448
  259. #include <wolfssl/wolfcrypt/curve448.h>
  260. #endif
  261. #ifdef HAVE_PKCS12
  262. #include <wolfssl/wolfcrypt/pkcs12.h>
  263. #endif
  264. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || defined(OPENSSL_ALL))
  265. #include <wolfssl/openssl/ssl.h>
  266. #ifndef NO_ASN
  267. /* for ASN_COMMON_NAME DN_tags enum */
  268. #include <wolfssl/wolfcrypt/asn.h>
  269. #endif
  270. #ifdef HAVE_OCSP
  271. #include <wolfssl/openssl/ocsp.h>
  272. #endif
  273. #endif
  274. #ifdef OPENSSL_EXTRA
  275. #include <wolfssl/openssl/x509v3.h>
  276. #include <wolfssl/openssl/asn1.h>
  277. #include <wolfssl/openssl/crypto.h>
  278. #include <wolfssl/openssl/pkcs12.h>
  279. #include <wolfssl/openssl/evp.h>
  280. #include <wolfssl/openssl/dh.h>
  281. #include <wolfssl/openssl/bn.h>
  282. #include <wolfssl/openssl/buffer.h>
  283. #include <wolfssl/openssl/pem.h>
  284. #include <wolfssl/openssl/ec.h>
  285. #include <wolfssl/openssl/engine.h>
  286. #include <wolfssl/openssl/crypto.h>
  287. #include <wolfssl/openssl/hmac.h>
  288. #include <wolfssl/openssl/objects.h>
  289. #ifndef NO_AES
  290. #include <wolfssl/openssl/aes.h>
  291. #endif
  292. #ifndef NO_DES3
  293. #include <wolfssl/openssl/des.h>
  294. #endif
  295. #ifdef HAVE_ECC
  296. #include <wolfssl/openssl/ecdsa.h>
  297. #endif
  298. #ifdef HAVE_PKCS7
  299. #include <wolfssl/openssl/pkcs7.h>
  300. #endif
  301. #ifdef HAVE_ED25519
  302. #include <wolfssl/openssl/ed25519.h>
  303. #endif
  304. #ifdef HAVE_ED448
  305. #include <wolfssl/openssl/ed448.h>
  306. #endif
  307. #endif /* OPENSSL_EXTRA */
  308. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  309. && !defined(NO_SHA256) && !defined(RC_NO_RNG)
  310. #include <wolfssl/wolfcrypt/srp.h>
  311. #endif
  312. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  313. #include "wolfssl/internal.h" /* for testing SSL_get_peer_cert_chain */
  314. #endif
  315. /* force enable test buffers */
  316. #ifndef USE_CERT_BUFFERS_2048
  317. #define USE_CERT_BUFFERS_2048
  318. #endif
  319. #ifndef USE_CERT_BUFFERS_256
  320. #define USE_CERT_BUFFERS_256
  321. #endif
  322. #include <wolfssl/certs_test.h>
  323. typedef struct testVector {
  324. const char* input;
  325. const char* output;
  326. size_t inLen;
  327. size_t outLen;
  328. } testVector;
  329. #if defined(HAVE_PKCS7)
  330. typedef struct {
  331. const byte* content;
  332. word32 contentSz;
  333. int contentOID;
  334. int encryptOID;
  335. int keyWrapOID;
  336. int keyAgreeOID;
  337. byte* cert;
  338. size_t certSz;
  339. byte* privateKey;
  340. word32 privateKeySz;
  341. } pkcs7EnvelopedVector;
  342. #ifndef NO_PKCS7_ENCRYPTED_DATA
  343. typedef struct {
  344. const byte* content;
  345. word32 contentSz;
  346. int contentOID;
  347. int encryptOID;
  348. byte* encryptionKey;
  349. word32 encryptionKeySz;
  350. } pkcs7EncryptedVector;
  351. #endif
  352. #endif /* HAVE_PKCS7 */
  353. /*----------------------------------------------------------------------------*
  354. | Constants
  355. *----------------------------------------------------------------------------*/
  356. #define TEST_SUCCESS (1)
  357. #define TEST_FAIL (0)
  358. #define testingFmt " %s:"
  359. #define resultFmt " %s\n"
  360. static const char* passed = "passed";
  361. static const char* failed = "failed";
  362. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  363. (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT))
  364. static const char* bogusFile =
  365. #ifdef _WIN32
  366. "NUL"
  367. #else
  368. "/dev/null"
  369. #endif
  370. ;
  371. #endif /* !NO_FILESYSTEM && !NO_CERTS && (!NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT) */
  372. enum {
  373. TESTING_RSA = 1,
  374. TESTING_ECC = 2
  375. };
  376. static int devId = INVALID_DEVID;
  377. /*----------------------------------------------------------------------------*
  378. | Setup
  379. *----------------------------------------------------------------------------*/
  380. static int test_wolfSSL_Init(void)
  381. {
  382. int result;
  383. printf(testingFmt, "wolfSSL_Init()");
  384. result = wolfSSL_Init();
  385. printf(resultFmt, result == WOLFSSL_SUCCESS ? passed : failed);
  386. return result;
  387. }
  388. static int test_wolfSSL_Cleanup(void)
  389. {
  390. int result;
  391. printf(testingFmt, "wolfSSL_Cleanup()");
  392. result = wolfSSL_Cleanup();
  393. printf(resultFmt, result == WOLFSSL_SUCCESS ? passed : failed);
  394. return result;
  395. }
  396. /* Initialize the wolfCrypt state.
  397. * POST: 0 success.
  398. */
  399. static int test_wolfCrypt_Init(void)
  400. {
  401. int result;
  402. printf(testingFmt, "wolfCrypt_Init()");
  403. result = wolfCrypt_Init();
  404. printf(resultFmt, result == 0 ? passed : failed);
  405. return result;
  406. } /* END test_wolfCrypt_Init */
  407. /*----------------------------------------------------------------------------*
  408. | Platform dependent function test
  409. *----------------------------------------------------------------------------*/
  410. static int test_fileAccess()
  411. {
  412. #if defined(WOLFSSL_TEST_PLATFORMDEPEND) && !defined(NO_FILESYSTEM)
  413. const char *fname[] = {
  414. svrCertFile, svrKeyFile, caCertFile,
  415. eccCertFile, eccKeyFile, eccRsaCertFile,
  416. cliCertFile, cliCertDerFile, cliKeyFile,
  417. ntruCertFile, ntruKeyFile, dhParamFile,
  418. cliEccKeyFile, cliEccCertFile, caEccCertFile, edCertFile, edKeyFile,
  419. cliEdCertFile, cliEdKeyFile, caEdCertFile,
  420. NULL
  421. };
  422. const char derfile[] = "./certs/server-cert.der";
  423. XFILE f;
  424. size_t sz;
  425. byte *buff;
  426. int i;
  427. printf(testingFmt, "test_fileAccess()");
  428. AssertTrue(XFOPEN("badfilename", "rb") == XBADFILE);
  429. for(i=0; fname[i] != NULL ; i++){
  430. AssertTrue((f = XFOPEN(fname[i], "rb")) != XBADFILE);
  431. XFCLOSE(f);
  432. }
  433. AssertTrue((f = XFOPEN(derfile, "rb")) != XBADFILE);
  434. AssertTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  435. sz = (size_t) XFTELL(f);
  436. XREWIND(f);
  437. AssertTrue(sz == sizeof_server_cert_der_2048);
  438. AssertTrue((buff = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE)) != NULL) ;
  439. AssertTrue(XFREAD(buff, 1, sz, f) == sz);
  440. XMEMCMP(server_cert_der_2048, buff, sz);
  441. printf(resultFmt, passed);
  442. #endif
  443. return WOLFSSL_SUCCESS;
  444. }
  445. /*----------------------------------------------------------------------------*
  446. | Method Allocators
  447. *----------------------------------------------------------------------------*/
  448. static void test_wolfSSL_Method_Allocators(void)
  449. {
  450. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  451. do { \
  452. WOLFSSL_METHOD *method; \
  453. condition(method = allocator()); \
  454. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  455. } while(0)
  456. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  457. TEST_METHOD_ALLOCATOR(a, AssertNotNull)
  458. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  459. TEST_METHOD_ALLOCATOR(a, AssertNull)
  460. #ifndef NO_OLD_TLS
  461. #ifdef WOLFSSL_ALLOW_SSLV3
  462. #ifndef NO_WOLFSSL_SERVER
  463. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  464. #endif
  465. #ifndef NO_WOLFSSL_CLIENT
  466. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  467. #endif
  468. #endif
  469. #ifdef WOLFSSL_ALLOW_TLSV10
  470. #ifndef NO_WOLFSSL_SERVER
  471. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  472. #endif
  473. #ifndef NO_WOLFSSL_CLIENT
  474. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  475. #endif
  476. #endif
  477. #ifndef NO_WOLFSSL_SERVER
  478. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  479. #endif
  480. #ifndef NO_WOLFSSL_CLIENT
  481. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  482. #endif
  483. #endif /* !NO_OLD_TLS */
  484. #ifndef WOLFSSL_NO_TLS12
  485. #ifndef NO_WOLFSSL_SERVER
  486. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  487. #endif
  488. #ifndef NO_WOLFSSL_CLIENT
  489. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  490. #endif
  491. #endif /* !WOLFSSL_NO_TLS12 */
  492. #ifdef WOLFSSL_TLS13
  493. #ifndef NO_WOLFSSL_SERVER
  494. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
  495. #endif
  496. #ifndef NO_WOLFSSL_CLIENT
  497. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
  498. #endif
  499. #endif /* WOLFSSL_TLS13 */
  500. #ifndef NO_WOLFSSL_SERVER
  501. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
  502. #endif
  503. #ifndef NO_WOLFSSL_CLIENT
  504. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  505. #endif
  506. #ifdef WOLFSSL_DTLS
  507. #ifndef NO_OLD_TLS
  508. #ifndef NO_WOLFSSL_SERVER
  509. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  510. #endif
  511. #ifndef NO_WOLFSSL_CLIENT
  512. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  513. #endif
  514. #endif
  515. #ifndef WOLFSSL_NO_TLS12
  516. #ifndef NO_WOLFSSL_SERVER
  517. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  518. #endif
  519. #ifndef NO_WOLFSSL_CLIENT
  520. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  521. #endif
  522. #endif
  523. #endif /* WOLFSSL_DTLS */
  524. #if !defined(NO_OLD_TLS) && defined(OPENSSL_EXTRA)
  525. /* Stubs */
  526. #ifndef NO_WOLFSSL_SERVER
  527. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  528. #endif
  529. #ifndef NO_WOLFSSL_CLIENT
  530. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  531. #endif
  532. #endif
  533. /* Test Either Method (client or server) */
  534. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  535. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_method);
  536. #ifndef NO_OLD_TLS
  537. #ifdef WOLFSSL_ALLOW_TLSV10
  538. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_method);
  539. #endif
  540. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_method);
  541. #endif /* !NO_OLD_TLS */
  542. #ifndef WOLFSSL_NO_TLS12
  543. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_method);
  544. #endif /* !WOLFSSL_NO_TLS12 */
  545. #ifdef WOLFSSL_TLS13
  546. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_method);
  547. #endif /* WOLFSSL_TLS13 */
  548. #ifdef WOLFSSL_DTLS
  549. TEST_VALID_METHOD_ALLOCATOR(wolfDTLS_method);
  550. #ifndef NO_OLD_TLS
  551. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_method);
  552. #endif /* !NO_OLD_TLS */
  553. #ifndef WOLFSSL_NO_TLS12
  554. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_method);
  555. #endif /* !WOLFSSL_NO_TLS12 */
  556. #endif /* WOLFSSL_DTLS */
  557. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  558. }
  559. /*----------------------------------------------------------------------------*
  560. | Context
  561. *----------------------------------------------------------------------------*/
  562. #ifndef NO_WOLFSSL_SERVER
  563. static void test_wolfSSL_CTX_new(WOLFSSL_METHOD *method)
  564. {
  565. WOLFSSL_CTX *ctx;
  566. AssertNull(ctx = wolfSSL_CTX_new(NULL));
  567. AssertNotNull(method);
  568. AssertNotNull(ctx = wolfSSL_CTX_new(method));
  569. wolfSSL_CTX_free(ctx);
  570. }
  571. #endif
  572. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  573. (!defined(NO_RSA) || defined(HAVE_ECC))
  574. static void test_for_double_Free(void)
  575. {
  576. WOLFSSL_CTX* ctx;
  577. WOLFSSL* ssl;
  578. int skipTest = 0;
  579. const char* testCertFile;
  580. const char* testKeyFile;
  581. char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
  582. ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
  583. "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
  584. "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
  585. "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
  586. "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
  587. "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
  588. "NULL-SHA:HC128-MD5:HC128-SHA:RABBIT-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
  589. "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
  590. "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
  591. "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
  592. ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
  593. "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
  594. ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
  595. "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
  596. "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
  597. "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
  598. "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
  599. "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
  600. "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
  601. "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
  602. "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
  603. "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
  604. "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
  605. "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
  606. "LY1305-OLD:IDEA-CBC-SHA:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
  607. "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
  608. "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
  609. "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
  610. "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
  611. #ifndef NO_RSA
  612. testCertFile = svrCertFile;
  613. testKeyFile = svrKeyFile;
  614. #elif defined(HAVE_ECC)
  615. testCertFile = eccCertFile;
  616. testKeyFile = eccKeyFile;
  617. #else
  618. skipTest = 1;
  619. #endif
  620. if (skipTest != 1) {
  621. #ifndef NO_WOLFSSL_SERVER
  622. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  623. AssertNotNull(ctx);
  624. #else
  625. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  626. AssertNotNull(ctx);
  627. #endif
  628. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  629. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  630. ssl = wolfSSL_new(ctx);
  631. AssertNotNull(ssl);
  632. /* First test freeing SSL, then CTX */
  633. wolfSSL_free(ssl);
  634. wolfSSL_CTX_free(ctx);
  635. #ifndef NO_WOLFSSL_CLIENT
  636. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  637. AssertNotNull(ctx);
  638. #else
  639. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  640. AssertNotNull(ctx);
  641. #endif
  642. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  643. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  644. ssl = wolfSSL_new(ctx);
  645. AssertNotNull(ssl);
  646. /* Next test freeing CTX then SSL */
  647. wolfSSL_CTX_free(ctx);
  648. wolfSSL_free(ssl);
  649. #ifndef NO_WOLFSSL_SERVER
  650. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  651. AssertNotNull(ctx);
  652. #else
  653. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  654. AssertNotNull(ctx);
  655. #endif
  656. /* Test setting ciphers at ctx level */
  657. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  658. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  659. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
  660. AssertNotNull(ssl = wolfSSL_new(ctx));
  661. wolfSSL_CTX_free(ctx);
  662. wolfSSL_free(ssl);
  663. #ifndef NO_WOLFSSL_CLIENT
  664. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  665. AssertNotNull(ctx);
  666. #else
  667. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  668. AssertNotNull(ctx);
  669. #endif
  670. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  671. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  672. ssl = wolfSSL_new(ctx);
  673. AssertNotNull(ssl);
  674. /* test setting ciphers at SSL level */
  675. AssertTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
  676. wolfSSL_CTX_free(ctx);
  677. wolfSSL_free(ssl);
  678. }
  679. }
  680. #endif
  681. static void test_wolfSSL_CTX_use_certificate_file(void)
  682. {
  683. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  684. WOLFSSL_CTX *ctx;
  685. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  686. /* invalid context */
  687. AssertFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  688. WOLFSSL_FILETYPE_PEM));
  689. /* invalid cert file */
  690. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  691. WOLFSSL_FILETYPE_PEM));
  692. /* invalid cert type */
  693. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  694. #ifdef NO_RSA
  695. /* rsa needed */
  696. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,WOLFSSL_FILETYPE_PEM));
  697. #else
  698. /* success */
  699. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  700. #endif
  701. wolfSSL_CTX_free(ctx);
  702. #endif
  703. }
  704. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  705. static int test_wolfSSL_CTX_use_certificate_ASN1(void)
  706. {
  707. #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
  708. WOLFSSL_CTX* ctx;
  709. int ret;
  710. printf(testingFmt, "wolfSSL_CTX_use_certificate_ASN1()");
  711. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  712. ret = SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
  713. server_cert_der_2048);
  714. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  715. wolfSSL_CTX_free(ctx);
  716. return ret;
  717. #else
  718. return WOLFSSL_SUCCESS;
  719. #endif
  720. }
  721. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  722. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  723. * context using buffer.
  724. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  725. * --enable-testcert flag.
  726. */
  727. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  728. {
  729. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  730. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  731. WOLFSSL_CTX* ctx;
  732. int ret;
  733. printf(testingFmt, "wolfSSL_CTX_use_certificate_buffer()");
  734. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  735. ret = wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  736. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1);
  737. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  738. wolfSSL_CTX_free(ctx);
  739. return ret;
  740. #else
  741. return WOLFSSL_SUCCESS;
  742. #endif
  743. } /*END test_wolfSSL_CTX_use_certificate_buffer*/
  744. static void test_wolfSSL_CTX_use_PrivateKey_file(void)
  745. {
  746. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  747. WOLFSSL_CTX *ctx;
  748. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  749. /* invalid context */
  750. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  751. WOLFSSL_FILETYPE_PEM));
  752. /* invalid key file */
  753. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  754. WOLFSSL_FILETYPE_PEM));
  755. /* invalid key type */
  756. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  757. /* success */
  758. #ifdef NO_RSA
  759. /* rsa needed */
  760. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  761. #else
  762. /* success */
  763. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  764. #endif
  765. wolfSSL_CTX_free(ctx);
  766. #endif
  767. }
  768. /* test both file and buffer versions along with unloading trusted peer certs */
  769. static void test_wolfSSL_CTX_trust_peer_cert(void)
  770. {
  771. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  772. !defined(NO_WOLFSSL_CLIENT)
  773. WOLFSSL_CTX *ctx;
  774. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  775. #if !defined(NO_FILESYSTEM)
  776. /* invalid file */
  777. assert(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  778. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS);
  779. assert(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  780. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS);
  781. assert(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  782. WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS);
  783. /* success */
  784. assert(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile, WOLFSSL_FILETYPE_PEM)
  785. == WOLFSSL_SUCCESS);
  786. /* unload cert */
  787. assert(wolfSSL_CTX_Unload_trust_peers(NULL) != WOLFSSL_SUCCESS);
  788. assert(wolfSSL_CTX_Unload_trust_peers(ctx) == WOLFSSL_SUCCESS);
  789. #endif
  790. /* Test of loading certs from buffers */
  791. /* invalid buffer */
  792. assert(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  793. WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS);
  794. /* success */
  795. #ifdef USE_CERT_BUFFERS_1024
  796. assert(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  797. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1) == WOLFSSL_SUCCESS);
  798. #endif
  799. #ifdef USE_CERT_BUFFERS_2048
  800. assert(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  801. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1) == WOLFSSL_SUCCESS);
  802. #endif
  803. /* unload cert */
  804. assert(wolfSSL_CTX_Unload_trust_peers(NULL) != WOLFSSL_SUCCESS);
  805. assert(wolfSSL_CTX_Unload_trust_peers(ctx) == WOLFSSL_SUCCESS);
  806. wolfSSL_CTX_free(ctx);
  807. #endif
  808. }
  809. static void test_wolfSSL_CTX_load_verify_locations(void)
  810. {
  811. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  812. WOLFSSL_CTX *ctx;
  813. #ifndef NO_RSA
  814. WOLFSSL_CERT_MANAGER* cm;
  815. #ifdef PERSIST_CERT_CACHE
  816. int cacheSz;
  817. #endif
  818. #endif
  819. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  820. const char* load_certs_path = "./certs/external";
  821. const char* load_no_certs_path = "./examples";
  822. const char* load_expired_path = "./certs/test/expired";
  823. #endif
  824. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  825. /* invalid arguments */
  826. AssertIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL), WOLFSSL_FAILURE);
  827. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL), WOLFSSL_FAILURE);
  828. /* invalid ca file */
  829. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL), WOLFSSL_BAD_FILE);
  830. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  831. /* invalid path */
  832. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile), BAD_PATH_ERROR);
  833. #endif
  834. /* load ca cert */
  835. #ifdef NO_RSA
  836. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL), ASN_UNKNOWN_OID_E);
  837. #else /* Skip the following test without RSA certs. */
  838. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL), WOLFSSL_SUCCESS);
  839. #ifdef PERSIST_CERT_CACHE
  840. /* Get cert cache size */
  841. cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx);
  842. #endif
  843. /* Test unloading CA's */
  844. AssertIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
  845. #ifdef PERSIST_CERT_CACHE
  846. /* Verify no certs (result is less than cacheSz) */
  847. AssertIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  848. #endif
  849. /* load ca cert again */
  850. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL), WOLFSSL_SUCCESS);
  851. /* Test getting CERT_MANAGER */
  852. AssertNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  853. /* Test unloading CA's using CM */
  854. AssertIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  855. #ifdef PERSIST_CERT_CACHE
  856. /* Verify no certs (result is less than cacheSz) */
  857. AssertIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  858. #endif
  859. #endif
  860. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  861. /* Test loading CA certificates using a path */
  862. #ifdef NO_RSA
  863. /* failure here okay since certs in external directory are RSA */
  864. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  865. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  866. #else
  867. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  868. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  869. #endif
  870. /* Test loading path with no files */
  871. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_no_certs_path,
  872. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
  873. /* Test loading expired CA certificates */
  874. #ifdef NO_RSA
  875. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_expired_path,
  876. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  877. WOLFSSL_SUCCESS);
  878. #else
  879. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_expired_path,
  880. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  881. WOLFSSL_SUCCESS);
  882. #endif
  883. /* Test loading CA certificates and ignoring all errors */
  884. #ifdef NO_RSA
  885. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  886. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
  887. #else
  888. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  889. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
  890. #endif
  891. #endif
  892. wolfSSL_CTX_free(ctx);
  893. #endif
  894. }
  895. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  896. static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz, int file_type)
  897. {
  898. int ret;
  899. WOLFSSL_CERT_MANAGER* cm;
  900. cm = wolfSSL_CertManagerNew();
  901. if (cm == NULL) {
  902. printf("test_cm_load_ca failed\n");
  903. return -1;
  904. }
  905. ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
  906. wolfSSL_CertManagerFree(cm);
  907. return ret;
  908. }
  909. static int test_cm_load_ca_file(const char* ca_cert_file)
  910. {
  911. int ret = 0;
  912. byte* cert_buf = NULL;
  913. size_t cert_sz = 0;
  914. #if defined(WOLFSSL_PEM_TO_DER)
  915. DerBuffer* pDer = NULL;
  916. #endif
  917. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  918. if (ret == 0) {
  919. /* normal test */
  920. ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
  921. if (ret == 0) {
  922. /* test including null terminator in length */
  923. ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1, WOLFSSL_FILETYPE_PEM);
  924. }
  925. #if defined(WOLFSSL_PEM_TO_DER)
  926. if (ret == 0) {
  927. /* test loading DER */
  928. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  929. if (ret == 0 && pDer != NULL) {
  930. ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
  931. WOLFSSL_FILETYPE_ASN1);
  932. wc_FreeDer(&pDer);
  933. }
  934. }
  935. #endif
  936. free(cert_buf);
  937. }
  938. return ret;
  939. }
  940. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  941. static int test_wolfSSL_CertManagerLoadCABuffer(void)
  942. {
  943. int ret = 0;
  944. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  945. const char* ca_cert = "./certs/ca-cert.pem";
  946. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  947. ret = test_cm_load_ca_file(ca_cert);
  948. #ifdef NO_RSA
  949. AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
  950. #else
  951. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  952. #endif
  953. ret = test_cm_load_ca_file(ca_expired_cert);
  954. #ifdef NO_RSA
  955. AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
  956. #else
  957. AssertIntEQ(ret, ASN_AFTER_DATE_E);
  958. #endif
  959. #endif
  960. return ret;
  961. }
  962. static void test_wolfSSL_CertManagerGetCerts(void)
  963. {
  964. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  965. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  966. defined(WOLFSSL_SIGNER_DER_CERT)
  967. WOLFSSL_CERT_MANAGER* cm = NULL;
  968. WOLFSSL_STACK* sk = NULL;
  969. X509* x509 = NULL;
  970. X509* cert1 = NULL;
  971. FILE* file1 = NULL;
  972. #ifdef DEBUG_WOLFSSL_VERBOSE
  973. WOLFSSL_BIO* bio = NULL;
  974. #endif
  975. int i = 0;
  976. printf(testingFmt, "wolfSSL_CertManagerGetCerts()");
  977. AssertNotNull(file1=fopen("./certs/ca-cert.pem", "rb"));
  978. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  979. fclose(file1);
  980. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  981. AssertNull(sk = wolfSSL_CertManagerGetCerts(cm));
  982. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  983. "./certs/ca-cert.pem", NULL));
  984. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  985. for (i = 0; i < sk_X509_num(sk); i++) {
  986. x509 = sk_X509_value(sk, i);
  987. AssertIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  988. #ifdef DEBUG_WOLFSSL_VERBOSE
  989. bio = BIO_new(wolfSSL_BIO_s_file());
  990. if (bio != NULL) {
  991. BIO_set_fp(bio, stdout, BIO_NOCLOSE);
  992. X509_print(bio, x509);
  993. BIO_free(bio);
  994. }
  995. #endif /* DEBUG_WOLFSSL_VERBOSE */
  996. }
  997. wolfSSL_X509_free(cert1);
  998. sk_X509_free(sk);
  999. wolfSSL_CertManagerFree(cm);
  1000. printf(resultFmt, passed);
  1001. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1002. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1003. defined(WOLFSSL_SIGNER_DER_CERT) */
  1004. }
  1005. static int test_wolfSSL_CertManagerSetVerify(void)
  1006. {
  1007. int ret = 0;
  1008. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1009. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA)
  1010. WOLFSSL_CERT_MANAGER* cm;
  1011. int tmp = myVerifyAction;
  1012. const char* ca_cert = "./certs/ca-cert.pem";
  1013. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  1014. cm = wolfSSL_CertManagerNew();
  1015. AssertNotNull(cm);
  1016. wolfSSL_CertManagerSetVerify(cm, myVerify);
  1017. ret = wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL);
  1018. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1019. /* Use the test CB that always accepts certs */
  1020. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  1021. ret = wolfSSL_CertManagerVerify(cm, expiredCert, WOLFSSL_FILETYPE_PEM);
  1022. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1023. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  1024. {
  1025. const char* verifyCert = "./certs/server-cert.pem";
  1026. /* Use the test CB that always fails certs */
  1027. myVerifyAction = VERIFY_FORCE_FAIL;
  1028. ret = wolfSSL_CertManagerVerify(cm, verifyCert, WOLFSSL_FILETYPE_PEM);
  1029. AssertIntEQ(ret, VERIFY_CERT_ERROR);
  1030. }
  1031. #endif
  1032. wolfSSL_CertManagerFree(cm);
  1033. myVerifyAction = tmp;
  1034. #endif
  1035. return ret;
  1036. }
  1037. static void test_wolfSSL_CertManagerNameConstraint(void)
  1038. {
  1039. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1040. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1041. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1042. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  1043. WOLFSSL_CERT_MANAGER* cm;
  1044. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  1045. int i = 0;
  1046. static const byte extNameConsOid[] = {85, 29, 30};
  1047. RsaKey key;
  1048. WC_RNG rng;
  1049. byte *der;
  1050. int derSz;
  1051. word32 idx = 0;
  1052. byte *pt;
  1053. WOLFSSL_X509 *x509;
  1054. wc_InitRng(&rng);
  1055. /* load in CA private key for signing */
  1056. AssertIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, devId), 0);
  1057. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  1058. sizeof_server_key_der_2048), 0);
  1059. /* get ca certificate then alter it */
  1060. AssertNotNull(der =
  1061. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  1062. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  1063. WOLFSSL_FILETYPE_ASN1));
  1064. AssertNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  1065. XMEMCPY(der, pt, derSz);
  1066. /* find the name constraint extension and alter it */
  1067. pt = der;
  1068. for (i = 0; i < derSz - 3; i++) {
  1069. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  1070. pt += 3;
  1071. break;
  1072. }
  1073. pt++;
  1074. }
  1075. AssertIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  1076. /* go to the length value and set it to 0 */
  1077. while (i < derSz && *pt != 0x81) {
  1078. pt++;
  1079. i++;
  1080. }
  1081. AssertIntNE(i, derSz); /* did not place to alter */
  1082. pt++;
  1083. *pt = 0x00;
  1084. /* resign the altered certificate */
  1085. AssertIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  1086. FOURK_BUF, &key, NULL, &rng)), 0);
  1087. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1088. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1089. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  1090. wolfSSL_CertManagerFree(cm);
  1091. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1092. wolfSSL_X509_free(x509);
  1093. wc_FreeRsaKey(&key);
  1094. wc_FreeRng(&rng);
  1095. #endif
  1096. }
  1097. static void test_wolfSSL_CertManagerCRL(void)
  1098. {
  1099. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  1100. !defined(NO_RSA)
  1101. const char* ca_cert = "./certs/ca-cert.pem";
  1102. const char* crl1 = "./certs/crl/crl.pem";
  1103. const char* crl2 = "./certs/crl/crl2.pem";
  1104. WOLFSSL_CERT_MANAGER* cm = NULL;
  1105. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1106. AssertIntEQ(WOLFSSL_SUCCESS,
  1107. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  1108. AssertIntEQ(WOLFSSL_SUCCESS,
  1109. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  1110. AssertIntEQ(WOLFSSL_SUCCESS,
  1111. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  1112. wolfSSL_CertManagerFreeCRL(cm);
  1113. AssertIntEQ(WOLFSSL_SUCCESS,
  1114. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  1115. AssertIntEQ(WOLFSSL_SUCCESS,
  1116. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  1117. wolfSSL_CertManagerFree(cm);
  1118. #endif
  1119. }
  1120. static void test_wolfSSL_CTX_load_verify_locations_ex(void)
  1121. {
  1122. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  1123. !defined(NO_WOLFSSL_CLIENT)
  1124. WOLFSSL_CTX* ctx;
  1125. const char* ca_cert = "./certs/ca-cert.pem";
  1126. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1127. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1128. AssertNotNull(ctx);
  1129. /* test good CA */
  1130. AssertTrue(WOLFSSL_SUCCESS ==
  1131. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  1132. WOLFSSL_LOAD_FLAG_NONE));
  1133. /* test expired CA */
  1134. AssertTrue(WOLFSSL_SUCCESS !=
  1135. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  1136. WOLFSSL_LOAD_FLAG_NONE));
  1137. AssertTrue(WOLFSSL_SUCCESS ==
  1138. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  1139. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY));
  1140. wolfSSL_CTX_free(ctx);
  1141. #endif
  1142. }
  1143. static void test_wolfSSL_CTX_load_verify_buffer_ex(void)
  1144. {
  1145. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  1146. defined(USE_CERT_BUFFERS_2048)
  1147. WOLFSSL_CTX* ctx;
  1148. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  1149. byte ca_expired_cert[TWOK_BUF];
  1150. word32 sizeof_ca_expired_cert;
  1151. XFILE fp;
  1152. #ifndef NO_WOLFSSL_CLIENT
  1153. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1154. #else
  1155. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  1156. #endif
  1157. AssertNotNull(ctx);
  1158. /* test good CA */
  1159. AssertTrue(WOLFSSL_SUCCESS ==
  1160. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  1161. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  1162. WOLFSSL_LOAD_FLAG_NONE));
  1163. /* load expired CA */
  1164. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  1165. fp = XFOPEN(ca_expired_cert_file, "rb");
  1166. AssertTrue(fp != XBADFILE);
  1167. sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  1168. sizeof(ca_expired_cert), fp);
  1169. XFCLOSE(fp);
  1170. /* test expired CA failure */
  1171. AssertTrue(WOLFSSL_SUCCESS !=
  1172. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  1173. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  1174. WOLFSSL_LOAD_FLAG_NONE));
  1175. /* test expired CA success */
  1176. AssertTrue(WOLFSSL_SUCCESS ==
  1177. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  1178. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  1179. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY));
  1180. wolfSSL_CTX_free(ctx);
  1181. #endif
  1182. }
  1183. static void test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  1184. {
  1185. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  1186. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048)
  1187. WOLFSSL_CTX* ctx;
  1188. #ifndef NO_WOLFSSL_CLIENT
  1189. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1190. #else
  1191. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1192. #endif
  1193. AssertTrue(WOLFSSL_SUCCESS ==
  1194. wolfSSL_CTX_load_verify_chain_buffer_format(ctx, ca_cert_chain_der,
  1195. sizeof_ca_cert_chain_der,
  1196. WOLFSSL_FILETYPE_ASN1));
  1197. wolfSSL_CTX_free(ctx);
  1198. #endif
  1199. }
  1200. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  1201. {
  1202. int ret = 0;
  1203. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  1204. const char* server_chain_der = "./certs/server-cert-chain.der";
  1205. WOLFSSL_CTX* ctx;
  1206. #ifndef NO_WOLFSSL_CLIENT
  1207. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1208. AssertNotNull(ctx);
  1209. #else
  1210. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  1211. AssertNotNull(ctx);
  1212. #endif
  1213. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  1214. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1215. wolfSSL_CTX_free(ctx);
  1216. #endif
  1217. return ret;
  1218. }
  1219. static void test_wolfSSL_CTX_SetTmpDH_file(void)
  1220. {
  1221. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH)
  1222. WOLFSSL_CTX *ctx;
  1223. #ifndef NO_WOLFSSL_CLIENT
  1224. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1225. #else
  1226. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1227. #endif
  1228. /* invalid context */
  1229. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  1230. dhParamFile, WOLFSSL_FILETYPE_PEM));
  1231. /* invalid dhParamFile file */
  1232. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  1233. NULL, WOLFSSL_FILETYPE_PEM));
  1234. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  1235. bogusFile, WOLFSSL_FILETYPE_PEM));
  1236. /* success */
  1237. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  1238. WOLFSSL_FILETYPE_PEM));
  1239. wolfSSL_CTX_free(ctx);
  1240. #endif
  1241. }
  1242. static void test_wolfSSL_CTX_SetTmpDH_buffer(void)
  1243. {
  1244. #if !defined(NO_CERTS) && !defined(NO_DH)
  1245. WOLFSSL_CTX *ctx;
  1246. #ifndef NO_WOLFSSL_CLIENT
  1247. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1248. #else
  1249. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1250. #endif
  1251. /* invalid context */
  1252. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, dh_key_der_2048,
  1253. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1254. /* invalid dhParamFile file */
  1255. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  1256. 0, WOLFSSL_FILETYPE_ASN1));
  1257. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dsa_key_der_2048,
  1258. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1259. /* success */
  1260. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  1261. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1262. wolfSSL_CTX_free(ctx);
  1263. #endif
  1264. }
  1265. static void test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  1266. {
  1267. #if !defined(NO_CERTS) && !defined(NO_DH)
  1268. WOLFSSL_CTX *ctx;
  1269. #ifndef NO_WOLFSSL_CLIENT
  1270. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1271. AssertNotNull(ctx);
  1272. #else
  1273. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  1274. AssertNotNull(ctx);
  1275. #endif
  1276. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  1277. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  1278. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1279. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  1280. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  1281. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1282. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  1283. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  1284. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1285. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  1286. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  1287. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1288. wolfSSL_CTX_free(ctx);
  1289. #endif
  1290. }
  1291. static void test_wolfSSL_CTX_der_load_verify_locations(void)
  1292. {
  1293. #ifdef WOLFSSL_DER_LOAD
  1294. WOLFSSL_CTX* ctx = NULL;
  1295. const char* derCert = "./certs/server-cert.der";
  1296. const char* nullPath = NULL;
  1297. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  1298. const char* emptyPath = "";
  1299. /* der load Case 1 ctx NULL */
  1300. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  1301. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  1302. #ifndef NO_WOLFSSL_CLIENT
  1303. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1304. #else
  1305. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1306. #endif
  1307. /* Case 2 filePath NULL */
  1308. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  1309. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  1310. /* Case 3 invalid format */
  1311. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  1312. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  1313. /* Case 4 filePath not valid */
  1314. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  1315. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  1316. /* Case 5 filePath empty */
  1317. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  1318. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  1319. #ifndef NO_RSA
  1320. /* Case 6 success case */
  1321. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  1322. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1323. #endif
  1324. wolfSSL_CTX_free(ctx);
  1325. #endif
  1326. }
  1327. static void test_wolfSSL_CTX_enable_disable(void)
  1328. {
  1329. #ifndef NO_CERTS
  1330. WOLFSSL_CTX* ctx = NULL;
  1331. #ifdef HAVE_CRL
  1332. AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  1333. AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  1334. #endif
  1335. #ifdef HAVE_OCSP
  1336. AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  1337. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  1338. #endif
  1339. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  1340. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  1341. AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  1342. AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  1343. #endif
  1344. #ifndef NO_WOLFSSL_CLIENT
  1345. #ifdef HAVE_EXTENDED_MASTER
  1346. AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  1347. #endif
  1348. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1349. AssertNotNull(ctx);
  1350. #ifdef HAVE_EXTENDED_MASTER
  1351. AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  1352. #endif
  1353. #elif !defined(NO_WOLFSSL_SERVER)
  1354. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1355. #else
  1356. return;
  1357. #endif
  1358. #ifdef HAVE_CRL
  1359. AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  1360. AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  1361. #endif
  1362. #ifdef HAVE_OCSP
  1363. AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  1364. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  1365. WOLFSSL_SUCCESS);
  1366. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  1367. WOLFSSL_SUCCESS);
  1368. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  1369. WOLFSSL_SUCCESS);
  1370. #endif
  1371. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  1372. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  1373. AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  1374. AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  1375. #endif
  1376. wolfSSL_CTX_free(ctx);
  1377. #endif /* NO_CERTS */
  1378. }
  1379. /*----------------------------------------------------------------------------*
  1380. | SSL
  1381. *----------------------------------------------------------------------------*/
  1382. static void test_server_wolfSSL_new(void)
  1383. {
  1384. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  1385. !defined(NO_WOLFSSL_SERVER)
  1386. WOLFSSL_CTX *ctx;
  1387. WOLFSSL_CTX *ctx_nocert;
  1388. WOLFSSL *ssl;
  1389. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1390. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1391. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  1392. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  1393. /* invalid context */
  1394. AssertNull(ssl = wolfSSL_new(NULL));
  1395. #ifndef WOLFSSL_SESSION_EXPORT
  1396. AssertNull(ssl = wolfSSL_new(ctx_nocert));
  1397. #endif
  1398. /* success */
  1399. AssertNotNull(ssl = wolfSSL_new(ctx));
  1400. wolfSSL_free(ssl);
  1401. wolfSSL_CTX_free(ctx);
  1402. wolfSSL_CTX_free(ctx_nocert);
  1403. #endif
  1404. }
  1405. static void test_client_wolfSSL_new(void)
  1406. {
  1407. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  1408. !defined(NO_WOLFSSL_CLIENT)
  1409. WOLFSSL_CTX *ctx;
  1410. WOLFSSL_CTX *ctx_nocert;
  1411. WOLFSSL *ssl;
  1412. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1413. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1414. AssertTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  1415. /* invalid context */
  1416. AssertNull(ssl = wolfSSL_new(NULL));
  1417. /* success */
  1418. AssertNotNull(ssl = wolfSSL_new(ctx_nocert));
  1419. wolfSSL_free(ssl);
  1420. /* success */
  1421. AssertNotNull(ssl = wolfSSL_new(ctx));
  1422. wolfSSL_free(ssl);
  1423. wolfSSL_CTX_free(ctx);
  1424. wolfSSL_CTX_free(ctx_nocert);
  1425. #endif
  1426. }
  1427. static void test_wolfSSL_SetTmpDH_file(void)
  1428. {
  1429. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  1430. !defined(NO_WOLFSSL_SERVER)
  1431. WOLFSSL_CTX *ctx;
  1432. WOLFSSL *ssl;
  1433. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1434. #ifndef NO_RSA
  1435. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1436. WOLFSSL_FILETYPE_PEM));
  1437. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1438. WOLFSSL_FILETYPE_PEM));
  1439. #elif defined(HAVE_ECC)
  1440. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  1441. WOLFSSL_FILETYPE_PEM));
  1442. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  1443. WOLFSSL_FILETYPE_PEM));
  1444. #elif defined(HAVE_ED25519)
  1445. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  1446. WOLFSSL_FILETYPE_PEM));
  1447. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  1448. WOLFSSL_FILETYPE_PEM));
  1449. #elif defined(HAVE_ED448)
  1450. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  1451. WOLFSSL_FILETYPE_PEM));
  1452. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  1453. WOLFSSL_FILETYPE_PEM));
  1454. #endif
  1455. AssertNotNull(ssl = wolfSSL_new(ctx));
  1456. /* invalid ssl */
  1457. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  1458. dhParamFile, WOLFSSL_FILETYPE_PEM));
  1459. /* invalid dhParamFile file */
  1460. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  1461. NULL, WOLFSSL_FILETYPE_PEM));
  1462. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  1463. bogusFile, WOLFSSL_FILETYPE_PEM));
  1464. /* success */
  1465. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  1466. WOLFSSL_FILETYPE_PEM));
  1467. wolfSSL_free(ssl);
  1468. wolfSSL_CTX_free(ctx);
  1469. #endif
  1470. }
  1471. static void test_wolfSSL_SetTmpDH_buffer(void)
  1472. {
  1473. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  1474. WOLFSSL_CTX *ctx;
  1475. WOLFSSL *ssl;
  1476. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1477. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  1478. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  1479. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  1480. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1481. AssertNotNull(ssl = wolfSSL_new(ctx));
  1482. /* invalid ssl */
  1483. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  1484. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1485. /* invalid dhParamFile file */
  1486. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  1487. 0, WOLFSSL_FILETYPE_ASN1));
  1488. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  1489. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1490. /* success */
  1491. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  1492. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1493. wolfSSL_free(ssl);
  1494. wolfSSL_CTX_free(ctx);
  1495. #endif
  1496. }
  1497. static void test_wolfSSL_SetMinMaxDhKey_Sz(void)
  1498. {
  1499. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  1500. WOLFSSL_CTX *ctx, *ctx2;
  1501. WOLFSSL *ssl, *ssl2;
  1502. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  1503. AssertNotNull(ctx);
  1504. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  1505. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  1506. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  1507. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1508. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  1509. ssl = wolfSSL_new(ctx);
  1510. AssertNotNull(ssl);
  1511. ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method());
  1512. AssertNotNull(ctx2);
  1513. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  1514. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  1515. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  1516. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1517. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  1518. ssl2 = wolfSSL_new(ctx2);
  1519. AssertNotNull(ssl2);
  1520. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  1521. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1522. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  1523. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  1524. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1525. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  1526. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  1527. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1528. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  1529. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1530. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  1531. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  1532. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1533. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  1534. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  1535. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1536. wolfSSL_free(ssl2);
  1537. wolfSSL_CTX_free(ctx2);
  1538. wolfSSL_free(ssl);
  1539. wolfSSL_CTX_free(ctx);
  1540. #endif
  1541. }
  1542. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  1543. * allowed.
  1544. * POST: return 1 on success.
  1545. */
  1546. static int test_wolfSSL_SetMinVersion(void)
  1547. {
  1548. int failFlag = WOLFSSL_SUCCESS;
  1549. #ifndef NO_WOLFSSL_CLIENT
  1550. WOLFSSL_CTX* ctx;
  1551. WOLFSSL* ssl;
  1552. int itr;
  1553. #ifndef NO_OLD_TLS
  1554. const int versions[] = {
  1555. #ifdef WOLFSSL_ALLOW_TLSV10
  1556. WOLFSSL_TLSV1,
  1557. #endif
  1558. WOLFSSL_TLSV1_1,
  1559. WOLFSSL_TLSV1_2};
  1560. #elif !defined(WOLFSSL_NO_TLS12)
  1561. const int versions[] = { WOLFSSL_TLSV1_2 };
  1562. #else
  1563. const int versions[] = { WOLFSSL_TLSV1_3 };
  1564. #endif
  1565. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1566. ssl = wolfSSL_new(ctx);
  1567. printf(testingFmt, "wolfSSL_SetMinVersion()");
  1568. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++){
  1569. if(wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS){
  1570. failFlag = WOLFSSL_FAILURE;
  1571. }
  1572. }
  1573. printf(resultFmt, failFlag == WOLFSSL_SUCCESS ? passed : failed);
  1574. wolfSSL_free(ssl);
  1575. wolfSSL_CTX_free(ctx);
  1576. #endif
  1577. return failFlag;
  1578. } /* END test_wolfSSL_SetMinVersion */
  1579. /*----------------------------------------------------------------------------*
  1580. | EC
  1581. *----------------------------------------------------------------------------*/
  1582. /* Test function for EC_POINT_new, EC_POINT_mul, EC_POINT_free,
  1583. EC_GROUP_new_by_curve_name, EC_GROUP_order_bits
  1584. */
  1585. # if defined(OPENSSL_EXTRA) && \
  1586. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  1587. static void test_wolfSSL_EC(void)
  1588. {
  1589. #if defined(HAVE_ECC)
  1590. BN_CTX *ctx;
  1591. EC_GROUP *group;
  1592. EC_GROUP *group2;
  1593. EC_POINT *Gxy, *new_point, *set_point;
  1594. BIGNUM *k = NULL, *Gx = NULL, *Gy = NULL, *Gz = NULL;
  1595. BIGNUM *X, *Y;
  1596. BIGNUM *set_point_bn;
  1597. char* hexStr;
  1598. int group_bits;
  1599. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD17AF381913FF7A96314EA47055EA0FD0";
  1600. /* NISTP256R1 Gx/Gy */
  1601. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
  1602. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5";
  1603. #ifndef HAVE_SELFTEST
  1604. EC_POINT *tmp;
  1605. size_t bin_len;
  1606. unsigned char* buf = NULL;
  1607. const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C2964FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5";
  1608. const unsigned char binUncompG[] = {
  1609. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  1610. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  1611. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  1612. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  1613. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  1614. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  1615. };
  1616. #ifdef HAVE_COMP_KEY
  1617. const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
  1618. const unsigned char binCompG[] = {
  1619. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  1620. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  1621. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  1622. };
  1623. #endif
  1624. #endif
  1625. AssertNotNull(ctx = BN_CTX_new());
  1626. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  1627. AssertNotNull(group2 = EC_GROUP_dup(group));
  1628. AssertIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  1629. AssertNotNull(Gxy = EC_POINT_new(group));
  1630. AssertNotNull(new_point = EC_POINT_new(group));
  1631. AssertNotNull(set_point = EC_POINT_new(group));
  1632. AssertNotNull(X = BN_new());
  1633. AssertNotNull(Y = BN_new());
  1634. AssertNotNull(set_point_bn = BN_new());
  1635. /* load test values */
  1636. AssertIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  1637. AssertIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  1638. AssertIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  1639. AssertIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  1640. /* populate coordinates for input point */
  1641. Gxy->X = Gx;
  1642. Gxy->Y = Gy;
  1643. Gxy->Z = Gz;
  1644. #ifndef HAVE_SELFTEST
  1645. /* perform point multiplication */
  1646. AssertIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), WOLFSSL_SUCCESS);
  1647. AssertIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), WOLFSSL_SUCCESS);
  1648. AssertIntEQ(BN_is_zero(new_point->X), 0);
  1649. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  1650. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  1651. AssertIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), WOLFSSL_SUCCESS);
  1652. AssertIntEQ(BN_is_zero(new_point->X), 0);
  1653. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  1654. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  1655. AssertIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), WOLFSSL_SUCCESS);
  1656. AssertIntEQ(BN_is_zero(new_point->X), 0);
  1657. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  1658. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  1659. #else
  1660. AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy, ctx), WOLFSSL_SUCCESS);
  1661. AssertIntEQ(BN_is_zero(new_point->X), 0);
  1662. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  1663. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  1664. #endif
  1665. /* check if point X coordinate is zero */
  1666. AssertIntEQ(BN_is_zero(new_point->X), 0);
  1667. #ifdef USE_ECC_B_PARAM
  1668. AssertIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  1669. #endif /* USE_ECC_B_PARAM */
  1670. /* Force non-affine coordinates */
  1671. AssertIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  1672. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  1673. new_point->inSet = 0;
  1674. /* extract the coordinates from point */
  1675. AssertIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y, ctx), WOLFSSL_SUCCESS);
  1676. /* check if point X coordinate is zero */
  1677. AssertIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  1678. /* set the same X and Y points in another object */
  1679. AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y, ctx), WOLFSSL_SUCCESS);
  1680. /* compare points as they should be the same */
  1681. AssertIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  1682. /* Test copying */
  1683. AssertIntEQ(EC_POINT_copy(new_point, set_point), 1);
  1684. /* Test inverting */
  1685. AssertIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  1686. AssertPtrEq(EC_POINT_point2bn(group, set_point, POINT_CONVERSION_UNCOMPRESSED,
  1687. set_point_bn, ctx), set_point_bn);
  1688. /* check bn2hex */
  1689. hexStr = BN_bn2hex(k);
  1690. AssertStrEQ(hexStr, kTest);
  1691. #ifndef NO_FILESYSTEM
  1692. BN_print_fp(stdout, k);
  1693. printf("\n");
  1694. #endif
  1695. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  1696. hexStr = BN_bn2hex(Gx);
  1697. AssertStrEQ(hexStr, kGx);
  1698. #ifndef NO_FILESYSTEM
  1699. BN_print_fp(stdout, Gx);
  1700. printf("\n");
  1701. #endif
  1702. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  1703. hexStr = BN_bn2hex(Gy);
  1704. AssertStrEQ(hexStr, kGy);
  1705. #ifndef NO_FILESYSTEM
  1706. BN_print_fp(stdout, Gy);
  1707. printf("\n");
  1708. #endif
  1709. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  1710. #ifndef HAVE_SELFTEST
  1711. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  1712. AssertStrEQ(hexStr, uncompG);
  1713. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  1714. #ifdef HAVE_COMP_KEY
  1715. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  1716. AssertStrEQ(hexStr, compG);
  1717. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  1718. #endif
  1719. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx);
  1720. AssertIntEQ(bin_len, sizeof(binUncompG));
  1721. AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL, DYNAMIC_TYPE_ECC));
  1722. AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, buf,
  1723. bin_len, ctx), bin_len);
  1724. AssertIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  1725. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  1726. #ifdef HAVE_COMP_KEY
  1727. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL, 0, ctx);
  1728. AssertIntEQ(bin_len, sizeof(binCompG));
  1729. AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL, DYNAMIC_TYPE_ECC));
  1730. AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  1731. bin_len, ctx), bin_len);
  1732. AssertIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  1733. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  1734. #endif
  1735. AssertNotNull(tmp = EC_POINT_new(group));
  1736. AssertIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG), ctx), 1);
  1737. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  1738. EC_POINT_free(tmp);
  1739. #ifdef HAVE_COMP_KEY
  1740. AssertNotNull(tmp = EC_POINT_new(group));
  1741. AssertIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx), 1);
  1742. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  1743. EC_POINT_free(tmp);
  1744. #endif
  1745. #endif
  1746. /* test BN_mod_add */
  1747. AssertIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  1748. (WOLFSSL_BIGNUM*)BN_value_one(),
  1749. (WOLFSSL_BIGNUM*)BN_value_one(), NULL), 1);
  1750. AssertIntEQ(BN_is_zero(new_point->Z), 1);
  1751. /* cleanup */
  1752. BN_free(X);
  1753. BN_free(Y);
  1754. BN_free(k);
  1755. BN_free(set_point_bn);
  1756. EC_POINT_free(new_point);
  1757. EC_POINT_free(set_point);
  1758. EC_POINT_free(Gxy);
  1759. EC_GROUP_free(group);
  1760. EC_GROUP_free(group2);
  1761. BN_CTX_free(ctx);
  1762. #endif /* HAVE_ECC */
  1763. }
  1764. #endif /* OPENSSL_EXTRA && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  1765. static void test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  1766. {
  1767. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA)
  1768. EC_GROUP *group;
  1769. BIO* bio;
  1770. AssertNotNull(bio = BIO_new(BIO_s_file()));
  1771. AssertIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  1772. AssertNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  1773. AssertIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  1774. EC_GROUP_free(group);
  1775. BIO_free(bio);
  1776. #endif /* HAVE_ECC */
  1777. }
  1778. # if defined(OPENSSL_EXTRA)
  1779. static void test_wolfSSL_ECDSA_SIG(void)
  1780. {
  1781. #ifdef HAVE_ECC
  1782. WOLFSSL_ECDSA_SIG* sig = NULL;
  1783. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  1784. const unsigned char* cp;
  1785. unsigned char* p;
  1786. unsigned char outSig[8];
  1787. unsigned char sigData[8] =
  1788. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  1789. sig = wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData));
  1790. AssertNull(sig);
  1791. cp = sigData;
  1792. AssertNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  1793. AssertIntEQ((cp == sigData + 8), 1);
  1794. cp = sigData;
  1795. AssertNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  1796. AssertNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  1797. AssertIntEQ((sig == sig2), 1);
  1798. cp = outSig;
  1799. p = outSig;
  1800. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  1801. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  1802. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  1803. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  1804. AssertIntEQ((p == outSig + 8), 1);
  1805. AssertIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  1806. wolfSSL_ECDSA_SIG_free(sig);
  1807. #endif /* HAVE_ECC */
  1808. }
  1809. static void test_EC_i2d(void)
  1810. {
  1811. #if defined(HAVE_ECC) && !defined(HAVE_FIPS)
  1812. EC_KEY *key;
  1813. EC_KEY *copy;
  1814. int len;
  1815. unsigned char *buf = NULL;
  1816. const unsigned char *tmp = NULL;
  1817. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  1818. AssertIntEQ(EC_KEY_generate_key(key), 1);
  1819. AssertIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  1820. AssertIntEQ(i2d_EC_PUBKEY(key, &buf), len);
  1821. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1822. buf = NULL;
  1823. AssertIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  1824. AssertIntEQ(i2d_ECPrivateKey(key, &buf), len);
  1825. tmp = buf;
  1826. AssertNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  1827. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1828. EC_KEY_free(key);
  1829. EC_KEY_free(copy);
  1830. #endif /* HAVE_ECC */
  1831. }
  1832. static void test_ECDSA_size_sign(void)
  1833. {
  1834. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  1835. EC_KEY *key;
  1836. int id;
  1837. byte hash[WC_MAX_DIGEST_SIZE];
  1838. byte sig[ECC_BUFSIZE];
  1839. unsigned int sigSz = sizeof(sig);
  1840. XMEMSET(hash, 123, sizeof(hash));
  1841. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  1842. AssertIntEQ(id, ECC_SECP256R1);
  1843. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  1844. AssertIntEQ(EC_KEY_generate_key(key), 1);
  1845. AssertIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  1846. AssertIntGE(ECDSA_size(key), sigSz);
  1847. EC_KEY_free(key);
  1848. #endif /* HAVE_ECC && !NO_ECC256 && !NO_ECC_SECP */
  1849. }
  1850. static void test_ED25519(void)
  1851. {
  1852. #if defined(HAVE_ED25519) && defined(WOLFSSL_KEY_GEN)
  1853. byte priv[ED25519_PRV_KEY_SIZE];
  1854. unsigned int privSz = (unsigned int)sizeof(priv);
  1855. byte pub[ED25519_PUB_KEY_SIZE];
  1856. unsigned int pubSz = (unsigned int)sizeof(pub);
  1857. const char* msg = "Everyone gets Friday off.";
  1858. unsigned int msglen = (unsigned int)XSTRLEN(msg);
  1859. byte sig[ED25519_SIG_SIZE];
  1860. unsigned int sigSz = (unsigned int)sizeof(sig);
  1861. AssertIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  1862. WOLFSSL_SUCCESS);
  1863. AssertIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  1864. AssertIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  1865. AssertIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  1866. &sigSz), WOLFSSL_SUCCESS);
  1867. AssertIntEQ(sigSz, ED25519_SIG_SIZE);
  1868. AssertIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  1869. sigSz), WOLFSSL_SUCCESS);
  1870. #endif /* HAVE_ED25519 && WOLFSSL_KEY_GEN */
  1871. }
  1872. static void test_ED448(void)
  1873. {
  1874. #if defined(HAVE_ED448) && defined(WOLFSSL_KEY_GEN)
  1875. byte priv[ED448_PRV_KEY_SIZE];
  1876. unsigned int privSz = (unsigned int)sizeof(priv);
  1877. byte pub[ED448_PUB_KEY_SIZE];
  1878. unsigned int pubSz = (unsigned int)sizeof(pub);
  1879. const char* msg = "Everyone gets Friday off.";
  1880. unsigned int msglen = (unsigned int)XSTRLEN(msg);
  1881. byte sig[ED448_SIG_SIZE];
  1882. unsigned int sigSz = (unsigned int)sizeof(sig);
  1883. AssertIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz),
  1884. WOLFSSL_SUCCESS);
  1885. AssertIntEQ(privSz, ED448_PRV_KEY_SIZE);
  1886. AssertIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  1887. AssertIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  1888. &sigSz), WOLFSSL_SUCCESS);
  1889. AssertIntEQ(sigSz, ED448_SIG_SIZE);
  1890. AssertIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  1891. sigSz), WOLFSSL_SUCCESS);
  1892. #endif /* HAVE_ED448 && WOLFSSL_KEY_GEN */
  1893. }
  1894. #endif /* OPENSSL_EXTRA */
  1895. #include <wolfssl/openssl/pem.h>
  1896. /*----------------------------------------------------------------------------*
  1897. | EVP
  1898. *----------------------------------------------------------------------------*/
  1899. /* Test function for wolfSSL_EVP_get_cipherbynid.
  1900. */
  1901. #ifdef OPENSSL_EXTRA
  1902. static void test_wolfSSL_EVP_get_cipherbynid(void)
  1903. {
  1904. #ifndef NO_AES
  1905. const WOLFSSL_EVP_CIPHER* c;
  1906. c = wolfSSL_EVP_get_cipherbynid(419);
  1907. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  1908. AssertNotNull(c);
  1909. AssertNotNull(strcmp("EVP_AES_128_CBC", c));
  1910. #else
  1911. AssertNull(c);
  1912. #endif
  1913. c = wolfSSL_EVP_get_cipherbynid(423);
  1914. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_192)
  1915. AssertNotNull(c);
  1916. AssertNotNull(strcmp("EVP_AES_192_CBC", c));
  1917. #else
  1918. AssertNull(c);
  1919. #endif
  1920. c = wolfSSL_EVP_get_cipherbynid(427);
  1921. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  1922. AssertNotNull(c);
  1923. AssertNotNull(strcmp("EVP_AES_256_CBC", c));
  1924. #else
  1925. AssertNull(c);
  1926. #endif
  1927. c = wolfSSL_EVP_get_cipherbynid(904);
  1928. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  1929. AssertNotNull(c);
  1930. AssertNotNull(strcmp("EVP_AES_128_CTR", c));
  1931. #else
  1932. AssertNull(c);
  1933. #endif
  1934. c = wolfSSL_EVP_get_cipherbynid(905);
  1935. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  1936. AssertNotNull(c);
  1937. AssertNotNull(strcmp("EVP_AES_192_CTR", c));
  1938. #else
  1939. AssertNull(c);
  1940. #endif
  1941. c = wolfSSL_EVP_get_cipherbynid(906);
  1942. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  1943. AssertNotNull(c);
  1944. AssertNotNull(strcmp("EVP_AES_256_CTR", c));
  1945. #else
  1946. AssertNull(c);
  1947. #endif
  1948. c = wolfSSL_EVP_get_cipherbynid(418);
  1949. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  1950. AssertNotNull(c);
  1951. AssertNotNull(strcmp("EVP_AES_128_ECB", c));
  1952. #else
  1953. AssertNull(c);
  1954. #endif
  1955. c = wolfSSL_EVP_get_cipherbynid(422);
  1956. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  1957. AssertNotNull(c);
  1958. AssertNotNull(strcmp("EVP_AES_192_ECB", c));
  1959. #else
  1960. AssertNull(c);
  1961. #endif
  1962. c = wolfSSL_EVP_get_cipherbynid(426);
  1963. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  1964. AssertNotNull(c);
  1965. AssertNotNull(strcmp("EVP_AES_256_ECB", c));
  1966. #else
  1967. AssertNull(c);
  1968. #endif
  1969. #endif /* !NO_AES */
  1970. #ifndef NO_DES3
  1971. AssertNotNull(strcmp("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  1972. #ifdef WOLFSSL_DES_ECB
  1973. AssertNotNull(strcmp("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  1974. #endif
  1975. AssertNotNull(strcmp("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  1976. #ifdef WOLFSSL_DES_ECB
  1977. AssertNotNull(strcmp("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  1978. #endif
  1979. #endif /* !NO_DES3 */
  1980. #ifdef HAVE_IDEA
  1981. AssertNotNull(strcmp("EVP_IDEA_CBC", wolfSSL_EVP_get_cipherbynid(34)));
  1982. #endif
  1983. /* test for nid is out of range */
  1984. AssertNull(wolfSSL_EVP_get_cipherbynid(1));
  1985. }
  1986. static void test_wolfSSL_EVP_CIPHER_CTX()
  1987. {
  1988. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  1989. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  1990. const EVP_CIPHER *init = EVP_aes_128_cbc();
  1991. const EVP_CIPHER *test;
  1992. byte key[AES_BLOCK_SIZE] = {0};
  1993. byte iv[AES_BLOCK_SIZE] = {0};
  1994. AssertNotNull(ctx);
  1995. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  1996. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  1997. test = EVP_CIPHER_CTX_cipher(ctx);
  1998. AssertTrue(init == test);
  1999. AssertIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  2000. AssertIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  2001. AssertIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  2002. EVP_CIPHER_CTX_free(ctx);
  2003. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  2004. }
  2005. #endif /* OPENSSL_EXTRA */
  2006. /*----------------------------------------------------------------------------*
  2007. | IO
  2008. *----------------------------------------------------------------------------*/
  2009. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2010. !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
  2011. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  2012. #define HAVE_IO_TESTS_DEPENDENCIES
  2013. #endif
  2014. /* helper functions */
  2015. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  2016. #ifdef WOLFSSL_SESSION_EXPORT
  2017. /* set up function for sending session information */
  2018. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  2019. {
  2020. WOLFSSL_CTX* ctx;
  2021. WOLFSSL* ssl;
  2022. AssertNotNull(inSsl);
  2023. AssertNotNull(buf);
  2024. AssertIntNE(0, sz);
  2025. /* Set ctx to DTLS 1.2 */
  2026. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  2027. AssertNotNull(ctx);
  2028. ssl = wolfSSL_new(ctx);
  2029. AssertNotNull(ssl);
  2030. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  2031. wolfSSL_free(ssl);
  2032. wolfSSL_CTX_free(ctx);
  2033. (void)userCtx;
  2034. return WOLFSSL_SUCCESS;
  2035. }
  2036. /* returns negative value on fail and positive (including 0) on success */
  2037. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  2038. {
  2039. int ret, err, loop_count, count, timeout = 10;
  2040. char msg[] = "I hear you fa shizzle!";
  2041. char input[1024];
  2042. loop_count = ((func_args*)args)->argc;
  2043. do {
  2044. #ifdef WOLFSSL_ASYNC_CRYPT
  2045. if (err == WC_PENDING_E) {
  2046. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  2047. if (ret < 0) { break; } else if (ret == 0) { continue; }
  2048. }
  2049. #endif
  2050. err = 0; /* Reset error */
  2051. ret = wolfSSL_accept(ssl);
  2052. if (ret != WOLFSSL_SUCCESS) {
  2053. err = wolfSSL_get_error(ssl, 0);
  2054. if (err == WOLFSSL_ERROR_WANT_READ ||
  2055. err == WOLFSSL_ERROR_WANT_WRITE) {
  2056. int select_ret;
  2057. err = WC_PENDING_E;
  2058. select_ret = tcp_select(*sockfd, timeout);
  2059. if (select_ret == TEST_TIMEOUT) {
  2060. return WOLFSSL_FATAL_ERROR;
  2061. }
  2062. }
  2063. }
  2064. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  2065. if (ret != WOLFSSL_SUCCESS) {
  2066. char buff[WOLFSSL_MAX_ERROR_SZ];
  2067. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  2068. return ret;
  2069. }
  2070. for (count = 0; count < loop_count; count++) {
  2071. int select_ret;
  2072. select_ret = tcp_select(*sockfd, timeout);
  2073. if (select_ret == TEST_TIMEOUT) {
  2074. ret = WOLFSSL_FATAL_ERROR;
  2075. break;
  2076. }
  2077. do {
  2078. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  2079. if (ret > 0) {
  2080. input[ret] = '\0';
  2081. printf("Client message: %s\n", input);
  2082. }
  2083. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  2084. do {
  2085. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  2086. return WOLFSSL_FATAL_ERROR;
  2087. }
  2088. err = wolfSSL_get_error(ssl, ret);
  2089. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  2090. }
  2091. return ret;
  2092. }
  2093. #endif /* WOLFSSL_SESSION_EXPORT */
  2094. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  2095. {
  2096. SOCKET_T sockfd = 0;
  2097. SOCKET_T clientfd = 0;
  2098. word16 port;
  2099. callback_functions* cbf;
  2100. WOLFSSL_CTX* ctx = 0;
  2101. WOLFSSL* ssl = 0;
  2102. char msg[] = "I hear you fa shizzle!";
  2103. char input[1024];
  2104. int idx;
  2105. int ret, err = 0;
  2106. int sharedCtx = 0;
  2107. #ifdef WOLFSSL_TIRTOS
  2108. fdOpenSession(Task_self());
  2109. #endif
  2110. ((func_args*)args)->return_code = TEST_FAIL;
  2111. cbf = ((func_args*)args)->callbacks;
  2112. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2113. if (cbf != NULL && cbf->ctx) {
  2114. ctx = cbf->ctx;
  2115. sharedCtx = 1;
  2116. }
  2117. else
  2118. #endif
  2119. {
  2120. WOLFSSL_METHOD* method = NULL;
  2121. if (cbf != NULL && cbf->method != NULL) {
  2122. method = cbf->method();
  2123. }
  2124. else {
  2125. method = wolfSSLv23_server_method();
  2126. }
  2127. ctx = wolfSSL_CTX_new(method);
  2128. }
  2129. #if defined(HAVE_SESSION_TICKET) && defined(HAVE_CHACHA) && \
  2130. defined(HAVE_POLY1305)
  2131. TicketInit();
  2132. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  2133. #endif
  2134. #if defined(USE_WINDOWS_API)
  2135. port = ((func_args*)args)->signal->port;
  2136. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  2137. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  2138. /* Let tcp_listen assign port */
  2139. port = 0;
  2140. #else
  2141. /* Use default port */
  2142. port = wolfSSLPort;
  2143. #endif
  2144. /* do it here to detect failure */
  2145. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1);
  2146. CloseSocket(sockfd);
  2147. wolfSSL_CTX_set_verify(ctx,
  2148. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  2149. #ifdef WOLFSSL_ENCRYPTED_KEYS
  2150. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  2151. #endif
  2152. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  2153. != WOLFSSL_SUCCESS) {
  2154. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  2155. goto done;
  2156. }
  2157. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2158. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  2159. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2160. #else
  2161. if (wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  2162. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2163. #endif
  2164. /*err_sys("can't load server cert chain file, "
  2165. "Please run from wolfSSL home dir");*/
  2166. goto done;
  2167. }
  2168. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2169. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  2170. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2171. #else
  2172. if (wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  2173. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2174. #endif
  2175. /*err_sys("can't load server key file, "
  2176. "Please run from wolfSSL home dir");*/
  2177. goto done;
  2178. }
  2179. /* call ctx setup callback */
  2180. if (cbf != NULL && cbf->ctx_ready != NULL) {
  2181. cbf->ctx_ready(ctx);
  2182. }
  2183. ssl = wolfSSL_new(ctx);
  2184. if (ssl == NULL) {
  2185. goto done;
  2186. }
  2187. #ifdef WOLFSSL_SESSION_EXPORT
  2188. /* only add in more complex nonblocking case with session export tests */
  2189. if (args && ((func_args*)args)->argc > 0) {
  2190. /* set as nonblock and time out for waiting on read/write */
  2191. tcp_set_nonblocking(&clientfd);
  2192. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  2193. }
  2194. #endif
  2195. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2196. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  2197. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2198. #else
  2199. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  2200. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2201. #endif
  2202. /*err_sys("can't load server cert chain file, "
  2203. "Please run from wolfSSL home dir");*/
  2204. goto done;
  2205. }
  2206. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2207. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  2208. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2209. #else
  2210. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  2211. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2212. #endif
  2213. /*err_sys("can't load server key file, "
  2214. "Please run from wolfSSL home dir");*/
  2215. goto done;
  2216. }
  2217. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  2218. /*err_sys("SSL_set_fd failed");*/
  2219. goto done;
  2220. }
  2221. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  2222. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  2223. #elif !defined(NO_DH)
  2224. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  2225. #endif
  2226. /* call ssl setup callback */
  2227. if (cbf != NULL && cbf->ssl_ready != NULL) {
  2228. cbf->ssl_ready(ssl);
  2229. }
  2230. #ifdef WOLFSSL_SESSION_EXPORT
  2231. /* only add in more complex nonblocking case with session export tests */
  2232. if (((func_args*)args)->argc > 0) {
  2233. ret = nonblocking_accept_read(args, ssl, &clientfd);
  2234. if (ret >= 0) {
  2235. ((func_args*)args)->return_code = TEST_SUCCESS;
  2236. }
  2237. #ifdef WOLFSSL_TIRTOS
  2238. Task_yield();
  2239. #endif
  2240. goto done;
  2241. }
  2242. #endif
  2243. do {
  2244. #ifdef WOLFSSL_ASYNC_CRYPT
  2245. if (err == WC_PENDING_E) {
  2246. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  2247. if (ret < 0) { break; } else if (ret == 0) { continue; }
  2248. }
  2249. #endif
  2250. err = 0; /* Reset error */
  2251. ret = wolfSSL_accept(ssl);
  2252. if (ret != WOLFSSL_SUCCESS) {
  2253. err = wolfSSL_get_error(ssl, 0);
  2254. }
  2255. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  2256. if (ret != WOLFSSL_SUCCESS) {
  2257. char buff[WOLFSSL_MAX_ERROR_SZ];
  2258. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  2259. /*err_sys("SSL_accept failed");*/
  2260. goto done;
  2261. }
  2262. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  2263. if (idx > 0) {
  2264. input[idx] = '\0';
  2265. printf("Client message: %s\n", input);
  2266. }
  2267. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  2268. /*err_sys("SSL_write failed");*/
  2269. #ifdef WOLFSSL_TIRTOS
  2270. return;
  2271. #else
  2272. return 0;
  2273. #endif
  2274. }
  2275. #ifdef WOLFSSL_TIRTOS
  2276. Task_yield();
  2277. #endif
  2278. ((func_args*)args)->return_code = TEST_SUCCESS;
  2279. done:
  2280. wolfSSL_shutdown(ssl);
  2281. wolfSSL_free(ssl);
  2282. if (!sharedCtx)
  2283. wolfSSL_CTX_free(ctx);
  2284. CloseSocket(clientfd);
  2285. #ifdef WOLFSSL_TIRTOS
  2286. fdCloseSession(Task_self());
  2287. #endif
  2288. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  2289. && defined(HAVE_THREAD_LS)
  2290. wc_ecc_fp_free(); /* free per thread cache */
  2291. #endif
  2292. #if defined(HAVE_SESSION_TICKET) && defined(HAVE_CHACHA) && \
  2293. defined(HAVE_POLY1305)
  2294. TicketCleanup();
  2295. #endif
  2296. #ifndef WOLFSSL_TIRTOS
  2297. return 0;
  2298. #endif
  2299. }
  2300. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  2301. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  2302. {
  2303. SOCKET_T sockfd = 0;
  2304. SOCKET_T clientfd = 0;
  2305. word16 port;
  2306. callback_functions* cbf;
  2307. WOLFSSL_CTX* ctx = 0;
  2308. WOLFSSL* ssl = 0;
  2309. char msg[] = "I hear you fa shizzle!";
  2310. char input[1024];
  2311. int idx;
  2312. int ret, err = 0;
  2313. int sharedCtx = 0;
  2314. int loop_count = ((func_args*)args)->argc;
  2315. int count = 0;
  2316. #ifdef WOLFSSL_TIRTOS
  2317. fdOpenSession(Task_self());
  2318. #endif
  2319. ((func_args*)args)->return_code = TEST_FAIL;
  2320. cbf = ((func_args*)args)->callbacks;
  2321. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2322. if (cbf != NULL && cbf->ctx) {
  2323. ctx = cbf->ctx;
  2324. sharedCtx = 1;
  2325. }
  2326. else
  2327. #endif
  2328. {
  2329. WOLFSSL_METHOD* method = NULL;
  2330. if (cbf != NULL && cbf->method != NULL) {
  2331. method = cbf->method();
  2332. }
  2333. else {
  2334. method = wolfSSLv23_server_method();
  2335. }
  2336. ctx = wolfSSL_CTX_new(method);
  2337. }
  2338. #if defined(USE_WINDOWS_API)
  2339. port = ((func_args*)args)->signal->port;
  2340. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  2341. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  2342. /* Let tcp_listen assign port */
  2343. port = 0;
  2344. #else
  2345. /* Use default port */
  2346. port = wolfSSLPort;
  2347. #endif
  2348. wolfSSL_CTX_set_verify(ctx,
  2349. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  2350. #ifdef WOLFSSL_ENCRYPTED_KEYS
  2351. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  2352. #endif
  2353. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  2354. != WOLFSSL_SUCCESS) {
  2355. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  2356. goto done;
  2357. }
  2358. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  2359. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2360. /*err_sys("can't load server cert chain file, "
  2361. "Please run from wolfSSL home dir");*/
  2362. goto done;
  2363. }
  2364. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  2365. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2366. /*err_sys("can't load server key file, "
  2367. "Please run from wolfSSL home dir");*/
  2368. goto done;
  2369. }
  2370. /* call ctx setup callback */
  2371. if (cbf != NULL && cbf->ctx_ready != NULL) {
  2372. cbf->ctx_ready(ctx);
  2373. }
  2374. while(count != loop_count) {
  2375. ssl = wolfSSL_new(ctx);
  2376. if (ssl == NULL) {
  2377. goto done;
  2378. }
  2379. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  2380. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2381. /*err_sys("can't load server cert chain file, "
  2382. "Please run from wolfSSL home dir");*/
  2383. goto done;
  2384. }
  2385. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  2386. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2387. /*err_sys("can't load server key file, "
  2388. "Please run from wolfSSL home dir");*/
  2389. goto done;
  2390. }
  2391. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  2392. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  2393. #elif !defined(NO_DH)
  2394. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  2395. #endif
  2396. /* call ssl setup callback */
  2397. if (cbf != NULL && cbf->ssl_ready != NULL) {
  2398. cbf->ssl_ready(ssl);
  2399. }
  2400. /* do it here to detect failure */
  2401. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1);
  2402. CloseSocket(sockfd);
  2403. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  2404. /*err_sys("SSL_set_fd failed");*/
  2405. goto done;
  2406. }
  2407. do {
  2408. #ifdef WOLFSSL_ASYNC_CRYPT
  2409. if (err == WC_PENDING_E) {
  2410. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  2411. if (ret < 0) { break; } else if (ret == 0) { continue; }
  2412. }
  2413. #endif
  2414. err = 0; /* Reset error */
  2415. ret = wolfSSL_accept(ssl);
  2416. if (ret != WOLFSSL_SUCCESS) {
  2417. err = wolfSSL_get_error(ssl, 0);
  2418. }
  2419. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  2420. if (ret != WOLFSSL_SUCCESS) {
  2421. char buff[WOLFSSL_MAX_ERROR_SZ];
  2422. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  2423. /*err_sys("SSL_accept failed");*/
  2424. goto done;
  2425. }
  2426. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  2427. if (idx > 0) {
  2428. input[idx] = '\0';
  2429. printf("Client message: %s\n", input);
  2430. }
  2431. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  2432. /*err_sys("SSL_write failed");*/
  2433. #ifdef WOLFSSL_TIRTOS
  2434. return;
  2435. #else
  2436. return 0;
  2437. #endif
  2438. }
  2439. /* free ssl for this connection */
  2440. wolfSSL_shutdown(ssl);
  2441. wolfSSL_free(ssl); ssl = NULL;
  2442. CloseSocket(clientfd);
  2443. count++;
  2444. }
  2445. #ifdef WOLFSSL_TIRTOS
  2446. Task_yield();
  2447. #endif
  2448. ((func_args*)args)->return_code = TEST_SUCCESS;
  2449. done:
  2450. if(ssl != NULL) {
  2451. wolfSSL_shutdown(ssl);
  2452. wolfSSL_free(ssl);
  2453. }
  2454. if (!sharedCtx)
  2455. wolfSSL_CTX_free(ctx);
  2456. CloseSocket(clientfd);
  2457. #ifdef WOLFSSL_TIRTOS
  2458. fdCloseSession(Task_self());
  2459. #endif
  2460. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  2461. && defined(HAVE_THREAD_LS)
  2462. wc_ecc_fp_free(); /* free per thread cache */
  2463. #endif
  2464. #ifndef WOLFSSL_TIRTOS
  2465. return 0;
  2466. #endif
  2467. }
  2468. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  2469. typedef int (*cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  2470. static void test_client_nofail(void* args, void *cb)
  2471. {
  2472. SOCKET_T sockfd = 0;
  2473. callback_functions* cbf;
  2474. WOLFSSL_CTX* ctx = 0;
  2475. WOLFSSL* ssl = 0;
  2476. WOLFSSL_CIPHER* cipher;
  2477. char msg[64] = "hello wolfssl!";
  2478. char reply[1024];
  2479. int input;
  2480. int msgSz = (int)XSTRLEN(msg);
  2481. int ret, err = 0;
  2482. int cipherSuite;
  2483. int sharedCtx = 0;
  2484. const char* cipherName1, *cipherName2;
  2485. #ifdef WOLFSSL_TIRTOS
  2486. fdOpenSession(Task_self());
  2487. #endif
  2488. ((func_args*)args)->return_code = TEST_FAIL;
  2489. cbf = ((func_args*)args)->callbacks;
  2490. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2491. if (cbf != NULL && cbf->ctx) {
  2492. ctx = cbf->ctx;
  2493. sharedCtx = cbf->isSharedCtx;
  2494. }
  2495. else
  2496. #endif
  2497. {
  2498. WOLFSSL_METHOD* method = NULL;
  2499. if (cbf != NULL && cbf->method != NULL) {
  2500. method = cbf->method();
  2501. }
  2502. else {
  2503. method = wolfSSLv23_client_method();
  2504. }
  2505. ctx = wolfSSL_CTX_new(method);
  2506. }
  2507. #ifdef WOLFSSL_ENCRYPTED_KEYS
  2508. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  2509. #endif
  2510. /* Do connect here so server detects failures */
  2511. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  2512. 0, 0, NULL);
  2513. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  2514. {
  2515. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  2516. goto done;
  2517. }
  2518. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2519. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  2520. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2521. #else
  2522. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  2523. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2524. #endif
  2525. /*err_sys("can't load client cert file, "
  2526. "Please run from wolfSSL home dir");*/
  2527. goto done;
  2528. }
  2529. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2530. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  2531. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2532. #else
  2533. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  2534. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2535. #endif
  2536. /*err_sys("can't load client key file, "
  2537. "Please run from wolfSSL home dir");*/
  2538. goto done;
  2539. }
  2540. /* call ctx setup callback */
  2541. if (cbf != NULL && cbf->ctx_ready != NULL) {
  2542. cbf->ctx_ready(ctx);
  2543. }
  2544. ssl = wolfSSL_new(ctx);
  2545. if (ssl == NULL) {
  2546. goto done;
  2547. }
  2548. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2549. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  2550. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2551. #else
  2552. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  2553. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2554. #endif
  2555. /*err_sys("can't load client cert file, "
  2556. "Please run from wolfSSL home dir");*/
  2557. goto done;
  2558. }
  2559. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2560. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  2561. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2562. #else
  2563. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  2564. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2565. #endif
  2566. /*err_sys("can't load client key file, "
  2567. "Please run from wolfSSL home dir");*/
  2568. goto done;
  2569. }
  2570. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  2571. /*err_sys("SSL_set_fd failed");*/
  2572. goto done;
  2573. }
  2574. /* call ssl setup callback */
  2575. if (cbf != NULL && cbf->ssl_ready != NULL) {
  2576. cbf->ssl_ready(ssl);
  2577. }
  2578. do {
  2579. #ifdef WOLFSSL_ASYNC_CRYPT
  2580. if (err == WC_PENDING_E) {
  2581. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  2582. if (ret < 0) { break; } else if (ret == 0) { continue; }
  2583. }
  2584. #endif
  2585. err = 0; /* Reset error */
  2586. ret = wolfSSL_connect(ssl);
  2587. if (ret != WOLFSSL_SUCCESS) {
  2588. err = wolfSSL_get_error(ssl, 0);
  2589. }
  2590. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  2591. if (ret != WOLFSSL_SUCCESS) {
  2592. char buff[WOLFSSL_MAX_ERROR_SZ];
  2593. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  2594. /*err_sys("SSL_connect failed");*/
  2595. goto done;
  2596. }
  2597. /* test the various get cipher methods */
  2598. /* Internal cipher suite names */
  2599. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  2600. cipherName1 = wolfSSL_get_cipher_name(ssl);
  2601. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  2602. (cipherSuite >> 8), cipherSuite & 0xFF);
  2603. AssertStrEQ(cipherName1, cipherName2);
  2604. /* IANA Cipher Suites Names */
  2605. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  2606. then it's the internal cipher suite name */
  2607. cipher = wolfSSL_get_current_cipher(ssl);
  2608. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  2609. cipherName2 = wolfSSL_get_cipher(ssl);
  2610. AssertStrEQ(cipherName1, cipherName2);
  2611. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  2612. !defined(WOLFSSL_QT)
  2613. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  2614. (cipherSuite >> 8), cipherSuite & 0xFF);
  2615. AssertStrEQ(cipherName1, cipherName2);
  2616. #endif
  2617. if (cb != NULL)
  2618. ((cbType)cb)(ctx, ssl);
  2619. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  2620. /*err_sys("SSL_write failed");*/
  2621. goto done;
  2622. }
  2623. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  2624. if (input > 0) {
  2625. reply[input] = '\0';
  2626. printf("Server response: %s\n", reply);
  2627. }
  2628. ((func_args*)args)->return_code = TEST_SUCCESS;
  2629. done:
  2630. wolfSSL_free(ssl);
  2631. if (!sharedCtx)
  2632. wolfSSL_CTX_free(ctx);
  2633. CloseSocket(sockfd);
  2634. #ifdef WOLFSSL_TIRTOS
  2635. fdCloseSession(Task_self());
  2636. #endif
  2637. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  2638. && defined(HAVE_THREAD_LS)
  2639. wc_ecc_fp_free(); /* free per thread cache */
  2640. #endif
  2641. return;
  2642. }
  2643. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  2644. static void test_client_reuse_WOLFSSLobj(void* args, void *cb, void* server_args)
  2645. {
  2646. SOCKET_T sockfd = 0;
  2647. callback_functions* cbf;
  2648. WOLFSSL_CTX* ctx = 0;
  2649. WOLFSSL* ssl = 0;
  2650. WOLFSSL_SESSION* session = NULL;
  2651. char msg[64] = "hello wolfssl!";
  2652. char reply[1024];
  2653. int input;
  2654. int msgSz = (int)XSTRLEN(msg);
  2655. int ret, err = 0;
  2656. int sharedCtx = 0;
  2657. #ifdef WOLFSSL_TIRTOS
  2658. fdOpenSession(Task_self());
  2659. #endif
  2660. ((func_args*)args)->return_code = TEST_FAIL;
  2661. cbf = ((func_args*)args)->callbacks;
  2662. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2663. if (cbf != NULL && cbf->ctx) {
  2664. ctx = cbf->ctx;
  2665. sharedCtx = 1;
  2666. }
  2667. else
  2668. #endif
  2669. {
  2670. WOLFSSL_METHOD* method = NULL;
  2671. if (cbf != NULL && cbf->method != NULL) {
  2672. method = cbf->method();
  2673. }
  2674. else {
  2675. method = wolfSSLv23_client_method();
  2676. }
  2677. ctx = wolfSSL_CTX_new(method);
  2678. }
  2679. #ifdef WOLFSSL_ENCRYPTED_KEYS
  2680. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  2681. #endif
  2682. /* Do connect here so server detects failures */
  2683. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  2684. 0, 0, NULL);
  2685. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  2686. {
  2687. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  2688. goto done;
  2689. }
  2690. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  2691. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2692. /*err_sys("can't load client cert file, "
  2693. "Please run from wolfSSL home dir");*/
  2694. goto done;
  2695. }
  2696. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  2697. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2698. /*err_sys("can't load client key file, "
  2699. "Please run from wolfSSL home dir");*/
  2700. goto done;
  2701. }
  2702. /* call ctx setup callback */
  2703. if (cbf != NULL && cbf->ctx_ready != NULL) {
  2704. cbf->ctx_ready(ctx);
  2705. }
  2706. ssl = wolfSSL_new(ctx);
  2707. if (ssl == NULL) {
  2708. goto done;
  2709. }
  2710. /* keep handshakre resources for re-using WOLFSSL obj */
  2711. wolfSSL_KeepArrays(ssl);
  2712. if(wolfSSL_KeepHandshakeResources(ssl)) {
  2713. /* err_sys("SSL_KeepHandshakeResources failed"); */
  2714. goto done;
  2715. }
  2716. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  2717. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2718. /*err_sys("can't load client cert file, "
  2719. "Please run from wolfSSL home dir");*/
  2720. goto done;
  2721. }
  2722. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  2723. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2724. /*err_sys("can't load client key file, "
  2725. "Please run from wolfSSL home dir");*/
  2726. goto done;
  2727. }
  2728. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  2729. /*err_sys("SSL_set_fd failed");*/
  2730. goto done;
  2731. }
  2732. /* call ssl setup callback */
  2733. if (cbf != NULL && cbf->ssl_ready != NULL) {
  2734. cbf->ssl_ready(ssl);
  2735. }
  2736. do {
  2737. #ifdef WOLFSSL_ASYNC_CRYPT
  2738. if (err == WC_PENDING_E) {
  2739. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  2740. if (ret < 0) { break; } else if (ret == 0) { continue; }
  2741. }
  2742. #endif
  2743. err = 0; /* Reset error */
  2744. ret = wolfSSL_connect(ssl);
  2745. if (ret != WOLFSSL_SUCCESS) {
  2746. err = wolfSSL_get_error(ssl, 0);
  2747. }
  2748. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  2749. if (ret != WOLFSSL_SUCCESS) {
  2750. char buff[WOLFSSL_MAX_ERROR_SZ];
  2751. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  2752. /*err_sys("SSL_connect failed");*/
  2753. goto done;
  2754. }
  2755. /* Build first session */
  2756. if (cb != NULL)
  2757. ((cbType)cb)(ctx, ssl);
  2758. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  2759. /*err_sys("SSL_write failed");*/
  2760. goto done;
  2761. }
  2762. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  2763. if (input > 0) {
  2764. reply[input] = '\0';
  2765. printf("Server response: %s\n", reply);
  2766. }
  2767. /* Session Resumption by re-using WOLFSSL object */
  2768. wolfSSL_set_quiet_shutdown(ssl, 1);
  2769. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  2770. /* err_sys ("SSL shutdown failed"); */
  2771. goto done;
  2772. }
  2773. session = wolfSSL_get_session(ssl);
  2774. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  2775. /* err_sys ("SSL_clear failed"); */
  2776. goto done;
  2777. }
  2778. wolfSSL_set_session(ssl, session);
  2779. /* close socket once */
  2780. CloseSocket(sockfd);
  2781. sockfd = 0;
  2782. /* wait until server ready */
  2783. wait_tcp_ready((func_args*)server_args);
  2784. printf("session resumption\n");
  2785. /* Do re-connect */
  2786. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  2787. 0, 0, NULL);
  2788. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  2789. /*err_sys("SSL_set_fd failed");*/
  2790. goto done;
  2791. }
  2792. do {
  2793. #ifdef WOLFSSL_ASYNC_CRYPT
  2794. if (err == WC_PENDING_E) {
  2795. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  2796. if (ret < 0) { break; } else if (ret == 0) { continue; }
  2797. }
  2798. #endif
  2799. err = 0; /* Reset error */
  2800. ret = wolfSSL_connect(ssl);
  2801. if (ret != WOLFSSL_SUCCESS) {
  2802. err = wolfSSL_get_error(ssl, 0);
  2803. }
  2804. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  2805. if (ret != WOLFSSL_SUCCESS) {
  2806. char buff[WOLFSSL_MAX_ERROR_SZ];
  2807. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  2808. /*err_sys("SSL_connect failed");*/
  2809. goto done;
  2810. }
  2811. /* Build first session */
  2812. if (cb != NULL)
  2813. ((cbType)cb)(ctx, ssl);
  2814. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  2815. /*err_sys("SSL_write failed");*/
  2816. goto done;
  2817. }
  2818. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  2819. if (input > 0) {
  2820. reply[input] = '\0';
  2821. printf("Server response: %s\n", reply);
  2822. }
  2823. ((func_args*)args)->return_code = TEST_SUCCESS;
  2824. done:
  2825. wolfSSL_free(ssl);
  2826. if (!sharedCtx)
  2827. wolfSSL_CTX_free(ctx);
  2828. CloseSocket(sockfd);
  2829. #ifdef WOLFSSL_TIRTOS
  2830. fdCloseSession(Task_self());
  2831. #endif
  2832. return;
  2833. }
  2834. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  2835. /* SNI / ALPN / session export helper functions */
  2836. #if defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLFSSL_SESSION_EXPORT)
  2837. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  2838. {
  2839. callback_functions* callbacks = ((func_args*)args)->callbacks;
  2840. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(callbacks->method());
  2841. WOLFSSL* ssl = NULL;
  2842. SOCKET_T sfd = 0;
  2843. SOCKET_T cfd = 0;
  2844. word16 port;
  2845. char msg[] = "I hear you fa shizzle!";
  2846. int len = (int) XSTRLEN(msg);
  2847. char input[1024];
  2848. int idx;
  2849. int ret, err = 0;
  2850. #ifdef WOLFSSL_TIRTOS
  2851. fdOpenSession(Task_self());
  2852. #endif
  2853. ((func_args*)args)->return_code = TEST_FAIL;
  2854. #if defined(USE_WINDOWS_API)
  2855. port = ((func_args*)args)->signal->port;
  2856. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  2857. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  2858. /* Let tcp_listen assign port */
  2859. port = 0;
  2860. #else
  2861. /* Use default port */
  2862. port = wolfSSLPort;
  2863. #endif
  2864. wolfSSL_CTX_set_verify(ctx,
  2865. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  2866. #ifdef WOLFSSL_ENCRYPTED_KEYS
  2867. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  2868. #endif
  2869. #ifdef WOLFSSL_SESSION_EXPORT
  2870. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_dtls_set_export(ctx, test_export));
  2871. #endif
  2872. AssertIntEQ(WOLFSSL_SUCCESS,
  2873. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  2874. AssertIntEQ(WOLFSSL_SUCCESS,
  2875. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  2876. WOLFSSL_FILETYPE_PEM));
  2877. AssertIntEQ(WOLFSSL_SUCCESS,
  2878. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  2879. if (callbacks->ctx_ready)
  2880. callbacks->ctx_ready(ctx);
  2881. ssl = wolfSSL_new(ctx);
  2882. if (wolfSSL_dtls(ssl)) {
  2883. SOCKADDR_IN_T cliAddr;
  2884. socklen_t cliLen;
  2885. cliLen = sizeof(cliAddr);
  2886. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0);
  2887. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  2888. (struct sockaddr*)&cliAddr, &cliLen);
  2889. AssertIntGT(idx, 0);
  2890. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  2891. }
  2892. else {
  2893. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1);
  2894. CloseSocket(sfd);
  2895. }
  2896. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  2897. #ifdef NO_PSK
  2898. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  2899. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  2900. #elif !defined(NO_DH)
  2901. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  2902. #endif
  2903. #endif
  2904. if (callbacks->ssl_ready)
  2905. callbacks->ssl_ready(ssl);
  2906. do {
  2907. #ifdef WOLFSSL_ASYNC_CRYPT
  2908. if (err == WC_PENDING_E) {
  2909. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  2910. if (ret < 0) { break; } else if (ret == 0) { continue; }
  2911. }
  2912. #endif
  2913. err = 0; /* Reset error */
  2914. ret = wolfSSL_accept(ssl);
  2915. if (ret != WOLFSSL_SUCCESS) {
  2916. err = wolfSSL_get_error(ssl, 0);
  2917. }
  2918. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  2919. if (ret != WOLFSSL_SUCCESS) {
  2920. char buff[WOLFSSL_MAX_ERROR_SZ];
  2921. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  2922. /*err_sys("SSL_accept failed");*/
  2923. }
  2924. else {
  2925. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  2926. input[idx] = 0;
  2927. printf("Client message: %s\n", input);
  2928. }
  2929. AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
  2930. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL)
  2931. if (wolfSSL_dtls(ssl)) {
  2932. byte* import;
  2933. word32 sz;
  2934. wolfSSL_dtls_export(ssl, NULL, &sz);
  2935. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2936. AssertNotNull(import);
  2937. idx = wolfSSL_dtls_export(ssl, import, &sz);
  2938. AssertIntGE(idx, 0);
  2939. AssertIntGE(wolfSSL_dtls_import(ssl, import, idx), 0);
  2940. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2941. }
  2942. #endif
  2943. #ifdef WOLFSSL_TIRTOS
  2944. Task_yield();
  2945. #endif
  2946. ((func_args*)args)->return_code = TEST_SUCCESS;
  2947. }
  2948. if (callbacks->on_result)
  2949. callbacks->on_result(ssl);
  2950. wolfSSL_shutdown(ssl);
  2951. wolfSSL_free(ssl);
  2952. wolfSSL_CTX_free(ctx);
  2953. CloseSocket(cfd);
  2954. #ifdef WOLFSSL_TIRTOS
  2955. fdCloseSession(Task_self());
  2956. #endif
  2957. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  2958. && defined(HAVE_THREAD_LS)
  2959. wc_ecc_fp_free(); /* free per thread cache */
  2960. #endif
  2961. #ifndef WOLFSSL_TIRTOS
  2962. return 0;
  2963. #endif
  2964. }
  2965. static void run_wolfssl_client(void* args)
  2966. {
  2967. callback_functions* callbacks = ((func_args*)args)->callbacks;
  2968. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(callbacks->method());
  2969. WOLFSSL* ssl = NULL;
  2970. SOCKET_T sfd = 0;
  2971. char msg[] = "hello wolfssl server!";
  2972. int len = (int) XSTRLEN(msg);
  2973. char input[1024];
  2974. int idx;
  2975. int ret, err = 0;
  2976. #ifdef WOLFSSL_TIRTOS
  2977. fdOpenSession(Task_self());
  2978. #endif
  2979. ((func_args*)args)->return_code = TEST_FAIL;
  2980. #ifdef WOLFSSL_ENCRYPTED_KEYS
  2981. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  2982. #endif
  2983. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  2984. AssertIntEQ(WOLFSSL_SUCCESS,
  2985. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, WOLFSSL_FILETYPE_PEM));
  2986. AssertIntEQ(WOLFSSL_SUCCESS,
  2987. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, WOLFSSL_FILETYPE_PEM));
  2988. if (callbacks->ctx_ready)
  2989. callbacks->ctx_ready(ctx);
  2990. ssl = wolfSSL_new(ctx);
  2991. if (wolfSSL_dtls(ssl)) {
  2992. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  2993. 1, 0, ssl);
  2994. }
  2995. else {
  2996. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  2997. 0, 0, ssl);
  2998. }
  2999. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  3000. if (callbacks->ssl_ready)
  3001. callbacks->ssl_ready(ssl);
  3002. do {
  3003. #ifdef WOLFSSL_ASYNC_CRYPT
  3004. if (err == WC_PENDING_E) {
  3005. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  3006. if (ret < 0) { break; } else if (ret == 0) { continue; }
  3007. }
  3008. #endif
  3009. err = 0; /* Reset error */
  3010. ret = wolfSSL_connect(ssl);
  3011. if (ret != WOLFSSL_SUCCESS) {
  3012. err = wolfSSL_get_error(ssl, 0);
  3013. }
  3014. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  3015. if (ret != WOLFSSL_SUCCESS) {
  3016. char buff[WOLFSSL_MAX_ERROR_SZ];
  3017. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  3018. /*err_sys("SSL_connect failed");*/
  3019. }
  3020. else {
  3021. AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
  3022. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  3023. input[idx] = 0;
  3024. printf("Server response: %s\n", input);
  3025. }
  3026. ((func_args*)args)->return_code = TEST_SUCCESS;
  3027. }
  3028. if (callbacks->on_result)
  3029. callbacks->on_result(ssl);
  3030. wolfSSL_free(ssl);
  3031. wolfSSL_CTX_free(ctx);
  3032. CloseSocket(sfd);
  3033. #ifdef WOLFSSL_TIRTOS
  3034. fdCloseSession(Task_self());
  3035. #endif
  3036. }
  3037. #endif /* defined(HAVE_SNI) || defined(HAVE_ALPN) ||
  3038. defined(WOLFSSL_SESSION_EXPORT) */
  3039. static void test_wolfSSL_read_write(void)
  3040. {
  3041. /* The unit testing for read and write shall happen simultaneously, since
  3042. * one can't do anything with one without the other. (Except for a failure
  3043. * test case.) This function will call all the others that will set up,
  3044. * execute, and report their test findings.
  3045. *
  3046. * Set up the success case first. This function will become the template
  3047. * for the other tests. This should eventually be renamed
  3048. *
  3049. * The success case isn't interesting, how can this fail?
  3050. * - Do not give the client context a CA certificate. The connect should
  3051. * fail. Do not need server for this?
  3052. * - Using NULL for the ssl object on server. Do not need client for this.
  3053. * - Using NULL for the ssl object on client. Do not need server for this.
  3054. * - Good ssl objects for client and server. Client write() without server
  3055. * read().
  3056. * - Good ssl objects for client and server. Server write() without client
  3057. * read().
  3058. * - Forgetting the password callback?
  3059. */
  3060. tcp_ready ready;
  3061. func_args client_args;
  3062. func_args server_args;
  3063. THREAD_TYPE serverThread;
  3064. XMEMSET(&client_args, 0, sizeof(func_args));
  3065. XMEMSET(&server_args, 0, sizeof(func_args));
  3066. #ifdef WOLFSSL_TIRTOS
  3067. fdOpenSession(Task_self());
  3068. #endif
  3069. StartTCP();
  3070. InitTcpReady(&ready);
  3071. #if defined(USE_WINDOWS_API)
  3072. /* use RNG to get random port if using windows */
  3073. ready.port = GetRandomPort();
  3074. #endif
  3075. server_args.signal = &ready;
  3076. client_args.signal = &ready;
  3077. start_thread(test_server_nofail, &server_args, &serverThread);
  3078. wait_tcp_ready(&server_args);
  3079. test_client_nofail(&client_args, NULL);
  3080. join_thread(serverThread);
  3081. AssertTrue(client_args.return_code);
  3082. AssertTrue(server_args.return_code);
  3083. FreeTcpReady(&ready);
  3084. #ifdef WOLFSSL_TIRTOS
  3085. fdOpenSession(Task_self());
  3086. #endif
  3087. }
  3088. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  3089. static void test_wolfSSL_reuse_WOLFSSLobj(void)
  3090. {
  3091. /* The unit test for session resumption by re-using WOLFSSL object.
  3092. * WOLFSSL object is not cleared after first session. It re-use the obeject
  3093. * for second connection.
  3094. */
  3095. tcp_ready ready;
  3096. func_args client_args;
  3097. func_args server_args;
  3098. THREAD_TYPE serverThread;
  3099. XMEMSET(&client_args, 0, sizeof(func_args));
  3100. XMEMSET(&server_args, 0, sizeof(func_args));
  3101. #ifdef WOLFSSL_TIRTOS
  3102. fdOpenSession(Task_self());
  3103. #endif
  3104. StartTCP();
  3105. InitTcpReady(&ready);
  3106. #if defined(USE_WINDOWS_API)
  3107. /* use RNG to get random port if using windows */
  3108. ready.port = GetRandomPort();
  3109. #endif
  3110. server_args.signal = &ready;
  3111. client_args.signal = &ready;
  3112. /* the var is used for loop number */
  3113. server_args.argc = 2;
  3114. start_thread(test_server_loop, &server_args, &serverThread);
  3115. wait_tcp_ready(&server_args);
  3116. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  3117. join_thread(serverThread);
  3118. AssertTrue(client_args.return_code);
  3119. AssertTrue(server_args.return_code);
  3120. FreeTcpReady(&ready);
  3121. #ifdef WOLFSSL_TIRTOS
  3122. fdOpenSession(Task_self());
  3123. #endif
  3124. }
  3125. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  3126. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  3127. /* canned export of a session using older version 3 */
  3128. static unsigned char version_3[] = {
  3129. 0xA5, 0xA3, 0x01, 0x87, 0x00, 0x3b, 0x00, 0x01,
  3130. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  3131. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  3132. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  3133. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  3134. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3135. 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30, 0x05,
  3136. 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05, 0xFE,
  3137. 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00,
  3138. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3139. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  3140. 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
  3141. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3142. 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00, 0x00,
  3143. 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  3144. 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00, 0x00,
  3145. 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01, 0x00,
  3146. 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  3147. 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F, 0x00,
  3148. 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00,
  3149. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3150. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3151. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3152. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3153. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3154. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3155. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3156. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3157. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3158. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3159. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3160. 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05, 0x12,
  3161. 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D, 0x31,
  3162. 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B, 0xF3,
  3163. 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98, 0x91,
  3164. 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0, 0x00,
  3165. 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74, 0xDF,
  3166. 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26, 0xA5,
  3167. 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9, 0xEF,
  3168. 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04, 0xAA,
  3169. 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80, 0x00,
  3170. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0C,
  3171. 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00, 0x00,
  3172. 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D, 0x92,
  3173. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3174. 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00, 0x10,
  3175. 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01, 0x30,
  3176. 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00, 0x09,
  3177. 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30, 0x2E,
  3178. 0x31, 0xED, 0x4F
  3179. };
  3180. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  3181. static void test_wolfSSL_dtls_export(void)
  3182. {
  3183. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  3184. tcp_ready ready;
  3185. func_args client_args;
  3186. func_args server_args;
  3187. THREAD_TYPE serverThread;
  3188. callback_functions server_cbf;
  3189. callback_functions client_cbf;
  3190. #ifdef WOLFSSL_TIRTOS
  3191. fdOpenSession(Task_self());
  3192. #endif
  3193. InitTcpReady(&ready);
  3194. #if defined(USE_WINDOWS_API)
  3195. /* use RNG to get random port if using windows */
  3196. ready.port = GetRandomPort();
  3197. #endif
  3198. /* set using dtls */
  3199. XMEMSET(&client_args, 0, sizeof(func_args));
  3200. XMEMSET(&server_args, 0, sizeof(func_args));
  3201. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  3202. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  3203. server_cbf.method = wolfDTLSv1_2_server_method;
  3204. client_cbf.method = wolfDTLSv1_2_client_method;
  3205. server_args.callbacks = &server_cbf;
  3206. client_args.callbacks = &client_cbf;
  3207. server_args.signal = &ready;
  3208. client_args.signal = &ready;
  3209. start_thread(run_wolfssl_server, &server_args, &serverThread);
  3210. wait_tcp_ready(&server_args);
  3211. run_wolfssl_client(&client_args);
  3212. join_thread(serverThread);
  3213. AssertTrue(client_args.return_code);
  3214. AssertTrue(server_args.return_code);
  3215. FreeTcpReady(&ready);
  3216. #ifdef WOLFSSL_TIRTOS
  3217. fdOpenSession(Task_self());
  3218. #endif
  3219. {
  3220. SOCKET_T sockfd = 0;
  3221. WOLFSSL_CTX* ctx;
  3222. WOLFSSL* ssl;
  3223. char msg[64] = "hello wolfssl!";
  3224. char reply[1024];
  3225. int msgSz = (int)XSTRLEN(msg);
  3226. byte *session, *window;
  3227. unsigned int sessionSz, windowSz;
  3228. struct sockaddr_in peerAddr;
  3229. int i;
  3230. /* Set ctx to DTLS 1.2 */
  3231. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  3232. AssertNotNull(ssl = wolfSSL_new(ctx));
  3233. /* test importing version 3 */
  3234. AssertIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  3235. /* test importing bad length and bad version */
  3236. version_3[2] += 1;
  3237. AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  3238. version_3[2] -= 1; version_3[1] = 0XA0;
  3239. AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  3240. wolfSSL_free(ssl);
  3241. wolfSSL_CTX_free(ctx);
  3242. /* check storing client state after connection and storing window only */
  3243. #ifdef WOLFSSL_TIRTOS
  3244. fdOpenSession(Task_self());
  3245. #endif
  3246. InitTcpReady(&ready);
  3247. #if defined(USE_WINDOWS_API)
  3248. /* use RNG to get random port if using windows */
  3249. ready.port = GetRandomPort();
  3250. #endif
  3251. /* set using dtls */
  3252. XMEMSET(&server_args, 0, sizeof(func_args));
  3253. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  3254. server_cbf.method = wolfDTLSv1_2_server_method;
  3255. server_args.callbacks = &server_cbf;
  3256. server_args.argc = 3; /* set loop_count to 3 */
  3257. server_args.signal = &ready;
  3258. start_thread(test_server_nofail, &server_args, &serverThread);
  3259. wait_tcp_ready(&server_args);
  3260. /* create and connect with client */
  3261. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  3262. AssertIntEQ(WOLFSSL_SUCCESS,
  3263. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  3264. AssertIntEQ(WOLFSSL_SUCCESS,
  3265. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  3266. AssertIntEQ(WOLFSSL_SUCCESS,
  3267. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  3268. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  3269. AssertNotNull(ssl = wolfSSL_new(ctx));
  3270. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  3271. /* store server information connected too */
  3272. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  3273. peerAddr.sin_family = AF_INET;
  3274. peerAddr.sin_port = XHTONS(server_args.signal->port);
  3275. wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr));
  3276. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  3277. AssertIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  3278. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  3279. AssertIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  3280. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  3281. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  3282. AssertIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  3283. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  3284. AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  3285. wolfSSL_free(ssl);
  3286. for (i = 1; i < server_args.argc; i++) {
  3287. /* restore state */
  3288. AssertNotNull(ssl = wolfSSL_new(ctx));
  3289. AssertIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  3290. AssertIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  3291. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  3292. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  3293. AssertIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  3294. AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  3295. wolfSSL_free(ssl);
  3296. }
  3297. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  3298. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  3299. wolfSSL_CTX_free(ctx);
  3300. printf("done and waiting for server\n");
  3301. join_thread(serverThread);
  3302. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  3303. FreeTcpReady(&ready);
  3304. #ifdef WOLFSSL_TIRTOS
  3305. fdOpenSession(Task_self());
  3306. #endif
  3307. }
  3308. printf(testingFmt, "wolfSSL_dtls_export()");
  3309. printf(resultFmt, passed);
  3310. #endif
  3311. }
  3312. /*----------------------------------------------------------------------------*
  3313. | TLS extensions tests
  3314. *----------------------------------------------------------------------------*/
  3315. #if defined(HAVE_SNI) || defined(HAVE_ALPN)
  3316. /* connection test runner */
  3317. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  3318. callback_functions* server_callbacks)
  3319. {
  3320. tcp_ready ready;
  3321. func_args client_args;
  3322. func_args server_args;
  3323. THREAD_TYPE serverThread;
  3324. XMEMSET(&client_args, 0, sizeof(func_args));
  3325. XMEMSET(&server_args, 0, sizeof(func_args));
  3326. StartTCP();
  3327. client_args.callbacks = client_callbacks;
  3328. server_args.callbacks = server_callbacks;
  3329. #ifdef WOLFSSL_TIRTOS
  3330. fdOpenSession(Task_self());
  3331. #endif
  3332. /* RUN Server side */
  3333. InitTcpReady(&ready);
  3334. #if defined(USE_WINDOWS_API)
  3335. /* use RNG to get random port if using windows */
  3336. ready.port = GetRandomPort();
  3337. #endif
  3338. server_args.signal = &ready;
  3339. client_args.signal = &ready;
  3340. start_thread(run_wolfssl_server, &server_args, &serverThread);
  3341. wait_tcp_ready(&server_args);
  3342. /* RUN Client side */
  3343. run_wolfssl_client(&client_args);
  3344. join_thread(serverThread);
  3345. FreeTcpReady(&ready);
  3346. #ifdef WOLFSSL_TIRTOS
  3347. fdCloseSession(Task_self());
  3348. #endif
  3349. }
  3350. #endif /* defined(HAVE_SNI) || defined(HAVE_ALPN) */
  3351. #ifdef HAVE_SNI
  3352. static void test_wolfSSL_UseSNI_params(void)
  3353. {
  3354. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3355. WOLFSSL *ssl = wolfSSL_new(ctx);
  3356. AssertNotNull(ctx);
  3357. AssertNotNull(ssl);
  3358. /* invalid [ctx|ssl] */
  3359. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  3360. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  3361. /* invalid type */
  3362. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  3363. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  3364. /* invalid data */
  3365. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  3366. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  3367. /* success case */
  3368. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  3369. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  3370. wolfSSL_free(ssl);
  3371. wolfSSL_CTX_free(ctx);
  3372. }
  3373. /* BEGIN of connection tests callbacks */
  3374. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  3375. {
  3376. AssertIntEQ(WOLFSSL_SUCCESS,
  3377. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  3378. }
  3379. static void use_SNI_at_ssl(WOLFSSL* ssl)
  3380. {
  3381. AssertIntEQ(WOLFSSL_SUCCESS,
  3382. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  3383. }
  3384. static void different_SNI_at_ssl(WOLFSSL* ssl)
  3385. {
  3386. AssertIntEQ(WOLFSSL_SUCCESS,
  3387. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  3388. }
  3389. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  3390. {
  3391. use_SNI_at_ssl(ssl);
  3392. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  3393. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  3394. }
  3395. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  3396. {
  3397. use_SNI_at_ssl(ssl);
  3398. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  3399. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  3400. }
  3401. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  3402. {
  3403. use_SNI_at_ctx(ctx);
  3404. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  3405. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  3406. }
  3407. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  3408. {
  3409. use_SNI_at_ssl(ssl);
  3410. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  3411. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  3412. }
  3413. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  3414. {
  3415. use_SNI_at_ctx(ctx);
  3416. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  3417. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  3418. }
  3419. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  3420. {
  3421. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  3422. }
  3423. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  3424. {
  3425. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  3426. }
  3427. static void verify_SNI_no_matching(WOLFSSL* ssl)
  3428. {
  3429. byte type = WOLFSSL_SNI_HOST_NAME;
  3430. char* request = (char*) &type; /* to be overwritten */
  3431. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  3432. AssertNotNull(request);
  3433. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
  3434. AssertNull(request);
  3435. }
  3436. static void verify_SNI_real_matching(WOLFSSL* ssl)
  3437. {
  3438. byte type = WOLFSSL_SNI_HOST_NAME;
  3439. char* request = NULL;
  3440. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  3441. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
  3442. AssertNotNull(request);
  3443. AssertStrEQ("www.wolfssl.com", request);
  3444. }
  3445. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  3446. {
  3447. byte type = WOLFSSL_SNI_HOST_NAME;
  3448. char* request = NULL;
  3449. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  3450. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
  3451. AssertNotNull(request);
  3452. AssertStrEQ("ww2.wolfssl.com", request);
  3453. }
  3454. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  3455. {
  3456. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  3457. }
  3458. /* END of connection tests callbacks */
  3459. static void test_wolfSSL_UseSNI_connection(void)
  3460. {
  3461. unsigned long i;
  3462. callback_functions callbacks[] = {
  3463. /* success case at ctx */
  3464. {0, use_SNI_at_ctx, 0, 0, 0, 0},
  3465. {0, use_SNI_at_ctx, 0, verify_SNI_real_matching, 0, 0},
  3466. /* success case at ssl */
  3467. {0, 0, use_SNI_at_ssl, verify_SNI_real_matching, 0, 0},
  3468. {0, 0, use_SNI_at_ssl, verify_SNI_real_matching, 0, 0},
  3469. /* default mismatch behavior */
  3470. {0, 0, different_SNI_at_ssl, verify_FATAL_ERROR_on_client, 0, 0},
  3471. {0, 0, use_SNI_at_ssl, verify_UNKNOWN_SNI_on_server, 0, 0},
  3472. /* continue on mismatch */
  3473. {0, 0, different_SNI_at_ssl, 0, 0, 0},
  3474. {0, 0, use_SNI_WITH_CONTINUE_at_ssl, verify_SNI_no_matching, 0, 0},
  3475. /* fake answer on mismatch */
  3476. {0, 0, different_SNI_at_ssl, 0, 0, 0},
  3477. {0, 0, use_SNI_WITH_FAKE_ANSWER_at_ssl, verify_SNI_fake_matching, 0, 0},
  3478. /* sni abort - success */
  3479. {0, use_SNI_at_ctx, 0, 0, 0, 0},
  3480. {0, use_MANDATORY_SNI_at_ctx, 0, verify_SNI_real_matching, 0, 0},
  3481. /* sni abort - abort when absent (ctx) */
  3482. {0, 0, 0, verify_FATAL_ERROR_on_client, 0, 0},
  3483. {0, use_MANDATORY_SNI_at_ctx, 0, verify_SNI_ABSENT_on_server, 0, 0},
  3484. /* sni abort - abort when absent (ssl) */
  3485. {0, 0, 0, verify_FATAL_ERROR_on_client, 0, 0},
  3486. {0, 0, use_MANDATORY_SNI_at_ssl, verify_SNI_ABSENT_on_server, 0, 0},
  3487. /* sni abort - success when overwritten */
  3488. {0, 0, 0, 0, 0, 0},
  3489. {0, use_MANDATORY_SNI_at_ctx, use_SNI_at_ssl, verify_SNI_no_matching, 0, 0},
  3490. /* sni abort - success when allowing mismatches */
  3491. {0, 0, different_SNI_at_ssl, 0, 0, 0},
  3492. {0, use_PSEUDO_MANDATORY_SNI_at_ctx, 0, verify_SNI_fake_matching, 0, 0},
  3493. };
  3494. for (i = 0; i < sizeof(callbacks) / sizeof(callback_functions); i += 2) {
  3495. callbacks[i ].method = wolfSSLv23_client_method;
  3496. callbacks[i + 1].method = wolfSSLv23_server_method;
  3497. test_wolfSSL_client_server(&callbacks[i], &callbacks[i + 1]);
  3498. }
  3499. }
  3500. static void test_wolfSSL_SNI_GetFromBuffer(void)
  3501. {
  3502. byte buffer[] = { /* www.paypal.com */
  3503. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  3504. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  3505. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  3506. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  3507. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  3508. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  3509. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  3510. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  3511. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  3512. };
  3513. byte buffer2[] = { /* api.textmate.org */
  3514. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  3515. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  3516. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  3517. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  3518. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  3519. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  3520. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  3521. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  3522. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  3523. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  3524. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  3525. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  3526. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  3527. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  3528. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  3529. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  3530. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  3531. };
  3532. byte buffer3[] = { /* no sni extension */
  3533. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  3534. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  3535. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  3536. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  3537. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  3538. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  3539. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  3540. };
  3541. byte buffer4[] = { /* last extension has zero size */
  3542. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  3543. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  3544. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  3545. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  3546. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  3547. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  3548. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  3549. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  3550. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  3551. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  3552. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  3553. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  3554. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  3555. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  3556. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  3557. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  3558. 0x12, 0x00, 0x00
  3559. };
  3560. byte buffer5[] = { /* SSL v2.0 client hello */
  3561. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  3562. /* dummy bytes bellow, just to pass size check */
  3563. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  3564. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  3565. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  3566. };
  3567. byte result[32] = {0};
  3568. word32 length = 32;
  3569. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buffer4, sizeof(buffer4),
  3570. 0, result, &length));
  3571. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buffer3, sizeof(buffer3),
  3572. 0, result, &length));
  3573. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buffer2, sizeof(buffer2),
  3574. 1, result, &length));
  3575. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buffer, sizeof(buffer),
  3576. 0, result, &length));
  3577. buffer[0] = 0x16;
  3578. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buffer, sizeof(buffer),
  3579. 0, result, &length));
  3580. buffer[1] = 0x03;
  3581. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buffer,
  3582. sizeof(buffer), 0, result, &length));
  3583. buffer[2] = 0x03;
  3584. AssertIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buffer,
  3585. sizeof(buffer), 0, result, &length));
  3586. buffer[4] = 0x64;
  3587. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buffer, sizeof(buffer),
  3588. 0, result, &length));
  3589. result[length] = 0;
  3590. AssertStrEQ("www.paypal.com", (const char*) result);
  3591. length = 32;
  3592. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buffer2, sizeof(buffer2),
  3593. 0, result, &length));
  3594. result[length] = 0;
  3595. AssertStrEQ("api.textmate.org", (const char*) result);
  3596. /* SSL v2.0 tests */
  3597. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buffer5,
  3598. sizeof(buffer5), 0, result, &length));
  3599. buffer5[2] = 0x02;
  3600. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buffer5,
  3601. sizeof(buffer5), 0, result, &length));
  3602. buffer5[2] = 0x01; buffer5[6] = 0x08;
  3603. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buffer5,
  3604. sizeof(buffer5), 0, result, &length));
  3605. buffer5[6] = 0x09; buffer5[8] = 0x01;
  3606. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buffer5,
  3607. sizeof(buffer5), 0, result, &length));
  3608. }
  3609. #endif /* HAVE_SNI */
  3610. static void test_wolfSSL_UseSNI(void)
  3611. {
  3612. #ifdef HAVE_SNI
  3613. test_wolfSSL_UseSNI_params();
  3614. test_wolfSSL_UseSNI_connection();
  3615. test_wolfSSL_SNI_GetFromBuffer();
  3616. #endif
  3617. }
  3618. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  3619. static void test_wolfSSL_UseTrustedCA(void)
  3620. {
  3621. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  3622. WOLFSSL_CTX *ctx;
  3623. WOLFSSL *ssl;
  3624. byte id[20];
  3625. #ifndef NO_WOLFSSL_SERVER
  3626. AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  3627. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  3628. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  3629. #else
  3630. AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  3631. #endif
  3632. AssertNotNull((ssl = wolfSSL_new(ctx)));
  3633. XMEMSET(id, 0, sizeof(id));
  3634. /* error cases */
  3635. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  3636. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  3637. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  3638. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  3639. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  3640. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  3641. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  3642. #ifdef NO_SHA
  3643. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  3644. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  3645. #endif
  3646. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  3647. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  3648. /* success cases */
  3649. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  3650. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  3651. #ifndef NO_SHA
  3652. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  3653. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  3654. #endif
  3655. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  3656. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  3657. wolfSSL_free(ssl);
  3658. wolfSSL_CTX_free(ctx);
  3659. #endif /* HAVE_TRUSTED_CA */
  3660. }
  3661. static void test_wolfSSL_UseMaxFragment(void)
  3662. {
  3663. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  3664. #ifndef NO_WOLFSSL_SERVER
  3665. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3666. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  3667. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  3668. #else
  3669. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3670. #endif
  3671. WOLFSSL *ssl = wolfSSL_new(ctx);
  3672. AssertNotNull(ctx);
  3673. AssertNotNull(ssl);
  3674. /* error cases */
  3675. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  3676. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  3677. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  3678. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  3679. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  3680. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  3681. /* success case */
  3682. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  3683. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  3684. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  3685. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  3686. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  3687. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  3688. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  3689. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  3690. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  3691. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  3692. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  3693. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  3694. wolfSSL_free(ssl);
  3695. wolfSSL_CTX_free(ctx);
  3696. #endif
  3697. }
  3698. static void test_wolfSSL_UseTruncatedHMAC(void)
  3699. {
  3700. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  3701. #ifndef NO_WOLFSSL_SERVER
  3702. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3703. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  3704. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  3705. #else
  3706. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3707. #endif
  3708. WOLFSSL *ssl = wolfSSL_new(ctx);
  3709. AssertNotNull(ctx);
  3710. AssertNotNull(ssl);
  3711. /* error cases */
  3712. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  3713. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  3714. /* success case */
  3715. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  3716. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  3717. wolfSSL_free(ssl);
  3718. wolfSSL_CTX_free(ctx);
  3719. #endif
  3720. }
  3721. static void test_wolfSSL_UseSupportedCurve(void)
  3722. {
  3723. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && !defined(NO_TLS)
  3724. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3725. WOLFSSL *ssl = wolfSSL_new(ctx);
  3726. AssertNotNull(ctx);
  3727. AssertNotNull(ssl);
  3728. /* error cases */
  3729. AssertIntNE(WOLFSSL_SUCCESS,
  3730. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  3731. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  3732. AssertIntNE(WOLFSSL_SUCCESS,
  3733. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  3734. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  3735. /* success case */
  3736. AssertIntEQ(WOLFSSL_SUCCESS,
  3737. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  3738. AssertIntEQ(WOLFSSL_SUCCESS,
  3739. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  3740. wolfSSL_free(ssl);
  3741. wolfSSL_CTX_free(ctx);
  3742. #endif
  3743. }
  3744. #if defined(HAVE_ALPN) && !defined(NO_WOLFSSL_SERVER) && \
  3745. defined(HAVE_IO_TESTS_DEPENDENCIES)
  3746. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  3747. {
  3748. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  3749. }
  3750. static void use_ALPN_all(WOLFSSL* ssl)
  3751. {
  3752. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  3753. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  3754. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  3755. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  3756. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  3757. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  3758. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  3759. }
  3760. static void use_ALPN_all_continue(WOLFSSL* ssl)
  3761. {
  3762. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  3763. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  3764. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  3765. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  3766. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  3767. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  3768. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  3769. }
  3770. static void use_ALPN_one(WOLFSSL* ssl)
  3771. {
  3772. /* spdy/2 */
  3773. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  3774. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  3775. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  3776. }
  3777. static void use_ALPN_unknown(WOLFSSL* ssl)
  3778. {
  3779. /* http/2.0 */
  3780. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  3781. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  3782. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  3783. }
  3784. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  3785. {
  3786. /* http/2.0 */
  3787. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  3788. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  3789. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  3790. }
  3791. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  3792. {
  3793. /* spdy/3 */
  3794. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  3795. char *proto = NULL;
  3796. word16 protoSz = 0;
  3797. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  3798. /* check value */
  3799. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  3800. if (proto) {
  3801. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  3802. }
  3803. }
  3804. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  3805. {
  3806. char *proto = NULL;
  3807. word16 protoSz = 0;
  3808. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  3809. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  3810. /* check value */
  3811. AssertIntEQ(1, (0 == protoSz));
  3812. AssertIntEQ(1, (NULL == proto));
  3813. }
  3814. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  3815. {
  3816. /* http/1.1 */
  3817. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  3818. char *proto;
  3819. word16 protoSz = 0;
  3820. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  3821. /* check value */
  3822. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  3823. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  3824. }
  3825. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  3826. {
  3827. /* spdy/2 */
  3828. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  3829. char *proto;
  3830. word16 protoSz = 0;
  3831. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  3832. /* check value */
  3833. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  3834. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  3835. }
  3836. static void verify_ALPN_client_list(WOLFSSL* ssl)
  3837. {
  3838. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  3839. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  3840. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  3841. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  3842. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  3843. char *clist = NULL;
  3844. word16 clistSz = 0;
  3845. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  3846. &clistSz));
  3847. /* check value */
  3848. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  3849. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  3850. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  3851. }
  3852. static void test_wolfSSL_UseALPN_connection(void)
  3853. {
  3854. unsigned long i;
  3855. callback_functions callbacks[] = {
  3856. /* success case same list */
  3857. {0, 0, use_ALPN_all, 0, 0, 0},
  3858. {0, 0, use_ALPN_all, verify_ALPN_matching_http1, 0, 0},
  3859. /* success case only one for server */
  3860. {0, 0, use_ALPN_all, 0, 0, 0},
  3861. {0, 0, use_ALPN_one, verify_ALPN_matching_spdy2, 0, 0},
  3862. /* success case only one for client */
  3863. {0, 0, use_ALPN_one, 0, 0, 0},
  3864. {0, 0, use_ALPN_all, verify_ALPN_matching_spdy2, 0, 0},
  3865. /* success case none for client */
  3866. {0, 0, 0, 0, 0, 0},
  3867. {0, 0, use_ALPN_all, 0, 0, 0},
  3868. /* success case mismatch behavior but option 'continue' set */
  3869. {0, 0, use_ALPN_all_continue, verify_ALPN_not_matching_continue, 0, 0},
  3870. {0, 0, use_ALPN_unknown_continue, 0, 0, 0},
  3871. /* success case read protocol send by client */
  3872. {0, 0, use_ALPN_all, 0, 0, 0},
  3873. {0, 0, use_ALPN_one, verify_ALPN_client_list, 0, 0},
  3874. /* mismatch behavior with same list
  3875. * the first and only this one must be taken */
  3876. {0, 0, use_ALPN_all, 0, 0, 0},
  3877. {0, 0, use_ALPN_all, verify_ALPN_not_matching_spdy3, 0, 0},
  3878. /* default mismatch behavior */
  3879. {0, 0, use_ALPN_all, 0, 0, 0},
  3880. {0, 0, use_ALPN_unknown, verify_ALPN_FATAL_ERROR_on_client, 0, 0},
  3881. };
  3882. for (i = 0; i < sizeof(callbacks) / sizeof(callback_functions); i += 2) {
  3883. callbacks[i ].method = wolfSSLv23_client_method;
  3884. callbacks[i + 1].method = wolfSSLv23_server_method;
  3885. test_wolfSSL_client_server(&callbacks[i], &callbacks[i + 1]);
  3886. }
  3887. }
  3888. static void test_wolfSSL_UseALPN_params(void)
  3889. {
  3890. #ifndef NO_WOLFSSL_CLIENT
  3891. /* "http/1.1" */
  3892. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  3893. /* "spdy/1" */
  3894. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  3895. /* "spdy/2" */
  3896. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  3897. /* "spdy/3" */
  3898. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  3899. char buff[256];
  3900. word32 idx;
  3901. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3902. WOLFSSL *ssl = wolfSSL_new(ctx);
  3903. AssertNotNull(ctx);
  3904. AssertNotNull(ssl);
  3905. /* error cases */
  3906. AssertIntNE(WOLFSSL_SUCCESS,
  3907. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  3908. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  3909. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  3910. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  3911. /* success case */
  3912. /* http1 only */
  3913. AssertIntEQ(WOLFSSL_SUCCESS,
  3914. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  3915. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  3916. /* http1, spdy1 */
  3917. XMEMCPY(buff, http1, sizeof(http1));
  3918. idx = sizeof(http1);
  3919. buff[idx++] = ',';
  3920. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  3921. idx += sizeof(spdy1);
  3922. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  3923. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  3924. /* http1, spdy2, spdy1 */
  3925. XMEMCPY(buff, http1, sizeof(http1));
  3926. idx = sizeof(http1);
  3927. buff[idx++] = ',';
  3928. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  3929. idx += sizeof(spdy2);
  3930. buff[idx++] = ',';
  3931. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  3932. idx += sizeof(spdy1);
  3933. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  3934. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  3935. /* spdy3, http1, spdy2, spdy1 */
  3936. XMEMCPY(buff, spdy3, sizeof(spdy3));
  3937. idx = sizeof(spdy3);
  3938. buff[idx++] = ',';
  3939. XMEMCPY(buff+idx, http1, sizeof(http1));
  3940. idx += sizeof(http1);
  3941. buff[idx++] = ',';
  3942. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  3943. idx += sizeof(spdy2);
  3944. buff[idx++] = ',';
  3945. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  3946. idx += sizeof(spdy1);
  3947. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  3948. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  3949. wolfSSL_free(ssl);
  3950. wolfSSL_CTX_free(ctx);
  3951. #endif
  3952. }
  3953. #endif /* HAVE_ALPN */
  3954. static void test_wolfSSL_UseALPN(void)
  3955. {
  3956. #if defined(HAVE_ALPN) && !defined(NO_WOLFSSL_SERVER) &&\
  3957. defined(HAVE_IO_TESTS_DEPENDENCIES)
  3958. test_wolfSSL_UseALPN_connection();
  3959. test_wolfSSL_UseALPN_params();
  3960. #endif
  3961. }
  3962. static void test_wolfSSL_DisableExtendedMasterSecret(void)
  3963. {
  3964. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  3965. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3966. WOLFSSL *ssl = wolfSSL_new(ctx);
  3967. AssertNotNull(ctx);
  3968. AssertNotNull(ssl);
  3969. /* error cases */
  3970. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  3971. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  3972. /* success cases */
  3973. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  3974. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  3975. wolfSSL_free(ssl);
  3976. wolfSSL_CTX_free(ctx);
  3977. #endif
  3978. }
  3979. static void test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  3980. {
  3981. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  3982. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3983. WOLFSSL *ssl = wolfSSL_new(ctx);
  3984. AssertNotNull(ctx);
  3985. AssertNotNull(ssl);
  3986. /* error cases */
  3987. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  3988. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  3989. /* success cases */
  3990. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  3991. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  3992. wolfSSL_free(ssl);
  3993. wolfSSL_CTX_free(ctx);
  3994. #endif
  3995. }
  3996. /*----------------------------------------------------------------------------*
  3997. | X509 Tests
  3998. *----------------------------------------------------------------------------*/
  3999. static void test_wolfSSL_X509_NAME_get_entry(void)
  4000. {
  4001. #if !defined(NO_CERTS) && !defined(NO_RSA)
  4002. #if defined(OPENSSL_ALL) || \
  4003. (defined(OPENSSL_EXTRA) && \
  4004. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  4005. printf(testingFmt, "wolfSSL_X509_NAME_get_entry()");
  4006. {
  4007. /* use openssl like name to test mapping */
  4008. X509_NAME_ENTRY* ne;
  4009. X509_NAME* name;
  4010. X509* x509;
  4011. #ifndef NO_FILESYSTEM
  4012. ASN1_STRING* asn;
  4013. char* subCN = NULL;
  4014. #endif
  4015. int idx;
  4016. ASN1_OBJECT *object = NULL;
  4017. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)
  4018. BIO* bio;
  4019. #endif
  4020. #ifndef NO_FILESYSTEM
  4021. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  4022. AssertNotNull(x509);
  4023. name = X509_get_subject_name(x509);
  4024. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  4025. AssertIntGE(idx, 0);
  4026. ne = X509_NAME_get_entry(name, idx);
  4027. AssertNotNull(ne);
  4028. asn = X509_NAME_ENTRY_get_data(ne);
  4029. AssertNotNull(asn);
  4030. subCN = (char*)ASN1_STRING_data(asn);
  4031. AssertNotNull(subCN);
  4032. wolfSSL_FreeX509(x509);
  4033. #endif
  4034. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  4035. AssertNotNull(x509);
  4036. name = X509_get_subject_name(x509);
  4037. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  4038. AssertIntGE(idx, 0);
  4039. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)
  4040. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  4041. AssertIntEQ(X509_NAME_print_ex(bio, name, 4,
  4042. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  4043. BIO_free(bio);
  4044. #endif
  4045. ne = X509_NAME_get_entry(name, idx);
  4046. AssertNotNull(ne);
  4047. AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
  4048. wolfSSL_FreeX509(x509);
  4049. }
  4050. printf(resultFmt, passed);
  4051. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  4052. #endif /* !NO_CERTS && !NO_RSA */
  4053. }
  4054. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  4055. static void test_wolfSSL_PKCS12(void)
  4056. {
  4057. /* .p12 file is encrypted with DES3 */
  4058. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  4059. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  4060. * Password Key
  4061. */
  4062. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  4063. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  4064. !defined(NO_SHA)
  4065. byte buffer[6000];
  4066. char file[] = "./certs/test-servercert.p12";
  4067. char order[] = "./certs/ecc-rsa-server.p12";
  4068. char pass[] = "a password";
  4069. #ifdef HAVE_ECC
  4070. WOLFSSL_X509_NAME* subject;
  4071. WOLFSSL_X509 *x509;
  4072. #endif
  4073. XFILE f;
  4074. int bytes, ret;
  4075. WOLFSSL_BIO *bio;
  4076. WOLFSSL_EVP_PKEY *pkey;
  4077. WC_PKCS12 *pkcs12;
  4078. WC_PKCS12 *pkcs12_2;
  4079. WOLFSSL_X509 *cert;
  4080. WOLFSSL_X509 *tmp;
  4081. WOLF_STACK_OF(WOLFSSL_X509) *ca;
  4082. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  4083. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  4084. WOLFSSL_CTX *ctx;
  4085. WOLFSSL *ssl;
  4086. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  4087. #endif
  4088. printf(testingFmt, "wolfSSL_PKCS12()");
  4089. f = XFOPEN(file, "rb");
  4090. AssertTrue((f != XBADFILE));
  4091. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  4092. XFCLOSE(f);
  4093. bio = BIO_new_mem_buf((void*)buffer, bytes);
  4094. AssertNotNull(bio);
  4095. pkcs12 = d2i_PKCS12_bio(bio, NULL);
  4096. AssertNotNull(pkcs12);
  4097. PKCS12_free(pkcs12);
  4098. d2i_PKCS12_bio(bio, &pkcs12);
  4099. AssertNotNull(pkcs12);
  4100. BIO_free(bio);
  4101. /* check verify MAC fail case */
  4102. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  4103. AssertIntEQ(ret, 0);
  4104. AssertNull(pkey);
  4105. AssertNull(cert);
  4106. /* check parse with no extra certs kept */
  4107. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  4108. AssertIntEQ(ret, 1);
  4109. AssertNotNull(pkey);
  4110. AssertNotNull(cert);
  4111. wolfSSL_EVP_PKEY_free(pkey);
  4112. wolfSSL_X509_free(cert);
  4113. /* check parse with extra certs kept */
  4114. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  4115. AssertIntEQ(ret, 1);
  4116. AssertNotNull(pkey);
  4117. AssertNotNull(cert);
  4118. AssertNotNull(ca);
  4119. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  4120. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  4121. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  4122. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  4123. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4124. #else
  4125. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4126. #endif
  4127. /* Copy stack structure */
  4128. AssertNotNull(tmp_ca = sk_X509_dup(ca));
  4129. AssertIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  4130. /* CTX now owns the tmp_ca stack structure */
  4131. tmp_ca = NULL;
  4132. AssertIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  4133. AssertNotNull(tmp_ca);
  4134. AssertIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  4135. /* Check that the main cert is also set */
  4136. AssertNotNull(ssl = SSL_new(ctx));
  4137. AssertNotNull(SSL_get_certificate(ssl));
  4138. SSL_free(ssl);
  4139. SSL_CTX_free(ctx);
  4140. #endif
  4141. /* should be 2 other certs on stack */
  4142. tmp = sk_X509_pop(ca);
  4143. AssertNotNull(tmp);
  4144. X509_free(tmp);
  4145. tmp = sk_X509_pop(ca);
  4146. AssertNotNull(tmp);
  4147. X509_free(tmp);
  4148. AssertNull(sk_X509_pop(ca));
  4149. EVP_PKEY_free(pkey);
  4150. X509_free(cert);
  4151. sk_X509_pop_free(ca, X509_free);
  4152. /* check PKCS12_create */
  4153. AssertNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  4154. AssertIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  4155. SSL_SUCCESS);
  4156. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  4157. -1, -1, 100, -1, 0)));
  4158. EVP_PKEY_free(pkey);
  4159. X509_free(cert);
  4160. sk_X509_free(ca);
  4161. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  4162. SSL_SUCCESS);
  4163. PKCS12_free(pkcs12_2);
  4164. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  4165. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  4166. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  4167. 2000, 1, 0)));
  4168. EVP_PKEY_free(pkey);
  4169. X509_free(cert);
  4170. sk_X509_free(ca);
  4171. /* convert to DER then back and parse */
  4172. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  4173. AssertIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  4174. PKCS12_free(pkcs12_2);
  4175. AssertNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  4176. BIO_free(bio);
  4177. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  4178. SSL_SUCCESS);
  4179. /* should be 2 other certs on stack */
  4180. tmp = sk_X509_pop(ca);
  4181. AssertNotNull(tmp);
  4182. X509_free(tmp);
  4183. tmp = sk_X509_pop(ca);
  4184. AssertNotNull(tmp);
  4185. X509_free(tmp);
  4186. AssertNull(sk_X509_pop(ca));
  4187. #ifndef NO_RC4
  4188. PKCS12_free(pkcs12_2);
  4189. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  4190. NID_pbe_WithSHA1And128BitRC4,
  4191. NID_pbe_WithSHA1And128BitRC4,
  4192. 2000, 1, 0)));
  4193. EVP_PKEY_free(pkey);
  4194. X509_free(cert);
  4195. sk_X509_free(ca);
  4196. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  4197. SSL_SUCCESS);
  4198. #endif /* NO_RC4 */
  4199. EVP_PKEY_free(pkey);
  4200. X509_free(cert);
  4201. PKCS12_free(pkcs12);
  4202. PKCS12_free(pkcs12_2);
  4203. sk_X509_free(ca);
  4204. #ifdef HAVE_ECC
  4205. /* test order of parsing */
  4206. f = XFOPEN(order, "rb");
  4207. AssertTrue(f != XBADFILE);
  4208. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  4209. XFCLOSE(f);
  4210. AssertNotNull(bio = BIO_new_mem_buf((void*)buffer, bytes));
  4211. AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  4212. AssertIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  4213. WOLFSSL_SUCCESS);
  4214. AssertNotNull(pkey);
  4215. AssertNotNull(cert);
  4216. AssertNotNull(ca);
  4217. /* compare subject lines of certificates */
  4218. AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  4219. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  4220. SSL_FILETYPE_PEM));
  4221. AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  4222. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  4223. X509_free(x509);
  4224. /* test expected fail case */
  4225. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  4226. SSL_FILETYPE_PEM));
  4227. AssertIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  4228. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  4229. X509_free(x509);
  4230. X509_free(cert);
  4231. /* get subject line from ca stack */
  4232. AssertNotNull(cert = sk_X509_pop(ca));
  4233. AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  4234. /* compare subject from certificate in ca to expected */
  4235. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  4236. SSL_FILETYPE_PEM));
  4237. AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  4238. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  4239. EVP_PKEY_free(pkey);
  4240. X509_free(x509);
  4241. X509_free(cert);
  4242. BIO_free(bio);
  4243. PKCS12_free(pkcs12);
  4244. sk_X509_free(ca); /* TEST d2i_PKCS12_fp */
  4245. /* test order of parsing */
  4246. f = XFOPEN(file, "rb");
  4247. AssertTrue(f != XBADFILE);
  4248. AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  4249. XFCLOSE(f);
  4250. /* check verify MAC fail case */
  4251. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  4252. AssertIntEQ(ret, 0);
  4253. AssertNull(pkey);
  4254. AssertNull(cert);
  4255. /* check parse with no extra certs kept */
  4256. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  4257. AssertIntEQ(ret, 1);
  4258. AssertNotNull(pkey);
  4259. AssertNotNull(cert);
  4260. wolfSSL_EVP_PKEY_free(pkey);
  4261. wolfSSL_X509_free(cert);
  4262. /* check parse with extra certs kept */
  4263. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  4264. AssertIntEQ(ret, 1);
  4265. AssertNotNull(pkey);
  4266. AssertNotNull(cert);
  4267. AssertNotNull(ca);
  4268. wolfSSL_EVP_PKEY_free(pkey);
  4269. wolfSSL_X509_free(cert);
  4270. sk_X509_free(ca);
  4271. PKCS12_free(pkcs12);
  4272. #endif /* HAVE_ECC */
  4273. /* Test i2d_PKCS12_bio */
  4274. f = XFOPEN(file, "rb");
  4275. AssertTrue((f != XBADFILE));
  4276. AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  4277. XFCLOSE(f);
  4278. bio = BIO_new(BIO_s_mem());
  4279. AssertNotNull(bio);
  4280. ret = i2d_PKCS12_bio(bio, pkcs12);
  4281. AssertIntEQ(ret, 1);
  4282. ret = i2d_PKCS12_bio(NULL, pkcs12);
  4283. AssertIntEQ(ret, 0);
  4284. ret = i2d_PKCS12_bio(bio, NULL);
  4285. AssertIntEQ(ret, 0);
  4286. PKCS12_free(pkcs12);
  4287. BIO_free(bio);
  4288. (void)order;
  4289. printf(resultFmt, passed);
  4290. #endif /* OPENSSL_EXTRA */
  4291. #endif /* HAVE_FIPS */
  4292. }
  4293. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  4294. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  4295. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  4296. #define TEST_PKCS8_ENC
  4297. #endif
  4298. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  4299. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  4300. /* used to keep track if FailTestCallback was called */
  4301. static int failTestCallbackCalled = 0;
  4302. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  4303. {
  4304. (void)passwd;
  4305. (void)sz;
  4306. (void)rw;
  4307. (void)userdata;
  4308. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  4309. failTestCallbackCalled = 1;
  4310. return -1;
  4311. }
  4312. #endif
  4313. static void test_wolfSSL_no_password_cb(void)
  4314. {
  4315. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  4316. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  4317. WOLFSSL_CTX* ctx;
  4318. byte buffer[FOURK_BUF];
  4319. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  4320. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  4321. XFILE f;
  4322. int bytes;
  4323. printf(testingFmt, "test_wolfSSL_no_password_cb()");
  4324. #ifndef NO_WOLFSSL_CLIENT
  4325. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  4326. #else
  4327. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  4328. #endif
  4329. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  4330. AssertTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  4331. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  4332. XFCLOSE(f);
  4333. AssertIntLE(bytes, sizeof(buffer));
  4334. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  4335. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  4336. AssertTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  4337. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  4338. XFCLOSE(f);
  4339. AssertIntLE(bytes, sizeof(buffer));
  4340. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  4341. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4342. wolfSSL_CTX_free(ctx);
  4343. if (failTestCallbackCalled != 0) {
  4344. Fail(("Password callback should not be called by default"),
  4345. ("Password callback was called without attempting "
  4346. "to first decipher private key without password."));
  4347. }
  4348. printf(resultFmt, passed);
  4349. #endif
  4350. }
  4351. #ifdef TEST_PKCS8_ENC
  4352. /* for PKCS8 test case */
  4353. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  4354. {
  4355. int flag = 0;
  4356. (void)rw;
  4357. if (userdata != NULL) {
  4358. flag = *((int*)userdata); /* user set data */
  4359. }
  4360. switch (flag) {
  4361. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  4362. * can be anything the user wishes to be passed to the callback
  4363. * associated with the WOLFSSL_CTX */
  4364. XSTRNCPY(passwd, "yassl123", sz);
  4365. return 8;
  4366. default:
  4367. return BAD_FUNC_ARG;
  4368. }
  4369. }
  4370. #endif /* TEST_PKCS8_ENC */
  4371. /* Testing functions dealing with PKCS8 */
  4372. static void test_wolfSSL_PKCS8(void)
  4373. {
  4374. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8)
  4375. byte buffer[FOURK_BUF];
  4376. byte der[FOURK_BUF];
  4377. #ifndef NO_RSA
  4378. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  4379. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  4380. #endif
  4381. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  4382. #ifdef HAVE_ECC
  4383. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  4384. #endif
  4385. XFILE f;
  4386. int bytes;
  4387. WOLFSSL_CTX* ctx;
  4388. #ifdef HAVE_ECC
  4389. int ret;
  4390. ecc_key key;
  4391. word32 x = 0;
  4392. #endif
  4393. #ifdef TEST_PKCS8_ENC
  4394. #if !defined(NO_RSA) && !defined(NO_SHA)
  4395. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  4396. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  4397. #endif
  4398. #if defined(HAVE_ECC) && !defined(NO_SHA)
  4399. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  4400. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  4401. #endif
  4402. int flag;
  4403. #endif
  4404. printf(testingFmt, "wolfSSL_PKCS8()");
  4405. #ifndef NO_WOLFSSL_CLIENT
  4406. #ifndef WOLFSSL_NO_TLS12
  4407. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  4408. #else
  4409. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  4410. #endif
  4411. #else
  4412. #ifndef WOLFSSL_NO_TLS12
  4413. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  4414. #else
  4415. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  4416. #endif
  4417. #endif
  4418. #ifdef TEST_PKCS8_ENC
  4419. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  4420. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  4421. flag = 1; /* used by password callback as return code */
  4422. #if !defined(NO_RSA) && !defined(NO_SHA)
  4423. /* test loading PEM PKCS8 encrypted file */
  4424. f = XFOPEN(serverKeyPkcs8EncPemFile, "rb");
  4425. AssertTrue((f != XBADFILE));
  4426. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  4427. XFCLOSE(f);
  4428. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  4429. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4430. /* this next case should fail because of password callback return code */
  4431. flag = 0; /* used by password callback as return code */
  4432. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  4433. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4434. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  4435. AssertIntGT(wc_KeyPemToDer(buffer, bytes, der, (word32)sizeof(der),
  4436. "yassl123"), 0);
  4437. /* test that error value is returned with a bad password */
  4438. AssertIntLT(wc_KeyPemToDer(buffer, bytes, der, (word32)sizeof(der),
  4439. "bad"), 0);
  4440. /* test loading PEM PKCS8 encrypted file */
  4441. f = XFOPEN(serverKeyPkcs8EncDerFile, "rb");
  4442. AssertTrue((f != XBADFILE));
  4443. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  4444. XFCLOSE(f);
  4445. flag = 1; /* used by password callback as return code */
  4446. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  4447. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  4448. /* this next case should fail because of password callback return code */
  4449. flag = 0; /* used by password callback as return code */
  4450. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  4451. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  4452. #endif /* !NO_RSA && !NO_SHA */
  4453. #if defined(HAVE_ECC) && !defined(NO_SHA)
  4454. /* test loading PEM PKCS8 encrypted ECC Key file */
  4455. f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb");
  4456. AssertTrue((f != XBADFILE));
  4457. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  4458. XFCLOSE(f);
  4459. flag = 1; /* used by password callback as return code */
  4460. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  4461. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4462. /* this next case should fail because of password callback return code */
  4463. flag = 0; /* used by password callback as return code */
  4464. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  4465. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4466. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  4467. AssertIntGT(wc_KeyPemToDer(buffer, bytes, der, (word32)sizeof(der),
  4468. "yassl123"), 0);
  4469. /* test that error value is returned with a bad password */
  4470. AssertIntLT(wc_KeyPemToDer(buffer, bytes, der, (word32)sizeof(der),
  4471. "bad"), 0);
  4472. /* test loading DER PKCS8 encrypted ECC Key file */
  4473. f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb");
  4474. AssertTrue((f != XBADFILE));
  4475. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  4476. XFCLOSE(f);
  4477. flag = 1; /* used by password callback as return code */
  4478. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  4479. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  4480. /* this next case should fail because of password callback return code */
  4481. flag = 0; /* used by password callback as return code */
  4482. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  4483. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  4484. /* leave flag as "okay" */
  4485. flag = 1;
  4486. #endif /* HAVE_ECC && !NO_SHA */
  4487. #endif /* TEST_PKCS8_ENC */
  4488. #ifndef NO_RSA
  4489. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  4490. f = XFOPEN(serverKeyPkcs8DerFile, "rb");
  4491. AssertTrue((f != XBADFILE));
  4492. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  4493. XFCLOSE(f);
  4494. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  4495. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  4496. /* test loading PEM PKCS8 private key file (not encrypted) */
  4497. f = XFOPEN(serverKeyPkcs8PemFile, "rb");
  4498. AssertTrue((f != XBADFILE));
  4499. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  4500. XFCLOSE(f);
  4501. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  4502. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4503. #endif /* !NO_RSA */
  4504. /* Test PKCS8 PEM ECC key no crypt */
  4505. f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb");
  4506. AssertTrue((f != XBADFILE));
  4507. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  4508. XFCLOSE(f);
  4509. #ifdef HAVE_ECC
  4510. /* Test PKCS8 PEM ECC key no crypt */
  4511. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  4512. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4513. /* decrypt PKCS8 PEM to key in DER format */
  4514. AssertIntGT((bytes = wc_KeyPemToDer(buffer, bytes, der,
  4515. (word32)sizeof(der), NULL)), 0);
  4516. ret = wc_ecc_init(&key);
  4517. if (ret == 0) {
  4518. ret = wc_EccPrivateKeyDecode(der, &x, &key, bytes);
  4519. wc_ecc_free(&key);
  4520. }
  4521. AssertIntEQ(ret, 0);
  4522. /* Test PKCS8 DER ECC key no crypt */
  4523. f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb");
  4524. AssertTrue((f != XBADFILE));
  4525. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  4526. XFCLOSE(f);
  4527. /* Test using a PKCS8 ECC PEM */
  4528. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  4529. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  4530. #else
  4531. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  4532. AssertIntEQ((bytes = wc_KeyPemToDer(buffer, bytes, der,
  4533. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  4534. #endif /* HAVE_ECC */
  4535. wolfSSL_CTX_free(ctx);
  4536. printf(resultFmt, passed);
  4537. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  4538. }
  4539. static void test_wolfSSL_PKCS8_ED25519(void)
  4540. {
  4541. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  4542. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519)
  4543. const byte encPrivKey[] = \
  4544. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  4545. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  4546. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  4547. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  4548. "2L9W4v7swXkV+X+a1ww=\n"
  4549. "-----END ENCRYPTED PRIVATE KEY-----\n";
  4550. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  4551. byte der[FOURK_BUF];
  4552. WOLFSSL_CTX* ctx;
  4553. int bytes;
  4554. XMEMSET(der, 0, sizeof(der));
  4555. AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  4556. (word32)sizeof(der), password)), 0);
  4557. #ifndef NO_WOLFSSL_SERVER
  4558. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4559. #else
  4560. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4561. #endif
  4562. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  4563. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  4564. wolfSSL_CTX_free(ctx);
  4565. #endif
  4566. }
  4567. static void test_wolfSSL_PKCS8_ED448(void)
  4568. {
  4569. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  4570. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448)
  4571. const byte encPrivKey[] = \
  4572. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  4573. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  4574. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  4575. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  4576. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  4577. "-----END ENCRYPTED PRIVATE KEY-----\n";
  4578. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  4579. byte der[FOURK_BUF];
  4580. WOLFSSL_CTX* ctx;
  4581. int bytes;
  4582. XMEMSET(der, 0, sizeof(der));
  4583. AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  4584. (word32)sizeof(der), password)), 0);
  4585. #ifndef NO_WOLFSSL_SERVER
  4586. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4587. #else
  4588. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4589. #endif
  4590. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  4591. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  4592. wolfSSL_CTX_free(ctx);
  4593. #endif
  4594. }
  4595. /* Testing functions dealing with PKCS5 */
  4596. static void test_wolfSSL_PKCS5(void)
  4597. {
  4598. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  4599. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  4600. const char* passwd = "myfipsPa$$W0rd";
  4601. #else
  4602. const char *passwd = "pass1234";
  4603. #endif
  4604. const unsigned char *salt = (unsigned char *)"salt1234";
  4605. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  4606. DYNAMIC_TYPE_TMP_BUFFER);
  4607. int ret = 0;
  4608. AssertNotNull(out);
  4609. ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  4610. (int)XSTRLEN((const char *) salt), 10,
  4611. WC_SHA_DIGEST_SIZE,out);
  4612. AssertIntEQ(ret, SSL_SUCCESS);
  4613. #ifdef WOLFSSL_SHA512
  4614. ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  4615. (int)XSTRLEN((const char *) salt), 10,
  4616. wolfSSL_EVP_sha512(), WC_SHA_DIGEST_SIZE, out);
  4617. AssertIntEQ(ret, SSL_SUCCESS);
  4618. #endif
  4619. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4620. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  4621. }
  4622. /* test parsing URI from certificate */
  4623. static void test_wolfSSL_URI(void)
  4624. {
  4625. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  4626. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  4627. defined(OPENSSL_EXTRA))
  4628. WOLFSSL_X509* x509;
  4629. const char uri[] = "./certs/client-uri-cert.pem";
  4630. const char badUri[] = "./certs/client-relative-uri.pem";
  4631. printf(testingFmt, "wolfSSL URI parse");
  4632. x509 = wolfSSL_X509_load_certificate_file(uri, WOLFSSL_FILETYPE_PEM);
  4633. AssertNotNull(x509);
  4634. wolfSSL_FreeX509(x509);
  4635. x509 = wolfSSL_X509_load_certificate_file(badUri, WOLFSSL_FILETYPE_PEM);
  4636. #ifndef IGNORE_NAME_CONSTRAINTS
  4637. AssertNull(x509);
  4638. #else
  4639. AssertNotNull(x509);
  4640. #endif
  4641. printf(resultFmt, passed);
  4642. #endif
  4643. }
  4644. static void test_wolfSSL_TBS(void)
  4645. {
  4646. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  4647. && defined(OPENSSL_EXTRA)
  4648. WOLFSSL_X509* x509;
  4649. const unsigned char* tbs;
  4650. int tbsSz;
  4651. printf(testingFmt, "wolfSSL TBS");
  4652. AssertNotNull(x509 =
  4653. wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
  4654. AssertNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  4655. AssertNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  4656. AssertNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  4657. AssertIntEQ(tbsSz, 1003);
  4658. wolfSSL_FreeX509(x509);
  4659. printf(resultFmt, passed);
  4660. #endif
  4661. }
  4662. static void test_wolfSSL_X509_verify(void)
  4663. {
  4664. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  4665. && defined(OPENSSL_EXTRA)
  4666. WOLFSSL_X509* ca;
  4667. WOLFSSL_X509* server;
  4668. WOLFSSL_EVP_PKEY* pkey;
  4669. unsigned char buf[2048];
  4670. const unsigned char* pt = NULL;
  4671. int bufSz;
  4672. printf(testingFmt, "wolfSSL X509 verify");
  4673. AssertNotNull(ca =
  4674. wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
  4675. AssertIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  4676. WOLFSSL_SUCCESS);
  4677. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  4678. WOLFSSL_SUCCESS);
  4679. AssertIntEQ(bufSz, 294);
  4680. bufSz = 2048;
  4681. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  4682. WOLFSSL_SUCCESS);
  4683. AssertIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  4684. AssertIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  4685. AssertNotNull(server =
  4686. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM));
  4687. /* success case */
  4688. pt = buf;
  4689. AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  4690. AssertIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  4691. AssertIntEQ(wolfSSL_X509_verify(server, pkey), WOLFSSL_SUCCESS);
  4692. wolfSSL_EVP_PKEY_free(pkey);
  4693. /* fail case */
  4694. bufSz = 2048;
  4695. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(server, buf, &bufSz),
  4696. WOLFSSL_SUCCESS);
  4697. pt = buf;
  4698. AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  4699. AssertIntEQ(wolfSSL_X509_verify(server, pkey), WOLFSSL_FAILURE);
  4700. AssertIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  4701. AssertIntEQ(wolfSSL_X509_verify(server, NULL), WOLFSSL_FATAL_ERROR);
  4702. wolfSSL_EVP_PKEY_free(pkey);
  4703. wolfSSL_FreeX509(ca);
  4704. wolfSSL_FreeX509(server);
  4705. printf(resultFmt, passed);
  4706. #endif
  4707. }
  4708. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  4709. * version allowed.
  4710. * POST: 1 on success.
  4711. */
  4712. static int test_wolfSSL_CTX_SetMinVersion(void)
  4713. {
  4714. int failFlag = WOLFSSL_SUCCESS;
  4715. #ifndef NO_WOLFSSL_CLIENT
  4716. WOLFSSL_CTX* ctx;
  4717. int itr;
  4718. #ifndef NO_OLD_TLS
  4719. const int versions[] = {
  4720. #ifdef WOLFSSL_ALLOW_TLSV10
  4721. WOLFSSL_TLSV1,
  4722. #endif
  4723. WOLFSSL_TLSV1_1,
  4724. WOLFSSL_TLSV1_2 };
  4725. #elif !defined(WOLFSSL_NO_TLS12)
  4726. const int versions[] = { WOLFSSL_TLSV1_2 };
  4727. #elif defined(WOLFSSL_TLS13)
  4728. const int versions[] = { WOLFSSL_TLSV1_3 };
  4729. #else
  4730. const int versions[0];
  4731. #endif
  4732. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  4733. printf(testingFmt, "wolfSSL_CTX_SetMinVersion()");
  4734. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++){
  4735. if(wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr)) != WOLFSSL_SUCCESS){
  4736. failFlag = WOLFSSL_FAILURE;
  4737. }
  4738. }
  4739. printf(resultFmt, failFlag == WOLFSSL_SUCCESS ? passed : failed);
  4740. wolfSSL_CTX_free(ctx);
  4741. #endif
  4742. return failFlag;
  4743. } /* END test_wolfSSL_CTX_SetMinVersion */
  4744. /*----------------------------------------------------------------------------*
  4745. | OCSP Stapling
  4746. *----------------------------------------------------------------------------*/
  4747. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  4748. * need to contact the CA, lowering the cost of cert revocation checking.
  4749. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  4750. * POST: 1 returned for success.
  4751. */
  4752. static int test_wolfSSL_UseOCSPStapling(void)
  4753. {
  4754. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  4755. !defined(NO_WOLFSSL_CLIENT)
  4756. int ret;
  4757. WOLFSSL_CTX* ctx;
  4758. WOLFSSL* ssl;
  4759. #ifndef NO_WOLFSSL_CLIENT
  4760. #ifndef WOLFSSL_NO_TLS12
  4761. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  4762. #else
  4763. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  4764. #endif
  4765. #else
  4766. #ifndef WOLFSSL_NO_TLS12
  4767. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  4768. #else
  4769. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  4770. #endif
  4771. #endif
  4772. ssl = wolfSSL_new(ctx);
  4773. printf(testingFmt, "wolfSSL_UseOCSPStapling()");
  4774. ret = wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  4775. WOLFSSL_CSR2_OCSP_USE_NONCE);
  4776. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  4777. wolfSSL_free(ssl);
  4778. wolfSSL_CTX_free(ctx);
  4779. return ret;
  4780. #else
  4781. return WOLFSSL_SUCCESS;
  4782. #endif
  4783. } /*END test_wolfSSL_UseOCSPStapling */
  4784. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  4785. * stapling eliminates the need to contact the CA and lowers cert revocation
  4786. * check.
  4787. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  4788. */
  4789. static int test_wolfSSL_UseOCSPStaplingV2 (void)
  4790. {
  4791. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  4792. !defined(NO_WOLFSSL_CLIENT)
  4793. int ret;
  4794. WOLFSSL_CTX* ctx;
  4795. WOLFSSL* ssl;
  4796. #ifndef NO_WOLFSSL_CLIENT
  4797. #ifndef WOLFSSL_NO_TLS12
  4798. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  4799. #else
  4800. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  4801. #endif
  4802. #else
  4803. #ifndef WOLFSSL_NO_TLS12
  4804. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  4805. #else
  4806. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  4807. #endif
  4808. #endif
  4809. ssl = wolfSSL_new(ctx);
  4810. printf(testingFmt, "wolfSSL_UseOCSPStaplingV2()");
  4811. ret = wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  4812. WOLFSSL_CSR2_OCSP_USE_NONCE );
  4813. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  4814. wolfSSL_free(ssl);
  4815. wolfSSL_CTX_free(ctx);
  4816. return ret;
  4817. #else
  4818. return WOLFSSL_SUCCESS;
  4819. #endif
  4820. } /*END test_wolfSSL_UseOCSPStaplingV2*/
  4821. /*----------------------------------------------------------------------------*
  4822. | Multicast Tests
  4823. *----------------------------------------------------------------------------*/
  4824. static void test_wolfSSL_mcast(void)
  4825. {
  4826. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST)
  4827. WOLFSSL_CTX* ctx;
  4828. WOLFSSL* ssl;
  4829. int result;
  4830. byte preMasterSecret[512];
  4831. byte clientRandom[32];
  4832. byte serverRandom[32];
  4833. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  4834. byte buf[256];
  4835. word16 newId;
  4836. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method());
  4837. AssertNotNull(ctx);
  4838. result = wolfSSL_CTX_mcast_set_member_id(ctx, 0);
  4839. AssertIntEQ(result, WOLFSSL_SUCCESS);
  4840. ssl = wolfSSL_new(ctx);
  4841. AssertNotNull(ssl);
  4842. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  4843. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  4844. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  4845. result = wolfSSL_set_secret(ssl, 23,
  4846. preMasterSecret, sizeof(preMasterSecret),
  4847. clientRandom, serverRandom, suite);
  4848. AssertIntEQ(result, WOLFSSL_SUCCESS);
  4849. result = wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf));
  4850. AssertIntLE(result, 0);
  4851. AssertIntLE(newId, 100);
  4852. wolfSSL_free(ssl);
  4853. wolfSSL_CTX_free(ctx);
  4854. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST */
  4855. }
  4856. /*----------------------------------------------------------------------------*
  4857. | Wolfcrypt
  4858. *----------------------------------------------------------------------------*/
  4859. /*
  4860. * Unit test for the wc_InitBlake2b()
  4861. */
  4862. static int test_wc_InitBlake2b (void)
  4863. {
  4864. int ret = 0;
  4865. #ifdef HAVE_BLAKE2
  4866. Blake2b blake2;
  4867. printf(testingFmt, "wc_InitBlake2B()");
  4868. /* Test good arg. */
  4869. ret = wc_InitBlake2b(&blake2, 64);
  4870. if (ret != 0) {
  4871. ret = WOLFSSL_FATAL_ERROR;
  4872. }
  4873. /* Test bad arg. */
  4874. if (!ret) {
  4875. ret = wc_InitBlake2b(NULL, 64);
  4876. if (ret == 0) {
  4877. ret = WOLFSSL_FATAL_ERROR;
  4878. } else {
  4879. ret = 0;
  4880. }
  4881. }
  4882. if (!ret) {
  4883. ret = wc_InitBlake2b(NULL, 128);
  4884. if (ret == 0) {
  4885. ret = WOLFSSL_FATAL_ERROR;
  4886. } else {
  4887. ret = 0;
  4888. }
  4889. }
  4890. if (!ret) {
  4891. ret = wc_InitBlake2b(&blake2, 128);
  4892. if (ret == 0) {
  4893. ret = WOLFSSL_FATAL_ERROR;
  4894. } else {
  4895. ret = 0;
  4896. }
  4897. }
  4898. if (!ret) {
  4899. ret = wc_InitBlake2b(NULL, 0);
  4900. if (ret == 0) {
  4901. ret = WOLFSSL_FATAL_ERROR;
  4902. } else {
  4903. ret = 0;
  4904. }
  4905. }
  4906. if (!ret) {
  4907. ret = wc_InitBlake2b(&blake2, 0);
  4908. if (ret == 0) {
  4909. ret = WOLFSSL_FATAL_ERROR;
  4910. } else {
  4911. ret = 0;
  4912. }
  4913. }
  4914. printf(resultFmt, ret == 0 ? passed : failed);
  4915. #endif
  4916. return ret;
  4917. } /*END test_wc_InitBlake2b*/
  4918. /*
  4919. * Unit test for the wc_InitMd5()
  4920. */
  4921. static int test_wc_InitMd5 (void)
  4922. {
  4923. int flag = 0;
  4924. #ifndef NO_MD5
  4925. wc_Md5 md5;
  4926. int ret;
  4927. printf(testingFmt, "wc_InitMd5()");
  4928. /* Test good arg. */
  4929. ret = wc_InitMd5(&md5);
  4930. if (ret != 0) {
  4931. flag = WOLFSSL_FATAL_ERROR;
  4932. }
  4933. /* Test bad arg. */
  4934. if (!flag) {
  4935. ret = wc_InitMd5(NULL);
  4936. if (ret != BAD_FUNC_ARG) {
  4937. flag = WOLFSSL_FATAL_ERROR;
  4938. }
  4939. }
  4940. wc_Md5Free(&md5);
  4941. printf(resultFmt, flag == 0 ? passed : failed);
  4942. #endif
  4943. return flag;
  4944. } /* END test_wc_InitMd5 */
  4945. /*
  4946. * Testing wc_UpdateMd5()
  4947. */
  4948. static int test_wc_Md5Update (void)
  4949. {
  4950. int flag = 0;
  4951. #ifndef NO_MD5
  4952. wc_Md5 md5;
  4953. byte hash[WC_MD5_DIGEST_SIZE];
  4954. testVector a, b, c;
  4955. int ret;
  4956. ret = wc_InitMd5(&md5);
  4957. if (ret != 0) {
  4958. flag = ret;
  4959. }
  4960. printf(testingFmt, "wc_Md5Update()");
  4961. /* Input */
  4962. if (!flag) {
  4963. a.input = "a";
  4964. a.inLen = XSTRLEN(a.input);
  4965. ret = wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen);
  4966. if (ret != 0) {
  4967. flag = ret;
  4968. }
  4969. }
  4970. if (!flag) {
  4971. ret = wc_Md5Final(&md5, hash);
  4972. if (ret != 0) {
  4973. flag = ret;
  4974. }
  4975. }
  4976. /* Update input. */
  4977. if (!flag) {
  4978. a.input = "abc";
  4979. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  4980. "\x72";
  4981. a.inLen = XSTRLEN(a.input);
  4982. a.outLen = XSTRLEN(a.output);
  4983. ret = wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen);
  4984. if (ret != 0) {
  4985. flag = ret;
  4986. }
  4987. }
  4988. if (!flag) {
  4989. ret = wc_Md5Final(&md5, hash);
  4990. if (ret != 0) {
  4991. flag = ret;
  4992. }
  4993. }
  4994. if (!flag) {
  4995. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  4996. flag = WOLFSSL_FATAL_ERROR;
  4997. }
  4998. }
  4999. /*Pass in bad values. */
  5000. if (!flag) {
  5001. b.input = NULL;
  5002. b.inLen = 0;
  5003. ret = wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen);
  5004. if (ret != 0) {
  5005. flag = ret;
  5006. }
  5007. }
  5008. if (!flag) {
  5009. c.input = NULL;
  5010. c.inLen = WC_MD5_DIGEST_SIZE;
  5011. ret = wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen);
  5012. if (ret != BAD_FUNC_ARG) {
  5013. flag = WOLFSSL_FATAL_ERROR;
  5014. }
  5015. }
  5016. if (!flag) {
  5017. ret = wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen);
  5018. if (ret != BAD_FUNC_ARG) {
  5019. flag = WOLFSSL_FATAL_ERROR;
  5020. }
  5021. }
  5022. wc_Md5Free(&md5);
  5023. printf(resultFmt, flag == 0 ? passed : failed);
  5024. #endif
  5025. return flag;
  5026. } /* END test_wc_Md5Update() */
  5027. /*
  5028. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  5029. */
  5030. static int test_wc_Md5Final (void)
  5031. {
  5032. int flag = 0;
  5033. #ifndef NO_MD5
  5034. /* Instantiate */
  5035. wc_Md5 md5;
  5036. byte* hash_test[3];
  5037. byte hash1[WC_MD5_DIGEST_SIZE];
  5038. byte hash2[2*WC_MD5_DIGEST_SIZE];
  5039. byte hash3[5*WC_MD5_DIGEST_SIZE];
  5040. int times, i, ret;
  5041. /* Initialize */
  5042. ret = wc_InitMd5(&md5);
  5043. if (ret != 0) {
  5044. flag = ret;
  5045. }
  5046. if (!flag) {
  5047. hash_test[0] = hash1;
  5048. hash_test[1] = hash2;
  5049. hash_test[2] = hash3;
  5050. }
  5051. times = sizeof(hash_test)/sizeof(byte*);
  5052. /* Test good args. */
  5053. printf(testingFmt, "wc_Md5Final()");
  5054. for (i = 0; i < times; i++) {
  5055. if (!flag) {
  5056. ret = wc_Md5Final(&md5, hash_test[i]);
  5057. if (ret != 0) {
  5058. flag = WOLFSSL_FATAL_ERROR;
  5059. }
  5060. }
  5061. }
  5062. /* Test bad args. */
  5063. if (!flag) {
  5064. ret = wc_Md5Final(NULL, NULL);
  5065. if (ret != BAD_FUNC_ARG) {
  5066. flag = WOLFSSL_FATAL_ERROR;
  5067. }
  5068. }
  5069. if (!flag) {
  5070. ret = wc_Md5Final(NULL, hash1);
  5071. if (ret != BAD_FUNC_ARG) {
  5072. flag = WOLFSSL_FATAL_ERROR;
  5073. }
  5074. }
  5075. if (!flag) {
  5076. ret = wc_Md5Final(&md5, NULL);
  5077. if (ret != BAD_FUNC_ARG) {
  5078. flag = WOLFSSL_FATAL_ERROR;
  5079. }
  5080. }
  5081. wc_Md5Free(&md5);
  5082. printf(resultFmt, flag == 0 ? passed : failed);
  5083. #endif
  5084. return flag;
  5085. }
  5086. /*
  5087. * Unit test for the wc_InitSha()
  5088. */
  5089. static int test_wc_InitSha(void)
  5090. {
  5091. int flag = 0;
  5092. #ifndef NO_SHA
  5093. wc_Sha sha;
  5094. int ret;
  5095. printf(testingFmt, "wc_InitSha()");
  5096. /* Test good arg. */
  5097. ret = wc_InitSha(&sha);
  5098. if (ret != 0) {
  5099. flag = WOLFSSL_FATAL_ERROR;
  5100. }
  5101. /* Test bad arg. */
  5102. if (!flag) {
  5103. ret = wc_InitSha(NULL);
  5104. if (ret != BAD_FUNC_ARG) {
  5105. flag = WOLFSSL_FATAL_ERROR;
  5106. }
  5107. }
  5108. wc_ShaFree(&sha);
  5109. printf(resultFmt, flag == 0 ? passed : failed);
  5110. #endif
  5111. return flag;
  5112. } /* END test_wc_InitSha */
  5113. /*
  5114. * Tesing wc_ShaUpdate()
  5115. */
  5116. static int test_wc_ShaUpdate (void)
  5117. {
  5118. int flag = 0;
  5119. #ifndef NO_SHA
  5120. wc_Sha sha;
  5121. byte hash[WC_SHA_DIGEST_SIZE];
  5122. testVector a, b, c;
  5123. int ret;
  5124. ret = wc_InitSha(&sha);
  5125. if (ret != 0) {
  5126. flag = ret;
  5127. }
  5128. printf(testingFmt, "wc_ShaUpdate()");
  5129. /* Input. */
  5130. if (!flag) {
  5131. a.input = "a";
  5132. a.inLen = XSTRLEN(a.input);
  5133. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  5134. if (ret != 0) {
  5135. flag = ret;
  5136. }
  5137. }
  5138. if (!flag) {
  5139. ret = wc_ShaFinal(&sha, hash);
  5140. if (ret != 0) {
  5141. flag = ret;
  5142. }
  5143. }
  5144. /* Update input. */
  5145. if (!flag) {
  5146. a.input = "abc";
  5147. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  5148. "\x6C\x9C\xD0\xD8\x9D";
  5149. a.inLen = XSTRLEN(a.input);
  5150. a.outLen = XSTRLEN(a.output);
  5151. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  5152. if (ret != 0) {
  5153. flag = ret;
  5154. }
  5155. }
  5156. if (!flag) {
  5157. ret = wc_ShaFinal(&sha, hash);
  5158. if (ret !=0) {
  5159. flag = ret;
  5160. }
  5161. }
  5162. if (!flag) {
  5163. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  5164. flag = WOLFSSL_FATAL_ERROR;
  5165. }
  5166. }
  5167. /* Try passing in bad values. */
  5168. if (!flag) {
  5169. b.input = NULL;
  5170. b.inLen = 0;
  5171. ret = wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen);
  5172. if (ret != 0) {
  5173. flag = ret;
  5174. }
  5175. }
  5176. if (!flag) {
  5177. c.input = NULL;
  5178. c.inLen = WC_SHA_DIGEST_SIZE;
  5179. ret = wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen);
  5180. if (ret != BAD_FUNC_ARG) {
  5181. flag = WOLFSSL_FATAL_ERROR;
  5182. }
  5183. }
  5184. if (!flag) {
  5185. ret = wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  5186. if (ret != BAD_FUNC_ARG) {
  5187. flag = WOLFSSL_FATAL_ERROR;
  5188. }
  5189. }
  5190. wc_ShaFree(&sha);
  5191. /* If not returned then the unit test passed test vectors. */
  5192. printf(resultFmt, flag == 0 ? passed : failed);
  5193. #endif
  5194. return flag;
  5195. } /* END test_wc_ShaUpdate() */
  5196. /*
  5197. * Unit test on wc_ShaFinal
  5198. */
  5199. static int test_wc_ShaFinal (void)
  5200. {
  5201. int flag = 0;
  5202. #ifndef NO_SHA
  5203. wc_Sha sha;
  5204. byte* hash_test[3];
  5205. byte hash1[WC_SHA_DIGEST_SIZE];
  5206. byte hash2[2*WC_SHA_DIGEST_SIZE];
  5207. byte hash3[5*WC_SHA_DIGEST_SIZE];
  5208. int times, i, ret;
  5209. /*Initialize*/
  5210. ret = wc_InitSha(&sha);
  5211. if (ret) {
  5212. flag = ret;
  5213. }
  5214. if (!flag) {
  5215. hash_test[0] = hash1;
  5216. hash_test[1] = hash2;
  5217. hash_test[2] = hash3;
  5218. }
  5219. times = sizeof(hash_test)/sizeof(byte*);
  5220. /* Good test args. */
  5221. printf(testingFmt, "wc_ShaFinal()");
  5222. for (i = 0; i < times; i++) {
  5223. if (!flag) {
  5224. ret = wc_ShaFinal(&sha, hash_test[i]);
  5225. if (ret != 0) {
  5226. flag = WOLFSSL_FATAL_ERROR;
  5227. }
  5228. }
  5229. }
  5230. /* Test bad args. */
  5231. if (!flag) {
  5232. ret = wc_ShaFinal(NULL, NULL);
  5233. if (ret != BAD_FUNC_ARG) {
  5234. flag = WOLFSSL_FATAL_ERROR;
  5235. }
  5236. }
  5237. if (!flag) {
  5238. ret = wc_ShaFinal(NULL, hash1);
  5239. if (ret != BAD_FUNC_ARG) {
  5240. flag = WOLFSSL_FATAL_ERROR;
  5241. }
  5242. }
  5243. if (!flag) {
  5244. ret = wc_ShaFinal(&sha, NULL);
  5245. if (ret != BAD_FUNC_ARG) {
  5246. flag = WOLFSSL_FATAL_ERROR;
  5247. }
  5248. }
  5249. wc_ShaFree(&sha);
  5250. printf(resultFmt, flag == 0 ? passed : failed);
  5251. #endif
  5252. return flag;
  5253. } /* END test_wc_ShaFinal */
  5254. /*
  5255. * Unit test for wc_InitSha256()
  5256. */
  5257. static int test_wc_InitSha256 (void)
  5258. {
  5259. int flag = 0;
  5260. #ifndef NO_SHA256
  5261. wc_Sha256 sha256;
  5262. int ret;
  5263. printf(testingFmt, "wc_InitSha256()");
  5264. /* Test good arg. */
  5265. ret = wc_InitSha256(&sha256);
  5266. if (ret != 0) {
  5267. flag = WOLFSSL_FATAL_ERROR;
  5268. }
  5269. /* Test bad arg. */
  5270. if (!flag) {
  5271. ret = wc_InitSha256(NULL);
  5272. if (ret != BAD_FUNC_ARG) {
  5273. flag = WOLFSSL_FATAL_ERROR;
  5274. }
  5275. }
  5276. wc_Sha256Free(&sha256);
  5277. printf(resultFmt, flag == 0 ? passed : failed);
  5278. #endif
  5279. return flag;
  5280. } /* END test_wc_InitSha256 */
  5281. /*
  5282. * Unit test for wc_Sha256Update()
  5283. */
  5284. static int test_wc_Sha256Update (void)
  5285. {
  5286. int flag = 0;
  5287. #ifndef NO_SHA256
  5288. wc_Sha256 sha256;
  5289. byte hash[WC_SHA256_DIGEST_SIZE];
  5290. testVector a, b, c;
  5291. int ret;
  5292. ret = wc_InitSha256(&sha256);
  5293. if (ret != 0) {
  5294. flag = ret;
  5295. }
  5296. printf(testingFmt, "wc_Sha256Update()");
  5297. /* Input. */
  5298. if (!flag) {
  5299. a.input = "a";
  5300. a.inLen = XSTRLEN(a.input);
  5301. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  5302. if (ret != 0) {
  5303. flag = ret;
  5304. }
  5305. }
  5306. if (!flag) {
  5307. ret = wc_Sha256Final(&sha256, hash);
  5308. if (ret != 0) {
  5309. flag = ret;
  5310. }
  5311. }
  5312. /* Update input. */
  5313. if (!flag) {
  5314. a.input = "abc";
  5315. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  5316. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  5317. "\x15\xAD";
  5318. a.inLen = XSTRLEN(a.input);
  5319. a.outLen = XSTRLEN(a.output);
  5320. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  5321. if (ret != 0) {
  5322. flag = ret;
  5323. }
  5324. }
  5325. if (!flag) {
  5326. ret = wc_Sha256Final(&sha256, hash);
  5327. if (ret != 0) {
  5328. flag = ret;
  5329. }
  5330. }
  5331. if (!flag) {
  5332. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  5333. flag = WOLFSSL_FATAL_ERROR;
  5334. }
  5335. }
  5336. /* Try passing in bad values */
  5337. if (!flag) {
  5338. b.input = NULL;
  5339. b.inLen = 0;
  5340. ret = wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen);
  5341. if (ret != 0) {
  5342. flag = ret;
  5343. }
  5344. }
  5345. if (!flag) {
  5346. c.input = NULL;
  5347. c.inLen = WC_SHA256_DIGEST_SIZE;
  5348. ret = wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen);
  5349. if (ret != BAD_FUNC_ARG) {
  5350. flag = WOLFSSL_FATAL_ERROR;
  5351. }
  5352. }
  5353. if (!flag) {
  5354. ret = wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen);
  5355. if (ret != BAD_FUNC_ARG) {
  5356. flag = WOLFSSL_FATAL_ERROR;
  5357. }
  5358. }
  5359. wc_Sha256Free(&sha256);
  5360. /* If not returned then the unit test passed. */
  5361. printf(resultFmt, flag == 0 ? passed : failed);
  5362. #endif
  5363. return flag;
  5364. } /* END test_wc_Sha256Update */
  5365. /*
  5366. * Unit test function for wc_Sha256Final()
  5367. */
  5368. static int test_wc_Sha256Final (void)
  5369. {
  5370. int flag = 0;
  5371. #ifndef NO_SHA256
  5372. wc_Sha256 sha256;
  5373. byte* hash_test[3];
  5374. byte hash1[WC_SHA256_DIGEST_SIZE];
  5375. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  5376. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  5377. int times, i, ret;
  5378. /* Initialize */
  5379. ret = wc_InitSha256(&sha256);
  5380. if (ret != 0) {
  5381. flag = ret;
  5382. }
  5383. if (!flag) {
  5384. hash_test[0] = hash1;
  5385. hash_test[1] = hash2;
  5386. hash_test[2] = hash3;
  5387. }
  5388. times = sizeof(hash_test) / sizeof(byte*);
  5389. /* Good test args. */
  5390. printf(testingFmt, "wc_Sha256Final()");
  5391. for (i = 0; i < times; i++) {
  5392. if (!flag) {
  5393. ret = wc_Sha256Final(&sha256, hash_test[i]);
  5394. if (ret != 0) {
  5395. flag = WOLFSSL_FATAL_ERROR;
  5396. }
  5397. }
  5398. }
  5399. /* Test bad args. */
  5400. if (!flag ) {
  5401. ret = wc_Sha256Final(NULL, NULL);
  5402. if (ret != BAD_FUNC_ARG) {
  5403. flag = WOLFSSL_FATAL_ERROR;
  5404. }
  5405. }
  5406. if (!flag) {
  5407. ret = wc_Sha256Final(NULL, hash1);
  5408. if (ret != BAD_FUNC_ARG) {
  5409. flag = WOLFSSL_FATAL_ERROR;
  5410. }
  5411. }
  5412. if (!flag) {
  5413. ret = wc_Sha256Final(&sha256, NULL);
  5414. if (ret != BAD_FUNC_ARG) {
  5415. flag = WOLFSSL_FATAL_ERROR;
  5416. }
  5417. }
  5418. wc_Sha256Free(&sha256);
  5419. printf(resultFmt, flag == 0 ? passed : failed);
  5420. #endif
  5421. return flag;
  5422. } /* END test_wc_Sha256Final */
  5423. /*
  5424. * Unit test function for wc_Sha256FinalRaw()
  5425. */
  5426. static int test_wc_Sha256FinalRaw (void)
  5427. {
  5428. int flag = 0;
  5429. #if !defined(NO_SHA256) && \
  5430. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  5431. wc_Sha256 sha256;
  5432. byte* hash_test[3];
  5433. byte hash1[WC_SHA256_DIGEST_SIZE];
  5434. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  5435. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  5436. int times, i, ret;
  5437. /* Initialize */
  5438. ret = wc_InitSha256(&sha256);
  5439. if (ret != 0) {
  5440. flag = ret;
  5441. }
  5442. if (!flag) {
  5443. hash_test[0] = hash1;
  5444. hash_test[1] = hash2;
  5445. hash_test[2] = hash3;
  5446. }
  5447. times = sizeof(hash_test) / sizeof(byte*);
  5448. /* Good test args. */
  5449. printf(testingFmt, "wc_Sha256FinalRaw()");
  5450. for (i = 0; i < times; i++) {
  5451. if (!flag) {
  5452. ret = wc_Sha256FinalRaw(&sha256, hash_test[i]);
  5453. if (ret != 0) {
  5454. flag = WOLFSSL_FATAL_ERROR;
  5455. }
  5456. }
  5457. }
  5458. /* Test bad args. */
  5459. if (!flag ) {
  5460. ret = wc_Sha256FinalRaw(NULL, NULL);
  5461. if (ret != BAD_FUNC_ARG) {
  5462. flag = WOLFSSL_FATAL_ERROR;
  5463. }
  5464. }
  5465. if (!flag) {
  5466. ret = wc_Sha256FinalRaw(NULL, hash1);
  5467. if (ret != BAD_FUNC_ARG) {
  5468. flag = WOLFSSL_FATAL_ERROR;
  5469. }
  5470. }
  5471. if (!flag) {
  5472. ret = wc_Sha256FinalRaw(&sha256, NULL);
  5473. if (ret != BAD_FUNC_ARG) {
  5474. flag = WOLFSSL_FATAL_ERROR;
  5475. }
  5476. }
  5477. wc_Sha256Free(&sha256);
  5478. printf(resultFmt, flag == 0 ? passed : failed);
  5479. #endif
  5480. return flag;
  5481. } /* END test_wc_Sha256FinalRaw */
  5482. /*
  5483. * Unit test function for wc_Sha256GetFlags()
  5484. */
  5485. static int test_wc_Sha256GetFlags (void)
  5486. {
  5487. int flag = 0;
  5488. #if !defined(NO_SHA256) && \
  5489. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  5490. wc_Sha256 sha256;
  5491. word32 flags = 0;
  5492. printf(testingFmt, "wc_Sha256GetFlags()");
  5493. /* Initialize */
  5494. flag = wc_InitSha256(&sha256);
  5495. if (flag == 0) {
  5496. flag = wc_Sha256GetFlags(&sha256, &flags);
  5497. }
  5498. if (flag == 0) {
  5499. if (flags & WC_HASH_FLAG_ISCOPY) {
  5500. flag = 0;
  5501. }
  5502. }
  5503. wc_Sha256Free(&sha256);
  5504. printf(resultFmt, flag == 0 ? passed : failed);
  5505. #endif
  5506. return flag;
  5507. } /* END test_wc_Sha256GetFlags */
  5508. /*
  5509. * Unit test function for wc_Sha256Free()
  5510. */
  5511. static int test_wc_Sha256Free (void)
  5512. {
  5513. int flag = 0;
  5514. #ifndef NO_SHA256
  5515. printf(testingFmt, "wc_Sha256Free()");
  5516. wc_Sha256Free(NULL);
  5517. printf(resultFmt, flag == 0 ? passed : failed);
  5518. #endif
  5519. return flag;
  5520. } /* END test_wc_Sha256Free */
  5521. /*
  5522. * Unit test function for wc_Sha256GetHash()
  5523. */
  5524. static int test_wc_Sha256GetHash (void)
  5525. {
  5526. int flag = 0;
  5527. #ifndef NO_SHA256
  5528. wc_Sha256 sha256;
  5529. byte hash1[WC_SHA256_DIGEST_SIZE];
  5530. printf(testingFmt, "wc_Sha256GetHash()");
  5531. /* Initialize */
  5532. flag = wc_InitSha256(&sha256);
  5533. if (flag == 0) {
  5534. flag = wc_Sha256GetHash(&sha256, hash1);
  5535. }
  5536. /*test bad arguements*/
  5537. if (flag == 0) {
  5538. flag = wc_Sha256GetHash(NULL, NULL);
  5539. if (flag == BAD_FUNC_ARG) {
  5540. flag = 0;
  5541. }
  5542. }
  5543. if (flag == 0) {
  5544. flag = wc_Sha256GetHash(NULL, hash1);
  5545. if (flag == BAD_FUNC_ARG) {
  5546. flag = 0;
  5547. }
  5548. }
  5549. if (flag == 0) {
  5550. flag = wc_Sha256GetHash(&sha256, NULL);
  5551. if (flag == BAD_FUNC_ARG) {
  5552. flag = 0;
  5553. }
  5554. }
  5555. wc_Sha256Free(&sha256);
  5556. printf(resultFmt, flag == 0 ? passed : failed);
  5557. #endif
  5558. return flag;
  5559. } /* END test_wc_Sha256GetHash */
  5560. /*
  5561. * Unit test function for wc_Sha256Copy()
  5562. */
  5563. static int test_wc_Sha256Copy (void)
  5564. {
  5565. int flag = 0;
  5566. #ifndef NO_SHA256
  5567. wc_Sha256 sha256;
  5568. wc_Sha256 temp;
  5569. printf(testingFmt, "wc_Sha256Copy()");
  5570. /* Initialize */
  5571. flag = wc_InitSha256(&sha256);
  5572. if (flag == 0) {
  5573. flag = wc_InitSha256(&temp);
  5574. }
  5575. if (flag == 0) {
  5576. flag = wc_Sha256Copy(&sha256, &temp);
  5577. }
  5578. /*test bad arguements*/
  5579. if (flag == 0) {
  5580. flag = wc_Sha256Copy(NULL, NULL);
  5581. if (flag == BAD_FUNC_ARG) {
  5582. flag = 0;
  5583. }
  5584. }
  5585. if (flag == 0) {
  5586. flag = wc_Sha256Copy(NULL, &temp);
  5587. if (flag == BAD_FUNC_ARG) {
  5588. flag = 0;
  5589. }
  5590. }
  5591. if (flag == 0) {
  5592. flag = wc_Sha256Copy(&sha256, NULL);
  5593. if (flag == BAD_FUNC_ARG) {
  5594. flag = 0;
  5595. }
  5596. }
  5597. wc_Sha256Free(&sha256);
  5598. wc_Sha256Free(&temp);
  5599. printf(resultFmt, flag == 0 ? passed : failed);
  5600. #endif
  5601. return flag;
  5602. } /* END test_wc_Sha256Copy */
  5603. /*
  5604. * Testing wc_InitSha512()
  5605. */
  5606. static int test_wc_InitSha512 (void)
  5607. {
  5608. int flag = 0;
  5609. #ifdef WOLFSSL_SHA512
  5610. wc_Sha512 sha512;
  5611. int ret;
  5612. printf(testingFmt, "wc_InitSha512()");
  5613. /* Test good arg. */
  5614. ret = wc_InitSha512(&sha512);
  5615. if (ret != 0) {
  5616. flag = WOLFSSL_FATAL_ERROR;
  5617. }
  5618. /* Test bad arg. */
  5619. if (!flag) {
  5620. ret = wc_InitSha512(NULL);
  5621. if (ret != BAD_FUNC_ARG) {
  5622. flag = WOLFSSL_FATAL_ERROR;
  5623. }
  5624. }
  5625. wc_Sha512Free(&sha512);
  5626. printf(resultFmt, flag == 0 ? passed : failed);
  5627. #endif
  5628. return flag;
  5629. } /* END test_wc_InitSha512 */
  5630. /*
  5631. * wc_Sha512Update() test.
  5632. */
  5633. static int test_wc_Sha512Update (void)
  5634. {
  5635. int flag = 0;
  5636. #ifdef WOLFSSL_SHA512
  5637. wc_Sha512 sha512;
  5638. byte hash[WC_SHA512_DIGEST_SIZE];
  5639. testVector a, b, c;
  5640. int ret;
  5641. ret = wc_InitSha512(&sha512);
  5642. if (ret != 0) {
  5643. flag = ret;
  5644. }
  5645. printf(testingFmt, "wc_Sha512Update()");
  5646. /* Input. */
  5647. if (!flag) {
  5648. a.input = "a";
  5649. a.inLen = XSTRLEN(a.input);
  5650. ret = wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen);
  5651. if (ret != 0) {
  5652. flag = ret;
  5653. }
  5654. ret = wc_Sha512Final(&sha512, hash);
  5655. if (ret != 0) {
  5656. flag = ret;
  5657. }
  5658. }
  5659. /* Update input. */
  5660. if (!flag) {
  5661. a.input = "abc";
  5662. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  5663. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  5664. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  5665. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  5666. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  5667. a.inLen = XSTRLEN(a.input);
  5668. a.outLen = XSTRLEN(a.output);
  5669. ret = wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen);
  5670. if (ret != 0) {
  5671. flag = ret;
  5672. }
  5673. }
  5674. if (!flag) {
  5675. ret = wc_Sha512Final(&sha512, hash);
  5676. if (ret != 0) {
  5677. flag = ret;
  5678. }
  5679. }
  5680. if (!flag) {
  5681. if (XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE) != 0) {
  5682. flag = WOLFSSL_FATAL_ERROR;
  5683. }
  5684. }
  5685. /* Try passing in bad values */
  5686. if (!flag) {
  5687. b.input = NULL;
  5688. b.inLen = 0;
  5689. ret = wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen);
  5690. if (ret != 0) {
  5691. flag = ret;
  5692. }
  5693. }
  5694. if (!flag) {
  5695. c.input = NULL;
  5696. c.inLen = WC_SHA512_DIGEST_SIZE;
  5697. ret = wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen);
  5698. if (ret != BAD_FUNC_ARG) {
  5699. flag = WOLFSSL_FATAL_ERROR;
  5700. }
  5701. }
  5702. if (!flag) {
  5703. ret = wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen);
  5704. if (ret != BAD_FUNC_ARG) {
  5705. flag = WOLFSSL_FATAL_ERROR;
  5706. }
  5707. }
  5708. wc_Sha512Free(&sha512);
  5709. /* If not returned then the unit test passed test vectors. */
  5710. printf(resultFmt, flag == 0 ? passed : failed);
  5711. #endif
  5712. return flag;
  5713. } /* END test_wc_Sha512Update */
  5714. /*
  5715. * Unit test function for wc_Sha512Final()
  5716. */
  5717. static int test_wc_Sha512Final (void)
  5718. {
  5719. int flag = 0;
  5720. #ifdef WOLFSSL_SHA512
  5721. wc_Sha512 sha512;
  5722. byte* hash_test[3];
  5723. byte hash1[WC_SHA512_DIGEST_SIZE];
  5724. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  5725. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  5726. int times, i, ret;
  5727. /* Initialize */
  5728. ret = wc_InitSha512(&sha512);
  5729. if (ret != 0) {
  5730. flag = ret;
  5731. }
  5732. if (!flag) {
  5733. hash_test[0] = hash1;
  5734. hash_test[1] = hash2;
  5735. hash_test[2] = hash3;
  5736. }
  5737. times = sizeof(hash_test) / sizeof(byte *);
  5738. /* Good test args. */
  5739. printf(testingFmt, "wc_Sha512Final()");
  5740. for (i = 0; i < times; i++) {
  5741. if (!flag) {
  5742. ret = wc_Sha512Final(&sha512, hash_test[i]);
  5743. if (ret != 0) {
  5744. flag = WOLFSSL_FATAL_ERROR;
  5745. }
  5746. }
  5747. }
  5748. /* Test bad args. */
  5749. if (!flag) {
  5750. ret = wc_Sha512Final(NULL, NULL);
  5751. if (ret != BAD_FUNC_ARG) {
  5752. flag = WOLFSSL_FATAL_ERROR;
  5753. }
  5754. if (!flag) {}
  5755. ret = wc_Sha512Final(NULL, hash1);
  5756. if (ret != BAD_FUNC_ARG) {
  5757. flag = WOLFSSL_FATAL_ERROR;
  5758. }
  5759. }
  5760. if (!flag) {
  5761. ret = wc_Sha512Final(&sha512, NULL);
  5762. if (ret != BAD_FUNC_ARG) {
  5763. flag = WOLFSSL_FATAL_ERROR;
  5764. }
  5765. }
  5766. wc_Sha512Free(&sha512);
  5767. printf(resultFmt, flag == 0 ? passed : failed);
  5768. #endif
  5769. return flag;
  5770. } /* END test_wc_Sha512Final */
  5771. /*
  5772. * Unit test function for wc_Sha512GetFlags()
  5773. */
  5774. static int test_wc_Sha512GetFlags (void)
  5775. {
  5776. int flag = 0;
  5777. #if !defined(NO_SHA512) && \
  5778. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  5779. wc_Sha512 sha512;
  5780. word32 flags = 0;
  5781. printf(testingFmt, "wc_Sha512GetFlags()");
  5782. /* Initialize */
  5783. flag = wc_InitSha512(&sha512);
  5784. if (flag == 0) {
  5785. flag = wc_Sha512GetFlags(&sha512, &flags);
  5786. }
  5787. if (flag == 0) {
  5788. if (flags & WC_HASH_FLAG_ISCOPY) {
  5789. flag = 0;
  5790. }
  5791. }
  5792. wc_Sha512Free(&sha512);
  5793. printf(resultFmt, flag == 0 ? passed : failed);
  5794. #endif
  5795. return flag;
  5796. } /* END test_wc_Sha512GetFlags */
  5797. /*
  5798. * Unit test function for wc_Sha512FinalRaw()
  5799. */
  5800. static int test_wc_Sha512FinalRaw (void)
  5801. {
  5802. int flag = 0;
  5803. #if !defined(NO_SHA512) && \
  5804. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  5805. wc_Sha512 sha512;
  5806. byte* hash_test[3];
  5807. byte hash1[WC_SHA512_DIGEST_SIZE];
  5808. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  5809. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  5810. int times, i, ret;
  5811. /* Initialize */
  5812. ret = wc_InitSha512(&sha512);
  5813. if (ret != 0) {
  5814. flag = ret;
  5815. }
  5816. if (!flag) {
  5817. hash_test[0] = hash1;
  5818. hash_test[1] = hash2;
  5819. hash_test[2] = hash3;
  5820. }
  5821. times = sizeof(hash_test) / sizeof(byte*);
  5822. /* Good test args. */
  5823. printf(testingFmt, "wc_Sha512FinalRaw()");
  5824. for (i = 0; i < times; i++) {
  5825. if (!flag) {
  5826. ret = wc_Sha512FinalRaw(&sha512, hash_test[i]);
  5827. if (ret != 0) {
  5828. flag = WOLFSSL_FATAL_ERROR;
  5829. }
  5830. }
  5831. }
  5832. /* Test bad args. */
  5833. if (!flag ) {
  5834. ret = wc_Sha512FinalRaw(NULL, NULL);
  5835. if (ret != BAD_FUNC_ARG) {
  5836. flag = WOLFSSL_FATAL_ERROR;
  5837. }
  5838. }
  5839. if (!flag) {
  5840. ret = wc_Sha512FinalRaw(NULL, hash1);
  5841. if (ret != BAD_FUNC_ARG) {
  5842. flag = WOLFSSL_FATAL_ERROR;
  5843. }
  5844. }
  5845. if (!flag) {
  5846. ret = wc_Sha512FinalRaw(&sha512, NULL);
  5847. if (ret != BAD_FUNC_ARG) {
  5848. flag = WOLFSSL_FATAL_ERROR;
  5849. }
  5850. }
  5851. wc_Sha512Free(&sha512);
  5852. printf(resultFmt, flag == 0 ? passed : failed);
  5853. #endif
  5854. return flag;
  5855. } /* END test_wc_Sha512FinalRaw */
  5856. /*
  5857. * Unit test function for wc_Sha512Free()
  5858. */
  5859. static int test_wc_Sha512Free (void)
  5860. {
  5861. int flag = 0;
  5862. #if !defined(NO_SHA512) && \
  5863. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  5864. printf(testingFmt, "wc_Sha512Free()");
  5865. wc_Sha512Free(NULL);
  5866. printf(resultFmt, flag == 0 ? passed : failed);
  5867. #endif
  5868. return flag;
  5869. } /* END test_wc_Sha512Free */
  5870. /*
  5871. * Unit test function for wc_Sha512GetHash()
  5872. */
  5873. static int test_wc_Sha512GetHash (void)
  5874. {
  5875. int flag = 0;
  5876. #if !defined(NO_SHA512) && \
  5877. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  5878. wc_Sha512 sha512;
  5879. byte hash1[WC_SHA512_DIGEST_SIZE];
  5880. printf(testingFmt, "wc_Sha512GetHash()");
  5881. /* Initialize */
  5882. flag = wc_InitSha512(&sha512);
  5883. if (flag == 0) {
  5884. flag = wc_Sha512GetHash(&sha512, hash1);
  5885. }
  5886. /*test bad arguements*/
  5887. if (flag == 0) {
  5888. flag = wc_Sha512GetHash(NULL, NULL);
  5889. if (flag == BAD_FUNC_ARG) {
  5890. flag = 0;
  5891. }
  5892. }
  5893. if (flag == 0) {
  5894. flag = wc_Sha512GetHash(NULL, hash1);
  5895. if (flag == BAD_FUNC_ARG) {
  5896. flag = 0;
  5897. }
  5898. }
  5899. if (flag == 0) {
  5900. flag = wc_Sha512GetHash(&sha512, NULL);
  5901. if (flag == BAD_FUNC_ARG) {
  5902. flag = 0;
  5903. }
  5904. }
  5905. wc_Sha512Free(&sha512);
  5906. printf(resultFmt, flag == 0 ? passed : failed);
  5907. #endif
  5908. return flag;
  5909. } /* END test_wc_Sha512GetHash */
  5910. /*
  5911. * Unit test function for wc_Sha512Copy()
  5912. */
  5913. static int test_wc_Sha512Copy (void)
  5914. {
  5915. int flag = 0;
  5916. #if !defined(NO_SHA512) && \
  5917. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  5918. wc_Sha512 sha512;
  5919. wc_Sha512 temp;
  5920. printf(testingFmt, "wc_Sha512Copy()");
  5921. /* Initialize */
  5922. flag = wc_InitSha512(&sha512);
  5923. if (flag == 0) {
  5924. flag = wc_InitSha512(&temp);
  5925. }
  5926. if (flag == 0) {
  5927. flag = wc_Sha512Copy(&sha512, &temp);
  5928. }
  5929. /*test bad arguements*/
  5930. if (flag == 0) {
  5931. flag = wc_Sha512Copy(NULL, NULL);
  5932. if (flag == BAD_FUNC_ARG) {
  5933. flag = 0;
  5934. }
  5935. }
  5936. if (flag == 0) {
  5937. flag = wc_Sha512Copy(NULL, &temp);
  5938. if (flag == BAD_FUNC_ARG) {
  5939. flag = 0;
  5940. }
  5941. }
  5942. if (flag == 0) {
  5943. flag = wc_Sha512Copy(&sha512, NULL);
  5944. if (flag == BAD_FUNC_ARG) {
  5945. flag = 0;
  5946. }
  5947. }
  5948. wc_Sha512Free(&sha512);
  5949. wc_Sha512Free(&temp);
  5950. printf(resultFmt, flag == 0 ? passed : failed);
  5951. #endif
  5952. return flag;
  5953. } /* END test_wc_Sha512Copy */
  5954. /*
  5955. * Testing wc_InitSha384()
  5956. */
  5957. static int test_wc_InitSha384 (void)
  5958. {
  5959. int flag = 0;
  5960. #ifdef WOLFSSL_SHA384
  5961. wc_Sha384 sha384;
  5962. int ret;
  5963. printf(testingFmt, "wc_InitSha384()");
  5964. /* Test good arg. */
  5965. ret = wc_InitSha384(&sha384);
  5966. if (ret != 0) {
  5967. flag = WOLFSSL_FATAL_ERROR;
  5968. }
  5969. /* Test bad arg. */
  5970. if (!flag) {
  5971. ret = wc_InitSha384(NULL);
  5972. if (ret != BAD_FUNC_ARG) {
  5973. flag = WOLFSSL_FATAL_ERROR;
  5974. }
  5975. }
  5976. wc_Sha384Free(&sha384);
  5977. printf(resultFmt, flag == 0 ? passed : failed);
  5978. #endif
  5979. return flag;
  5980. } /* END test_wc_InitSha384 */
  5981. /*
  5982. * test wc_Sha384Update()
  5983. */
  5984. static int test_wc_Sha384Update (void)
  5985. {
  5986. int flag = 0;
  5987. #ifdef WOLFSSL_SHA384
  5988. wc_Sha384 sha384;
  5989. byte hash[WC_SHA384_DIGEST_SIZE];
  5990. testVector a, b, c;
  5991. int ret;
  5992. ret = wc_InitSha384(&sha384);
  5993. if (ret != 0) {
  5994. flag = ret;
  5995. }
  5996. printf(testingFmt, "wc_Sha384Update()");
  5997. /* Input */
  5998. if (!flag) {
  5999. a.input = "a";
  6000. a.inLen = XSTRLEN(a.input);
  6001. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  6002. if (ret != 0) {
  6003. flag = ret;
  6004. }
  6005. }
  6006. if (!flag) {
  6007. ret = wc_Sha384Final(&sha384, hash);
  6008. if (ret != 0) {
  6009. flag = ret;
  6010. }
  6011. }
  6012. /* Update input. */
  6013. if (!flag) {
  6014. a.input = "abc";
  6015. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  6016. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  6017. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  6018. "\xc8\x25\xa7";
  6019. a.inLen = XSTRLEN(a.input);
  6020. a.outLen = XSTRLEN(a.output);
  6021. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  6022. if (ret != 0) {
  6023. flag = ret;
  6024. }
  6025. }
  6026. if (!flag) {
  6027. ret = wc_Sha384Final(&sha384, hash);
  6028. if (ret != 0) {
  6029. flag = ret;
  6030. }
  6031. }
  6032. if (!flag) {
  6033. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  6034. flag = WOLFSSL_FATAL_ERROR;
  6035. }
  6036. }
  6037. /* Pass in bad values. */
  6038. if (!flag) {
  6039. b.input = NULL;
  6040. b.inLen = 0;
  6041. ret = wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen);
  6042. if (ret != 0) {
  6043. flag = ret;
  6044. }
  6045. }
  6046. if (!flag) {
  6047. c.input = NULL;
  6048. c.inLen = WC_SHA384_DIGEST_SIZE;
  6049. ret = wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen);
  6050. if (ret != BAD_FUNC_ARG) {
  6051. flag = WOLFSSL_FATAL_ERROR;
  6052. }
  6053. }
  6054. if (!flag) {
  6055. ret = wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen);
  6056. if (ret != BAD_FUNC_ARG) {
  6057. flag = WOLFSSL_FATAL_ERROR;
  6058. }
  6059. }
  6060. wc_Sha384Free(&sha384);
  6061. /* If not returned then the unit test passed test vectors. */
  6062. printf(resultFmt, flag == 0 ? passed : failed);
  6063. #endif
  6064. return flag;
  6065. } /* END test_wc_Sha384Update */
  6066. /*
  6067. * Unit test function for wc_Sha384Final();
  6068. */
  6069. static int test_wc_Sha384Final (void)
  6070. {
  6071. int flag = 0;
  6072. #ifdef WOLFSSL_SHA384
  6073. wc_Sha384 sha384;
  6074. byte* hash_test[3];
  6075. byte hash1[WC_SHA384_DIGEST_SIZE];
  6076. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  6077. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  6078. int times, i, ret;
  6079. /* Initialize */
  6080. ret = wc_InitSha384(&sha384);
  6081. if (ret) {
  6082. flag = ret;
  6083. }
  6084. if (!flag) {
  6085. hash_test[0] = hash1;
  6086. hash_test[1] = hash2;
  6087. hash_test[2] = hash3;
  6088. }
  6089. times = sizeof(hash_test) / sizeof(byte*);
  6090. /* Good test args. */
  6091. printf(testingFmt, "wc_Sha384Final()");
  6092. for (i = 0; i < times; i++) {
  6093. if (!flag) {
  6094. ret = wc_Sha384Final(&sha384, hash_test[i]);
  6095. if (ret != 0) {
  6096. flag = WOLFSSL_FATAL_ERROR;
  6097. }
  6098. }
  6099. }
  6100. /* Test bad args. */
  6101. if (!flag) {
  6102. ret = wc_Sha384Final(NULL, NULL);
  6103. if (ret != BAD_FUNC_ARG) {
  6104. flag = WOLFSSL_FATAL_ERROR;
  6105. }
  6106. }
  6107. if (!flag) {
  6108. ret = wc_Sha384Final(NULL, hash1);
  6109. if (ret != BAD_FUNC_ARG) {
  6110. flag = WOLFSSL_FATAL_ERROR;
  6111. }
  6112. }
  6113. if (!flag) {
  6114. ret = wc_Sha384Final(&sha384, NULL);
  6115. if (ret != BAD_FUNC_ARG) {
  6116. flag = WOLFSSL_FATAL_ERROR;
  6117. }
  6118. }
  6119. wc_Sha384Free(&sha384);
  6120. printf(resultFmt, flag == 0 ? passed : failed);
  6121. #endif
  6122. return flag;
  6123. } /* END test_wc_Sha384Final */
  6124. /*
  6125. * Unit test function for wc_Sha384GetFlags()
  6126. */
  6127. static int test_wc_Sha384GetFlags (void)
  6128. {
  6129. int flag = 0;
  6130. #if !defined(NO_SHA384) && \
  6131. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  6132. wc_Sha384 sha384;
  6133. word32 flags = 0;
  6134. printf(testingFmt, "wc_Sha384GetFlags()");
  6135. /* Initialize */
  6136. flag = wc_InitSha384(&sha384);
  6137. if (flag == 0) {
  6138. flag = wc_Sha384GetFlags(&sha384, &flags);
  6139. }
  6140. if (flag == 0) {
  6141. if (flags & WC_HASH_FLAG_ISCOPY) {
  6142. flag = 0;
  6143. }
  6144. }
  6145. wc_Sha384Free(&sha384);
  6146. printf(resultFmt, flag == 0 ? passed : failed);
  6147. #endif
  6148. return flag;
  6149. } /* END test_wc_Sha384GetFlags */
  6150. /*
  6151. * Unit test function for wc_Sha384FinalRaw()
  6152. */
  6153. static int test_wc_Sha384FinalRaw (void)
  6154. {
  6155. int flag = 0;
  6156. #if !defined(NO_SHA384) && \
  6157. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  6158. wc_Sha384 sha384;
  6159. byte* hash_test[3];
  6160. byte hash1[WC_SHA384_DIGEST_SIZE];
  6161. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  6162. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  6163. int times, i, ret;
  6164. /* Initialize */
  6165. ret = wc_InitSha384(&sha384);
  6166. if (ret != 0) {
  6167. flag = ret;
  6168. }
  6169. if (!flag) {
  6170. hash_test[0] = hash1;
  6171. hash_test[1] = hash2;
  6172. hash_test[2] = hash3;
  6173. }
  6174. times = sizeof(hash_test) / sizeof(byte*);
  6175. /* Good test args. */
  6176. printf(testingFmt, "wc_Sha384FinalRaw()");
  6177. for (i = 0; i < times; i++) {
  6178. if (!flag) {
  6179. ret = wc_Sha384FinalRaw(&sha384, hash_test[i]);
  6180. if (ret != 0) {
  6181. flag = WOLFSSL_FATAL_ERROR;
  6182. }
  6183. }
  6184. }
  6185. /* Test bad args. */
  6186. if (!flag ) {
  6187. ret = wc_Sha384FinalRaw(NULL, NULL);
  6188. if (ret != BAD_FUNC_ARG) {
  6189. flag = WOLFSSL_FATAL_ERROR;
  6190. }
  6191. }
  6192. if (!flag) {
  6193. ret = wc_Sha384FinalRaw(NULL, hash1);
  6194. if (ret != BAD_FUNC_ARG) {
  6195. flag = WOLFSSL_FATAL_ERROR;
  6196. }
  6197. }
  6198. if (!flag) {
  6199. ret = wc_Sha384FinalRaw(&sha384, NULL);
  6200. if (ret != BAD_FUNC_ARG) {
  6201. flag = WOLFSSL_FATAL_ERROR;
  6202. }
  6203. }
  6204. wc_Sha384Free(&sha384);
  6205. printf(resultFmt, flag == 0 ? passed : failed);
  6206. #endif
  6207. return flag;
  6208. } /* END test_wc_Sha384FinalRaw */
  6209. /*
  6210. * Unit test function for wc_Sha384Free()
  6211. */
  6212. static int test_wc_Sha384Free (void)
  6213. {
  6214. int flag = 0;
  6215. #if !defined(NO_SHA384) && \
  6216. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  6217. printf(testingFmt, "wc_Sha384Free()");
  6218. wc_Sha384Free(NULL);
  6219. printf(resultFmt, flag == 0 ? passed : failed);
  6220. #endif
  6221. return flag;
  6222. } /* END test_wc_Sha384Free */
  6223. /*
  6224. * Unit test function for wc_Sha384GetHash()
  6225. */
  6226. static int test_wc_Sha384GetHash (void)
  6227. {
  6228. int flag = 0;
  6229. #if !defined(NO_SHA384) && \
  6230. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  6231. wc_Sha384 sha384;
  6232. byte hash1[WC_SHA384_DIGEST_SIZE];
  6233. printf(testingFmt, "wc_Sha384GetHash()");
  6234. /* Initialize */
  6235. flag = wc_InitSha384(&sha384);
  6236. if (flag == 0) {
  6237. flag = wc_Sha384GetHash(&sha384, hash1);
  6238. }
  6239. /*test bad arguements*/
  6240. if (flag == 0) {
  6241. flag = wc_Sha384GetHash(NULL, NULL);
  6242. if (flag == BAD_FUNC_ARG) {
  6243. flag = 0;
  6244. }
  6245. }
  6246. if (flag == 0) {
  6247. flag = wc_Sha384GetHash(NULL, hash1);
  6248. if (flag == BAD_FUNC_ARG) {
  6249. flag = 0;
  6250. }
  6251. }
  6252. if (flag == 0) {
  6253. flag = wc_Sha384GetHash(&sha384, NULL);
  6254. if (flag == BAD_FUNC_ARG) {
  6255. flag = 0;
  6256. }
  6257. }
  6258. wc_Sha384Free(&sha384);
  6259. printf(resultFmt, flag == 0 ? passed : failed);
  6260. #endif
  6261. return flag;
  6262. } /* END test_wc_Sha384GetHash */
  6263. /*
  6264. * Unit test function for wc_Sha384Copy()
  6265. */
  6266. static int test_wc_Sha384Copy (void)
  6267. {
  6268. int flag = 0;
  6269. #if !defined(NO_SHA384) && \
  6270. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  6271. wc_Sha384 sha384;
  6272. wc_Sha384 temp;
  6273. printf(testingFmt, "wc_Sha384Copy()");
  6274. /* Initialize */
  6275. flag = wc_InitSha384(&sha384);
  6276. if (flag == 0) {
  6277. flag = wc_InitSha384(&temp);
  6278. }
  6279. if (flag == 0) {
  6280. flag = wc_Sha384Copy(&sha384, &temp);
  6281. }
  6282. /*test bad arguements*/
  6283. if (flag == 0) {
  6284. flag = wc_Sha384Copy(NULL, NULL);
  6285. if (flag == BAD_FUNC_ARG) {
  6286. flag = 0;
  6287. }
  6288. }
  6289. if (flag == 0) {
  6290. flag = wc_Sha384Copy(NULL, &temp);
  6291. if (flag == BAD_FUNC_ARG) {
  6292. flag = 0;
  6293. }
  6294. }
  6295. if (flag == 0) {
  6296. flag = wc_Sha384Copy(&sha384, NULL);
  6297. if (flag == BAD_FUNC_ARG) {
  6298. flag = 0;
  6299. }
  6300. }
  6301. wc_Sha384Free(&sha384);
  6302. wc_Sha384Free(&temp);
  6303. printf(resultFmt, flag == 0 ? passed : failed);
  6304. #endif
  6305. return flag;
  6306. } /* END test_wc_Sha384Copy */
  6307. /*
  6308. * Testing wc_InitSha224();
  6309. */
  6310. static int test_wc_InitSha224 (void)
  6311. {
  6312. int flag = 0;
  6313. #ifdef WOLFSSL_SHA224
  6314. wc_Sha224 sha224;
  6315. int ret;
  6316. printf(testingFmt, "wc_InitSha224()");
  6317. /* Test good arg. */
  6318. ret = wc_InitSha224(&sha224);
  6319. if (ret != 0) {
  6320. flag = WOLFSSL_FATAL_ERROR;
  6321. }
  6322. /* Test bad arg. */
  6323. if (!flag) {
  6324. ret = wc_InitSha224(NULL);
  6325. if (ret != BAD_FUNC_ARG) {
  6326. flag = WOLFSSL_FATAL_ERROR;
  6327. }
  6328. }
  6329. wc_Sha224Free(&sha224);
  6330. printf(resultFmt, flag == 0 ? passed : failed);
  6331. #endif
  6332. return flag;
  6333. } /* END test_wc_InitSha224 */
  6334. /*
  6335. * Unit test on wc_Sha224Update
  6336. */
  6337. static int test_wc_Sha224Update (void)
  6338. {
  6339. int flag = 0;
  6340. #ifdef WOLFSSL_SHA224
  6341. wc_Sha224 sha224;
  6342. byte hash[WC_SHA224_DIGEST_SIZE];
  6343. testVector a, b, c;
  6344. int ret;
  6345. ret = wc_InitSha224(&sha224);
  6346. if (ret != 0) {
  6347. flag = ret;
  6348. }
  6349. printf(testingFmt, "wc_Sha224Update()");
  6350. /* Input. */
  6351. if (!flag) {
  6352. a.input = "a";
  6353. a.inLen = XSTRLEN(a.input);
  6354. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  6355. if (ret != 0) {
  6356. flag = ret;
  6357. }
  6358. }
  6359. if (!flag) {
  6360. ret = wc_Sha224Final(&sha224, hash);
  6361. if (ret != 0) {
  6362. flag = ret;
  6363. }
  6364. }
  6365. /* Update input. */
  6366. if (!flag) {
  6367. a.input = "abc";
  6368. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  6369. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  6370. a.inLen = XSTRLEN(a.input);
  6371. a.outLen = XSTRLEN(a.output);
  6372. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  6373. if (ret != 0) {
  6374. flag = ret;
  6375. }
  6376. }
  6377. if (!flag) {
  6378. ret = wc_Sha224Final(&sha224, hash);
  6379. if (ret != 0) {
  6380. flag = ret;
  6381. }
  6382. }
  6383. if (!flag) {
  6384. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  6385. flag = WOLFSSL_FATAL_ERROR;
  6386. }
  6387. }
  6388. /* Pass in bad values. */
  6389. if (!flag) {
  6390. b.input = NULL;
  6391. b.inLen = 0;
  6392. ret = wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen);
  6393. if (ret != 0) {
  6394. flag = ret;
  6395. }
  6396. }
  6397. if (!flag) {
  6398. c.input = NULL;
  6399. c.inLen = WC_SHA224_DIGEST_SIZE;
  6400. ret = wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen);
  6401. if (ret != BAD_FUNC_ARG) {
  6402. flag = WOLFSSL_FATAL_ERROR;
  6403. }
  6404. }
  6405. if (!flag) {
  6406. ret = wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen);
  6407. if (ret != BAD_FUNC_ARG) {
  6408. flag = WOLFSSL_FATAL_ERROR;
  6409. }
  6410. }
  6411. wc_Sha224Free(&sha224);
  6412. /* If not returned then the unit test passed test vectors. */
  6413. printf(resultFmt, flag == 0 ? passed : failed);
  6414. #endif
  6415. return flag;
  6416. } /* END test_wc_Sha224Update */
  6417. /*
  6418. * Unit test for wc_Sha224Final();
  6419. */
  6420. static int test_wc_Sha224Final (void)
  6421. {
  6422. int flag = 0;
  6423. #ifdef WOLFSSL_SHA224
  6424. wc_Sha224 sha224;
  6425. byte* hash_test[3];
  6426. byte hash1[WC_SHA224_DIGEST_SIZE];
  6427. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  6428. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  6429. int times, i, ret;
  6430. /* Initialize */
  6431. ret = wc_InitSha224(&sha224);
  6432. if (ret) {
  6433. flag = ret;
  6434. }
  6435. if (!flag) {
  6436. hash_test[0] = hash1;
  6437. hash_test[1] = hash2;
  6438. hash_test[2] = hash3;
  6439. }
  6440. times = sizeof(hash_test) / sizeof(byte*);
  6441. /* Good test args. */
  6442. printf(testingFmt, "wc_sha224Final()");
  6443. /* Testing oversized buffers. */
  6444. for (i = 0; i < times; i++) {
  6445. if (!flag) {
  6446. ret = wc_Sha224Final(&sha224, hash_test[i]);
  6447. if (ret != 0) {
  6448. flag = WOLFSSL_FATAL_ERROR;
  6449. }
  6450. }
  6451. }
  6452. /* Test bad args. */
  6453. if (!flag) {
  6454. ret = wc_Sha224Final(NULL, NULL);
  6455. if (ret != BAD_FUNC_ARG) {
  6456. flag = WOLFSSL_FATAL_ERROR;
  6457. }
  6458. }
  6459. if (!flag) {
  6460. ret = wc_Sha224Final(NULL, hash1);
  6461. if (ret != BAD_FUNC_ARG) {
  6462. flag = WOLFSSL_FATAL_ERROR;
  6463. }
  6464. }
  6465. if (!flag) {
  6466. ret = wc_Sha224Final(&sha224, NULL);
  6467. if (ret != BAD_FUNC_ARG) {
  6468. flag = WOLFSSL_FATAL_ERROR;
  6469. }
  6470. }
  6471. wc_Sha224Free(&sha224);
  6472. printf(resultFmt, flag == 0 ? passed : failed);
  6473. #endif
  6474. return flag;
  6475. } /* END test_wc_Sha224Final */
  6476. /*
  6477. * Unit test function for wc_Sha224SetFlags()
  6478. */
  6479. static int test_wc_Sha224SetFlags (void)
  6480. {
  6481. int flag = 0;
  6482. #if defined(WOLFSSL_SHA224) && \
  6483. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  6484. wc_Sha224 sha224;
  6485. word32 flags = 0;
  6486. printf(testingFmt, "wc_Sha224SetFlags()");
  6487. /* Initialize */
  6488. flag = wc_InitSha224(&sha224);
  6489. if (flag == 0) {
  6490. flag = wc_Sha224SetFlags(&sha224, flags);
  6491. }
  6492. if (flag == 0) {
  6493. if (flags & WC_HASH_FLAG_ISCOPY) {
  6494. flag = 0;
  6495. }
  6496. }
  6497. wc_Sha224Free(&sha224);
  6498. printf(resultFmt, flag == 0 ? passed : failed);
  6499. #endif
  6500. return flag;
  6501. } /* END test_wc_Sha224SetFlags */
  6502. /*
  6503. * Unit test function for wc_Sha224GetFlags()
  6504. */
  6505. static int test_wc_Sha224GetFlags (void)
  6506. {
  6507. int flag = 0;
  6508. #if defined(WOLFSSL_SHA224) && \
  6509. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  6510. wc_Sha224 sha224;
  6511. word32 flags = 0;
  6512. printf(testingFmt, "wc_Sha224GetFlags()");
  6513. /* Initialize */
  6514. flag = wc_InitSha224(&sha224);
  6515. if (flag == 0) {
  6516. flag = wc_Sha224GetFlags(&sha224, &flags);
  6517. }
  6518. if (flag == 0) {
  6519. if (flags & WC_HASH_FLAG_ISCOPY) {
  6520. flag = 0;
  6521. }
  6522. }
  6523. wc_Sha224Free(&sha224);
  6524. printf(resultFmt, flag == 0 ? passed : failed);
  6525. #endif
  6526. return flag;
  6527. } /* END test_wc_Sha224GetFlags */
  6528. /*
  6529. * Unit test function for wc_Sha224Free()
  6530. */
  6531. static int test_wc_Sha224Free (void)
  6532. {
  6533. int flag = 0;
  6534. #if defined(WOLFSSL_SHA224) && \
  6535. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  6536. printf(testingFmt, "wc_Sha224Free()");
  6537. wc_Sha224Free(NULL);
  6538. printf(resultFmt, flag == 0 ? passed : failed);
  6539. #endif
  6540. return flag;
  6541. } /* END test_wc_Sha224Free */
  6542. /*
  6543. * Unit test function for wc_Sha224GetHash()
  6544. */
  6545. static int test_wc_Sha224GetHash (void)
  6546. {
  6547. int flag = 0;
  6548. #if defined(WOLFSSL_SHA224) && \
  6549. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  6550. wc_Sha224 sha224;
  6551. byte hash1[WC_SHA224_DIGEST_SIZE];
  6552. printf(testingFmt, "wc_Sha224GetHash()");
  6553. /* Initialize */
  6554. flag = wc_InitSha224(&sha224);
  6555. if (flag == 0) {
  6556. flag = wc_Sha224GetHash(&sha224, hash1);
  6557. }
  6558. /*test bad arguements*/
  6559. if (flag == 0) {
  6560. flag = wc_Sha224GetHash(NULL, NULL);
  6561. if (flag == BAD_FUNC_ARG) {
  6562. flag = 0;
  6563. }
  6564. }
  6565. if (flag == 0) {
  6566. flag = wc_Sha224GetHash(NULL, hash1);
  6567. if (flag == BAD_FUNC_ARG) {
  6568. flag = 0;
  6569. }
  6570. }
  6571. if (flag == 0) {
  6572. flag = wc_Sha224GetHash(&sha224, NULL);
  6573. if (flag == BAD_FUNC_ARG) {
  6574. flag = 0;
  6575. }
  6576. }
  6577. wc_Sha224Free(&sha224);
  6578. printf(resultFmt, flag == 0 ? passed : failed);
  6579. #endif
  6580. return flag;
  6581. } /* END test_wc_Sha224GetHash */
  6582. /*
  6583. * Unit test function for wc_Sha224Copy()
  6584. */
  6585. static int test_wc_Sha224Copy (void)
  6586. {
  6587. int flag = 0;
  6588. #if defined(WOLFSSL_SHA224) && \
  6589. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  6590. wc_Sha224 sha224;
  6591. wc_Sha224 temp;
  6592. printf(testingFmt, "wc_Sha224Copy()");
  6593. /* Initialize */
  6594. flag = wc_InitSha224(&sha224);
  6595. if (flag == 0) {
  6596. flag = wc_InitSha224(&temp);
  6597. }
  6598. if (flag == 0) {
  6599. flag = wc_Sha224Copy(&sha224, &temp);
  6600. }
  6601. /*test bad arguements*/
  6602. if (flag == 0) {
  6603. flag = wc_Sha224Copy(NULL, NULL);
  6604. if (flag == BAD_FUNC_ARG) {
  6605. flag = 0;
  6606. }
  6607. }
  6608. if (flag == 0) {
  6609. flag = wc_Sha224Copy(NULL, &temp);
  6610. if (flag == BAD_FUNC_ARG) {
  6611. flag = 0;
  6612. }
  6613. }
  6614. if (flag == 0) {
  6615. flag = wc_Sha224Copy(&sha224, NULL);
  6616. if (flag == BAD_FUNC_ARG) {
  6617. flag = 0;
  6618. }
  6619. }
  6620. wc_Sha224Free(&sha224);
  6621. wc_Sha224Free(&temp);
  6622. printf(resultFmt, flag == 0 ? passed : failed);
  6623. #endif
  6624. return flag;
  6625. } /* END test_wc_Sha224Copy */
  6626. /*
  6627. * Testing wc_InitRipeMd()
  6628. */
  6629. static int test_wc_InitRipeMd (void)
  6630. {
  6631. int flag = 0;
  6632. #ifdef WOLFSSL_RIPEMD
  6633. RipeMd ripemd;
  6634. int ret;
  6635. printf(testingFmt, "wc_InitRipeMd()");
  6636. /* Test good arg. */
  6637. ret = wc_InitRipeMd(&ripemd);
  6638. if (ret != 0) {
  6639. flag = WOLFSSL_FATAL_ERROR;
  6640. }
  6641. /* Test bad arg. */
  6642. if (!flag) {
  6643. ret = wc_InitRipeMd(NULL);
  6644. if (ret != BAD_FUNC_ARG) {
  6645. flag = WOLFSSL_FATAL_ERROR;
  6646. }
  6647. }
  6648. printf(resultFmt, flag == 0 ? passed : failed);
  6649. #endif
  6650. return flag;
  6651. } /* END test_wc_InitRipeMd */
  6652. /*
  6653. * Testing wc_RipeMdUpdate()
  6654. */
  6655. static int test_wc_RipeMdUpdate (void)
  6656. {
  6657. int flag = 0;
  6658. #ifdef WOLFSSL_RIPEMD
  6659. RipeMd ripemd;
  6660. byte hash[RIPEMD_DIGEST_SIZE];
  6661. testVector a, b, c;
  6662. int ret;
  6663. ret = wc_InitRipeMd(&ripemd);
  6664. if (ret != 0) {
  6665. flag = ret;
  6666. }
  6667. printf(testingFmt, "wc_RipeMdUpdate()");
  6668. /* Input */
  6669. if (!flag) {
  6670. a.input = "a";
  6671. a.inLen = XSTRLEN(a.input);
  6672. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  6673. if (ret != 0) {
  6674. flag = ret;
  6675. }
  6676. }
  6677. if (!flag) {
  6678. ret = wc_RipeMdFinal(&ripemd, hash);
  6679. if (ret != 0) {
  6680. flag = ret;
  6681. }
  6682. }
  6683. /* Update input. */
  6684. if (!flag) {
  6685. a.input = "abc";
  6686. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  6687. "\xb0\x87\xf1\x5a\x0b\xfc";
  6688. a.inLen = XSTRLEN(a.input);
  6689. a.outLen = XSTRLEN(a.output);
  6690. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  6691. if (ret != 0) {
  6692. flag = ret;
  6693. }
  6694. }
  6695. if (!flag) {
  6696. ret = wc_RipeMdFinal(&ripemd, hash);
  6697. if (ret != 0) {
  6698. flag = ret;
  6699. }
  6700. }
  6701. if (!flag) {
  6702. if (XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE) != 0) {
  6703. flag = WOLFSSL_FATAL_ERROR;
  6704. }
  6705. }
  6706. /* Pass in bad values. */
  6707. if (!flag) {
  6708. b.input = NULL;
  6709. b.inLen = 0;
  6710. ret = wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen);
  6711. if (ret != 0) {
  6712. flag = ret;
  6713. }
  6714. }
  6715. if (!flag) {
  6716. c.input = NULL;
  6717. c.inLen = RIPEMD_DIGEST_SIZE;
  6718. ret = wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen);
  6719. if (ret != BAD_FUNC_ARG) {
  6720. flag = WOLFSSL_FATAL_ERROR;
  6721. }
  6722. }
  6723. if (!flag) {
  6724. ret = wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  6725. if (ret != BAD_FUNC_ARG) {
  6726. flag = WOLFSSL_FATAL_ERROR;
  6727. }
  6728. }
  6729. printf(resultFmt, flag == 0 ? passed : failed);
  6730. #endif
  6731. return flag;
  6732. } /* END test_wc_RipeMdUdpate */
  6733. /*
  6734. * Unit test function for wc_RipeMdFinal()
  6735. */
  6736. static int test_wc_RipeMdFinal (void)
  6737. {
  6738. int flag = 0;
  6739. #ifdef WOLFSSL_RIPEMD
  6740. RipeMd ripemd;
  6741. byte* hash_test[3];
  6742. byte hash1[RIPEMD_DIGEST_SIZE];
  6743. byte hash2[2*RIPEMD_DIGEST_SIZE];
  6744. byte hash3[5*RIPEMD_DIGEST_SIZE];
  6745. int times, i, ret;
  6746. /* Initialize */
  6747. ret = wc_InitRipeMd(&ripemd);
  6748. if (ret != 0) {
  6749. flag = ret;
  6750. }
  6751. if (!flag) {
  6752. hash_test[0] = hash1;
  6753. hash_test[1] = hash2;
  6754. hash_test[2] = hash3;
  6755. }
  6756. times = sizeof(hash_test) / sizeof(byte*);
  6757. /* Good test args. */
  6758. printf(testingFmt, "wc_RipeMdFinal()");
  6759. /* Testing oversized buffers. */
  6760. for (i = 0; i < times; i++) {
  6761. if (!flag) {
  6762. ret = wc_RipeMdFinal(&ripemd, hash_test[i]);
  6763. if (ret != 0) {
  6764. flag = WOLFSSL_FATAL_ERROR;
  6765. }
  6766. }
  6767. }
  6768. /* Test bad args. */
  6769. if (!flag) {
  6770. ret = wc_RipeMdFinal(NULL, NULL);
  6771. if (ret != BAD_FUNC_ARG) {
  6772. flag = WOLFSSL_FATAL_ERROR;
  6773. }
  6774. }
  6775. if (!flag) {
  6776. ret = wc_RipeMdFinal(NULL, hash1);
  6777. if (ret != BAD_FUNC_ARG) {
  6778. flag = WOLFSSL_FATAL_ERROR;
  6779. }
  6780. }
  6781. if (!flag) {
  6782. ret = wc_RipeMdFinal(&ripemd, NULL);
  6783. if (ret != BAD_FUNC_ARG) {
  6784. flag = WOLFSSL_FATAL_ERROR;
  6785. }
  6786. }
  6787. printf(resultFmt, flag == 0 ? passed : failed);
  6788. #endif
  6789. return flag;
  6790. } /* END test_wc_RipeMdFinal */
  6791. /*
  6792. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  6793. * wc_InitSha3_512
  6794. */
  6795. static int test_wc_InitSha3 (void)
  6796. {
  6797. int ret = 0;
  6798. #if defined(WOLFSSL_SHA3)
  6799. wc_Sha3 sha3;
  6800. (void)sha3;
  6801. #if !defined(WOLFSSL_NOSHA3_224)
  6802. printf(testingFmt, "wc_InitSha3_224()");
  6803. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  6804. /* Test bad args. */
  6805. if (ret == 0) {
  6806. ret = wc_InitSha3_224(NULL, HEAP_HINT, devId);
  6807. if (ret == BAD_FUNC_ARG) {
  6808. ret = 0;
  6809. } else if (ret == 0) {
  6810. ret = WOLFSSL_FATAL_ERROR;
  6811. }
  6812. }
  6813. wc_Sha3_224_Free(&sha3);
  6814. printf(resultFmt, ret == 0 ? passed : failed);
  6815. #endif /* NOSHA3_224 */
  6816. #if !defined(WOLFSSL_NOSHA3_256)
  6817. if (ret == 0) {
  6818. printf(testingFmt, "wc_InitSha3_256()");
  6819. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  6820. /* Test bad args. */
  6821. if (ret == 0) {
  6822. ret = wc_InitSha3_256(NULL, HEAP_HINT, devId);
  6823. if (ret == BAD_FUNC_ARG) {
  6824. ret = 0;
  6825. } else if (ret == 0) {
  6826. ret = WOLFSSL_FATAL_ERROR;
  6827. }
  6828. }
  6829. wc_Sha3_256_Free(&sha3);
  6830. printf(resultFmt, ret == 0 ? passed : failed);
  6831. } /* END sha3_256 */
  6832. #endif /* NOSHA3_256 */
  6833. #if !defined(WOLFSSL_NOSHA3_384)
  6834. if (ret == 0) {
  6835. printf(testingFmt, "wc_InitSha3_384()");
  6836. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  6837. /* Test bad args. */
  6838. if (ret == 0) {
  6839. ret = wc_InitSha3_384(NULL, HEAP_HINT, devId);
  6840. if (ret == BAD_FUNC_ARG) {
  6841. ret = 0;
  6842. } else if (ret == 0) {
  6843. ret = WOLFSSL_FATAL_ERROR;
  6844. }
  6845. }
  6846. wc_Sha3_384_Free(&sha3);
  6847. printf(resultFmt, ret == 0 ? passed : failed);
  6848. } /* END sha3_384 */
  6849. #endif /* NOSHA3_384 */
  6850. #if !defined(WOLFSSL_NOSHA3_512)
  6851. if (ret == 0) {
  6852. printf(testingFmt, "wc_InitSha3_512()");
  6853. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  6854. /* Test bad args. */
  6855. if (ret == 0) {
  6856. ret = wc_InitSha3_512(NULL, HEAP_HINT, devId);
  6857. if (ret == BAD_FUNC_ARG) {
  6858. ret = 0;
  6859. } else if (ret == 0) {
  6860. ret = WOLFSSL_FATAL_ERROR;
  6861. }
  6862. }
  6863. wc_Sha3_512_Free(&sha3);
  6864. printf(resultFmt, ret == 0 ? passed : failed);
  6865. } /* END sha3_512 */
  6866. #endif /* NOSHA3_512 */
  6867. #endif
  6868. return ret;
  6869. } /* END test_wc_InitSha3 */
  6870. /*
  6871. * Testing wc_Sha3_Update()
  6872. */
  6873. static int testing_wc_Sha3_Update (void)
  6874. {
  6875. int ret = 0;
  6876. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  6877. !defined(WOLFSSL_AFALG_XILINX)
  6878. wc_Sha3 sha3;
  6879. byte msg[] = "Everybody's working for the weekend.";
  6880. byte msg2[] = "Everybody gets Friday off.";
  6881. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  6882. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  6883. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  6884. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  6885. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  6886. word32 msglen = sizeof(msg) - 1;
  6887. word32 msg2len = sizeof(msg2);
  6888. word32 msgCmplen = sizeof(msgCmp);
  6889. #if !defined(WOLFSSL_NOSHA3_224)
  6890. printf(testingFmt, "wc_Sha3_224_Update()");
  6891. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  6892. if (ret != 0) {
  6893. return ret;
  6894. }
  6895. ret = wc_Sha3_224_Update(&sha3, msg, msglen);
  6896. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  6897. ret = WOLFSSL_FATAL_ERROR;
  6898. }
  6899. if (ret == 0) {
  6900. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  6901. if (ret == 0 && XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  6902. ret = WOLFSSL_FATAL_ERROR;
  6903. }
  6904. }
  6905. /* Pass bad args. */
  6906. if (ret == 0) {
  6907. ret = wc_Sha3_224_Update(NULL, msg2, msg2len);
  6908. if (ret == BAD_FUNC_ARG) {
  6909. ret = wc_Sha3_224_Update(&sha3, NULL, 5);
  6910. }
  6911. if (ret == BAD_FUNC_ARG) {
  6912. wc_Sha3_224_Free(&sha3);
  6913. if (wc_InitSha3_224(&sha3, HEAP_HINT, devId)) {
  6914. return ret;
  6915. }
  6916. ret = wc_Sha3_224_Update(&sha3, NULL, 0);
  6917. if (ret == 0) {
  6918. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  6919. }
  6920. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  6921. ret = WOLFSSL_FATAL_ERROR;
  6922. }
  6923. }
  6924. }
  6925. wc_Sha3_224_Free(&sha3);
  6926. printf(resultFmt, ret == 0 ? passed : failed);
  6927. #endif /* SHA3_224 */
  6928. #if !defined(WOLFSSL_NOSHA3_256)
  6929. if (ret == 0) {
  6930. printf(testingFmt, "wc_Sha3_256_Update()");
  6931. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  6932. if (ret != 0) {
  6933. return ret;
  6934. }
  6935. ret = wc_Sha3_256_Update(&sha3, msg, msglen);
  6936. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  6937. ret = WOLFSSL_FATAL_ERROR;
  6938. }
  6939. if (ret == 0) {
  6940. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  6941. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  6942. ret = WOLFSSL_FATAL_ERROR;
  6943. }
  6944. }
  6945. /* Pass bad args. */
  6946. if (ret == 0) {
  6947. ret = wc_Sha3_256_Update(NULL, msg2, msg2len);
  6948. if (ret == BAD_FUNC_ARG) {
  6949. ret = wc_Sha3_256_Update(&sha3, NULL, 5);
  6950. }
  6951. if (ret == BAD_FUNC_ARG) {
  6952. wc_Sha3_256_Free(&sha3);
  6953. if (wc_InitSha3_256(&sha3, HEAP_HINT, devId)) {
  6954. return ret;
  6955. }
  6956. ret = wc_Sha3_256_Update(&sha3, NULL, 0);
  6957. if (ret == 0) {
  6958. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  6959. }
  6960. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  6961. ret = WOLFSSL_FATAL_ERROR;
  6962. }
  6963. }
  6964. }
  6965. wc_Sha3_256_Free(&sha3);
  6966. printf(resultFmt, ret == 0 ? passed : failed);
  6967. }
  6968. #endif /* SHA3_256 */
  6969. #if !defined(WOLFSSL_NOSHA3_384)
  6970. if (ret == 0) {
  6971. printf(testingFmt, "wc_Sha3_384_Update()");
  6972. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  6973. if (ret != 0) {
  6974. return ret;
  6975. }
  6976. ret = wc_Sha3_384_Update(&sha3, msg, msglen);
  6977. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  6978. ret = WOLFSSL_FATAL_ERROR;
  6979. }
  6980. if (ret == 0) {
  6981. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  6982. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  6983. ret = WOLFSSL_FATAL_ERROR;
  6984. }
  6985. }
  6986. /* Pass bad args. */
  6987. if (ret == 0) {
  6988. ret = wc_Sha3_384_Update(NULL, msg2, msg2len);
  6989. if (ret == BAD_FUNC_ARG) {
  6990. ret = wc_Sha3_384_Update(&sha3, NULL, 5);
  6991. }
  6992. if (ret == BAD_FUNC_ARG) {
  6993. wc_Sha3_384_Free(&sha3);
  6994. if (wc_InitSha3_384(&sha3, HEAP_HINT, devId)) {
  6995. return ret;
  6996. }
  6997. ret = wc_Sha3_384_Update(&sha3, NULL, 0);
  6998. if (ret == 0) {
  6999. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  7000. }
  7001. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  7002. ret = WOLFSSL_FATAL_ERROR;
  7003. }
  7004. }
  7005. }
  7006. wc_Sha3_384_Free(&sha3);
  7007. printf(resultFmt, ret == 0 ? passed : failed);
  7008. }
  7009. #endif /* SHA3_384 */
  7010. #if !defined(WOLFSSL_NOSHA3_512)
  7011. if (ret == 0) {
  7012. printf(testingFmt, "wc_Sha3_512_Update()");
  7013. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  7014. if (ret != 0) {
  7015. return ret;
  7016. }
  7017. ret = wc_Sha3_512_Update(&sha3, msg, msglen);
  7018. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  7019. ret = WOLFSSL_FATAL_ERROR;
  7020. }
  7021. if (ret == 0) {
  7022. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  7023. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  7024. ret = WOLFSSL_FATAL_ERROR;
  7025. }
  7026. }
  7027. /* Pass bad args. */
  7028. if (ret == 0) {
  7029. ret = wc_Sha3_512_Update(NULL, msg2, msg2len);
  7030. if (ret == BAD_FUNC_ARG) {
  7031. ret = wc_Sha3_512_Update(&sha3, NULL, 5);
  7032. }
  7033. if (ret == BAD_FUNC_ARG) {
  7034. wc_Sha3_512_Free(&sha3);
  7035. if (wc_InitSha3_512(&sha3, HEAP_HINT, devId)) {
  7036. return ret;
  7037. }
  7038. ret = wc_Sha3_512_Update(&sha3, NULL, 0);
  7039. if (ret == 0) {
  7040. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  7041. }
  7042. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  7043. ret = WOLFSSL_FATAL_ERROR;
  7044. }
  7045. }
  7046. }
  7047. wc_Sha3_512_Free(&sha3);
  7048. printf(resultFmt, ret == 0 ? passed : failed);
  7049. }
  7050. #endif /* SHA3_512 */
  7051. #endif /* WOLFSSL_SHA3 */
  7052. return ret;
  7053. } /* END testing_wc_Sha3_Update */
  7054. /*
  7055. * Testing wc_Sha3_224_Final()
  7056. */
  7057. static int test_wc_Sha3_224_Final (void)
  7058. {
  7059. int ret = 0;
  7060. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  7061. wc_Sha3 sha3;
  7062. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  7063. "nopnopq";
  7064. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  7065. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  7066. "\x64\xea\xd0\xfc\xce\x33";
  7067. byte hash[WC_SHA3_224_DIGEST_SIZE];
  7068. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  7069. /* Init stack variables. */
  7070. XMEMSET(hash, 0, sizeof(hash));
  7071. printf(testingFmt, "wc_Sha3_224_Final()");
  7072. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  7073. if (ret != 0) {
  7074. return ret;
  7075. }
  7076. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  7077. if (ret == 0) {
  7078. ret = wc_Sha3_224_Final(&sha3, hash);
  7079. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE) != 0) {
  7080. ret = WOLFSSL_FATAL_ERROR;
  7081. }
  7082. }
  7083. /* Test bad args. */
  7084. if (ret == 0) {
  7085. ret = wc_Sha3_224_Final(NULL, hash);
  7086. if (ret == 0) {
  7087. ret = wc_Sha3_224_Final(&sha3, NULL);
  7088. }
  7089. if (ret == BAD_FUNC_ARG) {
  7090. ret = 0;
  7091. } else if (ret == 0) {
  7092. ret = WOLFSSL_FATAL_ERROR;
  7093. }
  7094. }
  7095. wc_Sha3_224_Free(&sha3);
  7096. printf(resultFmt, ret == 0 ? passed : failed);
  7097. if (ret == 0) {
  7098. printf(testingFmt, "wc_Sha3_224_GetHash()");
  7099. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  7100. if (ret != 0) {
  7101. return ret;
  7102. }
  7103. /* Init stack variables. */
  7104. XMEMSET(hash, 0, sizeof(hash));
  7105. XMEMSET(hashRet, 0, sizeof(hashRet));
  7106. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  7107. if (ret == 0) {
  7108. ret = wc_Sha3_224_GetHash(&sha3, hashRet);
  7109. }
  7110. if (ret == 0) {
  7111. ret = wc_Sha3_224_Final(&sha3, hash);
  7112. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE) != 0) {
  7113. ret = WOLFSSL_FATAL_ERROR;
  7114. }
  7115. }
  7116. if (ret == 0) {
  7117. /* Test bad args. */
  7118. ret = wc_Sha3_224_GetHash(NULL, hashRet);
  7119. if (ret == BAD_FUNC_ARG) {
  7120. ret = wc_Sha3_224_GetHash(&sha3, NULL);
  7121. }
  7122. if (ret == BAD_FUNC_ARG) {
  7123. ret = 0;
  7124. } else if (ret == 0) {
  7125. ret = WOLFSSL_FATAL_ERROR;
  7126. }
  7127. }
  7128. printf(resultFmt, ret == 0 ? passed : failed);
  7129. }
  7130. wc_Sha3_224_Free(&sha3);
  7131. #endif
  7132. return ret;
  7133. } /* END test_wc_Sha3_224_Final */
  7134. /*
  7135. * Testing wc_Sha3_256_Final()
  7136. */
  7137. static int test_wc_Sha3_256_Final (void)
  7138. {
  7139. int ret = 0;
  7140. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  7141. wc_Sha3 sha3;
  7142. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  7143. "nopnopq";
  7144. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  7145. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  7146. "\xdd\x97\x49\x6d\x33\x76";
  7147. byte hash[WC_SHA3_256_DIGEST_SIZE];
  7148. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  7149. /* Init stack variables. */
  7150. XMEMSET(hash, 0, sizeof(hash));
  7151. printf(testingFmt, "wc_Sha3_256_Final()");
  7152. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  7153. if (ret != 0) {
  7154. return ret;
  7155. }
  7156. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  7157. if (ret == 0) {
  7158. ret = wc_Sha3_256_Final(&sha3, hash);
  7159. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE) != 0) {
  7160. ret = WOLFSSL_FATAL_ERROR;
  7161. }
  7162. }
  7163. /* Test bad args. */
  7164. if (ret == 0) {
  7165. ret = wc_Sha3_256_Final(NULL, hash);
  7166. if (ret == 0) {
  7167. ret = wc_Sha3_256_Final(&sha3, NULL);
  7168. }
  7169. if (ret == BAD_FUNC_ARG) {
  7170. ret = 0;
  7171. } else if (ret == 0) {
  7172. ret = WOLFSSL_FATAL_ERROR;
  7173. }
  7174. }
  7175. wc_Sha3_256_Free(&sha3);
  7176. printf(resultFmt, ret == 0 ? passed : failed);
  7177. if (ret == 0) {
  7178. printf(testingFmt, "wc_Sha3_256_GetHash()");
  7179. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  7180. if (ret != 0) {
  7181. return ret;
  7182. }
  7183. /* Init stack variables. */
  7184. XMEMSET(hash, 0, sizeof(hash));
  7185. XMEMSET(hashRet, 0, sizeof(hashRet));
  7186. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  7187. if (ret == 0) {
  7188. ret = wc_Sha3_256_GetHash(&sha3, hashRet);
  7189. }
  7190. if (ret == 0) {
  7191. ret = wc_Sha3_256_Final(&sha3, hash);
  7192. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE) != 0) {
  7193. ret = WOLFSSL_FATAL_ERROR;
  7194. }
  7195. }
  7196. if (ret == 0) {
  7197. /* Test bad args. */
  7198. ret = wc_Sha3_256_GetHash(NULL, hashRet);
  7199. if (ret == BAD_FUNC_ARG) {
  7200. ret = wc_Sha3_256_GetHash(&sha3, NULL);
  7201. }
  7202. if (ret == BAD_FUNC_ARG) {
  7203. ret = 0;
  7204. } else if (ret == 0) {
  7205. ret = WOLFSSL_FATAL_ERROR;
  7206. }
  7207. }
  7208. printf(resultFmt, ret == 0 ? passed : failed);
  7209. }
  7210. wc_Sha3_256_Free(&sha3);
  7211. #endif
  7212. return ret;
  7213. } /* END test_wc_Sha3_256_Final */
  7214. /*
  7215. * Testing wc_Sha3_384_Final()
  7216. */
  7217. static int test_wc_Sha3_384_Final (void)
  7218. {
  7219. int ret = 0;
  7220. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  7221. wc_Sha3 sha3;
  7222. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  7223. "nopnopq";
  7224. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  7225. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  7226. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  7227. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  7228. byte hash[WC_SHA3_384_DIGEST_SIZE];
  7229. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  7230. /* Init stack variables. */
  7231. XMEMSET(hash, 0, sizeof(hash));
  7232. printf(testingFmt, "wc_Sha3_384_Final()");
  7233. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  7234. if (ret != 0) {
  7235. return ret;
  7236. }
  7237. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  7238. if (ret == 0) {
  7239. ret = wc_Sha3_384_Final(&sha3, hash);
  7240. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE) != 0) {
  7241. ret = WOLFSSL_FATAL_ERROR;
  7242. }
  7243. }
  7244. /* Test bad args. */
  7245. if (ret == 0) {
  7246. ret = wc_Sha3_384_Final(NULL, hash);
  7247. if (ret == 0) {
  7248. ret = wc_Sha3_384_Final(&sha3, NULL);
  7249. }
  7250. if (ret == BAD_FUNC_ARG) {
  7251. ret = 0;
  7252. } else if (ret == 0) {
  7253. ret = WOLFSSL_FATAL_ERROR;
  7254. }
  7255. }
  7256. wc_Sha3_384_Free(&sha3);
  7257. printf(resultFmt, ret == 0 ? passed : failed);
  7258. if (ret == 0) {
  7259. printf(testingFmt, "wc_Sha3_384_GetHash()");
  7260. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  7261. if (ret != 0) {
  7262. return ret;
  7263. }
  7264. /* Init stack variables. */
  7265. XMEMSET(hash, 0, sizeof(hash));
  7266. XMEMSET(hashRet, 0, sizeof(hashRet));
  7267. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  7268. if (ret == 0) {
  7269. ret = wc_Sha3_384_GetHash(&sha3, hashRet);
  7270. }
  7271. if (ret == 0) {
  7272. ret = wc_Sha3_384_Final(&sha3, hash);
  7273. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE) != 0) {
  7274. ret = WOLFSSL_FATAL_ERROR;
  7275. }
  7276. }
  7277. if (ret == 0) {
  7278. /* Test bad args. */
  7279. ret = wc_Sha3_384_GetHash(NULL, hashRet);
  7280. if (ret == BAD_FUNC_ARG) {
  7281. ret = wc_Sha3_384_GetHash(&sha3, NULL);
  7282. }
  7283. if (ret == BAD_FUNC_ARG) {
  7284. ret = 0;
  7285. } else if (ret == 0) {
  7286. ret = WOLFSSL_FATAL_ERROR;
  7287. }
  7288. }
  7289. printf(resultFmt, ret == 0 ? passed : failed);
  7290. }
  7291. wc_Sha3_384_Free(&sha3);
  7292. #endif
  7293. return ret;
  7294. } /* END test_wc_Sha3_384_Final */
  7295. /*
  7296. * Testing wc_Sha3_512_Final()
  7297. */
  7298. static int test_wc_Sha3_512_Final (void)
  7299. {
  7300. int ret = 0;
  7301. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  7302. !defined(WOLFSSL_NOSHA3_384)
  7303. wc_Sha3 sha3;
  7304. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  7305. "nopnopq";
  7306. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  7307. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  7308. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  7309. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  7310. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  7311. byte hash[WC_SHA3_512_DIGEST_SIZE];
  7312. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  7313. /* Init stack variables. */
  7314. XMEMSET(hash, 0, sizeof(hash));
  7315. printf(testingFmt, "wc_Sha3_512_Final()");
  7316. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  7317. if (ret != 0) {
  7318. return ret;
  7319. }
  7320. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  7321. if (ret == 0) {
  7322. ret = wc_Sha3_512_Final(&sha3, hash);
  7323. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE) != 0) {
  7324. ret = WOLFSSL_FATAL_ERROR;
  7325. }
  7326. }
  7327. /* Test bad args. */
  7328. if (ret == 0) {
  7329. ret = wc_Sha3_512_Final(NULL, hash);
  7330. if (ret == 0) {
  7331. ret = wc_Sha3_384_Final(&sha3, NULL);
  7332. }
  7333. if (ret == BAD_FUNC_ARG) {
  7334. ret = 0;
  7335. } else if (ret == 0) {
  7336. ret = WOLFSSL_FATAL_ERROR;
  7337. }
  7338. }
  7339. wc_Sha3_512_Free(&sha3);
  7340. printf(resultFmt, ret == 0 ? passed : failed);
  7341. if (ret == 0) {
  7342. printf(testingFmt, "wc_Sha3_512_GetHash()");
  7343. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  7344. if (ret != 0) {
  7345. return ret;
  7346. }
  7347. /* Init stack variables. */
  7348. XMEMSET(hash, 0, sizeof(hash));
  7349. XMEMSET(hashRet, 0, sizeof(hashRet));
  7350. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  7351. if (ret == 0) {
  7352. ret = wc_Sha3_512_GetHash(&sha3, hashRet);
  7353. }
  7354. if (ret == 0) {
  7355. ret = wc_Sha3_512_Final(&sha3, hash);
  7356. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE) != 0) {
  7357. ret = WOLFSSL_FATAL_ERROR;
  7358. }
  7359. }
  7360. if (ret == 0) {
  7361. /* Test bad args. */
  7362. ret = wc_Sha3_512_GetHash(NULL, hashRet);
  7363. if (ret == BAD_FUNC_ARG) {
  7364. ret = wc_Sha3_512_GetHash(&sha3, NULL);
  7365. }
  7366. if (ret == BAD_FUNC_ARG) {
  7367. ret = 0;
  7368. } else if (ret == 0) {
  7369. ret = WOLFSSL_FATAL_ERROR;
  7370. }
  7371. }
  7372. printf(resultFmt, ret == 0 ? passed : failed);
  7373. }
  7374. wc_Sha3_512_Free(&sha3);
  7375. #endif
  7376. return ret;
  7377. } /* END test_wc_Sha3_512_Final */
  7378. /*
  7379. * Testing wc_Sha3_224_Copy()
  7380. */
  7381. static int test_wc_Sha3_224_Copy (void)
  7382. {
  7383. int ret = 0;
  7384. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  7385. wc_Sha3 sha3, sha3Cpy;
  7386. const char* msg = "Everyone gets Friday off.";
  7387. word32 msglen = (word32)XSTRLEN(msg);
  7388. byte hash[WC_SHA3_224_DIGEST_SIZE];
  7389. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  7390. XMEMSET(hash, 0, sizeof(hash));
  7391. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  7392. printf(testingFmt, "wc_Sha3_224_Copy()");
  7393. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  7394. if (ret != 0) {
  7395. return ret;
  7396. }
  7397. ret = wc_InitSha3_224(&sha3Cpy, HEAP_HINT, devId);
  7398. if (ret != 0) {
  7399. wc_Sha3_224_Free(&sha3);
  7400. return ret;
  7401. }
  7402. ret = wc_Sha3_224_Update(&sha3, (byte*)msg, msglen);
  7403. if (ret == 0) {
  7404. ret = wc_Sha3_224_Copy(&sha3Cpy, &sha3);
  7405. if (ret == 0) {
  7406. ret = wc_Sha3_224_Final(&sha3, hash);
  7407. if (ret == 0) {
  7408. ret = wc_Sha3_224_Final(&sha3Cpy, hashCpy);
  7409. }
  7410. }
  7411. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  7412. ret = WOLFSSL_FATAL_ERROR;
  7413. }
  7414. }
  7415. /* Test bad args. */
  7416. if (ret == 0) {
  7417. ret = wc_Sha3_224_Copy(NULL, &sha3);
  7418. if (ret == BAD_FUNC_ARG) {
  7419. ret = wc_Sha3_224_Copy(&sha3Cpy, NULL);
  7420. }
  7421. if (ret == BAD_FUNC_ARG) {
  7422. ret = 0;
  7423. } else if (ret == 0) {
  7424. ret = WOLFSSL_FATAL_ERROR;
  7425. }
  7426. }
  7427. printf(resultFmt, ret == 0 ? passed : failed);
  7428. #endif
  7429. return ret;
  7430. } /* END test_wc_Sha3_224_Copy */
  7431. /*
  7432. * Testing wc_Sha3_256_Copy()
  7433. */
  7434. static int test_wc_Sha3_256_Copy (void)
  7435. {
  7436. int ret = 0;
  7437. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  7438. wc_Sha3 sha3, sha3Cpy;
  7439. const char* msg = "Everyone gets Friday off.";
  7440. word32 msglen = (word32)XSTRLEN(msg);
  7441. byte hash[WC_SHA3_256_DIGEST_SIZE];
  7442. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  7443. XMEMSET(hash, 0, sizeof(hash));
  7444. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  7445. printf(testingFmt, "wc_Sha3_256_Copy()");
  7446. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  7447. if (ret != 0) {
  7448. return ret;
  7449. }
  7450. ret = wc_InitSha3_256(&sha3Cpy, HEAP_HINT, devId);
  7451. if (ret != 0) {
  7452. wc_Sha3_256_Free(&sha3);
  7453. return ret;
  7454. }
  7455. ret = wc_Sha3_256_Update(&sha3, (byte*)msg, msglen);
  7456. if (ret == 0) {
  7457. ret = wc_Sha3_256_Copy(&sha3Cpy, &sha3);
  7458. if (ret == 0) {
  7459. ret = wc_Sha3_256_Final(&sha3, hash);
  7460. if (ret == 0) {
  7461. ret = wc_Sha3_256_Final(&sha3Cpy, hashCpy);
  7462. }
  7463. }
  7464. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  7465. ret = WOLFSSL_FATAL_ERROR;
  7466. }
  7467. }
  7468. /* Test bad args. */
  7469. if (ret == 0) {
  7470. ret = wc_Sha3_256_Copy(NULL, &sha3);
  7471. if (ret == BAD_FUNC_ARG) {
  7472. ret = wc_Sha3_256_Copy(&sha3Cpy, NULL);
  7473. }
  7474. if (ret == BAD_FUNC_ARG) {
  7475. ret = 0;
  7476. } else if (ret == 0) {
  7477. ret = WOLFSSL_FATAL_ERROR;
  7478. }
  7479. }
  7480. printf(resultFmt, ret == 0 ? passed : failed);
  7481. #endif
  7482. return ret;
  7483. } /* END test_wc_Sha3_256_Copy */
  7484. /*
  7485. * Testing wc_Sha3_384_Copy()
  7486. */
  7487. static int test_wc_Sha3_384_Copy (void)
  7488. {
  7489. int ret = 0;
  7490. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  7491. wc_Sha3 sha3, sha3Cpy;
  7492. const char* msg = "Everyone gets Friday off.";
  7493. word32 msglen = (word32)XSTRLEN(msg);
  7494. byte hash[WC_SHA3_384_DIGEST_SIZE];
  7495. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  7496. XMEMSET(hash, 0, sizeof(hash));
  7497. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  7498. printf(testingFmt, "wc_Sha3_384_Copy()");
  7499. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  7500. if (ret != 0) {
  7501. return ret;
  7502. }
  7503. ret = wc_InitSha3_384(&sha3Cpy, HEAP_HINT, devId);
  7504. if (ret != 0) {
  7505. wc_Sha3_384_Free(&sha3);
  7506. return ret;
  7507. }
  7508. ret = wc_Sha3_384_Update(&sha3, (byte*)msg, msglen);
  7509. if (ret == 0) {
  7510. ret = wc_Sha3_384_Copy(&sha3Cpy, &sha3);
  7511. if (ret == 0) {
  7512. ret = wc_Sha3_384_Final(&sha3, hash);
  7513. if (ret == 0) {
  7514. ret = wc_Sha3_384_Final(&sha3Cpy, hashCpy);
  7515. }
  7516. }
  7517. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  7518. ret = WOLFSSL_FATAL_ERROR;
  7519. }
  7520. }
  7521. /* Test bad args. */
  7522. if (ret == 0) {
  7523. ret = wc_Sha3_384_Copy(NULL, &sha3);
  7524. if (ret == BAD_FUNC_ARG) {
  7525. ret = wc_Sha3_384_Copy(&sha3Cpy, NULL);
  7526. }
  7527. if (ret == BAD_FUNC_ARG) {
  7528. ret = 0;
  7529. } else if (ret == 0) {
  7530. ret = WOLFSSL_FATAL_ERROR;
  7531. }
  7532. }
  7533. printf(resultFmt, ret == 0 ? passed : failed);
  7534. #endif
  7535. return ret;
  7536. } /* END test_wc_Sha3_384_Copy */
  7537. /*
  7538. * Testing wc_Sha3_512_Copy()
  7539. */
  7540. static int test_wc_Sha3_512_Copy (void)
  7541. {
  7542. int ret = 0;
  7543. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  7544. wc_Sha3 sha3, sha3Cpy;
  7545. const char* msg = "Everyone gets Friday off.";
  7546. word32 msglen = (word32)XSTRLEN(msg);
  7547. byte hash[WC_SHA3_512_DIGEST_SIZE];
  7548. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  7549. XMEMSET(hash, 0, sizeof(hash));
  7550. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  7551. printf(testingFmt, "wc_Sha3_512_Copy()");
  7552. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  7553. if (ret != 0) {
  7554. return ret;
  7555. }
  7556. ret = wc_InitSha3_512(&sha3Cpy, HEAP_HINT, devId);
  7557. if (ret != 0) {
  7558. wc_Sha3_512_Free(&sha3);
  7559. return ret;
  7560. }
  7561. ret = wc_Sha3_512_Update(&sha3, (byte*)msg, msglen);
  7562. if (ret == 0) {
  7563. ret = wc_Sha3_512_Copy(&sha3Cpy, &sha3);
  7564. if (ret == 0) {
  7565. ret = wc_Sha3_512_Final(&sha3, hash);
  7566. if (ret == 0) {
  7567. ret = wc_Sha3_512_Final(&sha3Cpy, hashCpy);
  7568. }
  7569. }
  7570. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  7571. ret = WOLFSSL_FATAL_ERROR;
  7572. }
  7573. }
  7574. /* Test bad args. */
  7575. if (ret == 0) {
  7576. ret = wc_Sha3_512_Copy(NULL, &sha3);
  7577. if (ret == BAD_FUNC_ARG) {
  7578. ret = wc_Sha3_512_Copy(&sha3Cpy, NULL);
  7579. }
  7580. if (ret == BAD_FUNC_ARG) {
  7581. ret = 0;
  7582. } else if (ret == 0) {
  7583. ret = WOLFSSL_FATAL_ERROR;
  7584. }
  7585. }
  7586. printf(resultFmt, ret == 0 ? passed : failed);
  7587. #endif
  7588. return ret;
  7589. } /* END test_wc_Sha3_512_Copy */
  7590. /*
  7591. * Unit test function for wc_Sha3_GetFlags()
  7592. */
  7593. static int test_wc_Sha3_GetFlags (void)
  7594. {
  7595. int ret = 0;
  7596. #if defined(WOLFSSL_SHA3) && \
  7597. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  7598. wc_Sha3 sha3;
  7599. word32 flags = 0;
  7600. printf(testingFmt, "wc_Sha3_GetFlags()");
  7601. /* Initialize */
  7602. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  7603. if (ret != 0) {
  7604. return ret;
  7605. }
  7606. if (ret == 0) {
  7607. ret = wc_Sha3_GetFlags(&sha3, &flags);
  7608. }
  7609. if (ret == 0) {
  7610. if (flags & WC_HASH_FLAG_ISCOPY) {
  7611. ret = 0;
  7612. }
  7613. }
  7614. wc_Sha3_224_Free(&sha3);
  7615. printf(resultFmt, ret == 0 ? passed : failed);
  7616. #endif
  7617. return ret;
  7618. } /* END test_wc_Sha3_GetFlags */
  7619. static int test_wc_InitShake256 (void)
  7620. {
  7621. int ret = 0;
  7622. #if defined(WOLFSSL_SHAKE256) && !defined(WOLFSSL_NO_SHAKE256)
  7623. wc_Shake shake;
  7624. printf(testingFmt, "wc_InitShake256()");
  7625. ret = wc_InitShake256(&shake, HEAP_HINT, devId);
  7626. /* Test bad args. */
  7627. if (ret == 0) {
  7628. ret = wc_InitShake256(NULL, HEAP_HINT, devId);
  7629. if (ret == BAD_FUNC_ARG) {
  7630. ret = 0;
  7631. } else if (ret == 0) {
  7632. ret = WOLFSSL_FATAL_ERROR;
  7633. }
  7634. }
  7635. wc_Shake256_Free(&shake);
  7636. printf(resultFmt, ret == 0 ? passed : failed);
  7637. #endif
  7638. return ret;
  7639. } /* END test_wc_InitSha3 */
  7640. static int testing_wc_Shake256_Update (void)
  7641. {
  7642. int ret = 0;
  7643. #if defined(WOLFSSL_SHAKE256) && !defined(WOLFSSL_NO_SHAKE256)
  7644. wc_Shake shake;
  7645. byte msg[] = "Everybody's working for the weekend.";
  7646. byte msg2[] = "Everybody gets Friday off.";
  7647. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  7648. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  7649. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  7650. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  7651. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  7652. word32 msglen = sizeof(msg) - 1;
  7653. word32 msg2len = sizeof(msg2);
  7654. word32 msgCmplen = sizeof(msgCmp);
  7655. printf(testingFmt, "wc_Shake256_Update()");
  7656. ret = wc_InitShake256(&shake, HEAP_HINT, devId);
  7657. if (ret != 0) {
  7658. return ret;
  7659. }
  7660. ret = wc_Shake256_Update(&shake, msg, msglen);
  7661. if (XMEMCMP(msg, shake.t, msglen) || shake.i != msglen) {
  7662. ret = WOLFSSL_FATAL_ERROR;
  7663. }
  7664. if (ret == 0) {
  7665. ret = wc_Shake256_Update(&shake, msg2, msg2len);
  7666. if (XMEMCMP(shake.t, msgCmp, msgCmplen) != 0) {
  7667. ret = WOLFSSL_FATAL_ERROR;
  7668. }
  7669. }
  7670. /* Pass bad args. */
  7671. if (ret == 0) {
  7672. ret = wc_Shake256_Update(NULL, msg2, msg2len);
  7673. if (ret == BAD_FUNC_ARG) {
  7674. ret = wc_Shake256_Update(&shake, NULL, 5);
  7675. }
  7676. if (ret == BAD_FUNC_ARG) {
  7677. wc_Shake256_Free(&shake);
  7678. if (wc_InitShake256(&shake, HEAP_HINT, devId)) {
  7679. return ret;
  7680. }
  7681. ret = wc_Shake256_Update(&shake, NULL, 0);
  7682. if (ret == 0) {
  7683. ret = wc_Shake256_Update(&shake, msg2, msg2len);
  7684. }
  7685. if (ret == 0 && XMEMCMP(msg2, shake.t, msg2len) != 0) {
  7686. ret = WOLFSSL_FATAL_ERROR;
  7687. }
  7688. }
  7689. }
  7690. wc_Shake256_Free(&shake);
  7691. printf(resultFmt, ret == 0 ? passed : failed);
  7692. #endif /* WOLFSSL_SHAKE256 && !WOLFSSL_NO_SHAKE256 */
  7693. return ret;
  7694. }
  7695. static int test_wc_Shake256_Final (void)
  7696. {
  7697. int ret = 0;
  7698. #if defined(WOLFSSL_SHAKE256) && !defined(WOLFSSL_NO_SHAKE256)
  7699. wc_Shake shake;
  7700. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  7701. "nopnopq";
  7702. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  7703. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  7704. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  7705. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  7706. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  7707. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  7708. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  7709. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  7710. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  7711. byte hash[114];
  7712. /* Init stack variables. */
  7713. XMEMSET(hash, 0, sizeof(hash));
  7714. printf(testingFmt, "wc_Shake256_Final()");
  7715. ret = wc_InitShake256(&shake, HEAP_HINT, devId);
  7716. if (ret != 0) {
  7717. return ret;
  7718. }
  7719. ret= wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg));
  7720. if (ret == 0) {
  7721. ret = wc_Shake256_Final(&shake, hash, (word32)sizeof(hash));
  7722. if (ret == 0 && XMEMCMP(expOut, hash, (word32)sizeof(hash)) != 0) {
  7723. ret = WOLFSSL_FATAL_ERROR;
  7724. }
  7725. }
  7726. /* Test bad args. */
  7727. if (ret == 0) {
  7728. ret = wc_Shake256_Final(NULL, hash, (word32)sizeof(hash));
  7729. if (ret == 0) {
  7730. ret = wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash));
  7731. }
  7732. if (ret == BAD_FUNC_ARG) {
  7733. ret = 0;
  7734. } else if (ret == 0) {
  7735. ret = WOLFSSL_FATAL_ERROR;
  7736. }
  7737. }
  7738. wc_Shake256_Free(&shake);
  7739. printf(resultFmt, ret == 0 ? passed : failed);
  7740. #endif
  7741. return ret;
  7742. }
  7743. /*
  7744. * Testing wc_Shake256_Copy()
  7745. */
  7746. static int test_wc_Shake256_Copy (void)
  7747. {
  7748. int ret = 0;
  7749. #if defined(WOLFSSL_SHAKE256) && !defined(WOLFSSL_NO_SHAKE256)
  7750. wc_Shake shake, shakeCpy;
  7751. const char* msg = "Everyone gets Friday off.";
  7752. word32 msglen = (word32)XSTRLEN(msg);
  7753. byte hash[144];
  7754. byte hashCpy[144];
  7755. word32 hashLen = sizeof(hash);
  7756. word32 hashLenCpy = sizeof(hashCpy);
  7757. XMEMSET(hash, 0, sizeof(hash));
  7758. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  7759. printf(testingFmt, "wc_Shake256_Copy()");
  7760. ret = wc_InitShake256(&shake, HEAP_HINT, devId);
  7761. if (ret != 0) {
  7762. return ret;
  7763. }
  7764. ret = wc_InitShake256(&shakeCpy, HEAP_HINT, devId);
  7765. if (ret != 0) {
  7766. wc_Shake256_Free(&shake);
  7767. return ret;
  7768. }
  7769. ret = wc_Shake256_Update(&shake, (byte*)msg, msglen);
  7770. if (ret == 0) {
  7771. ret = wc_Shake256_Copy(&shakeCpy, &shake);
  7772. if (ret == 0) {
  7773. ret = wc_Shake256_Final(&shake, hash, hashLen);
  7774. if (ret == 0) {
  7775. ret = wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy);
  7776. }
  7777. }
  7778. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  7779. ret = WOLFSSL_FATAL_ERROR;
  7780. }
  7781. }
  7782. /* Test bad args. */
  7783. if (ret == 0) {
  7784. ret = wc_Shake256_Copy(NULL, &shake);
  7785. if (ret == BAD_FUNC_ARG) {
  7786. ret = wc_Shake256_Copy(&shakeCpy, NULL);
  7787. }
  7788. if (ret == BAD_FUNC_ARG) {
  7789. ret = 0;
  7790. } else if (ret == 0) {
  7791. ret = WOLFSSL_FATAL_ERROR;
  7792. }
  7793. }
  7794. wc_Shake256_Free(&shake);
  7795. printf(resultFmt, ret == 0 ? passed : failed);
  7796. #endif
  7797. return ret;
  7798. } /* END test_wc_Shake256_Copy */
  7799. /*
  7800. * Unit test function for wc_Shake256Hash()
  7801. */
  7802. static int test_wc_Shake256Hash(void)
  7803. {
  7804. int ret = 0;
  7805. #if defined(WOLFSSL_SHAKE256) && !defined(WOLFSSL_NO_SHAKE256)
  7806. const byte data[] = { /* Hello World */
  7807. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  7808. 0x72,0x6c,0x64
  7809. };
  7810. word32 len = sizeof(data);
  7811. byte hash[144];
  7812. word32 hashLen = sizeof(hash);
  7813. printf(testingFmt, "wc_Shake256Hash()");
  7814. ret = wc_Shake256Hash(data, len, hash, hashLen);
  7815. printf(resultFmt, ret == 0 ? passed : failed);
  7816. #endif
  7817. return ret;
  7818. } /* END test_wc_Shake256Hash */
  7819. /*
  7820. * unit test for wc_IdeaSetKey()
  7821. */
  7822. static int test_wc_IdeaSetKey (void)
  7823. {
  7824. int ret = 0;
  7825. #ifdef HAVE_IDEA
  7826. Idea idea;
  7827. const byte key[] =
  7828. {
  7829. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37,
  7830. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37
  7831. };
  7832. int flag = 0;
  7833. printf(testingFmt, "wc_IdeaSetKey()");
  7834. /*IV can be NULL, default value is 0*/
  7835. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
  7836. if (ret == 0) {
  7837. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_DECRYPTION);
  7838. }
  7839. /* Bad args. */
  7840. if (ret == 0) {
  7841. ret = wc_IdeaSetKey(NULL, key, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
  7842. if (ret != BAD_FUNC_ARG) {
  7843. flag = 1;
  7844. }
  7845. ret = wc_IdeaSetKey(&idea, NULL, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
  7846. if (ret != BAD_FUNC_ARG) {
  7847. flag = 1;
  7848. }
  7849. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE - 1,
  7850. NULL, IDEA_ENCRYPTION);
  7851. if (ret != BAD_FUNC_ARG) {
  7852. flag = 1;
  7853. }
  7854. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, -1);
  7855. if (ret != BAD_FUNC_ARG) {
  7856. flag = 1;
  7857. }
  7858. if (flag == 1) {
  7859. ret = WOLFSSL_FATAL_ERROR;
  7860. } else {
  7861. ret = 0;
  7862. }
  7863. } /* END Test Bad Args. */
  7864. printf(resultFmt, ret == 0 ? passed : failed);
  7865. #endif
  7866. return ret;
  7867. } /* END test_wc_IdeaSetKey */
  7868. /*
  7869. * Unit test for wc_IdeaSetIV()
  7870. */
  7871. static int test_wc_IdeaSetIV (void)
  7872. {
  7873. int ret = 0;
  7874. #ifdef HAVE_IDEA
  7875. Idea idea;
  7876. printf(testingFmt, "wc_IdeaSetIV()");
  7877. ret = wc_IdeaSetIV(&idea, NULL);
  7878. /* Test bad args. */
  7879. if (ret == 0) {
  7880. ret = wc_IdeaSetIV(NULL, NULL);
  7881. if (ret == BAD_FUNC_ARG) {
  7882. ret = 0;
  7883. } else {
  7884. ret = WOLFSSL_FATAL_ERROR;
  7885. }
  7886. }
  7887. printf(resultFmt, ret == 0 ? passed : failed);
  7888. #endif
  7889. return ret;
  7890. } /* END test_wc_IdeaSetIV */
  7891. /*
  7892. * Unit test for wc_IdeaCipher()
  7893. */
  7894. static int test_wc_IdeaCipher (void)
  7895. {
  7896. int ret = 0;
  7897. #ifdef HAVE_IDEA
  7898. Idea idea;
  7899. const byte key[] =
  7900. {
  7901. 0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00,
  7902. 0x95, 0x2C, 0x49, 0x10, 0x48, 0x81, 0xFF, 0x48
  7903. };
  7904. const byte plain[] =
  7905. {
  7906. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37
  7907. };
  7908. byte enc[sizeof(plain)];
  7909. byte dec[sizeof(enc)];
  7910. printf(testingFmt, "wc_IdeaCipher()");
  7911. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
  7912. if (ret == 0) {
  7913. ret = wc_IdeaCipher(&idea, enc, plain);
  7914. if (ret != 0) {
  7915. ret = WOLFSSL_FATAL_ERROR;
  7916. }
  7917. }
  7918. if (ret == 0) {
  7919. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_DECRYPTION);
  7920. if (ret == 0) {
  7921. ret = wc_IdeaCipher(&idea, dec, enc);
  7922. }
  7923. if (ret == 0) {
  7924. ret = XMEMCMP(plain, dec, IDEA_BLOCK_SIZE);
  7925. }
  7926. if (ret != 0) {
  7927. ret = WOLFSSL_FATAL_ERROR;
  7928. }
  7929. }
  7930. /* Pass Bad Args. */
  7931. if (ret == 0) {
  7932. ret = wc_IdeaCipher(NULL, enc, dec);
  7933. if (ret == BAD_FUNC_ARG) {
  7934. ret = wc_IdeaCipher(&idea, NULL, dec);
  7935. }
  7936. if (ret == BAD_FUNC_ARG) {
  7937. ret = wc_IdeaCipher(&idea, enc, NULL);
  7938. }
  7939. if (ret == BAD_FUNC_ARG) {
  7940. ret = 0;
  7941. } else {
  7942. ret = WOLFSSL_FATAL_ERROR;
  7943. }
  7944. }
  7945. printf(resultFmt, ret == 0 ? passed : failed);
  7946. #endif
  7947. return ret;
  7948. } /* END test_wc_IdeaCipher */
  7949. /*
  7950. * Unit test for functions wc_IdeaCbcEncrypt and wc_IdeaCbcDecrypt
  7951. */
  7952. static int test_wc_IdeaCbcEncyptDecrypt (void)
  7953. {
  7954. int ret = 0;
  7955. #ifdef HAVE_IDEA
  7956. Idea idea;
  7957. const byte key[] =
  7958. {
  7959. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37,
  7960. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37
  7961. };
  7962. const char* message = "International Data Encryption Algorithm";
  7963. byte msg_enc[40];
  7964. byte msg_dec[40];
  7965. printf(testingFmt, "wc_IdeaCbcEncrypt()");
  7966. ret = wc_IdeaSetKey(&idea, key, sizeof(key), NULL, IDEA_ENCRYPTION);
  7967. if (ret == 0) {
  7968. ret = wc_IdeaCbcEncrypt(&idea, msg_enc, (byte *)message,
  7969. (word32)XSTRLEN(message) + 1);
  7970. }
  7971. if (ret == 0) {
  7972. ret = wc_IdeaSetKey(&idea, key, sizeof(key), NULL, IDEA_DECRYPTION);
  7973. }
  7974. if (ret == 0) {
  7975. ret = wc_IdeaCbcDecrypt(&idea, msg_dec, msg_enc,
  7976. (word32)XSTRLEN(message) + 1);
  7977. if (XMEMCMP(message, msg_dec, (word32)XSTRLEN(message))) {
  7978. ret = WOLFSSL_FATAL_ERROR;
  7979. }
  7980. }
  7981. /* Test bad args. Enc */
  7982. if (ret == 0) {
  7983. ret = wc_IdeaCbcEncrypt(NULL, msg_enc, (byte*)message,
  7984. (word32)XSTRLEN(message) + 1);
  7985. if (ret == BAD_FUNC_ARG) {
  7986. ret = wc_IdeaCbcEncrypt(&idea, NULL, (byte*)message,
  7987. (word32)XSTRLEN(message) + 1);
  7988. }
  7989. if (ret == BAD_FUNC_ARG) {
  7990. ret = wc_IdeaCbcEncrypt(&idea, msg_enc, NULL,
  7991. (word32)XSTRLEN(message) + 1);
  7992. }
  7993. if (ret != BAD_FUNC_ARG) {
  7994. ret = WOLFSSL_FATAL_ERROR;
  7995. } else {
  7996. ret = 0;
  7997. }
  7998. } /* END test bad args ENC */
  7999. /* Test bad args DEC */
  8000. if (ret == 0) {
  8001. ret = wc_IdeaCbcDecrypt(NULL, msg_dec, msg_enc,
  8002. (word32)XSTRLEN(message) + 1);
  8003. if (ret == BAD_FUNC_ARG) {
  8004. ret = wc_IdeaCbcDecrypt(&idea, NULL, msg_enc,
  8005. (word32)XSTRLEN(message) + 1);
  8006. }
  8007. if (ret == BAD_FUNC_ARG) {
  8008. ret = wc_IdeaCbcDecrypt(&idea, msg_dec, NULL,
  8009. (word32)XSTRLEN(message) + 1);
  8010. }
  8011. if (ret != BAD_FUNC_ARG) {
  8012. ret = WOLFSSL_FATAL_ERROR;
  8013. } else {
  8014. ret = 0;
  8015. }
  8016. }
  8017. printf(resultFmt, ret == 0 ? passed : failed);
  8018. #endif
  8019. return ret;
  8020. } /* END test_wc_IdeaCbcEncryptDecrypt */
  8021. /*
  8022. * Test function for wc_HmacSetKey
  8023. */
  8024. static int test_wc_Md5HmacSetKey (void)
  8025. {
  8026. int flag = 0;
  8027. #if !defined(NO_HMAC) && !defined(NO_MD5)
  8028. Hmac hmac;
  8029. int ret, times, itr;
  8030. const char* keys[]=
  8031. {
  8032. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  8033. #ifndef HAVE_FIPS
  8034. "Jefe", /* smaller than minimum FIPS key size */
  8035. #endif
  8036. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  8037. };
  8038. times = sizeof(keys) / sizeof(char*);
  8039. flag = 0;
  8040. printf(testingFmt, "wc_HmacSetKey() with MD5");
  8041. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8042. if (ret != 0)
  8043. return ret;
  8044. for (itr = 0; itr < times; itr++) {
  8045. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  8046. (word32)XSTRLEN(keys[itr]));
  8047. if (ret != 0) {
  8048. flag = ret;
  8049. }
  8050. }
  8051. /* Bad args. */
  8052. if (!flag) {
  8053. ret = wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  8054. (word32)XSTRLEN(keys[0]));
  8055. if (ret != BAD_FUNC_ARG) {
  8056. flag = WOLFSSL_FATAL_ERROR;
  8057. }
  8058. }
  8059. if (!flag) {
  8060. ret = wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0]));
  8061. if (ret != BAD_FUNC_ARG) {
  8062. flag = WOLFSSL_FATAL_ERROR;
  8063. }
  8064. }
  8065. if (!flag) {
  8066. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  8067. (word32)XSTRLEN(keys[0]));
  8068. if (ret != BAD_FUNC_ARG) {
  8069. flag = WOLFSSL_FATAL_ERROR;
  8070. }
  8071. }
  8072. if (!flag) {
  8073. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  8074. #ifdef HAVE_FIPS
  8075. if (ret != HMAC_MIN_KEYLEN_E) {
  8076. flag = WOLFSSL_FATAL_ERROR;
  8077. }
  8078. #else
  8079. if (ret != 0) {
  8080. flag = WOLFSSL_FATAL_ERROR;
  8081. }
  8082. #endif
  8083. }
  8084. wc_HmacFree(&hmac);
  8085. printf(resultFmt, flag == 0 ? passed : failed);
  8086. #endif
  8087. return flag;
  8088. } /* END test_wc_Md5HmacSetKey */
  8089. /*
  8090. * testing wc_HmacSetKey() on wc_Sha hash.
  8091. */
  8092. static int test_wc_ShaHmacSetKey (void)
  8093. {
  8094. int flag = 0;
  8095. #if !defined(NO_HMAC) && !defined(NO_SHA)
  8096. Hmac hmac;
  8097. int ret, times, itr;
  8098. const char* keys[]=
  8099. {
  8100. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  8101. "\x0b\x0b\x0b",
  8102. #ifndef HAVE_FIPS
  8103. "Jefe", /* smaller than minimum FIPS key size */
  8104. #endif
  8105. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  8106. "\xAA\xAA\xAA"
  8107. };
  8108. times = sizeof(keys) / sizeof(char*);
  8109. flag = 0;
  8110. printf(testingFmt, "wc_HmacSetKey() with SHA");
  8111. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8112. if (ret != 0)
  8113. return ret;
  8114. for (itr = 0; itr < times; itr++) {
  8115. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  8116. (word32)XSTRLEN(keys[itr]));
  8117. if (ret != 0) {
  8118. flag = ret;
  8119. }
  8120. }
  8121. /* Bad args. */
  8122. if (!flag) {
  8123. ret = wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  8124. (word32)XSTRLEN(keys[0]));
  8125. if (ret != BAD_FUNC_ARG) {
  8126. flag = WOLFSSL_FATAL_ERROR;
  8127. }
  8128. }
  8129. if (!flag) {
  8130. ret = wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0]));
  8131. if (ret != BAD_FUNC_ARG) {
  8132. flag = WOLFSSL_FATAL_ERROR;
  8133. }
  8134. }
  8135. if (!flag) {
  8136. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  8137. (word32)XSTRLEN(keys[0]));
  8138. if (ret != BAD_FUNC_ARG) {
  8139. flag = WOLFSSL_FATAL_ERROR;
  8140. }
  8141. }
  8142. if (!flag) {
  8143. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  8144. #ifdef HAVE_FIPS
  8145. if (ret != HMAC_MIN_KEYLEN_E) {
  8146. flag = WOLFSSL_FATAL_ERROR;
  8147. }
  8148. #else
  8149. if (ret != 0) {
  8150. flag = WOLFSSL_FATAL_ERROR;
  8151. }
  8152. #endif
  8153. }
  8154. wc_HmacFree(&hmac);
  8155. printf(resultFmt, flag == 0 ? passed : failed);
  8156. #endif
  8157. return flag;
  8158. } /* END test_wc_ShaHmacSetKey() */
  8159. /*
  8160. * testing wc_HmacSetKey() on Sha224 hash.
  8161. */
  8162. static int test_wc_Sha224HmacSetKey (void)
  8163. {
  8164. int flag = 0;
  8165. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  8166. Hmac hmac;
  8167. int ret, times, itr;
  8168. const char* keys[]=
  8169. {
  8170. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  8171. "\x0b\x0b\x0b",
  8172. #ifndef HAVE_FIPS
  8173. "Jefe", /* smaller than minimum FIPS key size */
  8174. #endif
  8175. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  8176. "\xAA\xAA\xAA"
  8177. };
  8178. times = sizeof(keys) / sizeof(char*);
  8179. flag = 0;
  8180. printf(testingFmt, "wc_HmacSetKey() with SHA 224");
  8181. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8182. if (ret != 0)
  8183. return ret;
  8184. for (itr = 0; itr < times; itr++) {
  8185. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  8186. (word32)XSTRLEN(keys[itr]));
  8187. if (ret != 0) {
  8188. flag = ret;
  8189. }
  8190. }
  8191. /* Bad args. */
  8192. if (!flag) {
  8193. ret = wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  8194. (word32)XSTRLEN(keys[0]));
  8195. if (ret != BAD_FUNC_ARG) {
  8196. flag = WOLFSSL_FATAL_ERROR;
  8197. }
  8198. }
  8199. if (!flag) {
  8200. ret = wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0]));
  8201. if (ret != BAD_FUNC_ARG) {
  8202. flag = WOLFSSL_FATAL_ERROR;
  8203. }
  8204. }
  8205. if (!flag) {
  8206. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  8207. (word32)XSTRLEN(keys[0]));
  8208. if (ret != BAD_FUNC_ARG) {
  8209. flag = WOLFSSL_FATAL_ERROR;
  8210. }
  8211. }
  8212. if (!flag) {
  8213. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  8214. #ifdef HAVE_FIPS
  8215. if (ret != HMAC_MIN_KEYLEN_E) {
  8216. flag = WOLFSSL_FATAL_ERROR;
  8217. }
  8218. #else
  8219. if (ret != 0) {
  8220. flag = WOLFSSL_FATAL_ERROR;
  8221. }
  8222. #endif
  8223. }
  8224. wc_HmacFree(&hmac);
  8225. printf(resultFmt, flag == 0 ? passed : failed);
  8226. #endif
  8227. return flag;
  8228. } /* END test_wc_Sha224HmacSetKey() */
  8229. /*
  8230. * testing wc_HmacSetKey() on Sha256 hash
  8231. */
  8232. static int test_wc_Sha256HmacSetKey (void)
  8233. {
  8234. int flag = 0;
  8235. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  8236. Hmac hmac;
  8237. int ret, times, itr;
  8238. const char* keys[]=
  8239. {
  8240. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  8241. "\x0b\x0b\x0b",
  8242. #ifndef HAVE_FIPS
  8243. "Jefe", /* smaller than minimum FIPS key size */
  8244. #endif
  8245. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  8246. "\xAA\xAA\xAA"
  8247. };
  8248. times = sizeof(keys) / sizeof(char*);
  8249. flag = 0;
  8250. printf(testingFmt, "wc_HmacSetKey() with SHA256");
  8251. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8252. if (ret != 0)
  8253. return ret;
  8254. for (itr = 0; itr < times; itr++) {
  8255. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  8256. (word32)XSTRLEN(keys[itr]));
  8257. if (ret != 0) {
  8258. flag = ret;
  8259. }
  8260. }
  8261. /* Bad args. */
  8262. if (!flag) {
  8263. ret = wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  8264. (word32)XSTRLEN(keys[0]));
  8265. if (ret != BAD_FUNC_ARG) {
  8266. flag = WOLFSSL_FATAL_ERROR;
  8267. }
  8268. }
  8269. if (!flag) {
  8270. ret = wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0]));
  8271. if (ret != BAD_FUNC_ARG) {
  8272. flag = WOLFSSL_FATAL_ERROR;
  8273. }
  8274. }
  8275. if (!flag) {
  8276. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  8277. (word32)XSTRLEN(keys[0]));
  8278. if (ret != BAD_FUNC_ARG) {
  8279. flag = WOLFSSL_FATAL_ERROR;
  8280. }
  8281. }
  8282. if (!flag) {
  8283. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  8284. #ifdef HAVE_FIPS
  8285. if (ret != HMAC_MIN_KEYLEN_E) {
  8286. flag = WOLFSSL_FATAL_ERROR;
  8287. }
  8288. #else
  8289. if (ret != 0) {
  8290. flag = WOLFSSL_FATAL_ERROR;
  8291. }
  8292. #endif
  8293. }
  8294. wc_HmacFree(&hmac);
  8295. printf(resultFmt, flag == 0 ? passed : failed);
  8296. #endif
  8297. return flag;
  8298. } /* END test_wc_Sha256HmacSetKey() */
  8299. /*
  8300. * testing wc_HmacSetKey on Sha384 hash.
  8301. */
  8302. static int test_wc_Sha384HmacSetKey (void)
  8303. {
  8304. int flag = 0;
  8305. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  8306. Hmac hmac;
  8307. int ret, times, itr;
  8308. const char* keys[]=
  8309. {
  8310. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  8311. "\x0b\x0b\x0b",
  8312. #ifndef HAVE_FIPS
  8313. "Jefe", /* smaller than minimum FIPS key size */
  8314. #endif
  8315. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  8316. "\xAA\xAA\xAA"
  8317. };
  8318. times = sizeof(keys) / sizeof(char*);
  8319. flag = 0;
  8320. printf(testingFmt, "wc_HmacSetKey() with SHA384");
  8321. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8322. if (ret != 0)
  8323. return ret;
  8324. for (itr = 0; itr < times; itr++) {
  8325. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  8326. (word32)XSTRLEN(keys[itr]));
  8327. if (ret != 0) {
  8328. flag = ret;
  8329. }
  8330. }
  8331. /* Bad args. */
  8332. if (!flag) {
  8333. ret = wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  8334. (word32)XSTRLEN(keys[0]));
  8335. if (ret != BAD_FUNC_ARG) {
  8336. flag = WOLFSSL_FATAL_ERROR;
  8337. }
  8338. }
  8339. if (!flag) {
  8340. ret = wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0]));
  8341. if (ret != BAD_FUNC_ARG) {
  8342. flag = WOLFSSL_FATAL_ERROR;
  8343. }
  8344. }
  8345. if (!flag) {
  8346. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  8347. (word32)XSTRLEN(keys[0]));
  8348. if (ret != BAD_FUNC_ARG) {
  8349. flag = WOLFSSL_FATAL_ERROR;
  8350. }
  8351. }
  8352. if (!flag) {
  8353. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  8354. #ifdef HAVE_FIPS
  8355. if (ret != HMAC_MIN_KEYLEN_E) {
  8356. flag = WOLFSSL_FATAL_ERROR;
  8357. }
  8358. #else
  8359. if (ret != 0) {
  8360. flag = WOLFSSL_FATAL_ERROR;
  8361. }
  8362. #endif
  8363. }
  8364. wc_HmacFree(&hmac);
  8365. printf(resultFmt, flag == 0 ? passed : failed);
  8366. #endif
  8367. return flag;
  8368. } /* END test_wc_Sha384HmacSetKey() */
  8369. /*
  8370. * testing wc_HmacUpdate on wc_Md5 hash.
  8371. */
  8372. static int test_wc_Md5HmacUpdate (void)
  8373. {
  8374. int flag = 0;
  8375. #if !defined(NO_HMAC) && !defined(NO_MD5)
  8376. Hmac hmac;
  8377. testVector a, b;
  8378. int ret;
  8379. #ifdef HAVE_FIPS
  8380. const char* keys =
  8381. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  8382. #else
  8383. const char* keys = "Jefe";
  8384. #endif
  8385. a.input = "what do ya want for nothing?";
  8386. a.inLen = XSTRLEN(a.input);
  8387. b.input = "Hi There";
  8388. b.inLen = XSTRLEN(b.input);
  8389. flag = 0;
  8390. printf(testingFmt, "wc_HmacUpdate() with MD5");
  8391. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8392. if (ret != 0)
  8393. return ret;
  8394. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys, (word32)XSTRLEN(keys));
  8395. if (ret != 0) {
  8396. flag = ret;
  8397. }
  8398. if (!flag) {
  8399. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  8400. if (ret != 0) {
  8401. flag = ret;
  8402. }
  8403. }
  8404. /* Update Hmac. */
  8405. if (!flag) {
  8406. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  8407. if (ret != 0) {
  8408. flag = ret;
  8409. }
  8410. }
  8411. /* Test bad args. */
  8412. if (!flag) {
  8413. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  8414. if (ret != BAD_FUNC_ARG) {
  8415. flag = WOLFSSL_FATAL_ERROR;
  8416. }
  8417. }
  8418. if (!flag) {
  8419. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  8420. if (ret != BAD_FUNC_ARG) {
  8421. flag = WOLFSSL_FATAL_ERROR;
  8422. }
  8423. }
  8424. if (!flag) {
  8425. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  8426. if (ret != 0) {
  8427. flag = ret;
  8428. }
  8429. }
  8430. wc_HmacFree(&hmac);
  8431. printf(resultFmt, flag == 0 ? passed : failed);
  8432. #endif
  8433. return flag;
  8434. } /* END test_wc_Md5HmacUpdate */
  8435. /*
  8436. * testing wc_HmacUpdate on SHA hash.
  8437. */
  8438. static int test_wc_ShaHmacUpdate (void)
  8439. {
  8440. int flag = 0;
  8441. #if !defined(NO_HMAC) && !defined(NO_SHA)
  8442. Hmac hmac;
  8443. testVector a, b;
  8444. int ret;
  8445. #ifdef HAVE_FIPS
  8446. const char* keys =
  8447. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  8448. #else
  8449. const char* keys = "Jefe";
  8450. #endif
  8451. a.input = "what do ya want for nothing?";
  8452. a.inLen = XSTRLEN(a.input);
  8453. b.input = "Hi There";
  8454. b.inLen = XSTRLEN(b.input);
  8455. flag = 0;
  8456. printf(testingFmt, "wc_HmacUpdate() with SHA");
  8457. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8458. if (ret != 0)
  8459. return ret;
  8460. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys, (word32)XSTRLEN(keys));
  8461. if (ret != 0) {
  8462. flag = ret;
  8463. }
  8464. if (!flag) {
  8465. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  8466. if (ret != 0) {
  8467. flag = ret;
  8468. }
  8469. }
  8470. /* Update Hmac. */
  8471. if (!flag) {
  8472. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  8473. if (ret != 0) {
  8474. flag = ret;
  8475. }
  8476. }
  8477. /* Test bad args. */
  8478. if (!flag) {
  8479. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  8480. if (ret != BAD_FUNC_ARG) {
  8481. flag = WOLFSSL_FATAL_ERROR;
  8482. }
  8483. }
  8484. if (!flag) {
  8485. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  8486. if (ret != BAD_FUNC_ARG) {
  8487. flag = WOLFSSL_FATAL_ERROR;
  8488. }
  8489. }
  8490. if (!flag) {
  8491. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  8492. if (ret != 0) {
  8493. flag = ret;
  8494. }
  8495. }
  8496. wc_HmacFree(&hmac);
  8497. printf(resultFmt, flag == 0 ? passed : failed);
  8498. #endif
  8499. return flag;
  8500. } /* END test_wc_ShaHmacUpdate */
  8501. /*
  8502. * testing wc_HmacUpdate on SHA224 hash.
  8503. */
  8504. static int test_wc_Sha224HmacUpdate (void)
  8505. {
  8506. int flag = 0;
  8507. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  8508. Hmac hmac;
  8509. testVector a, b;
  8510. int ret;
  8511. #ifdef HAVE_FIPS
  8512. const char* keys =
  8513. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  8514. #else
  8515. const char* keys = "Jefe";
  8516. #endif
  8517. a.input = "what do ya want for nothing?";
  8518. a.inLen = XSTRLEN(a.input);
  8519. b.input = "Hi There";
  8520. b.inLen = XSTRLEN(b.input);
  8521. flag = 0;
  8522. printf(testingFmt, "wc_HmacUpdate() with SHA224");
  8523. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8524. if (ret != 0)
  8525. return ret;
  8526. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys, (word32)XSTRLEN(keys));
  8527. if (ret != 0) {
  8528. flag = ret;
  8529. }
  8530. if (!flag) {
  8531. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  8532. if (ret != 0) {
  8533. flag = ret;
  8534. }
  8535. }
  8536. /* Update Hmac. */
  8537. if (!flag) {
  8538. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  8539. if (ret != 0) {
  8540. flag = ret;
  8541. }
  8542. }
  8543. /* Test bad args. */
  8544. if (!flag) {
  8545. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  8546. if (ret != BAD_FUNC_ARG) {
  8547. flag = WOLFSSL_FATAL_ERROR;
  8548. }
  8549. }
  8550. if (!flag) {
  8551. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  8552. if (ret != BAD_FUNC_ARG) {
  8553. flag = WOLFSSL_FATAL_ERROR;
  8554. }
  8555. }
  8556. if (!flag) {
  8557. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  8558. if (ret != 0) {
  8559. flag = ret;
  8560. }
  8561. }
  8562. wc_HmacFree(&hmac);
  8563. printf(resultFmt, flag == 0 ? passed : failed);
  8564. #endif
  8565. return flag;
  8566. } /* END test_wc_Sha224HmacUpdate */
  8567. /*
  8568. * testing wc_HmacUpdate on SHA256 hash.
  8569. */
  8570. static int test_wc_Sha256HmacUpdate (void)
  8571. {
  8572. int flag = 0;
  8573. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  8574. Hmac hmac;
  8575. testVector a, b;
  8576. int ret;
  8577. #ifdef HAVE_FIPS
  8578. const char* keys =
  8579. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  8580. #else
  8581. const char* keys = "Jefe";
  8582. #endif
  8583. a.input = "what do ya want for nothing?";
  8584. a.inLen = XSTRLEN(a.input);
  8585. b.input = "Hi There";
  8586. b.inLen = XSTRLEN(b.input);
  8587. flag = 0;
  8588. printf(testingFmt, "wc_HmacUpdate() with WC_SHA256");
  8589. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8590. if (ret != 0)
  8591. return ret;
  8592. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys, (word32)XSTRLEN(keys));
  8593. if (ret != 0) {
  8594. flag = ret;
  8595. }
  8596. if (!flag) {
  8597. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  8598. if (ret != 0) {
  8599. flag = ret;
  8600. }
  8601. }
  8602. /* Update Hmac. */
  8603. if (!flag) {
  8604. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  8605. if (ret != 0) {
  8606. flag = ret;
  8607. }
  8608. }
  8609. /* Test bad args. */
  8610. if (!flag) {
  8611. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  8612. if (ret != BAD_FUNC_ARG) {
  8613. flag = WOLFSSL_FATAL_ERROR;
  8614. }
  8615. }
  8616. if (!flag) {
  8617. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  8618. if (ret != BAD_FUNC_ARG) {
  8619. flag = WOLFSSL_FATAL_ERROR;
  8620. }
  8621. }
  8622. if (!flag) {
  8623. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  8624. if (ret != 0) {
  8625. flag = ret;
  8626. }
  8627. }
  8628. wc_HmacFree(&hmac);
  8629. printf(resultFmt, flag == 0 ? passed : failed);
  8630. #endif
  8631. return flag;
  8632. } /* END test_wc_Sha256HmacUpdate */
  8633. /*
  8634. * testing wc_HmacUpdate on SHA384 hash.
  8635. */
  8636. static int test_wc_Sha384HmacUpdate (void)
  8637. {
  8638. int flag = 0;
  8639. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  8640. Hmac hmac;
  8641. testVector a, b;
  8642. int ret;
  8643. #ifdef HAVE_FIPS
  8644. const char* keys =
  8645. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  8646. #else
  8647. const char* keys = "Jefe";
  8648. #endif
  8649. a.input = "what do ya want for nothing?";
  8650. a.inLen = XSTRLEN(a.input);
  8651. b.input = "Hi There";
  8652. b.inLen = XSTRLEN(b.input);
  8653. flag = 0;
  8654. printf(testingFmt, "wc_HmacUpdate() with SHA384");
  8655. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8656. if (ret != 0)
  8657. return ret;
  8658. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys, (word32)XSTRLEN(keys));
  8659. if (ret != 0) {
  8660. flag = ret;
  8661. }
  8662. if (!flag) {
  8663. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  8664. if (ret != 0) {
  8665. flag = ret;
  8666. }
  8667. }
  8668. /* Update Hmac. */
  8669. if (!flag) {
  8670. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  8671. if (ret != 0) {
  8672. flag = ret;
  8673. }
  8674. }
  8675. /* Test bad args. */
  8676. if (!flag) {
  8677. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  8678. if (ret != BAD_FUNC_ARG) {
  8679. flag = WOLFSSL_FATAL_ERROR;
  8680. }
  8681. }
  8682. if (!flag) {
  8683. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  8684. if (ret != BAD_FUNC_ARG) {
  8685. flag = WOLFSSL_FATAL_ERROR;
  8686. }
  8687. }
  8688. if (!flag) {
  8689. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  8690. if (ret != 0) {
  8691. flag = ret;
  8692. }
  8693. }
  8694. wc_HmacFree(&hmac);
  8695. printf(resultFmt, flag == 0 ? passed : failed);
  8696. #endif
  8697. return flag;
  8698. } /* END test_wc_Sha384HmacUpdate */
  8699. /*
  8700. * Testing wc_HmacFinal() with MD5
  8701. */
  8702. static int test_wc_Md5HmacFinal (void)
  8703. {
  8704. int flag = 0;
  8705. #if !defined(NO_HMAC) && !defined(NO_MD5)
  8706. Hmac hmac;
  8707. byte hash[WC_MD5_DIGEST_SIZE];
  8708. testVector a;
  8709. int ret;
  8710. const char* key;
  8711. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  8712. a.input = "Hi There";
  8713. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  8714. "\x9d";
  8715. a.inLen = XSTRLEN(a.input);
  8716. a.outLen = XSTRLEN(a.output);
  8717. flag = 0;
  8718. printf(testingFmt, "wc_HmacFinal() with MD5");
  8719. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8720. if (ret != 0)
  8721. return ret;
  8722. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key));
  8723. if (ret != 0) {
  8724. flag = ret;
  8725. }
  8726. if (!flag) {
  8727. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  8728. if (ret != 0) {
  8729. flag = ret;
  8730. }
  8731. }
  8732. if (!flag) {
  8733. ret = wc_HmacFinal(&hmac, hash);
  8734. if (ret != 0) {
  8735. flag = ret;
  8736. }
  8737. }
  8738. if (!flag) {
  8739. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  8740. flag = WOLFSSL_FATAL_ERROR;
  8741. }
  8742. }
  8743. /* Try bad parameters. */
  8744. if (!flag) {
  8745. ret = wc_HmacFinal(NULL, hash);
  8746. if (ret != BAD_FUNC_ARG) {
  8747. flag = WOLFSSL_FATAL_ERROR;
  8748. }
  8749. }
  8750. #ifndef HAVE_FIPS
  8751. if (!flag) {
  8752. ret = wc_HmacFinal(&hmac, NULL);
  8753. if (ret != BAD_FUNC_ARG) {
  8754. flag = WOLFSSL_FATAL_ERROR;
  8755. }
  8756. }
  8757. #endif
  8758. wc_HmacFree(&hmac);
  8759. printf(resultFmt, flag == 0 ? passed : failed);
  8760. #endif
  8761. return flag;
  8762. } /* END test_wc_Md5HmacFinal */
  8763. /*
  8764. * Testing wc_HmacFinal() with SHA
  8765. */
  8766. static int test_wc_ShaHmacFinal (void)
  8767. {
  8768. int flag = 0;
  8769. #if !defined(NO_HMAC) && !defined(NO_SHA)
  8770. Hmac hmac;
  8771. byte hash[WC_SHA_DIGEST_SIZE];
  8772. testVector a;
  8773. int ret;
  8774. const char* key;
  8775. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  8776. "\x0b\x0b\x0b";
  8777. a.input = "Hi There";
  8778. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  8779. "\x8e\xf1\x46\xbe\x00";
  8780. a.inLen = XSTRLEN(a.input);
  8781. a.outLen = XSTRLEN(a.output);
  8782. flag = 0;
  8783. printf(testingFmt, "wc_HmacFinal() with SHA");
  8784. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8785. if (ret != 0)
  8786. return ret;
  8787. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key));
  8788. if (ret != 0) {
  8789. flag = ret;
  8790. }
  8791. if (!flag) {
  8792. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  8793. if (ret != 0) {
  8794. flag = ret;
  8795. }
  8796. }
  8797. if (!flag) {
  8798. ret = wc_HmacFinal(&hmac, hash);
  8799. if (ret != 0) {
  8800. flag = ret;
  8801. }
  8802. }
  8803. if (!flag) {
  8804. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  8805. flag = WOLFSSL_FATAL_ERROR;
  8806. }
  8807. }
  8808. /* Try bad parameters. */
  8809. if (!flag) {
  8810. ret = wc_HmacFinal(NULL, hash);
  8811. if (ret != BAD_FUNC_ARG) {
  8812. flag = WOLFSSL_FATAL_ERROR;
  8813. }
  8814. }
  8815. #ifndef HAVE_FIPS
  8816. if (!flag) {
  8817. ret = wc_HmacFinal(&hmac, NULL);
  8818. if (ret != BAD_FUNC_ARG) {
  8819. flag = WOLFSSL_FATAL_ERROR;
  8820. }
  8821. }
  8822. #endif
  8823. wc_HmacFree(&hmac);
  8824. printf(resultFmt, flag == 0 ? passed : failed);
  8825. #endif
  8826. return flag;
  8827. } /* END test_wc_ShaHmacFinal */
  8828. /*
  8829. * Testing wc_HmacFinal() with SHA224
  8830. */
  8831. static int test_wc_Sha224HmacFinal (void)
  8832. {
  8833. int flag = 0;
  8834. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  8835. Hmac hmac;
  8836. byte hash[WC_SHA224_DIGEST_SIZE];
  8837. testVector a;
  8838. int ret;
  8839. const char* key;
  8840. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  8841. "\x0b\x0b\x0b";
  8842. a.input = "Hi There";
  8843. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  8844. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  8845. a.inLen = XSTRLEN(a.input);
  8846. a.outLen = XSTRLEN(a.output);
  8847. flag = 0;
  8848. printf(testingFmt, "wc_HmacFinal() with SHA224");
  8849. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8850. if (ret != 0)
  8851. return ret;
  8852. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key, (word32)XSTRLEN(key));
  8853. if (ret != 0) {
  8854. flag = ret;
  8855. }
  8856. if (!flag) {
  8857. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  8858. if (ret != 0) {
  8859. flag = ret;
  8860. }
  8861. }
  8862. if (!flag) {
  8863. ret = wc_HmacFinal(&hmac, hash);
  8864. if (ret != 0) {
  8865. flag = ret;
  8866. }
  8867. }
  8868. if (!flag) {
  8869. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  8870. flag = WOLFSSL_FATAL_ERROR;
  8871. }
  8872. }
  8873. /* Try bad parameters. */
  8874. if (!flag) {
  8875. ret = wc_HmacFinal(NULL, hash);
  8876. if (ret != BAD_FUNC_ARG) {
  8877. flag = WOLFSSL_FATAL_ERROR;
  8878. }
  8879. }
  8880. #ifndef HAVE_FIPS
  8881. if (!flag) {
  8882. ret = wc_HmacFinal(&hmac, NULL);
  8883. if (ret != BAD_FUNC_ARG) {
  8884. flag = WOLFSSL_FATAL_ERROR;
  8885. }
  8886. }
  8887. #endif
  8888. wc_HmacFree(&hmac);
  8889. printf(resultFmt, flag == 0 ? passed : failed);
  8890. #endif
  8891. return flag;
  8892. } /* END test_wc_Sha224HmacFinal */
  8893. /*
  8894. * Testing wc_HmacFinal() with SHA256
  8895. */
  8896. static int test_wc_Sha256HmacFinal (void)
  8897. {
  8898. int flag = 0;
  8899. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  8900. Hmac hmac;
  8901. byte hash[WC_SHA256_DIGEST_SIZE];
  8902. testVector a;
  8903. int ret;
  8904. const char* key;
  8905. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  8906. "\x0b\x0b\x0b";
  8907. a.input = "Hi There";
  8908. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  8909. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  8910. "\xcf\xf7";
  8911. a.inLen = XSTRLEN(a.input);
  8912. a.outLen = XSTRLEN(a.output);
  8913. flag = 0;
  8914. printf(testingFmt, "wc_HmacFinal() with WC_SHA256");
  8915. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8916. if (ret != 0)
  8917. return ret;
  8918. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key, (word32)XSTRLEN(key));
  8919. if (ret != 0) {
  8920. flag = ret;
  8921. }
  8922. if (!flag) {
  8923. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  8924. if (ret != 0) {
  8925. flag = ret;
  8926. }
  8927. }
  8928. if (!flag) {
  8929. ret = wc_HmacFinal(&hmac, hash);
  8930. if (ret != 0) {
  8931. flag = ret;
  8932. }
  8933. }
  8934. if (!flag) {
  8935. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  8936. flag = WOLFSSL_FATAL_ERROR;
  8937. }
  8938. }
  8939. /* Try bad parameters. */
  8940. if (!flag) {
  8941. ret = wc_HmacFinal(NULL, hash);
  8942. if (ret != BAD_FUNC_ARG) {
  8943. flag = WOLFSSL_FATAL_ERROR;
  8944. }
  8945. }
  8946. #ifndef HAVE_FIPS
  8947. if (!flag) {
  8948. ret = wc_HmacFinal(&hmac, NULL);
  8949. if (ret != BAD_FUNC_ARG) {
  8950. flag = WOLFSSL_FATAL_ERROR;
  8951. }
  8952. }
  8953. #endif
  8954. wc_HmacFree(&hmac);
  8955. printf(resultFmt, flag == 0 ? passed : failed);
  8956. #endif
  8957. return flag;
  8958. } /* END test_wc_Sha256HmacFinal */
  8959. /*
  8960. * Testing wc_HmacFinal() with SHA384
  8961. */
  8962. static int test_wc_Sha384HmacFinal (void)
  8963. {
  8964. int flag = 0;
  8965. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  8966. Hmac hmac;
  8967. byte hash[WC_SHA384_DIGEST_SIZE];
  8968. testVector a;
  8969. int ret;
  8970. const char* key;
  8971. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  8972. "\x0b\x0b\x0b";
  8973. a.input = "Hi There";
  8974. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  8975. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  8976. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  8977. "\xfa\x9c\xb6";
  8978. a.inLen = XSTRLEN(a.input);
  8979. a.outLen = XSTRLEN(a.output);
  8980. flag = 0;
  8981. printf(testingFmt, "wc_HmacFinal() with SHA384");
  8982. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8983. if (ret != 0)
  8984. return ret;
  8985. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key, (word32)XSTRLEN(key));
  8986. if (ret != 0) {
  8987. flag = ret;
  8988. }
  8989. if (!flag) {
  8990. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  8991. if (ret != 0) {
  8992. flag = ret;
  8993. }
  8994. }
  8995. if (!flag) {
  8996. ret = wc_HmacFinal(&hmac, hash);
  8997. if (ret != 0) {
  8998. flag = ret;
  8999. }
  9000. }
  9001. if (!flag) {
  9002. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  9003. flag = WOLFSSL_FATAL_ERROR;
  9004. }
  9005. }
  9006. /* Try bad parameters. */
  9007. if (!flag) {
  9008. ret = wc_HmacFinal(NULL, hash);
  9009. if (ret != BAD_FUNC_ARG) {
  9010. flag = WOLFSSL_FATAL_ERROR;
  9011. }
  9012. }
  9013. #ifndef HAVE_FIPS
  9014. if (!flag) {
  9015. ret = wc_HmacFinal(&hmac, NULL);
  9016. if (ret != BAD_FUNC_ARG) {
  9017. flag = WOLFSSL_FATAL_ERROR;
  9018. }
  9019. }
  9020. #endif
  9021. wc_HmacFree(&hmac);
  9022. printf(resultFmt, flag == 0 ? passed : failed);
  9023. #endif
  9024. return flag;
  9025. } /* END test_wc_Sha384HmacFinal */
  9026. /*
  9027. * Testing wc_InitCmac()
  9028. */
  9029. static int test_wc_InitCmac (void)
  9030. {
  9031. int ret = 0;
  9032. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  9033. Cmac cmac1, cmac2, cmac3;
  9034. /* AES 128 key. */
  9035. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  9036. "\x09\x10\x11\x12\x13\x14\x15\x16";
  9037. /* AES 192 key. */
  9038. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  9039. "\x09\x01\x11\x12\x13\x14\x15\x16"
  9040. "\x01\x02\x03\x04\x05\x06\x07\x08";
  9041. /* AES 256 key. */
  9042. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  9043. "\x09\x01\x11\x12\x13\x14\x15\x16"
  9044. "\x01\x02\x03\x04\x05\x06\x07\x08"
  9045. "\x09\x01\x11\x12\x13\x14\x15\x16";
  9046. word32 key1Sz = (word32)sizeof(key1) - 1;
  9047. word32 key2Sz = (word32)sizeof(key2) - 1;
  9048. word32 key3Sz = (word32)sizeof(key3) - 1;
  9049. int type = WC_CMAC_AES;
  9050. printf(testingFmt, "wc_InitCmac()");
  9051. #ifdef WOLFSSL_AES_128
  9052. ret = wc_InitCmac(&cmac1, key1, key1Sz, type, NULL);
  9053. #endif
  9054. #ifdef WOLFSSL_AES_192
  9055. if (ret == 0)
  9056. ret = wc_InitCmac(&cmac2, key2, key2Sz, type, NULL);
  9057. #endif
  9058. #ifdef WOLFSSL_AES_256
  9059. if (ret == 0)
  9060. ret = wc_InitCmac(&cmac3, key3, key3Sz, type, NULL);
  9061. #endif
  9062. /* Test bad args. */
  9063. if (ret == 0) {
  9064. ret = wc_InitCmac(NULL, key3, key3Sz, type, NULL);
  9065. if (ret == BAD_FUNC_ARG) {
  9066. ret = wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL);
  9067. }
  9068. if (ret == BAD_FUNC_ARG) {
  9069. ret = wc_InitCmac(&cmac3, key3, 0, type, NULL);
  9070. }
  9071. if (ret == BAD_FUNC_ARG) {
  9072. ret = wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL);
  9073. }
  9074. if (ret == BAD_FUNC_ARG) {
  9075. ret = 0;
  9076. } else {
  9077. ret = WOLFSSL_FATAL_ERROR;
  9078. }
  9079. }
  9080. (void)key1;
  9081. (void)key1Sz;
  9082. (void)key2;
  9083. (void)key2Sz;
  9084. (void)cmac1;
  9085. (void)cmac2;
  9086. printf(resultFmt, ret == 0 ? passed : failed);
  9087. #endif
  9088. return ret;
  9089. } /* END test_wc_InitCmac */
  9090. /*
  9091. * Testing wc_CmacUpdate()
  9092. */
  9093. static int test_wc_CmacUpdate (void)
  9094. {
  9095. int ret = 0;
  9096. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  9097. Cmac cmac;
  9098. byte key[] =
  9099. {
  9100. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  9101. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  9102. };
  9103. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  9104. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  9105. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  9106. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  9107. "\xf4";
  9108. word32 inSz = (word32)sizeof(in) - 1;
  9109. word32 keySz = (word32)sizeof(key);
  9110. int type = WC_CMAC_AES;
  9111. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  9112. if (ret != 0) {
  9113. return ret;
  9114. }
  9115. printf(testingFmt, "wc_CmacUpdate()");
  9116. ret = wc_CmacUpdate(&cmac, in, inSz);
  9117. /* Test bad args. */
  9118. if (ret == 0) {
  9119. ret = wc_CmacUpdate(NULL, in, inSz);
  9120. if (ret == BAD_FUNC_ARG) {
  9121. ret = wc_CmacUpdate(&cmac, NULL, 30);
  9122. }
  9123. if (ret == BAD_FUNC_ARG) {
  9124. ret = 0;
  9125. } else if (ret == 0) {
  9126. ret = WOLFSSL_FATAL_ERROR;
  9127. }
  9128. }
  9129. printf(resultFmt, ret == 0 ? passed : failed);
  9130. #endif
  9131. return ret;
  9132. } /* END test_wc_CmacUpdate */
  9133. /*
  9134. * Testing wc_CmacFinal()
  9135. */
  9136. static int test_wc_CmacFinal (void)
  9137. {
  9138. int ret = 0;
  9139. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  9140. Cmac cmac;
  9141. byte key[] =
  9142. {
  9143. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  9144. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  9145. };
  9146. byte msg[] =
  9147. {
  9148. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  9149. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  9150. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  9151. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  9152. 0xf4
  9153. };
  9154. /* Test vectors from CMACGenAES128.rsp from
  9155. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  9156. * Per RFC4493 truncation of lsb is possible.
  9157. */
  9158. byte expMac[] =
  9159. {
  9160. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  9161. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  9162. };
  9163. byte mac[AES_BLOCK_SIZE];
  9164. word32 msgSz = (word32)sizeof(msg);
  9165. word32 keySz = (word32)sizeof(key);
  9166. word32 macSz = sizeof(mac);
  9167. word32 badMacSz = 17;
  9168. int expMacSz = sizeof(expMac);
  9169. int type = WC_CMAC_AES;
  9170. XMEMSET(mac, 0, macSz);
  9171. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  9172. if (ret != 0) {
  9173. return ret;
  9174. }
  9175. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  9176. printf(testingFmt, "wc_CmacFinal()");
  9177. if (ret == 0) {
  9178. ret = wc_CmacFinal(&cmac, mac, &macSz);
  9179. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  9180. ret = WOLFSSL_FATAL_ERROR;
  9181. }
  9182. /* Pass in bad args. */
  9183. if (ret == 0) {
  9184. ret = wc_CmacFinal(NULL, mac, &macSz);
  9185. if (ret == BAD_FUNC_ARG) {
  9186. ret = wc_CmacFinal(&cmac, NULL, &macSz);
  9187. }
  9188. if (ret == BAD_FUNC_ARG) {
  9189. ret = wc_CmacFinal(&cmac, mac, &badMacSz);
  9190. if (ret == BUFFER_E) {
  9191. ret = 0;
  9192. }
  9193. } else if (ret == 0) {
  9194. ret = WOLFSSL_FATAL_ERROR;
  9195. }
  9196. }
  9197. }
  9198. printf(resultFmt, ret == 0 ? passed : failed);
  9199. #endif
  9200. return ret;
  9201. } /* END test_wc_CmacFinal */
  9202. /*
  9203. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  9204. */
  9205. static int test_wc_AesCmacGenerate (void)
  9206. {
  9207. int ret = 0;
  9208. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  9209. Cmac cmac;
  9210. byte key[] =
  9211. {
  9212. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  9213. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  9214. };
  9215. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  9216. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  9217. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  9218. "\x3d\x32\x65\x4c\x66\x23\xc5";
  9219. byte mac[AES_BLOCK_SIZE];
  9220. word32 keySz = sizeof(key);
  9221. word32 macSz = sizeof(mac);
  9222. word32 msgSz = sizeof(msg) - 1;
  9223. word32 expMacSz = sizeof(expMac) - 1;
  9224. int type = WC_CMAC_AES;
  9225. XMEMSET(mac, 0, macSz);
  9226. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  9227. if (ret != 0) {
  9228. return ret;
  9229. }
  9230. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  9231. if (ret != 0) {
  9232. return ret;
  9233. }
  9234. printf(testingFmt, "wc_AesCmacGenerate()");
  9235. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz);
  9236. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  9237. ret = WOLFSSL_FATAL_ERROR;
  9238. }
  9239. /* Pass in bad args. */
  9240. if (ret == 0) {
  9241. ret = wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz);
  9242. if (ret == BAD_FUNC_ARG) {
  9243. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz);
  9244. }
  9245. if (ret == BAD_FUNC_ARG) {
  9246. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0);
  9247. }
  9248. if (ret == BAD_FUNC_ARG) {
  9249. ret = wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz);
  9250. }
  9251. if (ret == BAD_FUNC_ARG) {
  9252. ret = 0;
  9253. } else if (ret == 0) {
  9254. ret = WOLFSSL_FATAL_ERROR;
  9255. }
  9256. }
  9257. printf(resultFmt, ret == 0 ? passed : failed);
  9258. if (ret == 0) {
  9259. printf(testingFmt, "wc_AesCmacVerify()");
  9260. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz);
  9261. /* Test bad args. */
  9262. if (ret == 0) {
  9263. ret = wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz);
  9264. if (ret == BAD_FUNC_ARG) {
  9265. ret = wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz);
  9266. }
  9267. if (ret == BAD_FUNC_ARG) {
  9268. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz);
  9269. }
  9270. if (ret == BAD_FUNC_ARG) {
  9271. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0);
  9272. }
  9273. if (ret == BAD_FUNC_ARG) {
  9274. ret = wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz);
  9275. }
  9276. if (ret == BAD_FUNC_ARG) {
  9277. ret = 0;
  9278. } else if (ret == 0) {
  9279. ret = WOLFSSL_FATAL_ERROR;
  9280. }
  9281. }
  9282. printf(resultFmt, ret == 0 ? passed : failed);
  9283. }
  9284. #endif
  9285. return ret;
  9286. } /* END test_wc_AesCmacGenerate */
  9287. /*
  9288. * unit test for wc_Des3_SetIV()
  9289. */
  9290. static int test_wc_Des3_SetIV (void)
  9291. {
  9292. int ret = 0;
  9293. #ifndef NO_DES3
  9294. Des3 des;
  9295. const byte key[] =
  9296. {
  9297. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  9298. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  9299. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  9300. };
  9301. const byte iv[] =
  9302. {
  9303. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  9304. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  9305. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  9306. };
  9307. printf(testingFmt, "wc_Des3_SetIV()");
  9308. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  9309. if (ret != 0)
  9310. return ret;
  9311. /* DES_ENCRYPTION or DES_DECRYPTION */
  9312. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  9313. if (ret == 0) {
  9314. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  9315. ret = WOLFSSL_FATAL_ERROR;
  9316. }
  9317. }
  9318. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  9319. /* Test explicitly wc_Des3_SetIV() */
  9320. if (ret == 0) {
  9321. ret = wc_Des3_SetIV(NULL, iv);
  9322. if (ret == BAD_FUNC_ARG) {
  9323. ret = wc_Des3_SetIV(&des, NULL);
  9324. } else if (ret == 0) {
  9325. ret = WOLFSSL_FATAL_ERROR;
  9326. }
  9327. }
  9328. #endif
  9329. wc_Des3Free(&des);
  9330. printf(resultFmt, ret == 0 ? passed : failed);
  9331. #endif
  9332. return ret;
  9333. } /* END test_wc_Des3_SetIV */
  9334. /*
  9335. * unit test for wc_Des3_SetKey()
  9336. */
  9337. static int test_wc_Des3_SetKey (void)
  9338. {
  9339. int ret = 0;
  9340. #ifndef NO_DES3
  9341. Des3 des;
  9342. const byte key[] =
  9343. {
  9344. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  9345. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  9346. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  9347. };
  9348. const byte iv[] =
  9349. {
  9350. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  9351. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  9352. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  9353. };
  9354. printf(testingFmt, "wc_Des3_SetKey()");
  9355. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  9356. if (ret != 0)
  9357. return ret;
  9358. /* DES_ENCRYPTION or DES_DECRYPTION */
  9359. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  9360. if (ret == 0) {
  9361. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  9362. ret = WOLFSSL_FATAL_ERROR;
  9363. }
  9364. }
  9365. /* Test bad args. */
  9366. if (ret == 0) {
  9367. ret = wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION);
  9368. if (ret == BAD_FUNC_ARG) {
  9369. ret = wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION);
  9370. }
  9371. if (ret == BAD_FUNC_ARG) {
  9372. ret = wc_Des3_SetKey(&des, key, iv, -1);
  9373. }
  9374. if (ret == BAD_FUNC_ARG) {
  9375. /* Default case. Should return 0. */
  9376. ret = wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION);
  9377. }
  9378. } /* END if ret != 0 */
  9379. wc_Des3Free(&des);
  9380. printf(resultFmt, ret == 0 ? passed : failed);
  9381. #endif
  9382. return ret;
  9383. } /* END test_wc_Des3_SetKey */
  9384. /*
  9385. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  9386. */
  9387. static int test_wc_Des3_CbcEncryptDecrypt (void)
  9388. {
  9389. int ret = 0;
  9390. #ifndef NO_DES3
  9391. Des3 des;
  9392. byte cipher[24];
  9393. byte plain[24];
  9394. const byte key[] =
  9395. {
  9396. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  9397. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  9398. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  9399. };
  9400. const byte iv[] =
  9401. {
  9402. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  9403. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  9404. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  9405. };
  9406. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  9407. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  9408. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  9409. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  9410. };
  9411. printf(testingFmt, "wc_Des3_CbcEncrypt()");
  9412. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  9413. if (ret != 0)
  9414. return ret;
  9415. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  9416. if (ret == 0) {
  9417. ret = wc_Des3_CbcEncrypt(&des, cipher, vector, 24);
  9418. if (ret == 0) {
  9419. ret = wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION);
  9420. }
  9421. if (ret == 0) {
  9422. ret = wc_Des3_CbcDecrypt(&des, plain, cipher, 24);
  9423. }
  9424. }
  9425. if (ret == 0) {
  9426. if (XMEMCMP(plain, vector, 24) != 0) {
  9427. ret = WOLFSSL_FATAL_ERROR;
  9428. }
  9429. }
  9430. /* Pass in bad args. */
  9431. if (ret == 0) {
  9432. ret = wc_Des3_CbcEncrypt(NULL, cipher, vector, 24);
  9433. if (ret == BAD_FUNC_ARG) {
  9434. ret = wc_Des3_CbcEncrypt(&des, NULL, vector, 24);
  9435. }
  9436. if (ret == BAD_FUNC_ARG) {
  9437. ret = wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector));
  9438. }
  9439. if (ret != BAD_FUNC_ARG) {
  9440. ret = WOLFSSL_FATAL_ERROR;
  9441. } else {
  9442. ret = 0;
  9443. }
  9444. }
  9445. if (ret == 0) {
  9446. ret = wc_Des3_CbcDecrypt(NULL, plain, cipher, 24);
  9447. if (ret == BAD_FUNC_ARG) {
  9448. ret = wc_Des3_CbcDecrypt(&des, NULL, cipher, 24);
  9449. }
  9450. if (ret == BAD_FUNC_ARG) {
  9451. ret = wc_Des3_CbcDecrypt(&des, plain, NULL, 24);
  9452. }
  9453. if (ret != BAD_FUNC_ARG) {
  9454. ret = WOLFSSL_FATAL_ERROR;
  9455. } else {
  9456. ret = 0;
  9457. }
  9458. }
  9459. wc_Des3Free(&des);
  9460. printf(resultFmt, ret == 0 ? passed : failed);
  9461. #endif
  9462. return ret;
  9463. } /* END wc_Des3_CbcEncrypt */
  9464. /*
  9465. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  9466. */
  9467. static int test_wc_Des3_CbcEncryptDecryptWithKey (void)
  9468. {
  9469. int ret = 0;
  9470. #ifndef NO_DES3
  9471. word32 vectorSz, cipherSz;
  9472. byte cipher[24];
  9473. byte plain[24];
  9474. byte vector[] = /* Now is the time for all w/o trailing 0 */
  9475. {
  9476. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  9477. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  9478. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  9479. };
  9480. byte key[] =
  9481. {
  9482. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  9483. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  9484. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  9485. };
  9486. byte iv[] =
  9487. {
  9488. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  9489. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  9490. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  9491. };
  9492. vectorSz = sizeof(byte) * 24;
  9493. cipherSz = sizeof(byte) * 24;
  9494. printf(testingFmt, "wc_Des3_CbcEncryptWithKey()");
  9495. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv);
  9496. if (ret == 0) {
  9497. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv);
  9498. if (ret == 0) {
  9499. if (XMEMCMP(plain, vector, 24) != 0) {
  9500. ret = WOLFSSL_FATAL_ERROR;
  9501. }
  9502. }
  9503. }
  9504. /* pass in bad args. */
  9505. if (ret == 0) {
  9506. ret = wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv);
  9507. if (ret == BAD_FUNC_ARG) {
  9508. ret = wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv);
  9509. }
  9510. if (ret == BAD_FUNC_ARG) {
  9511. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv);
  9512. }
  9513. if (ret == BAD_FUNC_ARG) {
  9514. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz,
  9515. key, NULL);
  9516. } else {
  9517. /* Return code catch. */
  9518. ret = WOLFSSL_FAILURE;
  9519. }
  9520. }
  9521. if (ret == 0) {
  9522. ret = wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv);
  9523. if (ret == BAD_FUNC_ARG) {
  9524. ret = wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv);
  9525. }
  9526. if (ret == BAD_FUNC_ARG) {
  9527. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv);
  9528. }
  9529. if (ret == BAD_FUNC_ARG) {
  9530. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL);
  9531. } else {
  9532. ret = WOLFSSL_FAILURE;
  9533. }
  9534. }
  9535. printf(resultFmt, ret == 0 ? passed : failed);
  9536. #endif
  9537. return ret;
  9538. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  9539. /*
  9540. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  9541. */
  9542. static int test_wc_Chacha_SetKey (void)
  9543. {
  9544. int ret = 0;
  9545. #ifdef HAVE_CHACHA
  9546. ChaCha ctx;
  9547. const byte key[] =
  9548. {
  9549. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  9550. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  9551. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  9552. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  9553. };
  9554. byte cipher[128];
  9555. printf(testingFmt, "wc_Chacha_SetKey()");
  9556. ret = wc_Chacha_SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  9557. /* Test bad args. */
  9558. if (ret == 0) {
  9559. ret = wc_Chacha_SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  9560. if (ret == BAD_FUNC_ARG) {
  9561. ret = wc_Chacha_SetKey(&ctx, key, 18);
  9562. }
  9563. if (ret == BAD_FUNC_ARG) {
  9564. ret = 0;
  9565. } else {
  9566. ret = WOLFSSL_FATAL_ERROR;
  9567. }
  9568. }
  9569. printf(resultFmt, ret == 0 ? passed : failed);
  9570. if (ret != 0) {
  9571. return ret;
  9572. }
  9573. printf(testingFmt, "wc_Chacha_SetIV");
  9574. ret = wc_Chacha_SetIV(&ctx, cipher, 0);
  9575. if (ret == 0) {
  9576. /* Test bad args. */
  9577. ret = wc_Chacha_SetIV(NULL, cipher, 0);
  9578. if (ret == BAD_FUNC_ARG) {
  9579. ret = 0;
  9580. } else {
  9581. ret = WOLFSSL_FAILURE;
  9582. }
  9583. }
  9584. printf(resultFmt, ret == 0 ? passed : failed);
  9585. #endif
  9586. return ret;
  9587. } /* END test_wc_Chacha_SetKey */
  9588. /*
  9589. * unit test for wc_Poly1305SetKey()
  9590. */
  9591. static int test_wc_Poly1305SetKey(void)
  9592. {
  9593. int ret = 0;
  9594. #ifdef HAVE_POLY1305
  9595. Poly1305 ctx;
  9596. const byte key[] =
  9597. {
  9598. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  9599. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  9600. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  9601. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  9602. };
  9603. printf(testingFmt, "wc_Poly1305_SetKey()");
  9604. ret = wc_Poly1305SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  9605. /* Test bad args. */
  9606. if (ret == 0) {
  9607. ret = wc_Poly1305SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  9608. if(ret == BAD_FUNC_ARG) {
  9609. ret = wc_Poly1305SetKey(&ctx, NULL, (word32)(sizeof(key)/sizeof(byte)));
  9610. }
  9611. if (ret == BAD_FUNC_ARG) {
  9612. ret = wc_Poly1305SetKey(&ctx, key, 18);
  9613. }
  9614. if (ret == BAD_FUNC_ARG) {
  9615. ret = 0;
  9616. } else {
  9617. ret = WOLFSSL_FATAL_ERROR;
  9618. }
  9619. }
  9620. printf(resultFmt, ret == 0 ? passed : failed);
  9621. #endif
  9622. return ret;
  9623. } /* END test_wc_Poly1305_SetKey() */
  9624. /*
  9625. * Testing wc_Chacha_Process()
  9626. */
  9627. static int test_wc_Chacha_Process (void)
  9628. {
  9629. int ret = 0;
  9630. #ifdef HAVE_CHACHA
  9631. ChaCha enc, dec;
  9632. byte cipher[128];
  9633. byte plain[128];
  9634. const byte key[] =
  9635. {
  9636. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  9637. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  9638. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  9639. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  9640. };
  9641. const char* input = "Everybody gets Friday off.";
  9642. word32 keySz = sizeof(key)/sizeof(byte);
  9643. unsigned long int inlen = XSTRLEN(input);
  9644. /*Initialize stack varialbes.*/
  9645. XMEMSET(cipher, 0, 128);
  9646. XMEMSET(plain, 0, 128);
  9647. printf(testingFmt, "wc_Chacha_Process()");
  9648. ret = wc_Chacha_SetKey(&enc, key, keySz);
  9649. AssertIntEQ(ret, 0);
  9650. ret = wc_Chacha_SetKey(&dec, key, keySz);
  9651. AssertIntEQ(ret, 0);
  9652. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  9653. AssertIntEQ(ret, 0);
  9654. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  9655. AssertIntEQ(ret, 0);
  9656. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen);
  9657. AssertIntEQ(ret, 0);
  9658. ret = wc_Chacha_Process(&dec, plain, cipher, (word32)inlen);
  9659. AssertIntEQ(ret, 0);
  9660. ret = XMEMCMP(input, plain, (int)inlen);
  9661. AssertIntEQ(ret, 0);
  9662. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  9663. /* test checking and using leftovers, currently just in C code */
  9664. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  9665. AssertIntEQ(ret, 0);
  9666. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  9667. AssertIntEQ(ret, 0);
  9668. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen - 2);
  9669. AssertIntEQ(ret, 0);
  9670. ret = wc_Chacha_Process(&enc, cipher + (inlen - 2),
  9671. (byte*)input + (inlen - 2), 2);
  9672. AssertIntEQ(ret, 0);
  9673. ret = wc_Chacha_Process(&dec, plain, (byte*)cipher, (word32)inlen - 2);
  9674. AssertIntEQ(ret, 0);
  9675. ret = wc_Chacha_Process(&dec, cipher + (inlen - 2),
  9676. (byte*)input + (inlen - 2), 2);
  9677. AssertIntEQ(ret, 0);
  9678. ret = XMEMCMP(input, plain, (int)inlen);
  9679. AssertIntEQ(ret, 0);
  9680. /* check edge cases with counter increment */
  9681. {
  9682. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  9683. const byte expected[] = {
  9684. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  9685. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  9686. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  9687. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  9688. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  9689. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  9690. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  9691. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  9692. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  9693. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  9694. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  9695. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  9696. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  9697. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  9698. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  9699. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  9700. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  9701. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  9702. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  9703. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  9704. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  9705. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  9706. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  9707. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  9708. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  9709. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  9710. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  9711. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  9712. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  9713. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  9714. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  9715. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  9716. };
  9717. const byte iv2[] = {
  9718. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  9719. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  9720. };
  9721. byte input2[256];
  9722. int i;
  9723. for (i = 0; i < 256; i++)
  9724. input2[i] = i;
  9725. ret = wc_Chacha_SetIV(&enc, iv2, 0);
  9726. AssertIntEQ(ret, 0);
  9727. ret = wc_Chacha_Process(&enc, cipher, input2, 64);
  9728. AssertIntEQ(ret, 0);
  9729. AssertIntEQ(XMEMCMP(expected, cipher, 64), 0);
  9730. ret = wc_Chacha_Process(&enc, cipher, input2 + 64, 128);
  9731. AssertIntEQ(ret, 0);
  9732. AssertIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  9733. /* partial */
  9734. ret = wc_Chacha_Process(&enc, cipher, input2 + 192, 32);
  9735. AssertIntEQ(ret, 0);
  9736. AssertIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  9737. ret = wc_Chacha_Process(&enc, cipher, input2 + 224, 32);
  9738. AssertIntEQ(ret, 0);
  9739. AssertIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  9740. }
  9741. #endif
  9742. /* Test bad args. */
  9743. ret = wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen);
  9744. AssertIntEQ(ret, BAD_FUNC_ARG);
  9745. if (ret == BAD_FUNC_ARG) {
  9746. ret = 0;
  9747. }
  9748. printf(resultFmt, ret == 0 ? passed : failed);
  9749. #endif
  9750. return ret;
  9751. } /* END test_wc_Chacha_Process */
  9752. /*
  9753. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  9754. */
  9755. static int test_wc_ChaCha20Poly1305_aead (void)
  9756. {
  9757. int ret = 0;
  9758. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  9759. const byte key[] = {
  9760. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  9761. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  9762. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  9763. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  9764. };
  9765. const byte plaintext[] = {
  9766. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  9767. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  9768. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  9769. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  9770. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  9771. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  9772. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  9773. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  9774. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  9775. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  9776. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  9777. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  9778. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  9779. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  9780. 0x74, 0x2e
  9781. };
  9782. const byte iv[] = {
  9783. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  9784. 0x44, 0x45, 0x46, 0x47
  9785. };
  9786. const byte aad[] = { /* additional data */
  9787. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  9788. 0xc4, 0xc5, 0xc6, 0xc7
  9789. };
  9790. const byte cipher[] = { /* expected output from operation */
  9791. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  9792. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  9793. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  9794. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  9795. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  9796. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  9797. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  9798. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  9799. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  9800. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  9801. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  9802. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  9803. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  9804. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  9805. 0x61, 0x16
  9806. };
  9807. const byte authTag[] = { /* expected output from operation */
  9808. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  9809. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  9810. };
  9811. byte generatedCiphertext[272];
  9812. byte generatedPlaintext[272];
  9813. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  9814. /* Initialize stack variables. */
  9815. XMEMSET(generatedCiphertext, 0, 272);
  9816. XMEMSET(generatedPlaintext, 0, 272);
  9817. /* Test Encrypt */
  9818. printf(testingFmt, "wc_ChaCha20Poly1305_Encrypt()");
  9819. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), plaintext,
  9820. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  9821. AssertIntEQ(ret, 0);
  9822. ret = XMEMCMP(generatedCiphertext, cipher, sizeof(cipher)/sizeof(byte));
  9823. AssertIntEQ(ret, 0);
  9824. /* Test bad args. */
  9825. ret = wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad), plaintext,
  9826. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  9827. AssertIntEQ(ret, BAD_FUNC_ARG);
  9828. ret = wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  9829. plaintext, sizeof(plaintext),
  9830. generatedCiphertext, generatedAuthTag);
  9831. AssertIntEQ(ret, BAD_FUNC_ARG);
  9832. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  9833. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  9834. AssertIntEQ(ret, BAD_FUNC_ARG);
  9835. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  9836. plaintext, 0, generatedCiphertext, generatedAuthTag);
  9837. AssertIntEQ(ret, BAD_FUNC_ARG);
  9838. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  9839. plaintext, sizeof(plaintext), NULL, generatedAuthTag);
  9840. AssertIntEQ(ret, BAD_FUNC_ARG);
  9841. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  9842. plaintext, sizeof(plaintext), generatedCiphertext, NULL);
  9843. if (ret == BAD_FUNC_ARG) {
  9844. ret = 0;
  9845. }
  9846. printf(resultFmt, ret == 0 ? passed : failed);
  9847. if (ret != 0) {
  9848. return ret;
  9849. }
  9850. printf(testingFmt, "wc_ChaCha20Poly1305_Decrypt()");
  9851. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  9852. sizeof(cipher), authTag, generatedPlaintext);
  9853. AssertIntEQ(ret, 0);
  9854. ret = XMEMCMP(generatedPlaintext, plaintext,
  9855. sizeof(plaintext)/sizeof(byte));
  9856. AssertIntEQ(ret, 0);
  9857. /* Test bad args. */
  9858. ret = wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  9859. sizeof(cipher), authTag, generatedPlaintext);
  9860. AssertIntEQ(ret, BAD_FUNC_ARG);
  9861. ret = wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  9862. cipher, sizeof(cipher), authTag, generatedPlaintext);
  9863. AssertIntEQ(ret, BAD_FUNC_ARG);
  9864. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  9865. sizeof(cipher), authTag, generatedPlaintext);
  9866. AssertIntEQ(ret, BAD_FUNC_ARG);
  9867. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  9868. sizeof(cipher), NULL, generatedPlaintext);
  9869. AssertIntEQ(ret, BAD_FUNC_ARG);
  9870. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  9871. sizeof(cipher), authTag, NULL);
  9872. AssertIntEQ(ret, BAD_FUNC_ARG);
  9873. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  9874. 0, authTag, generatedPlaintext);
  9875. AssertIntEQ(ret, BAD_FUNC_ARG);
  9876. if (ret == BAD_FUNC_ARG) {
  9877. ret = 0;
  9878. }
  9879. printf(resultFmt, ret == 0 ? passed : failed);
  9880. #endif
  9881. return ret;
  9882. } /* END test-wc_ChaCha20Poly1305_EncryptDecrypt */
  9883. /*
  9884. * Testing function for wc_AesSetIV
  9885. */
  9886. static int test_wc_AesSetIV (void)
  9887. {
  9888. int ret = 0;
  9889. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  9890. Aes aes;
  9891. byte key16[] =
  9892. {
  9893. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  9894. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  9895. };
  9896. byte iv1[] = "1234567890abcdef";
  9897. byte iv2[] = "0987654321fedcba";
  9898. printf(testingFmt, "wc_AesSetIV()");
  9899. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  9900. if (ret != 0)
  9901. return ret;
  9902. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  9903. iv1, AES_ENCRYPTION);
  9904. if(ret == 0) {
  9905. ret = wc_AesSetIV(&aes, iv2);
  9906. }
  9907. /* Test bad args. */
  9908. if(ret == 0) {
  9909. ret = wc_AesSetIV(NULL, iv1);
  9910. if(ret == BAD_FUNC_ARG) {
  9911. /* NULL iv should return 0. */
  9912. ret = wc_AesSetIV(&aes, NULL);
  9913. } else {
  9914. ret = WOLFSSL_FATAL_ERROR;
  9915. }
  9916. }
  9917. wc_AesFree(&aes);
  9918. printf(resultFmt, ret == 0 ? passed : failed);
  9919. #endif
  9920. return ret;
  9921. } /* test_wc_AesSetIV */
  9922. /*
  9923. * Testing function for wc_AesSetKey().
  9924. */
  9925. static int test_wc_AesSetKey (void)
  9926. {
  9927. int ret = 0;
  9928. #ifndef NO_AES
  9929. Aes aes;
  9930. byte key16[] =
  9931. {
  9932. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  9933. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  9934. };
  9935. #ifdef WOLFSSL_AES_192
  9936. byte key24[] =
  9937. {
  9938. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  9939. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  9940. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  9941. };
  9942. #endif
  9943. #ifdef WOLFSSL_AES_256
  9944. byte key32[] =
  9945. {
  9946. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  9947. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  9948. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  9949. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  9950. };
  9951. #endif
  9952. byte badKey16[] =
  9953. {
  9954. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  9955. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  9956. };
  9957. byte iv[] = "1234567890abcdef";
  9958. printf(testingFmt, "wc_AesSetKey()");
  9959. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  9960. if (ret != 0)
  9961. return ret;
  9962. #ifdef WOLFSSL_AES_128
  9963. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  9964. iv, AES_ENCRYPTION);
  9965. #endif
  9966. #ifdef WOLFSSL_AES_192
  9967. if (ret == 0) {
  9968. ret = wc_AesSetKey (&aes, key24, (word32) sizeof(key24) / sizeof(byte),
  9969. iv, AES_ENCRYPTION);
  9970. }
  9971. #endif
  9972. #ifdef WOLFSSL_AES_256
  9973. if (ret == 0) {
  9974. ret = wc_AesSetKey (&aes, key32, (word32) sizeof(key32) / sizeof(byte),
  9975. iv, AES_ENCRYPTION);
  9976. }
  9977. #endif
  9978. /* Pass in bad args. */
  9979. if (ret == 0) {
  9980. ret = wc_AesSetKey (NULL, key16, (word32) sizeof(key16) / sizeof(byte),
  9981. iv, AES_ENCRYPTION);
  9982. if (ret == BAD_FUNC_ARG) {
  9983. ret = wc_AesSetKey(&aes, badKey16,
  9984. (word32) sizeof(badKey16) / sizeof(byte),
  9985. iv, AES_ENCRYPTION);
  9986. }
  9987. if (ret == BAD_FUNC_ARG) {
  9988. ret = 0;
  9989. } else {
  9990. ret = WOLFSSL_FATAL_ERROR;
  9991. }
  9992. }
  9993. wc_AesFree(&aes);
  9994. printf(resultFmt, ret == 0 ? passed : failed);
  9995. #endif
  9996. return ret;
  9997. } /* END test_wc_AesSetKey */
  9998. /*
  9999. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  10000. * and wc_AesCbcDecryptWithKey()
  10001. */
  10002. static int test_wc_AesCbcEncryptDecrypt (void)
  10003. {
  10004. int ret = 0;
  10005. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  10006. defined(WOLFSSL_AES_256)
  10007. Aes aes;
  10008. byte key32[] =
  10009. {
  10010. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10011. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  10012. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10013. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  10014. };
  10015. byte vector[] = /* Now is the time for all w/o trailing 0 */
  10016. {
  10017. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  10018. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  10019. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  10020. };
  10021. byte iv[] = "1234567890abcdef";
  10022. byte enc[sizeof(vector)];
  10023. byte dec[sizeof(vector)];
  10024. int cbcE = WOLFSSL_FATAL_ERROR;
  10025. int cbcD = WOLFSSL_FATAL_ERROR;
  10026. int cbcDWK = WOLFSSL_FATAL_ERROR;
  10027. byte dec2[sizeof(vector)];
  10028. /* Init stack variables. */
  10029. XMEMSET(enc, 0, sizeof(enc));
  10030. XMEMSET(dec, 0, sizeof(vector));
  10031. XMEMSET(dec2, 0, sizeof(vector));
  10032. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  10033. if (ret != 0)
  10034. return ret;
  10035. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv, AES_ENCRYPTION);
  10036. if (ret == 0) {
  10037. ret = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector));
  10038. if (ret == 0) {
  10039. /* Re init for decrypt and set flag. */
  10040. cbcE = 0;
  10041. wc_AesFree(&aes);
  10042. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2,
  10043. iv, AES_DECRYPTION);
  10044. }
  10045. if (ret == 0) {
  10046. ret = wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE);
  10047. if (ret != 0 || XMEMCMP(vector, dec, AES_BLOCK_SIZE) != 0) {
  10048. ret = WOLFSSL_FATAL_ERROR;
  10049. } else {
  10050. /* Set flag. */
  10051. cbcD = 0;
  10052. }
  10053. }
  10054. }
  10055. /* If encrypt succeeds but cbc decrypt fails, we can still test. */
  10056. if (ret == 0 || cbcE == 0) {
  10057. ret = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  10058. key32, sizeof(key32)/sizeof(byte), iv);
  10059. if (ret == 0 || XMEMCMP(vector, dec2, AES_BLOCK_SIZE) == 0) {
  10060. cbcDWK = 0;
  10061. }
  10062. }
  10063. printf(testingFmt, "wc_AesCbcEncrypt()");
  10064. /* Pass in bad args */
  10065. if (cbcE == 0) {
  10066. cbcE = wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector));
  10067. if (cbcE == BAD_FUNC_ARG) {
  10068. cbcE = wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector));
  10069. }
  10070. if (cbcE == BAD_FUNC_ARG) {
  10071. cbcE = wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector));
  10072. }
  10073. if (cbcE == BAD_FUNC_ARG) {
  10074. cbcE = 0;
  10075. } else {
  10076. cbcE = WOLFSSL_FATAL_ERROR;
  10077. }
  10078. }
  10079. printf(resultFmt, cbcE == 0 ? passed : failed);
  10080. if (cbcE != 0) {
  10081. wc_AesFree(&aes);
  10082. return cbcE;
  10083. }
  10084. printf(testingFmt, "wc_AesCbcDecrypt()");
  10085. if (cbcD == 0) {
  10086. cbcD = wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE);
  10087. if (cbcD == BAD_FUNC_ARG) {
  10088. cbcD = wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE);
  10089. }
  10090. if (cbcD == BAD_FUNC_ARG) {
  10091. cbcD = wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE);
  10092. }
  10093. if (cbcD == BAD_FUNC_ARG) {
  10094. cbcD = wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1);
  10095. }
  10096. if (cbcD == BAD_FUNC_ARG) {
  10097. cbcD = 0;
  10098. } else {
  10099. cbcD = WOLFSSL_FATAL_ERROR;
  10100. }
  10101. }
  10102. printf(resultFmt, cbcD == 0 ? passed : failed);
  10103. if (cbcD != 0) {
  10104. wc_AesFree(&aes);
  10105. return cbcD;
  10106. }
  10107. printf(testingFmt, "wc_AesCbcDecryptWithKey()");
  10108. if (cbcDWK == 0) {
  10109. cbcDWK = wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  10110. key32, sizeof(key32)/sizeof(byte), iv);
  10111. if (cbcDWK == BAD_FUNC_ARG) {
  10112. cbcDWK = wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  10113. key32, sizeof(key32)/sizeof(byte), iv);
  10114. }
  10115. if (cbcDWK == BAD_FUNC_ARG) {
  10116. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  10117. NULL, sizeof(key32)/sizeof(byte), iv);
  10118. }
  10119. if (cbcDWK == BAD_FUNC_ARG) {
  10120. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  10121. key32, sizeof(key32)/sizeof(byte), NULL);
  10122. }
  10123. if (cbcDWK == BAD_FUNC_ARG) {
  10124. cbcDWK = 0;
  10125. } else {
  10126. cbcDWK = WOLFSSL_FATAL_ERROR;
  10127. }
  10128. }
  10129. wc_AesFree(&aes);
  10130. printf(resultFmt, cbcDWK == 0 ? passed : failed);
  10131. if (cbcDWK != 0) {
  10132. return cbcDWK;
  10133. }
  10134. #endif
  10135. return ret;
  10136. } /* END test_wc_AesCbcEncryptDecrypt */
  10137. /*
  10138. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  10139. */
  10140. static int test_wc_AesCtrEncryptDecrypt (void)
  10141. {
  10142. int ret = 0;
  10143. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  10144. Aes aesEnc, aesDec;
  10145. byte key32[] =
  10146. {
  10147. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10148. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  10149. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10150. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  10151. };
  10152. byte vector[] = /* Now is the time for all w/o trailing 0 */
  10153. {
  10154. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  10155. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  10156. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  10157. };
  10158. byte iv[] = "1234567890abcdef";
  10159. byte enc[AES_BLOCK_SIZE * 2];
  10160. byte dec[AES_BLOCK_SIZE * 2];
  10161. /* Init stack variables. */
  10162. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  10163. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  10164. printf(testingFmt, "wc_AesCtrEncrypt()");
  10165. ret = wc_AesInit(&aesEnc, NULL, INVALID_DEVID);
  10166. if (ret != 0)
  10167. return ret;
  10168. ret = wc_AesInit(&aesDec, NULL, INVALID_DEVID);
  10169. if (ret != 0) {
  10170. wc_AesFree(&aesEnc);
  10171. return ret;
  10172. }
  10173. ret = wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2,
  10174. iv, AES_ENCRYPTION);
  10175. if (ret == 0) {
  10176. ret = wc_AesCtrEncrypt(&aesEnc, enc, vector,
  10177. sizeof(vector)/sizeof(byte));
  10178. if (ret == 0) {
  10179. /* Decrypt with wc_AesCtrEncrypt() */
  10180. ret = wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2,
  10181. iv, AES_ENCRYPTION);
  10182. }
  10183. if (ret == 0) {
  10184. ret = wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte));
  10185. if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector))) {
  10186. ret = WOLFSSL_FATAL_ERROR;
  10187. }
  10188. }
  10189. }
  10190. /* Test bad args. */
  10191. if (ret == 0) {
  10192. ret = wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte));
  10193. if (ret == BAD_FUNC_ARG) {
  10194. ret = wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte));
  10195. }
  10196. if (ret == BAD_FUNC_ARG) {
  10197. ret = wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte));
  10198. }
  10199. if (ret == BAD_FUNC_ARG) {
  10200. ret = 0;
  10201. } else {
  10202. ret = WOLFSSL_FATAL_ERROR;
  10203. }
  10204. }
  10205. wc_AesFree(&aesEnc);
  10206. wc_AesFree(&aesDec);
  10207. printf(resultFmt, ret == 0 ? passed : failed);
  10208. #endif
  10209. return ret;
  10210. } /* END test_wc_AesCtrEncryptDecrypt */
  10211. /*
  10212. * test function for wc_AesGcmSetKey()
  10213. */
  10214. static int test_wc_AesGcmSetKey (void)
  10215. {
  10216. int ret = 0;
  10217. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  10218. Aes aes;
  10219. #ifdef WOLFSSL_AES_128
  10220. byte key16[] =
  10221. {
  10222. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10223. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  10224. };
  10225. #endif
  10226. #ifdef WOLFSSL_AES_192
  10227. byte key24[] =
  10228. {
  10229. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10230. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  10231. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  10232. };
  10233. #endif
  10234. #ifdef WOLFSSL_AES_256
  10235. byte key32[] =
  10236. {
  10237. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10238. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  10239. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10240. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  10241. };
  10242. #endif
  10243. byte badKey16[] =
  10244. {
  10245. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10246. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  10247. };
  10248. byte badKey24[] =
  10249. {
  10250. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10251. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  10252. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  10253. };
  10254. byte badKey32[] =
  10255. {
  10256. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  10257. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  10258. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10259. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  10260. };
  10261. printf(testingFmt, "wc_AesGcmSetKey()");
  10262. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  10263. if (ret != 0)
  10264. return ret;
  10265. #ifdef WOLFSSL_AES_128
  10266. ret = wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte));
  10267. #endif
  10268. #ifdef WOLFSSL_AES_192
  10269. if (ret == 0) {
  10270. ret = wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte));
  10271. }
  10272. #endif
  10273. #ifdef WOLFSSL_AES_256
  10274. if (ret == 0) {
  10275. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  10276. }
  10277. #endif
  10278. /* Pass in bad args. */
  10279. if (ret == 0) {
  10280. ret = wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte));
  10281. if (ret == BAD_FUNC_ARG) {
  10282. ret = wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte));
  10283. }
  10284. if (ret == BAD_FUNC_ARG) {
  10285. ret = wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte));
  10286. }
  10287. if (ret == BAD_FUNC_ARG) {
  10288. ret = 0;
  10289. } else {
  10290. ret = WOLFSSL_FATAL_ERROR;
  10291. }
  10292. }
  10293. wc_AesFree(&aes);
  10294. printf(resultFmt, ret == 0 ? passed : failed);
  10295. #endif
  10296. return ret;
  10297. } /* END test_wc_AesGcmSetKey */
  10298. /*
  10299. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  10300. */
  10301. static int test_wc_AesGcmEncryptDecrypt (void)
  10302. {
  10303. int ret = 0;
  10304. /* WOLFSSL_AFALG requires 12 byte IV */
  10305. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  10306. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  10307. Aes aes;
  10308. byte key32[] =
  10309. {
  10310. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10311. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  10312. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10313. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  10314. };
  10315. byte vector[] = /* Now is the time for all w/o trailing 0 */
  10316. {
  10317. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  10318. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  10319. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  10320. };
  10321. const byte a[] =
  10322. {
  10323. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  10324. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  10325. 0xab, 0xad, 0xda, 0xd2
  10326. };
  10327. byte iv[] = "1234567890a";
  10328. byte longIV[] = "1234567890abcdefghij";
  10329. byte enc[sizeof(vector)];
  10330. byte resultT[AES_BLOCK_SIZE];
  10331. byte dec[sizeof(vector)];
  10332. int gcmD = WOLFSSL_FATAL_ERROR;
  10333. int gcmE = WOLFSSL_FATAL_ERROR;
  10334. /* Init stack variables. */
  10335. XMEMSET(enc, 0, sizeof(vector));
  10336. XMEMSET(dec, 0, sizeof(vector));
  10337. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  10338. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  10339. if (ret != 0)
  10340. return ret;
  10341. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  10342. if (ret == 0) {
  10343. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector),
  10344. iv, sizeof(iv)/sizeof(byte), resultT,
  10345. sizeof(resultT), a, sizeof(a));
  10346. }
  10347. if (gcmE == 0) { /* If encrypt fails, no decrypt. */
  10348. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector),
  10349. iv, sizeof(iv)/sizeof(byte), resultT,
  10350. sizeof(resultT), a, sizeof(a));
  10351. if(gcmD == 0 && (XMEMCMP(vector, dec, sizeof(vector)) != 0)) {
  10352. gcmD = WOLFSSL_FATAL_ERROR;
  10353. }
  10354. }
  10355. printf(testingFmt, "wc_AesGcmEncrypt()");
  10356. /*Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  10357. if (gcmE == 0) {
  10358. gcmE = wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector),
  10359. iv, sizeof(iv)/sizeof(byte), resultT, sizeof(resultT),
  10360. a, sizeof(a));
  10361. if (gcmE == BAD_FUNC_ARG) {
  10362. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  10363. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  10364. resultT, sizeof(resultT) + 1, a, sizeof(a));
  10365. }
  10366. if (gcmE == BAD_FUNC_ARG) {
  10367. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  10368. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  10369. resultT, sizeof(resultT) - 5, a, sizeof(a));
  10370. }
  10371. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  10372. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)
  10373. /* FIPS does not check the lower bound of ivSz */
  10374. #else
  10375. if (gcmE == BAD_FUNC_ARG) {
  10376. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  10377. sizeof(vector), iv, 0,
  10378. resultT, sizeof(resultT), a, sizeof(a));
  10379. }
  10380. #endif
  10381. if (gcmE == BAD_FUNC_ARG) {
  10382. gcmE = 0;
  10383. } else {
  10384. gcmE = WOLFSSL_FATAL_ERROR;
  10385. }
  10386. }
  10387. /* This case is now considered good. Long IVs are now allowed.
  10388. * Except for the original FIPS release, it still has an upper
  10389. * bound on the IV length. */
  10390. #if !defined(HAVE_FIPS) || \
  10391. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  10392. if (gcmE == 0) {
  10393. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  10394. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT),
  10395. a, sizeof(a));
  10396. }
  10397. #else
  10398. (void)longIV;
  10399. #endif /* Old FIPS */
  10400. /* END wc_AesGcmEncrypt */
  10401. printf(resultFmt, gcmE == 0 ? passed : failed);
  10402. if (gcmE != 0) {
  10403. wc_AesFree(&aes);
  10404. return gcmE;
  10405. }
  10406. #ifdef HAVE_AES_DECRYPT
  10407. printf(testingFmt, "wc_AesGcmDecrypt()");
  10408. if (gcmD == 0) {
  10409. gcmD = wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte),
  10410. iv, sizeof(iv)/sizeof(byte), resultT,
  10411. sizeof(resultT), a, sizeof(a));
  10412. if (gcmD == BAD_FUNC_ARG) {
  10413. gcmD = wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte),
  10414. iv, sizeof(iv)/sizeof(byte), resultT,
  10415. sizeof(resultT), a, sizeof(a));
  10416. }
  10417. if (gcmD == BAD_FUNC_ARG) {
  10418. gcmD = wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte),
  10419. iv, sizeof(iv)/sizeof(byte), resultT,
  10420. sizeof(resultT), a, sizeof(a));
  10421. }
  10422. if (gcmD == BAD_FUNC_ARG) {
  10423. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  10424. NULL, sizeof(iv)/sizeof(byte), resultT,
  10425. sizeof(resultT), a, sizeof(a));
  10426. }
  10427. if (gcmD == BAD_FUNC_ARG) {
  10428. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  10429. iv, sizeof(iv)/sizeof(byte), NULL,
  10430. sizeof(resultT), a, sizeof(a));
  10431. }
  10432. if (gcmD == BAD_FUNC_ARG) {
  10433. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  10434. iv, sizeof(iv)/sizeof(byte), resultT,
  10435. sizeof(resultT) + 1, a, sizeof(a));
  10436. }
  10437. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  10438. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)
  10439. /* FIPS does not check the lower bound of ivSz */
  10440. #else
  10441. if (gcmD == BAD_FUNC_ARG) {
  10442. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  10443. iv, 0, resultT,
  10444. sizeof(resultT), a, sizeof(a));
  10445. }
  10446. #endif
  10447. if (gcmD == BAD_FUNC_ARG) {
  10448. gcmD = 0;
  10449. } else {
  10450. gcmD = WOLFSSL_FATAL_ERROR;
  10451. }
  10452. } /* END wc_AesGcmDecrypt */
  10453. printf(resultFmt, gcmD == 0 ? passed : failed);
  10454. #endif /* HAVE_AES_DECRYPT */
  10455. wc_AesFree(&aes);
  10456. #endif
  10457. return ret;
  10458. } /* END test_wc_AesGcmEncryptDecrypt */
  10459. /*
  10460. * unit test for wc_GmacSetKey()
  10461. */
  10462. static int test_wc_GmacSetKey (void)
  10463. {
  10464. int ret = 0;
  10465. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  10466. Gmac gmac;
  10467. byte key16[] =
  10468. {
  10469. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10470. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  10471. };
  10472. #ifdef WOLFSSL_AES_192
  10473. byte key24[] =
  10474. {
  10475. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10476. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  10477. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  10478. };
  10479. #endif
  10480. #ifdef WOLFSSL_AES_256
  10481. byte key32[] =
  10482. {
  10483. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10484. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  10485. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10486. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  10487. };
  10488. #endif
  10489. byte badKey16[] =
  10490. {
  10491. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10492. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  10493. };
  10494. byte badKey24[] =
  10495. {
  10496. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  10497. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  10498. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  10499. };
  10500. byte badKey32[] =
  10501. {
  10502. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10503. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  10504. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10505. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  10506. };
  10507. printf(testingFmt, "wc_GmacSetKey()");
  10508. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  10509. if (ret != 0)
  10510. return ret;
  10511. #ifdef WOLFSSL_AES_128
  10512. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte));
  10513. #endif
  10514. #ifdef WOLFSSL_AES_192
  10515. if (ret == 0) {
  10516. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  10517. }
  10518. #endif
  10519. #ifdef WOLFSSL_AES_256
  10520. if (ret == 0) {
  10521. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  10522. }
  10523. #endif
  10524. /* Pass in bad args. */
  10525. if (ret == 0) {
  10526. ret = wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte));
  10527. if (ret == BAD_FUNC_ARG) {
  10528. ret = wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte));
  10529. }
  10530. if (ret == BAD_FUNC_ARG) {
  10531. ret = wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte));
  10532. }
  10533. if (ret == BAD_FUNC_ARG) {
  10534. ret = wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte));
  10535. }
  10536. if (ret == BAD_FUNC_ARG) {
  10537. ret = wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte));
  10538. }
  10539. if (ret == BAD_FUNC_ARG) {
  10540. ret = 0;
  10541. } else {
  10542. ret = WOLFSSL_FATAL_ERROR;
  10543. }
  10544. }
  10545. wc_AesFree(&gmac.aes);
  10546. printf(resultFmt, ret == 0 ? passed : failed);
  10547. #endif
  10548. return ret;
  10549. } /* END test_wc_GmacSetKey */
  10550. /*
  10551. * unit test for wc_GmacUpdate
  10552. */
  10553. static int test_wc_GmacUpdate (void)
  10554. {
  10555. int ret = 0;
  10556. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  10557. Gmac gmac;
  10558. #ifdef WOLFSSL_AES_128
  10559. const byte key16[] =
  10560. {
  10561. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  10562. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  10563. };
  10564. #endif
  10565. #ifdef WOLFSSL_AES_192
  10566. byte key24[] =
  10567. {
  10568. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  10569. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  10570. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  10571. };
  10572. #endif
  10573. #ifdef WOLFSSL_AES_256
  10574. byte key32[] =
  10575. {
  10576. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  10577. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  10578. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  10579. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  10580. };
  10581. #endif
  10582. #ifdef WOLFSSL_AES_128
  10583. const byte authIn[] =
  10584. {
  10585. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  10586. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  10587. };
  10588. #endif
  10589. #ifdef WOLFSSL_AES_192
  10590. const byte authIn2[] =
  10591. {
  10592. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  10593. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  10594. };
  10595. #endif
  10596. const byte authIn3[] =
  10597. {
  10598. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  10599. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  10600. };
  10601. #ifdef WOLFSSL_AES_128
  10602. const byte tag1[] = /* Known. */
  10603. {
  10604. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  10605. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  10606. };
  10607. #endif
  10608. #ifdef WOLFSSL_AES_192
  10609. const byte tag2[] = /* Known */
  10610. {
  10611. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  10612. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  10613. };
  10614. #endif
  10615. const byte tag3[] = /* Known */
  10616. {
  10617. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  10618. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  10619. };
  10620. #ifdef WOLFSSL_AES_128
  10621. const byte iv[] =
  10622. {
  10623. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  10624. 0xe2, 0x8c, 0x8f, 0x16
  10625. };
  10626. #endif
  10627. #ifdef WOLFSSL_AES_192
  10628. const byte iv2[] =
  10629. {
  10630. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  10631. 0x7e, 0x1a, 0x6f, 0xbc
  10632. };
  10633. #endif
  10634. const byte iv3[] =
  10635. {
  10636. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  10637. 0xc3, 0xfb, 0x6c, 0x8a
  10638. };
  10639. byte tagOut[16];
  10640. byte tagOut2[24];
  10641. byte tagOut3[32];
  10642. /* Init stack variables. */
  10643. XMEMSET(tagOut, 0, sizeof(tagOut));
  10644. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  10645. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  10646. printf(testingFmt, "wc_GmacUpdate()");
  10647. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  10648. if (ret != 0)
  10649. return ret;
  10650. #ifdef WOLFSSL_AES_128
  10651. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16));
  10652. if (ret == 0) {
  10653. ret = wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  10654. tagOut, sizeof(tag1));
  10655. if (ret == 0) {
  10656. ret = XMEMCMP(tag1, tagOut, sizeof(tag1));
  10657. }
  10658. }
  10659. #endif
  10660. #ifdef WOLFSSL_AES_192
  10661. if (ret == 0) {
  10662. XMEMSET(&gmac, 0, sizeof(Gmac));
  10663. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  10664. }
  10665. if (ret == 0) {
  10666. ret = wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2,
  10667. sizeof(authIn2), tagOut2, sizeof(tag2));
  10668. }
  10669. if (ret == 0) {
  10670. ret = XMEMCMP(tagOut2, tag2, sizeof(tag2));
  10671. }
  10672. #endif
  10673. #ifdef WOLFSSL_AES_256
  10674. if (ret == 0) {
  10675. XMEMSET(&gmac, 0, sizeof(Gmac));
  10676. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  10677. }
  10678. if (ret == 0) {
  10679. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  10680. sizeof(authIn3), tagOut3, sizeof(tag3));
  10681. }
  10682. if (ret == 0) {
  10683. ret = XMEMCMP(tag3, tagOut3, sizeof(tag3));
  10684. }
  10685. #endif
  10686. /*Pass bad args. */
  10687. if (ret == 0) {
  10688. ret = wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3,
  10689. sizeof(authIn3), tagOut3, sizeof(tag3));
  10690. if (ret == BAD_FUNC_ARG) {
  10691. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  10692. sizeof(authIn3), tagOut3, sizeof(tag3) - 5);
  10693. }
  10694. if (ret == BAD_FUNC_ARG) {
  10695. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  10696. sizeof(authIn3), tagOut3, sizeof(tag3) + 1);
  10697. }
  10698. if (ret == BAD_FUNC_ARG) {
  10699. ret = 0;
  10700. } else {
  10701. ret = WOLFSSL_FATAL_ERROR;
  10702. }
  10703. }
  10704. wc_AesFree(&gmac.aes);
  10705. printf(resultFmt, ret == 0 ? passed : failed);
  10706. #endif
  10707. return ret;
  10708. } /* END test_wc_GmacUpdate */
  10709. /*
  10710. * testing wc_CamelliaSetKey
  10711. */
  10712. static int test_wc_CamelliaSetKey (void)
  10713. {
  10714. int ret = 0;
  10715. #ifdef HAVE_CAMELLIA
  10716. Camellia camellia;
  10717. /*128-bit key*/
  10718. static const byte key16[] =
  10719. {
  10720. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10721. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  10722. };
  10723. /* 192-bit key */
  10724. static const byte key24[] =
  10725. {
  10726. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10727. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  10728. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  10729. };
  10730. /* 256-bit key */
  10731. static const byte key32[] =
  10732. {
  10733. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10734. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  10735. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10736. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  10737. };
  10738. static const byte iv[] =
  10739. {
  10740. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10741. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  10742. };
  10743. printf(testingFmt, "wc_CamelliaSetKey()");
  10744. ret = wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv);
  10745. if (ret == 0) {
  10746. ret = wc_CamelliaSetKey(&camellia, key16,
  10747. (word32)sizeof(key16), NULL);
  10748. if (ret == 0) {
  10749. ret = wc_CamelliaSetKey(&camellia, key24,
  10750. (word32)sizeof(key24), iv);
  10751. }
  10752. if (ret == 0) {
  10753. ret = wc_CamelliaSetKey(&camellia, key24,
  10754. (word32)sizeof(key24), NULL);
  10755. }
  10756. if (ret == 0) {
  10757. ret = wc_CamelliaSetKey(&camellia, key32,
  10758. (word32)sizeof(key32), iv);
  10759. }
  10760. if (ret == 0) {
  10761. ret = wc_CamelliaSetKey(&camellia, key32,
  10762. (word32)sizeof(key32), NULL);
  10763. }
  10764. }
  10765. /* Bad args. */
  10766. if (ret == 0) {
  10767. ret = wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv);
  10768. if (ret != BAD_FUNC_ARG) {
  10769. ret = WOLFSSL_FATAL_ERROR;
  10770. } else {
  10771. ret = 0;
  10772. }
  10773. } /* END bad args. */
  10774. #endif
  10775. return ret;
  10776. } /* END test_wc_CammeliaSetKey */
  10777. /*
  10778. * Testing wc_CamelliaSetIV()
  10779. */
  10780. static int test_wc_CamelliaSetIV (void)
  10781. {
  10782. int ret = 0;
  10783. #ifdef HAVE_CAMELLIA
  10784. Camellia camellia;
  10785. static const byte iv[] =
  10786. {
  10787. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10788. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  10789. };
  10790. printf(testingFmt, "wc_CamelliaSetIV()");
  10791. ret = wc_CamelliaSetIV(&camellia, iv);
  10792. if (ret == 0) {
  10793. ret = wc_CamelliaSetIV(&camellia, NULL);
  10794. }
  10795. /* Bad args. */
  10796. if (ret == 0) {
  10797. ret = wc_CamelliaSetIV(NULL, NULL);
  10798. if (ret != BAD_FUNC_ARG) {
  10799. ret = WOLFSSL_FATAL_ERROR;
  10800. } else {
  10801. ret = 0;
  10802. }
  10803. }
  10804. printf(resultFmt, ret == 0 ? passed : failed);
  10805. #endif
  10806. return ret;
  10807. } /*END test_wc_CamelliaSetIV*/
  10808. /*
  10809. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  10810. */
  10811. static int test_wc_CamelliaEncryptDecryptDirect (void)
  10812. {
  10813. int ret = 0;
  10814. #ifdef HAVE_CAMELLIA
  10815. Camellia camellia;
  10816. static const byte key24[] =
  10817. {
  10818. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10819. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  10820. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  10821. };
  10822. static const byte iv[] =
  10823. {
  10824. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10825. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  10826. };
  10827. static const byte plainT[] =
  10828. {
  10829. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  10830. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  10831. };
  10832. byte enc[sizeof(plainT)];
  10833. byte dec[sizeof(enc)];
  10834. int camE = WOLFSSL_FATAL_ERROR;
  10835. int camD = WOLFSSL_FATAL_ERROR;
  10836. /*Init stack variables.*/
  10837. XMEMSET(enc, 0, 16);
  10838. XMEMSET(enc, 0, 16);
  10839. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv);
  10840. if (ret == 0) {
  10841. ret = wc_CamelliaEncryptDirect(&camellia, enc, plainT);
  10842. if (ret == 0) {
  10843. ret = wc_CamelliaDecryptDirect(&camellia, dec, enc);
  10844. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  10845. ret = WOLFSSL_FATAL_ERROR;
  10846. }
  10847. }
  10848. }
  10849. printf(testingFmt, "wc_CamelliaEncryptDirect()");
  10850. /* Pass bad args. */
  10851. if (ret == 0) {
  10852. camE = wc_CamelliaEncryptDirect(NULL, enc, plainT);
  10853. if (camE == BAD_FUNC_ARG) {
  10854. camE = wc_CamelliaEncryptDirect(&camellia, NULL, plainT);
  10855. }
  10856. if (camE == BAD_FUNC_ARG) {
  10857. camE = wc_CamelliaEncryptDirect(&camellia, enc, NULL);
  10858. }
  10859. if (camE == BAD_FUNC_ARG) {
  10860. camE = 0;
  10861. } else {
  10862. camE = WOLFSSL_FATAL_ERROR;
  10863. }
  10864. }
  10865. printf(resultFmt, camE == 0 ? passed : failed);
  10866. if (camE != 0) {
  10867. return camE;
  10868. }
  10869. printf(testingFmt, "wc_CamelliaDecryptDirect()");
  10870. if (ret == 0) {
  10871. camD = wc_CamelliaDecryptDirect(NULL, dec, enc);
  10872. if (camD == BAD_FUNC_ARG) {
  10873. camD = wc_CamelliaDecryptDirect(&camellia, NULL, enc);
  10874. }
  10875. if (camD == BAD_FUNC_ARG) {
  10876. camD = wc_CamelliaDecryptDirect(&camellia, dec, NULL);
  10877. }
  10878. if (camD == BAD_FUNC_ARG) {
  10879. camD = 0;
  10880. } else {
  10881. camD = WOLFSSL_FATAL_ERROR;
  10882. }
  10883. }
  10884. printf(resultFmt, camD == 0 ? passed : failed);
  10885. if (camD != 0) {
  10886. return camD;
  10887. }
  10888. #endif
  10889. return ret;
  10890. } /* END test-wc_CamelliaEncryptDecryptDirect */
  10891. /*
  10892. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  10893. */
  10894. static int test_wc_CamelliaCbcEncryptDecrypt (void)
  10895. {
  10896. int ret = 0;
  10897. #ifdef HAVE_CAMELLIA
  10898. Camellia camellia;
  10899. static const byte key24[] =
  10900. {
  10901. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10902. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  10903. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  10904. };
  10905. static const byte plainT[] =
  10906. {
  10907. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  10908. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  10909. };
  10910. byte enc[CAMELLIA_BLOCK_SIZE];
  10911. byte dec[CAMELLIA_BLOCK_SIZE];
  10912. int camCbcE = WOLFSSL_FATAL_ERROR;
  10913. int camCbcD = WOLFSSL_FATAL_ERROR;
  10914. /* Init stack variables. */
  10915. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  10916. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  10917. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  10918. if (ret == 0) {
  10919. ret = wc_CamelliaCbcEncrypt(&camellia, enc, plainT, CAMELLIA_BLOCK_SIZE);
  10920. if (ret != 0) {
  10921. ret = WOLFSSL_FATAL_ERROR;
  10922. }
  10923. }
  10924. if (ret == 0) {
  10925. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  10926. if (ret == 0) {
  10927. ret = wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE);
  10928. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  10929. ret = WOLFSSL_FATAL_ERROR;
  10930. }
  10931. }
  10932. }
  10933. printf(testingFmt, "wc_CamelliaCbcEncrypt");
  10934. /* Pass in bad args. */
  10935. if (ret == 0) {
  10936. camCbcE = wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE);
  10937. if (camCbcE == BAD_FUNC_ARG) {
  10938. camCbcE = wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  10939. CAMELLIA_BLOCK_SIZE);
  10940. }
  10941. if (camCbcE == BAD_FUNC_ARG) {
  10942. camCbcE = wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  10943. CAMELLIA_BLOCK_SIZE);
  10944. }
  10945. if (camCbcE == BAD_FUNC_ARG) {
  10946. camCbcE = 0;
  10947. } else {
  10948. camCbcE = WOLFSSL_FATAL_ERROR;
  10949. }
  10950. }
  10951. printf(resultFmt, camCbcE == 0 ? passed : failed);
  10952. if (camCbcE != 0) {
  10953. return camCbcE;
  10954. }
  10955. printf(testingFmt, "wc_CamelliaCbcDecrypt()");
  10956. if (ret == 0) {
  10957. camCbcD = wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE);
  10958. if (camCbcD == BAD_FUNC_ARG) {
  10959. camCbcD = wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  10960. CAMELLIA_BLOCK_SIZE);
  10961. }
  10962. if (camCbcD == BAD_FUNC_ARG) {
  10963. camCbcD = wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  10964. CAMELLIA_BLOCK_SIZE);
  10965. }
  10966. if (camCbcD == BAD_FUNC_ARG) {
  10967. camCbcD = 0;
  10968. } else {
  10969. camCbcD = WOLFSSL_FATAL_ERROR;
  10970. }
  10971. } /* END bad args. */
  10972. printf(resultFmt, camCbcD == 0 ? passed : failed);
  10973. if (camCbcD != 0) {
  10974. return camCbcD;
  10975. }
  10976. #endif
  10977. return ret;
  10978. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  10979. /*
  10980. * Testing wc_RabbitSetKey()
  10981. */
  10982. static int test_wc_RabbitSetKey (void)
  10983. {
  10984. int ret = 0;
  10985. #ifndef NO_RABBIT
  10986. Rabbit rabbit;
  10987. const char* key = "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B"
  10988. "\xFE\x36\x3D\x2E\x29\x13\x28\x91";
  10989. const char* iv = "\x59\x7E\x26\xC1\x75\xF5\x73\xC3";
  10990. printf(testingFmt, "wc_RabbitSetKey()");
  10991. ret = wc_RabbitSetKey(&rabbit, (byte*)key, (byte*)iv);
  10992. /* Test bad args. */
  10993. if (ret == 0) {
  10994. ret = wc_RabbitSetKey(NULL, (byte*)key, (byte*)iv);
  10995. if (ret == BAD_FUNC_ARG) {
  10996. ret = wc_RabbitSetKey(&rabbit, NULL, (byte*)iv);
  10997. }
  10998. if (ret == BAD_FUNC_ARG) {
  10999. ret = wc_RabbitSetKey(&rabbit, (byte*)key, NULL);
  11000. }
  11001. }
  11002. printf(resultFmt, ret == 0 ? passed : failed);
  11003. #endif
  11004. return ret;
  11005. } /* END test_wc_RabbitSetKey */
  11006. /*
  11007. * Test wc_RabbitProcess()
  11008. */
  11009. static int test_wc_RabbitProcess (void)
  11010. {
  11011. int ret = 0;
  11012. #ifndef NO_RABBIT
  11013. Rabbit enc, dec;
  11014. byte cipher[25];
  11015. byte plain[25];
  11016. const char* key = "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B"
  11017. "\xFE\x36\x3D\x2E\x29\x13\x28\x91";
  11018. const char* iv = "\x59\x7E\x26\xC1\x75\xF5\x73\xC3";
  11019. const char* input = "Everyone gets Friday off.";
  11020. unsigned long int inlen = XSTRLEN(input);
  11021. /* Initialize stack variables. */
  11022. XMEMSET(cipher, 0, sizeof(cipher));
  11023. XMEMSET(plain, 0, sizeof(plain));
  11024. printf(testingFmt, "wc_RabbitProcess()");
  11025. ret = wc_RabbitSetKey(&enc, (byte*)key, (byte*)iv);
  11026. if (ret == 0) {
  11027. ret = wc_RabbitSetKey(&dec, (byte*)key, (byte*)iv);
  11028. }
  11029. if (ret == 0) {
  11030. ret = wc_RabbitProcess(&enc, cipher, (byte*)input, (word32)inlen);
  11031. }
  11032. if (ret == 0) {
  11033. ret = wc_RabbitProcess(&dec, plain, cipher, (word32)inlen);
  11034. if (ret != 0 || XMEMCMP(input, plain, inlen)) {
  11035. ret = WOLFSSL_FATAL_ERROR;
  11036. } else {
  11037. ret = 0;
  11038. }
  11039. }
  11040. /* Test bad args. */
  11041. if (ret == 0) {
  11042. ret = wc_RabbitProcess(NULL, plain, cipher, (word32)inlen);
  11043. if (ret == BAD_FUNC_ARG) {
  11044. ret = wc_RabbitProcess(&dec, NULL, cipher, (word32)inlen);
  11045. }
  11046. if (ret == BAD_FUNC_ARG) {
  11047. ret = wc_RabbitProcess(&dec, plain, NULL, (word32)inlen);
  11048. }
  11049. if (ret == BAD_FUNC_ARG) {
  11050. ret = 0;
  11051. } else {
  11052. ret = WOLFSSL_FATAL_ERROR;
  11053. }
  11054. }
  11055. printf(resultFmt, ret == 0 ? passed : failed);
  11056. #endif
  11057. return ret;
  11058. } /* END test_wc_RabbitProcess */
  11059. /*
  11060. * Testing wc_Arc4SetKey()
  11061. */
  11062. static int test_wc_Arc4SetKey (void)
  11063. {
  11064. int ret = 0;
  11065. #ifndef NO_RC4
  11066. Arc4 arc;
  11067. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  11068. int keyLen = 8;
  11069. printf(testingFmt, "wc_Arch4SetKey()");
  11070. ret = wc_Arc4SetKey(&arc, (byte*)key, keyLen);
  11071. /* Test bad args. */
  11072. if (ret == 0) {
  11073. ret = wc_Arc4SetKey(NULL, (byte*)key, keyLen);
  11074. if (ret == BAD_FUNC_ARG)
  11075. ret = wc_Arc4SetKey(&arc, NULL, keyLen); /* NULL key */
  11076. if (ret == BAD_FUNC_ARG)
  11077. ret = wc_Arc4SetKey(&arc, (byte*)key, 0); /* length == 0 */
  11078. if (ret == BAD_FUNC_ARG)
  11079. ret = WOLFSSL_ERROR_NONE;
  11080. else
  11081. ret = WOLFSSL_FATAL_ERROR;
  11082. } /* END test bad args. */
  11083. printf(resultFmt, ret == 0 ? passed : failed);
  11084. #endif
  11085. return ret;
  11086. } /* END test_wc_Arc4SetKey */
  11087. /*
  11088. * Testing wc_Arc4Process for ENC/DEC.
  11089. */
  11090. static int test_wc_Arc4Process (void)
  11091. {
  11092. int ret = 0;
  11093. #ifndef NO_RC4
  11094. Arc4 enc, dec;
  11095. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  11096. int keyLen = 8;
  11097. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  11098. byte cipher[8];
  11099. byte plain[8];
  11100. /* Init stack variables */
  11101. XMEMSET(cipher, 0, sizeof(cipher));
  11102. XMEMSET(plain, 0, sizeof(plain));
  11103. /* Use for async. */
  11104. ret = wc_Arc4Init(&enc, NULL, INVALID_DEVID);
  11105. if (ret == 0) {
  11106. ret = wc_Arc4Init(&dec, NULL, INVALID_DEVID);
  11107. }
  11108. printf(testingFmt, "wc_Arc4Process()");
  11109. if (ret == 0) {
  11110. ret = wc_Arc4SetKey(&enc, (byte*)key, keyLen);
  11111. }
  11112. if (ret == 0) {
  11113. ret = wc_Arc4SetKey(&dec, (byte*)key, keyLen);
  11114. }
  11115. if (ret == 0) {
  11116. ret = wc_Arc4Process(&enc, cipher, (byte*)input, keyLen);
  11117. }
  11118. if (ret == 0) {
  11119. ret = wc_Arc4Process(&dec, plain, cipher, keyLen);
  11120. if (ret != 0 || XMEMCMP(plain, input, keyLen)) {
  11121. ret = WOLFSSL_FATAL_ERROR;
  11122. } else {
  11123. ret = 0;
  11124. }
  11125. }
  11126. /* Bad args. */
  11127. if (ret == 0) {
  11128. ret = wc_Arc4Process(NULL, plain, cipher, keyLen);
  11129. if (ret == BAD_FUNC_ARG) {
  11130. ret = wc_Arc4Process(&dec, NULL, cipher, keyLen);
  11131. }
  11132. if (ret == BAD_FUNC_ARG) {
  11133. ret = wc_Arc4Process(&dec, plain, NULL, keyLen);
  11134. }
  11135. if (ret == BAD_FUNC_ARG) {
  11136. ret = 0;
  11137. } else {
  11138. ret = WOLFSSL_FATAL_ERROR;
  11139. }
  11140. }
  11141. printf(resultFmt, ret == 0 ? passed : failed);
  11142. wc_Arc4Free(&enc);
  11143. wc_Arc4Free(&dec);
  11144. #endif
  11145. return ret;
  11146. }/* END test_wc_Arc4Process */
  11147. /*
  11148. * Testing wc_Init RsaKey()
  11149. */
  11150. static int test_wc_InitRsaKey (void)
  11151. {
  11152. int ret = 0;
  11153. #ifndef NO_RSA
  11154. RsaKey key;
  11155. printf(testingFmt, "wc_InitRsaKey()");
  11156. ret = wc_InitRsaKey(&key, NULL);
  11157. /* Test bad args. */
  11158. if (ret == 0) {
  11159. ret = wc_InitRsaKey(NULL, NULL);
  11160. #ifndef HAVE_USER_RSA
  11161. if (ret == BAD_FUNC_ARG) {
  11162. ret = 0;
  11163. } else {
  11164. #else
  11165. if (ret == USER_CRYPTO_ERROR) {
  11166. ret = 0;
  11167. } else {
  11168. #endif
  11169. ret = WOLFSSL_FATAL_ERROR;
  11170. }
  11171. } /* end if */
  11172. if (wc_FreeRsaKey(&key) || ret != 0) {
  11173. ret = WOLFSSL_FATAL_ERROR;
  11174. }
  11175. printf(resultFmt, ret == 0 ? passed : failed);
  11176. #endif
  11177. return ret;
  11178. } /* END test_wc_InitRsaKey */
  11179. /*
  11180. * Testing wc_RsaPrivateKeyDecode()
  11181. */
  11182. static int test_wc_RsaPrivateKeyDecode (void)
  11183. {
  11184. int ret = 0;
  11185. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  11186. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  11187. RsaKey key;
  11188. byte* tmp;
  11189. word32 idx = 0;
  11190. int bytes = 0;
  11191. printf(testingFmt, "wc_RsaPrivateKeyDecode()");
  11192. tmp = (byte*)XMALLOC(FOURK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11193. if (tmp == NULL) {
  11194. ret = WOLFSSL_FATAL_ERROR;
  11195. }
  11196. if (ret == 0) {
  11197. ret = wc_InitRsaKey(&key, NULL);
  11198. }
  11199. if (ret == 0) {
  11200. #ifdef USE_CERT_BUFFERS_1024
  11201. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  11202. bytes = sizeof_client_key_der_1024;
  11203. #else
  11204. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  11205. bytes = sizeof_client_key_der_2048;
  11206. #endif /* Use cert buffers. */
  11207. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
  11208. }
  11209. #ifndef HAVE_USER_RSA
  11210. /* Test bad args. */
  11211. if (ret == 0) {
  11212. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  11213. if (ret == BAD_FUNC_ARG) {
  11214. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  11215. }
  11216. if (ret == BAD_FUNC_ARG) {
  11217. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  11218. }
  11219. if (ret == BAD_FUNC_ARG) {
  11220. ret = 0;
  11221. } else {
  11222. ret = WOLFSSL_FATAL_ERROR;
  11223. }
  11224. }
  11225. #else
  11226. /* Test bad args. User RSA. */
  11227. if (ret == 0) {
  11228. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  11229. if (ret == USER_CRYPTO_ERROR) {
  11230. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  11231. }
  11232. if (ret == USER_CRYPTO_ERROR) {
  11233. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  11234. }
  11235. if (ret == USER_CRYPTO_ERROR) {
  11236. ret = 0;
  11237. } else {
  11238. ret = WOLFSSL_FATAL_ERROR;
  11239. }
  11240. }
  11241. #endif
  11242. if (tmp != NULL) {
  11243. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11244. }
  11245. if (wc_FreeRsaKey(&key) || ret != 0) {
  11246. ret = WOLFSSL_FATAL_ERROR;
  11247. }
  11248. printf(resultFmt, ret == 0 ? passed : failed);
  11249. #endif
  11250. return ret;
  11251. } /* END test_wc_RsaPrivateKeyDecode */
  11252. /*
  11253. * Testing wc_RsaPublicKeyDecode()
  11254. */
  11255. static int test_wc_RsaPublicKeyDecode (void)
  11256. {
  11257. int ret = 0;
  11258. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  11259. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  11260. RsaKey keyPub;
  11261. byte* tmp;
  11262. word32 idx = 0;
  11263. int bytes = 0;
  11264. word32 keySz = 0;
  11265. word32 tstKeySz = 0;
  11266. tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11267. if (tmp == NULL) {
  11268. ret = WOLFSSL_FATAL_ERROR;
  11269. }
  11270. if (ret == 0) {
  11271. ret = wc_InitRsaKey(&keyPub, NULL);
  11272. }
  11273. if (ret == 0) {
  11274. #ifdef USE_CERT_BUFFERS_1024
  11275. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  11276. bytes = sizeof_client_keypub_der_1024;
  11277. keySz = 1024;
  11278. #else
  11279. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  11280. bytes = sizeof_client_keypub_der_2048;
  11281. keySz = 2048;
  11282. #endif
  11283. printf(testingFmt, "wc_RsaPublicKeyDecode()");
  11284. ret = wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes);
  11285. }
  11286. #ifndef HAVE_USER_RSA
  11287. /* Pass in bad args. */
  11288. if (ret == 0) {
  11289. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  11290. if (ret == BAD_FUNC_ARG) {
  11291. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  11292. }
  11293. if (ret == BAD_FUNC_ARG) {
  11294. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  11295. }
  11296. if (ret == BAD_FUNC_ARG) {
  11297. ret = 0;
  11298. } else {
  11299. ret = WOLFSSL_FATAL_ERROR;
  11300. }
  11301. }
  11302. #else
  11303. /* Pass in bad args. */
  11304. if (ret == 0) {
  11305. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  11306. if (ret == USER_CRYPTO_ERROR) {
  11307. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  11308. }
  11309. if (ret == USER_CRYPTO_ERROR) {
  11310. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  11311. }
  11312. if (ret == USER_CRYPTO_ERROR) {
  11313. ret = 0;
  11314. } else {
  11315. ret = WOLFSSL_FATAL_ERROR;
  11316. }
  11317. }
  11318. #endif
  11319. if (wc_FreeRsaKey(&keyPub) || ret != 0) {
  11320. ret = WOLFSSL_FATAL_ERROR;
  11321. }
  11322. if (ret == 0) {
  11323. /* Test for getting modulus key size */
  11324. idx = 0;
  11325. ret = wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  11326. &tstKeySz, NULL, NULL);
  11327. ret = (ret == 0 && tstKeySz == keySz/8) ? 0 : WOLFSSL_FATAL_ERROR;
  11328. }
  11329. if (tmp != NULL) {
  11330. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11331. }
  11332. printf(resultFmt, ret == 0 ? passed : failed);
  11333. #endif
  11334. return ret;
  11335. } /* END test_wc_RsaPublicKeyDecode */
  11336. /*
  11337. * Testing wc_RsaPublicKeyDecodeRaw()
  11338. */
  11339. static int test_wc_RsaPublicKeyDecodeRaw (void)
  11340. {
  11341. int ret = 0;
  11342. #if !defined(NO_RSA)
  11343. RsaKey key;
  11344. const byte n = 0x23;
  11345. const byte e = 0x03;
  11346. int nSz = sizeof(n);
  11347. int eSz = sizeof(e);
  11348. printf(testingFmt, "wc_RsaPublicKeyDecodeRaw()");
  11349. ret = wc_InitRsaKey(&key, NULL);
  11350. if (ret == 0) {
  11351. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key);
  11352. }
  11353. #ifndef HAVE_USER_RSA
  11354. /* Pass in bad args. */
  11355. if (ret == 0) {
  11356. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  11357. if (ret == BAD_FUNC_ARG) {
  11358. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  11359. }
  11360. if (ret == BAD_FUNC_ARG) {
  11361. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  11362. }
  11363. if (ret == BAD_FUNC_ARG) {
  11364. ret = 0;
  11365. } else {
  11366. ret = WOLFSSL_FATAL_ERROR;
  11367. }
  11368. }
  11369. #else
  11370. /* Pass in bad args. User RSA. */
  11371. if (ret == 0) {
  11372. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  11373. if (ret == USER_CRYPTO_ERROR) {
  11374. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  11375. }
  11376. if (ret == USER_CRYPTO_ERROR) {
  11377. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  11378. }
  11379. if (ret == USER_CRYPTO_ERROR) {
  11380. ret = 0;
  11381. } else {
  11382. ret = WOLFSSL_FATAL_ERROR;
  11383. }
  11384. }
  11385. #endif
  11386. if (wc_FreeRsaKey(&key) || ret != 0) {
  11387. ret = WOLFSSL_FATAL_ERROR;
  11388. }
  11389. printf(resultFmt, ret == 0 ? passed : failed);
  11390. #endif
  11391. return ret;
  11392. } /* END test_wc_RsaPublicKeyDecodeRaw */
  11393. #if (!defined(NO_RSA) || !defined(HAVE_FAST_RSA)) && defined(WOLFSSL_KEY_GEN)
  11394. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  11395. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  11396. * trying until it gets a probable prime. */
  11397. #ifdef HAVE_FIPS
  11398. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  11399. {
  11400. int ret;
  11401. for (;;) {
  11402. ret = wc_MakeRsaKey(key, size, e, rng);
  11403. if (ret != PRIME_GEN_E) break;
  11404. printf("MakeRsaKey couldn't find prime; trying again.\n");
  11405. }
  11406. return ret;
  11407. }
  11408. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  11409. #else
  11410. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  11411. #endif
  11412. #endif
  11413. /*
  11414. * Testing wc_MakeRsaKey()
  11415. */
  11416. static int test_wc_MakeRsaKey (void)
  11417. {
  11418. int ret = 0;
  11419. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  11420. RsaKey genKey;
  11421. WC_RNG rng;
  11422. #ifndef WOLFSSL_SP_MATH
  11423. int bits = 1024;
  11424. #else
  11425. int bits = 2048;
  11426. #endif
  11427. printf(testingFmt, "wc_MakeRsaKey()");
  11428. ret = wc_InitRsaKey(&genKey, NULL);
  11429. if (ret == 0) {
  11430. ret = wc_InitRng(&rng);
  11431. if (ret == 0) {
  11432. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
  11433. if (ret == 0 && wc_FreeRsaKey(&genKey) != 0) {
  11434. ret = WOLFSSL_FATAL_ERROR;
  11435. }
  11436. }
  11437. }
  11438. #ifndef HAVE_USER_RSA
  11439. /* Test bad args. */
  11440. if (ret == 0) {
  11441. ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
  11442. if (ret == BAD_FUNC_ARG) {
  11443. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
  11444. }
  11445. if (ret == BAD_FUNC_ARG) {
  11446. /* e < 3 */
  11447. ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
  11448. }
  11449. if (ret == BAD_FUNC_ARG) {
  11450. /* e & 1 == 0 */
  11451. ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
  11452. }
  11453. if (ret == BAD_FUNC_ARG) {
  11454. ret = 0;
  11455. } else {
  11456. ret = WOLFSSL_FATAL_ERROR;
  11457. }
  11458. }
  11459. #else
  11460. /* Test bad args. */
  11461. if (ret == 0) {
  11462. ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
  11463. if (ret == USER_CRYPTO_ERROR) {
  11464. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
  11465. }
  11466. if (ret == USER_CRYPTO_ERROR) {
  11467. /* e < 3 */
  11468. ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
  11469. }
  11470. if (ret == USER_CRYPTO_ERROR) {
  11471. /* e & 1 == 0 */
  11472. ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
  11473. }
  11474. if (ret == USER_CRYPTO_ERROR) {
  11475. ret = 0;
  11476. } else {
  11477. ret = WOLFSSL_FATAL_ERROR;
  11478. }
  11479. }
  11480. #endif
  11481. if (wc_FreeRng(&rng) || ret != 0) {
  11482. ret = WOLFSSL_FATAL_ERROR;
  11483. }
  11484. printf(resultFmt, ret == 0 ? passed : failed);
  11485. #endif
  11486. return ret;
  11487. } /* END test_wc_MakeRsaKey */
  11488. /*
  11489. * Test the bounds checking on the cipher text versus the key modulus.
  11490. * 1. Make a new RSA key.
  11491. * 2. Set c to 1.
  11492. * 3. Decrypt c into k. (error)
  11493. * 4. Copy the key modulus to c and sub 1 from the copy.
  11494. * 5. Decrypt c into k. (error)
  11495. * Valid bounds test cases are covered by all the other RSA tests.
  11496. */
  11497. static int test_RsaDecryptBoundsCheck(void)
  11498. {
  11499. int ret = 0;
  11500. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  11501. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  11502. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  11503. RsaKey key;
  11504. byte flatC[256];
  11505. word32 flatCSz;
  11506. byte out[256];
  11507. word32 outSz = sizeof(out);
  11508. WC_RNG rng;
  11509. printf(testingFmt, "RSA decrypt bounds check");
  11510. XMEMSET(&rng, 0, sizeof(rng));
  11511. ret = wc_InitRng(&rng);
  11512. if (ret == 0)
  11513. ret = wc_InitRsaKey(&key, NULL);
  11514. if (ret == 0) {
  11515. const byte* derKey;
  11516. word32 derKeySz;
  11517. word32 idx = 0;
  11518. #ifdef USE_CERT_BUFFERS_1024
  11519. derKey = server_key_der_1024;
  11520. derKeySz = (word32)sizeof_server_key_der_1024;
  11521. flatCSz = 128;
  11522. #else
  11523. derKey = server_key_der_2048;
  11524. derKeySz = (word32)sizeof_server_key_der_2048;
  11525. flatCSz = 256;
  11526. #endif
  11527. ret = wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz);
  11528. }
  11529. if (ret == 0) {
  11530. XMEMSET(flatC, 0, flatCSz);
  11531. flatC[flatCSz-1] = 1;
  11532. ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  11533. RSA_PRIVATE_DECRYPT, &rng);
  11534. }
  11535. if (ret == RSA_OUT_OF_RANGE_E) {
  11536. mp_int c;
  11537. mp_init_copy(&c, &key.n);
  11538. mp_sub_d(&c, 1, &c);
  11539. mp_to_unsigned_bin(&c, flatC);
  11540. ret = wc_RsaDirect(flatC, sizeof(flatC), out, &outSz, &key,
  11541. RSA_PRIVATE_DECRYPT, NULL);
  11542. mp_clear(&c);
  11543. }
  11544. if (ret == RSA_OUT_OF_RANGE_E)
  11545. ret = 0;
  11546. if (wc_FreeRsaKey(&key) || wc_FreeRng(&rng) || ret != 0)
  11547. ret = WOLFSSL_FATAL_ERROR;
  11548. printf(resultFmt, ret == 0 ? passed : failed);
  11549. #endif
  11550. return ret;
  11551. } /* END test_wc_RsaDecryptBoundsCheck */
  11552. /*
  11553. * Testing wc_SetKeyUsage()
  11554. */
  11555. static int test_wc_SetKeyUsage (void)
  11556. {
  11557. int ret = 0;
  11558. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  11559. Cert myCert;
  11560. ret = wc_InitCert(&myCert);
  11561. printf(testingFmt, "wc_SetKeyUsage()");
  11562. if (ret == 0) {
  11563. ret = wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement");
  11564. if (ret == 0) {
  11565. ret = wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation");
  11566. }
  11567. if (ret == 0) {
  11568. ret = wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly");
  11569. }
  11570. if (ret == 0) {
  11571. ret = wc_SetKeyUsage(&myCert, "decipherOnly");
  11572. }
  11573. if (ret == 0) {
  11574. ret = wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign");
  11575. }
  11576. }
  11577. /* Test bad args. */
  11578. if (ret == 0) {
  11579. ret = wc_SetKeyUsage(NULL, "decipherOnly");
  11580. if (ret == BAD_FUNC_ARG) {
  11581. ret = wc_SetKeyUsage(&myCert, NULL);
  11582. }
  11583. if (ret == BAD_FUNC_ARG) {
  11584. ret = wc_SetKeyUsage(&myCert, "");
  11585. }
  11586. if (ret == KEYUSAGE_E) {
  11587. ret = wc_SetKeyUsage(&myCert, ",");
  11588. }
  11589. if (ret == KEYUSAGE_E) {
  11590. ret = wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign");
  11591. }
  11592. if (ret == KEYUSAGE_E) {
  11593. ret = 0;
  11594. } else {
  11595. ret = WOLFSSL_FATAL_ERROR;
  11596. }
  11597. }
  11598. printf(resultFmt, ret == 0 ? passed : failed);
  11599. #endif
  11600. return ret;
  11601. } /* END test_wc_SetKeyUsage */
  11602. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  11603. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  11604. {
  11605. (void)flag;
  11606. (void)type;
  11607. (void)file;
  11608. (void)line;
  11609. }
  11610. #endif
  11611. /*
  11612. * Testing wc_LockMutex_ex
  11613. */
  11614. static int test_wc_LockMutex_ex (void)
  11615. {
  11616. int ret = 0;
  11617. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  11618. int flag = CRYPTO_LOCK;
  11619. int type = 0;
  11620. const char* file = "./test-LockMutex_ex.txt";
  11621. int line = 0;
  11622. printf(testingFmt, "wc_LockMutex_ex()");
  11623. /*without SetMutexCb*/
  11624. ret = wc_LockMutex_ex(flag, type, file, line);
  11625. if (ret == BAD_STATE_E) {
  11626. ret = 0;
  11627. }
  11628. /*with SetMutexCb*/
  11629. if (ret == 0) {
  11630. ret = wc_SetMutexCb(sample_mutex_cb);
  11631. if (ret == 0) {
  11632. ret = wc_LockMutex_ex(flag, type, file, line);
  11633. }
  11634. }
  11635. printf(resultFmt, ret == 0 ? passed : failed);
  11636. #endif
  11637. return ret;
  11638. }/*End test_wc_LockMutex_ex*/
  11639. /*
  11640. * Testing wc_SetMutexCb
  11641. */
  11642. static int test_wc_SetMutexCb (void)
  11643. {
  11644. int ret = 0;
  11645. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  11646. printf(testingFmt, "wc_SetMutexCb()");
  11647. ret = wc_SetMutexCb(sample_mutex_cb);
  11648. printf(resultFmt, ret == 0 ? passed : failed);
  11649. #endif
  11650. return ret;
  11651. }/*End test_wc_SetMutexCb*/
  11652. /*
  11653. * Testing wc_RsaKeyToDer()
  11654. */
  11655. static int test_wc_RsaKeyToDer (void)
  11656. {
  11657. int ret = 0;
  11658. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  11659. RsaKey genKey;
  11660. WC_RNG rng;
  11661. byte* der;
  11662. #ifndef WOLFSSL_SP_MATH
  11663. int bits = 1024;
  11664. word32 derSz = 611;
  11665. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  11666. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  11667. #else
  11668. int bits = 2048;
  11669. word32 derSz = 1196;
  11670. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  11671. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  11672. #endif
  11673. XMEMSET(&rng, 0, sizeof(rng));
  11674. XMEMSET(&genKey, 0, sizeof(genKey));
  11675. der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11676. if (der == NULL) {
  11677. ret = WOLFSSL_FATAL_ERROR;
  11678. }
  11679. /* Init structures. */
  11680. if (ret == 0) {
  11681. ret = wc_InitRsaKey(&genKey, NULL);
  11682. }
  11683. if (ret == 0) {
  11684. ret = wc_InitRng(&rng);
  11685. }
  11686. /* Make key. */
  11687. if (ret == 0) {
  11688. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
  11689. if (ret != 0) {
  11690. ret = WOLFSSL_FATAL_ERROR;
  11691. }
  11692. }
  11693. printf(testingFmt, "wc_RsaKeyToDer()");
  11694. if (ret == 0) {
  11695. ret = wc_RsaKeyToDer(&genKey, der, derSz);
  11696. if (ret > 0) {
  11697. ret = 0;
  11698. } else {
  11699. ret = WOLFSSL_FATAL_ERROR;
  11700. }
  11701. }
  11702. #ifndef HAVE_USER_RSA
  11703. /* Pass good/bad args. */
  11704. if (ret == 0) {
  11705. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  11706. if (ret == BAD_FUNC_ARG) {
  11707. /* Get just the output length */
  11708. ret = wc_RsaKeyToDer(&genKey, NULL, 0);
  11709. }
  11710. if (ret > 0) {
  11711. /* Try Public Key. */
  11712. genKey.type = 0;
  11713. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  11714. }
  11715. if (ret == BAD_FUNC_ARG) {
  11716. ret = 0;
  11717. } else {
  11718. ret = WOLFSSL_FATAL_ERROR;
  11719. }
  11720. }
  11721. #else
  11722. /* Pass good/bad args. */
  11723. if (ret == 0) {
  11724. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  11725. if (ret == USER_CRYPTO_ERROR) {
  11726. /* Get just the output length */
  11727. ret = wc_RsaKeyToDer(&genKey, NULL, 0);
  11728. }
  11729. if (ret > 0) {
  11730. /* Try Public Key. */
  11731. genKey.type = 0;
  11732. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  11733. }
  11734. if (ret == USER_CRYPTO_ERROR) {
  11735. ret = 0;
  11736. } else {
  11737. ret = WOLFSSL_FATAL_ERROR;
  11738. }
  11739. }
  11740. #endif
  11741. if (der != NULL) {
  11742. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11743. }
  11744. if (wc_FreeRsaKey(&genKey) || ret != 0) {
  11745. ret = WOLFSSL_FATAL_ERROR;
  11746. }
  11747. if (wc_FreeRng(&rng) || ret != 0) {
  11748. ret = WOLFSSL_FATAL_ERROR;
  11749. }
  11750. printf(resultFmt, ret == 0 ? passed : failed);
  11751. #endif
  11752. return ret;
  11753. } /* END test_wc_RsaKeyToDer */
  11754. /*
  11755. * Testing wc_RsaKeyToPublicDer()
  11756. */
  11757. static int test_wc_RsaKeyToPublicDer (void)
  11758. {
  11759. int ret = 0;
  11760. #if !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) &&\
  11761. (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL))
  11762. RsaKey key;
  11763. WC_RNG rng;
  11764. byte* der;
  11765. #ifndef WOLFSSL_SP_MATH
  11766. int bits = 1024;
  11767. word32 derLen = 162;
  11768. #else
  11769. int bits = 2048;
  11770. word32 derLen = 290;
  11771. #endif
  11772. XMEMSET(&rng, 0, sizeof(rng));
  11773. XMEMSET(&key, 0, sizeof(key));
  11774. der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11775. if (der == NULL) {
  11776. ret = WOLFSSL_FATAL_ERROR;
  11777. }
  11778. if (ret == 0) {
  11779. ret = wc_InitRsaKey(&key, NULL);
  11780. }
  11781. if (ret == 0) {
  11782. ret = wc_InitRng(&rng);
  11783. }
  11784. if (ret == 0) {
  11785. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  11786. }
  11787. printf(testingFmt, "wc_RsaKeyToPublicDer()");
  11788. if (ret == 0) {
  11789. ret = wc_RsaKeyToPublicDer(&key, der, derLen);
  11790. if (ret >= 0) {
  11791. ret = 0;
  11792. } else {
  11793. ret = WOLFSSL_FATAL_ERROR;
  11794. }
  11795. }
  11796. #ifndef HAVE_USER_RSA
  11797. /* Pass in bad args. */
  11798. if (ret == 0) {
  11799. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  11800. if (ret == BAD_FUNC_ARG) {
  11801. ret = wc_RsaKeyToPublicDer(&key, NULL, derLen);
  11802. }
  11803. if (ret == BAD_FUNC_ARG) {
  11804. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  11805. }
  11806. if (ret == BAD_FUNC_ARG) {
  11807. ret = 0;
  11808. } else {
  11809. ret = WOLFSSL_FATAL_ERROR;
  11810. }
  11811. }
  11812. #else
  11813. /* Pass in bad args. */
  11814. if (ret == 0) {
  11815. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  11816. if (ret == USER_CRYPTO_ERROR) {
  11817. ret = wc_RsaKeyToPublicDer(&key, NULL, derLen);
  11818. }
  11819. if (ret == USER_CRYPTO_ERROR) {
  11820. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  11821. }
  11822. if (ret == USER_CRYPTO_ERROR) {
  11823. ret = 0;
  11824. } else {
  11825. ret = WOLFSSL_FATAL_ERROR;
  11826. }
  11827. }
  11828. #endif
  11829. if (der != NULL) {
  11830. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11831. }
  11832. if (wc_FreeRsaKey(&key) || ret != 0) {
  11833. ret = WOLFSSL_FATAL_ERROR;
  11834. }
  11835. if (wc_FreeRng(&rng) || ret != 0) {
  11836. ret = WOLFSSL_FATAL_ERROR;
  11837. }
  11838. printf(resultFmt, ret == 0 ? passed : failed);
  11839. #endif
  11840. return ret;
  11841. } /* END test_wc_RsaKeyToPublicDer */
  11842. /*
  11843. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  11844. */
  11845. static int test_wc_RsaPublicEncryptDecrypt (void)
  11846. {
  11847. int ret = 0;
  11848. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  11849. RsaKey key;
  11850. WC_RNG rng;
  11851. const char* inStr = "Everyone gets Friday off.";
  11852. word32 plainLen = 25;
  11853. word32 inLen = (word32)XSTRLEN(inStr);
  11854. #ifndef WOLFSSL_SP_MATH
  11855. int bits = 1024;
  11856. word32 cipherLen = 128;
  11857. #else
  11858. int bits = 2048;
  11859. word32 cipherLen = 256;
  11860. #endif
  11861. DECLARE_VAR_INIT(in, byte, inLen, inStr, NULL);
  11862. DECLARE_VAR(plain, byte, plainLen, NULL);
  11863. DECLARE_VAR(cipher, byte, cipherLen, NULL);
  11864. ret = wc_InitRsaKey(&key, NULL);
  11865. if (ret == 0) {
  11866. ret = wc_InitRng(&rng);
  11867. }
  11868. if (ret == 0) {
  11869. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  11870. }
  11871. /* Encrypt. */
  11872. printf(testingFmt, "wc_RsaPublicEncrypt()");
  11873. if (ret == 0) {
  11874. ret = wc_RsaPublicEncrypt(in, inLen, cipher, cipherLen, &key, &rng);
  11875. if (ret >= 0) {
  11876. cipherLen = ret;
  11877. ret = 0;
  11878. } else {
  11879. ret = WOLFSSL_FATAL_ERROR;
  11880. }
  11881. }
  11882. /* Pass bad args. */
  11883. /* Tests PsaPublicEncryptEx() which, is tested by another fn. No need dup.*/
  11884. printf(resultFmt, ret == 0 ? passed : failed);
  11885. if (ret != 0) {
  11886. return ret;
  11887. }
  11888. /* Decrypt */
  11889. printf(testingFmt, "wc_RsaPrivateDecrypt()");
  11890. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  11891. /* Bind rng */
  11892. if (ret == 0) {
  11893. ret = wc_RsaSetRNG(&key, &rng);
  11894. }
  11895. #endif
  11896. if (ret == 0) {
  11897. ret = wc_RsaPrivateDecrypt(cipher, cipherLen, plain, plainLen, &key);
  11898. }
  11899. if (ret >= 0) {
  11900. ret = XMEMCMP(plain, inStr, plainLen);
  11901. }
  11902. /* Pass in bad args. */
  11903. /* Tests RsaPrivateDecryptEx() which, is tested by another fn. No need dup.*/
  11904. FREE_VAR(in, NULL);
  11905. FREE_VAR(plain, NULL);
  11906. FREE_VAR(cipher, NULL);
  11907. if (wc_FreeRsaKey(&key) || ret != 0) {
  11908. ret = WOLFSSL_FATAL_ERROR;
  11909. }
  11910. if (wc_FreeRng(&rng) || ret != 0) {
  11911. ret = WOLFSSL_FATAL_ERROR;
  11912. }
  11913. printf(resultFmt, ret == 0 ? passed : failed);
  11914. #endif
  11915. return ret;
  11916. } /* END test_wc_RsaPublicEncryptDecrypt */
  11917. /*
  11918. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  11919. */
  11920. static int test_wc_RsaPublicEncryptDecrypt_ex (void)
  11921. {
  11922. int ret = 0;
  11923. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  11924. && !defined(WC_NO_RSA_OAEP) && !defined(HAVE_USER_RSA)\
  11925. && !defined(NO_SHA)
  11926. RsaKey key;
  11927. WC_RNG rng;
  11928. const char* inStr = "Everyone gets Friday off.";
  11929. word32 inLen = (word32)XSTRLEN(inStr);
  11930. const word32 plainSz = 25;
  11931. byte* res = NULL;
  11932. int idx = 0;
  11933. #ifndef WOLFSSL_SP_MATH
  11934. int bits = 1024;
  11935. const word32 cipherSz = 128;
  11936. #else
  11937. int bits = 2048;
  11938. const word32 cipherSz = 256;
  11939. #endif
  11940. DECLARE_VAR_INIT(in, byte, inLen, inStr, NULL);
  11941. DECLARE_VAR(plain, byte, plainSz, NULL);
  11942. DECLARE_VAR(cipher, byte, cipherSz, NULL);
  11943. /* Initialize stack structures. */
  11944. XMEMSET(&rng, 0, sizeof(rng));
  11945. XMEMSET(&key, 0, sizeof(key));
  11946. ret = wc_InitRsaKey_ex(&key, NULL, INVALID_DEVID);
  11947. if (ret == 0) {
  11948. ret = wc_InitRng(&rng);
  11949. }
  11950. if (ret == 0) {
  11951. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  11952. }
  11953. /* Encrypt */
  11954. printf(testingFmt, "wc_RsaPublicEncrypt_ex()");
  11955. if (ret == 0) {
  11956. ret = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key, &rng,
  11957. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  11958. if (ret >= 0) {
  11959. idx = ret;
  11960. ret = 0;
  11961. } else {
  11962. ret = WOLFSSL_FATAL_ERROR;
  11963. }
  11964. }
  11965. /*Pass bad args.*/
  11966. /* Tests RsaPublicEncryptEx again. No need duplicate. */
  11967. printf(resultFmt, ret == 0 ? passed : failed);
  11968. if (ret != 0) {
  11969. return ret;
  11970. }
  11971. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  11972. /* Decrypt */
  11973. printf(testingFmt, "wc_RsaPrivateDecrypt_ex()");
  11974. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  11975. if (ret == 0) {
  11976. ret = wc_RsaSetRNG(&key, &rng);
  11977. }
  11978. #endif
  11979. if (ret == 0) {
  11980. ret = wc_RsaPrivateDecrypt_ex(cipher, (word32)idx,
  11981. plain, plainSz, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA,
  11982. WC_MGF1SHA1, NULL, 0);
  11983. }
  11984. if (ret >= 0) {
  11985. if (!XMEMCMP(plain, inStr, plainSz)) {
  11986. ret = 0;
  11987. } else {
  11988. ret = WOLFSSL_FATAL_ERROR;
  11989. }
  11990. }
  11991. /*Pass bad args.*/
  11992. /* Tests RsaPrivateDecryptEx() again. No need duplicate. */
  11993. printf(resultFmt, ret == 0 ? passed : failed);
  11994. if (ret != 0) {
  11995. return ret;
  11996. }
  11997. printf(testingFmt, "wc_RsaPrivateDecryptInline_ex()");
  11998. if (ret == 0) {
  11999. ret = wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx,
  12000. &res, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA,
  12001. WC_MGF1SHA1, NULL, 0);
  12002. if (ret >= 0) {
  12003. if (!XMEMCMP(inStr, res, plainSz)) {
  12004. ret = 0;
  12005. } else {
  12006. ret = WOLFSSL_FATAL_ERROR;
  12007. }
  12008. }
  12009. }
  12010. #endif
  12011. FREE_VAR(in, NULL);
  12012. FREE_VAR(plain, NULL);
  12013. FREE_VAR(cipher, NULL);
  12014. if (wc_FreeRsaKey(&key) || ret != 0) {
  12015. ret = WOLFSSL_FATAL_ERROR;
  12016. }
  12017. if (wc_FreeRng(&rng) || ret != 0) {
  12018. ret = WOLFSSL_FATAL_ERROR;
  12019. }
  12020. printf(resultFmt, ret == 0 ? passed : failed);
  12021. #endif
  12022. return ret;
  12023. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  12024. /*
  12025. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  12026. */
  12027. static int test_wc_RsaSSL_SignVerify (void)
  12028. {
  12029. int ret = 0;
  12030. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  12031. RsaKey key;
  12032. WC_RNG rng;
  12033. const char* inStr = "Everyone gets Friday off.";
  12034. const word32 plainSz = 25;
  12035. word32 inLen = (word32)XSTRLEN(inStr);
  12036. word32 idx = 0;
  12037. #ifndef WOLFSSL_SP_MATH
  12038. int bits = 1024;
  12039. const word32 outSz = 128;
  12040. #else
  12041. int bits = 2048;
  12042. const word32 outSz = 256;
  12043. #endif
  12044. DECLARE_VAR_INIT(in, byte, inLen, inStr, NULL);
  12045. DECLARE_VAR(out, byte, outSz, NULL);
  12046. DECLARE_VAR(plain, byte, plainSz, NULL);
  12047. ret = wc_InitRsaKey(&key, NULL);
  12048. if (ret == 0) {
  12049. ret = wc_InitRng(&rng);
  12050. }
  12051. if (ret == 0) {
  12052. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  12053. }
  12054. /* Sign. */
  12055. printf(testingFmt, "wc_RsaSSL_Sign()");
  12056. if (ret == 0) {
  12057. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng);
  12058. if (ret == (int)outSz) {
  12059. idx = ret;
  12060. ret = 0;
  12061. } else {
  12062. ret = WOLFSSL_FATAL_ERROR;
  12063. }
  12064. }
  12065. #ifndef HAVE_USER_RSA
  12066. /* Test bad args. */
  12067. if (ret == 0) {
  12068. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  12069. if (ret == BAD_FUNC_ARG) {
  12070. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  12071. }
  12072. if (ret == BAD_FUNC_ARG) {
  12073. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  12074. }
  12075. if (ret == BAD_FUNC_ARG) {
  12076. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  12077. }
  12078. if (ret == BAD_FUNC_ARG) {
  12079. ret = 0;
  12080. } else {
  12081. ret = WOLFSSL_FATAL_ERROR;
  12082. }
  12083. }
  12084. #else
  12085. /* Test bad args. */
  12086. if (ret == 0) {
  12087. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  12088. if (ret == USER_CRYPTO_ERROR) {
  12089. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  12090. }
  12091. if (ret == USER_CRYPTO_ERROR) {
  12092. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  12093. }
  12094. if (ret == USER_CRYPTO_ERROR) {
  12095. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  12096. }
  12097. if (ret == USER_CRYPTO_ERROR) {
  12098. ret = 0;
  12099. } else {
  12100. ret = WOLFSSL_FATAL_ERROR;
  12101. }
  12102. }
  12103. #endif
  12104. printf(resultFmt, ret == 0 ? passed : failed);
  12105. if (ret != 0) {
  12106. return ret;
  12107. }
  12108. /* Verify. */
  12109. printf(testingFmt, "wc_RsaSSL_Verify()");
  12110. if (ret == 0) {
  12111. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, &key);
  12112. if (ret == (int)inLen) {
  12113. ret = 0;
  12114. } else {
  12115. ret = WOLFSSL_FATAL_ERROR;
  12116. }
  12117. }
  12118. #ifndef HAVE_USER_RSA
  12119. /* Pass bad args. */
  12120. if (ret == 0) {
  12121. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  12122. if (ret == BAD_FUNC_ARG) {
  12123. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  12124. }
  12125. if (ret == BAD_FUNC_ARG) {
  12126. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  12127. }
  12128. if (ret == BAD_FUNC_ARG) {
  12129. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  12130. }
  12131. if (ret == BAD_FUNC_ARG) {
  12132. ret = 0;
  12133. } else {
  12134. ret = WOLFSSL_FATAL_ERROR;
  12135. }
  12136. }
  12137. #else
  12138. /* Pass bad args. */
  12139. if (ret == 0) {
  12140. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  12141. if (ret == USER_CRYPTO_ERROR) {
  12142. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  12143. }
  12144. if (ret == USER_CRYPTO_ERROR) {
  12145. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  12146. }
  12147. if (ret == USER_CRYPTO_ERROR) {
  12148. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  12149. }
  12150. if (ret == USER_CRYPTO_ERROR) {
  12151. ret = 0;
  12152. } else {
  12153. ret = WOLFSSL_FATAL_ERROR;
  12154. }
  12155. }
  12156. #endif
  12157. FREE_VAR(in, NULL);
  12158. FREE_VAR(out, NULL);
  12159. FREE_VAR(plain, NULL);
  12160. if (wc_FreeRsaKey(&key) || ret != 0) {
  12161. ret = WOLFSSL_FATAL_ERROR;
  12162. }
  12163. if (wc_FreeRng(&rng) || ret != 0) {
  12164. ret = WOLFSSL_FATAL_ERROR;
  12165. }
  12166. printf(resultFmt, ret == 0 ? passed : failed);
  12167. #endif
  12168. return ret;
  12169. } /* END test_wc_RsaSSL_SignVerify */
  12170. /*
  12171. * Testing wc_RsaEncryptSize()
  12172. */
  12173. static int test_wc_RsaEncryptSize (void)
  12174. {
  12175. int ret = 0;
  12176. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  12177. RsaKey key;
  12178. WC_RNG rng;
  12179. ret = wc_InitRsaKey(&key, NULL);
  12180. if (ret == 0) {
  12181. ret = wc_InitRng(&rng);
  12182. }
  12183. printf(testingFmt, "wc_RsaEncryptSize()");
  12184. #ifndef WOLFSSL_SP_MATH
  12185. if (ret == 0) {
  12186. ret = MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng);
  12187. if (ret == 0) {
  12188. ret = wc_RsaEncryptSize(&key);
  12189. }
  12190. if (ret == 128) {
  12191. ret = 0;
  12192. } else {
  12193. ret = WOLFSSL_FATAL_ERROR;
  12194. }
  12195. }
  12196. if (wc_FreeRsaKey(&key) || ret != 0) {
  12197. ret = WOLFSSL_FATAL_ERROR;
  12198. } else {
  12199. ret = 0;
  12200. }
  12201. #endif
  12202. if (ret == 0) {
  12203. ret = MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng);
  12204. if (ret == 0) {
  12205. ret = wc_RsaEncryptSize(&key);
  12206. }
  12207. if (ret == 256) {
  12208. ret = 0;
  12209. } else {
  12210. ret = WOLFSSL_FATAL_ERROR;
  12211. }
  12212. }
  12213. /* Pass in bad arg. */
  12214. if (ret == 0) {
  12215. ret = wc_RsaEncryptSize(NULL);
  12216. #ifndef HAVE_USER_RSA
  12217. if (ret == BAD_FUNC_ARG) {
  12218. ret = 0;
  12219. } else {
  12220. ret = WOLFSSL_FATAL_ERROR;
  12221. }
  12222. #endif
  12223. }
  12224. if (wc_FreeRsaKey(&key) || ret != 0) {
  12225. ret = WOLFSSL_FATAL_ERROR;
  12226. }
  12227. if (wc_FreeRng(&rng) || ret != 0) {
  12228. ret = WOLFSSL_FATAL_ERROR;
  12229. }
  12230. printf(resultFmt, ret == 0 ? passed : failed);
  12231. #endif
  12232. return ret;
  12233. } /* END test_wc_RsaEncryptSize*/
  12234. /*
  12235. * Testing wc_RsaFlattenPublicKey()
  12236. */
  12237. static int test_wc_RsaFlattenPublicKey (void)
  12238. {
  12239. int ret = 0;
  12240. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  12241. RsaKey key;
  12242. WC_RNG rng;
  12243. byte e[256];
  12244. byte n[256];
  12245. word32 eSz = sizeof(e);
  12246. word32 nSz = sizeof(n);
  12247. #ifndef WOLFSSL_SP_MATH
  12248. int bits = 1024;
  12249. #else
  12250. int bits = 2048;
  12251. #endif
  12252. ret = wc_InitRsaKey(&key, NULL);
  12253. if (ret == 0) {
  12254. ret = wc_InitRng(&rng);
  12255. }
  12256. if (ret == 0) {
  12257. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  12258. if (ret >= 0) {
  12259. ret = 0;
  12260. } else {
  12261. ret = WOLFSSL_FATAL_ERROR;
  12262. }
  12263. }
  12264. printf(testingFmt, "wc_RsaFlattenPublicKey()");
  12265. if (ret == 0) {
  12266. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz);
  12267. }
  12268. #ifndef HAVE_USER_RSA
  12269. /* Pass bad args. */
  12270. if (ret == 0) {
  12271. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  12272. if (ret == BAD_FUNC_ARG) {
  12273. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  12274. }
  12275. if (ret == BAD_FUNC_ARG) {
  12276. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  12277. }
  12278. if (ret == BAD_FUNC_ARG) {
  12279. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  12280. }
  12281. if (ret == BAD_FUNC_ARG) {
  12282. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  12283. }
  12284. if (ret == BAD_FUNC_ARG) {
  12285. ret = 0;
  12286. } else {
  12287. ret = WOLFSSL_FATAL_ERROR;
  12288. }
  12289. }
  12290. #else
  12291. /* Pass bad args. */
  12292. if (ret == 0) {
  12293. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  12294. if (ret == USER_CRYPTO_ERROR) {
  12295. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  12296. }
  12297. if (ret == USER_CRYPTO_ERROR) {
  12298. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  12299. }
  12300. if (ret == USER_CRYPTO_ERROR) {
  12301. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  12302. }
  12303. if (ret == USER_CRYPTO_ERROR) {
  12304. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  12305. }
  12306. if (ret == USER_CRYPTO_ERROR) {
  12307. ret = 0;
  12308. } else {
  12309. ret = WOLFSSL_FATAL_ERROR;
  12310. }
  12311. }
  12312. #endif
  12313. if (wc_FreeRsaKey(&key) || ret != 0) {
  12314. ret = WOLFSSL_FATAL_ERROR;
  12315. }
  12316. if (wc_FreeRng(&rng) || ret != 0) {
  12317. ret = WOLFSSL_FATAL_ERROR;
  12318. }
  12319. printf(resultFmt, ret == 0 ? passed : failed);
  12320. #endif
  12321. return ret;
  12322. } /* END test_wc_RsaFlattenPublicKey */
  12323. /*
  12324. * unit test for wc_AesCcmSetKey
  12325. */
  12326. static int test_wc_AesCcmSetKey (void)
  12327. {
  12328. int ret = 0;
  12329. #ifdef HAVE_AESCCM
  12330. Aes aes;
  12331. const byte key16[] =
  12332. {
  12333. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  12334. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  12335. };
  12336. const byte key24[] =
  12337. {
  12338. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  12339. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  12340. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  12341. };
  12342. const byte key32[] =
  12343. {
  12344. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  12345. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  12346. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  12347. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  12348. };
  12349. printf(testingFmt, "wc_AesCcmSetKey()");
  12350. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  12351. if (ret != 0)
  12352. return ret;
  12353. #ifdef WOLFSSL_AES_128
  12354. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  12355. #endif
  12356. #ifdef WOLFSSL_AES_192
  12357. if (ret == 0) {
  12358. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24));
  12359. }
  12360. #endif
  12361. #ifdef WOLFSSL_AES_256
  12362. if (ret == 0) {
  12363. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32));
  12364. }
  12365. #endif
  12366. /* Test bad args. */
  12367. if (ret == 0) {
  12368. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1);
  12369. if (ret == BAD_FUNC_ARG) {
  12370. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1);
  12371. }
  12372. if (ret == BAD_FUNC_ARG) {
  12373. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1);
  12374. }
  12375. if (ret != BAD_FUNC_ARG) {
  12376. ret = WOLFSSL_FATAL_ERROR;
  12377. } else {
  12378. ret = 0;
  12379. }
  12380. }
  12381. wc_AesFree(&aes);
  12382. printf(resultFmt, ret == 0 ? passed : failed);
  12383. #endif
  12384. return ret;
  12385. } /* END test_wc_AesCcmSetKey */
  12386. /*
  12387. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  12388. */
  12389. static int test_wc_AesCcmEncryptDecrypt (void)
  12390. {
  12391. int ret = 0;
  12392. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  12393. Aes aes;
  12394. const byte key16[] =
  12395. {
  12396. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  12397. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  12398. };
  12399. /* plaintext */
  12400. const byte plainT[] =
  12401. {
  12402. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  12403. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  12404. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  12405. };
  12406. /* nonce */
  12407. const byte iv[] =
  12408. {
  12409. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  12410. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  12411. };
  12412. const byte c[] = /* cipher text. */
  12413. {
  12414. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  12415. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  12416. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  12417. };
  12418. const byte t[] = /* Auth tag */
  12419. {
  12420. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  12421. };
  12422. const byte authIn[] =
  12423. {
  12424. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  12425. };
  12426. byte cipherOut[sizeof(plainT)];
  12427. byte authTag[sizeof(t)];
  12428. int ccmE = WOLFSSL_FATAL_ERROR;
  12429. #ifdef HAVE_AES_DECRYPT
  12430. int ccmD = WOLFSSL_FATAL_ERROR;
  12431. byte plainOut[sizeof(cipherOut)];
  12432. #endif
  12433. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  12434. if (ret != 0)
  12435. return ret;
  12436. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  12437. if (ret == 0) {
  12438. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  12439. iv, sizeof(iv), authTag, sizeof(authTag),
  12440. authIn , sizeof(authIn));
  12441. if ((XMEMCMP(cipherOut, c, sizeof(c)) && ccmE == 0) ||
  12442. XMEMCMP(t, authTag, sizeof(t))) {
  12443. ccmE = WOLFSSL_FATAL_ERROR;
  12444. ret = WOLFSSL_FATAL_ERROR;
  12445. }
  12446. #ifdef HAVE_AES_DECRYPT
  12447. if (ret == 0) {
  12448. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  12449. sizeof(plainOut), iv, sizeof(iv),
  12450. authTag, sizeof(authTag),
  12451. authIn, sizeof(authIn));
  12452. if (XMEMCMP(plainOut, plainT, sizeof(plainT)) && ccmD == 0) {
  12453. ccmD = WOLFSSL_FATAL_ERROR;
  12454. }
  12455. }
  12456. #endif
  12457. }
  12458. printf(testingFmt, "wc_AesCcmEncrypt()");
  12459. /* Pass in bad args. Encrypt*/
  12460. if (ret == 0 && ccmE == 0) {
  12461. ccmE = wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  12462. iv, sizeof(iv), authTag, sizeof(authTag),
  12463. authIn , sizeof(authIn));
  12464. if (ccmE == BAD_FUNC_ARG) {
  12465. ccmE = wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  12466. iv, sizeof(iv), authTag, sizeof(authTag),
  12467. authIn , sizeof(authIn));
  12468. }
  12469. if (ccmE == BAD_FUNC_ARG) {
  12470. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  12471. iv, sizeof(iv), authTag, sizeof(authTag),
  12472. authIn , sizeof(authIn));
  12473. }
  12474. if (ccmE == BAD_FUNC_ARG) {
  12475. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  12476. NULL, sizeof(iv), authTag, sizeof(authTag),
  12477. authIn , sizeof(authIn));
  12478. }
  12479. if (ccmE == BAD_FUNC_ARG) {
  12480. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  12481. iv, sizeof(iv), NULL, sizeof(authTag),
  12482. authIn , sizeof(authIn));
  12483. }
  12484. if (ccmE == BAD_FUNC_ARG) {
  12485. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  12486. iv, sizeof(iv) + 1, authTag, sizeof(authTag),
  12487. authIn , sizeof(authIn));
  12488. }
  12489. if (ccmE == BAD_FUNC_ARG) {
  12490. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  12491. iv, sizeof(iv) - 7, authTag, sizeof(authTag),
  12492. authIn , sizeof(authIn));
  12493. }
  12494. if (ccmE != BAD_FUNC_ARG) {
  12495. ccmE = WOLFSSL_FATAL_ERROR;
  12496. } else {
  12497. ccmE = 0;
  12498. }
  12499. } /* End Encrypt */
  12500. printf(resultFmt, ccmE == 0 ? passed : failed);
  12501. if (ccmE != 0) {
  12502. wc_AesFree(&aes);
  12503. return ccmE;
  12504. }
  12505. #ifdef HAVE_AES_DECRYPT
  12506. printf(testingFmt, "wc_AesCcmDecrypt()");
  12507. /* Pass in bad args. Decrypt*/
  12508. if (ret == 0 && ccmD == 0) {
  12509. ccmD = wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  12510. iv, sizeof(iv), authTag, sizeof(authTag),
  12511. authIn, sizeof(authIn));
  12512. if (ccmD == BAD_FUNC_ARG) {
  12513. ccmD = wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  12514. iv, sizeof(iv), authTag, sizeof(authTag),
  12515. authIn, sizeof(authIn));
  12516. }
  12517. if (ccmD == BAD_FUNC_ARG) {
  12518. ccmD = wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  12519. iv, sizeof(iv), authTag, sizeof(authTag),
  12520. authIn, sizeof(authIn));
  12521. }
  12522. if (ccmD == BAD_FUNC_ARG) {
  12523. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  12524. sizeof(plainOut), NULL, sizeof(iv),
  12525. authTag, sizeof(authTag),
  12526. authIn, sizeof(authIn));
  12527. }
  12528. if (ccmD == BAD_FUNC_ARG) {
  12529. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  12530. sizeof(plainOut), iv, sizeof(iv), NULL,
  12531. sizeof(authTag), authIn, sizeof(authIn));
  12532. }
  12533. if (ccmD == BAD_FUNC_ARG) {
  12534. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  12535. sizeof(plainOut), iv, sizeof(iv) + 1,
  12536. authTag, sizeof(authTag),
  12537. authIn, sizeof(authIn));
  12538. }
  12539. if (ccmD == BAD_FUNC_ARG) {
  12540. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  12541. sizeof(plainOut), iv, sizeof(iv) - 7,
  12542. authTag, sizeof(authTag),
  12543. authIn, sizeof(authIn));
  12544. }
  12545. if (ccmD != BAD_FUNC_ARG) {
  12546. ccmD = WOLFSSL_FATAL_ERROR;
  12547. } else {
  12548. ccmD = 0;
  12549. }
  12550. } /* END Decrypt */
  12551. printf(resultFmt, ccmD == 0 ? passed : failed);
  12552. if (ccmD != 0) {
  12553. return ccmD;
  12554. }
  12555. #endif
  12556. wc_AesFree(&aes);
  12557. #endif /* HAVE_AESCCM */
  12558. return ret;
  12559. } /* END test_wc_AesCcmEncryptDecrypt */
  12560. /*
  12561. * Test wc_Hc128_SetKey()
  12562. */
  12563. static int test_wc_Hc128_SetKey (void)
  12564. {
  12565. int ret = 0;
  12566. #ifdef HAVE_HC128
  12567. HC128 ctx;
  12568. const char* key = "\x80\x00\x00\x00\x00\x00\x00\x00"
  12569. "\x00\x00\x00\x00\x00\x00\x00\x00";
  12570. const char* iv = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  12571. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  12572. printf(testingFmt, "wc_Hc128_SetKey()");
  12573. ret = wc_Hc128_SetKey(&ctx, (byte*)key, (byte*)iv);
  12574. /* Test bad args. */
  12575. if (ret == 0) {
  12576. ret = wc_Hc128_SetKey(NULL, (byte*)key, (byte*)iv);
  12577. if (ret == BAD_FUNC_ARG) {
  12578. ret = wc_Hc128_SetKey(&ctx, NULL, (byte*)iv);
  12579. }
  12580. if (ret == BAD_FUNC_ARG) {
  12581. ret = wc_Hc128_SetKey(&ctx, (byte*)key, NULL);
  12582. }
  12583. }
  12584. printf(resultFmt, ret == 0 ? passed : failed);
  12585. #endif
  12586. return ret;
  12587. } /* END test_wc_Hc128_SetKey */
  12588. /*
  12589. * Testing wc_Hc128_Process()
  12590. */
  12591. static int test_wc_Hc128_Process (void)
  12592. {
  12593. int ret = 0;
  12594. #ifdef HAVE_HC128
  12595. HC128 enc;
  12596. HC128 dec;
  12597. const char* key = "\x0F\x62\xB5\x08\x5B\xAE\x01\x54"
  12598. "\xA7\xFA\x4D\xA0\xF3\x46\x99\xEC";
  12599. const char* input = "Encrypt Hc128, and then Decrypt.";
  12600. size_t inlen = XSTRLEN(input) + 1; /* Add null terminator */
  12601. byte cipher[inlen];
  12602. byte plain[inlen];
  12603. printf(testingFmt, "wc_Hc128_Process()");
  12604. ret = wc_Hc128_SetKey(&enc, (byte*)key, NULL);
  12605. if (ret == 0) {
  12606. ret = wc_Hc128_SetKey(&dec, (byte*)key, NULL);
  12607. }
  12608. if (ret == 0) {
  12609. ret = wc_Hc128_Process(&enc, cipher, (byte*)input, (word32)inlen);
  12610. if (ret == 0) {
  12611. ret = wc_Hc128_Process(&dec, plain, cipher, (word32)inlen);
  12612. }
  12613. }
  12614. /* Bad args. */
  12615. if (ret == 0) {
  12616. ret = wc_Hc128_Process(NULL, plain, cipher, (word32)inlen);
  12617. if (ret == BAD_FUNC_ARG) {
  12618. ret = wc_Hc128_Process(&dec, NULL, cipher, (word32)inlen);
  12619. }
  12620. if (ret == BAD_FUNC_ARG) {
  12621. ret = wc_Hc128_Process(&dec, plain, NULL, (word32)inlen);
  12622. }
  12623. if (ret == BAD_FUNC_ARG) {
  12624. ret = 0;
  12625. } else {
  12626. ret = WOLFSSL_FATAL_ERROR;
  12627. }
  12628. }
  12629. printf(resultFmt, ret == 0 ? passed : failed);
  12630. #endif
  12631. return ret;
  12632. } /* END test_wc_Hc128_Process */
  12633. /*
  12634. * Testing wc_InitDsaKey()
  12635. */
  12636. static int test_wc_InitDsaKey (void)
  12637. {
  12638. int ret = 0;
  12639. #ifndef NO_DSA
  12640. DsaKey key;
  12641. printf(testingFmt, "wc_InitDsaKey()");
  12642. ret = wc_InitDsaKey(&key);
  12643. /* Pass in bad args. */
  12644. if (ret == 0) {
  12645. ret = wc_InitDsaKey(NULL);
  12646. if (ret == BAD_FUNC_ARG) {
  12647. ret = 0;
  12648. } else {
  12649. ret = WOLFSSL_FATAL_ERROR;
  12650. }
  12651. }
  12652. printf(resultFmt, ret == 0 ? passed : failed);
  12653. wc_FreeDsaKey(&key);
  12654. #endif
  12655. return ret;
  12656. } /* END test_wc_InitDsaKey */
  12657. /*
  12658. * Testing wc_DsaSign() and wc_DsaVerify()
  12659. */
  12660. static int test_wc_DsaSignVerify (void)
  12661. {
  12662. int ret = 0;
  12663. #if !defined(NO_DSA)
  12664. DsaKey key;
  12665. WC_RNG rng;
  12666. wc_Sha sha;
  12667. byte signature[DSA_SIG_SIZE];
  12668. byte hash[WC_SHA_DIGEST_SIZE];
  12669. word32 idx = 0;
  12670. word32 bytes;
  12671. int answer;
  12672. #ifdef USE_CERT_BUFFERS_1024
  12673. byte tmp[ONEK_BUF];
  12674. XMEMSET(tmp, 0, sizeof(tmp));
  12675. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  12676. bytes = sizeof_dsa_key_der_1024;
  12677. #elif defined(USE_CERT_BUFFERS_2048)
  12678. byte tmp[TWOK_BUF];
  12679. XMEMSET(tmp, 0, sizeof(tmp));
  12680. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  12681. bytes = sizeof_dsa_key_der_2048;
  12682. #else
  12683. byte tmp[TWOK_BUF];
  12684. XMEMSET(tmp, 0, sizeof(tmp));
  12685. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  12686. if (fp == XBADFILE) {
  12687. return WOLFSSL_BAD_FILE;
  12688. }
  12689. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  12690. XFCLOSE(fp);
  12691. #endif /* END USE_CERT_BUFFERS_1024 */
  12692. ret = wc_InitSha(&sha);
  12693. if (ret == 0) {
  12694. ret = wc_ShaUpdate(&sha, tmp, bytes);
  12695. if (ret == 0) {
  12696. ret = wc_ShaFinal(&sha, hash);
  12697. }
  12698. if (ret == 0) {
  12699. ret = wc_InitDsaKey(&key);
  12700. }
  12701. if (ret == 0) {
  12702. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  12703. }
  12704. if (ret == 0) {
  12705. ret = wc_InitRng(&rng);
  12706. }
  12707. }
  12708. printf(testingFmt, "wc_DsaSign()");
  12709. /* Sign. */
  12710. if (ret == 0) {
  12711. ret = wc_DsaSign(hash, signature, &key, &rng);
  12712. }
  12713. /* Test bad args. */
  12714. if (ret == 0) {
  12715. ret = wc_DsaSign(NULL, signature, &key, &rng);
  12716. if (ret == BAD_FUNC_ARG) {
  12717. ret = wc_DsaSign(hash, NULL, &key, &rng);
  12718. }
  12719. if (ret == BAD_FUNC_ARG) {
  12720. ret = wc_DsaSign(hash, signature, NULL, &rng);
  12721. }
  12722. if (ret == BAD_FUNC_ARG) {
  12723. ret = wc_DsaSign(hash, signature, &key, NULL);
  12724. }
  12725. if (ret == BAD_FUNC_ARG) {
  12726. ret = 0;
  12727. } else {
  12728. ret = WOLFSSL_FATAL_ERROR;
  12729. }
  12730. }
  12731. printf(resultFmt, ret == 0 ? passed : failed);
  12732. if (ret != 0) {
  12733. return ret;
  12734. }
  12735. /* Verify. */
  12736. printf(testingFmt, "wc_DsaVerify()");
  12737. ret = wc_DsaVerify(hash, signature, &key, &answer);
  12738. if (ret != 0 || answer != 1) {
  12739. ret = WOLFSSL_FATAL_ERROR;
  12740. } else {
  12741. ret = 0;
  12742. }
  12743. /* Pass in bad args. */
  12744. if (ret == 0) {
  12745. ret = wc_DsaVerify(NULL, signature, &key, &answer);
  12746. if (ret == BAD_FUNC_ARG) {
  12747. ret = wc_DsaVerify(hash, NULL, &key, &answer);
  12748. }
  12749. if (ret == BAD_FUNC_ARG) {
  12750. ret = wc_DsaVerify(hash, signature, NULL, &answer);
  12751. }
  12752. if (ret == BAD_FUNC_ARG) {
  12753. ret = wc_DsaVerify(hash, signature, &key, NULL);
  12754. }
  12755. if (ret == BAD_FUNC_ARG) {
  12756. ret = 0;
  12757. } else {
  12758. ret = WOLFSSL_FATAL_ERROR;
  12759. }
  12760. }
  12761. if (wc_FreeRng(&rng) && ret == 0) {
  12762. ret = WOLFSSL_FATAL_ERROR;
  12763. }
  12764. printf(resultFmt, ret == 0 ? passed : failed);
  12765. wc_FreeDsaKey(&key);
  12766. wc_ShaFree(&sha);
  12767. #endif
  12768. return ret;
  12769. } /* END test_wc_DsaSign */
  12770. /*
  12771. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  12772. */
  12773. static int test_wc_DsaPublicPrivateKeyDecode (void)
  12774. {
  12775. int ret = 0;
  12776. #if !defined(NO_DSA)
  12777. DsaKey key;
  12778. word32 bytes;
  12779. word32 idx = 0;
  12780. int priv = WOLFSSL_FATAL_ERROR;
  12781. int pub = WOLFSSL_FATAL_ERROR;
  12782. #ifdef USE_CERT_BUFFERS_1024
  12783. byte tmp[ONEK_BUF];
  12784. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  12785. bytes = sizeof_dsa_key_der_1024;
  12786. #elif defined(USE_CERT_BUFFERS_2048)
  12787. byte tmp[TWOK_BUF];
  12788. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  12789. bytes = sizeof_dsa_key_der_2048;
  12790. #else
  12791. byte tmp[TWOK_BUF];
  12792. XMEMSET(tmp, 0, sizeof(tmp));
  12793. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  12794. if (fp == XBADFILE)
  12795. {
  12796. return WOLFSSL_BAD_FILE;
  12797. }
  12798. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  12799. XFCLOSE(fp);
  12800. #endif /* END USE_CERT_BUFFERS_1024 */
  12801. ret = wc_InitDsaKey(&key);
  12802. printf(testingFmt, "wc_DsaPrivateKeyDecode()");
  12803. if (ret == 0) {
  12804. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  12805. /* Test bad args. */
  12806. if (priv == 0) {
  12807. priv = wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes);
  12808. if (priv == BAD_FUNC_ARG) {
  12809. priv = wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes);
  12810. }
  12811. if (priv == BAD_FUNC_ARG) {
  12812. priv = wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes);
  12813. }
  12814. if (priv == BAD_FUNC_ARG) {
  12815. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  12816. }
  12817. if (priv == ASN_PARSE_E) {
  12818. priv = 0;
  12819. } else {
  12820. priv = WOLFSSL_FATAL_ERROR;
  12821. }
  12822. }
  12823. wc_FreeDsaKey(&key);
  12824. ret = wc_InitDsaKey(&key);
  12825. }
  12826. printf(resultFmt, priv == 0 ? passed : failed);
  12827. printf(testingFmt, "wc_DsaPublicKeyDecode()");
  12828. if (ret == 0) {
  12829. idx = 0; /* Reset */
  12830. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  12831. /* Test bad args. */
  12832. if (pub == 0) {
  12833. pub = wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes);
  12834. if (pub == BAD_FUNC_ARG) {
  12835. pub = wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes);
  12836. }
  12837. if (pub == BAD_FUNC_ARG) {
  12838. pub = wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes);
  12839. }
  12840. if (pub == BAD_FUNC_ARG) {
  12841. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  12842. }
  12843. if (pub == ASN_PARSE_E) {
  12844. pub = 0;
  12845. } else {
  12846. pub = WOLFSSL_FATAL_ERROR;
  12847. }
  12848. }
  12849. } /* END Public Key */
  12850. printf(resultFmt, pub == 0 ? passed : failed);
  12851. wc_FreeDsaKey(&key);
  12852. #endif
  12853. return ret;
  12854. } /* END test_wc_DsaPublicPrivateKeyDecode */
  12855. /*
  12856. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  12857. */
  12858. static int test_wc_MakeDsaKey (void)
  12859. {
  12860. int ret = 0;
  12861. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  12862. DsaKey genKey;
  12863. WC_RNG rng;
  12864. XMEMSET(&rng, 0, sizeof(rng));
  12865. XMEMSET(&genKey, 0, sizeof(genKey));
  12866. ret = wc_InitRng(&rng);
  12867. if (ret == 0) {
  12868. ret = wc_InitDsaKey(&genKey);
  12869. }
  12870. printf(testingFmt, "wc_MakeDsaParameters()");
  12871. if (ret == 0) {
  12872. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  12873. }
  12874. /* Test bad args. */
  12875. if (ret == 0) {
  12876. ret = wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey);
  12877. if (ret == BAD_FUNC_ARG) {
  12878. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, NULL);
  12879. }
  12880. if (ret == BAD_FUNC_ARG) {
  12881. ret = wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey);
  12882. }
  12883. if (ret == BAD_FUNC_ARG) {
  12884. ret = 0;
  12885. } else {
  12886. ret = WOLFSSL_FATAL_ERROR;
  12887. }
  12888. }
  12889. printf(resultFmt, ret == 0 ? passed : failed);
  12890. printf(testingFmt, "wc_MakeDsaKey()");
  12891. if (ret == 0) {
  12892. ret = wc_MakeDsaKey(&rng, &genKey);
  12893. }
  12894. /* Test bad args. */
  12895. if (ret == 0) {
  12896. ret = wc_MakeDsaKey(NULL, &genKey);
  12897. if (ret == BAD_FUNC_ARG) {
  12898. ret = wc_MakeDsaKey(&rng, NULL);
  12899. }
  12900. if (ret == BAD_FUNC_ARG) {
  12901. ret = 0;
  12902. } else {
  12903. ret = WOLFSSL_FATAL_ERROR;
  12904. }
  12905. }
  12906. if (wc_FreeRng(&rng) && ret == 0) {
  12907. ret = WOLFSSL_FAILURE;
  12908. }
  12909. printf(resultFmt, ret == 0 ? passed : failed);
  12910. wc_FreeDsaKey(&genKey);
  12911. #endif
  12912. return ret;
  12913. } /* END test_wc_MakeDsaKey */
  12914. /*
  12915. * Testing wc_DsaKeyToDer()
  12916. */
  12917. static int test_wc_DsaKeyToDer (void)
  12918. {
  12919. int ret = 0;
  12920. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  12921. DsaKey genKey;
  12922. WC_RNG rng;
  12923. word32 bytes;
  12924. word32 idx = 0;
  12925. #ifdef USE_CERT_BUFFERS_1024
  12926. byte tmp[ONEK_BUF];
  12927. byte der[ONEK_BUF];
  12928. XMEMSET(tmp, 0, sizeof(tmp));
  12929. XMEMSET(der, 0, sizeof(der));
  12930. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  12931. bytes = sizeof_dsa_key_der_1024;
  12932. #elif defined(USE_CERT_BUFFERS_2048)
  12933. byte tmp[TWOK_BUF];
  12934. byte der[TWOK_BUF];
  12935. XMEMSET(tmp, 0, sizeof(tmp));
  12936. XMEMSET(der, 0, sizeof(der));
  12937. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  12938. bytes = sizeof_dsa_key_der_2048;
  12939. #else
  12940. byte tmp[TWOK_BUF];
  12941. byte der[TWOK_BUF];
  12942. XMEMSET(tmp, 0, sizeof(tmp));
  12943. XMEMSET(der, 0, sizeof(der));
  12944. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  12945. if (fp == XBADFILE) {
  12946. return WOLFSSL_BAD_FILE;
  12947. }
  12948. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  12949. XFCLOSE(fp);
  12950. #endif /* END USE_CERT_BUFFERS_1024 */
  12951. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  12952. XMEMSET(&rng, 0, sizeof(rng));
  12953. XMEMSET(&genKey, 0, sizeof(genKey));
  12954. #endif
  12955. ret = wc_InitRng(&rng);
  12956. if (ret == 0) {
  12957. ret = wc_InitDsaKey(&genKey);
  12958. }
  12959. if (ret == 0) {
  12960. ret = wc_MakeDsaParameters(&rng, sizeof(tmp), &genKey);
  12961. if (ret == 0) {
  12962. wc_FreeDsaKey(&genKey);
  12963. ret = wc_InitDsaKey(&genKey);
  12964. }
  12965. }
  12966. if (ret == 0) {
  12967. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &genKey, bytes);
  12968. }
  12969. printf(testingFmt, "wc_DsaKeyToDer()");
  12970. if (ret == 0) {
  12971. ret = wc_DsaKeyToDer(&genKey, der, bytes);
  12972. if ( ret >= 0 && ( ret = XMEMCMP(der, tmp, bytes) ) == 0 ) {
  12973. ret = 0;
  12974. }
  12975. }
  12976. /* Test bad args. */
  12977. if (ret == 0) {
  12978. ret = wc_DsaKeyToDer(NULL, der, FOURK_BUF);
  12979. if (ret == BAD_FUNC_ARG) {
  12980. ret = wc_DsaKeyToDer(&genKey, NULL, FOURK_BUF);
  12981. }
  12982. if (ret == BAD_FUNC_ARG) {
  12983. ret = 0;
  12984. } else {
  12985. ret = WOLFSSL_FATAL_ERROR;
  12986. }
  12987. }
  12988. if (wc_FreeRng(&rng) && ret == 0) {
  12989. ret = WOLFSSL_FATAL_ERROR;
  12990. }
  12991. printf(resultFmt, ret == 0 ? passed : failed);
  12992. wc_FreeDsaKey(&genKey);
  12993. #endif
  12994. return ret;
  12995. } /* END test_wc_DsaKeyToDer */
  12996. /*
  12997. * Testing wc_DsaKeyToPublicDer()
  12998. * (indirectly testing setDsaPublicKey())
  12999. */
  13000. static int test_wc_DsaKeyToPublicDer(void)
  13001. {
  13002. int ret = 0;
  13003. #ifndef HAVE_SELFTEST
  13004. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  13005. DsaKey genKey;
  13006. WC_RNG rng;
  13007. byte* der;
  13008. printf(testingFmt, "wc_DsaKeyToPublicDer()");
  13009. der = (byte*)XMALLOC(ONEK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  13010. if (der == NULL) {
  13011. ret = WOLFSSL_FATAL_ERROR;
  13012. }
  13013. if (ret == 0) {
  13014. ret = wc_InitDsaKey(&genKey);
  13015. }
  13016. if (ret == 0) {
  13017. ret = wc_InitRng(&rng);
  13018. }
  13019. if (ret == 0) {
  13020. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  13021. }
  13022. if (ret == 0) {
  13023. ret = wc_MakeDsaKey(&rng, &genKey);
  13024. }
  13025. if (ret == 0) {
  13026. ret = wc_DsaKeyToPublicDer(&genKey, der, ONEK_BUF);
  13027. if (ret >= 0) {
  13028. ret = 0;
  13029. } else {
  13030. ret = WOLFSSL_FATAL_ERROR;
  13031. }
  13032. }
  13033. /* Test bad args. */
  13034. if (ret == 0) {
  13035. ret = wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF);
  13036. if (ret == BAD_FUNC_ARG) {
  13037. ret = wc_DsaKeyToPublicDer(&genKey, NULL, FOURK_BUF);
  13038. }
  13039. if (ret == BAD_FUNC_ARG) {
  13040. ret = 0;
  13041. } else {
  13042. ret = WOLFSSL_FATAL_ERROR;
  13043. }
  13044. }
  13045. if (wc_FreeRng(&rng) && ret == 0) {
  13046. ret = WOLFSSL_FATAL_ERROR;
  13047. }
  13048. printf(resultFmt, ret == 0 ? passed : failed);
  13049. XFREE(der,NULL,DYNAMIC_TYPE_TMP_BUFFER);
  13050. wc_FreeDsaKey(&genKey);
  13051. #endif /* !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN) */
  13052. #endif /* HAVE_SELFTEST */
  13053. return ret;
  13054. } /* END test_wc_DsaKeyToPublicDer */
  13055. /*
  13056. * Testing wc_DsaImportParamsRaw()
  13057. */
  13058. static int test_wc_DsaImportParamsRaw (void)
  13059. {
  13060. int ret = 0;
  13061. #if !defined(NO_DSA)
  13062. DsaKey key;
  13063. /* [mod = L=1024, N=160], from CAVP KeyPair */
  13064. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  13065. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  13066. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  13067. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  13068. "47123188f8dc551054ee162b634d60f097f719076640e209"
  13069. "80a0093113a8bd73";
  13070. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  13071. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  13072. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  13073. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  13074. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  13075. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  13076. "76341a7e7d9";
  13077. /* invalid p and q parameters */
  13078. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  13079. const char* invalidQ = "96c5390a";
  13080. printf(testingFmt, "wc_DsaImportParamsRaw()");
  13081. ret = wc_InitDsaKey(&key);
  13082. if (ret == 0) {
  13083. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  13084. }
  13085. /* test bad args */
  13086. if (ret == 0) {
  13087. /* null key struct */
  13088. ret = wc_DsaImportParamsRaw(NULL, p, q, g);
  13089. if (ret == BAD_FUNC_ARG) {
  13090. /* null param pointers */
  13091. ret = wc_DsaImportParamsRaw(&key, NULL, NULL, NULL);
  13092. }
  13093. if (ret == BAD_FUNC_ARG) {
  13094. /* illegal p length */
  13095. ret = wc_DsaImportParamsRaw(&key, invalidP, q, g);
  13096. }
  13097. if (ret == BAD_FUNC_ARG) {
  13098. /* illegal q length */
  13099. ret = wc_DsaImportParamsRaw(&key, p, invalidQ, g);
  13100. if (ret == BAD_FUNC_ARG)
  13101. ret = 0;
  13102. }
  13103. }
  13104. printf(resultFmt, ret == 0 ? passed : failed);
  13105. wc_FreeDsaKey(&key);
  13106. #endif
  13107. return ret;
  13108. } /* END test_wc_DsaImportParamsRaw */
  13109. /*
  13110. * Testing wc_DsaImportParamsRawCheck()
  13111. */
  13112. static int test_wc_DsaImportParamsRawCheck (void)
  13113. {
  13114. int ret = 0;
  13115. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  13116. DsaKey key;
  13117. int trusted = 0;
  13118. /* [mod = L=1024, N=160], from CAVP KeyPair */
  13119. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  13120. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  13121. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  13122. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  13123. "47123188f8dc551054ee162b634d60f097f719076640e209"
  13124. "80a0093113a8bd73";
  13125. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  13126. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  13127. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  13128. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  13129. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  13130. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  13131. "76341a7e7d9";
  13132. /* invalid p and q parameters */
  13133. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  13134. const char* invalidQ = "96c5390a";
  13135. printf(testingFmt, "wc_DsaImportParamsRawCheck()");
  13136. ret = wc_InitDsaKey(&key);
  13137. if (ret == 0) {
  13138. ret = wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL);
  13139. }
  13140. /* test bad args */
  13141. if (ret == 0) {
  13142. /* null key struct */
  13143. ret = wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL);
  13144. if (ret == BAD_FUNC_ARG) {
  13145. /* null param pointers */
  13146. ret = wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted, NULL);
  13147. }
  13148. if (ret == BAD_FUNC_ARG) {
  13149. /* illegal p length */
  13150. ret = wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL);
  13151. }
  13152. if (ret == BAD_FUNC_ARG) {
  13153. /* illegal q length */
  13154. ret = wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL);
  13155. if (ret == BAD_FUNC_ARG)
  13156. ret = 0;
  13157. }
  13158. }
  13159. printf(resultFmt, ret == 0 ? passed : failed);
  13160. wc_FreeDsaKey(&key);
  13161. #endif
  13162. return ret;
  13163. } /* END test_wc_DsaImportParamsRawCheck */
  13164. /*
  13165. * Testing wc_DsaExportParamsRaw()
  13166. */
  13167. static int test_wc_DsaExportParamsRaw (void)
  13168. {
  13169. int ret = 0;
  13170. #if !defined(NO_DSA)
  13171. DsaKey key;
  13172. /* [mod = L=1024, N=160], from CAVP KeyPair */
  13173. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  13174. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  13175. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  13176. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  13177. "47123188f8dc551054ee162b634d60f097f719076640e209"
  13178. "80a0093113a8bd73";
  13179. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  13180. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  13181. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  13182. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  13183. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  13184. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  13185. "76341a7e7d9";
  13186. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  13187. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  13188. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  13189. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  13190. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  13191. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  13192. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  13193. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  13194. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  13195. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  13196. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  13197. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  13198. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  13199. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  13200. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  13201. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  13202. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  13203. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  13204. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  13205. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  13206. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  13207. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  13208. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  13209. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  13210. byte pOut[MAX_DSA_PARAM_SIZE];
  13211. byte qOut[MAX_DSA_PARAM_SIZE];
  13212. byte gOut[MAX_DSA_PARAM_SIZE];
  13213. word32 pOutSz, qOutSz, gOutSz;
  13214. printf(testingFmt, "wc_DsaExportParamsRaw()");
  13215. ret = wc_InitDsaKey(&key);
  13216. if (ret == 0) {
  13217. /* first test using imported raw parameters, for expected */
  13218. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  13219. }
  13220. if (ret == 0) {
  13221. pOutSz = sizeof(pOut);
  13222. qOutSz = sizeof(qOut);
  13223. gOutSz = sizeof(gOut);
  13224. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  13225. gOut, &gOutSz);
  13226. }
  13227. if (ret == 0) {
  13228. /* validate exported parameters are correct */
  13229. if ((XMEMCMP(pOut, pCompare, pOutSz) != 0) ||
  13230. (XMEMCMP(qOut, qCompare, qOutSz) != 0) ||
  13231. (XMEMCMP(gOut, gCompare, gOutSz) != 0) ) {
  13232. ret = -1;
  13233. }
  13234. }
  13235. /* test bad args */
  13236. if (ret == 0) {
  13237. /* null key struct */
  13238. ret = wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz,
  13239. gOut, &gOutSz);
  13240. if (ret == BAD_FUNC_ARG) {
  13241. /* null output pointers */
  13242. ret = wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz,
  13243. NULL, &gOutSz);
  13244. }
  13245. if (ret == LENGTH_ONLY_E) {
  13246. /* null output size pointers */
  13247. ret = wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL,
  13248. gOut, NULL);
  13249. }
  13250. if (ret == BAD_FUNC_ARG) {
  13251. /* p output buffer size too small */
  13252. pOutSz = 1;
  13253. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  13254. gOut, &gOutSz);
  13255. pOutSz = sizeof(pOut);
  13256. }
  13257. if (ret == BUFFER_E) {
  13258. /* q output buffer size too small */
  13259. qOutSz = 1;
  13260. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  13261. gOut, &gOutSz);
  13262. qOutSz = sizeof(qOut);
  13263. }
  13264. if (ret == BUFFER_E) {
  13265. /* g output buffer size too small */
  13266. gOutSz = 1;
  13267. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  13268. gOut, &gOutSz);
  13269. if (ret == BUFFER_E)
  13270. ret = 0;
  13271. }
  13272. }
  13273. printf(resultFmt, ret == 0 ? passed : failed);
  13274. wc_FreeDsaKey(&key);
  13275. #endif
  13276. return ret;
  13277. } /* END test_wc_DsaExportParamsRaw */
  13278. /*
  13279. * Testing wc_DsaExportKeyRaw()
  13280. */
  13281. static int test_wc_DsaExportKeyRaw (void)
  13282. {
  13283. int ret = 0;
  13284. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  13285. DsaKey key;
  13286. WC_RNG rng;
  13287. byte xOut[MAX_DSA_PARAM_SIZE];
  13288. byte yOut[MAX_DSA_PARAM_SIZE];
  13289. word32 xOutSz, yOutSz;
  13290. printf(testingFmt, "wc_DsaExportKeyRaw()");
  13291. XMEMSET(&rng, 0, sizeof(rng));
  13292. XMEMSET(&key, 0, sizeof(key));
  13293. ret = wc_InitRng(&rng);
  13294. if (ret == 0) {
  13295. ret = wc_InitDsaKey(&key);
  13296. }
  13297. if (ret == 0) {
  13298. ret = wc_MakeDsaParameters(&rng, 1024, &key);
  13299. if (ret == 0) {
  13300. ret = wc_MakeDsaKey(&rng, &key);
  13301. }
  13302. }
  13303. /* try successful export */
  13304. if (ret == 0) {
  13305. xOutSz = sizeof(xOut);
  13306. yOutSz = sizeof(yOut);
  13307. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  13308. }
  13309. /* test bad args */
  13310. if (ret == 0) {
  13311. /* null key struct */
  13312. ret = wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz);
  13313. if (ret == BAD_FUNC_ARG) {
  13314. /* null output pointers */
  13315. ret = wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz);
  13316. }
  13317. if (ret == LENGTH_ONLY_E) {
  13318. /* null output size pointers */
  13319. ret = wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL);
  13320. }
  13321. if (ret == BAD_FUNC_ARG) {
  13322. /* x output buffer size too small */
  13323. xOutSz = 1;
  13324. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  13325. xOutSz = sizeof(xOut);
  13326. }
  13327. if (ret == BUFFER_E) {
  13328. /* y output buffer size too small */
  13329. yOutSz = 1;
  13330. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  13331. if (ret == BUFFER_E)
  13332. ret = 0;
  13333. }
  13334. }
  13335. printf(resultFmt, ret == 0 ? passed : failed);
  13336. wc_FreeDsaKey(&key);
  13337. wc_FreeRng(&rng);
  13338. #endif
  13339. return ret;
  13340. } /* END test_wc_DsaExportParamsRaw */
  13341. /*
  13342. * Testing wc_ed25519_make_key().
  13343. */
  13344. static int test_wc_ed25519_make_key (void)
  13345. {
  13346. int ret = 0;
  13347. #if defined(HAVE_ED25519)
  13348. ed25519_key key;
  13349. WC_RNG rng;
  13350. ret = wc_InitRng(&rng);
  13351. if (ret == 0) {
  13352. ret = wc_ed25519_init(&key);
  13353. }
  13354. printf(testingFmt, "wc_ed25519_make_key()");
  13355. if (ret == 0) {
  13356. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  13357. }
  13358. /* Test bad args. */
  13359. if (ret == 0) {
  13360. ret = wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key);
  13361. if (ret == BAD_FUNC_ARG) {
  13362. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL);
  13363. }
  13364. if (ret == BAD_FUNC_ARG) {
  13365. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key);
  13366. }
  13367. if (ret == BAD_FUNC_ARG) {
  13368. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key);
  13369. }
  13370. if (ret == BAD_FUNC_ARG) {
  13371. ret = 0;
  13372. } else if (ret == 0) {
  13373. ret = SSL_FATAL_ERROR;
  13374. }
  13375. }
  13376. printf(resultFmt, ret == 0 ? passed : failed);
  13377. if (wc_FreeRng(&rng) && ret == 0) {
  13378. ret = SSL_FATAL_ERROR;
  13379. }
  13380. wc_ed25519_free(&key);
  13381. #endif
  13382. return ret;
  13383. } /* END test_wc_ed25519_make_key */
  13384. /*
  13385. * Testing wc_ed25519_init()
  13386. */
  13387. static int test_wc_ed25519_init (void)
  13388. {
  13389. int ret = 0;
  13390. #if defined(HAVE_ED25519)
  13391. ed25519_key key;
  13392. printf(testingFmt, "wc_ed25519_init()");
  13393. ret = wc_ed25519_init(&key);
  13394. /* Test bad args. */
  13395. if (ret == 0) {
  13396. ret = wc_ed25519_init(NULL);
  13397. if (ret == BAD_FUNC_ARG) {
  13398. ret = 0;
  13399. } else if (ret == 0) {
  13400. ret = SSL_FATAL_ERROR;
  13401. }
  13402. }
  13403. printf(resultFmt, ret == 0 ? passed : failed);
  13404. wc_ed25519_free(&key);
  13405. #endif
  13406. return ret;
  13407. } /* END test_wc_ed25519_init */
  13408. /*
  13409. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  13410. */
  13411. static int test_wc_ed25519_sign_msg (void)
  13412. {
  13413. int ret = 0;
  13414. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  13415. WC_RNG rng;
  13416. ed25519_key key;
  13417. byte msg[] = "Everybody gets Friday off.\n";
  13418. byte sig[ED25519_SIG_SIZE];
  13419. word32 msglen = sizeof(msg);
  13420. word32 siglen = sizeof(sig);
  13421. word32 badSigLen = sizeof(sig) - 1;
  13422. int verify_ok = 0; /*1 = Verify success.*/
  13423. /* Initialize stack variables. */
  13424. XMEMSET(sig, 0, siglen);
  13425. /* Initialize key. */
  13426. ret = wc_InitRng(&rng);
  13427. if (ret == 0) {
  13428. ret = wc_ed25519_init(&key);
  13429. if (ret == 0) {
  13430. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  13431. }
  13432. }
  13433. printf(testingFmt, "wc_ed25519_sign_msg()");
  13434. if (ret == 0) {
  13435. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key);
  13436. }
  13437. /* Test bad args. */
  13438. if (ret == 0 && siglen == ED25519_SIG_SIZE) {
  13439. ret = wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key);
  13440. if (ret == BAD_FUNC_ARG) {
  13441. ret = wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key);
  13442. }
  13443. if (ret == BAD_FUNC_ARG) {
  13444. ret = wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key);
  13445. }
  13446. if (ret == BAD_FUNC_ARG) {
  13447. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL);
  13448. }
  13449. if (ret == BAD_FUNC_ARG) {
  13450. ret = wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key);
  13451. }
  13452. if (ret == BUFFER_E && badSigLen == ED25519_SIG_SIZE) {
  13453. badSigLen -= 1;
  13454. ret = 0;
  13455. } else if (ret == 0) {
  13456. ret = SSL_FATAL_ERROR;
  13457. }
  13458. } /* END sign */
  13459. printf(resultFmt, ret == 0 ? passed : failed);
  13460. #ifdef HAVE_ED25519_VERIFY
  13461. printf(testingFmt, "wc_ed25519_verify_msg()");
  13462. if (ret == 0) {
  13463. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key);
  13464. if (ret == 0 && verify_ok == 1) {
  13465. ret = 0;
  13466. } else if (ret == 0) {
  13467. ret = SSL_FATAL_ERROR;
  13468. }
  13469. /* Test bad args. */
  13470. if (ret == 0) {
  13471. AssertIntEQ(wc_ed25519_verify_msg(sig, siglen - 1, msg,
  13472. msglen, &verify_ok, &key),
  13473. BAD_FUNC_ARG);
  13474. AssertIntEQ(wc_ed25519_verify_msg(sig, siglen + 1, msg,
  13475. msglen, &verify_ok, &key),
  13476. BAD_FUNC_ARG);
  13477. ret = wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  13478. &key);
  13479. if (ret == BAD_FUNC_ARG) {
  13480. ret = wc_ed25519_verify_msg(sig, siglen, NULL, msglen,
  13481. &verify_ok, &key);
  13482. }
  13483. if (ret == BAD_FUNC_ARG) {
  13484. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  13485. NULL, &key);
  13486. }
  13487. if (ret == BAD_FUNC_ARG) {
  13488. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  13489. &verify_ok, NULL);
  13490. }
  13491. if (ret == BAD_FUNC_ARG) {
  13492. ret = wc_ed25519_verify_msg(sig, badSigLen, msg, msglen,
  13493. &verify_ok, &key);
  13494. }
  13495. if (ret == BAD_FUNC_ARG) {
  13496. ret = 0;
  13497. } else if (ret == 0) {
  13498. ret = SSL_FATAL_ERROR;
  13499. }
  13500. }
  13501. } /* END verify. */
  13502. printf(resultFmt, ret == 0 ? passed : failed);
  13503. #endif /* Verify. */
  13504. if (wc_FreeRng(&rng) && ret == 0) {
  13505. ret = SSL_FATAL_ERROR;
  13506. }
  13507. wc_ed25519_free(&key);
  13508. #endif
  13509. return ret;
  13510. } /* END test_wc_ed25519_sign_msg */
  13511. /*
  13512. * Testing wc_ed25519_import_public()
  13513. */
  13514. static int test_wc_ed25519_import_public (void)
  13515. {
  13516. int ret = 0;
  13517. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  13518. WC_RNG rng;
  13519. ed25519_key pubKey;
  13520. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  13521. word32 inlen = sizeof(in);
  13522. ret = wc_InitRng(&rng);
  13523. if (ret == 0) {
  13524. ret = wc_ed25519_init(&pubKey);
  13525. if (ret == 0) {
  13526. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey);
  13527. }
  13528. }
  13529. printf(testingFmt, "wc_ed25519_import_public()");
  13530. if (ret == 0) {
  13531. ret = wc_ed25519_import_public(in, inlen, &pubKey);
  13532. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  13533. ret = 0;
  13534. } else {
  13535. ret = SSL_FATAL_ERROR;
  13536. }
  13537. /* Test bad args. */
  13538. if (ret == 0) {
  13539. ret = wc_ed25519_import_public(NULL, inlen, &pubKey);
  13540. if (ret == BAD_FUNC_ARG) {
  13541. ret = wc_ed25519_import_public(in, inlen, NULL);
  13542. }
  13543. if (ret == BAD_FUNC_ARG) {
  13544. ret = wc_ed25519_import_public(in, inlen - 1, &pubKey);
  13545. }
  13546. if (ret == BAD_FUNC_ARG) {
  13547. ret = 0;
  13548. } else if (ret == 0) {
  13549. ret = SSL_FATAL_ERROR;
  13550. }
  13551. }
  13552. }
  13553. printf(resultFmt, ret == 0 ? passed : failed);
  13554. if (wc_FreeRng(&rng) && ret == 0) {
  13555. ret = SSL_FATAL_ERROR;
  13556. }
  13557. wc_ed25519_free(&pubKey);
  13558. #endif
  13559. return ret;
  13560. } /* END wc_ed25519_import_public */
  13561. /*
  13562. * Testing wc_ed25519_import_private_key()
  13563. */
  13564. static int test_wc_ed25519_import_private_key (void)
  13565. {
  13566. int ret = 0;
  13567. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  13568. WC_RNG rng;
  13569. ed25519_key key;
  13570. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  13571. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  13572. word32 privKeySz = sizeof(privKey);
  13573. word32 pubKeySz = sizeof(pubKey);
  13574. ret = wc_InitRng(&rng);
  13575. if (ret != 0) {
  13576. return ret;
  13577. }
  13578. ret = wc_ed25519_init(&key);
  13579. if (ret != 0) {
  13580. wc_FreeRng(&rng);
  13581. return ret;
  13582. }
  13583. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  13584. printf(testingFmt, "wc_ed25519_import_private_key()");
  13585. if (ret == 0) {
  13586. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  13587. pubKeySz, &key);
  13588. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
  13589. || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  13590. ret = SSL_FATAL_ERROR;
  13591. }
  13592. }
  13593. /* Test bad args. */
  13594. if (ret == 0) {
  13595. ret = wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  13596. &key);
  13597. if (ret == BAD_FUNC_ARG) {
  13598. ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  13599. pubKeySz, &key);
  13600. }
  13601. if (ret == BAD_FUNC_ARG) {
  13602. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  13603. pubKeySz, NULL);
  13604. }
  13605. if (ret == BAD_FUNC_ARG) {
  13606. ret = wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  13607. pubKeySz, &key);
  13608. }
  13609. if (ret == BAD_FUNC_ARG) {
  13610. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  13611. pubKeySz - 1, &key);
  13612. }
  13613. if (ret == BAD_FUNC_ARG) {
  13614. ret = 0;
  13615. } else if (ret == 0) {
  13616. ret = SSL_FATAL_ERROR;
  13617. }
  13618. }
  13619. printf(resultFmt, ret == 0 ? passed : failed);
  13620. if (wc_FreeRng(&rng) && ret == 0) {
  13621. ret = SSL_FATAL_ERROR;
  13622. }
  13623. wc_ed25519_free(&key);
  13624. #endif
  13625. return ret;
  13626. } /* END test_wc_ed25519_import_private_key */
  13627. /*
  13628. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  13629. */
  13630. static int test_wc_ed25519_export (void)
  13631. {
  13632. int ret = 0;
  13633. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  13634. WC_RNG rng;
  13635. ed25519_key key;
  13636. byte priv[ED25519_PRV_KEY_SIZE];
  13637. byte pub[ED25519_PUB_KEY_SIZE];
  13638. word32 privSz = sizeof(priv);
  13639. word32 pubSz = sizeof(pub);
  13640. ret = wc_InitRng(&rng);
  13641. if (ret != 0) {
  13642. return ret;
  13643. }
  13644. ret = wc_ed25519_init(&key);
  13645. if (ret != 0) {
  13646. wc_FreeRng(&rng);
  13647. return ret;
  13648. }
  13649. if (ret == 0) {
  13650. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  13651. }
  13652. printf(testingFmt, "wc_ed25519_export_public()");
  13653. if (ret == 0) {
  13654. ret = wc_ed25519_export_public(&key, pub, &pubSz);
  13655. if (ret == 0 && (pubSz != ED25519_KEY_SIZE
  13656. || XMEMCMP(key.p, pub, pubSz) != 0)) {
  13657. ret = SSL_FATAL_ERROR;
  13658. }
  13659. if (ret == 0) {
  13660. ret = wc_ed25519_export_public(NULL, pub, &pubSz);
  13661. if (ret == BAD_FUNC_ARG) {
  13662. ret = wc_ed25519_export_public(&key, NULL, &pubSz);
  13663. }
  13664. if (ret == BAD_FUNC_ARG) {
  13665. ret = wc_ed25519_export_public(&key, pub, NULL);
  13666. }
  13667. if (ret == BAD_FUNC_ARG) {
  13668. ret = 0;
  13669. } else if (ret == 0) {
  13670. ret = SSL_FATAL_ERROR;
  13671. }
  13672. }
  13673. }
  13674. printf(resultFmt, ret == 0 ? passed : failed);
  13675. printf(testingFmt, "wc_ed25519_export_private_only()");
  13676. if (ret == 0) {
  13677. ret = wc_ed25519_export_private_only(&key, priv, &privSz);
  13678. if (ret == 0 && (privSz != ED25519_KEY_SIZE
  13679. || XMEMCMP(key.k, priv, privSz) != 0)) {
  13680. ret = SSL_FATAL_ERROR;
  13681. }
  13682. if (ret == 0) {
  13683. ret = wc_ed25519_export_private_only(NULL, priv, &privSz);
  13684. if (ret == BAD_FUNC_ARG) {
  13685. ret = wc_ed25519_export_private_only(&key, NULL, &privSz);
  13686. }
  13687. if (ret == BAD_FUNC_ARG) {
  13688. ret = wc_ed25519_export_private_only(&key, priv, NULL);
  13689. }
  13690. if (ret == BAD_FUNC_ARG) {
  13691. ret = 0;
  13692. } else if (ret == 0) {
  13693. ret = SSL_FATAL_ERROR;
  13694. }
  13695. }
  13696. }
  13697. printf(resultFmt, ret == 0 ? passed : failed);
  13698. if (wc_FreeRng(&rng) && ret == 0) {
  13699. ret = SSL_FATAL_ERROR;
  13700. }
  13701. wc_ed25519_free(&key);
  13702. #endif
  13703. return ret;
  13704. } /* END test_wc_ed25519_export */
  13705. /*
  13706. * Testing wc_ed25519_size()
  13707. */
  13708. static int test_wc_ed25519_size (void)
  13709. {
  13710. int ret = 0;
  13711. #if defined(HAVE_ED25519)
  13712. WC_RNG rng;
  13713. ed25519_key key;
  13714. ret = wc_InitRng(&rng);
  13715. if (ret != 0) {
  13716. return ret;
  13717. }
  13718. ret = wc_ed25519_init(&key);
  13719. if (ret != 0) {
  13720. wc_FreeRng(&rng);
  13721. return ret;
  13722. }
  13723. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  13724. if (ret != 0) {
  13725. wc_FreeRng(&rng);
  13726. wc_ed25519_free(&key);
  13727. return ret;
  13728. }
  13729. printf(testingFmt, "wc_ed25519_size()");
  13730. ret = wc_ed25519_size(&key);
  13731. /* Test bad args. */
  13732. if (ret == ED25519_KEY_SIZE) {
  13733. ret = wc_ed25519_size(NULL);
  13734. if (ret == BAD_FUNC_ARG) {
  13735. ret = 0;
  13736. }
  13737. }
  13738. printf(resultFmt, ret == 0 ? passed : failed);
  13739. if (ret == 0) {
  13740. printf(testingFmt, "wc_ed25519_sig_size()");
  13741. ret = wc_ed25519_sig_size(&key);
  13742. if (ret == ED25519_SIG_SIZE) {
  13743. ret = 0;
  13744. }
  13745. /* Test bad args. */
  13746. if (ret == 0) {
  13747. ret = wc_ed25519_sig_size(NULL);
  13748. if (ret == BAD_FUNC_ARG) {
  13749. ret = 0;
  13750. }
  13751. }
  13752. printf(resultFmt, ret == 0 ? passed : failed);
  13753. } /* END wc_ed25519_sig_size() */
  13754. if (ret == 0) {
  13755. printf(testingFmt, "wc_ed25519_pub_size");
  13756. ret = wc_ed25519_pub_size(&key);
  13757. if (ret == ED25519_PUB_KEY_SIZE) {
  13758. ret = 0;
  13759. }
  13760. if (ret == 0) {
  13761. ret = wc_ed25519_pub_size(NULL);
  13762. if (ret == BAD_FUNC_ARG) {
  13763. ret = 0;
  13764. }
  13765. }
  13766. printf(resultFmt, ret == 0 ? passed : failed);
  13767. } /* END wc_ed25519_pub_size */
  13768. if (ret == 0) {
  13769. printf(testingFmt, "wc_ed25519_priv_size");
  13770. ret = wc_ed25519_priv_size(&key);
  13771. if (ret == ED25519_PRV_KEY_SIZE) {
  13772. ret = 0;
  13773. }
  13774. if (ret == 0) {
  13775. ret = wc_ed25519_priv_size(NULL);
  13776. if (ret == BAD_FUNC_ARG) {
  13777. ret = 0;
  13778. }
  13779. }
  13780. printf(resultFmt, ret == 0 ? passed : failed);
  13781. } /* END wc_ed25519_pub_size */
  13782. if (wc_FreeRng(&rng) && ret == 0) {
  13783. ret = SSL_FATAL_ERROR;
  13784. }
  13785. wc_ed25519_free(&key);
  13786. #endif
  13787. return ret;
  13788. } /* END test_wc_ed25519_size */
  13789. /*
  13790. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  13791. */
  13792. static int test_wc_ed25519_exportKey (void)
  13793. {
  13794. int ret = 0;
  13795. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  13796. WC_RNG rng;
  13797. ed25519_key key;
  13798. byte priv[ED25519_PRV_KEY_SIZE];
  13799. byte pub[ED25519_PUB_KEY_SIZE];
  13800. byte privOnly[ED25519_PRV_KEY_SIZE];
  13801. word32 privSz = sizeof(priv);
  13802. word32 pubSz = sizeof(pub);
  13803. word32 privOnlySz = sizeof(privOnly);
  13804. ret = wc_InitRng(&rng);
  13805. if (ret != 0) {
  13806. return ret;
  13807. }
  13808. ret = wc_ed25519_init(&key);
  13809. if (ret != 0) {
  13810. wc_FreeRng(&rng);
  13811. return ret;
  13812. }
  13813. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  13814. if (ret != 0) {
  13815. wc_FreeRng(&rng);
  13816. wc_ed25519_free(&key);
  13817. return ret;
  13818. }
  13819. printf(testingFmt, "wc_ed25519_export_private()");
  13820. ret = wc_ed25519_export_private(&key, privOnly, &privOnlySz);
  13821. if (ret == 0) {
  13822. ret = wc_ed25519_export_private(NULL, privOnly, &privOnlySz);
  13823. if (ret == BAD_FUNC_ARG) {
  13824. ret = wc_ed25519_export_private(&key, NULL, &privOnlySz);
  13825. }
  13826. if (ret == BAD_FUNC_ARG) {
  13827. ret = wc_ed25519_export_private(&key, privOnly, NULL);
  13828. }
  13829. if (ret == BAD_FUNC_ARG) {
  13830. ret = 0;
  13831. } else if (ret == 0) {
  13832. ret = SSL_FATAL_ERROR;
  13833. }
  13834. }
  13835. printf(resultFmt, ret == 0 ? passed : failed);
  13836. if (ret == 0) {
  13837. printf(testingFmt, "wc_ed25519_export_key()");
  13838. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz);
  13839. if (ret == 0) {
  13840. ret = wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz);
  13841. if (ret == BAD_FUNC_ARG) {
  13842. ret = wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz);
  13843. }
  13844. if (ret == BAD_FUNC_ARG) {
  13845. ret = wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz);
  13846. }
  13847. if (ret == BAD_FUNC_ARG) {
  13848. ret = wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz);
  13849. }
  13850. if (ret == BAD_FUNC_ARG) {
  13851. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, NULL);
  13852. }
  13853. if (ret == BAD_FUNC_ARG) {
  13854. ret = 0;
  13855. } else if (ret == 0) {
  13856. ret = SSL_FATAL_ERROR;
  13857. }
  13858. }
  13859. printf(resultFmt, ret == 0 ? passed : failed);
  13860. } /* END wc_ed25519_export_key() */
  13861. /* Cross check output. */
  13862. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  13863. ret = SSL_FATAL_ERROR;
  13864. }
  13865. if (wc_FreeRng(&rng) && ret == 0) {
  13866. ret = SSL_FATAL_ERROR;
  13867. }
  13868. wc_ed25519_free(&key);
  13869. #endif
  13870. return ret;
  13871. } /* END test_wc_ed25519_exportKey */
  13872. /*
  13873. * Testing wc_Ed25519PublicKeyToDer
  13874. */
  13875. static int test_wc_Ed25519PublicKeyToDer (void)
  13876. {
  13877. int ret = 0;
  13878. #if defined(HAVE_ED25519) && (defined(WOLFSSL_CERT_GEN) || \
  13879. defined(WOLFSSL_KEY_GEN))
  13880. int tmp;
  13881. ed25519_key key;
  13882. byte derBuf[1024];
  13883. printf(testingFmt, "wc_Ed25519PublicKeyToDer()");
  13884. /* Test bad args */
  13885. tmp = wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0);
  13886. if (tmp != BAD_FUNC_ARG) {
  13887. ret = SSL_FATAL_ERROR;
  13888. }
  13889. if (ret == 0) {
  13890. wc_ed25519_init(&key);
  13891. tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0);
  13892. if (tmp != BUFFER_E) {
  13893. ret = SSL_FATAL_ERROR;
  13894. }
  13895. wc_ed25519_free(&key);
  13896. }
  13897. /* Test good args */
  13898. if (ret == 0) {
  13899. WC_RNG rng;
  13900. ret = wc_InitRng(&rng);
  13901. if (ret != 0) {
  13902. return ret;
  13903. }
  13904. ret = wc_ed25519_init(&key);
  13905. if (ret != 0) {
  13906. wc_FreeRng(&rng);
  13907. return ret;
  13908. }
  13909. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  13910. if (ret != 0) {
  13911. wc_FreeRng(&rng);
  13912. wc_ed25519_free(&key);
  13913. return ret;
  13914. }
  13915. tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1);
  13916. if (tmp <= 0) {
  13917. ret = SSL_FATAL_ERROR;
  13918. }
  13919. wc_FreeRng(&rng);
  13920. wc_ed25519_free(&key);
  13921. }
  13922. printf(resultFmt, ret == 0 ? passed : failed);
  13923. #endif
  13924. return ret;
  13925. } /* END testing wc_Ed25519PublicKeyToDer */
  13926. /*
  13927. * Testing wc_curve25519_init and wc_curve25519_free.
  13928. */
  13929. static int test_wc_curve25519_init (void)
  13930. {
  13931. int ret = 0;
  13932. #if defined(HAVE_CURVE25519)
  13933. curve25519_key key;
  13934. printf(testingFmt, "wc_curve25519_init()");
  13935. ret = wc_curve25519_init(&key);
  13936. /* Test bad args for wc_curve25519_init */
  13937. if (ret == 0) {
  13938. ret = wc_curve25519_init(NULL);
  13939. if (ret == BAD_FUNC_ARG) {
  13940. ret = 0;
  13941. } else if (ret == 0) {
  13942. ret = SSL_FATAL_ERROR;
  13943. }
  13944. }
  13945. printf(resultFmt, ret == 0 ? passed : failed);
  13946. /* Test good args for wc_curve_25519_free */
  13947. wc_curve25519_free(&key);
  13948. wc_curve25519_free(NULL);
  13949. #endif
  13950. return ret;
  13951. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  13952. /*
  13953. * Testing test_wc_curve25519_size.
  13954. */
  13955. static int test_wc_curve25519_size (void)
  13956. {
  13957. int ret = 0;
  13958. #if defined(HAVE_CURVE25519)
  13959. curve25519_key key;
  13960. printf(testingFmt, "wc_curve25519_size()");
  13961. ret = wc_curve25519_init(&key);
  13962. /* Test good args for wc_curve25519_size */
  13963. if (ret == 0) {
  13964. ret = wc_curve25519_size(&key);
  13965. }
  13966. /* Test bad args for wc_curve25519_size */
  13967. if (ret != 0) {
  13968. ret = wc_curve25519_size(NULL);
  13969. }
  13970. printf(resultFmt, ret == 0 ? passed : failed);
  13971. wc_curve25519_free(&key);
  13972. #endif
  13973. return ret;
  13974. } /* END test_wc_curve25519_size*/
  13975. /*
  13976. * Testing test_wc_curve25519_export_key_raw().
  13977. */
  13978. static int test_wc_curve25519_export_key_raw (void)
  13979. {
  13980. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  13981. curve25519_key key;
  13982. WC_RNG rng;
  13983. byte privateKey[CURVE25519_KEYSIZE];
  13984. byte publicKey[CURVE25519_KEYSIZE];
  13985. word32 prvkSz;
  13986. word32 pubkSz;
  13987. byte prik[CURVE25519_KEYSIZE];
  13988. byte pubk[CURVE25519_KEYSIZE];
  13989. word32 prksz;
  13990. word32 pbksz;
  13991. printf(testingFmt, "wc_curve25519_export_key_raw()");
  13992. if(0 != wc_InitRng(&rng)){
  13993. printf(testingFmt, "failed due to wc_InitRng");
  13994. fflush( stdout );
  13995. return 1;
  13996. }
  13997. if(0 != wc_curve25519_init(&key)){
  13998. printf(testingFmt, "failed due to wc_curve25519_init");
  13999. fflush( stdout );
  14000. wc_FreeRng(&rng);
  14001. return 1;
  14002. }
  14003. if(0 != wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key)){
  14004. printf(testingFmt, "failed due to wc_curve25519_make_key");
  14005. fflush( stdout );
  14006. wc_curve25519_free(&key);
  14007. wc_FreeRng(&rng);
  14008. return 1;
  14009. }
  14010. /*
  14011. bad-argument-test cases
  14012. target function sould return BAD_FUNC_ARG
  14013. */
  14014. prvkSz = CURVE25519_KEYSIZE;
  14015. pubkSz = CURVE25519_KEYSIZE;
  14016. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  14017. NULL , privateKey, &prvkSz, publicKey, &pubkSz)){
  14018. printf(testingFmt,"failed at bad-arg-case-1.");
  14019. fflush( stdout );
  14020. wc_curve25519_free(&key);
  14021. wc_FreeRng(&rng);
  14022. return 1;
  14023. }
  14024. prvkSz = CURVE25519_KEYSIZE;
  14025. pubkSz = CURVE25519_KEYSIZE;
  14026. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  14027. &key , NULL, &prvkSz, publicKey, &pubkSz)){
  14028. printf(testingFmt,"failed at bad-arg-case-2.");
  14029. fflush( stdout );
  14030. wc_curve25519_free(&key);
  14031. wc_FreeRng(&rng);
  14032. return 1;
  14033. }
  14034. prvkSz = CURVE25519_KEYSIZE;
  14035. pubkSz = CURVE25519_KEYSIZE;
  14036. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  14037. &key , privateKey, NULL, publicKey, &pubkSz)){
  14038. printf(testingFmt,"failed at bad-arg-case-3.");
  14039. fflush( stdout );
  14040. wc_curve25519_free(&key);
  14041. wc_FreeRng(&rng);
  14042. return 1;
  14043. }
  14044. prvkSz = CURVE25519_KEYSIZE;
  14045. pubkSz = CURVE25519_KEYSIZE;
  14046. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  14047. &key , privateKey, &prvkSz, NULL, &pubkSz)){
  14048. printf(testingFmt,"failed at bad-arg-case-4.");
  14049. fflush( stdout );
  14050. wc_curve25519_free(&key);
  14051. wc_FreeRng(&rng);
  14052. return 1;
  14053. }
  14054. prvkSz = CURVE25519_KEYSIZE;
  14055. pubkSz = CURVE25519_KEYSIZE;
  14056. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  14057. &key , privateKey, &prvkSz, publicKey, NULL )){
  14058. printf(testingFmt,"failed at bad-arg-case-5.");
  14059. fflush( stdout );
  14060. wc_curve25519_free(&key);
  14061. wc_FreeRng(&rng);
  14062. return 1;
  14063. }
  14064. /*
  14065. cross-testing
  14066. */
  14067. prksz = CURVE25519_KEYSIZE;
  14068. if( 0 != wc_curve25519_export_private_raw(&key, prik, &prksz)){
  14069. printf(testingFmt,"failed due to wc_curve25519_export_private_raw");
  14070. fflush( stdout );
  14071. wc_curve25519_free(&key);
  14072. wc_FreeRng(&rng);
  14073. return 1;
  14074. }
  14075. pbksz = CURVE25519_KEYSIZE;
  14076. if(0 != wc_curve25519_export_public(&key, pubk, &pbksz)){
  14077. printf(testingFmt,"failed due to wc_curve25519_export_public");
  14078. fflush( stdout );
  14079. wc_curve25519_free(&key);
  14080. wc_FreeRng(&rng);
  14081. return 1;
  14082. }
  14083. prvkSz = CURVE25519_KEYSIZE;
  14084. pubkSz = CURVE25519_KEYSIZE;
  14085. if(0 != wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  14086. publicKey, &pubkSz)){
  14087. printf(testingFmt,"failed due to wc_curve25519_export_key_raw");
  14088. fflush( stdout );
  14089. wc_curve25519_free(&key);
  14090. wc_FreeRng(&rng);
  14091. return 1;
  14092. }
  14093. if((prksz == CURVE25519_KEYSIZE) &&
  14094. (pbksz == CURVE25519_KEYSIZE) &&
  14095. (prvkSz == CURVE25519_KEYSIZE) &&
  14096. (pubkSz == CURVE25519_KEYSIZE)){
  14097. if( 0 == XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE) &&
  14098. 0 == XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE)){
  14099. printf(resultFmt,passed);
  14100. fflush( stdout );
  14101. wc_curve25519_free(&key);
  14102. wc_FreeRng(&rng);
  14103. return 0;
  14104. }
  14105. else{
  14106. printf(testingFmt,"failed due to key-contents-inconsistency.");
  14107. fflush( stdout );
  14108. wc_curve25519_free(&key);
  14109. wc_FreeRng(&rng);
  14110. return 1;
  14111. }
  14112. }
  14113. else{
  14114. printf(testingFmt,"failed due to bad-key-size.");
  14115. fflush( stdout );
  14116. wc_curve25519_free(&key);
  14117. wc_FreeRng(&rng);
  14118. return 1;
  14119. }
  14120. #endif
  14121. fflush( stdout );
  14122. return 0;
  14123. } /* end of test_wc_curve25519_export_key_raw */
  14124. /*
  14125. * Testing test_wc_curve25519_export_key_raw_ex().
  14126. */
  14127. static int test_wc_curve25519_export_key_raw_ex (void)
  14128. {
  14129. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  14130. curve25519_key key;
  14131. WC_RNG rng;
  14132. byte privateKey[CURVE25519_KEYSIZE];
  14133. byte publicKey[CURVE25519_KEYSIZE];
  14134. word32 prvkSz;
  14135. word32 pubkSz;
  14136. byte prik[CURVE25519_KEYSIZE];
  14137. byte pubk[CURVE25519_KEYSIZE];
  14138. word32 prksz;
  14139. word32 pbksz;
  14140. printf(testingFmt, "wc_curve25519_export_key_raw_ex()");
  14141. if(0 != wc_InitRng(&rng)){
  14142. printf(testingFmt, "failed due to wc_InitRng");
  14143. fflush( stdout );
  14144. return 1;
  14145. }
  14146. if(0 != wc_curve25519_init(&key)){
  14147. printf(testingFmt, "failed due to wc_curve25519_init");
  14148. fflush( stdout );
  14149. wc_FreeRng(&rng);
  14150. return 1;
  14151. }
  14152. if(0 != wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key)){
  14153. printf(testingFmt, "failed due to wc_curve25519_make_key");
  14154. fflush( stdout );
  14155. wc_curve25519_free(&key);
  14156. wc_FreeRng(&rng);
  14157. return 1;
  14158. }
  14159. /*
  14160. bad-argument-test cases
  14161. target function sould return BAD_FUNC_ARG
  14162. */
  14163. prvkSz = CURVE25519_KEYSIZE;
  14164. pubkSz = CURVE25519_KEYSIZE;
  14165. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL , privateKey,
  14166. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
  14167. printf(testingFmt,"failed at bad-arg-case-1.");
  14168. fflush( stdout );
  14169. wc_curve25519_free(&key);
  14170. wc_FreeRng(&rng);
  14171. return 1;
  14172. }
  14173. prvkSz = CURVE25519_KEYSIZE;
  14174. pubkSz = CURVE25519_KEYSIZE;
  14175. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key , NULL,
  14176. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
  14177. printf(testingFmt,"failed at bad-arg-case-2.");
  14178. fflush( stdout );
  14179. wc_curve25519_free(&key);
  14180. wc_FreeRng(&rng);
  14181. return 1;
  14182. }
  14183. prvkSz = CURVE25519_KEYSIZE;
  14184. pubkSz = CURVE25519_KEYSIZE;
  14185. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key,privateKey,
  14186. NULL,publicKey, &pubkSz,EC25519_LITTLE_ENDIAN)){
  14187. printf(testingFmt,"failed at bad-arg-case-3.");
  14188. fflush( stdout );
  14189. wc_curve25519_free(&key);
  14190. wc_FreeRng(&rng);
  14191. return 1;
  14192. }
  14193. prvkSz = CURVE25519_KEYSIZE;
  14194. pubkSz = CURVE25519_KEYSIZE;
  14195. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  14196. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN)){
  14197. printf(testingFmt,"failed at bad-arg-case-4.");
  14198. fflush( stdout );
  14199. wc_curve25519_free(&key);
  14200. wc_FreeRng(&rng);
  14201. return 1;
  14202. }
  14203. prvkSz = CURVE25519_KEYSIZE;
  14204. pubkSz = CURVE25519_KEYSIZE;
  14205. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  14206. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN)){
  14207. printf(testingFmt,"failed at bad-arg-case-5.");
  14208. fflush( stdout );
  14209. wc_curve25519_free(&key);
  14210. wc_FreeRng(&rng);
  14211. return 1;
  14212. }
  14213. prvkSz = CURVE25519_KEYSIZE;
  14214. pubkSz = CURVE25519_KEYSIZE;
  14215. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL, privateKey,
  14216. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  14217. printf(testingFmt,"failed at bad-arg-case-6.");
  14218. fflush( stdout );
  14219. wc_curve25519_free(&key);
  14220. wc_FreeRng(&rng);
  14221. return 1;
  14222. }
  14223. prvkSz = CURVE25519_KEYSIZE;
  14224. pubkSz = CURVE25519_KEYSIZE;
  14225. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, NULL, &prvkSz,
  14226. publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  14227. printf(testingFmt,"failed at bad-arg-case-7.");
  14228. fflush( stdout );
  14229. wc_curve25519_free(&key);
  14230. wc_FreeRng(&rng);
  14231. return 1;
  14232. }
  14233. prvkSz = CURVE25519_KEYSIZE;
  14234. pubkSz = CURVE25519_KEYSIZE;
  14235. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  14236. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  14237. printf(testingFmt,"failed at bad-arg-case-8.");
  14238. fflush( stdout );
  14239. wc_curve25519_free(&key);
  14240. wc_FreeRng(&rng);
  14241. return 1;
  14242. }
  14243. prvkSz = CURVE25519_KEYSIZE;
  14244. pubkSz = CURVE25519_KEYSIZE;
  14245. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  14246. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN)){
  14247. printf(testingFmt,"failed at bad-arg-case-9.");
  14248. fflush( stdout );
  14249. wc_curve25519_free(&key);
  14250. wc_FreeRng(&rng);
  14251. return 1;
  14252. }
  14253. prvkSz = CURVE25519_KEYSIZE;
  14254. pubkSz = CURVE25519_KEYSIZE;
  14255. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  14256. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN)){
  14257. printf(testingFmt,"failed at bad-arg-case-10.");
  14258. fflush( stdout );
  14259. wc_curve25519_free(&key);
  14260. wc_FreeRng(&rng);
  14261. return 1;
  14262. }
  14263. /* illegal value for endien */
  14264. prvkSz = CURVE25519_KEYSIZE;
  14265. pubkSz = CURVE25519_KEYSIZE;
  14266. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  14267. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN + 10 )){
  14268. printf(testingFmt,"failed at bad-arg-case-11.");
  14269. fflush( stdout );
  14270. wc_curve25519_free(&key);
  14271. wc_FreeRng(&rng);
  14272. return 1;
  14273. }
  14274. /*
  14275. cross-testing
  14276. */
  14277. prksz = CURVE25519_KEYSIZE;
  14278. if(0 != wc_curve25519_export_private_raw( &key, prik, &prksz )){
  14279. printf(testingFmt,"failed due to wc_curve25519_export_private_raw");
  14280. fflush( stdout );
  14281. wc_curve25519_free(&key);
  14282. wc_FreeRng(&rng);
  14283. return 1;
  14284. }
  14285. pbksz = CURVE25519_KEYSIZE;
  14286. if(0 != wc_curve25519_export_public( &key, pubk, &pbksz )){
  14287. printf(testingFmt,"failed due to wc_curve25519_export_public");
  14288. fflush( stdout );
  14289. wc_curve25519_free(&key);
  14290. wc_FreeRng(&rng);
  14291. return 1;
  14292. }
  14293. prvkSz = CURVE25519_KEYSIZE;
  14294. pubkSz = CURVE25519_KEYSIZE;
  14295. if(0 != wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  14296. publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
  14297. printf(testingFmt,"failed due to wc_curve25519_export_key_raw_ex");
  14298. fflush( stdout );
  14299. wc_curve25519_free(&key);
  14300. wc_FreeRng(&rng);
  14301. return 1;
  14302. }
  14303. if( prksz == CURVE25519_KEYSIZE &&
  14304. pbksz == CURVE25519_KEYSIZE &&
  14305. prvkSz == CURVE25519_KEYSIZE &&
  14306. pubkSz == CURVE25519_KEYSIZE ){
  14307. if( 0 == XMEMCMP( privateKey, prik, CURVE25519_KEYSIZE ) &&
  14308. 0 == XMEMCMP( publicKey, pubk, CURVE25519_KEYSIZE )){
  14309. if( 0 == wc_curve25519_export_key_raw_ex( &key, privateKey,
  14310. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
  14311. if( prvkSz == CURVE25519_KEYSIZE &&
  14312. pubkSz == CURVE25519_KEYSIZE ){
  14313. ; /* proceed to the next test */
  14314. }
  14315. else{
  14316. printf(testingFmt,"failed due to key-size-inconsistency");
  14317. fflush( stdout );
  14318. wc_curve25519_free(&key);
  14319. wc_FreeRng(&rng);
  14320. return 1;
  14321. }
  14322. }
  14323. else{
  14324. printf(testingFmt,
  14325. "failed due to wc_curve25519_export_key_raw_ex");
  14326. fflush( stdout );
  14327. wc_curve25519_free(&key);
  14328. wc_FreeRng(&rng);
  14329. return 1;
  14330. }
  14331. }
  14332. else{
  14333. printf(testingFmt,"failed due to key-contents-inconsistency");
  14334. fflush( stdout );
  14335. wc_curve25519_free(&key);
  14336. wc_FreeRng(&rng);
  14337. return 1;
  14338. }
  14339. }
  14340. else{
  14341. printf(testingFmt,"failed due to bad-key-size");
  14342. fflush( stdout );
  14343. wc_curve25519_free(&key);
  14344. wc_FreeRng(&rng);
  14345. return 1;
  14346. }
  14347. /*
  14348. try once with another endian
  14349. */
  14350. prvkSz = CURVE25519_KEYSIZE;
  14351. pubkSz = CURVE25519_KEYSIZE;
  14352. if( 0 == wc_curve25519_export_key_raw_ex( &key, privateKey,
  14353. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  14354. if( prvkSz == CURVE25519_KEYSIZE &&
  14355. pubkSz == CURVE25519_KEYSIZE ){
  14356. /* no more test*/
  14357. printf(resultFmt, passed );
  14358. fflush( stdout );
  14359. wc_curve25519_free(&key);
  14360. wc_FreeRng(&rng);
  14361. return 0;
  14362. }
  14363. else{
  14364. printf(testingFmt,"failed due to key-size-inconsistency");
  14365. fflush( stdout );
  14366. wc_curve25519_free(&key);
  14367. wc_FreeRng(&rng);
  14368. return 1;
  14369. }
  14370. }
  14371. else{
  14372. printf(testingFmt,
  14373. "failed due to wc_curve25519_export_key_raw_ex(BIGENDIAN)");
  14374. fflush( stdout );
  14375. wc_curve25519_free(&key);
  14376. wc_FreeRng(&rng);
  14377. return 1;
  14378. }
  14379. #endif
  14380. return 0;
  14381. } /* end of test_wc_curve25519_export_key_raw_ex */
  14382. /*
  14383. * Testing wc_curve25519_make_key
  14384. */
  14385. static int test_wc_curve25519_make_key (void)
  14386. {
  14387. int ret = 0;
  14388. #if defined(HAVE_CURVE25519)
  14389. WC_RNG rng;
  14390. curve25519_key key;
  14391. int keysize;
  14392. printf(testingFmt, "wc_curve25519_make_key()");
  14393. ret = wc_curve25519_init(&key);
  14394. if (ret == 0) {
  14395. ret = wc_InitRng(&rng);
  14396. }
  14397. if (ret == 0) {
  14398. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  14399. if (ret == 0) {
  14400. keysize = wc_curve25519_size(&key);
  14401. if (keysize != CURVE25519_KEYSIZE) {
  14402. ret = SSL_FATAL_ERROR;
  14403. }
  14404. }
  14405. if (ret == 0) {
  14406. ret = wc_curve25519_make_key(&rng, keysize, &key);
  14407. }
  14408. }
  14409. /*test bad cases*/
  14410. if (ret == 0) {
  14411. ret = wc_curve25519_make_key(NULL, 0, NULL);
  14412. if (ret == BAD_FUNC_ARG) {
  14413. ret = 0;
  14414. }
  14415. }
  14416. if (ret == 0) {
  14417. ret = wc_curve25519_make_key(&rng, keysize, NULL);
  14418. if (ret == BAD_FUNC_ARG) {
  14419. ret = 0;
  14420. }
  14421. }
  14422. if (ret == 0) {
  14423. ret = wc_curve25519_make_key(NULL, keysize, &key);
  14424. if (ret == BAD_FUNC_ARG) {
  14425. ret = 0;
  14426. }
  14427. }
  14428. if (ret == 0) {
  14429. ret = wc_curve25519_make_key(&rng, 0, &key);
  14430. if (ret == ECC_BAD_ARG_E) {
  14431. ret = 0;
  14432. }
  14433. }
  14434. printf(resultFmt, ret == 0 ? passed : failed);
  14435. wc_curve25519_free(&key);
  14436. wc_FreeRng(&rng);
  14437. #endif
  14438. return ret;
  14439. } /*END test_wc_curve25519_make_key*/
  14440. /*
  14441. * Testing wc_curve25519_shared_secret_ex
  14442. */
  14443. static int test_wc_curve25519_shared_secret_ex (void)
  14444. {
  14445. int ret = 0;
  14446. #if defined(HAVE_CURVE25519)
  14447. WC_RNG rng;
  14448. curve25519_key private_key, public_key;
  14449. byte out[CURVE25519_KEYSIZE];
  14450. word32 outLen = sizeof(out);
  14451. int endian = EC25519_BIG_ENDIAN;
  14452. printf(testingFmt, "wc_curve25519_shared_secret_ex()");
  14453. ret = wc_curve25519_init(&private_key);
  14454. if (ret == 0) {
  14455. ret = wc_InitRng(&rng);
  14456. if (ret == 0) {
  14457. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key);
  14458. }
  14459. }
  14460. if (ret == 0) {
  14461. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key);
  14462. }
  14463. if (ret == 0) {
  14464. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  14465. &outLen, endian);
  14466. }
  14467. /*test bad cases*/
  14468. if (ret == 0) {
  14469. ret = wc_curve25519_shared_secret_ex(NULL, NULL, NULL,
  14470. 0, endian);
  14471. if (ret == BAD_FUNC_ARG) {
  14472. ret = 0;
  14473. }
  14474. }
  14475. if (ret == 0) {
  14476. ret = wc_curve25519_shared_secret_ex(NULL, &public_key, out,
  14477. &outLen, endian);
  14478. if (ret == BAD_FUNC_ARG) {
  14479. ret = 0;
  14480. }
  14481. }
  14482. if (ret == 0) {
  14483. ret = wc_curve25519_shared_secret_ex(&private_key, NULL, out,
  14484. &outLen, endian);
  14485. if (ret == BAD_FUNC_ARG) {
  14486. ret = 0;
  14487. }
  14488. }
  14489. if (ret == 0) {
  14490. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  14491. &outLen, endian);
  14492. if (ret == BAD_FUNC_ARG) {
  14493. ret = 0;
  14494. }
  14495. }
  14496. if (ret == 0) {
  14497. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  14498. NULL, endian);
  14499. if (ret == BAD_FUNC_ARG) {
  14500. ret = 0;
  14501. }
  14502. }
  14503. if (ret == 0) {
  14504. /*curve25519.c is checking for public_key size less than or equal to 0x7f,
  14505. *increasing to 0x8f checks for error being returned*/
  14506. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  14507. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  14508. &outLen, endian);
  14509. if (ret == ECC_BAD_ARG_E) {
  14510. ret = 0;
  14511. }
  14512. }
  14513. outLen = outLen - 2;
  14514. if (ret == 0) {
  14515. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  14516. &outLen, endian);
  14517. if (ret == BAD_FUNC_ARG) {
  14518. ret = 0;
  14519. }
  14520. }
  14521. printf(resultFmt, ret == 0 ? passed : failed);
  14522. wc_curve25519_free(&private_key);
  14523. wc_curve25519_free(&public_key);
  14524. wc_FreeRng(&rng);
  14525. #endif
  14526. return ret;
  14527. } /*END test_wc_curve25519_shared_secret_ex*/
  14528. /*
  14529. * Testing wc_curve25519_make_pub
  14530. */
  14531. static int test_wc_curve25519_make_pub (void)
  14532. {
  14533. int ret = 0;
  14534. #if defined(HAVE_CURVE25519)
  14535. WC_RNG rng;
  14536. curve25519_key key;
  14537. byte out[CURVE25519_KEYSIZE];
  14538. printf(testingFmt, "wc_curve25519_make_pub()");
  14539. ret = wc_curve25519_init(&key);
  14540. if (ret == 0) {
  14541. ret = wc_InitRng(&rng);
  14542. if (ret == 0) {
  14543. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  14544. }
  14545. }
  14546. if (ret == 0) {
  14547. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof key.k.point, key.k.point);
  14548. }
  14549. /*test bad cases*/
  14550. if (ret == 0) {
  14551. ret = wc_curve25519_make_pub((int)sizeof key.k.point - 1, key.k.point, (int)sizeof out, out);
  14552. if (ret == ECC_BAD_ARG_E) {
  14553. ret = 0;
  14554. }
  14555. }
  14556. if (ret == 0) {
  14557. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof key.k.point, NULL);
  14558. if (ret == ECC_BAD_ARG_E) {
  14559. ret = 0;
  14560. }
  14561. }
  14562. if (ret == 0) {
  14563. ret = wc_curve25519_make_pub((int)sizeof out - 1, out, (int)sizeof key.k.point, key.k.point);
  14564. if (ret == ECC_BAD_ARG_E) {
  14565. ret = 0;
  14566. }
  14567. }
  14568. if (ret == 0) {
  14569. ret = wc_curve25519_make_pub((int)sizeof out, NULL, (int)sizeof key.k.point, key.k.point);
  14570. if (ret == ECC_BAD_ARG_E) {
  14571. ret = 0;
  14572. }
  14573. }
  14574. if (ret == 0) {
  14575. /* verify clamping test */
  14576. key.k.point[0] |= ~248;
  14577. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof key.k.point, key.k.point);
  14578. if (ret == ECC_BAD_ARG_E) {
  14579. ret = 0;
  14580. }
  14581. key.k.point[0] &= 248;
  14582. }
  14583. /* repeat the expected-to-succeed test. */
  14584. if (ret == 0) {
  14585. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof key.k.point, key.k.point);
  14586. }
  14587. printf(resultFmt, ret == 0 ? passed : failed);
  14588. wc_curve25519_free(&key);
  14589. wc_FreeRng(&rng);
  14590. #endif
  14591. return ret;
  14592. } /*END test_wc_curve25519_make_pub */
  14593. /*
  14594. * Testing test_wc_curve25519_export_public_ex
  14595. */
  14596. static int test_wc_curve25519_export_public_ex (void)
  14597. {
  14598. int ret = 0;
  14599. #if defined(HAVE_CURVE25519)
  14600. WC_RNG rng;
  14601. curve25519_key key;
  14602. byte out[CURVE25519_KEYSIZE];
  14603. word32 outLen = sizeof(out);
  14604. int endian = EC25519_BIG_ENDIAN;
  14605. printf(testingFmt, "wc_curve25519_export_public_ex()");
  14606. ret = wc_curve25519_init(&key);
  14607. if (ret == 0) {
  14608. ret = wc_InitRng(&rng);
  14609. }
  14610. if (ret == 0) {
  14611. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  14612. if (ret == 0) {
  14613. ret = wc_curve25519_export_public(&key, out, &outLen);
  14614. }
  14615. if (ret == 0) {
  14616. ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
  14617. }
  14618. }
  14619. /*test bad cases*/
  14620. if (ret == 0) {
  14621. ret = wc_curve25519_export_public_ex(NULL, NULL, NULL, endian);
  14622. if (ret == BAD_FUNC_ARG) {
  14623. ret = 0;
  14624. }
  14625. }
  14626. if (ret == 0) {
  14627. ret = wc_curve25519_export_public_ex(NULL, out, &outLen, endian);
  14628. if (ret == BAD_FUNC_ARG) {
  14629. ret = 0;
  14630. }
  14631. }
  14632. if (ret == 0) {
  14633. ret = wc_curve25519_export_public_ex(&key, NULL, &outLen, endian);
  14634. if (ret == BAD_FUNC_ARG) {
  14635. ret = 0;
  14636. }
  14637. }
  14638. if (ret == 0) {
  14639. ret = wc_curve25519_export_public_ex(&key, out, NULL, endian);
  14640. if (ret == BAD_FUNC_ARG) {
  14641. ret = 0;
  14642. }
  14643. }
  14644. outLen = outLen - 2;
  14645. if (ret == 0) {
  14646. ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
  14647. if (ret == ECC_BAD_ARG_E) {
  14648. ret = 0;
  14649. }
  14650. }
  14651. printf(resultFmt, ret == 0 ? passed : failed);
  14652. wc_curve25519_free(&key);
  14653. wc_FreeRng(&rng);
  14654. #endif
  14655. return ret;
  14656. } /*END test_wc_curve25519_export_public_ex*/
  14657. /*
  14658. * Testing test_wc_curve25519_import_private_raw_ex
  14659. */
  14660. static int test_wc_curve25519_import_private_raw_ex (void)
  14661. {
  14662. int ret = 0;
  14663. #if defined(HAVE_CURVE25519)
  14664. WC_RNG rng;
  14665. curve25519_key key;
  14666. byte priv[CURVE25519_KEYSIZE];
  14667. byte pub[CURVE25519_KEYSIZE];
  14668. word32 privSz = sizeof(priv);
  14669. word32 pubSz = sizeof(pub);
  14670. int endian = EC25519_BIG_ENDIAN;
  14671. printf(testingFmt, "wc_curve25519_import_private_raw_ex()");
  14672. ret = wc_curve25519_init(&key);
  14673. if (ret == 0) {
  14674. ret = wc_InitRng(&rng);
  14675. }
  14676. if (ret == 0) {
  14677. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  14678. if (ret == 0) {
  14679. ret = wc_curve25519_export_private_raw_ex(&key, priv, &privSz, endian);
  14680. }
  14681. if (ret == 0) {
  14682. ret = wc_curve25519_export_public(&key, pub, &pubSz);
  14683. }
  14684. if (ret == 0) {
  14685. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  14686. &key, endian);
  14687. }
  14688. }
  14689. /*test bad cases*/
  14690. if (ret == 0) {
  14691. ret = wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  14692. endian);
  14693. if (ret == BAD_FUNC_ARG) {
  14694. ret = 0;
  14695. }
  14696. }
  14697. if (ret == 0) {
  14698. ret = wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  14699. &key, endian);
  14700. if (ret == BAD_FUNC_ARG) {
  14701. ret = 0;
  14702. }
  14703. }
  14704. if (ret == 0) {
  14705. ret = wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  14706. &key, endian);
  14707. if (ret == BAD_FUNC_ARG) {
  14708. ret = 0;
  14709. }
  14710. }
  14711. if (ret == 0) {
  14712. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  14713. NULL, endian);
  14714. if (ret == BAD_FUNC_ARG) {
  14715. ret = 0;
  14716. }
  14717. }
  14718. if (ret == 0) {
  14719. ret = wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  14720. &key, endian);
  14721. if (ret == ECC_BAD_ARG_E) {
  14722. ret = 0;
  14723. }
  14724. }
  14725. if (ret == 0) {
  14726. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  14727. &key, endian);
  14728. if (ret == ECC_BAD_ARG_E) {
  14729. ret = 0;
  14730. }
  14731. }
  14732. if (ret == 0) {
  14733. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  14734. &key, EC25519_LITTLE_ENDIAN);
  14735. }
  14736. printf(resultFmt, ret == 0 ? passed : failed);
  14737. wc_curve25519_free(&key);
  14738. wc_FreeRng(&rng);
  14739. #endif
  14740. return ret;
  14741. } /*END test_wc_curve25519_import_private_raw_ex*/
  14742. /*
  14743. * Testing test_wc_curve25519_import_private
  14744. */
  14745. static int test_wc_curve25519_import_private (void)
  14746. {
  14747. int ret = 0;
  14748. #if defined(HAVE_CURVE25519)
  14749. curve25519_key key;
  14750. WC_RNG rng;
  14751. byte priv[CURVE25519_KEYSIZE];
  14752. word32 privSz = sizeof(priv);
  14753. printf(testingFmt, "wc_curve25519_import_private()");
  14754. ret = wc_curve25519_init(&key);
  14755. if (ret == 0) {
  14756. ret = wc_InitRng(&rng);
  14757. }
  14758. if (ret == 0) {
  14759. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  14760. if (ret == 0) {
  14761. ret = wc_curve25519_export_private_raw(&key, priv, &privSz);
  14762. }
  14763. }
  14764. if (ret == 0) {
  14765. ret = wc_curve25519_import_private(priv, privSz, &key);
  14766. }
  14767. printf(resultFmt, ret == 0 ? passed : failed);
  14768. wc_curve25519_free(&key);
  14769. wc_FreeRng(&rng);
  14770. #endif
  14771. return ret;
  14772. } /*END test_wc_curve25519_import*/
  14773. /*
  14774. * Testing test_wc_curve25519_export_private_raw_ex
  14775. */
  14776. static int test_wc_curve25519_export_private_raw_ex (void)
  14777. {
  14778. int ret = 0;
  14779. #if defined(HAVE_CURVE25519)
  14780. WC_RNG rng;
  14781. curve25519_key key;
  14782. byte out[CURVE25519_KEYSIZE];
  14783. word32 outLen = sizeof(out);
  14784. int endian = EC25519_BIG_ENDIAN;
  14785. printf(testingFmt, "wc_curve25519_export_private_raw_ex()");
  14786. ret = wc_curve25519_init(&key);
  14787. if (ret == 0) {
  14788. ret = wc_InitRng(&rng);
  14789. }
  14790. if (ret == 0) {
  14791. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
  14792. }
  14793. /*test bad cases*/
  14794. if (ret == 0) {
  14795. ret = wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian);
  14796. if (ret == BAD_FUNC_ARG) {
  14797. ret = 0;
  14798. }
  14799. }
  14800. if (ret == 0) {
  14801. ret = wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian);
  14802. if (ret == BAD_FUNC_ARG) {
  14803. ret = 0;
  14804. }
  14805. }
  14806. if (ret == 0) {
  14807. ret = wc_curve25519_export_private_raw_ex(&key, NULL, &outLen, endian);
  14808. if (ret == BAD_FUNC_ARG) {
  14809. ret = 0;
  14810. }
  14811. }
  14812. if (ret == 0) {
  14813. ret = wc_curve25519_export_private_raw_ex(&key, out, NULL, endian);
  14814. if (ret == BAD_FUNC_ARG) {
  14815. ret = 0;
  14816. }
  14817. }
  14818. if (ret == 0) {
  14819. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  14820. EC25519_LITTLE_ENDIAN);
  14821. }
  14822. outLen = outLen - 2;
  14823. if (ret == 0) {
  14824. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
  14825. if (ret == ECC_BAD_ARG_E) {
  14826. ret = 0;
  14827. }
  14828. }
  14829. printf(resultFmt, ret == 0 ? passed : failed);
  14830. wc_curve25519_free(&key);
  14831. wc_FreeRng(&rng);
  14832. #endif
  14833. return ret;
  14834. }/*END test_wc_curve25519_export_private_raw_ex*/
  14835. /*
  14836. * Testing wc_ed448_make_key().
  14837. */
  14838. static int test_wc_ed448_make_key (void)
  14839. {
  14840. int ret = 0;
  14841. #if defined(HAVE_ED448)
  14842. ed448_key key;
  14843. WC_RNG rng;
  14844. ret = wc_InitRng(&rng);
  14845. if (ret == 0) {
  14846. ret = wc_ed448_init(&key);
  14847. }
  14848. printf(testingFmt, "wc_ed448_make_key()");
  14849. if (ret == 0) {
  14850. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  14851. }
  14852. /* Test bad args. */
  14853. if (ret == 0) {
  14854. ret = wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key);
  14855. if (ret == BAD_FUNC_ARG) {
  14856. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL);
  14857. }
  14858. if (ret == BAD_FUNC_ARG) {
  14859. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key);
  14860. }
  14861. if (ret == BAD_FUNC_ARG) {
  14862. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key);
  14863. }
  14864. if (ret == BAD_FUNC_ARG) {
  14865. ret = 0;
  14866. } else if (ret == 0) {
  14867. ret = SSL_FATAL_ERROR;
  14868. }
  14869. }
  14870. printf(resultFmt, ret == 0 ? passed : failed);
  14871. if (wc_FreeRng(&rng) && ret == 0) {
  14872. ret = SSL_FATAL_ERROR;
  14873. }
  14874. wc_ed448_free(&key);
  14875. #endif
  14876. return ret;
  14877. } /* END test_wc_ed448_make_key */
  14878. /*
  14879. * Testing wc_ed448_init()
  14880. */
  14881. static int test_wc_ed448_init (void)
  14882. {
  14883. int ret = 0;
  14884. #if defined(HAVE_ED448)
  14885. ed448_key key;
  14886. printf(testingFmt, "wc_ed448_init()");
  14887. ret = wc_ed448_init(&key);
  14888. /* Test bad args. */
  14889. if (ret == 0) {
  14890. ret = wc_ed448_init(NULL);
  14891. if (ret == BAD_FUNC_ARG) {
  14892. ret = 0;
  14893. } else if (ret == 0) {
  14894. ret = SSL_FATAL_ERROR;
  14895. }
  14896. }
  14897. printf(resultFmt, ret == 0 ? passed : failed);
  14898. wc_ed448_free(&key);
  14899. #endif
  14900. return ret;
  14901. } /* END test_wc_ed448_init */
  14902. /*
  14903. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  14904. */
  14905. static int test_wc_ed448_sign_msg (void)
  14906. {
  14907. int ret = 0;
  14908. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  14909. WC_RNG rng;
  14910. ed448_key key;
  14911. byte msg[] = "Everybody gets Friday off.\n";
  14912. byte sig[ED448_SIG_SIZE];
  14913. word32 msglen = sizeof(msg);
  14914. word32 siglen = sizeof(sig);
  14915. word32 badSigLen = sizeof(sig) - 1;
  14916. int verify_ok = 0; /*1 = Verify success.*/
  14917. /* Initialize stack variables. */
  14918. XMEMSET(sig, 0, siglen);
  14919. /* Initialize key. */
  14920. ret = wc_InitRng(&rng);
  14921. if (ret == 0) {
  14922. ret = wc_ed448_init(&key);
  14923. if (ret == 0) {
  14924. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  14925. }
  14926. }
  14927. printf(testingFmt, "wc_ed448_sign_msg()");
  14928. if (ret == 0) {
  14929. ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0);
  14930. }
  14931. /* Test bad args. */
  14932. if (ret == 0 && siglen == ED448_SIG_SIZE) {
  14933. ret = wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0);
  14934. if (ret == BAD_FUNC_ARG) {
  14935. ret = wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0);
  14936. }
  14937. if (ret == BAD_FUNC_ARG) {
  14938. ret = wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0);
  14939. }
  14940. if (ret == BAD_FUNC_ARG) {
  14941. ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0);
  14942. }
  14943. if (ret == BAD_FUNC_ARG) {
  14944. ret = wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key,
  14945. NULL, 0);
  14946. }
  14947. if (ret == BUFFER_E && badSigLen == ED448_SIG_SIZE) {
  14948. badSigLen -= 1;
  14949. ret = 0;
  14950. } else if (ret == 0) {
  14951. ret = SSL_FATAL_ERROR;
  14952. }
  14953. } /* END sign */
  14954. printf(resultFmt, ret == 0 ? passed : failed);
  14955. #ifdef HAVE_ED448_VERIFY
  14956. printf(testingFmt, "wc_ed448_verify_msg()");
  14957. if (ret == 0) {
  14958. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  14959. &key, NULL, 0);
  14960. if (ret == 0 && verify_ok == 1) {
  14961. ret = 0;
  14962. } else if (ret == 0) {
  14963. ret = SSL_FATAL_ERROR;
  14964. }
  14965. /* Test bad args. */
  14966. if (ret == 0) {
  14967. AssertIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg,
  14968. msglen, &verify_ok, &key,
  14969. NULL, 0),
  14970. BAD_FUNC_ARG);
  14971. AssertIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg,
  14972. msglen, &verify_ok, &key,
  14973. NULL, 0),
  14974. BAD_FUNC_ARG);
  14975. ret = wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  14976. &key, NULL, 0);
  14977. if (ret == BAD_FUNC_ARG) {
  14978. ret = wc_ed448_verify_msg(sig, siglen, NULL, msglen,
  14979. &verify_ok, &key, NULL, 0);
  14980. }
  14981. if (ret == BAD_FUNC_ARG) {
  14982. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
  14983. NULL, &key, NULL, 0);
  14984. }
  14985. if (ret == BAD_FUNC_ARG) {
  14986. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
  14987. &verify_ok, NULL, NULL, 0);
  14988. }
  14989. if (ret == BAD_FUNC_ARG) {
  14990. ret = wc_ed448_verify_msg(sig, badSigLen, msg, msglen,
  14991. &verify_ok, &key, NULL, 0);
  14992. }
  14993. if (ret == BAD_FUNC_ARG) {
  14994. ret = 0;
  14995. } else if (ret == 0) {
  14996. ret = SSL_FATAL_ERROR;
  14997. }
  14998. }
  14999. } /* END verify. */
  15000. printf(resultFmt, ret == 0 ? passed : failed);
  15001. #endif /* Verify. */
  15002. if (wc_FreeRng(&rng) && ret == 0) {
  15003. ret = SSL_FATAL_ERROR;
  15004. }
  15005. wc_ed448_free(&key);
  15006. #endif
  15007. return ret;
  15008. } /* END test_wc_ed448_sign_msg */
  15009. /*
  15010. * Testing wc_ed448_import_public()
  15011. */
  15012. static int test_wc_ed448_import_public (void)
  15013. {
  15014. int ret = 0;
  15015. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  15016. WC_RNG rng;
  15017. ed448_key pubKey;
  15018. const byte in[] =
  15019. "Ed448PublicKeyUnitTest.................................\n";
  15020. word32 inlen = sizeof(in);
  15021. ret = wc_InitRng(&rng);
  15022. if (ret == 0) {
  15023. ret = wc_ed448_init(&pubKey);
  15024. if (ret == 0) {
  15025. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey);
  15026. }
  15027. }
  15028. printf(testingFmt, "wc_ed448_import_public()");
  15029. if (ret == 0) {
  15030. ret = wc_ed448_import_public(in, inlen, &pubKey);
  15031. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  15032. ret = 0;
  15033. } else {
  15034. ret = SSL_FATAL_ERROR;
  15035. }
  15036. /* Test bad args. */
  15037. if (ret == 0) {
  15038. ret = wc_ed448_import_public(NULL, inlen, &pubKey);
  15039. if (ret == BAD_FUNC_ARG) {
  15040. ret = wc_ed448_import_public(in, inlen, NULL);
  15041. }
  15042. if (ret == BAD_FUNC_ARG) {
  15043. ret = wc_ed448_import_public(in, inlen - 1, &pubKey);
  15044. }
  15045. if (ret == BAD_FUNC_ARG) {
  15046. ret = 0;
  15047. } else if (ret == 0) {
  15048. ret = SSL_FATAL_ERROR;
  15049. }
  15050. }
  15051. }
  15052. printf(resultFmt, ret == 0 ? passed : failed);
  15053. if (wc_FreeRng(&rng) && ret == 0) {
  15054. ret = SSL_FATAL_ERROR;
  15055. }
  15056. wc_ed448_free(&pubKey);
  15057. #endif
  15058. return ret;
  15059. } /* END wc_ed448_import_public */
  15060. /*
  15061. * Testing wc_ed448_import_private_key()
  15062. */
  15063. static int test_wc_ed448_import_private_key (void)
  15064. {
  15065. int ret = 0;
  15066. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  15067. WC_RNG rng;
  15068. ed448_key key;
  15069. const byte privKey[] =
  15070. "Ed448PrivateKeyUnitTest................................\n";
  15071. const byte pubKey[] =
  15072. "Ed448PublicKeyUnitTest.................................\n";
  15073. word32 privKeySz = sizeof(privKey);
  15074. word32 pubKeySz = sizeof(pubKey);
  15075. ret = wc_InitRng(&rng);
  15076. if (ret != 0) {
  15077. return ret;
  15078. }
  15079. ret = wc_ed448_init(&key);
  15080. if (ret != 0) {
  15081. wc_FreeRng(&rng);
  15082. return ret;
  15083. }
  15084. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  15085. printf(testingFmt, "wc_ed448_import_private_key()");
  15086. if (ret == 0) {
  15087. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey, pubKeySz,
  15088. &key);
  15089. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
  15090. XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  15091. ret = SSL_FATAL_ERROR;
  15092. }
  15093. }
  15094. /* Test bad args. */
  15095. if (ret == 0) {
  15096. ret = wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  15097. &key);
  15098. if (ret == BAD_FUNC_ARG) {
  15099. ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
  15100. pubKeySz, &key);
  15101. }
  15102. if (ret == BAD_FUNC_ARG) {
  15103. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  15104. pubKeySz, NULL);
  15105. }
  15106. if (ret == BAD_FUNC_ARG) {
  15107. ret = wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  15108. pubKeySz, &key);
  15109. }
  15110. if (ret == BAD_FUNC_ARG) {
  15111. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  15112. pubKeySz - 1, &key);
  15113. }
  15114. if (ret == BAD_FUNC_ARG) {
  15115. ret = 0;
  15116. } else if (ret == 0) {
  15117. ret = SSL_FATAL_ERROR;
  15118. }
  15119. }
  15120. printf(resultFmt, ret == 0 ? passed : failed);
  15121. if (wc_FreeRng(&rng) && ret == 0) {
  15122. ret = SSL_FATAL_ERROR;
  15123. }
  15124. wc_ed448_free(&key);
  15125. #endif
  15126. return ret;
  15127. } /* END test_wc_ed448_import_private_key */
  15128. /*
  15129. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  15130. */
  15131. static int test_wc_ed448_export (void)
  15132. {
  15133. int ret = 0;
  15134. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  15135. WC_RNG rng;
  15136. ed448_key key;
  15137. byte priv[ED448_PRV_KEY_SIZE];
  15138. byte pub[ED448_PUB_KEY_SIZE];
  15139. word32 privSz = sizeof(priv);
  15140. word32 pubSz = sizeof(pub);
  15141. ret = wc_InitRng(&rng);
  15142. if (ret != 0) {
  15143. return ret;
  15144. }
  15145. ret = wc_ed448_init(&key);
  15146. if (ret != 0) {
  15147. wc_FreeRng(&rng);
  15148. return ret;
  15149. }
  15150. if (ret == 0) {
  15151. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  15152. }
  15153. printf(testingFmt, "wc_ed448_export_public()");
  15154. if (ret == 0) {
  15155. ret = wc_ed448_export_public(&key, pub, &pubSz);
  15156. if (ret == 0 && (pubSz != ED448_KEY_SIZE ||
  15157. XMEMCMP(key.p, pub, pubSz) != 0)) {
  15158. ret = SSL_FATAL_ERROR;
  15159. }
  15160. if (ret == 0) {
  15161. ret = wc_ed448_export_public(NULL, pub, &pubSz);
  15162. if (ret == BAD_FUNC_ARG) {
  15163. ret = wc_ed448_export_public(&key, NULL, &pubSz);
  15164. }
  15165. if (ret == BAD_FUNC_ARG) {
  15166. ret = wc_ed448_export_public(&key, pub, NULL);
  15167. }
  15168. if (ret == BAD_FUNC_ARG) {
  15169. ret = 0;
  15170. } else if (ret == 0) {
  15171. ret = SSL_FATAL_ERROR;
  15172. }
  15173. }
  15174. }
  15175. printf(resultFmt, ret == 0 ? passed : failed);
  15176. printf(testingFmt, "wc_ed448_export_private_only()");
  15177. if (ret == 0) {
  15178. ret = wc_ed448_export_private_only(&key, priv, &privSz);
  15179. if (ret == 0 && (privSz != ED448_KEY_SIZE ||
  15180. XMEMCMP(key.k, priv, privSz) != 0)) {
  15181. ret = SSL_FATAL_ERROR;
  15182. }
  15183. if (ret == 0) {
  15184. ret = wc_ed448_export_private_only(NULL, priv, &privSz);
  15185. if (ret == BAD_FUNC_ARG) {
  15186. ret = wc_ed448_export_private_only(&key, NULL, &privSz);
  15187. }
  15188. if (ret == BAD_FUNC_ARG) {
  15189. ret = wc_ed448_export_private_only(&key, priv, NULL);
  15190. }
  15191. if (ret == BAD_FUNC_ARG) {
  15192. ret = 0;
  15193. } else if (ret == 0) {
  15194. ret = SSL_FATAL_ERROR;
  15195. }
  15196. }
  15197. }
  15198. printf(resultFmt, ret == 0 ? passed : failed);
  15199. if (wc_FreeRng(&rng) && ret == 0) {
  15200. ret = SSL_FATAL_ERROR;
  15201. }
  15202. wc_ed448_free(&key);
  15203. #endif
  15204. return ret;
  15205. } /* END test_wc_ed448_export */
  15206. /*
  15207. * Testing wc_ed448_size()
  15208. */
  15209. static int test_wc_ed448_size (void)
  15210. {
  15211. int ret = 0;
  15212. #if defined(HAVE_ED448)
  15213. WC_RNG rng;
  15214. ed448_key key;
  15215. ret = wc_InitRng(&rng);
  15216. if (ret != 0) {
  15217. return ret;
  15218. }
  15219. ret = wc_ed448_init(&key);
  15220. if (ret != 0) {
  15221. wc_FreeRng(&rng);
  15222. return ret;
  15223. }
  15224. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  15225. if (ret != 0) {
  15226. wc_FreeRng(&rng);
  15227. wc_ed448_free(&key);
  15228. return ret;
  15229. }
  15230. printf(testingFmt, "wc_ed448_size()");
  15231. ret = wc_ed448_size(&key);
  15232. /* Test bad args. */
  15233. if (ret == ED448_KEY_SIZE) {
  15234. ret = wc_ed448_size(NULL);
  15235. if (ret == BAD_FUNC_ARG) {
  15236. ret = 0;
  15237. }
  15238. }
  15239. printf(resultFmt, ret == 0 ? passed : failed);
  15240. if (ret == 0) {
  15241. printf(testingFmt, "wc_ed448_sig_size()");
  15242. ret = wc_ed448_sig_size(&key);
  15243. if (ret == ED448_SIG_SIZE) {
  15244. ret = 0;
  15245. }
  15246. /* Test bad args. */
  15247. if (ret == 0) {
  15248. ret = wc_ed448_sig_size(NULL);
  15249. if (ret == BAD_FUNC_ARG) {
  15250. ret = 0;
  15251. }
  15252. }
  15253. printf(resultFmt, ret == 0 ? passed : failed);
  15254. } /* END wc_ed448_sig_size() */
  15255. if (ret == 0) {
  15256. printf(testingFmt, "wc_ed448_pub_size");
  15257. ret = wc_ed448_pub_size(&key);
  15258. if (ret == ED448_PUB_KEY_SIZE) {
  15259. ret = 0;
  15260. }
  15261. if (ret == 0) {
  15262. ret = wc_ed448_pub_size(NULL);
  15263. if (ret == BAD_FUNC_ARG) {
  15264. ret = 0;
  15265. }
  15266. }
  15267. printf(resultFmt, ret == 0 ? passed : failed);
  15268. } /* END wc_ed448_pub_size */
  15269. if (ret == 0) {
  15270. printf(testingFmt, "wc_ed448_priv_size");
  15271. ret = wc_ed448_priv_size(&key);
  15272. if (ret == ED448_PRV_KEY_SIZE) {
  15273. ret = 0;
  15274. }
  15275. if (ret == 0) {
  15276. ret = wc_ed448_priv_size(NULL);
  15277. if (ret == BAD_FUNC_ARG) {
  15278. ret = 0;
  15279. }
  15280. }
  15281. printf(resultFmt, ret == 0 ? passed : failed);
  15282. } /* END wc_ed448_pub_size */
  15283. if (wc_FreeRng(&rng) && ret == 0) {
  15284. ret = SSL_FATAL_ERROR;
  15285. }
  15286. wc_ed448_free(&key);
  15287. #endif
  15288. return ret;
  15289. } /* END test_wc_ed448_size */
  15290. /*
  15291. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  15292. */
  15293. static int test_wc_ed448_exportKey (void)
  15294. {
  15295. int ret = 0;
  15296. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  15297. WC_RNG rng;
  15298. ed448_key key;
  15299. byte priv[ED448_PRV_KEY_SIZE];
  15300. byte pub[ED448_PUB_KEY_SIZE];
  15301. byte privOnly[ED448_PRV_KEY_SIZE];
  15302. word32 privSz = sizeof(priv);
  15303. word32 pubSz = sizeof(pub);
  15304. word32 privOnlySz = sizeof(privOnly);
  15305. ret = wc_InitRng(&rng);
  15306. if (ret != 0) {
  15307. return ret;
  15308. }
  15309. ret = wc_ed448_init(&key);
  15310. if (ret != 0) {
  15311. wc_FreeRng(&rng);
  15312. return ret;
  15313. }
  15314. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  15315. if (ret != 0) {
  15316. wc_FreeRng(&rng);
  15317. wc_ed448_free(&key);
  15318. return ret;
  15319. }
  15320. printf(testingFmt, "wc_ed448_export_private()");
  15321. ret = wc_ed448_export_private(&key, privOnly, &privOnlySz);
  15322. if (ret == 0) {
  15323. ret = wc_ed448_export_private(NULL, privOnly, &privOnlySz);
  15324. if (ret == BAD_FUNC_ARG) {
  15325. ret = wc_ed448_export_private(&key, NULL, &privOnlySz);
  15326. }
  15327. if (ret == BAD_FUNC_ARG) {
  15328. ret = wc_ed448_export_private(&key, privOnly, NULL);
  15329. }
  15330. if (ret == BAD_FUNC_ARG) {
  15331. ret = 0;
  15332. } else if (ret == 0) {
  15333. ret = SSL_FATAL_ERROR;
  15334. }
  15335. }
  15336. printf(resultFmt, ret == 0 ? passed : failed);
  15337. if (ret == 0) {
  15338. printf(testingFmt, "wc_ed448_export_key()");
  15339. ret = wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz);
  15340. if (ret == 0) {
  15341. ret = wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz);
  15342. if (ret == BAD_FUNC_ARG) {
  15343. ret = wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz);
  15344. }
  15345. if (ret == BAD_FUNC_ARG) {
  15346. ret = wc_ed448_export_key(&key, priv, NULL, pub, &pubSz);
  15347. }
  15348. if (ret == BAD_FUNC_ARG) {
  15349. ret = wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz);
  15350. }
  15351. if (ret == BAD_FUNC_ARG) {
  15352. ret = wc_ed448_export_key(&key, priv, &privSz, pub, NULL);
  15353. }
  15354. if (ret == BAD_FUNC_ARG) {
  15355. ret = 0;
  15356. } else if (ret == 0) {
  15357. ret = SSL_FATAL_ERROR;
  15358. }
  15359. }
  15360. printf(resultFmt, ret == 0 ? passed : failed);
  15361. } /* END wc_ed448_export_key() */
  15362. /* Cross check output. */
  15363. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  15364. ret = SSL_FATAL_ERROR;
  15365. }
  15366. if (wc_FreeRng(&rng) && ret == 0) {
  15367. ret = SSL_FATAL_ERROR;
  15368. }
  15369. wc_ed448_free(&key);
  15370. #endif
  15371. return ret;
  15372. } /* END test_wc_ed448_exportKey */
  15373. /*
  15374. * Testing wc_Ed448PublicKeyToDer
  15375. */
  15376. static int test_wc_Ed448PublicKeyToDer (void)
  15377. {
  15378. int ret = 0;
  15379. #if defined(HAVE_ED448) && (defined(WOLFSSL_CERT_GEN) || \
  15380. defined(WOLFSSL_KEY_GEN))
  15381. int tmp;
  15382. ed448_key key;
  15383. byte derBuf[1024];
  15384. printf(testingFmt, "wc_Ed448PublicKeyToDer()");
  15385. /* Test bad args */
  15386. tmp = wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0);
  15387. if (tmp != BAD_FUNC_ARG) {
  15388. ret = SSL_FATAL_ERROR;
  15389. }
  15390. if (ret == 0) {
  15391. wc_ed448_init(&key);
  15392. tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0);
  15393. if (tmp != BUFFER_E) {
  15394. ret = SSL_FATAL_ERROR;
  15395. }
  15396. wc_ed448_free(&key);
  15397. }
  15398. /* Test good args */
  15399. if (ret == 0) {
  15400. WC_RNG rng;
  15401. ret = wc_InitRng(&rng);
  15402. if (ret != 0) {
  15403. return ret;
  15404. }
  15405. ret = wc_ed448_init(&key);
  15406. if (ret != 0) {
  15407. wc_FreeRng(&rng);
  15408. return ret;
  15409. }
  15410. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  15411. if (ret != 0) {
  15412. wc_FreeRng(&rng);
  15413. wc_ed448_free(&key);
  15414. return ret;
  15415. }
  15416. tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1);
  15417. if (tmp <= 0) {
  15418. ret = SSL_FATAL_ERROR;
  15419. }
  15420. wc_FreeRng(&rng);
  15421. wc_ed448_free(&key);
  15422. }
  15423. printf(resultFmt, ret == 0 ? passed : failed);
  15424. #endif
  15425. return ret;
  15426. } /* END testing wc_Ed448PublicKeyToDer */
  15427. /*
  15428. * Testing wc_curve448_init and wc_curve448_free.
  15429. */
  15430. static int test_wc_curve448_init (void)
  15431. {
  15432. int ret = 0;
  15433. #if defined(HAVE_CURVE448)
  15434. curve448_key key;
  15435. printf(testingFmt, "wc_curve448_init()");
  15436. ret = wc_curve448_init(&key);
  15437. /* Test bad args for wc_curve448_init */
  15438. if (ret == 0) {
  15439. ret = wc_curve448_init(NULL);
  15440. if (ret == BAD_FUNC_ARG) {
  15441. ret = 0;
  15442. } else if (ret == 0) {
  15443. ret = SSL_FATAL_ERROR;
  15444. }
  15445. }
  15446. printf(resultFmt, ret == 0 ? passed : failed);
  15447. /* Test good args for wc_curve_448_free */
  15448. wc_curve448_free(&key);
  15449. wc_curve448_free(NULL);
  15450. #endif
  15451. return ret;
  15452. } /* END test_wc_curve448_init and wc_curve_448_free*/
  15453. /*
  15454. * Testing wc_curve448_make_key
  15455. */
  15456. static int test_wc_curve448_make_key (void)
  15457. {
  15458. int ret = 0;
  15459. #if defined(HAVE_CURVE448)
  15460. WC_RNG rng;
  15461. curve448_key key;
  15462. int keysize;
  15463. printf(testingFmt, "wc_curve448_make_key()");
  15464. ret = wc_curve448_init(&key);
  15465. if (ret == 0) {
  15466. ret = wc_InitRng(&rng);
  15467. }
  15468. if (ret == 0) {
  15469. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  15470. if (ret == 0) {
  15471. keysize = wc_curve448_size(&key);
  15472. if (keysize != CURVE448_KEY_SIZE) {
  15473. ret = SSL_FATAL_ERROR;
  15474. }
  15475. }
  15476. if (ret == 0) {
  15477. ret = wc_curve448_make_key(&rng, keysize, &key);
  15478. }
  15479. }
  15480. /*test bad cases*/
  15481. if (ret == 0) {
  15482. ret = wc_curve448_make_key(NULL, 0, NULL);
  15483. if (ret == BAD_FUNC_ARG) {
  15484. ret = 0;
  15485. }
  15486. }
  15487. if (ret == 0) {
  15488. ret = wc_curve448_make_key(&rng, keysize, NULL);
  15489. if (ret == BAD_FUNC_ARG) {
  15490. ret = 0;
  15491. }
  15492. }
  15493. if (ret == 0) {
  15494. ret = wc_curve448_make_key(NULL, keysize, &key);
  15495. if (ret == BAD_FUNC_ARG) {
  15496. ret = 0;
  15497. }
  15498. }
  15499. if (ret == 0) {
  15500. ret = wc_curve448_make_key(&rng, 0, &key);
  15501. if (ret == ECC_BAD_ARG_E) {
  15502. ret = 0;
  15503. }
  15504. }
  15505. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  15506. ret = WOLFSSL_FATAL_ERROR;
  15507. }
  15508. printf(resultFmt, ret == 0 ? passed : failed);
  15509. wc_curve448_free(&key);
  15510. #endif
  15511. return ret;
  15512. } /*END test_wc_curve448_make_key*/
  15513. /*
  15514. * Testing test_wc_curve448_shared_secret_ex
  15515. */
  15516. static int test_wc_curve448_shared_secret_ex (void)
  15517. {
  15518. int ret = 0;
  15519. #if defined(HAVE_CURVE448)
  15520. WC_RNG rng;
  15521. curve448_key private_key, public_key;
  15522. byte out[CURVE448_KEY_SIZE];
  15523. word32 outLen = sizeof(out);
  15524. int endian = EC448_BIG_ENDIAN;
  15525. printf(testingFmt, "wc_curve448_shared_secret_ex()");
  15526. ret = wc_curve448_init(&private_key);
  15527. if (ret == 0) {
  15528. ret = wc_InitRng(&rng);
  15529. if (ret == 0) {
  15530. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key);
  15531. }
  15532. }
  15533. if (ret == 0) {
  15534. ret = wc_curve448_init(&public_key);
  15535. }
  15536. if (ret == 0) {
  15537. if (ret == 0) {
  15538. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key);
  15539. }
  15540. }
  15541. if (ret == 0) {
  15542. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  15543. &outLen, endian);
  15544. }
  15545. /*test bad cases*/
  15546. if (ret == 0) {
  15547. ret = wc_curve448_shared_secret_ex(NULL, NULL, NULL,
  15548. 0, endian);
  15549. if (ret == BAD_FUNC_ARG) {
  15550. ret = 0;
  15551. }
  15552. }
  15553. if (ret == 0) {
  15554. ret = wc_curve448_shared_secret_ex(NULL, &public_key, out,
  15555. &outLen, endian);
  15556. if (ret == BAD_FUNC_ARG) {
  15557. ret = 0;
  15558. }
  15559. }
  15560. if (ret == 0) {
  15561. ret = wc_curve448_shared_secret_ex(&private_key, NULL, out,
  15562. &outLen, endian);
  15563. if (ret == BAD_FUNC_ARG) {
  15564. ret = 0;
  15565. }
  15566. }
  15567. if (ret == 0) {
  15568. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  15569. &outLen, endian);
  15570. if (ret == BAD_FUNC_ARG) {
  15571. ret = 0;
  15572. }
  15573. }
  15574. if (ret == 0) {
  15575. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  15576. NULL, endian);
  15577. if (ret == BAD_FUNC_ARG) {
  15578. ret = 0;
  15579. }
  15580. }
  15581. outLen = outLen - 2;
  15582. if (ret == 0) {
  15583. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  15584. &outLen, endian);
  15585. if (ret == BAD_FUNC_ARG) {
  15586. ret = 0;
  15587. }
  15588. }
  15589. printf(resultFmt, ret == 0 ? passed : failed);
  15590. wc_curve448_free(&private_key);
  15591. wc_curve448_free(&public_key);
  15592. wc_FreeRng(&rng);
  15593. #endif
  15594. return ret;
  15595. } /*END test_wc_curve448_shared_secret_ex*/
  15596. /*
  15597. * Testing test_wc_curve448_export_public_ex
  15598. */
  15599. static int test_wc_curve448_export_public_ex (void)
  15600. {
  15601. int ret = 0;
  15602. #if defined(HAVE_CURVE448)
  15603. WC_RNG rng;
  15604. curve448_key key;
  15605. byte out[CURVE448_KEY_SIZE];
  15606. word32 outLen = sizeof(out);
  15607. int endian = EC448_BIG_ENDIAN;
  15608. printf(testingFmt, "wc_curve448_export_public_ex()");
  15609. ret = wc_curve448_init(&key);
  15610. if (ret == 0) {
  15611. ret = wc_InitRng(&rng);
  15612. }
  15613. if (ret == 0) {
  15614. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  15615. if (ret == 0){
  15616. ret = wc_curve448_export_public(&key, out, &outLen);
  15617. }
  15618. if (ret == 0) {
  15619. ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
  15620. }
  15621. }
  15622. /*test bad cases*/
  15623. if (ret == 0) {
  15624. ret = wc_curve448_export_public_ex(NULL, NULL, NULL, endian);
  15625. if (ret == BAD_FUNC_ARG) {
  15626. ret = 0;
  15627. }
  15628. }
  15629. if (ret == 0) {
  15630. ret = wc_curve448_export_public_ex(NULL, out, &outLen, endian);
  15631. if (ret == BAD_FUNC_ARG) {
  15632. ret = 0;
  15633. }
  15634. }
  15635. if (ret == 0) {
  15636. ret = wc_curve448_export_public_ex(&key, NULL, &outLen, endian);
  15637. if (ret == BAD_FUNC_ARG) {
  15638. ret = 0;
  15639. }
  15640. }
  15641. if (ret == 0) {
  15642. ret = wc_curve448_export_public_ex(&key, out, NULL, endian);
  15643. if (ret == BAD_FUNC_ARG) {
  15644. ret = 0;
  15645. }
  15646. }
  15647. outLen = outLen - 2;
  15648. if (ret == 0) {
  15649. ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
  15650. if (ret == ECC_BAD_ARG_E) {
  15651. ret = 0;
  15652. }
  15653. }
  15654. printf(resultFmt, ret == 0 ? passed : failed);
  15655. wc_curve448_free(&key);
  15656. wc_FreeRng(&rng);
  15657. #endif
  15658. return ret;
  15659. } /*END test_wc_curve448_export_public_ex*/
  15660. /*
  15661. * Testing test_wc_curve448_export_private_raw_ex
  15662. */
  15663. static int test_wc_curve448_export_private_raw_ex (void)
  15664. {
  15665. int ret = 0;
  15666. #if defined(HAVE_CURVE448)
  15667. WC_RNG rng;
  15668. curve448_key key;
  15669. byte out[CURVE448_KEY_SIZE];
  15670. word32 outLen = sizeof(out);
  15671. int endian = EC448_BIG_ENDIAN;
  15672. printf(testingFmt, "wc_curve448_export_private_raw_ex()");
  15673. ret = wc_curve448_init(&key);
  15674. if (ret == 0) {
  15675. ret = wc_InitRng(&rng);
  15676. }
  15677. if (ret == 0) {
  15678. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
  15679. }
  15680. /*test bad cases*/
  15681. if (ret == 0) {
  15682. ret = wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian);
  15683. if (ret == BAD_FUNC_ARG) {
  15684. ret = 0;
  15685. }
  15686. }
  15687. if (ret == 0) {
  15688. ret = wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian);
  15689. if (ret == BAD_FUNC_ARG) {
  15690. ret = 0;
  15691. }
  15692. }
  15693. if (ret == 0) {
  15694. ret = wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian);
  15695. if (ret == BAD_FUNC_ARG) {
  15696. ret = 0;
  15697. }
  15698. }
  15699. if (ret == 0) {
  15700. ret = wc_curve448_export_private_raw_ex(&key, out, NULL, endian);
  15701. if (ret == BAD_FUNC_ARG) {
  15702. ret = 0;
  15703. }
  15704. }
  15705. if (ret == 0) {
  15706. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen,
  15707. EC448_LITTLE_ENDIAN);
  15708. }
  15709. outLen = outLen - 2;
  15710. if (ret == 0) {
  15711. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
  15712. if (ret == ECC_BAD_ARG_E) {
  15713. ret = 0;
  15714. }
  15715. }
  15716. printf(resultFmt, ret == 0 ? passed : failed);
  15717. wc_curve448_free(&key);
  15718. wc_FreeRng(&rng);
  15719. #endif
  15720. return ret;
  15721. }/*END test_wc_curve448_export_private_raw_ex*/
  15722. /*
  15723. * Testing test_wc_curve448_import_private_raw_ex
  15724. */
  15725. static int test_wc_curve448_import_private_raw_ex (void)
  15726. {
  15727. int ret = 0;
  15728. #if defined(HAVE_CURVE448)
  15729. WC_RNG rng;
  15730. curve448_key key;
  15731. byte priv[CURVE448_KEY_SIZE];
  15732. byte pub[CURVE448_KEY_SIZE];
  15733. word32 privSz = sizeof(priv);
  15734. word32 pubSz = sizeof(pub);
  15735. int endian = EC448_BIG_ENDIAN;
  15736. printf(testingFmt, "wc_curve448_import_private_raw_ex()");
  15737. ret = wc_curve448_init(&key);
  15738. if (ret == 0) {
  15739. ret = wc_InitRng(&rng);
  15740. }
  15741. if (ret == 0) {
  15742. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  15743. if (ret == 0){
  15744. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  15745. }
  15746. if (ret == 0){
  15747. ret = wc_curve448_export_public(&key, pub, &pubSz);
  15748. }
  15749. if (ret == 0) {
  15750. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  15751. &key, endian);
  15752. }
  15753. }
  15754. /*test bad cases*/
  15755. if (ret == 0) {
  15756. ret = wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0);
  15757. if (ret == BAD_FUNC_ARG) {
  15758. ret = 0;
  15759. }
  15760. }
  15761. if (ret == 0) {
  15762. ret = wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  15763. &key, endian);
  15764. if (ret == BAD_FUNC_ARG) {
  15765. ret = 0;
  15766. }
  15767. }
  15768. if (ret == 0) {
  15769. ret = wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  15770. &key, endian);
  15771. if (ret == BAD_FUNC_ARG) {
  15772. ret = 0;
  15773. }
  15774. }
  15775. if (ret == 0) {
  15776. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  15777. NULL, endian);
  15778. if (ret == BAD_FUNC_ARG) {
  15779. ret = 0;
  15780. }
  15781. }
  15782. if (ret == 0) {
  15783. ret = wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  15784. &key, endian);
  15785. if (ret == ECC_BAD_ARG_E) {
  15786. ret = 0;
  15787. }
  15788. }
  15789. if (ret == 0) {
  15790. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  15791. &key, endian);
  15792. if (ret == ECC_BAD_ARG_E) {
  15793. ret = 0;
  15794. }
  15795. }
  15796. if (ret == 0) {
  15797. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  15798. &key, EC448_LITTLE_ENDIAN);
  15799. }
  15800. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  15801. ret = WOLFSSL_FATAL_ERROR;
  15802. }
  15803. printf(resultFmt, ret == 0 ? passed : failed);
  15804. wc_curve448_free(&key);
  15805. #endif
  15806. return ret;
  15807. } /*END test_wc_curve448_import_private_raw_ex*/
  15808. /*
  15809. * Testing test_curve448_export_key_raw
  15810. */
  15811. static int test_wc_curve448_export_key_raw (void)
  15812. {
  15813. int ret = 0;
  15814. #if defined(HAVE_CURVE448)
  15815. WC_RNG rng;
  15816. curve448_key key;
  15817. byte priv[CURVE448_KEY_SIZE];
  15818. byte pub[CURVE448_KEY_SIZE];
  15819. word32 privSz = sizeof(priv);
  15820. word32 pubSz = sizeof(pub);
  15821. printf(testingFmt, "wc_curve448_export_key_raw()");
  15822. ret = wc_curve448_init(&key);
  15823. if (ret == 0) {
  15824. ret = wc_InitRng(&rng);
  15825. }
  15826. if (ret == 0) {
  15827. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  15828. if (ret == 0) {
  15829. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  15830. }
  15831. if (ret == 0) {
  15832. ret = wc_curve448_export_public(&key, pub, &pubSz);
  15833. }
  15834. if (ret == 0) {
  15835. ret = wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz);
  15836. }
  15837. }
  15838. printf(resultFmt, ret == 0 ? passed : failed);
  15839. wc_curve448_free(&key);
  15840. wc_FreeRng(&rng);
  15841. #endif
  15842. return ret;
  15843. }/*END test_wc_curve448_import_private_raw_ex*/
  15844. /*
  15845. * Testing test_wc_curve448_import_private
  15846. */
  15847. static int test_wc_curve448_import_private (void)
  15848. {
  15849. int ret = 0;
  15850. #if defined(HAVE_CURVE448)
  15851. curve448_key key;
  15852. WC_RNG rng;
  15853. byte priv[CURVE448_KEY_SIZE];
  15854. word32 privSz = sizeof(priv);
  15855. printf(testingFmt, "wc_curve448_import_private()");
  15856. ret = wc_curve448_init(&key);
  15857. if (ret == 0) {
  15858. ret = wc_InitRng(&rng);
  15859. }
  15860. if (ret == 0) {
  15861. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  15862. if (ret == 0) {
  15863. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  15864. }
  15865. }
  15866. if (ret == 0) {
  15867. ret = wc_curve448_import_private(priv, privSz, &key);
  15868. }
  15869. printf(resultFmt, ret == 0 ? passed : failed);
  15870. wc_curve448_free(&key);
  15871. wc_FreeRng(&rng);
  15872. #endif
  15873. return ret;
  15874. } /*END test_wc_curve448_import*/
  15875. /*
  15876. * Testing test_wc_curve448_size.
  15877. */
  15878. static int test_wc_curve448_size (void)
  15879. {
  15880. int ret = 0;
  15881. #if defined(HAVE_CURVE448)
  15882. curve448_key key;
  15883. printf(testingFmt, "wc_curve448_size()");
  15884. ret = wc_curve448_init(&key);
  15885. /* Test good args for wc_curve448_size */
  15886. if (ret == 0) {
  15887. ret = wc_curve448_size(&key);
  15888. }
  15889. /* Test bad args for wc_curve448_size */
  15890. if (ret != 0) {
  15891. ret = wc_curve448_size(NULL);
  15892. }
  15893. printf(resultFmt, ret == 0 ? passed : failed);
  15894. wc_curve448_free(&key);
  15895. #endif
  15896. return ret;
  15897. } /* END test_wc_curve448_size*/
  15898. /*
  15899. * Testing wc_ecc_make_key.
  15900. */
  15901. static int test_wc_ecc_make_key (void)
  15902. {
  15903. int ret = 0;
  15904. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  15905. WC_RNG rng;
  15906. ecc_key key;
  15907. printf(testingFmt, "wc_ecc_make_key()");
  15908. ret = wc_InitRng(&rng);
  15909. if (ret != 0)
  15910. return ret;
  15911. ret = wc_ecc_init(&key);
  15912. if (ret == 0) {
  15913. ret = wc_ecc_make_key(&rng, KEY14, &key);
  15914. /* Pass in bad args. */
  15915. if (ret == 0) {
  15916. ret = wc_ecc_make_key(NULL, KEY14, &key);
  15917. if (ret == BAD_FUNC_ARG) {
  15918. ret = wc_ecc_make_key(&rng, KEY14, NULL);
  15919. }
  15920. if (ret == BAD_FUNC_ARG) {
  15921. ret = 0;
  15922. } else if (ret == 0) {
  15923. ret = WOLFSSL_FATAL_ERROR;
  15924. }
  15925. }
  15926. wc_ecc_free(&key);
  15927. }
  15928. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  15929. ret = WOLFSSL_FATAL_ERROR;
  15930. }
  15931. #ifdef FP_ECC
  15932. wc_ecc_fp_free();
  15933. #endif
  15934. printf(resultFmt, ret == 0 ? passed : failed);
  15935. #endif
  15936. return ret;
  15937. } /* END test_wc_ecc_make_key */
  15938. /*
  15939. * Testing wc_ecc_init()
  15940. */
  15941. static int test_wc_ecc_init (void)
  15942. {
  15943. int ret = 0;
  15944. #ifdef HAVE_ECC
  15945. ecc_key key;
  15946. printf(testingFmt, "wc_ecc_init()");
  15947. ret = wc_ecc_init(&key);
  15948. /* Pass in bad args. */
  15949. if (ret == 0) {
  15950. ret = wc_ecc_init(NULL);
  15951. if (ret == BAD_FUNC_ARG) {
  15952. ret = 0;
  15953. } else if (ret == 0) {
  15954. ret = WOLFSSL_FATAL_ERROR;
  15955. }
  15956. }
  15957. printf(resultFmt, ret == 0 ? passed : failed);
  15958. wc_ecc_free(&key);
  15959. #endif
  15960. return ret;
  15961. } /* END test_wc_ecc_init */
  15962. /*
  15963. * Testing wc_ecc_check_key()
  15964. */
  15965. static int test_wc_ecc_check_key (void)
  15966. {
  15967. int ret = 0;
  15968. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  15969. WC_RNG rng;
  15970. ecc_key key;
  15971. XMEMSET(&rng, 0, sizeof(rng));
  15972. XMEMSET(&key, 0, sizeof(key));
  15973. ret = wc_InitRng(&rng);
  15974. if (ret == 0) {
  15975. ret = wc_ecc_init(&key);
  15976. if (ret == 0) {
  15977. ret = wc_ecc_make_key(&rng, KEY14, &key);
  15978. }
  15979. }
  15980. printf(testingFmt, "wc_ecc_check_key()");
  15981. if (ret == 0) {
  15982. ret = wc_ecc_check_key(&key);
  15983. }
  15984. /* Pass in bad args. */
  15985. if (ret == 0) {
  15986. ret = wc_ecc_check_key(NULL);
  15987. if (ret == BAD_FUNC_ARG) {
  15988. ret = 0;
  15989. } else if (ret == 0) {
  15990. ret = WOLFSSL_FATAL_ERROR;
  15991. }
  15992. }
  15993. printf(resultFmt, ret == 0 ? passed : failed);
  15994. if (wc_FreeRng(&rng) && ret == 0) {
  15995. ret = WOLFSSL_FATAL_ERROR;
  15996. }
  15997. wc_ecc_free(&key);
  15998. #ifdef FP_ECC
  15999. wc_ecc_fp_free();
  16000. #endif
  16001. #endif
  16002. return ret;
  16003. } /* END test_wc_ecc_check_key */
  16004. /*
  16005. * Testing wc_ecc_get_generator()
  16006. */
  16007. static int test_wc_ecc_get_generator(void)
  16008. {
  16009. int ret = 0;
  16010. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  16011. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  16012. ecc_point* pt;
  16013. printf(testingFmt, "wc_ecc_new_point()");
  16014. pt = wc_ecc_new_point();
  16015. if (!pt) {
  16016. ret = WOLFSSL_FATAL_ERROR;
  16017. }
  16018. printf(testingFmt, "wc_ecc_get_generator()");
  16019. if (ret == 0) {
  16020. ret = wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1));
  16021. }
  16022. /* Test bad args. */
  16023. if (ret == MP_OKAY) {
  16024. /* Returns Zero for bad arg. */
  16025. ret = wc_ecc_get_generator(pt, -1);
  16026. if (ret != MP_OKAY)
  16027. wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1));
  16028. if (ret != MP_OKAY)
  16029. wc_ecc_get_generator(pt, 1000); /* If we ever get to 1000 curves
  16030. * increase this number */
  16031. if (ret != MP_OKAY)
  16032. wc_ecc_get_generator(NULL, -1);
  16033. ret = ret == MP_OKAY ? WOLFSSL_FATAL_ERROR : 0;
  16034. }
  16035. printf(resultFmt, ret == 0 ? passed : failed);
  16036. wc_ecc_del_point(pt);
  16037. #endif
  16038. return ret;
  16039. } /* END test_wc_ecc_get_generator */
  16040. /*
  16041. * Testing wc_ecc_size()
  16042. */
  16043. static int test_wc_ecc_size (void)
  16044. {
  16045. int ret = 0;
  16046. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  16047. WC_RNG rng;
  16048. ecc_key key;
  16049. XMEMSET(&rng, 0, sizeof(rng));
  16050. XMEMSET(&key, 0, sizeof(key));
  16051. ret = wc_InitRng(&rng);
  16052. if (ret == 0) {
  16053. ret = wc_ecc_init(&key);
  16054. if (ret == 0) {
  16055. ret = wc_ecc_make_key(&rng, KEY14, &key);
  16056. }
  16057. }
  16058. printf(testingFmt, "wc_ecc_size()");
  16059. if (ret == 0) {
  16060. ret = wc_ecc_size(&key);
  16061. if (ret == KEY14) {
  16062. ret = 0;
  16063. } else if (ret == 0){
  16064. ret = WOLFSSL_FATAL_ERROR;
  16065. }
  16066. }
  16067. /* Test bad args. */
  16068. if (ret == 0) {
  16069. /* Returns Zero for bad arg. */
  16070. ret = wc_ecc_size(NULL);
  16071. }
  16072. printf(resultFmt, ret == 0 ? passed : failed);
  16073. if (wc_FreeRng(&rng) && ret == 0) {
  16074. ret = WOLFSSL_FATAL_ERROR;
  16075. }
  16076. wc_ecc_free(&key);
  16077. #endif
  16078. return ret;
  16079. } /* END test_wc_ecc_size */
  16080. static void test_wc_ecc_params(void)
  16081. {
  16082. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  16083. It was added after certifications */
  16084. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  16085. const ecc_set_type* ecc_set;
  16086. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  16087. /* Test for SECP256R1 curve */
  16088. int curve_id = ECC_SECP256R1;
  16089. int curve_idx = wc_ecc_get_curve_idx(curve_id);
  16090. AssertIntNE(curve_idx, ECC_CURVE_INVALID);
  16091. ecc_set = wc_ecc_get_curve_params(curve_idx);
  16092. AssertNotNull(ecc_set);
  16093. AssertIntEQ(ecc_set->id, curve_id);
  16094. #endif
  16095. /* Test case when SECP256R1 is not enabled */
  16096. /* Test that we get curve params for index 0 */
  16097. ecc_set = wc_ecc_get_curve_params(0);
  16098. AssertNotNull(ecc_set);
  16099. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  16100. }
  16101. /*
  16102. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  16103. */
  16104. static int test_wc_ecc_signVerify_hash (void)
  16105. {
  16106. int ret = 0;
  16107. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  16108. WC_RNG rng;
  16109. ecc_key key;
  16110. int signH = WOLFSSL_FATAL_ERROR;
  16111. #ifdef HAVE_ECC_VERIFY
  16112. int verifyH = WOLFSSL_FATAL_ERROR;
  16113. int verify = 0;
  16114. #endif
  16115. word32 siglen = ECC_BUFSIZE;
  16116. byte sig[ECC_BUFSIZE];
  16117. byte digest[] = "Everyone gets Friday off.";
  16118. word32 digestlen = (word32)XSTRLEN((char*)digest);
  16119. /* Init stack var */
  16120. XMEMSET(sig, 0, siglen);
  16121. XMEMSET(&key, 0, sizeof(key));
  16122. /* Init structs. */
  16123. ret = wc_InitRng(&rng);
  16124. if (ret == 0) {
  16125. ret = wc_ecc_init(&key);
  16126. if (ret == 0) {
  16127. ret = wc_ecc_make_key(&rng, KEY14, &key);
  16128. }
  16129. }
  16130. printf(testingFmt, "wc_ecc_sign_hash()");
  16131. if (ret == 0) {
  16132. ret = wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key);
  16133. }
  16134. /* Check bad args. */
  16135. if (ret == 0) {
  16136. signH = wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key);
  16137. if (signH == ECC_BAD_ARG_E) {
  16138. signH = wc_ecc_sign_hash(digest, digestlen, NULL, &siglen,
  16139. &rng, &key);
  16140. }
  16141. if (signH == ECC_BAD_ARG_E) {
  16142. signH = wc_ecc_sign_hash(digest, digestlen, sig, NULL,
  16143. &rng, &key);
  16144. }
  16145. if (signH == ECC_BAD_ARG_E) {
  16146. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  16147. NULL, &key);
  16148. }
  16149. if (signH == ECC_BAD_ARG_E) {
  16150. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  16151. &rng, NULL);
  16152. }
  16153. if (signH == ECC_BAD_ARG_E) {
  16154. signH = 0;
  16155. } else if (ret == 0) {
  16156. signH = WOLFSSL_FATAL_ERROR;
  16157. }
  16158. }
  16159. printf(resultFmt, signH == 0 ? passed : failed);
  16160. #ifdef HAVE_ECC_VERIFY
  16161. printf(testingFmt, "wc_ecc_verify_hash()");
  16162. ret = wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify, &key);
  16163. if (verify != 1 && ret == 0) {
  16164. ret = WOLFSSL_FATAL_ERROR;
  16165. }
  16166. /* Test bad args. */
  16167. if (ret == 0) {
  16168. verifyH = wc_ecc_verify_hash(NULL, siglen, digest, digestlen,
  16169. &verify, &key);
  16170. if (verifyH == ECC_BAD_ARG_E) {
  16171. verifyH = wc_ecc_verify_hash(sig, siglen, NULL, digestlen,
  16172. &verify, &key);
  16173. }
  16174. if (verifyH == ECC_BAD_ARG_E) {
  16175. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  16176. NULL, &key);
  16177. }
  16178. if (verifyH == ECC_BAD_ARG_E) {
  16179. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  16180. &verify, NULL);
  16181. }
  16182. if (verifyH == ECC_BAD_ARG_E) {
  16183. verifyH = 0;
  16184. } else if (ret == 0) {
  16185. verifyH = WOLFSSL_FATAL_ERROR;
  16186. }
  16187. }
  16188. printf(resultFmt, verifyH == 0 ? passed : failed);
  16189. #endif /* HAVE_ECC_VERIFY */
  16190. if (wc_FreeRng(&rng) && ret == 0) {
  16191. ret = WOLFSSL_FATAL_ERROR;
  16192. }
  16193. wc_ecc_free(&key);
  16194. #ifdef FP_ECC
  16195. wc_ecc_fp_free();
  16196. #endif
  16197. #endif
  16198. return ret;
  16199. } /* END test_wc_ecc_sign_hash */
  16200. /*
  16201. * Testing wc_ecc_shared_secret()
  16202. */
  16203. static int test_wc_ecc_shared_secret (void)
  16204. {
  16205. int ret = 0;
  16206. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  16207. ecc_key key, pubKey;
  16208. WC_RNG rng;
  16209. int keySz = KEY16;
  16210. byte out[KEY16];
  16211. word32 outlen = (word32)sizeof(out);
  16212. /* Initialize variables. */
  16213. XMEMSET(out, 0, keySz);
  16214. XMEMSET(&rng, 0, sizeof(rng));
  16215. XMEMSET(&key, 0, sizeof(key));
  16216. XMEMSET(&pubKey, 0, sizeof(pubKey));
  16217. ret = wc_InitRng(&rng);
  16218. if (ret == 0) {
  16219. ret = wc_ecc_init(&key);
  16220. if (ret == 0) {
  16221. ret = wc_ecc_init(&pubKey);
  16222. }
  16223. }
  16224. if (ret == 0) {
  16225. ret = wc_ecc_make_key(&rng, keySz, &key);
  16226. }
  16227. if (ret == 0) {
  16228. ret = wc_ecc_make_key(&rng, keySz, &pubKey);
  16229. }
  16230. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  16231. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  16232. !defined(HAVE_SELFTEST)
  16233. if (ret == 0) {
  16234. ret = wc_ecc_set_rng(&key, &rng);
  16235. }
  16236. #endif
  16237. printf(testingFmt, "wc_ecc_shared_secret()");
  16238. if (ret == 0) {
  16239. ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
  16240. /* Test bad args. */
  16241. if (ret == 0) {
  16242. ret = wc_ecc_shared_secret(NULL, &pubKey, out, &outlen);
  16243. if (ret == BAD_FUNC_ARG) {
  16244. ret = wc_ecc_shared_secret(&key, NULL, out, &outlen);
  16245. }
  16246. if (ret == BAD_FUNC_ARG) {
  16247. ret = wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen);
  16248. }
  16249. if (ret == BAD_FUNC_ARG) {
  16250. ret = wc_ecc_shared_secret(&key, &pubKey, out, NULL);
  16251. }
  16252. if (ret == BAD_FUNC_ARG) {
  16253. ret = 0;
  16254. } else if (ret == 0) {
  16255. ret = WOLFSSL_FATAL_ERROR;
  16256. }
  16257. }
  16258. }
  16259. printf(resultFmt, ret == 0 ? passed : failed);
  16260. if (wc_FreeRng(&rng) && ret == 0) {
  16261. ret = WOLFSSL_FATAL_ERROR;
  16262. }
  16263. wc_ecc_free(&key);
  16264. wc_ecc_free(&pubKey);
  16265. #ifdef FP_ECC
  16266. wc_ecc_fp_free();
  16267. #endif
  16268. #endif
  16269. return ret;
  16270. } /* END tests_wc_ecc_shared_secret */
  16271. /*
  16272. * testint wc_ecc_export_x963()
  16273. */
  16274. static int test_wc_ecc_export_x963 (void)
  16275. {
  16276. int ret = 0;
  16277. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  16278. ecc_key key;
  16279. WC_RNG rng;
  16280. byte out[ECC_ASN963_MAX_BUF_SZ];
  16281. word32 outlen = sizeof(out);
  16282. /* Initialize variables. */
  16283. XMEMSET(out, 0, outlen);
  16284. XMEMSET(&rng, 0, sizeof(rng));
  16285. XMEMSET(&key, 0, sizeof(key));
  16286. ret = wc_InitRng(&rng);
  16287. if (ret == 0) {
  16288. ret = wc_ecc_init(&key);
  16289. if (ret == 0) {
  16290. ret = wc_ecc_make_key(&rng, KEY20, &key);
  16291. }
  16292. }
  16293. printf(testingFmt, "wc_ecc_export_x963()");
  16294. if (ret == 0) {
  16295. ret = wc_ecc_export_x963(&key, out, &outlen);
  16296. }
  16297. /* Test bad args. */
  16298. if (ret == 0) {
  16299. ret = wc_ecc_export_x963(NULL, out, &outlen);
  16300. if (ret == ECC_BAD_ARG_E) {
  16301. ret = wc_ecc_export_x963(&key, NULL, &outlen);
  16302. }
  16303. if (ret == LENGTH_ONLY_E) {
  16304. ret = wc_ecc_export_x963(&key, out, NULL);
  16305. }
  16306. if (ret == ECC_BAD_ARG_E) {
  16307. key.idx = -4;
  16308. ret = wc_ecc_export_x963(&key, out, &outlen);
  16309. }
  16310. if (ret == ECC_BAD_ARG_E) {
  16311. ret = 0;
  16312. } else {
  16313. ret = WOLFSSL_FATAL_ERROR;
  16314. }
  16315. }
  16316. printf(resultFmt, ret == 0 ? passed : failed);
  16317. if (wc_FreeRng(&rng) && ret == 0) {
  16318. ret = WOLFSSL_FATAL_ERROR;
  16319. }
  16320. wc_ecc_free(&key);
  16321. #ifdef FP_ECC
  16322. wc_ecc_fp_free();
  16323. #endif
  16324. #endif
  16325. return ret;
  16326. } /* END test_wc_ecc_export_x963 */
  16327. /*
  16328. * Testing wc_ecc_export_x963_ex()
  16329. * compile with --enable-compkey will use compression.
  16330. */
  16331. static int test_wc_ecc_export_x963_ex (void)
  16332. {
  16333. int ret = 0;
  16334. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  16335. ecc_key key;
  16336. WC_RNG rng;
  16337. byte out[ECC_ASN963_MAX_BUF_SZ];
  16338. word32 outlen = sizeof(out);
  16339. #ifdef HAVE_COMP_KEY
  16340. word32 badOutLen = 5;
  16341. #endif
  16342. /* Init stack variables. */
  16343. XMEMSET(out, 0, outlen);
  16344. XMEMSET(&rng, 0, sizeof(rng));
  16345. XMEMSET(&key, 0, sizeof(key));
  16346. ret = wc_InitRng(&rng);
  16347. if (ret == 0) {
  16348. ret = wc_ecc_init(&key);
  16349. if (ret == 0) {
  16350. ret = wc_ecc_make_key(&rng, KEY64, &key);
  16351. }
  16352. }
  16353. printf(testingFmt, "wc_ecc_export_x963_ex()");
  16354. #ifdef HAVE_COMP_KEY
  16355. if (ret == 0) {
  16356. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  16357. }
  16358. #else
  16359. if (ret == 0) {
  16360. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  16361. }
  16362. #endif
  16363. /* Test bad args. */
  16364. #ifdef HAVE_COMP_KEY
  16365. if (ret == 0) {
  16366. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, COMP);
  16367. if (ret == BAD_FUNC_ARG) {
  16368. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP);
  16369. }
  16370. if (ret == BAD_FUNC_ARG) {
  16371. ret = wc_ecc_export_x963_ex(&key, out, NULL, COMP);
  16372. }
  16373. if (ret == BAD_FUNC_ARG) {
  16374. ret = wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP);
  16375. }
  16376. if (ret == BUFFER_E) {
  16377. key.idx = -4;
  16378. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  16379. }
  16380. if (ret == ECC_BAD_ARG_E) {
  16381. ret = 0;
  16382. } else {
  16383. ret = WOLFSSL_FATAL_ERROR;
  16384. }
  16385. }
  16386. #else
  16387. if (ret == 0) {
  16388. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP);
  16389. if (ret == BAD_FUNC_ARG) {
  16390. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP);
  16391. }
  16392. if (ret == BAD_FUNC_ARG) {
  16393. ret = wc_ecc_export_x963_ex(&key, out, &outlen, 1);
  16394. }
  16395. if (ret == NOT_COMPILED_IN) {
  16396. ret = wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP);
  16397. }
  16398. if (ret == BAD_FUNC_ARG) {
  16399. key.idx = -4;
  16400. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  16401. }
  16402. if (ret == ECC_BAD_ARG_E) {
  16403. ret = 0;
  16404. } else if (ret == 0) {
  16405. ret = WOLFSSL_FATAL_ERROR;
  16406. }
  16407. }
  16408. #endif
  16409. printf(resultFmt, ret == 0 ? passed : failed);
  16410. if (wc_FreeRng(&rng) && ret == 0) {
  16411. ret = WOLFSSL_FATAL_ERROR;
  16412. }
  16413. wc_ecc_free(&key);
  16414. #ifdef FP_ECC
  16415. wc_ecc_fp_free();
  16416. #endif
  16417. #endif
  16418. return ret;
  16419. } /* END test_wc_ecc_export_x963_ex */
  16420. /*
  16421. * testing wc_ecc_import_x963()
  16422. */
  16423. static int test_wc_ecc_import_x963 (void)
  16424. {
  16425. int ret = 0;
  16426. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  16427. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  16428. ecc_key pubKey, key;
  16429. WC_RNG rng;
  16430. byte x963[ECC_ASN963_MAX_BUF_SZ];
  16431. word32 x963Len = (word32)sizeof(x963);
  16432. /* Init stack variables. */
  16433. XMEMSET(x963, 0, x963Len);
  16434. XMEMSET(&rng, 0, sizeof(rng));
  16435. XMEMSET(&key, 0, sizeof(key));
  16436. XMEMSET(&pubKey, 0, sizeof(pubKey));
  16437. ret = wc_InitRng(&rng);
  16438. if (ret == 0) {
  16439. ret = wc_ecc_init(&pubKey);
  16440. if (ret == 0) {
  16441. ret = wc_ecc_init(&key);
  16442. }
  16443. if (ret == 0) {
  16444. ret = wc_ecc_make_key(&rng, KEY24, &key);
  16445. }
  16446. if (ret == 0) {
  16447. ret = wc_ecc_export_x963(&key, x963, &x963Len);
  16448. }
  16449. }
  16450. printf(testingFmt, "wc_ecc_import_x963()");
  16451. if (ret == 0) {
  16452. ret = wc_ecc_import_x963(x963, x963Len, &pubKey);
  16453. }
  16454. /* Test bad args. */
  16455. if (ret == 0) {
  16456. ret = wc_ecc_import_x963(NULL, x963Len, &pubKey);
  16457. if (ret == BAD_FUNC_ARG) {
  16458. ret = wc_ecc_import_x963(x963, x963Len, NULL);
  16459. }
  16460. if (ret == BAD_FUNC_ARG) {
  16461. ret = wc_ecc_import_x963(x963, x963Len + 1, &pubKey);
  16462. }
  16463. if (ret == ECC_BAD_ARG_E) {
  16464. ret = 0;
  16465. } else if (ret == 0) {
  16466. ret = WOLFSSL_FATAL_ERROR;
  16467. }
  16468. }
  16469. printf(resultFmt, ret == 0 ? passed : failed);
  16470. if (wc_FreeRng(&rng) && ret == 0) {
  16471. ret = WOLFSSL_FATAL_ERROR;
  16472. }
  16473. wc_ecc_free(&key);
  16474. wc_ecc_free(&pubKey);
  16475. #ifdef FP_ECC
  16476. wc_ecc_fp_free();
  16477. #endif
  16478. #endif
  16479. return ret;
  16480. } /* END wc_ecc_import_x963 */
  16481. /*
  16482. * testing wc_ecc_import_private_key()
  16483. */
  16484. static int ecc_import_private_key (void)
  16485. {
  16486. int ret = 0;
  16487. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  16488. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  16489. ecc_key key, keyImp;
  16490. WC_RNG rng;
  16491. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  16492. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  16493. word32 privKeySz = (word32)sizeof(privKey);
  16494. word32 x963KeySz = (word32)sizeof(x963Key);
  16495. /* Init stack variables. */
  16496. XMEMSET(privKey, 0, privKeySz);
  16497. XMEMSET(x963Key, 0, x963KeySz);
  16498. XMEMSET(&rng, 0, sizeof(rng));
  16499. XMEMSET(&key, 0, sizeof(key));
  16500. XMEMSET(&keyImp, 0, sizeof(keyImp));
  16501. ret = wc_InitRng(&rng);
  16502. if (ret == 0) {
  16503. ret = wc_ecc_init(&key);
  16504. if (ret == 0) {
  16505. ret = wc_ecc_init(&keyImp);
  16506. }
  16507. if (ret == 0) {
  16508. ret = wc_ecc_make_key(&rng, KEY48, &key);
  16509. }
  16510. if (ret == 0) {
  16511. ret = wc_ecc_export_x963(&key, x963Key, &x963KeySz);
  16512. }
  16513. if (ret == 0) {
  16514. ret = wc_ecc_export_private_only(&key, privKey, &privKeySz);
  16515. }
  16516. }
  16517. printf(testingFmt, "wc_ecc_import_private_key()");
  16518. if (ret == 0) {
  16519. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  16520. x963KeySz, &keyImp);
  16521. }
  16522. /* Pass in bad args. */
  16523. if (ret == 0) {
  16524. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  16525. x963KeySz, NULL);
  16526. if (ret == BAD_FUNC_ARG) {
  16527. ret = wc_ecc_import_private_key(NULL, privKeySz, x963Key,
  16528. x963KeySz, &keyImp);
  16529. }
  16530. if (ret == BAD_FUNC_ARG) {
  16531. ret = 0;
  16532. } else if (ret == 0) {
  16533. ret = WOLFSSL_FATAL_ERROR;
  16534. }
  16535. }
  16536. printf(resultFmt, ret == 0 ? passed : failed);
  16537. if (wc_FreeRng(&rng) && ret == 0) {
  16538. ret = WOLFSSL_FATAL_ERROR;
  16539. }
  16540. wc_ecc_free(&key);
  16541. wc_ecc_free(&keyImp);
  16542. #ifdef FP_ECC
  16543. wc_ecc_fp_free();
  16544. #endif
  16545. #endif
  16546. return ret;
  16547. } /* END wc_ecc_import_private_key */
  16548. /*
  16549. * Testing wc_ecc_export_private_only()
  16550. */
  16551. static int test_wc_ecc_export_private_only (void)
  16552. {
  16553. int ret = 0;
  16554. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  16555. ecc_key key;
  16556. WC_RNG rng;
  16557. byte out[ECC_PRIV_KEY_BUF];
  16558. word32 outlen = sizeof(out);
  16559. /* Init stack variables. */
  16560. XMEMSET(out, 0, outlen);
  16561. XMEMSET(&rng, 0, sizeof(rng));
  16562. XMEMSET(&key, 0, sizeof(key));
  16563. ret = wc_InitRng(&rng);
  16564. if (ret == 0) {
  16565. ret = wc_ecc_init(&key);
  16566. if (ret == 0) {
  16567. ret = wc_ecc_make_key(&rng, KEY32, &key);
  16568. }
  16569. }
  16570. printf(testingFmt, "wc_ecc_export_private_only()");
  16571. if (ret == 0) {
  16572. ret = wc_ecc_export_private_only(&key, out, &outlen);
  16573. }
  16574. /* Pass in bad args. */
  16575. if (ret == 0) {
  16576. ret = wc_ecc_export_private_only(NULL, out, &outlen);
  16577. if (ret == BAD_FUNC_ARG) {
  16578. ret = wc_ecc_export_private_only(&key, NULL, &outlen);
  16579. }
  16580. if (ret == BAD_FUNC_ARG) {
  16581. ret = wc_ecc_export_private_only(&key, out, NULL);
  16582. }
  16583. if (ret == BAD_FUNC_ARG) {
  16584. ret = 0;
  16585. } else if (ret == 0) {
  16586. ret = WOLFSSL_FATAL_ERROR;
  16587. }
  16588. }
  16589. printf(resultFmt, ret == 0 ? passed : failed);
  16590. if (wc_FreeRng(&rng) && ret == 0) {
  16591. ret = WOLFSSL_FATAL_ERROR;
  16592. }
  16593. wc_ecc_free(&key);
  16594. #ifdef FP_ECC
  16595. wc_ecc_fp_free();
  16596. #endif
  16597. #endif
  16598. return ret;
  16599. } /* END test_wc_ecc_export_private_only */
  16600. /*
  16601. * Testing wc_ecc_rs_to_sig()
  16602. */
  16603. static int test_wc_ecc_rs_to_sig (void)
  16604. {
  16605. int ret = 0;
  16606. #if defined(HAVE_ECC) && !defined(NO_ASN)
  16607. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  16608. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  16609. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  16610. byte sig[ECC_MAX_SIG_SIZE];
  16611. word32 siglen = (word32)sizeof(sig);
  16612. /*R and S max size is the order of curve. 2^192.*/
  16613. int keySz = KEY24;
  16614. byte r[KEY24];
  16615. byte s[KEY24];
  16616. word32 rlen = (word32)sizeof(r);
  16617. word32 slen = (word32)sizeof(s);
  16618. /* Init stack variables. */
  16619. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  16620. XMEMSET(r, 0, keySz);
  16621. XMEMSET(s, 0, keySz);
  16622. printf(testingFmt, "wc_ecc_rs_to_sig()");
  16623. ret = wc_ecc_rs_to_sig(R, S, sig, &siglen);
  16624. /* Test bad args. */
  16625. if (ret == 0) {
  16626. ret = wc_ecc_rs_to_sig(NULL, S, sig, &siglen);
  16627. if (ret == ECC_BAD_ARG_E) {
  16628. ret = wc_ecc_rs_to_sig(R, NULL, sig, &siglen);
  16629. }
  16630. if (ret == ECC_BAD_ARG_E) {
  16631. ret = wc_ecc_rs_to_sig(R, S, sig, NULL);
  16632. }
  16633. if (ret == ECC_BAD_ARG_E) {
  16634. ret = wc_ecc_rs_to_sig(R, S, NULL, &siglen);
  16635. }
  16636. if (ret == ECC_BAD_ARG_E) {
  16637. ret = 0;
  16638. } else {
  16639. ret = WOLFSSL_FATAL_ERROR;
  16640. }
  16641. }
  16642. printf(resultFmt, ret == 0 ? passed : failed);
  16643. printf(testingFmt, "wc_ecc_sig_to_rs()");
  16644. if (ret == 0) {
  16645. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen);
  16646. }
  16647. /* Test bad args. */
  16648. if (ret == 0) {
  16649. ret = wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen);
  16650. if (ret == ECC_BAD_ARG_E) {
  16651. ret = wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen);
  16652. }
  16653. if (ret == ECC_BAD_ARG_E) {
  16654. ret = wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen);
  16655. }
  16656. if (ret == ECC_BAD_ARG_E) {
  16657. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen);
  16658. }
  16659. if (ret == ECC_BAD_ARG_E) {
  16660. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL);
  16661. }
  16662. if (ret == ECC_BAD_ARG_E) {
  16663. ret = 0;
  16664. } else if (ret == 0) {
  16665. ret = WOLFSSL_FATAL_ERROR;
  16666. }
  16667. }
  16668. printf(resultFmt, ret == 0 ? passed : failed);
  16669. #endif
  16670. return ret;
  16671. } /* END test_wc_ecc_rs_to_sig */
  16672. static int test_wc_ecc_import_raw(void)
  16673. {
  16674. int ret = 0;
  16675. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  16676. ecc_key key;
  16677. const char* qx =
  16678. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  16679. const char* qy =
  16680. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  16681. const char* d =
  16682. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  16683. const char* curveName = "SECP256R1";
  16684. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  16685. const char* kNullStr = "";
  16686. #endif
  16687. ret = wc_ecc_init(&key);
  16688. printf(testingFmt, "wc_ecc_import_raw()");
  16689. if (ret == 0) {
  16690. ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
  16691. }
  16692. /* Test bad args. */
  16693. if (ret == 0) {
  16694. ret = wc_ecc_import_raw(NULL, qx, qy, d, curveName);
  16695. if (ret == BAD_FUNC_ARG) {
  16696. ret = wc_ecc_import_raw(&key, NULL, qy, d, curveName);
  16697. }
  16698. if (ret == BAD_FUNC_ARG) {
  16699. ret = wc_ecc_import_raw(&key, qx, NULL, d, curveName);
  16700. }
  16701. if (ret == BAD_FUNC_ARG) {
  16702. ret = wc_ecc_import_raw(&key, qx, qy, d, NULL);
  16703. }
  16704. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  16705. if (ret == BAD_FUNC_ARG) {
  16706. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  16707. wc_ecc_free(&key);
  16708. #endif
  16709. ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr, curveName);
  16710. if (ret == ECC_INF_E)
  16711. ret = BAD_FUNC_ARG; /* This is expected by other tests */
  16712. }
  16713. #endif
  16714. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  16715. if (ret == BAD_FUNC_ARG) {
  16716. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  16717. wc_ecc_free(&key);
  16718. #endif
  16719. ret = wc_ecc_import_raw(&key, "0", qy, d, curveName);
  16720. }
  16721. if (ret == BAD_FUNC_ARG) {
  16722. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  16723. wc_ecc_free(&key);
  16724. #endif
  16725. ret = wc_ecc_import_raw(&key, qx, "0", d, curveName);
  16726. }
  16727. #endif
  16728. if (ret == BAD_FUNC_ARG) {
  16729. ret = 0;
  16730. }
  16731. }
  16732. printf(resultFmt, ret == 0 ? passed : failed);
  16733. wc_ecc_free(&key);
  16734. #endif
  16735. return ret;
  16736. } /* END test_wc_ecc_import_raw */
  16737. static int test_wc_ecc_import_unsigned(void)
  16738. {
  16739. int ret = 0;
  16740. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  16741. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  16742. ecc_key key;
  16743. const byte qx[] = {
  16744. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  16745. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  16746. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  16747. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  16748. };
  16749. const byte qy[] = {
  16750. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  16751. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  16752. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  16753. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  16754. };
  16755. const byte d[] = {
  16756. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  16757. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  16758. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  16759. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  16760. };
  16761. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  16762. const byte nullBytes[32] = {0};
  16763. #endif
  16764. int curveId = ECC_SECP256R1;
  16765. ret = wc_ecc_init(&key);
  16766. printf(testingFmt, "wc_ecc_import_unsigned()");
  16767. if (ret == 0) {
  16768. ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  16769. curveId);
  16770. }
  16771. /* Test bad args. */
  16772. if (ret == 0) {
  16773. ret = wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  16774. curveId);
  16775. if (ret == BAD_FUNC_ARG) {
  16776. ret = wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  16777. curveId);
  16778. }
  16779. if (ret == BAD_FUNC_ARG) {
  16780. ret = wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  16781. curveId);
  16782. }
  16783. if (ret == BAD_FUNC_ARG) {
  16784. ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  16785. ECC_CURVE_INVALID);
  16786. }
  16787. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  16788. if (ret == BAD_FUNC_ARG) {
  16789. ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  16790. (byte*)nullBytes, (byte*)nullBytes, curveId);
  16791. }
  16792. #endif
  16793. if (ret == BAD_FUNC_ARG || ret == ECC_INF_E) {
  16794. ret = 0;
  16795. }
  16796. }
  16797. printf(resultFmt, ret == 0 ? passed : failed);
  16798. wc_ecc_free(&key);
  16799. #endif
  16800. return ret;
  16801. } /* END test_wc_ecc_import_unsigned */
  16802. /*
  16803. * Testing wc_ecc_sig_size()
  16804. */
  16805. static int test_wc_ecc_sig_size (void)
  16806. {
  16807. int ret = 0;
  16808. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  16809. ecc_key key;
  16810. WC_RNG rng;
  16811. int keySz = KEY16;
  16812. XMEMSET(&rng, 0, sizeof(rng));
  16813. XMEMSET(&key, 0, sizeof(key));
  16814. ret = wc_InitRng(&rng);
  16815. if (ret == 0) {
  16816. ret = wc_ecc_init(&key);
  16817. if (ret == 0) {
  16818. ret = wc_ecc_make_key(&rng, keySz, &key);
  16819. }
  16820. }
  16821. printf(testingFmt, "wc_ecc_sig_size()");
  16822. if (ret == 0) {
  16823. ret = wc_ecc_sig_size(&key);
  16824. if (ret <= (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ)) {
  16825. ret = 0;
  16826. }
  16827. }
  16828. printf(resultFmt, ret == 0 ? passed : failed);
  16829. if (wc_FreeRng(&rng) && ret == 0) {
  16830. ret = WOLFSSL_FATAL_ERROR;
  16831. }
  16832. wc_ecc_free(&key);
  16833. #endif
  16834. return ret;
  16835. } /* END test_wc_ecc_sig_size */
  16836. /*
  16837. * Testing wc_ecc_ctx_new()
  16838. */
  16839. static int test_wc_ecc_ctx_new (void)
  16840. {
  16841. int ret = 0;
  16842. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  16843. WC_RNG rng;
  16844. ecEncCtx* cli = NULL;
  16845. ecEncCtx* srv = NULL;
  16846. ret = wc_InitRng(&rng);
  16847. printf(testingFmt, "wc_ecc_ctx_new()");
  16848. if (ret == 0) {
  16849. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
  16850. srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng);
  16851. }
  16852. if (ret == 0 && (cli == NULL || srv == NULL)) {
  16853. ret = WOLFSSL_FATAL_ERROR;
  16854. }
  16855. wc_ecc_ctx_free(cli);
  16856. wc_ecc_ctx_free(srv);
  16857. /* Test bad args. */
  16858. if (ret == 0) {
  16859. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  16860. cli = wc_ecc_ctx_new(0, &rng);
  16861. if (cli != NULL) {
  16862. ret = WOLFSSL_FATAL_ERROR;
  16863. }
  16864. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL);
  16865. if (cli != NULL) {
  16866. ret = WOLFSSL_FATAL_ERROR;
  16867. }
  16868. }
  16869. printf(resultFmt, ret == 0 ? passed : failed);
  16870. if (wc_FreeRng(&rng) && ret == 0) {
  16871. ret = WOLFSSL_FATAL_ERROR;
  16872. }
  16873. wc_ecc_ctx_free(cli);
  16874. #endif
  16875. return ret;
  16876. } /* END test_wc_ecc_ctx_new */
  16877. /*
  16878. * Tesing wc_ecc_reset()
  16879. */
  16880. static int test_wc_ecc_ctx_reset (void)
  16881. {
  16882. int ret = 0;
  16883. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  16884. ecEncCtx* ctx = NULL;
  16885. WC_RNG rng;
  16886. ret = wc_InitRng(&rng);
  16887. if (ret == 0) {
  16888. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) {
  16889. ret = WOLFSSL_FATAL_ERROR;
  16890. }
  16891. }
  16892. printf(testingFmt, "wc_ecc_ctx_reset()");
  16893. if (ret == 0) {
  16894. ret = wc_ecc_ctx_reset(ctx, &rng);
  16895. }
  16896. /* Pass in bad args. */
  16897. if (ret == 0) {
  16898. ret = wc_ecc_ctx_reset(NULL, &rng);
  16899. if (ret == BAD_FUNC_ARG) {
  16900. ret = wc_ecc_ctx_reset(ctx, NULL);
  16901. }
  16902. if (ret == BAD_FUNC_ARG) {
  16903. ret = 0;
  16904. } else if (ret == 0) {
  16905. ret = WOLFSSL_FATAL_ERROR;
  16906. }
  16907. }
  16908. printf(resultFmt, ret == 0 ? passed : failed);
  16909. if (wc_FreeRng(&rng) && ret == 0) {
  16910. ret = WOLFSSL_FATAL_ERROR;
  16911. }
  16912. wc_ecc_ctx_free(ctx);
  16913. #endif
  16914. return ret;
  16915. } /* END test_wc_ecc_ctx_reset */
  16916. /*
  16917. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  16918. */
  16919. static int test_wc_ecc_ctx_set_peer_salt (void)
  16920. {
  16921. int ret = 0;
  16922. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  16923. WC_RNG rng;
  16924. ecEncCtx* cliCtx = NULL;
  16925. ecEncCtx* servCtx = NULL;
  16926. const byte* cliSalt = NULL;
  16927. const byte* servSalt = NULL;
  16928. ret = wc_InitRng(&rng);
  16929. if (ret == 0) {
  16930. if ( ( (cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) ||
  16931. ( (servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng)) == NULL) ) {
  16932. ret = WOLFSSL_FATAL_ERROR;
  16933. }
  16934. }
  16935. printf(testingFmt, "wc_ecc_ctx_get_own_salt()");
  16936. /* Test bad args. */
  16937. if (ret == 0) {
  16938. cliSalt = wc_ecc_ctx_get_own_salt(NULL);
  16939. if (cliSalt != NULL) {
  16940. ret = WOLFSSL_FATAL_ERROR;
  16941. }
  16942. }
  16943. if (ret == 0) {
  16944. cliSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  16945. servSalt = wc_ecc_ctx_get_own_salt(servCtx);
  16946. if (cliSalt == NULL || servSalt == NULL) {
  16947. ret = WOLFSSL_FATAL_ERROR;
  16948. }
  16949. }
  16950. printf(resultFmt, ret == 0 ? passed : failed);
  16951. printf(testingFmt, "wc_ecc_ctx_set_peer_salt()");
  16952. if (ret == 0) {
  16953. ret = wc_ecc_ctx_set_peer_salt(cliCtx, servSalt);
  16954. }
  16955. /* Test bad args. */
  16956. if (ret == 0) {
  16957. ret = wc_ecc_ctx_set_peer_salt(NULL, servSalt);
  16958. if (ret == BAD_FUNC_ARG) {
  16959. ret = wc_ecc_ctx_set_peer_salt(cliCtx, NULL);
  16960. }
  16961. if (ret == BAD_FUNC_ARG) {
  16962. ret = 0;
  16963. } else if (ret == 0) {
  16964. ret = WOLFSSL_FATAL_ERROR;
  16965. }
  16966. }
  16967. printf(resultFmt, ret == 0 ? passed : failed);
  16968. if (wc_FreeRng(&rng) && ret == 0) {
  16969. ret = WOLFSSL_FATAL_ERROR;
  16970. }
  16971. wc_ecc_ctx_free(cliCtx);
  16972. wc_ecc_ctx_free(servCtx);
  16973. #endif
  16974. return ret;
  16975. } /* END test_wc_ecc_ctx_set_peer_salt */
  16976. /*
  16977. * Testing wc_ecc_ctx_set_info()
  16978. */
  16979. static int test_wc_ecc_ctx_set_info (void)
  16980. {
  16981. int ret = 0;
  16982. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  16983. ecEncCtx* ctx = NULL;
  16984. WC_RNG rng;
  16985. const char* optInfo = "Optional Test Info.";
  16986. int optInfoSz = (int)XSTRLEN(optInfo);
  16987. const char* badOptInfo = NULL;
  16988. ret = wc_InitRng(&rng);
  16989. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL || ret != 0 ) {
  16990. ret = WOLFSSL_FATAL_ERROR;
  16991. }
  16992. printf(testingFmt, "wc_ecc_ctx_set_info()");
  16993. if (ret == 0) {
  16994. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz);
  16995. }
  16996. /* Test bad args. */
  16997. if (ret == 0) {
  16998. ret = wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz);
  16999. if (ret == BAD_FUNC_ARG) {
  17000. ret = wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz);
  17001. }
  17002. if (ret == BAD_FUNC_ARG) {
  17003. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1);
  17004. }
  17005. if (ret == BAD_FUNC_ARG) {
  17006. ret = 0;
  17007. } else if (ret == 0) {
  17008. ret = WOLFSSL_FATAL_ERROR;
  17009. }
  17010. }
  17011. printf(resultFmt, ret == 0 ? passed : failed);
  17012. if (wc_FreeRng(&rng) && ret == 0) {
  17013. ret = WOLFSSL_FATAL_ERROR;
  17014. }
  17015. wc_ecc_ctx_free(ctx);
  17016. #endif
  17017. return ret;
  17018. } /* END test_wc_ecc_ctx_set_info */
  17019. /*
  17020. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  17021. */
  17022. static int test_wc_ecc_encryptDecrypt (void)
  17023. {
  17024. int ret = 0;
  17025. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && defined(WOLFSSL_AES_128) \
  17026. && !defined(WC_NO_RNG)
  17027. ecc_key srvKey, cliKey;
  17028. WC_RNG rng;
  17029. const char* msg = "EccBlock Size 16";
  17030. word32 msgSz = (word32)XSTRLEN(msg);
  17031. byte out[XSTRLEN(msg) + WC_SHA256_DIGEST_SIZE];
  17032. word32 outSz = (word32)sizeof(out);
  17033. byte plain[XSTRLEN(msg) + 1];
  17034. word32 plainSz = (word32)sizeof(plain);
  17035. int keySz = KEY20;
  17036. /* Init stack variables. */
  17037. XMEMSET(out, 0, outSz);
  17038. XMEMSET(plain, 0, plainSz);
  17039. XMEMSET(&rng, 0, sizeof(rng));
  17040. XMEMSET(&srvKey, 0, sizeof(srvKey));
  17041. XMEMSET(&cliKey, 0, sizeof(cliKey));
  17042. ret = wc_InitRng(&rng);
  17043. if (ret == 0) {
  17044. ret = wc_ecc_init(&cliKey);
  17045. if (ret == 0) {
  17046. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  17047. }
  17048. if (ret == 0) {
  17049. ret = wc_ecc_init(&srvKey);
  17050. }
  17051. if (ret == 0) {
  17052. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  17053. }
  17054. }
  17055. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  17056. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  17057. !defined(HAVE_SELFTEST)
  17058. if (ret == 0) {
  17059. ret = wc_ecc_set_rng(&srvKey, &rng);
  17060. }
  17061. if (ret == 0) {
  17062. ret = wc_ecc_set_rng(&cliKey, &rng);
  17063. }
  17064. #endif
  17065. printf(testingFmt, "wc_ecc_encrypt()");
  17066. if (ret == 0) {
  17067. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  17068. &outSz, NULL);
  17069. }
  17070. if (ret == 0) {
  17071. ret = wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out,
  17072. &outSz, NULL);
  17073. if (ret == BAD_FUNC_ARG) {
  17074. ret = wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out,
  17075. &outSz, NULL);
  17076. }
  17077. if (ret == BAD_FUNC_ARG) {
  17078. ret = wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out,
  17079. &outSz, NULL);
  17080. }
  17081. if (ret == BAD_FUNC_ARG) {
  17082. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  17083. &outSz, NULL);
  17084. }
  17085. if (ret == BAD_FUNC_ARG) {
  17086. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  17087. NULL, NULL);
  17088. }
  17089. if (ret == BAD_FUNC_ARG) {
  17090. ret = 0;
  17091. } else if (ret == 0) {
  17092. ret = WOLFSSL_FATAL_ERROR;
  17093. }
  17094. }
  17095. printf(resultFmt, ret == 0 ? passed : failed);
  17096. printf(testingFmt, "wc_ecc_decrypt()");
  17097. if (ret == 0) {
  17098. ret = wc_ecc_decrypt(&srvKey, &cliKey, out, outSz, plain,
  17099. &plainSz, NULL);
  17100. }
  17101. if (ret == 0) {
  17102. ret = wc_ecc_decrypt(NULL, &cliKey, out, outSz, plain,
  17103. &plainSz, NULL);
  17104. if (ret == BAD_FUNC_ARG) {
  17105. ret = wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain,
  17106. &plainSz, NULL);
  17107. }
  17108. if (ret == BAD_FUNC_ARG) {
  17109. ret = wc_ecc_decrypt(&srvKey, &cliKey, NULL, outSz, plain,
  17110. &plainSz, NULL);
  17111. }
  17112. if (ret == BAD_FUNC_ARG) {
  17113. ret = wc_ecc_decrypt(&srvKey, &cliKey, out, outSz, NULL,
  17114. &plainSz, NULL);
  17115. }
  17116. if (ret == BAD_FUNC_ARG) {
  17117. ret = wc_ecc_decrypt(&srvKey, &cliKey, out, outSz,
  17118. plain, NULL, NULL);
  17119. }
  17120. if (ret == BAD_FUNC_ARG) {
  17121. ret = 0;
  17122. } else if (ret == 0) {
  17123. ret = WOLFSSL_FATAL_ERROR;
  17124. }
  17125. }
  17126. if (XMEMCMP(msg, plain, msgSz) != 0) {
  17127. ret = WOLFSSL_FATAL_ERROR;
  17128. }
  17129. printf(resultFmt, ret == 0 ? passed : failed);
  17130. if (wc_FreeRng(&rng) && ret == 0) {
  17131. ret = WOLFSSL_FATAL_ERROR;
  17132. }
  17133. wc_ecc_free(&cliKey);
  17134. wc_ecc_free(&srvKey);
  17135. #endif
  17136. return ret;
  17137. } /* END test_wc_ecc_encryptDecrypt */
  17138. /*
  17139. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  17140. */
  17141. static int test_wc_ecc_del_point (void)
  17142. {
  17143. int ret = 0;
  17144. #if defined(HAVE_ECC)
  17145. ecc_point* pt;
  17146. printf(testingFmt, "wc_ecc_new_point()");
  17147. pt = wc_ecc_new_point();
  17148. if (!pt) {
  17149. ret = WOLFSSL_FATAL_ERROR;
  17150. }
  17151. printf(resultFmt, ret == 0 ? passed : failed);
  17152. wc_ecc_del_point(pt);
  17153. #endif
  17154. return ret;
  17155. } /* END test_wc_ecc_del_point */
  17156. /*
  17157. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  17158. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  17159. * and wc_ecc_cmp_point()
  17160. */
  17161. static int test_wc_ecc_pointFns (void)
  17162. {
  17163. int ret = 0;
  17164. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  17165. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  17166. !defined(WOLFSSL_ATECC608A)
  17167. ecc_key key;
  17168. WC_RNG rng;
  17169. ecc_point* point = NULL;
  17170. ecc_point* cpypt = NULL;
  17171. int idx = 0;
  17172. int keySz = KEY32;
  17173. byte der[DER_SZ(KEY32)];
  17174. word32 derlenChk = 0;
  17175. word32 derSz = DER_SZ(KEY32);
  17176. /* Init stack variables. */
  17177. XMEMSET(der, 0, derSz);
  17178. XMEMSET(&rng, 0, sizeof(rng));
  17179. XMEMSET(&key, 0, sizeof(key));
  17180. ret = wc_InitRng(&rng);
  17181. if (ret == 0) {
  17182. ret = wc_ecc_init(&key);
  17183. if (ret == 0) {
  17184. ret = wc_ecc_make_key(&rng, keySz, &key);
  17185. }
  17186. }
  17187. if (ret == 0) {
  17188. point = wc_ecc_new_point();
  17189. if (!point) {
  17190. ret = WOLFSSL_FATAL_ERROR;
  17191. }
  17192. }
  17193. if (ret == 0) {
  17194. cpypt = wc_ecc_new_point();
  17195. if (!cpypt) {
  17196. ret = WOLFSSL_FATAL_ERROR;
  17197. }
  17198. }
  17199. /* Export */
  17200. printf(testingFmt, "wc_ecc_export_point_der()");
  17201. if (ret == 0) {
  17202. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  17203. NULL, &derlenChk);
  17204. /* Check length value. */
  17205. if (derSz == derlenChk && ret == LENGTH_ONLY_E) {
  17206. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  17207. der, &derSz);
  17208. }
  17209. }
  17210. /* Test bad args. */
  17211. if (ret == 0) {
  17212. ret = wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz);
  17213. if (ret == ECC_BAD_ARG_E) {
  17214. ret = wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz);
  17215. }
  17216. if (ret == ECC_BAD_ARG_E) {
  17217. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  17218. der, NULL);
  17219. }
  17220. if (ret == ECC_BAD_ARG_E) {
  17221. ret = 0;
  17222. } else if (ret == 0) {
  17223. ret = WOLFSSL_FATAL_ERROR;
  17224. }
  17225. }
  17226. printf(resultFmt, ret == 0 ? passed : failed);
  17227. /* Import */
  17228. printf(testingFmt, "wc_ecc_import_point_der()");
  17229. if (ret == 0) {
  17230. ret = wc_ecc_import_point_der(der, derSz, idx, point);
  17231. /* Condition double checks wc_ecc_cmp_point(). */
  17232. if (ret == 0 && XMEMCMP(&key.pubkey, point, sizeof(key.pubkey))) {
  17233. ret = wc_ecc_cmp_point(&key.pubkey, point);
  17234. }
  17235. }
  17236. /* Test bad args. */
  17237. if (ret == 0) {
  17238. ret = wc_ecc_import_point_der(NULL, derSz, idx, point);
  17239. if (ret == ECC_BAD_ARG_E) {
  17240. ret = wc_ecc_import_point_der(der, derSz, idx, NULL);
  17241. }
  17242. if (ret == ECC_BAD_ARG_E) {
  17243. ret = wc_ecc_import_point_der(der, derSz, -1, point);
  17244. }
  17245. if (ret == ECC_BAD_ARG_E) {
  17246. ret = wc_ecc_import_point_der(der, derSz + 1, idx, point);
  17247. }
  17248. if (ret == ECC_BAD_ARG_E) {
  17249. ret = 0;
  17250. } else if (ret == 0) {
  17251. ret = WOLFSSL_FATAL_ERROR;
  17252. }
  17253. }
  17254. printf(resultFmt, ret == 0 ? passed : failed);
  17255. /* Copy */
  17256. printf(testingFmt, "wc_ecc_copy_point()");
  17257. if (ret == 0) {
  17258. ret = wc_ecc_copy_point(point, cpypt);
  17259. }
  17260. /* Test bad args. */
  17261. if (ret == 0) {
  17262. ret = wc_ecc_copy_point(NULL, cpypt);
  17263. if (ret == ECC_BAD_ARG_E) {
  17264. ret = wc_ecc_copy_point(point, NULL);
  17265. }
  17266. if (ret == ECC_BAD_ARG_E) {
  17267. ret = 0;
  17268. } else if (ret == 0) {
  17269. ret = WOLFSSL_FATAL_ERROR;
  17270. }
  17271. }
  17272. printf(resultFmt, ret == 0 ? passed : failed);
  17273. printf(testingFmt, "wc_ecc_cmp_point()");
  17274. /* Compare point */
  17275. if (ret == 0) {
  17276. ret = wc_ecc_cmp_point(point, cpypt);
  17277. }
  17278. /* Test bad args. */
  17279. if (ret == 0) {
  17280. ret = wc_ecc_cmp_point(NULL, cpypt);
  17281. if (ret == BAD_FUNC_ARG) {
  17282. ret = wc_ecc_cmp_point(point, NULL);
  17283. }
  17284. if (ret == BAD_FUNC_ARG) {
  17285. ret = 0;
  17286. } else if (ret == 0) {
  17287. ret = WOLFSSL_FATAL_ERROR;
  17288. }
  17289. }
  17290. printf(resultFmt, ret == 0 ? passed : failed);
  17291. printf(testingFmt, "wc_ecc_point_is_at_infinity()");
  17292. /* At infinity if return == 1, otherwise return == 0. */
  17293. if (ret == 0) {
  17294. ret = wc_ecc_point_is_at_infinity(point);
  17295. }
  17296. /* Test bad args. */
  17297. if (ret == 0) {
  17298. ret = wc_ecc_point_is_at_infinity(NULL);
  17299. if (ret == BAD_FUNC_ARG) {
  17300. ret = 0;
  17301. } else if (ret == 0) {
  17302. ret = WOLFSSL_FATAL_ERROR;
  17303. }
  17304. }
  17305. printf(resultFmt, ret == 0 ? passed : failed);
  17306. #ifdef USE_ECC_B_PARAM
  17307. printf(testingFmt, "wc_ecc_point_is_on_curve()");
  17308. /* On curve if ret == 0 */
  17309. if (ret == 0) {
  17310. ret = wc_ecc_point_is_on_curve(point, idx);
  17311. }
  17312. /* Test bad args. */
  17313. if (ret == 0) {
  17314. ret = wc_ecc_point_is_on_curve(NULL, idx);
  17315. if (ret == BAD_FUNC_ARG) {
  17316. ret = wc_ecc_point_is_on_curve(point, 1000);
  17317. }
  17318. if (ret == ECC_BAD_ARG_E) {
  17319. ret = 0;
  17320. } else if (ret == 0) {
  17321. ret = WOLFSSL_FATAL_ERROR;
  17322. }
  17323. }
  17324. printf(resultFmt, ret == 0 ? passed : failed);
  17325. #endif /* USE_ECC_B_PARAM */
  17326. /* Free */
  17327. wc_ecc_del_point(point);
  17328. wc_ecc_del_point(cpypt);
  17329. wc_ecc_free(&key);
  17330. if (wc_FreeRng(&rng) && ret == 0) {
  17331. ret = WOLFSSL_FATAL_ERROR;
  17332. }
  17333. #endif
  17334. return ret;
  17335. } /* END test_wc_ecc_pointFns */
  17336. /*
  17337. * Testing wc_ecc_sahred_secret_ssh()
  17338. */
  17339. static int test_wc_ecc_shared_secret_ssh (void)
  17340. {
  17341. int ret = 0;
  17342. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  17343. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  17344. !defined(WOLFSSL_ATECC608A)
  17345. ecc_key key, key2;
  17346. WC_RNG rng;
  17347. int keySz = KEY32;
  17348. int key2Sz = KEY24;
  17349. byte secret[KEY32];
  17350. word32 secretLen = keySz;
  17351. /* Init stack variables. */
  17352. XMEMSET(secret, 0, secretLen);
  17353. XMEMSET(&rng, 0, sizeof(rng));
  17354. XMEMSET(&key, 0, sizeof(key));
  17355. XMEMSET(&key2, 0, sizeof(key2));
  17356. /* Make keys */
  17357. ret = wc_InitRng(&rng);
  17358. if (ret == 0) {
  17359. ret = wc_ecc_init(&key);
  17360. if (ret == 0) {
  17361. ret = wc_ecc_make_key(&rng, keySz, &key);
  17362. }
  17363. if (wc_FreeRng(&rng) && ret == 0) {
  17364. ret = WOLFSSL_FATAL_ERROR;
  17365. }
  17366. }
  17367. if (ret == 0) {
  17368. ret = wc_InitRng(&rng);
  17369. if (ret == 0) {
  17370. ret = wc_ecc_init(&key2);
  17371. }
  17372. if (ret == 0) {
  17373. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  17374. }
  17375. }
  17376. printf(testingFmt, "ecc_shared_secret_ssh()");
  17377. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  17378. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  17379. !defined(HAVE_SELFTEST)
  17380. if (ret == 0) {
  17381. ret = wc_ecc_set_rng(&key, &rng);
  17382. }
  17383. #endif
  17384. if (ret == 0) {
  17385. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  17386. }
  17387. /* Pass in bad args. */
  17388. if (ret == 0) {
  17389. ret = wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret, &secretLen);
  17390. if (ret == BAD_FUNC_ARG) {
  17391. ret = wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen);
  17392. }
  17393. if (ret == BAD_FUNC_ARG) {
  17394. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen);
  17395. }
  17396. if (ret == BAD_FUNC_ARG) {
  17397. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL);
  17398. }
  17399. if (ret == BAD_FUNC_ARG) {
  17400. key.type = ECC_PUBLICKEY;
  17401. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  17402. if (ret == ECC_BAD_ARG_E) {
  17403. ret = 0;
  17404. } else if (ret == 0) {
  17405. ret = WOLFSSL_FATAL_ERROR;
  17406. }
  17407. } else if (ret == 0) {
  17408. ret = WOLFSSL_FATAL_ERROR;
  17409. }
  17410. }
  17411. printf(resultFmt, ret == 0 ? passed : failed);
  17412. if (wc_FreeRng(&rng) && ret == 0) {
  17413. ret = WOLFSSL_FATAL_ERROR;
  17414. }
  17415. wc_ecc_free(&key);
  17416. wc_ecc_free(&key2);
  17417. #ifdef FP_ECC
  17418. wc_ecc_fp_free();
  17419. #endif
  17420. #endif
  17421. return ret;
  17422. } /* END test_wc_ecc_shared_secret_ssh */
  17423. /*
  17424. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  17425. */
  17426. static int test_wc_ecc_verify_hash_ex (void)
  17427. {
  17428. int ret = 0;
  17429. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  17430. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  17431. !defined(WOLFSSL_ATECC608A)
  17432. ecc_key key;
  17433. WC_RNG rng;
  17434. mp_int r;
  17435. mp_int s;
  17436. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  17437. unsigned char iHash[] = "Everyone gets Friday off.......";
  17438. unsigned char shortHash[] = "Everyone gets Friday off.";
  17439. word32 hashlen = sizeof(hash);
  17440. word32 iHashLen = sizeof(iHash);
  17441. word32 shortHashLen = sizeof(shortHash);
  17442. int keySz = KEY32;
  17443. int sig = WOLFSSL_FATAL_ERROR;
  17444. int ver = WOLFSSL_FATAL_ERROR;
  17445. int verify_ok = 0;
  17446. /* Initialize r and s. */
  17447. ret = mp_init_multi(&r, &s, NULL, NULL, NULL, NULL);
  17448. if (ret != MP_OKAY) {
  17449. return MP_INIT_E;
  17450. }
  17451. ret = wc_InitRng(&rng);
  17452. if (ret == 0) {
  17453. ret = wc_ecc_init(&key);
  17454. if (ret == 0) {
  17455. ret = wc_ecc_make_key(&rng, keySz, &key);
  17456. }
  17457. }
  17458. if (ret == 0) {
  17459. ret = wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s);
  17460. if (ret == 0) {
  17461. /* verify_ok should be 1. */
  17462. ret = wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key);
  17463. if (verify_ok != 1 && ret == 0) {
  17464. ret = WOLFSSL_FATAL_ERROR;
  17465. }
  17466. }
  17467. if (ret == 0) {
  17468. /* verify_ok should be 0 */
  17469. ret = wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen,
  17470. &verify_ok, &key);
  17471. if (verify_ok != 0 && ret == 0) {
  17472. ret = WOLFSSL_FATAL_ERROR;
  17473. }
  17474. }
  17475. if (ret == 0) {
  17476. /* verify_ok should be 0. */
  17477. ret = wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  17478. &verify_ok, &key);
  17479. if (verify_ok != 0 && ret == 0) {
  17480. ret = WOLFSSL_FATAL_ERROR;
  17481. }
  17482. }
  17483. }
  17484. printf(testingFmt, "wc_ecc_sign_hash_ex()");
  17485. /* Test bad args. */
  17486. if (ret == 0) {
  17487. if (wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s)
  17488. == ECC_BAD_ARG_E) {
  17489. sig = 0;
  17490. }
  17491. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s)
  17492. != ECC_BAD_ARG_E) {
  17493. sig = WOLFSSL_FATAL_ERROR;
  17494. }
  17495. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s)
  17496. != ECC_BAD_ARG_E) {
  17497. sig = WOLFSSL_FATAL_ERROR;
  17498. }
  17499. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s)
  17500. != ECC_BAD_ARG_E) {
  17501. sig = WOLFSSL_FATAL_ERROR;
  17502. }
  17503. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL)
  17504. != ECC_BAD_ARG_E) {
  17505. sig = WOLFSSL_FATAL_ERROR;
  17506. }
  17507. }
  17508. printf(resultFmt, sig == 0 ? passed : failed);
  17509. printf(testingFmt, "wc_ecc_verify_hash_ex()");
  17510. /* Test bad args. */
  17511. if (ret == 0) {
  17512. if (wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen, &verify_ok, &key)
  17513. == ECC_BAD_ARG_E) {
  17514. ver = 0;
  17515. }
  17516. if (ver == 0 && wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  17517. &verify_ok, &key) != ECC_BAD_ARG_E) {
  17518. ver = WOLFSSL_FATAL_ERROR;
  17519. }
  17520. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  17521. &key) != ECC_BAD_ARG_E) {
  17522. ver = WOLFSSL_FATAL_ERROR;
  17523. }
  17524. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  17525. NULL, &key) != ECC_BAD_ARG_E) {
  17526. ver = WOLFSSL_FATAL_ERROR;
  17527. }
  17528. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  17529. &verify_ok, NULL) != ECC_BAD_ARG_E) {
  17530. ver = WOLFSSL_FATAL_ERROR;
  17531. }
  17532. }
  17533. printf(resultFmt, ver == 0 ? passed : failed);
  17534. wc_ecc_free(&key);
  17535. mp_free(&r);
  17536. mp_free(&s);
  17537. if (wc_FreeRng(&rng)) {
  17538. return WOLFSSL_FATAL_ERROR;
  17539. }
  17540. if (ret == 0 && (sig != 0 || ver != 0)) {
  17541. ret = WOLFSSL_FATAL_ERROR;
  17542. }
  17543. #endif
  17544. return ret;
  17545. } /* END test_wc_ecc_verify_hash_ex */
  17546. /*
  17547. * Testing wc_ecc_mulmod()
  17548. */
  17549. static int test_wc_ecc_mulmod (void)
  17550. {
  17551. int ret = 0;
  17552. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  17553. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  17554. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  17555. ecc_key key1, key2, key3;
  17556. WC_RNG rng;
  17557. ret = wc_InitRng(&rng);
  17558. if (ret == 0) {
  17559. ret = wc_ecc_init(&key1);
  17560. if (ret == 0) {
  17561. ret = wc_ecc_init(&key2);
  17562. }
  17563. if (ret == 0) {
  17564. ret = wc_ecc_init(&key3);
  17565. }
  17566. if (ret == 0) {
  17567. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  17568. }
  17569. wc_FreeRng(&rng);
  17570. }
  17571. if (ret == 0) {
  17572. ret = wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy, key1.dp->Af,
  17573. ECC_SECP256R1);
  17574. if (ret == 0) {
  17575. ret = wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  17576. key1.dp->prime, ECC_SECP256R1);
  17577. }
  17578. }
  17579. printf(testingFmt, "wc_ecc_mulmod()");
  17580. if (ret == 0) {
  17581. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey, &key2.k,
  17582. &key3.k, 1);
  17583. }
  17584. /* Test bad args. */
  17585. if (ret == 0) {
  17586. ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey, &key2.k,
  17587. &key3.k, 1);
  17588. if (ret == ECC_BAD_ARG_E) {
  17589. ret = wc_ecc_mulmod(&key1.k, NULL, &key3.pubkey, &key2.k,
  17590. &key3.k, 1);
  17591. }
  17592. if (ret == ECC_BAD_ARG_E) {
  17593. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, NULL, &key2.k,
  17594. &key3.k, 1);
  17595. }
  17596. if (ret == ECC_BAD_ARG_E) {
  17597. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey,
  17598. &key2.k, NULL, 1);
  17599. }
  17600. if (ret == ECC_BAD_ARG_E) {
  17601. ret = 0;
  17602. } else if (ret == 0) {
  17603. ret = WOLFSSL_FATAL_ERROR;
  17604. }
  17605. }
  17606. printf(resultFmt, ret == 0 ? passed : failed);
  17607. wc_ecc_free(&key1);
  17608. wc_ecc_free(&key2);
  17609. wc_ecc_free(&key3);
  17610. #ifdef FP_ECC
  17611. wc_ecc_fp_free();
  17612. #endif
  17613. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  17614. return ret;
  17615. } /* END test_wc_ecc_mulmod */
  17616. /*
  17617. * Testing wc_ecc_is_valid_idx()
  17618. */
  17619. static int test_wc_ecc_is_valid_idx (void)
  17620. {
  17621. int ret = 0;
  17622. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  17623. ecc_key key;
  17624. WC_RNG rng;
  17625. int iVal = -2;
  17626. int iVal2 = 3000;
  17627. XMEMSET(&rng, 0, sizeof(rng));
  17628. XMEMSET(&key, 0, sizeof(key));
  17629. ret = wc_InitRng(&rng);
  17630. if (ret == 0) {
  17631. ret = wc_ecc_init(&key);
  17632. if (ret == 0) {
  17633. ret = wc_ecc_make_key(&rng, 32, &key);
  17634. }
  17635. }
  17636. printf(testingFmt, "wc_ecc_is_valid_idx()");
  17637. if (ret == 0) {
  17638. ret = wc_ecc_is_valid_idx(key.idx);
  17639. if (ret == 1) {
  17640. ret = 0;
  17641. } else {
  17642. ret = WOLFSSL_FATAL_ERROR;
  17643. }
  17644. }
  17645. /* Test bad args. */
  17646. if (ret == 0) {
  17647. ret = wc_ecc_is_valid_idx(iVal); /* should return 0 */
  17648. if (ret == 0) {
  17649. ret = wc_ecc_is_valid_idx(iVal2);
  17650. }
  17651. if (ret != 0) {
  17652. ret = WOLFSSL_FATAL_ERROR;
  17653. }
  17654. }
  17655. printf(resultFmt, ret == 0 ? passed : failed);
  17656. if (wc_FreeRng(&rng) && ret == 0) {
  17657. ret = WOLFSSL_FATAL_ERROR;
  17658. }
  17659. wc_ecc_free(&key);
  17660. #ifdef FP_ECC
  17661. wc_ecc_fp_free();
  17662. #endif
  17663. #endif
  17664. return ret;
  17665. } /* END test_wc_ecc_is_valid_idx */
  17666. /*
  17667. * Testing ToTraditional
  17668. */
  17669. static int test_ToTraditional (void)
  17670. {
  17671. int ret = 0;
  17672. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  17673. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  17674. defined(OPENSSL_EXTRA_X509_SMALL))
  17675. XFILE f;
  17676. byte input[TWOK_BUF];
  17677. word32 sz;
  17678. printf(testingFmt, "ToTraditional()");
  17679. f = XFOPEN("./certs/server-keyPkcs8.der", "rb");
  17680. AssertTrue((f != XBADFILE));
  17681. sz = (word32)XFREAD(input, 1, sizeof(input), f);
  17682. XFCLOSE(f);
  17683. /* Good case */
  17684. ret = ToTraditional(input, sz);
  17685. if (ret > 0) {
  17686. ret = 0;
  17687. }
  17688. /* Bad cases */
  17689. if (ret == 0) {
  17690. ret = ToTraditional(NULL, 0);
  17691. if (ret == BAD_FUNC_ARG) {
  17692. ret = 0;
  17693. }
  17694. }
  17695. if (ret == 0) {
  17696. ret = ToTraditional(NULL, sz);
  17697. if (ret == BAD_FUNC_ARG) {
  17698. ret = 0;
  17699. }
  17700. }
  17701. if (ret == 0) {
  17702. ret = ToTraditional(input, 0);
  17703. if (ret == ASN_PARSE_E) {
  17704. ret = 0;
  17705. }
  17706. }
  17707. printf(resultFmt, ret == 0 ? passed : failed);
  17708. #endif
  17709. return ret;
  17710. }/* End test_ToTraditional*/
  17711. /*
  17712. * Testing wc_EccPrivateKeyToDer
  17713. */
  17714. static int test_wc_EccPrivateKeyToDer (void)
  17715. {
  17716. int ret = 0;
  17717. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  17718. byte output[ONEK_BUF];
  17719. ecc_key eccKey;
  17720. WC_RNG rng;
  17721. word32 inLen;
  17722. printf(testingFmt, "wc_EccPrivateKeyToDer()");
  17723. ret = wc_InitRng(&rng);
  17724. if (ret == 0) {
  17725. ret = wc_ecc_init(&eccKey);
  17726. if (ret == 0) {
  17727. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  17728. }
  17729. inLen = (word32)sizeof(output);
  17730. /* Bad Cases */
  17731. if (ret == 0) {
  17732. ret = wc_EccPrivateKeyToDer(NULL, NULL, 0);
  17733. if (ret == BAD_FUNC_ARG) {
  17734. ret = 0;
  17735. }
  17736. }
  17737. if (ret == 0) {
  17738. ret = wc_EccPrivateKeyToDer(NULL, output, inLen);
  17739. if (ret == BAD_FUNC_ARG) {
  17740. ret = 0;
  17741. }
  17742. }
  17743. if (ret == 0) {
  17744. ret = wc_EccPrivateKeyToDer(&eccKey, NULL, inLen);
  17745. if (ret == BAD_FUNC_ARG) {
  17746. ret = 0;
  17747. }
  17748. }
  17749. if (ret == 0) {
  17750. ret = wc_EccPrivateKeyToDer(&eccKey, output, 0);
  17751. if (ret == BAD_FUNC_ARG) {
  17752. ret = 0;
  17753. }
  17754. }
  17755. /*Good Case */
  17756. if (ret == 0) {
  17757. ret = wc_EccPrivateKeyToDer(&eccKey, output, inLen);
  17758. if (ret > 0) {
  17759. ret = 0;
  17760. }
  17761. }
  17762. wc_ecc_free(&eccKey);
  17763. }
  17764. wc_FreeRng(&rng);
  17765. printf(resultFmt, ret == 0 ? passed : failed);
  17766. #endif
  17767. return ret;
  17768. }/* End test_wc_EccPrivateKeyToDer*/
  17769. /*
  17770. * Testing wc_Ed25519KeyToDer
  17771. */
  17772. static int test_wc_Ed25519KeyToDer (void)
  17773. {
  17774. int ret = 0;
  17775. #if defined(HAVE_ED25519) && (defined(WOLFSSL_CERT_GEN) || \
  17776. defined(WOLFSSL_KEY_GEN))
  17777. byte output[ONEK_BUF];
  17778. ed25519_key ed25519Key;
  17779. WC_RNG rng;
  17780. word32 inLen;
  17781. printf(testingFmt, "wc_Ed25519KeyToDer()");
  17782. ret = wc_InitRng(&rng);
  17783. if (ret == 0) {
  17784. ret = wc_ed25519_init(&ed25519Key);
  17785. if (ret == 0) {
  17786. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  17787. }
  17788. inLen = (word32)sizeof(output);
  17789. /* Bad Cases */
  17790. if (ret == 0) {
  17791. ret = wc_Ed25519KeyToDer(NULL, NULL, 0);
  17792. if (ret == BAD_FUNC_ARG) {
  17793. ret = 0;
  17794. }
  17795. }
  17796. if (ret == 0) {
  17797. ret = wc_Ed25519KeyToDer(NULL, output, inLen);
  17798. if (ret == BAD_FUNC_ARG) {
  17799. ret = 0;
  17800. }
  17801. }
  17802. if (ret == 0) {
  17803. ret = wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen);
  17804. if (ret == BAD_FUNC_ARG) {
  17805. ret = 0;
  17806. }
  17807. }
  17808. if (ret == 0) {
  17809. ret = wc_Ed25519KeyToDer(&ed25519Key, output, 0);
  17810. if (ret == BAD_FUNC_ARG) {
  17811. ret = 0;
  17812. }
  17813. }
  17814. /* Good Case */
  17815. if (ret == 0) {
  17816. ret = wc_Ed25519KeyToDer(&ed25519Key, output, inLen);
  17817. if (ret > 0) {
  17818. ret = 0;
  17819. }
  17820. }
  17821. wc_ed25519_free(&ed25519Key);
  17822. }
  17823. wc_FreeRng(&rng);
  17824. printf(resultFmt, ret == 0 ? passed : failed);
  17825. #endif
  17826. return ret;
  17827. }/* End test_wc_Ed25519KeyToDer*/
  17828. /*
  17829. * Testing wc_Ed25519PrivateKeyToDer
  17830. */
  17831. static int test_wc_Ed25519PrivateKeyToDer (void)
  17832. {
  17833. int ret = 0;
  17834. #if defined(HAVE_ED25519) && (defined(WOLFSSL_CERT_GEN) || \
  17835. defined(WOLFSSL_KEY_GEN))
  17836. byte output[ONEK_BUF];
  17837. ed25519_key ed25519PrivKey;
  17838. WC_RNG rng;
  17839. word32 inLen;
  17840. printf(testingFmt, "wc_Ed25519PrivateKeyToDer()");
  17841. ret = wc_InitRng(&rng);
  17842. if (ret == 0) {
  17843. ret = wc_ed25519_init(&ed25519PrivKey);
  17844. if (ret == 0) {
  17845. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey);
  17846. }
  17847. inLen = (word32)sizeof(output);
  17848. /* Bad Cases */
  17849. if (ret == 0) {
  17850. ret = wc_Ed25519PrivateKeyToDer(NULL, NULL, 0);
  17851. if (ret == BAD_FUNC_ARG) {
  17852. ret = 0;
  17853. }
  17854. }
  17855. if (ret == 0) {
  17856. ret = wc_Ed25519PrivateKeyToDer(NULL, output, inLen);
  17857. if (ret == BAD_FUNC_ARG) {
  17858. ret = 0;
  17859. }
  17860. }
  17861. if (ret == 0) {
  17862. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen);
  17863. if (ret == BAD_FUNC_ARG) {
  17864. ret = 0;
  17865. }
  17866. }
  17867. if (ret == 0) {
  17868. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0);
  17869. if (ret == BAD_FUNC_ARG) {
  17870. ret = 0;
  17871. }
  17872. }
  17873. /* Good Case */
  17874. if (ret == 0) {
  17875. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen);
  17876. if (ret > 0) {
  17877. ret = 0;
  17878. }
  17879. }
  17880. wc_ed25519_free(&ed25519PrivKey);
  17881. }
  17882. wc_FreeRng(&rng);
  17883. printf(resultFmt, ret == 0 ? passed : failed);
  17884. #endif
  17885. return ret;
  17886. }/* End test_wc_Ed25519PrivateKeyToDer*/
  17887. /*
  17888. * Testing wc_Ed448KeyToDer
  17889. */
  17890. static int test_wc_Ed448KeyToDer (void)
  17891. {
  17892. int ret = 0;
  17893. #if defined(HAVE_ED448) && (defined(WOLFSSL_CERT_GEN) || \
  17894. defined(WOLFSSL_KEY_GEN))
  17895. byte output[ONEK_BUF];
  17896. ed448_key ed448Key;
  17897. WC_RNG rng;
  17898. word32 inLen;
  17899. printf(testingFmt, "wc_Ed448KeyToDer()");
  17900. ret = wc_InitRng(&rng);
  17901. if (ret == 0) {
  17902. ret = wc_ed448_init(&ed448Key);
  17903. if (ret == 0) {
  17904. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  17905. }
  17906. inLen = sizeof(output);
  17907. /* Bad Cases */
  17908. if (ret == 0) {
  17909. ret = wc_Ed448KeyToDer(NULL, NULL, 0);
  17910. if (ret == BAD_FUNC_ARG) {
  17911. ret = 0;
  17912. }
  17913. }
  17914. if (ret == 0) {
  17915. ret = wc_Ed448KeyToDer(NULL, output, inLen);
  17916. if (ret == BAD_FUNC_ARG) {
  17917. ret = 0;
  17918. }
  17919. }
  17920. if (ret == 0) {
  17921. ret = wc_Ed448KeyToDer(&ed448Key, NULL, inLen);
  17922. if (ret == BAD_FUNC_ARG) {
  17923. ret = 0;
  17924. }
  17925. }
  17926. if (ret == 0) {
  17927. ret = wc_Ed448KeyToDer(&ed448Key, output, 0);
  17928. if (ret == BAD_FUNC_ARG) {
  17929. ret = 0;
  17930. }
  17931. }
  17932. /* Good Case */
  17933. if (ret == 0) {
  17934. ret = wc_Ed448KeyToDer(&ed448Key, output, inLen);
  17935. if (ret > 0) {
  17936. ret = 0;
  17937. }
  17938. }
  17939. wc_ed448_free(&ed448Key);
  17940. }
  17941. wc_FreeRng(&rng);
  17942. printf(resultFmt, ret == 0 ? passed : failed);
  17943. #endif
  17944. return ret;
  17945. }/* End test_wc_Ed448KeyToDer*/
  17946. /*
  17947. * Testing wc_Ed448PrivateKeyToDer
  17948. */
  17949. static int test_wc_Ed448PrivateKeyToDer (void)
  17950. {
  17951. int ret = 0;
  17952. #if defined(HAVE_ED448) && (defined(WOLFSSL_CERT_GEN) || \
  17953. defined(WOLFSSL_KEY_GEN))
  17954. byte output[ONEK_BUF];
  17955. ed448_key ed448PrivKey;
  17956. WC_RNG rng;
  17957. word32 inLen;
  17958. printf(testingFmt, "wc_Ed448PrivateKeyToDer()");
  17959. ret = wc_InitRng(&rng);
  17960. if (ret == 0) {
  17961. ret = wc_ed448_init(&ed448PrivKey);
  17962. if (ret == 0) {
  17963. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey);
  17964. }
  17965. inLen = sizeof(output);
  17966. /* Bad Cases */
  17967. if (ret == 0) {
  17968. ret = wc_Ed448PrivateKeyToDer(NULL, NULL, 0);
  17969. if (ret == BAD_FUNC_ARG) {
  17970. ret = 0;
  17971. }
  17972. }
  17973. if (ret == 0) {
  17974. ret = wc_Ed448PrivateKeyToDer(NULL, output, inLen);
  17975. if (ret == BAD_FUNC_ARG) {
  17976. ret = 0;
  17977. }
  17978. }
  17979. if (ret == 0) {
  17980. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen);
  17981. if (ret == BAD_FUNC_ARG) {
  17982. ret = 0;
  17983. }
  17984. }
  17985. if (ret == 0) {
  17986. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0);
  17987. if (ret == BAD_FUNC_ARG) {
  17988. ret = 0;
  17989. }
  17990. }
  17991. /* Good case */
  17992. if (ret == 0) {
  17993. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen);
  17994. if (ret > 0) {
  17995. ret = 0;
  17996. }
  17997. }
  17998. wc_ed448_free(&ed448PrivKey);
  17999. }
  18000. wc_FreeRng(&rng);
  18001. printf(resultFmt, ret == 0 ? passed : failed);
  18002. #endif
  18003. return ret;
  18004. }/* End test_wc_Ed448PrivateKeyToDer*/
  18005. /*
  18006. * Testing wc_SetSubjectBuffer
  18007. */
  18008. static int test_wc_SetSubjectBuffer (void)
  18009. {
  18010. int ret = 0;
  18011. #if defined(WOLFSSL_CERT_GEN)
  18012. Cert cert;
  18013. FILE* file;
  18014. byte* der;
  18015. word32 derSz;
  18016. printf(testingFmt, "wc_SetSubjectBuffer()");
  18017. derSz = FOURK_BUF;
  18018. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18019. if (der == NULL) {
  18020. ret = -1;
  18021. }
  18022. if (ret == 0) {
  18023. file = XFOPEN("./certs/ca-cert.der", "rb");
  18024. if (file != NULL) {
  18025. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  18026. XFCLOSE(file);
  18027. }
  18028. }
  18029. if (ret == 0) {
  18030. ret = wc_InitCert(&cert);
  18031. }
  18032. if (ret == 0) {
  18033. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  18034. }
  18035. if (ret == 0) {
  18036. ret = wc_SetSubjectBuffer(NULL, der, derSz);
  18037. if (ret == BAD_FUNC_ARG) {
  18038. ret = 0;
  18039. }
  18040. }
  18041. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18042. printf(resultFmt, ret == 0 ? passed : failed);
  18043. #endif
  18044. return ret;
  18045. }/* End test_wc_SetSubjectBuffer*/
  18046. /*
  18047. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  18048. */
  18049. static int test_wc_SetSubjectKeyIdFromPublicKey_ex (void)
  18050. {
  18051. int ret = 0;
  18052. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  18053. WC_RNG rng;
  18054. Cert cert;
  18055. #if defined(HAVE_ED25519)
  18056. ed25519_key ed25519Key;
  18057. #endif
  18058. #if !defined(NO_RSA) && defined(HAVE_RSA)
  18059. RsaKey rsaKey;
  18060. int bits = 2048;
  18061. #endif
  18062. ecc_key eccKey;
  18063. #if defined(HAVE_ED448)
  18064. ed448_key ed448Key;
  18065. #endif
  18066. printf(testingFmt, "wc_SetSubjectKeyIdFromPublicKey_ex()");
  18067. #ifndef HAVE_FIPS
  18068. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  18069. #else
  18070. ret = wc_InitRng(&rng);
  18071. #endif
  18072. wc_InitCert(&cert);
  18073. #if defined(HAVE_ED25519)
  18074. if (ret == 0) { /*ED25519*/
  18075. ret = wc_ed25519_init(&ed25519Key);
  18076. if (ret == 0) {
  18077. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  18078. }
  18079. if (ret == 0) {
  18080. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  18081. &ed25519Key);
  18082. }
  18083. wc_ed25519_free(&ed25519Key);
  18084. }
  18085. #endif
  18086. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  18087. if (ret == 0) { /*RSA*/
  18088. ret = wc_InitRsaKey(&rsaKey, NULL);
  18089. if (ret == 0) {
  18090. MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
  18091. }
  18092. if (ret == 0) {
  18093. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
  18094. }
  18095. wc_FreeRsaKey(&rsaKey);
  18096. }
  18097. #endif
  18098. if (ret == 0) { /*ECC*/
  18099. ret = wc_ecc_init(&eccKey);
  18100. if (ret == 0) {
  18101. wc_ecc_make_key(&rng, KEY14, &eccKey);
  18102. }
  18103. if (ret == 0) {
  18104. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
  18105. }
  18106. wc_ecc_free(&eccKey);
  18107. }
  18108. #if defined(HAVE_ED448) && (defined(WOLFSSL_CERT_GEN) || \
  18109. defined(WOLFSSL_KEY_GEN))
  18110. if (ret == 0) { /*ED448*/
  18111. ret = wc_ed448_init(&ed448Key);
  18112. if (ret == 0) {
  18113. wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  18114. }
  18115. if (ret == 0) {
  18116. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  18117. &ed448Key);
  18118. }
  18119. wc_ed448_free(&ed448Key);
  18120. }
  18121. #endif
  18122. printf(resultFmt, ret == 0 ? passed : failed);
  18123. wc_FreeRng(&rng);
  18124. #endif
  18125. return ret;
  18126. }/* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  18127. /*
  18128. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  18129. */
  18130. static int test_wc_SetAuthKeyIdFromPublicKey_ex (void)
  18131. {
  18132. int ret = 0;
  18133. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  18134. WC_RNG rng;
  18135. Cert cert;
  18136. #if defined(HAVE_ED25519)
  18137. ed25519_key ed25519Key;
  18138. #endif
  18139. #if !defined(NO_RSA) && defined(HAVE_RSA)
  18140. RsaKey rsaKey;
  18141. int bits = 2048;
  18142. #endif
  18143. ecc_key eccKey;
  18144. #if defined(HAVE_ED448)
  18145. ed448_key ed448Key;
  18146. #endif
  18147. printf(testingFmt, "wc_SetAuthKeyIdFromPublicKey_ex()");
  18148. #ifndef HAVE_FIPS
  18149. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  18150. #else
  18151. ret = wc_InitRng(&rng);
  18152. #endif
  18153. wc_InitCert(&cert);
  18154. #if defined(HAVE_ED25519)
  18155. if (ret == 0) { /*ED25519*/
  18156. ret = wc_ed25519_init(&ed25519Key);
  18157. if (ret == 0) {
  18158. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  18159. }
  18160. if (ret == 0) {
  18161. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  18162. &ed25519Key);
  18163. }
  18164. wc_ed25519_free(&ed25519Key);
  18165. }
  18166. #endif
  18167. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  18168. if (ret == 0) { /*RSA*/
  18169. ret = wc_InitRsaKey(&rsaKey, NULL);
  18170. if (ret == 0) {
  18171. MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
  18172. }
  18173. if (ret == 0) {
  18174. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
  18175. }
  18176. wc_FreeRsaKey(&rsaKey);
  18177. }
  18178. #endif
  18179. if (ret == 0) { /*ECC*/
  18180. ret = wc_ecc_init(&eccKey);
  18181. if (ret == 0) {
  18182. wc_ecc_make_key(&rng, KEY14, &eccKey);
  18183. }
  18184. if (ret == 0) {
  18185. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
  18186. }
  18187. wc_ecc_free(&eccKey);
  18188. }
  18189. #if defined(HAVE_ED448) && (defined(WOLFSSL_CERT_GEN) || \
  18190. defined(WOLFSSL_KEY_GEN))
  18191. if (ret == 0) { /*ED448*/
  18192. ret = wc_ed448_init(&ed448Key);
  18193. if (ret == 0) {
  18194. wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  18195. }
  18196. if (ret == 0) {
  18197. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  18198. &ed448Key);
  18199. }
  18200. wc_ed448_free(&ed448Key);
  18201. }
  18202. #endif
  18203. printf(resultFmt, ret == 0 ? passed : failed);
  18204. wc_FreeRng(&rng);
  18205. #endif /*defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  18206. return ret;
  18207. }/* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  18208. /*
  18209. * Testing wc_PKCS7_New()
  18210. */
  18211. static void test_wc_PKCS7_New (void)
  18212. {
  18213. #if defined(HAVE_PKCS7)
  18214. PKCS7* pkcs7;
  18215. void* heap = NULL;
  18216. printf(testingFmt, "wc_PKCS7_New()");
  18217. pkcs7 = wc_PKCS7_New(heap, devId);
  18218. AssertNotNull(pkcs7);
  18219. printf(resultFmt, passed);
  18220. wc_PKCS7_Free(pkcs7);
  18221. #endif
  18222. } /* END test-wc_PKCS7_New */
  18223. /*
  18224. * Testing wc_PKCS7_Init()
  18225. */
  18226. static void test_wc_PKCS7_Init (void)
  18227. {
  18228. #if defined(HAVE_PKCS7)
  18229. PKCS7* pkcs7;
  18230. void* heap = NULL;
  18231. printf(testingFmt, "wc_PKCS7_Init()");
  18232. pkcs7 = wc_PKCS7_New(heap, devId);
  18233. AssertNotNull(pkcs7);
  18234. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, devId), 0);
  18235. /* Pass in bad args. */
  18236. AssertIntEQ(wc_PKCS7_Init(NULL, heap, devId), BAD_FUNC_ARG);
  18237. printf(resultFmt, passed);
  18238. wc_PKCS7_Free(pkcs7);
  18239. #endif
  18240. } /* END test-wc_PKCS7_Init */
  18241. /*
  18242. * Testing wc_PKCS7_InitWithCert()
  18243. */
  18244. static void test_wc_PKCS7_InitWithCert (void)
  18245. {
  18246. #if defined(HAVE_PKCS7)
  18247. PKCS7* pkcs7;
  18248. #ifndef NO_RSA
  18249. #if defined(USE_CERT_BUFFERS_2048)
  18250. unsigned char cert[sizeof_client_cert_der_2048];
  18251. int certSz = (int)sizeof(cert);
  18252. XMEMSET(cert, 0, certSz);
  18253. XMEMCPY(cert, client_cert_der_2048, sizeof_client_cert_der_2048);
  18254. #elif defined(USE_CERT_BUFFERS_1024)
  18255. unsigned char cert[sizeof_client_cert_der_1024];
  18256. int certSz = (int)sizeof(cert);
  18257. XMEMSET(cert, 0, certSz);
  18258. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  18259. #else
  18260. unsigned char cert[ONEK_BUF];
  18261. XFILE fp;
  18262. int certSz;
  18263. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  18264. AssertTrue(fp != XBADFILE);
  18265. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  18266. XFCLOSE(fp);
  18267. #endif
  18268. #elif defined(HAVE_ECC)
  18269. #if defined(USE_CERT_BUFFERS_256)
  18270. unsigned char cert[sizeof_cliecc_cert_der_256];
  18271. int certSz = (int)sizeof(cert);
  18272. XMEMSET(cert, 0, certSz);
  18273. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  18274. #else
  18275. unsigned char cert[ONEK_BUF];
  18276. XFILE fp;
  18277. int certSz;
  18278. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  18279. AssertTrue(fp != XBADFILE);
  18280. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  18281. XFCLOSE(fp);
  18282. #endif
  18283. #else
  18284. #error PKCS7 requires ECC or RSA
  18285. #endif
  18286. printf(testingFmt, "wc_PKCS7_InitWithCert()");
  18287. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  18288. /* If initialization is not successful, it's free'd in init func. */
  18289. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  18290. wc_PKCS7_Free(pkcs7);
  18291. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  18292. /* Valid initialization usage. */
  18293. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  18294. /* Pass in bad args. No need free for null checks, free at end.*/
  18295. AssertIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  18296. BAD_FUNC_ARG);
  18297. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  18298. BAD_FUNC_ARG);
  18299. printf(resultFmt, passed);
  18300. wc_PKCS7_Free(pkcs7);
  18301. #endif
  18302. } /* END test_wc_PKCS7_InitWithCert */
  18303. /*
  18304. * Testing wc_PKCS7_EncodeData()
  18305. */
  18306. static void test_wc_PKCS7_EncodeData (void)
  18307. {
  18308. #if defined(HAVE_PKCS7)
  18309. PKCS7* pkcs7;
  18310. byte output[FOURK_BUF];
  18311. byte data[] = "My encoded DER cert.";
  18312. #ifndef NO_RSA
  18313. #if defined(USE_CERT_BUFFERS_2048)
  18314. unsigned char cert[sizeof_client_cert_der_2048];
  18315. unsigned char key[sizeof_client_key_der_2048];
  18316. int certSz = (int)sizeof(cert);
  18317. int keySz = (int)sizeof(key);
  18318. XMEMSET(cert, 0, certSz);
  18319. XMEMSET(key, 0, keySz);
  18320. XMEMCPY(cert, client_cert_der_2048, certSz);
  18321. XMEMCPY(key, client_key_der_2048, keySz);
  18322. #elif defined(USE_CERT_BUFFERS_1024)
  18323. unsigned char cert[sizeof_client_cert_der_1024];
  18324. unsigned char key[sizeof_client_key_der_1024];
  18325. int certSz = (int)sizeof(cert);
  18326. int keySz = (int)sizeof(key);
  18327. XMEMSET(cert, 0, certSz);
  18328. XMEMSET(key, 0, keySz);
  18329. XMEMCPY(cert, client_cert_der_1024, certSz);
  18330. XMEMCPY(key, client_key_der_1024, keySz);
  18331. #else
  18332. unsigned char cert[ONEK_BUF];
  18333. unsigned char key[ONEK_BUF];
  18334. XFILE fp;
  18335. int certSz;
  18336. int keySz;
  18337. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  18338. AssertTrue(fp != XBADFILE);
  18339. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  18340. XFCLOSE(fp);
  18341. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  18342. AssertTrue(fp != XBADFILE);
  18343. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  18344. XFCLOSE(fp);
  18345. #endif
  18346. #elif defined(HAVE_ECC)
  18347. #if defined(USE_CERT_BUFFERS_256)
  18348. unsigned char cert[sizeof_cliecc_cert_der_256];
  18349. unsigned char key[sizeof_ecc_clikey_der_256];
  18350. int certSz = (int)sizeof(cert);
  18351. int keySz = (int)sizeof(key);
  18352. XMEMSET(cert, 0, certSz);
  18353. XMEMSET(key, 0, keySz);
  18354. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  18355. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  18356. #else
  18357. unsigned char cert[ONEK_BUF];
  18358. unsigned char key[ONEK_BUF];
  18359. XFILE fp;
  18360. int certSz, keySz;
  18361. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  18362. AssertTrue(fp != XBADFILE);
  18363. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  18364. XFCLOSE(fp);
  18365. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  18366. AssertTrue(fp != XBADFILE);
  18367. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  18368. XFCLOSE(fp);
  18369. #endif
  18370. #endif
  18371. XMEMSET(output, 0, sizeof(output));
  18372. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  18373. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  18374. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, certSz), 0);
  18375. printf(testingFmt, "wc_PKCS7_EncodeData()");
  18376. pkcs7->content = data;
  18377. pkcs7->contentSz = sizeof(data);
  18378. pkcs7->privateKey = key;
  18379. pkcs7->privateKeySz = keySz;
  18380. AssertIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  18381. /* Test bad args. */
  18382. AssertIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  18383. BAD_FUNC_ARG);
  18384. AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  18385. BAD_FUNC_ARG);
  18386. AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  18387. printf(resultFmt, passed);
  18388. wc_PKCS7_Free(pkcs7);
  18389. #endif
  18390. } /* END test_wc_PKCS7_EncodeData */
  18391. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  18392. !defined(NO_RSA) && !defined(NO_SHA256)
  18393. /* RSA sign raw digest callback */
  18394. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  18395. byte* out, word32 outSz, byte* privateKey,
  18396. word32 privateKeySz, int devid, int hashOID)
  18397. {
  18398. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  18399. byte digInfoEncoding[] = {
  18400. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  18401. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  18402. 0x00, 0x04, 0x20
  18403. };
  18404. int ret;
  18405. byte digestInfo[ONEK_BUF];
  18406. byte sig[FOURK_BUF];
  18407. word32 digestInfoSz = 0;
  18408. word32 idx = 0;
  18409. RsaKey rsa;
  18410. /* SHA-256 required only for this example callback due to above
  18411. * digInfoEncoding[] */
  18412. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  18413. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  18414. (hashOID != SHA256h)) {
  18415. return -1;
  18416. }
  18417. /* build DigestInfo */
  18418. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  18419. digestInfoSz += sizeof(digInfoEncoding);
  18420. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  18421. digestInfoSz += digestSz;
  18422. /* set up RSA key */
  18423. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  18424. if (ret != 0) {
  18425. return ret;
  18426. }
  18427. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  18428. /* sign DigestInfo */
  18429. if (ret == 0) {
  18430. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  18431. &rsa, pkcs7->rng);
  18432. if (ret > 0) {
  18433. if (ret > (int)outSz) {
  18434. /* output buffer too small */
  18435. ret = -1;
  18436. } else {
  18437. /* success, ret holds sig size */
  18438. XMEMCPY(out, sig, ret);
  18439. }
  18440. }
  18441. }
  18442. wc_FreeRsaKey(&rsa);
  18443. return ret;
  18444. }
  18445. #endif
  18446. /*
  18447. * Testing wc_PKCS7_EncodeSignedData()
  18448. */
  18449. static void test_wc_PKCS7_EncodeSignedData(void)
  18450. {
  18451. #if defined(HAVE_PKCS7)
  18452. PKCS7* pkcs7;
  18453. WC_RNG rng;
  18454. byte output[FOURK_BUF];
  18455. byte badOut[0];
  18456. word32 outputSz = (word32)sizeof(output);
  18457. word32 badOutSz = (word32)sizeof(badOut);
  18458. byte data[] = "Test data to encode.";
  18459. #ifndef NO_RSA
  18460. #if defined(USE_CERT_BUFFERS_2048)
  18461. byte key[sizeof_client_key_der_2048];
  18462. byte cert[sizeof_client_cert_der_2048];
  18463. word32 keySz = (word32)sizeof(key);
  18464. word32 certSz = (word32)sizeof(cert);
  18465. XMEMSET(key, 0, keySz);
  18466. XMEMSET(cert, 0, certSz);
  18467. XMEMCPY(key, client_key_der_2048, keySz);
  18468. XMEMCPY(cert, client_cert_der_2048, certSz);
  18469. #elif defined(USE_CERT_BUFFERS_1024)
  18470. byte key[sizeof_client_key_der_1024];
  18471. byte cert[sizeof_client_cert_der_1024];
  18472. word32 keySz = (word32)sizeof(key);
  18473. word32 certSz = (word32)sizeof(cert);
  18474. XMEMSET(key, 0, keySz);
  18475. XMEMSET(cert, 0, certSz);
  18476. XMEMCPY(key, client_key_der_1024, keySz);
  18477. XMEMCPY(cert, client_cert_der_1024, certSz);
  18478. #else
  18479. unsigned char cert[ONEK_BUF];
  18480. unsigned char key[ONEK_BUF];
  18481. XFILE fp;
  18482. int certSz;
  18483. int keySz;
  18484. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  18485. AssertTrue(fp != XBADFILE);
  18486. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  18487. XFCLOSE(fp);
  18488. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  18489. AssertTrue(fp != XBADFILE);
  18490. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  18491. XFCLOSE(fp);
  18492. #endif
  18493. #elif defined(HAVE_ECC)
  18494. #if defined(USE_CERT_BUFFERS_256)
  18495. unsigned char cert[sizeof_cliecc_cert_der_256];
  18496. unsigned char key[sizeof_ecc_clikey_der_256];
  18497. int certSz = (int)sizeof(cert);
  18498. int keySz = (int)sizeof(key);
  18499. XMEMSET(cert, 0, certSz);
  18500. XMEMSET(key, 0, keySz);
  18501. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  18502. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  18503. #else
  18504. unsigned char cert[ONEK_BUF];
  18505. unsigned char key[ONEK_BUF];
  18506. XFILE fp;
  18507. int certSz, keySz;
  18508. fp = XOPEN("./certs/client-ecc-cert.der", "rb");
  18509. AssertTrue(fp != XBADFILE);
  18510. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  18511. XFCLOSE(fp);
  18512. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  18513. AssertTrue(fp != XBADFILE);
  18514. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  18515. XFCLOSE(fp);
  18516. #endif
  18517. #endif
  18518. XMEMSET(output, 0, outputSz);
  18519. AssertIntEQ(wc_InitRng(&rng), 0);
  18520. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  18521. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  18522. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  18523. printf(testingFmt, "wc_PKCS7_EncodeSignedData()");
  18524. pkcs7->content = data;
  18525. pkcs7->contentSz = (word32)sizeof(data);
  18526. pkcs7->privateKey = key;
  18527. pkcs7->privateKeySz = (word32)sizeof(key);
  18528. pkcs7->encryptOID = RSAk;
  18529. pkcs7->hashOID = SHAh;
  18530. pkcs7->rng = &rng;
  18531. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  18532. wc_PKCS7_Free(pkcs7);
  18533. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  18534. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  18535. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  18536. /* Pass in bad args. */
  18537. AssertIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz), BAD_FUNC_ARG);
  18538. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  18539. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  18540. badOutSz), BAD_FUNC_ARG);
  18541. pkcs7->hashOID = 0; /* bad hashOID */
  18542. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), BAD_FUNC_ARG);
  18543. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  18544. !defined(NO_RSA) && !defined(NO_SHA256)
  18545. /* test RSA sign raw digest callback, if using RSA and compiled in.
  18546. * Example callback assumes SHA-256, so only run test if compiled in. */
  18547. wc_PKCS7_Free(pkcs7);
  18548. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  18549. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  18550. pkcs7->content = data;
  18551. pkcs7->contentSz = (word32)sizeof(data);
  18552. pkcs7->privateKey = key;
  18553. pkcs7->privateKeySz = (word32)sizeof(key);
  18554. pkcs7->encryptOID = RSAk;
  18555. pkcs7->hashOID = SHA256h;
  18556. pkcs7->rng = &rng;
  18557. AssertIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  18558. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  18559. #endif
  18560. printf(resultFmt, passed);
  18561. wc_PKCS7_Free(pkcs7);
  18562. wc_FreeRng(&rng);
  18563. #endif
  18564. } /* END test_wc_PKCS7_EncodeSignedData */
  18565. /*
  18566. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  18567. */
  18568. static void test_wc_PKCS7_EncodeSignedData_ex(void)
  18569. {
  18570. #if defined(HAVE_PKCS7)
  18571. int ret, i;
  18572. PKCS7* pkcs7;
  18573. WC_RNG rng;
  18574. byte outputHead[FOURK_BUF/2];
  18575. byte outputFoot[FOURK_BUF/2];
  18576. word32 outputHeadSz = (word32)sizeof(outputHead);
  18577. word32 outputFootSz = (word32)sizeof(outputFoot);
  18578. byte data[FOURK_BUF];
  18579. wc_HashAlg hash;
  18580. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  18581. byte hashBuf[WC_MAX_DIGEST_SIZE];
  18582. word32 hashSz = wc_HashGetDigestSize(hashType);
  18583. #ifndef NO_RSA
  18584. #if defined(USE_CERT_BUFFERS_2048)
  18585. byte key[sizeof_client_key_der_2048];
  18586. byte cert[sizeof_client_cert_der_2048];
  18587. word32 keySz = (word32)sizeof(key);
  18588. word32 certSz = (word32)sizeof(cert);
  18589. XMEMSET(key, 0, keySz);
  18590. XMEMSET(cert, 0, certSz);
  18591. XMEMCPY(key, client_key_der_2048, keySz);
  18592. XMEMCPY(cert, client_cert_der_2048, certSz);
  18593. #elif defined(USE_CERT_BUFFERS_1024)
  18594. byte key[sizeof_client_key_der_1024];
  18595. byte cert[sizeof_client_cert_der_1024];
  18596. word32 keySz = (word32)sizeof(key);
  18597. word32 certSz = (word32)sizeof(cert);
  18598. XMEMSET(key, 0, keySz);
  18599. XMEMSET(cert, 0, certSz);
  18600. XMEMCPY(key, client_key_der_1024, keySz);
  18601. XMEMCPY(cert, client_cert_der_1024, certSz);
  18602. #else
  18603. unsigned char cert[ONEK_BUF];
  18604. unsigned char key[ONEK_BUF];
  18605. XFILE fp;
  18606. int certSz;
  18607. int keySz;
  18608. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  18609. AssertTrue((fp != XBADFILE));
  18610. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  18611. XFCLOSE(fp);
  18612. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  18613. AssertTrue(fp != XBADFILE);
  18614. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  18615. XFCLOSE(fp);
  18616. #endif
  18617. #elif defined(HAVE_ECC)
  18618. #if defined(USE_CERT_BUFFERS_256)
  18619. unsigned char cert[sizeof_cliecc_cert_der_256];
  18620. unsigned char key[sizeof_ecc_clikey_der_256];
  18621. int certSz = (int)sizeof(cert);
  18622. int keySz = (int)sizeof(key);
  18623. XMEMSET(cert, 0, certSz);
  18624. XMEMSET(key, 0, keySz);
  18625. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  18626. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  18627. #else
  18628. unsigned char cert[ONEK_BUF];
  18629. unsigned char key[ONEK_BUF];
  18630. XFILE fp;
  18631. int certSz, keySz;
  18632. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  18633. AssertTrue(fp != XBADFILE);
  18634. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  18635. XFCLOSE(fp);
  18636. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  18637. AssertTrue(fp != XBADFILE);
  18638. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  18639. XFCLOSE(fp);
  18640. #endif
  18641. #endif
  18642. /* initialize large data with sequence */
  18643. for (i=0; i<(int)sizeof(data); i++)
  18644. data[i] = i & 0xff;
  18645. XMEMSET(outputHead, 0, outputHeadSz);
  18646. XMEMSET(outputFoot, 0, outputFootSz);
  18647. AssertIntEQ(wc_InitRng(&rng), 0);
  18648. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  18649. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  18650. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  18651. printf(testingFmt, "wc_PKCS7_EncodeSignedData()");
  18652. pkcs7->content = NULL; /* not used for ex */
  18653. pkcs7->contentSz = (word32)sizeof(data);
  18654. pkcs7->privateKey = key;
  18655. pkcs7->privateKeySz = (word32)sizeof(key);
  18656. pkcs7->encryptOID = RSAk;
  18657. pkcs7->hashOID = SHAh;
  18658. pkcs7->rng = &rng;
  18659. /* calculate hash for content */
  18660. ret = wc_HashInit(&hash, hashType);
  18661. if (ret == 0) {
  18662. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  18663. if (ret == 0) {
  18664. ret = wc_HashFinal(&hash, hashType, hashBuf);
  18665. }
  18666. wc_HashFree(&hash, hashType);
  18667. }
  18668. AssertIntEQ(ret, 0);
  18669. /* Perform PKCS7 sign using hash directly */
  18670. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  18671. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  18672. AssertIntGT(outputHeadSz, 0);
  18673. AssertIntGT(outputFootSz, 0);
  18674. wc_PKCS7_Free(pkcs7);
  18675. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  18676. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  18677. /* required parameter even on verify when using _ex */
  18678. pkcs7->contentSz = (word32)sizeof(data);
  18679. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  18680. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  18681. wc_PKCS7_Free(pkcs7);
  18682. /* assembly complete PKCS7 sign and use normal verify */
  18683. {
  18684. byte* output = (byte*)XMALLOC(outputHeadSz + sizeof(data) + outputFootSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18685. word32 outputSz = 0;
  18686. AssertNotNull(output);
  18687. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  18688. outputSz += outputHeadSz;
  18689. XMEMCPY(&output[outputSz], data, sizeof(data));
  18690. outputSz += sizeof(data);
  18691. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  18692. outputSz += outputFootSz;
  18693. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  18694. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  18695. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  18696. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18697. }
  18698. /* Pass in bad args. */
  18699. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  18700. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  18701. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  18702. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  18703. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  18704. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  18705. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  18706. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  18707. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  18708. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  18709. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  18710. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  18711. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  18712. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  18713. pkcs7->hashOID = 0; /* bad hashOID */
  18714. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  18715. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  18716. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  18717. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  18718. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  18719. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  18720. #ifndef NO_PKCS7_STREAM
  18721. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  18722. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  18723. #else
  18724. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  18725. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  18726. #endif
  18727. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  18728. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  18729. #ifndef NO_PKCS7_STREAM
  18730. /* can pass in 0 buffer length with streaming API */
  18731. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  18732. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  18733. #else
  18734. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  18735. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  18736. #endif
  18737. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  18738. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  18739. #ifndef NO_PKCS7_STREAM
  18740. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  18741. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  18742. #else
  18743. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  18744. outputHead, outputHeadSz, outputFoot, 0), ASN_PARSE_E);
  18745. #endif
  18746. printf(resultFmt, passed);
  18747. wc_PKCS7_Free(pkcs7);
  18748. wc_FreeRng(&rng);
  18749. #endif
  18750. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  18751. #if defined(HAVE_PKCS7)
  18752. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  18753. byte* data, word32 dataSz,
  18754. int withAttribs, int detachedSig)
  18755. {
  18756. PKCS7* pkcs7;
  18757. WC_RNG rng;
  18758. static byte messageTypeOid[] =
  18759. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  18760. 0x09, 0x02 };
  18761. static byte messageType[] = { 0x13, 2, '1', '9' };
  18762. PKCS7Attrib attribs[] =
  18763. {
  18764. { messageTypeOid, sizeof(messageTypeOid), messageType,
  18765. sizeof(messageType) }
  18766. };
  18767. #ifndef NO_RSA
  18768. #if defined(USE_CERT_BUFFERS_2048)
  18769. byte key[sizeof_client_key_der_2048];
  18770. byte cert[sizeof_client_cert_der_2048];
  18771. word32 keySz = (word32)sizeof(key);
  18772. word32 certSz = (word32)sizeof(cert);
  18773. XMEMSET(key, 0, keySz);
  18774. XMEMSET(cert, 0, certSz);
  18775. XMEMCPY(key, client_key_der_2048, keySz);
  18776. XMEMCPY(cert, client_cert_der_2048, certSz);
  18777. #elif defined(USE_CERT_BUFFERS_1024)
  18778. byte key[sizeof_client_key_der_1024];
  18779. byte cert[sizeof_client_cert_der_1024];
  18780. word32 keySz = (word32)sizeof(key);
  18781. word32 certSz = (word32)sizeof(cert);
  18782. XMEMSET(key, 0, keySz);
  18783. XMEMSET(cert, 0, certSz);
  18784. XMEMCPY(key, client_key_der_1024, keySz);
  18785. XMEMCPY(cert, client_cert_der_1024, certSz);
  18786. #else
  18787. unsigned char cert[ONEK_BUF];
  18788. unsigned char key[ONEK_BUF];
  18789. FILE* fp;
  18790. int certSz;
  18791. int keySz;
  18792. fp = fopen("./certs/1024/client-cert.der", "rb");
  18793. AssertNotNull(fp);
  18794. certSz = fread(cert, 1, sizeof_client_cert_der_1024, fp);
  18795. fclose(fp);
  18796. fp = fopen("./certs/1024/client-key.der", "rb");
  18797. AssertNotNull(fp);
  18798. keySz = fread(key, 1, sizeof_client_key_der_1024, fp);
  18799. fclose(fp);
  18800. #endif
  18801. #elif defined(HAVE_ECC)
  18802. #if defined(USE_CERT_BUFFERS_256)
  18803. unsigned char cert[sizeof_cliecc_cert_der_256];
  18804. unsigned char key[sizeof_ecc_clikey_der_256];
  18805. int certSz = (int)sizeof(cert);
  18806. int keySz = (int)sizeof(key);
  18807. XMEMSET(cert, 0, certSz);
  18808. XMEMSET(key, 0, keySz);
  18809. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  18810. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  18811. #else
  18812. unsigned char cert[ONEK_BUF];
  18813. unsigned char key[ONEK_BUF];
  18814. FILE* fp;
  18815. int certSz, keySz;
  18816. fp = fopen("./certs/client-ecc-cert.der", "rb");
  18817. AssertNotNull(fp);
  18818. certSz = fread(cert, 1, sizeof_cliecc_cert_der_256, fp);
  18819. fclose(fp);
  18820. fp = fopen("./certs/client-ecc-key.der", "rb");
  18821. AssertNotNull(fp);
  18822. keySz = fread(key, 1, sizeof_ecc_clikey_der_256, fp);
  18823. fclose(fp);
  18824. #endif
  18825. #endif
  18826. XMEMSET(output, 0, outputSz);
  18827. AssertIntEQ(wc_InitRng(&rng), 0);
  18828. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  18829. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  18830. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  18831. printf(testingFmt, "wc_PKCS7_VerifySignedData()");
  18832. pkcs7->content = data;
  18833. pkcs7->contentSz = dataSz;
  18834. pkcs7->privateKey = key;
  18835. pkcs7->privateKeySz = (word32)sizeof(key);
  18836. pkcs7->encryptOID = RSAk;
  18837. pkcs7->hashOID = SHAh;
  18838. pkcs7->rng = &rng;
  18839. if (withAttribs) {
  18840. /* include a signed attribute */
  18841. pkcs7->signedAttribs = attribs;
  18842. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  18843. }
  18844. if (detachedSig) {
  18845. AssertIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  18846. }
  18847. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  18848. wc_PKCS7_Free(pkcs7);
  18849. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  18850. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  18851. if (detachedSig) {
  18852. pkcs7->content = data;
  18853. pkcs7->contentSz = dataSz;
  18854. }
  18855. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  18856. wc_PKCS7_Free(pkcs7);
  18857. wc_FreeRng(&rng);
  18858. return outputSz;
  18859. }
  18860. #endif
  18861. /*
  18862. * Testing wc_PKCS_VerifySignedData()
  18863. */
  18864. static void test_wc_PKCS7_VerifySignedData(void)
  18865. {
  18866. #if defined(HAVE_PKCS7)
  18867. PKCS7* pkcs7;
  18868. byte output[FOURK_BUF];
  18869. word32 outputSz = sizeof(output);
  18870. byte data[] = "Test data to encode.";
  18871. byte badOut[0];
  18872. word32 badOutSz = (word32)sizeof(badOut);
  18873. byte badContent[] = "This is different content than was signed";
  18874. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  18875. (word32)sizeof(data),
  18876. 0, 0)), 0);
  18877. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  18878. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  18879. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  18880. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  18881. /* Test bad args. */
  18882. AssertIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz), BAD_FUNC_ARG);
  18883. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  18884. #ifndef NO_PKCS7_STREAM
  18885. /* can pass in 0 buffer length with streaming API */
  18886. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  18887. badOutSz), WC_PKCS7_WANT_READ_E);
  18888. #else
  18889. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  18890. badOutSz), BAD_FUNC_ARG);
  18891. #endif
  18892. wc_PKCS7_Free(pkcs7);
  18893. /* Invalid content should error, use detached signature so we can
  18894. * easily change content */
  18895. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  18896. (word32)sizeof(data),
  18897. 1, 1)), 0);
  18898. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  18899. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  18900. pkcs7->content = badContent;
  18901. pkcs7->contentSz = sizeof(badContent);
  18902. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), SIG_VERIFY_E);
  18903. wc_PKCS7_Free(pkcs7);
  18904. /* Test success case with detached signature and valid content */
  18905. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  18906. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  18907. pkcs7->content = data;
  18908. pkcs7->contentSz = sizeof(data);
  18909. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  18910. wc_PKCS7_Free(pkcs7);
  18911. printf(resultFmt, passed);
  18912. #endif
  18913. } /* END test_wc_PKCS7_VerifySignedData() */
  18914. #if defined(HAVE_PKCS7) && !defined(NO_AES) && !defined(NO_AES_256)
  18915. static const byte defKey[] = {
  18916. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  18917. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  18918. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  18919. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  18920. };
  18921. static byte aesHandle[32]; /* simulated hardware key handle */
  18922. /* return 0 on success */
  18923. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  18924. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  18925. byte* in, int inSz, byte* out, void* usrCtx)
  18926. {
  18927. int ret;
  18928. Aes aes;
  18929. if (usrCtx == NULL) {
  18930. /* no simulated handle passed in */
  18931. return -1;
  18932. }
  18933. switch (encryptOID) {
  18934. case AES256CBCb:
  18935. if (ivSz != AES_BLOCK_SIZE)
  18936. return BAD_FUNC_ARG;
  18937. break;
  18938. default:
  18939. WOLFSSL_MSG("Unsupported content cipher type for test");
  18940. return ALGO_ID_E;
  18941. };
  18942. /* simulate using handle to get key */
  18943. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  18944. if (ret == 0) {
  18945. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  18946. if (ret == 0)
  18947. ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
  18948. wc_AesFree(&aes);
  18949. }
  18950. (void)aad;
  18951. (void)aadSz;
  18952. (void)authTag;
  18953. (void)authTagSz;
  18954. (void)pkcs7;
  18955. return ret;
  18956. }
  18957. /* returns key size on success */
  18958. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  18959. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  18960. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  18961. {
  18962. int ret = -1;
  18963. if (out == NULL)
  18964. return BAD_FUNC_ARG;
  18965. if (keyId[0] != 0x00) {
  18966. return -1;
  18967. }
  18968. if (type != (int)PKCS7_KEKRI) {
  18969. return -1;
  18970. }
  18971. switch (keyWrapAlgo) {
  18972. case AES256_WRAP:
  18973. /* simulate setting a handle for later decryption but use key
  18974. * as handle in the test case here */
  18975. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  18976. aesHandle, sizeof(aesHandle), NULL);
  18977. if (ret < 0)
  18978. return ret;
  18979. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  18980. if (ret < 0)
  18981. return ret;
  18982. /* return key size on success */
  18983. return sizeof(defKey);
  18984. default:
  18985. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  18986. return BAD_KEYWRAP_ALG_E;
  18987. };
  18988. (void)cekSz;
  18989. (void)cek;
  18990. (void)outSz;
  18991. (void)keyIdSz;
  18992. (void)direction;
  18993. (void)orginKey; /* used with KAKRI */
  18994. (void)orginKeySz;
  18995. return ret;
  18996. }
  18997. #endif /* HAVE_PKCS7 && !NO_AES && !NO_AES_256 */
  18998. /*
  18999. * Testing wc_PKCS7_EncodeEnvelopedData()
  19000. */
  19001. static void test_wc_PKCS7_EncodeDecodeEnvelopedData (void)
  19002. {
  19003. #if defined(HAVE_PKCS7)
  19004. PKCS7* pkcs7;
  19005. WC_RNG rng;
  19006. word32 tempWrd32 = 0;
  19007. byte* tmpBytePtr = NULL;
  19008. const char input[] = "Test data to encode.";
  19009. int i;
  19010. int testSz = 0;
  19011. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  19012. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  19013. byte* rsaCert = NULL;
  19014. byte* rsaPrivKey = NULL;
  19015. word32 rsaCertSz;
  19016. word32 rsaPrivKeySz;
  19017. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  19018. !defined(USE_CERT_BUFFERS_2048) )
  19019. static const char* rsaClientCert = "./certs/client-cert.der";
  19020. static const char* rsaClientKey = "./certs/client-key.der";
  19021. rsaCertSz = (word32)sizeof(rsaClientCert);
  19022. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  19023. #endif
  19024. #endif
  19025. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  19026. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  19027. byte* eccCert = NULL;
  19028. byte* eccPrivKey = NULL;
  19029. word32 eccCertSz;
  19030. word32 eccPrivKeySz;
  19031. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  19032. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  19033. static const char* eccClientKey = "./certs/ecc-client-key.der";
  19034. #endif
  19035. #endif
  19036. /* Generic buffer size. */
  19037. byte output[ONEK_BUF];
  19038. byte decoded[sizeof(input)/sizeof(char)];
  19039. int decodedSz = 0;
  19040. #ifndef NO_FILESYSTEM
  19041. XFILE certFile;
  19042. XFILE keyFile;
  19043. #endif
  19044. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  19045. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  19046. /* RSA certs and keys. */
  19047. #if defined(USE_CERT_BUFFERS_1024)
  19048. /* Allocate buffer space. */
  19049. AssertNotNull(rsaCert =
  19050. (byte*)XMALLOC(ONEK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  19051. /* Init buffer. */
  19052. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  19053. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  19054. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(ONEK_BUF, HEAP_HINT,
  19055. DYNAMIC_TYPE_TMP_BUFFER));
  19056. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  19057. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  19058. #elif defined(USE_CERT_BUFFERS_2048)
  19059. /* Allocate buffer */
  19060. AssertNotNull(rsaCert =
  19061. (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  19062. /* Init buffer. */
  19063. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  19064. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  19065. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  19066. DYNAMIC_TYPE_TMP_BUFFER));
  19067. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  19068. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  19069. #else
  19070. /* File system. */
  19071. certFile = XFOPEN(rsaClientCert, "rb");
  19072. AssertTrue(certFile != XBADFILE);
  19073. rsaCertSz = (word32)FOURK_BUF;
  19074. AssertNotNull(rsaCert =
  19075. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  19076. rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz, certFile);
  19077. XFCLOSE(certFile);
  19078. keyFile = XFOPEN(rsaClientKey, "rb");
  19079. AssertTrue(keyFile != XBADFILE);
  19080. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  19081. DYNAMIC_TYPE_TMP_BUFFER));
  19082. rsaPrivKeySz = (word32)FOURK_BUF;
  19083. rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz, keyFile);
  19084. XFCLOSE(keyFile);
  19085. #endif /* USE_CERT_BUFFERS */
  19086. #endif /* NO_RSA */
  19087. /* ECC */
  19088. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  19089. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  19090. #ifdef USE_CERT_BUFFERS_256
  19091. AssertNotNull(eccCert =
  19092. (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  19093. /* Init buffer. */
  19094. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  19095. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  19096. AssertNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  19097. DYNAMIC_TYPE_TMP_BUFFER));
  19098. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  19099. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  19100. #else /* File system. */
  19101. certFile = XFOPEN(eccClientCert, "rb");
  19102. AssertTrue(certFile != XBADFILE);
  19103. eccCertSz = (word32)FOURK_BUF;
  19104. AssertNotNull(eccCert =
  19105. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  19106. eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz, certFile);
  19107. XFCLOSE(certFile);
  19108. keyFile = XFOPEN(eccClientKey, "rb");
  19109. AssertTrue(keyFile != XBADFILE);
  19110. eccPrivKeySz = (word32)FOURK_BUF;
  19111. AssertNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  19112. DYNAMIC_TYPE_TMP_BUFFER));
  19113. eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz, keyFile);
  19114. XFCLOSE(keyFile);
  19115. #endif /* USE_CERT_BUFFERS_256 */
  19116. #endif /* END HAVE_ECC */
  19117. /* Silence. */
  19118. (void)keyFile;
  19119. (void)certFile;
  19120. const pkcs7EnvelopedVector testVectors[] = {
  19121. /* DATA is a global variable defined in the makefile. */
  19122. #if !defined(NO_RSA)
  19123. #ifndef NO_DES3
  19124. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  19125. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  19126. #endif /* NO_DES3 */
  19127. #ifndef NO_AES
  19128. #ifndef NO_AES_128
  19129. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  19130. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  19131. #endif
  19132. #ifndef NO_AES_192
  19133. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  19134. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  19135. #endif
  19136. #ifndef NO_AES_256
  19137. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  19138. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  19139. #endif
  19140. #endif /* NO_AES */
  19141. #endif /* NO_RSA */
  19142. #if defined(HAVE_ECC)
  19143. #ifndef NO_AES
  19144. #if !defined(NO_SHA) && !defined(NO_AES_128)
  19145. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  19146. AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme, eccCert,
  19147. eccCertSz, eccPrivKey, eccPrivKeySz},
  19148. #endif
  19149. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  19150. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  19151. AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme, eccCert,
  19152. eccCertSz, eccPrivKey, eccPrivKeySz},
  19153. #endif
  19154. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  19155. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  19156. AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme, eccCert,
  19157. eccCertSz, eccPrivKey, eccPrivKeySz},
  19158. #endif
  19159. #endif /* NO_AES */
  19160. #endif /* END HAVE_ECC */
  19161. }; /* END pkcs7EnvelopedVector */
  19162. #ifdef ECC_TIMING_RESISTANT
  19163. AssertIntEQ(wc_InitRng(&rng), 0);
  19164. #endif
  19165. printf(testingFmt, "wc_PKCS7_EncodeEnvelopedData()");
  19166. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  19167. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, devId), 0);
  19168. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  19169. for (i = 0; i < testSz; i++) {
  19170. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  19171. (word32)(testVectors + i)->certSz), 0);
  19172. #ifdef ECC_TIMING_RESISTANT
  19173. pkcs7->rng = &rng;
  19174. #endif
  19175. pkcs7->content = (byte*)(testVectors + i)->content;
  19176. pkcs7->contentSz = (testVectors + i)->contentSz;
  19177. pkcs7->contentOID = (testVectors + i)->contentOID;
  19178. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  19179. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  19180. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  19181. pkcs7->privateKey = (testVectors + i)->privateKey;
  19182. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  19183. AssertIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  19184. (word32)sizeof(output)), 0);
  19185. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  19186. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  19187. AssertIntGE(decodedSz, 0);
  19188. /* Verify the size of each buffer. */
  19189. AssertIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  19190. /* Don't free the last time through the loop. */
  19191. if (i < testSz - 1 ){
  19192. wc_PKCS7_Free(pkcs7);
  19193. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  19194. }
  19195. } /* END test loop. */
  19196. /* Test bad args. */
  19197. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  19198. (word32)sizeof(output)), BAD_FUNC_ARG);
  19199. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  19200. (word32)sizeof(output)), BAD_FUNC_ARG);
  19201. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  19202. printf(resultFmt, passed);
  19203. /* Decode. */
  19204. printf(testingFmt, "wc_PKCS7_DecodeEnvelopedData()");
  19205. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  19206. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  19207. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  19208. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  19209. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  19210. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  19211. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  19212. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  19213. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  19214. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  19215. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  19216. #if defined(HAVE_ECC) && !defined(NO_AES)
  19217. /* only a failure for KARI test cases */
  19218. tempWrd32 = pkcs7->singleCertSz;
  19219. pkcs7->singleCertSz = 0;
  19220. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  19221. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  19222. pkcs7->singleCertSz = tempWrd32;
  19223. tmpBytePtr = pkcs7->singleCert;
  19224. pkcs7->singleCert = NULL;
  19225. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  19226. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  19227. pkcs7->singleCert = tmpBytePtr;
  19228. #endif
  19229. tempWrd32 = pkcs7->privateKeySz;
  19230. pkcs7->privateKeySz = 0;
  19231. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  19232. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  19233. pkcs7->privateKeySz = tempWrd32;
  19234. tmpBytePtr = pkcs7->privateKey;
  19235. pkcs7->privateKey = NULL;
  19236. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  19237. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  19238. pkcs7->privateKey = tmpBytePtr;
  19239. wc_PKCS7_Free(pkcs7);
  19240. #if !defined(NO_AES) && !defined(NO_AES_256)
  19241. /* test of decrypt callback with KEKRI enveloped data */
  19242. {
  19243. int envelopedSz;
  19244. const byte keyId[] = { 0x00 };
  19245. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  19246. pkcs7->content = (byte*)input;
  19247. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  19248. pkcs7->contentOID = DATA;
  19249. pkcs7->encryptOID = AES256CBCb;
  19250. AssertIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  19251. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  19252. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  19253. AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  19254. AssertIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  19255. (word32)sizeof(output))), 0);
  19256. wc_PKCS7_Free(pkcs7);
  19257. /* decode envelopedData */
  19258. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  19259. AssertIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  19260. AssertIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  19261. AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  19262. envelopedSz, decoded, sizeof(decoded))), 0);
  19263. wc_PKCS7_Free(pkcs7);
  19264. }
  19265. #endif /* !NO_AES && !NO_AES_256 */
  19266. printf(resultFmt, passed);
  19267. #ifndef NO_RSA
  19268. if (rsaCert) {
  19269. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19270. }
  19271. if (rsaPrivKey) {
  19272. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19273. }
  19274. #endif /*NO_RSA */
  19275. #ifdef HAVE_ECC
  19276. if (eccCert) {
  19277. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19278. }
  19279. if (eccPrivKey) {
  19280. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19281. }
  19282. #endif /* HAVE_ECC */
  19283. #ifdef ECC_TIMING_RESISTANT
  19284. wc_FreeRng(&rng);
  19285. #endif
  19286. #endif /* HAVE_PKCS7 */
  19287. } /* END test_wc_PKCS7_EncodeEnvelopedData() */
  19288. /*
  19289. * Testing wc_PKCS7_EncodeEncryptedData()
  19290. */
  19291. static void test_wc_PKCS7_EncodeEncryptedData (void)
  19292. {
  19293. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  19294. PKCS7* pkcs7;
  19295. byte* tmpBytePtr = NULL;
  19296. byte encrypted[TWOK_BUF];
  19297. byte decoded[TWOK_BUF];
  19298. word32 tmpWrd32 = 0;
  19299. int tmpInt = 0;
  19300. int decodedSz;
  19301. int encryptedSz;
  19302. int testSz;
  19303. int i;
  19304. const byte data[] = { /* Hello World */
  19305. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  19306. 0x72,0x6c,0x64
  19307. };
  19308. #ifndef NO_DES3
  19309. byte desKey[] = {
  19310. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  19311. };
  19312. byte des3Key[] = {
  19313. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  19314. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  19315. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  19316. };
  19317. #endif
  19318. #ifndef NO_AES
  19319. #ifndef NO_AES_128
  19320. byte aes128Key[] = {
  19321. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  19322. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  19323. };
  19324. #endif
  19325. #ifndef NO_AES_192
  19326. byte aes192Key[] = {
  19327. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  19328. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  19329. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  19330. };
  19331. #endif
  19332. #ifndef NO_AES_256
  19333. byte aes256Key[] = {
  19334. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  19335. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  19336. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  19337. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  19338. };
  19339. #endif
  19340. #endif
  19341. const pkcs7EncryptedVector testVectors[] =
  19342. {
  19343. #ifndef NO_DES3
  19344. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  19345. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  19346. #endif /* NO_DES3 */
  19347. #ifndef NO_AES
  19348. #ifndef NO_AES_128
  19349. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  19350. sizeof(aes128Key)},
  19351. #endif
  19352. #ifndef NO_AES_192
  19353. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  19354. sizeof(aes192Key)},
  19355. #endif
  19356. #ifndef NO_AES_256
  19357. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  19358. sizeof(aes256Key)},
  19359. #endif
  19360. #endif /* NO_AES */
  19361. };
  19362. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  19363. for (i = 0; i < testSz; i++) {
  19364. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  19365. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, devId), 0);
  19366. pkcs7->content = (byte*)testVectors[i].content;
  19367. pkcs7->contentSz = testVectors[i].contentSz;
  19368. pkcs7->contentOID = testVectors[i].contentOID;
  19369. pkcs7->encryptOID = testVectors[i].encryptOID;
  19370. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  19371. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  19372. pkcs7->heap = HEAP_HINT;
  19373. /* encode encryptedData */
  19374. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  19375. sizeof(encrypted));
  19376. AssertIntGT(encryptedSz, 0);
  19377. /* Decode encryptedData */
  19378. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  19379. decoded, sizeof(decoded));
  19380. AssertIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  19381. /* Keep values for last itr. */
  19382. if (i < testSz - 1) {
  19383. wc_PKCS7_Free(pkcs7);
  19384. }
  19385. }
  19386. printf(testingFmt, "wc_PKCS7_EncodeEncryptedData()");
  19387. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  19388. sizeof(encrypted)),BAD_FUNC_ARG);
  19389. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  19390. sizeof(encrypted)), BAD_FUNC_ARG);
  19391. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  19392. 0), BAD_FUNC_ARG);
  19393. /* Testing the struct. */
  19394. tmpBytePtr = pkcs7->content;
  19395. pkcs7->content = NULL;
  19396. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  19397. sizeof(encrypted)), BAD_FUNC_ARG);
  19398. pkcs7->content = tmpBytePtr;
  19399. tmpWrd32 = pkcs7->contentSz;
  19400. pkcs7->contentSz = 0;
  19401. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  19402. sizeof(encrypted)), BAD_FUNC_ARG);
  19403. pkcs7->contentSz = tmpWrd32;
  19404. tmpInt = pkcs7->encryptOID;
  19405. pkcs7->encryptOID = 0;
  19406. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  19407. sizeof(encrypted)), BAD_FUNC_ARG);
  19408. pkcs7->encryptOID = tmpInt;
  19409. tmpBytePtr = pkcs7->encryptionKey;
  19410. pkcs7->encryptionKey = NULL;
  19411. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  19412. sizeof(encrypted)), BAD_FUNC_ARG);
  19413. pkcs7->encryptionKey = tmpBytePtr;
  19414. tmpWrd32 = pkcs7->encryptionKeySz;
  19415. pkcs7->encryptionKeySz = 0;
  19416. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  19417. sizeof(encrypted)), BAD_FUNC_ARG);
  19418. pkcs7->encryptionKeySz = tmpWrd32;
  19419. printf(resultFmt, passed);
  19420. printf(testingFmt, "wc_PKCS7_EncodeEncryptedData()");
  19421. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  19422. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  19423. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
  19424. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  19425. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  19426. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  19427. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  19428. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  19429. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  19430. decoded, 0), BAD_FUNC_ARG);
  19431. /* Test struct fields */
  19432. tmpBytePtr = pkcs7->encryptionKey;
  19433. pkcs7->encryptionKey = NULL;
  19434. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  19435. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  19436. pkcs7->encryptionKey = tmpBytePtr;
  19437. pkcs7->encryptionKeySz = 0;
  19438. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  19439. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  19440. printf(resultFmt, passed);
  19441. wc_PKCS7_Free(pkcs7);
  19442. #endif
  19443. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  19444. /*
  19445. * Testing wc_PKCS7_Degenerate()
  19446. */
  19447. static void test_wc_PKCS7_Degenerate(void)
  19448. {
  19449. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  19450. PKCS7* pkcs7;
  19451. char fName[] = "./certs/test-degenerate.p7b";
  19452. XFILE f;
  19453. byte der[4096];
  19454. word32 derSz;
  19455. int ret;
  19456. printf(testingFmt, "wc_PKCS7_Degenerate()");
  19457. AssertNotNull(f = XFOPEN(fName, "rb"));
  19458. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  19459. derSz = (word32)ret;
  19460. XFCLOSE(f);
  19461. /* test degenerate success */
  19462. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  19463. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  19464. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  19465. #ifndef NO_RSA
  19466. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  19467. #else
  19468. AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  19469. #endif
  19470. wc_PKCS7_Free(pkcs7);
  19471. /* test with turning off degenerate cases */
  19472. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  19473. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  19474. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  19475. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  19476. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), PKCS7_NO_SIGNER_E);
  19477. wc_PKCS7_Free(pkcs7);
  19478. printf(resultFmt, passed);
  19479. #endif
  19480. } /* END test_wc_PKCS7_Degenerate() */
  19481. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  19482. defined(ASN_BER_TO_DER) && !defined(NO_DES3)
  19483. static byte berContent[] = {
  19484. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  19485. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  19486. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  19487. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  19488. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  19489. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  19490. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  19491. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  19492. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  19493. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  19494. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  19495. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  19496. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  19497. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  19498. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  19499. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  19500. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  19501. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  19502. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  19503. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  19504. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  19505. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  19506. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  19507. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  19508. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  19509. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  19510. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  19511. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  19512. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  19513. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  19514. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  19515. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  19516. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  19517. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  19518. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  19519. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  19520. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  19521. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  19522. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  19523. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  19524. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  19525. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  19526. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  19527. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  19528. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  19529. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  19530. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  19531. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  19532. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  19533. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  19534. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  19535. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  19536. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  19537. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  19538. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  19539. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  19540. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  19541. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  19542. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  19543. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  19544. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  19545. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  19546. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  19547. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  19548. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  19549. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  19550. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  19551. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  19552. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  19553. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  19554. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  19555. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  19556. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  19557. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  19558. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  19559. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  19560. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  19561. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  19562. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  19563. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  19564. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  19565. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  19566. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  19567. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  19568. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  19569. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  19570. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  19571. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  19572. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  19573. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  19574. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  19575. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  19576. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  19577. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  19578. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  19579. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  19580. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  19581. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  19582. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  19583. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  19584. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  19585. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  19586. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  19587. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  19588. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  19589. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  19590. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  19591. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  19592. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  19593. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  19594. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  19595. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  19596. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  19597. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  19598. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  19599. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  19600. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  19601. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  19602. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  19603. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  19604. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  19605. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  19606. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  19607. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  19608. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  19609. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  19610. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  19611. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  19612. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  19613. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  19614. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  19615. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  19616. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  19617. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  19618. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  19619. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  19620. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  19621. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  19622. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  19623. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  19624. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  19625. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  19626. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  19627. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  19628. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  19629. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  19630. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  19631. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  19632. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  19633. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  19634. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  19635. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  19636. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  19637. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  19638. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  19639. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  19640. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  19641. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  19642. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  19643. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  19644. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  19645. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  19646. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  19647. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  19648. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  19649. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  19650. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  19651. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  19652. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  19653. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  19654. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  19655. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  19656. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  19657. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  19658. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  19659. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  19660. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  19661. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  19662. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  19663. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  19664. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  19665. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  19666. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  19667. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  19668. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  19669. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  19670. 0x00, 0x00, 0x00, 0x00, 0x00
  19671. };
  19672. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER && !NO_DES3 */
  19673. /*
  19674. * Testing wc_PKCS7_BER()
  19675. */
  19676. static void test_wc_PKCS7_BER(void)
  19677. {
  19678. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  19679. defined(ASN_BER_TO_DER)
  19680. PKCS7* pkcs7;
  19681. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  19682. XFILE f;
  19683. byte der[4096];
  19684. #ifndef NO_DES3
  19685. byte decoded[2048];
  19686. #endif
  19687. word32 derSz;
  19688. int ret;
  19689. printf(testingFmt, "wc_PKCS7_BER()");
  19690. AssertNotNull(f = XFOPEN(fName, "rb"));
  19691. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  19692. derSz = (word32)ret;
  19693. XFCLOSE(f);
  19694. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  19695. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  19696. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  19697. #ifndef NO_RSA
  19698. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  19699. #else
  19700. AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  19701. #endif
  19702. wc_PKCS7_Free(pkcs7);
  19703. #ifndef NO_DES3
  19704. /* decode BER content */
  19705. AssertNotNull(f = XFOPEN("./certs/1024/client-cert.der", "rb"));
  19706. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  19707. derSz = (word32)ret;
  19708. XFCLOSE(f);
  19709. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  19710. #ifndef NO_RSA
  19711. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  19712. #else
  19713. AssertIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  19714. #endif
  19715. AssertNotNull(f = XFOPEN("./certs/1024/client-key.der", "rb"));
  19716. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  19717. derSz = (word32)ret;
  19718. XFCLOSE(f);
  19719. pkcs7->privateKey = der;
  19720. pkcs7->privateKeySz = derSz;
  19721. #ifndef NO_RSA
  19722. AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  19723. sizeof(berContent), decoded, sizeof(decoded)), 0);
  19724. #else
  19725. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  19726. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  19727. #endif
  19728. wc_PKCS7_Free(pkcs7);
  19729. #endif /* !NO_DES3 */
  19730. printf(resultFmt, passed);
  19731. #endif
  19732. } /* END test_wc_PKCS7_BER() */
  19733. static void test_PKCS7_signed_enveloped(void)
  19734. {
  19735. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  19736. && !defined(NO_AES)
  19737. XFILE f;
  19738. PKCS7* pkcs7;
  19739. PKCS7* inner;
  19740. void* pt;
  19741. WC_RNG rng;
  19742. unsigned char key[FOURK_BUF/2];
  19743. unsigned char cert[FOURK_BUF/2];
  19744. unsigned char env[FOURK_BUF];
  19745. int envSz = FOURK_BUF;
  19746. int keySz;
  19747. int certSz;
  19748. unsigned char sig[FOURK_BUF * 2];
  19749. int sigSz = FOURK_BUF * 2;
  19750. unsigned char decoded[FOURK_BUF];
  19751. int decodedSz = FOURK_BUF;
  19752. printf(testingFmt, "PKCS7_signed_enveloped");
  19753. /* load cert */
  19754. AssertNotNull(f = XFOPEN(cliCertDerFile, "rb"));
  19755. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  19756. XFCLOSE(f);
  19757. /* load key */
  19758. AssertNotNull(f = XFOPEN(cliKeyFile, "rb"));
  19759. AssertIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  19760. XFCLOSE(f);
  19761. keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL);
  19762. /* sign cert for envelope */
  19763. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  19764. AssertIntEQ(wc_InitRng(&rng), 0);
  19765. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  19766. pkcs7->content = cert;
  19767. pkcs7->contentSz = certSz;
  19768. pkcs7->contentOID = DATA;
  19769. pkcs7->privateKey = key;
  19770. pkcs7->privateKeySz = keySz;
  19771. pkcs7->encryptOID = RSAk;
  19772. pkcs7->hashOID = SHA256h;
  19773. pkcs7->rng = &rng;
  19774. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  19775. wc_PKCS7_Free(pkcs7);
  19776. wc_FreeRng(&rng);
  19777. /* create envelope */
  19778. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  19779. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  19780. pkcs7->content = sig;
  19781. pkcs7->contentSz = sigSz;
  19782. pkcs7->contentOID = DATA;
  19783. pkcs7->encryptOID = AES256CBCb;
  19784. pkcs7->privateKey = key;
  19785. pkcs7->privateKeySz = keySz;
  19786. AssertIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
  19787. wc_PKCS7_Free(pkcs7);
  19788. /* create bad signed enveloped data */
  19789. sigSz = FOURK_BUF * 2;
  19790. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  19791. AssertIntEQ(wc_InitRng(&rng), 0);
  19792. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  19793. pkcs7->content = env;
  19794. pkcs7->contentSz = envSz;
  19795. pkcs7->contentOID = DATA;
  19796. pkcs7->privateKey = key;
  19797. pkcs7->privateKeySz = keySz;
  19798. pkcs7->encryptOID = RSAk;
  19799. pkcs7->hashOID = SHA256h;
  19800. pkcs7->rng = &rng;
  19801. /* Set no certs in bundle for this test. Hang on to the pointer though to
  19802. * free it later. */
  19803. pt = (void*)pkcs7->certList;
  19804. pkcs7->certList = NULL; /* no certs in bundle */
  19805. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  19806. pkcs7->certList = (Pkcs7Cert*)pt; /* restore pointer for PKCS7 free call */
  19807. wc_PKCS7_Free(pkcs7);
  19808. /* check verify fails */
  19809. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  19810. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  19811. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
  19812. PKCS7_SIGNEEDS_CHECK);
  19813. /* try verifying the signature manually */
  19814. {
  19815. RsaKey rKey;
  19816. word32 idx = 0;
  19817. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  19818. WC_MAX_DIGEST_SIZE];
  19819. int digestSz;
  19820. AssertIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  19821. AssertIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
  19822. digestSz = wc_RsaSSL_Verify(pkcs7->signature, pkcs7->signatureSz,
  19823. digest, sizeof(digest), &rKey);
  19824. AssertIntGT(digestSz, 0);
  19825. AssertIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  19826. AssertIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  19827. AssertIntEQ(wc_FreeRsaKey(&rKey), 0);
  19828. /* verify was success */
  19829. }
  19830. wc_PKCS7_Free(pkcs7);
  19831. /* initializing the PKCS7 struct with the signing certificate should pass */
  19832. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  19833. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  19834. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  19835. wc_PKCS7_Free(pkcs7);
  19836. /* create valid degenerate bundle */
  19837. sigSz = FOURK_BUF * 2;
  19838. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  19839. pkcs7->content = env;
  19840. pkcs7->contentSz = envSz;
  19841. pkcs7->contentOID = DATA;
  19842. pkcs7->privateKey = key;
  19843. pkcs7->privateKeySz = keySz;
  19844. pkcs7->encryptOID = RSAk;
  19845. pkcs7->hashOID = SHA256h;
  19846. pkcs7->rng = &rng;
  19847. AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  19848. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  19849. wc_PKCS7_Free(pkcs7);
  19850. wc_FreeRng(&rng);
  19851. /* check verify */
  19852. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  19853. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, devId), 0);
  19854. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  19855. AssertNotNull(pkcs7->content);
  19856. /* check decode */
  19857. AssertNotNull(inner = wc_PKCS7_New(NULL, 0));
  19858. AssertIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
  19859. inner->privateKey = key;
  19860. inner->privateKeySz = keySz;
  19861. AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  19862. pkcs7->contentSz, decoded, decodedSz)), 0);
  19863. wc_PKCS7_Free(inner);
  19864. wc_PKCS7_Free(pkcs7);
  19865. /* check cert set */
  19866. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  19867. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  19868. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
  19869. AssertNotNull(pkcs7->singleCert);
  19870. AssertIntNE(pkcs7->singleCertSz, 0);
  19871. wc_PKCS7_Free(pkcs7);
  19872. printf(resultFmt, passed);
  19873. #endif
  19874. }
  19875. static void test_wc_i2d_PKCS12(void)
  19876. {
  19877. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  19878. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  19879. && !defined(NO_AES) && !defined(NO_DES3) && !defined(NO_SHA)
  19880. WC_PKCS12* pkcs12 = NULL;
  19881. unsigned char der[FOURK_BUF * 2];
  19882. unsigned char* pt;
  19883. int derSz;
  19884. unsigned char out[FOURK_BUF * 2];
  19885. int outSz = FOURK_BUF * 2;
  19886. const char p12_f[] = "./certs/test-servercert.p12";
  19887. XFILE f;
  19888. printf(testingFmt, "wc_i2d_PKCS12");
  19889. f = XFOPEN(p12_f, "rb");
  19890. AssertNotNull(f);
  19891. derSz = (int)XFREAD(der, 1, sizeof(der), f);
  19892. AssertIntGT(derSz, 0);
  19893. XFCLOSE(f);
  19894. AssertNotNull(pkcs12 = wc_PKCS12_new());
  19895. AssertIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
  19896. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  19897. AssertIntEQ(outSz, derSz);
  19898. outSz = derSz - 1;
  19899. pt = out;
  19900. AssertIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  19901. outSz = derSz;
  19902. AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  19903. AssertIntEQ((pt == out), 0);
  19904. pt = NULL;
  19905. AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  19906. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  19907. wc_PKCS12_free(pkcs12);
  19908. printf(resultFmt, passed);
  19909. #endif
  19910. }
  19911. /* Testing wc_SignatureGetSize() for signature type ECC */
  19912. static int test_wc_SignatureGetSize_ecc(void)
  19913. {
  19914. int ret = 0;
  19915. #ifndef NO_SIG_WRAPPER
  19916. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  19917. enum wc_SignatureType sig_type;
  19918. word32 key_len;
  19919. /* Initialize ECC Key */
  19920. ecc_key ecc;
  19921. const char* qx =
  19922. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  19923. const char* qy =
  19924. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  19925. const char* d =
  19926. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  19927. ret = wc_ecc_init(&ecc);
  19928. if (ret == 0) {
  19929. ret = wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1");
  19930. }
  19931. printf(testingFmt, "wc_SigntureGetSize_ecc()");
  19932. if (ret == 0) {
  19933. /* Input for signature type ECC */
  19934. sig_type = WC_SIGNATURE_TYPE_ECC;
  19935. key_len = sizeof(ecc_key);
  19936. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  19937. /* Test bad args */
  19938. if (ret > 0) {
  19939. sig_type = (enum wc_SignatureType) 100;
  19940. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  19941. if (ret == BAD_FUNC_ARG) {
  19942. sig_type = WC_SIGNATURE_TYPE_ECC;
  19943. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  19944. }
  19945. if (ret >= 0) {
  19946. key_len = (word32) 0;
  19947. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  19948. }
  19949. if (ret == BAD_FUNC_ARG) {
  19950. ret = SIG_TYPE_E;
  19951. }
  19952. }
  19953. } else {
  19954. ret = WOLFSSL_FATAL_ERROR;
  19955. }
  19956. wc_ecc_free(&ecc);
  19957. #else
  19958. ret = SIG_TYPE_E;
  19959. #endif
  19960. if (ret == SIG_TYPE_E) {
  19961. ret = 0;
  19962. }
  19963. else {
  19964. ret = WOLFSSL_FATAL_ERROR;
  19965. }
  19966. printf(resultFmt, ret == 0 ? passed : failed);
  19967. #endif /* NO_SIG_WRAPPER */
  19968. return ret;
  19969. }/* END test_wc_SignatureGetSize_ecc() */
  19970. /* Testing wc_SignatureGetSize() for signature type rsa */
  19971. static int test_wc_SignatureGetSize_rsa(void)
  19972. {
  19973. int ret = 0;
  19974. #ifndef NO_SIG_WRAPPER
  19975. #ifndef NO_RSA
  19976. enum wc_SignatureType sig_type;
  19977. word32 key_len;
  19978. word32 idx = 0;
  19979. /* Initialize RSA Key */
  19980. RsaKey rsa_key;
  19981. byte* tmp = NULL;
  19982. size_t bytes;
  19983. #ifdef USE_CERT_BUFFERS_1024
  19984. bytes = (size_t)sizeof_client_key_der_1024;
  19985. if (bytes < (size_t)sizeof_client_key_der_1024)
  19986. bytes = (size_t)sizeof_client_cert_der_1024;
  19987. #elif defined(USE_CERT_BUFFERS_2048)
  19988. bytes = (size_t)sizeof_client_key_der_2048;
  19989. if (bytes < (size_t)sizeof_client_cert_der_2048)
  19990. bytes = (size_t)sizeof_client_cert_der_2048;
  19991. #else
  19992. bytes = FOURK_BUF;
  19993. #endif
  19994. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19995. if (tmp != NULL) {
  19996. #ifdef USE_CERT_BUFFERS_1024
  19997. XMEMCPY(tmp, client_key_der_1024,
  19998. (size_t)sizeof_client_key_der_1024);
  19999. #elif defined(USE_CERT_BUFFERS_2048)
  20000. XMEMCPY(tmp, client_key_der_2048,
  20001. (size_t)sizeof_client_key_der_2048);
  20002. #elif !defined(NO_FILESYSTEM)
  20003. file = XFOPEN(clientKey, "rb");
  20004. if (file != XBADFILE) {
  20005. bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file);
  20006. XFCLOSE(file);
  20007. }
  20008. else {
  20009. ret = WOLFSSL_FATAL_ERROR;
  20010. }
  20011. #else
  20012. ret = WOLFSSL_FATAL_ERROR;
  20013. #endif
  20014. } else {
  20015. ret = WOLFSSL_FATAL_ERROR;
  20016. }
  20017. if (ret == 0) {
  20018. ret = wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, devId);
  20019. }
  20020. if (ret == 0) {
  20021. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes);
  20022. }
  20023. printf(testingFmt, "wc_SigntureGetSize_rsa()");
  20024. if (ret == 0) {
  20025. /* Input for signature type RSA */
  20026. sig_type = WC_SIGNATURE_TYPE_RSA;
  20027. key_len = sizeof(RsaKey);
  20028. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  20029. /* Test bad args */
  20030. if (ret > 0) {
  20031. sig_type = (enum wc_SignatureType) 100;
  20032. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  20033. if (ret == BAD_FUNC_ARG) {
  20034. sig_type = WC_SIGNATURE_TYPE_RSA;
  20035. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  20036. }
  20037. #ifndef HAVE_USER_RSA
  20038. if (ret == BAD_FUNC_ARG) {
  20039. #else
  20040. if (ret == 0) {
  20041. #endif
  20042. key_len = (word32)0;
  20043. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  20044. }
  20045. if (ret == BAD_FUNC_ARG) {
  20046. ret = SIG_TYPE_E;
  20047. }
  20048. }
  20049. } else {
  20050. ret = WOLFSSL_FATAL_ERROR;
  20051. }
  20052. wc_FreeRsaKey(&rsa_key);
  20053. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20054. #else
  20055. ret = SIG_TYPE_E;
  20056. #endif
  20057. if (ret == SIG_TYPE_E) {
  20058. ret = 0;
  20059. }else {
  20060. ret = WOLFSSL_FATAL_ERROR;
  20061. }
  20062. printf(resultFmt, ret == 0 ? passed : failed);
  20063. #endif /* NO_SIG_WRAPPER */
  20064. return ret;
  20065. }/* END test_wc_SignatureGetSize_rsa(void) */
  20066. /*----------------------------------------------------------------------------*
  20067. | hash.h Tests
  20068. *----------------------------------------------------------------------------*/
  20069. static int test_wc_HashInit(void)
  20070. {
  20071. int ret = 0, i; /* 0 indicates tests passed, 1 indicates failure */
  20072. wc_HashAlg hash;
  20073. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  20074. enum wc_HashType enumArray[] = {
  20075. #ifndef NO_MD5
  20076. WC_HASH_TYPE_MD5,
  20077. #endif
  20078. #ifndef NO_SHA
  20079. WC_HASH_TYPE_SHA,
  20080. #endif
  20081. #ifndef WOLFSSL_SHA224
  20082. WC_HASH_TYPE_SHA224,
  20083. #endif
  20084. #ifndef NO_SHA256
  20085. WC_HASH_TYPE_SHA256,
  20086. #endif
  20087. #ifndef WOLFSSL_SHA384
  20088. WC_HASH_TYPE_SHA384,
  20089. #endif
  20090. #ifndef WOLFSSL_SHA512
  20091. WC_HASH_TYPE_SHA512,
  20092. #endif
  20093. };
  20094. /* dynamically finds the length */
  20095. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  20096. /* For loop to test various arguments... */
  20097. for (i = 0; i < enumlen; i++) {
  20098. /* check for bad args */
  20099. if (wc_HashInit(&hash, enumArray[i]) == BAD_FUNC_ARG) {
  20100. ret = 1;
  20101. break;
  20102. }
  20103. wc_HashFree(&hash, enumArray[i]);
  20104. /* check for null ptr */
  20105. if (wc_HashInit(NULL, enumArray[i]) != BAD_FUNC_ARG) {
  20106. ret = 1;
  20107. break;
  20108. }
  20109. } /* end of for loop */
  20110. printf(testingFmt, "wc_HashInit()");
  20111. if (ret==0) { /* all tests have passed */
  20112. printf(resultFmt, passed);
  20113. }
  20114. else { /* a test has failed */
  20115. printf(resultFmt, failed);
  20116. }
  20117. return ret;
  20118. } /* end of test_wc_HashInit */
  20119. /*
  20120. * Unit test function for wc_HashSetFlags()
  20121. */
  20122. static int test_wc_HashSetFlags(void)
  20123. {
  20124. int ret = 0;
  20125. #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
  20126. wc_HashAlg hash;
  20127. word32 flags = 0;
  20128. int i, j;
  20129. printf(testingFmt, "wc_HashSetFlags()");
  20130. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  20131. enum wc_HashType enumArray[] = {
  20132. #ifndef NO_MD5
  20133. WC_HASH_TYPE_MD5,
  20134. #endif
  20135. #ifndef NO_SHA
  20136. WC_HASH_TYPE_SHA,
  20137. #endif
  20138. #ifdef WOLFSSL_SHA224
  20139. WC_HASH_TYPE_SHA224,
  20140. #endif
  20141. #ifndef NO_SHA256
  20142. WC_HASH_TYPE_SHA256,
  20143. #endif
  20144. #ifdef WOLFSSL_SHA384
  20145. WC_HASH_TYPE_SHA384,
  20146. #endif
  20147. #ifdef WOLFSSL_SHA512
  20148. WC_HASH_TYPE_SHA512,
  20149. #endif
  20150. #ifdef WOLFSSL_SHA3
  20151. WC_HASH_TYPE_SHA3_224,
  20152. #endif
  20153. };
  20154. enum wc_HashType notSupported[] = {
  20155. WC_HASH_TYPE_MD5_SHA,
  20156. WC_HASH_TYPE_MD2,
  20157. WC_HASH_TYPE_MD4,
  20158. WC_HASH_TYPE_BLAKE2B,
  20159. WC_HASH_TYPE_BLAKE2S,
  20160. WC_HASH_TYPE_NONE,
  20161. };
  20162. /* dynamically finds the length */
  20163. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  20164. /* For loop to test various arguments... */
  20165. for (i = 0; i < enumlen; i++) {
  20166. ret = wc_HashInit(&hash, enumArray[i]);
  20167. if (ret == 0) {
  20168. ret = wc_HashSetFlags(&hash, enumArray[i], flags);
  20169. }
  20170. if (ret == 0) {
  20171. if (flags & WC_HASH_FLAG_ISCOPY) {
  20172. ret = 0;
  20173. }
  20174. }
  20175. if (ret == 0) {
  20176. ret = wc_HashSetFlags(NULL, enumArray[i], flags);
  20177. if (ret == BAD_FUNC_ARG) {
  20178. ret = 0;
  20179. }
  20180. }
  20181. wc_HashFree(&hash, enumArray[i]);
  20182. }
  20183. /* For loop to test not supported cases */
  20184. int notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  20185. for (j = 0; j < notSupportedLen; j++){
  20186. if (ret == 0) {
  20187. ret = wc_HashInit(&hash, notSupported[j]);
  20188. if (ret == BAD_FUNC_ARG){
  20189. ret = 0;
  20190. if (ret == 0){
  20191. ret = wc_HashSetFlags(&hash, notSupported[j], flags);
  20192. if (ret == BAD_FUNC_ARG) {
  20193. ret = 0;
  20194. }
  20195. }
  20196. }
  20197. }
  20198. if (ret == 0) {
  20199. ret = wc_HashFree(&hash, notSupported[j]);
  20200. if (ret == BAD_FUNC_ARG) {
  20201. ret = 0;
  20202. }
  20203. }
  20204. }
  20205. printf(resultFmt, ret == 0 ? passed : failed);
  20206. #endif
  20207. return ret;
  20208. } /* END test_wc_HashSetFlags */
  20209. /*
  20210. * Unit test function for wc_HashGetFlags()
  20211. */
  20212. static int test_wc_HashGetFlags(void)
  20213. {
  20214. int ret = 0;
  20215. #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
  20216. wc_HashAlg hash;
  20217. word32 flags = 0;
  20218. int i, j;
  20219. printf(testingFmt, "wc_HashGetFlags()");
  20220. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  20221. enum wc_HashType enumArray[] = {
  20222. #ifndef NO_MD5
  20223. WC_HASH_TYPE_MD5,
  20224. #endif
  20225. #ifndef NO_SHA
  20226. WC_HASH_TYPE_SHA,
  20227. #endif
  20228. #ifdef WOLFSSL_SHA224
  20229. WC_HASH_TYPE_SHA224,
  20230. #endif
  20231. #ifndef NO_SHA256
  20232. WC_HASH_TYPE_SHA256,
  20233. #endif
  20234. #ifdef WOLFSSL_SHA384
  20235. WC_HASH_TYPE_SHA384,
  20236. #endif
  20237. #ifdef WOLFSSL_SHA512
  20238. WC_HASH_TYPE_SHA512,
  20239. #endif
  20240. #ifdef WOLFSSL_SHA3
  20241. WC_HASH_TYPE_SHA3_224,
  20242. #endif
  20243. };
  20244. enum wc_HashType notSupported[] = {
  20245. WC_HASH_TYPE_MD5_SHA,
  20246. WC_HASH_TYPE_MD2,
  20247. WC_HASH_TYPE_MD4,
  20248. WC_HASH_TYPE_BLAKE2B,
  20249. WC_HASH_TYPE_BLAKE2S,
  20250. WC_HASH_TYPE_NONE,
  20251. };
  20252. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  20253. /* For loop to test various arguments... */
  20254. for (i = 0; i < enumlen; i++) {
  20255. ret = wc_HashInit(&hash, enumArray[i]);
  20256. if (ret == 0) {
  20257. ret = wc_HashGetFlags(&hash, enumArray[i], &flags);
  20258. }
  20259. if (ret == 0) {
  20260. if (flags & WC_HASH_FLAG_ISCOPY) {
  20261. ret = 0;
  20262. }
  20263. }
  20264. if (ret == 0) {
  20265. ret = wc_HashGetFlags(NULL, enumArray[i], &flags);
  20266. if (ret == BAD_FUNC_ARG) {
  20267. ret = 0;
  20268. }
  20269. }
  20270. wc_HashFree(&hash, enumArray[i]);
  20271. if (ret != 0) {
  20272. break;
  20273. }
  20274. }
  20275. /* For loop to test not supported cases */
  20276. int notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  20277. for (j = 0; j < notSupportedLen; j++){
  20278. if (ret == 0) {
  20279. ret = wc_HashInit(&hash, notSupported[j]);
  20280. if (ret == BAD_FUNC_ARG){
  20281. ret = 0;
  20282. if (ret == 0){
  20283. ret = wc_HashGetFlags(&hash, notSupported[j], &flags);
  20284. if (ret == BAD_FUNC_ARG) {
  20285. ret = 0;
  20286. }
  20287. }
  20288. }
  20289. }
  20290. if (ret == 0) {
  20291. ret = wc_HashFree(&hash, notSupported[j]);
  20292. if (ret == BAD_FUNC_ARG) {
  20293. ret = 0;
  20294. }
  20295. }
  20296. }
  20297. printf(resultFmt, ret == 0 ? passed : failed);
  20298. #endif
  20299. return ret;
  20300. } /* END test_wc_HashGetFlags */
  20301. /*----------------------------------------------------------------------------*
  20302. | Compatibility Tests
  20303. *----------------------------------------------------------------------------*/
  20304. static void test_wolfSSL_X509_NAME(void)
  20305. {
  20306. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  20307. && !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  20308. X509* x509;
  20309. const unsigned char* c;
  20310. unsigned char buf[4096];
  20311. int bytes;
  20312. XFILE f;
  20313. const X509_NAME* a;
  20314. const X509_NAME* b;
  20315. X509_NAME* d2i_name;
  20316. int sz;
  20317. unsigned char* tmp;
  20318. char file[] = "./certs/ca-cert.der";
  20319. printf(testingFmt, "wolfSSL_X509_NAME()");
  20320. /* test compile of deprecated function, returns 0 */
  20321. AssertIntEQ(CRYPTO_thread_id(), 0);
  20322. AssertNotNull(a = X509_NAME_new());
  20323. X509_NAME_free((X509_NAME*)a);
  20324. f = XFOPEN(file, "rb");
  20325. AssertTrue(f != XBADFILE);
  20326. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  20327. XFCLOSE(f);
  20328. c = buf;
  20329. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(c, bytes,
  20330. SSL_FILETYPE_ASN1));
  20331. /* test cmp function */
  20332. AssertNotNull(a = X509_get_issuer_name(x509));
  20333. AssertNotNull(b = X509_get_subject_name(x509));
  20334. AssertIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  20335. tmp = buf;
  20336. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  20337. if (sz > 0 && tmp == buf) {
  20338. printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__); \
  20339. printf(" Expected pointer to be incremented\n");
  20340. abort();
  20341. }
  20342. tmp = buf;
  20343. AssertNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  20344. /* retry but with the function creating a buffer */
  20345. tmp = NULL;
  20346. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  20347. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  20348. AssertNotNull(b = X509_NAME_dup((X509_NAME*)a));
  20349. AssertIntEQ(X509_NAME_cmp(a, b), 0);
  20350. X509_NAME_free((X509_NAME*)b);
  20351. X509_NAME_free(d2i_name);
  20352. X509_free(x509);
  20353. printf(resultFmt, passed);
  20354. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  20355. }
  20356. static void test_wolfSSL_X509_INFO(void)
  20357. {
  20358. #if defined(OPENSSL_ALL)
  20359. STACK_OF(X509_INFO) *info_stack;
  20360. X509_INFO *info;
  20361. BIO *cert;
  20362. int i;
  20363. printf(testingFmt, "wolfSSL_X509_INFO");
  20364. AssertNotNull(cert = BIO_new_file(cliCertFileExt, "r"));
  20365. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  20366. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  20367. AssertNotNull(info = sk_X509_INFO_value(info_stack, i));
  20368. AssertNotNull(info->x509);
  20369. AssertNull(info->crl);
  20370. }
  20371. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  20372. BIO_free(cert);
  20373. AssertNotNull(cert = BIO_new_file(cliCertFileExt, "r"));
  20374. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  20375. sk_X509_INFO_free(info_stack);
  20376. BIO_free(cert);
  20377. printf(resultFmt, passed);
  20378. #endif
  20379. }
  20380. static void test_wolfSSL_X509_subject_name_hash(void)
  20381. {
  20382. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  20383. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  20384. X509* x509;
  20385. X509_NAME* subjectName = NULL;
  20386. unsigned long ret = 0;
  20387. printf(testingFmt, "wolfSSL_X509_subject_name_hash()");
  20388. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  20389. SSL_FILETYPE_PEM));
  20390. AssertNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  20391. ret = X509_subject_name_hash(x509);
  20392. AssertIntNE(ret, 0);
  20393. X509_free(x509);
  20394. printf(resultFmt, passed);
  20395. #endif
  20396. }
  20397. static void test_wolfSSL_X509_issuer_name_hash(void)
  20398. {
  20399. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  20400. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  20401. X509* x509;
  20402. X509_NAME* issuertName = NULL;
  20403. unsigned long ret = 0;
  20404. printf(testingFmt, "wolfSSL_X509_issuer_name_hash()");
  20405. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  20406. SSL_FILETYPE_PEM));
  20407. AssertNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  20408. ret = X509_issuer_name_hash(x509);
  20409. AssertIntNE(ret, 0);
  20410. X509_free(x509);
  20411. printf(resultFmt, passed);
  20412. #endif
  20413. }
  20414. static void test_wolfSSL_DES(void)
  20415. {
  20416. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  20417. const_DES_cblock myDes;
  20418. DES_cblock iv;
  20419. DES_key_schedule key;
  20420. word32 i;
  20421. DES_LONG dl;
  20422. unsigned char msg[] = "hello wolfssl";
  20423. printf(testingFmt, "wolfSSL_DES()");
  20424. DES_check_key(1);
  20425. DES_set_key(&myDes, &key);
  20426. /* check, check of odd parity */
  20427. XMEMSET(myDes, 4, sizeof(const_DES_cblock)); myDes[0] = 6; /*set even parity*/
  20428. XMEMSET(key, 5, sizeof(DES_key_schedule));
  20429. AssertIntEQ(DES_set_key_checked(&myDes, &key), -1);
  20430. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  20431. /* set odd parity for success case */
  20432. DES_set_odd_parity(&myDes);
  20433. AssertIntEQ(DES_check_key_parity(&myDes), 1);
  20434. printf("%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2], myDes[3]);
  20435. AssertIntEQ(DES_set_key_checked(&myDes, &key), 0);
  20436. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  20437. AssertIntEQ(key[i], myDes[i]);
  20438. }
  20439. AssertIntEQ(DES_is_weak_key(&myDes), 0);
  20440. /* check weak key */
  20441. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  20442. XMEMSET(key, 5, sizeof(DES_key_schedule));
  20443. AssertIntEQ(DES_set_key_checked(&myDes, &key), -2);
  20444. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  20445. /* now do unchecked copy of a weak key over */
  20446. DES_set_key_unchecked(&myDes, &key);
  20447. /* compare arrays, should be the same */
  20448. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  20449. AssertIntEQ(key[i], myDes[i]);
  20450. }
  20451. AssertIntEQ(DES_is_weak_key(&myDes), 1);
  20452. /* check DES_key_sched API */
  20453. XMEMSET(key, 1, sizeof(DES_key_schedule));
  20454. AssertIntEQ(DES_key_sched(&myDes, NULL), 0);
  20455. AssertIntEQ(DES_key_sched(NULL, &key), 0);
  20456. AssertIntEQ(DES_key_sched(&myDes, &key), 0);
  20457. /* compare arrays, should be the same */
  20458. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  20459. AssertIntEQ(key[i], myDes[i]);
  20460. }
  20461. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  20462. * DES_cbc_encrypt on the input */
  20463. XMEMSET(iv, 0, sizeof(DES_cblock));
  20464. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  20465. AssertIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  20466. AssertIntEQ(dl, 480052723);
  20467. printf(resultFmt, passed);
  20468. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  20469. }
  20470. static void test_wc_PemToDer(void)
  20471. {
  20472. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER)
  20473. int ret;
  20474. DerBuffer* pDer = NULL;
  20475. const char* ca_cert = "./certs/server-cert.pem";
  20476. byte* cert_buf = NULL;
  20477. size_t cert_sz = 0;
  20478. int eccKey = 0;
  20479. EncryptedInfo info;
  20480. printf(testingFmt, "wc_PemToDer()");
  20481. XMEMSET(&info, 0, sizeof(info));
  20482. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  20483. if (ret == 0) {
  20484. ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE,
  20485. &pDer, NULL, &info, &eccKey);
  20486. AssertIntEQ(ret, 0);
  20487. wc_FreeDer(&pDer);
  20488. }
  20489. if (cert_buf)
  20490. free(cert_buf);
  20491. #ifdef HAVE_ECC
  20492. {
  20493. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  20494. byte key_buf[256] = {0};
  20495. /* Test fail of loading a key with cert type */
  20496. AssertIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  20497. key_buf[0] = '\n';
  20498. XMEMCPY(key_buf + 1, cert_buf, cert_sz);
  20499. AssertIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  20500. &pDer, NULL, &info, &eccKey)), 0);
  20501. #ifdef OPENSSL_EXTRA
  20502. AssertIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  20503. &pDer, NULL, &info, &eccKey)), 0);
  20504. #endif
  20505. wc_FreeDer(&pDer);
  20506. if (cert_buf)
  20507. free(cert_buf);
  20508. }
  20509. #endif
  20510. printf(resultFmt, passed);
  20511. #endif
  20512. }
  20513. static void test_wc_AllocDer(void)
  20514. {
  20515. #if !defined(NO_CERTS)
  20516. int ret;
  20517. DerBuffer* pDer = NULL;
  20518. word32 testSize = 1024;
  20519. printf(testingFmt, "wc_AllocDer()");
  20520. ret = wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT);
  20521. AssertIntEQ(ret, 0);
  20522. AssertNotNull(pDer);
  20523. wc_FreeDer(&pDer);
  20524. printf(resultFmt, passed);
  20525. #endif
  20526. }
  20527. static void test_wc_CertPemToDer(void)
  20528. {
  20529. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER)
  20530. int ret;
  20531. const char* ca_cert = "./certs/ca-cert.pem";
  20532. byte* cert_buf = NULL;
  20533. size_t cert_sz = 0, cert_dersz = 0;
  20534. byte* cert_der = NULL;
  20535. printf(testingFmt, "wc_CertPemToDer()");
  20536. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  20537. if (ret == 0) {
  20538. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  20539. cert_der = (byte*)malloc(cert_dersz);
  20540. if (cert_der) {
  20541. ret = wc_CertPemToDer(cert_buf, (int)cert_sz,
  20542. cert_der, (int)cert_dersz, CERT_TYPE);
  20543. AssertIntGE(ret, 0);
  20544. }
  20545. }
  20546. if (cert_der)
  20547. free(cert_der);
  20548. if (cert_buf)
  20549. free(cert_buf);
  20550. #endif
  20551. }
  20552. static void test_wc_PubKeyPemToDer(void)
  20553. {
  20554. #ifdef WOLFSSL_PEM_TO_DER
  20555. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER)
  20556. int ret;
  20557. const char* key = "./certs/ecc-client-keyPub.pem";
  20558. byte* cert_buf = NULL;
  20559. size_t cert_sz = 0, cert_dersz = 0;
  20560. byte* cert_der = NULL;
  20561. printf(testingFmt, "wc_PubKeyPemToDer()");
  20562. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  20563. cert_der, (int)cert_dersz);
  20564. AssertIntGE(ret, BAD_FUNC_ARG);
  20565. ret = load_file(key, &cert_buf, &cert_sz);
  20566. if (ret == 0) {
  20567. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  20568. cert_der = (byte*)malloc(cert_dersz);
  20569. if (cert_der) {
  20570. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  20571. cert_der, (int)cert_dersz);
  20572. AssertIntGE(ret, 0);
  20573. }
  20574. }
  20575. if (cert_der)
  20576. free(cert_der);
  20577. if (cert_buf)
  20578. free(cert_buf);
  20579. #endif
  20580. #endif
  20581. }
  20582. static void test_wc_PemPubKeyToDer(void)
  20583. {
  20584. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER)
  20585. int ret;
  20586. const char* key = "./certs/ecc-client-keyPub.pem";
  20587. size_t cert_dersz = 1024;
  20588. byte* cert_der = (byte*)malloc(cert_dersz);
  20589. printf(testingFmt, "wc_PemPubKeyToDer()");
  20590. ret = wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz);
  20591. AssertIntGE(ret, BAD_FUNC_ARG);
  20592. if (cert_der) {
  20593. ret = wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz);
  20594. AssertIntGE(ret, 0);
  20595. free(cert_der);
  20596. }
  20597. #endif
  20598. }
  20599. static void test_wolfSSL_certs(void)
  20600. {
  20601. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  20602. !defined(NO_RSA)
  20603. X509* x509ext;
  20604. #ifdef OPENSSL_ALL
  20605. X509* x509;
  20606. WOLFSSL_X509_EXTENSION* ext;
  20607. #endif
  20608. WOLFSSL* ssl;
  20609. WOLFSSL_CTX* ctx;
  20610. STACK_OF(ASN1_OBJECT)* sk;
  20611. ASN1_STRING* asn1_str;
  20612. AUTHORITY_KEYID* akey;
  20613. BASIC_CONSTRAINTS* bc;
  20614. int crit;
  20615. printf(testingFmt, "wolfSSL_certs()");
  20616. #ifndef NO_WOLFSSL_SERVER
  20617. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  20618. #else
  20619. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  20620. #endif
  20621. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  20622. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  20623. #ifndef HAVE_USER_RSA
  20624. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  20625. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  20626. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  20627. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  20628. #endif
  20629. AssertNotNull(ssl = SSL_new(ctx));
  20630. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  20631. #ifdef HAVE_PK_CALLBACKS
  20632. AssertIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  20633. #endif /* HAVE_PK_CALLBACKS */
  20634. /* create and use x509 */
  20635. #ifdef OPENSSL_ALL
  20636. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  20637. AssertNotNull(x509);
  20638. #endif
  20639. x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt, WOLFSSL_FILETYPE_PEM);
  20640. AssertNotNull(x509ext);
  20641. AssertIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  20642. #ifndef HAVE_USER_RSA
  20643. /* with loading in a new cert the check on private key should now fail */
  20644. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  20645. #endif
  20646. #if defined(USE_CERT_BUFFERS_2048)
  20647. AssertIntEQ(SSL_use_certificate_ASN1(ssl,
  20648. (unsigned char*)server_cert_der_2048,
  20649. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  20650. #endif
  20651. #if !defined(NO_SHA) && !defined(NO_SHA256)
  20652. /************* Get Digest of Certificate ******************/
  20653. {
  20654. byte digest[64]; /* max digest size */
  20655. word32 digestSz;
  20656. XMEMSET(digest, 0, sizeof(digest));
  20657. AssertIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  20658. WOLFSSL_SUCCESS);
  20659. AssertIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  20660. WOLFSSL_SUCCESS);
  20661. AssertIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  20662. WOLFSSL_FAILURE);
  20663. }
  20664. #endif /* !NO_SHA && !NO_SHA256*/
  20665. /* test and checkout X509 extensions */
  20666. bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext, NID_basic_constraints,
  20667. &crit, NULL);
  20668. AssertNotNull(bc);
  20669. #ifdef OPENSSL_ALL
  20670. ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc);
  20671. AssertNotNull(ext);
  20672. X509_EXTENSION_free(ext);
  20673. #endif
  20674. AssertIntEQ(crit, 0);
  20675. BASIC_CONSTRAINTS_free(bc);
  20676. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit, NULL);
  20677. AssertNotNull(asn1_str);
  20678. AssertIntEQ(crit, 1);
  20679. AssertIntEQ(asn1_str->type, NID_key_usage);
  20680. #ifdef OPENSSL_ALL
  20681. ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str);
  20682. AssertNotNull(ext);
  20683. X509_EXTENSION_free(ext);
  20684. #endif
  20685. ASN1_STRING_free(asn1_str);
  20686. #ifdef OPENSSL_ALL
  20687. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_ext_key_usage,
  20688. &crit, NULL);
  20689. AssertNotNull(sk);
  20690. ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk);
  20691. AssertNotNull(ext);
  20692. X509_EXTENSION_free(ext);
  20693. sk_ASN1_OBJECT_free(sk);
  20694. #else
  20695. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  20696. &crit, NULL);
  20697. AssertNull(sk);
  20698. #endif
  20699. akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  20700. NID_authority_key_identifier, &crit, NULL);
  20701. AssertNotNull(akey);
  20702. #ifdef OPENSSL_ALL
  20703. ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit, akey);
  20704. AssertNotNull(ext);
  20705. X509_EXTENSION_free(ext);
  20706. #endif
  20707. wolfSSL_AUTHORITY_KEYID_free(akey);
  20708. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  20709. NID_private_key_usage_period, &crit, NULL);
  20710. /* AssertNotNull(sk); NID not yet supported */
  20711. AssertIntEQ(crit, -1);
  20712. sk_ASN1_OBJECT_free(sk);
  20713. sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext, NID_subject_alt_name,
  20714. &crit, NULL);
  20715. /* AssertNotNull(sk); no alt names set */
  20716. sk_GENERAL_NAME_free(sk);
  20717. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_issuer_alt_name,
  20718. &crit, NULL);
  20719. /* AssertNotNull(sk); NID not yet supported */
  20720. AssertIntEQ(crit, -1);
  20721. sk_ASN1_OBJECT_free(sk);
  20722. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_info_access, &crit,
  20723. NULL);
  20724. /* AssertNotNull(sk); no auth info set */
  20725. sk_ASN1_OBJECT_free(sk);
  20726. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_sinfo_access,
  20727. &crit, NULL);
  20728. /* AssertNotNull(sk); NID not yet supported */
  20729. AssertIntEQ(crit, -1);
  20730. sk_ASN1_OBJECT_free(sk);
  20731. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_name_constraints,
  20732. &crit, NULL);
  20733. /* AssertNotNull(sk); NID not yet supported */
  20734. AssertIntEQ(crit, -1);
  20735. sk_ASN1_OBJECT_free(sk);
  20736. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  20737. NID_certificate_policies, &crit, NULL);
  20738. #if !defined(WOLFSSL_SEP) && !defined(WOLFSSL_CERT_EXT)
  20739. AssertNull(sk);
  20740. #else
  20741. /* AssertNotNull(sk); no cert policy set */
  20742. #endif
  20743. sk_ASN1_OBJECT_free(sk);
  20744. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_policy_mappings,
  20745. &crit, NULL);
  20746. /* AssertNotNull(sk); NID not yet supported */
  20747. AssertIntEQ(crit, -1);
  20748. sk_ASN1_OBJECT_free(sk);
  20749. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_policy_constraints,
  20750. &crit, NULL);
  20751. /* AssertNotNull(sk); NID not yet supported */
  20752. AssertIntEQ(crit, -1);
  20753. sk_ASN1_OBJECT_free(sk);
  20754. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_inhibit_any_policy,
  20755. &crit, NULL);
  20756. /* AssertNotNull(sk); NID not yet supported */
  20757. AssertIntEQ(crit, -1);
  20758. sk_ASN1_OBJECT_free(sk);
  20759. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_tlsfeature, &crit,
  20760. NULL);
  20761. /* AssertNotNull(sk); NID not yet supported */
  20762. AssertIntEQ(crit, -1);
  20763. sk_ASN1_OBJECT_free(sk);
  20764. /* test invalid cases */
  20765. crit = 0;
  20766. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit, NULL);
  20767. AssertNull(sk);
  20768. AssertIntEQ(crit, -1);
  20769. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL, NID_tlsfeature,
  20770. NULL, NULL);
  20771. AssertNull(sk);
  20772. AssertIntEQ(SSL_get_hit(ssl), 0);
  20773. #ifdef OPENSSL_ALL
  20774. X509_free(x509);
  20775. #endif
  20776. X509_free(x509ext);
  20777. SSL_free(ssl);
  20778. SSL_CTX_free(ctx);
  20779. printf(resultFmt, passed);
  20780. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  20781. }
  20782. static void test_wolfSSL_ASN1_TIME_print(void)
  20783. {
  20784. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) \
  20785. && (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  20786. defined(WOLFSSL_HAPROXY)) && defined(USE_CERT_BUFFERS_2048)
  20787. BIO* bio;
  20788. X509* x509;
  20789. const unsigned char* der = client_cert_der_2048;
  20790. ASN1_TIME* t;
  20791. unsigned char buf[25];
  20792. printf(testingFmt, "wolfSSL_ASN1_TIME_print()");
  20793. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  20794. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  20795. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  20796. AssertIntEQ(ASN1_TIME_print(bio, X509_get_notBefore(x509)), 1);
  20797. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  20798. AssertIntEQ(XMEMCMP(buf, "Jun 19 13:23:41 2020 GMT", sizeof(buf) - 1), 0);
  20799. /* create a bad time and test results */
  20800. AssertNotNull(t = X509_get_notAfter(x509));
  20801. AssertIntEQ(ASN1_TIME_check(t), WOLFSSL_SUCCESS);
  20802. t->data[8] = 0;
  20803. t->data[3] = 0;
  20804. AssertIntNE(ASN1_TIME_print(bio, t), 1);
  20805. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  20806. AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  20807. AssertIntEQ(ASN1_TIME_check(t), WOLFSSL_FAILURE);
  20808. BIO_free(bio);
  20809. X509_free(x509);
  20810. printf(resultFmt, passed);
  20811. #endif
  20812. }
  20813. static void test_wolfSSL_ASN1_UTCTIME_print(void)
  20814. {
  20815. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  20816. BIO* bio;
  20817. ASN1_UTCTIME* utc = NULL;
  20818. unsigned char buf[25];
  20819. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  20820. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  20821. printf(testingFmt, "ASN1_UTCTIME_print()");
  20822. /* NULL parameter check */
  20823. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  20824. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  20825. BIO_free(bio);
  20826. /* Valid date */
  20827. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  20828. AssertNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  20829. DYNAMIC_TYPE_ASN1));
  20830. utc->type = ASN_UTC_TIME;
  20831. utc->length = ASN_UTC_TIME_SIZE;
  20832. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  20833. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  20834. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  20835. AssertIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  20836. XMEMSET(buf, 0, sizeof(buf));
  20837. BIO_free(bio);
  20838. /* Invalid format */
  20839. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  20840. utc->type = ASN_UTC_TIME;
  20841. utc->length = ASN_UTC_TIME_SIZE;
  20842. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  20843. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  20844. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  20845. AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  20846. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  20847. BIO_free(bio);
  20848. printf(resultFmt, passed);
  20849. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME */
  20850. }
  20851. static void test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  20852. {
  20853. #if defined(OPENSSL_EXTRA)
  20854. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime;
  20855. unsigned char nullstr[32];
  20856. XMEMSET(nullstr, 0, 32);
  20857. asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  20858. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL,
  20859. DYNAMIC_TYPE_TMP_BUFFER);
  20860. if (asn1_gtime) {
  20861. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  20862. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  20863. AssertIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  20864. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  20865. }
  20866. #endif /* OPENSSL_EXTRA */
  20867. }
  20868. static void test_wolfSSL_private_keys(void)
  20869. {
  20870. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  20871. !defined(NO_FILESYSTEM)
  20872. WOLFSSL* ssl;
  20873. WOLFSSL_CTX* ctx;
  20874. EVP_PKEY* pkey = NULL;
  20875. printf(testingFmt, "wolfSSL_private_keys()");
  20876. OpenSSL_add_all_digests();
  20877. OpenSSL_add_all_algorithms();
  20878. #ifndef NO_RSA
  20879. #ifndef NO_WOLFSSL_SERVER
  20880. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  20881. #else
  20882. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  20883. #endif
  20884. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  20885. /* Have to load a cert before you can check the private key against that
  20886. * certificates public key! */
  20887. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  20888. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  20889. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  20890. AssertNotNull(ssl = SSL_new(ctx));
  20891. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  20892. #ifdef USE_CERT_BUFFERS_2048
  20893. {
  20894. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  20895. unsigned char buf[FOURK_BUF];
  20896. word32 bufSz;
  20897. AssertIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  20898. (unsigned char*)client_key_der_2048,
  20899. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  20900. #ifndef HAVE_USER_RSA
  20901. /* Should mismatch now that a different private key loaded */
  20902. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  20903. #endif
  20904. AssertIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  20905. (unsigned char*)server_key,
  20906. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  20907. /* After loading back in DER format of original key, should match */
  20908. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  20909. /* test loading private key to the WOLFSSL_CTX */
  20910. AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  20911. (unsigned char*)client_key_der_2048,
  20912. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  20913. #ifndef NO_CHECK_PRIVATE_KEY
  20914. #ifndef HAVE_USER_RSA
  20915. /* Should mismatch now that a different private key loaded */
  20916. AssertIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  20917. #endif
  20918. AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  20919. (unsigned char*)server_key,
  20920. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  20921. /* After loading back in DER format of original key, should match */
  20922. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  20923. #endif /* !NO_CHECK_PRIVATE_KEY */
  20924. /* pkey not set yet, expecting to fail */
  20925. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  20926. /* set PKEY and test again */
  20927. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  20928. &server_key, (long)sizeof_server_key_der_2048));
  20929. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  20930. /* reuse PKEY structure and test
  20931. * this should be checked with a memory management sanity checker */
  20932. AssertFalse(server_key == (const unsigned char*)server_key_der_2048);
  20933. server_key = (const unsigned char*)server_key_der_2048;
  20934. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  20935. &server_key, (long)sizeof_server_key_der_2048));
  20936. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  20937. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  20938. bufSz = FOURK_BUF;
  20939. AssertIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  20940. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  20941. RSAk, NULL, 0)), 0);
  20942. server_key = (const unsigned char*)buf;
  20943. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  20944. (long)bufSz));
  20945. }
  20946. #endif
  20947. EVP_PKEY_free(pkey);
  20948. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  20949. SSL_CTX_free(ctx);
  20950. #endif /* end of RSA private key match tests */
  20951. #ifdef HAVE_ECC
  20952. #ifndef NO_WOLFSSL_SERVER
  20953. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  20954. #else
  20955. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  20956. #endif
  20957. AssertTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  20958. WOLFSSL_FILETYPE_PEM));
  20959. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  20960. WOLFSSL_FILETYPE_PEM));
  20961. AssertNotNull(ssl = SSL_new(ctx));
  20962. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  20963. SSL_free(ssl);
  20964. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  20965. WOLFSSL_FILETYPE_PEM));
  20966. AssertNotNull(ssl = SSL_new(ctx));
  20967. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  20968. SSL_free(ssl);
  20969. SSL_CTX_free(ctx);
  20970. #endif /* end of ECC private key match tests */
  20971. #ifdef HAVE_ED25519
  20972. #ifndef NO_WOLFSSL_SERVER
  20973. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  20974. #else
  20975. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  20976. #endif
  20977. AssertTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  20978. WOLFSSL_FILETYPE_PEM));
  20979. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  20980. WOLFSSL_FILETYPE_PEM));
  20981. AssertNotNull(ssl = SSL_new(ctx));
  20982. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  20983. SSL_free(ssl);
  20984. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  20985. WOLFSSL_FILETYPE_PEM));
  20986. AssertNotNull(ssl = SSL_new(ctx));
  20987. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  20988. SSL_free(ssl);
  20989. SSL_CTX_free(ctx);
  20990. #endif /* end of Ed25519 private key match tests */
  20991. #ifdef HAVE_ED448
  20992. #ifndef NO_WOLFSSL_SERVER
  20993. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  20994. #else
  20995. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  20996. #endif
  20997. AssertTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  20998. WOLFSSL_FILETYPE_PEM));
  20999. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  21000. WOLFSSL_FILETYPE_PEM));
  21001. AssertNotNull(ssl = SSL_new(ctx));
  21002. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  21003. SSL_free(ssl);
  21004. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  21005. WOLFSSL_FILETYPE_PEM));
  21006. AssertNotNull(ssl = SSL_new(ctx));
  21007. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  21008. SSL_free(ssl);
  21009. SSL_CTX_free(ctx);
  21010. #endif /* end of Ed448 private key match tests */
  21011. EVP_cleanup();
  21012. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  21013. CONF_modules_free();
  21014. ENGINE_cleanup();
  21015. CONF_modules_unload();
  21016. (void)ssl;
  21017. (void)ctx;
  21018. (void)pkey;
  21019. printf(resultFmt, passed);
  21020. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  21021. }
  21022. static void test_wolfSSL_PEM_PrivateKey(void)
  21023. {
  21024. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  21025. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  21026. BIO* bio = NULL;
  21027. EVP_PKEY* pkey = NULL;
  21028. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  21029. /* test creating new EVP_PKEY with bad arg */
  21030. AssertNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  21031. /* test loading RSA key using BIO */
  21032. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  21033. {
  21034. XFILE file;
  21035. const char* fname = "./certs/server-key.pem";
  21036. size_t sz;
  21037. byte* buf;
  21038. file = XFOPEN(fname, "rb");
  21039. AssertTrue((file != XBADFILE));
  21040. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  21041. sz = XFTELL(file);
  21042. XREWIND(file);
  21043. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  21044. if (buf) {
  21045. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  21046. }
  21047. XFCLOSE(file);
  21048. /* Test using BIO new mem and loading PEM private key */
  21049. bio = BIO_new_mem_buf(buf, (int)sz);
  21050. AssertNotNull(bio);
  21051. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  21052. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  21053. BIO_free(bio);
  21054. bio = NULL;
  21055. EVP_PKEY_free(pkey);
  21056. pkey = NULL;
  21057. }
  21058. #endif
  21059. /* test loading ECC key using BIO */
  21060. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  21061. {
  21062. XFILE file;
  21063. const char* fname = "./certs/ecc-key.pem";
  21064. size_t sz;
  21065. byte* buf;
  21066. file = XFOPEN(fname, "rb");
  21067. AssertTrue((file != XBADFILE));
  21068. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  21069. sz = XFTELL(file);
  21070. XREWIND(file);
  21071. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  21072. if (buf)
  21073. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  21074. XFCLOSE(file);
  21075. /* Test using BIO new mem and loading PEM private key */
  21076. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  21077. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  21078. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  21079. BIO_free(bio);
  21080. bio = NULL;
  21081. EVP_PKEY_free(pkey);
  21082. pkey = NULL;
  21083. }
  21084. #endif
  21085. #if !defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN))
  21086. {
  21087. #define BIO_PEM_TEST_CHAR 'a'
  21088. EVP_PKEY* pkey2 = NULL;
  21089. unsigned char extra[10];
  21090. int i;
  21091. printf(testingFmt, "wolfSSL_PEM_PrivateKey()");
  21092. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  21093. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  21094. AssertIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  21095. AssertNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
  21096. &server_key, (long)sizeof_server_key_der_2048));
  21097. AssertNull(pkey);
  21098. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  21099. &server_key, (long)sizeof_server_key_der_2048));
  21100. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  21101. WOLFSSL_SUCCESS);
  21102. /* test creating new EVP_PKEY with good args */
  21103. AssertNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  21104. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr)
  21105. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz), 0);
  21106. /* test of reuse of EVP_PKEY */
  21107. AssertNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  21108. AssertIntEQ(BIO_pending(bio), 0);
  21109. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  21110. SSL_SUCCESS);
  21111. AssertIntEQ(BIO_write(bio, extra, 10), 10); /* add 10 extra bytes after PEM */
  21112. AssertNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  21113. AssertNotNull(pkey);
  21114. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  21115. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
  21116. }
  21117. AssertIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
  21118. AssertIntEQ(BIO_read(bio, extra, 10), 10);
  21119. for (i = 0; i < 10; i++) {
  21120. AssertIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  21121. }
  21122. BIO_free(bio);
  21123. bio = NULL;
  21124. EVP_PKEY_free(pkey);
  21125. pkey = NULL;
  21126. EVP_PKEY_free(pkey2);
  21127. }
  21128. #endif
  21129. /* key is DES encrypted */
  21130. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  21131. !defined(NO_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_MD5)
  21132. {
  21133. XFILE f;
  21134. pem_password_cb* passwd_cb;
  21135. void* passwd_cb_userdata;
  21136. SSL_CTX* ctx;
  21137. char passwd[] = "bad password";
  21138. #ifndef WOLFSSL_NO_TLS12
  21139. #ifndef NO_WOLFSSL_SERVER
  21140. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  21141. #else
  21142. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  21143. #endif
  21144. #else
  21145. #ifndef NO_WOLFSSL_SERVER
  21146. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  21147. #else
  21148. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  21149. #endif
  21150. #endif
  21151. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  21152. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  21153. AssertNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  21154. AssertNull(passwd_cb_userdata =
  21155. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  21156. /* fail case with password call back */
  21157. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  21158. (void*)passwd));
  21159. BIO_free(bio);
  21160. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  21161. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  21162. (void*)passwd));
  21163. BIO_free(bio);
  21164. f = XFOPEN("./certs/server-keyEnc.pem", "rb");
  21165. AssertNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  21166. /* use callback that works */
  21167. AssertNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  21168. (void*)"yassl123"));
  21169. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  21170. EVP_PKEY_free(pkey);
  21171. pkey = NULL;
  21172. BIO_free(bio);
  21173. bio = NULL;
  21174. SSL_CTX_free(ctx);
  21175. }
  21176. #endif /* !defined(NO_DES3) */
  21177. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  21178. {
  21179. unsigned char buf[2048];
  21180. size_t bytes;
  21181. XFILE f;
  21182. SSL_CTX* ctx;
  21183. #ifndef WOLFSSL_NO_TLS12
  21184. #ifndef NO_WOLFSSL_SERVER
  21185. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  21186. #else
  21187. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  21188. #endif
  21189. #else
  21190. #ifndef NO_WOLFSSL_SERVER
  21191. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  21192. #else
  21193. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  21194. #endif
  21195. #endif
  21196. f = XFOPEN("./certs/ecc-key.der", "rb");
  21197. AssertTrue((f != XBADFILE));
  21198. bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f);
  21199. XFCLOSE(f);
  21200. server_key = buf;
  21201. pkey = NULL;
  21202. AssertNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  21203. AssertNull(pkey);
  21204. AssertNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  21205. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  21206. EVP_PKEY_free(pkey);
  21207. pkey = NULL;
  21208. SSL_CTX_free(ctx);
  21209. }
  21210. #endif
  21211. printf(resultFmt, passed);
  21212. (void)bio;
  21213. (void)pkey;
  21214. (void)server_key;
  21215. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  21216. }
  21217. static void test_wolfSSL_PEM_bio_RSAKey(void)
  21218. {
  21219. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  21220. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  21221. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  21222. RSA* rsa = NULL;
  21223. BIO* bio = NULL;
  21224. printf(testingFmt, "wolfSSL_PEM_bio_RSAKey");
  21225. /* PrivateKey */
  21226. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  21227. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  21228. AssertNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  21229. AssertIntEQ(RSA_size(rsa), 256);
  21230. AssertIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  21231. NULL), WOLFSSL_FAILURE);
  21232. BIO_free(bio);
  21233. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  21234. AssertIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  21235. NULL), WOLFSSL_SUCCESS);
  21236. BIO_free(bio);
  21237. RSA_free(rsa);
  21238. /* PUBKEY */
  21239. AssertNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  21240. AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  21241. AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  21242. AssertIntEQ(RSA_size(rsa), 256);
  21243. AssertIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  21244. BIO_free(bio);
  21245. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  21246. AssertIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  21247. BIO_free(bio);
  21248. RSA_free(rsa);
  21249. #ifdef HAVE_ECC
  21250. /* ensure that non-rsa keys do not work */
  21251. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  21252. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  21253. AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  21254. BIO_free(bio);
  21255. RSA_free(rsa);
  21256. #endif /* HAVE_ECC */
  21257. printf(resultFmt, passed);
  21258. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  21259. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  21260. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  21261. }
  21262. static void test_wolfSSL_PEM_RSAPrivateKey(void)
  21263. {
  21264. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  21265. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  21266. RSA* rsa = NULL;
  21267. RSA* rsa_dup = NULL;
  21268. BIO* bio = NULL;
  21269. printf(testingFmt, "wolfSSL_PEM_RSAPrivateKey()");
  21270. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  21271. AssertNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  21272. AssertIntEQ(RSA_size(rsa), 256);
  21273. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  21274. AssertNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  21275. AssertPtrNE(rsa_dup, rsa);
  21276. #endif
  21277. /* test if valgrind complains about unreleased memory */
  21278. RSA_up_ref(rsa);
  21279. RSA_free(rsa);
  21280. BIO_free(bio);
  21281. RSA_free(rsa);
  21282. RSA_free(rsa_dup);
  21283. #ifdef HAVE_ECC
  21284. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  21285. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  21286. BIO_free(bio);
  21287. #endif /* HAVE_ECC */
  21288. printf(resultFmt, passed);
  21289. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  21290. }
  21291. static void test_wolfSSL_PEM_bio_DSAKey(void)
  21292. {
  21293. #ifndef HAVE_SELFTEST
  21294. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  21295. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  21296. DSA* dsa = NULL;
  21297. BIO* bio = NULL;
  21298. printf(testingFmt, "wolfSSL_PEM_bio_DSAKey");
  21299. /* PrivateKey */
  21300. AssertNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  21301. AssertNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  21302. AssertNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  21303. AssertIntEQ(BN_num_bytes(dsa->g), 128);
  21304. AssertIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, NULL),
  21305. WOLFSSL_FAILURE);
  21306. BIO_free(bio);
  21307. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  21308. AssertIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL, NULL),
  21309. WOLFSSL_SUCCESS);
  21310. BIO_free(bio);
  21311. DSA_free(dsa);
  21312. /* PUBKEY */
  21313. AssertNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  21314. AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  21315. AssertNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  21316. AssertIntEQ(BN_num_bytes(dsa->g), 128);
  21317. AssertIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  21318. BIO_free(bio);
  21319. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  21320. AssertIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  21321. BIO_free(bio);
  21322. DSA_free(dsa);
  21323. #ifdef HAVE_ECC
  21324. /* ensure that non-dsa keys do not work */
  21325. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  21326. AssertNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  21327. AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  21328. BIO_free(bio);
  21329. DSA_free(dsa);
  21330. #endif /* HAVE_ECC */
  21331. printf(resultFmt, passed);
  21332. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  21333. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  21334. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  21335. #endif /* HAVE_SELFTEST */
  21336. }
  21337. static void test_wolfSSL_PEM_bio_ECKey(void)
  21338. {
  21339. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  21340. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  21341. EC_KEY* ec = NULL;
  21342. BIO* bio = NULL;
  21343. printf(testingFmt, "wolfSSL_PEM_bio_ECKey");
  21344. /* PrivateKey */
  21345. AssertNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  21346. AssertNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  21347. AssertNotNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  21348. AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  21349. AssertIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  21350. NULL),WOLFSSL_FAILURE);
  21351. BIO_free(bio);
  21352. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  21353. AssertIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  21354. NULL), WOLFSSL_SUCCESS);
  21355. BIO_free(bio);
  21356. EC_KEY_free(ec);
  21357. /* PUBKEY */
  21358. AssertNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  21359. AssertNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  21360. AssertNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  21361. AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  21362. AssertIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  21363. BIO_free(bio);
  21364. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  21365. AssertIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  21366. BIO_free(bio);
  21367. EC_KEY_free(ec);
  21368. #ifndef NO_RSA
  21369. /* ensure that non-ec keys do not work */
  21370. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  21371. AssertNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  21372. AssertNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  21373. BIO_free(bio);
  21374. EC_KEY_free(ec);
  21375. #endif /* HAVE_ECC */
  21376. printf(resultFmt, passed);
  21377. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  21378. }
  21379. static void test_wolfSSL_PEM_PUBKEY(void)
  21380. {
  21381. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  21382. BIO* bio = NULL;
  21383. EVP_PKEY* pkey = NULL;
  21384. /* test creating new EVP_PKEY with bad arg */
  21385. AssertNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  21386. /* test loading ECC key using BIO */
  21387. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  21388. {
  21389. XFILE file;
  21390. const char* fname = "./certs/ecc-client-keyPub.pem";
  21391. size_t sz;
  21392. byte* buf;
  21393. file = XFOPEN(fname, "rb");
  21394. AssertTrue((file != XBADFILE));
  21395. XFSEEK(file, 0, XSEEK_END);
  21396. sz = XFTELL(file);
  21397. XREWIND(file);
  21398. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  21399. if (buf)
  21400. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  21401. XFCLOSE(file);
  21402. /* Test using BIO new mem and loading PEM private key */
  21403. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  21404. AssertNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  21405. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  21406. BIO_free(bio);
  21407. bio = NULL;
  21408. EVP_PKEY_free(pkey);
  21409. pkey = NULL;
  21410. }
  21411. #endif
  21412. (void)bio;
  21413. (void)pkey;
  21414. #endif
  21415. }
  21416. static void test_DSA_do_sign_verify(void)
  21417. {
  21418. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  21419. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  21420. !defined(NO_DSA)
  21421. unsigned char digest[WC_SHA_DIGEST_SIZE];
  21422. DSA_SIG* sig;
  21423. DSA* dsa;
  21424. word32 bytes;
  21425. byte sigBin[DSA_SIG_SIZE];
  21426. int dsacheck;
  21427. #ifdef USE_CERT_BUFFERS_1024
  21428. byte tmp[ONEK_BUF];
  21429. XMEMSET(tmp, 0, sizeof(tmp));
  21430. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  21431. bytes = sizeof_dsa_key_der_1024;
  21432. #elif defined(USE_CERT_BUFFERS_2048)
  21433. byte tmp[TWOK_BUF];
  21434. XMEMSET(tmp, 0, sizeof(tmp));
  21435. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  21436. bytes = sizeof_dsa_key_der_2048;
  21437. #else
  21438. byte tmp[TWOK_BUF];
  21439. XMEMSET(tmp, 0, sizeof(tmp));
  21440. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  21441. if (fp == XBADFILE) {
  21442. return WOLFSSL_BAD_FILE;
  21443. }
  21444. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  21445. XFCLOSE(fp);
  21446. #endif /* END USE_CERT_BUFFERS_1024 */
  21447. printf(testingFmt, "DSA_do_sign_verify()");
  21448. XMEMSET(digest, 202, sizeof(digest));
  21449. AssertNotNull(dsa = DSA_new());
  21450. AssertIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
  21451. AssertIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  21452. AssertIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  21453. AssertNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  21454. AssertIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  21455. DSA_SIG_free(sig);
  21456. DSA_free(dsa);
  21457. #endif
  21458. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  21459. }
  21460. static void test_wolfSSL_tmp_dh(void)
  21461. {
  21462. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  21463. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH)
  21464. byte buffer[6000];
  21465. char file[] = "./certs/dsaparams.pem";
  21466. XFILE f;
  21467. int bytes;
  21468. DSA* dsa;
  21469. DH* dh;
  21470. BIO* bio;
  21471. SSL* ssl;
  21472. SSL_CTX* ctx;
  21473. printf(testingFmt, "wolfSSL_tmp_dh()");
  21474. #ifndef NO_WOLFSSL_SERVER
  21475. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  21476. #else
  21477. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  21478. #endif
  21479. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  21480. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  21481. AssertNotNull(ssl = SSL_new(ctx));
  21482. f = XFOPEN(file, "rb");
  21483. AssertTrue((f != XBADFILE));
  21484. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  21485. XFCLOSE(f);
  21486. bio = BIO_new_mem_buf((void*)buffer, bytes);
  21487. AssertNotNull(bio);
  21488. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  21489. AssertNotNull(dsa);
  21490. dh = wolfSSL_DSA_dup_DH(dsa);
  21491. AssertNotNull(dh);
  21492. AssertIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  21493. #ifndef NO_WOLFSSL_SERVER
  21494. AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  21495. #else
  21496. AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  21497. #endif
  21498. BIO_free(bio);
  21499. DSA_free(dsa);
  21500. DH_free(dh);
  21501. SSL_free(ssl);
  21502. SSL_CTX_free(ctx);
  21503. printf(resultFmt, passed);
  21504. #endif
  21505. }
  21506. static void test_wolfSSL_ctrl(void)
  21507. {
  21508. #if defined (OPENSSL_EXTRA)
  21509. byte buff[6000];
  21510. BIO* bio;
  21511. int bytes;
  21512. BUF_MEM* ptr = NULL;
  21513. printf(testingFmt, "wolfSSL_crtl()");
  21514. bytes = sizeof(buff);
  21515. bio = BIO_new_mem_buf((void*)buff, bytes);
  21516. AssertNotNull(bio);
  21517. AssertNotNull(BIO_s_socket());
  21518. AssertIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  21519. /* needs tested after stubs filled out @TODO
  21520. SSL_ctrl
  21521. SSL_CTX_ctrl
  21522. */
  21523. BIO_free(bio);
  21524. printf(resultFmt, passed);
  21525. #endif /* defined(OPENSSL_EXTRA) */
  21526. }
  21527. static void test_wolfSSL_EVP_PKEY_new_mac_key(void)
  21528. {
  21529. #ifdef OPENSSL_EXTRA
  21530. static const unsigned char pw[] = "password";
  21531. static const int pwSz = sizeof(pw) - 1;
  21532. size_t checkPwSz = 0;
  21533. const unsigned char* checkPw = NULL;
  21534. WOLFSSL_EVP_PKEY* key = NULL;
  21535. printf(testingFmt, "wolfSSL_EVP_PKEY_new_mac_key()");
  21536. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  21537. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  21538. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, pwSz));
  21539. if (key) {
  21540. AssertIntEQ(key->type, EVP_PKEY_HMAC);
  21541. AssertIntEQ(key->save_type, EVP_PKEY_HMAC);
  21542. AssertIntEQ(key->pkey_sz, pwSz);
  21543. AssertIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  21544. }
  21545. AssertNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  21546. AssertIntEQ((int)checkPwSz, pwSz);
  21547. if (checkPw) {
  21548. AssertIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  21549. }
  21550. wolfSSL_EVP_PKEY_free(key);
  21551. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, 0));
  21552. if (key) {
  21553. AssertIntEQ(key->pkey_sz, 0);
  21554. }
  21555. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  21556. (void)checkPw;
  21557. AssertIntEQ((int)checkPwSz, 0);
  21558. wolfSSL_EVP_PKEY_free(key);
  21559. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL, 0));
  21560. if (key) {
  21561. AssertIntEQ(key->pkey_sz, 0);
  21562. }
  21563. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  21564. (void)checkPw;
  21565. AssertIntEQ((int)checkPwSz, 0);
  21566. wolfSSL_EVP_PKEY_free(key);
  21567. printf(resultFmt, passed);
  21568. #endif /* OPENSSL_EXTRA */
  21569. }
  21570. static void test_wolfSSL_EVP_Digest(void)
  21571. {
  21572. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  21573. const char* in = "abc";
  21574. int inLen = (int)XSTRLEN(in);
  21575. byte out[WC_SHA256_DIGEST_SIZE];
  21576. unsigned int outLen;
  21577. const char* expOut = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  21578. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  21579. "\x15\xAD";
  21580. printf(testingFmt, "wolfSSL_EVP_Digest()");
  21581. AssertIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen, "SHA256", NULL), 1);
  21582. AssertIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  21583. AssertIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  21584. printf(resultFmt, passed);
  21585. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  21586. }
  21587. static void test_wolfSSL_EVP_MD_size(void)
  21588. {
  21589. #ifdef OPENSSL_EXTRA
  21590. WOLFSSL_EVP_MD_CTX mdCtx;
  21591. printf(testingFmt, "wolfSSL_EVP_MD_size()");
  21592. #ifndef NO_SHA256
  21593. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21594. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  21595. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA256_DIGEST_SIZE);
  21596. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  21597. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21598. #endif
  21599. #ifndef NO_MD5
  21600. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21601. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  21602. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  21603. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  21604. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21605. #endif
  21606. #ifdef WOLFSSL_SHA224
  21607. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21608. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  21609. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  21610. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  21611. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21612. #endif
  21613. #ifdef WOLFSSL_SHA384
  21614. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21615. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  21616. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  21617. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  21618. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21619. #endif
  21620. #ifdef WOLFSSL_SHA512
  21621. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21622. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  21623. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  21624. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  21625. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21626. #endif
  21627. #ifndef NO_SHA
  21628. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21629. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  21630. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  21631. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  21632. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21633. #endif
  21634. /* error case */
  21635. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21636. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  21637. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), BAD_FUNC_ARG);
  21638. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  21639. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 0);
  21640. printf(resultFmt, passed);
  21641. #endif /* OPENSSL_EXTRA */
  21642. }
  21643. static void test_wolfSSL_EVP_MD_hmac_signing(void)
  21644. {
  21645. #ifdef OPENSSL_EXTRA
  21646. const unsigned char testKey[] =
  21647. {
  21648. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  21649. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  21650. 0x0b, 0x0b, 0x0b, 0x0b
  21651. };
  21652. const char testData[] = "Hi There";
  21653. const unsigned char testResult[] =
  21654. {
  21655. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  21656. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  21657. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  21658. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  21659. };
  21660. unsigned char check[sizeof(testResult)];
  21661. size_t checkSz = -1;
  21662. WOLFSSL_EVP_PKEY* key;
  21663. WOLFSSL_EVP_MD_CTX mdCtx;
  21664. printf(testingFmt, "wolfSSL_EVP_MD_hmac_signing()");
  21665. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  21666. testKey, (int)sizeof(testKey)));
  21667. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21668. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  21669. NULL, key), 1);
  21670. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  21671. (unsigned int)XSTRLEN(testData)), 1);
  21672. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  21673. AssertIntEQ((int)checkSz, sizeof(testResult));
  21674. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  21675. AssertIntEQ((int)checkSz,(int)sizeof(testResult));
  21676. AssertIntEQ(XMEMCMP(testResult, check, sizeof(testResult)), 0);
  21677. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21678. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  21679. NULL, key), 1);
  21680. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  21681. (unsigned int)XSTRLEN(testData)),
  21682. 1);
  21683. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  21684. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21685. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21686. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  21687. NULL, key), 1);
  21688. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  21689. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  21690. AssertIntEQ((int)checkSz, sizeof(testResult));
  21691. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  21692. AssertIntEQ((int)checkSz,(int)sizeof(testResult));
  21693. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  21694. (unsigned int)XSTRLEN(testData) - 4), 1);
  21695. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  21696. AssertIntEQ((int)checkSz,(int)sizeof(testResult));
  21697. AssertIntEQ(XMEMCMP(testResult, check, sizeof(testResult)), 0);
  21698. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21699. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  21700. NULL, key), 1);
  21701. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  21702. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  21703. (unsigned int)XSTRLEN(testData) - 4),
  21704. 1);
  21705. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  21706. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21707. wolfSSL_EVP_PKEY_free(key);
  21708. printf(resultFmt, passed);
  21709. #endif /* OPENSSL_EXTRA */
  21710. }
  21711. static void test_wolfSSL_EVP_MD_rsa_signing(void)
  21712. {
  21713. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  21714. defined(USE_CERT_BUFFERS_2048)
  21715. WOLFSSL_EVP_PKEY* privKey;
  21716. WOLFSSL_EVP_PKEY* pubKey;
  21717. const char testData[] = "Hi There";
  21718. WOLFSSL_EVP_MD_CTX mdCtx;
  21719. size_t checkSz = -1;
  21720. int sz = 2048 / 8;
  21721. const unsigned char* cp;
  21722. const unsigned char* p;
  21723. unsigned char check[2048/8];
  21724. printf(testingFmt, "wolfSSL_EVP_MD_rsa_signing()");
  21725. cp = client_key_der_2048;
  21726. AssertNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  21727. sizeof_client_key_der_2048)));
  21728. p = client_keypub_der_2048;
  21729. AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  21730. sizeof_client_keypub_der_2048)));
  21731. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21732. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  21733. NULL, privKey), 1);
  21734. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  21735. (unsigned int)XSTRLEN(testData)), 1);
  21736. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  21737. AssertIntEQ((int)checkSz, sz);
  21738. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  21739. AssertIntEQ((int)checkSz,sz);
  21740. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21741. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21742. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  21743. NULL, pubKey), 1);
  21744. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  21745. (unsigned int)XSTRLEN(testData)),
  21746. 1);
  21747. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  21748. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21749. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21750. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  21751. NULL, privKey), 1);
  21752. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  21753. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  21754. AssertIntEQ((int)checkSz, sz);
  21755. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  21756. AssertIntEQ((int)checkSz, sz);
  21757. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  21758. (unsigned int)XSTRLEN(testData) - 4), 1);
  21759. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  21760. AssertIntEQ((int)checkSz, sz);
  21761. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21762. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21763. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  21764. NULL, pubKey), 1);
  21765. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  21766. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  21767. (unsigned int)XSTRLEN(testData) - 4),
  21768. 1);
  21769. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  21770. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21771. wolfSSL_EVP_PKEY_free(pubKey);
  21772. wolfSSL_EVP_PKEY_free(privKey);
  21773. printf(resultFmt, passed);
  21774. #endif
  21775. }
  21776. static void test_wolfSSL_EVP_MD_ecc_signing(void)
  21777. {
  21778. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  21779. WOLFSSL_EVP_PKEY* privKey;
  21780. WOLFSSL_EVP_PKEY* pubKey;
  21781. const char testData[] = "Hi There";
  21782. WOLFSSL_EVP_MD_CTX mdCtx;
  21783. size_t checkSz = -1;
  21784. const unsigned char* cp;
  21785. const unsigned char* p;
  21786. unsigned char check[2048/8];
  21787. printf(testingFmt, "wolfSSL_EVP_MD_ecc_signing()");
  21788. cp = ecc_clikey_der_256;
  21789. privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  21790. sizeof_ecc_clikey_der_256);
  21791. AssertNotNull(privKey);
  21792. p = ecc_clikeypub_der_256;
  21793. AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  21794. sizeof_ecc_clikeypub_der_256)));
  21795. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21796. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  21797. NULL, privKey), 1);
  21798. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  21799. (unsigned int)XSTRLEN(testData)), 1);
  21800. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  21801. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  21802. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21803. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21804. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  21805. NULL, pubKey), 1);
  21806. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  21807. (unsigned int)XSTRLEN(testData)),
  21808. 1);
  21809. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  21810. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21811. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21812. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  21813. NULL, privKey), 1);
  21814. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  21815. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  21816. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  21817. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  21818. (unsigned int)XSTRLEN(testData) - 4), 1);
  21819. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  21820. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21821. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21822. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  21823. NULL, pubKey), 1);
  21824. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  21825. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  21826. (unsigned int)XSTRLEN(testData) - 4),
  21827. 1);
  21828. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  21829. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21830. wolfSSL_EVP_PKEY_free(pubKey);
  21831. wolfSSL_EVP_PKEY_free(privKey);
  21832. printf(resultFmt, passed);
  21833. #endif
  21834. }
  21835. static void test_wolfSSL_CTX_add_extra_chain_cert(void)
  21836. {
  21837. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  21838. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  21839. char caFile[] = "./certs/client-ca.pem";
  21840. char clientFile[] = "./certs/client-cert.pem";
  21841. SSL_CTX* ctx;
  21842. X509* x509;
  21843. printf(testingFmt, "wolfSSL_CTX_add_extra_chain_cert()");
  21844. #ifndef NO_WOLFSSL_SERVER
  21845. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  21846. #else
  21847. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  21848. #endif
  21849. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  21850. AssertNotNull(x509);
  21851. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  21852. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  21853. AssertNotNull(x509);
  21854. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  21855. /* additional test of getting EVP_PKEY key size from X509
  21856. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  21857. * allowed with user RSA */
  21858. {
  21859. EVP_PKEY* pkey;
  21860. #if defined(HAVE_ECC)
  21861. X509* ecX509;
  21862. #endif /* HAVE_ECC */
  21863. AssertNotNull(pkey = X509_get_pubkey(x509));
  21864. /* current RSA key is 2048 bit (256 bytes) */
  21865. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  21866. EVP_PKEY_free(pkey);
  21867. #if defined(HAVE_ECC)
  21868. #if defined(USE_CERT_BUFFERS_256)
  21869. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  21870. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  21871. SSL_FILETYPE_ASN1));
  21872. #else
  21873. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(cliEccCertFile,
  21874. SSL_FILETYPE_PEM));
  21875. #endif
  21876. pkey = X509_get_pubkey(ecX509);
  21877. AssertNotNull(pkey);
  21878. /* current ECC key is 256 bit (32 bytes) */
  21879. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  21880. X509_free(ecX509);
  21881. EVP_PKEY_free(pkey);
  21882. #endif /* HAVE_ECC */
  21883. }
  21884. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  21885. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  21886. #ifdef WOLFSSL_ENCRYPTED_KEYS
  21887. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  21888. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  21889. #endif
  21890. SSL_CTX_free(ctx);
  21891. printf(resultFmt, passed);
  21892. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  21893. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  21894. }
  21895. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  21896. static void test_wolfSSL_ERR_peek_last_error_line(void)
  21897. {
  21898. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  21899. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  21900. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  21901. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  21902. tcp_ready ready;
  21903. func_args client_args;
  21904. func_args server_args;
  21905. #ifndef SINGLE_THREADED
  21906. THREAD_TYPE serverThread;
  21907. #endif
  21908. callback_functions client_cb;
  21909. callback_functions server_cb;
  21910. int line = 0;
  21911. int flag = ERR_TXT_STRING;
  21912. const char* file = NULL;
  21913. const char* data = NULL;
  21914. printf(testingFmt, "wolfSSL_ERR_peek_last_error_line()");
  21915. /* create a failed connection and inspect the error */
  21916. #ifdef WOLFSSL_TIRTOS
  21917. fdOpenSession(Task_self());
  21918. #endif
  21919. XMEMSET(&client_args, 0, sizeof(func_args));
  21920. XMEMSET(&server_args, 0, sizeof(func_args));
  21921. StartTCP();
  21922. InitTcpReady(&ready);
  21923. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  21924. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  21925. client_cb.method = wolfTLSv1_1_client_method;
  21926. server_cb.method = wolfTLSv1_2_server_method;
  21927. server_args.signal = &ready;
  21928. server_args.callbacks = &server_cb;
  21929. client_args.signal = &ready;
  21930. client_args.callbacks = &client_cb;
  21931. #ifndef SINGLE_THREADED
  21932. start_thread(test_server_nofail, &server_args, &serverThread);
  21933. wait_tcp_ready(&server_args);
  21934. test_client_nofail(&client_args, NULL);
  21935. join_thread(serverThread);
  21936. #endif
  21937. FreeTcpReady(&ready);
  21938. AssertIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  21939. AssertNotNull(data);
  21940. /* check clearing error state */
  21941. ERR_remove_state(0);
  21942. AssertIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  21943. ERR_peek_last_error_line(NULL, &line);
  21944. AssertIntEQ(line, 0);
  21945. ERR_peek_last_error_line(&file, NULL);
  21946. AssertNull(file);
  21947. /* retry connection to fill error queue */
  21948. XMEMSET(&client_args, 0, sizeof(func_args));
  21949. XMEMSET(&server_args, 0, sizeof(func_args));
  21950. StartTCP();
  21951. InitTcpReady(&ready);
  21952. client_cb.method = wolfTLSv1_1_client_method;
  21953. server_cb.method = wolfTLSv1_2_server_method;
  21954. server_args.signal = &ready;
  21955. server_args.callbacks = &server_cb;
  21956. client_args.signal = &ready;
  21957. client_args.callbacks = &client_cb;
  21958. start_thread(test_server_nofail, &server_args, &serverThread);
  21959. wait_tcp_ready(&server_args);
  21960. test_client_nofail(&client_args, NULL);
  21961. join_thread(serverThread);
  21962. FreeTcpReady(&ready);
  21963. /* check that error code was stored */
  21964. AssertIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  21965. ERR_peek_last_error_line(NULL, &line);
  21966. AssertIntNE(line, 0);
  21967. ERR_peek_last_error_line(&file, NULL);
  21968. AssertNotNull(file);
  21969. #ifdef WOLFSSL_TIRTOS
  21970. fdOpenSession(Task_self());
  21971. #endif
  21972. printf(resultFmt, passed);
  21973. printf("\nTesting error print out\n");
  21974. ERR_print_errors_fp(stdout);
  21975. printf("Done testing print out\n\n");
  21976. fflush(stdout);
  21977. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  21978. !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  21979. }
  21980. #endif
  21981. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  21982. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  21983. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  21984. {
  21985. (void) ok;
  21986. (void) ctx;
  21987. printf("ENTER verify_cb\n");
  21988. return SSL_SUCCESS;
  21989. }
  21990. #endif
  21991. static void test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  21992. {
  21993. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  21994. #ifdef WOLFSSL_SIGNER_DER_CERT
  21995. int cmp;
  21996. #endif
  21997. X509_STORE_CTX* ctx;
  21998. X509_STORE* str;
  21999. X509* x509Ca;
  22000. X509* x509Svr;
  22001. X509* issuer;
  22002. X509_NAME* caName;
  22003. X509_NAME* issuerName;
  22004. printf(testingFmt, "wolfSSL_X509_STORE_CTX_get0_current_issuer()");
  22005. AssertNotNull(ctx = X509_STORE_CTX_new());
  22006. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  22007. AssertNotNull((x509Ca =
  22008. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  22009. AssertIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  22010. AssertNotNull((x509Svr =
  22011. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  22012. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  22013. AssertNull(X509_STORE_CTX_get0_current_issuer(NULL));
  22014. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  22015. AssertNotNull(issuer);
  22016. caName = X509_get_subject_name(x509Ca);
  22017. AssertNotNull(caName);
  22018. issuerName = X509_get_subject_name(issuer);
  22019. #ifdef WOLFSSL_SIGNER_DER_CERT
  22020. AssertNotNull(issuerName);
  22021. cmp = X509_NAME_cmp(caName, issuerName);
  22022. AssertIntEQ(cmp, 0);
  22023. #else
  22024. /* X509_STORE_CTX_get0_current_issuer() returns empty issuer */
  22025. AssertNull(issuerName);
  22026. #endif
  22027. X509_free(issuer);
  22028. X509_STORE_CTX_free(ctx);
  22029. X509_free(x509Svr);
  22030. X509_STORE_free(str);
  22031. X509_free(x509Ca);
  22032. printf(resultFmt, passed);
  22033. #endif
  22034. }
  22035. static void test_wolfSSL_X509_STORE_CTX(void)
  22036. {
  22037. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  22038. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  22039. X509_STORE_CTX* ctx;
  22040. X509_STORE* str;
  22041. X509* x509;
  22042. #ifdef OPENSSL_ALL
  22043. X509* x5092;
  22044. STACK_OF(X509) *sk, *sk2, *sk3;
  22045. #endif
  22046. printf(testingFmt, "wolfSSL_X509_STORE_CTX()");
  22047. AssertNotNull(ctx = X509_STORE_CTX_new());
  22048. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  22049. AssertNotNull((x509 =
  22050. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  22051. AssertIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  22052. #ifdef OPENSSL_ALL
  22053. /* sk_X509_new only in OPENSSL_ALL */
  22054. sk = sk_X509_new();
  22055. AssertNotNull(sk);
  22056. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  22057. #else
  22058. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  22059. #endif
  22060. AssertIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  22061. X509_STORE_CTX_set_error(ctx, -5);
  22062. X509_STORE_CTX_set_error(NULL, -5);
  22063. X509_STORE_CTX_free(ctx);
  22064. X509_STORE_free(str);
  22065. X509_free(x509);
  22066. AssertNotNull(ctx = X509_STORE_CTX_new());
  22067. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  22068. X509_STORE_CTX_free(ctx);
  22069. #ifdef OPENSSL_ALL
  22070. /* test X509_STORE_CTX_get(1)_chain */
  22071. AssertNotNull((x509 = X509_load_certificate_file(svrCertFile,
  22072. SSL_FILETYPE_PEM)));
  22073. AssertNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  22074. SSL_FILETYPE_PEM)));
  22075. AssertNotNull((sk = sk_X509_new()));
  22076. AssertIntEQ(sk_X509_push(sk, x509), 1);
  22077. AssertNotNull((str = X509_STORE_new()));
  22078. AssertNotNull((ctx = X509_STORE_CTX_new()));
  22079. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  22080. AssertNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  22081. AssertNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  22082. AssertIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  22083. AssertNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  22084. AssertNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  22085. AssertIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  22086. X509_STORE_CTX_free(ctx);
  22087. X509_STORE_free(str);
  22088. /* CTX certs not freed yet */
  22089. X509_free(x5092);
  22090. /* sk2 freed as part of X509_STORE_CTX_free(), sk3 is dup so free here */
  22091. sk_X509_free(sk3);
  22092. #endif
  22093. /* test X509_STORE_CTX_get/set_ex_data */
  22094. {
  22095. int i = 0, tmpData = 5;
  22096. void* tmpDataRet;
  22097. AssertNotNull(ctx = X509_STORE_CTX_new());
  22098. #if defined(HAVE_EX_DATA) || defined(FORTRESS)
  22099. for (i = 0; i < MAX_EX_DATA; i++) {
  22100. AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  22101. WOLFSSL_SUCCESS);
  22102. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  22103. AssertNotNull(tmpDataRet);
  22104. AssertIntEQ(tmpData, *(int*)tmpDataRet);
  22105. }
  22106. #else
  22107. AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  22108. WOLFSSL_FAILURE);
  22109. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  22110. AssertNull(tmpDataRet);
  22111. #endif
  22112. X509_STORE_CTX_free(ctx);
  22113. }
  22114. printf(resultFmt, passed);
  22115. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  22116. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  22117. }
  22118. static void test_wolfSSL_X509_STORE_set_flags(void)
  22119. {
  22120. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  22121. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  22122. X509_STORE* store;
  22123. X509* x509;
  22124. printf(testingFmt, "wolfSSL_X509_STORE_set_flags()");
  22125. AssertNotNull((store = wolfSSL_X509_STORE_new()));
  22126. AssertNotNull((x509 =
  22127. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM)));
  22128. AssertIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  22129. #ifdef HAVE_CRL
  22130. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL), WOLFSSL_SUCCESS);
  22131. #else
  22132. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  22133. NOT_COMPILED_IN);
  22134. #endif
  22135. wolfSSL_X509_free(x509);
  22136. wolfSSL_X509_STORE_free(store);
  22137. printf(resultFmt, passed);
  22138. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  22139. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  22140. }
  22141. static void test_wolfSSL_X509_LOOKUP_load_file(void)
  22142. {
  22143. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  22144. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  22145. WOLFSSL_X509_STORE* store;
  22146. WOLFSSL_X509_LOOKUP* lookup;
  22147. printf(testingFmt, "wolfSSL_X509_LOOKUP_load_file()");
  22148. AssertNotNull(store = wolfSSL_X509_STORE_new());
  22149. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  22150. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  22151. X509_FILETYPE_PEM), 1);
  22152. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  22153. X509_FILETYPE_PEM), 1);
  22154. if (store) {
  22155. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  22156. WOLFSSL_FILETYPE_PEM), 1);
  22157. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  22158. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  22159. }
  22160. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  22161. X509_FILETYPE_PEM), 1);
  22162. if (store) {
  22163. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  22164. WOLFSSL_FILETYPE_PEM), 1);
  22165. }
  22166. wolfSSL_X509_STORE_free(store);
  22167. printf(resultFmt, passed);
  22168. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  22169. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  22170. }
  22171. static void test_wolfSSL_X509_STORE_CTX_set_time(void)
  22172. {
  22173. #if defined(OPENSSL_EXTRA)
  22174. WOLFSSL_X509_STORE_CTX* ctx;
  22175. time_t c_time;
  22176. printf(testingFmt, "wolfSSL_X509_set_time()");
  22177. AssertNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  22178. c_time = 365*24*60*60;
  22179. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  22180. AssertTrue(
  22181. (ctx->param->flags & WOLFSSL_USE_CHECK_TIME) == WOLFSSL_USE_CHECK_TIME);
  22182. AssertTrue(ctx->param->check_time == c_time);
  22183. wolfSSL_X509_STORE_CTX_free(ctx);
  22184. printf(resultFmt, passed);
  22185. #endif /* OPENSSL_EXTRA */
  22186. }
  22187. static void test_wolfSSL_get0_param(void)
  22188. {
  22189. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  22190. SSL_CTX* ctx;
  22191. SSL* ssl;
  22192. WOLFSSL_X509_VERIFY_PARAM* pParam;
  22193. printf(testingFmt, "wolfSSL_get0_param()");
  22194. #ifndef NO_WOLFSSL_SERVER
  22195. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  22196. #else
  22197. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  22198. #endif
  22199. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  22200. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  22201. AssertNotNull(ssl = SSL_new(ctx));
  22202. pParam = SSL_get0_param(ssl);
  22203. (void)pParam;
  22204. SSL_free(ssl);
  22205. SSL_CTX_free(ctx);
  22206. printf(resultFmt, passed);
  22207. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  22208. }
  22209. static void test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  22210. {
  22211. #if defined(OPENSSL_EXTRA)
  22212. const char host[] = "www.example.com";
  22213. WOLFSSL_X509_VERIFY_PARAM* pParam;
  22214. printf(testingFmt, "wolfSSL_X509_VERIFY_PARAM_set1_host()");
  22215. AssertNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  22216. sizeof(WOLFSSL_X509_VERIFY_PARAM),
  22217. HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  22218. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  22219. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  22220. AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  22221. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  22222. AssertIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  22223. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  22224. printf(resultFmt, passed);
  22225. #endif /* OPENSSL_EXTRA */
  22226. }
  22227. static void test_wolfSSL_X509_STORE_CTX_get0_store(void)
  22228. {
  22229. #if defined(OPENSSL_EXTRA)
  22230. X509_STORE* store;
  22231. X509_STORE_CTX* ctx;
  22232. X509_STORE_CTX* ctx_no_init;
  22233. printf(testingFmt, "wolfSSL_X509_STORE_CTX_get0_store()");
  22234. AssertNotNull((store = X509_STORE_new()));
  22235. AssertNotNull(ctx = X509_STORE_CTX_new());
  22236. AssertNotNull(ctx_no_init = X509_STORE_CTX_new());
  22237. AssertIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  22238. AssertNull(X509_STORE_CTX_get0_store(NULL));
  22239. /* should return NULL if ctx has not bee initialized */
  22240. AssertNull(X509_STORE_CTX_get0_store(ctx_no_init));
  22241. AssertNotNull(X509_STORE_CTX_get0_store(ctx));
  22242. wolfSSL_X509_STORE_CTX_free(ctx);
  22243. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  22244. X509_STORE_free(store);
  22245. printf(resultFmt, passed);
  22246. #endif /* OPENSSL_EXTRA */
  22247. }
  22248. static void test_wolfSSL_CTX_set_client_CA_list(void)
  22249. {
  22250. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  22251. !defined(NO_WOLFSSL_CLIENT)
  22252. WOLFSSL_CTX* ctx;
  22253. X509_NAME* name = NULL;
  22254. STACK_OF(X509_NAME)* names = NULL;
  22255. STACK_OF(X509_NAME)* ca_list = NULL;
  22256. int i, names_len;
  22257. printf(testingFmt, "wolfSSL_CTX_set_client_CA_list()");
  22258. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  22259. names = SSL_load_client_CA_file(cliCertFile);
  22260. AssertNotNull(names);
  22261. SSL_CTX_set_client_CA_list(ctx,names);
  22262. AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  22263. AssertIntGT((names_len = sk_X509_NAME_num(names)), 0);
  22264. for (i=0; i<names_len; i++) {
  22265. AssertNotNull(name = sk_X509_NAME_value(names, i));
  22266. AssertIntEQ(sk_X509_NAME_find(names, name), i);
  22267. }
  22268. wolfSSL_CTX_free(ctx);
  22269. printf(resultFmt, passed);
  22270. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  22271. }
  22272. static void test_wolfSSL_CTX_add_client_CA(void)
  22273. {
  22274. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  22275. !defined(NO_WOLFSSL_CLIENT)
  22276. WOLFSSL_CTX* ctx;
  22277. WOLFSSL_X509* x509;
  22278. WOLFSSL_X509* x509_a;
  22279. STACK_OF(X509_NAME)* ca_list;
  22280. int ret = 0;
  22281. printf(testingFmt, "wolfSSL_CTX_add_client_CA()");
  22282. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  22283. /* Add client cert */
  22284. x509 = X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
  22285. AssertNotNull(x509);
  22286. ret = SSL_CTX_add_client_CA(ctx, x509);
  22287. AssertIntEQ(ret, SSL_SUCCESS);
  22288. AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  22289. /* Add another client cert */
  22290. AssertNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  22291. SSL_FILETYPE_PEM));
  22292. AssertIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  22293. X509_free(x509);
  22294. X509_free(x509_a);
  22295. SSL_CTX_free(ctx);
  22296. printf(resultFmt, passed);
  22297. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  22298. }
  22299. static void test_wolfSSL_X509_NID(void)
  22300. {
  22301. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  22302. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  22303. int sigType;
  22304. int nameSz;
  22305. X509* cert;
  22306. EVP_PKEY* pubKeyTmp;
  22307. X509_NAME* name;
  22308. char commonName[80];
  22309. char countryName[80];
  22310. char localityName[80];
  22311. char stateName[80];
  22312. char orgName[80];
  22313. char orgUnit[80];
  22314. printf(testingFmt, "wolfSSL_X509_NID()");
  22315. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  22316. /* convert cert from DER to internal WOLFSSL_X509 struct */
  22317. AssertNotNull(cert = wolfSSL_X509_d2i(&cert, client_cert_der_2048,
  22318. sizeof_client_cert_der_2048));
  22319. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  22320. /* extract PUBLIC KEY from cert */
  22321. AssertNotNull(pubKeyTmp = X509_get_pubkey(cert));
  22322. /* extract signatureType */
  22323. AssertIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  22324. /* extract subjectName info */
  22325. AssertNotNull(name = X509_get_subject_name(cert));
  22326. AssertIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  22327. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  22328. NULL, 0)), 0);
  22329. AssertIntEQ(nameSz, 15);
  22330. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  22331. commonName, sizeof(commonName))), 0);
  22332. AssertIntEQ(nameSz, 15);
  22333. AssertIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  22334. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  22335. commonName, 9)), 0);
  22336. AssertIntEQ(nameSz, 8);
  22337. AssertIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  22338. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  22339. countryName, sizeof(countryName))), 0);
  22340. AssertIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  22341. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  22342. localityName, sizeof(localityName))), 0);
  22343. AssertIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  22344. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_stateOrProvinceName,
  22345. stateName, sizeof(stateName))), 0);
  22346. AssertIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  22347. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  22348. orgName, sizeof(orgName))), 0);
  22349. AssertIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  22350. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationalUnitName,
  22351. orgUnit, sizeof(orgUnit))), 0);
  22352. AssertIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  22353. EVP_PKEY_free(pubKeyTmp);
  22354. X509_free(cert);
  22355. printf(resultFmt, passed);
  22356. #endif
  22357. }
  22358. static void test_wolfSSL_CTX_set_srp_username(void)
  22359. {
  22360. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  22361. && !defined(NO_SHA256) && !defined(WC_NO_RNG)
  22362. WOLFSSL_CTX* ctx;
  22363. const char *username = "TESTUSER";
  22364. const char *password = "TESTPASSWORD";
  22365. int r;
  22366. printf(testingFmt, "wolfSSL_CTX_set_srp_username()");
  22367. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  22368. AssertNotNull(ctx);
  22369. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  22370. AssertIntEQ(r,SSL_SUCCESS);
  22371. wolfSSL_CTX_free(ctx);
  22372. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  22373. AssertNotNull(ctx);
  22374. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  22375. AssertIntEQ(r,SSL_SUCCESS);
  22376. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  22377. AssertIntEQ(r,SSL_SUCCESS);
  22378. wolfSSL_CTX_free(ctx);
  22379. printf(resultFmt, passed);
  22380. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  22381. /* && !NO_SHA256 && !WC_NO_RNG */
  22382. }
  22383. static void test_wolfSSL_CTX_set_srp_password(void)
  22384. {
  22385. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  22386. && !defined(NO_SHA256) && !defined(WC_NO_RNG)
  22387. WOLFSSL_CTX* ctx;
  22388. const char *username = "TESTUSER";
  22389. const char *password = "TESTPASSWORD";
  22390. int r;
  22391. printf(testingFmt, "wolfSSL_CTX_set_srp_password()");
  22392. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  22393. AssertNotNull(ctx);
  22394. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  22395. AssertIntEQ(r,SSL_SUCCESS);
  22396. wolfSSL_CTX_free(ctx);
  22397. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  22398. AssertNotNull(ctx);
  22399. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  22400. AssertIntEQ(r,SSL_SUCCESS);
  22401. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  22402. AssertIntEQ(r,SSL_SUCCESS);
  22403. wolfSSL_CTX_free(ctx);
  22404. printf(resultFmt, passed);
  22405. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  22406. /* && !NO_SHA256 && !WC_NO_RNG */
  22407. }
  22408. static void test_wolfSSL_X509_STORE(void)
  22409. {
  22410. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  22411. X509_STORE *store;
  22412. #ifdef HAVE_CRL
  22413. X509_STORE_CTX *storeCtx;
  22414. X509_CRL *crl;
  22415. X509 *ca, *cert;
  22416. const char crlPem[] = "./certs/crl/crl.revoked";
  22417. const char srvCert[] = "./certs/server-revoked-cert.pem";
  22418. const char caCert[] = "./certs/ca-cert.pem";
  22419. XFILE fp;
  22420. printf(testingFmt, "test_wolfSSL_X509_STORE");
  22421. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  22422. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  22423. SSL_FILETYPE_PEM)));
  22424. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  22425. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  22426. SSL_FILETYPE_PEM)));
  22427. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  22428. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  22429. AssertIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  22430. X509_STORE_free(store);
  22431. X509_STORE_CTX_free(storeCtx);
  22432. X509_free(cert);
  22433. X509_free(ca);
  22434. /* should fail to verify now after adding in CRL */
  22435. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  22436. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  22437. SSL_FILETYPE_PEM)));
  22438. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  22439. fp = XFOPEN(crlPem, "rb");
  22440. AssertTrue((fp != XBADFILE));
  22441. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  22442. NULL, NULL));
  22443. XFCLOSE(fp);
  22444. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  22445. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  22446. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  22447. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  22448. SSL_FILETYPE_PEM)));
  22449. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  22450. AssertIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  22451. AssertIntEQ(X509_STORE_CTX_get_error(storeCtx), CRL_CERT_REVOKED);
  22452. X509_CRL_free(crl);
  22453. X509_STORE_free(store);
  22454. X509_STORE_CTX_free(storeCtx);
  22455. X509_free(cert);
  22456. X509_free(ca);
  22457. #endif /* HAVE_CRL */
  22458. #ifndef WOLFCRYPT_ONLY
  22459. {
  22460. SSL_CTX* ctx;
  22461. #ifndef NO_WOLFSSL_SERVER
  22462. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  22463. #else
  22464. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  22465. #endif
  22466. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  22467. SSL_CTX_set_cert_store(ctx, store);
  22468. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  22469. SSL_CTX_set_cert_store(ctx, store);
  22470. SSL_CTX_free(ctx);
  22471. }
  22472. #endif
  22473. printf(resultFmt, passed);
  22474. #endif
  22475. return;
  22476. }
  22477. static void test_wolfSSL_X509_STORE_load_locations(void)
  22478. {
  22479. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_FILESYSTEM)
  22480. SSL_CTX *ctx;
  22481. X509_STORE *store;
  22482. const char ca_file[] = "./certs/ca-cert.pem";
  22483. const char client_pem_file[] = "./certs/client-cert.pem";
  22484. const char client_der_file[] = "./certs/client-cert.der";
  22485. const char ecc_file[] = "./certs/ecc-key.pem";
  22486. const char certs_path[] = "./certs/";
  22487. const char bad_path[] = "./bad-path/";
  22488. #ifdef HAVE_CRL
  22489. const char crl_path[] = "./certs/crl/";
  22490. const char crl_file[] = "./certs/crl/crl.pem";
  22491. #endif
  22492. printf(testingFmt, "wolfSSL_X509_STORE_load_locations");
  22493. #ifndef NO_WOLFSSL_SERVER
  22494. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  22495. #else
  22496. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  22497. #endif
  22498. AssertNotNull(store = SSL_CTX_get_cert_store(ctx));
  22499. AssertIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL), WOLFSSL_SUCCESS);
  22500. /* Test bad arguments */
  22501. AssertIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL), WOLFSSL_FAILURE);
  22502. AssertIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  22503. AssertIntEQ(X509_STORE_load_locations(store, client_der_file, NULL), WOLFSSL_FAILURE);
  22504. AssertIntEQ(X509_STORE_load_locations(store, ecc_file, NULL), WOLFSSL_FAILURE);
  22505. AssertIntEQ(X509_STORE_load_locations(store, NULL, bad_path), WOLFSSL_FAILURE);
  22506. #ifdef HAVE_CRL
  22507. /* Test with CRL */
  22508. AssertIntEQ(X509_STORE_load_locations(store, crl_file, NULL), WOLFSSL_SUCCESS);
  22509. AssertIntEQ(X509_STORE_load_locations(store, NULL, crl_path), WOLFSSL_SUCCESS);
  22510. #endif
  22511. /* Test with CA */
  22512. AssertIntEQ(X509_STORE_load_locations(store, ca_file, NULL), WOLFSSL_SUCCESS);
  22513. /* Test with client_cert and certs path */
  22514. AssertIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL), WOLFSSL_SUCCESS);
  22515. AssertIntEQ(X509_STORE_load_locations(store, NULL, certs_path), WOLFSSL_SUCCESS);
  22516. SSL_CTX_free(ctx);
  22517. printf(resultFmt, passed);
  22518. #endif
  22519. }
  22520. static void test_wolfSSL_BN(void)
  22521. {
  22522. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  22523. BIGNUM* a;
  22524. BIGNUM* b;
  22525. BIGNUM* c;
  22526. BIGNUM* d;
  22527. ASN1_INTEGER* ai;
  22528. unsigned char value[1];
  22529. printf(testingFmt, "wolfSSL_BN()");
  22530. AssertNotNull(b = BN_new());
  22531. AssertNotNull(c = BN_new());
  22532. AssertNotNull(d = BN_new());
  22533. value[0] = 0x03;
  22534. ai = ASN1_INTEGER_new();
  22535. AssertNotNull(ai);
  22536. /* at the moment hard setting since no set function */
  22537. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  22538. ai->data[1] = 0x01; /* length of integer */
  22539. ai->data[2] = value[0];
  22540. AssertNotNull(a = ASN1_INTEGER_to_BN(ai, NULL));
  22541. ASN1_INTEGER_free(ai);
  22542. value[0] = 0x02;
  22543. AssertNotNull(BN_bin2bn(value, sizeof(value), b));
  22544. value[0] = 0x05;
  22545. AssertNotNull(BN_bin2bn(value, sizeof(value), c));
  22546. /* a^b mod c = */
  22547. AssertIntEQ(BN_mod_exp(d, NULL, b, c, NULL), WOLFSSL_FAILURE);
  22548. AssertIntEQ(BN_mod_exp(d, a, b, c, NULL), WOLFSSL_SUCCESS);
  22549. /* check result 3^2 mod 5 */
  22550. value[0] = 0;
  22551. AssertIntEQ(BN_bn2bin(d, value), sizeof(value));
  22552. AssertIntEQ((int)(value[0]), 4);
  22553. /* a*b mod c = */
  22554. AssertIntEQ(BN_mod_mul(d, NULL, b, c, NULL), SSL_FAILURE);
  22555. AssertIntEQ(BN_mod_mul(d, a, b, c, NULL), SSL_SUCCESS);
  22556. /* check result 3*2 mod 5 */
  22557. value[0] = 0;
  22558. AssertIntEQ(BN_bn2bin(d, value), sizeof(value));
  22559. AssertIntEQ((int)(value[0]), 1);
  22560. /* BN_mod_inverse test */
  22561. value[0] = 0;
  22562. BIGNUM *r = BN_new();
  22563. BIGNUM *val = BN_mod_inverse(r,b,c,NULL);
  22564. AssertIntEQ(BN_bn2bin(r, value), 1);
  22565. AssertIntEQ((int)(value[0] & 0x03), 3);
  22566. BN_free(val);
  22567. AssertIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  22568. AssertIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  22569. AssertIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  22570. AssertIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  22571. AssertIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  22572. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  22573. {
  22574. char* ret;
  22575. AssertNotNull(ret = BN_bn2dec(c));
  22576. AssertIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  22577. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  22578. }
  22579. #endif
  22580. AssertIntEQ(BN_get_word(c), 4);
  22581. BN_free(a);
  22582. BN_free(b);
  22583. BN_free(c);
  22584. BN_clear_free(d);
  22585. /* check that converting NULL and the null string returns an error */
  22586. a = NULL;
  22587. AssertIntLE(BN_hex2bn(&a, NULL), 0);
  22588. AssertIntLE(BN_hex2bn(&a, ""), 0);
  22589. AssertNull(a);
  22590. /* check that getting a string and a bin of the same number are equal,
  22591. * and that the comparison works EQ, LT and GT */
  22592. AssertIntGT(BN_hex2bn(&a, "03"), 0);
  22593. value[0] = 0x03;
  22594. AssertNotNull(b = BN_new());
  22595. AssertNotNull(BN_bin2bn(value, sizeof(value), b));
  22596. value[0] = 0x04;
  22597. AssertNotNull(c = BN_new());
  22598. AssertNotNull(BN_bin2bn(value, sizeof(value), c));
  22599. AssertIntEQ(BN_cmp(a, b), 0);
  22600. AssertIntLT(BN_cmp(a, c), 0);
  22601. AssertIntGT(BN_cmp(c, b), 0);
  22602. AssertIntEQ(BN_set_word(a, 0), 1);
  22603. AssertIntEQ(BN_is_zero(a), 1);
  22604. AssertIntEQ(BN_set_bit(a, 0x45), 1);
  22605. AssertIntEQ(BN_is_zero(a), 0);
  22606. AssertIntEQ(BN_is_bit_set(a, 0x45), 1);
  22607. AssertIntEQ(BN_clear_bit(a, 0x45), 1);
  22608. AssertIntEQ(BN_is_bit_set(a, 0x45), 0);
  22609. AssertIntEQ(BN_is_zero(a), 1);
  22610. BN_free(a);
  22611. BN_free(b);
  22612. BN_free(c);
  22613. #if defined(USE_FAST_MATH) && !defined(HAVE_WOLF_BIGINT)
  22614. {
  22615. BIGNUM *ap;
  22616. BIGNUM bv;
  22617. BIGNUM cv;
  22618. BIGNUM dv;
  22619. AssertNotNull(ap = BN_new());
  22620. BN_init(&bv);
  22621. BN_init(&cv);
  22622. BN_init(&dv);
  22623. value[0] = 0x3;
  22624. AssertNotNull(BN_bin2bn(value, sizeof(value), ap));
  22625. value[0] = 0x02;
  22626. AssertNotNull(BN_bin2bn(value, sizeof(value), &bv));
  22627. value[0] = 0x05;
  22628. AssertNotNull(BN_bin2bn(value, sizeof(value), &cv));
  22629. /* a^b mod c = */
  22630. AssertIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  22631. AssertIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  22632. /* check result 3^2 mod 5 */
  22633. value[0] = 0;
  22634. AssertIntEQ(BN_bn2bin(&dv, value), sizeof(value));
  22635. AssertIntEQ((int)(value[0]), 4);
  22636. /* a*b mod c = */
  22637. AssertIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  22638. AssertIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  22639. /* check result 3*2 mod 5 */
  22640. value[0] = 0;
  22641. AssertIntEQ(BN_bn2bin(&dv, value), sizeof(value));
  22642. AssertIntEQ((int)(value[0]), 1);
  22643. BN_free(ap);
  22644. }
  22645. #endif
  22646. printf(resultFmt, passed);
  22647. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  22648. }
  22649. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  22650. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  22651. #define TEST_ARG 0x1234
  22652. static void msg_cb(int write_p, int version, int content_type,
  22653. const void *buf, size_t len, SSL *ssl, void *arg)
  22654. {
  22655. (void)write_p;
  22656. (void)version;
  22657. (void)content_type;
  22658. (void)buf;
  22659. (void)len;
  22660. (void)ssl;
  22661. AssertTrue(arg == (void*)TEST_ARG);
  22662. }
  22663. #endif
  22664. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  22665. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  22666. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
  22667. !defined(NO_WOLFSSL_SERVER)
  22668. #ifndef SINGLE_THREADED
  22669. #if defined(SESSION_CERTS)
  22670. #include "wolfssl/internal.h"
  22671. #endif
  22672. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  22673. {
  22674. (void) ctx;
  22675. (void) ssl;
  22676. #ifdef WOLFSSL_QT
  22677. STACK_OF(X509)* sk;
  22678. X509* x509;
  22679. int i, num;
  22680. BIO* bio;
  22681. #endif
  22682. printf("\n===== msgcb called ====\n");
  22683. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  22684. AssertTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  22685. AssertIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 1);
  22686. #endif
  22687. #ifdef WOLFSSL_QT
  22688. bio = BIO_new(BIO_s_file());
  22689. BIO_set_fp(bio, stdout, BIO_NOCLOSE);
  22690. sk = SSL_get_peer_cert_chain(ssl);
  22691. AssertNotNull(sk);
  22692. if (!sk) {
  22693. BIO_free(bio);
  22694. return SSL_FAILURE;
  22695. }
  22696. num = sk_X509_num(sk);
  22697. AssertTrue(num > 0);
  22698. for (i = 0; i < num; i++) {
  22699. x509 = sk_X509_value(sk,i);
  22700. AssertNotNull(x509);
  22701. if (!x509)
  22702. break;
  22703. printf("Certificate at index [%d] = :\n",i);
  22704. X509_print(bio,x509);
  22705. printf("\n\n");
  22706. }
  22707. BIO_free(bio);
  22708. #endif
  22709. return SSL_SUCCESS;
  22710. }
  22711. #endif
  22712. #endif
  22713. static void test_wolfSSL_msgCb(void)
  22714. {
  22715. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  22716. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  22717. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
  22718. !defined(NO_WOLFSSL_SERVER)
  22719. tcp_ready ready;
  22720. func_args client_args;
  22721. func_args server_args;
  22722. #ifndef SINGLE_THREADED
  22723. THREAD_TYPE serverThread;
  22724. #endif
  22725. callback_functions client_cb;
  22726. callback_functions server_cb;
  22727. printf(testingFmt, "test_wolfSSL_msgCb");
  22728. /* create a failed connection and inspect the error */
  22729. #ifdef WOLFSSL_TIRTOS
  22730. fdOpenSession(Task_self());
  22731. #endif
  22732. XMEMSET(&client_args, 0, sizeof(func_args));
  22733. XMEMSET(&server_args, 0, sizeof(func_args));
  22734. StartTCP();
  22735. InitTcpReady(&ready);
  22736. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  22737. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  22738. #ifndef WOLFSSL_NO_TLS12
  22739. client_cb.method = wolfTLSv1_2_client_method;
  22740. server_cb.method = wolfTLSv1_2_server_method;
  22741. #else
  22742. client_cb.method = wolfTLSv1_3_client_method;
  22743. server_cb.method = wolfTLSv1_3_server_method;
  22744. #endif
  22745. server_args.signal = &ready;
  22746. server_args.callbacks = &server_cb;
  22747. client_args.signal = &ready;
  22748. client_args.callbacks = &client_cb;
  22749. client_args.return_code = TEST_FAIL;
  22750. #ifndef SINGLE_THREADED
  22751. start_thread(test_server_nofail, &server_args, &serverThread);
  22752. wait_tcp_ready(&server_args);
  22753. test_client_nofail(&client_args, (void *)msgCb);
  22754. join_thread(serverThread);
  22755. #endif
  22756. FreeTcpReady(&ready);
  22757. #ifndef SINGLE_THREADED
  22758. AssertTrue(client_args.return_code);
  22759. AssertTrue(server_args.return_code);
  22760. #endif
  22761. #ifdef WOLFSSL_TIRTOS
  22762. fdOpenSession(Task_self());
  22763. #endif
  22764. printf(resultFmt, passed);
  22765. #endif
  22766. }
  22767. static void test_wolfSSL_either_side(void)
  22768. {
  22769. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  22770. !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  22771. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  22772. tcp_ready ready;
  22773. func_args client_args;
  22774. func_args server_args;
  22775. #ifndef SINGLE_THREADED
  22776. THREAD_TYPE serverThread;
  22777. #endif
  22778. callback_functions client_cb;
  22779. callback_functions server_cb;
  22780. printf(testingFmt, "test_wolfSSL_either_side");
  22781. /* create a failed connection and inspect the error */
  22782. #ifdef WOLFSSL_TIRTOS
  22783. fdOpenSession(Task_self());
  22784. #endif
  22785. XMEMSET(&client_args, 0, sizeof(func_args));
  22786. XMEMSET(&server_args, 0, sizeof(func_args));
  22787. StartTCP();
  22788. InitTcpReady(&ready);
  22789. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  22790. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  22791. /* Use same CTX for both client and server */
  22792. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  22793. AssertNotNull(client_cb.ctx);
  22794. server_cb.ctx = client_cb.ctx;
  22795. /* we are responsible for free'ing WOLFSSL_CTX */
  22796. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  22797. server_args.signal = &ready;
  22798. server_args.callbacks = &server_cb;
  22799. client_args.signal = &ready;
  22800. client_args.callbacks = &client_cb;
  22801. client_args.return_code = TEST_FAIL;
  22802. #ifndef SINGLE_THREADED
  22803. start_thread(test_server_nofail, &server_args, &serverThread);
  22804. wait_tcp_ready(&server_args);
  22805. test_client_nofail(&client_args, NULL);
  22806. join_thread(serverThread);
  22807. #endif
  22808. wolfSSL_CTX_free(client_cb.ctx);
  22809. FreeTcpReady(&ready);
  22810. #ifndef SINGLE_THREADED
  22811. AssertTrue(client_args.return_code);
  22812. AssertTrue(server_args.return_code);
  22813. #endif
  22814. #ifdef WOLFSSL_TIRTOS
  22815. fdOpenSession(Task_self());
  22816. #endif
  22817. printf(resultFmt, passed);
  22818. #endif
  22819. }
  22820. static void test_wolfSSL_DTLS_either_side(void)
  22821. {
  22822. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  22823. !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  22824. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  22825. defined(WOLFSSL_DTLS)
  22826. tcp_ready ready;
  22827. func_args client_args;
  22828. func_args server_args;
  22829. #ifndef SINGLE_THREADED
  22830. THREAD_TYPE serverThread;
  22831. #endif
  22832. callback_functions client_cb;
  22833. callback_functions server_cb;
  22834. printf(testingFmt, "test_wolfSSL_DTLS_either_side");
  22835. /* create a failed connection and inspect the error */
  22836. #ifdef WOLFSSL_TIRTOS
  22837. fdOpenSession(Task_self());
  22838. #endif
  22839. XMEMSET(&client_args, 0, sizeof(func_args));
  22840. XMEMSET(&server_args, 0, sizeof(func_args));
  22841. StartTCP();
  22842. InitTcpReady(&ready);
  22843. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  22844. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  22845. /* Use same CTX for both client and server */
  22846. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  22847. AssertNotNull(client_cb.ctx);
  22848. server_cb.ctx = client_cb.ctx;
  22849. /* we are responsible for free'ing WOLFSSL_CTX */
  22850. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  22851. server_args.signal = &ready;
  22852. server_args.callbacks = &server_cb;
  22853. client_args.signal = &ready;
  22854. client_args.callbacks = &client_cb;
  22855. client_args.return_code = TEST_FAIL;
  22856. #ifndef SINGLE_THREADED
  22857. start_thread(test_server_nofail, &server_args, &serverThread);
  22858. wait_tcp_ready(&server_args);
  22859. test_client_nofail(&client_args, NULL);
  22860. join_thread(serverThread);
  22861. #endif
  22862. wolfSSL_CTX_free(client_cb.ctx);
  22863. FreeTcpReady(&ready);
  22864. #ifndef SINGLE_THREADED
  22865. AssertTrue(client_args.return_code);
  22866. AssertTrue(server_args.return_code);
  22867. #endif
  22868. #ifdef WOLFSSL_TIRTOS
  22869. fdOpenSession(Task_self());
  22870. #endif
  22871. printf(resultFmt, passed);
  22872. #endif
  22873. }
  22874. static void test_generate_cookie(void)
  22875. {
  22876. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA)
  22877. SSL_CTX* ctx;
  22878. SSL* ssl;
  22879. byte buf[FOURK_BUF] = {0};
  22880. printf(testingFmt, "test_generate_cookie");
  22881. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  22882. AssertNotNull(ssl = SSL_new(ctx));
  22883. /* Test unconnected */
  22884. AssertIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  22885. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  22886. wolfSSL_SetCookieCtx(ssl, ctx);
  22887. AssertNotNull(wolfSSL_GetCookieCtx(ssl));
  22888. AssertNull(wolfSSL_GetCookieCtx(NULL));
  22889. SSL_free(ssl);
  22890. SSL_CTX_free(ctx);
  22891. printf(resultFmt, passed);
  22892. #endif
  22893. }
  22894. static void test_wolfSSL_set_options(void)
  22895. {
  22896. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  22897. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  22898. SSL* ssl;
  22899. SSL_CTX* ctx;
  22900. char appData[] = "extra msg";
  22901. unsigned char protos[] = {
  22902. 7, 't', 'l', 's', '/', '1', '.', '2',
  22903. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  22904. };
  22905. unsigned int len = sizeof(protos);
  22906. void *arg = (void *)TEST_ARG;
  22907. printf(testingFmt, "wolfSSL_set_options()");
  22908. #ifndef NO_WOLFSSL_SERVER
  22909. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  22910. #else
  22911. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  22912. #endif
  22913. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  22914. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  22915. AssertTrue(SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1) == SSL_OP_NO_TLSv1);
  22916. AssertTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  22917. AssertIntGT((int)SSL_CTX_set_options(ctx, (SSL_OP_COOKIE_EXCHANGE |
  22918. SSL_OP_NO_SSLv2)), 0);
  22919. AssertTrue((SSL_CTX_set_options(ctx, SSL_OP_COOKIE_EXCHANGE) &
  22920. SSL_OP_COOKIE_EXCHANGE) == SSL_OP_COOKIE_EXCHANGE);
  22921. AssertTrue((SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_2) &
  22922. SSL_OP_NO_TLSv1_2) == SSL_OP_NO_TLSv1_2);
  22923. AssertTrue((SSL_CTX_set_options(ctx, SSL_OP_NO_COMPRESSION) &
  22924. SSL_OP_NO_COMPRESSION) == SSL_OP_NO_COMPRESSION);
  22925. AssertNull((SSL_CTX_clear_options(ctx, SSL_OP_NO_COMPRESSION) &
  22926. SSL_OP_NO_COMPRESSION));
  22927. SSL_CTX_free(ctx);
  22928. #ifndef NO_WOLFSSL_SERVER
  22929. ctx = SSL_CTX_new(wolfSSLv23_server_method());
  22930. AssertNotNull(ctx);
  22931. #else
  22932. ctx = SSL_CTX_new(wolfSSLv23_client_method());
  22933. AssertNotNull(ctx);
  22934. #endif
  22935. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  22936. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  22937. AssertNotNull(ssl = SSL_new(ctx));
  22938. #if defined(HAVE_EX_DATA) || defined(FORTRESS)
  22939. AssertIntEQ(SSL_set_app_data(ssl, (void*)appData), SSL_SUCCESS);
  22940. AssertNotNull(SSL_get_app_data((const WOLFSSL*)ssl));
  22941. if (ssl) {
  22942. AssertIntEQ(XMEMCMP(SSL_get_app_data((const WOLFSSL*)ssl),
  22943. appData, sizeof(appData)), 0);
  22944. }
  22945. #else
  22946. AssertIntEQ(SSL_set_app_data(ssl, (void*)appData), SSL_FAILURE);
  22947. AssertNull(SSL_get_app_data((const WOLFSSL*)ssl));
  22948. #endif
  22949. AssertTrue(SSL_set_options(ssl, SSL_OP_NO_TLSv1) == SSL_OP_NO_TLSv1);
  22950. AssertTrue(SSL_get_options(ssl) == SSL_OP_NO_TLSv1);
  22951. AssertIntGT((int)SSL_set_options(ssl, (SSL_OP_COOKIE_EXCHANGE |
  22952. WOLFSSL_OP_NO_SSLv2)), 0);
  22953. AssertTrue((SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE) &
  22954. SSL_OP_COOKIE_EXCHANGE) == SSL_OP_COOKIE_EXCHANGE);
  22955. AssertTrue((SSL_set_options(ssl, SSL_OP_NO_TLSv1_2) &
  22956. SSL_OP_NO_TLSv1_2) == SSL_OP_NO_TLSv1_2);
  22957. AssertTrue((SSL_set_options(ssl, SSL_OP_NO_COMPRESSION) &
  22958. SSL_OP_NO_COMPRESSION) == SSL_OP_NO_COMPRESSION);
  22959. AssertNull((SSL_clear_options(ssl, SSL_OP_NO_COMPRESSION) &
  22960. SSL_OP_NO_COMPRESSION));
  22961. AssertTrue(SSL_set_msg_callback(ssl, msg_cb) == SSL_SUCCESS);
  22962. SSL_set_msg_callback_arg(ssl, arg);
  22963. AssertTrue(SSL_CTX_set_alpn_protos(ctx, protos, len) == SSL_SUCCESS);
  22964. SSL_free(ssl);
  22965. SSL_CTX_free(ctx);
  22966. printf(resultFmt, passed);
  22967. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  22968. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  22969. }
  22970. static void test_wolfSSL_sk_SSL_CIPHER(void)
  22971. {
  22972. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  22973. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  22974. SSL* ssl;
  22975. SSL_CTX* ctx;
  22976. STACK_OF(SSL_CIPHER) *sk, *dup;
  22977. printf(testingFmt, "wolfSSL_sk_SSL_CIPHER_*()");
  22978. #ifndef NO_WOLFSSL_SERVER
  22979. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  22980. #else
  22981. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  22982. #endif
  22983. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  22984. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  22985. AssertNotNull(ssl = SSL_new(ctx));
  22986. AssertNotNull(sk = SSL_get_ciphers(ssl));
  22987. AssertNotNull(dup = sk_SSL_CIPHER_dup(sk));
  22988. AssertIntGT(sk_SSL_CIPHER_num(sk), 0);
  22989. AssertIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dup));
  22990. /* error case because connection has not been established yet */
  22991. AssertIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  22992. sk_SSL_CIPHER_free(dup);
  22993. /* sk is pointer to internal struct that should be free'd in SSL_free */
  22994. SSL_free(ssl);
  22995. SSL_CTX_free(ctx);
  22996. printf(resultFmt, passed);
  22997. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  22998. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  22999. }
  23000. /* Testing wolfSSL_set_tlsext_status_type function.
  23001. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  23002. */
  23003. static void test_wolfSSL_set_tlsext_status_type(void){
  23004. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  23005. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  23006. SSL* ssl;
  23007. SSL_CTX* ctx;
  23008. printf(testingFmt, "wolfSSL_set_tlsext_status_type()");
  23009. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  23010. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  23011. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  23012. AssertNotNull(ssl = SSL_new(ctx));
  23013. AssertTrue(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp)
  23014. == SSL_SUCCESS);
  23015. SSL_free(ssl);
  23016. SSL_CTX_free(ctx);
  23017. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  23018. }
  23019. static void test_wolfSSL_PEM_read_bio(void)
  23020. {
  23021. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  23022. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  23023. byte buff[6000];
  23024. XFILE f;
  23025. int bytes;
  23026. X509* x509;
  23027. BIO* bio = NULL;
  23028. BUF_MEM* buf;
  23029. printf(testingFmt, "wolfSSL_PEM_read_bio()");
  23030. f = XFOPEN(cliCertFile, "rb");
  23031. AssertTrue((f != XBADFILE));
  23032. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  23033. XFCLOSE(f);
  23034. AssertNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  23035. AssertNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  23036. AssertNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  23037. AssertIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  23038. AssertIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  23039. AssertIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  23040. AssertIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  23041. BIO_free(bio);
  23042. BUF_MEM_free(buf);
  23043. X509_free(x509);
  23044. printf(resultFmt, passed);
  23045. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  23046. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  23047. }
  23048. #if defined(OPENSSL_EXTRA)
  23049. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  23050. long argl, long ret)
  23051. {
  23052. (void)bio;
  23053. (void)cmd;
  23054. (void)argp;
  23055. (void)argi;
  23056. (void)argl;
  23057. return ret;
  23058. }
  23059. #endif
  23060. static void test_wolfSSL_BIO(void)
  23061. {
  23062. #if defined(OPENSSL_EXTRA)
  23063. const unsigned char* p;
  23064. byte buff[20];
  23065. BIO* bio1;
  23066. BIO* bio2;
  23067. BIO* bio3;
  23068. char* bufPt;
  23069. int i;
  23070. printf(testingFmt, "wolfSSL_BIO()");
  23071. for (i = 0; i < 20; i++) {
  23072. buff[i] = i;
  23073. }
  23074. /* Creating and testing type BIO_s_bio */
  23075. AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
  23076. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  23077. AssertNotNull(bio3 = BIO_new(BIO_s_bio()));
  23078. /* read/write before set up */
  23079. AssertIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  23080. AssertIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  23081. AssertIntEQ(BIO_set_nbio(bio1, 1), 1);
  23082. AssertIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  23083. AssertIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  23084. AssertIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  23085. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  23086. XMEMCPY(bufPt, buff, 10);
  23087. AssertIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  23088. /* write buffer full */
  23089. AssertIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  23090. AssertIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  23091. AssertIntEQ((int)BIO_ctrl_pending(bio1), 0);
  23092. /* write the other direction with pair */
  23093. AssertIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  23094. XMEMCPY(bufPt, buff, 8);
  23095. AssertIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  23096. /* try read */
  23097. AssertIntEQ((int)BIO_ctrl_pending(bio1), 8);
  23098. AssertIntEQ((int)BIO_ctrl_pending(bio2), 20);
  23099. /* try read using ctrl function */
  23100. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  23101. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  23102. AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  23103. AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  23104. AssertIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  23105. for (i = 0; i < 20; i++) {
  23106. AssertIntEQ((int)bufPt[i], i);
  23107. }
  23108. AssertIntEQ(BIO_nread(bio2, &bufPt, 1), WOLFSSL_BIO_ERROR);
  23109. AssertIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  23110. for (i = 0; i < 8; i++) {
  23111. AssertIntEQ((int)bufPt[i], i);
  23112. }
  23113. AssertIntEQ(BIO_nread(bio1, &bufPt, 1), WOLFSSL_BIO_ERROR);
  23114. AssertIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  23115. /* new pair */
  23116. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  23117. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  23118. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  23119. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  23120. AssertIntEQ(BIO_nread(bio3, &bufPt, 10), WOLFSSL_BIO_ERROR);
  23121. /* test wrap around... */
  23122. AssertIntEQ(BIO_reset(bio1), 0);
  23123. AssertIntEQ(BIO_reset(bio3), 0);
  23124. /* fill write buffer, read only small amount then write again */
  23125. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  23126. XMEMCPY(bufPt, buff, 20);
  23127. AssertIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  23128. for (i = 0; i < 4; i++) {
  23129. AssertIntEQ(bufPt[i], i);
  23130. }
  23131. /* try writing over read index */
  23132. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  23133. XMEMSET(bufPt, 0, 4);
  23134. AssertIntEQ((int)BIO_ctrl_pending(bio3), 20);
  23135. /* read and write 0 bytes */
  23136. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  23137. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  23138. /* should read only to end of write buffer then need to read again */
  23139. AssertIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  23140. for (i = 0; i < 16; i++) {
  23141. AssertIntEQ(bufPt[i], buff[4 + i]);
  23142. }
  23143. AssertIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  23144. AssertIntEQ(BIO_nread0(bio3, &bufPt), 4);
  23145. for (i = 0; i < 4; i++) {
  23146. AssertIntEQ(bufPt[i], 0);
  23147. }
  23148. /* read index should not have advanced with nread0 */
  23149. AssertIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  23150. for (i = 0; i < 4; i++) {
  23151. AssertIntEQ(bufPt[i], 0);
  23152. }
  23153. /* write and fill up buffer checking reset of index state */
  23154. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  23155. XMEMCPY(bufPt, buff, 20);
  23156. /* test reset on data in bio1 write buffer */
  23157. AssertIntEQ(BIO_reset(bio1), 0);
  23158. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  23159. AssertIntEQ(BIO_nread(bio3, &bufPt, 3), WOLFSSL_BIO_ERROR);
  23160. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  23161. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  23162. AssertNotNull(p);
  23163. XMEMCPY(bufPt, buff, 20);
  23164. AssertIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  23165. for (i = 0; i < 6; i++) {
  23166. AssertIntEQ(bufPt[i], i);
  23167. }
  23168. /* test case of writing twice with offset read index */
  23169. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  23170. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  23171. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  23172. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  23173. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  23174. AssertIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  23175. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  23176. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  23177. BIO_free(bio1);
  23178. BIO_free(bio3);
  23179. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  23180. {
  23181. BIO* bioA = NULL;
  23182. BIO* bioB = NULL;
  23183. AssertIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  23184. AssertIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  23185. BIO_free(bioA);
  23186. bioA = NULL;
  23187. BIO_free(bioB);
  23188. bioB = NULL;
  23189. }
  23190. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  23191. /* BIOs with file pointers */
  23192. #if !defined(NO_FILESYSTEM)
  23193. {
  23194. XFILE f1;
  23195. XFILE f2;
  23196. BIO* f_bio1;
  23197. BIO* f_bio2;
  23198. unsigned char cert[300];
  23199. char testFile[] = "tests/bio_write_test.txt";
  23200. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  23201. AssertNotNull(f_bio1 = BIO_new(BIO_s_file()));
  23202. AssertNotNull(f_bio2 = BIO_new(BIO_s_file()));
  23203. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  23204. AssertIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  23205. f1 = XFOPEN(svrCertFile, "rwb");
  23206. AssertTrue((f1 != XBADFILE));
  23207. AssertIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  23208. AssertIntEQ(BIO_write_filename(f_bio2, testFile),
  23209. WOLFSSL_SUCCESS);
  23210. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  23211. AssertIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  23212. AssertIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  23213. AssertIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  23214. AssertIntEQ(BIO_reset(f_bio2), 0);
  23215. AssertIntEQ(BIO_seek(f_bio2, 4), 0);
  23216. BIO_free(f_bio1);
  23217. BIO_free(f_bio2);
  23218. AssertNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  23219. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  23220. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  23221. BIO_free(f_bio1);
  23222. }
  23223. #endif /* !defined(NO_FILESYSTEM) */
  23224. /* BIO info callback */
  23225. {
  23226. const char* testArg = "test";
  23227. BIO* cb_bio;
  23228. AssertNotNull(cb_bio = BIO_new(BIO_s_mem()));
  23229. BIO_set_callback(cb_bio, bioCallback);
  23230. AssertNotNull(BIO_get_callback(cb_bio));
  23231. BIO_set_callback(cb_bio, NULL);
  23232. AssertNull(BIO_get_callback(cb_bio));
  23233. BIO_set_callback_arg(cb_bio, (char*)testArg);
  23234. AssertStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  23235. AssertNull(BIO_get_callback_arg(NULL));
  23236. BIO_free(cb_bio);
  23237. }
  23238. /* BIO_vfree */
  23239. AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
  23240. BIO_vfree(NULL);
  23241. BIO_vfree(bio1);
  23242. printf(resultFmt, passed);
  23243. #endif
  23244. }
  23245. static void test_wolfSSL_ASN1_STRING(void)
  23246. {
  23247. #if defined(OPENSSL_EXTRA)
  23248. ASN1_STRING* str = NULL;
  23249. const char data[] = "hello wolfSSL";
  23250. printf(testingFmt, "wolfSSL_ASN1_STRING()");
  23251. AssertNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  23252. AssertIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  23253. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  23254. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  23255. AssertIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  23256. ASN1_STRING_free(str);
  23257. printf(resultFmt, passed);
  23258. #endif
  23259. }
  23260. static void test_wolfSSL_ASN1_BIT_STRING(void)
  23261. {
  23262. #ifdef OPENSSL_ALL
  23263. ASN1_BIT_STRING* str;
  23264. printf(testingFmt, "test_wolfSSL_ASN1_BIT_STRING()");
  23265. AssertNotNull(str = ASN1_BIT_STRING_new());
  23266. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  23267. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  23268. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  23269. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  23270. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  23271. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  23272. ASN1_BIT_STRING_free(str);
  23273. printf(resultFmt, passed);
  23274. #endif
  23275. }
  23276. static void test_wolfSSL_DES_ecb_encrypt(void)
  23277. {
  23278. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  23279. WOLFSSL_DES_cblock input1,input2,output1,output2,back1,back2;
  23280. WOLFSSL_DES_key_schedule key;
  23281. printf(testingFmt, "wolfSSL_DES_ecb_encrypt()");
  23282. XMEMCPY(key,"12345678",sizeof(WOLFSSL_DES_key_schedule));
  23283. XMEMCPY(input1, "Iamhuman",sizeof(WOLFSSL_DES_cblock));
  23284. XMEMCPY(input2, "Whoisit?",sizeof(WOLFSSL_DES_cblock));
  23285. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  23286. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  23287. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  23288. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  23289. /* Encrypt messages */
  23290. wolfSSL_DES_ecb_encrypt(&input1,&output1,&key,DES_ENCRYPT);
  23291. wolfSSL_DES_ecb_encrypt(&input2,&output2,&key,DES_ENCRYPT);
  23292. /* Decrypt messages */
  23293. int ret1 = 0;
  23294. int ret2 = 0;
  23295. wolfSSL_DES_ecb_encrypt(&output1,&back1,&key,DES_DECRYPT);
  23296. ret1 = XMEMCMP((unsigned char *) back1,(unsigned char *) input1,sizeof(WOLFSSL_DES_cblock));
  23297. AssertIntEQ(ret1,0);
  23298. wolfSSL_DES_ecb_encrypt(&output2,&back2,&key,DES_DECRYPT);
  23299. ret2 = XMEMCMP((unsigned char *) back2,(unsigned char *) input2,sizeof(WOLFSSL_DES_cblock));
  23300. AssertIntEQ(ret2,0);
  23301. printf(resultFmt, passed);
  23302. #endif
  23303. }
  23304. static void test_wolfSSL_ASN1_TIME_adj(void)
  23305. {
  23306. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  23307. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  23308. const int year = 365*24*60*60;
  23309. const int day = 24*60*60;
  23310. const int hour = 60*60;
  23311. const int mini = 60;
  23312. const byte asn_utc_time = ASN_UTC_TIME;
  23313. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  23314. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  23315. #endif
  23316. WOLFSSL_ASN1_TIME *asn_time, *s;
  23317. int offset_day;
  23318. long offset_sec;
  23319. char date_str[CTC_DATE_SIZE + 1];
  23320. time_t t;
  23321. printf(testingFmt, "wolfSSL_ASN1_TIME_adj()");
  23322. s = wolfSSL_ASN1_TIME_new();
  23323. /* UTC notation test */
  23324. /* 2000/2/15 20:30:00 */
  23325. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  23326. offset_day = 7;
  23327. offset_sec = 45 * mini;
  23328. /* offset_sec = -45 * min;*/
  23329. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  23330. AssertTrue(asn_time->type == asn_utc_time);
  23331. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  23332. date_str[CTC_DATE_SIZE] = '\0';
  23333. AssertIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  23334. /* negative offset */
  23335. offset_sec = -45 * mini;
  23336. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  23337. AssertTrue(asn_time->type == asn_utc_time);
  23338. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  23339. date_str[CTC_DATE_SIZE] = '\0';
  23340. AssertIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  23341. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  23342. XMEMSET(date_str, 0, sizeof(date_str));
  23343. /* Generalized time will overflow time_t if not long */
  23344. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  23345. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  23346. DYNAMIC_TYPE_OPENSSL);
  23347. /* GeneralizedTime notation test */
  23348. /* 2055/03/01 09:00:00 */
  23349. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  23350. offset_day = 12;
  23351. offset_sec = 10 * mini;
  23352. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  23353. AssertTrue(asn_time->type == asn_gen_time);
  23354. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  23355. date_str[CTC_DATE_SIZE] = '\0';
  23356. AssertIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  23357. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  23358. XMEMSET(date_str, 0, sizeof(date_str));
  23359. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  23360. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  23361. s = NULL;
  23362. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  23363. offset_day = 7;
  23364. offset_sec = 45 * mini;
  23365. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  23366. AssertTrue(asn_time->type == asn_utc_time);
  23367. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  23368. date_str[CTC_DATE_SIZE] = '\0';
  23369. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  23370. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  23371. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, offset_sec);
  23372. AssertTrue(asn_time->type == asn_utc_time);
  23373. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  23374. date_str[CTC_DATE_SIZE] = '\0';
  23375. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  23376. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  23377. printf(resultFmt, passed);
  23378. #endif
  23379. }
  23380. static void test_wolfSSL_X509_cmp_time(void)
  23381. {
  23382. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  23383. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  23384. WOLFSSL_ASN1_TIME asn_time;
  23385. time_t t;
  23386. printf(testingFmt, "wolfSSL_X509_cmp_time()");
  23387. AssertIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  23388. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  23389. AssertIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  23390. asn_time.type = ASN_UTC_TIME;
  23391. asn_time.length = ASN_UTC_TIME_SIZE;
  23392. XMEMCPY(&asn_time.data, "000222211515Z", 13);
  23393. AssertIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  23394. printf(resultFmt, passed);
  23395. #endif
  23396. }
  23397. static void test_wolfSSL_X509_time_adj(void)
  23398. {
  23399. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  23400. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  23401. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  23402. !defined(NO_ASN_TIME)
  23403. X509* x509;
  23404. time_t t, not_before, not_after;
  23405. printf(testingFmt, "wolfSSL_X509_time_adj()");
  23406. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  23407. client_cert_der_2048, sizeof_client_cert_der_2048,
  23408. WOLFSSL_FILETYPE_ASN1));
  23409. t = 0;
  23410. not_before = XTIME(0);
  23411. not_after = XTIME(0) + (60 * 24 * 30); /* 30 days after */
  23412. AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  23413. AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  23414. X509_free(x509);
  23415. printf(resultFmt, passed);
  23416. #endif
  23417. }
  23418. static void test_wolfSSL_X509(void)
  23419. {
  23420. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)\
  23421. && !defined(NO_RSA)
  23422. X509* x509;
  23423. BIO* bio;
  23424. X509_STORE_CTX* ctx;
  23425. X509_STORE* store;
  23426. char der[] = "certs/ca-cert.der";
  23427. XFILE fp;
  23428. printf(testingFmt, "wolfSSL_X509()");
  23429. AssertNotNull(x509 = X509_new());
  23430. X509_free(x509);
  23431. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
  23432. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  23433. AssertIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  23434. AssertNotNull(ctx = X509_STORE_CTX_new());
  23435. AssertIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  23436. AssertNotNull(store = X509_STORE_new());
  23437. AssertIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  23438. AssertIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  23439. AssertIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  23440. X509_STORE_CTX_free(ctx);
  23441. X509_STORE_free(store);
  23442. X509_free(x509);
  23443. BIO_free(bio);
  23444. /** d2i_X509_fp test **/
  23445. fp = XFOPEN(der, "rb");
  23446. AssertTrue((fp != XBADFILE));
  23447. AssertNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  23448. AssertNotNull(x509);
  23449. X509_free(x509);
  23450. XFCLOSE(fp);
  23451. fp = XFOPEN(der, "rb");
  23452. AssertTrue((fp != XBADFILE));
  23453. AssertNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  23454. AssertNotNull(x509);
  23455. X509_free(x509);
  23456. XFCLOSE(fp);
  23457. /* X509_up_ref test */
  23458. AssertIntEQ(X509_up_ref(NULL), 0);
  23459. AssertNotNull(x509 = X509_new()); /* refCount = 1 */
  23460. AssertIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  23461. AssertIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  23462. X509_free(x509); /* refCount = 2 */
  23463. X509_free(x509); /* refCount = 1 */
  23464. X509_free(x509); /* refCount = 0, free */
  23465. printf(resultFmt, passed);
  23466. #endif
  23467. }
  23468. static void test_wolfSSL_X509_get_ext_count(void)
  23469. {
  23470. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  23471. int ret = 0;
  23472. WOLFSSL_X509* x509;
  23473. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  23474. FILE* f;
  23475. printf(testingFmt, "wolfSSL_X509_get_ext_count()");
  23476. /* NULL parameter check */
  23477. AssertIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  23478. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  23479. SSL_FILETYPE_PEM));
  23480. AssertIntEQ(X509_get_ext_count(x509), 5);
  23481. wolfSSL_X509_free(x509);
  23482. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  23483. SSL_FILETYPE_PEM));
  23484. AssertIntEQ(X509_get_ext_count(x509), 5);
  23485. wolfSSL_X509_free(x509);
  23486. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  23487. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  23488. fclose(f);
  23489. printf(testingFmt, "wolfSSL_X509_get_ext_count() valid input");
  23490. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  23491. printf(resultFmt, ret == 4 ? passed : failed);
  23492. printf(testingFmt, "wolfSSL_X509_get_ext_count() NULL argument");
  23493. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  23494. printf(resultFmt, ret == WOLFSSL_FAILURE ? passed : failed);
  23495. wolfSSL_X509_free(x509);
  23496. printf(resultFmt, passed);
  23497. #endif
  23498. }
  23499. static void test_wolfSSL_X509_sign(void)
  23500. {
  23501. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  23502. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  23503. int ret;
  23504. X509_NAME *name;
  23505. X509 *x509;
  23506. EVP_PKEY *pub;
  23507. EVP_PKEY *priv;
  23508. #if defined(USE_CERT_BUFFERS_1024)
  23509. const unsigned char* rsaPriv = client_key_der_1024;
  23510. const unsigned char* rsaPub = client_keypub_der_1024;
  23511. long clientKeySz = (long)sizeof_client_key_der_1024;
  23512. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  23513. #elif defined(USE_CERT_BUFFERS_2048)
  23514. const unsigned char* rsaPriv = client_key_der_2048;
  23515. const unsigned char* rsaPub = client_keypub_der_2048;
  23516. long clientKeySz = (long)sizeof_client_key_der_2048;
  23517. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  23518. #endif
  23519. byte sn[16];
  23520. int snSz = sizeof(sn);
  23521. printf(testingFmt, "wolfSSL_X509_sign\n");
  23522. /* Set X509_NAME fields */
  23523. AssertNotNull(name = X509_NAME_new());
  23524. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  23525. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  23526. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  23527. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  23528. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  23529. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  23530. /* Get private and public keys */
  23531. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  23532. clientKeySz));
  23533. AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  23534. AssertNotNull(x509 = X509_new());
  23535. /* Set version 3 */
  23536. AssertIntNE(X509_set_version(x509, 2L), 0);
  23537. /* Set subject name, add pubkey, and sign certificate */
  23538. AssertIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  23539. AssertIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  23540. #ifdef WOLFSSL_ALT_NAMES
  23541. /* Add some subject alt names */
  23542. AssertIntNE(wolfSSL_X509_add_altname(NULL,
  23543. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  23544. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  23545. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  23546. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  23547. "sphygmomanometer",
  23548. ASN_DNS_TYPE), SSL_SUCCESS);
  23549. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  23550. "supercalifragilisticexpialidocious",
  23551. ASN_DNS_TYPE), SSL_SUCCESS);
  23552. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  23553. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  23554. ASN_DNS_TYPE), SSL_SUCCESS);
  23555. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  23556. {
  23557. unsigned char ip_type[] = {127,0,0,1};
  23558. AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip_type,
  23559. sizeof(ip_type), ASN_IP_TYPE), SSL_SUCCESS);
  23560. }
  23561. #endif
  23562. #endif /* WOLFSSL_ALT_NAMES */
  23563. /* test valid sign case */
  23564. ret = X509_sign(x509, priv, EVP_sha256());
  23565. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  23566. AssertIntEQ(X509_get_ext_count(x509), 1);
  23567. #endif
  23568. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  23569. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  23570. #endif
  23571. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  23572. WOLFSSL_SUCCESS);
  23573. #if 0
  23574. /* example for writing to file */
  23575. XFILE tmpFile = XFOPEN("./signed.der", "wb");
  23576. if (tmpFile) {
  23577. int derSz = 0;
  23578. const byte* der = wolfSSL_X509_get_der(x509, &derSz);
  23579. XFWRITE(der, 1, derSz, tmpFile);
  23580. }
  23581. XFCLOSE(tmpFile);
  23582. #endif
  23583. /* Variation in size depends on ASN.1 encoding when MSB is set */
  23584. #ifndef WOLFSSL_ALT_NAMES
  23585. /* Valid case - size should be 798-797 with 16 byte serial number */
  23586. AssertTrue((ret == 781 + snSz) || (ret == 782 + snSz));
  23587. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  23588. /* Valid case - size should be 935-936 with 16 byte serial number */
  23589. AssertTrue((ret == 919 + snSz) || (ret == 920 + snSz));
  23590. #else
  23591. /* Valid case - size should be 926-927 with 16 byte serial number */
  23592. AssertTrue((ret == 910 + snSz) || (ret == 911 + snSz));
  23593. #endif
  23594. /* Test invalid parameters */
  23595. AssertIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  23596. AssertIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  23597. AssertIntEQ(X509_sign(x509, priv, NULL), 0);
  23598. /* test invalid version number */
  23599. #if defined(OPENSSL_ALL)
  23600. AssertIntNE(X509_set_version(x509, 6L), 0);
  23601. AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  23602. /* uses ParseCert which fails on bad version number */
  23603. AssertIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  23604. #endif
  23605. X509_NAME_free(name);
  23606. EVP_PKEY_free(priv);
  23607. EVP_PKEY_free(pub);
  23608. X509_free(x509);
  23609. printf(resultFmt, passed);
  23610. #endif
  23611. }
  23612. static void test_wolfSSL_X509_get0_tbs_sigalg(void)
  23613. {
  23614. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  23615. X509* x509 = NULL;
  23616. const X509_ALGOR* alg;
  23617. printf(testingFmt, "wolfSSL_X509_get0_tbs_sigalg");
  23618. AssertNotNull(x509 = X509_new());
  23619. AssertNull(alg = X509_get0_tbs_sigalg(NULL));
  23620. AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
  23621. X509_free(x509);
  23622. printf(resultFmt, passed);
  23623. #endif
  23624. }
  23625. static void test_wolfSSL_X509_ALGOR_get0(void)
  23626. {
  23627. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_SHA256)
  23628. X509* x509 = NULL;
  23629. const ASN1_OBJECT* obj = NULL;
  23630. const X509_ALGOR* alg;
  23631. int pptype = 0;
  23632. const void *ppval = NULL;
  23633. printf(testingFmt, "wolfSSL_X509_ALGOR_get0");
  23634. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  23635. SSL_FILETYPE_PEM));
  23636. AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
  23637. /* Invalid case */
  23638. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  23639. AssertNull(obj);
  23640. /* Valid case */
  23641. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  23642. AssertNotNull(obj);
  23643. AssertNotNull(ppval);
  23644. AssertIntNE(pptype, 0);
  23645. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  23646. AssertIntEQ(OBJ_obj2nid(obj), CTC_SHA256wRSA);
  23647. X509_free(x509);
  23648. printf(resultFmt, passed);
  23649. #endif
  23650. }
  23651. static void test_wolfSSL_X509_VERIFY_PARAM(void)
  23652. {
  23653. #if defined(OPENSSL_EXTRA)
  23654. WOLFSSL_X509_VERIFY_PARAM *param;
  23655. int ret;
  23656. char testIPv4[] = "127.0.0.1";
  23657. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  23658. printf(testingFmt, "wolfSSL_X509()");
  23659. /* Initializer function is not ported */
  23660. /* param = wolfSSL_X509_VERIFY_PARAM_new(); */
  23661. param = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  23662. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL);
  23663. AssertNotNull(param);
  23664. XMEMSET(param, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM ));
  23665. wolfSSL_X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  23666. wolfSSL_X509_VERIFY_PARAM_set_hostflags(param, 0x01);
  23667. AssertIntEQ(0x01, param->hostFlags);
  23668. ret = wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4);
  23669. AssertIntEQ(0, ret);
  23670. ret = wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(param, testIPv4);
  23671. AssertIntEQ(1, ret);
  23672. AssertIntEQ(0, XSTRNCMP(param->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  23673. ret = wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(param, NULL);
  23674. AssertIntEQ(1, ret);
  23675. ret = wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(param, testIPv6);
  23676. AssertIntEQ(1, ret);
  23677. AssertIntEQ(0, XSTRNCMP(param->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  23678. XFREE(param, NULL, DYNAMIC_TYPE_OPENSSL);
  23679. printf(resultFmt, passed);
  23680. #endif
  23681. }
  23682. static void test_wolfSSL_X509_get_X509_PUBKEY(void)
  23683. {
  23684. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  23685. X509* x509 = NULL;
  23686. X509_PUBKEY* pubKey;
  23687. printf(testingFmt, "wolfSSL_X509_get_X509_PUBKEY");
  23688. AssertNotNull(x509 = X509_new());
  23689. AssertNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  23690. AssertNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  23691. X509_free(x509);
  23692. printf(resultFmt, passed);
  23693. #endif
  23694. }
  23695. static void test_wolfSSL_X509_PUBKEY(void)
  23696. {
  23697. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_SHA256)
  23698. X509* x509 = NULL;
  23699. ASN1_OBJECT* obj = NULL;
  23700. X509_PUBKEY* pubKey;
  23701. X509_PUBKEY* pubKey2;
  23702. EVP_PKEY* evpKey;
  23703. const unsigned char *pk;
  23704. int ppklen;
  23705. WOLFSSL_X509_ALGOR *pa;
  23706. printf(testingFmt, "wolfSSL_X509_get_X509_PUBKEY");
  23707. AssertNotNull(x509 = X509_load_certificate_file(cliCertFile,
  23708. SSL_FILETYPE_PEM));
  23709. AssertNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  23710. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  23711. AssertNotNull(pk);
  23712. AssertNotNull(pa);
  23713. AssertNotNull(pubKey);
  23714. AssertIntGT(ppklen, 0);
  23715. AssertIntEQ(OBJ_obj2nid(obj), RSAk);
  23716. AssertNotNull(evpKey = X509_PUBKEY_get(pubKey));
  23717. AssertNotNull(pubKey2 = X509_PUBKEY_new());
  23718. AssertIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  23719. X509_PUBKEY_free(pubKey2);
  23720. X509_free(x509);
  23721. printf(resultFmt, passed);
  23722. #endif
  23723. }
  23724. static void test_wolfSSL_RAND(void)
  23725. {
  23726. #if defined(OPENSSL_EXTRA)
  23727. byte seed[16];
  23728. printf(testingFmt, "wolfSSL_RAND()");
  23729. RAND_seed(seed, sizeof(seed));
  23730. AssertIntEQ(RAND_poll(), 1);
  23731. RAND_cleanup();
  23732. AssertIntEQ(RAND_egd(NULL), -1);
  23733. #ifndef NO_FILESYSTEM
  23734. {
  23735. char fname[100];
  23736. AssertNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  23737. AssertIntEQ(RAND_write_file(NULL), 0);
  23738. }
  23739. #endif
  23740. printf(resultFmt, passed);
  23741. #endif
  23742. }
  23743. static void test_wolfSSL_BUF(void)
  23744. {
  23745. #if defined(OPENSSL_EXTRA)
  23746. BUF_MEM* buf;
  23747. AssertNotNull(buf = BUF_MEM_new());
  23748. AssertIntEQ(BUF_MEM_grow(buf, 10), 10);
  23749. AssertIntEQ(BUF_MEM_grow(buf, -1), 0);
  23750. BUF_MEM_free(buf);
  23751. #endif /* OPENSSL_EXTRA */
  23752. }
  23753. static void test_wolfSSL_pseudo_rand(void)
  23754. {
  23755. #if defined(OPENSSL_EXTRA)
  23756. BIGNUM* bn;
  23757. unsigned char bin[8];
  23758. int i;
  23759. printf(testingFmt, "wolfSSL_pseudo_rand()");
  23760. /* BN_pseudo_rand returns 1 on success 0 on failure
  23761. * int BN_pseudo_rand(BIGNUM* bn, int bits, int top, int bottom) */
  23762. for (i = 0; i < 10; i++) {
  23763. AssertNotNull(bn = BN_new());
  23764. AssertIntEQ(BN_pseudo_rand(bn, 8, 0, 0), SSL_SUCCESS);
  23765. AssertIntGT(BN_bn2bin(bn, bin),0);
  23766. AssertIntEQ((bin[0] & 0x80), 0x80); /* top bit should be set */
  23767. BN_free(bn);
  23768. }
  23769. for (i = 0; i < 10; i++) {
  23770. AssertNotNull(bn = BN_new());
  23771. AssertIntEQ(BN_pseudo_rand(bn, 8, 1, 1), SSL_SUCCESS);
  23772. AssertIntGT(BN_bn2bin(bn, bin),0);
  23773. AssertIntEQ((bin[0] & 0xc1), 0xc1); /* top bit should be set */
  23774. BN_free(bn);
  23775. }
  23776. printf(resultFmt, passed);
  23777. #endif
  23778. }
  23779. static void test_wolfSSL_PKCS8_Compat(void)
  23780. {
  23781. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  23782. PKCS8_PRIV_KEY_INFO* pt;
  23783. BIO* bio;
  23784. XFILE f;
  23785. int bytes;
  23786. char pkcs8_buffer[512];
  23787. printf(testingFmt, "wolfSSL_pkcs8()");
  23788. /* file from wolfssl/certs/ directory */
  23789. f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb");
  23790. AssertTrue(f != XBADFILE);
  23791. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)), 0);
  23792. XFCLOSE(f);
  23793. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  23794. AssertNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  23795. BIO_free(bio);
  23796. PKCS8_PRIV_KEY_INFO_free(pt);
  23797. printf(resultFmt, passed);
  23798. #endif
  23799. }
  23800. static void test_wolfSSL_PKCS8_d2i(void)
  23801. {
  23802. #ifndef HAVE_FIPS
  23803. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  23804. * requires a 12 byte password in FIPS mode. This test ends up
  23805. * trying to use an 8 byte password. */
  23806. #ifdef OPENSSL_ALL
  23807. WOLFSSL_EVP_PKEY* pkey = NULL;
  23808. #ifndef NO_FILESYSTEM
  23809. unsigned char pkcs8_buffer[2048];
  23810. const unsigned char* p;
  23811. int bytes;
  23812. XFILE file;
  23813. BIO* bio;
  23814. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  23815. #endif
  23816. #ifndef NO_RSA
  23817. #ifndef NO_FILESYSTEM
  23818. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  23819. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  23820. #ifndef NO_DES3
  23821. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  23822. #endif
  23823. #endif
  23824. #ifdef USE_CERT_BUFFERS_1024
  23825. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  23826. int rsaSz = sizeof_server_key_der_1024;
  23827. #else
  23828. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  23829. int rsaSz = sizeof_server_key_der_2048;
  23830. #endif
  23831. #endif
  23832. #ifdef HAVE_ECC
  23833. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  23834. int ecSz = sizeof_ecc_key_der_256;
  23835. #ifndef NO_FILESYSTEM
  23836. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  23837. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  23838. #ifndef NO_DES3
  23839. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  23840. #endif
  23841. #endif
  23842. #endif
  23843. #ifndef NO_FILESYSTEM
  23844. (void)pkcs8_buffer;
  23845. (void)p;
  23846. (void)bytes;
  23847. (void)file;
  23848. (void)bio;
  23849. #endif
  23850. #ifndef NO_RSA
  23851. /* Try to auto-detect normal RSA private key */
  23852. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  23853. wolfSSL_EVP_PKEY_free(pkey);
  23854. #endif
  23855. #ifdef HAVE_ECC
  23856. /* Try to auto-detect normal EC private key */
  23857. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  23858. wolfSSL_EVP_PKEY_free(pkey);
  23859. #endif
  23860. #ifndef NO_FILESYSTEM
  23861. #ifndef NO_RSA
  23862. /* Get DER encoded RSA PKCS#8 data. */
  23863. file = XFOPEN(rsaDerPkcs8File, "rb");
  23864. AssertTrue(file != XBADFILE);
  23865. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  23866. file)), 0);
  23867. XFCLOSE(file);
  23868. p = pkcs8_buffer;
  23869. /* Try to decode - auto-detect key type. */
  23870. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  23871. /* Get PEM encoded RSA PKCS#8 data. */
  23872. file = XFOPEN(rsaPemPkcs8File, "rb");
  23873. AssertTrue(file != XBADFILE);
  23874. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  23875. file)), 0);
  23876. XFCLOSE(file);
  23877. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  23878. /* Write PKCS#8 PEM to BIO. */
  23879. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  23880. NULL), bytes);
  23881. /* Compare file and written data */
  23882. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &p), bytes);
  23883. AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  23884. BIO_free(bio);
  23885. #ifndef NO_DES3
  23886. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  23887. /* Write Encrypted PKCS#8 PEM to BIO. */
  23888. bytes = 1834;
  23889. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  23890. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  23891. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  23892. (void*)"yassl123"));
  23893. wolfSSL_EVP_PKEY_free(evpPkey);
  23894. BIO_free(bio);
  23895. #endif
  23896. wolfSSL_EVP_PKEY_free(pkey);
  23897. /* PKCS#8 encrypted RSA key */
  23898. #ifndef NO_DES3
  23899. file = XFOPEN(rsaDerPkcs8EncFile, "rb");
  23900. AssertTrue(file != XBADFILE);
  23901. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  23902. file)), 0);
  23903. XFCLOSE(file);
  23904. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  23905. AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  23906. (void*)"yassl123"));
  23907. wolfSSL_EVP_PKEY_free(pkey);
  23908. BIO_free(bio);
  23909. #endif
  23910. #endif
  23911. #ifdef HAVE_ECC
  23912. /* PKCS#8 encode EC key */
  23913. file = XFOPEN(ecDerPkcs8File, "rb");
  23914. AssertTrue(file != XBADFILE);
  23915. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  23916. file)), 0);
  23917. XFCLOSE(file);
  23918. p = pkcs8_buffer;
  23919. /* Try to decode - auto-detect key type. */
  23920. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  23921. /* Get PEM encoded RSA PKCS#8 data. */
  23922. file = XFOPEN(ecPemPkcs8File, "rb");
  23923. AssertTrue(file != XBADFILE);
  23924. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  23925. file)), 0);
  23926. XFCLOSE(file);
  23927. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  23928. /* Write PKCS#8 PEM to BIO. */
  23929. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  23930. NULL), bytes);
  23931. /* Compare file and written data */
  23932. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &p), bytes);
  23933. AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  23934. BIO_free(bio);
  23935. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  23936. /* Write Encrypted PKCS#8 PEM to BIO. */
  23937. bytes = 379;
  23938. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  23939. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  23940. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  23941. (void*)"yassl123"));
  23942. wolfSSL_EVP_PKEY_free(evpPkey);
  23943. BIO_free(bio);
  23944. wolfSSL_EVP_PKEY_free(pkey);
  23945. /* PKCS#8 encrypted EC key */
  23946. #ifndef NO_DES3
  23947. file = XFOPEN(ecDerPkcs8EncFile, "rb");
  23948. AssertTrue(file != XBADFILE);
  23949. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  23950. file)), 0);
  23951. XFCLOSE(file);
  23952. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  23953. AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  23954. (void*)"yassl123"));
  23955. wolfSSL_EVP_PKEY_free(pkey);
  23956. BIO_free(bio);
  23957. #endif
  23958. #endif
  23959. #endif
  23960. printf(resultFmt, passed);
  23961. #endif
  23962. #endif /* HAVE_FIPS */
  23963. }
  23964. static void test_wolfSSL_ERR_put_error(void)
  23965. {
  23966. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  23967. defined(DEBUG_WOLFSSL)
  23968. const char* file;
  23969. int line;
  23970. printf(testingFmt, "wolfSSL_ERR_put_error()");
  23971. ERR_clear_error(); /* clear out any error nodes */
  23972. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  23973. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  23974. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  23975. AssertIntEQ(ERR_get_error_line(&file, &line), 1);
  23976. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  23977. AssertIntEQ(ERR_get_error_line(&file, &line), 2);
  23978. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  23979. AssertIntEQ(ERR_get_error_line(&file, &line), 3);
  23980. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  23981. AssertIntEQ(ERR_get_error_line(&file, &line), 4);
  23982. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  23983. AssertIntEQ(ERR_get_error_line(&file, &line), 5);
  23984. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  23985. AssertIntEQ(ERR_get_error_line(&file, &line), 6);
  23986. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  23987. AssertIntEQ(ERR_get_error_line(&file, &line), 7);
  23988. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  23989. AssertIntEQ(ERR_get_error_line(&file, &line), 8);
  23990. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  23991. AssertIntEQ(ERR_get_error_line(&file, &line), 9);
  23992. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  23993. AssertIntEQ(ERR_get_error_line(&file, &line), 10);
  23994. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  23995. AssertIntEQ(ERR_get_error_line(&file, &line), 11);
  23996. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  23997. AssertIntEQ(ERR_get_error_line(&file, &line), 12);
  23998. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  23999. AssertIntEQ(ERR_get_error_line(&file, &line), 13);
  24000. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  24001. AssertIntEQ(ERR_get_error_line(&file, &line), 14);
  24002. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  24003. AssertIntEQ(ERR_get_error_line(&file, &line), 15);
  24004. /* try reading past end of error queue */
  24005. file = NULL;
  24006. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  24007. AssertNull(file);
  24008. AssertIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  24009. PEMerr(4,4);
  24010. AssertIntEQ(ERR_get_error(), 4);
  24011. /* Empty and free up all error nodes */
  24012. ERR_clear_error();
  24013. /* Verify all nodes are cleared */
  24014. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  24015. ERR_clear_error();
  24016. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  24017. printf(resultFmt, passed);
  24018. #endif
  24019. }
  24020. static void test_wolfSSL_ERR_print_errors(void)
  24021. {
  24022. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  24023. defined(DEBUG_WOLFSSL)
  24024. BIO* bio;
  24025. char buf[1024];
  24026. printf(testingFmt, "wolfSSL_ERR_print_errors()");
  24027. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  24028. ERR_clear_error(); /* clear out any error nodes */
  24029. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  24030. /* Choosing -295 as an unused errno between MIN_CODE_E < x < WC_LAST_E. */
  24031. ERR_put_error(0,SYS_F_BIND, -295, "asn.c", 100);
  24032. ERR_print_errors(bio);
  24033. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  24034. AssertIntEQ(XSTRNCMP("error:173:wolfSSL library:Bad function argument:ssl.c:0",
  24035. buf, 55), 0);
  24036. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  24037. AssertIntEQ(XSTRNCMP("error:295:wolfSSL library:unknown error number:asn.c:100",
  24038. buf, 56), 0);
  24039. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  24040. AssertIntEQ(ERR_get_error_line(NULL, NULL), 0);
  24041. BIO_free(bio);
  24042. printf(resultFmt, passed);
  24043. #endif
  24044. }
  24045. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  24046. defined(DEBUG_WOLFSSL)
  24047. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  24048. {
  24049. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  24050. return 0;
  24051. }
  24052. #endif
  24053. static void test_wolfSSL_ERR_print_errors_cb(void)
  24054. {
  24055. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  24056. defined(DEBUG_WOLFSSL)
  24057. BIO* bio;
  24058. char buf[1024];
  24059. printf(testingFmt, "wolfSSL_ERR_print_errors_cb()");
  24060. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  24061. ERR_clear_error(); /* clear out any error nodes */
  24062. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  24063. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  24064. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  24065. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  24066. AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  24067. buf, 53), 0);
  24068. AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 275 line:100 file:asn.c",
  24069. buf + 53, 55), 0);
  24070. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  24071. BIO_free(bio);
  24072. printf(resultFmt, passed);
  24073. #endif
  24074. }
  24075. /*
  24076. * Testing WOLFSSL_ERROR_MSG
  24077. */
  24078. static int test_WOLFSSL_ERROR_MSG (void)
  24079. {
  24080. int ret = 0;
  24081. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  24082. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  24083. const char* msg = "Everyone gets Friday off.";
  24084. printf(testingFmt, "WOLFSSL_ERROR_MSG()");
  24085. WOLFSSL_ERROR_MSG(msg);
  24086. printf(resultFmt, ret == 0 ? passed : failed);
  24087. #endif
  24088. return ret;
  24089. }/*End test_WOLFSSL_ERROR_MSG*/
  24090. /*
  24091. * Testing wc_ERR_remove_state
  24092. */
  24093. static int test_wc_ERR_remove_state (void)
  24094. {
  24095. int ret = 0;
  24096. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  24097. printf(testingFmt, "wc_ERR_remove_state()");
  24098. wc_ERR_remove_state();
  24099. printf(resultFmt, ret == 0 ? passed : failed);
  24100. #endif
  24101. return ret;
  24102. }/*End test_wc_ERR_remove_state*/
  24103. /*
  24104. * Testing wc_ERR_print_errors_fp
  24105. */
  24106. static int test_wc_ERR_print_errors_fp (void)
  24107. {
  24108. int ret = 0;
  24109. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  24110. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  24111. long sz;
  24112. printf(testingFmt, "wc_ERR_print_errors_fp()");
  24113. WOLFSSL_ERROR(BAD_FUNC_ARG);
  24114. XFILE fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar");
  24115. wc_ERR_print_errors_fp(fp);
  24116. #if defined(DEBUG_WOLFSSL)
  24117. AssertTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  24118. sz = XFTELL(fp);
  24119. if (sz == 0) {
  24120. ret = BAD_FUNC_ARG;
  24121. }
  24122. #endif
  24123. printf(resultFmt, ret == 0 ? passed : failed);
  24124. XFCLOSE(fp);
  24125. (void)sz;
  24126. #endif
  24127. return ret;
  24128. }/*End test_wc_ERR_print_errors_fp*/
  24129. #ifdef DEBUG_WOLFSSL
  24130. static void Logging_cb(const int logLevel, const char *const logMessage)
  24131. {
  24132. (void)logLevel;
  24133. (void)logMessage;
  24134. }
  24135. #endif
  24136. /*
  24137. * Testing wolfSSL_GetLoggingCb
  24138. */
  24139. static int test_wolfSSL_GetLoggingCb (void)
  24140. {
  24141. int ret = 0;
  24142. #ifdef DEBUG_WOLFSSL
  24143. printf(testingFmt, "wolfSSL_GetLoggingCb()");
  24144. /* Testing without wolfSSL_SetLoggingCb() */
  24145. if (ret == 0) {
  24146. if (wolfSSL_GetLoggingCb() == NULL) { /* Should be true */
  24147. ret = 0;
  24148. }
  24149. if (wolfSSL_GetLoggingCb() != NULL) { /* Should not be true */
  24150. ret = -1;
  24151. }
  24152. }
  24153. /* Testing with wolfSSL_SetLoggingCb() */
  24154. if (ret == 0) {
  24155. ret = wolfSSL_SetLoggingCb(Logging_cb);
  24156. if (ret == 0){
  24157. if (wolfSSL_GetLoggingCb() == NULL) { /* Should not be true */
  24158. ret = -1;
  24159. }
  24160. if (ret == 0) {
  24161. if (wolfSSL_GetLoggingCb() == Logging_cb) { /* Should be true */
  24162. ret = 0;
  24163. }
  24164. }
  24165. /* reset logging callback */
  24166. wolfSSL_SetLoggingCb(NULL);
  24167. }
  24168. }
  24169. printf(resultFmt, ret == 0 ? passed : failed);
  24170. #endif
  24171. return ret;
  24172. }/*End test_wolfSSL_GetLoggingCb*/
  24173. static void test_wolfSSL_HMAC(void)
  24174. {
  24175. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  24176. HMAC_CTX* hmac;
  24177. ENGINE* e = NULL;
  24178. const unsigned char key[] = "simple test key";
  24179. unsigned char hash[WC_MAX_DIGEST_SIZE];
  24180. unsigned int len;
  24181. printf(testingFmt, "wolfSSL_HMAC()");
  24182. AssertNotNull(hmac = HMAC_CTX_new());
  24183. HMAC_CTX_init(hmac);
  24184. AssertIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key),
  24185. EVP_sha256(), e), SSL_SUCCESS);
  24186. /* re-using test key as data to hash */
  24187. AssertIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), SSL_SUCCESS);
  24188. AssertIntEQ(HMAC_Update(hmac, NULL, 0), SSL_SUCCESS);
  24189. AssertIntEQ(HMAC_Final(hmac, hash, &len), SSL_SUCCESS);
  24190. AssertIntEQ(len, (int)WC_SHA256_DIGEST_SIZE);
  24191. AssertIntEQ(HMAC_size(hmac), WC_SHA256_DIGEST_SIZE);
  24192. HMAC_cleanup(hmac);
  24193. HMAC_CTX_free(hmac);
  24194. #endif
  24195. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  24196. len = 0;
  24197. AssertNotNull(HMAC(EVP_sha256(), key, (int)sizeof(key), NULL, 0, hash, &len));
  24198. AssertIntEQ(len, (int)WC_SHA256_DIGEST_SIZE);
  24199. #endif
  24200. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224)
  24201. len = 0;
  24202. AssertNotNull(HMAC(EVP_sha224(), key, (int)sizeof(key), NULL, 0, hash, &len));
  24203. AssertIntEQ(len, (int)WC_SHA224_DIGEST_SIZE);
  24204. #endif
  24205. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA384)
  24206. len = 0;
  24207. AssertNotNull(HMAC(EVP_sha384(), key, (int)sizeof(key), NULL, 0, hash, &len));
  24208. AssertIntEQ(len, (int)WC_SHA384_DIGEST_SIZE);
  24209. #endif
  24210. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  24211. len = 0;
  24212. AssertNotNull(HMAC(EVP_sha512(), key, (int)sizeof(key), NULL, 0, hash, &len));
  24213. AssertIntEQ(len, (int)WC_SHA512_DIGEST_SIZE);
  24214. #endif
  24215. printf(resultFmt, passed);
  24216. }
  24217. static void test_wolfSSL_OBJ(void)
  24218. {
  24219. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  24220. * mode
  24221. */
  24222. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  24223. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  24224. defined(WOLFSSL_CERT_GEN)
  24225. ASN1_OBJECT *obj = NULL;
  24226. char buf[50];
  24227. XFILE fp;
  24228. X509 *x509 = NULL;
  24229. X509_NAME *x509Name;
  24230. X509_NAME_ENTRY *x509NameEntry;
  24231. ASN1_OBJECT *asn1Name = NULL;
  24232. int numNames;
  24233. BIO *bio = NULL;
  24234. int nid;
  24235. int i, j;
  24236. const char *f[] = {
  24237. #ifndef NO_RSA
  24238. "./certs/ca-cert.der",
  24239. #endif
  24240. #ifdef HAVE_ECC
  24241. "./certs/ca-ecc-cert.der",
  24242. "./certs/ca-ecc384-cert.der",
  24243. #endif
  24244. NULL};
  24245. ASN1_OBJECT *field_name_obj = NULL;
  24246. int lastpos = -1;
  24247. int tmp = -1;
  24248. ASN1_STRING *asn1 = NULL;
  24249. unsigned char *buf_dyn = NULL;
  24250. PKCS12 *p12;
  24251. int boolRet;
  24252. EVP_PKEY *pkey = NULL;
  24253. const char *p12_f[] = {
  24254. #if !defined(NO_DES3) && !defined(NO_RSA)
  24255. "./certs/test-servercert.p12",
  24256. #endif
  24257. NULL};
  24258. printf(testingFmt, "wolfSSL_OBJ()");
  24259. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  24260. AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
  24261. AssertIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  24262. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  24263. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  24264. ASN1_OBJECT_free(obj);
  24265. AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
  24266. AssertIntEQ(OBJ_obj2nid(obj), NID_sha256);
  24267. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  24268. #ifdef WOLFSSL_CERT_EXT
  24269. AssertIntEQ(OBJ_txt2nid(buf), NID_sha256);
  24270. #endif
  24271. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  24272. ASN1_OBJECT_free(obj);
  24273. for (i = 0; f[i] != NULL; i++)
  24274. {
  24275. AssertTrue((fp = XFOPEN(f[i], "r")) != XBADFILE);
  24276. AssertNotNull(x509 = d2i_X509_fp(fp, NULL));
  24277. XFCLOSE(fp);
  24278. AssertNotNull(x509Name = X509_get_issuer_name(x509));
  24279. AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  24280. /* Get the Common Name by using OBJ_txt2obj */
  24281. AssertNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  24282. do
  24283. {
  24284. lastpos = tmp;
  24285. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  24286. } while (tmp > -1);
  24287. AssertIntNE(lastpos, -1);
  24288. ASN1_OBJECT_free(field_name_obj);
  24289. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  24290. AssertNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  24291. AssertIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  24292. /*
  24293. * All Common Names should be www.wolfssl.com
  24294. * This makes testing easier as we can test for the expected value.
  24295. */
  24296. AssertStrEQ((char*)buf_dyn, "www.wolfssl.com");
  24297. OPENSSL_free(buf_dyn);
  24298. bio = BIO_new(BIO_s_mem());
  24299. AssertTrue(bio != NULL);
  24300. for (j = 0; j < numNames; j++)
  24301. {
  24302. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  24303. AssertNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  24304. AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  24305. }
  24306. BIO_free(bio);
  24307. ASN1_OBJECT_free(asn1Name);
  24308. X509_free(x509);
  24309. }
  24310. for (i = 0; p12_f[i] != NULL; i++)
  24311. {
  24312. AssertTrue((fp = XFOPEN(p12_f[i], "r")) != XBADFILE);
  24313. AssertNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  24314. XFCLOSE(fp);
  24315. AssertTrue((boolRet = PKCS12_parse(p12, "wolfSSL test", &pkey, &x509, NULL)) > 0);
  24316. wc_PKCS12_free(p12);
  24317. EVP_PKEY_free(pkey);
  24318. x509Name = X509_get_issuer_name(x509);
  24319. AssertNotNull(x509Name);
  24320. AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  24321. AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  24322. for (j = 0; j < numNames; j++)
  24323. {
  24324. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  24325. AssertNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  24326. AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  24327. }
  24328. BIO_free(bio);
  24329. ASN1_OBJECT_free(asn1Name);
  24330. X509_free(x509);
  24331. }
  24332. printf(resultFmt, passed);
  24333. #endif
  24334. }
  24335. static void test_wolfSSL_i2a_ASN1_OBJECT(void)
  24336. {
  24337. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  24338. ASN1_OBJECT *obj = NULL;
  24339. BIO *bio = NULL;
  24340. AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
  24341. AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  24342. AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  24343. AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  24344. AssertIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  24345. BIO_free(bio);
  24346. ASN1_OBJECT_free(obj);
  24347. #endif
  24348. }
  24349. static void test_wolfSSL_OBJ_cmp(void)
  24350. {
  24351. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  24352. ASN1_OBJECT *obj = NULL;
  24353. ASN1_OBJECT *obj2 = NULL;
  24354. printf(testingFmt, "wolfSSL_OBJ_cmp()");
  24355. AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
  24356. AssertNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  24357. AssertIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  24358. AssertIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  24359. AssertIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  24360. AssertIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  24361. AssertIntEQ(OBJ_cmp(obj, obj), 0);
  24362. AssertIntEQ(OBJ_cmp(obj2, obj2), 0);
  24363. ASN1_OBJECT_free(obj);
  24364. ASN1_OBJECT_free(obj2);
  24365. printf(resultFmt, passed);
  24366. #endif
  24367. }
  24368. static void test_wolfSSL_OBJ_txt2nid(void)
  24369. {
  24370. #if !defined(NO_WOLFSSL_STUB) && defined(WOLFSSL_APACHE_HTTPD)
  24371. int i;
  24372. static const struct {
  24373. const char* sn;
  24374. const char* ln;
  24375. const char* oid;
  24376. int nid;
  24377. } testVals[] = {
  24378. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  24379. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  24380. NID_id_on_dnsSRV },
  24381. { "msUPN", "Microsoft User Principal Name",
  24382. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  24383. { NULL, NULL, NULL, NID_undef }
  24384. };
  24385. printf(testingFmt, "wolfSSL_OBJ_txt2nid()");
  24386. /* Invalid cases */
  24387. AssertIntEQ(OBJ_txt2nid(NULL), NID_undef);
  24388. AssertIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  24389. /* Valid cases */
  24390. for (i = 0; testVals[i].sn != NULL; i++) {
  24391. AssertIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  24392. AssertIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  24393. AssertIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  24394. }
  24395. printf(resultFmt, passed);
  24396. #endif
  24397. }
  24398. static void test_wolfSSL_OBJ_txt2obj(void)
  24399. {
  24400. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  24401. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  24402. int i;
  24403. char buf[50];
  24404. ASN1_OBJECT* obj;
  24405. static const struct {
  24406. const char* oidStr;
  24407. const char* sn;
  24408. const char* ln;
  24409. } objs_list[] = {
  24410. #if defined(WOLFSSL_APACHE_HTTPD)
  24411. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  24412. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  24413. #endif
  24414. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  24415. { NULL, NULL, NULL }
  24416. };
  24417. printf(testingFmt, "wolfSSL_OBJ_txt2obj()");
  24418. AssertNull(obj = OBJ_txt2obj("Bad name", 0));
  24419. AssertNull(obj = OBJ_txt2obj(NULL, 0));
  24420. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  24421. /* Test numerical value of oid (oidStr) */
  24422. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  24423. /* Convert object back to text to confirm oid is correct */
  24424. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  24425. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  24426. ASN1_OBJECT_free(obj);
  24427. XMEMSET(buf, 0, sizeof(buf));
  24428. /* Test short name (sn) */
  24429. AssertNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  24430. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  24431. /* Convert object back to text to confirm oid is correct */
  24432. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  24433. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  24434. ASN1_OBJECT_free(obj);
  24435. XMEMSET(buf, 0, sizeof(buf));
  24436. /* Test long name (ln) - should fail when no_name = 1 */
  24437. AssertNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  24438. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  24439. /* Convert object back to text to confirm oid is correct */
  24440. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  24441. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  24442. ASN1_OBJECT_free(obj);
  24443. XMEMSET(buf, 0, sizeof(buf));
  24444. }
  24445. printf(resultFmt, passed);
  24446. #endif
  24447. }
  24448. static void test_wolfSSL_X509_NAME_ENTRY(void)
  24449. {
  24450. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  24451. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  24452. X509* x509;
  24453. BIO* bio;
  24454. X509_NAME* nm;
  24455. X509_NAME_ENTRY* entry;
  24456. unsigned char cn[] = "another name to add";
  24457. printf(testingFmt, "wolfSSL_X509_NAME_ENTRY()");
  24458. AssertNotNull(x509 =
  24459. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  24460. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  24461. AssertIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  24462. #ifdef WOLFSSL_CERT_REQ
  24463. {
  24464. X509_REQ* req;
  24465. BIO* bReq;
  24466. AssertNotNull(req =
  24467. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  24468. AssertNotNull(bReq = BIO_new(BIO_s_mem()));
  24469. AssertIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  24470. BIO_free(bReq);
  24471. X509_free(req);
  24472. }
  24473. #endif
  24474. AssertNotNull(nm = X509_get_subject_name(x509));
  24475. /* Test add entry */
  24476. AssertNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  24477. 0x0c, cn, (int)sizeof(cn)));
  24478. AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  24479. #ifdef WOLFSSL_CERT_EXT
  24480. AssertIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  24481. (byte*)"support@wolfssl.com", 19, -1,
  24482. 1), WOLFSSL_SUCCESS);
  24483. #endif
  24484. X509_NAME_ENTRY_free(entry);
  24485. /* Test add entry by text */
  24486. AssertNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  24487. 0x0c, cn, (int)sizeof(cn)));
  24488. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  24489. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  24490. AssertNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  24491. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  24492. #endif
  24493. AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  24494. X509_NAME_ENTRY_free(entry);
  24495. /* Test add entry by NID */
  24496. AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  24497. cn, -1, -1, 0), WOLFSSL_SUCCESS);
  24498. BIO_free(bio);
  24499. X509_free(x509); /* free's nm */
  24500. printf(resultFmt, passed);
  24501. #endif
  24502. }
  24503. static void test_wolfSSL_X509_set_name(void)
  24504. {
  24505. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  24506. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  24507. X509* x509;
  24508. X509_NAME* name;
  24509. printf(testingFmt, "wolfSSL_X509_set_name()");
  24510. AssertNotNull(name = X509_NAME_new());
  24511. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  24512. (byte*)"wolfssl.com", 11, 0, 1),
  24513. WOLFSSL_SUCCESS);
  24514. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  24515. (byte*)"support@wolfssl.com", 19, -1,
  24516. 1), WOLFSSL_SUCCESS);
  24517. AssertNotNull(x509 = X509_new());
  24518. AssertIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  24519. AssertIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  24520. AssertIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  24521. AssertIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  24522. AssertIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  24523. AssertIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  24524. AssertIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  24525. AssertIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  24526. X509_free(x509);
  24527. X509_NAME_free(name);
  24528. printf(resultFmt, passed);
  24529. #endif /* OPENSSL_ALL && !NO_CERTS */
  24530. }
  24531. static void test_wolfSSL_X509_set_notAfter(void)
  24532. {
  24533. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  24534. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  24535. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  24536. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  24537. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  24538. /* Generalized time will overflow time_t if not long */
  24539. X509* x;
  24540. BIO* bio;
  24541. ASN1_TIME *asn_time, *time_check;
  24542. const int year = 365*24*60*60;
  24543. const int day = 24*60*60;
  24544. const int hour = 60*60;
  24545. const int mini = 60;
  24546. int offset_day;
  24547. unsigned char buf[25];
  24548. time_t t;
  24549. printf(testingFmt, "wolfSSL_X509_set_notAfter()");
  24550. /*
  24551. * Setup asn_time. APACHE HTTPD uses time(NULL)
  24552. */
  24553. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  24554. offset_day = 7;
  24555. /*
  24556. * Free these.
  24557. */
  24558. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  24559. AssertNotNull(asn_time);
  24560. AssertNotNull(x = X509_new());
  24561. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  24562. /*
  24563. * Tests
  24564. */
  24565. AssertTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  24566. /* time_check is simply (ANS1_TIME*)x->notAfter */
  24567. AssertNotNull(time_check = X509_get_notAfter(x));
  24568. /* ANS1_TIME_check validates by checking if argument can be parsed */
  24569. AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  24570. /* Convert to human readable format and compare to intended date */
  24571. AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
  24572. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  24573. AssertIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  24574. /*
  24575. * Cleanup
  24576. */
  24577. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  24578. X509_free(x);
  24579. BIO_free(bio);
  24580. printf(resultFmt, passed);
  24581. #endif
  24582. }
  24583. static void test_wolfSSL_X509_set_notBefore(void)
  24584. {
  24585. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  24586. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  24587. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  24588. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  24589. X509* x;
  24590. BIO* bio;
  24591. ASN1_TIME *asn_time, *time_check;
  24592. const int year = 365*24*60*60;
  24593. const int day = 24*60*60;
  24594. const int hour = 60*60;
  24595. const int mini = 60;
  24596. int offset_day;
  24597. unsigned char buf[25];
  24598. time_t t;
  24599. printf(testingFmt, "wolfSSL_X509_set_notBefore()");
  24600. /*
  24601. * Setup asn_time. APACHE HTTPD uses time(NULL)
  24602. */
  24603. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  24604. offset_day = 7;
  24605. /*
  24606. * Free these.
  24607. */
  24608. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  24609. AssertNotNull(asn_time);
  24610. AssertNotNull(x = X509_new());
  24611. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  24612. AssertIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  24613. /*
  24614. * Main Tests
  24615. */
  24616. AssertTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  24617. /* time_check == (ANS1_TIME*)x->notBefore */
  24618. AssertNotNull(time_check = X509_get_notBefore(x));
  24619. /* ANS1_TIME_check validates by checking if argument can be parsed */
  24620. AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  24621. /* Convert to human readable format and compare to intended date */
  24622. AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
  24623. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  24624. AssertIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  24625. /*
  24626. * Cleanup
  24627. */
  24628. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  24629. X509_free(x);
  24630. BIO_free(bio);
  24631. printf(resultFmt, passed);
  24632. #endif
  24633. }
  24634. static void test_wolfSSL_X509_set_version(void)
  24635. {
  24636. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  24637. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  24638. X509* x509;
  24639. long v = 2L;
  24640. long max = INT_MAX;
  24641. AssertNotNull(x509 = X509_new());
  24642. /* These should pass. */
  24643. AssertTrue(wolfSSL_X509_set_version(x509, v));
  24644. AssertIntEQ(v, wolfSSL_X509_get_version(x509));
  24645. /* Fail Case: When v(long) is greater than x509->version(int). */
  24646. v = max+1;
  24647. AssertFalse(wolfSSL_X509_set_version(x509, v));
  24648. /* Cleanup */
  24649. X509_free(x509);
  24650. printf(resultFmt, passed);
  24651. #endif
  24652. }
  24653. static void test_wolfSSL_BIO_gets(void)
  24654. {
  24655. #if defined(OPENSSL_EXTRA)
  24656. BIO* bio;
  24657. BIO* bio2;
  24658. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  24659. char emp[] = "";
  24660. char bio_buffer[20];
  24661. int bufferSz = 20;
  24662. printf(testingFmt, "wolfSSL_BIO_gets()");
  24663. /* try with bad args */
  24664. AssertNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  24665. /* try with real msg */
  24666. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  24667. XMEMSET(bio_buffer, 0, bufferSz);
  24668. AssertNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  24669. AssertNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  24670. AssertNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  24671. AssertFalse(bio2 != BIO_next(bio));
  24672. /* make buffer filled with no terminating characters */
  24673. XMEMSET(bio_buffer, 1, bufferSz);
  24674. /* BIO_gets reads a line of data */
  24675. AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  24676. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  24677. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  24678. AssertStrEQ(bio_buffer, "hello wolfSSL\n");
  24679. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  24680. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  24681. AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  24682. /* check not null terminated string */
  24683. BIO_free(bio);
  24684. msg[0] = 0x33;
  24685. msg[1] = 0x33;
  24686. msg[2] = 0x33;
  24687. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  24688. AssertIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  24689. AssertIntEQ(bio_buffer[0], msg[0]);
  24690. AssertIntEQ(bio_buffer[1], msg[1]);
  24691. AssertIntNE(bio_buffer[2], msg[2]);
  24692. BIO_free(bio);
  24693. msg[3] = 0x33;
  24694. bio_buffer[3] = 0x33;
  24695. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  24696. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  24697. AssertIntEQ(bio_buffer[0], msg[0]);
  24698. AssertIntEQ(bio_buffer[1], msg[1]);
  24699. AssertIntEQ(bio_buffer[2], msg[2]);
  24700. AssertIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  24701. /* check reading an empty string */
  24702. BIO_free(bio);
  24703. AssertNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  24704. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  24705. AssertStrEQ(emp, bio_buffer);
  24706. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  24707. /* check error cases */
  24708. BIO_free(bio);
  24709. AssertIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  24710. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  24711. AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  24712. #if !defined(NO_FILESYSTEM)
  24713. {
  24714. BIO* f_bio;
  24715. XFILE f;
  24716. AssertNotNull(f_bio = BIO_new(BIO_s_file()));
  24717. AssertIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  24718. f = XFOPEN(svrCertFile, "rb");
  24719. AssertTrue((f != XBADFILE));
  24720. AssertIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  24721. AssertIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  24722. BIO_free(f_bio);
  24723. }
  24724. #endif /* NO_FILESYSTEM */
  24725. BIO_free(bio);
  24726. BIO_free(bio2);
  24727. /* try with type BIO */
  24728. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  24729. sizeof(msg));
  24730. AssertNotNull(bio = BIO_new(BIO_s_bio()));
  24731. AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  24732. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  24733. AssertIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  24734. AssertIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  24735. AssertIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  24736. AssertIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  24737. AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  24738. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  24739. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  24740. AssertStrEQ(bio_buffer, "hello wolfSSL\n");
  24741. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  24742. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  24743. AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  24744. BIO_free(bio);
  24745. BIO_free(bio2);
  24746. /* check reading an empty string */
  24747. AssertNotNull(bio = BIO_new(BIO_s_bio()));
  24748. AssertIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  24749. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  24750. AssertStrEQ(emp, bio_buffer);
  24751. BIO_free(bio);
  24752. printf(resultFmt, passed);
  24753. #endif
  24754. }
  24755. static void test_wolfSSL_BIO_puts(void)
  24756. {
  24757. #if defined(OPENSSL_EXTRA)
  24758. BIO* bio;
  24759. char input[] = "hello\0world\n.....ok\n\0";
  24760. char output[128];
  24761. printf(testingFmt, "wolfSSL_BIO_puts()");
  24762. XMEMSET(output, 0, sizeof(output));
  24763. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  24764. AssertIntEQ(BIO_puts(bio, input), 5);
  24765. AssertIntEQ(BIO_pending(bio), 5);
  24766. AssertIntEQ(BIO_puts(bio, input + 6), 14);
  24767. AssertIntEQ(BIO_pending(bio), 19);
  24768. AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  24769. AssertStrEQ(output, "helloworld\n");
  24770. AssertIntEQ(BIO_pending(bio), 8);
  24771. AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  24772. AssertStrEQ(output, ".....ok\n");
  24773. AssertIntEQ(BIO_pending(bio), 0);
  24774. AssertIntEQ(BIO_puts(bio, ""), -1);
  24775. BIO_free(bio);
  24776. printf(resultFmt, passed);
  24777. #endif
  24778. }
  24779. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  24780. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  24781. defined(HAVE_IO_TESTS_DEPENDENCIES)
  24782. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  24783. {
  24784. (void)ssl;
  24785. (void)buf;
  24786. (void)sz;
  24787. (void)ctx;
  24788. return WOLFSSL_CBIO_ERR_WANT_READ;
  24789. }
  24790. #endif
  24791. static void test_wolfSSL_BIO_should_retry(void)
  24792. {
  24793. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  24794. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  24795. defined(HAVE_IO_TESTS_DEPENDENCIES)
  24796. tcp_ready ready;
  24797. func_args server_args;
  24798. THREAD_TYPE serverThread;
  24799. SOCKET_T sockfd = 0;
  24800. WOLFSSL_CTX* ctx;
  24801. WOLFSSL* ssl;
  24802. char msg[64] = "hello wolfssl!";
  24803. char reply[1024];
  24804. int msgSz = (int)XSTRLEN(msg);
  24805. int ret;
  24806. BIO* bio;
  24807. printf(testingFmt, "wolfSSL_BIO_should_retry()");
  24808. XMEMSET(&server_args, 0, sizeof(func_args));
  24809. #ifdef WOLFSSL_TIRTOS
  24810. fdOpenSession(Task_self());
  24811. #endif
  24812. StartTCP();
  24813. InitTcpReady(&ready);
  24814. #if defined(USE_WINDOWS_API)
  24815. /* use RNG to get random port if using windows */
  24816. ready.port = GetRandomPort();
  24817. #endif
  24818. server_args.signal = &ready;
  24819. start_thread(test_server_nofail, &server_args, &serverThread);
  24820. wait_tcp_ready(&server_args);
  24821. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  24822. AssertIntEQ(WOLFSSL_SUCCESS,
  24823. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  24824. AssertIntEQ(WOLFSSL_SUCCESS,
  24825. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  24826. AssertIntEQ(WOLFSSL_SUCCESS,
  24827. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  24828. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  24829. /* force retry */
  24830. ssl = wolfSSL_new(ctx);
  24831. AssertNotNull(ssl);
  24832. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  24833. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  24834. AssertNotNull(bio = BIO_new(BIO_f_ssl()));
  24835. BIO_set_ssl(bio, ssl, BIO_CLOSE);
  24836. AssertIntLE(BIO_write(bio, msg, msgSz), 0);
  24837. AssertIntNE(BIO_should_retry(bio), 0);
  24838. /* now perform successful connection */
  24839. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  24840. AssertIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  24841. BIO_read(bio, reply, sizeof(reply));
  24842. ret = wolfSSL_get_error(ssl, -1);
  24843. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  24844. AssertIntNE(BIO_should_retry(bio), 0);
  24845. }
  24846. else {
  24847. AssertIntEQ(BIO_should_retry(bio), 0);
  24848. }
  24849. AssertIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  24850. XSTRLEN("I hear you fa shizzle!")), 0);
  24851. BIO_free(bio);
  24852. wolfSSL_CTX_free(ctx);
  24853. join_thread(serverThread);
  24854. FreeTcpReady(&ready);
  24855. #ifdef WOLFSSL_TIRTOS
  24856. fdOpenSession(Task_self());
  24857. #endif
  24858. printf(resultFmt, passed);
  24859. #endif
  24860. }
  24861. static void test_wolfSSL_BIO_write(void)
  24862. {
  24863. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  24864. BIO* bio;
  24865. BIO* bio64;
  24866. BIO* ptr;
  24867. int sz;
  24868. char msg[] = "conversion test";
  24869. char out[40];
  24870. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  24871. BUF_MEM* buf = NULL;
  24872. printf(testingFmt, "wolfSSL_BIO_write()");
  24873. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  24874. AssertNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  24875. /* now should convert to base64 then write to memory */
  24876. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  24877. BIO_flush(bio);
  24878. /* test BIO chain */
  24879. AssertIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  24880. AssertNotNull(buf);
  24881. AssertIntEQ(buf->length, 25);
  24882. AssertNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  24883. sz = sizeof(out);
  24884. XMEMSET(out, 0, sz);
  24885. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  24886. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  24887. /* write then read should return the same message */
  24888. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  24889. sz = sizeof(out);
  24890. XMEMSET(out, 0, sz);
  24891. AssertIntEQ(BIO_read(bio, out, sz), 16);
  24892. AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  24893. /* now try encoding with no line ending */
  24894. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  24895. #ifdef HAVE_EX_DATA
  24896. BIO_set_ex_data(bio64, 0, (void*) "data");
  24897. AssertIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  24898. #endif
  24899. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  24900. BIO_flush(bio);
  24901. sz = sizeof(out);
  24902. XMEMSET(out, 0, sz);
  24903. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  24904. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  24905. BIO_free_all(bio); /* frees bio64 also */
  24906. /* test with more than one bio64 in list */
  24907. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  24908. AssertNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  24909. AssertNotNull(BIO_push(bio64, BIO_new(BIO_s_mem())));
  24910. /* now should convert to base64 when stored and then decode with read */
  24911. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  24912. BIO_flush(bio);
  24913. sz = sizeof(out);
  24914. XMEMSET(out, 0, sz);
  24915. AssertIntEQ((sz = BIO_read(bio, out, sz)), 16);
  24916. AssertIntEQ(XMEMCMP(out, msg, sz), 0);
  24917. BIO_clear_flags(bio64, ~0);
  24918. BIO_set_retry_read(bio);
  24919. BIO_free_all(bio); /* frees bio64s also */
  24920. printf(resultFmt, passed);
  24921. #endif
  24922. }
  24923. static void test_wolfSSL_BIO_printf(void)
  24924. {
  24925. #if defined(OPENSSL_ALL)
  24926. BIO* bio;
  24927. int sz = 7;
  24928. char msg[] = "TLS 1.3 for the world";
  24929. char out[60];
  24930. char expected[] = "TLS 1.3 for the world : sz = 7";
  24931. printf(testingFmt, "wolfSSL_BIO_printf()");
  24932. XMEMSET(out, 0, sizeof(out));
  24933. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  24934. AssertIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  24935. AssertIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  24936. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  24937. AssertIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  24938. BIO_free(bio);
  24939. printf(resultFmt, passed);
  24940. #endif
  24941. }
  24942. static void test_wolfSSL_BIO_f_md(void)
  24943. {
  24944. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  24945. BIO *bio, *mem;
  24946. char msg[] = "message to hash";
  24947. char out[60];
  24948. EVP_MD_CTX* ctx;
  24949. const unsigned char testKey[] =
  24950. {
  24951. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  24952. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  24953. 0x0b, 0x0b, 0x0b, 0x0b
  24954. };
  24955. const char testData[] = "Hi There";
  24956. const unsigned char testResult[] =
  24957. {
  24958. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  24959. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  24960. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  24961. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  24962. };
  24963. const unsigned char expectedHash[] =
  24964. {
  24965. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  24966. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  24967. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  24968. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  24969. };
  24970. const unsigned char emptyHash[] =
  24971. {
  24972. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  24973. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  24974. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  24975. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  24976. };
  24977. unsigned char check[sizeof(testResult) + 1];
  24978. size_t checkSz = -1;
  24979. EVP_PKEY* key;
  24980. printf(testingFmt, "wolfSSL_BIO_f_md()");
  24981. XMEMSET(out, 0, sizeof(out));
  24982. AssertNotNull(bio = BIO_new(BIO_f_md()));
  24983. AssertNotNull(mem = BIO_new(BIO_s_mem()));
  24984. AssertIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  24985. AssertIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  24986. /* should not be able to write/read yet since just digest wrapper and no
  24987. * data is passing through the bio */
  24988. AssertIntEQ(BIO_write(bio, msg, 0), 0);
  24989. AssertIntEQ(BIO_pending(bio), 0);
  24990. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  24991. AssertIntEQ(BIO_gets(bio, out, 3), 0);
  24992. AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  24993. AssertIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  24994. BIO_reset(bio);
  24995. /* append BIO mem to bio in order to read/write */
  24996. AssertNotNull(bio = BIO_push(bio, mem));
  24997. XMEMSET(out, 0, sizeof(out));
  24998. AssertIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  24999. AssertIntEQ(BIO_pending(bio), 16);
  25000. /* this just reads the message and does not hash it (gets calls final) */
  25001. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  25002. AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  25003. /* create a message digest using BIO */
  25004. XMEMSET(out, 0, sizeof(out));
  25005. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  25006. AssertIntEQ(BIO_pending(mem), 16);
  25007. AssertIntEQ(BIO_pending(bio), 16);
  25008. AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  25009. AssertIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  25010. BIO_free(bio);
  25011. BIO_free(mem);
  25012. /* test with HMAC */
  25013. XMEMSET(out, 0, sizeof(out));
  25014. AssertNotNull(bio = BIO_new(BIO_f_md()));
  25015. AssertNotNull(mem = BIO_new(BIO_s_mem()));
  25016. BIO_get_md_ctx(bio, &ctx);
  25017. AssertNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  25018. testKey, (int)sizeof(testKey)));
  25019. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  25020. AssertNotNull(bio = BIO_push(bio, mem));
  25021. BIO_write(bio, testData, (int)strlen(testData));
  25022. EVP_DigestSignFinal(ctx, NULL, &checkSz);
  25023. EVP_DigestSignFinal(ctx, check, &checkSz);
  25024. AssertIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  25025. EVP_PKEY_free(key);
  25026. BIO_free(bio);
  25027. BIO_free(mem);
  25028. printf(resultFmt, passed);
  25029. #endif
  25030. }
  25031. static void test_wolfSSL_SESSION(void)
  25032. {
  25033. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  25034. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  25035. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_SESSION_CACHE)
  25036. WOLFSSL* ssl;
  25037. WOLFSSL_CTX* ctx;
  25038. WOLFSSL_SESSION* sess;
  25039. WOLFSSL_SESSION* sess_copy;
  25040. unsigned char* sessDer = NULL;
  25041. unsigned char* ptr = NULL;
  25042. #ifdef OPENSSL_EXTRA
  25043. const unsigned char context[] = "user app context";
  25044. unsigned int contextSz = (unsigned int)sizeof(context);
  25045. #endif
  25046. int ret, err, sockfd, sz;
  25047. tcp_ready ready;
  25048. func_args server_args;
  25049. THREAD_TYPE serverThread;
  25050. char msg[80];
  25051. printf(testingFmt, "wolfSSL_SESSION()");
  25052. /* TLS v1.3 requires session tickets */
  25053. /* CHACHA and POLY1305 required for myTicketEncCb */
  25054. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  25055. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  25056. defined(HAVE_POLY1305)))
  25057. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  25058. #else
  25059. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  25060. #endif
  25061. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  25062. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  25063. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  25064. #ifdef WOLFSSL_ENCRYPTED_KEYS
  25065. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  25066. #endif
  25067. XMEMSET(&server_args, 0, sizeof(func_args));
  25068. #ifdef WOLFSSL_TIRTOS
  25069. fdOpenSession(Task_self());
  25070. #endif
  25071. StartTCP();
  25072. InitTcpReady(&ready);
  25073. #if defined(USE_WINDOWS_API)
  25074. /* use RNG to get random port if using windows */
  25075. ready.port = GetRandomPort();
  25076. #endif
  25077. server_args.signal = &ready;
  25078. start_thread(test_server_nofail, &server_args, &serverThread);
  25079. wait_tcp_ready(&server_args);
  25080. /* client connection */
  25081. ssl = wolfSSL_new(ctx);
  25082. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  25083. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), SSL_SUCCESS);
  25084. err = 0; /* Reset error */
  25085. do {
  25086. #ifdef WOLFSSL_ASYNC_CRYPT
  25087. if (err == WC_PENDING_E) {
  25088. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  25089. if (ret < 0) { break; } else if (ret == 0) { continue; }
  25090. }
  25091. #endif
  25092. ret = wolfSSL_connect(ssl);
  25093. if (ret != SSL_SUCCESS) {
  25094. err = wolfSSL_get_error(ssl, 0);
  25095. }
  25096. } while (ret != SSL_SUCCESS && err == WC_PENDING_E);
  25097. AssertIntEQ(ret, SSL_SUCCESS);
  25098. AssertIntEQ(wolfSSL_write(ssl, "GET", 3), 3);
  25099. AssertIntEQ(wolfSSL_read(ssl, msg, sizeof(msg)), 23);
  25100. sess = wolfSSL_get_session(ssl);
  25101. wolfSSL_shutdown(ssl);
  25102. wolfSSL_free(ssl);
  25103. join_thread(serverThread);
  25104. FreeTcpReady(&ready);
  25105. #ifdef WOLFSSL_TIRTOS
  25106. fdOpenSession(Task_self());
  25107. #endif
  25108. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  25109. {
  25110. X509 *x509;
  25111. char buf[30];
  25112. int bufSz;
  25113. AssertNotNull(x509 = SSL_SESSION_get0_peer(sess));
  25114. AssertIntGT((bufSz = X509_NAME_get_text_by_NID(
  25115. X509_get_subject_name(x509), NID_organizationalUnitName,
  25116. buf, sizeof(buf))), 0);
  25117. AssertIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  25118. if (bufSz == 7) {
  25119. AssertIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  25120. }
  25121. if (bufSz == 16) {
  25122. AssertIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  25123. }
  25124. }
  25125. #endif
  25126. AssertNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  25127. wolfSSL_SESSION_free(sess_copy);
  25128. /* get session from DER and update the timeout */
  25129. AssertIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  25130. AssertIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  25131. wolfSSL_SESSION_free(sess);
  25132. ptr = sessDer;
  25133. AssertNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  25134. AssertNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  25135. (const unsigned char**)&ptr, sz));
  25136. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  25137. AssertIntGT(wolfSSL_SESSION_get_time(sess), 0);
  25138. AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  25139. /* successful set session test */
  25140. AssertNotNull(ssl = wolfSSL_new(ctx));
  25141. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_SUCCESS);
  25142. #ifdef HAVE_SESSION_TICKET
  25143. /* Test set/get session ticket */
  25144. {
  25145. const char* ticket = "This is a session ticket";
  25146. char buf[64] = {0};
  25147. word32 bufSz = (word32)sizeof(buf);
  25148. AssertIntEQ(SSL_SUCCESS,
  25149. wolfSSL_set_SessionTicket(ssl, (byte *)ticket, (word32)XSTRLEN(ticket)));
  25150. AssertIntEQ(SSL_SUCCESS,
  25151. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  25152. AssertStrEQ(ticket, buf);
  25153. }
  25154. #endif
  25155. #ifdef OPENSSL_EXTRA
  25156. /* fail case with miss match session context IDs (use compatibility API) */
  25157. AssertIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  25158. SSL_SUCCESS);
  25159. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  25160. wolfSSL_free(ssl);
  25161. AssertIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  25162. SSL_FAILURE);
  25163. AssertIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  25164. SSL_SUCCESS);
  25165. AssertNotNull(ssl = wolfSSL_new(ctx));
  25166. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  25167. #endif
  25168. wolfSSL_free(ssl);
  25169. SSL_SESSION_free(sess);
  25170. wolfSSL_CTX_free(ctx);
  25171. printf(resultFmt, passed);
  25172. #endif
  25173. }
  25174. static void test_wolfSSL_d2i_PUBKEY(void)
  25175. {
  25176. #if defined(OPENSSL_EXTRA)
  25177. BIO* bio;
  25178. EVP_PKEY* pkey;
  25179. printf(testingFmt, "wolfSSL_d2i_PUBKEY()");
  25180. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  25181. AssertNull(d2i_PUBKEY_bio(NULL, NULL));
  25182. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  25183. /* RSA PUBKEY test */
  25184. AssertIntGT(BIO_write(bio, client_keypub_der_2048,
  25185. sizeof_client_keypub_der_2048), 0);
  25186. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  25187. EVP_PKEY_free(pkey);
  25188. #endif
  25189. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  25190. /* ECC PUBKEY test */
  25191. AssertIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  25192. sizeof_ecc_clikeypub_der_256), 0);
  25193. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  25194. EVP_PKEY_free(pkey);
  25195. #endif
  25196. BIO_free(bio);
  25197. (void)pkey;
  25198. printf(resultFmt, passed);
  25199. #endif
  25200. }
  25201. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  25202. static void test_wolfSSL_d2i_PrivateKeys_bio(void)
  25203. {
  25204. BIO* bio = NULL;
  25205. EVP_PKEY* pkey = NULL;
  25206. #ifndef NO_RSA
  25207. #endif
  25208. WOLFSSL_CTX* ctx;
  25209. #if defined(WOLFSSL_KEY_GEN)
  25210. unsigned char buff[4096];
  25211. unsigned char* bufPtr = buff;
  25212. #endif
  25213. printf(testingFmt, "wolfSSL_d2i_PrivateKeys_bio()");
  25214. /* test creating new EVP_PKEY with bad arg */
  25215. AssertNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  25216. /* test loading RSA key using BIO */
  25217. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  25218. {
  25219. XFILE file;
  25220. const char* fname = "./certs/server-key.der";
  25221. size_t sz;
  25222. byte* buf;
  25223. file = XFOPEN(fname, "rb");
  25224. AssertTrue((file != XBADFILE));
  25225. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  25226. sz = XFTELL(file);
  25227. XREWIND(file);
  25228. AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  25229. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  25230. XFCLOSE(file);
  25231. /* Test using BIO new mem and loading DER private key */
  25232. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  25233. AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  25234. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  25235. BIO_free(bio);
  25236. bio = NULL;
  25237. EVP_PKEY_free(pkey);
  25238. pkey = NULL;
  25239. }
  25240. #endif
  25241. /* test loading ECC key using BIO */
  25242. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  25243. {
  25244. XFILE file;
  25245. const char* fname = "./certs/ecc-key.der";
  25246. size_t sz;
  25247. byte* buf;
  25248. file = XFOPEN(fname, "rb");
  25249. AssertTrue((file != XBADFILE));
  25250. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  25251. sz = XFTELL(file);
  25252. XREWIND(file);
  25253. AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  25254. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  25255. XFCLOSE(file);
  25256. /* Test using BIO new mem and loading DER private key */
  25257. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  25258. AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  25259. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  25260. BIO_free(bio);
  25261. bio = NULL;
  25262. EVP_PKEY_free(pkey);
  25263. pkey = NULL;
  25264. }
  25265. #endif
  25266. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  25267. #ifndef NO_WOLFSSL_SERVER
  25268. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  25269. #else
  25270. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  25271. #endif
  25272. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  25273. !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  25274. {
  25275. RSA* rsa = NULL;
  25276. /* Tests bad parameters */
  25277. AssertNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  25278. /* RSA not set yet, expecting to fail*/
  25279. AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  25280. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  25281. /* set RSA using bio*/
  25282. AssertIntGT(BIO_write(bio, client_key_der_2048,
  25283. sizeof_client_key_der_2048), 0);
  25284. AssertNotNull(rsa = d2i_RSAPrivateKey_bio(bio, NULL));
  25285. AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  25286. /*i2d RSAprivate key tests */
  25287. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  25288. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  25289. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  25290. sizeof_client_key_der_2048);
  25291. bufPtr = NULL;
  25292. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  25293. sizeof_client_key_der_2048);
  25294. AssertNotNull(bufPtr);
  25295. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  25296. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  25297. RSA_free(rsa);
  25298. }
  25299. #endif /* !HAVE_FAST_RSA && WOLFSSL_KEY_GEN && !NO_RSA && !HAVE_USER_RSA*/
  25300. SSL_CTX_free(ctx);
  25301. ctx = NULL;
  25302. BIO_free(bio);
  25303. bio = NULL;
  25304. printf(resultFmt, passed);
  25305. }
  25306. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  25307. static void test_wolfSSL_sk_GENERAL_NAME(void)
  25308. {
  25309. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  25310. !defined(NO_RSA)
  25311. X509* x509;
  25312. GENERAL_NAME* gn;
  25313. unsigned char buf[4096];
  25314. const unsigned char* bufPt;
  25315. int bytes, i;
  25316. XFILE f;
  25317. STACK_OF(GENERAL_NAME)* sk;
  25318. printf(testingFmt, "wolfSSL_sk_GENERAL_NAME()");
  25319. f = XFOPEN(cliCertDerFileExt, "rb");
  25320. AssertTrue((f != XBADFILE));
  25321. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  25322. XFCLOSE(f);
  25323. bufPt = buf;
  25324. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  25325. AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  25326. NID_subject_alt_name, NULL, NULL));
  25327. AssertIntEQ(sk_GENERAL_NAME_num(sk), 1);
  25328. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  25329. AssertNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  25330. switch (gn->type) {
  25331. case GEN_DNS:
  25332. printf("found type GEN_DNS\n");
  25333. break;
  25334. case GEN_EMAIL:
  25335. printf("found type GEN_EMAIL\n");
  25336. break;
  25337. case GEN_URI:
  25338. printf("found type GEN_URI\n");
  25339. break;
  25340. }
  25341. }
  25342. X509_free(x509);
  25343. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  25344. printf(resultFmt, passed);
  25345. #endif
  25346. }
  25347. static void test_wolfSSL_MD4(void)
  25348. {
  25349. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  25350. MD4_CTX md4;
  25351. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  25352. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  25353. "789012345678901234567890";
  25354. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  25355. "\xcc\x05\x36";
  25356. int msgSz = (int)XSTRLEN(msg);
  25357. printf(testingFmt, "wolfSSL_MD4()");
  25358. XMEMSET(out, 0, sizeof(out));
  25359. MD4_Init(&md4);
  25360. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  25361. MD4_Final(out, &md4);
  25362. AssertIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  25363. printf(resultFmt, passed);
  25364. #endif
  25365. }
  25366. static void test_wolfSSL_RSA(void)
  25367. {
  25368. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  25369. defined(WOLFSSL_KEY_GEN)
  25370. RSA* rsa;
  25371. const BIGNUM *n;
  25372. const BIGNUM *e;
  25373. const BIGNUM *d;
  25374. printf(testingFmt, "wolfSSL_RSA()");
  25375. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  25376. AssertIntEQ(RSA_size(rsa), 256);
  25377. RSA_get0_key(rsa, &n, &e, &d);
  25378. AssertPtrEq(rsa->n, n);
  25379. AssertPtrEq(rsa->e, e);
  25380. AssertPtrEq(rsa->d, d);
  25381. AssertNotNull(n = BN_new());
  25382. AssertNotNull(e = BN_new());
  25383. AssertNotNull(d = BN_new());
  25384. AssertIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  25385. AssertPtrEq(rsa->n, n);
  25386. AssertPtrEq(rsa->e, e);
  25387. AssertPtrEq(rsa->d, d);
  25388. RSA_free(rsa);
  25389. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  25390. AssertNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  25391. AssertIntEQ(RSA_size(rsa), 384);
  25392. RSA_free(rsa);
  25393. #endif
  25394. /* remove for now with odd key size until adjusting rsa key size check with
  25395. wc_MakeRsaKey()
  25396. AssertNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  25397. RSA_free(rsa);
  25398. */
  25399. AssertNull(RSA_generate_key(-1, 3, NULL, NULL));
  25400. AssertNull(RSA_generate_key(511, 3, NULL, NULL)); /* RSA_MIN_SIZE - 1 */
  25401. AssertNull(RSA_generate_key(4097, 3, NULL, NULL)); /* RSA_MAX_SIZE + 1 */
  25402. AssertNull(RSA_generate_key(2048, 0, NULL, NULL));
  25403. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  25404. {
  25405. byte buff[FOURK_BUF];
  25406. byte der[FOURK_BUF];
  25407. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  25408. XFILE f;
  25409. int bytes;
  25410. /* test loading encrypted RSA private pem w/o password */
  25411. f = XFOPEN(PrivKeyPemFile, "rb");
  25412. AssertTrue((f != XBADFILE));
  25413. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  25414. XFCLOSE(f);
  25415. XMEMSET(der, 0, sizeof(der));
  25416. /* test that error value is returned with no password */
  25417. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""), 0);
  25418. }
  25419. #endif
  25420. printf(resultFmt, passed);
  25421. #endif
  25422. }
  25423. static void test_wolfSSL_RSA_DER(void)
  25424. {
  25425. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  25426. !defined(NO_RSA) && !defined(HAVE_USER_RSA) && defined(OPENSSL_EXTRA)
  25427. RSA *rsa;
  25428. int i;
  25429. const unsigned char *buff = NULL;
  25430. struct tbl_s
  25431. {
  25432. const unsigned char *der;
  25433. int sz;
  25434. } tbl[] = {
  25435. #ifdef USE_CERT_BUFFERS_1024
  25436. {client_key_der_1024, sizeof_client_key_der_1024},
  25437. {server_key_der_1024, sizeof_server_key_der_1024},
  25438. #endif
  25439. #ifdef USE_CERT_BUFFERS_2048
  25440. {client_key_der_2048, sizeof_client_key_der_2048},
  25441. {server_key_der_2048, sizeof_server_key_der_2048},
  25442. #endif
  25443. {NULL, 0}
  25444. };
  25445. /* Public Key DER */
  25446. struct tbl_s pub[] = {
  25447. #ifdef USE_CERT_BUFFERS_1024
  25448. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  25449. #endif
  25450. #ifdef USE_CERT_BUFFERS_2048
  25451. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  25452. #endif
  25453. {NULL, 0}
  25454. };
  25455. printf(testingFmt, "test_wolfSSL_RSA_DER()");
  25456. for (i = 0; tbl[i].der != NULL; i++)
  25457. {
  25458. AssertNotNull(d2i_RSAPublicKey(&rsa, &tbl[i].der, tbl[i].sz));
  25459. AssertNotNull(rsa);
  25460. RSA_free(rsa);
  25461. }
  25462. for (i = 0; tbl[i].der != NULL; i++)
  25463. {
  25464. AssertNotNull(d2i_RSAPrivateKey(&rsa, &tbl[i].der, tbl[i].sz));
  25465. AssertNotNull(rsa);
  25466. RSA_free(rsa);
  25467. }
  25468. for (i = 0; pub[i].der != NULL; i++)
  25469. {
  25470. AssertNotNull(d2i_RSAPublicKey(&rsa, &pub[i].der, pub[i].sz));
  25471. AssertNotNull(rsa);
  25472. AssertIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  25473. buff = NULL;
  25474. AssertIntEQ(i2d_RSAPublicKey(rsa, &buff), pub[i].sz);
  25475. AssertNotNull(buff);
  25476. AssertIntEQ(0, memcmp((void *)buff, (void *)pub[i].der, pub[i].sz));
  25477. XFREE((void *)buff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  25478. RSA_free(rsa);
  25479. }
  25480. printf(resultFmt, passed);
  25481. #endif
  25482. }
  25483. static void test_wolfSSL_RSA_get0_key(void)
  25484. {
  25485. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  25486. RSA *rsa = NULL;
  25487. const BIGNUM* n = NULL;
  25488. const BIGNUM* e = NULL;
  25489. const BIGNUM* d = NULL;
  25490. const unsigned char* der;
  25491. int derSz;
  25492. #ifdef USE_CERT_BUFFERS_1024
  25493. der = client_key_der_1024;
  25494. derSz = sizeof_client_key_der_1024;
  25495. #elif defined(USE_CERT_BUFFERS_2048)
  25496. der = client_key_der_2048;
  25497. derSz = sizeof_client_key_der_2048;
  25498. #else
  25499. der = NULL;
  25500. derSz = 0;
  25501. #endif
  25502. printf(testingFmt, "test_wolfSSL_RSA_get0_key()");
  25503. if (der != NULL) {
  25504. RSA_get0_key(NULL, NULL, NULL, NULL);
  25505. RSA_get0_key(rsa, NULL, NULL, NULL);
  25506. RSA_get0_key(NULL, &n, &e, &d);
  25507. AssertNull(n);
  25508. AssertNull(e);
  25509. AssertNull(d);
  25510. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  25511. AssertNotNull(rsa);
  25512. RSA_get0_key(rsa, NULL, NULL, NULL);
  25513. RSA_get0_key(rsa, &n, NULL, NULL);
  25514. AssertNotNull(n);
  25515. RSA_get0_key(rsa, NULL, &e, NULL);
  25516. AssertNotNull(e);
  25517. RSA_get0_key(rsa, NULL, NULL, &d);
  25518. AssertNotNull(d);
  25519. RSA_get0_key(rsa, &n, &e, &d);
  25520. AssertNotNull(n);
  25521. AssertNotNull(e);
  25522. AssertNotNull(d);
  25523. RSA_free(rsa);
  25524. }
  25525. printf(resultFmt, passed);
  25526. #endif
  25527. }
  25528. static void test_wolfSSL_RSA_meth(void)
  25529. {
  25530. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  25531. RSA *rsa;
  25532. RSA_METHOD *rsa_meth;
  25533. printf(testingFmt, "test_wolfSSL_RSA_meth");
  25534. #ifdef WOLFSSL_KEY_GEN
  25535. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  25536. RSA_free(rsa);
  25537. #else
  25538. AssertNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  25539. #endif
  25540. AssertNotNull(rsa_meth =
  25541. RSA_meth_new("placeholder RSA method", RSA_METHOD_FLAG_NO_CHECK));
  25542. #ifndef NO_WOLFSSL_STUB
  25543. AssertIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  25544. AssertIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  25545. AssertIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  25546. AssertIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  25547. AssertIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  25548. AssertIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  25549. AssertIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  25550. #endif
  25551. AssertNotNull(rsa = RSA_new());
  25552. AssertIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  25553. AssertPtrEq(RSA_get_method(rsa), rsa_meth);
  25554. AssertIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  25555. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  25556. AssertIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  25557. /* rsa_meth is freed here */
  25558. RSA_free(rsa);
  25559. printf(resultFmt, passed);
  25560. #endif
  25561. }
  25562. static void test_wolfSSL_verify_depth(void)
  25563. {
  25564. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  25565. WOLFSSL* ssl;
  25566. WOLFSSL_CTX* ctx;
  25567. long depth;
  25568. printf(testingFmt, "test_wolfSSL_verify_depth()");
  25569. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  25570. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  25571. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  25572. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  25573. AssertIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  25574. AssertNotNull(ssl = SSL_new(ctx));
  25575. AssertIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  25576. SSL_free(ssl);
  25577. SSL_CTX_set_verify_depth(ctx, -1);
  25578. AssertIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  25579. SSL_CTX_set_verify_depth(ctx, 2);
  25580. AssertIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  25581. AssertNotNull(ssl = SSL_new(ctx));
  25582. AssertIntEQ(2, SSL_get_verify_depth(ssl));
  25583. SSL_free(ssl);
  25584. SSL_CTX_free(ctx);
  25585. printf(resultFmt, passed);
  25586. #endif
  25587. }
  25588. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  25589. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  25590. * buffer of 64 bytes.
  25591. *
  25592. * returns the size of the digest buffer on success and a negative value on
  25593. * failure.
  25594. */
  25595. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest)
  25596. {
  25597. HMAC_CTX ctx1;
  25598. HMAC_CTX ctx2;
  25599. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  25600. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  25601. unsigned char long_key[] =
  25602. "0123456789012345678901234567890123456789"
  25603. "0123456789012345678901234567890123456789"
  25604. "0123456789012345678901234567890123456789"
  25605. "0123456789012345678901234567890123456789";
  25606. unsigned char msg[] = "message to hash";
  25607. unsigned int digestSz = 64;
  25608. int keySz = sizeof(key);
  25609. int long_keySz = sizeof(long_key);
  25610. int msgSz = sizeof(msg);
  25611. unsigned char digest2[64];
  25612. unsigned int digestSz2 = 64;
  25613. HMAC_CTX_init(&ctx1);
  25614. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  25615. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  25616. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  25617. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  25618. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  25619. HMAC_CTX_cleanup(&ctx1);
  25620. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  25621. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  25622. HMAC_CTX_cleanup(&ctx2);
  25623. AssertIntEQ(digestSz, digestSz2);
  25624. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  25625. /* test HMAC_Init with NULL key */
  25626. /* init after copy */
  25627. printf("test HMAC_Init with NULL key (0)\n");
  25628. HMAC_CTX_init(&ctx1);
  25629. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  25630. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  25631. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  25632. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  25633. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  25634. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  25635. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  25636. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  25637. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  25638. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  25639. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  25640. HMAC_CTX_cleanup(&ctx2);
  25641. AssertIntEQ(digestSz, digestSz2);
  25642. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  25643. /* long key */
  25644. printf("test HMAC_Init with NULL key (1)\n");
  25645. HMAC_CTX_init(&ctx1);
  25646. AssertIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type), SSL_SUCCESS);
  25647. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  25648. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  25649. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  25650. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  25651. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  25652. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  25653. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  25654. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  25655. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  25656. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  25657. HMAC_CTX_cleanup(&ctx2);
  25658. AssertIntEQ(digestSz, digestSz2);
  25659. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  25660. /* init before copy */
  25661. printf("test HMAC_Init with NULL key (2)\n");
  25662. HMAC_CTX_init(&ctx1);
  25663. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  25664. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  25665. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  25666. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  25667. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  25668. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  25669. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  25670. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  25671. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  25672. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  25673. HMAC_CTX_cleanup(&ctx1);
  25674. HMAC_CTX_cleanup(&ctx2);
  25675. AssertIntEQ(digestSz, digestSz2);
  25676. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  25677. return digestSz;
  25678. }
  25679. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  25680. static void test_wolfSSL_HMAC_CTX(void)
  25681. {
  25682. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  25683. unsigned char digest[64];
  25684. int digestSz;
  25685. printf(testingFmt, "wolfSSL_HMAC_CTX()");
  25686. #ifndef NO_SHA
  25687. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha1(), digest)), 20);
  25688. AssertIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  25689. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  25690. #endif /* !NO_SHA */
  25691. #ifdef WOLFSSL_SHA224
  25692. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha224(), digest)), 28);
  25693. AssertIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  25694. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  25695. "\x02\x0E", digest, digestSz), 0);
  25696. #endif /* WOLFSSL_SHA224 */
  25697. #ifndef NO_SHA256
  25698. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha256(), digest)), 32);
  25699. AssertIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  25700. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  25701. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  25702. #endif /* !NO_SHA256 */
  25703. #ifdef WOLFSSL_SHA384
  25704. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha384(), digest)), 48);
  25705. AssertIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  25706. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  25707. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  25708. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  25709. digest, digestSz), 0);
  25710. #endif /* WOLFSSL_SHA384 */
  25711. #ifdef WOLFSSL_SHA512
  25712. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha512(), digest)), 64);
  25713. AssertIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  25714. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  25715. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  25716. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  25717. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  25718. digest, digestSz), 0);
  25719. #endif /* WOLFSSL_SHA512 */
  25720. #ifndef NO_MD5
  25721. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_md5(), digest)), 16);
  25722. AssertIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  25723. "\xE4\x98\xDD", digest, digestSz), 0);
  25724. #endif /* !NO_MD5 */
  25725. printf(resultFmt, passed);
  25726. #endif
  25727. }
  25728. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  25729. static void sslMsgCb(int w, int version, int type, const void* buf,
  25730. size_t sz, SSL* ssl, void* arg)
  25731. {
  25732. int i;
  25733. unsigned char* pt = (unsigned char*)buf;
  25734. printf("%s %d bytes of version %d , type %d : ", (w)?"Writing":"Reading",
  25735. (int)sz, version, type);
  25736. for (i = 0; i < (int)sz; i++) printf("%02X", pt[i]);
  25737. printf("\n");
  25738. (void)ssl;
  25739. (void)arg;
  25740. }
  25741. #endif /* OPENSSL_EXTRA */
  25742. static void test_wolfSSL_msg_callback(void)
  25743. {
  25744. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  25745. WOLFSSL* ssl;
  25746. WOLFSSL_CTX* ctx;
  25747. printf(testingFmt, "wolfSSL_msg_callback()");
  25748. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  25749. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  25750. SSL_FILETYPE_PEM));
  25751. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  25752. SSL_FILETYPE_PEM));
  25753. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  25754. SSL_SUCCESS);
  25755. AssertNotNull(ssl = SSL_new(ctx));
  25756. AssertIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  25757. AssertIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  25758. AssertIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  25759. SSL_free(ssl);
  25760. SSL_CTX_free(ctx);
  25761. printf(resultFmt, passed);
  25762. #endif
  25763. }
  25764. static void test_wolfSSL_SHA(void)
  25765. {
  25766. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  25767. printf(testingFmt, "wolfSSL_SHA()");
  25768. #if !defined(NO_SHA)
  25769. {
  25770. const unsigned char in[] = "abc";
  25771. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  25772. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  25773. unsigned char out[WC_SHA_DIGEST_SIZE];
  25774. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  25775. AssertNotNull(SHA1(in, XSTRLEN((char*)in), out));
  25776. AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  25777. }
  25778. #endif
  25779. #if !defined(NO_SHA256)
  25780. {
  25781. const unsigned char in[] = "abc";
  25782. unsigned char expected[] = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  25783. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  25784. "\x15\xAD";
  25785. unsigned char out[WC_SHA256_DIGEST_SIZE];
  25786. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  25787. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  25788. AssertNotNull(SHA256(in, XSTRLEN((char*)in), out));
  25789. #else
  25790. AssertNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  25791. #endif
  25792. AssertIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  25793. }
  25794. #endif
  25795. #if defined(WOLFSSL_SHA384)
  25796. {
  25797. const unsigned char in[] = "abc";
  25798. unsigned char expected[] = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  25799. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  25800. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  25801. "\xc8\x25\xa7";
  25802. unsigned char out[WC_SHA384_DIGEST_SIZE];
  25803. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  25804. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  25805. AssertNotNull(SHA384(in, XSTRLEN((char*)in), out));
  25806. #else
  25807. AssertNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  25808. #endif
  25809. AssertIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  25810. }
  25811. #endif
  25812. #if defined(WOLFSSL_SHA512)
  25813. {
  25814. const unsigned char in[] = "abc";
  25815. unsigned char expected[] = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  25816. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  25817. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  25818. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  25819. "\xa5\x4c\xa4\x9f";
  25820. unsigned char out[WC_SHA512_DIGEST_SIZE];
  25821. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  25822. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  25823. AssertNotNull(SHA512(in, XSTRLEN((char*)in), out));
  25824. #else
  25825. AssertNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  25826. #endif
  25827. AssertIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  25828. }
  25829. #endif
  25830. printf(resultFmt, passed);
  25831. #endif
  25832. }
  25833. static void test_wolfSSL_DH_1536_prime(void)
  25834. {
  25835. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  25836. BIGNUM* bn;
  25837. unsigned char bits[200];
  25838. int sz = 192; /* known binary size */
  25839. const byte expected[] = {
  25840. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  25841. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  25842. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  25843. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  25844. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  25845. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  25846. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  25847. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  25848. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  25849. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  25850. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  25851. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  25852. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  25853. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  25854. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  25855. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  25856. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  25857. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  25858. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  25859. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  25860. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  25861. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  25862. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  25863. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  25864. };
  25865. printf(testingFmt, "wolfSSL_DH_1536_prime()");
  25866. bn = get_rfc3526_prime_1536(NULL);
  25867. AssertNotNull(bn);
  25868. AssertIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  25869. AssertIntEQ(0, XMEMCMP(expected, bits, sz));
  25870. BN_free(bn);
  25871. printf(resultFmt, passed);
  25872. #endif
  25873. }
  25874. static void test_wolfSSL_PEM_write_DHparams(void)
  25875. {
  25876. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  25877. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && !defined(NO_FILESYSTEM)
  25878. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  25879. DH* dh;
  25880. BIO* bio;
  25881. XFILE fp;
  25882. byte pem[2048];
  25883. int pemSz;
  25884. const char expected[] =
  25885. "-----BEGIN DH PARAMETERS-----\n\
  25886. MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n\
  25887. v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n\
  25888. nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n\
  25889. joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n\
  25890. wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n\
  25891. tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n\
  25892. -----END DH PARAMETERS-----\n";
  25893. printf(testingFmt, "wolfSSL_PEM_write_DHparams()");
  25894. AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
  25895. AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  25896. XFCLOSE(fp);
  25897. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  25898. AssertIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  25899. AssertNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  25900. BIO_free(bio);
  25901. AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  25902. AssertIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  25903. AssertIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  25904. XFCLOSE(fp);
  25905. DH_free(dh);
  25906. /* check results */
  25907. XMEMSET(pem, 0, sizeof(pem));
  25908. AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "rb"));
  25909. AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  25910. AssertIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  25911. XFCLOSE(fp);
  25912. printf(resultFmt, passed);
  25913. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  25914. #endif /* OPENSSL_ALL || OPENSSL_QT */
  25915. #endif
  25916. }
  25917. static void test_wolfSSL_AES_ecb_encrypt(void)
  25918. {
  25919. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB)
  25920. AES_KEY aes;
  25921. const byte msg[] =
  25922. {
  25923. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  25924. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  25925. };
  25926. const byte verify[] =
  25927. {
  25928. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  25929. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  25930. };
  25931. const byte key[] =
  25932. {
  25933. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  25934. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  25935. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  25936. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  25937. };
  25938. byte out[AES_BLOCK_SIZE];
  25939. printf(testingFmt, "wolfSSL_AES_ecb_encrypt()");
  25940. AssertIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  25941. XMEMSET(out, 0, AES_BLOCK_SIZE);
  25942. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  25943. AssertIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  25944. #ifdef HAVE_AES_DECRYPT
  25945. AssertIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  25946. XMEMSET(out, 0, AES_BLOCK_SIZE);
  25947. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  25948. AssertIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  25949. #endif
  25950. /* test bad arguments */
  25951. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  25952. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  25953. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  25954. printf(resultFmt, passed);
  25955. #endif
  25956. }
  25957. static void test_wolfSSL_SHA256(void)
  25958. {
  25959. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  25960. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  25961. unsigned char input[] =
  25962. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  25963. unsigned char output[] =
  25964. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  25965. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  25966. "\x06\xC1";
  25967. size_t inLen;
  25968. byte hash[WC_SHA256_DIGEST_SIZE];
  25969. printf(testingFmt, "wolfSSL_SHA256()");
  25970. inLen = XSTRLEN((char*)input);
  25971. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  25972. AssertNotNull(SHA256(input, inLen, hash));
  25973. AssertIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  25974. printf(resultFmt, passed);
  25975. #endif
  25976. }
  25977. static void test_wolfSSL_X509_get_serialNumber(void)
  25978. {
  25979. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  25980. ASN1_INTEGER* a;
  25981. BIGNUM* bn;
  25982. X509* x509;
  25983. char *serialHex;
  25984. printf(testingFmt, "wolfSSL_X509_get_serialNumber()");
  25985. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  25986. SSL_FILETYPE_PEM));
  25987. AssertNotNull(a = X509_get_serialNumber(x509));
  25988. /* check on value of ASN1 Integer */
  25989. AssertNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  25990. X509_free(x509); /* free's a */
  25991. AssertNotNull(serialHex = BN_bn2hex(bn));
  25992. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  25993. AssertStrEQ(serialHex, "01");
  25994. #else
  25995. AssertStrEQ(serialHex, "1");
  25996. #endif
  25997. OPENSSL_free(serialHex);
  25998. AssertIntEQ(BN_get_word(bn), 1);
  25999. BN_free(bn);
  26000. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  26001. a = ASN1_INTEGER_new();
  26002. if (a) {
  26003. AssertNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  26004. DYNAMIC_TYPE_OPENSSL));
  26005. a->isDynamic = 1;
  26006. ASN1_INTEGER_free(a);
  26007. }
  26008. printf(resultFmt, passed);
  26009. #endif
  26010. }
  26011. static void test_wolfSSL_OpenSSL_add_all_algorithms(void){
  26012. #if defined(OPENSSL_EXTRA)
  26013. printf(testingFmt, "wolfSSL_OpenSSL_add_all_algorithms()");
  26014. AssertIntEQ(wolfSSL_add_all_algorithms(),WOLFSSL_SUCCESS);
  26015. wolfSSL_Cleanup();
  26016. AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(),WOLFSSL_SUCCESS);
  26017. wolfSSL_Cleanup();
  26018. AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(),WOLFSSL_SUCCESS);
  26019. wolfSSL_Cleanup();
  26020. printf(resultFmt, passed);
  26021. #endif
  26022. }
  26023. static void test_wolfSSL_ASN1_STRING_print_ex(void){
  26024. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  26025. ASN1_STRING* asn_str;
  26026. const char data[] = "Hello wolfSSL!";
  26027. ASN1_STRING* esc_str;
  26028. const char esc_data[] = "a+;<>";
  26029. BIO *bio;
  26030. unsigned long flags;
  26031. int p_len;
  26032. unsigned char rbuf[255];
  26033. printf(testingFmt, "wolfSSL_ASN1_STRING_print_ex()");
  26034. /* setup */
  26035. XMEMSET(rbuf, 0, 255);
  26036. bio = BIO_new(BIO_s_mem());
  26037. BIO_set_write_buf_size(bio,255);
  26038. asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  26039. ASN1_STRING_set(asn_str, (const void*)data, sizeof(data));
  26040. esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  26041. ASN1_STRING_set(esc_str, (const void*)esc_data, sizeof(esc_data));
  26042. /* no flags */
  26043. XMEMSET(rbuf, 0, 255);
  26044. flags = 0;
  26045. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  26046. AssertIntEQ(p_len, 15);
  26047. BIO_read(bio, (void*)rbuf, 15);
  26048. AssertStrEQ((char*)rbuf, "Hello wolfSSL!");
  26049. /* RFC2253 Escape */
  26050. XMEMSET(rbuf, 0, 255);
  26051. flags = ASN1_STRFLGS_ESC_2253;
  26052. p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags);
  26053. AssertIntEQ(p_len, 9);
  26054. BIO_read(bio, (void*)rbuf, 9);
  26055. AssertStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  26056. /* Show type */
  26057. XMEMSET(rbuf, 0, 255);
  26058. flags = ASN1_STRFLGS_SHOW_TYPE;
  26059. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  26060. AssertIntEQ(p_len, 28);
  26061. BIO_read(bio, (void*)rbuf, 28);
  26062. AssertStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  26063. /* Dump All */
  26064. XMEMSET(rbuf, 0, 255);
  26065. flags = ASN1_STRFLGS_DUMP_ALL;
  26066. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  26067. AssertIntEQ(p_len, 31);
  26068. BIO_read(bio, (void*)rbuf, 31);
  26069. AssertStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  26070. /* Dump Der */
  26071. XMEMSET(rbuf, 0, 255);
  26072. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  26073. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  26074. AssertIntEQ(p_len, 35);
  26075. BIO_read(bio, (void*)rbuf, 35);
  26076. AssertStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  26077. /* Dump All + Show type */
  26078. XMEMSET(rbuf, 0, 255);
  26079. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  26080. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  26081. AssertIntEQ(p_len, 44);
  26082. BIO_read(bio, (void*)rbuf, 44);
  26083. AssertStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  26084. BIO_free(bio);
  26085. ASN1_STRING_free(asn_str);
  26086. ASN1_STRING_free(esc_str);
  26087. printf(resultFmt, passed);
  26088. #endif
  26089. }
  26090. static void test_wolfSSL_ASN1_TIME_to_generalizedtime(void){
  26091. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  26092. WOLFSSL_ASN1_TIME *t;
  26093. WOLFSSL_ASN1_TIME *out;
  26094. WOLFSSL_ASN1_TIME *gtime;
  26095. int tlen = 0;
  26096. unsigned char *data;
  26097. printf(testingFmt, "wolfSSL_ASN1_TIME_to_generalizedtime()");
  26098. /* UTC Time test */
  26099. AssertNotNull(t = wolfSSL_ASN1_TIME_new());
  26100. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  26101. AssertNotNull(out = wolfSSL_ASN1_TIME_new());
  26102. t->type = ASN_UTC_TIME;
  26103. t->length = ASN_UTC_TIME_SIZE;
  26104. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  26105. tlen = wolfSSL_ASN1_TIME_get_length(t);
  26106. AssertIntEQ(tlen, ASN_UTC_TIME_SIZE);
  26107. data = wolfSSL_ASN1_TIME_get_data(t);
  26108. AssertStrEQ((char*)data, "050727123456Z");
  26109. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  26110. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  26111. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  26112. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  26113. /* Generalized Time test */
  26114. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  26115. XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE);
  26116. XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE);
  26117. t->type = ASN_GENERALIZED_TIME;
  26118. t->length = ASN_GENERALIZED_TIME_SIZE;
  26119. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  26120. tlen = wolfSSL_ASN1_TIME_get_length(t);
  26121. AssertIntEQ(tlen, ASN_GENERALIZED_TIME_SIZE);
  26122. data = wolfSSL_ASN1_TIME_get_data(t);
  26123. AssertStrEQ((char*)data, "20050727123456Z");
  26124. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  26125. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  26126. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  26127. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  26128. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26129. /* Null parameter test */
  26130. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  26131. gtime = NULL;
  26132. out = NULL;
  26133. t->type = ASN_UTC_TIME;
  26134. t->length = ASN_UTC_TIME_SIZE;
  26135. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  26136. AssertNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  26137. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  26138. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  26139. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  26140. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26141. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26142. printf(resultFmt, passed);
  26143. #endif
  26144. }
  26145. static void test_wolfSSL_X509_CA_num(void){
  26146. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  26147. defined(HAVE_ECC) && !defined(NO_RSA)
  26148. WOLFSSL_X509_STORE *store;
  26149. WOLFSSL_X509 *x509_1, *x509_2;
  26150. int ca_num = 0;
  26151. printf(testingFmt, "wolfSSL_X509_CA_num()");
  26152. store = wolfSSL_X509_STORE_new();
  26153. x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  26154. wolfSSL_X509_STORE_add_cert(store, x509_1);
  26155. ca_num = wolfSSL_X509_CA_num(store);
  26156. AssertIntEQ(ca_num, 1);
  26157. x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile, WOLFSSL_FILETYPE_PEM);
  26158. wolfSSL_X509_STORE_add_cert(store, x509_2);
  26159. ca_num = wolfSSL_X509_CA_num(store);
  26160. AssertIntEQ(ca_num, 2);
  26161. wolfSSL_X509_free(x509_1);
  26162. wolfSSL_X509_free(x509_2);
  26163. wolfSSL_X509_STORE_free(store);
  26164. printf(resultFmt, passed);
  26165. #endif
  26166. }
  26167. static void test_wolfSSL_X509_check_ca(void){
  26168. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  26169. WOLFSSL_X509 *x509;
  26170. printf(testingFmt, "wolfSSL_X509_check_ca()");
  26171. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  26172. AssertIntEQ(wolfSSL_X509_check_ca(x509), 1);
  26173. wolfSSL_X509_free(x509);
  26174. x509 = wolfSSL_X509_load_certificate_file(ntruCertFile, WOLFSSL_FILETYPE_PEM);
  26175. AssertIntEQ(wolfSSL_X509_check_ca(x509), 0);
  26176. wolfSSL_X509_free(x509);
  26177. printf(resultFmt, passed);
  26178. #endif
  26179. }
  26180. static void test_wolfSSL_X509_check_ip_asc(void){
  26181. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  26182. WOLFSSL_X509 *x509;
  26183. printf(testingFmt, "wolfSSL_X509_check_ip_asc()");
  26184. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  26185. #if 0
  26186. /* TODO: add cert gen for testing positive case */
  26187. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  26188. #endif
  26189. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  26190. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  26191. wolfSSL_X509_free(x509);
  26192. printf(resultFmt, passed);
  26193. #endif
  26194. }
  26195. static void test_wolfSSL_DC_cert(void)
  26196. {
  26197. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  26198. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_KEY_GEN) && \
  26199. defined(WOLFSSL_CERT_EXT)
  26200. Cert cert;
  26201. RsaKey key;
  26202. WC_RNG rng;
  26203. byte der[FOURK_BUF];
  26204. int certSz;
  26205. int ret, idx;
  26206. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  26207. const unsigned char* pt;
  26208. X509* x509;
  26209. X509_NAME* x509name;
  26210. X509_NAME_ENTRY* entry;
  26211. ASN1_STRING* entryValue;
  26212. CertName name;
  26213. printf(testingFmt, "wolfSSL Certs with DC");
  26214. XMEMSET(&name, 0, sizeof(CertName));
  26215. /* set up cert name */
  26216. XMEMCPY(name.country, "US", sizeof("US"));
  26217. name.countryEnc = CTC_PRINTABLE;
  26218. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  26219. name.stateEnc = CTC_UTF8;
  26220. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  26221. name.localityEnc = CTC_UTF8;
  26222. XMEMCPY(name.sur, "Test", sizeof("Test"));
  26223. name.surEnc = CTC_UTF8;
  26224. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  26225. name.orgEnc = CTC_UTF8;
  26226. XMEMCPY(name.unit, "Development", sizeof("Development"));
  26227. name.unitEnc = CTC_UTF8;
  26228. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  26229. name.commonNameEnc = CTC_UTF8;
  26230. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  26231. name.serialDevEnc = CTC_PRINTABLE;
  26232. #ifdef WOLFSSL_MULTI_ATTRIB
  26233. #if CTC_MAX_ATTRIB > 2
  26234. {
  26235. NameAttrib* n;
  26236. n = &name.name[0];
  26237. n->id = ASN_DOMAIN_COMPONENT;
  26238. n->type = CTC_UTF8;
  26239. n->sz = sizeof("com");
  26240. XMEMCPY(n->value, "com", sizeof("com"));
  26241. n = &name.name[1];
  26242. n->id = ASN_DOMAIN_COMPONENT;
  26243. n->type = CTC_UTF8;
  26244. n->sz = sizeof("wolfssl");
  26245. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  26246. }
  26247. #endif
  26248. #endif /* WOLFSSL_MULTI_ATTRIB */
  26249. AssertIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  26250. #ifndef HAVE_FIPS
  26251. AssertIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, devId), 0);
  26252. #else
  26253. AssertIntEQ(wc_InitRng(&rng), 0);
  26254. #endif
  26255. AssertIntEQ(wc_MakeRsaKey(&key, 1024, 3, &rng), 0);
  26256. XMEMSET(&cert, 0 , sizeof(Cert));
  26257. AssertIntEQ(wc_InitCert(&cert), 0);
  26258. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  26259. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  26260. cert.serialSz = (int)sizeof(mySerial);
  26261. cert.isCA = 1;
  26262. #ifndef NO_SHA256
  26263. cert.sigType = CTC_SHA256wRSA;
  26264. #else
  26265. cert.sigType = CTC_SHAwRSA;
  26266. #endif
  26267. /* add SKID from the Public Key */
  26268. AssertIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  26269. /* add AKID from the Public Key */
  26270. AssertIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  26271. ret = 0;
  26272. do {
  26273. #if defined(WOLFSSL_ASYNC_CRYPT)
  26274. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26275. #endif
  26276. if (ret >= 0) {
  26277. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  26278. }
  26279. } while (ret == WC_PENDING_E);
  26280. AssertIntGT(ret, 0);
  26281. certSz = ret;
  26282. /* der holds a certificate with DC's now check X509 parsing of it */
  26283. pt = der;
  26284. AssertNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  26285. AssertNotNull(x509name = X509_get_subject_name(x509));
  26286. #ifdef WOLFSSL_MULTI_ATTRIB
  26287. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  26288. -1)), 5);
  26289. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  26290. idx)), 6);
  26291. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  26292. idx)), -1);
  26293. #endif /* WOLFSSL_MULTI_ATTRIB */
  26294. /* compare DN at index 0 */
  26295. AssertNotNull(entry = X509_NAME_get_entry(x509name, 0));
  26296. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  26297. AssertIntEQ(ASN1_STRING_length(entryValue), 2);
  26298. AssertStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  26299. #ifdef WOLFSSL_MULTI_ATTRIB
  26300. /* get first and second DC and compare result */
  26301. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  26302. -1)), 5);
  26303. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  26304. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  26305. AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  26306. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  26307. idx)), 6);
  26308. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  26309. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  26310. AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  26311. #endif /* WOLFSSL_MULTI_ATTRIB */
  26312. /* try invalid index locations for regression test and sanity check */
  26313. AssertNull(entry = X509_NAME_get_entry(x509name, 11));
  26314. AssertNull(entry = X509_NAME_get_entry(x509name, 20));
  26315. (void)idx;
  26316. X509_free(x509);
  26317. wc_FreeRsaKey(&key);
  26318. wc_FreeRng(&rng);
  26319. printf(resultFmt, passed);
  26320. #endif
  26321. }
  26322. static void test_wolfSSL_X509_get_version(void){
  26323. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  26324. WOLFSSL_X509 *x509;
  26325. printf(testingFmt, "wolfSSL_X509_get_version()");
  26326. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  26327. AssertNotNull(x509);
  26328. AssertIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  26329. wolfSSL_X509_free(x509);
  26330. printf(resultFmt, passed);
  26331. #endif
  26332. }
  26333. static void test_wolfSSL_DES_ncbc(void){
  26334. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  26335. const_DES_cblock myDes;
  26336. DES_cblock iv = {1};
  26337. DES_key_schedule key = {0};
  26338. unsigned char msg[] = "hello wolfssl";
  26339. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  26340. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  26341. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  26342. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  26343. printf(testingFmt, "wolfSSL_DES_ncbc()");
  26344. /* partial block test */
  26345. DES_set_key(&key, &myDes);
  26346. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  26347. AssertIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  26348. AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  26349. DES_set_key(&key, &myDes);
  26350. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  26351. *((byte*)&iv) = 1;
  26352. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  26353. AssertIntEQ(XMEMCMP(msg, pln, 3), 0);
  26354. AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  26355. /* full block test */
  26356. DES_set_key(&key, &myDes);
  26357. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  26358. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  26359. *((byte*)&iv) = 1;
  26360. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  26361. AssertIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  26362. AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  26363. DES_set_key(&key, &myDes);
  26364. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  26365. *((byte*)&iv) = 1;
  26366. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  26367. AssertIntEQ(XMEMCMP(msg, pln, 8), 0);
  26368. AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  26369. printf(resultFmt, passed);
  26370. #endif
  26371. }
  26372. static void test_wolfSSL_AES_cbc_encrypt()
  26373. {
  26374. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA)
  26375. AES_KEY aes;
  26376. AES_KEY* aesN = NULL;
  26377. size_t len = 0;
  26378. size_t lenB = 0;
  26379. int keySz0 = 0;
  26380. int keySzN = -1;
  26381. byte out[AES_BLOCK_SIZE] = {0};
  26382. byte* outN = NULL;
  26383. const int enc1 = AES_ENCRYPT;
  26384. const int enc2 = AES_DECRYPT;
  26385. /* Test vectors retrieved from:
  26386. * <begin URL>
  26387. * https://csrc.nist.gov/
  26388. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  26389. * documents/aes/KAT_AES.zip
  26390. * </end URL>
  26391. */
  26392. const byte* pt128N = NULL;
  26393. byte* key128N = NULL;
  26394. byte* iv128N = NULL;
  26395. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  26396. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26397. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  26398. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  26399. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  26400. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26401. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  26402. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  26403. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  26404. len = sizeof(pt128);
  26405. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  26406. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  26407. AssertIntNE(XMEMCMP(b, g, h), i)
  26408. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  26409. printf(testingFmt, "Stressing wolfSSL_AES_cbc_encrypt()");
  26410. STRESS_T(pt128N, out, len, &aes, iv128tmp, enc1, ct128, AES_BLOCK_SIZE, 0);
  26411. STRESS_T(pt128, out, len, &aes, iv128N, enc1, ct128, AES_BLOCK_SIZE, 0);
  26412. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, enc1);
  26413. AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  26414. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, enc1);
  26415. AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  26416. STRESS_T(pt128, out, lenB, &aes, iv128tmp, enc1, ct128, AES_BLOCK_SIZE, 0);
  26417. printf(resultFmt, "Stress Tests: passed");
  26418. printf(testingFmt, "Stressing wolfSSL_AES_set_encrypt_key");
  26419. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  26420. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  26421. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  26422. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  26423. printf(resultFmt, "Stress Tests: passed");
  26424. printf(testingFmt, "Stressing wolfSSL_AES_set_decrypt_key");
  26425. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  26426. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  26427. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  26428. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  26429. printf(resultFmt, "Stress Tests: passed");
  26430. #ifdef WOLFSSL_AES_128
  26431. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 128-bit");
  26432. XMEMSET(out, 0, AES_BLOCK_SIZE);
  26433. RESET_IV(iv128tmp, iv128);
  26434. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  26435. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, enc1);
  26436. AssertIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  26437. printf(resultFmt, "passed");
  26438. #ifdef HAVE_AES_DECRYPT
  26439. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode");
  26440. XMEMSET(out, 0, AES_BLOCK_SIZE);
  26441. RESET_IV(iv128tmp, iv128);
  26442. len = sizeof(ct128);
  26443. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  26444. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, enc2);
  26445. AssertIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  26446. printf(resultFmt, "passed");
  26447. #endif
  26448. #endif /* WOLFSSL_AES_128 */
  26449. #ifdef WOLFSSL_AES_192
  26450. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  26451. * Appendix F.2.3 */
  26452. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  26453. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  26454. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  26455. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  26456. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  26457. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  26458. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  26459. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  26460. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  26461. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  26462. len = sizeof(pt192);
  26463. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 192-bit");
  26464. XMEMSET(out, 0, AES_BLOCK_SIZE);
  26465. RESET_IV(iv192tmp, iv192);
  26466. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  26467. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, enc1);
  26468. AssertIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  26469. printf(resultFmt, "passed");
  26470. #ifdef HAVE_AES_DECRYPT
  26471. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode");
  26472. len = sizeof(ct192);
  26473. RESET_IV(iv192tmp, iv192);
  26474. XMEMSET(out, 0, AES_BLOCK_SIZE);
  26475. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  26476. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, enc2);
  26477. AssertIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  26478. printf(resultFmt, "passed");
  26479. #endif
  26480. #endif /* WOLFSSL_AES_192 */
  26481. #ifdef WOLFSSL_AES_256
  26482. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  26483. * Appendix F.2.5 */
  26484. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  26485. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  26486. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  26487. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  26488. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  26489. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  26490. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  26491. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  26492. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  26493. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  26494. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  26495. len = sizeof(pt256);
  26496. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 256-bit");
  26497. XMEMSET(out, 0, AES_BLOCK_SIZE);
  26498. RESET_IV(iv256tmp, iv256);
  26499. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  26500. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, enc1);
  26501. AssertIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  26502. printf(resultFmt, "passed");
  26503. #ifdef HAVE_AES_DECRYPT
  26504. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode");
  26505. len = sizeof(ct256);
  26506. RESET_IV(iv256tmp, iv256);
  26507. XMEMSET(out, 0, AES_BLOCK_SIZE);
  26508. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  26509. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, enc2);
  26510. AssertIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  26511. printf(resultFmt, "passed");
  26512. #endif
  26513. #endif /* WOLFSSL_AES_256 */
  26514. #endif
  26515. }
  26516. #if defined(OPENSSL_ALL)
  26517. #if !defined(NO_ASN)
  26518. static void test_wolfSSL_ASN1_STRING_to_UTF8(void)
  26519. {
  26520. WOLFSSL_X509* x509;
  26521. WOLFSSL_X509_NAME* subject;
  26522. WOLFSSL_X509_NAME_ENTRY* e;
  26523. WOLFSSL_ASN1_STRING* a;
  26524. FILE* file;
  26525. int idx = 0;
  26526. char targetOutput[16] = "www.wolfssl.com";
  26527. unsigned char* actual_output;
  26528. int len = 0;
  26529. int result = 0;
  26530. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  26531. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  26532. fclose(file);
  26533. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName");
  26534. AssertNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  26535. AssertIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  26536. NID_commonName, -1)), 5);
  26537. AssertNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  26538. AssertNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  26539. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  26540. result = strncmp((const char*)actual_output, targetOutput, len);
  26541. AssertIntEQ(result, 0);
  26542. printf(resultFmt, result == 0 ? passed : failed);
  26543. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(NULL, valid): ");
  26544. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)),
  26545. WOLFSSL_FATAL_ERROR);
  26546. printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
  26547. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(valid, NULL): ");
  26548. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)),
  26549. WOLFSSL_FATAL_ERROR);
  26550. printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
  26551. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL): ");
  26552. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)),
  26553. WOLFSSL_FATAL_ERROR);
  26554. printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
  26555. wolfSSL_X509_free(x509);
  26556. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26557. }
  26558. #endif /* !defined(NO_ASN) */
  26559. static void test_wolfSSL_sk_CIPHER_description(void)
  26560. {
  26561. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  26562. int i,j,k;
  26563. int numCiphers = 0;
  26564. const SSL_METHOD *method = NULL;
  26565. const SSL_CIPHER *cipher = NULL;
  26566. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  26567. SSL_CTX *ctx = NULL;
  26568. SSL *ssl = NULL;
  26569. char buf[256];
  26570. char test_str[9] = "0000000";
  26571. const char badStr[] = "unknown";
  26572. const char certPath[] = "./certs/client-cert.pem";
  26573. XMEMSET(buf, 0, sizeof(buf));
  26574. printf(testingFmt, "wolfSSL_sk_CIPHER_description");
  26575. AssertNotNull(method = TLSv1_2_client_method());
  26576. AssertNotNull(ctx = SSL_CTX_new(method));
  26577. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  26578. SSL_CTX_set_verify_depth(ctx, 4);
  26579. SSL_CTX_set_options(ctx, flags);
  26580. AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  26581. WOLFSSL_SUCCESS);
  26582. AssertNotNull(ssl = SSL_new(ctx));
  26583. /* SSL_get_ciphers returns a stack of all configured ciphers
  26584. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  26585. */
  26586. AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  26587. /* loop through the amount of supportedCiphers */
  26588. numCiphers = sk_num(supportedCiphers);
  26589. for (i = 0; i < numCiphers; ++i) {
  26590. /* sk_value increments "sk->data.cipher->cipherOffset".
  26591. * wolfSSL_sk_CIPHER_description sets the description for
  26592. * the cipher based on the provided offset.
  26593. */
  26594. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  26595. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  26596. }
  26597. /* Search cipher description string for "unknown" descriptor */
  26598. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  26599. k = 0;
  26600. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  26601. test_str[k] = badStr[k];
  26602. j++;
  26603. k++;
  26604. }
  26605. }
  26606. /* Fail if test_str == badStr == "unknown" */
  26607. AssertStrNE(test_str,badStr);
  26608. }
  26609. SSL_free(ssl);
  26610. SSL_CTX_free(ctx);
  26611. printf(resultFmt, passed);
  26612. }
  26613. static void test_wolfSSL_get_ciphers_compat(void)
  26614. {
  26615. const SSL_METHOD *method = NULL;
  26616. const char certPath[] = "./certs/client-cert.pem";
  26617. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  26618. SSL_CTX *ctx = NULL;
  26619. WOLFSSL *ssl = NULL;
  26620. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  26621. printf(testingFmt, "wolfSSL_get_ciphers_compat");
  26622. method = SSLv23_client_method();
  26623. AssertNotNull(method);
  26624. ctx = SSL_CTX_new(method);
  26625. AssertNotNull(ctx);
  26626. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  26627. SSL_CTX_set_verify_depth(ctx, 4);
  26628. SSL_CTX_set_options(ctx, flags);
  26629. AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  26630. WOLFSSL_SUCCESS);
  26631. AssertNotNull(ssl = SSL_new(ctx));
  26632. /* Test Bad NULL input */
  26633. AssertNull(supportedCiphers = SSL_get_ciphers(NULL));
  26634. /* Test for Good input */
  26635. AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  26636. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  26637. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  26638. SSL_free(ssl);
  26639. SSL_CTX_free(ctx);
  26640. printf(resultFmt, passed);
  26641. }
  26642. static void test_wolfSSL_X509_PUBKEY_get(void)
  26643. {
  26644. WOLFSSL_X509_PUBKEY pubkey;
  26645. WOLFSSL_X509_PUBKEY* key;
  26646. WOLFSSL_EVP_PKEY evpkey;
  26647. WOLFSSL_EVP_PKEY* evpPkey;
  26648. WOLFSSL_EVP_PKEY* retEvpPkey;
  26649. key = &pubkey;
  26650. evpPkey = &evpkey;
  26651. evpPkey->type = WOLFSSL_SUCCESS;
  26652. key->pkey = evpPkey;
  26653. printf(testingFmt, "wolfSSL_X509_PUBKEY_get()");
  26654. AssertNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  26655. AssertIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  26656. AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  26657. key->pkey = NULL;
  26658. AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  26659. printf(resultFmt,retEvpPkey == NULL ? passed : failed);
  26660. }
  26661. static void test_wolfSSL_d2i_DHparams()
  26662. {
  26663. #if !defined(NO_DH)
  26664. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  26665. FILE* f = NULL;
  26666. unsigned char buf[4096];
  26667. const unsigned char* pt = buf;
  26668. const char* params1 = "./certs/dh2048.der";
  26669. const char* params2 = "./certs/dh3072.der";
  26670. long len = 0;
  26671. WOLFSSL_DH* dh = NULL;
  26672. XMEMSET(buf, 0, sizeof(buf));
  26673. /* Test 2048 bit parameters */
  26674. printf(testingFmt, "wolfSSL_d2i_DHparams() 2048-bit");
  26675. f = XFOPEN(params1, "rb");
  26676. AssertTrue(f != XBADFILE);
  26677. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  26678. XFCLOSE(f);
  26679. /* Valid case */
  26680. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  26681. AssertNotNull(dh->p);
  26682. AssertNotNull(dh->g);
  26683. AssertTrue(pt != buf);
  26684. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  26685. /* Invalid cases */
  26686. AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  26687. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  26688. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  26689. DH_free(dh);
  26690. printf(resultFmt, passed);
  26691. *buf = 0;
  26692. pt = buf;
  26693. /* Test 3072 bit parameters */
  26694. printf(testingFmt, "wolfSSL_d2i_DHparams() 3072-bit");
  26695. f = XFOPEN(params2, "rb");
  26696. AssertTrue(f != XBADFILE);
  26697. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  26698. XFCLOSE(f);
  26699. /* Valid case */
  26700. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  26701. AssertNotNull(dh->p);
  26702. AssertNotNull(dh->g);
  26703. AssertTrue(pt != buf);
  26704. AssertIntEQ(DH_generate_key(dh), 1);
  26705. /* Invalid cases */
  26706. AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  26707. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  26708. DH_free(dh);
  26709. printf(resultFmt, passed);
  26710. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  26711. #endif /* !NO_DH */
  26712. }
  26713. static void test_wolfSSL_i2d_DHparams()
  26714. {
  26715. #if !defined(NO_DH)
  26716. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  26717. FILE* f;
  26718. unsigned char buf[4096];
  26719. const unsigned char* pt = buf;
  26720. unsigned char* pt2 = buf;
  26721. const char* params1 = "./certs/dh2048.der";
  26722. const char* params2 = "./certs/dh3072.der";
  26723. long len;
  26724. WOLFSSL_DH* dh;
  26725. /* Test 2048 bit parameters */
  26726. printf(testingFmt, "wolfSSL_i2d_DHparams() 2048-bit");
  26727. f = XFOPEN(params1, "rb");
  26728. AssertTrue(f != XBADFILE);
  26729. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  26730. XFCLOSE(f);
  26731. /* Valid case */
  26732. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  26733. AssertTrue(pt != buf);
  26734. AssertIntEQ(DH_generate_key(dh), 1);
  26735. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  26736. /* Invalid cases */
  26737. AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  26738. AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 264);
  26739. DH_free(dh);
  26740. printf(resultFmt, passed);
  26741. *buf = 0;
  26742. pt = buf;
  26743. pt2 = buf;
  26744. /* Test 3072 bit parameters */
  26745. printf(testingFmt, "wolfSSL_i2d_DHparams() 3072-bit");
  26746. f = XFOPEN(params2, "rb");
  26747. AssertTrue(f != XBADFILE);
  26748. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  26749. XFCLOSE(f);
  26750. /* Valid case */
  26751. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  26752. AssertTrue(pt != buf);
  26753. AssertIntEQ(DH_generate_key(dh), 1);
  26754. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  26755. /* Invalid cases */
  26756. AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  26757. AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 392);
  26758. DH_free(dh);
  26759. printf(resultFmt, passed);
  26760. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  26761. #endif
  26762. }
  26763. static void test_wolfSSL_EC_KEY_dup(void)
  26764. {
  26765. #if defined(HAVE_ECC) && (defined(OPENSSL_EXTRA) || \
  26766. defined(OPENSSL_EXTRA_X509_SMALL))
  26767. WOLFSSL_EC_KEY* ecKey;
  26768. WOLFSSL_EC_KEY* dupKey;
  26769. ecc_key* srcKey;
  26770. ecc_key* destKey;
  26771. printf(testingFmt, "wolfSSL_EC_KEY_dup()");
  26772. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  26773. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  26774. /* Valid cases */
  26775. AssertNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  26776. AssertIntEQ(wc_ecc_check_key((ecc_key*)dupKey->internal), 0);
  26777. /* Compare pubkey */
  26778. srcKey = (ecc_key*)ecKey->internal;
  26779. destKey = (ecc_key*)dupKey->internal;
  26780. AssertIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  26781. /* compare EC_GROUP */
  26782. AssertIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  26783. /* compare EC_POINT */
  26784. AssertIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  26785. dupKey->pub_key, NULL), MP_EQ);
  26786. /* compare BIGNUM */
  26787. AssertIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  26788. wolfSSL_EC_KEY_free(dupKey);
  26789. /* Invalid cases */
  26790. /* NULL key */
  26791. AssertNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  26792. /* NULL ecc_key */
  26793. wc_ecc_free((ecc_key*)ecKey->internal);
  26794. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  26795. ecKey->internal = NULL; /* Set ecc_key to NULL */
  26796. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  26797. wolfSSL_EC_KEY_free(ecKey);
  26798. wolfSSL_EC_KEY_free(dupKey);
  26799. /* NULL Group */
  26800. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  26801. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  26802. wolfSSL_EC_GROUP_free(ecKey->group);
  26803. ecKey->group = NULL; /* Set group to NULL */
  26804. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  26805. wolfSSL_EC_KEY_free(ecKey);
  26806. wolfSSL_EC_KEY_free(dupKey);
  26807. /* NULL public key */
  26808. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  26809. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  26810. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  26811. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  26812. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  26813. wolfSSL_EC_POINT_free(ecKey->pub_key);
  26814. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  26815. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  26816. wolfSSL_EC_KEY_free(ecKey);
  26817. wolfSSL_EC_KEY_free(dupKey);
  26818. /* NULL private key */
  26819. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  26820. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  26821. wolfSSL_BN_free(ecKey->priv_key);
  26822. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  26823. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  26824. wolfSSL_EC_KEY_free(ecKey);
  26825. wolfSSL_EC_KEY_free(dupKey);
  26826. printf(resultFmt, passed);
  26827. #endif
  26828. }
  26829. static void test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  26830. {
  26831. #if !defined(NO_DSA)
  26832. DSA *dsa = NULL;
  26833. DSA *setDsa = NULL;
  26834. EVP_PKEY *pkey = NULL;
  26835. EVP_PKEY *set1Pkey = NULL;
  26836. SHA_CTX sha;
  26837. byte signature[DSA_SIG_SIZE];
  26838. byte hash[WC_SHA_DIGEST_SIZE];
  26839. word32 bytes;
  26840. int answer;
  26841. #ifdef USE_CERT_BUFFERS_1024
  26842. const unsigned char* dsaKeyDer = dsa_key_der1024;
  26843. int dsaKeySz = sizeof_dsa_key_der_1024;
  26844. byte tmp[ONEK_BUF];
  26845. XMEMSET(tmp, 0, sizeof(tmp));
  26846. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  26847. bytes = dsa_key_der_sz;
  26848. #elif defined(USE_CERT_BUFFERS_2048)
  26849. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  26850. int dsaKeySz = sizeof_dsa_key_der_2048;
  26851. byte tmp[TWOK_BUF];
  26852. XMEMSET(tmp, 0, sizeof(tmp));
  26853. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  26854. bytes = dsaKeySz;
  26855. #else
  26856. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  26857. int dsaKeySz = sizeof_dsa_key_der_2048;
  26858. byte tmp[TWOK_BUF];
  26859. XMEMSET(tmp, 0, sizeof(tmp));
  26860. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  26861. XFILE fp = XOPEN("./certs/dsa2048.der", "rb");
  26862. if (fp == XBADFILE) {
  26863. return WOLFSSL_BAD_FILE;
  26864. }
  26865. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  26866. XFCLOSE(fp);
  26867. #endif /* END USE_CERT_BUFFERS_1024 */
  26868. printf(testingFmt,
  26869. "wolfSSL_EVP_PKEY_set1_DSA and wolfSSL_EVP_PKEY_get1_DSA");
  26870. /* Create hash to later Sign and Verify */
  26871. AssertIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  26872. AssertIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  26873. AssertIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  26874. /* Initialize pkey with der format dsa key */
  26875. AssertNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey,
  26876. &dsaKeyDer ,(long)dsaKeySz));
  26877. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  26878. /* Should Fail: NULL argument */
  26879. AssertNull(dsa = EVP_PKEY_get0_DSA(NULL));
  26880. AssertNull(dsa = EVP_PKEY_get1_DSA(NULL));
  26881. /* Should Pass: Initialized pkey argument */
  26882. AssertNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  26883. AssertNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  26884. AssertIntEQ(DSA_bits(dsa), 2048);
  26885. /* Sign */
  26886. AssertIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  26887. /* Verify. */
  26888. AssertIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  26889. WOLFSSL_SUCCESS);
  26890. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  26891. /* Should Fail: set1Pkey not initialized */
  26892. AssertIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  26893. /* Initialize set1Pkey */
  26894. set1Pkey = EVP_PKEY_new();
  26895. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  26896. AssertIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  26897. WOLFSSL_SUCCESS);
  26898. /* Should Pass: set dsa into set1Pkey */
  26899. AssertIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  26900. printf(resultFmt, passed);
  26901. DSA_free(dsa);
  26902. DSA_free(setDsa);
  26903. EVP_PKEY_free(pkey);
  26904. EVP_PKEY_free(set1Pkey);
  26905. #endif /* NO_DSA */
  26906. } /* END test_EVP_PKEY_set1_get1_DSA */
  26907. static void test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  26908. {
  26909. #ifdef HAVE_ECC
  26910. WOLFSSL_EC_KEY *ecKey = NULL;
  26911. WOLFSSL_EC_KEY *ecGet1 = NULL;
  26912. EVP_PKEY *pkey = NULL;
  26913. printf(testingFmt,
  26914. "wolfSSL_EVP_PKEY_set1_EC_KEY and wolfSSL_EVP_PKEY_get1_EC_KEY");
  26915. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  26916. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  26917. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  26918. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  26919. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  26920. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  26921. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  26922. AssertNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  26923. AssertNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  26924. wolfSSL_EC_KEY_free(ecKey);
  26925. wolfSSL_EC_KEY_free(ecGet1);
  26926. EVP_PKEY_free(pkey);
  26927. /* PASSED */
  26928. printf(resultFmt, passed);
  26929. #endif /* HAVE_ECC */
  26930. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  26931. static void test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  26932. {
  26933. #if !defined(NO_DH)
  26934. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  26935. DH *dh = NULL;
  26936. DH *setDh = NULL;
  26937. EVP_PKEY *pkey = NULL;
  26938. FILE* f = NULL;
  26939. unsigned char buf[4096];
  26940. const unsigned char* pt = buf;
  26941. const char* dh2048 = "./certs/dh2048.der";
  26942. long len = 0;
  26943. int code = -1;
  26944. printf(testingFmt,"wolfSSL_EVP_PKEY_set1_DH and wolfSSL_EVP_PKEY_get1_DH");
  26945. XMEMSET(buf, 0, sizeof(buf));
  26946. f = XFOPEN(dh2048, "rb");
  26947. AssertTrue(f != XBADFILE);
  26948. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  26949. XFCLOSE(f);
  26950. /* Load dh2048.der into DH with internal format */
  26951. AssertNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  26952. AssertIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  26953. AssertIntEQ(code, 0);
  26954. code = -1;
  26955. pkey = wolfSSL_EVP_PKEY_new();
  26956. /* Set DH into PKEY */
  26957. AssertIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  26958. /* Get DH from PKEY */
  26959. AssertNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  26960. AssertIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  26961. AssertIntEQ(code, 0);
  26962. EVP_PKEY_free(pkey);
  26963. DH_free(setDh);
  26964. DH_free(dh);
  26965. printf(resultFmt, passed);
  26966. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  26967. #endif /* NO_DH */
  26968. } /* END test_EVP_PKEY_set1_get1_DH */
  26969. static void test_wolfSSL_CTX_ctrl(void)
  26970. {
  26971. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  26972. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  26973. char caFile[] = "./certs/client-ca.pem";
  26974. char clientFile[] = "./certs/client-cert.pem";
  26975. SSL_CTX* ctx;
  26976. X509* x509 = NULL;
  26977. #if !defined(NO_DH) && !defined(NO_DSA)
  26978. byte buf[6000];
  26979. char file[] = "./certs/dsaparams.pem";
  26980. XFILE f;
  26981. int bytes;
  26982. BIO* bio;
  26983. DSA* dsa;
  26984. DH* dh;
  26985. #endif
  26986. #ifdef HAVE_ECC
  26987. WOLFSSL_EC_KEY* ecKey;
  26988. #endif
  26989. printf(testingFmt, "wolfSSL_CTX_ctrl");
  26990. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  26991. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  26992. AssertNotNull(x509);
  26993. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  26994. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  26995. AssertNotNull(x509);
  26996. #if !defined(NO_DH) && !defined(NO_DSA)
  26997. /* Initialize DH */
  26998. f = XFOPEN(file, "rb");
  26999. AssertTrue((f != XBADFILE));
  27000. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  27001. XFCLOSE(f);
  27002. bio = BIO_new_mem_buf((void*)buf, bytes);
  27003. AssertNotNull(bio);
  27004. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  27005. AssertNotNull(dsa);
  27006. dh = wolfSSL_DSA_dup_DH(dsa);
  27007. AssertNotNull(dh);
  27008. #endif
  27009. #ifdef HAVE_ECC
  27010. /* Initialize WOLFSSL_EC_KEY */
  27011. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  27012. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey),1);
  27013. #endif
  27014. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  27015. /* additional test of getting EVP_PKEY key size from X509
  27016. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  27017. * allowed with user RSA */
  27018. {
  27019. EVP_PKEY* pkey;
  27020. #if defined(HAVE_ECC)
  27021. X509* ecX509;
  27022. #endif /* HAVE_ECC */
  27023. AssertNotNull(pkey = X509_get_pubkey(x509));
  27024. /* current RSA key is 2048 bit (256 bytes) */
  27025. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  27026. EVP_PKEY_free(pkey);
  27027. #if defined(HAVE_ECC)
  27028. #if defined(USE_CERT_BUFFERS_256)
  27029. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  27030. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  27031. SSL_FILETYPE_ASN1));
  27032. #else
  27033. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  27034. cliEccCertFile, SSL_FILETYPE_PEM));
  27035. #endif
  27036. AssertNotNull(pkey = X509_get_pubkey(ecX509));
  27037. /* current ECC key is 256 bit (32 bytes) */
  27038. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  27039. X509_free(ecX509);
  27040. EVP_PKEY_free(pkey);
  27041. #endif /* HAVE_ECC */
  27042. }
  27043. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  27044. /* Tests should fail with passed in NULL pointer */
  27045. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,NULL),
  27046. SSL_FAILURE);
  27047. #if !defined(NO_DH) && !defined(NO_DSA)
  27048. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,NULL),
  27049. SSL_FAILURE);
  27050. #endif
  27051. #ifdef HAVE_ECC
  27052. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,NULL),
  27053. SSL_FAILURE);
  27054. #endif
  27055. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  27056. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  27057. */
  27058. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,x509),
  27059. SSL_SUCCESS);
  27060. /* Test with SSL_CTRL_OPTIONS
  27061. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  27062. */
  27063. AssertTrue(wolfSSL_CTX_ctrl(ctx,SSL_CTRL_OPTIONS,SSL_OP_NO_TLSv1,NULL)
  27064. == SSL_OP_NO_TLSv1);
  27065. AssertTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  27066. /* Test with SSL_CTRL_SET_TMP_DH
  27067. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  27068. */
  27069. #if !defined(NO_DH) && !defined(NO_DSA)
  27070. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,dh),
  27071. SSL_SUCCESS);
  27072. #endif
  27073. /* Test with SSL_CTRL_SET_TMP_ECDH
  27074. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  27075. */
  27076. #ifdef HAVE_ECC
  27077. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,ecKey),
  27078. SSL_SUCCESS);
  27079. #endif
  27080. #ifdef WOLFSSL_ENCRYPTED_KEYS
  27081. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  27082. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  27083. #endif
  27084. /* Cleanup and Pass */
  27085. #if !defined(NO_DH) && !defined(NO_DSA)
  27086. BIO_free(bio);
  27087. DSA_free(dsa);
  27088. DH_free(dh);
  27089. #endif
  27090. #ifdef HAVE_ECC
  27091. wolfSSL_EC_KEY_free(ecKey);
  27092. #endif
  27093. SSL_CTX_free(ctx);
  27094. printf(resultFmt, passed);
  27095. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  27096. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  27097. }
  27098. static void test_wolfSSL_DH_check(void)
  27099. {
  27100. #if !defined(NO_DH) && !defined(NO_DSA)
  27101. byte buf[6000];
  27102. char file[] = "./certs/dsaparams.pem";
  27103. XFILE f;
  27104. int bytes;
  27105. BIO* bio;
  27106. DSA* dsa;
  27107. DH* dh = NULL;
  27108. WOLFSSL_BIGNUM* pTmp = NULL;
  27109. WOLFSSL_BIGNUM* gTmp = NULL;
  27110. int codes = -1;
  27111. printf(testingFmt, "wolfSSL_DH_check");
  27112. /* Initialize DH */
  27113. f = XFOPEN(file, "rb");
  27114. AssertTrue((f != XBADFILE));
  27115. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  27116. XFCLOSE(f);
  27117. bio = BIO_new_mem_buf((void*)buf, bytes);
  27118. AssertNotNull(bio);
  27119. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  27120. AssertNotNull(dsa);
  27121. dh = wolfSSL_DSA_dup_DH(dsa);
  27122. AssertNotNull(dh);
  27123. /* Test assumed to be valid dh.
  27124. * Should return WOLFSSL_SUCCESS
  27125. * codes should be 0
  27126. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  27127. */
  27128. AssertIntEQ(wolfSSL_DH_check(dh, &codes), WOLFSSL_SUCCESS);
  27129. AssertIntEQ(codes, 0);
  27130. /* Test NULL dh: expected BAD_FUNC_ARG */
  27131. AssertIntEQ(wolfSSL_DH_check(NULL, &codes), WOLFSSL_FAILURE);
  27132. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  27133. pTmp = dh->p;
  27134. dh->p = NULL;
  27135. AssertIntEQ(wolfSSL_DH_check(dh, &codes), WOLFSSL_FAILURE);
  27136. AssertIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  27137. /* set dh->p back to normal so it wont fail on next tests */
  27138. dh->p = pTmp;
  27139. pTmp = NULL;
  27140. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  27141. gTmp = dh->g;
  27142. dh->g = NULL;
  27143. AssertIntEQ(wolfSSL_DH_check(dh, &codes), WOLFSSL_FAILURE);
  27144. AssertIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  27145. dh->g = gTmp;
  27146. gTmp = NULL;
  27147. /* Cleanup and Pass Test */
  27148. BIO_free(bio);
  27149. DSA_free(dsa);
  27150. DH_free(dh);
  27151. printf(resultFmt, passed);
  27152. #endif /* !NO_DH && !NO_DSA */
  27153. }
  27154. static void test_wolfSSL_EVP_PKEY_assign(void)
  27155. {
  27156. #if defined(OPENSSL_ALL)
  27157. int type;
  27158. WOLFSSL_EVP_PKEY* pkey;
  27159. #ifndef NO_RSA
  27160. WOLFSSL_RSA* rsa;
  27161. #endif
  27162. #ifndef NO_DSA
  27163. WOLFSSL_DSA* dsa;
  27164. #endif
  27165. #ifdef HAVE_ECC
  27166. WOLFSSL_EC_KEY* ecKey;
  27167. #endif
  27168. printf(testingFmt, "wolfSSL_EVP_PKEY_assign");
  27169. #ifndef NO_RSA
  27170. type = EVP_PKEY_RSA;
  27171. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  27172. AssertNotNull(rsa = wolfSSL_RSA_new());
  27173. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,rsa), WOLFSSL_FAILURE);
  27174. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  27175. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,rsa), WOLFSSL_FAILURE);
  27176. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,rsa), WOLFSSL_SUCCESS);
  27177. wolfSSL_EVP_PKEY_free(pkey);
  27178. #endif /* NO_RSA */
  27179. #ifndef NO_DSA
  27180. type = EVP_PKEY_DSA;
  27181. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  27182. AssertNotNull(dsa = wolfSSL_DSA_new());
  27183. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,dsa), WOLFSSL_FAILURE);
  27184. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  27185. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,dsa), WOLFSSL_FAILURE);
  27186. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,dsa), WOLFSSL_SUCCESS);
  27187. wolfSSL_EVP_PKEY_free(pkey);
  27188. #endif /* NO_DSA */
  27189. #ifdef HAVE_ECC
  27190. type = EVP_PKEY_EC;
  27191. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  27192. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  27193. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,ecKey), WOLFSSL_FAILURE);
  27194. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  27195. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,ecKey), WOLFSSL_FAILURE);
  27196. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_SUCCESS);
  27197. wolfSSL_EVP_PKEY_free(pkey);
  27198. #endif /* HAVE_ECC */
  27199. printf(resultFmt, passed);
  27200. #endif /* OPENSSL_ALL */
  27201. }
  27202. static void test_wolfSSL_OBJ_ln(void)
  27203. {
  27204. const int nid_set[] = {
  27205. NID_commonName,
  27206. NID_serialNumber,
  27207. NID_countryName,
  27208. NID_localityName,
  27209. NID_stateOrProvinceName,
  27210. NID_organizationName,
  27211. NID_organizationalUnitName,
  27212. NID_domainComponent,
  27213. NID_businessCategory,
  27214. NID_jurisdictionCountryName,
  27215. NID_jurisdictionStateOrProvinceName,
  27216. NID_emailAddress
  27217. };
  27218. const char* ln_set[] = {
  27219. "commonName",
  27220. "serialNumber",
  27221. "countryName",
  27222. "localityName",
  27223. "stateOrProvinceName",
  27224. "organizationName",
  27225. "organizationalUnitName",
  27226. "domainComponent",
  27227. "businessCategory",
  27228. "jurisdictionCountryName",
  27229. "jurisdictionStateOrProvinceName",
  27230. "emailAddress",
  27231. };
  27232. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  27233. printf(testingFmt, "wolfSSL_OBJ_ln");
  27234. AssertIntEQ(OBJ_ln2nid(NULL), NID_undef);
  27235. #ifdef HAVE_ECC
  27236. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  27237. {
  27238. size_t nCurves = 27;
  27239. EC_builtin_curve r[nCurves];
  27240. nCurves = EC_get_builtin_curves(r,nCurves);
  27241. for (i = 0; i < nCurves; i++) {
  27242. AssertIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  27243. AssertStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  27244. }
  27245. }
  27246. #endif
  27247. #endif
  27248. for (i = 0; i < maxIdx; i++) {
  27249. AssertIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  27250. AssertStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  27251. }
  27252. printf(resultFmt, passed);
  27253. }
  27254. static void test_wolfSSL_OBJ_sn(void)
  27255. {
  27256. int i = 0, maxIdx = 7;
  27257. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  27258. NID_stateOrProvinceName,NID_organizationName,
  27259. NID_organizationalUnitName,NID_emailAddress};
  27260. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  27261. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  27262. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  27263. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  27264. WOLFSSL_EMAIL_ADDR};
  27265. printf(testingFmt, "wolfSSL_OBJ_sn");
  27266. AssertIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  27267. for (i = 0; i < maxIdx; i++) {
  27268. AssertIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  27269. AssertStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  27270. }
  27271. printf(resultFmt, passed);
  27272. }
  27273. #endif /* OPENSSL_ALL */
  27274. static void test_wolfSSL_X509V3_EXT_get(void) {
  27275. #if !defined(NO_FILESYSTEM) && defined (OPENSSL_ALL)
  27276. FILE* f;
  27277. int numOfExt =0;
  27278. int extNid = 0;
  27279. int i = 0;
  27280. WOLFSSL_X509* x509;
  27281. WOLFSSL_X509_EXTENSION* ext;
  27282. const WOLFSSL_v3_ext_method* method;
  27283. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  27284. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  27285. fclose(f);
  27286. printf(testingFmt, "wolfSSL_X509V3_EXT_get() return struct and nid test");
  27287. AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  27288. for (i = 0; i < numOfExt; i++) {
  27289. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  27290. AssertNotNull(extNid = ext->obj->nid);
  27291. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  27292. AssertIntEQ(method->ext_nid, extNid);
  27293. }
  27294. printf(resultFmt, "passed");
  27295. printf(testingFmt, "wolfSSL_X509V3_EXT_get() NULL argument test");
  27296. AssertNull(method = wolfSSL_X509V3_EXT_get(NULL));
  27297. printf(resultFmt, "passed");
  27298. wolfSSL_X509_free(x509);
  27299. #endif
  27300. }
  27301. static void test_wolfSSL_X509V3_EXT(void) {
  27302. #if !defined(NO_FILESYSTEM) && defined (OPENSSL_ALL)
  27303. FILE* f;
  27304. int numOfExt = 0, nid = 0, i = 0, expected, actual;
  27305. char* str;
  27306. unsigned char* data;
  27307. const WOLFSSL_v3_ext_method* method;
  27308. WOLFSSL_X509* x509;
  27309. WOLFSSL_X509_EXTENSION* ext;
  27310. WOLFSSL_X509_EXTENSION* ext2;
  27311. WOLFSSL_ASN1_OBJECT *obj, *adObj;
  27312. WOLFSSL_ASN1_STRING* asn1str;
  27313. WOLFSSL_AUTHORITY_KEYID* aKeyId;
  27314. WOLFSSL_AUTHORITY_INFO_ACCESS* aia;
  27315. WOLFSSL_BASIC_CONSTRAINTS* bc;
  27316. WOLFSSL_ACCESS_DESCRIPTION* ad;
  27317. WOLFSSL_GENERAL_NAME* gn;
  27318. printf(testingFmt, "wolfSSL_X509V3_EXT_d2i()");
  27319. /* Check NULL argument */
  27320. AssertNull(wolfSSL_X509V3_EXT_d2i(NULL));
  27321. /* Using OCSP cert with X509V3 extensions */
  27322. AssertNotNull(f = fopen("./certs/ocsp/root-ca-cert.pem", "rb"));
  27323. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  27324. fclose(f);
  27325. AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  27326. /* Basic Constraints */
  27327. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  27328. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  27329. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  27330. AssertNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  27331. AssertIntEQ(bc->ca, 1);
  27332. AssertNull(bc->pathlen);
  27333. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  27334. i++;
  27335. /* Subject Key Identifier */
  27336. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  27337. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  27338. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  27339. AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  27340. AssertNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  27341. asn1str));
  27342. X509_EXTENSION_free(ext2);
  27343. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  27344. AssertNotNull(method->i2s);
  27345. AssertNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  27346. wolfSSL_ASN1_STRING_free(asn1str);
  27347. actual = strcmp(str,
  27348. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  27349. AssertIntEQ(actual, 0);
  27350. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27351. i++;
  27352. /* Authority Key Identifier */
  27353. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  27354. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  27355. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  27356. AssertNotNull(aKeyId =
  27357. (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(ext));
  27358. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  27359. AssertNotNull(asn1str = aKeyId->keyid);
  27360. AssertNotNull(str =
  27361. wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method, asn1str));
  27362. actual = strcmp(str,
  27363. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  27364. AssertIntEQ(actual, 0);
  27365. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27366. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  27367. i++;
  27368. /* Key Usage */
  27369. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  27370. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  27371. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  27372. AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  27373. AssertNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  27374. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  27375. #ifdef BIG_ENDIAN_ORDER
  27376. actual = data[1];
  27377. #else
  27378. actual = data[0];
  27379. #endif
  27380. AssertIntEQ(actual, expected);
  27381. wolfSSL_ASN1_STRING_free(asn1str);
  27382. #if 1
  27383. i++;
  27384. /* Authority Info Access */
  27385. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  27386. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  27387. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  27388. AssertNotNull(aia =
  27389. (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext));
  27390. AssertIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  27391. /* URI entry is an ACCESS_DESCRIPTION type */
  27392. AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  27393. AssertNotNull(adObj = ad->method);
  27394. /* Make sure nid is OCSP */
  27395. AssertIntEQ(wolfSSL_OBJ_obj2nid(adObj), AIA_OCSP_OID);
  27396. /* GENERAL_NAME stores URI as an ASN1_STRING */
  27397. AssertNotNull(gn = ad->location);
  27398. AssertIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  27399. AssertNotNull(asn1str = gn->d.uniformResourceIdentifier);
  27400. AssertIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  27401. str = (char*)wolfSSL_ASN1_STRING_data(asn1str);
  27402. actual = strcmp(str, "http://127.0.0.1:22220");
  27403. AssertIntEQ(actual, 0);
  27404. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  27405. XFREE(ad, NULL, DYNAMIC_TYPE_X509_EXT);
  27406. #else
  27407. (void) aia; (void) ad; (void) adObj; (void) gn;
  27408. #endif
  27409. wolfSSL_X509_free(x509);
  27410. printf(resultFmt, "passed");
  27411. #endif
  27412. }
  27413. static void test_wolfSSL_X509_get_ext(void){
  27414. #if !defined(NO_FILESYSTEM) && defined (OPENSSL_ALL)
  27415. int ret = 0;
  27416. FILE* f;
  27417. WOLFSSL_X509* x509;
  27418. WOLFSSL_X509_EXTENSION* foundExtension;
  27419. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  27420. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  27421. fclose(f);
  27422. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  27423. printf(testingFmt, "wolfSSL_X509_get_ext() valid input");
  27424. AssertNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  27425. printf(resultFmt, "passed");
  27426. printf(testingFmt, "wolfSSL_X509_get_ext() valid x509, idx out of bounds");
  27427. AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  27428. AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  27429. printf(resultFmt, "passed");
  27430. printf(testingFmt, "wolfSSL_X509_get_ext() NULL x509, idx out of bounds");
  27431. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  27432. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  27433. printf(resultFmt, "passed");
  27434. printf(testingFmt, "wolfSSL_X509_get_ext() NULL x509, valid idx");
  27435. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  27436. printf(resultFmt, "passed");
  27437. wolfSSL_X509_free(x509);
  27438. #endif
  27439. }
  27440. static void test_wolfSSL_X509_get_ext_by_NID(void)
  27441. {
  27442. #if defined(OPENSSL_ALL)
  27443. int rc;
  27444. FILE* f;
  27445. WOLFSSL_X509* x509;
  27446. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  27447. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  27448. fclose(f);
  27449. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
  27450. AssertIntGE(rc, 0);
  27451. /* Start search from last location (should fail) */
  27452. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, rc);
  27453. AssertIntGE(rc, -1);
  27454. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -2);
  27455. AssertIntGE(rc, -1);
  27456. rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints, -1);
  27457. AssertIntEQ(rc, -1);
  27458. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1);
  27459. AssertIntEQ(rc, -1);
  27460. wolfSSL_X509_free(x509);
  27461. #endif
  27462. }
  27463. static void test_wolfSSL_X509_EXTENSION_new(void)
  27464. {
  27465. #if defined (OPENSSL_ALL)
  27466. WOLFSSL_X509_EXTENSION* ext;
  27467. AssertNotNull(ext = wolfSSL_X509_EXTENSION_new());
  27468. AssertNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  27469. ext->obj->nid = WOLFSSL_SUCCESS;
  27470. AssertIntEQ(WOLFSSL_SUCCESS, ext->obj->nid);
  27471. wolfSSL_X509_EXTENSION_free(ext);
  27472. #endif
  27473. }
  27474. static void test_wolfSSL_X509_EXTENSION_get_object(void)
  27475. {
  27476. #if !defined(NO_FILESYSTEM) && defined (OPENSSL_ALL)
  27477. WOLFSSL_X509* x509;
  27478. WOLFSSL_X509_EXTENSION* ext;
  27479. WOLFSSL_ASN1_OBJECT* o;
  27480. FILE* file;
  27481. int nid = 0;
  27482. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  27483. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  27484. fclose(file);
  27485. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_object() testing ext idx 0");
  27486. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  27487. AssertNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  27488. AssertIntEQ(o->nid, 128);
  27489. nid = o->nid;
  27490. printf(resultFmt, nid == 128 ? passed : failed);
  27491. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_object() NULL argument");
  27492. AssertNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  27493. printf(resultFmt, passed);
  27494. wolfSSL_X509_free(x509);
  27495. #endif
  27496. }
  27497. static void test_wolfSSL_X509_EXTENSION_get_data(void)
  27498. {
  27499. #if !defined(NO_FILESYSTEM) && defined (OPENSSL_ALL)
  27500. WOLFSSL_X509* x509;
  27501. WOLFSSL_X509_EXTENSION* ext;
  27502. WOLFSSL_ASN1_STRING* str;
  27503. FILE* file;
  27504. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_data");
  27505. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  27506. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  27507. fclose(file);
  27508. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  27509. AssertNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  27510. printf(resultFmt, passed);
  27511. wolfSSL_X509_free(x509);
  27512. #endif
  27513. }
  27514. static void test_wolfSSL_X509_EXTENSION_get_critical(void)
  27515. {
  27516. #if !defined(NO_FILESYSTEM) && defined (OPENSSL_ALL)
  27517. WOLFSSL_X509* x509;
  27518. WOLFSSL_X509_EXTENSION* ext;
  27519. FILE* file;
  27520. int crit;
  27521. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_critical");
  27522. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  27523. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  27524. fclose(file);
  27525. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  27526. crit = wolfSSL_X509_EXTENSION_get_critical(ext);
  27527. AssertIntEQ(crit, 0);
  27528. printf(resultFmt, passed);
  27529. wolfSSL_X509_free(x509);
  27530. #endif
  27531. }
  27532. static void test_wolfSSL_X509V3_EXT_print(void)
  27533. {
  27534. #if !defined(NO_FILESYSTEM) && defined (OPENSSL_ALL)
  27535. printf(testingFmt, "wolfSSL_X509V3_EXT_print");
  27536. {
  27537. FILE* f;
  27538. WOLFSSL_X509* x509;
  27539. X509_EXTENSION * ext = NULL;
  27540. int loc;
  27541. BIO *bio = NULL;
  27542. AssertNotNull(f = fopen(svrCertFile, "rb"));
  27543. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  27544. fclose(f);
  27545. AssertNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  27546. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
  27547. AssertIntGT(loc, -1);
  27548. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  27549. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  27550. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_subject_key_identifier, -1);
  27551. AssertIntGT(loc, -1);
  27552. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  27553. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  27554. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_authority_key_identifier, -1);
  27555. AssertIntGT(loc, -1);
  27556. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  27557. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  27558. wolfSSL_BIO_free(bio);
  27559. wolfSSL_X509_free(x509);
  27560. }
  27561. {
  27562. X509 *x509;
  27563. BIO *bio;
  27564. X509_EXTENSION *ext;
  27565. unsigned int i;
  27566. unsigned int idx;
  27567. /* Some NIDs to test with */
  27568. int nids[] = {
  27569. /* NID_key_usage, currently X509_get_ext returns this as a bit
  27570. * string, which messes up X509V3_EXT_print */
  27571. /* NID_ext_key_usage, */
  27572. NID_subject_alt_name,
  27573. };
  27574. int* n;
  27575. printf(testingFmt, "wolfSSL_X509V3_EXT_print");
  27576. AssertNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  27577. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  27578. WOLFSSL_FILETYPE_PEM));
  27579. printf("\nPrinting extension values:\n");
  27580. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  27581. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  27582. * update the index */
  27583. AssertIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  27584. AssertNotNull(ext = X509_get_ext(x509, idx));
  27585. AssertIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  27586. printf("\n");
  27587. }
  27588. BIO_free(bio);
  27589. X509_free(x509);
  27590. }
  27591. printf(resultFmt, passed);
  27592. #endif
  27593. }
  27594. static void test_wolfSSL_X509_cmp(void)
  27595. {
  27596. #if defined(OPENSSL_ALL)
  27597. FILE* file1;
  27598. FILE* file2;
  27599. WOLFSSL_X509* cert1;
  27600. WOLFSSL_X509* cert2;
  27601. int ret = 0;
  27602. AssertNotNull(file1=fopen("./certs/server-cert.pem", "rb"));
  27603. AssertNotNull(file2=fopen("./certs/3072/client-cert.pem", "rb"));
  27604. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  27605. AssertNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  27606. fclose(file1);
  27607. fclose(file2);
  27608. printf(testingFmt, "wolfSSL_X509_cmp() testing matching certs");
  27609. ret = wolfSSL_X509_cmp(cert1, cert1);
  27610. AssertIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  27611. printf(resultFmt, ret == 0 ? passed : failed);
  27612. printf(testingFmt, "wolfSSL_X509_cmp() testing mismatched certs");
  27613. ret = wolfSSL_X509_cmp(cert1, cert2);
  27614. AssertIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  27615. printf(resultFmt, ret == -1 ? passed : failed);
  27616. printf(testingFmt, "wolfSSL_X509_cmp() testing NULL, valid args");
  27617. ret = wolfSSL_X509_cmp(NULL, cert2);
  27618. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  27619. printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
  27620. printf(testingFmt, "wolfSSL_X509_cmp() testing valid, NULL args");
  27621. ret = wolfSSL_X509_cmp(cert1, NULL);
  27622. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  27623. printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
  27624. printf(testingFmt, "wolfSSL_X509_cmp() testing NULL, NULL args");
  27625. ret = wolfSSL_X509_cmp(NULL, NULL);
  27626. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  27627. printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
  27628. wolfSSL_X509_free(cert1);
  27629. wolfSSL_X509_free(cert2);
  27630. #endif
  27631. }
  27632. static void test_wolfSSL_PKEY_up_ref()
  27633. {
  27634. #if defined(OPENSSL_ALL)
  27635. EVP_PKEY* pkey;
  27636. printf(testingFmt, "wolfSSL_PKEY_up_ref()");
  27637. pkey = EVP_PKEY_new();
  27638. AssertIntEQ(EVP_PKEY_up_ref(NULL), 0);
  27639. AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
  27640. EVP_PKEY_free(pkey);
  27641. AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
  27642. EVP_PKEY_free(pkey);
  27643. EVP_PKEY_free(pkey);
  27644. printf(resultFmt, "passed");
  27645. #endif
  27646. }
  27647. static void test_wolfSSL_i2d_PrivateKey()
  27648. {
  27649. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA)
  27650. printf(testingFmt, "wolfSSL_i2d_PrivateKey()");
  27651. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  27652. {
  27653. EVP_PKEY* pkey;
  27654. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  27655. unsigned char buf[FOURK_BUF];
  27656. unsigned char* pt = NULL;
  27657. int bufSz;
  27658. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  27659. (long)sizeof_server_key_der_2048));
  27660. AssertIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  27661. pt = buf;
  27662. AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  27663. AssertIntNE((pt - buf), 0);
  27664. AssertIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  27665. EVP_PKEY_free(pkey);
  27666. }
  27667. #endif
  27668. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  27669. {
  27670. EVP_PKEY* pkey;
  27671. const unsigned char* client_key =
  27672. (const unsigned char*)ecc_clikey_der_256;
  27673. unsigned char buf[FOURK_BUF];
  27674. unsigned char* pt = NULL;
  27675. int bufSz;
  27676. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  27677. sizeof_ecc_clikey_der_256)));
  27678. AssertIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  27679. pt = buf;
  27680. AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  27681. AssertIntNE((pt - buf), 0);
  27682. AssertIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  27683. EVP_PKEY_free(pkey);
  27684. }
  27685. #endif
  27686. printf(resultFmt, "passed");
  27687. #endif
  27688. }
  27689. static void test_wolfSSL_OCSP_get0_info()
  27690. {
  27691. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP) && !defined(NO_FILESYSTEM)
  27692. X509* cert;
  27693. X509* issuer;
  27694. OCSP_CERTID* id;
  27695. ASN1_STRING* name = NULL;
  27696. ASN1_OBJECT* pmd = NULL;
  27697. ASN1_STRING* keyHash = NULL;
  27698. ASN1_INTEGER* serial = NULL;
  27699. ASN1_INTEGER* x509Int;
  27700. printf(testingFmt, "wolfSSL_OCSP_get0_info()");
  27701. AssertNotNull(cert =
  27702. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM));
  27703. AssertNotNull(issuer =
  27704. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
  27705. id = OCSP_cert_to_id(NULL, cert, issuer);
  27706. AssertNotNull(id);
  27707. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  27708. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  27709. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  27710. AssertIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  27711. AssertIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  27712. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  27713. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  27714. AssertNotNull(serial);
  27715. /* compare serial number to one in cert, should be equal */
  27716. x509Int = X509_get_serialNumber(cert);
  27717. AssertNotNull(x509Int);
  27718. AssertIntEQ(x509Int->dataMax, serial->dataMax);
  27719. AssertIntEQ(XMEMCMP(x509Int->data, serial->data, serial->dataMax), 0);
  27720. OCSP_CERTID_free(id);
  27721. X509_free(cert); /* free's x509Int */
  27722. X509_free(issuer);
  27723. printf(resultFmt, "passed");
  27724. #endif /* OPENSSL_EXTRA & HAVE_OCSP */
  27725. }
  27726. static void test_wolfSSL_EVP_PKEY_derive(void)
  27727. {
  27728. #ifdef OPENSSL_ALL
  27729. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  27730. EVP_PKEY_CTX *ctx;
  27731. unsigned char *skey;
  27732. size_t skeylen;
  27733. EVP_PKEY *pkey, *peerkey;
  27734. const unsigned char* key;
  27735. /* DH */
  27736. key = dh_key_der_2048;
  27737. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  27738. sizeof_dh_key_der_2048)));
  27739. AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  27740. key = dh_key_der_2048;
  27741. AssertNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  27742. sizeof_dh_key_der_2048)));
  27743. AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  27744. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  27745. AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
  27746. AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  27747. AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  27748. AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
  27749. AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  27750. EVP_PKEY_CTX_free(ctx);
  27751. EVP_PKEY_free(peerkey);
  27752. EVP_PKEY_free(pkey);
  27753. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  27754. #ifdef HAVE_ECC
  27755. /* ECDH */
  27756. key = ecc_clikey_der_256;
  27757. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  27758. sizeof_ecc_clikey_der_256)));
  27759. key = ecc_clikeypub_der_256;
  27760. AssertNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  27761. sizeof_ecc_clikeypub_der_256)));
  27762. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  27763. AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
  27764. AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  27765. AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  27766. AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
  27767. AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  27768. EVP_PKEY_CTX_free(ctx);
  27769. EVP_PKEY_free(peerkey);
  27770. EVP_PKEY_free(pkey);
  27771. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  27772. #endif /* HAVE_ECC */
  27773. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  27774. #endif /* OPENSSL_ALL */
  27775. }
  27776. static void test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  27777. {
  27778. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  27779. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  27780. RSA *rsa;
  27781. const unsigned char *derBuf = client_key_der_2048;
  27782. unsigned char em[256] = {0}; /* len = 2048/8 */
  27783. /* Random data simulating a hash */
  27784. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  27785. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  27786. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  27787. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  27788. };
  27789. AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  27790. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -1), 1);
  27791. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -1), 1);
  27792. RSA_free(rsa);
  27793. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  27794. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  27795. }
  27796. static void test_wolfSSL_EC_get_builtin_curves(void)
  27797. {
  27798. #if defined(HAVE_ECC) && (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL))
  27799. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  27800. EC_builtin_curve* curves = NULL;
  27801. size_t crv_len = 0;
  27802. size_t i = 0;
  27803. printf(testingFmt, "wolfSSL_EC_get_builtin_curves");
  27804. AssertIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  27805. AssertNotNull(curves = (EC_builtin_curve*)
  27806. XMALLOC(sizeof(EC_builtin_curve)*crv_len, NULL,
  27807. DYNAMIC_TYPE_TMP_BUFFER));
  27808. AssertIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  27809. for (i = 0; i < crv_len; i++)
  27810. {
  27811. if (curves[i].comment != NULL)
  27812. AssertStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  27813. }
  27814. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27815. printf(resultFmt, passed);
  27816. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  27817. #endif /* defined(HAVE_ECC) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  27818. }
  27819. static void test_no_op_functions(void)
  27820. {
  27821. #if defined(OPENSSL_EXTRA)
  27822. printf(testingFmt, "no_op_functions()");
  27823. /* this makes sure wolfSSL can compile and run these no-op functions */
  27824. SSL_load_error_strings();
  27825. ENGINE_load_builtin_engines();
  27826. OpenSSL_add_all_ciphers();
  27827. AssertIntEQ(CRYPTO_malloc_init(), 0);
  27828. printf(resultFmt, passed);
  27829. #endif
  27830. }
  27831. static void test_wolfSSL_CRYPTO_memcmp(void)
  27832. {
  27833. #ifdef OPENSSL_EXTRA
  27834. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  27835. "implementation of TLS/SSL for embedded devices to the cloud.";
  27836. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  27837. "implementation of TLS/SSL for embedded devices to the cloud.";
  27838. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  27839. "implementation of TLS/SSL for embedded devices to the cloud!";
  27840. AssertIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  27841. AssertIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  27842. #endif
  27843. }
  27844. /*----------------------------------------------------------------------------*
  27845. | wolfCrypt ASN
  27846. *----------------------------------------------------------------------------*/
  27847. static void test_wc_GetPkcs8TraditionalOffset(void)
  27848. {
  27849. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  27850. int length, derSz;
  27851. word32 inOutIdx;
  27852. const char* path = "./certs/server-keyPkcs8.der";
  27853. XFILE file;
  27854. byte der[2048];
  27855. printf(testingFmt, "wc_GetPkcs8TraditionalOffset");
  27856. file = XFOPEN(path, "rb");
  27857. AssertTrue(file != XBADFILE);
  27858. derSz = (int)XFREAD(der, 1, sizeof(der), file);
  27859. XFCLOSE(file);
  27860. /* valid case */
  27861. inOutIdx = 0;
  27862. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  27863. AssertIntGT(length, 0);
  27864. /* inOutIdx > sz */
  27865. inOutIdx = 4000;
  27866. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  27867. AssertIntEQ(length, BAD_FUNC_ARG);
  27868. /* null input */
  27869. inOutIdx = 0;
  27870. length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0);
  27871. AssertIntEQ(length, BAD_FUNC_ARG);
  27872. /* invalid input, fill buffer with 1's */
  27873. XMEMSET(der, 1, sizeof(der));
  27874. inOutIdx = 0;
  27875. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  27876. AssertIntEQ(length, ASN_PARSE_E);
  27877. printf(resultFmt, passed);
  27878. #endif /* NO_ASN */
  27879. }
  27880. static void test_wc_SetSubjectRaw(void)
  27881. {
  27882. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  27883. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  27884. const char* joiCertFile = "./certs/test/cert-ext-joi.pem";
  27885. WOLFSSL_X509* x509;
  27886. int peerCertSz;
  27887. const byte* peerCertBuf;
  27888. Cert forgedCert;
  27889. printf(testingFmt, "test_wc_SetSubjectRaw()");
  27890. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_PEM));
  27891. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  27892. AssertIntEQ(0, wc_InitCert(&forgedCert));
  27893. AssertIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  27894. wolfSSL_FreeX509(x509);
  27895. printf(resultFmt, passed);
  27896. #endif
  27897. }
  27898. static void test_wc_GetSubjectRaw(void)
  27899. {
  27900. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  27901. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  27902. Cert cert;
  27903. byte *subjectRaw;
  27904. printf(testingFmt, "test_wc_GetSubjectRaw()");
  27905. AssertIntEQ(0, wc_InitCert(&cert));
  27906. AssertIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  27907. printf(resultFmt, passed);
  27908. #endif
  27909. }
  27910. static void test_wc_SetIssuerRaw(void)
  27911. {
  27912. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  27913. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  27914. const char* joiCertFile = "./certs/test/cert-ext-joi.pem";
  27915. WOLFSSL_X509* x509;
  27916. int peerCertSz;
  27917. const byte* peerCertBuf;
  27918. Cert forgedCert;
  27919. printf(testingFmt, "test_wc_SetIssuerRaw()");
  27920. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_PEM));
  27921. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  27922. AssertIntEQ(0, wc_InitCert(&forgedCert));
  27923. AssertIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  27924. wolfSSL_FreeX509(x509);
  27925. printf(resultFmt, passed);
  27926. #endif
  27927. }
  27928. static void test_wc_SetIssueBuffer(void)
  27929. {
  27930. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  27931. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  27932. const char* joiCertFile = "./certs/test/cert-ext-joi.pem";
  27933. WOLFSSL_X509* x509;
  27934. int peerCertSz;
  27935. const byte* peerCertBuf;
  27936. Cert forgedCert;
  27937. printf(testingFmt, "test_wc_SetIssuerBuffer()");
  27938. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_PEM));
  27939. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  27940. AssertIntEQ(0, wc_InitCert(&forgedCert));
  27941. AssertIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  27942. wolfSSL_FreeX509(x509);
  27943. printf(resultFmt, passed);
  27944. #endif
  27945. }
  27946. /*
  27947. * Testing wc_SetSubjectKeyId
  27948. */
  27949. static void test_wc_SetSubjectKeyId(void)
  27950. {
  27951. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  27952. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  27953. Cert cert;
  27954. const char* file = "certs/ecc-client-keyPub.pem";
  27955. printf(testingFmt, "wc_SetSubjectKeyId()");
  27956. AssertIntEQ(0, wc_InitCert(&cert));
  27957. AssertIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  27958. AssertIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  27959. AssertIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  27960. printf(resultFmt, passed);
  27961. #endif
  27962. } /* END test_wc_SetSubjectKeyId */
  27963. /*
  27964. * Testing wc_SetSubject
  27965. */
  27966. static void test_wc_SetSubject(void)
  27967. {
  27968. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  27969. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  27970. Cert cert;
  27971. const char* file = "./certs/ca-ecc-cert.pem";
  27972. printf(testingFmt, "wc_SetSubject()");
  27973. AssertIntEQ(0, wc_InitCert(&cert));
  27974. AssertIntEQ(0, wc_SetSubject(&cert, file));
  27975. AssertIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  27976. AssertIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  27977. printf(resultFmt, passed);
  27978. #endif
  27979. } /* END test_wc_SetSubject */
  27980. static void test_CheckCertSignature(void)
  27981. {
  27982. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  27983. WOLFSSL_CERT_MANAGER* cm = NULL;
  27984. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  27985. FILE* fp;
  27986. byte cert[4096];
  27987. int certSz;
  27988. #endif
  27989. AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
  27990. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  27991. AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
  27992. #ifndef NO_RSA
  27993. #ifdef USE_CERT_BUFFERS_1024
  27994. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
  27995. sizeof_server_cert_der_1024, NULL, cm));
  27996. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  27997. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  27998. WOLFSSL_FILETYPE_ASN1));
  27999. AssertIntEQ(0, CheckCertSignature(server_cert_der_1024,
  28000. sizeof_server_cert_der_1024, NULL, cm));
  28001. #elif defined(USE_CERT_BUFFERS_2048)
  28002. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
  28003. sizeof_server_cert_der_2048, NULL, cm));
  28004. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  28005. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  28006. WOLFSSL_FILETYPE_ASN1));
  28007. AssertIntEQ(0, CheckCertSignature(server_cert_der_2048,
  28008. sizeof_server_cert_der_2048, NULL, cm));
  28009. #endif
  28010. #endif
  28011. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  28012. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
  28013. sizeof_serv_ecc_der_256, NULL, cm));
  28014. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  28015. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  28016. WOLFSSL_FILETYPE_ASN1));
  28017. AssertIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  28018. NULL, cm));
  28019. #endif
  28020. #if !defined(NO_FILESYSTEM)
  28021. wolfSSL_CertManagerFree(cm);
  28022. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  28023. #ifndef NO_RSA
  28024. AssertNotNull(fp = XFOPEN("./certs/server-cert.der", "rb"));
  28025. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  28026. XFCLOSE(fp);
  28027. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  28028. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  28029. "./certs/ca-cert.pem", NULL));
  28030. AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  28031. #endif
  28032. #ifdef HAVE_ECC
  28033. AssertNotNull(fp = XFOPEN("./certs/server-ecc.der", "rb"));
  28034. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  28035. XFCLOSE(fp);
  28036. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  28037. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  28038. "./certs/ca-ecc-cert.pem", NULL));
  28039. AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  28040. #endif
  28041. #endif
  28042. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  28043. (void)fp;
  28044. (void)cert;
  28045. (void)certSz;
  28046. #endif
  28047. wolfSSL_CertManagerFree(cm);
  28048. #endif
  28049. }
  28050. /*----------------------------------------------------------------------------*
  28051. | wolfCrypt ECC
  28052. *----------------------------------------------------------------------------*/
  28053. static void test_wc_ecc_get_curve_size_from_name(void)
  28054. {
  28055. #ifdef HAVE_ECC
  28056. int ret;
  28057. printf(testingFmt, "wc_ecc_get_curve_size_from_name");
  28058. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  28059. ret = wc_ecc_get_curve_size_from_name("SECP256R1");
  28060. AssertIntEQ(ret, 32);
  28061. #endif
  28062. /* invalid case */
  28063. ret = wc_ecc_get_curve_size_from_name("BADCURVE");
  28064. AssertIntEQ(ret, -1);
  28065. /* NULL input */
  28066. ret = wc_ecc_get_curve_size_from_name(NULL);
  28067. AssertIntEQ(ret, BAD_FUNC_ARG);
  28068. printf(resultFmt, passed);
  28069. #endif /* HAVE_ECC */
  28070. }
  28071. static void test_wc_ecc_get_curve_id_from_name(void)
  28072. {
  28073. #ifdef HAVE_ECC
  28074. int id;
  28075. printf(testingFmt, "wc_ecc_get_curve_id_from_name");
  28076. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  28077. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  28078. AssertIntEQ(id, ECC_SECP256R1);
  28079. #endif
  28080. /* invalid case */
  28081. id = wc_ecc_get_curve_id_from_name("BADCURVE");
  28082. AssertIntEQ(id, -1);
  28083. /* NULL input */
  28084. id = wc_ecc_get_curve_id_from_name(NULL);
  28085. AssertIntEQ(id, BAD_FUNC_ARG);
  28086. printf(resultFmt, passed);
  28087. #endif /* HAVE_ECC */
  28088. }
  28089. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  28090. !defined(HAVE_SELFTEST) && \
  28091. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  28092. static void test_wc_ecc_get_curve_id_from_dp_params(void)
  28093. {
  28094. int id;
  28095. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  28096. int curve_id;
  28097. ecc_key* key;
  28098. const ecc_set_type* params;
  28099. int ret;
  28100. #endif
  28101. WOLFSSL_EC_KEY *ecKey = NULL;
  28102. printf(testingFmt, "wc_ecc_get_curve_id_from_dp_params");
  28103. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  28104. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  28105. AssertIntEQ(id, ECC_SECP256R1);
  28106. ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  28107. AssertNotNull(ecKey);
  28108. ret = EC_KEY_generate_key(ecKey);
  28109. if (ret == 0) {
  28110. /* normal test */
  28111. key = (ecc_key*)ecKey->internal;
  28112. params = key->dp;
  28113. curve_id = wc_ecc_get_curve_id_from_dp_params(params);
  28114. AssertIntEQ(curve_id, id);
  28115. }
  28116. #endif
  28117. /* invalid case, NULL input*/
  28118. id = wc_ecc_get_curve_id_from_dp_params(NULL);
  28119. AssertIntEQ(id, BAD_FUNC_ARG);
  28120. wolfSSL_EC_KEY_free(ecKey);
  28121. printf(resultFmt, passed);
  28122. }
  28123. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  28124. static void test_wc_ecc_get_curve_id_from_params(void)
  28125. {
  28126. #ifdef HAVE_ECC
  28127. int id;
  28128. const byte prime[] =
  28129. {
  28130. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  28131. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28132. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  28133. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  28134. };
  28135. const byte primeInvalid[] =
  28136. {
  28137. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  28138. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28139. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  28140. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  28141. };
  28142. const byte Af[] =
  28143. {
  28144. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  28145. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28146. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  28147. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  28148. };
  28149. const byte Bf[] =
  28150. {
  28151. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  28152. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  28153. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  28154. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  28155. };
  28156. const byte order[] =
  28157. {
  28158. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  28159. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  28160. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  28161. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  28162. };
  28163. const byte Gx[] =
  28164. {
  28165. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  28166. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  28167. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  28168. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  28169. };
  28170. const byte Gy[] =
  28171. {
  28172. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  28173. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  28174. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  28175. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  28176. };
  28177. int cofactor = 1;
  28178. int fieldSize = 256;
  28179. printf(testingFmt, "wc_ecc_get_curve_id_from_params");
  28180. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  28181. id = wc_ecc_get_curve_id_from_params(fieldSize, prime, sizeof(prime),
  28182. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  28183. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  28184. AssertIntEQ(id, ECC_SECP256R1);
  28185. #endif
  28186. /* invalid case, fieldSize = 0 */
  28187. id = wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  28188. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  28189. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  28190. AssertIntEQ(id, ECC_CURVE_INVALID);
  28191. /* invalid case, NULL prime */
  28192. id = wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  28193. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  28194. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  28195. AssertIntEQ(id, BAD_FUNC_ARG);
  28196. /* invalid case, invalid prime */
  28197. id = wc_ecc_get_curve_id_from_params(fieldSize,
  28198. primeInvalid, sizeof(primeInvalid),
  28199. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  28200. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  28201. AssertIntEQ(id, ECC_CURVE_INVALID);
  28202. printf(resultFmt, passed);
  28203. #endif
  28204. }
  28205. static void test_wolfSSL_EVP_PKEY_encrypt(void)
  28206. {
  28207. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  28208. !defined(HAVE_FAST_RSA)
  28209. WOLFSSL_RSA* rsa = NULL;
  28210. WOLFSSL_EVP_PKEY* pkey = NULL;
  28211. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  28212. const char* in = "What is easy to do is easy not to do.";
  28213. size_t inlen = XSTRLEN(in);
  28214. size_t outEncLen = 0;
  28215. byte* outEnc = NULL;
  28216. byte* outDec = NULL;
  28217. size_t outDecLen = 0;
  28218. size_t rsaKeySz = 2048/8; /* Bytes */
  28219. #ifdef WC_RSA_NO_PADDING
  28220. byte* inTmp = NULL;
  28221. byte* outEncTmp = NULL;
  28222. byte* outDecTmp = NULL;
  28223. #endif
  28224. printf(testingFmt, "wolfSSL_EVP_PKEY_encrypt()");
  28225. AssertNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  28226. XMEMSET(outEnc, 0, rsaKeySz);
  28227. AssertNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  28228. XMEMSET(outDec, 0, rsaKeySz);
  28229. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  28230. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  28231. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  28232. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  28233. AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  28234. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  28235. WOLFSSL_SUCCESS);
  28236. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  28237. since ctx uses it.*/
  28238. AssertIntEQ(pkey->references, 2);
  28239. EVP_PKEY_free(pkey);
  28240. AssertIntEQ(pkey->references, 1);
  28241. /* Encrypt data */
  28242. AssertIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  28243. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  28244. /* Decrypt data */
  28245. AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  28246. AssertIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  28247. WOLFSSL_SUCCESS);
  28248. AssertIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  28249. #ifdef WC_RSA_NO_PADDING
  28250. /* The input length must be the same size as the RSA key.*/
  28251. AssertNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  28252. XMEMSET(inTmp, 9, rsaKeySz);
  28253. AssertNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  28254. XMEMSET(outEncTmp, 0, rsaKeySz);
  28255. AssertNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  28256. XMEMSET(outDecTmp, 0, rsaKeySz);
  28257. AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  28258. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  28259. WOLFSSL_SUCCESS);
  28260. AssertIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  28261. WOLFSSL_SUCCESS);
  28262. AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  28263. AssertIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp, outEncLen),
  28264. WOLFSSL_SUCCESS);
  28265. AssertIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  28266. #endif
  28267. EVP_PKEY_CTX_free(ctx);
  28268. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28269. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28270. #ifdef WC_RSA_NO_PADDING
  28271. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28272. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28273. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28274. #endif
  28275. printf(resultFmt, passed);
  28276. #endif
  28277. }
  28278. static void test_wolfSSL_EVP_PKEY_sign(void)
  28279. {
  28280. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  28281. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  28282. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  28283. WOLFSSL_RSA* rsa = NULL;
  28284. WOLFSSL_EVP_PKEY* pkey = NULL;
  28285. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  28286. const char* in = "What is easy to do is easy not to do.";
  28287. size_t inlen = XSTRLEN(in);
  28288. byte hash[SHA256_DIGEST_LENGTH] = {0};
  28289. SHA256_CTX c;
  28290. byte* sig = NULL;
  28291. byte* sigVerify = NULL;
  28292. size_t siglen = 0;
  28293. size_t rsaKeySz = 2048/8; /* Bytes */
  28294. printf(testingFmt, "wolfSSL_EVP_PKEY_sign()");
  28295. sig = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28296. AssertNotNull(sig);
  28297. XMEMSET(sig, 0, rsaKeySz);
  28298. AssertNotNull(sigVerify = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  28299. XMEMSET(sigVerify, 0, rsaKeySz);
  28300. /* Generate hash */
  28301. SHA256_Init(&c);
  28302. SHA256_Update(&c, in, inlen);
  28303. SHA256_Final(hash, &c);
  28304. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  28305. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  28306. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  28307. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  28308. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  28309. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  28310. WOLFSSL_SUCCESS);
  28311. /* Sign data */
  28312. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash, SHA256_DIGEST_LENGTH),
  28313. WOLFSSL_SUCCESS);
  28314. /* Verify signature.
  28315. EVP_PKEY_verify() doesn't exist yet, so use RSA_public_decrypt(). */
  28316. AssertIntEQ(RSA_public_decrypt((int)siglen, sig, sigVerify,
  28317. rsa, RSA_PKCS1_PADDING), SHA256_DIGEST_LENGTH);
  28318. AssertIntEQ(XMEMCMP(hash, sigVerify, SHA256_DIGEST_LENGTH), 0);
  28319. /* error cases */
  28320. AssertIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  28321. ctx->pkey->type = EVP_PKEY_RSA2;
  28322. AssertIntNE(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  28323. AssertIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  28324. WOLFSSL_SUCCESS);
  28325. AssertIntNE(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  28326. WOLFSSL_SUCCESS);
  28327. EVP_PKEY_free(pkey);
  28328. EVP_PKEY_CTX_free(ctx);
  28329. wolfSSL_RSA_free(rsa);
  28330. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28331. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28332. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  28333. printf(resultFmt, passed);
  28334. #endif
  28335. }
  28336. static void test_EVP_PKEY_rsa(void)
  28337. {
  28338. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  28339. WOLFSSL_RSA* rsa;
  28340. WOLFSSL_EVP_PKEY* pkey;
  28341. AssertNotNull(rsa = wolfSSL_RSA_new());
  28342. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  28343. AssertIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  28344. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  28345. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  28346. AssertPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  28347. wolfSSL_EVP_PKEY_free(pkey);
  28348. printf(resultFmt, passed);
  28349. #endif
  28350. }
  28351. static void test_EVP_PKEY_ec(void)
  28352. {
  28353. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  28354. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  28355. WOLFSSL_EC_KEY* ecKey;
  28356. WOLFSSL_EVP_PKEY* pkey;
  28357. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  28358. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  28359. AssertIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  28360. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  28361. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  28362. wolfSSL_EVP_PKEY_free(pkey);
  28363. printf(resultFmt, passed);
  28364. #endif
  28365. #endif
  28366. }
  28367. static void test_EVP_PKEY_cmp(void)
  28368. {
  28369. #if defined(OPENSSL_EXTRA)
  28370. EVP_PKEY *a, *b;
  28371. const unsigned char *in;
  28372. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  28373. in = client_key_der_2048;
  28374. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  28375. &in, (long)sizeof_client_key_der_2048));
  28376. in = client_key_der_2048;
  28377. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  28378. &in, (long)sizeof_client_key_der_2048));
  28379. /* Test success case RSA */
  28380. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  28381. EVP_PKEY_free(b);
  28382. EVP_PKEY_free(a);
  28383. #endif
  28384. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  28385. in = ecc_clikey_der_256;
  28386. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  28387. &in, (long)sizeof_ecc_clikey_der_256));
  28388. in = ecc_clikey_der_256;
  28389. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  28390. &in, (long)sizeof_ecc_clikey_der_256));
  28391. /* Test success case ECC */
  28392. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  28393. EVP_PKEY_free(b);
  28394. EVP_PKEY_free(a);
  28395. #endif
  28396. /* Test failure cases */
  28397. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  28398. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  28399. in = client_key_der_2048;
  28400. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  28401. &in, (long)sizeof_client_key_der_2048));
  28402. in = ecc_clikey_der_256;
  28403. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  28404. &in, (long)sizeof_ecc_clikey_der_256));
  28405. AssertIntNE(EVP_PKEY_cmp(a, b), 0);
  28406. EVP_PKEY_free(b);
  28407. EVP_PKEY_free(a);
  28408. #endif
  28409. /* invalid or empty failure cases */
  28410. a = EVP_PKEY_new();
  28411. b = EVP_PKEY_new();
  28412. AssertIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  28413. AssertIntNE(EVP_PKEY_cmp(a, NULL), 0);
  28414. AssertIntNE(EVP_PKEY_cmp(NULL, b), 0);
  28415. AssertIntNE(EVP_PKEY_cmp(a, b), 0);
  28416. EVP_PKEY_free(b);
  28417. EVP_PKEY_free(a);
  28418. (void)in;
  28419. printf(resultFmt, passed);
  28420. #endif
  28421. }
  28422. static void test_ERR_load_crypto_strings(void)
  28423. {
  28424. #if defined(OPENSSL_ALL)
  28425. ERR_load_crypto_strings();
  28426. printf(resultFmt, passed);
  28427. #endif
  28428. }
  28429. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  28430. static void free_x509(X509* x)
  28431. {
  28432. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  28433. }
  28434. #endif
  28435. static void test_sk_X509(void)
  28436. {
  28437. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  28438. STACK_OF(X509)* s;
  28439. AssertNotNull(s = sk_X509_new());
  28440. AssertIntEQ(sk_X509_num(s), 0);
  28441. sk_X509_free(s);
  28442. AssertNotNull(s = sk_X509_new_null());
  28443. AssertIntEQ(sk_X509_num(s), 0);
  28444. sk_X509_free(s);
  28445. AssertNotNull(s = sk_X509_new());
  28446. sk_X509_push(s, (X509*)1);
  28447. AssertIntEQ(sk_X509_num(s), 1);
  28448. AssertIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  28449. sk_X509_push(s, (X509*)2);
  28450. AssertIntEQ(sk_X509_num(s), 2);
  28451. AssertIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  28452. AssertIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  28453. sk_X509_push(s, (X509*)2);
  28454. sk_X509_pop_free(s, free_x509);
  28455. printf(resultFmt, passed);
  28456. #endif
  28457. }
  28458. static void test_X509_get_signature_nid(void)
  28459. {
  28460. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  28461. X509* x509;
  28462. AssertIntEQ(X509_get_signature_nid(NULL), 0);
  28463. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  28464. SSL_FILETYPE_PEM));
  28465. AssertIntEQ(X509_get_signature_nid(x509), CTC_SHA256wRSA);
  28466. X509_free(x509);
  28467. printf(resultFmt, passed);
  28468. #endif
  28469. }
  28470. static void test_X509_REQ(void)
  28471. {
  28472. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  28473. defined(WOLFSSL_CERT_REQ)
  28474. X509_NAME* name;
  28475. #if !defined(NO_RSA) || defined(HAVE_ECC)
  28476. X509_REQ* req;
  28477. EVP_PKEY* priv;
  28478. EVP_PKEY* pub;
  28479. unsigned char* der = NULL;
  28480. #endif
  28481. #ifndef NO_RSA
  28482. #ifdef USE_CERT_BUFFERS_1024
  28483. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  28484. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  28485. #elif defined(USE_CERT_BUFFERS_2048)
  28486. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  28487. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  28488. #endif
  28489. #endif
  28490. #ifdef HAVE_ECC
  28491. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  28492. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  28493. int len;
  28494. #endif
  28495. AssertNotNull(name = X509_NAME_new());
  28496. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  28497. (byte*)"wolfssl.com", 11, 0, 1),
  28498. WOLFSSL_SUCCESS);
  28499. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  28500. (byte*)"support@wolfssl.com", 19, -1,
  28501. 1), WOLFSSL_SUCCESS);
  28502. #ifndef NO_RSA
  28503. AssertNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  28504. (long)sizeof_client_key_der_2048));
  28505. AssertNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  28506. (long)sizeof_client_keypub_der_2048));
  28507. AssertNotNull(req = X509_REQ_new());
  28508. AssertIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  28509. AssertIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  28510. AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  28511. AssertIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  28512. AssertIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  28513. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  28514. AssertIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  28515. AssertIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  28516. AssertIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  28517. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  28518. AssertIntEQ(i2d_X509_REQ(req, &der), 643);
  28519. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  28520. der = NULL;
  28521. X509_REQ_free(NULL);
  28522. X509_REQ_free(req);
  28523. EVP_PKEY_free(pub);
  28524. EVP_PKEY_free(priv);
  28525. #endif
  28526. #ifdef HAVE_ECC
  28527. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  28528. sizeof_ecc_clikey_der_256));
  28529. AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  28530. sizeof_ecc_clikeypub_der_256));
  28531. AssertNotNull(req = X509_REQ_new());
  28532. AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  28533. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  28534. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  28535. /* Signature is random and may be shorter or longer. */
  28536. AssertIntGE((len = i2d_X509_REQ(req, &der)), 245);
  28537. AssertIntLE(len, 253);
  28538. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  28539. X509_REQ_free(req);
  28540. EVP_PKEY_free(pub);
  28541. EVP_PKEY_free(priv);
  28542. #ifdef FP_ECC
  28543. wc_ecc_fp_free();
  28544. #endif
  28545. #endif /* HAVE_ECC */
  28546. X509_NAME_free(name);
  28547. printf(resultFmt, passed);
  28548. #endif
  28549. }
  28550. static void test_wolfssl_PKCS7(void)
  28551. {
  28552. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  28553. PKCS7* pkcs7;
  28554. byte data[FOURK_BUF];
  28555. word32 len = sizeof(data);
  28556. const byte* p = data;
  28557. byte content[] = "Test data to encode.";
  28558. AssertIntGT((len = CreatePKCS7SignedData(data, len, content,
  28559. (word32)sizeof(content),
  28560. 0, 0)), 0);
  28561. AssertNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
  28562. AssertNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  28563. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  28564. AssertIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  28565. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  28566. PKCS7_free(pkcs7);
  28567. /* fail case, without PKCS7_NOVERIFY */
  28568. p = data;
  28569. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  28570. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  28571. 0), WOLFSSL_FAILURE);
  28572. PKCS7_free(pkcs7);
  28573. /* success case, with PKCS7_NOVERIFY */
  28574. p = data;
  28575. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  28576. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  28577. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  28578. PKCS7_free(NULL);
  28579. PKCS7_free(pkcs7);
  28580. printf(resultFmt, passed);
  28581. #endif
  28582. }
  28583. static void test_wolfSSL_PKCS7_SIGNED_new(void)
  28584. {
  28585. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  28586. PKCS7_SIGNED* pkcs7;
  28587. printf(testingFmt, "wolfSSL_PKCS7_SIGNED_new()");
  28588. pkcs7 = PKCS7_SIGNED_new();
  28589. AssertNotNull(pkcs7);
  28590. AssertIntEQ(pkcs7->contentOID, SIGNED_DATA);
  28591. PKCS7_SIGNED_free(pkcs7);
  28592. printf(resultFmt, passed);
  28593. #endif
  28594. }
  28595. static void test_wolfSSL_PEM_write_bio_PKCS7(void)
  28596. {
  28597. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  28598. PKCS7* pkcs7 = NULL;
  28599. BIO* bio = NULL;
  28600. const byte* cert_buf = NULL;
  28601. int ret = 0;
  28602. WC_RNG rng;
  28603. const byte data[] = { /* Hello World */
  28604. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  28605. 0x72,0x6c,0x64
  28606. };
  28607. #ifndef NO_RSA
  28608. #if defined(USE_CERT_BUFFERS_2048)
  28609. byte key[sizeof_client_key_der_2048];
  28610. byte cert[sizeof_client_cert_der_2048];
  28611. word32 keySz = (word32)sizeof(key);
  28612. word32 certSz = (word32)sizeof(cert);
  28613. XMEMSET(key, 0, keySz);
  28614. XMEMSET(cert, 0, certSz);
  28615. XMEMCPY(key, client_key_der_2048, keySz);
  28616. XMEMCPY(cert, client_cert_der_2048, certSz);
  28617. #elif defined(USE_CERT_BUFFERS_1024)
  28618. byte key[sizeof_client_key_der_1024];
  28619. byte cert[sizeof_client_cert_der_1024];
  28620. word32 keySz = (word32)sizeof(key);
  28621. word32 certSz = (word32)sizeof(cert);
  28622. XMEMSET(key, 0, keySz);
  28623. XMEMSET(cert, 0, certSz);
  28624. XMEMCPY(key, client_key_der_1024, keySz);
  28625. XMEMCPY(cert, client_cert_der_1024, certSz);
  28626. #else
  28627. unsigned char cert[ONEK_BUF];
  28628. unsigned char key[ONEK_BUF];
  28629. XFILE fp;
  28630. int certSz;
  28631. int keySz;
  28632. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  28633. AssertTrue((fp != XBADFILE));
  28634. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  28635. XFCLOSE(fp);
  28636. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  28637. AssertTrue(fp != XBADFILE);
  28638. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  28639. XFCLOSE(fp);
  28640. #endif
  28641. #elif defined(HAVE_ECC)
  28642. #if defined(USE_CERT_BUFFERS_256)
  28643. unsigned char cert[sizeof_cliecc_cert_der_256];
  28644. unsigned char key[sizeof_ecc_clikey_der_256];
  28645. int certSz = (int)sizeof(cert);
  28646. int keySz = (int)sizeof(key);
  28647. XMEMSET(cert, 0, certSz);
  28648. XMEMSET(key, 0, keySz);
  28649. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  28650. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  28651. #else
  28652. unsigned char cert[ONEK_BUF];
  28653. unsigned char key[ONEK_BUF];
  28654. XFILE fp;
  28655. int certSz, keySz;
  28656. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  28657. AssertTrue(fp != XBADFILE);
  28658. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  28659. XFCLOSE(fp);
  28660. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  28661. AssertTrue(fp != XBADFILE);
  28662. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  28663. XFCLOSE(fp);
  28664. #endif
  28665. #else
  28666. #error PKCS7 requires ECC or RSA
  28667. #endif
  28668. printf(testingFmt, "wolfSSL_PEM_write_bio_PKCS7()");
  28669. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  28670. /* initialize with DER encoded cert */
  28671. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  28672. /* init rng */
  28673. AssertIntEQ(wc_InitRng(&rng), 0);
  28674. pkcs7->rng = &rng;
  28675. pkcs7->content = (byte*)data; /* not used for ex */
  28676. pkcs7->contentSz = (word32)sizeof(data);
  28677. pkcs7->contentOID = SIGNED_DATA;
  28678. pkcs7->privateKey = key;
  28679. pkcs7->privateKeySz = (word32)sizeof(key);
  28680. pkcs7->encryptOID = RSAk;
  28681. pkcs7->hashOID = SHAh;
  28682. pkcs7->signedAttribs = NULL;
  28683. pkcs7->signedAttribsSz = 0;
  28684. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  28685. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  28686. AssertIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  28687. /* Read PKCS#7 PEM from BIO */
  28688. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  28689. AssertIntGE(ret, 0);
  28690. BIO_free(bio);
  28691. wc_PKCS7_Free(pkcs7);
  28692. wc_FreeRng(&rng);
  28693. printf(resultFmt, passed);
  28694. #endif
  28695. }
  28696. /*----------------------------------------------------------------------------*
  28697. | Certificate Failure Checks
  28698. *----------------------------------------------------------------------------*/
  28699. #ifndef NO_CERTS
  28700. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  28701. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  28702. int type)
  28703. {
  28704. int ret;
  28705. WOLFSSL_CERT_MANAGER* cm = NULL;
  28706. switch (type) {
  28707. case TESTING_RSA:
  28708. #ifdef NO_RSA
  28709. printf("RSA disabled, skipping test\n");
  28710. return ASN_SIG_CONFIRM_E;
  28711. #else
  28712. break;
  28713. #endif
  28714. case TESTING_ECC:
  28715. #ifndef HAVE_ECC
  28716. printf("ECC disabled, skipping test\n");
  28717. return ASN_SIG_CONFIRM_E;
  28718. #else
  28719. break;
  28720. #endif
  28721. default:
  28722. printf("Bad function argument\n");
  28723. return BAD_FUNC_ARG;
  28724. }
  28725. cm = wolfSSL_CertManagerNew();
  28726. if (cm == NULL) {
  28727. printf("wolfSSL_CertManagerNew failed\n");
  28728. return -1;
  28729. }
  28730. #ifndef NO_FILESYSTEM
  28731. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  28732. if (ret != WOLFSSL_SUCCESS) {
  28733. printf("wolfSSL_CertManagerLoadCA failed\n");
  28734. wolfSSL_CertManagerFree(cm);
  28735. return ret;
  28736. }
  28737. #else
  28738. (void)ca;
  28739. #endif
  28740. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz, WOLFSSL_FILETYPE_ASN1);
  28741. /* Let AssertIntEQ handle return code */
  28742. wolfSSL_CertManagerFree(cm);
  28743. return ret;
  28744. }
  28745. static int test_RsaSigFailure_cm(void)
  28746. {
  28747. int ret = 0;
  28748. const char* ca_cert = "./certs/ca-cert.pem";
  28749. const char* server_cert = "./certs/server-cert.der";
  28750. byte* cert_buf = NULL;
  28751. size_t cert_sz = 0;
  28752. ret = load_file(server_cert, &cert_buf, &cert_sz);
  28753. if (ret == 0) {
  28754. /* corrupt DER - invert last byte, which is signature */
  28755. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  28756. /* test bad cert */
  28757. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA);
  28758. }
  28759. printf("Signature failure test: RSA: Ret %d\n", ret);
  28760. if (cert_buf)
  28761. free(cert_buf);
  28762. return ret;
  28763. }
  28764. static int test_EccSigFailure_cm(void)
  28765. {
  28766. int ret = 0;
  28767. /* self-signed ECC cert, so use server cert as CA */
  28768. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  28769. const char* server_cert = "./certs/server-ecc.der";
  28770. byte* cert_buf = NULL;
  28771. size_t cert_sz = 0;
  28772. ret = load_file(server_cert, &cert_buf, &cert_sz);
  28773. if (ret == 0) {
  28774. /* corrupt DER - invert last byte, which is signature */
  28775. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  28776. /* test bad cert */
  28777. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC);
  28778. }
  28779. printf("Signature failure test: ECC: Ret %d\n", ret);
  28780. if (cert_buf)
  28781. free(cert_buf);
  28782. #ifdef FP_ECC
  28783. wc_ecc_fp_free();
  28784. #endif
  28785. return ret;
  28786. }
  28787. #endif /* NO_CERTS */
  28788. #ifdef WOLFSSL_TLS13
  28789. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  28790. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  28791. #endif
  28792. #ifdef WOLFSSL_EARLY_DATA
  28793. static const char earlyData[] = "Early Data";
  28794. static char earlyDataBuffer[1];
  28795. #endif
  28796. static int test_tls13_apis(void)
  28797. {
  28798. int ret = 0;
  28799. #ifndef WOLFSSL_NO_TLS12
  28800. #ifndef NO_WOLFSSL_CLIENT
  28801. WOLFSSL_CTX* clientTls12Ctx;
  28802. WOLFSSL* clientTls12Ssl;
  28803. #endif
  28804. #ifndef NO_WOLFSSL_SERVER
  28805. WOLFSSL_CTX* serverTls12Ctx;
  28806. WOLFSSL* serverTls12Ssl;
  28807. #endif
  28808. #endif
  28809. #ifndef NO_WOLFSSL_CLIENT
  28810. WOLFSSL_CTX* clientCtx;
  28811. WOLFSSL* clientSsl;
  28812. #endif
  28813. #ifndef NO_WOLFSSL_SERVER
  28814. WOLFSSL_CTX* serverCtx;
  28815. WOLFSSL* serverSsl;
  28816. #ifndef NO_CERTS
  28817. const char* ourCert = svrCertFile;
  28818. const char* ourKey = svrKeyFile;
  28819. #endif
  28820. #endif
  28821. #ifdef WOLFSSL_EARLY_DATA
  28822. int outSz;
  28823. #endif
  28824. int groups[2] = { WOLFSSL_ECC_X25519, WOLFSSL_ECC_X448 };
  28825. int numGroups = 2;
  28826. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  28827. char groupList[] = "P-521:P-384:P-256";
  28828. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  28829. #ifndef WOLFSSL_NO_TLS12
  28830. #ifndef NO_WOLFSSL_CLIENT
  28831. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  28832. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  28833. #endif
  28834. #ifndef NO_WOLFSSL_SERVER
  28835. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  28836. #ifndef NO_CERTS
  28837. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  28838. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey, WOLFSSL_FILETYPE_PEM);
  28839. #endif
  28840. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  28841. #endif
  28842. #endif
  28843. #ifndef NO_WOLFSSL_CLIENT
  28844. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  28845. clientSsl = wolfSSL_new(clientCtx);
  28846. #endif
  28847. #ifndef NO_WOLFSSL_SERVER
  28848. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  28849. #ifndef NO_CERTS
  28850. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  28851. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  28852. #endif
  28853. serverSsl = wolfSSL_new(serverCtx);
  28854. #endif
  28855. #ifdef WOLFSSL_SEND_HRR_COOKIE
  28856. AssertIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  28857. #ifndef NO_WOLFSSL_CLIENT
  28858. AssertIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  28859. #endif
  28860. #ifndef NO_WOLFSSL_SERVER
  28861. #ifndef WOLFSSL_NO_TLS12
  28862. AssertIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0), BAD_FUNC_ARG);
  28863. #endif
  28864. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  28865. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  28866. WOLFSSL_SUCCESS);
  28867. #endif
  28868. #endif
  28869. #ifdef HAVE_ECC
  28870. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  28871. #ifndef NO_WOLFSSL_SERVER
  28872. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1),
  28873. WOLFSSL_SUCCESS);
  28874. #endif
  28875. #ifndef NO_WOLFSSL_CLIENT
  28876. #ifndef WOLFSSL_NO_TLS12
  28877. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  28878. WOLFSSL_SUCCESS);
  28879. #endif
  28880. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  28881. WOLFSSL_SUCCESS);
  28882. #endif
  28883. #elif defined(HAVE_CURVE25519)
  28884. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  28885. #ifndef NO_WOLFSSL_SERVER
  28886. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  28887. WOLFSSL_SUCCESS);
  28888. #endif
  28889. #ifndef NO_WOLFSSL_CLIENT
  28890. #ifndef WOLFSSL_NO_TLS12
  28891. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  28892. WOLFSSL_SUCCESS);
  28893. #endif
  28894. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  28895. WOLFSSL_SUCCESS);
  28896. #endif
  28897. #elif defined(HAVE_CURVE448)
  28898. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  28899. #ifndef NO_WOLFSSL_SERVER
  28900. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  28901. WOLFSSL_SUCCESS);
  28902. #endif
  28903. #ifndef NO_WOLFSSL_CLIENT
  28904. #ifndef WOLFSSL_NO_TLS12
  28905. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  28906. WOLFSSL_SUCCESS);
  28907. #endif
  28908. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  28909. WOLFSSL_SUCCESS);
  28910. #endif
  28911. #else
  28912. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  28913. #ifndef NO_WOLFSSL_CLIENT
  28914. #ifndef WOLFSSL_NO_TLS12
  28915. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  28916. NOT_COMPILED_IN);
  28917. #endif
  28918. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  28919. NOT_COMPILED_IN);
  28920. #endif
  28921. #endif
  28922. AssertIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  28923. #ifndef NO_WOLFSSL_SERVER
  28924. AssertIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  28925. #endif
  28926. #ifndef NO_WOLFSSL_CLIENT
  28927. #ifndef WOLFSSL_NO_TLS12
  28928. AssertIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  28929. #endif
  28930. AssertIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  28931. #endif
  28932. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  28933. #ifndef NO_WOLFSSL_CLIENT
  28934. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  28935. #endif
  28936. #ifndef NO_WOLFSSL_SERVER
  28937. #ifndef WOLFSSL_NO_TLS12
  28938. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  28939. #endif
  28940. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  28941. #endif
  28942. AssertIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  28943. #ifndef NO_WOLFSSL_CLIENT
  28944. AssertIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  28945. #endif
  28946. #ifndef NO_WOLFSSL_SERVER
  28947. #ifndef WOLFSSL_NO_TLS12
  28948. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  28949. #endif
  28950. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  28951. #endif
  28952. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  28953. #ifndef NO_WOLFSSL_CLIENT
  28954. #ifndef WOLFSSL_NO_TLS12
  28955. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  28956. #endif
  28957. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  28958. #endif
  28959. #ifndef NO_WOLFSSL_SERVER
  28960. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  28961. #endif
  28962. AssertIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  28963. #ifndef NO_WOLFSSL_CLIENT
  28964. #ifndef WOLFSSL_NO_TLS12
  28965. AssertIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  28966. #endif
  28967. AssertIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  28968. #endif
  28969. #ifndef NO_WOLFSSL_SERVER
  28970. AssertIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  28971. #endif
  28972. AssertIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  28973. #ifndef NO_WOLFSSL_CLIENT
  28974. #ifndef WOLFSSL_NO_TLS12
  28975. AssertIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  28976. #endif
  28977. AssertIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  28978. #endif
  28979. #ifndef NO_WOLFSSL_SERVER
  28980. AssertIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  28981. #endif
  28982. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  28983. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  28984. #ifndef NO_WOLFSSL_SERVER
  28985. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  28986. #endif
  28987. #ifndef NO_WOLFSSL_CLIENT
  28988. #ifndef WOLFSSL_NO_TLS12
  28989. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  28990. BAD_FUNC_ARG);
  28991. #endif
  28992. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  28993. #endif
  28994. AssertIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  28995. #ifndef NO_WOLFSSL_SERVER
  28996. AssertIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  28997. #endif
  28998. #ifndef NO_WOLFSSL_CLIENT
  28999. #ifndef WOLFSSL_NO_TLS12
  29000. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  29001. BAD_FUNC_ARG);
  29002. #endif
  29003. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  29004. #endif
  29005. AssertIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  29006. #ifndef NO_WOLFSSL_CLIENT
  29007. AssertIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  29008. #endif
  29009. #ifndef NO_WOLFSSL_SERVER
  29010. #ifndef WOLFSSL_NO_TLS12
  29011. AssertIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  29012. BAD_FUNC_ARG);
  29013. #endif
  29014. AssertIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  29015. #endif
  29016. #endif
  29017. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  29018. AssertIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  29019. #ifndef NO_WOLFSSL_SERVER
  29020. AssertIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  29021. #endif
  29022. #ifndef NO_WOLFSSL_CLIENT
  29023. #ifndef WOLFSSL_NO_TLS12
  29024. AssertIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  29025. #endif
  29026. AssertIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  29027. #endif
  29028. #endif
  29029. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  29030. #ifndef NO_WOLFSSL_CLIENT
  29031. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  29032. #endif
  29033. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  29034. #ifndef NO_WOLFSSL_CLIENT
  29035. #ifndef WOLFSSL_NO_TLS12
  29036. AssertIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  29037. BAD_FUNC_ARG);
  29038. #endif
  29039. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  29040. WOLFSSL_MAX_GROUP_COUNT + 1),
  29041. BAD_FUNC_ARG);
  29042. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  29043. WOLFSSL_SUCCESS);
  29044. #endif
  29045. #ifndef NO_WOLFSSL_SERVER
  29046. AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  29047. WOLFSSL_SUCCESS);
  29048. #endif
  29049. AssertIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  29050. #ifndef NO_WOLFSSL_CLIENT
  29051. AssertIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  29052. #endif
  29053. AssertIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  29054. #ifndef NO_WOLFSSL_CLIENT
  29055. #ifndef WOLFSSL_NO_TLS12
  29056. AssertIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  29057. BAD_FUNC_ARG);
  29058. #endif
  29059. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups,
  29060. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  29061. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  29062. WOLFSSL_SUCCESS);
  29063. #endif
  29064. #ifndef NO_WOLFSSL_SERVER
  29065. AssertIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  29066. WOLFSSL_SUCCESS);
  29067. #endif
  29068. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  29069. AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  29070. #ifndef NO_WOLFSSL_CLIENT
  29071. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL), WOLFSSL_FAILURE);
  29072. #endif
  29073. AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  29074. #ifndef NO_WOLFSSL_CLIENT
  29075. #ifndef WOLFSSL_NO_TLS12
  29076. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  29077. WOLFSSL_FAILURE);
  29078. #endif
  29079. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  29080. WOLFSSL_SUCCESS);
  29081. #endif
  29082. #ifndef NO_WOLFSSL_SERVER
  29083. AssertIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  29084. WOLFSSL_SUCCESS);
  29085. #endif
  29086. AssertIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  29087. #ifndef NO_WOLFSSL_CLIENT
  29088. AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  29089. #endif
  29090. AssertIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  29091. #ifndef NO_WOLFSSL_CLIENT
  29092. #ifndef WOLFSSL_NO_TLS12
  29093. AssertIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  29094. WOLFSSL_FAILURE);
  29095. #endif
  29096. AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  29097. WOLFSSL_SUCCESS);
  29098. #endif
  29099. #ifndef NO_WOLFSSL_SERVER
  29100. AssertIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  29101. WOLFSSL_SUCCESS);
  29102. #endif
  29103. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  29104. #ifdef WOLFSSL_EARLY_DATA
  29105. AssertIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  29106. #ifndef NO_WOLFSSL_CLIENT
  29107. AssertIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  29108. #endif
  29109. #ifndef NO_WOLFSSL_SERVER
  29110. #ifndef WOLFSSL_NO_TLS12
  29111. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  29112. BAD_FUNC_ARG);
  29113. #endif
  29114. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 0), 0);
  29115. #endif
  29116. AssertIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  29117. #ifndef NO_WOLFSSL_CLIENT
  29118. AssertIntEQ(wolfSSL_set_max_early_data(clientSsl, 0), SIDE_ERROR);
  29119. #endif
  29120. #ifndef NO_WOLFSSL_SERVER
  29121. #ifndef WOLFSSL_NO_TLS12
  29122. AssertIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  29123. #endif
  29124. AssertIntEQ(wolfSSL_set_max_early_data(serverSsl, 0), 0);
  29125. #endif
  29126. AssertIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  29127. &outSz), BAD_FUNC_ARG);
  29128. #ifndef NO_WOLFSSL_CLIENT
  29129. AssertIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  29130. &outSz), BAD_FUNC_ARG);
  29131. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  29132. BAD_FUNC_ARG);
  29133. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  29134. sizeof(earlyData), NULL),
  29135. BAD_FUNC_ARG);
  29136. #endif
  29137. #ifndef NO_WOLFSSL_SERVER
  29138. AssertIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  29139. sizeof(earlyData), &outSz),
  29140. SIDE_ERROR);
  29141. #endif
  29142. #ifndef NO_WOLFSSL_CLIENT
  29143. #ifndef WOLFSSL_NO_TLS12
  29144. AssertIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  29145. sizeof(earlyData), &outSz),
  29146. BAD_FUNC_ARG);
  29147. #endif
  29148. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  29149. sizeof(earlyData), &outSz),
  29150. WOLFSSL_FATAL_ERROR);
  29151. #endif
  29152. AssertIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  29153. sizeof(earlyDataBuffer), &outSz),
  29154. BAD_FUNC_ARG);
  29155. #ifndef NO_WOLFSSL_SERVER
  29156. AssertIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  29157. sizeof(earlyDataBuffer), &outSz),
  29158. BAD_FUNC_ARG);
  29159. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1, &outSz),
  29160. BAD_FUNC_ARG);
  29161. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  29162. sizeof(earlyDataBuffer), NULL),
  29163. BAD_FUNC_ARG);
  29164. #endif
  29165. #ifndef NO_WOLFSSL_CLIENT
  29166. AssertIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  29167. sizeof(earlyDataBuffer), &outSz),
  29168. SIDE_ERROR);
  29169. #endif
  29170. #ifndef NO_WOLFSSL_SERVER
  29171. #ifndef WOLFSSL_NO_TLS12
  29172. AssertIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  29173. sizeof(earlyDataBuffer), &outSz),
  29174. BAD_FUNC_ARG);
  29175. #endif
  29176. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  29177. sizeof(earlyDataBuffer), &outSz),
  29178. WOLFSSL_FATAL_ERROR);
  29179. #endif
  29180. #endif
  29181. #ifndef NO_WOLFSSL_SERVER
  29182. wolfSSL_free(serverSsl);
  29183. wolfSSL_CTX_free(serverCtx);
  29184. #endif
  29185. #ifndef NO_WOLFSSL_CLIENT
  29186. wolfSSL_free(clientSsl);
  29187. wolfSSL_CTX_free(clientCtx);
  29188. #endif
  29189. #ifndef WOLFSSL_NO_TLS12
  29190. #ifndef NO_WOLFSSL_SERVER
  29191. wolfSSL_free(serverTls12Ssl);
  29192. wolfSSL_CTX_free(serverTls12Ctx);
  29193. #endif
  29194. #ifndef NO_WOLFSSL_CLIENT
  29195. wolfSSL_free(clientTls12Ssl);
  29196. wolfSSL_CTX_free(clientTls12Ctx);
  29197. #endif
  29198. #endif
  29199. return ret;
  29200. }
  29201. #endif
  29202. #ifdef HAVE_PK_CALLBACKS
  29203. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  29204. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  29205. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  29206. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  29207. const unsigned char* priv, unsigned int privSz,
  29208. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  29209. unsigned char* out, unsigned int* outlen,
  29210. void* ctx)
  29211. {
  29212. /* Test fail when context associated with WOLFSSL is NULL */
  29213. if (ctx == NULL) {
  29214. return -1;
  29215. }
  29216. (void)ssl;
  29217. /* return 0 on success */
  29218. return wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  29219. };
  29220. static void test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  29221. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  29222. #ifdef WOLFSSL_AES_128
  29223. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  29224. WOLFSSL_SUCCESS);
  29225. #endif
  29226. #ifdef WOLFSSL_AES_256
  29227. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  29228. WOLFSSL_SUCCESS);
  29229. #endif
  29230. }
  29231. static void test_dh_ssl_setup(WOLFSSL* ssl)
  29232. {
  29233. static int dh_test_ctx = 1;
  29234. int ret;
  29235. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  29236. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  29237. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  29238. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  29239. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  29240. }
  29241. }
  29242. static void test_dh_ssl_setup_fail(WOLFSSL* ssl)
  29243. {
  29244. int ret;
  29245. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  29246. AssertNull(wolfSSL_GetDhAgreeCtx(ssl));
  29247. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  29248. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  29249. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  29250. }
  29251. }
  29252. #endif
  29253. static void test_DhCallbacks(void)
  29254. {
  29255. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  29256. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  29257. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  29258. WOLFSSL_CTX *ctx;
  29259. WOLFSSL *ssl;
  29260. tcp_ready ready;
  29261. func_args server_args;
  29262. func_args client_args;
  29263. THREAD_TYPE serverThread;
  29264. callback_functions func_cb_client;
  29265. callback_functions func_cb_server;
  29266. int test;
  29267. printf(testingFmt, "test_DhCallbacks");
  29268. #ifndef NO_WOLFSSL_CLIENT
  29269. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  29270. #else
  29271. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  29272. #endif
  29273. AssertIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  29274. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  29275. /* load client ca cert */
  29276. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  29277. WOLFSSL_SUCCESS);
  29278. /* test with NULL arguments */
  29279. wolfSSL_SetDhAgreeCtx(NULL, &test);
  29280. AssertNull(wolfSSL_GetDhAgreeCtx(NULL));
  29281. /* test success case */
  29282. test = 1;
  29283. AssertNotNull(ssl = wolfSSL_new(ctx));
  29284. wolfSSL_SetDhAgreeCtx(ssl, &test);
  29285. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  29286. wolfSSL_free(ssl);
  29287. wolfSSL_CTX_free(ctx);
  29288. /* test a connection where callback is used */
  29289. #ifdef WOLFSSL_TIRTOS
  29290. fdOpenSession(Task_self());
  29291. #endif
  29292. XMEMSET(&server_args, 0, sizeof(func_args));
  29293. XMEMSET(&client_args, 0, sizeof(func_args));
  29294. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  29295. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  29296. StartTCP();
  29297. InitTcpReady(&ready);
  29298. #if defined(USE_WINDOWS_API)
  29299. /* use RNG to get random port if using windows */
  29300. ready.port = GetRandomPort();
  29301. #endif
  29302. server_args.signal = &ready;
  29303. client_args.signal = &ready;
  29304. server_args.return_code = TEST_FAIL;
  29305. client_args.return_code = TEST_FAIL;
  29306. /* set callbacks to use DH functions */
  29307. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  29308. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  29309. #ifndef WOLFSSL_NO_TLS12
  29310. func_cb_client.method = wolfTLSv1_2_client_method;
  29311. #else
  29312. func_cb_client.method = wolfTLSv1_3_client_method;
  29313. #endif
  29314. client_args.callbacks = &func_cb_client;
  29315. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  29316. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  29317. #ifndef WOLFSSL_NO_TLS12
  29318. func_cb_server.method = wolfTLSv1_2_server_method;
  29319. #else
  29320. func_cb_server.method = wolfTLSv1_3_server_method;
  29321. #endif
  29322. server_args.callbacks = &func_cb_server;
  29323. start_thread(test_server_nofail, &server_args, &serverThread);
  29324. wait_tcp_ready(&server_args);
  29325. test_client_nofail(&client_args, NULL);
  29326. join_thread(serverThread);
  29327. AssertTrue(client_args.return_code);
  29328. AssertTrue(server_args.return_code);
  29329. FreeTcpReady(&ready);
  29330. #ifdef WOLFSSL_TIRTOS
  29331. fdOpenSession(Task_self());
  29332. #endif
  29333. /* now set user ctx to not be 1 so that the callback returns fail case */
  29334. #ifdef WOLFSSL_TIRTOS
  29335. fdOpenSession(Task_self());
  29336. #endif
  29337. XMEMSET(&server_args, 0, sizeof(func_args));
  29338. XMEMSET(&client_args, 0, sizeof(func_args));
  29339. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  29340. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  29341. StartTCP();
  29342. InitTcpReady(&ready);
  29343. #if defined(USE_WINDOWS_API)
  29344. /* use RNG to get random port if using windows */
  29345. ready.port = GetRandomPort();
  29346. #endif
  29347. server_args.signal = &ready;
  29348. client_args.signal = &ready;
  29349. server_args.return_code = TEST_FAIL;
  29350. client_args.return_code = TEST_FAIL;
  29351. /* set callbacks to use DH functions */
  29352. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  29353. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  29354. #ifndef WOLFSSL_NO_TLS12
  29355. func_cb_client.method = wolfTLSv1_2_client_method;
  29356. #else
  29357. func_cb_client.method = wolfTLSv1_3_client_method;
  29358. #endif
  29359. client_args.callbacks = &func_cb_client;
  29360. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  29361. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  29362. #ifndef WOLFSSL_NO_TLS12
  29363. func_cb_server.method = wolfTLSv1_2_server_method;
  29364. #else
  29365. func_cb_server.method = wolfTLSv1_3_server_method;
  29366. #endif
  29367. server_args.callbacks = &func_cb_server;
  29368. start_thread(test_server_nofail, &server_args, &serverThread);
  29369. wait_tcp_ready(&server_args);
  29370. test_client_nofail(&client_args, NULL);
  29371. join_thread(serverThread);
  29372. AssertIntEQ(client_args.return_code, TEST_FAIL);
  29373. AssertIntEQ(server_args.return_code, TEST_FAIL);
  29374. FreeTcpReady(&ready);
  29375. #ifdef WOLFSSL_TIRTOS
  29376. fdOpenSession(Task_self());
  29377. #endif
  29378. printf(resultFmt, passed);
  29379. #endif
  29380. }
  29381. #endif /* HAVE_PK_CALLBACKS */
  29382. #ifdef HAVE_HASHDRBG
  29383. #ifdef TEST_RESEED_INTERVAL
  29384. static int test_wc_RNG_GenerateBlock_Reseed(void)
  29385. {
  29386. int i, ret;
  29387. WC_RNG rng;
  29388. byte key[32];
  29389. ret = wc_InitRng(&rng);
  29390. if (ret == 0) {
  29391. for(i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  29392. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  29393. if (ret != 0) {
  29394. break;
  29395. }
  29396. }
  29397. }
  29398. wc_FreeRng(&rng);
  29399. return ret;
  29400. }
  29401. #endif /* TEST_RESEED_INTERVAL */
  29402. static int test_wc_RNG_GenerateBlock(void)
  29403. {
  29404. int i, ret;
  29405. WC_RNG rng;
  29406. byte key[32];
  29407. ret = wc_InitRng(&rng);
  29408. if (ret == 0) {
  29409. for(i = 0; i < 10; i++) {
  29410. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  29411. if (ret != 0) {
  29412. break;
  29413. }
  29414. }
  29415. }
  29416. wc_FreeRng(&rng);
  29417. (void)rng; /* for WC_NO_RNG case */
  29418. (void)key;
  29419. return ret;
  29420. }
  29421. #endif
  29422. /*
  29423. * Testing get_rand_digit
  29424. */
  29425. static int test_get_rand_digit (void)
  29426. {
  29427. int ret = 0;
  29428. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  29429. WC_RNG rng;
  29430. mp_digit d;
  29431. printf(testingFmt, "get_rand_digit()");
  29432. ret = wc_InitRng(&rng);
  29433. if (ret == 0) {
  29434. ret = get_rand_digit(&rng, &d);
  29435. }
  29436. if (ret == 0) {
  29437. ret = get_rand_digit(NULL, NULL);
  29438. if (ret == BAD_FUNC_ARG) {
  29439. ret = 0;
  29440. }
  29441. }
  29442. if (ret == 0) {
  29443. ret = get_rand_digit(NULL, &d);
  29444. if (ret == BAD_FUNC_ARG) {
  29445. ret = 0;
  29446. }
  29447. }
  29448. if (ret == 0) {
  29449. ret = get_rand_digit(&rng, NULL);
  29450. if (ret == BAD_FUNC_ARG) {
  29451. ret = 0;
  29452. }
  29453. }
  29454. if (ret == 0) {
  29455. ret = wc_FreeRng(&rng);
  29456. }
  29457. printf(resultFmt, ret == 0 ? passed : failed);
  29458. #endif
  29459. return ret;
  29460. }/* End test_get_rand_digit*/
  29461. /*
  29462. * Testing get_digit_count
  29463. */
  29464. static int test_get_digit_count (void)
  29465. {
  29466. int ret = 0;
  29467. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  29468. mp_int a;
  29469. printf(testingFmt, "get_digit_count()");
  29470. if (mp_init(&a) != MP_OKAY) {
  29471. ret = -1;
  29472. }
  29473. if (ret == 0) {
  29474. ret = get_digit_count(NULL);
  29475. }
  29476. if (ret == 0) {
  29477. ret = get_digit_count(&a);
  29478. }
  29479. printf(resultFmt, ret == 0 ? passed : failed);
  29480. mp_clear(&a);
  29481. #endif
  29482. return ret;
  29483. }/* End test_get_digit_count*/
  29484. /*
  29485. * Testing mp_cond_copy
  29486. */
  29487. static int test_mp_cond_copy (void)
  29488. {
  29489. int ret = 0;
  29490. #if defined(WOLFSSL_PUBLIC_MP)
  29491. mp_int a;
  29492. mp_int b;
  29493. int copy = 0;
  29494. printf(testingFmt, "mp_cond_copy()");
  29495. if (mp_init(&a) != MP_OKAY) {
  29496. ret = -1;
  29497. }
  29498. if (ret == 0) {
  29499. if (mp_init(&b) != MP_OKAY) {
  29500. ret = -1;
  29501. }
  29502. }
  29503. if (ret == 0) {
  29504. ret = mp_cond_copy(NULL, copy, NULL);
  29505. if (ret == BAD_FUNC_ARG) {
  29506. ret = 0;
  29507. }
  29508. }
  29509. if (ret == 0) {
  29510. ret = mp_cond_copy(NULL, copy, &b);
  29511. if (ret == BAD_FUNC_ARG) {
  29512. ret = 0;
  29513. }
  29514. }
  29515. if (ret == 0) {
  29516. ret = mp_cond_copy(&a, copy, NULL);
  29517. if (ret == BAD_FUNC_ARG) {
  29518. ret = 0;
  29519. }
  29520. }
  29521. if (ret == 0) {
  29522. ret = mp_cond_copy(&a, copy, &b);
  29523. }
  29524. printf(resultFmt, ret == 0 ? passed : failed);
  29525. mp_clear(&a);
  29526. mp_clear(&b);
  29527. #endif
  29528. return ret;
  29529. }/* End test_mp_cond_copy*/
  29530. /*
  29531. * Testing mp_rand
  29532. */
  29533. static int test_mp_rand (void)
  29534. {
  29535. int ret = 0;
  29536. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  29537. mp_int a;
  29538. int digits = 1;
  29539. WC_RNG rng;
  29540. printf(testingFmt, "mp_rand()");
  29541. if (mp_init(&a) != MP_OKAY) {
  29542. ret = -1;
  29543. }
  29544. if (ret == 0) {
  29545. ret = wc_InitRng(&rng);
  29546. }
  29547. if (ret == 0) {
  29548. ret = mp_rand(&a, digits, NULL);
  29549. if (ret == MISSING_RNG_E) {
  29550. ret = 0;
  29551. }
  29552. }
  29553. if (ret == 0) {
  29554. ret = mp_rand(NULL, digits, &rng);
  29555. if (ret == BAD_FUNC_ARG) {
  29556. ret = 0;
  29557. }
  29558. }
  29559. if (ret == 0) {
  29560. ret = mp_rand(&a, 0, &rng);
  29561. if (ret == BAD_FUNC_ARG) {
  29562. ret = 0;
  29563. }
  29564. }
  29565. if (ret == 0) {
  29566. ret = mp_rand(&a, digits, &rng);
  29567. }
  29568. printf(resultFmt, ret == 0 ? passed : failed);
  29569. mp_clear(&a);
  29570. wc_FreeRng(&rng);
  29571. #endif
  29572. return ret;
  29573. }/* End test_mp_rand*/
  29574. /*
  29575. * Testing get_digit
  29576. */
  29577. static int test_get_digit (void)
  29578. {
  29579. int ret = 0;
  29580. #if defined(WOLFSSL_PUBLIC_MP)
  29581. mp_int a;
  29582. int n = 0;
  29583. printf(testingFmt, "get_digit()");
  29584. if (mp_init(&a) != MP_OKAY) {
  29585. ret = -1;
  29586. }
  29587. if (ret == 0) {
  29588. if (get_digit(NULL, n) != 0) { /* Should not hit this */
  29589. ret = -1;
  29590. }
  29591. }
  29592. if (ret == 0) {
  29593. if (get_digit(NULL, n) == 0) { /* Should hit this */
  29594. ret = 0;
  29595. }
  29596. }
  29597. if (ret == 0) {
  29598. if (get_digit(&a, n) != 0) { /* Should not hit this */
  29599. ret = -1;
  29600. }
  29601. }
  29602. if (ret == 0) {
  29603. if (get_digit(&a, n) == 0) { /* Should hit this */
  29604. ret = 0;
  29605. }
  29606. }
  29607. printf(resultFmt, ret == 0 ? passed : failed);
  29608. mp_clear(&a);
  29609. #endif
  29610. return ret;
  29611. }/* End test_get_digit*/
  29612. /*
  29613. * Testing wc_export_int
  29614. */
  29615. static int test_wc_export_int (void)
  29616. {
  29617. int ret = 0;
  29618. #if defined(WOLFSSL_PUBLIC_MP)
  29619. mp_int mp;
  29620. byte buf[256];
  29621. word32 keySz = (word32)sizeof(buf);
  29622. word32 len = (word32)sizeof(buf);
  29623. int encType = WC_TYPE_UNSIGNED_BIN;
  29624. printf(testingFmt, "wc_export_int()");
  29625. if (mp_init(&mp) != MP_OKAY) {
  29626. ret = -1;
  29627. }
  29628. if (ret == 0) {
  29629. ret = wc_export_int(NULL, buf, &len, keySz, encType);
  29630. if (ret == BAD_FUNC_ARG) {
  29631. ret = 0;
  29632. }
  29633. }
  29634. len = sizeof(buf)-1;
  29635. if (ret == 0) {
  29636. ret = wc_export_int(&mp, buf, &len, keySz, encType);
  29637. if (ret == BUFFER_E) {
  29638. ret = 0;
  29639. }
  29640. }
  29641. len = sizeof(buf);
  29642. if (ret == 0) {
  29643. ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_HEX_STR);
  29644. }
  29645. if (ret == 0) {
  29646. ret = wc_export_int(&mp, buf, &len, keySz, encType);
  29647. }
  29648. printf(resultFmt, ret == 0 ? passed : failed);
  29649. mp_clear(&mp);
  29650. #endif
  29651. return ret;
  29652. }/* End test_wc_export_int*/
  29653. static int test_wc_InitRngNonce(void)
  29654. {
  29655. int ret=0;
  29656. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  29657. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  29658. WC_RNG rng;
  29659. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  29660. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  29661. word32 nonceSz = sizeof(nonce);
  29662. printf(testingFmt, "wc_InitRngNonce()");
  29663. if (ret == 0){
  29664. ret = wc_InitRngNonce(&rng, nonce, nonceSz);
  29665. }
  29666. wc_FreeRng(&rng);
  29667. printf(resultFmt, ret == 0 ? passed : failed);
  29668. #endif
  29669. return ret;
  29670. }/* End test_wc_InitRngNonce*/
  29671. /*
  29672. * Testing wc_InitRngNonce_ex
  29673. */
  29674. static int test_wc_InitRngNonce_ex(void)
  29675. {
  29676. int ret=0;
  29677. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  29678. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  29679. WC_RNG rng;
  29680. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  29681. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  29682. word32 nonceSz = sizeof(nonce);
  29683. printf(testingFmt, "wc_InitRngNonce_ex()");
  29684. if (ret == 0){
  29685. ret = wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, devId);
  29686. }
  29687. wc_FreeRng(&rng);
  29688. printf(resultFmt, ret == 0 ? passed : failed);
  29689. #endif
  29690. return ret;
  29691. }/*End test_wc_InitRngNonce_ex*/
  29692. static void test_wolfSSL_X509_CRL(void)
  29693. {
  29694. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  29695. X509_CRL *crl;
  29696. char pem[][100] = {
  29697. "./certs/crl/crl.pem",
  29698. "./certs/crl/crl2.pem",
  29699. "./certs/crl/caEccCrl.pem",
  29700. "./certs/crl/eccCliCRL.pem",
  29701. "./certs/crl/eccSrvCRL.pem",
  29702. ""
  29703. };
  29704. BIO *bio;
  29705. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  29706. char der[][100] = {
  29707. "./certs/crl/crl.der",
  29708. "./certs/crl/crl2.der",
  29709. ""};
  29710. #endif
  29711. XFILE fp;
  29712. int i;
  29713. printf(testingFmt, "test_wolfSSL_X509_CRL");
  29714. for (i = 0; pem[i][0] != '\0'; i++)
  29715. {
  29716. fp = XFOPEN(pem[i], "rb");
  29717. AssertTrue((fp != XBADFILE));
  29718. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
  29719. AssertNotNull(crl);
  29720. X509_CRL_free(crl);
  29721. XFCLOSE(fp);
  29722. fp = XFOPEN(pem[i], "rb");
  29723. AssertTrue((fp != XBADFILE));
  29724. AssertNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL, NULL));
  29725. AssertNotNull(crl);
  29726. X509_CRL_free(crl);
  29727. XFCLOSE(fp);
  29728. }
  29729. for (i = 0; pem[i][0] != '\0'; i++)
  29730. {
  29731. AssertNotNull(bio = BIO_new_file(pem[i], "r"));
  29732. AssertNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  29733. X509_CRL_free(crl);
  29734. BIO_free(bio);
  29735. }
  29736. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  29737. for(i = 0; der[i][0] != '\0'; i++){
  29738. fp = XFOPEN(der[i], "rb");
  29739. AssertTrue((fp != XBADFILE));
  29740. AssertNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  29741. AssertNotNull(crl);
  29742. X509_CRL_free(crl);
  29743. XFCLOSE(fp);
  29744. fp = XFOPEN(der[i], "rb");
  29745. AssertTrue((fp != XBADFILE));
  29746. AssertNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  29747. AssertNotNull(crl);
  29748. X509_CRL_free(crl);
  29749. XFCLOSE(fp);
  29750. }
  29751. #endif
  29752. printf(resultFmt, passed);
  29753. #endif
  29754. return;
  29755. }
  29756. static void test_wolfSSL_PEM_read_X509(void)
  29757. {
  29758. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  29759. !defined(NO_RSA)
  29760. X509 *x509 = NULL;
  29761. XFILE fp;
  29762. printf(testingFmt, "wolfSSL_PEM_read_X509");
  29763. fp = XFOPEN(svrCertFile, "rb");
  29764. AssertTrue((fp != XBADFILE));
  29765. AssertNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  29766. X509_free(x509);
  29767. XFCLOSE(fp);
  29768. printf(resultFmt, passed);
  29769. #endif
  29770. }
  29771. static void test_wolfSSL_PEM_read(void)
  29772. {
  29773. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  29774. const char* filename = "./certs/server-keyEnc.pem";
  29775. XFILE fp;
  29776. char* name = NULL;
  29777. char* header = NULL;
  29778. byte* data = NULL;
  29779. long len;
  29780. EVP_CIPHER_INFO cipher;
  29781. WOLFSSL_BIO* bio;
  29782. byte* fileData;
  29783. size_t fileDataSz;
  29784. byte* out;
  29785. printf(testingFmt, "wolfSSL_PEM_read");
  29786. fp = XFOPEN(filename, "rb");
  29787. AssertTrue((fp != XBADFILE));
  29788. /* Fail cases. */
  29789. AssertIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  29790. AssertIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  29791. AssertIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  29792. AssertIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  29793. AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  29794. AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  29795. AssertIntGT(XSTRLEN(header), 0);
  29796. AssertIntGT(len, 0);
  29797. AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  29798. AssertIntGT((fileDataSz = XFTELL(fp)), 0);
  29799. AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  29800. AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  29801. DYNAMIC_TYPE_TMP_BUFFER));
  29802. AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  29803. XFCLOSE(fp);
  29804. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29805. /* Fail cases. */
  29806. AssertIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  29807. AssertIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  29808. AssertIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  29809. AssertIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  29810. AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  29811. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  29812. AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  29813. /* Fail cases. */
  29814. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  29815. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  29816. AssertIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  29817. #ifndef NO_DES3
  29818. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  29819. #endif
  29820. /* Fail cases. */
  29821. AssertIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  29822. (void*)"yassl123"), WOLFSSL_FAILURE);
  29823. AssertIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  29824. (void*)"yassl123"), WOLFSSL_FAILURE);
  29825. AssertIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  29826. (void*)"yassl123"), WOLFSSL_FAILURE);
  29827. #if !defined(NO_DES3) && !defined(NO_MD5)
  29828. AssertIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  29829. (void*)"yassl123"), WOLFSSL_SUCCESS);
  29830. #endif
  29831. BIO_free(bio);
  29832. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29833. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29834. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29835. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29836. name = NULL;
  29837. header = NULL;
  29838. data = NULL;
  29839. fp = XFOPEN(svrKeyFile, "rb");
  29840. AssertTrue((fp != XBADFILE));
  29841. AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  29842. AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  29843. AssertIntEQ(XSTRLEN(header), 0);
  29844. AssertIntGT(len, 0);
  29845. AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  29846. AssertIntGT((fileDataSz = XFTELL(fp)), 0);
  29847. AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  29848. AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  29849. DYNAMIC_TYPE_TMP_BUFFER));
  29850. AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  29851. XFCLOSE(fp);
  29852. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29853. AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  29854. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  29855. AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  29856. BIO_free(bio);
  29857. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29858. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29859. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29860. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29861. printf(resultFmt, passed);
  29862. #endif
  29863. }
  29864. static void test_wolfssl_EVP_aes_gcm(void)
  29865. {
  29866. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  29867. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  29868. /* A 256 bit key, AES_128 will use the first 128 bit*/
  29869. byte *key = (byte*)"01234567890123456789012345678901";
  29870. /* A 128 bit IV */
  29871. byte *iv = (byte*)"0123456789012345";
  29872. int ivSz = AES_BLOCK_SIZE;
  29873. /* Message to be encrypted */
  29874. byte *plaintxt = (byte*)"for things to change you have to change";
  29875. /* Additional non-confidential data */
  29876. byte *aad = (byte*)"Don't spend major time on minor things.";
  29877. unsigned char tag[AES_BLOCK_SIZE] = {0};
  29878. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  29879. int aadSz = (int)XSTRLEN((char*)aad);
  29880. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  29881. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  29882. int ciphertxtSz = 0;
  29883. int decryptedtxtSz = 0;
  29884. int len = 0;
  29885. int i = 0;
  29886. EVP_CIPHER_CTX en[2];
  29887. EVP_CIPHER_CTX de[2];
  29888. printf(testingFmt, "wolfssl_EVP_aes_gcm");
  29889. for (i = 0; i < 2; i++) {
  29890. EVP_CIPHER_CTX_init(&en[i]);
  29891. if (i == 0) {
  29892. /* Default uses 96-bits IV length */
  29893. #ifdef WOLFSSL_AES_128
  29894. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, key, iv));
  29895. #elif defined(WOLFSSL_AES_192)
  29896. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, key, iv));
  29897. #elif defined(WOLFSSL_AES_256)
  29898. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, key, iv));
  29899. #endif
  29900. }
  29901. else {
  29902. #ifdef WOLFSSL_AES_128
  29903. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
  29904. #elif defined(WOLFSSL_AES_192)
  29905. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
  29906. #elif defined(WOLFSSL_AES_256)
  29907. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
  29908. #endif
  29909. /* non-default must to set the IV length first */
  29910. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  29911. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  29912. }
  29913. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  29914. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  29915. ciphertxtSz = len;
  29916. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  29917. ciphertxtSz += len;
  29918. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, AES_BLOCK_SIZE, tag));
  29919. EVP_CIPHER_CTX_init(&de[i]);
  29920. if (i == 0) {
  29921. /* Default uses 96-bits IV length */
  29922. #ifdef WOLFSSL_AES_128
  29923. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, key, iv));
  29924. #elif defined(WOLFSSL_AES_192)
  29925. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, key, iv));
  29926. #elif defined(WOLFSSL_AES_256)
  29927. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, key, iv));
  29928. #endif
  29929. }
  29930. else {
  29931. #ifdef WOLFSSL_AES_128
  29932. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
  29933. #elif defined(WOLFSSL_AES_192)
  29934. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
  29935. #elif defined(WOLFSSL_AES_256)
  29936. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
  29937. #endif
  29938. /* non-default must to set the IV length first */
  29939. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  29940. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  29941. }
  29942. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  29943. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  29944. decryptedtxtSz = len;
  29945. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
  29946. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  29947. decryptedtxtSz += len;
  29948. AssertIntEQ(ciphertxtSz, decryptedtxtSz);
  29949. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  29950. /* modify tag*/
  29951. tag[AES_BLOCK_SIZE-1]+=0xBB;
  29952. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  29953. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
  29954. /* fail due to wrong tag */
  29955. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  29956. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  29957. AssertIntEQ(0, len);
  29958. }
  29959. printf(resultFmt, passed);
  29960. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  29961. }
  29962. static void test_wolfSSL_PEM_X509_INFO_read_bio(void)
  29963. {
  29964. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM)
  29965. BIO* bio;
  29966. X509_INFO* info;
  29967. STACK_OF(X509_INFO)* sk;
  29968. char* subject;
  29969. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  29970. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  29971. printf(testingFmt, "wolfSSL_PEM_X509_INFO_read_bio");
  29972. AssertNotNull(bio = BIO_new(BIO_s_file()));
  29973. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  29974. AssertNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  29975. AssertIntEQ(sk_X509_INFO_num(sk), 2);
  29976. /* using dereference to maintain testing for Apache port*/
  29977. AssertNotNull(info = sk_X509_INFO_pop(sk));
  29978. AssertNotNull(info->x_pkey);
  29979. AssertNotNull(info->x_pkey->dec_pkey);
  29980. AssertIntEQ(EVP_PKEY_bits(info->x_pkey->dec_pkey), 2048);
  29981. AssertNotNull(subject =
  29982. X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
  29983. AssertIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  29984. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  29985. X509_INFO_free(info);
  29986. AssertNotNull(info = sk_X509_INFO_pop(sk));
  29987. AssertNotNull(subject =
  29988. X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
  29989. AssertIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  29990. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  29991. X509_INFO_free(info);
  29992. AssertNull(info = sk_X509_INFO_pop(sk));
  29993. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  29994. BIO_free(bio);
  29995. printf(resultFmt, passed);
  29996. #endif
  29997. }
  29998. static void test_wolfSSL_X509_NAME_ENTRY_get_object()
  29999. {
  30000. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30001. X509 *x509;
  30002. X509_NAME* name;
  30003. int idx = 0;
  30004. X509_NAME_ENTRY *ne;
  30005. ASN1_OBJECT *object = NULL;
  30006. printf(testingFmt, "wolfSSL_X509_NAME_ENTRY_get_object");
  30007. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  30008. AssertNotNull(x509);
  30009. name = X509_get_subject_name(x509);
  30010. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  30011. AssertIntGE(idx, 0);
  30012. ne = X509_NAME_get_entry(name, idx);
  30013. AssertNotNull(ne);
  30014. AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
  30015. X509_free(x509);
  30016. printf(resultFmt, passed);
  30017. #endif
  30018. }
  30019. static void test_wolfSSL_ASN1_INTEGER_set()
  30020. {
  30021. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  30022. ASN1_INTEGER *a;
  30023. long val;
  30024. int ret;
  30025. printf(testingFmt, "wolfSSL_ASN1_INTEGER_set");
  30026. a = wolfSSL_ASN1_INTEGER_new();
  30027. val = 0;
  30028. ret = ASN1_INTEGER_set(NULL, val);
  30029. AssertIntEQ(ret, 0);
  30030. wolfSSL_ASN1_INTEGER_free(a);
  30031. /* 0 */
  30032. a = wolfSSL_ASN1_INTEGER_new();
  30033. val = 0;
  30034. ret = ASN1_INTEGER_set(a, val);
  30035. AssertIntEQ(ret, 1);
  30036. wolfSSL_ASN1_INTEGER_free(a);
  30037. /* 40 */
  30038. a = wolfSSL_ASN1_INTEGER_new();
  30039. val = 40;
  30040. ret = ASN1_INTEGER_set(a, val);
  30041. AssertIntEQ(ret, 1);
  30042. wolfSSL_ASN1_INTEGER_free(a);
  30043. /* -40 */
  30044. a = wolfSSL_ASN1_INTEGER_new();
  30045. val = -40;
  30046. ret = ASN1_INTEGER_set(a, val);
  30047. AssertIntEQ(ret, 1);
  30048. AssertIntEQ(a->negative, 1);
  30049. wolfSSL_ASN1_INTEGER_free(a);
  30050. /* 128 */
  30051. a = wolfSSL_ASN1_INTEGER_new();
  30052. val = 128;
  30053. ret = ASN1_INTEGER_set(a, val);
  30054. AssertIntEQ(ret, 1);
  30055. wolfSSL_ASN1_INTEGER_free(a);
  30056. /* -128 */
  30057. a = wolfSSL_ASN1_INTEGER_new();
  30058. val = -128;
  30059. ret = ASN1_INTEGER_set(a, val);
  30060. AssertIntEQ(ret, 1);
  30061. AssertIntEQ(a->negative, 1);
  30062. wolfSSL_ASN1_INTEGER_free(a);
  30063. /* 200 */
  30064. a = wolfSSL_ASN1_INTEGER_new();
  30065. val = 200;
  30066. ret = ASN1_INTEGER_set(a, val);
  30067. AssertIntEQ(ret, 1);
  30068. wolfSSL_ASN1_INTEGER_free(a);
  30069. #ifndef TIME_T_NOT_64BIT
  30070. /* int max (2147483647) */
  30071. a = wolfSSL_ASN1_INTEGER_new();
  30072. val = 2147483647;
  30073. ret = ASN1_INTEGER_set(a, val);
  30074. AssertIntEQ(ret, 1);
  30075. wolfSSL_ASN1_INTEGER_free(a);
  30076. /* int min (-2147483648) */
  30077. a = wolfSSL_ASN1_INTEGER_new();
  30078. val = -2147483647 - 1;
  30079. ret = ASN1_INTEGER_set(a, val);
  30080. AssertIntEQ(a->negative, 1);
  30081. AssertIntEQ(ret, 1);
  30082. wolfSSL_ASN1_INTEGER_free(a);
  30083. #endif
  30084. printf(resultFmt, passed);
  30085. #endif
  30086. }
  30087. /* Testing code used in dpp.c in hostap */
  30088. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  30089. typedef struct {
  30090. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  30091. * as an OID identifying the curve */
  30092. X509_ALGOR *alg;
  30093. /* Compressed format public key per ANSI X9.63 */
  30094. ASN1_BIT_STRING *pub_key;
  30095. } DPP_BOOTSTRAPPING_KEY;
  30096. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  30097. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  30098. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  30099. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY);
  30100. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY);
  30101. #endif
  30102. static void test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS()
  30103. {
  30104. /* Testing code used in dpp.c in hostap */
  30105. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  30106. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  30107. EC_KEY *eckey;
  30108. EVP_PKEY *key;
  30109. size_t len;
  30110. unsigned char *der = NULL;
  30111. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  30112. const unsigned char *in = ecc_clikey_der_256;
  30113. const EC_GROUP *group;
  30114. const EC_POINT *point;
  30115. int nid;
  30116. AssertNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  30117. AssertNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  30118. (long)sizeof_ecc_clikey_der_256));
  30119. AssertNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  30120. AssertNotNull(group = EC_KEY_get0_group(eckey));
  30121. AssertNotNull(point = EC_KEY_get0_public_key(eckey));
  30122. nid = EC_GROUP_get_curve_name(group);
  30123. AssertIntEQ(X509_ALGOR_set0(bootstrap->alg, OBJ_nid2obj(EVP_PKEY_EC),
  30124. V_ASN1_OBJECT, OBJ_nid2obj(nid)), 1);
  30125. #ifdef HAVE_COMP_KEY
  30126. AssertIntGT((len = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  30127. NULL, 0, NULL)), 0);
  30128. #else
  30129. AssertIntGT((len = EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  30130. NULL, 0, NULL)), 0);
  30131. #endif
  30132. AssertNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  30133. #ifdef HAVE_COMP_KEY
  30134. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  30135. der, len, NULL), len);
  30136. #else
  30137. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  30138. der, len, NULL), len);
  30139. #endif
  30140. bootstrap->pub_key->data = der;
  30141. bootstrap->pub_key->length = (int)len;
  30142. /* Not actually used */
  30143. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  30144. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  30145. der = NULL;
  30146. AssertIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  30147. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  30148. EVP_PKEY_free(key);
  30149. EC_KEY_free(eckey);
  30150. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  30151. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  30152. #endif /* WOLFSSL_WPAS && HAVE_ECC && USE_CERT_BUFFERS_256 */
  30153. }
  30154. static void test_wolfSSL_i2c_ASN1_INTEGER()
  30155. {
  30156. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  30157. ASN1_INTEGER *a;
  30158. unsigned char *pp,*tpp;
  30159. int ret;
  30160. printf(testingFmt, "wolfSSL_i2c_ASN1_INTEGER");
  30161. a = wolfSSL_ASN1_INTEGER_new();
  30162. /* 40 */
  30163. a->intData[0] = ASN_INTEGER;
  30164. a->intData[1] = 1;
  30165. a->intData[2] = 40;
  30166. ret = i2c_ASN1_INTEGER(a, NULL);
  30167. AssertIntEQ(ret, 1);
  30168. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  30169. DYNAMIC_TYPE_TMP_BUFFER));
  30170. tpp = pp;
  30171. XMEMSET(pp, 0, ret + 1);
  30172. i2c_ASN1_INTEGER(a, &pp);
  30173. pp--;
  30174. AssertIntEQ(*pp, 40);
  30175. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  30176. /* 128 */
  30177. a->intData[0] = ASN_INTEGER;
  30178. a->intData[1] = 1;
  30179. a->intData[2] = 128;
  30180. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  30181. AssertIntEQ(ret, 2);
  30182. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  30183. DYNAMIC_TYPE_TMP_BUFFER));
  30184. tpp = pp;
  30185. XMEMSET(pp, 0, ret + 1);
  30186. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  30187. pp--;
  30188. AssertIntEQ(*(pp--), 128);
  30189. AssertIntEQ(*pp, 0);
  30190. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  30191. /* -40 */
  30192. a->intData[0] = ASN_INTEGER;
  30193. a->intData[1] = 1;
  30194. a->intData[2] = 40;
  30195. a->negative = 1;
  30196. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  30197. AssertIntEQ(ret, 1);
  30198. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  30199. DYNAMIC_TYPE_TMP_BUFFER));
  30200. tpp = pp;
  30201. XMEMSET(pp, 0, ret + 1);
  30202. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  30203. pp--;
  30204. AssertIntEQ(*pp, 216);
  30205. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  30206. /* -128 */
  30207. a->intData[0] = ASN_INTEGER;
  30208. a->intData[1] = 1;
  30209. a->intData[2] = 128;
  30210. a->negative = 1;
  30211. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  30212. AssertIntEQ(ret, 1);
  30213. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  30214. DYNAMIC_TYPE_TMP_BUFFER));
  30215. tpp = pp;
  30216. XMEMSET(pp, 0, ret + 1);
  30217. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  30218. pp--;
  30219. AssertIntEQ(*pp, 128);
  30220. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  30221. /* -200 */
  30222. a->intData[0] = ASN_INTEGER;
  30223. a->intData[1] = 1;
  30224. a->intData[2] = 200;
  30225. a->negative = 1;
  30226. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  30227. AssertIntEQ(ret, 2);
  30228. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  30229. DYNAMIC_TYPE_TMP_BUFFER));
  30230. tpp = pp;
  30231. XMEMSET(pp, 0, ret + 1);
  30232. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  30233. pp--;
  30234. AssertIntEQ(*(pp--), 56);
  30235. AssertIntEQ(*pp, 255);
  30236. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  30237. wolfSSL_ASN1_INTEGER_free(a);
  30238. printf(resultFmt, passed);
  30239. #endif /* OPENSSL_EXTRA && !NO_ASN */
  30240. }
  30241. #ifndef NO_INLINE
  30242. #define WOLFSSL_MISC_INCLUDED
  30243. #include <wolfcrypt/src/misc.c>
  30244. #else
  30245. #include <wolfssl/wolfcrypt/misc.h>
  30246. #endif
  30247. static int test_ForceZero(void)
  30248. {
  30249. unsigned char data[32];
  30250. unsigned int i, j, len;
  30251. /* Test case with 0 length */
  30252. ForceZero(data, 0);
  30253. /* Test ForceZero */
  30254. for (i = 0; i < sizeof(data); i++) {
  30255. for (len = 1; len < sizeof(data) - i; len++) {
  30256. for (j = 0; j < sizeof(data); j++)
  30257. data[j] = j + 1;
  30258. ForceZero(data + i, len);
  30259. for (j = 0; j < sizeof(data); j++) {
  30260. if (j < i || j >= i + len) {
  30261. if (data[j] == 0x00)
  30262. return -10200;
  30263. }
  30264. else if (data[j] != 0x00)
  30265. return -10201;
  30266. }
  30267. }
  30268. }
  30269. return 0;
  30270. }
  30271. static void test_wolfSSL_X509_print()
  30272. {
  30273. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  30274. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(XSNPRINTF)
  30275. X509 *x509;
  30276. BIO *bio;
  30277. printf(testingFmt, "wolfSSL_X509_print");
  30278. x509 = X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  30279. AssertNotNull(x509);
  30280. /* print to memory */
  30281. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  30282. AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  30283. #if defined(WOLFSSL_QT)
  30284. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3113);
  30285. #else
  30286. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3103);
  30287. #endif
  30288. BIO_free(bio);
  30289. /* print to stdout */
  30290. AssertNotNull(bio = BIO_new(BIO_s_file()));
  30291. wolfSSL_BIO_set_fp(bio, stdout, BIO_NOCLOSE);
  30292. AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  30293. BIO_free(bio);
  30294. X509_free(x509);
  30295. printf(resultFmt, passed);
  30296. #endif
  30297. }
  30298. static void test_wolfSSL_RSA_print()
  30299. {
  30300. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  30301. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  30302. !defined(HAVE_FAST_RSA)
  30303. BIO *bio;
  30304. WOLFSSL_RSA* rsa = NULL;
  30305. printf(testingFmt, "wolfSSL_RSA_print");
  30306. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  30307. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file()));
  30308. wolfSSL_BIO_set_fp(bio, stdout, BIO_NOCLOSE);
  30309. AssertIntEQ(RSA_print(bio, rsa, 0), SSL_SUCCESS);
  30310. BIO_free(bio);
  30311. wolfSSL_RSA_free(rsa);
  30312. printf(resultFmt, passed);
  30313. #endif
  30314. }
  30315. static void test_wolfSSL_BIO_get_len()
  30316. {
  30317. #if defined(OPENSSL_EXTRA)
  30318. BIO *bio;
  30319. const char txt[] = "Some example text to push to the BIO.";
  30320. printf(testingFmt, "wolfSSL_BIO_get_len");
  30321. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30322. AssertIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  30323. AssertIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  30324. BIO_free(bio);
  30325. printf(resultFmt, passed);
  30326. #endif
  30327. }
  30328. static void test_wolfSSL_ASN1_STRING_print(void){
  30329. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS)
  30330. ASN1_STRING* asnStr = NULL;
  30331. const char HELLO_DATA[]= \
  30332. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  30333. const unsigned int MAX_UNPRINTABLE_CHAR = 32;
  30334. const unsigned int MAX_BUF = 255;
  30335. const int LF = 10, CR = 13;
  30336. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  30337. unsigned char expected[sizeof(unprintableData)+1];
  30338. unsigned char rbuf[MAX_BUF];
  30339. BIO *bio;
  30340. int p_len, i;
  30341. printf(testingFmt, "wolfSSL_ASN1_STRING_print()");
  30342. /* setup */
  30343. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  30344. unprintableData[i] = HELLO_DATA[i];
  30345. expected[i] = HELLO_DATA[i];
  30346. }
  30347. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  30348. unprintableData[sizeof(HELLO_DATA)+i] = i;
  30349. if (i == LF || i == CR)
  30350. expected[sizeof(HELLO_DATA)+i] = i;
  30351. else
  30352. expected[sizeof(HELLO_DATA)+i] = '.';
  30353. }
  30354. unprintableData[sizeof(unprintableData)-1] = '\0';
  30355. expected[sizeof(expected)-1] = '\0';
  30356. XMEMSET(rbuf, 0, MAX_BUF);
  30357. bio = BIO_new(BIO_s_mem());
  30358. BIO_set_write_buf_size(bio, MAX_BUF);
  30359. asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  30360. ASN1_STRING_set(asnStr,(const void*)unprintableData,
  30361. (int)sizeof(unprintableData));
  30362. /* test */
  30363. p_len = wolfSSL_ASN1_STRING_print(bio, asnStr);
  30364. AssertIntEQ(p_len, 46);
  30365. BIO_read(bio, (void*)rbuf, 46);
  30366. AssertStrEQ((char*)rbuf, (const char*)expected);
  30367. BIO_free(bio);
  30368. ASN1_STRING_free(asnStr);
  30369. printf(resultFmt, passed);
  30370. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS */
  30371. }
  30372. static void test_wolfSSL_RSA_verify()
  30373. {
  30374. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && \
  30375. !defined(NO_FILESYSTEM) && defined(HAVE_CRL)
  30376. XFILE fp;
  30377. RSA *pKey, *pubKey;
  30378. X509 *cert;
  30379. const char *text = "Hello wolfSSL !";
  30380. unsigned char hash[SHA256_DIGEST_LENGTH];
  30381. unsigned char signature[2048/8];
  30382. unsigned int signatureLength;
  30383. byte *buf;
  30384. BIO *bio;
  30385. SHA256_CTX c;
  30386. EVP_PKEY *evpPkey, *evpPubkey;
  30387. size_t sz;
  30388. printf(testingFmt, "wolfSSL_RSA_verify");
  30389. /* generate hash */
  30390. SHA256_Init(&c);
  30391. SHA256_Update(&c, text, strlen(text));
  30392. SHA256_Final(hash, &c);
  30393. /* read privete key file */
  30394. fp = XFOPEN(svrKeyFile, "r");
  30395. AssertTrue((fp != XBADFILE));
  30396. XFSEEK(fp, 0, XSEEK_END);
  30397. sz = XFTELL(fp);
  30398. XREWIND(fp);
  30399. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30400. AssertIntEQ(XFREAD(buf, 1, sz, fp), sz);
  30401. XFCLOSE(fp);
  30402. /* read private key and sign hash data */
  30403. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30404. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  30405. AssertNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  30406. AssertIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  30407. signature, &signatureLength, pKey), SSL_SUCCESS);
  30408. /* read public key and verify signed data */
  30409. fp = XFOPEN(svrCertFile,"r");
  30410. AssertTrue((fp != XBADFILE));
  30411. cert = PEM_read_X509(fp, 0, 0, 0 );
  30412. XFCLOSE(fp);
  30413. evpPubkey = X509_get_pubkey(cert);
  30414. pubKey = EVP_PKEY_get1_RSA(evpPubkey);
  30415. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  30416. signatureLength, pubKey), SSL_SUCCESS);
  30417. RSA_free(pKey);
  30418. EVP_PKEY_free(evpPkey);
  30419. RSA_free(pubKey);
  30420. EVP_PKEY_free(evpPubkey);
  30421. X509_free(cert);
  30422. BIO_free(bio);
  30423. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30424. printf(resultFmt, passed);
  30425. #endif
  30426. }
  30427. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30428. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  30429. static void test_openssl_make_self_signed_certificate(EVP_PKEY* pkey)
  30430. {
  30431. X509* x509 = NULL;
  30432. BIGNUM* serial_number = NULL;
  30433. X509_NAME* name = NULL;
  30434. time_t epoch_off = 0;
  30435. ASN1_INTEGER* asn1_serial_number;
  30436. long not_before, not_after;
  30437. AssertNotNull(x509 = X509_new());
  30438. AssertIntNE(X509_set_pubkey(x509, pkey), 0);
  30439. AssertNotNull(serial_number = BN_new());
  30440. AssertIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  30441. AssertNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  30442. AssertNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  30443. /* version 3 */
  30444. AssertIntNE(X509_set_version(x509, 2L), 0);
  30445. AssertNotNull(name = X509_NAME_new());
  30446. AssertIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  30447. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  30448. AssertIntNE(X509_set_subject_name(x509, name), 0);
  30449. AssertIntNE(X509_set_issuer_name(x509, name), 0);
  30450. not_before = (long)XTIME(NULL);
  30451. not_after = not_before + (365 * 24 * 60 * 60);
  30452. AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &epoch_off));
  30453. AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &epoch_off));
  30454. AssertIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  30455. BN_free(serial_number);
  30456. X509_NAME_free(name);
  30457. X509_free(x509);
  30458. }
  30459. #endif
  30460. static void test_openssl_generate_key_and_cert(void)
  30461. {
  30462. #if defined(OPENSSL_EXTRA)
  30463. #if !defined(NO_RSA)
  30464. {
  30465. EVP_PKEY* pkey = EVP_PKEY_new();
  30466. int key_length = 2048;
  30467. BIGNUM* exponent = BN_new();
  30468. RSA* rsa = RSA_new();
  30469. AssertNotNull(pkey);
  30470. AssertNotNull(exponent);
  30471. AssertNotNull(rsa);
  30472. AssertIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  30473. #ifndef WOLFSSL_KEY_GEN
  30474. AssertIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), WOLFSSL_FAILURE);
  30475. #if defined(USE_CERT_BUFFERS_1024)
  30476. AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  30477. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  30478. key_length = 1024;
  30479. #elif defined(USE_CERT_BUFFERS_2048)
  30480. AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  30481. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  30482. #else
  30483. RSA_free(rsa);
  30484. rsa = NULL;
  30485. #endif
  30486. #else
  30487. AssertIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  30488. #endif
  30489. if (rsa) {
  30490. AssertIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  30491. BN_free(exponent);
  30492. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  30493. test_openssl_make_self_signed_certificate(pkey);
  30494. #endif
  30495. }
  30496. EVP_PKEY_free(pkey);
  30497. }
  30498. #endif /* !NO_RSA */
  30499. #ifdef HAVE_ECC
  30500. {
  30501. EVP_PKEY* pkey = EVP_PKEY_new();
  30502. EC_KEY* ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  30503. AssertNotNull(pkey);
  30504. AssertNotNull(ec_key);
  30505. #ifndef NO_WOLFSSL_STUB
  30506. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  30507. #endif
  30508. AssertIntNE(EC_KEY_generate_key(ec_key), 0);
  30509. AssertIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  30510. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  30511. test_openssl_make_self_signed_certificate(pkey);
  30512. #endif
  30513. EVP_PKEY_free(pkey);
  30514. }
  30515. #endif /* HAVE_ECC */
  30516. #endif /* OPENSSL_EXTRA */
  30517. }
  30518. static void test_stubs_are_stubs()
  30519. {
  30520. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB)
  30521. WOLFSSL_CTX* ctx = NULL;
  30522. WOLFSSL_CTX* ctxN = NULL;
  30523. #ifndef NO_WOLFSSL_CLIENT
  30524. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  30525. AssertNotNull(ctx);
  30526. #elif !defined(NO_WOLFSSL_SERVER)
  30527. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  30528. AssertNotNull(ctx);
  30529. #else
  30530. return;
  30531. #endif
  30532. #define CHECKZERO_RET(x, y, z) AssertIntEQ((int) x(y), 0); \
  30533. AssertIntEQ((int) x(z), 0)
  30534. /* test logic, all stubs return same result regardless of ctx being NULL
  30535. * as there are no sanity checks, it's just a stub! If at some
  30536. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  30537. * if invalid inputs are supplied. Test calling both
  30538. * with and without valid inputs, if a stub functionality remains unchanged.
  30539. */
  30540. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  30541. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  30542. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  30543. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  30544. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  30545. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  30546. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  30547. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  30548. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  30549. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  30550. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  30551. wolfSSL_CTX_free(ctx);
  30552. ctx = NULL;
  30553. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB */
  30554. }
  30555. static void test_wolfSSL_CTX_LoadCRL()
  30556. {
  30557. #ifdef HAVE_CRL
  30558. WOLFSSL_CTX* ctx = NULL;
  30559. const char* badPath = "dummypath";
  30560. const char* validPath = "./certs/crl";
  30561. int derType = WOLFSSL_FILETYPE_ASN1;
  30562. int rawType = WOLFSSL_FILETYPE_RAW;
  30563. int pemType = WOLFSSL_FILETYPE_PEM;
  30564. int monitor = WOLFSSL_CRL_MONITOR;
  30565. #define FAIL_T1(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
  30566. BAD_FUNC_ARG)
  30567. #define SUCC_T(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
  30568. WOLFSSL_SUCCESS)
  30569. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  30570. #ifndef NO_WOLFSSL_CLIENT
  30571. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  30572. #elif !defined(NO_WOLFSSL_SERVER)
  30573. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  30574. #else
  30575. return;
  30576. #endif
  30577. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  30578. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  30579. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  30580. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, rawType, monitor);
  30581. wolfSSL_CTX_free(ctx);
  30582. ctx = NULL;
  30583. #endif
  30584. }
  30585. static void test_SetTmpEC_DHE_Sz(void)
  30586. {
  30587. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  30588. WOLFSSL_CTX *ctx;
  30589. WOLFSSL *ssl;
  30590. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  30591. AssertNotNull(ctx);
  30592. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  30593. ssl = wolfSSL_new(ctx);
  30594. AssertNotNull(ssl);
  30595. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  30596. wolfSSL_free(ssl);
  30597. wolfSSL_CTX_free(ctx);
  30598. #endif
  30599. }
  30600. static void test_wolfSSL_dtls_set_mtu(void)
  30601. {
  30602. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  30603. defined(WOLFSSL_DTLS)
  30604. WOLFSSL_CTX* ctx = NULL;
  30605. WOLFSSL* ssl = NULL;
  30606. const char* testCertFile;
  30607. const char* testKeyFile;
  30608. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  30609. #ifndef NO_RSA
  30610. testCertFile = svrCertFile;
  30611. testKeyFile = svrKeyFile;
  30612. #elif defined(HAVE_ECC)
  30613. testCertFile = eccCertFile;
  30614. testKeyFile = eccKeyFile;
  30615. #endif
  30616. if (testCertFile != NULL && testKeyFile != NULL) {
  30617. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  30618. WOLFSSL_FILETYPE_PEM));
  30619. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  30620. WOLFSSL_FILETYPE_PEM));
  30621. }
  30622. AssertNotNull(ssl = wolfSSL_new(ctx));
  30623. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  30624. AssertIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  30625. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  30626. AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  30627. AssertIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  30628. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  30629. AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  30630. wolfSSL_free(ssl);
  30631. wolfSSL_CTX_free(ctx);
  30632. printf(testingFmt, "wolfSSL_dtls_set_mtu()");
  30633. printf(resultFmt, passed);
  30634. #endif
  30635. }
  30636. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  30637. !defined(NO_CERTS)
  30638. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  30639. {
  30640. int ret;
  30641. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  30642. printf("loading cert %s failed\n", certA);
  30643. printf("Error: (%d): %s\n", ret, wolfSSL_ERR_reason_error_string(ret));
  30644. return -1;
  30645. }
  30646. return 0;
  30647. }
  30648. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  30649. {
  30650. int ret;
  30651. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  30652. != WOLFSSL_SUCCESS) {
  30653. printf("could not verify the cert: %s\n", certA);
  30654. printf("Error: (%d): %s\n", ret, wolfSSL_ERR_reason_error_string(ret));
  30655. return -1;
  30656. } else {
  30657. printf("successfully verified: %s\n", certA);
  30658. }
  30659. return 0;
  30660. }
  30661. #define LOAD_ONE_CA(a, b, c, d) \
  30662. do { \
  30663. a = load_ca_into_cm(c, d); \
  30664. if (a != 0) \
  30665. return b; \
  30666. else \
  30667. b--; \
  30668. } while(0)
  30669. #define VERIFY_ONE_CERT(a, b, c, d) \
  30670. do { \
  30671. a = verify_cert_with_cm(c, d); \
  30672. if (a != 0) \
  30673. return b; \
  30674. else \
  30675. b--; \
  30676. } while(0)
  30677. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  30678. {
  30679. int ret;
  30680. int i = -1;
  30681. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  30682. char chainGArr[9][50] = {"certs/ca-cert.pem",
  30683. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  30684. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  30685. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  30686. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  30687. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  30688. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  30689. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  30690. "certs/test-pathlen/chainG-entity.pem"};
  30691. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  30692. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  30693. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  30694. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  30695. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  30696. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  30697. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  30698. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  30699. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  30700. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  30701. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  30702. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  30703. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  30704. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  30705. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  30706. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  30707. /* test validating the entity twice, should have no effect on pathLen since
  30708. * entity/leaf cert */
  30709. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  30710. return ret;
  30711. }
  30712. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  30713. {
  30714. int ret;
  30715. int i = -1;
  30716. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  30717. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  30718. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  30719. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  30720. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  30721. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  30722. */
  30723. char chainHArr[6][50] = {"certs/ca-cert.pem",
  30724. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  30725. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  30726. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  30727. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  30728. "certs/test-pathlen/chainH-entity.pem"};
  30729. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  30730. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  30731. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  30732. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  30733. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  30734. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  30735. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  30736. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  30737. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  30738. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  30739. return ret;
  30740. }
  30741. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  30742. {
  30743. int ret;
  30744. int i = -1;
  30745. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  30746. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  30747. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  30748. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  30749. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  30750. */
  30751. char chainIArr[5][50] = {"certs/ca-cert.pem",
  30752. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  30753. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  30754. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  30755. "certs/test-pathlen/chainI-entity.pem"};
  30756. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  30757. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  30758. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  30759. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  30760. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  30761. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  30762. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  30763. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  30764. return ret;
  30765. }
  30766. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  30767. {
  30768. int ret;
  30769. int i = -1;
  30770. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  30771. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  30772. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  30773. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  30774. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  30775. */
  30776. char chainJArr[6][50] = {"certs/ca-cert.pem",
  30777. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  30778. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  30779. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  30780. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  30781. "certs/test-pathlen/chainJ-entity.pem"};
  30782. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  30783. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  30784. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  30785. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  30786. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  30787. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  30788. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  30789. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  30790. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  30791. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  30792. return ret;
  30793. }
  30794. static int test_various_pathlen_chains(void)
  30795. {
  30796. int ret;
  30797. WOLFSSL_CERT_MANAGER* cm;
  30798. /* Test chain G (large chain with varying pathLens) */
  30799. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  30800. printf("cert manager new failed\n");
  30801. return -1;
  30802. }
  30803. AssertIntEQ(test_chainG(cm), 0);
  30804. ret = wolfSSL_CertManagerUnloadCAs(cm);
  30805. if (ret != WOLFSSL_SUCCESS)
  30806. return -1;
  30807. wolfSSL_CertManagerFree(cm);
  30808. /* end test chain G */
  30809. /* Test chain H (5 chain with same pathLens) */
  30810. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  30811. printf("cert manager new failed\n");
  30812. return -1;
  30813. }
  30814. AssertIntLT(test_chainH(cm), 0);
  30815. wolfSSL_CertManagerUnloadCAs(cm);
  30816. wolfSSL_CertManagerFree(cm);
  30817. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  30818. printf("cert manager new failed\n");
  30819. return -1;
  30820. }
  30821. ret = wolfSSL_CertManagerUnloadCAs(cm);
  30822. if (ret != WOLFSSL_SUCCESS)
  30823. return -1;
  30824. wolfSSL_CertManagerFree(cm);
  30825. /* end test chain H */
  30826. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  30827. * followed by 2 ICA's, should pass) */
  30828. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  30829. printf("cert manager new failed\n");
  30830. return -1;
  30831. }
  30832. AssertIntEQ(test_chainI(cm), 0);
  30833. wolfSSL_CertManagerUnloadCAs(cm);
  30834. wolfSSL_CertManagerFree(cm);
  30835. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  30836. printf("cert manager new failed\n");
  30837. return -1;
  30838. }
  30839. ret = wolfSSL_CertManagerUnloadCAs(cm);
  30840. if (ret != WOLFSSL_SUCCESS)
  30841. return -1;
  30842. wolfSSL_CertManagerFree(cm);
  30843. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  30844. * this time followed by 3 ICA's, should fail */
  30845. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  30846. printf("cert manager new failed\n");
  30847. return -1;
  30848. }
  30849. AssertIntLT(test_chainJ(cm), 0);
  30850. wolfSSL_CertManagerUnloadCAs(cm);
  30851. wolfSSL_CertManagerFree(cm);
  30852. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  30853. printf("cert manager new failed\n");
  30854. return -1;
  30855. }
  30856. ret = wolfSSL_CertManagerUnloadCAs(cm);
  30857. wolfSSL_CertManagerFree(cm);
  30858. return ret;
  30859. }
  30860. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  30861. /*----------------------------------------------------------------------------*
  30862. | Main
  30863. *----------------------------------------------------------------------------*/
  30864. void ApiTest(void)
  30865. {
  30866. printf("\n-----------------Porting tests------------------\n");
  30867. AssertTrue(test_fileAccess());
  30868. printf(" Begin API Tests\n");
  30869. AssertIntEQ(test_wolfSSL_Init(), WOLFSSL_SUCCESS);
  30870. /* wolfcrypt initialization tests */
  30871. test_wolfSSL_Method_Allocators();
  30872. #ifndef NO_WOLFSSL_SERVER
  30873. test_wolfSSL_CTX_new(wolfSSLv23_server_method());
  30874. #endif
  30875. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  30876. (!defined(NO_RSA) || defined(HAVE_ECC))
  30877. test_for_double_Free();
  30878. #endif
  30879. test_wolfSSL_CTX_use_certificate_file();
  30880. AssertIntEQ(test_wolfSSL_CTX_use_certificate_buffer(), WOLFSSL_SUCCESS);
  30881. test_wolfSSL_CTX_use_PrivateKey_file();
  30882. test_wolfSSL_CTX_load_verify_locations();
  30883. test_wolfSSL_CertManagerLoadCABuffer();
  30884. test_wolfSSL_CertManagerGetCerts();
  30885. test_wolfSSL_CertManagerSetVerify();
  30886. test_wolfSSL_CertManagerNameConstraint();
  30887. test_wolfSSL_CertManagerCRL();
  30888. test_wolfSSL_CTX_load_verify_locations_ex();
  30889. test_wolfSSL_CTX_load_verify_buffer_ex();
  30890. test_wolfSSL_CTX_load_verify_chain_buffer_format();
  30891. test_wolfSSL_CTX_use_certificate_chain_file_format();
  30892. test_wolfSSL_CTX_trust_peer_cert();
  30893. test_wolfSSL_CTX_SetTmpDH_file();
  30894. test_wolfSSL_CTX_SetTmpDH_buffer();
  30895. test_wolfSSL_CTX_SetMinMaxDhKey_Sz();
  30896. test_wolfSSL_CTX_der_load_verify_locations();
  30897. test_wolfSSL_CTX_enable_disable();
  30898. test_server_wolfSSL_new();
  30899. test_client_wolfSSL_new();
  30900. test_wolfSSL_SetTmpDH_file();
  30901. test_wolfSSL_SetTmpDH_buffer();
  30902. test_wolfSSL_SetMinMaxDhKey_Sz();
  30903. test_SetTmpEC_DHE_Sz();
  30904. test_wolfSSL_dtls_set_mtu();
  30905. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  30906. defined(HAVE_IO_TESTS_DEPENDENCIES)
  30907. test_wolfSSL_read_write();
  30908. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  30909. test_wolfSSL_reuse_WOLFSSLobj();
  30910. #endif
  30911. test_wolfSSL_dtls_export();
  30912. #endif
  30913. AssertIntEQ(test_wolfSSL_SetMinVersion(), WOLFSSL_SUCCESS);
  30914. AssertIntEQ(test_wolfSSL_CTX_SetMinVersion(), WOLFSSL_SUCCESS);
  30915. /* TLS extensions tests */
  30916. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  30917. test_wolfSSL_UseSNI();
  30918. #endif
  30919. test_wolfSSL_UseTrustedCA();
  30920. test_wolfSSL_UseMaxFragment();
  30921. test_wolfSSL_UseTruncatedHMAC();
  30922. test_wolfSSL_UseSupportedCurve();
  30923. test_wolfSSL_UseALPN();
  30924. test_wolfSSL_DisableExtendedMasterSecret();
  30925. test_wolfSSL_wolfSSL_UseSecureRenegotiation();
  30926. /* X509 tests */
  30927. test_wolfSSL_X509_NAME_get_entry();
  30928. test_wolfSSL_PKCS12();
  30929. test_wolfSSL_no_password_cb();
  30930. test_wolfSSL_PKCS8();
  30931. test_wolfSSL_PKCS8_ED25519();
  30932. test_wolfSSL_PKCS8_ED448();
  30933. test_wolfSSL_PKCS5();
  30934. test_wolfSSL_URI();
  30935. test_wolfSSL_TBS();
  30936. test_wolfSSL_X509_verify();
  30937. test_wc_PemToDer();
  30938. test_wc_AllocDer();
  30939. test_wc_CertPemToDer();
  30940. test_wc_PubKeyPemToDer();
  30941. test_wc_PemPubKeyToDer();
  30942. /*OCSP Stapling. */
  30943. AssertIntEQ(test_wolfSSL_UseOCSPStapling(), WOLFSSL_SUCCESS);
  30944. AssertIntEQ(test_wolfSSL_UseOCSPStaplingV2(), WOLFSSL_SUCCESS);
  30945. /* Multicast */
  30946. test_wolfSSL_mcast();
  30947. /* compatibility tests */
  30948. test_wolfSSL_X509_NAME();
  30949. test_wolfSSL_X509_INFO();
  30950. test_wolfSSL_X509_subject_name_hash();
  30951. test_wolfSSL_X509_issuer_name_hash();
  30952. test_wolfSSL_DES();
  30953. test_wolfSSL_certs();
  30954. test_wolfSSL_ASN1_TIME_print();
  30955. test_wolfSSL_ASN1_UTCTIME_print();
  30956. test_wolfSSL_ASN1_GENERALIZEDTIME_free();
  30957. test_wolfSSL_private_keys();
  30958. test_wolfSSL_PEM_PrivateKey();
  30959. test_wolfSSL_PEM_bio_RSAKey();
  30960. test_wolfSSL_PEM_bio_DSAKey();
  30961. test_wolfSSL_PEM_bio_ECKey();
  30962. test_wolfSSL_PEM_RSAPrivateKey();
  30963. test_wolfSSL_PEM_PUBKEY();
  30964. test_DSA_do_sign_verify();
  30965. test_wolfSSL_tmp_dh();
  30966. test_wolfSSL_ctrl();
  30967. test_wolfSSL_EVP_MD_size();
  30968. test_wolfSSL_EVP_Digest();
  30969. test_wolfSSL_EVP_PKEY_new_mac_key();
  30970. test_wolfSSL_EVP_MD_hmac_signing();
  30971. test_wolfSSL_EVP_MD_rsa_signing();
  30972. test_wolfSSL_EVP_MD_ecc_signing();
  30973. test_wolfSSL_CTX_add_extra_chain_cert();
  30974. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  30975. test_wolfSSL_ERR_peek_last_error_line();
  30976. #endif
  30977. test_wolfSSL_ERR_print_errors_cb();
  30978. AssertFalse(test_wolfSSL_GetLoggingCb());
  30979. AssertFalse(test_WOLFSSL_ERROR_MSG());
  30980. AssertFalse(test_wc_ERR_remove_state());
  30981. AssertFalse(test_wc_ERR_print_errors_fp());
  30982. test_wolfSSL_set_options();
  30983. test_wolfSSL_sk_SSL_CIPHER();
  30984. test_wolfSSL_X509_STORE_CTX();
  30985. test_wolfSSL_X509_STORE_CTX_get0_current_issuer();
  30986. test_wolfSSL_msgCb();
  30987. test_wolfSSL_either_side();
  30988. test_wolfSSL_DTLS_either_side();
  30989. test_generate_cookie();
  30990. test_wolfSSL_X509_STORE_set_flags();
  30991. test_wolfSSL_X509_LOOKUP_load_file();
  30992. test_wolfSSL_X509_NID();
  30993. test_wolfSSL_X509_STORE_CTX_set_time();
  30994. test_wolfSSL_get0_param();
  30995. test_wolfSSL_X509_VERIFY_PARAM_set1_host();
  30996. test_wolfSSL_X509_STORE_CTX_get0_store();
  30997. test_wolfSSL_X509_STORE();
  30998. test_wolfSSL_X509_STORE_load_locations();
  30999. test_wolfSSL_BN();
  31000. test_wolfSSL_PEM_read_bio();
  31001. test_wolfSSL_BIO();
  31002. test_wolfSSL_ASN1_STRING();
  31003. test_wolfSSL_ASN1_BIT_STRING();
  31004. test_wolfSSL_X509();
  31005. test_wolfSSL_X509_VERIFY_PARAM();
  31006. test_wolfSSL_X509_sign();
  31007. test_wolfSSL_X509_get0_tbs_sigalg();
  31008. test_wolfSSL_X509_ALGOR_get0();
  31009. test_wolfSSL_X509_get_X509_PUBKEY();
  31010. test_wolfSSL_X509_PUBKEY();
  31011. test_wolfSSL_RAND();
  31012. test_wolfSSL_BUF();
  31013. test_wolfSSL_set_tlsext_status_type();
  31014. test_wolfSSL_ASN1_TIME_adj();
  31015. test_wolfSSL_X509_cmp_time();
  31016. test_wolfSSL_X509_time_adj();
  31017. test_wolfSSL_CTX_set_client_CA_list();
  31018. test_wolfSSL_CTX_add_client_CA();
  31019. test_wolfSSL_CTX_set_srp_username();
  31020. test_wolfSSL_CTX_set_srp_password();
  31021. test_wolfSSL_pseudo_rand();
  31022. test_wolfSSL_PKCS8_Compat();
  31023. test_wolfSSL_PKCS8_d2i();
  31024. test_wolfSSL_ERR_put_error();
  31025. test_wolfSSL_ERR_print_errors();
  31026. test_wolfSSL_HMAC();
  31027. test_wolfSSL_OBJ();
  31028. test_wolfSSL_i2a_ASN1_OBJECT();
  31029. test_wolfSSL_OBJ_cmp();
  31030. test_wolfSSL_OBJ_txt2nid();
  31031. test_wolfSSL_OBJ_txt2obj();
  31032. test_wolfSSL_X509_NAME_ENTRY();
  31033. test_wolfSSL_X509_set_name();
  31034. test_wolfSSL_X509_set_notAfter();
  31035. test_wolfSSL_X509_set_notBefore();
  31036. test_wolfSSL_X509_set_version();
  31037. test_wolfSSL_BIO_gets();
  31038. test_wolfSSL_BIO_puts();
  31039. test_wolfSSL_BIO_should_retry();
  31040. test_wolfSSL_d2i_PUBKEY();
  31041. test_wolfSSL_BIO_write();
  31042. test_wolfSSL_BIO_printf();
  31043. test_wolfSSL_BIO_f_md();
  31044. test_wolfSSL_SESSION();
  31045. test_wolfSSL_DES_ecb_encrypt();
  31046. test_wolfSSL_sk_GENERAL_NAME();
  31047. test_wolfSSL_MD4();
  31048. test_wolfSSL_RSA();
  31049. test_wolfSSL_RSA_DER();
  31050. test_wolfSSL_RSA_get0_key();
  31051. test_wolfSSL_RSA_meth();
  31052. test_wolfSSL_verify_depth();
  31053. test_wolfSSL_HMAC_CTX();
  31054. test_wolfSSL_msg_callback();
  31055. test_wolfSSL_SHA();
  31056. test_wolfSSL_DH_1536_prime();
  31057. test_wolfSSL_PEM_write_DHparams();
  31058. test_wolfSSL_AES_ecb_encrypt();
  31059. test_wolfSSL_SHA256();
  31060. test_wolfSSL_X509_get_serialNumber();
  31061. test_wolfSSL_X509_CRL();
  31062. test_wolfSSL_PEM_read_X509();
  31063. test_wolfSSL_PEM_read();
  31064. test_wolfSSL_PEM_X509_INFO_read_bio();
  31065. test_wolfSSL_PEM_read_bio_ECPKParameters();
  31066. test_wolfSSL_X509_NAME_ENTRY_get_object();
  31067. test_wolfSSL_OpenSSL_add_all_algorithms();
  31068. test_wolfSSL_ASN1_STRING_print_ex();
  31069. test_wolfSSL_ASN1_TIME_to_generalizedtime();
  31070. test_wolfSSL_ASN1_INTEGER_set();
  31071. test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS();
  31072. test_wolfSSL_i2c_ASN1_INTEGER();
  31073. test_wolfSSL_X509_check_ca();
  31074. test_wolfSSL_X509_check_ip_asc();
  31075. test_wolfSSL_DC_cert();
  31076. test_wolfSSL_DES_ncbc();
  31077. test_wolfSSL_AES_cbc_encrypt();
  31078. test_wolfssl_EVP_aes_gcm();
  31079. test_wolfSSL_PKEY_up_ref();
  31080. test_wolfSSL_i2d_PrivateKey();
  31081. test_wolfSSL_OCSP_get0_info();
  31082. test_wolfSSL_EVP_PKEY_derive();
  31083. test_wolfSSL_RSA_padding_add_PKCS1_PSS();
  31084. #if defined(OPENSSL_ALL)
  31085. test_wolfSSL_X509_PUBKEY_get();
  31086. test_wolfSSL_sk_CIPHER_description();
  31087. test_wolfSSL_get_ciphers_compat();
  31088. test_wolfSSL_d2i_DHparams();
  31089. test_wolfSSL_i2d_DHparams();
  31090. test_wolfSSL_ASN1_STRING_to_UTF8();
  31091. test_wolfSSL_EC_KEY_dup();
  31092. test_wolfSSL_EVP_PKEY_set1_get1_DSA();
  31093. test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY();
  31094. test_wolfSSL_EVP_PKEY_set1_get1_DH();
  31095. test_wolfSSL_CTX_ctrl();
  31096. test_wolfSSL_DH_check();
  31097. test_wolfSSL_EVP_PKEY_assign();
  31098. test_wolfSSL_OBJ_ln();
  31099. test_wolfSSL_OBJ_sn();
  31100. #endif /* OPENSSL_ALL */
  31101. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  31102. AssertIntEQ(test_wolfSSL_CTX_use_certificate_ASN1(), WOLFSSL_SUCCESS);
  31103. test_wolfSSL_d2i_PrivateKeys_bio();
  31104. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  31105. test_wolfSSL_X509_CA_num();
  31106. test_wolfSSL_X509_get_version();
  31107. test_wolfSSL_X509_print();
  31108. test_wolfSSL_BIO_get_len();
  31109. test_wolfSSL_RSA_verify();
  31110. test_wolfSSL_X509V3_EXT_get();
  31111. test_wolfSSL_X509V3_EXT();
  31112. test_wolfSSL_X509_get_ext();
  31113. test_wolfSSL_X509_get_ext_by_NID();
  31114. test_wolfSSL_X509_get_ext_count();
  31115. test_wolfSSL_X509_EXTENSION_new();
  31116. test_wolfSSL_X509_EXTENSION_get_object();
  31117. test_wolfSSL_X509_EXTENSION_get_data();
  31118. test_wolfSSL_X509_EXTENSION_get_critical();
  31119. test_wolfSSL_X509V3_EXT_print();
  31120. test_wolfSSL_X509_cmp();
  31121. test_wolfSSL_RSA_print();
  31122. test_wolfSSL_ASN1_STRING_print();
  31123. test_openssl_generate_key_and_cert();
  31124. test_wolfSSL_EC_get_builtin_curves();
  31125. test_wolfSSL_CRYPTO_memcmp();
  31126. /* test the no op functions for compatibility */
  31127. test_no_op_functions();
  31128. /* OpenSSL EVP_PKEY API tests */
  31129. test_EVP_PKEY_rsa();
  31130. test_wolfSSL_EVP_PKEY_encrypt();
  31131. test_wolfSSL_EVP_PKEY_sign();
  31132. test_EVP_PKEY_ec();
  31133. test_EVP_PKEY_cmp();
  31134. /* OpenSSL error API tests */
  31135. test_ERR_load_crypto_strings();
  31136. /* OpenSSL sk_X509 API test */
  31137. test_sk_X509();
  31138. /* OpenSSL X509 API test */
  31139. test_X509_get_signature_nid();
  31140. /* OpenSSL X509 REQ API test */
  31141. test_X509_REQ();
  31142. /* OpenSSL PKCS7 API test */
  31143. test_wolfssl_PKCS7();
  31144. test_wolfSSL_PKCS7_SIGNED_new();
  31145. test_wolfSSL_PEM_write_bio_PKCS7();
  31146. /* wolfCrypt ASN tests */
  31147. test_wc_GetPkcs8TraditionalOffset();
  31148. test_wc_SetSubjectRaw();
  31149. test_wc_GetSubjectRaw();
  31150. test_wc_SetIssuerRaw();
  31151. test_wc_SetIssueBuffer();
  31152. test_wc_SetSubjectKeyId();
  31153. test_wc_SetSubject();
  31154. test_CheckCertSignature();
  31155. /* wolfCrypt ECC tests */
  31156. test_wc_ecc_get_curve_size_from_name();
  31157. test_wc_ecc_get_curve_id_from_name();
  31158. test_wc_ecc_get_curve_id_from_params();
  31159. #ifdef WOLFSSL_TLS13
  31160. /* TLS v1.3 API tests */
  31161. test_tls13_apis();
  31162. #endif
  31163. #ifndef NO_CERTS
  31164. /* Bad certificate signature tests */
  31165. AssertIntEQ(test_EccSigFailure_cm(), ASN_SIG_CONFIRM_E);
  31166. AssertIntEQ(test_RsaSigFailure_cm(), ASN_SIG_CONFIRM_E);
  31167. #endif /* NO_CERTS */
  31168. #ifdef HAVE_PK_CALLBACKS
  31169. /* public key callback tests */
  31170. test_DhCallbacks();
  31171. #endif
  31172. /*wolfcrypt */
  31173. printf("\n-----------------wolfcrypt unit tests------------------\n");
  31174. AssertFalse(test_wolfCrypt_Init());
  31175. AssertFalse(test_wc_InitMd5());
  31176. AssertFalse(test_wc_Md5Update());
  31177. AssertFalse(test_wc_Md5Final());
  31178. AssertFalse(test_wc_InitSha());
  31179. AssertFalse(test_wc_ShaUpdate());
  31180. AssertFalse(test_wc_ShaFinal());
  31181. AssertFalse(test_wc_InitSha256());
  31182. AssertFalse(test_wc_Sha256Update());
  31183. AssertFalse(test_wc_Sha256Final());
  31184. AssertFalse(test_wc_Sha256FinalRaw());
  31185. AssertFalse(test_wc_Sha256GetFlags());
  31186. AssertFalse(test_wc_Sha256Free());
  31187. AssertFalse(test_wc_Sha256GetHash());
  31188. AssertFalse(test_wc_Sha256Copy());
  31189. AssertFalse(test_wc_InitSha512());
  31190. AssertFalse(test_wc_Sha512Update());
  31191. AssertFalse(test_wc_Sha512Final());
  31192. AssertFalse(test_wc_Sha512GetFlags());
  31193. AssertFalse(test_wc_Sha512FinalRaw());
  31194. AssertFalse(test_wc_Sha512Free());
  31195. AssertFalse(test_wc_Sha512GetHash());
  31196. AssertFalse(test_wc_Sha512Copy());
  31197. AssertFalse(test_wc_InitSha384());
  31198. AssertFalse(test_wc_Sha384Update());
  31199. AssertFalse(test_wc_Sha384Final());
  31200. AssertFalse(test_wc_Sha384GetFlags());
  31201. AssertFalse(test_wc_Sha384FinalRaw());
  31202. AssertFalse(test_wc_Sha384Free());
  31203. AssertFalse(test_wc_Sha384GetHash());
  31204. AssertFalse(test_wc_Sha384Copy());
  31205. AssertFalse(test_wc_InitSha224());
  31206. AssertFalse(test_wc_Sha224Update());
  31207. AssertFalse(test_wc_Sha224Final());
  31208. AssertFalse(test_wc_Sha224SetFlags());
  31209. AssertFalse(test_wc_Sha224GetFlags());
  31210. AssertFalse(test_wc_Sha224Free());
  31211. AssertFalse(test_wc_Sha224GetHash());
  31212. AssertFalse(test_wc_Sha224Copy());
  31213. AssertFalse(test_wc_InitBlake2b());
  31214. AssertFalse(test_wc_InitRipeMd());
  31215. AssertFalse(test_wc_RipeMdUpdate());
  31216. AssertFalse(test_wc_RipeMdFinal());
  31217. AssertIntEQ(test_wc_InitSha3(), 0);
  31218. AssertIntEQ(testing_wc_Sha3_Update(), 0);
  31219. AssertIntEQ(test_wc_Sha3_224_Final(), 0);
  31220. AssertIntEQ(test_wc_Sha3_256_Final(), 0);
  31221. AssertIntEQ(test_wc_Sha3_384_Final(), 0);
  31222. AssertIntEQ(test_wc_Sha3_512_Final(), 0);
  31223. AssertIntEQ(test_wc_Sha3_224_Copy(), 0);
  31224. AssertIntEQ(test_wc_Sha3_256_Copy(), 0);
  31225. AssertIntEQ(test_wc_Sha3_384_Copy(), 0);
  31226. AssertIntEQ(test_wc_Sha3_512_Copy(), 0);
  31227. AssertIntEQ(test_wc_Sha3_GetFlags(), 0);
  31228. AssertIntEQ(test_wc_InitShake256(), 0);
  31229. AssertIntEQ(testing_wc_Shake256_Update(), 0);
  31230. AssertIntEQ(test_wc_Shake256_Final(), 0);
  31231. AssertIntEQ(test_wc_Shake256_Copy(), 0);
  31232. AssertIntEQ(test_wc_Shake256Hash(), 0);
  31233. AssertFalse(test_wc_Md5HmacSetKey());
  31234. AssertFalse(test_wc_Md5HmacUpdate());
  31235. AssertFalse(test_wc_Md5HmacFinal());
  31236. AssertFalse(test_wc_ShaHmacSetKey());
  31237. AssertFalse(test_wc_ShaHmacUpdate());
  31238. AssertFalse(test_wc_ShaHmacFinal());
  31239. AssertFalse(test_wc_Sha224HmacSetKey());
  31240. AssertFalse(test_wc_Sha224HmacUpdate());
  31241. AssertFalse(test_wc_Sha224HmacFinal());
  31242. AssertFalse(test_wc_Sha256HmacSetKey());
  31243. AssertFalse(test_wc_Sha256HmacUpdate());
  31244. AssertFalse(test_wc_Sha256HmacFinal());
  31245. AssertFalse(test_wc_Sha384HmacSetKey());
  31246. AssertFalse(test_wc_Sha384HmacUpdate());
  31247. AssertFalse(test_wc_Sha384HmacFinal());
  31248. AssertIntEQ(test_wc_HashInit(), 0);
  31249. AssertIntEQ(test_wc_HashSetFlags(), 0);
  31250. AssertIntEQ(test_wc_HashGetFlags(), 0);
  31251. AssertIntEQ(test_wc_InitCmac(), 0);
  31252. AssertIntEQ(test_wc_CmacUpdate(), 0);
  31253. AssertIntEQ(test_wc_CmacFinal(), 0);
  31254. AssertIntEQ(test_wc_AesCmacGenerate(), 0);
  31255. AssertIntEQ(test_wc_Des3_SetIV(), 0);
  31256. AssertIntEQ(test_wc_Des3_SetKey(), 0);
  31257. AssertIntEQ(test_wc_Des3_CbcEncryptDecrypt(), 0);
  31258. AssertIntEQ(test_wc_Des3_CbcEncryptDecryptWithKey(), 0);
  31259. AssertIntEQ(test_wc_IdeaSetKey(), 0);
  31260. AssertIntEQ(test_wc_IdeaSetIV(), 0);
  31261. AssertIntEQ(test_wc_IdeaCipher(), 0);
  31262. AssertIntEQ(test_wc_IdeaCbcEncyptDecrypt(), 0);
  31263. AssertIntEQ(test_wc_Chacha_SetKey(), 0);
  31264. AssertIntEQ(test_wc_Chacha_Process(), 0);
  31265. AssertIntEQ(test_wc_ChaCha20Poly1305_aead(), 0);
  31266. AssertIntEQ(test_wc_Poly1305SetKey(), 0);
  31267. AssertIntEQ(test_wc_CamelliaSetKey(), 0);
  31268. AssertIntEQ(test_wc_CamelliaSetIV(), 0);
  31269. AssertIntEQ(test_wc_CamelliaEncryptDecryptDirect(), 0);
  31270. AssertIntEQ(test_wc_CamelliaCbcEncryptDecrypt(), 0);
  31271. AssertIntEQ(test_wc_RabbitSetKey(), 0);
  31272. AssertIntEQ(test_wc_RabbitProcess(), 0);
  31273. AssertIntEQ(test_wc_Arc4SetKey(), 0);
  31274. AssertIntEQ(test_wc_Arc4Process(), 0);
  31275. AssertIntEQ(test_wc_AesSetKey(), 0);
  31276. AssertIntEQ(test_wc_AesSetIV(), 0);
  31277. AssertIntEQ(test_wc_AesCbcEncryptDecrypt(), 0);
  31278. AssertIntEQ(test_wc_AesCtrEncryptDecrypt(), 0);
  31279. AssertIntEQ(test_wc_AesGcmSetKey(), 0);
  31280. AssertIntEQ(test_wc_AesGcmEncryptDecrypt(), 0);
  31281. AssertIntEQ(test_wc_GmacSetKey(), 0);
  31282. AssertIntEQ(test_wc_GmacUpdate(), 0);
  31283. AssertIntEQ(test_wc_InitRsaKey(), 0);
  31284. AssertIntEQ(test_wc_RsaPrivateKeyDecode(), 0);
  31285. AssertIntEQ(test_wc_RsaPublicKeyDecode(), 0);
  31286. AssertIntEQ(test_wc_RsaPublicKeyDecodeRaw(), 0);
  31287. AssertIntEQ(test_wc_MakeRsaKey(), 0);
  31288. AssertIntEQ(test_wc_SetKeyUsage (), 0);
  31289. AssertIntEQ(test_wc_SetMutexCb(), 0);
  31290. AssertIntEQ(test_wc_LockMutex_ex(), 0);
  31291. AssertIntEQ(test_wc_RsaKeyToDer(), 0);
  31292. AssertIntEQ(test_wc_RsaKeyToPublicDer(), 0);
  31293. AssertIntEQ(test_wc_RsaPublicEncryptDecrypt(), 0);
  31294. AssertIntEQ(test_wc_RsaPublicEncryptDecrypt_ex(), 0);
  31295. AssertIntEQ(test_wc_RsaEncryptSize(), 0);
  31296. AssertIntEQ(test_wc_RsaSSL_SignVerify(), 0);
  31297. AssertIntEQ(test_wc_RsaFlattenPublicKey(), 0);
  31298. AssertIntEQ(test_RsaDecryptBoundsCheck(), 0);
  31299. AssertIntEQ(test_wc_AesCcmSetKey(), 0);
  31300. AssertIntEQ(test_wc_AesCcmEncryptDecrypt(), 0);
  31301. AssertIntEQ(test_wc_Hc128_SetKey(), 0);
  31302. AssertIntEQ(test_wc_Hc128_Process(), 0);
  31303. AssertIntEQ(test_wc_InitDsaKey(), 0);
  31304. AssertIntEQ(test_wc_DsaSignVerify(), 0);
  31305. AssertIntEQ(test_wc_DsaPublicPrivateKeyDecode(), 0);
  31306. AssertIntEQ(test_wc_MakeDsaKey(), 0);
  31307. AssertIntEQ(test_wc_DsaKeyToDer(), 0);
  31308. AssertIntEQ(test_wc_DsaKeyToPublicDer(), 0);
  31309. AssertIntEQ(test_wc_DsaImportParamsRaw(), 0);
  31310. AssertIntEQ(test_wc_DsaImportParamsRawCheck(), 0);
  31311. AssertIntEQ(test_wc_DsaExportParamsRaw(), 0);
  31312. AssertIntEQ(test_wc_DsaExportKeyRaw(), 0);
  31313. AssertIntEQ(test_wc_SignatureGetSize_ecc(), 0);
  31314. AssertIntEQ(test_wc_SignatureGetSize_rsa(), 0);
  31315. wolfCrypt_Cleanup();
  31316. #ifdef OPENSSL_EXTRA
  31317. /*wolfSSL_EVP_get_cipherbynid test*/
  31318. test_wolfSSL_EVP_get_cipherbynid();
  31319. test_wolfSSL_EVP_CIPHER_CTX();
  31320. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  31321. test_wolfSSL_EC();
  31322. #endif
  31323. test_wolfSSL_ECDSA_SIG();
  31324. test_ECDSA_size_sign();
  31325. test_ED25519();
  31326. test_ED448();
  31327. test_EC_i2d();
  31328. #endif
  31329. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  31330. !defined(HAVE_SELFTEST) && \
  31331. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  31332. test_wc_ecc_get_curve_id_from_dp_params();
  31333. #endif
  31334. #ifdef HAVE_HASHDRBG
  31335. #ifdef TEST_RESEED_INTERVAL
  31336. AssertIntEQ(test_wc_RNG_GenerateBlock_Reseed(), 0);
  31337. #endif
  31338. AssertIntEQ(test_wc_RNG_GenerateBlock(), 0);
  31339. #endif
  31340. AssertIntEQ(test_get_rand_digit(), 0);
  31341. AssertIntEQ(test_get_digit_count(), 0);
  31342. AssertIntEQ(test_mp_cond_copy(), 0);
  31343. AssertIntEQ(test_mp_rand(), 0);
  31344. AssertIntEQ(test_get_digit(), 0);
  31345. AssertIntEQ(test_wc_export_int(), 0);
  31346. AssertIntEQ(test_wc_InitRngNonce(), 0);
  31347. AssertIntEQ(test_wc_InitRngNonce_ex(), 0);
  31348. AssertIntEQ(test_wc_ed25519_make_key(), 0);
  31349. AssertIntEQ(test_wc_ed25519_init(), 0);
  31350. AssertIntEQ(test_wc_ed25519_sign_msg(), 0);
  31351. AssertIntEQ(test_wc_ed25519_import_public(), 0);
  31352. AssertIntEQ(test_wc_ed25519_import_private_key(), 0);
  31353. AssertIntEQ(test_wc_ed25519_export(), 0);
  31354. AssertIntEQ(test_wc_ed25519_size(), 0);
  31355. AssertIntEQ(test_wc_ed25519_exportKey(), 0);
  31356. AssertIntEQ(test_wc_Ed25519PublicKeyToDer(), 0);
  31357. AssertIntEQ(test_wc_curve25519_init(), 0);
  31358. AssertIntEQ(test_wc_curve25519_size(), 0);
  31359. AssertIntEQ(test_wc_curve25519_export_key_raw(), 0);
  31360. AssertIntEQ(test_wc_curve25519_export_key_raw_ex(), 0);
  31361. AssertIntEQ(test_wc_curve25519_size (), 0);
  31362. AssertIntEQ(test_wc_curve25519_make_key (), 0);
  31363. AssertIntEQ(test_wc_curve25519_shared_secret_ex (), 0);
  31364. AssertIntEQ(test_wc_curve25519_make_pub (), 0);
  31365. AssertIntEQ(test_wc_curve25519_export_public_ex (), 0);
  31366. AssertIntEQ(test_wc_curve25519_export_private_raw_ex (), 0);
  31367. AssertIntEQ(test_wc_curve25519_import_private_raw_ex (), 0);
  31368. AssertIntEQ(test_wc_curve25519_import_private (), 0);
  31369. AssertIntEQ(test_wc_ed448_make_key(), 0);
  31370. AssertIntEQ(test_wc_ed448_init(), 0);
  31371. AssertIntEQ(test_wc_ed448_sign_msg(), 0);
  31372. AssertIntEQ(test_wc_ed448_import_public(), 0);
  31373. AssertIntEQ(test_wc_ed448_import_private_key(), 0);
  31374. AssertIntEQ(test_wc_ed448_export(), 0);
  31375. AssertIntEQ(test_wc_ed448_size(), 0);
  31376. AssertIntEQ(test_wc_ed448_exportKey(), 0);
  31377. AssertIntEQ(test_wc_Ed448PublicKeyToDer(), 0);
  31378. AssertIntEQ(test_wc_curve448_make_key (), 0);
  31379. AssertIntEQ(test_wc_curve448_shared_secret_ex (), 0);
  31380. AssertIntEQ(test_wc_curve448_export_public_ex (), 0);
  31381. AssertIntEQ(test_wc_curve448_export_private_raw_ex (), 0);
  31382. AssertIntEQ(test_wc_curve448_export_key_raw (), 0);
  31383. AssertIntEQ(test_wc_curve448_import_private_raw_ex (), 0);
  31384. AssertIntEQ(test_wc_curve448_import_private (), 0);
  31385. AssertIntEQ(test_wc_curve448_init(), 0);
  31386. AssertIntEQ(test_wc_curve448_size (), 0);
  31387. AssertIntEQ(test_wc_ecc_make_key(), 0);
  31388. AssertIntEQ(test_wc_ecc_init(), 0);
  31389. AssertIntEQ(test_wc_ecc_check_key(), 0);
  31390. AssertIntEQ(test_wc_ecc_get_generator(), 0);
  31391. AssertIntEQ(test_wc_ecc_size(), 0);
  31392. test_wc_ecc_params();
  31393. AssertIntEQ(test_wc_ecc_signVerify_hash(), 0);
  31394. AssertIntEQ(test_wc_ecc_shared_secret(), 0);
  31395. AssertIntEQ(test_wc_ecc_export_x963(), 0);
  31396. AssertIntEQ(test_wc_ecc_export_x963_ex(), 0);
  31397. AssertIntEQ(test_wc_ecc_import_x963(), 0);
  31398. AssertIntEQ(ecc_import_private_key(), 0);
  31399. AssertIntEQ(test_wc_ecc_export_private_only(), 0);
  31400. AssertIntEQ(test_wc_ecc_rs_to_sig(), 0);
  31401. AssertIntEQ(test_wc_ecc_import_raw(), 0);
  31402. AssertIntEQ(test_wc_ecc_import_unsigned(), 0);
  31403. AssertIntEQ(test_wc_ecc_sig_size(), 0);
  31404. AssertIntEQ(test_wc_ecc_ctx_new(), 0);
  31405. AssertIntEQ(test_wc_ecc_ctx_reset(), 0);
  31406. AssertIntEQ(test_wc_ecc_ctx_set_peer_salt(), 0);
  31407. AssertIntEQ(test_wc_ecc_ctx_set_info(), 0);
  31408. AssertIntEQ(test_wc_ecc_encryptDecrypt(), 0);
  31409. AssertIntEQ(test_wc_ecc_del_point(), 0);
  31410. AssertIntEQ(test_wc_ecc_pointFns(), 0);
  31411. AssertIntEQ(test_wc_ecc_shared_secret_ssh(), 0);
  31412. AssertIntEQ(test_wc_ecc_verify_hash_ex(), 0);
  31413. AssertIntEQ(test_wc_ecc_mulmod(), 0);
  31414. AssertIntEQ(test_wc_ecc_is_valid_idx(), 0);
  31415. AssertIntEQ(test_ToTraditional(), 0);
  31416. AssertIntEQ(test_wc_EccPrivateKeyToDer(), 0);
  31417. AssertIntEQ(test_wc_Ed25519KeyToDer(), 0);
  31418. AssertIntEQ(test_wc_Ed25519PrivateKeyToDer(), 0);
  31419. AssertIntEQ(test_wc_Ed448KeyToDer(), 0);
  31420. AssertIntEQ(test_wc_Ed448PrivateKeyToDer(), 0);
  31421. AssertIntEQ(test_wc_SetAuthKeyIdFromPublicKey_ex(), 0);
  31422. AssertIntEQ(test_wc_SetSubjectBuffer(), 0);
  31423. AssertIntEQ(test_wc_SetSubjectKeyIdFromPublicKey_ex(), 0);
  31424. test_wc_PKCS7_New();
  31425. test_wc_PKCS7_Init();
  31426. test_wc_PKCS7_InitWithCert();
  31427. test_wc_PKCS7_EncodeData();
  31428. test_wc_PKCS7_EncodeSignedData();
  31429. test_wc_PKCS7_EncodeSignedData_ex();
  31430. test_wc_PKCS7_VerifySignedData();
  31431. test_wc_PKCS7_EncodeDecodeEnvelopedData();
  31432. test_wc_PKCS7_EncodeEncryptedData();
  31433. test_wc_PKCS7_Degenerate();
  31434. test_wc_PKCS7_BER();
  31435. test_PKCS7_signed_enveloped();
  31436. test_wc_i2d_PKCS12();
  31437. test_wolfSSL_CTX_LoadCRL();
  31438. AssertIntEQ(test_ForceZero(), 0);
  31439. AssertIntEQ(test_wolfSSL_Cleanup(), WOLFSSL_SUCCESS);
  31440. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  31441. !defined(NO_CERTS)
  31442. AssertIntEQ(test_various_pathlen_chains(), WOLFSSL_SUCCESS);
  31443. #endif
  31444. /* If at some point a stub get implemented this test should fail indicating
  31445. * a need to implement a new test case
  31446. */
  31447. test_stubs_are_stubs();
  31448. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  31449. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  31450. wc_ecc_fp_free(); /* free per thread cache */
  31451. #endif
  31452. wolfSSL_Cleanup();
  31453. (void)devId;
  31454. printf(" End API Tests\n");
  31455. }