api.c 1.1 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836
  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_FreeRng(&rng);
  1094. #endif
  1095. }
  1096. static void test_wolfSSL_CertManagerCRL(void)
  1097. {
  1098. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  1099. !defined(NO_RSA)
  1100. const char* ca_cert = "./certs/ca-cert.pem";
  1101. const char* crl1 = "./certs/crl/crl.pem";
  1102. const char* crl2 = "./certs/crl/crl2.pem";
  1103. WOLFSSL_CERT_MANAGER* cm = NULL;
  1104. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1105. AssertIntEQ(WOLFSSL_SUCCESS,
  1106. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  1107. AssertIntEQ(WOLFSSL_SUCCESS,
  1108. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  1109. AssertIntEQ(WOLFSSL_SUCCESS,
  1110. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  1111. wolfSSL_CertManagerFreeCRL(cm);
  1112. AssertIntEQ(WOLFSSL_SUCCESS,
  1113. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  1114. AssertIntEQ(WOLFSSL_SUCCESS,
  1115. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  1116. wolfSSL_CertManagerFree(cm);
  1117. #endif
  1118. }
  1119. static void test_wolfSSL_CTX_load_verify_locations_ex(void)
  1120. {
  1121. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  1122. !defined(NO_WOLFSSL_CLIENT)
  1123. WOLFSSL_CTX* ctx;
  1124. const char* ca_cert = "./certs/ca-cert.pem";
  1125. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1126. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1127. AssertNotNull(ctx);
  1128. /* test good CA */
  1129. AssertTrue(WOLFSSL_SUCCESS ==
  1130. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  1131. WOLFSSL_LOAD_FLAG_NONE));
  1132. /* test expired CA */
  1133. AssertTrue(WOLFSSL_SUCCESS !=
  1134. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  1135. WOLFSSL_LOAD_FLAG_NONE));
  1136. AssertTrue(WOLFSSL_SUCCESS ==
  1137. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  1138. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY));
  1139. wolfSSL_CTX_free(ctx);
  1140. #endif
  1141. }
  1142. static void test_wolfSSL_CTX_load_verify_buffer_ex(void)
  1143. {
  1144. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  1145. defined(USE_CERT_BUFFERS_2048)
  1146. WOLFSSL_CTX* ctx;
  1147. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  1148. byte ca_expired_cert[TWOK_BUF];
  1149. word32 sizeof_ca_expired_cert;
  1150. XFILE fp;
  1151. #ifndef NO_WOLFSSL_CLIENT
  1152. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1153. #else
  1154. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  1155. #endif
  1156. AssertNotNull(ctx);
  1157. /* test good CA */
  1158. AssertTrue(WOLFSSL_SUCCESS ==
  1159. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  1160. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  1161. WOLFSSL_LOAD_FLAG_NONE));
  1162. /* load expired CA */
  1163. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  1164. fp = XFOPEN(ca_expired_cert_file, "rb");
  1165. AssertTrue(fp != XBADFILE);
  1166. sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  1167. sizeof(ca_expired_cert), fp);
  1168. XFCLOSE(fp);
  1169. /* test expired CA failure */
  1170. AssertTrue(WOLFSSL_SUCCESS !=
  1171. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  1172. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  1173. WOLFSSL_LOAD_FLAG_NONE));
  1174. /* test expired CA success */
  1175. AssertTrue(WOLFSSL_SUCCESS ==
  1176. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  1177. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  1178. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY));
  1179. wolfSSL_CTX_free(ctx);
  1180. #endif
  1181. }
  1182. static void test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  1183. {
  1184. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  1185. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048)
  1186. WOLFSSL_CTX* ctx;
  1187. #ifndef NO_WOLFSSL_CLIENT
  1188. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1189. #else
  1190. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1191. #endif
  1192. AssertTrue(WOLFSSL_SUCCESS ==
  1193. wolfSSL_CTX_load_verify_chain_buffer_format(ctx, ca_cert_chain_der,
  1194. sizeof_ca_cert_chain_der,
  1195. WOLFSSL_FILETYPE_ASN1));
  1196. wolfSSL_CTX_free(ctx);
  1197. #endif
  1198. }
  1199. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  1200. {
  1201. int ret = 0;
  1202. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  1203. const char* server_chain_der = "./certs/server-cert-chain.der";
  1204. WOLFSSL_CTX* ctx;
  1205. #ifndef NO_WOLFSSL_CLIENT
  1206. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1207. AssertNotNull(ctx);
  1208. #else
  1209. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  1210. AssertNotNull(ctx);
  1211. #endif
  1212. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  1213. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1214. wolfSSL_CTX_free(ctx);
  1215. #endif
  1216. return ret;
  1217. }
  1218. static void test_wolfSSL_CTX_SetTmpDH_file(void)
  1219. {
  1220. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH)
  1221. WOLFSSL_CTX *ctx;
  1222. #ifndef NO_WOLFSSL_CLIENT
  1223. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1224. #else
  1225. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1226. #endif
  1227. /* invalid context */
  1228. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  1229. dhParamFile, WOLFSSL_FILETYPE_PEM));
  1230. /* invalid dhParamFile file */
  1231. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  1232. NULL, WOLFSSL_FILETYPE_PEM));
  1233. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  1234. bogusFile, WOLFSSL_FILETYPE_PEM));
  1235. /* success */
  1236. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  1237. WOLFSSL_FILETYPE_PEM));
  1238. wolfSSL_CTX_free(ctx);
  1239. #endif
  1240. }
  1241. static void test_wolfSSL_CTX_SetTmpDH_buffer(void)
  1242. {
  1243. #if !defined(NO_CERTS) && !defined(NO_DH)
  1244. WOLFSSL_CTX *ctx;
  1245. #ifndef NO_WOLFSSL_CLIENT
  1246. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1247. #else
  1248. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1249. #endif
  1250. /* invalid context */
  1251. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, dh_key_der_2048,
  1252. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1253. /* invalid dhParamFile file */
  1254. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  1255. 0, WOLFSSL_FILETYPE_ASN1));
  1256. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dsa_key_der_2048,
  1257. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1258. /* success */
  1259. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  1260. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1261. wolfSSL_CTX_free(ctx);
  1262. #endif
  1263. }
  1264. static void test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  1265. {
  1266. #if !defined(NO_CERTS) && !defined(NO_DH)
  1267. WOLFSSL_CTX *ctx;
  1268. #ifndef NO_WOLFSSL_CLIENT
  1269. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1270. AssertNotNull(ctx);
  1271. #else
  1272. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  1273. AssertNotNull(ctx);
  1274. #endif
  1275. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  1276. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  1277. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1278. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  1279. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  1280. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1281. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  1282. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  1283. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1284. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  1285. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  1286. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1287. wolfSSL_CTX_free(ctx);
  1288. #endif
  1289. }
  1290. static void test_wolfSSL_CTX_der_load_verify_locations(void)
  1291. {
  1292. #ifdef WOLFSSL_DER_LOAD
  1293. WOLFSSL_CTX* ctx = NULL;
  1294. const char* derCert = "./certs/server-cert.der";
  1295. const char* nullPath = NULL;
  1296. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  1297. const char* emptyPath = "";
  1298. /* der load Case 1 ctx NULL */
  1299. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  1300. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  1301. #ifndef NO_WOLFSSL_CLIENT
  1302. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1303. #else
  1304. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1305. #endif
  1306. /* Case 2 filePath NULL */
  1307. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  1308. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  1309. /* Case 3 invalid format */
  1310. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  1311. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  1312. /* Case 4 filePath not valid */
  1313. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  1314. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  1315. /* Case 5 filePath empty */
  1316. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  1317. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  1318. #ifndef NO_RSA
  1319. /* Case 6 success case */
  1320. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  1321. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1322. #endif
  1323. wolfSSL_CTX_free(ctx);
  1324. #endif
  1325. }
  1326. static void test_wolfSSL_CTX_enable_disable(void)
  1327. {
  1328. #ifndef NO_CERTS
  1329. WOLFSSL_CTX* ctx = NULL;
  1330. #ifdef HAVE_CRL
  1331. AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  1332. AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  1333. #endif
  1334. #ifdef HAVE_OCSP
  1335. AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  1336. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  1337. #endif
  1338. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  1339. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  1340. AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  1341. AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  1342. #endif
  1343. #ifndef NO_WOLFSSL_CLIENT
  1344. #ifdef HAVE_EXTENDED_MASTER
  1345. AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  1346. #endif
  1347. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1348. AssertNotNull(ctx);
  1349. #ifdef HAVE_EXTENDED_MASTER
  1350. AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  1351. #endif
  1352. #elif !defined(NO_WOLFSSL_SERVER)
  1353. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1354. #else
  1355. return;
  1356. #endif
  1357. #ifdef HAVE_CRL
  1358. AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  1359. AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  1360. #endif
  1361. #ifdef HAVE_OCSP
  1362. AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  1363. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  1364. WOLFSSL_SUCCESS);
  1365. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  1366. WOLFSSL_SUCCESS);
  1367. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  1368. WOLFSSL_SUCCESS);
  1369. #endif
  1370. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  1371. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  1372. AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  1373. AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  1374. #endif
  1375. wolfSSL_CTX_free(ctx);
  1376. #endif /* NO_CERTS */
  1377. }
  1378. /*----------------------------------------------------------------------------*
  1379. | SSL
  1380. *----------------------------------------------------------------------------*/
  1381. static void test_server_wolfSSL_new(void)
  1382. {
  1383. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  1384. !defined(NO_WOLFSSL_SERVER)
  1385. WOLFSSL_CTX *ctx;
  1386. WOLFSSL_CTX *ctx_nocert;
  1387. WOLFSSL *ssl;
  1388. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1389. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1390. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  1391. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  1392. /* invalid context */
  1393. AssertNull(ssl = wolfSSL_new(NULL));
  1394. #ifndef WOLFSSL_SESSION_EXPORT
  1395. AssertNull(ssl = wolfSSL_new(ctx_nocert));
  1396. #endif
  1397. /* success */
  1398. AssertNotNull(ssl = wolfSSL_new(ctx));
  1399. wolfSSL_free(ssl);
  1400. wolfSSL_CTX_free(ctx);
  1401. wolfSSL_CTX_free(ctx_nocert);
  1402. #endif
  1403. }
  1404. static void test_client_wolfSSL_new(void)
  1405. {
  1406. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  1407. !defined(NO_WOLFSSL_CLIENT)
  1408. WOLFSSL_CTX *ctx;
  1409. WOLFSSL_CTX *ctx_nocert;
  1410. WOLFSSL *ssl;
  1411. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1412. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1413. AssertTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  1414. /* invalid context */
  1415. AssertNull(ssl = wolfSSL_new(NULL));
  1416. /* success */
  1417. AssertNotNull(ssl = wolfSSL_new(ctx_nocert));
  1418. wolfSSL_free(ssl);
  1419. /* success */
  1420. AssertNotNull(ssl = wolfSSL_new(ctx));
  1421. wolfSSL_free(ssl);
  1422. wolfSSL_CTX_free(ctx);
  1423. wolfSSL_CTX_free(ctx_nocert);
  1424. #endif
  1425. }
  1426. static void test_wolfSSL_SetTmpDH_file(void)
  1427. {
  1428. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  1429. !defined(NO_WOLFSSL_SERVER)
  1430. WOLFSSL_CTX *ctx;
  1431. WOLFSSL *ssl;
  1432. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1433. #ifndef NO_RSA
  1434. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1435. WOLFSSL_FILETYPE_PEM));
  1436. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1437. WOLFSSL_FILETYPE_PEM));
  1438. #elif defined(HAVE_ECC)
  1439. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  1440. WOLFSSL_FILETYPE_PEM));
  1441. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  1442. WOLFSSL_FILETYPE_PEM));
  1443. #elif defined(HAVE_ED25519)
  1444. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  1445. WOLFSSL_FILETYPE_PEM));
  1446. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  1447. WOLFSSL_FILETYPE_PEM));
  1448. #elif defined(HAVE_ED448)
  1449. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  1450. WOLFSSL_FILETYPE_PEM));
  1451. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  1452. WOLFSSL_FILETYPE_PEM));
  1453. #endif
  1454. AssertNotNull(ssl = wolfSSL_new(ctx));
  1455. /* invalid ssl */
  1456. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  1457. dhParamFile, WOLFSSL_FILETYPE_PEM));
  1458. /* invalid dhParamFile file */
  1459. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  1460. NULL, WOLFSSL_FILETYPE_PEM));
  1461. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  1462. bogusFile, WOLFSSL_FILETYPE_PEM));
  1463. /* success */
  1464. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  1465. WOLFSSL_FILETYPE_PEM));
  1466. wolfSSL_free(ssl);
  1467. wolfSSL_CTX_free(ctx);
  1468. #endif
  1469. }
  1470. static void test_wolfSSL_SetTmpDH_buffer(void)
  1471. {
  1472. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  1473. WOLFSSL_CTX *ctx;
  1474. WOLFSSL *ssl;
  1475. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1476. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  1477. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  1478. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  1479. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1480. AssertNotNull(ssl = wolfSSL_new(ctx));
  1481. /* invalid ssl */
  1482. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  1483. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1484. /* invalid dhParamFile file */
  1485. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  1486. 0, WOLFSSL_FILETYPE_ASN1));
  1487. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  1488. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1489. /* success */
  1490. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  1491. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1492. wolfSSL_free(ssl);
  1493. wolfSSL_CTX_free(ctx);
  1494. #endif
  1495. }
  1496. static void test_wolfSSL_SetMinMaxDhKey_Sz(void)
  1497. {
  1498. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  1499. WOLFSSL_CTX *ctx, *ctx2;
  1500. WOLFSSL *ssl, *ssl2;
  1501. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  1502. AssertNotNull(ctx);
  1503. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  1504. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  1505. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  1506. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1507. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  1508. ssl = wolfSSL_new(ctx);
  1509. AssertNotNull(ssl);
  1510. ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method());
  1511. AssertNotNull(ctx2);
  1512. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  1513. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  1514. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  1515. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1516. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  1517. ssl2 = wolfSSL_new(ctx2);
  1518. AssertNotNull(ssl2);
  1519. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  1520. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1521. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  1522. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  1523. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1524. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  1525. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  1526. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1527. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  1528. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1529. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  1530. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  1531. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1532. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  1533. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  1534. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1535. wolfSSL_free(ssl2);
  1536. wolfSSL_CTX_free(ctx2);
  1537. wolfSSL_free(ssl);
  1538. wolfSSL_CTX_free(ctx);
  1539. #endif
  1540. }
  1541. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  1542. * allowed.
  1543. * POST: return 1 on success.
  1544. */
  1545. static int test_wolfSSL_SetMinVersion(void)
  1546. {
  1547. int failFlag = WOLFSSL_SUCCESS;
  1548. #ifndef NO_WOLFSSL_CLIENT
  1549. WOLFSSL_CTX* ctx;
  1550. WOLFSSL* ssl;
  1551. int itr;
  1552. #ifndef NO_OLD_TLS
  1553. const int versions[] = {
  1554. #ifdef WOLFSSL_ALLOW_TLSV10
  1555. WOLFSSL_TLSV1,
  1556. #endif
  1557. WOLFSSL_TLSV1_1,
  1558. WOLFSSL_TLSV1_2};
  1559. #elif !defined(WOLFSSL_NO_TLS12)
  1560. const int versions[] = { WOLFSSL_TLSV1_2 };
  1561. #else
  1562. const int versions[] = { WOLFSSL_TLSV1_3 };
  1563. #endif
  1564. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1565. ssl = wolfSSL_new(ctx);
  1566. printf(testingFmt, "wolfSSL_SetMinVersion()");
  1567. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++){
  1568. if(wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS){
  1569. failFlag = WOLFSSL_FAILURE;
  1570. }
  1571. }
  1572. printf(resultFmt, failFlag == WOLFSSL_SUCCESS ? passed : failed);
  1573. wolfSSL_free(ssl);
  1574. wolfSSL_CTX_free(ctx);
  1575. #endif
  1576. return failFlag;
  1577. } /* END test_wolfSSL_SetMinVersion */
  1578. /*----------------------------------------------------------------------------*
  1579. | EC
  1580. *----------------------------------------------------------------------------*/
  1581. /* Test function for EC_POINT_new, EC_POINT_mul, EC_POINT_free,
  1582. EC_GROUP_new_by_curve_name, EC_GROUP_order_bits
  1583. */
  1584. # if defined(OPENSSL_EXTRA) && \
  1585. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  1586. static void test_wolfSSL_EC(void)
  1587. {
  1588. #if defined(HAVE_ECC)
  1589. BN_CTX *ctx;
  1590. EC_GROUP *group;
  1591. EC_POINT *Gxy, *new_point, *set_point;
  1592. BIGNUM *k = NULL, *Gx = NULL, *Gy = NULL, *Gz = NULL;
  1593. BIGNUM *X, *Y;
  1594. BIGNUM *set_point_bn;
  1595. char* hexStr;
  1596. int group_bits;
  1597. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD17AF381913FF7A96314EA47055EA0FD0";
  1598. /* NISTP256R1 Gx/Gy */
  1599. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
  1600. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5";
  1601. #ifndef HAVE_SELFTEST
  1602. EC_POINT *tmp;
  1603. size_t bin_len;
  1604. unsigned char* buf = NULL;
  1605. const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C2964FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5";
  1606. const unsigned char binUncompG[] = {
  1607. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  1608. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  1609. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  1610. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  1611. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  1612. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  1613. };
  1614. #ifdef HAVE_COMP_KEY
  1615. const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
  1616. const unsigned char binCompG[] = {
  1617. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  1618. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  1619. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  1620. };
  1621. #endif
  1622. #endif
  1623. AssertNotNull(ctx = BN_CTX_new());
  1624. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  1625. AssertIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  1626. AssertNotNull(Gxy = EC_POINT_new(group));
  1627. AssertNotNull(new_point = EC_POINT_new(group));
  1628. AssertNotNull(set_point = EC_POINT_new(group));
  1629. AssertNotNull(X = BN_new());
  1630. AssertNotNull(Y = BN_new());
  1631. AssertNotNull(set_point_bn = BN_new());
  1632. /* load test values */
  1633. AssertIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  1634. AssertIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  1635. AssertIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  1636. AssertIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  1637. /* populate coordinates for input point */
  1638. Gxy->X = Gx;
  1639. Gxy->Y = Gy;
  1640. Gxy->Z = Gz;
  1641. #ifndef HAVE_SELFTEST
  1642. /* perform point multiplication */
  1643. AssertIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), WOLFSSL_SUCCESS);
  1644. AssertIntEQ(BN_is_zero(new_point->X), 0);
  1645. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  1646. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  1647. AssertIntEQ(EC_POINT_mul(group, new_point, NULL, 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, Gx, NULL, NULL, 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. #else
  1656. AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy, ctx), WOLFSSL_SUCCESS);
  1657. AssertIntEQ(BN_is_zero(new_point->X), 0);
  1658. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  1659. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  1660. #endif
  1661. /* Force non-affine coordinates */
  1662. AssertIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  1663. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  1664. new_point->inSet = 0;
  1665. /* extract the coordinates from point */
  1666. AssertIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y, ctx), WOLFSSL_SUCCESS);
  1667. /* check if point X coordinate is zero */
  1668. AssertIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  1669. /* set the same X and Y points in another object */
  1670. AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y, ctx), WOLFSSL_SUCCESS);
  1671. /* compare points as they should be the same */
  1672. AssertIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  1673. /* Test copying */
  1674. AssertIntEQ(EC_POINT_copy(new_point, set_point), 1);
  1675. AssertPtrEq(EC_POINT_point2bn(group, set_point, POINT_CONVERSION_UNCOMPRESSED,
  1676. set_point_bn, ctx), set_point_bn);
  1677. /* check bn2hex */
  1678. hexStr = BN_bn2hex(k);
  1679. AssertStrEQ(hexStr, kTest);
  1680. #ifndef NO_FILESYSTEM
  1681. BN_print_fp(stdout, k);
  1682. printf("\n");
  1683. #endif
  1684. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  1685. hexStr = BN_bn2hex(Gx);
  1686. AssertStrEQ(hexStr, kGx);
  1687. #ifndef NO_FILESYSTEM
  1688. BN_print_fp(stdout, Gx);
  1689. printf("\n");
  1690. #endif
  1691. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  1692. hexStr = BN_bn2hex(Gy);
  1693. AssertStrEQ(hexStr, kGy);
  1694. #ifndef NO_FILESYSTEM
  1695. BN_print_fp(stdout, Gy);
  1696. printf("\n");
  1697. #endif
  1698. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  1699. #ifndef HAVE_SELFTEST
  1700. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  1701. AssertStrEQ(hexStr, uncompG);
  1702. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  1703. #ifdef HAVE_COMP_KEY
  1704. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  1705. AssertStrEQ(hexStr, compG);
  1706. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  1707. #endif
  1708. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx);
  1709. AssertIntEQ(bin_len, sizeof(binUncompG));
  1710. AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL, DYNAMIC_TYPE_ECC));
  1711. AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, buf,
  1712. bin_len, ctx), bin_len);
  1713. AssertIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  1714. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  1715. #ifdef HAVE_COMP_KEY
  1716. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL, 0, ctx);
  1717. AssertIntEQ(bin_len, sizeof(binCompG));
  1718. AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL, DYNAMIC_TYPE_ECC));
  1719. AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  1720. bin_len, ctx), bin_len);
  1721. AssertIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  1722. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  1723. #endif
  1724. AssertNotNull(tmp = EC_POINT_new(group));
  1725. AssertIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG), ctx), 1);
  1726. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  1727. EC_POINT_free(tmp);
  1728. #ifdef HAVE_COMP_KEY
  1729. AssertNotNull(tmp = EC_POINT_new(group));
  1730. AssertIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx), 1);
  1731. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  1732. EC_POINT_free(tmp);
  1733. #endif
  1734. #endif
  1735. /* test BN_mod_add */
  1736. AssertIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  1737. (WOLFSSL_BIGNUM*)BN_value_one(),
  1738. (WOLFSSL_BIGNUM*)BN_value_one(), NULL), 1);
  1739. AssertIntEQ(BN_is_zero(new_point->Z), 1);
  1740. /* cleanup */
  1741. BN_free(X);
  1742. BN_free(Y);
  1743. BN_free(k);
  1744. BN_free(set_point_bn);
  1745. EC_POINT_free(new_point);
  1746. EC_POINT_free(set_point);
  1747. EC_POINT_free(Gxy);
  1748. EC_GROUP_free(group);
  1749. BN_CTX_free(ctx);
  1750. #endif /* HAVE_ECC */
  1751. }
  1752. #endif /* OPENSSL_EXTRA && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  1753. static void test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  1754. {
  1755. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA)
  1756. EC_GROUP *group;
  1757. BIO* bio;
  1758. AssertNotNull(bio = BIO_new(BIO_s_file()));
  1759. AssertIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  1760. AssertNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  1761. AssertIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  1762. EC_GROUP_free(group);
  1763. BIO_free(bio);
  1764. #endif /* HAVE_ECC */
  1765. }
  1766. # if defined(OPENSSL_EXTRA)
  1767. static void test_wolfSSL_ECDSA_SIG(void)
  1768. {
  1769. #ifdef HAVE_ECC
  1770. WOLFSSL_ECDSA_SIG* sig = NULL;
  1771. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  1772. const unsigned char* cp;
  1773. unsigned char* p;
  1774. unsigned char outSig[8];
  1775. unsigned char sigData[8] =
  1776. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  1777. sig = wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData));
  1778. AssertNull(sig);
  1779. cp = sigData;
  1780. AssertNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  1781. AssertIntEQ((cp == sigData + 8), 1);
  1782. cp = sigData;
  1783. AssertNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  1784. AssertNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  1785. AssertIntEQ((sig == sig2), 1);
  1786. cp = outSig;
  1787. p = outSig;
  1788. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  1789. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  1790. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  1791. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  1792. AssertIntEQ((p == outSig + 8), 1);
  1793. AssertIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  1794. wolfSSL_ECDSA_SIG_free(sig);
  1795. #endif /* HAVE_ECC */
  1796. }
  1797. static void test_ECDSA_size_sign(void)
  1798. {
  1799. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  1800. EC_KEY *key;
  1801. int id;
  1802. byte hash[WC_MAX_DIGEST_SIZE];
  1803. byte sig[ECC_BUFSIZE];
  1804. unsigned int sigSz = sizeof(sig);
  1805. XMEMSET(hash, 123, sizeof(hash));
  1806. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  1807. AssertIntEQ(id, ECC_SECP256R1);
  1808. AssertNotNull(key = wolfSSL_EC_KEY_new_by_curve_name(id));
  1809. AssertIntEQ(EC_KEY_generate_key(key), 1);
  1810. AssertIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  1811. AssertIntGE(ECDSA_size(key), sigSz);
  1812. EC_KEY_free(key);
  1813. #endif /* HAVE_ECC && !NO_ECC256 && !NO_ECC_SECP */
  1814. }
  1815. static void test_ED25519(void)
  1816. {
  1817. #if defined(HAVE_ED25519) && defined(WOLFSSL_KEY_GEN)
  1818. byte priv[ED25519_PRV_KEY_SIZE];
  1819. unsigned int privSz = (unsigned int)sizeof(priv);
  1820. byte pub[ED25519_PUB_KEY_SIZE];
  1821. unsigned int pubSz = (unsigned int)sizeof(pub);
  1822. const char* msg = "Everyone gets Friday off.";
  1823. unsigned int msglen = (unsigned int)XSTRLEN(msg);
  1824. byte sig[ED25519_SIG_SIZE];
  1825. unsigned int sigSz = (unsigned int)sizeof(sig);
  1826. AssertIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  1827. WOLFSSL_SUCCESS);
  1828. AssertIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  1829. AssertIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  1830. AssertIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  1831. &sigSz), WOLFSSL_SUCCESS);
  1832. AssertIntEQ(sigSz, ED25519_SIG_SIZE);
  1833. AssertIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  1834. sigSz), WOLFSSL_SUCCESS);
  1835. #endif /* HAVE_ED25519 && WOLFSSL_KEY_GEN */
  1836. }
  1837. static void test_ED448(void)
  1838. {
  1839. #if defined(HAVE_ED448) && defined(WOLFSSL_KEY_GEN)
  1840. byte priv[ED448_PRV_KEY_SIZE];
  1841. unsigned int privSz = (unsigned int)sizeof(priv);
  1842. byte pub[ED448_PUB_KEY_SIZE];
  1843. unsigned int pubSz = (unsigned int)sizeof(pub);
  1844. const char* msg = "Everyone gets Friday off.";
  1845. unsigned int msglen = (unsigned int)XSTRLEN(msg);
  1846. byte sig[ED448_SIG_SIZE];
  1847. unsigned int sigSz = (unsigned int)sizeof(sig);
  1848. AssertIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz),
  1849. WOLFSSL_SUCCESS);
  1850. AssertIntEQ(privSz, ED448_PRV_KEY_SIZE);
  1851. AssertIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  1852. AssertIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  1853. &sigSz), WOLFSSL_SUCCESS);
  1854. AssertIntEQ(sigSz, ED448_SIG_SIZE);
  1855. AssertIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  1856. sigSz), WOLFSSL_SUCCESS);
  1857. #endif /* HAVE_ED448 && WOLFSSL_KEY_GEN */
  1858. }
  1859. #endif /* OPENSSL_EXTRA */
  1860. #include <wolfssl/openssl/pem.h>
  1861. /*----------------------------------------------------------------------------*
  1862. | EVP
  1863. *----------------------------------------------------------------------------*/
  1864. /* Test function for wolfSSL_EVP_get_cipherbynid.
  1865. */
  1866. #ifdef OPENSSL_EXTRA
  1867. static void test_wolfSSL_EVP_get_cipherbynid(void)
  1868. {
  1869. #ifndef NO_AES
  1870. const WOLFSSL_EVP_CIPHER* c;
  1871. c = wolfSSL_EVP_get_cipherbynid(419);
  1872. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  1873. AssertNotNull(c);
  1874. AssertNotNull(strcmp("EVP_AES_128_CBC", c));
  1875. #else
  1876. AssertNull(c);
  1877. #endif
  1878. c = wolfSSL_EVP_get_cipherbynid(423);
  1879. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_192)
  1880. AssertNotNull(c);
  1881. AssertNotNull(strcmp("EVP_AES_192_CBC", c));
  1882. #else
  1883. AssertNull(c);
  1884. #endif
  1885. c = wolfSSL_EVP_get_cipherbynid(427);
  1886. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  1887. AssertNotNull(c);
  1888. AssertNotNull(strcmp("EVP_AES_256_CBC", c));
  1889. #else
  1890. AssertNull(c);
  1891. #endif
  1892. c = wolfSSL_EVP_get_cipherbynid(904);
  1893. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  1894. AssertNotNull(c);
  1895. AssertNotNull(strcmp("EVP_AES_128_CTR", c));
  1896. #else
  1897. AssertNull(c);
  1898. #endif
  1899. c = wolfSSL_EVP_get_cipherbynid(905);
  1900. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  1901. AssertNotNull(c);
  1902. AssertNotNull(strcmp("EVP_AES_192_CTR", c));
  1903. #else
  1904. AssertNull(c);
  1905. #endif
  1906. c = wolfSSL_EVP_get_cipherbynid(906);
  1907. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  1908. AssertNotNull(c);
  1909. AssertNotNull(strcmp("EVP_AES_256_CTR", c));
  1910. #else
  1911. AssertNull(c);
  1912. #endif
  1913. c = wolfSSL_EVP_get_cipherbynid(418);
  1914. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  1915. AssertNotNull(c);
  1916. AssertNotNull(strcmp("EVP_AES_128_ECB", c));
  1917. #else
  1918. AssertNull(c);
  1919. #endif
  1920. c = wolfSSL_EVP_get_cipherbynid(422);
  1921. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  1922. AssertNotNull(c);
  1923. AssertNotNull(strcmp("EVP_AES_192_ECB", c));
  1924. #else
  1925. AssertNull(c);
  1926. #endif
  1927. c = wolfSSL_EVP_get_cipherbynid(426);
  1928. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  1929. AssertNotNull(c);
  1930. AssertNotNull(strcmp("EVP_AES_256_ECB", c));
  1931. #else
  1932. AssertNull(c);
  1933. #endif
  1934. #endif /* !NO_AES */
  1935. #ifndef NO_DES3
  1936. AssertNotNull(strcmp("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  1937. #ifdef WOLFSSL_DES_ECB
  1938. AssertNotNull(strcmp("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  1939. #endif
  1940. AssertNotNull(strcmp("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  1941. #ifdef WOLFSSL_DES_ECB
  1942. AssertNotNull(strcmp("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  1943. #endif
  1944. #endif /* !NO_DES3 */
  1945. #ifdef HAVE_IDEA
  1946. AssertNotNull(strcmp("EVP_IDEA_CBC", wolfSSL_EVP_get_cipherbynid(34)));
  1947. #endif
  1948. /* test for nid is out of range */
  1949. AssertNull(wolfSSL_EVP_get_cipherbynid(1));
  1950. }
  1951. static void test_wolfSSL_EVP_CIPHER_CTX()
  1952. {
  1953. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  1954. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  1955. const EVP_CIPHER *init = EVP_aes_128_cbc();
  1956. const EVP_CIPHER *test;
  1957. byte key[AES_BLOCK_SIZE] = {0};
  1958. byte iv[AES_BLOCK_SIZE] = {0};
  1959. AssertNotNull(ctx);
  1960. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  1961. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  1962. test = EVP_CIPHER_CTX_cipher(ctx);
  1963. AssertTrue(init == test);
  1964. AssertIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  1965. AssertIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  1966. AssertIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  1967. EVP_CIPHER_CTX_free(ctx);
  1968. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  1969. }
  1970. #endif /* OPENSSL_EXTRA */
  1971. /*----------------------------------------------------------------------------*
  1972. | IO
  1973. *----------------------------------------------------------------------------*/
  1974. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1975. !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
  1976. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  1977. #define HAVE_IO_TESTS_DEPENDENCIES
  1978. #endif
  1979. /* helper functions */
  1980. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  1981. #ifdef WOLFSSL_SESSION_EXPORT
  1982. /* set up function for sending session information */
  1983. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  1984. {
  1985. WOLFSSL_CTX* ctx;
  1986. WOLFSSL* ssl;
  1987. AssertNotNull(inSsl);
  1988. AssertNotNull(buf);
  1989. AssertIntNE(0, sz);
  1990. /* Set ctx to DTLS 1.2 */
  1991. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  1992. AssertNotNull(ctx);
  1993. ssl = wolfSSL_new(ctx);
  1994. AssertNotNull(ssl);
  1995. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  1996. wolfSSL_free(ssl);
  1997. wolfSSL_CTX_free(ctx);
  1998. (void)userCtx;
  1999. return WOLFSSL_SUCCESS;
  2000. }
  2001. /* returns negative value on fail and positive (including 0) on success */
  2002. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  2003. {
  2004. int ret, err, loop_count, count, timeout = 10;
  2005. char msg[] = "I hear you fa shizzle!";
  2006. char input[1024];
  2007. loop_count = ((func_args*)args)->argc;
  2008. do {
  2009. #ifdef WOLFSSL_ASYNC_CRYPT
  2010. if (err == WC_PENDING_E) {
  2011. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  2012. if (ret < 0) { break; } else if (ret == 0) { continue; }
  2013. }
  2014. #endif
  2015. err = 0; /* Reset error */
  2016. ret = wolfSSL_accept(ssl);
  2017. if (ret != WOLFSSL_SUCCESS) {
  2018. err = wolfSSL_get_error(ssl, 0);
  2019. if (err == WOLFSSL_ERROR_WANT_READ ||
  2020. err == WOLFSSL_ERROR_WANT_WRITE) {
  2021. int select_ret;
  2022. err = WC_PENDING_E;
  2023. select_ret = tcp_select(*sockfd, timeout);
  2024. if (select_ret == TEST_TIMEOUT) {
  2025. return WOLFSSL_FATAL_ERROR;
  2026. }
  2027. }
  2028. }
  2029. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  2030. if (ret != WOLFSSL_SUCCESS) {
  2031. char buff[WOLFSSL_MAX_ERROR_SZ];
  2032. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  2033. return ret;
  2034. }
  2035. for (count = 0; count < loop_count; count++) {
  2036. int select_ret;
  2037. select_ret = tcp_select(*sockfd, timeout);
  2038. if (select_ret == TEST_TIMEOUT) {
  2039. ret = WOLFSSL_FATAL_ERROR;
  2040. break;
  2041. }
  2042. do {
  2043. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  2044. if (ret > 0) {
  2045. input[ret] = '\0';
  2046. printf("Client message: %s\n", input);
  2047. }
  2048. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  2049. do {
  2050. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  2051. return WOLFSSL_FATAL_ERROR;
  2052. }
  2053. err = wolfSSL_get_error(ssl, ret);
  2054. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  2055. }
  2056. return ret;
  2057. }
  2058. #endif /* WOLFSSL_SESSION_EXPORT */
  2059. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  2060. {
  2061. SOCKET_T sockfd = 0;
  2062. SOCKET_T clientfd = 0;
  2063. word16 port;
  2064. callback_functions* cbf;
  2065. WOLFSSL_CTX* ctx = 0;
  2066. WOLFSSL* ssl = 0;
  2067. char msg[] = "I hear you fa shizzle!";
  2068. char input[1024];
  2069. int idx;
  2070. int ret, err = 0;
  2071. int sharedCtx = 0;
  2072. #ifdef WOLFSSL_TIRTOS
  2073. fdOpenSession(Task_self());
  2074. #endif
  2075. ((func_args*)args)->return_code = TEST_FAIL;
  2076. cbf = ((func_args*)args)->callbacks;
  2077. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2078. if (cbf != NULL && cbf->ctx) {
  2079. ctx = cbf->ctx;
  2080. sharedCtx = 1;
  2081. }
  2082. else
  2083. #endif
  2084. {
  2085. WOLFSSL_METHOD* method = NULL;
  2086. if (cbf != NULL && cbf->method != NULL) {
  2087. method = cbf->method();
  2088. }
  2089. else {
  2090. method = wolfSSLv23_server_method();
  2091. }
  2092. ctx = wolfSSL_CTX_new(method);
  2093. }
  2094. #if defined(HAVE_SESSION_TICKET) && defined(HAVE_CHACHA) && \
  2095. defined(HAVE_POLY1305)
  2096. TicketInit();
  2097. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  2098. #endif
  2099. #if defined(USE_WINDOWS_API)
  2100. port = ((func_args*)args)->signal->port;
  2101. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  2102. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  2103. /* Let tcp_listen assign port */
  2104. port = 0;
  2105. #else
  2106. /* Use default port */
  2107. port = wolfSSLPort;
  2108. #endif
  2109. /* do it here to detect failure */
  2110. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1);
  2111. CloseSocket(sockfd);
  2112. wolfSSL_CTX_set_verify(ctx,
  2113. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  2114. #ifdef WOLFSSL_ENCRYPTED_KEYS
  2115. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  2116. #endif
  2117. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  2118. != WOLFSSL_SUCCESS) {
  2119. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  2120. goto done;
  2121. }
  2122. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2123. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  2124. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2125. #else
  2126. if (wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  2127. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2128. #endif
  2129. /*err_sys("can't load server cert chain file, "
  2130. "Please run from wolfSSL home dir");*/
  2131. goto done;
  2132. }
  2133. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2134. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  2135. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2136. #else
  2137. if (wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  2138. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2139. #endif
  2140. /*err_sys("can't load server key file, "
  2141. "Please run from wolfSSL home dir");*/
  2142. goto done;
  2143. }
  2144. /* call ctx setup callback */
  2145. if (cbf != NULL && cbf->ctx_ready != NULL) {
  2146. cbf->ctx_ready(ctx);
  2147. }
  2148. ssl = wolfSSL_new(ctx);
  2149. if (ssl == NULL) {
  2150. goto done;
  2151. }
  2152. #ifdef WOLFSSL_SESSION_EXPORT
  2153. /* only add in more complex nonblocking case with session export tests */
  2154. if (args && ((func_args*)args)->argc > 0) {
  2155. /* set as nonblock and time out for waiting on read/write */
  2156. tcp_set_nonblocking(&clientfd);
  2157. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  2158. }
  2159. #endif
  2160. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2161. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  2162. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2163. #else
  2164. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  2165. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2166. #endif
  2167. /*err_sys("can't load server cert chain file, "
  2168. "Please run from wolfSSL home dir");*/
  2169. goto done;
  2170. }
  2171. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2172. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  2173. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2174. #else
  2175. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  2176. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2177. #endif
  2178. /*err_sys("can't load server key file, "
  2179. "Please run from wolfSSL home dir");*/
  2180. goto done;
  2181. }
  2182. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  2183. /*err_sys("SSL_set_fd failed");*/
  2184. goto done;
  2185. }
  2186. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  2187. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  2188. #elif !defined(NO_DH)
  2189. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  2190. #endif
  2191. /* call ssl setup callback */
  2192. if (cbf != NULL && cbf->ssl_ready != NULL) {
  2193. cbf->ssl_ready(ssl);
  2194. }
  2195. #ifdef WOLFSSL_SESSION_EXPORT
  2196. /* only add in more complex nonblocking case with session export tests */
  2197. if (((func_args*)args)->argc > 0) {
  2198. ret = nonblocking_accept_read(args, ssl, &clientfd);
  2199. if (ret >= 0) {
  2200. ((func_args*)args)->return_code = TEST_SUCCESS;
  2201. }
  2202. #ifdef WOLFSSL_TIRTOS
  2203. Task_yield();
  2204. #endif
  2205. goto done;
  2206. }
  2207. #endif
  2208. do {
  2209. #ifdef WOLFSSL_ASYNC_CRYPT
  2210. if (err == WC_PENDING_E) {
  2211. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  2212. if (ret < 0) { break; } else if (ret == 0) { continue; }
  2213. }
  2214. #endif
  2215. err = 0; /* Reset error */
  2216. ret = wolfSSL_accept(ssl);
  2217. if (ret != WOLFSSL_SUCCESS) {
  2218. err = wolfSSL_get_error(ssl, 0);
  2219. }
  2220. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  2221. if (ret != WOLFSSL_SUCCESS) {
  2222. char buff[WOLFSSL_MAX_ERROR_SZ];
  2223. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  2224. /*err_sys("SSL_accept failed");*/
  2225. goto done;
  2226. }
  2227. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  2228. if (idx > 0) {
  2229. input[idx] = '\0';
  2230. printf("Client message: %s\n", input);
  2231. }
  2232. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  2233. /*err_sys("SSL_write failed");*/
  2234. #ifdef WOLFSSL_TIRTOS
  2235. return;
  2236. #else
  2237. return 0;
  2238. #endif
  2239. }
  2240. #ifdef WOLFSSL_TIRTOS
  2241. Task_yield();
  2242. #endif
  2243. ((func_args*)args)->return_code = TEST_SUCCESS;
  2244. done:
  2245. wolfSSL_shutdown(ssl);
  2246. wolfSSL_free(ssl);
  2247. if (!sharedCtx)
  2248. wolfSSL_CTX_free(ctx);
  2249. CloseSocket(clientfd);
  2250. #ifdef WOLFSSL_TIRTOS
  2251. fdCloseSession(Task_self());
  2252. #endif
  2253. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  2254. && defined(HAVE_THREAD_LS)
  2255. wc_ecc_fp_free(); /* free per thread cache */
  2256. #endif
  2257. #if defined(HAVE_SESSION_TICKET) && defined(HAVE_CHACHA) && \
  2258. defined(HAVE_POLY1305)
  2259. TicketCleanup();
  2260. #endif
  2261. #ifndef WOLFSSL_TIRTOS
  2262. return 0;
  2263. #endif
  2264. }
  2265. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  2266. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  2267. {
  2268. SOCKET_T sockfd = 0;
  2269. SOCKET_T clientfd = 0;
  2270. word16 port;
  2271. callback_functions* cbf;
  2272. WOLFSSL_CTX* ctx = 0;
  2273. WOLFSSL* ssl = 0;
  2274. char msg[] = "I hear you fa shizzle!";
  2275. char input[1024];
  2276. int idx;
  2277. int ret, err = 0;
  2278. int sharedCtx = 0;
  2279. int loop_count = ((func_args*)args)->argc;
  2280. int count = 0;
  2281. #ifdef WOLFSSL_TIRTOS
  2282. fdOpenSession(Task_self());
  2283. #endif
  2284. ((func_args*)args)->return_code = TEST_FAIL;
  2285. cbf = ((func_args*)args)->callbacks;
  2286. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2287. if (cbf != NULL && cbf->ctx) {
  2288. ctx = cbf->ctx;
  2289. sharedCtx = 1;
  2290. }
  2291. else
  2292. #endif
  2293. {
  2294. WOLFSSL_METHOD* method = NULL;
  2295. if (cbf != NULL && cbf->method != NULL) {
  2296. method = cbf->method();
  2297. }
  2298. else {
  2299. method = wolfSSLv23_server_method();
  2300. }
  2301. ctx = wolfSSL_CTX_new(method);
  2302. }
  2303. #if defined(USE_WINDOWS_API)
  2304. port = ((func_args*)args)->signal->port;
  2305. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  2306. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  2307. /* Let tcp_listen assign port */
  2308. port = 0;
  2309. #else
  2310. /* Use default port */
  2311. port = wolfSSLPort;
  2312. #endif
  2313. wolfSSL_CTX_set_verify(ctx,
  2314. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  2315. #ifdef WOLFSSL_ENCRYPTED_KEYS
  2316. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  2317. #endif
  2318. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  2319. != WOLFSSL_SUCCESS) {
  2320. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  2321. goto done;
  2322. }
  2323. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  2324. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2325. /*err_sys("can't load server cert chain file, "
  2326. "Please run from wolfSSL home dir");*/
  2327. goto done;
  2328. }
  2329. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  2330. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2331. /*err_sys("can't load server key file, "
  2332. "Please run from wolfSSL home dir");*/
  2333. goto done;
  2334. }
  2335. /* call ctx setup callback */
  2336. if (cbf != NULL && cbf->ctx_ready != NULL) {
  2337. cbf->ctx_ready(ctx);
  2338. }
  2339. while(count != loop_count) {
  2340. ssl = wolfSSL_new(ctx);
  2341. if (ssl == NULL) {
  2342. goto done;
  2343. }
  2344. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  2345. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2346. /*err_sys("can't load server cert chain file, "
  2347. "Please run from wolfSSL home dir");*/
  2348. goto done;
  2349. }
  2350. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  2351. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2352. /*err_sys("can't load server key file, "
  2353. "Please run from wolfSSL home dir");*/
  2354. goto done;
  2355. }
  2356. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  2357. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  2358. #elif !defined(NO_DH)
  2359. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  2360. #endif
  2361. /* call ssl setup callback */
  2362. if (cbf != NULL && cbf->ssl_ready != NULL) {
  2363. cbf->ssl_ready(ssl);
  2364. }
  2365. /* do it here to detect failure */
  2366. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1);
  2367. CloseSocket(sockfd);
  2368. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  2369. /*err_sys("SSL_set_fd failed");*/
  2370. goto done;
  2371. }
  2372. do {
  2373. #ifdef WOLFSSL_ASYNC_CRYPT
  2374. if (err == WC_PENDING_E) {
  2375. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  2376. if (ret < 0) { break; } else if (ret == 0) { continue; }
  2377. }
  2378. #endif
  2379. err = 0; /* Reset error */
  2380. ret = wolfSSL_accept(ssl);
  2381. if (ret != WOLFSSL_SUCCESS) {
  2382. err = wolfSSL_get_error(ssl, 0);
  2383. }
  2384. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  2385. if (ret != WOLFSSL_SUCCESS) {
  2386. char buff[WOLFSSL_MAX_ERROR_SZ];
  2387. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  2388. /*err_sys("SSL_accept failed");*/
  2389. goto done;
  2390. }
  2391. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  2392. if (idx > 0) {
  2393. input[idx] = '\0';
  2394. printf("Client message: %s\n", input);
  2395. }
  2396. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  2397. /*err_sys("SSL_write failed");*/
  2398. #ifdef WOLFSSL_TIRTOS
  2399. return;
  2400. #else
  2401. return 0;
  2402. #endif
  2403. }
  2404. /* free ssl for this connection */
  2405. wolfSSL_shutdown(ssl);
  2406. wolfSSL_free(ssl); ssl = NULL;
  2407. CloseSocket(clientfd);
  2408. count++;
  2409. }
  2410. #ifdef WOLFSSL_TIRTOS
  2411. Task_yield();
  2412. #endif
  2413. ((func_args*)args)->return_code = TEST_SUCCESS;
  2414. done:
  2415. if(ssl != NULL) {
  2416. wolfSSL_shutdown(ssl);
  2417. wolfSSL_free(ssl);
  2418. }
  2419. if (!sharedCtx)
  2420. wolfSSL_CTX_free(ctx);
  2421. CloseSocket(clientfd);
  2422. #ifdef WOLFSSL_TIRTOS
  2423. fdCloseSession(Task_self());
  2424. #endif
  2425. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  2426. && defined(HAVE_THREAD_LS)
  2427. wc_ecc_fp_free(); /* free per thread cache */
  2428. #endif
  2429. #ifndef WOLFSSL_TIRTOS
  2430. return 0;
  2431. #endif
  2432. }
  2433. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  2434. typedef int (*cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  2435. static void test_client_nofail(void* args, void *cb)
  2436. {
  2437. SOCKET_T sockfd = 0;
  2438. callback_functions* cbf;
  2439. WOLFSSL_CTX* ctx = 0;
  2440. WOLFSSL* ssl = 0;
  2441. WOLFSSL_CIPHER* cipher;
  2442. char msg[64] = "hello wolfssl!";
  2443. char reply[1024];
  2444. int input;
  2445. int msgSz = (int)XSTRLEN(msg);
  2446. int ret, err = 0;
  2447. int cipherSuite;
  2448. int sharedCtx = 0;
  2449. const char* cipherName1, *cipherName2;
  2450. #ifdef WOLFSSL_TIRTOS
  2451. fdOpenSession(Task_self());
  2452. #endif
  2453. ((func_args*)args)->return_code = TEST_FAIL;
  2454. cbf = ((func_args*)args)->callbacks;
  2455. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2456. if (cbf != NULL && cbf->ctx) {
  2457. ctx = cbf->ctx;
  2458. sharedCtx = cbf->isSharedCtx;
  2459. }
  2460. else
  2461. #endif
  2462. {
  2463. WOLFSSL_METHOD* method = NULL;
  2464. if (cbf != NULL && cbf->method != NULL) {
  2465. method = cbf->method();
  2466. }
  2467. else {
  2468. method = wolfSSLv23_client_method();
  2469. }
  2470. ctx = wolfSSL_CTX_new(method);
  2471. }
  2472. #ifdef WOLFSSL_ENCRYPTED_KEYS
  2473. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  2474. #endif
  2475. /* Do connect here so server detects failures */
  2476. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  2477. 0, 0, NULL);
  2478. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  2479. {
  2480. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  2481. goto done;
  2482. }
  2483. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2484. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  2485. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2486. #else
  2487. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  2488. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2489. #endif
  2490. /*err_sys("can't load client cert file, "
  2491. "Please run from wolfSSL home dir");*/
  2492. goto done;
  2493. }
  2494. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2495. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  2496. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2497. #else
  2498. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  2499. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2500. #endif
  2501. /*err_sys("can't load client key file, "
  2502. "Please run from wolfSSL home dir");*/
  2503. goto done;
  2504. }
  2505. /* call ctx setup callback */
  2506. if (cbf != NULL && cbf->ctx_ready != NULL) {
  2507. cbf->ctx_ready(ctx);
  2508. }
  2509. ssl = wolfSSL_new(ctx);
  2510. if (ssl == NULL) {
  2511. goto done;
  2512. }
  2513. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2514. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  2515. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2516. #else
  2517. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  2518. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2519. #endif
  2520. /*err_sys("can't load client cert file, "
  2521. "Please run from wolfSSL home dir");*/
  2522. goto done;
  2523. }
  2524. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2525. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  2526. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2527. #else
  2528. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  2529. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2530. #endif
  2531. /*err_sys("can't load client key file, "
  2532. "Please run from wolfSSL home dir");*/
  2533. goto done;
  2534. }
  2535. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  2536. /*err_sys("SSL_set_fd failed");*/
  2537. goto done;
  2538. }
  2539. /* call ssl setup callback */
  2540. if (cbf != NULL && cbf->ssl_ready != NULL) {
  2541. cbf->ssl_ready(ssl);
  2542. }
  2543. do {
  2544. #ifdef WOLFSSL_ASYNC_CRYPT
  2545. if (err == WC_PENDING_E) {
  2546. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  2547. if (ret < 0) { break; } else if (ret == 0) { continue; }
  2548. }
  2549. #endif
  2550. err = 0; /* Reset error */
  2551. ret = wolfSSL_connect(ssl);
  2552. if (ret != WOLFSSL_SUCCESS) {
  2553. err = wolfSSL_get_error(ssl, 0);
  2554. }
  2555. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  2556. if (ret != WOLFSSL_SUCCESS) {
  2557. char buff[WOLFSSL_MAX_ERROR_SZ];
  2558. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  2559. /*err_sys("SSL_connect failed");*/
  2560. goto done;
  2561. }
  2562. /* test the various get cipher methods */
  2563. /* Internal cipher suite names */
  2564. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  2565. cipherName1 = wolfSSL_get_cipher_name(ssl);
  2566. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  2567. (cipherSuite >> 8), cipherSuite & 0xFF);
  2568. AssertStrEQ(cipherName1, cipherName2);
  2569. /* IANA Cipher Suites Names */
  2570. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  2571. then it's the internal cipher suite name */
  2572. cipher = wolfSSL_get_current_cipher(ssl);
  2573. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  2574. cipherName2 = wolfSSL_get_cipher(ssl);
  2575. AssertStrEQ(cipherName1, cipherName2);
  2576. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  2577. !defined(WOLFSSL_QT)
  2578. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  2579. (cipherSuite >> 8), cipherSuite & 0xFF);
  2580. AssertStrEQ(cipherName1, cipherName2);
  2581. #endif
  2582. if (cb != NULL)
  2583. ((cbType)cb)(ctx, ssl);
  2584. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  2585. /*err_sys("SSL_write failed");*/
  2586. goto done;
  2587. }
  2588. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  2589. if (input > 0) {
  2590. reply[input] = '\0';
  2591. printf("Server response: %s\n", reply);
  2592. }
  2593. ((func_args*)args)->return_code = TEST_SUCCESS;
  2594. done:
  2595. wolfSSL_free(ssl);
  2596. if (!sharedCtx)
  2597. wolfSSL_CTX_free(ctx);
  2598. CloseSocket(sockfd);
  2599. #ifdef WOLFSSL_TIRTOS
  2600. fdCloseSession(Task_self());
  2601. #endif
  2602. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  2603. && defined(HAVE_THREAD_LS)
  2604. wc_ecc_fp_free(); /* free per thread cache */
  2605. #endif
  2606. return;
  2607. }
  2608. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  2609. static void test_client_reuse_WOLFSSLobj(void* args, void *cb, void* server_args)
  2610. {
  2611. SOCKET_T sockfd = 0;
  2612. callback_functions* cbf;
  2613. WOLFSSL_CTX* ctx = 0;
  2614. WOLFSSL* ssl = 0;
  2615. WOLFSSL_SESSION* session = NULL;
  2616. char msg[64] = "hello wolfssl!";
  2617. char reply[1024];
  2618. int input;
  2619. int msgSz = (int)XSTRLEN(msg);
  2620. int ret, err = 0;
  2621. int sharedCtx = 0;
  2622. #ifdef WOLFSSL_TIRTOS
  2623. fdOpenSession(Task_self());
  2624. #endif
  2625. ((func_args*)args)->return_code = TEST_FAIL;
  2626. cbf = ((func_args*)args)->callbacks;
  2627. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2628. if (cbf != NULL && cbf->ctx) {
  2629. ctx = cbf->ctx;
  2630. sharedCtx = 1;
  2631. }
  2632. else
  2633. #endif
  2634. {
  2635. WOLFSSL_METHOD* method = NULL;
  2636. if (cbf != NULL && cbf->method != NULL) {
  2637. method = cbf->method();
  2638. }
  2639. else {
  2640. method = wolfSSLv23_client_method();
  2641. }
  2642. ctx = wolfSSL_CTX_new(method);
  2643. }
  2644. #ifdef WOLFSSL_ENCRYPTED_KEYS
  2645. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  2646. #endif
  2647. /* Do connect here so server detects failures */
  2648. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  2649. 0, 0, NULL);
  2650. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  2651. {
  2652. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  2653. goto done;
  2654. }
  2655. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  2656. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2657. /*err_sys("can't load client cert file, "
  2658. "Please run from wolfSSL home dir");*/
  2659. goto done;
  2660. }
  2661. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  2662. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2663. /*err_sys("can't load client key file, "
  2664. "Please run from wolfSSL home dir");*/
  2665. goto done;
  2666. }
  2667. /* call ctx setup callback */
  2668. if (cbf != NULL && cbf->ctx_ready != NULL) {
  2669. cbf->ctx_ready(ctx);
  2670. }
  2671. ssl = wolfSSL_new(ctx);
  2672. if (ssl == NULL) {
  2673. goto done;
  2674. }
  2675. /* keep handshakre resources for re-using WOLFSSL obj */
  2676. wolfSSL_KeepArrays(ssl);
  2677. if(wolfSSL_KeepHandshakeResources(ssl)) {
  2678. /* err_sys("SSL_KeepHandshakeResources failed"); */
  2679. goto done;
  2680. }
  2681. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  2682. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2683. /*err_sys("can't load client cert file, "
  2684. "Please run from wolfSSL home dir");*/
  2685. goto done;
  2686. }
  2687. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  2688. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2689. /*err_sys("can't load client key file, "
  2690. "Please run from wolfSSL home dir");*/
  2691. goto done;
  2692. }
  2693. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  2694. /*err_sys("SSL_set_fd failed");*/
  2695. goto done;
  2696. }
  2697. /* call ssl setup callback */
  2698. if (cbf != NULL && cbf->ssl_ready != NULL) {
  2699. cbf->ssl_ready(ssl);
  2700. }
  2701. do {
  2702. #ifdef WOLFSSL_ASYNC_CRYPT
  2703. if (err == WC_PENDING_E) {
  2704. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  2705. if (ret < 0) { break; } else if (ret == 0) { continue; }
  2706. }
  2707. #endif
  2708. err = 0; /* Reset error */
  2709. ret = wolfSSL_connect(ssl);
  2710. if (ret != WOLFSSL_SUCCESS) {
  2711. err = wolfSSL_get_error(ssl, 0);
  2712. }
  2713. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  2714. if (ret != WOLFSSL_SUCCESS) {
  2715. char buff[WOLFSSL_MAX_ERROR_SZ];
  2716. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  2717. /*err_sys("SSL_connect failed");*/
  2718. goto done;
  2719. }
  2720. /* Build first session */
  2721. if (cb != NULL)
  2722. ((cbType)cb)(ctx, ssl);
  2723. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  2724. /*err_sys("SSL_write failed");*/
  2725. goto done;
  2726. }
  2727. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  2728. if (input > 0) {
  2729. reply[input] = '\0';
  2730. printf("Server response: %s\n", reply);
  2731. }
  2732. /* Session Resumption by re-using WOLFSSL object */
  2733. wolfSSL_set_quiet_shutdown(ssl, 1);
  2734. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  2735. /* err_sys ("SSL shutdown failed"); */
  2736. goto done;
  2737. }
  2738. session = wolfSSL_get_session(ssl);
  2739. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  2740. /* err_sys ("SSL_clear failed"); */
  2741. goto done;
  2742. }
  2743. wolfSSL_set_session(ssl, session);
  2744. /* close socket once */
  2745. CloseSocket(sockfd);
  2746. sockfd = 0;
  2747. /* wait until server ready */
  2748. wait_tcp_ready((func_args*)server_args);
  2749. printf("session resumption\n");
  2750. /* Do re-connect */
  2751. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  2752. 0, 0, NULL);
  2753. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  2754. /*err_sys("SSL_set_fd failed");*/
  2755. goto done;
  2756. }
  2757. do {
  2758. #ifdef WOLFSSL_ASYNC_CRYPT
  2759. if (err == WC_PENDING_E) {
  2760. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  2761. if (ret < 0) { break; } else if (ret == 0) { continue; }
  2762. }
  2763. #endif
  2764. err = 0; /* Reset error */
  2765. ret = wolfSSL_connect(ssl);
  2766. if (ret != WOLFSSL_SUCCESS) {
  2767. err = wolfSSL_get_error(ssl, 0);
  2768. }
  2769. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  2770. if (ret != WOLFSSL_SUCCESS) {
  2771. char buff[WOLFSSL_MAX_ERROR_SZ];
  2772. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  2773. /*err_sys("SSL_connect failed");*/
  2774. goto done;
  2775. }
  2776. /* Build first session */
  2777. if (cb != NULL)
  2778. ((cbType)cb)(ctx, ssl);
  2779. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  2780. /*err_sys("SSL_write failed");*/
  2781. goto done;
  2782. }
  2783. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  2784. if (input > 0) {
  2785. reply[input] = '\0';
  2786. printf("Server response: %s\n", reply);
  2787. }
  2788. ((func_args*)args)->return_code = TEST_SUCCESS;
  2789. done:
  2790. wolfSSL_free(ssl);
  2791. if (!sharedCtx)
  2792. wolfSSL_CTX_free(ctx);
  2793. CloseSocket(sockfd);
  2794. #ifdef WOLFSSL_TIRTOS
  2795. fdCloseSession(Task_self());
  2796. #endif
  2797. return;
  2798. }
  2799. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  2800. /* SNI / ALPN / session export helper functions */
  2801. #if defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLFSSL_SESSION_EXPORT)
  2802. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  2803. {
  2804. callback_functions* callbacks = ((func_args*)args)->callbacks;
  2805. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(callbacks->method());
  2806. WOLFSSL* ssl = NULL;
  2807. SOCKET_T sfd = 0;
  2808. SOCKET_T cfd = 0;
  2809. word16 port;
  2810. char msg[] = "I hear you fa shizzle!";
  2811. int len = (int) XSTRLEN(msg);
  2812. char input[1024];
  2813. int idx;
  2814. int ret, err = 0;
  2815. #ifdef WOLFSSL_TIRTOS
  2816. fdOpenSession(Task_self());
  2817. #endif
  2818. ((func_args*)args)->return_code = TEST_FAIL;
  2819. #if defined(USE_WINDOWS_API)
  2820. port = ((func_args*)args)->signal->port;
  2821. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  2822. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  2823. /* Let tcp_listen assign port */
  2824. port = 0;
  2825. #else
  2826. /* Use default port */
  2827. port = wolfSSLPort;
  2828. #endif
  2829. wolfSSL_CTX_set_verify(ctx,
  2830. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  2831. #ifdef WOLFSSL_ENCRYPTED_KEYS
  2832. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  2833. #endif
  2834. #ifdef WOLFSSL_SESSION_EXPORT
  2835. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_dtls_set_export(ctx, test_export));
  2836. #endif
  2837. AssertIntEQ(WOLFSSL_SUCCESS,
  2838. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  2839. AssertIntEQ(WOLFSSL_SUCCESS,
  2840. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  2841. WOLFSSL_FILETYPE_PEM));
  2842. AssertIntEQ(WOLFSSL_SUCCESS,
  2843. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  2844. if (callbacks->ctx_ready)
  2845. callbacks->ctx_ready(ctx);
  2846. ssl = wolfSSL_new(ctx);
  2847. if (wolfSSL_dtls(ssl)) {
  2848. SOCKADDR_IN_T cliAddr;
  2849. socklen_t cliLen;
  2850. cliLen = sizeof(cliAddr);
  2851. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0);
  2852. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  2853. (struct sockaddr*)&cliAddr, &cliLen);
  2854. AssertIntGT(idx, 0);
  2855. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  2856. }
  2857. else {
  2858. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1);
  2859. CloseSocket(sfd);
  2860. }
  2861. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  2862. #ifdef NO_PSK
  2863. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  2864. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  2865. #elif !defined(NO_DH)
  2866. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  2867. #endif
  2868. #endif
  2869. if (callbacks->ssl_ready)
  2870. callbacks->ssl_ready(ssl);
  2871. do {
  2872. #ifdef WOLFSSL_ASYNC_CRYPT
  2873. if (err == WC_PENDING_E) {
  2874. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  2875. if (ret < 0) { break; } else if (ret == 0) { continue; }
  2876. }
  2877. #endif
  2878. err = 0; /* Reset error */
  2879. ret = wolfSSL_accept(ssl);
  2880. if (ret != WOLFSSL_SUCCESS) {
  2881. err = wolfSSL_get_error(ssl, 0);
  2882. }
  2883. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  2884. if (ret != WOLFSSL_SUCCESS) {
  2885. char buff[WOLFSSL_MAX_ERROR_SZ];
  2886. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  2887. /*err_sys("SSL_accept failed");*/
  2888. }
  2889. else {
  2890. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  2891. input[idx] = 0;
  2892. printf("Client message: %s\n", input);
  2893. }
  2894. AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
  2895. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL)
  2896. if (wolfSSL_dtls(ssl)) {
  2897. byte* import;
  2898. word32 sz;
  2899. wolfSSL_dtls_export(ssl, NULL, &sz);
  2900. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2901. AssertNotNull(import);
  2902. idx = wolfSSL_dtls_export(ssl, import, &sz);
  2903. AssertIntGE(idx, 0);
  2904. AssertIntGE(wolfSSL_dtls_import(ssl, import, idx), 0);
  2905. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2906. }
  2907. #endif
  2908. #ifdef WOLFSSL_TIRTOS
  2909. Task_yield();
  2910. #endif
  2911. ((func_args*)args)->return_code = TEST_SUCCESS;
  2912. }
  2913. if (callbacks->on_result)
  2914. callbacks->on_result(ssl);
  2915. wolfSSL_shutdown(ssl);
  2916. wolfSSL_free(ssl);
  2917. wolfSSL_CTX_free(ctx);
  2918. CloseSocket(cfd);
  2919. #ifdef WOLFSSL_TIRTOS
  2920. fdCloseSession(Task_self());
  2921. #endif
  2922. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  2923. && defined(HAVE_THREAD_LS)
  2924. wc_ecc_fp_free(); /* free per thread cache */
  2925. #endif
  2926. #ifndef WOLFSSL_TIRTOS
  2927. return 0;
  2928. #endif
  2929. }
  2930. static void run_wolfssl_client(void* args)
  2931. {
  2932. callback_functions* callbacks = ((func_args*)args)->callbacks;
  2933. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(callbacks->method());
  2934. WOLFSSL* ssl = NULL;
  2935. SOCKET_T sfd = 0;
  2936. char msg[] = "hello wolfssl server!";
  2937. int len = (int) XSTRLEN(msg);
  2938. char input[1024];
  2939. int idx;
  2940. int ret, err = 0;
  2941. #ifdef WOLFSSL_TIRTOS
  2942. fdOpenSession(Task_self());
  2943. #endif
  2944. ((func_args*)args)->return_code = TEST_FAIL;
  2945. #ifdef WOLFSSL_ENCRYPTED_KEYS
  2946. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  2947. #endif
  2948. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  2949. AssertIntEQ(WOLFSSL_SUCCESS,
  2950. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, WOLFSSL_FILETYPE_PEM));
  2951. AssertIntEQ(WOLFSSL_SUCCESS,
  2952. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, WOLFSSL_FILETYPE_PEM));
  2953. if (callbacks->ctx_ready)
  2954. callbacks->ctx_ready(ctx);
  2955. ssl = wolfSSL_new(ctx);
  2956. if (wolfSSL_dtls(ssl)) {
  2957. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  2958. 1, 0, ssl);
  2959. }
  2960. else {
  2961. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  2962. 0, 0, ssl);
  2963. }
  2964. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  2965. if (callbacks->ssl_ready)
  2966. callbacks->ssl_ready(ssl);
  2967. do {
  2968. #ifdef WOLFSSL_ASYNC_CRYPT
  2969. if (err == WC_PENDING_E) {
  2970. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  2971. if (ret < 0) { break; } else if (ret == 0) { continue; }
  2972. }
  2973. #endif
  2974. err = 0; /* Reset error */
  2975. ret = wolfSSL_connect(ssl);
  2976. if (ret != WOLFSSL_SUCCESS) {
  2977. err = wolfSSL_get_error(ssl, 0);
  2978. }
  2979. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  2980. if (ret != WOLFSSL_SUCCESS) {
  2981. char buff[WOLFSSL_MAX_ERROR_SZ];
  2982. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  2983. /*err_sys("SSL_connect failed");*/
  2984. }
  2985. else {
  2986. AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
  2987. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  2988. input[idx] = 0;
  2989. printf("Server response: %s\n", input);
  2990. }
  2991. ((func_args*)args)->return_code = TEST_SUCCESS;
  2992. }
  2993. if (callbacks->on_result)
  2994. callbacks->on_result(ssl);
  2995. wolfSSL_free(ssl);
  2996. wolfSSL_CTX_free(ctx);
  2997. CloseSocket(sfd);
  2998. #ifdef WOLFSSL_TIRTOS
  2999. fdCloseSession(Task_self());
  3000. #endif
  3001. }
  3002. #endif /* defined(HAVE_SNI) || defined(HAVE_ALPN) ||
  3003. defined(WOLFSSL_SESSION_EXPORT) */
  3004. static void test_wolfSSL_read_write(void)
  3005. {
  3006. /* The unit testing for read and write shall happen simultaneously, since
  3007. * one can't do anything with one without the other. (Except for a failure
  3008. * test case.) This function will call all the others that will set up,
  3009. * execute, and report their test findings.
  3010. *
  3011. * Set up the success case first. This function will become the template
  3012. * for the other tests. This should eventually be renamed
  3013. *
  3014. * The success case isn't interesting, how can this fail?
  3015. * - Do not give the client context a CA certificate. The connect should
  3016. * fail. Do not need server for this?
  3017. * - Using NULL for the ssl object on server. Do not need client for this.
  3018. * - Using NULL for the ssl object on client. Do not need server for this.
  3019. * - Good ssl objects for client and server. Client write() without server
  3020. * read().
  3021. * - Good ssl objects for client and server. Server write() without client
  3022. * read().
  3023. * - Forgetting the password callback?
  3024. */
  3025. tcp_ready ready;
  3026. func_args client_args;
  3027. func_args server_args;
  3028. THREAD_TYPE serverThread;
  3029. XMEMSET(&client_args, 0, sizeof(func_args));
  3030. XMEMSET(&server_args, 0, sizeof(func_args));
  3031. #ifdef WOLFSSL_TIRTOS
  3032. fdOpenSession(Task_self());
  3033. #endif
  3034. StartTCP();
  3035. InitTcpReady(&ready);
  3036. #if defined(USE_WINDOWS_API)
  3037. /* use RNG to get random port if using windows */
  3038. ready.port = GetRandomPort();
  3039. #endif
  3040. server_args.signal = &ready;
  3041. client_args.signal = &ready;
  3042. start_thread(test_server_nofail, &server_args, &serverThread);
  3043. wait_tcp_ready(&server_args);
  3044. test_client_nofail(&client_args, NULL);
  3045. join_thread(serverThread);
  3046. AssertTrue(client_args.return_code);
  3047. AssertTrue(server_args.return_code);
  3048. FreeTcpReady(&ready);
  3049. #ifdef WOLFSSL_TIRTOS
  3050. fdOpenSession(Task_self());
  3051. #endif
  3052. }
  3053. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  3054. static void test_wolfSSL_reuse_WOLFSSLobj(void)
  3055. {
  3056. /* The unit test for session resumption by re-using WOLFSSL object.
  3057. * WOLFSSL object is not cleared after first session. It re-use the obeject
  3058. * for second connection.
  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. /* the var is used for loop number */
  3078. server_args.argc = 2;
  3079. start_thread(test_server_loop, &server_args, &serverThread);
  3080. wait_tcp_ready(&server_args);
  3081. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  3082. join_thread(serverThread);
  3083. AssertTrue(client_args.return_code);
  3084. AssertTrue(server_args.return_code);
  3085. FreeTcpReady(&ready);
  3086. #ifdef WOLFSSL_TIRTOS
  3087. fdOpenSession(Task_self());
  3088. #endif
  3089. }
  3090. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  3091. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  3092. /* canned export of a session using older version 3 */
  3093. static unsigned char version_3[] = {
  3094. 0xA5, 0xA3, 0x01, 0x87, 0x00, 0x3b, 0x00, 0x01,
  3095. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  3096. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  3097. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  3098. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  3099. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3100. 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30, 0x05,
  3101. 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05, 0xFE,
  3102. 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00,
  3103. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3104. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  3105. 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
  3106. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3107. 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00, 0x00,
  3108. 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  3109. 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00, 0x00,
  3110. 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01, 0x00,
  3111. 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  3112. 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F, 0x00,
  3113. 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00,
  3114. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3115. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3116. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3117. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3118. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3119. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3120. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3121. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3122. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3123. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3124. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3125. 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05, 0x12,
  3126. 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D, 0x31,
  3127. 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B, 0xF3,
  3128. 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98, 0x91,
  3129. 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0, 0x00,
  3130. 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74, 0xDF,
  3131. 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26, 0xA5,
  3132. 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9, 0xEF,
  3133. 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04, 0xAA,
  3134. 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80, 0x00,
  3135. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0C,
  3136. 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00, 0x00,
  3137. 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D, 0x92,
  3138. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3139. 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00, 0x10,
  3140. 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01, 0x30,
  3141. 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00, 0x09,
  3142. 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30, 0x2E,
  3143. 0x31, 0xED, 0x4F
  3144. };
  3145. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  3146. static void test_wolfSSL_dtls_export(void)
  3147. {
  3148. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  3149. tcp_ready ready;
  3150. func_args client_args;
  3151. func_args server_args;
  3152. THREAD_TYPE serverThread;
  3153. callback_functions server_cbf;
  3154. callback_functions client_cbf;
  3155. #ifdef WOLFSSL_TIRTOS
  3156. fdOpenSession(Task_self());
  3157. #endif
  3158. InitTcpReady(&ready);
  3159. #if defined(USE_WINDOWS_API)
  3160. /* use RNG to get random port if using windows */
  3161. ready.port = GetRandomPort();
  3162. #endif
  3163. /* set using dtls */
  3164. XMEMSET(&client_args, 0, sizeof(func_args));
  3165. XMEMSET(&server_args, 0, sizeof(func_args));
  3166. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  3167. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  3168. server_cbf.method = wolfDTLSv1_2_server_method;
  3169. client_cbf.method = wolfDTLSv1_2_client_method;
  3170. server_args.callbacks = &server_cbf;
  3171. client_args.callbacks = &client_cbf;
  3172. server_args.signal = &ready;
  3173. client_args.signal = &ready;
  3174. start_thread(run_wolfssl_server, &server_args, &serverThread);
  3175. wait_tcp_ready(&server_args);
  3176. run_wolfssl_client(&client_args);
  3177. join_thread(serverThread);
  3178. AssertTrue(client_args.return_code);
  3179. AssertTrue(server_args.return_code);
  3180. FreeTcpReady(&ready);
  3181. #ifdef WOLFSSL_TIRTOS
  3182. fdOpenSession(Task_self());
  3183. #endif
  3184. {
  3185. SOCKET_T sockfd = 0;
  3186. WOLFSSL_CTX* ctx;
  3187. WOLFSSL* ssl;
  3188. char msg[64] = "hello wolfssl!";
  3189. char reply[1024];
  3190. int msgSz = (int)XSTRLEN(msg);
  3191. byte *session, *window;
  3192. unsigned int sessionSz, windowSz;
  3193. struct sockaddr_in peerAddr;
  3194. int i;
  3195. /* Set ctx to DTLS 1.2 */
  3196. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  3197. AssertNotNull(ssl = wolfSSL_new(ctx));
  3198. /* test importing version 3 */
  3199. AssertIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  3200. /* test importing bad length and bad version */
  3201. version_3[2] += 1;
  3202. AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  3203. version_3[2] -= 1; version_3[1] = 0XA0;
  3204. AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  3205. wolfSSL_free(ssl);
  3206. wolfSSL_CTX_free(ctx);
  3207. /* check storing client state after connection and storing window only */
  3208. #ifdef WOLFSSL_TIRTOS
  3209. fdOpenSession(Task_self());
  3210. #endif
  3211. InitTcpReady(&ready);
  3212. #if defined(USE_WINDOWS_API)
  3213. /* use RNG to get random port if using windows */
  3214. ready.port = GetRandomPort();
  3215. #endif
  3216. /* set using dtls */
  3217. XMEMSET(&server_args, 0, sizeof(func_args));
  3218. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  3219. server_cbf.method = wolfDTLSv1_2_server_method;
  3220. server_args.callbacks = &server_cbf;
  3221. server_args.argc = 3; /* set loop_count to 3 */
  3222. server_args.signal = &ready;
  3223. start_thread(test_server_nofail, &server_args, &serverThread);
  3224. wait_tcp_ready(&server_args);
  3225. /* create and connect with client */
  3226. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  3227. AssertIntEQ(WOLFSSL_SUCCESS,
  3228. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  3229. AssertIntEQ(WOLFSSL_SUCCESS,
  3230. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  3231. AssertIntEQ(WOLFSSL_SUCCESS,
  3232. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  3233. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  3234. AssertNotNull(ssl = wolfSSL_new(ctx));
  3235. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  3236. /* store server information connected too */
  3237. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  3238. peerAddr.sin_family = AF_INET;
  3239. peerAddr.sin_port = XHTONS(server_args.signal->port);
  3240. wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr));
  3241. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  3242. AssertIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  3243. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  3244. AssertIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  3245. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  3246. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  3247. AssertIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  3248. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  3249. AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  3250. wolfSSL_free(ssl);
  3251. for (i = 1; i < server_args.argc; i++) {
  3252. /* restore state */
  3253. AssertNotNull(ssl = wolfSSL_new(ctx));
  3254. AssertIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  3255. AssertIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  3256. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  3257. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  3258. AssertIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  3259. AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  3260. wolfSSL_free(ssl);
  3261. }
  3262. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  3263. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  3264. wolfSSL_CTX_free(ctx);
  3265. printf("done and waiting for server\n");
  3266. join_thread(serverThread);
  3267. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  3268. FreeTcpReady(&ready);
  3269. #ifdef WOLFSSL_TIRTOS
  3270. fdOpenSession(Task_self());
  3271. #endif
  3272. }
  3273. printf(testingFmt, "wolfSSL_dtls_export()");
  3274. printf(resultFmt, passed);
  3275. #endif
  3276. }
  3277. /*----------------------------------------------------------------------------*
  3278. | TLS extensions tests
  3279. *----------------------------------------------------------------------------*/
  3280. #if defined(HAVE_SNI) || defined(HAVE_ALPN)
  3281. /* connection test runner */
  3282. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  3283. callback_functions* server_callbacks)
  3284. {
  3285. tcp_ready ready;
  3286. func_args client_args;
  3287. func_args server_args;
  3288. THREAD_TYPE serverThread;
  3289. XMEMSET(&client_args, 0, sizeof(func_args));
  3290. XMEMSET(&server_args, 0, sizeof(func_args));
  3291. StartTCP();
  3292. client_args.callbacks = client_callbacks;
  3293. server_args.callbacks = server_callbacks;
  3294. #ifdef WOLFSSL_TIRTOS
  3295. fdOpenSession(Task_self());
  3296. #endif
  3297. /* RUN Server side */
  3298. InitTcpReady(&ready);
  3299. #if defined(USE_WINDOWS_API)
  3300. /* use RNG to get random port if using windows */
  3301. ready.port = GetRandomPort();
  3302. #endif
  3303. server_args.signal = &ready;
  3304. client_args.signal = &ready;
  3305. start_thread(run_wolfssl_server, &server_args, &serverThread);
  3306. wait_tcp_ready(&server_args);
  3307. /* RUN Client side */
  3308. run_wolfssl_client(&client_args);
  3309. join_thread(serverThread);
  3310. FreeTcpReady(&ready);
  3311. #ifdef WOLFSSL_TIRTOS
  3312. fdCloseSession(Task_self());
  3313. #endif
  3314. }
  3315. #endif /* defined(HAVE_SNI) || defined(HAVE_ALPN) */
  3316. #ifdef HAVE_SNI
  3317. static void test_wolfSSL_UseSNI_params(void)
  3318. {
  3319. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3320. WOLFSSL *ssl = wolfSSL_new(ctx);
  3321. AssertNotNull(ctx);
  3322. AssertNotNull(ssl);
  3323. /* invalid [ctx|ssl] */
  3324. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  3325. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  3326. /* invalid type */
  3327. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  3328. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  3329. /* invalid data */
  3330. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  3331. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  3332. /* success case */
  3333. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  3334. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  3335. wolfSSL_free(ssl);
  3336. wolfSSL_CTX_free(ctx);
  3337. }
  3338. /* BEGIN of connection tests callbacks */
  3339. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  3340. {
  3341. AssertIntEQ(WOLFSSL_SUCCESS,
  3342. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  3343. }
  3344. static void use_SNI_at_ssl(WOLFSSL* ssl)
  3345. {
  3346. AssertIntEQ(WOLFSSL_SUCCESS,
  3347. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  3348. }
  3349. static void different_SNI_at_ssl(WOLFSSL* ssl)
  3350. {
  3351. AssertIntEQ(WOLFSSL_SUCCESS,
  3352. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  3353. }
  3354. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  3355. {
  3356. use_SNI_at_ssl(ssl);
  3357. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  3358. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  3359. }
  3360. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  3361. {
  3362. use_SNI_at_ssl(ssl);
  3363. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  3364. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  3365. }
  3366. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  3367. {
  3368. use_SNI_at_ctx(ctx);
  3369. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  3370. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  3371. }
  3372. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  3373. {
  3374. use_SNI_at_ssl(ssl);
  3375. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  3376. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  3377. }
  3378. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  3379. {
  3380. use_SNI_at_ctx(ctx);
  3381. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  3382. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  3383. }
  3384. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  3385. {
  3386. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  3387. }
  3388. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  3389. {
  3390. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  3391. }
  3392. static void verify_SNI_no_matching(WOLFSSL* ssl)
  3393. {
  3394. byte type = WOLFSSL_SNI_HOST_NAME;
  3395. char* request = (char*) &type; /* to be overwritten */
  3396. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  3397. AssertNotNull(request);
  3398. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
  3399. AssertNull(request);
  3400. }
  3401. static void verify_SNI_real_matching(WOLFSSL* ssl)
  3402. {
  3403. byte type = WOLFSSL_SNI_HOST_NAME;
  3404. char* request = NULL;
  3405. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  3406. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
  3407. AssertNotNull(request);
  3408. AssertStrEQ("www.wolfssl.com", request);
  3409. }
  3410. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  3411. {
  3412. byte type = WOLFSSL_SNI_HOST_NAME;
  3413. char* request = NULL;
  3414. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  3415. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
  3416. AssertNotNull(request);
  3417. AssertStrEQ("ww2.wolfssl.com", request);
  3418. }
  3419. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  3420. {
  3421. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  3422. }
  3423. /* END of connection tests callbacks */
  3424. static void test_wolfSSL_UseSNI_connection(void)
  3425. {
  3426. unsigned long i;
  3427. callback_functions callbacks[] = {
  3428. /* success case at ctx */
  3429. {0, use_SNI_at_ctx, 0, 0, 0, 0},
  3430. {0, use_SNI_at_ctx, 0, verify_SNI_real_matching, 0, 0},
  3431. /* success case at ssl */
  3432. {0, 0, use_SNI_at_ssl, verify_SNI_real_matching, 0, 0},
  3433. {0, 0, use_SNI_at_ssl, verify_SNI_real_matching, 0, 0},
  3434. /* default mismatch behavior */
  3435. {0, 0, different_SNI_at_ssl, verify_FATAL_ERROR_on_client, 0, 0},
  3436. {0, 0, use_SNI_at_ssl, verify_UNKNOWN_SNI_on_server, 0, 0},
  3437. /* continue on mismatch */
  3438. {0, 0, different_SNI_at_ssl, 0, 0, 0},
  3439. {0, 0, use_SNI_WITH_CONTINUE_at_ssl, verify_SNI_no_matching, 0, 0},
  3440. /* fake answer on mismatch */
  3441. {0, 0, different_SNI_at_ssl, 0, 0, 0},
  3442. {0, 0, use_SNI_WITH_FAKE_ANSWER_at_ssl, verify_SNI_fake_matching, 0, 0},
  3443. /* sni abort - success */
  3444. {0, use_SNI_at_ctx, 0, 0, 0, 0},
  3445. {0, use_MANDATORY_SNI_at_ctx, 0, verify_SNI_real_matching, 0, 0},
  3446. /* sni abort - abort when absent (ctx) */
  3447. {0, 0, 0, verify_FATAL_ERROR_on_client, 0, 0},
  3448. {0, use_MANDATORY_SNI_at_ctx, 0, verify_SNI_ABSENT_on_server, 0, 0},
  3449. /* sni abort - abort when absent (ssl) */
  3450. {0, 0, 0, verify_FATAL_ERROR_on_client, 0, 0},
  3451. {0, 0, use_MANDATORY_SNI_at_ssl, verify_SNI_ABSENT_on_server, 0, 0},
  3452. /* sni abort - success when overwritten */
  3453. {0, 0, 0, 0, 0, 0},
  3454. {0, use_MANDATORY_SNI_at_ctx, use_SNI_at_ssl, verify_SNI_no_matching, 0, 0},
  3455. /* sni abort - success when allowing mismatches */
  3456. {0, 0, different_SNI_at_ssl, 0, 0, 0},
  3457. {0, use_PSEUDO_MANDATORY_SNI_at_ctx, 0, verify_SNI_fake_matching, 0, 0},
  3458. };
  3459. for (i = 0; i < sizeof(callbacks) / sizeof(callback_functions); i += 2) {
  3460. callbacks[i ].method = wolfSSLv23_client_method;
  3461. callbacks[i + 1].method = wolfSSLv23_server_method;
  3462. test_wolfSSL_client_server(&callbacks[i], &callbacks[i + 1]);
  3463. }
  3464. }
  3465. static void test_wolfSSL_SNI_GetFromBuffer(void)
  3466. {
  3467. byte buffer[] = { /* www.paypal.com */
  3468. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  3469. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  3470. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  3471. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  3472. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  3473. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  3474. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  3475. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  3476. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  3477. };
  3478. byte buffer2[] = { /* api.textmate.org */
  3479. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  3480. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  3481. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  3482. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  3483. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  3484. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  3485. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  3486. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  3487. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  3488. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  3489. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  3490. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  3491. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  3492. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  3493. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  3494. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  3495. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  3496. };
  3497. byte buffer3[] = { /* no sni extension */
  3498. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  3499. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  3500. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  3501. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  3502. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  3503. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  3504. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  3505. };
  3506. byte buffer4[] = { /* last extension has zero size */
  3507. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  3508. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  3509. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  3510. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  3511. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  3512. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  3513. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  3514. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  3515. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  3516. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  3517. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  3518. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  3519. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  3520. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  3521. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  3522. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  3523. 0x12, 0x00, 0x00
  3524. };
  3525. byte buffer5[] = { /* SSL v2.0 client hello */
  3526. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  3527. /* dummy bytes bellow, just to pass size check */
  3528. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  3529. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  3530. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  3531. };
  3532. byte result[32] = {0};
  3533. word32 length = 32;
  3534. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buffer4, sizeof(buffer4),
  3535. 0, result, &length));
  3536. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buffer3, sizeof(buffer3),
  3537. 0, result, &length));
  3538. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buffer2, sizeof(buffer2),
  3539. 1, result, &length));
  3540. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buffer, sizeof(buffer),
  3541. 0, result, &length));
  3542. buffer[0] = 0x16;
  3543. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buffer, sizeof(buffer),
  3544. 0, result, &length));
  3545. buffer[1] = 0x03;
  3546. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buffer,
  3547. sizeof(buffer), 0, result, &length));
  3548. buffer[2] = 0x03;
  3549. AssertIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buffer,
  3550. sizeof(buffer), 0, result, &length));
  3551. buffer[4] = 0x64;
  3552. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buffer, sizeof(buffer),
  3553. 0, result, &length));
  3554. result[length] = 0;
  3555. AssertStrEQ("www.paypal.com", (const char*) result);
  3556. length = 32;
  3557. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buffer2, sizeof(buffer2),
  3558. 0, result, &length));
  3559. result[length] = 0;
  3560. AssertStrEQ("api.textmate.org", (const char*) result);
  3561. /* SSL v2.0 tests */
  3562. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buffer5,
  3563. sizeof(buffer5), 0, result, &length));
  3564. buffer5[2] = 0x02;
  3565. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buffer5,
  3566. sizeof(buffer5), 0, result, &length));
  3567. buffer5[2] = 0x01; buffer5[6] = 0x08;
  3568. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buffer5,
  3569. sizeof(buffer5), 0, result, &length));
  3570. buffer5[6] = 0x09; buffer5[8] = 0x01;
  3571. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buffer5,
  3572. sizeof(buffer5), 0, result, &length));
  3573. }
  3574. #endif /* HAVE_SNI */
  3575. static void test_wolfSSL_UseSNI(void)
  3576. {
  3577. #ifdef HAVE_SNI
  3578. test_wolfSSL_UseSNI_params();
  3579. test_wolfSSL_UseSNI_connection();
  3580. test_wolfSSL_SNI_GetFromBuffer();
  3581. #endif
  3582. }
  3583. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  3584. static void test_wolfSSL_UseTrustedCA(void)
  3585. {
  3586. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  3587. WOLFSSL_CTX *ctx;
  3588. WOLFSSL *ssl;
  3589. byte id[20];
  3590. #ifndef NO_WOLFSSL_SERVER
  3591. AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  3592. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  3593. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  3594. #else
  3595. AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  3596. #endif
  3597. AssertNotNull((ssl = wolfSSL_new(ctx)));
  3598. XMEMSET(id, 0, sizeof(id));
  3599. /* error cases */
  3600. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  3601. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  3602. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  3603. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  3604. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  3605. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  3606. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  3607. #ifdef NO_SHA
  3608. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  3609. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  3610. #endif
  3611. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  3612. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  3613. /* success cases */
  3614. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  3615. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  3616. #ifndef NO_SHA
  3617. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  3618. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  3619. #endif
  3620. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  3621. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  3622. wolfSSL_free(ssl);
  3623. wolfSSL_CTX_free(ctx);
  3624. #endif /* HAVE_TRUSTED_CA */
  3625. }
  3626. static void test_wolfSSL_UseMaxFragment(void)
  3627. {
  3628. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  3629. #ifndef NO_WOLFSSL_SERVER
  3630. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3631. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  3632. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  3633. #else
  3634. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3635. #endif
  3636. WOLFSSL *ssl = wolfSSL_new(ctx);
  3637. AssertNotNull(ctx);
  3638. AssertNotNull(ssl);
  3639. /* error cases */
  3640. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  3641. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  3642. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  3643. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  3644. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  3645. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  3646. /* success case */
  3647. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  3648. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  3649. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  3650. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  3651. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  3652. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  3653. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  3654. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  3655. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  3656. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  3657. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  3658. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  3659. wolfSSL_free(ssl);
  3660. wolfSSL_CTX_free(ctx);
  3661. #endif
  3662. }
  3663. static void test_wolfSSL_UseTruncatedHMAC(void)
  3664. {
  3665. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  3666. #ifndef NO_WOLFSSL_SERVER
  3667. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3668. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  3669. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  3670. #else
  3671. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3672. #endif
  3673. WOLFSSL *ssl = wolfSSL_new(ctx);
  3674. AssertNotNull(ctx);
  3675. AssertNotNull(ssl);
  3676. /* error cases */
  3677. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  3678. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  3679. /* success case */
  3680. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  3681. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  3682. wolfSSL_free(ssl);
  3683. wolfSSL_CTX_free(ctx);
  3684. #endif
  3685. }
  3686. static void test_wolfSSL_UseSupportedCurve(void)
  3687. {
  3688. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && !defined(NO_TLS)
  3689. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3690. WOLFSSL *ssl = wolfSSL_new(ctx);
  3691. AssertNotNull(ctx);
  3692. AssertNotNull(ssl);
  3693. /* error cases */
  3694. AssertIntNE(WOLFSSL_SUCCESS,
  3695. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  3696. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  3697. AssertIntNE(WOLFSSL_SUCCESS,
  3698. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  3699. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  3700. /* success case */
  3701. AssertIntEQ(WOLFSSL_SUCCESS,
  3702. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  3703. AssertIntEQ(WOLFSSL_SUCCESS,
  3704. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  3705. wolfSSL_free(ssl);
  3706. wolfSSL_CTX_free(ctx);
  3707. #endif
  3708. }
  3709. #if defined(HAVE_ALPN) && !defined(NO_WOLFSSL_SERVER) && \
  3710. defined(HAVE_IO_TESTS_DEPENDENCIES)
  3711. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  3712. {
  3713. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  3714. }
  3715. static void use_ALPN_all(WOLFSSL* ssl)
  3716. {
  3717. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  3718. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  3719. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  3720. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  3721. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  3722. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  3723. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  3724. }
  3725. static void use_ALPN_all_continue(WOLFSSL* ssl)
  3726. {
  3727. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  3728. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  3729. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  3730. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  3731. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  3732. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  3733. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  3734. }
  3735. static void use_ALPN_one(WOLFSSL* ssl)
  3736. {
  3737. /* spdy/2 */
  3738. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  3739. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  3740. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  3741. }
  3742. static void use_ALPN_unknown(WOLFSSL* ssl)
  3743. {
  3744. /* http/2.0 */
  3745. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  3746. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  3747. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  3748. }
  3749. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  3750. {
  3751. /* http/2.0 */
  3752. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  3753. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  3754. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  3755. }
  3756. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  3757. {
  3758. /* spdy/3 */
  3759. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  3760. char *proto = NULL;
  3761. word16 protoSz = 0;
  3762. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  3763. /* check value */
  3764. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  3765. if (proto) {
  3766. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  3767. }
  3768. }
  3769. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  3770. {
  3771. char *proto = NULL;
  3772. word16 protoSz = 0;
  3773. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  3774. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  3775. /* check value */
  3776. AssertIntEQ(1, (0 == protoSz));
  3777. AssertIntEQ(1, (NULL == proto));
  3778. }
  3779. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  3780. {
  3781. /* http/1.1 */
  3782. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  3783. char *proto;
  3784. word16 protoSz = 0;
  3785. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  3786. /* check value */
  3787. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  3788. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  3789. }
  3790. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  3791. {
  3792. /* spdy/2 */
  3793. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  3794. char *proto;
  3795. word16 protoSz = 0;
  3796. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  3797. /* check value */
  3798. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  3799. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  3800. }
  3801. static void verify_ALPN_client_list(WOLFSSL* ssl)
  3802. {
  3803. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  3804. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  3805. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  3806. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  3807. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  3808. char *clist = NULL;
  3809. word16 clistSz = 0;
  3810. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  3811. &clistSz));
  3812. /* check value */
  3813. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  3814. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  3815. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  3816. }
  3817. static void test_wolfSSL_UseALPN_connection(void)
  3818. {
  3819. unsigned long i;
  3820. callback_functions callbacks[] = {
  3821. /* success case same list */
  3822. {0, 0, use_ALPN_all, 0, 0, 0},
  3823. {0, 0, use_ALPN_all, verify_ALPN_matching_http1, 0, 0},
  3824. /* success case only one for server */
  3825. {0, 0, use_ALPN_all, 0, 0, 0},
  3826. {0, 0, use_ALPN_one, verify_ALPN_matching_spdy2, 0, 0},
  3827. /* success case only one for client */
  3828. {0, 0, use_ALPN_one, 0, 0, 0},
  3829. {0, 0, use_ALPN_all, verify_ALPN_matching_spdy2, 0, 0},
  3830. /* success case none for client */
  3831. {0, 0, 0, 0, 0, 0},
  3832. {0, 0, use_ALPN_all, 0, 0, 0},
  3833. /* success case mismatch behavior but option 'continue' set */
  3834. {0, 0, use_ALPN_all_continue, verify_ALPN_not_matching_continue, 0, 0},
  3835. {0, 0, use_ALPN_unknown_continue, 0, 0, 0},
  3836. /* success case read protocol send by client */
  3837. {0, 0, use_ALPN_all, 0, 0, 0},
  3838. {0, 0, use_ALPN_one, verify_ALPN_client_list, 0, 0},
  3839. /* mismatch behavior with same list
  3840. * the first and only this one must be taken */
  3841. {0, 0, use_ALPN_all, 0, 0, 0},
  3842. {0, 0, use_ALPN_all, verify_ALPN_not_matching_spdy3, 0, 0},
  3843. /* default mismatch behavior */
  3844. {0, 0, use_ALPN_all, 0, 0, 0},
  3845. {0, 0, use_ALPN_unknown, verify_ALPN_FATAL_ERROR_on_client, 0, 0},
  3846. };
  3847. for (i = 0; i < sizeof(callbacks) / sizeof(callback_functions); i += 2) {
  3848. callbacks[i ].method = wolfSSLv23_client_method;
  3849. callbacks[i + 1].method = wolfSSLv23_server_method;
  3850. test_wolfSSL_client_server(&callbacks[i], &callbacks[i + 1]);
  3851. }
  3852. }
  3853. static void test_wolfSSL_UseALPN_params(void)
  3854. {
  3855. #ifndef NO_WOLFSSL_CLIENT
  3856. /* "http/1.1" */
  3857. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  3858. /* "spdy/1" */
  3859. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  3860. /* "spdy/2" */
  3861. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  3862. /* "spdy/3" */
  3863. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  3864. char buff[256];
  3865. word32 idx;
  3866. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3867. WOLFSSL *ssl = wolfSSL_new(ctx);
  3868. AssertNotNull(ctx);
  3869. AssertNotNull(ssl);
  3870. /* error cases */
  3871. AssertIntNE(WOLFSSL_SUCCESS,
  3872. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  3873. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  3874. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  3875. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  3876. /* success case */
  3877. /* http1 only */
  3878. AssertIntEQ(WOLFSSL_SUCCESS,
  3879. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  3880. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  3881. /* http1, spdy1 */
  3882. XMEMCPY(buff, http1, sizeof(http1));
  3883. idx = sizeof(http1);
  3884. buff[idx++] = ',';
  3885. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  3886. idx += sizeof(spdy1);
  3887. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  3888. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  3889. /* http1, spdy2, spdy1 */
  3890. XMEMCPY(buff, http1, sizeof(http1));
  3891. idx = sizeof(http1);
  3892. buff[idx++] = ',';
  3893. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  3894. idx += sizeof(spdy2);
  3895. buff[idx++] = ',';
  3896. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  3897. idx += sizeof(spdy1);
  3898. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  3899. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  3900. /* spdy3, http1, spdy2, spdy1 */
  3901. XMEMCPY(buff, spdy3, sizeof(spdy3));
  3902. idx = sizeof(spdy3);
  3903. buff[idx++] = ',';
  3904. XMEMCPY(buff+idx, http1, sizeof(http1));
  3905. idx += sizeof(http1);
  3906. buff[idx++] = ',';
  3907. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  3908. idx += sizeof(spdy2);
  3909. buff[idx++] = ',';
  3910. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  3911. idx += sizeof(spdy1);
  3912. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  3913. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  3914. wolfSSL_free(ssl);
  3915. wolfSSL_CTX_free(ctx);
  3916. #endif
  3917. }
  3918. #endif /* HAVE_ALPN */
  3919. static void test_wolfSSL_UseALPN(void)
  3920. {
  3921. #if defined(HAVE_ALPN) && !defined(NO_WOLFSSL_SERVER) &&\
  3922. defined(HAVE_IO_TESTS_DEPENDENCIES)
  3923. test_wolfSSL_UseALPN_connection();
  3924. test_wolfSSL_UseALPN_params();
  3925. #endif
  3926. }
  3927. static void test_wolfSSL_DisableExtendedMasterSecret(void)
  3928. {
  3929. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  3930. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3931. WOLFSSL *ssl = wolfSSL_new(ctx);
  3932. AssertNotNull(ctx);
  3933. AssertNotNull(ssl);
  3934. /* error cases */
  3935. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  3936. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  3937. /* success cases */
  3938. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  3939. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  3940. wolfSSL_free(ssl);
  3941. wolfSSL_CTX_free(ctx);
  3942. #endif
  3943. }
  3944. static void test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  3945. {
  3946. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  3947. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3948. WOLFSSL *ssl = wolfSSL_new(ctx);
  3949. AssertNotNull(ctx);
  3950. AssertNotNull(ssl);
  3951. /* error cases */
  3952. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  3953. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  3954. /* success cases */
  3955. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  3956. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  3957. wolfSSL_free(ssl);
  3958. wolfSSL_CTX_free(ctx);
  3959. #endif
  3960. }
  3961. /*----------------------------------------------------------------------------*
  3962. | X509 Tests
  3963. *----------------------------------------------------------------------------*/
  3964. static void test_wolfSSL_X509_NAME_get_entry(void)
  3965. {
  3966. #if !defined(NO_CERTS) && !defined(NO_RSA)
  3967. #if defined(OPENSSL_ALL) || \
  3968. (defined(OPENSSL_EXTRA) && \
  3969. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  3970. printf(testingFmt, "wolfSSL_X509_NAME_get_entry()");
  3971. {
  3972. /* use openssl like name to test mapping */
  3973. X509_NAME_ENTRY* ne;
  3974. X509_NAME* name;
  3975. X509* x509;
  3976. #ifndef NO_FILESYSTEM
  3977. ASN1_STRING* asn;
  3978. char* subCN = NULL;
  3979. #endif
  3980. int idx;
  3981. ASN1_OBJECT *object = NULL;
  3982. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)
  3983. BIO* bio;
  3984. #endif
  3985. #ifndef NO_FILESYSTEM
  3986. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  3987. AssertNotNull(x509);
  3988. name = X509_get_subject_name(x509);
  3989. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  3990. AssertIntGE(idx, 0);
  3991. ne = X509_NAME_get_entry(name, idx);
  3992. AssertNotNull(ne);
  3993. asn = X509_NAME_ENTRY_get_data(ne);
  3994. AssertNotNull(asn);
  3995. subCN = (char*)ASN1_STRING_data(asn);
  3996. AssertNotNull(subCN);
  3997. wolfSSL_FreeX509(x509);
  3998. #endif
  3999. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  4000. AssertNotNull(x509);
  4001. name = X509_get_subject_name(x509);
  4002. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  4003. AssertIntGE(idx, 0);
  4004. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)
  4005. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  4006. AssertIntEQ(X509_NAME_print_ex(bio, name, 4,
  4007. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  4008. BIO_free(bio);
  4009. #endif
  4010. ne = X509_NAME_get_entry(name, idx);
  4011. AssertNotNull(ne);
  4012. AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
  4013. wolfSSL_FreeX509(x509);
  4014. }
  4015. printf(resultFmt, passed);
  4016. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  4017. #endif /* !NO_CERTS && !NO_RSA */
  4018. }
  4019. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  4020. static void test_wolfSSL_PKCS12(void)
  4021. {
  4022. /* .p12 file is encrypted with DES3 */
  4023. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  4024. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  4025. * Password Key
  4026. */
  4027. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  4028. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  4029. !defined(NO_SHA)
  4030. byte buffer[6000];
  4031. char file[] = "./certs/test-servercert.p12";
  4032. char order[] = "./certs/ecc-rsa-server.p12";
  4033. char pass[] = "a password";
  4034. #ifdef HAVE_ECC
  4035. WOLFSSL_X509_NAME* subject;
  4036. WOLFSSL_X509 *x509;
  4037. #endif
  4038. XFILE f;
  4039. int bytes, ret;
  4040. WOLFSSL_BIO *bio;
  4041. WOLFSSL_EVP_PKEY *pkey;
  4042. WC_PKCS12 *pkcs12;
  4043. WC_PKCS12 *pkcs12_2;
  4044. WOLFSSL_X509 *cert;
  4045. WOLFSSL_X509 *tmp;
  4046. WOLF_STACK_OF(WOLFSSL_X509) *ca;
  4047. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  4048. || defined(WOLFSSL_NGINX)
  4049. WOLFSSL_CTX *ctx;
  4050. WOLFSSL *ssl;
  4051. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  4052. #endif
  4053. printf(testingFmt, "wolfSSL_PKCS12()");
  4054. f = XFOPEN(file, "rb");
  4055. AssertTrue((f != XBADFILE));
  4056. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  4057. XFCLOSE(f);
  4058. bio = BIO_new_mem_buf((void*)buffer, bytes);
  4059. AssertNotNull(bio);
  4060. pkcs12 = d2i_PKCS12_bio(bio, NULL);
  4061. AssertNotNull(pkcs12);
  4062. PKCS12_free(pkcs12);
  4063. d2i_PKCS12_bio(bio, &pkcs12);
  4064. AssertNotNull(pkcs12);
  4065. BIO_free(bio);
  4066. /* check verify MAC fail case */
  4067. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  4068. AssertIntEQ(ret, 0);
  4069. AssertNull(pkey);
  4070. AssertNull(cert);
  4071. /* check parse with no extra certs kept */
  4072. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  4073. AssertIntEQ(ret, 1);
  4074. AssertNotNull(pkey);
  4075. AssertNotNull(cert);
  4076. wolfSSL_EVP_PKEY_free(pkey);
  4077. wolfSSL_X509_free(cert);
  4078. /* check parse with extra certs kept */
  4079. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  4080. AssertIntEQ(ret, 1);
  4081. AssertNotNull(pkey);
  4082. AssertNotNull(cert);
  4083. AssertNotNull(ca);
  4084. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  4085. || defined(WOLFSSL_NGINX)
  4086. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  4087. #ifndef NO_WOLFSSL_CLIENT
  4088. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4089. #else
  4090. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4091. #endif
  4092. /* Copy stack structure */
  4093. AssertNotNull(tmp_ca = sk_X509_dup(ca));
  4094. AssertIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  4095. /* CTX now owns the tmp_ca stack structure */
  4096. tmp_ca = NULL;
  4097. AssertIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  4098. AssertNotNull(tmp_ca);
  4099. AssertIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  4100. /* Check that the main cert is also set */
  4101. AssertNotNull(ssl = SSL_new(ctx));
  4102. AssertNotNull(SSL_get_certificate(ssl));
  4103. SSL_free(ssl);
  4104. SSL_CTX_free(ctx);
  4105. #endif
  4106. /* should be 2 other certs on stack */
  4107. tmp = sk_X509_pop(ca);
  4108. AssertNotNull(tmp);
  4109. X509_free(tmp);
  4110. tmp = sk_X509_pop(ca);
  4111. AssertNotNull(tmp);
  4112. X509_free(tmp);
  4113. AssertNull(sk_X509_pop(ca));
  4114. EVP_PKEY_free(pkey);
  4115. X509_free(cert);
  4116. sk_X509_pop_free(ca, X509_free);
  4117. /* check PKCS12_create */
  4118. AssertNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  4119. AssertIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  4120. SSL_SUCCESS);
  4121. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  4122. -1, -1, 100, -1, 0)));
  4123. EVP_PKEY_free(pkey);
  4124. X509_free(cert);
  4125. sk_X509_free(ca);
  4126. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  4127. SSL_SUCCESS);
  4128. PKCS12_free(pkcs12_2);
  4129. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  4130. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  4131. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  4132. 2000, 1, 0)));
  4133. EVP_PKEY_free(pkey);
  4134. X509_free(cert);
  4135. sk_X509_free(ca);
  4136. /* convert to DER then back and parse */
  4137. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  4138. AssertIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  4139. PKCS12_free(pkcs12_2);
  4140. AssertNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  4141. BIO_free(bio);
  4142. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  4143. SSL_SUCCESS);
  4144. /* should be 2 other certs on stack */
  4145. tmp = sk_X509_pop(ca);
  4146. AssertNotNull(tmp);
  4147. X509_free(tmp);
  4148. tmp = sk_X509_pop(ca);
  4149. AssertNotNull(tmp);
  4150. X509_free(tmp);
  4151. AssertNull(sk_X509_pop(ca));
  4152. #ifndef NO_RC4
  4153. PKCS12_free(pkcs12_2);
  4154. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  4155. NID_pbe_WithSHA1And128BitRC4,
  4156. NID_pbe_WithSHA1And128BitRC4,
  4157. 2000, 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. #endif /* NO_RC4 */
  4164. EVP_PKEY_free(pkey);
  4165. X509_free(cert);
  4166. PKCS12_free(pkcs12);
  4167. PKCS12_free(pkcs12_2);
  4168. sk_X509_free(ca);
  4169. #ifdef HAVE_ECC
  4170. /* test order of parsing */
  4171. f = XFOPEN(order, "rb");
  4172. AssertTrue(f != XBADFILE);
  4173. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  4174. XFCLOSE(f);
  4175. AssertNotNull(bio = BIO_new_mem_buf((void*)buffer, bytes));
  4176. AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  4177. AssertIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  4178. WOLFSSL_SUCCESS);
  4179. AssertNotNull(pkey);
  4180. AssertNotNull(cert);
  4181. AssertNotNull(ca);
  4182. /* compare subject lines of certificates */
  4183. AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  4184. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  4185. SSL_FILETYPE_PEM));
  4186. AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  4187. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  4188. X509_free(x509);
  4189. /* test expected fail case */
  4190. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  4191. SSL_FILETYPE_PEM));
  4192. AssertIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  4193. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  4194. X509_free(x509);
  4195. X509_free(cert);
  4196. /* get subject line from ca stack */
  4197. AssertNotNull(cert = sk_X509_pop(ca));
  4198. AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  4199. /* compare subject from certificate in ca to expected */
  4200. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  4201. SSL_FILETYPE_PEM));
  4202. AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  4203. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  4204. EVP_PKEY_free(pkey);
  4205. X509_free(x509);
  4206. X509_free(cert);
  4207. BIO_free(bio);
  4208. PKCS12_free(pkcs12);
  4209. sk_X509_free(ca); /* TEST d2i_PKCS12_fp */
  4210. /* test order of parsing */
  4211. f = XFOPEN(file, "rb");
  4212. AssertTrue(f != XBADFILE);
  4213. AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  4214. XFCLOSE(f);
  4215. /* check verify MAC fail case */
  4216. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  4217. AssertIntEQ(ret, 0);
  4218. AssertNull(pkey);
  4219. AssertNull(cert);
  4220. /* check parse with no extra certs kept */
  4221. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  4222. AssertIntEQ(ret, 1);
  4223. AssertNotNull(pkey);
  4224. AssertNotNull(cert);
  4225. wolfSSL_EVP_PKEY_free(pkey);
  4226. wolfSSL_X509_free(cert);
  4227. /* check parse with extra certs kept */
  4228. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  4229. AssertIntEQ(ret, 1);
  4230. AssertNotNull(pkey);
  4231. AssertNotNull(cert);
  4232. AssertNotNull(ca);
  4233. wolfSSL_EVP_PKEY_free(pkey);
  4234. wolfSSL_X509_free(cert);
  4235. sk_X509_free(ca);
  4236. PKCS12_free(pkcs12);
  4237. #endif /* HAVE_ECC */
  4238. /* Test i2d_PKCS12_bio */
  4239. f = XFOPEN(file, "rb");
  4240. AssertTrue((f != XBADFILE));
  4241. AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  4242. XFCLOSE(f);
  4243. bio = BIO_new(BIO_s_mem());
  4244. AssertNotNull(bio);
  4245. ret = i2d_PKCS12_bio(bio, pkcs12);
  4246. AssertIntEQ(ret, 1);
  4247. ret = i2d_PKCS12_bio(NULL, pkcs12);
  4248. AssertIntEQ(ret, 0);
  4249. ret = i2d_PKCS12_bio(bio, NULL);
  4250. AssertIntEQ(ret, 0);
  4251. PKCS12_free(pkcs12);
  4252. BIO_free(bio);
  4253. (void)order;
  4254. printf(resultFmt, passed);
  4255. #endif /* OPENSSL_EXTRA */
  4256. #endif /* HAVE_FIPS */
  4257. }
  4258. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  4259. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  4260. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  4261. #define TEST_PKCS8_ENC
  4262. #endif
  4263. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  4264. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  4265. /* used to keep track if FailTestCallback was called */
  4266. static int failTestCallbackCalled = 0;
  4267. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  4268. {
  4269. (void)passwd;
  4270. (void)sz;
  4271. (void)rw;
  4272. (void)userdata;
  4273. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  4274. failTestCallbackCalled = 1;
  4275. return -1;
  4276. }
  4277. #endif
  4278. static void test_wolfSSL_no_password_cb(void)
  4279. {
  4280. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  4281. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  4282. WOLFSSL_CTX* ctx;
  4283. byte buffer[FOURK_BUF];
  4284. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  4285. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  4286. XFILE f;
  4287. int bytes;
  4288. printf(testingFmt, "test_wolfSSL_no_password_cb()");
  4289. #ifndef NO_WOLFSSL_CLIENT
  4290. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  4291. #else
  4292. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  4293. #endif
  4294. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  4295. AssertTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  4296. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  4297. XFCLOSE(f);
  4298. AssertIntLE(bytes, sizeof(buffer));
  4299. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  4300. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  4301. AssertTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  4302. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  4303. XFCLOSE(f);
  4304. AssertIntLE(bytes, sizeof(buffer));
  4305. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  4306. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4307. wolfSSL_CTX_free(ctx);
  4308. if (failTestCallbackCalled != 0) {
  4309. Fail(("Password callback should not be called by default"),
  4310. ("Password callback was called without attempting "
  4311. "to first decipher private key without password."));
  4312. }
  4313. printf(resultFmt, passed);
  4314. #endif
  4315. }
  4316. #ifdef TEST_PKCS8_ENC
  4317. /* for PKCS8 test case */
  4318. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  4319. {
  4320. int flag = 0;
  4321. (void)rw;
  4322. if (userdata != NULL) {
  4323. flag = *((int*)userdata); /* user set data */
  4324. }
  4325. switch (flag) {
  4326. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  4327. * can be anything the user wishes to be passed to the callback
  4328. * associated with the WOLFSSL_CTX */
  4329. XSTRNCPY(passwd, "yassl123", sz);
  4330. return 8;
  4331. default:
  4332. return BAD_FUNC_ARG;
  4333. }
  4334. }
  4335. #endif /* TEST_PKCS8_ENC */
  4336. /* Testing functions dealing with PKCS8 */
  4337. static void test_wolfSSL_PKCS8(void)
  4338. {
  4339. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8)
  4340. byte buffer[FOURK_BUF];
  4341. byte der[FOURK_BUF];
  4342. #ifndef NO_RSA
  4343. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  4344. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  4345. #endif
  4346. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  4347. #ifdef HAVE_ECC
  4348. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  4349. #endif
  4350. XFILE f;
  4351. int bytes;
  4352. WOLFSSL_CTX* ctx;
  4353. #ifdef HAVE_ECC
  4354. int ret;
  4355. ecc_key key;
  4356. word32 x = 0;
  4357. #endif
  4358. #ifdef TEST_PKCS8_ENC
  4359. #if !defined(NO_RSA) && !defined(NO_SHA)
  4360. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  4361. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  4362. #endif
  4363. #if defined(HAVE_ECC) && !defined(NO_SHA)
  4364. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  4365. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  4366. #endif
  4367. int flag;
  4368. #endif
  4369. printf(testingFmt, "wolfSSL_PKCS8()");
  4370. #ifndef NO_WOLFSSL_CLIENT
  4371. #ifndef WOLFSSL_NO_TLS12
  4372. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  4373. #else
  4374. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  4375. #endif
  4376. #else
  4377. #ifndef WOLFSSL_NO_TLS12
  4378. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  4379. #else
  4380. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  4381. #endif
  4382. #endif
  4383. #ifdef TEST_PKCS8_ENC
  4384. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  4385. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  4386. flag = 1; /* used by password callback as return code */
  4387. #if !defined(NO_RSA) && !defined(NO_SHA)
  4388. /* test loading PEM PKCS8 encrypted file */
  4389. f = XFOPEN(serverKeyPkcs8EncPemFile, "rb");
  4390. AssertTrue((f != XBADFILE));
  4391. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  4392. XFCLOSE(f);
  4393. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  4394. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4395. /* this next case should fail because of password callback return code */
  4396. flag = 0; /* used by password callback as return code */
  4397. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  4398. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4399. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  4400. AssertIntGT(wc_KeyPemToDer(buffer, bytes, der, (word32)sizeof(der),
  4401. "yassl123"), 0);
  4402. /* test that error value is returned with a bad password */
  4403. AssertIntLT(wc_KeyPemToDer(buffer, bytes, der, (word32)sizeof(der),
  4404. "bad"), 0);
  4405. /* test loading PEM PKCS8 encrypted file */
  4406. f = XFOPEN(serverKeyPkcs8EncDerFile, "rb");
  4407. AssertTrue((f != XBADFILE));
  4408. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  4409. XFCLOSE(f);
  4410. flag = 1; /* used by password callback as return code */
  4411. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  4412. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  4413. /* this next case should fail because of password callback return code */
  4414. flag = 0; /* used by password callback as return code */
  4415. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  4416. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  4417. #endif /* !NO_RSA && !NO_SHA */
  4418. #if defined(HAVE_ECC) && !defined(NO_SHA)
  4419. /* test loading PEM PKCS8 encrypted ECC Key file */
  4420. f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb");
  4421. AssertTrue((f != XBADFILE));
  4422. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  4423. XFCLOSE(f);
  4424. flag = 1; /* used by password callback as return code */
  4425. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  4426. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4427. /* this next case should fail because of password callback return code */
  4428. flag = 0; /* used by password callback as return code */
  4429. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  4430. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4431. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  4432. AssertIntGT(wc_KeyPemToDer(buffer, bytes, der, (word32)sizeof(der),
  4433. "yassl123"), 0);
  4434. /* test that error value is returned with a bad password */
  4435. AssertIntLT(wc_KeyPemToDer(buffer, bytes, der, (word32)sizeof(der),
  4436. "bad"), 0);
  4437. /* test loading DER PKCS8 encrypted ECC Key file */
  4438. f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb");
  4439. AssertTrue((f != XBADFILE));
  4440. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  4441. XFCLOSE(f);
  4442. flag = 1; /* used by password callback as return code */
  4443. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  4444. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  4445. /* this next case should fail because of password callback return code */
  4446. flag = 0; /* used by password callback as return code */
  4447. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  4448. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  4449. /* leave flag as "okay" */
  4450. flag = 1;
  4451. #endif /* HAVE_ECC && !NO_SHA */
  4452. #endif /* TEST_PKCS8_ENC */
  4453. #ifndef NO_RSA
  4454. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  4455. f = XFOPEN(serverKeyPkcs8DerFile, "rb");
  4456. AssertTrue((f != XBADFILE));
  4457. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  4458. XFCLOSE(f);
  4459. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  4460. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  4461. /* test loading PEM PKCS8 private key file (not encrypted) */
  4462. f = XFOPEN(serverKeyPkcs8PemFile, "rb");
  4463. AssertTrue((f != XBADFILE));
  4464. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  4465. XFCLOSE(f);
  4466. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  4467. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4468. #endif /* !NO_RSA */
  4469. /* Test PKCS8 PEM ECC key no crypt */
  4470. f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb");
  4471. AssertTrue((f != XBADFILE));
  4472. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  4473. XFCLOSE(f);
  4474. #ifdef HAVE_ECC
  4475. /* Test PKCS8 PEM ECC key no crypt */
  4476. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  4477. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4478. /* decrypt PKCS8 PEM to key in DER format */
  4479. AssertIntGT((bytes = wc_KeyPemToDer(buffer, bytes, der,
  4480. (word32)sizeof(der), NULL)), 0);
  4481. ret = wc_ecc_init(&key);
  4482. if (ret == 0) {
  4483. ret = wc_EccPrivateKeyDecode(der, &x, &key, bytes);
  4484. wc_ecc_free(&key);
  4485. }
  4486. AssertIntEQ(ret, 0);
  4487. /* Test PKCS8 DER ECC key no crypt */
  4488. f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb");
  4489. AssertTrue((f != XBADFILE));
  4490. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  4491. XFCLOSE(f);
  4492. /* Test using a PKCS8 ECC PEM */
  4493. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buffer, bytes,
  4494. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  4495. #else
  4496. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  4497. AssertIntEQ((bytes = wc_KeyPemToDer(buffer, bytes, der,
  4498. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  4499. #endif /* HAVE_ECC */
  4500. wolfSSL_CTX_free(ctx);
  4501. printf(resultFmt, passed);
  4502. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  4503. }
  4504. static void test_wolfSSL_PKCS8_ED25519(void)
  4505. {
  4506. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  4507. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519)
  4508. const byte encPrivKey[] = \
  4509. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  4510. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  4511. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  4512. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  4513. "2L9W4v7swXkV+X+a1ww=\n"
  4514. "-----END ENCRYPTED PRIVATE KEY-----\n";
  4515. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  4516. byte der[FOURK_BUF];
  4517. WOLFSSL_CTX* ctx;
  4518. int bytes;
  4519. XMEMSET(der, 0, sizeof(der));
  4520. AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  4521. (word32)sizeof(der), password)), 0);
  4522. #ifndef NO_WOLFSSL_SERVER
  4523. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4524. #else
  4525. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4526. #endif
  4527. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  4528. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  4529. wolfSSL_CTX_free(ctx);
  4530. #endif
  4531. }
  4532. static void test_wolfSSL_PKCS8_ED448(void)
  4533. {
  4534. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  4535. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448)
  4536. const byte encPrivKey[] = \
  4537. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  4538. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  4539. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  4540. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  4541. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  4542. "-----END ENCRYPTED PRIVATE KEY-----\n";
  4543. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  4544. byte der[FOURK_BUF];
  4545. WOLFSSL_CTX* ctx;
  4546. int bytes;
  4547. XMEMSET(der, 0, sizeof(der));
  4548. AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  4549. (word32)sizeof(der), password)), 0);
  4550. #ifndef NO_WOLFSSL_SERVER
  4551. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4552. #else
  4553. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4554. #endif
  4555. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  4556. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  4557. wolfSSL_CTX_free(ctx);
  4558. #endif
  4559. }
  4560. /* Testing functions dealing with PKCS5 */
  4561. static void test_wolfSSL_PKCS5(void)
  4562. {
  4563. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  4564. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  4565. const char* passwd = "myfipsPa$$W0rd";
  4566. #else
  4567. const char *passwd = "pass1234";
  4568. #endif
  4569. const unsigned char *salt = (unsigned char *)"salt1234";
  4570. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  4571. DYNAMIC_TYPE_TMP_BUFFER);
  4572. int ret = 0;
  4573. AssertNotNull(out);
  4574. ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  4575. (int)XSTRLEN((const char *) salt), 10,
  4576. WC_SHA_DIGEST_SIZE,out);
  4577. AssertIntEQ(ret, SSL_SUCCESS);
  4578. #ifdef WOLFSSL_SHA512
  4579. ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  4580. (int)XSTRLEN((const char *) salt), 10,
  4581. wolfSSL_EVP_sha512(), WC_SHA_DIGEST_SIZE, out);
  4582. AssertIntEQ(ret, SSL_SUCCESS);
  4583. #endif
  4584. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4585. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  4586. }
  4587. /* test parsing URI from certificate */
  4588. static void test_wolfSSL_URI(void)
  4589. {
  4590. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  4591. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  4592. defined(OPENSSL_EXTRA))
  4593. WOLFSSL_X509* x509;
  4594. const char uri[] = "./certs/client-uri-cert.pem";
  4595. const char badUri[] = "./certs/client-relative-uri.pem";
  4596. printf(testingFmt, "wolfSSL URI parse");
  4597. x509 = wolfSSL_X509_load_certificate_file(uri, WOLFSSL_FILETYPE_PEM);
  4598. AssertNotNull(x509);
  4599. wolfSSL_FreeX509(x509);
  4600. x509 = wolfSSL_X509_load_certificate_file(badUri, WOLFSSL_FILETYPE_PEM);
  4601. #ifndef IGNORE_NAME_CONSTRAINTS
  4602. AssertNull(x509);
  4603. #else
  4604. AssertNotNull(x509);
  4605. #endif
  4606. printf(resultFmt, passed);
  4607. #endif
  4608. }
  4609. static void test_wolfSSL_TBS(void)
  4610. {
  4611. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  4612. && defined(OPENSSL_EXTRA)
  4613. WOLFSSL_X509* x509;
  4614. const unsigned char* tbs;
  4615. int tbsSz;
  4616. printf(testingFmt, "wolfSSL TBS");
  4617. AssertNotNull(x509 =
  4618. wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
  4619. AssertNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  4620. AssertNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  4621. AssertNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  4622. AssertIntEQ(tbsSz, 972);
  4623. wolfSSL_FreeX509(x509);
  4624. printf(resultFmt, passed);
  4625. #endif
  4626. }
  4627. static void test_wolfSSL_X509_verify(void)
  4628. {
  4629. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  4630. && defined(OPENSSL_EXTRA)
  4631. WOLFSSL_X509* ca;
  4632. WOLFSSL_X509* server;
  4633. WOLFSSL_EVP_PKEY* pkey;
  4634. unsigned char buf[2048];
  4635. const unsigned char* pt;
  4636. int bufSz;
  4637. printf(testingFmt, "wolfSSL X509 verify");
  4638. AssertNotNull(ca =
  4639. wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
  4640. AssertIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  4641. WOLFSSL_SUCCESS);
  4642. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  4643. WOLFSSL_SUCCESS);
  4644. AssertIntEQ(bufSz, 294);
  4645. bufSz = 2048;
  4646. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  4647. WOLFSSL_SUCCESS);
  4648. AssertIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  4649. AssertIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  4650. AssertNotNull(server =
  4651. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM));
  4652. /* success case */
  4653. pt = buf;
  4654. AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  4655. AssertIntEQ(wolfSSL_X509_verify(server, pkey), WOLFSSL_SUCCESS);
  4656. wolfSSL_EVP_PKEY_free(pkey);
  4657. /* fail case */
  4658. bufSz = 2048;
  4659. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(server, buf, &bufSz),
  4660. WOLFSSL_SUCCESS);
  4661. pt = buf;
  4662. AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  4663. AssertIntEQ(wolfSSL_X509_verify(server, pkey), WOLFSSL_FAILURE);
  4664. AssertIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  4665. AssertIntEQ(wolfSSL_X509_verify(server, NULL), WOLFSSL_FATAL_ERROR);
  4666. wolfSSL_EVP_PKEY_free(pkey);
  4667. wolfSSL_FreeX509(ca);
  4668. wolfSSL_FreeX509(server);
  4669. printf(resultFmt, passed);
  4670. #endif
  4671. }
  4672. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  4673. * version allowed.
  4674. * POST: 1 on success.
  4675. */
  4676. static int test_wolfSSL_CTX_SetMinVersion(void)
  4677. {
  4678. int failFlag = WOLFSSL_SUCCESS;
  4679. #ifndef NO_WOLFSSL_CLIENT
  4680. WOLFSSL_CTX* ctx;
  4681. int itr;
  4682. #ifndef NO_OLD_TLS
  4683. const int versions[] = {
  4684. #ifdef WOLFSSL_ALLOW_TLSV10
  4685. WOLFSSL_TLSV1,
  4686. #endif
  4687. WOLFSSL_TLSV1_1,
  4688. WOLFSSL_TLSV1_2 };
  4689. #elif !defined(WOLFSSL_NO_TLS12)
  4690. const int versions[] = { WOLFSSL_TLSV1_2 };
  4691. #elif defined(WOLFSSL_TLS13)
  4692. const int versions[] = { WOLFSSL_TLSV1_3 };
  4693. #else
  4694. const int versions[0];
  4695. #endif
  4696. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  4697. printf(testingFmt, "wolfSSL_CTX_SetMinVersion()");
  4698. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++){
  4699. if(wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr)) != WOLFSSL_SUCCESS){
  4700. failFlag = WOLFSSL_FAILURE;
  4701. }
  4702. }
  4703. printf(resultFmt, failFlag == WOLFSSL_SUCCESS ? passed : failed);
  4704. wolfSSL_CTX_free(ctx);
  4705. #endif
  4706. return failFlag;
  4707. } /* END test_wolfSSL_CTX_SetMinVersion */
  4708. /*----------------------------------------------------------------------------*
  4709. | OCSP Stapling
  4710. *----------------------------------------------------------------------------*/
  4711. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  4712. * need to contact the CA, lowering the cost of cert revocation checking.
  4713. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  4714. * POST: 1 returned for success.
  4715. */
  4716. static int test_wolfSSL_UseOCSPStapling(void)
  4717. {
  4718. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  4719. !defined(NO_WOLFSSL_CLIENT)
  4720. int ret;
  4721. WOLFSSL_CTX* ctx;
  4722. WOLFSSL* ssl;
  4723. #ifndef NO_WOLFSSL_CLIENT
  4724. #ifndef WOLFSSL_NO_TLS12
  4725. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  4726. #else
  4727. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  4728. #endif
  4729. #else
  4730. #ifndef WOLFSSL_NO_TLS12
  4731. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  4732. #else
  4733. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  4734. #endif
  4735. #endif
  4736. ssl = wolfSSL_new(ctx);
  4737. printf(testingFmt, "wolfSSL_UseOCSPStapling()");
  4738. ret = wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  4739. WOLFSSL_CSR2_OCSP_USE_NONCE);
  4740. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  4741. wolfSSL_free(ssl);
  4742. wolfSSL_CTX_free(ctx);
  4743. return ret;
  4744. #else
  4745. return WOLFSSL_SUCCESS;
  4746. #endif
  4747. } /*END test_wolfSSL_UseOCSPStapling */
  4748. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  4749. * stapling eliminates the need to contact the CA and lowers cert revocation
  4750. * check.
  4751. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  4752. */
  4753. static int test_wolfSSL_UseOCSPStaplingV2 (void)
  4754. {
  4755. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  4756. !defined(NO_WOLFSSL_CLIENT)
  4757. int ret;
  4758. WOLFSSL_CTX* ctx;
  4759. WOLFSSL* ssl;
  4760. #ifndef NO_WOLFSSL_CLIENT
  4761. #ifndef WOLFSSL_NO_TLS12
  4762. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  4763. #else
  4764. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  4765. #endif
  4766. #else
  4767. #ifndef WOLFSSL_NO_TLS12
  4768. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  4769. #else
  4770. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  4771. #endif
  4772. #endif
  4773. ssl = wolfSSL_new(ctx);
  4774. printf(testingFmt, "wolfSSL_UseOCSPStaplingV2()");
  4775. ret = wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  4776. WOLFSSL_CSR2_OCSP_USE_NONCE );
  4777. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  4778. wolfSSL_free(ssl);
  4779. wolfSSL_CTX_free(ctx);
  4780. return ret;
  4781. #else
  4782. return WOLFSSL_SUCCESS;
  4783. #endif
  4784. } /*END test_wolfSSL_UseOCSPStaplingV2*/
  4785. /*----------------------------------------------------------------------------*
  4786. | Multicast Tests
  4787. *----------------------------------------------------------------------------*/
  4788. static void test_wolfSSL_mcast(void)
  4789. {
  4790. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST)
  4791. WOLFSSL_CTX* ctx;
  4792. WOLFSSL* ssl;
  4793. int result;
  4794. byte preMasterSecret[512];
  4795. byte clientRandom[32];
  4796. byte serverRandom[32];
  4797. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  4798. byte buf[256];
  4799. word16 newId;
  4800. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method());
  4801. AssertNotNull(ctx);
  4802. result = wolfSSL_CTX_mcast_set_member_id(ctx, 0);
  4803. AssertIntEQ(result, WOLFSSL_SUCCESS);
  4804. ssl = wolfSSL_new(ctx);
  4805. AssertNotNull(ssl);
  4806. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  4807. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  4808. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  4809. result = wolfSSL_set_secret(ssl, 23,
  4810. preMasterSecret, sizeof(preMasterSecret),
  4811. clientRandom, serverRandom, suite);
  4812. AssertIntEQ(result, WOLFSSL_SUCCESS);
  4813. result = wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf));
  4814. AssertIntLE(result, 0);
  4815. AssertIntLE(newId, 100);
  4816. wolfSSL_free(ssl);
  4817. wolfSSL_CTX_free(ctx);
  4818. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST */
  4819. }
  4820. /*----------------------------------------------------------------------------*
  4821. | Wolfcrypt
  4822. *----------------------------------------------------------------------------*/
  4823. /*
  4824. * Unit test for the wc_InitBlake2b()
  4825. */
  4826. static int test_wc_InitBlake2b (void)
  4827. {
  4828. int ret = 0;
  4829. #ifdef HAVE_BLAKE2
  4830. Blake2b blake2;
  4831. printf(testingFmt, "wc_InitBlake2B()");
  4832. /* Test good arg. */
  4833. ret = wc_InitBlake2b(&blake2, 64);
  4834. if (ret != 0) {
  4835. ret = WOLFSSL_FATAL_ERROR;
  4836. }
  4837. /* Test bad arg. */
  4838. if (!ret) {
  4839. ret = wc_InitBlake2b(NULL, 64);
  4840. if (ret == 0) {
  4841. ret = WOLFSSL_FATAL_ERROR;
  4842. } else {
  4843. ret = 0;
  4844. }
  4845. }
  4846. if (!ret) {
  4847. ret = wc_InitBlake2b(NULL, 128);
  4848. if (ret == 0) {
  4849. ret = WOLFSSL_FATAL_ERROR;
  4850. } else {
  4851. ret = 0;
  4852. }
  4853. }
  4854. if (!ret) {
  4855. ret = wc_InitBlake2b(&blake2, 128);
  4856. if (ret == 0) {
  4857. ret = WOLFSSL_FATAL_ERROR;
  4858. } else {
  4859. ret = 0;
  4860. }
  4861. }
  4862. if (!ret) {
  4863. ret = wc_InitBlake2b(NULL, 0);
  4864. if (ret == 0) {
  4865. ret = WOLFSSL_FATAL_ERROR;
  4866. } else {
  4867. ret = 0;
  4868. }
  4869. }
  4870. if (!ret) {
  4871. ret = wc_InitBlake2b(&blake2, 0);
  4872. if (ret == 0) {
  4873. ret = WOLFSSL_FATAL_ERROR;
  4874. } else {
  4875. ret = 0;
  4876. }
  4877. }
  4878. printf(resultFmt, ret == 0 ? passed : failed);
  4879. #endif
  4880. return ret;
  4881. } /*END test_wc_InitBlake2b*/
  4882. /*
  4883. * Unit test for the wc_InitMd5()
  4884. */
  4885. static int test_wc_InitMd5 (void)
  4886. {
  4887. int flag = 0;
  4888. #ifndef NO_MD5
  4889. wc_Md5 md5;
  4890. int ret;
  4891. printf(testingFmt, "wc_InitMd5()");
  4892. /* Test good arg. */
  4893. ret = wc_InitMd5(&md5);
  4894. if (ret != 0) {
  4895. flag = WOLFSSL_FATAL_ERROR;
  4896. }
  4897. /* Test bad arg. */
  4898. if (!flag) {
  4899. ret = wc_InitMd5(NULL);
  4900. if (ret != BAD_FUNC_ARG) {
  4901. flag = WOLFSSL_FATAL_ERROR;
  4902. }
  4903. }
  4904. wc_Md5Free(&md5);
  4905. printf(resultFmt, flag == 0 ? passed : failed);
  4906. #endif
  4907. return flag;
  4908. } /* END test_wc_InitMd5 */
  4909. /*
  4910. * Testing wc_UpdateMd5()
  4911. */
  4912. static int test_wc_Md5Update (void)
  4913. {
  4914. int flag = 0;
  4915. #ifndef NO_MD5
  4916. wc_Md5 md5;
  4917. byte hash[WC_MD5_DIGEST_SIZE];
  4918. testVector a, b, c;
  4919. int ret;
  4920. ret = wc_InitMd5(&md5);
  4921. if (ret != 0) {
  4922. flag = ret;
  4923. }
  4924. printf(testingFmt, "wc_Md5Update()");
  4925. /* Input */
  4926. if (!flag) {
  4927. a.input = "a";
  4928. a.inLen = XSTRLEN(a.input);
  4929. ret = wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen);
  4930. if (ret != 0) {
  4931. flag = ret;
  4932. }
  4933. }
  4934. if (!flag) {
  4935. ret = wc_Md5Final(&md5, hash);
  4936. if (ret != 0) {
  4937. flag = ret;
  4938. }
  4939. }
  4940. /* Update input. */
  4941. if (!flag) {
  4942. a.input = "abc";
  4943. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  4944. "\x72";
  4945. a.inLen = XSTRLEN(a.input);
  4946. a.outLen = XSTRLEN(a.output);
  4947. ret = wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen);
  4948. if (ret != 0) {
  4949. flag = ret;
  4950. }
  4951. }
  4952. if (!flag) {
  4953. ret = wc_Md5Final(&md5, hash);
  4954. if (ret != 0) {
  4955. flag = ret;
  4956. }
  4957. }
  4958. if (!flag) {
  4959. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  4960. flag = WOLFSSL_FATAL_ERROR;
  4961. }
  4962. }
  4963. /*Pass in bad values. */
  4964. if (!flag) {
  4965. b.input = NULL;
  4966. b.inLen = 0;
  4967. ret = wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen);
  4968. if (ret != 0) {
  4969. flag = ret;
  4970. }
  4971. }
  4972. if (!flag) {
  4973. c.input = NULL;
  4974. c.inLen = WC_MD5_DIGEST_SIZE;
  4975. ret = wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen);
  4976. if (ret != BAD_FUNC_ARG) {
  4977. flag = WOLFSSL_FATAL_ERROR;
  4978. }
  4979. }
  4980. if (!flag) {
  4981. ret = wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen);
  4982. if (ret != BAD_FUNC_ARG) {
  4983. flag = WOLFSSL_FATAL_ERROR;
  4984. }
  4985. }
  4986. wc_Md5Free(&md5);
  4987. printf(resultFmt, flag == 0 ? passed : failed);
  4988. #endif
  4989. return flag;
  4990. } /* END test_wc_Md5Update() */
  4991. /*
  4992. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  4993. */
  4994. static int test_wc_Md5Final (void)
  4995. {
  4996. int flag = 0;
  4997. #ifndef NO_MD5
  4998. /* Instantiate */
  4999. wc_Md5 md5;
  5000. byte* hash_test[3];
  5001. byte hash1[WC_MD5_DIGEST_SIZE];
  5002. byte hash2[2*WC_MD5_DIGEST_SIZE];
  5003. byte hash3[5*WC_MD5_DIGEST_SIZE];
  5004. int times, i, ret;
  5005. /* Initialize */
  5006. ret = wc_InitMd5(&md5);
  5007. if (ret != 0) {
  5008. flag = ret;
  5009. }
  5010. if (!flag) {
  5011. hash_test[0] = hash1;
  5012. hash_test[1] = hash2;
  5013. hash_test[2] = hash3;
  5014. }
  5015. times = sizeof(hash_test)/sizeof(byte*);
  5016. /* Test good args. */
  5017. printf(testingFmt, "wc_Md5Final()");
  5018. for (i = 0; i < times; i++) {
  5019. if (!flag) {
  5020. ret = wc_Md5Final(&md5, hash_test[i]);
  5021. if (ret != 0) {
  5022. flag = WOLFSSL_FATAL_ERROR;
  5023. }
  5024. }
  5025. }
  5026. /* Test bad args. */
  5027. if (!flag) {
  5028. ret = wc_Md5Final(NULL, NULL);
  5029. if (ret != BAD_FUNC_ARG) {
  5030. flag = WOLFSSL_FATAL_ERROR;
  5031. }
  5032. }
  5033. if (!flag) {
  5034. ret = wc_Md5Final(NULL, hash1);
  5035. if (ret != BAD_FUNC_ARG) {
  5036. flag = WOLFSSL_FATAL_ERROR;
  5037. }
  5038. }
  5039. if (!flag) {
  5040. ret = wc_Md5Final(&md5, NULL);
  5041. if (ret != BAD_FUNC_ARG) {
  5042. flag = WOLFSSL_FATAL_ERROR;
  5043. }
  5044. }
  5045. wc_Md5Free(&md5);
  5046. printf(resultFmt, flag == 0 ? passed : failed);
  5047. #endif
  5048. return flag;
  5049. }
  5050. /*
  5051. * Unit test for the wc_InitSha()
  5052. */
  5053. static int test_wc_InitSha(void)
  5054. {
  5055. int flag = 0;
  5056. #ifndef NO_SHA
  5057. wc_Sha sha;
  5058. int ret;
  5059. printf(testingFmt, "wc_InitSha()");
  5060. /* Test good arg. */
  5061. ret = wc_InitSha(&sha);
  5062. if (ret != 0) {
  5063. flag = WOLFSSL_FATAL_ERROR;
  5064. }
  5065. /* Test bad arg. */
  5066. if (!flag) {
  5067. ret = wc_InitSha(NULL);
  5068. if (ret != BAD_FUNC_ARG) {
  5069. flag = WOLFSSL_FATAL_ERROR;
  5070. }
  5071. }
  5072. wc_ShaFree(&sha);
  5073. printf(resultFmt, flag == 0 ? passed : failed);
  5074. #endif
  5075. return flag;
  5076. } /* END test_wc_InitSha */
  5077. /*
  5078. * Tesing wc_ShaUpdate()
  5079. */
  5080. static int test_wc_ShaUpdate (void)
  5081. {
  5082. int flag = 0;
  5083. #ifndef NO_SHA
  5084. wc_Sha sha;
  5085. byte hash[WC_SHA_DIGEST_SIZE];
  5086. testVector a, b, c;
  5087. int ret;
  5088. ret = wc_InitSha(&sha);
  5089. if (ret != 0) {
  5090. flag = ret;
  5091. }
  5092. printf(testingFmt, "wc_ShaUpdate()");
  5093. /* Input. */
  5094. if (!flag) {
  5095. a.input = "a";
  5096. a.inLen = XSTRLEN(a.input);
  5097. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  5098. if (ret != 0) {
  5099. flag = ret;
  5100. }
  5101. }
  5102. if (!flag) {
  5103. ret = wc_ShaFinal(&sha, hash);
  5104. if (ret != 0) {
  5105. flag = ret;
  5106. }
  5107. }
  5108. /* Update input. */
  5109. if (!flag) {
  5110. a.input = "abc";
  5111. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  5112. "\x6C\x9C\xD0\xD8\x9D";
  5113. a.inLen = XSTRLEN(a.input);
  5114. a.outLen = XSTRLEN(a.output);
  5115. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  5116. if (ret != 0) {
  5117. flag = ret;
  5118. }
  5119. }
  5120. if (!flag) {
  5121. ret = wc_ShaFinal(&sha, hash);
  5122. if (ret !=0) {
  5123. flag = ret;
  5124. }
  5125. }
  5126. if (!flag) {
  5127. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  5128. flag = WOLFSSL_FATAL_ERROR;
  5129. }
  5130. }
  5131. /* Try passing in bad values. */
  5132. if (!flag) {
  5133. b.input = NULL;
  5134. b.inLen = 0;
  5135. ret = wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen);
  5136. if (ret != 0) {
  5137. flag = ret;
  5138. }
  5139. }
  5140. if (!flag) {
  5141. c.input = NULL;
  5142. c.inLen = WC_SHA_DIGEST_SIZE;
  5143. ret = wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen);
  5144. if (ret != BAD_FUNC_ARG) {
  5145. flag = WOLFSSL_FATAL_ERROR;
  5146. }
  5147. }
  5148. if (!flag) {
  5149. ret = wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  5150. if (ret != BAD_FUNC_ARG) {
  5151. flag = WOLFSSL_FATAL_ERROR;
  5152. }
  5153. }
  5154. wc_ShaFree(&sha);
  5155. /* If not returned then the unit test passed test vectors. */
  5156. printf(resultFmt, flag == 0 ? passed : failed);
  5157. #endif
  5158. return flag;
  5159. } /* END test_wc_ShaUpdate() */
  5160. /*
  5161. * Unit test on wc_ShaFinal
  5162. */
  5163. static int test_wc_ShaFinal (void)
  5164. {
  5165. int flag = 0;
  5166. #ifndef NO_SHA
  5167. wc_Sha sha;
  5168. byte* hash_test[3];
  5169. byte hash1[WC_SHA_DIGEST_SIZE];
  5170. byte hash2[2*WC_SHA_DIGEST_SIZE];
  5171. byte hash3[5*WC_SHA_DIGEST_SIZE];
  5172. int times, i, ret;
  5173. /*Initialize*/
  5174. ret = wc_InitSha(&sha);
  5175. if (ret) {
  5176. flag = ret;
  5177. }
  5178. if (!flag) {
  5179. hash_test[0] = hash1;
  5180. hash_test[1] = hash2;
  5181. hash_test[2] = hash3;
  5182. }
  5183. times = sizeof(hash_test)/sizeof(byte*);
  5184. /* Good test args. */
  5185. printf(testingFmt, "wc_ShaFinal()");
  5186. for (i = 0; i < times; i++) {
  5187. if (!flag) {
  5188. ret = wc_ShaFinal(&sha, hash_test[i]);
  5189. if (ret != 0) {
  5190. flag = WOLFSSL_FATAL_ERROR;
  5191. }
  5192. }
  5193. }
  5194. /* Test bad args. */
  5195. if (!flag) {
  5196. ret = wc_ShaFinal(NULL, NULL);
  5197. if (ret != BAD_FUNC_ARG) {
  5198. flag = WOLFSSL_FATAL_ERROR;
  5199. }
  5200. }
  5201. if (!flag) {
  5202. ret = wc_ShaFinal(NULL, hash1);
  5203. if (ret != BAD_FUNC_ARG) {
  5204. flag = WOLFSSL_FATAL_ERROR;
  5205. }
  5206. }
  5207. if (!flag) {
  5208. ret = wc_ShaFinal(&sha, NULL);
  5209. if (ret != BAD_FUNC_ARG) {
  5210. flag = WOLFSSL_FATAL_ERROR;
  5211. }
  5212. }
  5213. wc_ShaFree(&sha);
  5214. printf(resultFmt, flag == 0 ? passed : failed);
  5215. #endif
  5216. return flag;
  5217. } /* END test_wc_ShaFinal */
  5218. /*
  5219. * Unit test for wc_InitSha256()
  5220. */
  5221. static int test_wc_InitSha256 (void)
  5222. {
  5223. int flag = 0;
  5224. #ifndef NO_SHA256
  5225. wc_Sha256 sha256;
  5226. int ret;
  5227. printf(testingFmt, "wc_InitSha256()");
  5228. /* Test good arg. */
  5229. ret = wc_InitSha256(&sha256);
  5230. if (ret != 0) {
  5231. flag = WOLFSSL_FATAL_ERROR;
  5232. }
  5233. /* Test bad arg. */
  5234. if (!flag) {
  5235. ret = wc_InitSha256(NULL);
  5236. if (ret != BAD_FUNC_ARG) {
  5237. flag = WOLFSSL_FATAL_ERROR;
  5238. }
  5239. }
  5240. wc_Sha256Free(&sha256);
  5241. printf(resultFmt, flag == 0 ? passed : failed);
  5242. #endif
  5243. return flag;
  5244. } /* END test_wc_InitSha256 */
  5245. /*
  5246. * Unit test for wc_Sha256Update()
  5247. */
  5248. static int test_wc_Sha256Update (void)
  5249. {
  5250. int flag = 0;
  5251. #ifndef NO_SHA256
  5252. wc_Sha256 sha256;
  5253. byte hash[WC_SHA256_DIGEST_SIZE];
  5254. testVector a, b, c;
  5255. int ret;
  5256. ret = wc_InitSha256(&sha256);
  5257. if (ret != 0) {
  5258. flag = ret;
  5259. }
  5260. printf(testingFmt, "wc_Sha256Update()");
  5261. /* Input. */
  5262. if (!flag) {
  5263. a.input = "a";
  5264. a.inLen = XSTRLEN(a.input);
  5265. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  5266. if (ret != 0) {
  5267. flag = ret;
  5268. }
  5269. }
  5270. if (!flag) {
  5271. ret = wc_Sha256Final(&sha256, hash);
  5272. if (ret != 0) {
  5273. flag = ret;
  5274. }
  5275. }
  5276. /* Update input. */
  5277. if (!flag) {
  5278. a.input = "abc";
  5279. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  5280. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  5281. "\x15\xAD";
  5282. a.inLen = XSTRLEN(a.input);
  5283. a.outLen = XSTRLEN(a.output);
  5284. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  5285. if (ret != 0) {
  5286. flag = ret;
  5287. }
  5288. }
  5289. if (!flag) {
  5290. ret = wc_Sha256Final(&sha256, hash);
  5291. if (ret != 0) {
  5292. flag = ret;
  5293. }
  5294. }
  5295. if (!flag) {
  5296. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  5297. flag = WOLFSSL_FATAL_ERROR;
  5298. }
  5299. }
  5300. /* Try passing in bad values */
  5301. if (!flag) {
  5302. b.input = NULL;
  5303. b.inLen = 0;
  5304. ret = wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen);
  5305. if (ret != 0) {
  5306. flag = ret;
  5307. }
  5308. }
  5309. if (!flag) {
  5310. c.input = NULL;
  5311. c.inLen = WC_SHA256_DIGEST_SIZE;
  5312. ret = wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen);
  5313. if (ret != BAD_FUNC_ARG) {
  5314. flag = WOLFSSL_FATAL_ERROR;
  5315. }
  5316. }
  5317. if (!flag) {
  5318. ret = wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen);
  5319. if (ret != BAD_FUNC_ARG) {
  5320. flag = WOLFSSL_FATAL_ERROR;
  5321. }
  5322. }
  5323. wc_Sha256Free(&sha256);
  5324. /* If not returned then the unit test passed. */
  5325. printf(resultFmt, flag == 0 ? passed : failed);
  5326. #endif
  5327. return flag;
  5328. } /* END test_wc_Sha256Update */
  5329. /*
  5330. * Unit test function for wc_Sha256Final()
  5331. */
  5332. static int test_wc_Sha256Final (void)
  5333. {
  5334. int flag = 0;
  5335. #ifndef NO_SHA256
  5336. wc_Sha256 sha256;
  5337. byte* hash_test[3];
  5338. byte hash1[WC_SHA256_DIGEST_SIZE];
  5339. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  5340. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  5341. int times, i, ret;
  5342. /* Initialize */
  5343. ret = wc_InitSha256(&sha256);
  5344. if (ret != 0) {
  5345. flag = ret;
  5346. }
  5347. if (!flag) {
  5348. hash_test[0] = hash1;
  5349. hash_test[1] = hash2;
  5350. hash_test[2] = hash3;
  5351. }
  5352. times = sizeof(hash_test) / sizeof(byte*);
  5353. /* Good test args. */
  5354. printf(testingFmt, "wc_Sha256Final()");
  5355. for (i = 0; i < times; i++) {
  5356. if (!flag) {
  5357. ret = wc_Sha256Final(&sha256, hash_test[i]);
  5358. if (ret != 0) {
  5359. flag = WOLFSSL_FATAL_ERROR;
  5360. }
  5361. }
  5362. }
  5363. /* Test bad args. */
  5364. if (!flag ) {
  5365. ret = wc_Sha256Final(NULL, NULL);
  5366. if (ret != BAD_FUNC_ARG) {
  5367. flag = WOLFSSL_FATAL_ERROR;
  5368. }
  5369. }
  5370. if (!flag) {
  5371. ret = wc_Sha256Final(NULL, hash1);
  5372. if (ret != BAD_FUNC_ARG) {
  5373. flag = WOLFSSL_FATAL_ERROR;
  5374. }
  5375. }
  5376. if (!flag) {
  5377. ret = wc_Sha256Final(&sha256, NULL);
  5378. if (ret != BAD_FUNC_ARG) {
  5379. flag = WOLFSSL_FATAL_ERROR;
  5380. }
  5381. }
  5382. wc_Sha256Free(&sha256);
  5383. printf(resultFmt, flag == 0 ? passed : failed);
  5384. #endif
  5385. return flag;
  5386. } /* END test_wc_Sha256Final */
  5387. /*
  5388. * Unit test function for wc_Sha256FinalRaw()
  5389. */
  5390. static int test_wc_Sha256FinalRaw (void)
  5391. {
  5392. int flag = 0;
  5393. #if !defined(NO_SHA256) && \
  5394. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  5395. wc_Sha256 sha256;
  5396. byte* hash_test[3];
  5397. byte hash1[WC_SHA256_DIGEST_SIZE];
  5398. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  5399. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  5400. int times, i, ret;
  5401. /* Initialize */
  5402. ret = wc_InitSha256(&sha256);
  5403. if (ret != 0) {
  5404. flag = ret;
  5405. }
  5406. if (!flag) {
  5407. hash_test[0] = hash1;
  5408. hash_test[1] = hash2;
  5409. hash_test[2] = hash3;
  5410. }
  5411. times = sizeof(hash_test) / sizeof(byte*);
  5412. /* Good test args. */
  5413. printf(testingFmt, "wc_Sha256FinalRaw()");
  5414. for (i = 0; i < times; i++) {
  5415. if (!flag) {
  5416. ret = wc_Sha256FinalRaw(&sha256, hash_test[i]);
  5417. if (ret != 0) {
  5418. flag = WOLFSSL_FATAL_ERROR;
  5419. }
  5420. }
  5421. }
  5422. /* Test bad args. */
  5423. if (!flag ) {
  5424. ret = wc_Sha256FinalRaw(NULL, NULL);
  5425. if (ret != BAD_FUNC_ARG) {
  5426. flag = WOLFSSL_FATAL_ERROR;
  5427. }
  5428. }
  5429. if (!flag) {
  5430. ret = wc_Sha256FinalRaw(NULL, hash1);
  5431. if (ret != BAD_FUNC_ARG) {
  5432. flag = WOLFSSL_FATAL_ERROR;
  5433. }
  5434. }
  5435. if (!flag) {
  5436. ret = wc_Sha256FinalRaw(&sha256, NULL);
  5437. if (ret != BAD_FUNC_ARG) {
  5438. flag = WOLFSSL_FATAL_ERROR;
  5439. }
  5440. }
  5441. wc_Sha256Free(&sha256);
  5442. printf(resultFmt, flag == 0 ? passed : failed);
  5443. #endif
  5444. return flag;
  5445. } /* END test_wc_Sha256FinalRaw */
  5446. /*
  5447. * Unit test function for wc_Sha256GetFlags()
  5448. */
  5449. static int test_wc_Sha256GetFlags (void)
  5450. {
  5451. int flag = 0;
  5452. #if !defined(NO_SHA256) && \
  5453. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  5454. wc_Sha256 sha256;
  5455. word32 flags = 0;
  5456. printf(testingFmt, "wc_Sha256GetFlags()");
  5457. /* Initialize */
  5458. flag = wc_InitSha256(&sha256);
  5459. if (flag == 0) {
  5460. flag = wc_Sha256GetFlags(&sha256, &flags);
  5461. }
  5462. if (flag == 0) {
  5463. if (flags & WC_HASH_FLAG_ISCOPY) {
  5464. flag = 0;
  5465. }
  5466. }
  5467. wc_Sha256Free(&sha256);
  5468. printf(resultFmt, flag == 0 ? passed : failed);
  5469. #endif
  5470. return flag;
  5471. } /* END test_wc_Sha256GetFlags */
  5472. /*
  5473. * Unit test function for wc_Sha256Free()
  5474. */
  5475. static int test_wc_Sha256Free (void)
  5476. {
  5477. int flag = 0;
  5478. #ifndef NO_SHA256
  5479. printf(testingFmt, "wc_Sha256Free()");
  5480. wc_Sha256Free(NULL);
  5481. printf(resultFmt, flag == 0 ? passed : failed);
  5482. #endif
  5483. return flag;
  5484. } /* END test_wc_Sha256Free */
  5485. /*
  5486. * Unit test function for wc_Sha256GetHash()
  5487. */
  5488. static int test_wc_Sha256GetHash (void)
  5489. {
  5490. int flag = 0;
  5491. #ifndef NO_SHA256
  5492. wc_Sha256 sha256;
  5493. byte hash1[WC_SHA256_DIGEST_SIZE];
  5494. printf(testingFmt, "wc_Sha256GetHash()");
  5495. /* Initialize */
  5496. flag = wc_InitSha256(&sha256);
  5497. if (flag == 0) {
  5498. flag = wc_Sha256GetHash(&sha256, hash1);
  5499. }
  5500. /*test bad arguements*/
  5501. if (flag == 0) {
  5502. flag = wc_Sha256GetHash(NULL, NULL);
  5503. if (flag == BAD_FUNC_ARG) {
  5504. flag = 0;
  5505. }
  5506. }
  5507. if (flag == 0) {
  5508. flag = wc_Sha256GetHash(NULL, hash1);
  5509. if (flag == BAD_FUNC_ARG) {
  5510. flag = 0;
  5511. }
  5512. }
  5513. if (flag == 0) {
  5514. flag = wc_Sha256GetHash(&sha256, NULL);
  5515. if (flag == BAD_FUNC_ARG) {
  5516. flag = 0;
  5517. }
  5518. }
  5519. wc_Sha256Free(&sha256);
  5520. printf(resultFmt, flag == 0 ? passed : failed);
  5521. #endif
  5522. return flag;
  5523. } /* END test_wc_Sha256GetHash */
  5524. /*
  5525. * Unit test function for wc_Sha256Copy()
  5526. */
  5527. static int test_wc_Sha256Copy (void)
  5528. {
  5529. int flag = 0;
  5530. #ifndef NO_SHA256
  5531. wc_Sha256 sha256;
  5532. wc_Sha256 temp;
  5533. printf(testingFmt, "wc_Sha256Copy()");
  5534. /* Initialize */
  5535. flag = wc_InitSha256(&sha256);
  5536. if (flag == 0) {
  5537. flag = wc_InitSha256(&temp);
  5538. }
  5539. if (flag == 0) {
  5540. flag = wc_Sha256Copy(&sha256, &temp);
  5541. }
  5542. /*test bad arguements*/
  5543. if (flag == 0) {
  5544. flag = wc_Sha256Copy(NULL, NULL);
  5545. if (flag == BAD_FUNC_ARG) {
  5546. flag = 0;
  5547. }
  5548. }
  5549. if (flag == 0) {
  5550. flag = wc_Sha256Copy(NULL, &temp);
  5551. if (flag == BAD_FUNC_ARG) {
  5552. flag = 0;
  5553. }
  5554. }
  5555. if (flag == 0) {
  5556. flag = wc_Sha256Copy(&sha256, NULL);
  5557. if (flag == BAD_FUNC_ARG) {
  5558. flag = 0;
  5559. }
  5560. }
  5561. wc_Sha256Free(&sha256);
  5562. wc_Sha256Free(&temp);
  5563. printf(resultFmt, flag == 0 ? passed : failed);
  5564. #endif
  5565. return flag;
  5566. } /* END test_wc_Sha256Copy */
  5567. /*
  5568. * Testing wc_InitSha512()
  5569. */
  5570. static int test_wc_InitSha512 (void)
  5571. {
  5572. int flag = 0;
  5573. #ifdef WOLFSSL_SHA512
  5574. wc_Sha512 sha512;
  5575. int ret;
  5576. printf(testingFmt, "wc_InitSha512()");
  5577. /* Test good arg. */
  5578. ret = wc_InitSha512(&sha512);
  5579. if (ret != 0) {
  5580. flag = WOLFSSL_FATAL_ERROR;
  5581. }
  5582. /* Test bad arg. */
  5583. if (!flag) {
  5584. ret = wc_InitSha512(NULL);
  5585. if (ret != BAD_FUNC_ARG) {
  5586. flag = WOLFSSL_FATAL_ERROR;
  5587. }
  5588. }
  5589. wc_Sha512Free(&sha512);
  5590. printf(resultFmt, flag == 0 ? passed : failed);
  5591. #endif
  5592. return flag;
  5593. } /* END test_wc_InitSha512 */
  5594. /*
  5595. * wc_Sha512Update() test.
  5596. */
  5597. static int test_wc_Sha512Update (void)
  5598. {
  5599. int flag = 0;
  5600. #ifdef WOLFSSL_SHA512
  5601. wc_Sha512 sha512;
  5602. byte hash[WC_SHA512_DIGEST_SIZE];
  5603. testVector a, b, c;
  5604. int ret;
  5605. ret = wc_InitSha512(&sha512);
  5606. if (ret != 0) {
  5607. flag = ret;
  5608. }
  5609. printf(testingFmt, "wc_Sha512Update()");
  5610. /* Input. */
  5611. if (!flag) {
  5612. a.input = "a";
  5613. a.inLen = XSTRLEN(a.input);
  5614. ret = wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen);
  5615. if (ret != 0) {
  5616. flag = ret;
  5617. }
  5618. ret = wc_Sha512Final(&sha512, hash);
  5619. if (ret != 0) {
  5620. flag = ret;
  5621. }
  5622. }
  5623. /* Update input. */
  5624. if (!flag) {
  5625. a.input = "abc";
  5626. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  5627. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  5628. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  5629. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  5630. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  5631. a.inLen = XSTRLEN(a.input);
  5632. a.outLen = XSTRLEN(a.output);
  5633. ret = wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen);
  5634. if (ret != 0) {
  5635. flag = ret;
  5636. }
  5637. }
  5638. if (!flag) {
  5639. ret = wc_Sha512Final(&sha512, hash);
  5640. if (ret != 0) {
  5641. flag = ret;
  5642. }
  5643. }
  5644. if (!flag) {
  5645. if (XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE) != 0) {
  5646. flag = WOLFSSL_FATAL_ERROR;
  5647. }
  5648. }
  5649. /* Try passing in bad values */
  5650. if (!flag) {
  5651. b.input = NULL;
  5652. b.inLen = 0;
  5653. ret = wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen);
  5654. if (ret != 0) {
  5655. flag = ret;
  5656. }
  5657. }
  5658. if (!flag) {
  5659. c.input = NULL;
  5660. c.inLen = WC_SHA512_DIGEST_SIZE;
  5661. ret = wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen);
  5662. if (ret != BAD_FUNC_ARG) {
  5663. flag = WOLFSSL_FATAL_ERROR;
  5664. }
  5665. }
  5666. if (!flag) {
  5667. ret = wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen);
  5668. if (ret != BAD_FUNC_ARG) {
  5669. flag = WOLFSSL_FATAL_ERROR;
  5670. }
  5671. }
  5672. wc_Sha512Free(&sha512);
  5673. /* If not returned then the unit test passed test vectors. */
  5674. printf(resultFmt, flag == 0 ? passed : failed);
  5675. #endif
  5676. return flag;
  5677. } /* END test_wc_Sha512Update */
  5678. /*
  5679. * Unit test function for wc_Sha512Final()
  5680. */
  5681. static int test_wc_Sha512Final (void)
  5682. {
  5683. int flag = 0;
  5684. #ifdef WOLFSSL_SHA512
  5685. wc_Sha512 sha512;
  5686. byte* hash_test[3];
  5687. byte hash1[WC_SHA512_DIGEST_SIZE];
  5688. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  5689. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  5690. int times, i, ret;
  5691. /* Initialize */
  5692. ret = wc_InitSha512(&sha512);
  5693. if (ret != 0) {
  5694. flag = ret;
  5695. }
  5696. if (!flag) {
  5697. hash_test[0] = hash1;
  5698. hash_test[1] = hash2;
  5699. hash_test[2] = hash3;
  5700. }
  5701. times = sizeof(hash_test) / sizeof(byte *);
  5702. /* Good test args. */
  5703. printf(testingFmt, "wc_Sha512Final()");
  5704. for (i = 0; i < times; i++) {
  5705. if (!flag) {
  5706. ret = wc_Sha512Final(&sha512, hash_test[i]);
  5707. if (ret != 0) {
  5708. flag = WOLFSSL_FATAL_ERROR;
  5709. }
  5710. }
  5711. }
  5712. /* Test bad args. */
  5713. if (!flag) {
  5714. ret = wc_Sha512Final(NULL, NULL);
  5715. if (ret != BAD_FUNC_ARG) {
  5716. flag = WOLFSSL_FATAL_ERROR;
  5717. }
  5718. if (!flag) {}
  5719. ret = wc_Sha512Final(NULL, hash1);
  5720. if (ret != BAD_FUNC_ARG) {
  5721. flag = WOLFSSL_FATAL_ERROR;
  5722. }
  5723. }
  5724. if (!flag) {
  5725. ret = wc_Sha512Final(&sha512, NULL);
  5726. if (ret != BAD_FUNC_ARG) {
  5727. flag = WOLFSSL_FATAL_ERROR;
  5728. }
  5729. }
  5730. wc_Sha512Free(&sha512);
  5731. printf(resultFmt, flag == 0 ? passed : failed);
  5732. #endif
  5733. return flag;
  5734. } /* END test_wc_Sha512Final */
  5735. /*
  5736. * Unit test function for wc_Sha512GetFlags()
  5737. */
  5738. static int test_wc_Sha512GetFlags (void)
  5739. {
  5740. int flag = 0;
  5741. #if !defined(NO_SHA512) && \
  5742. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  5743. wc_Sha512 sha512;
  5744. word32 flags = 0;
  5745. printf(testingFmt, "wc_Sha512GetFlags()");
  5746. /* Initialize */
  5747. flag = wc_InitSha512(&sha512);
  5748. if (flag == 0) {
  5749. flag = wc_Sha512GetFlags(&sha512, &flags);
  5750. }
  5751. if (flag == 0) {
  5752. if (flags & WC_HASH_FLAG_ISCOPY) {
  5753. flag = 0;
  5754. }
  5755. }
  5756. wc_Sha512Free(&sha512);
  5757. printf(resultFmt, flag == 0 ? passed : failed);
  5758. #endif
  5759. return flag;
  5760. } /* END test_wc_Sha512GetFlags */
  5761. /*
  5762. * Unit test function for wc_Sha512FinalRaw()
  5763. */
  5764. static int test_wc_Sha512FinalRaw (void)
  5765. {
  5766. int flag = 0;
  5767. #if !defined(NO_SHA512) && \
  5768. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  5769. wc_Sha512 sha512;
  5770. byte* hash_test[3];
  5771. byte hash1[WC_SHA512_DIGEST_SIZE];
  5772. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  5773. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  5774. int times, i, ret;
  5775. /* Initialize */
  5776. ret = wc_InitSha512(&sha512);
  5777. if (ret != 0) {
  5778. flag = ret;
  5779. }
  5780. if (!flag) {
  5781. hash_test[0] = hash1;
  5782. hash_test[1] = hash2;
  5783. hash_test[2] = hash3;
  5784. }
  5785. times = sizeof(hash_test) / sizeof(byte*);
  5786. /* Good test args. */
  5787. printf(testingFmt, "wc_Sha512FinalRaw()");
  5788. for (i = 0; i < times; i++) {
  5789. if (!flag) {
  5790. ret = wc_Sha512FinalRaw(&sha512, hash_test[i]);
  5791. if (ret != 0) {
  5792. flag = WOLFSSL_FATAL_ERROR;
  5793. }
  5794. }
  5795. }
  5796. /* Test bad args. */
  5797. if (!flag ) {
  5798. ret = wc_Sha512FinalRaw(NULL, NULL);
  5799. if (ret != BAD_FUNC_ARG) {
  5800. flag = WOLFSSL_FATAL_ERROR;
  5801. }
  5802. }
  5803. if (!flag) {
  5804. ret = wc_Sha512FinalRaw(NULL, hash1);
  5805. if (ret != BAD_FUNC_ARG) {
  5806. flag = WOLFSSL_FATAL_ERROR;
  5807. }
  5808. }
  5809. if (!flag) {
  5810. ret = wc_Sha512FinalRaw(&sha512, NULL);
  5811. if (ret != BAD_FUNC_ARG) {
  5812. flag = WOLFSSL_FATAL_ERROR;
  5813. }
  5814. }
  5815. wc_Sha512Free(&sha512);
  5816. printf(resultFmt, flag == 0 ? passed : failed);
  5817. #endif
  5818. return flag;
  5819. } /* END test_wc_Sha512FinalRaw */
  5820. /*
  5821. * Unit test function for wc_Sha512Free()
  5822. */
  5823. static int test_wc_Sha512Free (void)
  5824. {
  5825. int flag = 0;
  5826. #if !defined(NO_SHA512) && \
  5827. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  5828. printf(testingFmt, "wc_Sha512Free()");
  5829. wc_Sha512Free(NULL);
  5830. printf(resultFmt, flag == 0 ? passed : failed);
  5831. #endif
  5832. return flag;
  5833. } /* END test_wc_Sha512Free */
  5834. /*
  5835. * Unit test function for wc_Sha512GetHash()
  5836. */
  5837. static int test_wc_Sha512GetHash (void)
  5838. {
  5839. int flag = 0;
  5840. #if !defined(NO_SHA512) && \
  5841. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  5842. wc_Sha512 sha512;
  5843. byte hash1[WC_SHA512_DIGEST_SIZE];
  5844. printf(testingFmt, "wc_Sha512GetHash()");
  5845. /* Initialize */
  5846. flag = wc_InitSha512(&sha512);
  5847. if (flag == 0) {
  5848. flag = wc_Sha512GetHash(&sha512, hash1);
  5849. }
  5850. /*test bad arguements*/
  5851. if (flag == 0) {
  5852. flag = wc_Sha512GetHash(NULL, NULL);
  5853. if (flag == BAD_FUNC_ARG) {
  5854. flag = 0;
  5855. }
  5856. }
  5857. if (flag == 0) {
  5858. flag = wc_Sha512GetHash(NULL, hash1);
  5859. if (flag == BAD_FUNC_ARG) {
  5860. flag = 0;
  5861. }
  5862. }
  5863. if (flag == 0) {
  5864. flag = wc_Sha512GetHash(&sha512, NULL);
  5865. if (flag == BAD_FUNC_ARG) {
  5866. flag = 0;
  5867. }
  5868. }
  5869. wc_Sha512Free(&sha512);
  5870. printf(resultFmt, flag == 0 ? passed : failed);
  5871. #endif
  5872. return flag;
  5873. } /* END test_wc_Sha512GetHash */
  5874. /*
  5875. * Unit test function for wc_Sha512Copy()
  5876. */
  5877. static int test_wc_Sha512Copy (void)
  5878. {
  5879. int flag = 0;
  5880. #if !defined(NO_SHA512) && \
  5881. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  5882. wc_Sha512 sha512;
  5883. wc_Sha512 temp;
  5884. printf(testingFmt, "wc_Sha512Copy()");
  5885. /* Initialize */
  5886. flag = wc_InitSha512(&sha512);
  5887. if (flag == 0) {
  5888. flag = wc_InitSha512(&temp);
  5889. }
  5890. if (flag == 0) {
  5891. flag = wc_Sha512Copy(&sha512, &temp);
  5892. }
  5893. /*test bad arguements*/
  5894. if (flag == 0) {
  5895. flag = wc_Sha512Copy(NULL, NULL);
  5896. if (flag == BAD_FUNC_ARG) {
  5897. flag = 0;
  5898. }
  5899. }
  5900. if (flag == 0) {
  5901. flag = wc_Sha512Copy(NULL, &temp);
  5902. if (flag == BAD_FUNC_ARG) {
  5903. flag = 0;
  5904. }
  5905. }
  5906. if (flag == 0) {
  5907. flag = wc_Sha512Copy(&sha512, NULL);
  5908. if (flag == BAD_FUNC_ARG) {
  5909. flag = 0;
  5910. }
  5911. }
  5912. wc_Sha512Free(&sha512);
  5913. wc_Sha512Free(&temp);
  5914. printf(resultFmt, flag == 0 ? passed : failed);
  5915. #endif
  5916. return flag;
  5917. } /* END test_wc_Sha512Copy */
  5918. /*
  5919. * Testing wc_InitSha384()
  5920. */
  5921. static int test_wc_InitSha384 (void)
  5922. {
  5923. int flag = 0;
  5924. #ifdef WOLFSSL_SHA384
  5925. wc_Sha384 sha384;
  5926. int ret;
  5927. printf(testingFmt, "wc_InitSha384()");
  5928. /* Test good arg. */
  5929. ret = wc_InitSha384(&sha384);
  5930. if (ret != 0) {
  5931. flag = WOLFSSL_FATAL_ERROR;
  5932. }
  5933. /* Test bad arg. */
  5934. if (!flag) {
  5935. ret = wc_InitSha384(NULL);
  5936. if (ret != BAD_FUNC_ARG) {
  5937. flag = WOLFSSL_FATAL_ERROR;
  5938. }
  5939. }
  5940. wc_Sha384Free(&sha384);
  5941. printf(resultFmt, flag == 0 ? passed : failed);
  5942. #endif
  5943. return flag;
  5944. } /* END test_wc_InitSha384 */
  5945. /*
  5946. * test wc_Sha384Update()
  5947. */
  5948. static int test_wc_Sha384Update (void)
  5949. {
  5950. int flag = 0;
  5951. #ifdef WOLFSSL_SHA384
  5952. wc_Sha384 sha384;
  5953. byte hash[WC_SHA384_DIGEST_SIZE];
  5954. testVector a, b, c;
  5955. int ret;
  5956. ret = wc_InitSha384(&sha384);
  5957. if (ret != 0) {
  5958. flag = ret;
  5959. }
  5960. printf(testingFmt, "wc_Sha384Update()");
  5961. /* Input */
  5962. if (!flag) {
  5963. a.input = "a";
  5964. a.inLen = XSTRLEN(a.input);
  5965. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  5966. if (ret != 0) {
  5967. flag = ret;
  5968. }
  5969. }
  5970. if (!flag) {
  5971. ret = wc_Sha384Final(&sha384, hash);
  5972. if (ret != 0) {
  5973. flag = ret;
  5974. }
  5975. }
  5976. /* Update input. */
  5977. if (!flag) {
  5978. a.input = "abc";
  5979. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  5980. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  5981. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  5982. "\xc8\x25\xa7";
  5983. a.inLen = XSTRLEN(a.input);
  5984. a.outLen = XSTRLEN(a.output);
  5985. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  5986. if (ret != 0) {
  5987. flag = ret;
  5988. }
  5989. }
  5990. if (!flag) {
  5991. ret = wc_Sha384Final(&sha384, hash);
  5992. if (ret != 0) {
  5993. flag = ret;
  5994. }
  5995. }
  5996. if (!flag) {
  5997. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  5998. flag = WOLFSSL_FATAL_ERROR;
  5999. }
  6000. }
  6001. /* Pass in bad values. */
  6002. if (!flag) {
  6003. b.input = NULL;
  6004. b.inLen = 0;
  6005. ret = wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen);
  6006. if (ret != 0) {
  6007. flag = ret;
  6008. }
  6009. }
  6010. if (!flag) {
  6011. c.input = NULL;
  6012. c.inLen = WC_SHA384_DIGEST_SIZE;
  6013. ret = wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen);
  6014. if (ret != BAD_FUNC_ARG) {
  6015. flag = WOLFSSL_FATAL_ERROR;
  6016. }
  6017. }
  6018. if (!flag) {
  6019. ret = wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen);
  6020. if (ret != BAD_FUNC_ARG) {
  6021. flag = WOLFSSL_FATAL_ERROR;
  6022. }
  6023. }
  6024. wc_Sha384Free(&sha384);
  6025. /* If not returned then the unit test passed test vectors. */
  6026. printf(resultFmt, flag == 0 ? passed : failed);
  6027. #endif
  6028. return flag;
  6029. } /* END test_wc_Sha384Update */
  6030. /*
  6031. * Unit test function for wc_Sha384Final();
  6032. */
  6033. static int test_wc_Sha384Final (void)
  6034. {
  6035. int flag = 0;
  6036. #ifdef WOLFSSL_SHA384
  6037. wc_Sha384 sha384;
  6038. byte* hash_test[3];
  6039. byte hash1[WC_SHA384_DIGEST_SIZE];
  6040. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  6041. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  6042. int times, i, ret;
  6043. /* Initialize */
  6044. ret = wc_InitSha384(&sha384);
  6045. if (ret) {
  6046. flag = ret;
  6047. }
  6048. if (!flag) {
  6049. hash_test[0] = hash1;
  6050. hash_test[1] = hash2;
  6051. hash_test[2] = hash3;
  6052. }
  6053. times = sizeof(hash_test) / sizeof(byte*);
  6054. /* Good test args. */
  6055. printf(testingFmt, "wc_Sha384Final()");
  6056. for (i = 0; i < times; i++) {
  6057. if (!flag) {
  6058. ret = wc_Sha384Final(&sha384, hash_test[i]);
  6059. if (ret != 0) {
  6060. flag = WOLFSSL_FATAL_ERROR;
  6061. }
  6062. }
  6063. }
  6064. /* Test bad args. */
  6065. if (!flag) {
  6066. ret = wc_Sha384Final(NULL, NULL);
  6067. if (ret != BAD_FUNC_ARG) {
  6068. flag = WOLFSSL_FATAL_ERROR;
  6069. }
  6070. }
  6071. if (!flag) {
  6072. ret = wc_Sha384Final(NULL, hash1);
  6073. if (ret != BAD_FUNC_ARG) {
  6074. flag = WOLFSSL_FATAL_ERROR;
  6075. }
  6076. }
  6077. if (!flag) {
  6078. ret = wc_Sha384Final(&sha384, NULL);
  6079. if (ret != BAD_FUNC_ARG) {
  6080. flag = WOLFSSL_FATAL_ERROR;
  6081. }
  6082. }
  6083. wc_Sha384Free(&sha384);
  6084. printf(resultFmt, flag == 0 ? passed : failed);
  6085. #endif
  6086. return flag;
  6087. } /* END test_wc_Sha384Final */
  6088. /*
  6089. * Unit test function for wc_Sha384GetFlags()
  6090. */
  6091. static int test_wc_Sha384GetFlags (void)
  6092. {
  6093. int flag = 0;
  6094. #if !defined(NO_SHA384) && \
  6095. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  6096. wc_Sha384 sha384;
  6097. word32 flags = 0;
  6098. printf(testingFmt, "wc_Sha384GetFlags()");
  6099. /* Initialize */
  6100. flag = wc_InitSha384(&sha384);
  6101. if (flag == 0) {
  6102. flag = wc_Sha384GetFlags(&sha384, &flags);
  6103. }
  6104. if (flag == 0) {
  6105. if (flags & WC_HASH_FLAG_ISCOPY) {
  6106. flag = 0;
  6107. }
  6108. }
  6109. wc_Sha384Free(&sha384);
  6110. printf(resultFmt, flag == 0 ? passed : failed);
  6111. #endif
  6112. return flag;
  6113. } /* END test_wc_Sha384GetFlags */
  6114. /*
  6115. * Unit test function for wc_Sha384FinalRaw()
  6116. */
  6117. static int test_wc_Sha384FinalRaw (void)
  6118. {
  6119. int flag = 0;
  6120. #if !defined(NO_SHA384) && \
  6121. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  6122. wc_Sha384 sha384;
  6123. byte* hash_test[3];
  6124. byte hash1[WC_SHA384_DIGEST_SIZE];
  6125. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  6126. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  6127. int times, i, ret;
  6128. /* Initialize */
  6129. ret = wc_InitSha384(&sha384);
  6130. if (ret != 0) {
  6131. flag = ret;
  6132. }
  6133. if (!flag) {
  6134. hash_test[0] = hash1;
  6135. hash_test[1] = hash2;
  6136. hash_test[2] = hash3;
  6137. }
  6138. times = sizeof(hash_test) / sizeof(byte*);
  6139. /* Good test args. */
  6140. printf(testingFmt, "wc_Sha384FinalRaw()");
  6141. for (i = 0; i < times; i++) {
  6142. if (!flag) {
  6143. ret = wc_Sha384FinalRaw(&sha384, hash_test[i]);
  6144. if (ret != 0) {
  6145. flag = WOLFSSL_FATAL_ERROR;
  6146. }
  6147. }
  6148. }
  6149. /* Test bad args. */
  6150. if (!flag ) {
  6151. ret = wc_Sha384FinalRaw(NULL, NULL);
  6152. if (ret != BAD_FUNC_ARG) {
  6153. flag = WOLFSSL_FATAL_ERROR;
  6154. }
  6155. }
  6156. if (!flag) {
  6157. ret = wc_Sha384FinalRaw(NULL, hash1);
  6158. if (ret != BAD_FUNC_ARG) {
  6159. flag = WOLFSSL_FATAL_ERROR;
  6160. }
  6161. }
  6162. if (!flag) {
  6163. ret = wc_Sha384FinalRaw(&sha384, NULL);
  6164. if (ret != BAD_FUNC_ARG) {
  6165. flag = WOLFSSL_FATAL_ERROR;
  6166. }
  6167. }
  6168. wc_Sha384Free(&sha384);
  6169. printf(resultFmt, flag == 0 ? passed : failed);
  6170. #endif
  6171. return flag;
  6172. } /* END test_wc_Sha384FinalRaw */
  6173. /*
  6174. * Unit test function for wc_Sha384Free()
  6175. */
  6176. static int test_wc_Sha384Free (void)
  6177. {
  6178. int flag = 0;
  6179. #if !defined(NO_SHA384) && \
  6180. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  6181. printf(testingFmt, "wc_Sha384Free()");
  6182. wc_Sha384Free(NULL);
  6183. printf(resultFmt, flag == 0 ? passed : failed);
  6184. #endif
  6185. return flag;
  6186. } /* END test_wc_Sha384Free */
  6187. /*
  6188. * Unit test function for wc_Sha384GetHash()
  6189. */
  6190. static int test_wc_Sha384GetHash (void)
  6191. {
  6192. int flag = 0;
  6193. #if !defined(NO_SHA384) && \
  6194. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  6195. wc_Sha384 sha384;
  6196. byte hash1[WC_SHA384_DIGEST_SIZE];
  6197. printf(testingFmt, "wc_Sha384GetHash()");
  6198. /* Initialize */
  6199. flag = wc_InitSha384(&sha384);
  6200. if (flag == 0) {
  6201. flag = wc_Sha384GetHash(&sha384, hash1);
  6202. }
  6203. /*test bad arguements*/
  6204. if (flag == 0) {
  6205. flag = wc_Sha384GetHash(NULL, NULL);
  6206. if (flag == BAD_FUNC_ARG) {
  6207. flag = 0;
  6208. }
  6209. }
  6210. if (flag == 0) {
  6211. flag = wc_Sha384GetHash(NULL, hash1);
  6212. if (flag == BAD_FUNC_ARG) {
  6213. flag = 0;
  6214. }
  6215. }
  6216. if (flag == 0) {
  6217. flag = wc_Sha384GetHash(&sha384, NULL);
  6218. if (flag == BAD_FUNC_ARG) {
  6219. flag = 0;
  6220. }
  6221. }
  6222. wc_Sha384Free(&sha384);
  6223. printf(resultFmt, flag == 0 ? passed : failed);
  6224. #endif
  6225. return flag;
  6226. } /* END test_wc_Sha384GetHash */
  6227. /*
  6228. * Unit test function for wc_Sha384Copy()
  6229. */
  6230. static int test_wc_Sha384Copy (void)
  6231. {
  6232. int flag = 0;
  6233. #if !defined(NO_SHA384) && \
  6234. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  6235. wc_Sha384 sha384;
  6236. wc_Sha384 temp;
  6237. printf(testingFmt, "wc_Sha384Copy()");
  6238. /* Initialize */
  6239. flag = wc_InitSha384(&sha384);
  6240. if (flag == 0) {
  6241. flag = wc_InitSha384(&temp);
  6242. }
  6243. if (flag == 0) {
  6244. flag = wc_Sha384Copy(&sha384, &temp);
  6245. }
  6246. /*test bad arguements*/
  6247. if (flag == 0) {
  6248. flag = wc_Sha384Copy(NULL, NULL);
  6249. if (flag == BAD_FUNC_ARG) {
  6250. flag = 0;
  6251. }
  6252. }
  6253. if (flag == 0) {
  6254. flag = wc_Sha384Copy(NULL, &temp);
  6255. if (flag == BAD_FUNC_ARG) {
  6256. flag = 0;
  6257. }
  6258. }
  6259. if (flag == 0) {
  6260. flag = wc_Sha384Copy(&sha384, NULL);
  6261. if (flag == BAD_FUNC_ARG) {
  6262. flag = 0;
  6263. }
  6264. }
  6265. wc_Sha384Free(&sha384);
  6266. wc_Sha384Free(&temp);
  6267. printf(resultFmt, flag == 0 ? passed : failed);
  6268. #endif
  6269. return flag;
  6270. } /* END test_wc_Sha384Copy */
  6271. /*
  6272. * Testing wc_InitSha224();
  6273. */
  6274. static int test_wc_InitSha224 (void)
  6275. {
  6276. int flag = 0;
  6277. #ifdef WOLFSSL_SHA224
  6278. wc_Sha224 sha224;
  6279. int ret;
  6280. printf(testingFmt, "wc_InitSha224()");
  6281. /* Test good arg. */
  6282. ret = wc_InitSha224(&sha224);
  6283. if (ret != 0) {
  6284. flag = WOLFSSL_FATAL_ERROR;
  6285. }
  6286. /* Test bad arg. */
  6287. if (!flag) {
  6288. ret = wc_InitSha224(NULL);
  6289. if (ret != BAD_FUNC_ARG) {
  6290. flag = WOLFSSL_FATAL_ERROR;
  6291. }
  6292. }
  6293. wc_Sha224Free(&sha224);
  6294. printf(resultFmt, flag == 0 ? passed : failed);
  6295. #endif
  6296. return flag;
  6297. } /* END test_wc_InitSha224 */
  6298. /*
  6299. * Unit test on wc_Sha224Update
  6300. */
  6301. static int test_wc_Sha224Update (void)
  6302. {
  6303. int flag = 0;
  6304. #ifdef WOLFSSL_SHA224
  6305. wc_Sha224 sha224;
  6306. byte hash[WC_SHA224_DIGEST_SIZE];
  6307. testVector a, b, c;
  6308. int ret;
  6309. ret = wc_InitSha224(&sha224);
  6310. if (ret != 0) {
  6311. flag = ret;
  6312. }
  6313. printf(testingFmt, "wc_Sha224Update()");
  6314. /* Input. */
  6315. if (!flag) {
  6316. a.input = "a";
  6317. a.inLen = XSTRLEN(a.input);
  6318. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  6319. if (ret != 0) {
  6320. flag = ret;
  6321. }
  6322. }
  6323. if (!flag) {
  6324. ret = wc_Sha224Final(&sha224, hash);
  6325. if (ret != 0) {
  6326. flag = ret;
  6327. }
  6328. }
  6329. /* Update input. */
  6330. if (!flag) {
  6331. a.input = "abc";
  6332. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  6333. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  6334. a.inLen = XSTRLEN(a.input);
  6335. a.outLen = XSTRLEN(a.output);
  6336. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  6337. if (ret != 0) {
  6338. flag = ret;
  6339. }
  6340. }
  6341. if (!flag) {
  6342. ret = wc_Sha224Final(&sha224, hash);
  6343. if (ret != 0) {
  6344. flag = ret;
  6345. }
  6346. }
  6347. if (!flag) {
  6348. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  6349. flag = WOLFSSL_FATAL_ERROR;
  6350. }
  6351. }
  6352. /* Pass in bad values. */
  6353. if (!flag) {
  6354. b.input = NULL;
  6355. b.inLen = 0;
  6356. ret = wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen);
  6357. if (ret != 0) {
  6358. flag = ret;
  6359. }
  6360. }
  6361. if (!flag) {
  6362. c.input = NULL;
  6363. c.inLen = WC_SHA224_DIGEST_SIZE;
  6364. ret = wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen);
  6365. if (ret != BAD_FUNC_ARG) {
  6366. flag = WOLFSSL_FATAL_ERROR;
  6367. }
  6368. }
  6369. if (!flag) {
  6370. ret = wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen);
  6371. if (ret != BAD_FUNC_ARG) {
  6372. flag = WOLFSSL_FATAL_ERROR;
  6373. }
  6374. }
  6375. wc_Sha224Free(&sha224);
  6376. /* If not returned then the unit test passed test vectors. */
  6377. printf(resultFmt, flag == 0 ? passed : failed);
  6378. #endif
  6379. return flag;
  6380. } /* END test_wc_Sha224Update */
  6381. /*
  6382. * Unit test for wc_Sha224Final();
  6383. */
  6384. static int test_wc_Sha224Final (void)
  6385. {
  6386. int flag = 0;
  6387. #ifdef WOLFSSL_SHA224
  6388. wc_Sha224 sha224;
  6389. byte* hash_test[3];
  6390. byte hash1[WC_SHA224_DIGEST_SIZE];
  6391. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  6392. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  6393. int times, i, ret;
  6394. /* Initialize */
  6395. ret = wc_InitSha224(&sha224);
  6396. if (ret) {
  6397. flag = ret;
  6398. }
  6399. if (!flag) {
  6400. hash_test[0] = hash1;
  6401. hash_test[1] = hash2;
  6402. hash_test[2] = hash3;
  6403. }
  6404. times = sizeof(hash_test) / sizeof(byte*);
  6405. /* Good test args. */
  6406. printf(testingFmt, "wc_sha224Final()");
  6407. /* Testing oversized buffers. */
  6408. for (i = 0; i < times; i++) {
  6409. if (!flag) {
  6410. ret = wc_Sha224Final(&sha224, hash_test[i]);
  6411. if (ret != 0) {
  6412. flag = WOLFSSL_FATAL_ERROR;
  6413. }
  6414. }
  6415. }
  6416. /* Test bad args. */
  6417. if (!flag) {
  6418. ret = wc_Sha224Final(NULL, NULL);
  6419. if (ret != BAD_FUNC_ARG) {
  6420. flag = WOLFSSL_FATAL_ERROR;
  6421. }
  6422. }
  6423. if (!flag) {
  6424. ret = wc_Sha224Final(NULL, hash1);
  6425. if (ret != BAD_FUNC_ARG) {
  6426. flag = WOLFSSL_FATAL_ERROR;
  6427. }
  6428. }
  6429. if (!flag) {
  6430. ret = wc_Sha224Final(&sha224, NULL);
  6431. if (ret != BAD_FUNC_ARG) {
  6432. flag = WOLFSSL_FATAL_ERROR;
  6433. }
  6434. }
  6435. wc_Sha224Free(&sha224);
  6436. printf(resultFmt, flag == 0 ? passed : failed);
  6437. #endif
  6438. return flag;
  6439. } /* END test_wc_Sha224Final */
  6440. /*
  6441. * Unit test function for wc_Sha224SetFlags()
  6442. */
  6443. static int test_wc_Sha224SetFlags (void)
  6444. {
  6445. int flag = 0;
  6446. #if defined(WOLFSSL_SHA224) && \
  6447. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  6448. wc_Sha224 sha224;
  6449. word32 flags = 0;
  6450. printf(testingFmt, "wc_Sha224SetFlags()");
  6451. /* Initialize */
  6452. flag = wc_InitSha224(&sha224);
  6453. if (flag == 0) {
  6454. flag = wc_Sha224SetFlags(&sha224, flags);
  6455. }
  6456. if (flag == 0) {
  6457. if (flags & WC_HASH_FLAG_ISCOPY) {
  6458. flag = 0;
  6459. }
  6460. }
  6461. wc_Sha224Free(&sha224);
  6462. printf(resultFmt, flag == 0 ? passed : failed);
  6463. #endif
  6464. return flag;
  6465. } /* END test_wc_Sha224SetFlags */
  6466. /*
  6467. * Unit test function for wc_Sha224GetFlags()
  6468. */
  6469. static int test_wc_Sha224GetFlags (void)
  6470. {
  6471. int flag = 0;
  6472. #if defined(WOLFSSL_SHA224) && \
  6473. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  6474. wc_Sha224 sha224;
  6475. word32 flags = 0;
  6476. printf(testingFmt, "wc_Sha224GetFlags()");
  6477. /* Initialize */
  6478. flag = wc_InitSha224(&sha224);
  6479. if (flag == 0) {
  6480. flag = wc_Sha224GetFlags(&sha224, &flags);
  6481. }
  6482. if (flag == 0) {
  6483. if (flags & WC_HASH_FLAG_ISCOPY) {
  6484. flag = 0;
  6485. }
  6486. }
  6487. wc_Sha224Free(&sha224);
  6488. printf(resultFmt, flag == 0 ? passed : failed);
  6489. #endif
  6490. return flag;
  6491. } /* END test_wc_Sha224GetFlags */
  6492. /*
  6493. * Unit test function for wc_Sha224Free()
  6494. */
  6495. static int test_wc_Sha224Free (void)
  6496. {
  6497. int flag = 0;
  6498. #if defined(WOLFSSL_SHA224) && \
  6499. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  6500. printf(testingFmt, "wc_Sha224Free()");
  6501. wc_Sha224Free(NULL);
  6502. printf(resultFmt, flag == 0 ? passed : failed);
  6503. #endif
  6504. return flag;
  6505. } /* END test_wc_Sha224Free */
  6506. /*
  6507. * Unit test function for wc_Sha224GetHash()
  6508. */
  6509. static int test_wc_Sha224GetHash (void)
  6510. {
  6511. int flag = 0;
  6512. #if defined(WOLFSSL_SHA224) && \
  6513. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  6514. wc_Sha224 sha224;
  6515. byte hash1[WC_SHA224_DIGEST_SIZE];
  6516. printf(testingFmt, "wc_Sha224GetHash()");
  6517. /* Initialize */
  6518. flag = wc_InitSha224(&sha224);
  6519. if (flag == 0) {
  6520. flag = wc_Sha224GetHash(&sha224, hash1);
  6521. }
  6522. /*test bad arguements*/
  6523. if (flag == 0) {
  6524. flag = wc_Sha224GetHash(NULL, NULL);
  6525. if (flag == BAD_FUNC_ARG) {
  6526. flag = 0;
  6527. }
  6528. }
  6529. if (flag == 0) {
  6530. flag = wc_Sha224GetHash(NULL, hash1);
  6531. if (flag == BAD_FUNC_ARG) {
  6532. flag = 0;
  6533. }
  6534. }
  6535. if (flag == 0) {
  6536. flag = wc_Sha224GetHash(&sha224, NULL);
  6537. if (flag == BAD_FUNC_ARG) {
  6538. flag = 0;
  6539. }
  6540. }
  6541. wc_Sha224Free(&sha224);
  6542. printf(resultFmt, flag == 0 ? passed : failed);
  6543. #endif
  6544. return flag;
  6545. } /* END test_wc_Sha224GetHash */
  6546. /*
  6547. * Unit test function for wc_Sha224Copy()
  6548. */
  6549. static int test_wc_Sha224Copy (void)
  6550. {
  6551. int flag = 0;
  6552. #if defined(WOLFSSL_SHA224) && \
  6553. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  6554. wc_Sha224 sha224;
  6555. wc_Sha224 temp;
  6556. printf(testingFmt, "wc_Sha224Copy()");
  6557. /* Initialize */
  6558. flag = wc_InitSha224(&sha224);
  6559. if (flag == 0) {
  6560. flag = wc_InitSha224(&temp);
  6561. }
  6562. if (flag == 0) {
  6563. flag = wc_Sha224Copy(&sha224, &temp);
  6564. }
  6565. /*test bad arguements*/
  6566. if (flag == 0) {
  6567. flag = wc_Sha224Copy(NULL, NULL);
  6568. if (flag == BAD_FUNC_ARG) {
  6569. flag = 0;
  6570. }
  6571. }
  6572. if (flag == 0) {
  6573. flag = wc_Sha224Copy(NULL, &temp);
  6574. if (flag == BAD_FUNC_ARG) {
  6575. flag = 0;
  6576. }
  6577. }
  6578. if (flag == 0) {
  6579. flag = wc_Sha224Copy(&sha224, NULL);
  6580. if (flag == BAD_FUNC_ARG) {
  6581. flag = 0;
  6582. }
  6583. }
  6584. wc_Sha224Free(&sha224);
  6585. wc_Sha224Free(&temp);
  6586. printf(resultFmt, flag == 0 ? passed : failed);
  6587. #endif
  6588. return flag;
  6589. } /* END test_wc_Sha224Copy */
  6590. /*
  6591. * Testing wc_InitRipeMd()
  6592. */
  6593. static int test_wc_InitRipeMd (void)
  6594. {
  6595. int flag = 0;
  6596. #ifdef WOLFSSL_RIPEMD
  6597. RipeMd ripemd;
  6598. int ret;
  6599. printf(testingFmt, "wc_InitRipeMd()");
  6600. /* Test good arg. */
  6601. ret = wc_InitRipeMd(&ripemd);
  6602. if (ret != 0) {
  6603. flag = WOLFSSL_FATAL_ERROR;
  6604. }
  6605. /* Test bad arg. */
  6606. if (!flag) {
  6607. ret = wc_InitRipeMd(NULL);
  6608. if (ret != BAD_FUNC_ARG) {
  6609. flag = WOLFSSL_FATAL_ERROR;
  6610. }
  6611. }
  6612. printf(resultFmt, flag == 0 ? passed : failed);
  6613. #endif
  6614. return flag;
  6615. } /* END test_wc_InitRipeMd */
  6616. /*
  6617. * Testing wc_RipeMdUpdate()
  6618. */
  6619. static int test_wc_RipeMdUpdate (void)
  6620. {
  6621. int flag = 0;
  6622. #ifdef WOLFSSL_RIPEMD
  6623. RipeMd ripemd;
  6624. byte hash[RIPEMD_DIGEST_SIZE];
  6625. testVector a, b, c;
  6626. int ret;
  6627. ret = wc_InitRipeMd(&ripemd);
  6628. if (ret != 0) {
  6629. flag = ret;
  6630. }
  6631. printf(testingFmt, "wc_RipeMdUpdate()");
  6632. /* Input */
  6633. if (!flag) {
  6634. a.input = "a";
  6635. a.inLen = XSTRLEN(a.input);
  6636. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  6637. if (ret != 0) {
  6638. flag = ret;
  6639. }
  6640. }
  6641. if (!flag) {
  6642. ret = wc_RipeMdFinal(&ripemd, hash);
  6643. if (ret != 0) {
  6644. flag = ret;
  6645. }
  6646. }
  6647. /* Update input. */
  6648. if (!flag) {
  6649. a.input = "abc";
  6650. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  6651. "\xb0\x87\xf1\x5a\x0b\xfc";
  6652. a.inLen = XSTRLEN(a.input);
  6653. a.outLen = XSTRLEN(a.output);
  6654. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  6655. if (ret != 0) {
  6656. flag = ret;
  6657. }
  6658. }
  6659. if (!flag) {
  6660. ret = wc_RipeMdFinal(&ripemd, hash);
  6661. if (ret != 0) {
  6662. flag = ret;
  6663. }
  6664. }
  6665. if (!flag) {
  6666. if (XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE) != 0) {
  6667. flag = WOLFSSL_FATAL_ERROR;
  6668. }
  6669. }
  6670. /* Pass in bad values. */
  6671. if (!flag) {
  6672. b.input = NULL;
  6673. b.inLen = 0;
  6674. ret = wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen);
  6675. if (ret != 0) {
  6676. flag = ret;
  6677. }
  6678. }
  6679. if (!flag) {
  6680. c.input = NULL;
  6681. c.inLen = RIPEMD_DIGEST_SIZE;
  6682. ret = wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen);
  6683. if (ret != BAD_FUNC_ARG) {
  6684. flag = WOLFSSL_FATAL_ERROR;
  6685. }
  6686. }
  6687. if (!flag) {
  6688. ret = wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  6689. if (ret != BAD_FUNC_ARG) {
  6690. flag = WOLFSSL_FATAL_ERROR;
  6691. }
  6692. }
  6693. printf(resultFmt, flag == 0 ? passed : failed);
  6694. #endif
  6695. return flag;
  6696. } /* END test_wc_RipeMdUdpate */
  6697. /*
  6698. * Unit test function for wc_RipeMdFinal()
  6699. */
  6700. static int test_wc_RipeMdFinal (void)
  6701. {
  6702. int flag = 0;
  6703. #ifdef WOLFSSL_RIPEMD
  6704. RipeMd ripemd;
  6705. byte* hash_test[3];
  6706. byte hash1[RIPEMD_DIGEST_SIZE];
  6707. byte hash2[2*RIPEMD_DIGEST_SIZE];
  6708. byte hash3[5*RIPEMD_DIGEST_SIZE];
  6709. int times, i, ret;
  6710. /* Initialize */
  6711. ret = wc_InitRipeMd(&ripemd);
  6712. if (ret != 0) {
  6713. flag = ret;
  6714. }
  6715. if (!flag) {
  6716. hash_test[0] = hash1;
  6717. hash_test[1] = hash2;
  6718. hash_test[2] = hash3;
  6719. }
  6720. times = sizeof(hash_test) / sizeof(byte*);
  6721. /* Good test args. */
  6722. printf(testingFmt, "wc_RipeMdFinal()");
  6723. /* Testing oversized buffers. */
  6724. for (i = 0; i < times; i++) {
  6725. if (!flag) {
  6726. ret = wc_RipeMdFinal(&ripemd, hash_test[i]);
  6727. if (ret != 0) {
  6728. flag = WOLFSSL_FATAL_ERROR;
  6729. }
  6730. }
  6731. }
  6732. /* Test bad args. */
  6733. if (!flag) {
  6734. ret = wc_RipeMdFinal(NULL, NULL);
  6735. if (ret != BAD_FUNC_ARG) {
  6736. flag = WOLFSSL_FATAL_ERROR;
  6737. }
  6738. }
  6739. if (!flag) {
  6740. ret = wc_RipeMdFinal(NULL, hash1);
  6741. if (ret != BAD_FUNC_ARG) {
  6742. flag = WOLFSSL_FATAL_ERROR;
  6743. }
  6744. }
  6745. if (!flag) {
  6746. ret = wc_RipeMdFinal(&ripemd, NULL);
  6747. if (ret != BAD_FUNC_ARG) {
  6748. flag = WOLFSSL_FATAL_ERROR;
  6749. }
  6750. }
  6751. printf(resultFmt, flag == 0 ? passed : failed);
  6752. #endif
  6753. return flag;
  6754. } /* END test_wc_RipeMdFinal */
  6755. /*
  6756. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  6757. * wc_InitSha3_512
  6758. */
  6759. static int test_wc_InitSha3 (void)
  6760. {
  6761. int ret = 0;
  6762. #if defined(WOLFSSL_SHA3)
  6763. wc_Sha3 sha3;
  6764. (void)sha3;
  6765. #if !defined(WOLFSSL_NOSHA3_224)
  6766. printf(testingFmt, "wc_InitSha3_224()");
  6767. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  6768. /* Test bad args. */
  6769. if (ret == 0) {
  6770. ret = wc_InitSha3_224(NULL, HEAP_HINT, devId);
  6771. if (ret == BAD_FUNC_ARG) {
  6772. ret = 0;
  6773. } else if (ret == 0) {
  6774. ret = WOLFSSL_FATAL_ERROR;
  6775. }
  6776. }
  6777. wc_Sha3_224_Free(&sha3);
  6778. printf(resultFmt, ret == 0 ? passed : failed);
  6779. #endif /* NOSHA3_224 */
  6780. #if !defined(WOLFSSL_NOSHA3_256)
  6781. if (ret == 0) {
  6782. printf(testingFmt, "wc_InitSha3_256()");
  6783. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  6784. /* Test bad args. */
  6785. if (ret == 0) {
  6786. ret = wc_InitSha3_256(NULL, HEAP_HINT, devId);
  6787. if (ret == BAD_FUNC_ARG) {
  6788. ret = 0;
  6789. } else if (ret == 0) {
  6790. ret = WOLFSSL_FATAL_ERROR;
  6791. }
  6792. }
  6793. wc_Sha3_256_Free(&sha3);
  6794. printf(resultFmt, ret == 0 ? passed : failed);
  6795. } /* END sha3_256 */
  6796. #endif /* NOSHA3_256 */
  6797. #if !defined(WOLFSSL_NOSHA3_384)
  6798. if (ret == 0) {
  6799. printf(testingFmt, "wc_InitSha3_384()");
  6800. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  6801. /* Test bad args. */
  6802. if (ret == 0) {
  6803. ret = wc_InitSha3_384(NULL, HEAP_HINT, devId);
  6804. if (ret == BAD_FUNC_ARG) {
  6805. ret = 0;
  6806. } else if (ret == 0) {
  6807. ret = WOLFSSL_FATAL_ERROR;
  6808. }
  6809. }
  6810. wc_Sha3_384_Free(&sha3);
  6811. printf(resultFmt, ret == 0 ? passed : failed);
  6812. } /* END sha3_384 */
  6813. #endif /* NOSHA3_384 */
  6814. #if !defined(WOLFSSL_NOSHA3_512)
  6815. if (ret == 0) {
  6816. printf(testingFmt, "wc_InitSha3_512()");
  6817. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  6818. /* Test bad args. */
  6819. if (ret == 0) {
  6820. ret = wc_InitSha3_512(NULL, HEAP_HINT, devId);
  6821. if (ret == BAD_FUNC_ARG) {
  6822. ret = 0;
  6823. } else if (ret == 0) {
  6824. ret = WOLFSSL_FATAL_ERROR;
  6825. }
  6826. }
  6827. wc_Sha3_512_Free(&sha3);
  6828. printf(resultFmt, ret == 0 ? passed : failed);
  6829. } /* END sha3_512 */
  6830. #endif /* NOSHA3_512 */
  6831. #endif
  6832. return ret;
  6833. } /* END test_wc_InitSha3 */
  6834. /*
  6835. * Testing wc_Sha3_Update()
  6836. */
  6837. static int testing_wc_Sha3_Update (void)
  6838. {
  6839. int ret = 0;
  6840. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  6841. !defined(WOLFSSL_AFALG_XILINX)
  6842. wc_Sha3 sha3;
  6843. byte msg[] = "Everybody's working for the weekend.";
  6844. byte msg2[] = "Everybody gets Friday off.";
  6845. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  6846. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  6847. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  6848. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  6849. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  6850. word32 msglen = sizeof(msg) - 1;
  6851. word32 msg2len = sizeof(msg2);
  6852. word32 msgCmplen = sizeof(msgCmp);
  6853. #if !defined(WOLFSSL_NOSHA3_224)
  6854. printf(testingFmt, "wc_Sha3_224_Update()");
  6855. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  6856. if (ret != 0) {
  6857. return ret;
  6858. }
  6859. ret = wc_Sha3_224_Update(&sha3, msg, msglen);
  6860. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  6861. ret = WOLFSSL_FATAL_ERROR;
  6862. }
  6863. if (ret == 0) {
  6864. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  6865. if (ret == 0 && XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  6866. ret = WOLFSSL_FATAL_ERROR;
  6867. }
  6868. }
  6869. /* Pass bad args. */
  6870. if (ret == 0) {
  6871. ret = wc_Sha3_224_Update(NULL, msg2, msg2len);
  6872. if (ret == BAD_FUNC_ARG) {
  6873. ret = wc_Sha3_224_Update(&sha3, NULL, 5);
  6874. }
  6875. if (ret == BAD_FUNC_ARG) {
  6876. wc_Sha3_224_Free(&sha3);
  6877. if (wc_InitSha3_224(&sha3, HEAP_HINT, devId)) {
  6878. return ret;
  6879. }
  6880. ret = wc_Sha3_224_Update(&sha3, NULL, 0);
  6881. if (ret == 0) {
  6882. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  6883. }
  6884. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  6885. ret = WOLFSSL_FATAL_ERROR;
  6886. }
  6887. }
  6888. }
  6889. wc_Sha3_224_Free(&sha3);
  6890. printf(resultFmt, ret == 0 ? passed : failed);
  6891. #endif /* SHA3_224 */
  6892. #if !defined(WOLFSSL_NOSHA3_256)
  6893. if (ret == 0) {
  6894. printf(testingFmt, "wc_Sha3_256_Update()");
  6895. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  6896. if (ret != 0) {
  6897. return ret;
  6898. }
  6899. ret = wc_Sha3_256_Update(&sha3, msg, msglen);
  6900. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  6901. ret = WOLFSSL_FATAL_ERROR;
  6902. }
  6903. if (ret == 0) {
  6904. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  6905. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  6906. ret = WOLFSSL_FATAL_ERROR;
  6907. }
  6908. }
  6909. /* Pass bad args. */
  6910. if (ret == 0) {
  6911. ret = wc_Sha3_256_Update(NULL, msg2, msg2len);
  6912. if (ret == BAD_FUNC_ARG) {
  6913. ret = wc_Sha3_256_Update(&sha3, NULL, 5);
  6914. }
  6915. if (ret == BAD_FUNC_ARG) {
  6916. wc_Sha3_256_Free(&sha3);
  6917. if (wc_InitSha3_256(&sha3, HEAP_HINT, devId)) {
  6918. return ret;
  6919. }
  6920. ret = wc_Sha3_256_Update(&sha3, NULL, 0);
  6921. if (ret == 0) {
  6922. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  6923. }
  6924. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  6925. ret = WOLFSSL_FATAL_ERROR;
  6926. }
  6927. }
  6928. }
  6929. wc_Sha3_256_Free(&sha3);
  6930. printf(resultFmt, ret == 0 ? passed : failed);
  6931. }
  6932. #endif /* SHA3_256 */
  6933. #if !defined(WOLFSSL_NOSHA3_384)
  6934. if (ret == 0) {
  6935. printf(testingFmt, "wc_Sha3_384_Update()");
  6936. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  6937. if (ret != 0) {
  6938. return ret;
  6939. }
  6940. ret = wc_Sha3_384_Update(&sha3, msg, msglen);
  6941. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  6942. ret = WOLFSSL_FATAL_ERROR;
  6943. }
  6944. if (ret == 0) {
  6945. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  6946. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  6947. ret = WOLFSSL_FATAL_ERROR;
  6948. }
  6949. }
  6950. /* Pass bad args. */
  6951. if (ret == 0) {
  6952. ret = wc_Sha3_384_Update(NULL, msg2, msg2len);
  6953. if (ret == BAD_FUNC_ARG) {
  6954. ret = wc_Sha3_384_Update(&sha3, NULL, 5);
  6955. }
  6956. if (ret == BAD_FUNC_ARG) {
  6957. wc_Sha3_384_Free(&sha3);
  6958. if (wc_InitSha3_384(&sha3, HEAP_HINT, devId)) {
  6959. return ret;
  6960. }
  6961. ret = wc_Sha3_384_Update(&sha3, NULL, 0);
  6962. if (ret == 0) {
  6963. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  6964. }
  6965. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  6966. ret = WOLFSSL_FATAL_ERROR;
  6967. }
  6968. }
  6969. }
  6970. wc_Sha3_384_Free(&sha3);
  6971. printf(resultFmt, ret == 0 ? passed : failed);
  6972. }
  6973. #endif /* SHA3_384 */
  6974. #if !defined(WOLFSSL_NOSHA3_512)
  6975. if (ret == 0) {
  6976. printf(testingFmt, "wc_Sha3_512_Update()");
  6977. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  6978. if (ret != 0) {
  6979. return ret;
  6980. }
  6981. ret = wc_Sha3_512_Update(&sha3, msg, msglen);
  6982. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  6983. ret = WOLFSSL_FATAL_ERROR;
  6984. }
  6985. if (ret == 0) {
  6986. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  6987. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  6988. ret = WOLFSSL_FATAL_ERROR;
  6989. }
  6990. }
  6991. /* Pass bad args. */
  6992. if (ret == 0) {
  6993. ret = wc_Sha3_512_Update(NULL, msg2, msg2len);
  6994. if (ret == BAD_FUNC_ARG) {
  6995. ret = wc_Sha3_512_Update(&sha3, NULL, 5);
  6996. }
  6997. if (ret == BAD_FUNC_ARG) {
  6998. wc_Sha3_512_Free(&sha3);
  6999. if (wc_InitSha3_512(&sha3, HEAP_HINT, devId)) {
  7000. return ret;
  7001. }
  7002. ret = wc_Sha3_512_Update(&sha3, NULL, 0);
  7003. if (ret == 0) {
  7004. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  7005. }
  7006. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  7007. ret = WOLFSSL_FATAL_ERROR;
  7008. }
  7009. }
  7010. }
  7011. wc_Sha3_512_Free(&sha3);
  7012. printf(resultFmt, ret == 0 ? passed : failed);
  7013. }
  7014. #endif /* SHA3_512 */
  7015. #endif /* WOLFSSL_SHA3 */
  7016. return ret;
  7017. } /* END testing_wc_Sha3_Update */
  7018. /*
  7019. * Testing wc_Sha3_224_Final()
  7020. */
  7021. static int test_wc_Sha3_224_Final (void)
  7022. {
  7023. int ret = 0;
  7024. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  7025. wc_Sha3 sha3;
  7026. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  7027. "nopnopq";
  7028. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  7029. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  7030. "\x64\xea\xd0\xfc\xce\x33";
  7031. byte hash[WC_SHA3_224_DIGEST_SIZE];
  7032. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  7033. /* Init stack variables. */
  7034. XMEMSET(hash, 0, sizeof(hash));
  7035. printf(testingFmt, "wc_Sha3_224_Final()");
  7036. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  7037. if (ret != 0) {
  7038. return ret;
  7039. }
  7040. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  7041. if (ret == 0) {
  7042. ret = wc_Sha3_224_Final(&sha3, hash);
  7043. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE) != 0) {
  7044. ret = WOLFSSL_FATAL_ERROR;
  7045. }
  7046. }
  7047. /* Test bad args. */
  7048. if (ret == 0) {
  7049. ret = wc_Sha3_224_Final(NULL, hash);
  7050. if (ret == 0) {
  7051. ret = wc_Sha3_224_Final(&sha3, NULL);
  7052. }
  7053. if (ret == BAD_FUNC_ARG) {
  7054. ret = 0;
  7055. } else if (ret == 0) {
  7056. ret = WOLFSSL_FATAL_ERROR;
  7057. }
  7058. }
  7059. wc_Sha3_224_Free(&sha3);
  7060. printf(resultFmt, ret == 0 ? passed : failed);
  7061. if (ret == 0) {
  7062. printf(testingFmt, "wc_Sha3_224_GetHash()");
  7063. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  7064. if (ret != 0) {
  7065. return ret;
  7066. }
  7067. /* Init stack variables. */
  7068. XMEMSET(hash, 0, sizeof(hash));
  7069. XMEMSET(hashRet, 0, sizeof(hashRet));
  7070. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  7071. if (ret == 0) {
  7072. ret = wc_Sha3_224_GetHash(&sha3, hashRet);
  7073. }
  7074. if (ret == 0) {
  7075. ret = wc_Sha3_224_Final(&sha3, hash);
  7076. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE) != 0) {
  7077. ret = WOLFSSL_FATAL_ERROR;
  7078. }
  7079. }
  7080. if (ret == 0) {
  7081. /* Test bad args. */
  7082. ret = wc_Sha3_224_GetHash(NULL, hashRet);
  7083. if (ret == BAD_FUNC_ARG) {
  7084. ret = wc_Sha3_224_GetHash(&sha3, NULL);
  7085. }
  7086. if (ret == BAD_FUNC_ARG) {
  7087. ret = 0;
  7088. } else if (ret == 0) {
  7089. ret = WOLFSSL_FATAL_ERROR;
  7090. }
  7091. }
  7092. printf(resultFmt, ret == 0 ? passed : failed);
  7093. }
  7094. wc_Sha3_224_Free(&sha3);
  7095. #endif
  7096. return ret;
  7097. } /* END test_wc_Sha3_224_Final */
  7098. /*
  7099. * Testing wc_Sha3_256_Final()
  7100. */
  7101. static int test_wc_Sha3_256_Final (void)
  7102. {
  7103. int ret = 0;
  7104. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  7105. wc_Sha3 sha3;
  7106. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  7107. "nopnopq";
  7108. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  7109. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  7110. "\xdd\x97\x49\x6d\x33\x76";
  7111. byte hash[WC_SHA3_256_DIGEST_SIZE];
  7112. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  7113. /* Init stack variables. */
  7114. XMEMSET(hash, 0, sizeof(hash));
  7115. printf(testingFmt, "wc_Sha3_256_Final()");
  7116. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  7117. if (ret != 0) {
  7118. return ret;
  7119. }
  7120. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  7121. if (ret == 0) {
  7122. ret = wc_Sha3_256_Final(&sha3, hash);
  7123. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE) != 0) {
  7124. ret = WOLFSSL_FATAL_ERROR;
  7125. }
  7126. }
  7127. /* Test bad args. */
  7128. if (ret == 0) {
  7129. ret = wc_Sha3_256_Final(NULL, hash);
  7130. if (ret == 0) {
  7131. ret = wc_Sha3_256_Final(&sha3, NULL);
  7132. }
  7133. if (ret == BAD_FUNC_ARG) {
  7134. ret = 0;
  7135. } else if (ret == 0) {
  7136. ret = WOLFSSL_FATAL_ERROR;
  7137. }
  7138. }
  7139. wc_Sha3_256_Free(&sha3);
  7140. printf(resultFmt, ret == 0 ? passed : failed);
  7141. if (ret == 0) {
  7142. printf(testingFmt, "wc_Sha3_256_GetHash()");
  7143. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  7144. if (ret != 0) {
  7145. return ret;
  7146. }
  7147. /* Init stack variables. */
  7148. XMEMSET(hash, 0, sizeof(hash));
  7149. XMEMSET(hashRet, 0, sizeof(hashRet));
  7150. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  7151. if (ret == 0) {
  7152. ret = wc_Sha3_256_GetHash(&sha3, hashRet);
  7153. }
  7154. if (ret == 0) {
  7155. ret = wc_Sha3_256_Final(&sha3, hash);
  7156. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE) != 0) {
  7157. ret = WOLFSSL_FATAL_ERROR;
  7158. }
  7159. }
  7160. if (ret == 0) {
  7161. /* Test bad args. */
  7162. ret = wc_Sha3_256_GetHash(NULL, hashRet);
  7163. if (ret == BAD_FUNC_ARG) {
  7164. ret = wc_Sha3_256_GetHash(&sha3, NULL);
  7165. }
  7166. if (ret == BAD_FUNC_ARG) {
  7167. ret = 0;
  7168. } else if (ret == 0) {
  7169. ret = WOLFSSL_FATAL_ERROR;
  7170. }
  7171. }
  7172. printf(resultFmt, ret == 0 ? passed : failed);
  7173. }
  7174. wc_Sha3_256_Free(&sha3);
  7175. #endif
  7176. return ret;
  7177. } /* END test_wc_Sha3_256_Final */
  7178. /*
  7179. * Testing wc_Sha3_384_Final()
  7180. */
  7181. static int test_wc_Sha3_384_Final (void)
  7182. {
  7183. int ret = 0;
  7184. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  7185. wc_Sha3 sha3;
  7186. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  7187. "nopnopq";
  7188. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  7189. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  7190. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  7191. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  7192. byte hash[WC_SHA3_384_DIGEST_SIZE];
  7193. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  7194. /* Init stack variables. */
  7195. XMEMSET(hash, 0, sizeof(hash));
  7196. printf(testingFmt, "wc_Sha3_384_Final()");
  7197. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  7198. if (ret != 0) {
  7199. return ret;
  7200. }
  7201. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  7202. if (ret == 0) {
  7203. ret = wc_Sha3_384_Final(&sha3, hash);
  7204. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE) != 0) {
  7205. ret = WOLFSSL_FATAL_ERROR;
  7206. }
  7207. }
  7208. /* Test bad args. */
  7209. if (ret == 0) {
  7210. ret = wc_Sha3_384_Final(NULL, hash);
  7211. if (ret == 0) {
  7212. ret = wc_Sha3_384_Final(&sha3, NULL);
  7213. }
  7214. if (ret == BAD_FUNC_ARG) {
  7215. ret = 0;
  7216. } else if (ret == 0) {
  7217. ret = WOLFSSL_FATAL_ERROR;
  7218. }
  7219. }
  7220. wc_Sha3_384_Free(&sha3);
  7221. printf(resultFmt, ret == 0 ? passed : failed);
  7222. if (ret == 0) {
  7223. printf(testingFmt, "wc_Sha3_384_GetHash()");
  7224. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  7225. if (ret != 0) {
  7226. return ret;
  7227. }
  7228. /* Init stack variables. */
  7229. XMEMSET(hash, 0, sizeof(hash));
  7230. XMEMSET(hashRet, 0, sizeof(hashRet));
  7231. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  7232. if (ret == 0) {
  7233. ret = wc_Sha3_384_GetHash(&sha3, hashRet);
  7234. }
  7235. if (ret == 0) {
  7236. ret = wc_Sha3_384_Final(&sha3, hash);
  7237. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE) != 0) {
  7238. ret = WOLFSSL_FATAL_ERROR;
  7239. }
  7240. }
  7241. if (ret == 0) {
  7242. /* Test bad args. */
  7243. ret = wc_Sha3_384_GetHash(NULL, hashRet);
  7244. if (ret == BAD_FUNC_ARG) {
  7245. ret = wc_Sha3_384_GetHash(&sha3, NULL);
  7246. }
  7247. if (ret == BAD_FUNC_ARG) {
  7248. ret = 0;
  7249. } else if (ret == 0) {
  7250. ret = WOLFSSL_FATAL_ERROR;
  7251. }
  7252. }
  7253. printf(resultFmt, ret == 0 ? passed : failed);
  7254. }
  7255. wc_Sha3_384_Free(&sha3);
  7256. #endif
  7257. return ret;
  7258. } /* END test_wc_Sha3_384_Final */
  7259. /*
  7260. * Testing wc_Sha3_512_Final()
  7261. */
  7262. static int test_wc_Sha3_512_Final (void)
  7263. {
  7264. int ret = 0;
  7265. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  7266. !defined(WOLFSSL_NOSHA3_384)
  7267. wc_Sha3 sha3;
  7268. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  7269. "nopnopq";
  7270. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  7271. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  7272. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  7273. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  7274. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  7275. byte hash[WC_SHA3_512_DIGEST_SIZE];
  7276. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  7277. /* Init stack variables. */
  7278. XMEMSET(hash, 0, sizeof(hash));
  7279. printf(testingFmt, "wc_Sha3_512_Final()");
  7280. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  7281. if (ret != 0) {
  7282. return ret;
  7283. }
  7284. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  7285. if (ret == 0) {
  7286. ret = wc_Sha3_512_Final(&sha3, hash);
  7287. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE) != 0) {
  7288. ret = WOLFSSL_FATAL_ERROR;
  7289. }
  7290. }
  7291. /* Test bad args. */
  7292. if (ret == 0) {
  7293. ret = wc_Sha3_512_Final(NULL, hash);
  7294. if (ret == 0) {
  7295. ret = wc_Sha3_384_Final(&sha3, NULL);
  7296. }
  7297. if (ret == BAD_FUNC_ARG) {
  7298. ret = 0;
  7299. } else if (ret == 0) {
  7300. ret = WOLFSSL_FATAL_ERROR;
  7301. }
  7302. }
  7303. wc_Sha3_512_Free(&sha3);
  7304. printf(resultFmt, ret == 0 ? passed : failed);
  7305. if (ret == 0) {
  7306. printf(testingFmt, "wc_Sha3_512_GetHash()");
  7307. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  7308. if (ret != 0) {
  7309. return ret;
  7310. }
  7311. /* Init stack variables. */
  7312. XMEMSET(hash, 0, sizeof(hash));
  7313. XMEMSET(hashRet, 0, sizeof(hashRet));
  7314. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  7315. if (ret == 0) {
  7316. ret = wc_Sha3_512_GetHash(&sha3, hashRet);
  7317. }
  7318. if (ret == 0) {
  7319. ret = wc_Sha3_512_Final(&sha3, hash);
  7320. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE) != 0) {
  7321. ret = WOLFSSL_FATAL_ERROR;
  7322. }
  7323. }
  7324. if (ret == 0) {
  7325. /* Test bad args. */
  7326. ret = wc_Sha3_512_GetHash(NULL, hashRet);
  7327. if (ret == BAD_FUNC_ARG) {
  7328. ret = wc_Sha3_512_GetHash(&sha3, NULL);
  7329. }
  7330. if (ret == BAD_FUNC_ARG) {
  7331. ret = 0;
  7332. } else if (ret == 0) {
  7333. ret = WOLFSSL_FATAL_ERROR;
  7334. }
  7335. }
  7336. printf(resultFmt, ret == 0 ? passed : failed);
  7337. }
  7338. wc_Sha3_512_Free(&sha3);
  7339. #endif
  7340. return ret;
  7341. } /* END test_wc_Sha3_512_Final */
  7342. /*
  7343. * Testing wc_Sha3_224_Copy()
  7344. */
  7345. static int test_wc_Sha3_224_Copy (void)
  7346. {
  7347. int ret = 0;
  7348. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  7349. wc_Sha3 sha3, sha3Cpy;
  7350. const char* msg = "Everyone gets Friday off.";
  7351. word32 msglen = (word32)XSTRLEN(msg);
  7352. byte hash[WC_SHA3_224_DIGEST_SIZE];
  7353. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  7354. XMEMSET(hash, 0, sizeof(hash));
  7355. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  7356. printf(testingFmt, "wc_Sha3_224_Copy()");
  7357. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  7358. if (ret != 0) {
  7359. return ret;
  7360. }
  7361. ret = wc_InitSha3_224(&sha3Cpy, HEAP_HINT, devId);
  7362. if (ret != 0) {
  7363. wc_Sha3_224_Free(&sha3);
  7364. return ret;
  7365. }
  7366. ret = wc_Sha3_224_Update(&sha3, (byte*)msg, msglen);
  7367. if (ret == 0) {
  7368. ret = wc_Sha3_224_Copy(&sha3Cpy, &sha3);
  7369. if (ret == 0) {
  7370. ret = wc_Sha3_224_Final(&sha3, hash);
  7371. if (ret == 0) {
  7372. ret = wc_Sha3_224_Final(&sha3Cpy, hashCpy);
  7373. }
  7374. }
  7375. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  7376. ret = WOLFSSL_FATAL_ERROR;
  7377. }
  7378. }
  7379. /* Test bad args. */
  7380. if (ret == 0) {
  7381. ret = wc_Sha3_224_Copy(NULL, &sha3);
  7382. if (ret == BAD_FUNC_ARG) {
  7383. ret = wc_Sha3_224_Copy(&sha3Cpy, NULL);
  7384. }
  7385. if (ret == BAD_FUNC_ARG) {
  7386. ret = 0;
  7387. } else if (ret == 0) {
  7388. ret = WOLFSSL_FATAL_ERROR;
  7389. }
  7390. }
  7391. printf(resultFmt, ret == 0 ? passed : failed);
  7392. #endif
  7393. return ret;
  7394. } /* END test_wc_Sha3_224_Copy */
  7395. /*
  7396. * Testing wc_Sha3_256_Copy()
  7397. */
  7398. static int test_wc_Sha3_256_Copy (void)
  7399. {
  7400. int ret = 0;
  7401. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  7402. wc_Sha3 sha3, sha3Cpy;
  7403. const char* msg = "Everyone gets Friday off.";
  7404. word32 msglen = (word32)XSTRLEN(msg);
  7405. byte hash[WC_SHA3_256_DIGEST_SIZE];
  7406. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  7407. XMEMSET(hash, 0, sizeof(hash));
  7408. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  7409. printf(testingFmt, "wc_Sha3_256_Copy()");
  7410. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  7411. if (ret != 0) {
  7412. return ret;
  7413. }
  7414. ret = wc_InitSha3_256(&sha3Cpy, HEAP_HINT, devId);
  7415. if (ret != 0) {
  7416. wc_Sha3_256_Free(&sha3);
  7417. return ret;
  7418. }
  7419. ret = wc_Sha3_256_Update(&sha3, (byte*)msg, msglen);
  7420. if (ret == 0) {
  7421. ret = wc_Sha3_256_Copy(&sha3Cpy, &sha3);
  7422. if (ret == 0) {
  7423. ret = wc_Sha3_256_Final(&sha3, hash);
  7424. if (ret == 0) {
  7425. ret = wc_Sha3_256_Final(&sha3Cpy, hashCpy);
  7426. }
  7427. }
  7428. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  7429. ret = WOLFSSL_FATAL_ERROR;
  7430. }
  7431. }
  7432. /* Test bad args. */
  7433. if (ret == 0) {
  7434. ret = wc_Sha3_256_Copy(NULL, &sha3);
  7435. if (ret == BAD_FUNC_ARG) {
  7436. ret = wc_Sha3_256_Copy(&sha3Cpy, NULL);
  7437. }
  7438. if (ret == BAD_FUNC_ARG) {
  7439. ret = 0;
  7440. } else if (ret == 0) {
  7441. ret = WOLFSSL_FATAL_ERROR;
  7442. }
  7443. }
  7444. printf(resultFmt, ret == 0 ? passed : failed);
  7445. #endif
  7446. return ret;
  7447. } /* END test_wc_Sha3_256_Copy */
  7448. /*
  7449. * Testing wc_Sha3_384_Copy()
  7450. */
  7451. static int test_wc_Sha3_384_Copy (void)
  7452. {
  7453. int ret = 0;
  7454. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  7455. wc_Sha3 sha3, sha3Cpy;
  7456. const char* msg = "Everyone gets Friday off.";
  7457. word32 msglen = (word32)XSTRLEN(msg);
  7458. byte hash[WC_SHA3_384_DIGEST_SIZE];
  7459. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  7460. XMEMSET(hash, 0, sizeof(hash));
  7461. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  7462. printf(testingFmt, "wc_Sha3_384_Copy()");
  7463. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  7464. if (ret != 0) {
  7465. return ret;
  7466. }
  7467. ret = wc_InitSha3_384(&sha3Cpy, HEAP_HINT, devId);
  7468. if (ret != 0) {
  7469. wc_Sha3_384_Free(&sha3);
  7470. return ret;
  7471. }
  7472. ret = wc_Sha3_384_Update(&sha3, (byte*)msg, msglen);
  7473. if (ret == 0) {
  7474. ret = wc_Sha3_384_Copy(&sha3Cpy, &sha3);
  7475. if (ret == 0) {
  7476. ret = wc_Sha3_384_Final(&sha3, hash);
  7477. if (ret == 0) {
  7478. ret = wc_Sha3_384_Final(&sha3Cpy, hashCpy);
  7479. }
  7480. }
  7481. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  7482. ret = WOLFSSL_FATAL_ERROR;
  7483. }
  7484. }
  7485. /* Test bad args. */
  7486. if (ret == 0) {
  7487. ret = wc_Sha3_384_Copy(NULL, &sha3);
  7488. if (ret == BAD_FUNC_ARG) {
  7489. ret = wc_Sha3_384_Copy(&sha3Cpy, NULL);
  7490. }
  7491. if (ret == BAD_FUNC_ARG) {
  7492. ret = 0;
  7493. } else if (ret == 0) {
  7494. ret = WOLFSSL_FATAL_ERROR;
  7495. }
  7496. }
  7497. printf(resultFmt, ret == 0 ? passed : failed);
  7498. #endif
  7499. return ret;
  7500. } /* END test_wc_Sha3_384_Copy */
  7501. /*
  7502. * Testing wc_Sha3_512_Copy()
  7503. */
  7504. static int test_wc_Sha3_512_Copy (void)
  7505. {
  7506. int ret = 0;
  7507. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  7508. wc_Sha3 sha3, sha3Cpy;
  7509. const char* msg = "Everyone gets Friday off.";
  7510. word32 msglen = (word32)XSTRLEN(msg);
  7511. byte hash[WC_SHA3_512_DIGEST_SIZE];
  7512. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  7513. XMEMSET(hash, 0, sizeof(hash));
  7514. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  7515. printf(testingFmt, "wc_Sha3_512_Copy()");
  7516. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  7517. if (ret != 0) {
  7518. return ret;
  7519. }
  7520. ret = wc_InitSha3_512(&sha3Cpy, HEAP_HINT, devId);
  7521. if (ret != 0) {
  7522. wc_Sha3_512_Free(&sha3);
  7523. return ret;
  7524. }
  7525. ret = wc_Sha3_512_Update(&sha3, (byte*)msg, msglen);
  7526. if (ret == 0) {
  7527. ret = wc_Sha3_512_Copy(&sha3Cpy, &sha3);
  7528. if (ret == 0) {
  7529. ret = wc_Sha3_512_Final(&sha3, hash);
  7530. if (ret == 0) {
  7531. ret = wc_Sha3_512_Final(&sha3Cpy, hashCpy);
  7532. }
  7533. }
  7534. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  7535. ret = WOLFSSL_FATAL_ERROR;
  7536. }
  7537. }
  7538. /* Test bad args. */
  7539. if (ret == 0) {
  7540. ret = wc_Sha3_512_Copy(NULL, &sha3);
  7541. if (ret == BAD_FUNC_ARG) {
  7542. ret = wc_Sha3_512_Copy(&sha3Cpy, NULL);
  7543. }
  7544. if (ret == BAD_FUNC_ARG) {
  7545. ret = 0;
  7546. } else if (ret == 0) {
  7547. ret = WOLFSSL_FATAL_ERROR;
  7548. }
  7549. }
  7550. printf(resultFmt, ret == 0 ? passed : failed);
  7551. #endif
  7552. return ret;
  7553. } /* END test_wc_Sha3_512_Copy */
  7554. /*
  7555. * Unit test function for wc_Sha3_GetFlags()
  7556. */
  7557. static int test_wc_Sha3_GetFlags (void)
  7558. {
  7559. int ret = 0;
  7560. #if defined(WOLFSSL_SHA3) && \
  7561. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  7562. wc_Sha3 sha3;
  7563. word32 flags = 0;
  7564. printf(testingFmt, "wc_Sha3_GetFlags()");
  7565. /* Initialize */
  7566. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  7567. if (ret != 0) {
  7568. return ret;
  7569. }
  7570. if (ret == 0) {
  7571. ret = wc_Sha3_GetFlags(&sha3, &flags);
  7572. }
  7573. if (ret == 0) {
  7574. if (flags & WC_HASH_FLAG_ISCOPY) {
  7575. ret = 0;
  7576. }
  7577. }
  7578. wc_Sha3_224_Free(&sha3);
  7579. printf(resultFmt, ret == 0 ? passed : failed);
  7580. #endif
  7581. return ret;
  7582. } /* END test_wc_Sha3_GetFlags */
  7583. static int test_wc_InitShake256 (void)
  7584. {
  7585. int ret = 0;
  7586. #if defined(WOLFSSL_SHAKE256) && !defined(WOLFSSL_NO_SHAKE256)
  7587. wc_Shake shake;
  7588. printf(testingFmt, "wc_InitShake256()");
  7589. ret = wc_InitShake256(&shake, HEAP_HINT, devId);
  7590. /* Test bad args. */
  7591. if (ret == 0) {
  7592. ret = wc_InitShake256(NULL, HEAP_HINT, devId);
  7593. if (ret == BAD_FUNC_ARG) {
  7594. ret = 0;
  7595. } else if (ret == 0) {
  7596. ret = WOLFSSL_FATAL_ERROR;
  7597. }
  7598. }
  7599. wc_Shake256_Free(&shake);
  7600. printf(resultFmt, ret == 0 ? passed : failed);
  7601. #endif
  7602. return ret;
  7603. } /* END test_wc_InitSha3 */
  7604. static int testing_wc_Shake256_Update (void)
  7605. {
  7606. int ret = 0;
  7607. #if defined(WOLFSSL_SHAKE256) && !defined(WOLFSSL_NO_SHAKE256)
  7608. wc_Shake shake;
  7609. byte msg[] = "Everybody's working for the weekend.";
  7610. byte msg2[] = "Everybody gets Friday off.";
  7611. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  7612. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  7613. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  7614. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  7615. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  7616. word32 msglen = sizeof(msg) - 1;
  7617. word32 msg2len = sizeof(msg2);
  7618. word32 msgCmplen = sizeof(msgCmp);
  7619. printf(testingFmt, "wc_Shake256_Update()");
  7620. ret = wc_InitShake256(&shake, HEAP_HINT, devId);
  7621. if (ret != 0) {
  7622. return ret;
  7623. }
  7624. ret = wc_Shake256_Update(&shake, msg, msglen);
  7625. if (XMEMCMP(msg, shake.t, msglen) || shake.i != msglen) {
  7626. ret = WOLFSSL_FATAL_ERROR;
  7627. }
  7628. if (ret == 0) {
  7629. ret = wc_Shake256_Update(&shake, msg2, msg2len);
  7630. if (XMEMCMP(shake.t, msgCmp, msgCmplen) != 0) {
  7631. ret = WOLFSSL_FATAL_ERROR;
  7632. }
  7633. }
  7634. /* Pass bad args. */
  7635. if (ret == 0) {
  7636. ret = wc_Shake256_Update(NULL, msg2, msg2len);
  7637. if (ret == BAD_FUNC_ARG) {
  7638. ret = wc_Shake256_Update(&shake, NULL, 5);
  7639. }
  7640. if (ret == BAD_FUNC_ARG) {
  7641. wc_Shake256_Free(&shake);
  7642. if (wc_InitShake256(&shake, HEAP_HINT, devId)) {
  7643. return ret;
  7644. }
  7645. ret = wc_Shake256_Update(&shake, NULL, 0);
  7646. if (ret == 0) {
  7647. ret = wc_Shake256_Update(&shake, msg2, msg2len);
  7648. }
  7649. if (ret == 0 && XMEMCMP(msg2, shake.t, msg2len) != 0) {
  7650. ret = WOLFSSL_FATAL_ERROR;
  7651. }
  7652. }
  7653. }
  7654. wc_Shake256_Free(&shake);
  7655. printf(resultFmt, ret == 0 ? passed : failed);
  7656. #endif /* WOLFSSL_SHAKE256 && !WOLFSSL_NO_SHAKE256 */
  7657. return ret;
  7658. }
  7659. static int test_wc_Shake256_Final (void)
  7660. {
  7661. int ret = 0;
  7662. #if defined(WOLFSSL_SHAKE256) && !defined(WOLFSSL_NO_SHAKE256)
  7663. wc_Shake shake;
  7664. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  7665. "nopnopq";
  7666. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  7667. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  7668. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  7669. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  7670. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  7671. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  7672. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  7673. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  7674. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  7675. byte hash[114];
  7676. /* Init stack variables. */
  7677. XMEMSET(hash, 0, sizeof(hash));
  7678. printf(testingFmt, "wc_Shake256_Final()");
  7679. ret = wc_InitShake256(&shake, HEAP_HINT, devId);
  7680. if (ret != 0) {
  7681. return ret;
  7682. }
  7683. ret= wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg));
  7684. if (ret == 0) {
  7685. ret = wc_Shake256_Final(&shake, hash, (word32)sizeof(hash));
  7686. if (ret == 0 && XMEMCMP(expOut, hash, (word32)sizeof(hash)) != 0) {
  7687. ret = WOLFSSL_FATAL_ERROR;
  7688. }
  7689. }
  7690. /* Test bad args. */
  7691. if (ret == 0) {
  7692. ret = wc_Shake256_Final(NULL, hash, (word32)sizeof(hash));
  7693. if (ret == 0) {
  7694. ret = wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash));
  7695. }
  7696. if (ret == BAD_FUNC_ARG) {
  7697. ret = 0;
  7698. } else if (ret == 0) {
  7699. ret = WOLFSSL_FATAL_ERROR;
  7700. }
  7701. }
  7702. wc_Shake256_Free(&shake);
  7703. printf(resultFmt, ret == 0 ? passed : failed);
  7704. #endif
  7705. return ret;
  7706. }
  7707. /*
  7708. * Testing wc_Shake256_Copy()
  7709. */
  7710. static int test_wc_Shake256_Copy (void)
  7711. {
  7712. int ret = 0;
  7713. #if defined(WOLFSSL_SHAKE256) && !defined(WOLFSSL_NO_SHAKE256)
  7714. wc_Shake shake, shakeCpy;
  7715. const char* msg = "Everyone gets Friday off.";
  7716. word32 msglen = (word32)XSTRLEN(msg);
  7717. byte hash[144];
  7718. byte hashCpy[144];
  7719. word32 hashLen = sizeof(hash);
  7720. word32 hashLenCpy = sizeof(hashCpy);
  7721. XMEMSET(hash, 0, sizeof(hash));
  7722. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  7723. printf(testingFmt, "wc_Shake256_Copy()");
  7724. ret = wc_InitShake256(&shake, HEAP_HINT, devId);
  7725. if (ret != 0) {
  7726. return ret;
  7727. }
  7728. ret = wc_InitShake256(&shakeCpy, HEAP_HINT, devId);
  7729. if (ret != 0) {
  7730. wc_Shake256_Free(&shake);
  7731. return ret;
  7732. }
  7733. ret = wc_Shake256_Update(&shake, (byte*)msg, msglen);
  7734. if (ret == 0) {
  7735. ret = wc_Shake256_Copy(&shakeCpy, &shake);
  7736. if (ret == 0) {
  7737. ret = wc_Shake256_Final(&shake, hash, hashLen);
  7738. if (ret == 0) {
  7739. ret = wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy);
  7740. }
  7741. }
  7742. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  7743. ret = WOLFSSL_FATAL_ERROR;
  7744. }
  7745. }
  7746. /* Test bad args. */
  7747. if (ret == 0) {
  7748. ret = wc_Shake256_Copy(NULL, &shake);
  7749. if (ret == BAD_FUNC_ARG) {
  7750. ret = wc_Shake256_Copy(&shakeCpy, NULL);
  7751. }
  7752. if (ret == BAD_FUNC_ARG) {
  7753. ret = 0;
  7754. } else if (ret == 0) {
  7755. ret = WOLFSSL_FATAL_ERROR;
  7756. }
  7757. }
  7758. wc_Shake256_Free(&shake);
  7759. printf(resultFmt, ret == 0 ? passed : failed);
  7760. #endif
  7761. return ret;
  7762. } /* END test_wc_Shake256_Copy */
  7763. /*
  7764. * Unit test function for wc_Shake256Hash()
  7765. */
  7766. static int test_wc_Shake256Hash(void)
  7767. {
  7768. int ret = 0;
  7769. #if defined(WOLFSSL_SHAKE256) && !defined(WOLFSSL_NO_SHAKE256)
  7770. const byte data[] = { /* Hello World */
  7771. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  7772. 0x72,0x6c,0x64
  7773. };
  7774. word32 len = sizeof(data);
  7775. byte hash[144];
  7776. word32 hashLen = sizeof(hash);
  7777. printf(testingFmt, "wc_Shake256Hash()");
  7778. ret = wc_Shake256Hash(data, len, hash, hashLen);
  7779. printf(resultFmt, ret == 0 ? passed : failed);
  7780. #endif
  7781. return ret;
  7782. } /* END test_wc_Shake256Hash */
  7783. /*
  7784. * unit test for wc_IdeaSetKey()
  7785. */
  7786. static int test_wc_IdeaSetKey (void)
  7787. {
  7788. int ret = 0;
  7789. #ifdef HAVE_IDEA
  7790. Idea idea;
  7791. const byte key[] =
  7792. {
  7793. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37,
  7794. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37
  7795. };
  7796. int flag = 0;
  7797. printf(testingFmt, "wc_IdeaSetKey()");
  7798. /*IV can be NULL, default value is 0*/
  7799. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
  7800. if (ret == 0) {
  7801. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_DECRYPTION);
  7802. }
  7803. /* Bad args. */
  7804. if (ret == 0) {
  7805. ret = wc_IdeaSetKey(NULL, key, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
  7806. if (ret != BAD_FUNC_ARG) {
  7807. flag = 1;
  7808. }
  7809. ret = wc_IdeaSetKey(&idea, NULL, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
  7810. if (ret != BAD_FUNC_ARG) {
  7811. flag = 1;
  7812. }
  7813. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE - 1,
  7814. NULL, IDEA_ENCRYPTION);
  7815. if (ret != BAD_FUNC_ARG) {
  7816. flag = 1;
  7817. }
  7818. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, -1);
  7819. if (ret != BAD_FUNC_ARG) {
  7820. flag = 1;
  7821. }
  7822. if (flag == 1) {
  7823. ret = WOLFSSL_FATAL_ERROR;
  7824. } else {
  7825. ret = 0;
  7826. }
  7827. } /* END Test Bad Args. */
  7828. printf(resultFmt, ret == 0 ? passed : failed);
  7829. #endif
  7830. return ret;
  7831. } /* END test_wc_IdeaSetKey */
  7832. /*
  7833. * Unit test for wc_IdeaSetIV()
  7834. */
  7835. static int test_wc_IdeaSetIV (void)
  7836. {
  7837. int ret = 0;
  7838. #ifdef HAVE_IDEA
  7839. Idea idea;
  7840. printf(testingFmt, "wc_IdeaSetIV()");
  7841. ret = wc_IdeaSetIV(&idea, NULL);
  7842. /* Test bad args. */
  7843. if (ret == 0) {
  7844. ret = wc_IdeaSetIV(NULL, NULL);
  7845. if (ret == BAD_FUNC_ARG) {
  7846. ret = 0;
  7847. } else {
  7848. ret = WOLFSSL_FATAL_ERROR;
  7849. }
  7850. }
  7851. printf(resultFmt, ret == 0 ? passed : failed);
  7852. #endif
  7853. return ret;
  7854. } /* END test_wc_IdeaSetIV */
  7855. /*
  7856. * Unit test for wc_IdeaCipher()
  7857. */
  7858. static int test_wc_IdeaCipher (void)
  7859. {
  7860. int ret = 0;
  7861. #ifdef HAVE_IDEA
  7862. Idea idea;
  7863. const byte key[] =
  7864. {
  7865. 0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00,
  7866. 0x95, 0x2C, 0x49, 0x10, 0x48, 0x81, 0xFF, 0x48
  7867. };
  7868. const byte plain[] =
  7869. {
  7870. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37
  7871. };
  7872. byte enc[sizeof(plain)];
  7873. byte dec[sizeof(enc)];
  7874. printf(testingFmt, "wc_IdeaCipher()");
  7875. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
  7876. if (ret == 0) {
  7877. ret = wc_IdeaCipher(&idea, enc, plain);
  7878. if (ret != 0) {
  7879. ret = WOLFSSL_FATAL_ERROR;
  7880. }
  7881. }
  7882. if (ret == 0) {
  7883. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_DECRYPTION);
  7884. if (ret == 0) {
  7885. ret = wc_IdeaCipher(&idea, dec, enc);
  7886. }
  7887. if (ret == 0) {
  7888. ret = XMEMCMP(plain, dec, IDEA_BLOCK_SIZE);
  7889. }
  7890. if (ret != 0) {
  7891. ret = WOLFSSL_FATAL_ERROR;
  7892. }
  7893. }
  7894. /* Pass Bad Args. */
  7895. if (ret == 0) {
  7896. ret = wc_IdeaCipher(NULL, enc, dec);
  7897. if (ret == BAD_FUNC_ARG) {
  7898. ret = wc_IdeaCipher(&idea, NULL, dec);
  7899. }
  7900. if (ret == BAD_FUNC_ARG) {
  7901. ret = wc_IdeaCipher(&idea, enc, NULL);
  7902. }
  7903. if (ret == BAD_FUNC_ARG) {
  7904. ret = 0;
  7905. } else {
  7906. ret = WOLFSSL_FATAL_ERROR;
  7907. }
  7908. }
  7909. printf(resultFmt, ret == 0 ? passed : failed);
  7910. #endif
  7911. return ret;
  7912. } /* END test_wc_IdeaCipher */
  7913. /*
  7914. * Unit test for functions wc_IdeaCbcEncrypt and wc_IdeaCbcDecrypt
  7915. */
  7916. static int test_wc_IdeaCbcEncyptDecrypt (void)
  7917. {
  7918. int ret = 0;
  7919. #ifdef HAVE_IDEA
  7920. Idea idea;
  7921. const byte key[] =
  7922. {
  7923. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37,
  7924. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37
  7925. };
  7926. const char* message = "International Data Encryption Algorithm";
  7927. byte msg_enc[40];
  7928. byte msg_dec[40];
  7929. printf(testingFmt, "wc_IdeaCbcEncrypt()");
  7930. ret = wc_IdeaSetKey(&idea, key, sizeof(key), NULL, IDEA_ENCRYPTION);
  7931. if (ret == 0) {
  7932. ret = wc_IdeaCbcEncrypt(&idea, msg_enc, (byte *)message,
  7933. (word32)XSTRLEN(message) + 1);
  7934. }
  7935. if (ret == 0) {
  7936. ret = wc_IdeaSetKey(&idea, key, sizeof(key), NULL, IDEA_DECRYPTION);
  7937. }
  7938. if (ret == 0) {
  7939. ret = wc_IdeaCbcDecrypt(&idea, msg_dec, msg_enc,
  7940. (word32)XSTRLEN(message) + 1);
  7941. if (XMEMCMP(message, msg_dec, (word32)XSTRLEN(message))) {
  7942. ret = WOLFSSL_FATAL_ERROR;
  7943. }
  7944. }
  7945. /* Test bad args. Enc */
  7946. if (ret == 0) {
  7947. ret = wc_IdeaCbcEncrypt(NULL, msg_enc, (byte*)message,
  7948. (word32)XSTRLEN(message) + 1);
  7949. if (ret == BAD_FUNC_ARG) {
  7950. ret = wc_IdeaCbcEncrypt(&idea, NULL, (byte*)message,
  7951. (word32)XSTRLEN(message) + 1);
  7952. }
  7953. if (ret == BAD_FUNC_ARG) {
  7954. ret = wc_IdeaCbcEncrypt(&idea, msg_enc, NULL,
  7955. (word32)XSTRLEN(message) + 1);
  7956. }
  7957. if (ret != BAD_FUNC_ARG) {
  7958. ret = WOLFSSL_FATAL_ERROR;
  7959. } else {
  7960. ret = 0;
  7961. }
  7962. } /* END test bad args ENC */
  7963. /* Test bad args DEC */
  7964. if (ret == 0) {
  7965. ret = wc_IdeaCbcDecrypt(NULL, msg_dec, msg_enc,
  7966. (word32)XSTRLEN(message) + 1);
  7967. if (ret == BAD_FUNC_ARG) {
  7968. ret = wc_IdeaCbcDecrypt(&idea, NULL, msg_enc,
  7969. (word32)XSTRLEN(message) + 1);
  7970. }
  7971. if (ret == BAD_FUNC_ARG) {
  7972. ret = wc_IdeaCbcDecrypt(&idea, msg_dec, NULL,
  7973. (word32)XSTRLEN(message) + 1);
  7974. }
  7975. if (ret != BAD_FUNC_ARG) {
  7976. ret = WOLFSSL_FATAL_ERROR;
  7977. } else {
  7978. ret = 0;
  7979. }
  7980. }
  7981. printf(resultFmt, ret == 0 ? passed : failed);
  7982. #endif
  7983. return ret;
  7984. } /* END test_wc_IdeaCbcEncryptDecrypt */
  7985. /*
  7986. * Test function for wc_HmacSetKey
  7987. */
  7988. static int test_wc_Md5HmacSetKey (void)
  7989. {
  7990. int flag = 0;
  7991. #if !defined(NO_HMAC) && !defined(NO_MD5)
  7992. Hmac hmac;
  7993. int ret, times, itr;
  7994. const char* keys[]=
  7995. {
  7996. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  7997. #ifndef HAVE_FIPS
  7998. "Jefe", /* smaller than minimum FIPS key size */
  7999. #endif
  8000. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  8001. };
  8002. times = sizeof(keys) / sizeof(char*);
  8003. flag = 0;
  8004. printf(testingFmt, "wc_HmacSetKey() with MD5");
  8005. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8006. if (ret != 0)
  8007. return ret;
  8008. for (itr = 0; itr < times; itr++) {
  8009. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  8010. (word32)XSTRLEN(keys[itr]));
  8011. if (ret != 0) {
  8012. flag = ret;
  8013. }
  8014. }
  8015. /* Bad args. */
  8016. if (!flag) {
  8017. ret = wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  8018. (word32)XSTRLEN(keys[0]));
  8019. if (ret != BAD_FUNC_ARG) {
  8020. flag = WOLFSSL_FATAL_ERROR;
  8021. }
  8022. }
  8023. if (!flag) {
  8024. ret = wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0]));
  8025. if (ret != BAD_FUNC_ARG) {
  8026. flag = WOLFSSL_FATAL_ERROR;
  8027. }
  8028. }
  8029. if (!flag) {
  8030. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  8031. (word32)XSTRLEN(keys[0]));
  8032. if (ret != BAD_FUNC_ARG) {
  8033. flag = WOLFSSL_FATAL_ERROR;
  8034. }
  8035. }
  8036. if (!flag) {
  8037. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  8038. #ifdef HAVE_FIPS
  8039. if (ret != HMAC_MIN_KEYLEN_E) {
  8040. flag = WOLFSSL_FATAL_ERROR;
  8041. }
  8042. #else
  8043. if (ret != 0) {
  8044. flag = WOLFSSL_FATAL_ERROR;
  8045. }
  8046. #endif
  8047. }
  8048. wc_HmacFree(&hmac);
  8049. printf(resultFmt, flag == 0 ? passed : failed);
  8050. #endif
  8051. return flag;
  8052. } /* END test_wc_Md5HmacSetKey */
  8053. /*
  8054. * testing wc_HmacSetKey() on wc_Sha hash.
  8055. */
  8056. static int test_wc_ShaHmacSetKey (void)
  8057. {
  8058. int flag = 0;
  8059. #if !defined(NO_HMAC) && !defined(NO_SHA)
  8060. Hmac hmac;
  8061. int ret, times, itr;
  8062. const char* keys[]=
  8063. {
  8064. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  8065. "\x0b\x0b\x0b",
  8066. #ifndef HAVE_FIPS
  8067. "Jefe", /* smaller than minimum FIPS key size */
  8068. #endif
  8069. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  8070. "\xAA\xAA\xAA"
  8071. };
  8072. times = sizeof(keys) / sizeof(char*);
  8073. flag = 0;
  8074. printf(testingFmt, "wc_HmacSetKey() with SHA");
  8075. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8076. if (ret != 0)
  8077. return ret;
  8078. for (itr = 0; itr < times; itr++) {
  8079. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  8080. (word32)XSTRLEN(keys[itr]));
  8081. if (ret != 0) {
  8082. flag = ret;
  8083. }
  8084. }
  8085. /* Bad args. */
  8086. if (!flag) {
  8087. ret = wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  8088. (word32)XSTRLEN(keys[0]));
  8089. if (ret != BAD_FUNC_ARG) {
  8090. flag = WOLFSSL_FATAL_ERROR;
  8091. }
  8092. }
  8093. if (!flag) {
  8094. ret = wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0]));
  8095. if (ret != BAD_FUNC_ARG) {
  8096. flag = WOLFSSL_FATAL_ERROR;
  8097. }
  8098. }
  8099. if (!flag) {
  8100. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  8101. (word32)XSTRLEN(keys[0]));
  8102. if (ret != BAD_FUNC_ARG) {
  8103. flag = WOLFSSL_FATAL_ERROR;
  8104. }
  8105. }
  8106. if (!flag) {
  8107. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  8108. #ifdef HAVE_FIPS
  8109. if (ret != HMAC_MIN_KEYLEN_E) {
  8110. flag = WOLFSSL_FATAL_ERROR;
  8111. }
  8112. #else
  8113. if (ret != 0) {
  8114. flag = WOLFSSL_FATAL_ERROR;
  8115. }
  8116. #endif
  8117. }
  8118. wc_HmacFree(&hmac);
  8119. printf(resultFmt, flag == 0 ? passed : failed);
  8120. #endif
  8121. return flag;
  8122. } /* END test_wc_ShaHmacSetKey() */
  8123. /*
  8124. * testing wc_HmacSetKey() on Sha224 hash.
  8125. */
  8126. static int test_wc_Sha224HmacSetKey (void)
  8127. {
  8128. int flag = 0;
  8129. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  8130. Hmac hmac;
  8131. int ret, times, itr;
  8132. const char* keys[]=
  8133. {
  8134. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  8135. "\x0b\x0b\x0b",
  8136. #ifndef HAVE_FIPS
  8137. "Jefe", /* smaller than minimum FIPS key size */
  8138. #endif
  8139. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  8140. "\xAA\xAA\xAA"
  8141. };
  8142. times = sizeof(keys) / sizeof(char*);
  8143. flag = 0;
  8144. printf(testingFmt, "wc_HmacSetKey() with SHA 224");
  8145. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8146. if (ret != 0)
  8147. return ret;
  8148. for (itr = 0; itr < times; itr++) {
  8149. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  8150. (word32)XSTRLEN(keys[itr]));
  8151. if (ret != 0) {
  8152. flag = ret;
  8153. }
  8154. }
  8155. /* Bad args. */
  8156. if (!flag) {
  8157. ret = wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  8158. (word32)XSTRLEN(keys[0]));
  8159. if (ret != BAD_FUNC_ARG) {
  8160. flag = WOLFSSL_FATAL_ERROR;
  8161. }
  8162. }
  8163. if (!flag) {
  8164. ret = wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0]));
  8165. if (ret != BAD_FUNC_ARG) {
  8166. flag = WOLFSSL_FATAL_ERROR;
  8167. }
  8168. }
  8169. if (!flag) {
  8170. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  8171. (word32)XSTRLEN(keys[0]));
  8172. if (ret != BAD_FUNC_ARG) {
  8173. flag = WOLFSSL_FATAL_ERROR;
  8174. }
  8175. }
  8176. if (!flag) {
  8177. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  8178. #ifdef HAVE_FIPS
  8179. if (ret != HMAC_MIN_KEYLEN_E) {
  8180. flag = WOLFSSL_FATAL_ERROR;
  8181. }
  8182. #else
  8183. if (ret != 0) {
  8184. flag = WOLFSSL_FATAL_ERROR;
  8185. }
  8186. #endif
  8187. }
  8188. wc_HmacFree(&hmac);
  8189. printf(resultFmt, flag == 0 ? passed : failed);
  8190. #endif
  8191. return flag;
  8192. } /* END test_wc_Sha224HmacSetKey() */
  8193. /*
  8194. * testing wc_HmacSetKey() on Sha256 hash
  8195. */
  8196. static int test_wc_Sha256HmacSetKey (void)
  8197. {
  8198. int flag = 0;
  8199. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  8200. Hmac hmac;
  8201. int ret, times, itr;
  8202. const char* keys[]=
  8203. {
  8204. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  8205. "\x0b\x0b\x0b",
  8206. #ifndef HAVE_FIPS
  8207. "Jefe", /* smaller than minimum FIPS key size */
  8208. #endif
  8209. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  8210. "\xAA\xAA\xAA"
  8211. };
  8212. times = sizeof(keys) / sizeof(char*);
  8213. flag = 0;
  8214. printf(testingFmt, "wc_HmacSetKey() with SHA256");
  8215. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8216. if (ret != 0)
  8217. return ret;
  8218. for (itr = 0; itr < times; itr++) {
  8219. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  8220. (word32)XSTRLEN(keys[itr]));
  8221. if (ret != 0) {
  8222. flag = ret;
  8223. }
  8224. }
  8225. /* Bad args. */
  8226. if (!flag) {
  8227. ret = wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  8228. (word32)XSTRLEN(keys[0]));
  8229. if (ret != BAD_FUNC_ARG) {
  8230. flag = WOLFSSL_FATAL_ERROR;
  8231. }
  8232. }
  8233. if (!flag) {
  8234. ret = wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0]));
  8235. if (ret != BAD_FUNC_ARG) {
  8236. flag = WOLFSSL_FATAL_ERROR;
  8237. }
  8238. }
  8239. if (!flag) {
  8240. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  8241. (word32)XSTRLEN(keys[0]));
  8242. if (ret != BAD_FUNC_ARG) {
  8243. flag = WOLFSSL_FATAL_ERROR;
  8244. }
  8245. }
  8246. if (!flag) {
  8247. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  8248. #ifdef HAVE_FIPS
  8249. if (ret != HMAC_MIN_KEYLEN_E) {
  8250. flag = WOLFSSL_FATAL_ERROR;
  8251. }
  8252. #else
  8253. if (ret != 0) {
  8254. flag = WOLFSSL_FATAL_ERROR;
  8255. }
  8256. #endif
  8257. }
  8258. wc_HmacFree(&hmac);
  8259. printf(resultFmt, flag == 0 ? passed : failed);
  8260. #endif
  8261. return flag;
  8262. } /* END test_wc_Sha256HmacSetKey() */
  8263. /*
  8264. * testing wc_HmacSetKey on Sha384 hash.
  8265. */
  8266. static int test_wc_Sha384HmacSetKey (void)
  8267. {
  8268. int flag = 0;
  8269. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  8270. Hmac hmac;
  8271. int ret, times, itr;
  8272. const char* keys[]=
  8273. {
  8274. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  8275. "\x0b\x0b\x0b",
  8276. #ifndef HAVE_FIPS
  8277. "Jefe", /* smaller than minimum FIPS key size */
  8278. #endif
  8279. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  8280. "\xAA\xAA\xAA"
  8281. };
  8282. times = sizeof(keys) / sizeof(char*);
  8283. flag = 0;
  8284. printf(testingFmt, "wc_HmacSetKey() with SHA384");
  8285. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8286. if (ret != 0)
  8287. return ret;
  8288. for (itr = 0; itr < times; itr++) {
  8289. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  8290. (word32)XSTRLEN(keys[itr]));
  8291. if (ret != 0) {
  8292. flag = ret;
  8293. }
  8294. }
  8295. /* Bad args. */
  8296. if (!flag) {
  8297. ret = wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  8298. (word32)XSTRLEN(keys[0]));
  8299. if (ret != BAD_FUNC_ARG) {
  8300. flag = WOLFSSL_FATAL_ERROR;
  8301. }
  8302. }
  8303. if (!flag) {
  8304. ret = wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0]));
  8305. if (ret != BAD_FUNC_ARG) {
  8306. flag = WOLFSSL_FATAL_ERROR;
  8307. }
  8308. }
  8309. if (!flag) {
  8310. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  8311. (word32)XSTRLEN(keys[0]));
  8312. if (ret != BAD_FUNC_ARG) {
  8313. flag = WOLFSSL_FATAL_ERROR;
  8314. }
  8315. }
  8316. if (!flag) {
  8317. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  8318. #ifdef HAVE_FIPS
  8319. if (ret != HMAC_MIN_KEYLEN_E) {
  8320. flag = WOLFSSL_FATAL_ERROR;
  8321. }
  8322. #else
  8323. if (ret != 0) {
  8324. flag = WOLFSSL_FATAL_ERROR;
  8325. }
  8326. #endif
  8327. }
  8328. wc_HmacFree(&hmac);
  8329. printf(resultFmt, flag == 0 ? passed : failed);
  8330. #endif
  8331. return flag;
  8332. } /* END test_wc_Sha384HmacSetKey() */
  8333. /*
  8334. * testing wc_HmacUpdate on wc_Md5 hash.
  8335. */
  8336. static int test_wc_Md5HmacUpdate (void)
  8337. {
  8338. int flag = 0;
  8339. #if !defined(NO_HMAC) && !defined(NO_MD5)
  8340. Hmac hmac;
  8341. testVector a, b;
  8342. int ret;
  8343. #ifdef HAVE_FIPS
  8344. const char* keys =
  8345. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  8346. #else
  8347. const char* keys = "Jefe";
  8348. #endif
  8349. a.input = "what do ya want for nothing?";
  8350. a.inLen = XSTRLEN(a.input);
  8351. b.input = "Hi There";
  8352. b.inLen = XSTRLEN(b.input);
  8353. flag = 0;
  8354. printf(testingFmt, "wc_HmacUpdate() with MD5");
  8355. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8356. if (ret != 0)
  8357. return ret;
  8358. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys, (word32)XSTRLEN(keys));
  8359. if (ret != 0) {
  8360. flag = ret;
  8361. }
  8362. if (!flag) {
  8363. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  8364. if (ret != 0) {
  8365. flag = ret;
  8366. }
  8367. }
  8368. /* Update Hmac. */
  8369. if (!flag) {
  8370. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  8371. if (ret != 0) {
  8372. flag = ret;
  8373. }
  8374. }
  8375. /* Test bad args. */
  8376. if (!flag) {
  8377. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  8378. if (ret != BAD_FUNC_ARG) {
  8379. flag = WOLFSSL_FATAL_ERROR;
  8380. }
  8381. }
  8382. if (!flag) {
  8383. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  8384. if (ret != BAD_FUNC_ARG) {
  8385. flag = WOLFSSL_FATAL_ERROR;
  8386. }
  8387. }
  8388. if (!flag) {
  8389. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  8390. if (ret != 0) {
  8391. flag = ret;
  8392. }
  8393. }
  8394. wc_HmacFree(&hmac);
  8395. printf(resultFmt, flag == 0 ? passed : failed);
  8396. #endif
  8397. return flag;
  8398. } /* END test_wc_Md5HmacUpdate */
  8399. /*
  8400. * testing wc_HmacUpdate on SHA hash.
  8401. */
  8402. static int test_wc_ShaHmacUpdate (void)
  8403. {
  8404. int flag = 0;
  8405. #if !defined(NO_HMAC) && !defined(NO_SHA)
  8406. Hmac hmac;
  8407. testVector a, b;
  8408. int ret;
  8409. #ifdef HAVE_FIPS
  8410. const char* keys =
  8411. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  8412. #else
  8413. const char* keys = "Jefe";
  8414. #endif
  8415. a.input = "what do ya want for nothing?";
  8416. a.inLen = XSTRLEN(a.input);
  8417. b.input = "Hi There";
  8418. b.inLen = XSTRLEN(b.input);
  8419. flag = 0;
  8420. printf(testingFmt, "wc_HmacUpdate() with SHA");
  8421. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8422. if (ret != 0)
  8423. return ret;
  8424. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys, (word32)XSTRLEN(keys));
  8425. if (ret != 0) {
  8426. flag = ret;
  8427. }
  8428. if (!flag) {
  8429. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  8430. if (ret != 0) {
  8431. flag = ret;
  8432. }
  8433. }
  8434. /* Update Hmac. */
  8435. if (!flag) {
  8436. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  8437. if (ret != 0) {
  8438. flag = ret;
  8439. }
  8440. }
  8441. /* Test bad args. */
  8442. if (!flag) {
  8443. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  8444. if (ret != BAD_FUNC_ARG) {
  8445. flag = WOLFSSL_FATAL_ERROR;
  8446. }
  8447. }
  8448. if (!flag) {
  8449. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  8450. if (ret != BAD_FUNC_ARG) {
  8451. flag = WOLFSSL_FATAL_ERROR;
  8452. }
  8453. }
  8454. if (!flag) {
  8455. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  8456. if (ret != 0) {
  8457. flag = ret;
  8458. }
  8459. }
  8460. wc_HmacFree(&hmac);
  8461. printf(resultFmt, flag == 0 ? passed : failed);
  8462. #endif
  8463. return flag;
  8464. } /* END test_wc_ShaHmacUpdate */
  8465. /*
  8466. * testing wc_HmacUpdate on SHA224 hash.
  8467. */
  8468. static int test_wc_Sha224HmacUpdate (void)
  8469. {
  8470. int flag = 0;
  8471. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  8472. Hmac hmac;
  8473. testVector a, b;
  8474. int ret;
  8475. #ifdef HAVE_FIPS
  8476. const char* keys =
  8477. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  8478. #else
  8479. const char* keys = "Jefe";
  8480. #endif
  8481. a.input = "what do ya want for nothing?";
  8482. a.inLen = XSTRLEN(a.input);
  8483. b.input = "Hi There";
  8484. b.inLen = XSTRLEN(b.input);
  8485. flag = 0;
  8486. printf(testingFmt, "wc_HmacUpdate() with SHA224");
  8487. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8488. if (ret != 0)
  8489. return ret;
  8490. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys, (word32)XSTRLEN(keys));
  8491. if (ret != 0) {
  8492. flag = ret;
  8493. }
  8494. if (!flag) {
  8495. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  8496. if (ret != 0) {
  8497. flag = ret;
  8498. }
  8499. }
  8500. /* Update Hmac. */
  8501. if (!flag) {
  8502. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  8503. if (ret != 0) {
  8504. flag = ret;
  8505. }
  8506. }
  8507. /* Test bad args. */
  8508. if (!flag) {
  8509. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  8510. if (ret != BAD_FUNC_ARG) {
  8511. flag = WOLFSSL_FATAL_ERROR;
  8512. }
  8513. }
  8514. if (!flag) {
  8515. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  8516. if (ret != BAD_FUNC_ARG) {
  8517. flag = WOLFSSL_FATAL_ERROR;
  8518. }
  8519. }
  8520. if (!flag) {
  8521. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  8522. if (ret != 0) {
  8523. flag = ret;
  8524. }
  8525. }
  8526. wc_HmacFree(&hmac);
  8527. printf(resultFmt, flag == 0 ? passed : failed);
  8528. #endif
  8529. return flag;
  8530. } /* END test_wc_Sha224HmacUpdate */
  8531. /*
  8532. * testing wc_HmacUpdate on SHA256 hash.
  8533. */
  8534. static int test_wc_Sha256HmacUpdate (void)
  8535. {
  8536. int flag = 0;
  8537. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  8538. Hmac hmac;
  8539. testVector a, b;
  8540. int ret;
  8541. #ifdef HAVE_FIPS
  8542. const char* keys =
  8543. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  8544. #else
  8545. const char* keys = "Jefe";
  8546. #endif
  8547. a.input = "what do ya want for nothing?";
  8548. a.inLen = XSTRLEN(a.input);
  8549. b.input = "Hi There";
  8550. b.inLen = XSTRLEN(b.input);
  8551. flag = 0;
  8552. printf(testingFmt, "wc_HmacUpdate() with WC_SHA256");
  8553. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8554. if (ret != 0)
  8555. return ret;
  8556. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys, (word32)XSTRLEN(keys));
  8557. if (ret != 0) {
  8558. flag = ret;
  8559. }
  8560. if (!flag) {
  8561. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  8562. if (ret != 0) {
  8563. flag = ret;
  8564. }
  8565. }
  8566. /* Update Hmac. */
  8567. if (!flag) {
  8568. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  8569. if (ret != 0) {
  8570. flag = ret;
  8571. }
  8572. }
  8573. /* Test bad args. */
  8574. if (!flag) {
  8575. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  8576. if (ret != BAD_FUNC_ARG) {
  8577. flag = WOLFSSL_FATAL_ERROR;
  8578. }
  8579. }
  8580. if (!flag) {
  8581. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  8582. if (ret != BAD_FUNC_ARG) {
  8583. flag = WOLFSSL_FATAL_ERROR;
  8584. }
  8585. }
  8586. if (!flag) {
  8587. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  8588. if (ret != 0) {
  8589. flag = ret;
  8590. }
  8591. }
  8592. wc_HmacFree(&hmac);
  8593. printf(resultFmt, flag == 0 ? passed : failed);
  8594. #endif
  8595. return flag;
  8596. } /* END test_wc_Sha256HmacUpdate */
  8597. /*
  8598. * testing wc_HmacUpdate on SHA384 hash.
  8599. */
  8600. static int test_wc_Sha384HmacUpdate (void)
  8601. {
  8602. int flag = 0;
  8603. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  8604. Hmac hmac;
  8605. testVector a, b;
  8606. int ret;
  8607. #ifdef HAVE_FIPS
  8608. const char* keys =
  8609. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  8610. #else
  8611. const char* keys = "Jefe";
  8612. #endif
  8613. a.input = "what do ya want for nothing?";
  8614. a.inLen = XSTRLEN(a.input);
  8615. b.input = "Hi There";
  8616. b.inLen = XSTRLEN(b.input);
  8617. flag = 0;
  8618. printf(testingFmt, "wc_HmacUpdate() with SHA384");
  8619. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8620. if (ret != 0)
  8621. return ret;
  8622. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys, (word32)XSTRLEN(keys));
  8623. if (ret != 0) {
  8624. flag = ret;
  8625. }
  8626. if (!flag) {
  8627. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  8628. if (ret != 0) {
  8629. flag = ret;
  8630. }
  8631. }
  8632. /* Update Hmac. */
  8633. if (!flag) {
  8634. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  8635. if (ret != 0) {
  8636. flag = ret;
  8637. }
  8638. }
  8639. /* Test bad args. */
  8640. if (!flag) {
  8641. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  8642. if (ret != BAD_FUNC_ARG) {
  8643. flag = WOLFSSL_FATAL_ERROR;
  8644. }
  8645. }
  8646. if (!flag) {
  8647. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  8648. if (ret != BAD_FUNC_ARG) {
  8649. flag = WOLFSSL_FATAL_ERROR;
  8650. }
  8651. }
  8652. if (!flag) {
  8653. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  8654. if (ret != 0) {
  8655. flag = ret;
  8656. }
  8657. }
  8658. wc_HmacFree(&hmac);
  8659. printf(resultFmt, flag == 0 ? passed : failed);
  8660. #endif
  8661. return flag;
  8662. } /* END test_wc_Sha384HmacUpdate */
  8663. /*
  8664. * Testing wc_HmacFinal() with MD5
  8665. */
  8666. static int test_wc_Md5HmacFinal (void)
  8667. {
  8668. int flag = 0;
  8669. #if !defined(NO_HMAC) && !defined(NO_MD5)
  8670. Hmac hmac;
  8671. byte hash[WC_MD5_DIGEST_SIZE];
  8672. testVector a;
  8673. int ret;
  8674. const char* key;
  8675. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  8676. a.input = "Hi There";
  8677. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  8678. "\x9d";
  8679. a.inLen = XSTRLEN(a.input);
  8680. a.outLen = XSTRLEN(a.output);
  8681. flag = 0;
  8682. printf(testingFmt, "wc_HmacFinal() with MD5");
  8683. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8684. if (ret != 0)
  8685. return ret;
  8686. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key));
  8687. if (ret != 0) {
  8688. flag = ret;
  8689. }
  8690. if (!flag) {
  8691. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  8692. if (ret != 0) {
  8693. flag = ret;
  8694. }
  8695. }
  8696. if (!flag) {
  8697. ret = wc_HmacFinal(&hmac, hash);
  8698. if (ret != 0) {
  8699. flag = ret;
  8700. }
  8701. }
  8702. if (!flag) {
  8703. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  8704. flag = WOLFSSL_FATAL_ERROR;
  8705. }
  8706. }
  8707. /* Try bad parameters. */
  8708. if (!flag) {
  8709. ret = wc_HmacFinal(NULL, hash);
  8710. if (ret != BAD_FUNC_ARG) {
  8711. flag = WOLFSSL_FATAL_ERROR;
  8712. }
  8713. }
  8714. #ifndef HAVE_FIPS
  8715. if (!flag) {
  8716. ret = wc_HmacFinal(&hmac, NULL);
  8717. if (ret != BAD_FUNC_ARG) {
  8718. flag = WOLFSSL_FATAL_ERROR;
  8719. }
  8720. }
  8721. #endif
  8722. wc_HmacFree(&hmac);
  8723. printf(resultFmt, flag == 0 ? passed : failed);
  8724. #endif
  8725. return flag;
  8726. } /* END test_wc_Md5HmacFinal */
  8727. /*
  8728. * Testing wc_HmacFinal() with SHA
  8729. */
  8730. static int test_wc_ShaHmacFinal (void)
  8731. {
  8732. int flag = 0;
  8733. #if !defined(NO_HMAC) && !defined(NO_SHA)
  8734. Hmac hmac;
  8735. byte hash[WC_SHA_DIGEST_SIZE];
  8736. testVector a;
  8737. int ret;
  8738. const char* key;
  8739. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  8740. "\x0b\x0b\x0b";
  8741. a.input = "Hi There";
  8742. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  8743. "\x8e\xf1\x46\xbe\x00";
  8744. a.inLen = XSTRLEN(a.input);
  8745. a.outLen = XSTRLEN(a.output);
  8746. flag = 0;
  8747. printf(testingFmt, "wc_HmacFinal() with SHA");
  8748. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8749. if (ret != 0)
  8750. return ret;
  8751. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key));
  8752. if (ret != 0) {
  8753. flag = ret;
  8754. }
  8755. if (!flag) {
  8756. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  8757. if (ret != 0) {
  8758. flag = ret;
  8759. }
  8760. }
  8761. if (!flag) {
  8762. ret = wc_HmacFinal(&hmac, hash);
  8763. if (ret != 0) {
  8764. flag = ret;
  8765. }
  8766. }
  8767. if (!flag) {
  8768. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  8769. flag = WOLFSSL_FATAL_ERROR;
  8770. }
  8771. }
  8772. /* Try bad parameters. */
  8773. if (!flag) {
  8774. ret = wc_HmacFinal(NULL, hash);
  8775. if (ret != BAD_FUNC_ARG) {
  8776. flag = WOLFSSL_FATAL_ERROR;
  8777. }
  8778. }
  8779. #ifndef HAVE_FIPS
  8780. if (!flag) {
  8781. ret = wc_HmacFinal(&hmac, NULL);
  8782. if (ret != BAD_FUNC_ARG) {
  8783. flag = WOLFSSL_FATAL_ERROR;
  8784. }
  8785. }
  8786. #endif
  8787. wc_HmacFree(&hmac);
  8788. printf(resultFmt, flag == 0 ? passed : failed);
  8789. #endif
  8790. return flag;
  8791. } /* END test_wc_ShaHmacFinal */
  8792. /*
  8793. * Testing wc_HmacFinal() with SHA224
  8794. */
  8795. static int test_wc_Sha224HmacFinal (void)
  8796. {
  8797. int flag = 0;
  8798. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  8799. Hmac hmac;
  8800. byte hash[WC_SHA224_DIGEST_SIZE];
  8801. testVector a;
  8802. int ret;
  8803. const char* key;
  8804. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  8805. "\x0b\x0b\x0b";
  8806. a.input = "Hi There";
  8807. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  8808. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  8809. a.inLen = XSTRLEN(a.input);
  8810. a.outLen = XSTRLEN(a.output);
  8811. flag = 0;
  8812. printf(testingFmt, "wc_HmacFinal() with SHA224");
  8813. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8814. if (ret != 0)
  8815. return ret;
  8816. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key, (word32)XSTRLEN(key));
  8817. if (ret != 0) {
  8818. flag = ret;
  8819. }
  8820. if (!flag) {
  8821. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  8822. if (ret != 0) {
  8823. flag = ret;
  8824. }
  8825. }
  8826. if (!flag) {
  8827. ret = wc_HmacFinal(&hmac, hash);
  8828. if (ret != 0) {
  8829. flag = ret;
  8830. }
  8831. }
  8832. if (!flag) {
  8833. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  8834. flag = WOLFSSL_FATAL_ERROR;
  8835. }
  8836. }
  8837. /* Try bad parameters. */
  8838. if (!flag) {
  8839. ret = wc_HmacFinal(NULL, hash);
  8840. if (ret != BAD_FUNC_ARG) {
  8841. flag = WOLFSSL_FATAL_ERROR;
  8842. }
  8843. }
  8844. #ifndef HAVE_FIPS
  8845. if (!flag) {
  8846. ret = wc_HmacFinal(&hmac, NULL);
  8847. if (ret != BAD_FUNC_ARG) {
  8848. flag = WOLFSSL_FATAL_ERROR;
  8849. }
  8850. }
  8851. #endif
  8852. wc_HmacFree(&hmac);
  8853. printf(resultFmt, flag == 0 ? passed : failed);
  8854. #endif
  8855. return flag;
  8856. } /* END test_wc_Sha224HmacFinal */
  8857. /*
  8858. * Testing wc_HmacFinal() with SHA256
  8859. */
  8860. static int test_wc_Sha256HmacFinal (void)
  8861. {
  8862. int flag = 0;
  8863. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  8864. Hmac hmac;
  8865. byte hash[WC_SHA256_DIGEST_SIZE];
  8866. testVector a;
  8867. int ret;
  8868. const char* key;
  8869. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  8870. "\x0b\x0b\x0b";
  8871. a.input = "Hi There";
  8872. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  8873. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  8874. "\xcf\xf7";
  8875. a.inLen = XSTRLEN(a.input);
  8876. a.outLen = XSTRLEN(a.output);
  8877. flag = 0;
  8878. printf(testingFmt, "wc_HmacFinal() with WC_SHA256");
  8879. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8880. if (ret != 0)
  8881. return ret;
  8882. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key, (word32)XSTRLEN(key));
  8883. if (ret != 0) {
  8884. flag = ret;
  8885. }
  8886. if (!flag) {
  8887. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  8888. if (ret != 0) {
  8889. flag = ret;
  8890. }
  8891. }
  8892. if (!flag) {
  8893. ret = wc_HmacFinal(&hmac, hash);
  8894. if (ret != 0) {
  8895. flag = ret;
  8896. }
  8897. }
  8898. if (!flag) {
  8899. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  8900. flag = WOLFSSL_FATAL_ERROR;
  8901. }
  8902. }
  8903. /* Try bad parameters. */
  8904. if (!flag) {
  8905. ret = wc_HmacFinal(NULL, hash);
  8906. if (ret != BAD_FUNC_ARG) {
  8907. flag = WOLFSSL_FATAL_ERROR;
  8908. }
  8909. }
  8910. #ifndef HAVE_FIPS
  8911. if (!flag) {
  8912. ret = wc_HmacFinal(&hmac, NULL);
  8913. if (ret != BAD_FUNC_ARG) {
  8914. flag = WOLFSSL_FATAL_ERROR;
  8915. }
  8916. }
  8917. #endif
  8918. wc_HmacFree(&hmac);
  8919. printf(resultFmt, flag == 0 ? passed : failed);
  8920. #endif
  8921. return flag;
  8922. } /* END test_wc_Sha256HmacFinal */
  8923. /*
  8924. * Testing wc_HmacFinal() with SHA384
  8925. */
  8926. static int test_wc_Sha384HmacFinal (void)
  8927. {
  8928. int flag = 0;
  8929. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  8930. Hmac hmac;
  8931. byte hash[WC_SHA384_DIGEST_SIZE];
  8932. testVector a;
  8933. int ret;
  8934. const char* key;
  8935. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  8936. "\x0b\x0b\x0b";
  8937. a.input = "Hi There";
  8938. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  8939. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  8940. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  8941. "\xfa\x9c\xb6";
  8942. a.inLen = XSTRLEN(a.input);
  8943. a.outLen = XSTRLEN(a.output);
  8944. flag = 0;
  8945. printf(testingFmt, "wc_HmacFinal() with SHA384");
  8946. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  8947. if (ret != 0)
  8948. return ret;
  8949. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key, (word32)XSTRLEN(key));
  8950. if (ret != 0) {
  8951. flag = ret;
  8952. }
  8953. if (!flag) {
  8954. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  8955. if (ret != 0) {
  8956. flag = ret;
  8957. }
  8958. }
  8959. if (!flag) {
  8960. ret = wc_HmacFinal(&hmac, hash);
  8961. if (ret != 0) {
  8962. flag = ret;
  8963. }
  8964. }
  8965. if (!flag) {
  8966. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  8967. flag = WOLFSSL_FATAL_ERROR;
  8968. }
  8969. }
  8970. /* Try bad parameters. */
  8971. if (!flag) {
  8972. ret = wc_HmacFinal(NULL, hash);
  8973. if (ret != BAD_FUNC_ARG) {
  8974. flag = WOLFSSL_FATAL_ERROR;
  8975. }
  8976. }
  8977. #ifndef HAVE_FIPS
  8978. if (!flag) {
  8979. ret = wc_HmacFinal(&hmac, NULL);
  8980. if (ret != BAD_FUNC_ARG) {
  8981. flag = WOLFSSL_FATAL_ERROR;
  8982. }
  8983. }
  8984. #endif
  8985. wc_HmacFree(&hmac);
  8986. printf(resultFmt, flag == 0 ? passed : failed);
  8987. #endif
  8988. return flag;
  8989. } /* END test_wc_Sha384HmacFinal */
  8990. /*
  8991. * Testing wc_InitCmac()
  8992. */
  8993. static int test_wc_InitCmac (void)
  8994. {
  8995. int ret = 0;
  8996. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  8997. Cmac cmac1, cmac2, cmac3;
  8998. /* AES 128 key. */
  8999. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  9000. "\x09\x10\x11\x12\x13\x14\x15\x16";
  9001. /* AES 192 key. */
  9002. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  9003. "\x09\x01\x11\x12\x13\x14\x15\x16"
  9004. "\x01\x02\x03\x04\x05\x06\x07\x08";
  9005. /* AES 256 key. */
  9006. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  9007. "\x09\x01\x11\x12\x13\x14\x15\x16"
  9008. "\x01\x02\x03\x04\x05\x06\x07\x08"
  9009. "\x09\x01\x11\x12\x13\x14\x15\x16";
  9010. word32 key1Sz = (word32)sizeof(key1) - 1;
  9011. word32 key2Sz = (word32)sizeof(key2) - 1;
  9012. word32 key3Sz = (word32)sizeof(key3) - 1;
  9013. int type = WC_CMAC_AES;
  9014. printf(testingFmt, "wc_InitCmac()");
  9015. #ifdef WOLFSSL_AES_128
  9016. ret = wc_InitCmac(&cmac1, key1, key1Sz, type, NULL);
  9017. #endif
  9018. #ifdef WOLFSSL_AES_192
  9019. if (ret == 0)
  9020. ret = wc_InitCmac(&cmac2, key2, key2Sz, type, NULL);
  9021. #endif
  9022. #ifdef WOLFSSL_AES_256
  9023. if (ret == 0)
  9024. ret = wc_InitCmac(&cmac3, key3, key3Sz, type, NULL);
  9025. #endif
  9026. /* Test bad args. */
  9027. if (ret == 0) {
  9028. ret = wc_InitCmac(NULL, key3, key3Sz, type, NULL);
  9029. if (ret == BAD_FUNC_ARG) {
  9030. ret = wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL);
  9031. }
  9032. if (ret == BAD_FUNC_ARG) {
  9033. ret = wc_InitCmac(&cmac3, key3, 0, type, NULL);
  9034. }
  9035. if (ret == BAD_FUNC_ARG) {
  9036. ret = wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL);
  9037. }
  9038. if (ret == BAD_FUNC_ARG) {
  9039. ret = 0;
  9040. } else {
  9041. ret = WOLFSSL_FATAL_ERROR;
  9042. }
  9043. }
  9044. (void)key1;
  9045. (void)key1Sz;
  9046. (void)key2;
  9047. (void)key2Sz;
  9048. (void)cmac1;
  9049. (void)cmac2;
  9050. printf(resultFmt, ret == 0 ? passed : failed);
  9051. #endif
  9052. return ret;
  9053. } /* END test_wc_InitCmac */
  9054. /*
  9055. * Testing wc_CmacUpdate()
  9056. */
  9057. static int test_wc_CmacUpdate (void)
  9058. {
  9059. int ret = 0;
  9060. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  9061. Cmac cmac;
  9062. byte key[] =
  9063. {
  9064. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  9065. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  9066. };
  9067. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  9068. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  9069. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  9070. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  9071. "\xf4";
  9072. word32 inSz = (word32)sizeof(in) - 1;
  9073. word32 keySz = (word32)sizeof(key);
  9074. int type = WC_CMAC_AES;
  9075. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  9076. if (ret != 0) {
  9077. return ret;
  9078. }
  9079. printf(testingFmt, "wc_CmacUpdate()");
  9080. ret = wc_CmacUpdate(&cmac, in, inSz);
  9081. /* Test bad args. */
  9082. if (ret == 0) {
  9083. ret = wc_CmacUpdate(NULL, in, inSz);
  9084. if (ret == BAD_FUNC_ARG) {
  9085. ret = wc_CmacUpdate(&cmac, NULL, 30);
  9086. }
  9087. if (ret == BAD_FUNC_ARG) {
  9088. ret = 0;
  9089. } else if (ret == 0) {
  9090. ret = WOLFSSL_FATAL_ERROR;
  9091. }
  9092. }
  9093. printf(resultFmt, ret == 0 ? passed : failed);
  9094. #endif
  9095. return ret;
  9096. } /* END test_wc_CmacUpdate */
  9097. /*
  9098. * Testing wc_CmacFinal()
  9099. */
  9100. static int test_wc_CmacFinal (void)
  9101. {
  9102. int ret = 0;
  9103. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  9104. Cmac cmac;
  9105. byte key[] =
  9106. {
  9107. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  9108. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  9109. };
  9110. byte msg[] =
  9111. {
  9112. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  9113. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  9114. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  9115. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  9116. 0xf4
  9117. };
  9118. /* Test vectors from CMACGenAES128.rsp from
  9119. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  9120. * Per RFC4493 truncation of lsb is possible.
  9121. */
  9122. byte expMac[] =
  9123. {
  9124. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  9125. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  9126. };
  9127. byte mac[AES_BLOCK_SIZE];
  9128. word32 msgSz = (word32)sizeof(msg);
  9129. word32 keySz = (word32)sizeof(key);
  9130. word32 macSz = sizeof(mac);
  9131. word32 badMacSz = 17;
  9132. int expMacSz = sizeof(expMac);
  9133. int type = WC_CMAC_AES;
  9134. XMEMSET(mac, 0, macSz);
  9135. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  9136. if (ret != 0) {
  9137. return ret;
  9138. }
  9139. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  9140. printf(testingFmt, "wc_CmacFinal()");
  9141. if (ret == 0) {
  9142. ret = wc_CmacFinal(&cmac, mac, &macSz);
  9143. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  9144. ret = WOLFSSL_FATAL_ERROR;
  9145. }
  9146. /* Pass in bad args. */
  9147. if (ret == 0) {
  9148. ret = wc_CmacFinal(NULL, mac, &macSz);
  9149. if (ret == BAD_FUNC_ARG) {
  9150. ret = wc_CmacFinal(&cmac, NULL, &macSz);
  9151. }
  9152. if (ret == BAD_FUNC_ARG) {
  9153. ret = wc_CmacFinal(&cmac, mac, &badMacSz);
  9154. if (ret == BUFFER_E) {
  9155. ret = 0;
  9156. }
  9157. } else if (ret == 0) {
  9158. ret = WOLFSSL_FATAL_ERROR;
  9159. }
  9160. }
  9161. }
  9162. printf(resultFmt, ret == 0 ? passed : failed);
  9163. #endif
  9164. return ret;
  9165. } /* END test_wc_CmacFinal */
  9166. /*
  9167. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  9168. */
  9169. static int test_wc_AesCmacGenerate (void)
  9170. {
  9171. int ret = 0;
  9172. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  9173. Cmac cmac;
  9174. byte key[] =
  9175. {
  9176. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  9177. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  9178. };
  9179. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  9180. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  9181. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  9182. "\x3d\x32\x65\x4c\x66\x23\xc5";
  9183. byte mac[AES_BLOCK_SIZE];
  9184. word32 keySz = sizeof(key);
  9185. word32 macSz = sizeof(mac);
  9186. word32 msgSz = sizeof(msg) - 1;
  9187. word32 expMacSz = sizeof(expMac) - 1;
  9188. int type = WC_CMAC_AES;
  9189. XMEMSET(mac, 0, macSz);
  9190. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  9191. if (ret != 0) {
  9192. return ret;
  9193. }
  9194. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  9195. if (ret != 0) {
  9196. return ret;
  9197. }
  9198. printf(testingFmt, "wc_AesCmacGenerate()");
  9199. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz);
  9200. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  9201. ret = WOLFSSL_FATAL_ERROR;
  9202. }
  9203. /* Pass in bad args. */
  9204. if (ret == 0) {
  9205. ret = wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz);
  9206. if (ret == BAD_FUNC_ARG) {
  9207. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz);
  9208. }
  9209. if (ret == BAD_FUNC_ARG) {
  9210. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0);
  9211. }
  9212. if (ret == BAD_FUNC_ARG) {
  9213. ret = wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz);
  9214. }
  9215. if (ret == BAD_FUNC_ARG) {
  9216. ret = 0;
  9217. } else if (ret == 0) {
  9218. ret = WOLFSSL_FATAL_ERROR;
  9219. }
  9220. }
  9221. printf(resultFmt, ret == 0 ? passed : failed);
  9222. if (ret == 0) {
  9223. printf(testingFmt, "wc_AesCmacVerify()");
  9224. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz);
  9225. /* Test bad args. */
  9226. if (ret == 0) {
  9227. ret = wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz);
  9228. if (ret == BAD_FUNC_ARG) {
  9229. ret = wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz);
  9230. }
  9231. if (ret == BAD_FUNC_ARG) {
  9232. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz);
  9233. }
  9234. if (ret == BAD_FUNC_ARG) {
  9235. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0);
  9236. }
  9237. if (ret == BAD_FUNC_ARG) {
  9238. ret = wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz);
  9239. }
  9240. if (ret == BAD_FUNC_ARG) {
  9241. ret = 0;
  9242. } else if (ret == 0) {
  9243. ret = WOLFSSL_FATAL_ERROR;
  9244. }
  9245. }
  9246. printf(resultFmt, ret == 0 ? passed : failed);
  9247. }
  9248. #endif
  9249. return ret;
  9250. } /* END test_wc_AesCmacGenerate */
  9251. /*
  9252. * unit test for wc_Des3_SetIV()
  9253. */
  9254. static int test_wc_Des3_SetIV (void)
  9255. {
  9256. int ret = 0;
  9257. #ifndef NO_DES3
  9258. Des3 des;
  9259. const byte key[] =
  9260. {
  9261. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  9262. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  9263. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  9264. };
  9265. const byte iv[] =
  9266. {
  9267. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  9268. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  9269. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  9270. };
  9271. printf(testingFmt, "wc_Des3_SetIV()");
  9272. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  9273. if (ret != 0)
  9274. return ret;
  9275. /* DES_ENCRYPTION or DES_DECRYPTION */
  9276. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  9277. if (ret == 0) {
  9278. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  9279. ret = WOLFSSL_FATAL_ERROR;
  9280. }
  9281. }
  9282. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  9283. /* Test explicitly wc_Des3_SetIV() */
  9284. if (ret == 0) {
  9285. ret = wc_Des3_SetIV(NULL, iv);
  9286. if (ret == BAD_FUNC_ARG) {
  9287. ret = wc_Des3_SetIV(&des, NULL);
  9288. } else if (ret == 0) {
  9289. ret = WOLFSSL_FATAL_ERROR;
  9290. }
  9291. }
  9292. #endif
  9293. wc_Des3Free(&des);
  9294. printf(resultFmt, ret == 0 ? passed : failed);
  9295. #endif
  9296. return ret;
  9297. } /* END test_wc_Des3_SetIV */
  9298. /*
  9299. * unit test for wc_Des3_SetKey()
  9300. */
  9301. static int test_wc_Des3_SetKey (void)
  9302. {
  9303. int ret = 0;
  9304. #ifndef NO_DES3
  9305. Des3 des;
  9306. const byte key[] =
  9307. {
  9308. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  9309. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  9310. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  9311. };
  9312. const byte iv[] =
  9313. {
  9314. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  9315. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  9316. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  9317. };
  9318. printf(testingFmt, "wc_Des3_SetKey()");
  9319. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  9320. if (ret != 0)
  9321. return ret;
  9322. /* DES_ENCRYPTION or DES_DECRYPTION */
  9323. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  9324. if (ret == 0) {
  9325. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  9326. ret = WOLFSSL_FATAL_ERROR;
  9327. }
  9328. }
  9329. /* Test bad args. */
  9330. if (ret == 0) {
  9331. ret = wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION);
  9332. if (ret == BAD_FUNC_ARG) {
  9333. ret = wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION);
  9334. }
  9335. if (ret == BAD_FUNC_ARG) {
  9336. ret = wc_Des3_SetKey(&des, key, iv, -1);
  9337. }
  9338. if (ret == BAD_FUNC_ARG) {
  9339. /* Default case. Should return 0. */
  9340. ret = wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION);
  9341. }
  9342. } /* END if ret != 0 */
  9343. wc_Des3Free(&des);
  9344. printf(resultFmt, ret == 0 ? passed : failed);
  9345. #endif
  9346. return ret;
  9347. } /* END test_wc_Des3_SetKey */
  9348. /*
  9349. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  9350. */
  9351. static int test_wc_Des3_CbcEncryptDecrypt (void)
  9352. {
  9353. int ret = 0;
  9354. #ifndef NO_DES3
  9355. Des3 des;
  9356. byte cipher[24];
  9357. byte plain[24];
  9358. const byte key[] =
  9359. {
  9360. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  9361. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  9362. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  9363. };
  9364. const byte iv[] =
  9365. {
  9366. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  9367. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  9368. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  9369. };
  9370. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  9371. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  9372. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  9373. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  9374. };
  9375. printf(testingFmt, "wc_Des3_CbcEncrypt()");
  9376. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  9377. if (ret != 0)
  9378. return ret;
  9379. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  9380. if (ret == 0) {
  9381. ret = wc_Des3_CbcEncrypt(&des, cipher, vector, 24);
  9382. if (ret == 0) {
  9383. ret = wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION);
  9384. }
  9385. if (ret == 0) {
  9386. ret = wc_Des3_CbcDecrypt(&des, plain, cipher, 24);
  9387. }
  9388. }
  9389. if (ret == 0) {
  9390. if (XMEMCMP(plain, vector, 24) != 0) {
  9391. ret = WOLFSSL_FATAL_ERROR;
  9392. }
  9393. }
  9394. /* Pass in bad args. */
  9395. if (ret == 0) {
  9396. ret = wc_Des3_CbcEncrypt(NULL, cipher, vector, 24);
  9397. if (ret == BAD_FUNC_ARG) {
  9398. ret = wc_Des3_CbcEncrypt(&des, NULL, vector, 24);
  9399. }
  9400. if (ret == BAD_FUNC_ARG) {
  9401. ret = wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector));
  9402. }
  9403. if (ret != BAD_FUNC_ARG) {
  9404. ret = WOLFSSL_FATAL_ERROR;
  9405. } else {
  9406. ret = 0;
  9407. }
  9408. }
  9409. if (ret == 0) {
  9410. ret = wc_Des3_CbcDecrypt(NULL, plain, cipher, 24);
  9411. if (ret == BAD_FUNC_ARG) {
  9412. ret = wc_Des3_CbcDecrypt(&des, NULL, cipher, 24);
  9413. }
  9414. if (ret == BAD_FUNC_ARG) {
  9415. ret = wc_Des3_CbcDecrypt(&des, plain, NULL, 24);
  9416. }
  9417. if (ret != BAD_FUNC_ARG) {
  9418. ret = WOLFSSL_FATAL_ERROR;
  9419. } else {
  9420. ret = 0;
  9421. }
  9422. }
  9423. wc_Des3Free(&des);
  9424. printf(resultFmt, ret == 0 ? passed : failed);
  9425. #endif
  9426. return ret;
  9427. } /* END wc_Des3_CbcEncrypt */
  9428. /*
  9429. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  9430. */
  9431. static int test_wc_Des3_CbcEncryptDecryptWithKey (void)
  9432. {
  9433. int ret = 0;
  9434. #ifndef NO_DES3
  9435. word32 vectorSz, cipherSz;
  9436. byte cipher[24];
  9437. byte plain[24];
  9438. byte vector[] = /* Now is the time for all w/o trailing 0 */
  9439. {
  9440. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  9441. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  9442. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  9443. };
  9444. byte key[] =
  9445. {
  9446. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  9447. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  9448. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  9449. };
  9450. byte iv[] =
  9451. {
  9452. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  9453. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  9454. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  9455. };
  9456. vectorSz = sizeof(byte) * 24;
  9457. cipherSz = sizeof(byte) * 24;
  9458. printf(testingFmt, "wc_Des3_CbcEncryptWithKey()");
  9459. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv);
  9460. if (ret == 0) {
  9461. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv);
  9462. if (ret == 0) {
  9463. if (XMEMCMP(plain, vector, 24) != 0) {
  9464. ret = WOLFSSL_FATAL_ERROR;
  9465. }
  9466. }
  9467. }
  9468. /* pass in bad args. */
  9469. if (ret == 0) {
  9470. ret = wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv);
  9471. if (ret == BAD_FUNC_ARG) {
  9472. ret = wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv);
  9473. }
  9474. if (ret == BAD_FUNC_ARG) {
  9475. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv);
  9476. }
  9477. if (ret == BAD_FUNC_ARG) {
  9478. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz,
  9479. key, NULL);
  9480. } else {
  9481. /* Return code catch. */
  9482. ret = WOLFSSL_FAILURE;
  9483. }
  9484. }
  9485. if (ret == 0) {
  9486. ret = wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv);
  9487. if (ret == BAD_FUNC_ARG) {
  9488. ret = wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv);
  9489. }
  9490. if (ret == BAD_FUNC_ARG) {
  9491. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv);
  9492. }
  9493. if (ret == BAD_FUNC_ARG) {
  9494. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL);
  9495. } else {
  9496. ret = WOLFSSL_FAILURE;
  9497. }
  9498. }
  9499. printf(resultFmt, ret == 0 ? passed : failed);
  9500. #endif
  9501. return ret;
  9502. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  9503. /*
  9504. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  9505. */
  9506. static int test_wc_Chacha_SetKey (void)
  9507. {
  9508. int ret = 0;
  9509. #ifdef HAVE_CHACHA
  9510. ChaCha ctx;
  9511. const byte key[] =
  9512. {
  9513. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  9514. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  9515. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  9516. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  9517. };
  9518. byte cipher[128];
  9519. printf(testingFmt, "wc_Chacha_SetKey()");
  9520. ret = wc_Chacha_SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  9521. /* Test bad args. */
  9522. if (ret == 0) {
  9523. ret = wc_Chacha_SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  9524. if (ret == BAD_FUNC_ARG) {
  9525. ret = wc_Chacha_SetKey(&ctx, key, 18);
  9526. }
  9527. if (ret == BAD_FUNC_ARG) {
  9528. ret = 0;
  9529. } else {
  9530. ret = WOLFSSL_FATAL_ERROR;
  9531. }
  9532. }
  9533. printf(resultFmt, ret == 0 ? passed : failed);
  9534. if (ret != 0) {
  9535. return ret;
  9536. }
  9537. printf(testingFmt, "wc_Chacha_SetIV");
  9538. ret = wc_Chacha_SetIV(&ctx, cipher, 0);
  9539. if (ret == 0) {
  9540. /* Test bad args. */
  9541. ret = wc_Chacha_SetIV(NULL, cipher, 0);
  9542. if (ret == BAD_FUNC_ARG) {
  9543. ret = 0;
  9544. } else {
  9545. ret = WOLFSSL_FAILURE;
  9546. }
  9547. }
  9548. printf(resultFmt, ret == 0 ? passed : failed);
  9549. #endif
  9550. return ret;
  9551. } /* END test_wc_Chacha_SetKey */
  9552. /*
  9553. * unit test for wc_Poly1305SetKey()
  9554. */
  9555. static int test_wc_Poly1305SetKey(void)
  9556. {
  9557. int ret = 0;
  9558. #ifdef HAVE_POLY1305
  9559. Poly1305 ctx;
  9560. const byte key[] =
  9561. {
  9562. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  9563. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  9564. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  9565. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  9566. };
  9567. printf(testingFmt, "wc_Poly1305_SetKey()");
  9568. ret = wc_Poly1305SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  9569. /* Test bad args. */
  9570. if (ret == 0) {
  9571. ret = wc_Poly1305SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  9572. if(ret == BAD_FUNC_ARG) {
  9573. ret = wc_Poly1305SetKey(&ctx, NULL, (word32)(sizeof(key)/sizeof(byte)));
  9574. }
  9575. if (ret == BAD_FUNC_ARG) {
  9576. ret = wc_Poly1305SetKey(&ctx, key, 18);
  9577. }
  9578. if (ret == BAD_FUNC_ARG) {
  9579. ret = 0;
  9580. } else {
  9581. ret = WOLFSSL_FATAL_ERROR;
  9582. }
  9583. }
  9584. printf(resultFmt, ret == 0 ? passed : failed);
  9585. #endif
  9586. return ret;
  9587. } /* END test_wc_Poly1305_SetKey() */
  9588. /*
  9589. * Testing wc_Chacha_Process()
  9590. */
  9591. static int test_wc_Chacha_Process (void)
  9592. {
  9593. int ret = 0;
  9594. #ifdef HAVE_CHACHA
  9595. ChaCha enc, dec;
  9596. byte cipher[128];
  9597. byte plain[128];
  9598. const byte key[] =
  9599. {
  9600. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  9601. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  9602. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  9603. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  9604. };
  9605. const char* input = "Everybody gets Friday off.";
  9606. word32 keySz = sizeof(key)/sizeof(byte);
  9607. unsigned long int inlen = XSTRLEN(input);
  9608. /*Initialize stack varialbes.*/
  9609. XMEMSET(cipher, 0, 128);
  9610. XMEMSET(plain, 0, 128);
  9611. printf(testingFmt, "wc_Chacha_Process()");
  9612. ret = wc_Chacha_SetKey(&enc, key, keySz);
  9613. AssertIntEQ(ret, 0);
  9614. ret = wc_Chacha_SetKey(&dec, key, keySz);
  9615. AssertIntEQ(ret, 0);
  9616. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  9617. AssertIntEQ(ret, 0);
  9618. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  9619. AssertIntEQ(ret, 0);
  9620. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen);
  9621. AssertIntEQ(ret, 0);
  9622. ret = wc_Chacha_Process(&dec, plain, cipher, (word32)inlen);
  9623. AssertIntEQ(ret, 0);
  9624. ret = XMEMCMP(input, plain, (int)inlen);
  9625. AssertIntEQ(ret, 0);
  9626. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  9627. /* test checking and using leftovers, currently just in C code */
  9628. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  9629. AssertIntEQ(ret, 0);
  9630. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  9631. AssertIntEQ(ret, 0);
  9632. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen - 2);
  9633. AssertIntEQ(ret, 0);
  9634. ret = wc_Chacha_Process(&enc, cipher + (inlen - 2),
  9635. (byte*)input + (inlen - 2), 2);
  9636. AssertIntEQ(ret, 0);
  9637. ret = wc_Chacha_Process(&dec, plain, (byte*)cipher, (word32)inlen - 2);
  9638. AssertIntEQ(ret, 0);
  9639. ret = wc_Chacha_Process(&dec, cipher + (inlen - 2),
  9640. (byte*)input + (inlen - 2), 2);
  9641. AssertIntEQ(ret, 0);
  9642. ret = XMEMCMP(input, plain, (int)inlen);
  9643. AssertIntEQ(ret, 0);
  9644. /* check edge cases with counter increment */
  9645. {
  9646. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  9647. const byte expected[] = {
  9648. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  9649. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  9650. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  9651. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  9652. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  9653. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  9654. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  9655. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  9656. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  9657. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  9658. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  9659. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  9660. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  9661. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  9662. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  9663. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  9664. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  9665. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  9666. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  9667. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  9668. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  9669. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  9670. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  9671. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  9672. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  9673. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  9674. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  9675. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  9676. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  9677. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  9678. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  9679. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  9680. };
  9681. const byte iv2[] = {
  9682. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  9683. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  9684. };
  9685. byte input2[256];
  9686. int i;
  9687. for (i = 0; i < 256; i++)
  9688. input2[i] = i;
  9689. ret = wc_Chacha_SetIV(&enc, iv2, 0);
  9690. AssertIntEQ(ret, 0);
  9691. ret = wc_Chacha_Process(&enc, cipher, input2, 64);
  9692. AssertIntEQ(ret, 0);
  9693. AssertIntEQ(XMEMCMP(expected, cipher, 64), 0);
  9694. ret = wc_Chacha_Process(&enc, cipher, input2 + 64, 128);
  9695. AssertIntEQ(ret, 0);
  9696. AssertIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  9697. /* partial */
  9698. ret = wc_Chacha_Process(&enc, cipher, input2 + 192, 32);
  9699. AssertIntEQ(ret, 0);
  9700. AssertIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  9701. ret = wc_Chacha_Process(&enc, cipher, input2 + 224, 32);
  9702. AssertIntEQ(ret, 0);
  9703. AssertIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  9704. }
  9705. #endif
  9706. /* Test bad args. */
  9707. ret = wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen);
  9708. AssertIntEQ(ret, BAD_FUNC_ARG);
  9709. if (ret == BAD_FUNC_ARG) {
  9710. ret = 0;
  9711. }
  9712. printf(resultFmt, ret == 0 ? passed : failed);
  9713. #endif
  9714. return ret;
  9715. } /* END test_wc_Chacha_Process */
  9716. /*
  9717. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  9718. */
  9719. static int test_wc_ChaCha20Poly1305_aead (void)
  9720. {
  9721. int ret = 0;
  9722. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  9723. const byte key[] = {
  9724. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  9725. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  9726. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  9727. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  9728. };
  9729. const byte plaintext[] = {
  9730. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  9731. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  9732. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  9733. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  9734. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  9735. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  9736. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  9737. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  9738. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  9739. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  9740. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  9741. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  9742. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  9743. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  9744. 0x74, 0x2e
  9745. };
  9746. const byte iv[] = {
  9747. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  9748. 0x44, 0x45, 0x46, 0x47
  9749. };
  9750. const byte aad[] = { /* additional data */
  9751. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  9752. 0xc4, 0xc5, 0xc6, 0xc7
  9753. };
  9754. const byte cipher[] = { /* expected output from operation */
  9755. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  9756. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  9757. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  9758. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  9759. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  9760. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  9761. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  9762. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  9763. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  9764. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  9765. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  9766. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  9767. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  9768. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  9769. 0x61, 0x16
  9770. };
  9771. const byte authTag[] = { /* expected output from operation */
  9772. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  9773. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  9774. };
  9775. byte generatedCiphertext[272];
  9776. byte generatedPlaintext[272];
  9777. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  9778. /* Initialize stack variables. */
  9779. XMEMSET(generatedCiphertext, 0, 272);
  9780. XMEMSET(generatedPlaintext, 0, 272);
  9781. /* Test Encrypt */
  9782. printf(testingFmt, "wc_ChaCha20Poly1305_Encrypt()");
  9783. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), plaintext,
  9784. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  9785. AssertIntEQ(ret, 0);
  9786. ret = XMEMCMP(generatedCiphertext, cipher, sizeof(cipher)/sizeof(byte));
  9787. AssertIntEQ(ret, 0);
  9788. /* Test bad args. */
  9789. ret = wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad), plaintext,
  9790. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  9791. AssertIntEQ(ret, BAD_FUNC_ARG);
  9792. ret = wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  9793. plaintext, sizeof(plaintext),
  9794. generatedCiphertext, generatedAuthTag);
  9795. AssertIntEQ(ret, BAD_FUNC_ARG);
  9796. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  9797. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  9798. AssertIntEQ(ret, BAD_FUNC_ARG);
  9799. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  9800. plaintext, 0, generatedCiphertext, generatedAuthTag);
  9801. AssertIntEQ(ret, BAD_FUNC_ARG);
  9802. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  9803. plaintext, sizeof(plaintext), NULL, generatedAuthTag);
  9804. AssertIntEQ(ret, BAD_FUNC_ARG);
  9805. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  9806. plaintext, sizeof(plaintext), generatedCiphertext, NULL);
  9807. if (ret == BAD_FUNC_ARG) {
  9808. ret = 0;
  9809. }
  9810. printf(resultFmt, ret == 0 ? passed : failed);
  9811. if (ret != 0) {
  9812. return ret;
  9813. }
  9814. printf(testingFmt, "wc_ChaCha20Poly1305_Decrypt()");
  9815. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  9816. sizeof(cipher), authTag, generatedPlaintext);
  9817. AssertIntEQ(ret, 0);
  9818. ret = XMEMCMP(generatedPlaintext, plaintext,
  9819. sizeof(plaintext)/sizeof(byte));
  9820. AssertIntEQ(ret, 0);
  9821. /* Test bad args. */
  9822. ret = wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  9823. sizeof(cipher), authTag, generatedPlaintext);
  9824. AssertIntEQ(ret, BAD_FUNC_ARG);
  9825. ret = wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  9826. cipher, sizeof(cipher), authTag, generatedPlaintext);
  9827. AssertIntEQ(ret, BAD_FUNC_ARG);
  9828. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  9829. sizeof(cipher), authTag, generatedPlaintext);
  9830. AssertIntEQ(ret, BAD_FUNC_ARG);
  9831. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  9832. sizeof(cipher), NULL, generatedPlaintext);
  9833. AssertIntEQ(ret, BAD_FUNC_ARG);
  9834. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  9835. sizeof(cipher), authTag, NULL);
  9836. AssertIntEQ(ret, BAD_FUNC_ARG);
  9837. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  9838. 0, authTag, generatedPlaintext);
  9839. AssertIntEQ(ret, BAD_FUNC_ARG);
  9840. if (ret == BAD_FUNC_ARG) {
  9841. ret = 0;
  9842. }
  9843. printf(resultFmt, ret == 0 ? passed : failed);
  9844. #endif
  9845. return ret;
  9846. } /* END test-wc_ChaCha20Poly1305_EncryptDecrypt */
  9847. /*
  9848. * Testing function for wc_AesSetIV
  9849. */
  9850. static int test_wc_AesSetIV (void)
  9851. {
  9852. int ret = 0;
  9853. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  9854. Aes aes;
  9855. byte key16[] =
  9856. {
  9857. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  9858. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  9859. };
  9860. byte iv1[] = "1234567890abcdef";
  9861. byte iv2[] = "0987654321fedcba";
  9862. printf(testingFmt, "wc_AesSetIV()");
  9863. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  9864. if (ret != 0)
  9865. return ret;
  9866. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  9867. iv1, AES_ENCRYPTION);
  9868. if(ret == 0) {
  9869. ret = wc_AesSetIV(&aes, iv2);
  9870. }
  9871. /* Test bad args. */
  9872. if(ret == 0) {
  9873. ret = wc_AesSetIV(NULL, iv1);
  9874. if(ret == BAD_FUNC_ARG) {
  9875. /* NULL iv should return 0. */
  9876. ret = wc_AesSetIV(&aes, NULL);
  9877. } else {
  9878. ret = WOLFSSL_FATAL_ERROR;
  9879. }
  9880. }
  9881. wc_AesFree(&aes);
  9882. printf(resultFmt, ret == 0 ? passed : failed);
  9883. #endif
  9884. return ret;
  9885. } /* test_wc_AesSetIV */
  9886. /*
  9887. * Testing function for wc_AesSetKey().
  9888. */
  9889. static int test_wc_AesSetKey (void)
  9890. {
  9891. int ret = 0;
  9892. #ifndef NO_AES
  9893. Aes aes;
  9894. byte key16[] =
  9895. {
  9896. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  9897. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  9898. };
  9899. #ifdef WOLFSSL_AES_192
  9900. byte key24[] =
  9901. {
  9902. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  9903. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  9904. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  9905. };
  9906. #endif
  9907. #ifdef WOLFSSL_AES_256
  9908. byte key32[] =
  9909. {
  9910. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  9911. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  9912. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  9913. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  9914. };
  9915. #endif
  9916. byte badKey16[] =
  9917. {
  9918. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  9919. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  9920. };
  9921. byte iv[] = "1234567890abcdef";
  9922. printf(testingFmt, "wc_AesSetKey()");
  9923. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  9924. if (ret != 0)
  9925. return ret;
  9926. #ifdef WOLFSSL_AES_128
  9927. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  9928. iv, AES_ENCRYPTION);
  9929. #endif
  9930. #ifdef WOLFSSL_AES_192
  9931. if (ret == 0) {
  9932. ret = wc_AesSetKey (&aes, key24, (word32) sizeof(key24) / sizeof(byte),
  9933. iv, AES_ENCRYPTION);
  9934. }
  9935. #endif
  9936. #ifdef WOLFSSL_AES_256
  9937. if (ret == 0) {
  9938. ret = wc_AesSetKey (&aes, key32, (word32) sizeof(key32) / sizeof(byte),
  9939. iv, AES_ENCRYPTION);
  9940. }
  9941. #endif
  9942. /* Pass in bad args. */
  9943. if (ret == 0) {
  9944. ret = wc_AesSetKey (NULL, key16, (word32) sizeof(key16) / sizeof(byte),
  9945. iv, AES_ENCRYPTION);
  9946. if (ret == BAD_FUNC_ARG) {
  9947. ret = wc_AesSetKey(&aes, badKey16,
  9948. (word32) sizeof(badKey16) / sizeof(byte),
  9949. iv, AES_ENCRYPTION);
  9950. }
  9951. if (ret == BAD_FUNC_ARG) {
  9952. ret = 0;
  9953. } else {
  9954. ret = WOLFSSL_FATAL_ERROR;
  9955. }
  9956. }
  9957. wc_AesFree(&aes);
  9958. printf(resultFmt, ret == 0 ? passed : failed);
  9959. #endif
  9960. return ret;
  9961. } /* END test_wc_AesSetKey */
  9962. /*
  9963. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  9964. * and wc_AesCbcDecryptWithKey()
  9965. */
  9966. static int test_wc_AesCbcEncryptDecrypt (void)
  9967. {
  9968. int ret = 0;
  9969. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  9970. defined(WOLFSSL_AES_256)
  9971. Aes aes;
  9972. byte key32[] =
  9973. {
  9974. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  9975. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  9976. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  9977. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  9978. };
  9979. byte vector[] = /* Now is the time for all w/o trailing 0 */
  9980. {
  9981. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  9982. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  9983. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  9984. };
  9985. byte iv[] = "1234567890abcdef";
  9986. byte enc[sizeof(vector)];
  9987. byte dec[sizeof(vector)];
  9988. int cbcE = WOLFSSL_FATAL_ERROR;
  9989. int cbcD = WOLFSSL_FATAL_ERROR;
  9990. int cbcDWK = WOLFSSL_FATAL_ERROR;
  9991. byte dec2[sizeof(vector)];
  9992. /* Init stack variables. */
  9993. XMEMSET(enc, 0, sizeof(enc));
  9994. XMEMSET(dec, 0, sizeof(vector));
  9995. XMEMSET(dec2, 0, sizeof(vector));
  9996. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  9997. if (ret != 0)
  9998. return ret;
  9999. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv, AES_ENCRYPTION);
  10000. if (ret == 0) {
  10001. ret = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector));
  10002. if (ret == 0) {
  10003. /* Re init for decrypt and set flag. */
  10004. cbcE = 0;
  10005. wc_AesFree(&aes);
  10006. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2,
  10007. iv, AES_DECRYPTION);
  10008. }
  10009. if (ret == 0) {
  10010. ret = wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE);
  10011. if (ret != 0 || XMEMCMP(vector, dec, AES_BLOCK_SIZE) != 0) {
  10012. ret = WOLFSSL_FATAL_ERROR;
  10013. } else {
  10014. /* Set flag. */
  10015. cbcD = 0;
  10016. }
  10017. }
  10018. }
  10019. /* If encrypt succeeds but cbc decrypt fails, we can still test. */
  10020. if (ret == 0 || cbcE == 0) {
  10021. ret = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  10022. key32, sizeof(key32)/sizeof(byte), iv);
  10023. if (ret == 0 || XMEMCMP(vector, dec2, AES_BLOCK_SIZE) == 0) {
  10024. cbcDWK = 0;
  10025. }
  10026. }
  10027. printf(testingFmt, "wc_AesCbcEncrypt()");
  10028. /* Pass in bad args */
  10029. if (cbcE == 0) {
  10030. cbcE = wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector));
  10031. if (cbcE == BAD_FUNC_ARG) {
  10032. cbcE = wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector));
  10033. }
  10034. if (cbcE == BAD_FUNC_ARG) {
  10035. cbcE = wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector));
  10036. }
  10037. if (cbcE == BAD_FUNC_ARG) {
  10038. cbcE = 0;
  10039. } else {
  10040. cbcE = WOLFSSL_FATAL_ERROR;
  10041. }
  10042. }
  10043. printf(resultFmt, cbcE == 0 ? passed : failed);
  10044. if (cbcE != 0) {
  10045. wc_AesFree(&aes);
  10046. return cbcE;
  10047. }
  10048. printf(testingFmt, "wc_AesCbcDecrypt()");
  10049. if (cbcD == 0) {
  10050. cbcD = wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE);
  10051. if (cbcD == BAD_FUNC_ARG) {
  10052. cbcD = wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE);
  10053. }
  10054. if (cbcD == BAD_FUNC_ARG) {
  10055. cbcD = wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE);
  10056. }
  10057. if (cbcD == BAD_FUNC_ARG) {
  10058. cbcD = wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1);
  10059. }
  10060. if (cbcD == BAD_FUNC_ARG) {
  10061. cbcD = 0;
  10062. } else {
  10063. cbcD = WOLFSSL_FATAL_ERROR;
  10064. }
  10065. }
  10066. printf(resultFmt, cbcD == 0 ? passed : failed);
  10067. if (cbcD != 0) {
  10068. wc_AesFree(&aes);
  10069. return cbcD;
  10070. }
  10071. printf(testingFmt, "wc_AesCbcDecryptWithKey()");
  10072. if (cbcDWK == 0) {
  10073. cbcDWK = wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  10074. key32, sizeof(key32)/sizeof(byte), iv);
  10075. if (cbcDWK == BAD_FUNC_ARG) {
  10076. cbcDWK = wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  10077. key32, sizeof(key32)/sizeof(byte), iv);
  10078. }
  10079. if (cbcDWK == BAD_FUNC_ARG) {
  10080. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  10081. NULL, sizeof(key32)/sizeof(byte), iv);
  10082. }
  10083. if (cbcDWK == BAD_FUNC_ARG) {
  10084. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  10085. key32, sizeof(key32)/sizeof(byte), NULL);
  10086. }
  10087. if (cbcDWK == BAD_FUNC_ARG) {
  10088. cbcDWK = 0;
  10089. } else {
  10090. cbcDWK = WOLFSSL_FATAL_ERROR;
  10091. }
  10092. }
  10093. wc_AesFree(&aes);
  10094. printf(resultFmt, cbcDWK == 0 ? passed : failed);
  10095. if (cbcDWK != 0) {
  10096. return cbcDWK;
  10097. }
  10098. #endif
  10099. return ret;
  10100. } /* END test_wc_AesCbcEncryptDecrypt */
  10101. /*
  10102. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  10103. */
  10104. static int test_wc_AesCtrEncryptDecrypt (void)
  10105. {
  10106. int ret = 0;
  10107. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  10108. Aes aesEnc, aesDec;
  10109. byte key32[] =
  10110. {
  10111. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10112. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  10113. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10114. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  10115. };
  10116. byte vector[] = /* Now is the time for all w/o trailing 0 */
  10117. {
  10118. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  10119. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  10120. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  10121. };
  10122. byte iv[] = "1234567890abcdef";
  10123. byte enc[AES_BLOCK_SIZE * 2];
  10124. byte dec[AES_BLOCK_SIZE * 2];
  10125. /* Init stack variables. */
  10126. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  10127. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  10128. printf(testingFmt, "wc_AesCtrEncrypt()");
  10129. ret = wc_AesInit(&aesEnc, NULL, INVALID_DEVID);
  10130. if (ret != 0)
  10131. return ret;
  10132. ret = wc_AesInit(&aesDec, NULL, INVALID_DEVID);
  10133. if (ret != 0) {
  10134. wc_AesFree(&aesEnc);
  10135. return ret;
  10136. }
  10137. ret = wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2,
  10138. iv, AES_ENCRYPTION);
  10139. if (ret == 0) {
  10140. ret = wc_AesCtrEncrypt(&aesEnc, enc, vector,
  10141. sizeof(vector)/sizeof(byte));
  10142. if (ret == 0) {
  10143. /* Decrypt with wc_AesCtrEncrypt() */
  10144. ret = wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2,
  10145. iv, AES_ENCRYPTION);
  10146. }
  10147. if (ret == 0) {
  10148. ret = wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte));
  10149. if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector))) {
  10150. ret = WOLFSSL_FATAL_ERROR;
  10151. }
  10152. }
  10153. }
  10154. /* Test bad args. */
  10155. if (ret == 0) {
  10156. ret = wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte));
  10157. if (ret == BAD_FUNC_ARG) {
  10158. ret = wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte));
  10159. }
  10160. if (ret == BAD_FUNC_ARG) {
  10161. ret = wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte));
  10162. }
  10163. if (ret == BAD_FUNC_ARG) {
  10164. ret = 0;
  10165. } else {
  10166. ret = WOLFSSL_FATAL_ERROR;
  10167. }
  10168. }
  10169. wc_AesFree(&aesEnc);
  10170. wc_AesFree(&aesDec);
  10171. printf(resultFmt, ret == 0 ? passed : failed);
  10172. #endif
  10173. return ret;
  10174. } /* END test_wc_AesCtrEncryptDecrypt */
  10175. /*
  10176. * test function for wc_AesGcmSetKey()
  10177. */
  10178. static int test_wc_AesGcmSetKey (void)
  10179. {
  10180. int ret = 0;
  10181. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  10182. Aes aes;
  10183. #ifdef WOLFSSL_AES_128
  10184. byte key16[] =
  10185. {
  10186. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10187. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  10188. };
  10189. #endif
  10190. #ifdef WOLFSSL_AES_192
  10191. byte key24[] =
  10192. {
  10193. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10194. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  10195. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  10196. };
  10197. #endif
  10198. #ifdef WOLFSSL_AES_256
  10199. byte key32[] =
  10200. {
  10201. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10202. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  10203. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10204. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  10205. };
  10206. #endif
  10207. byte badKey16[] =
  10208. {
  10209. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10210. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  10211. };
  10212. byte badKey24[] =
  10213. {
  10214. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10215. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  10216. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  10217. };
  10218. byte badKey32[] =
  10219. {
  10220. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  10221. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  10222. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10223. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  10224. };
  10225. printf(testingFmt, "wc_AesGcmSetKey()");
  10226. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  10227. if (ret != 0)
  10228. return ret;
  10229. #ifdef WOLFSSL_AES_128
  10230. ret = wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte));
  10231. #endif
  10232. #ifdef WOLFSSL_AES_192
  10233. if (ret == 0) {
  10234. ret = wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte));
  10235. }
  10236. #endif
  10237. #ifdef WOLFSSL_AES_256
  10238. if (ret == 0) {
  10239. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  10240. }
  10241. #endif
  10242. /* Pass in bad args. */
  10243. if (ret == 0) {
  10244. ret = wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte));
  10245. if (ret == BAD_FUNC_ARG) {
  10246. ret = wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte));
  10247. }
  10248. if (ret == BAD_FUNC_ARG) {
  10249. ret = wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte));
  10250. }
  10251. if (ret == BAD_FUNC_ARG) {
  10252. ret = 0;
  10253. } else {
  10254. ret = WOLFSSL_FATAL_ERROR;
  10255. }
  10256. }
  10257. wc_AesFree(&aes);
  10258. printf(resultFmt, ret == 0 ? passed : failed);
  10259. #endif
  10260. return ret;
  10261. } /* END test_wc_AesGcmSetKey */
  10262. /*
  10263. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  10264. */
  10265. static int test_wc_AesGcmEncryptDecrypt (void)
  10266. {
  10267. int ret = 0;
  10268. /* WOLFSSL_AFALG requires 12 byte IV */
  10269. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  10270. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  10271. Aes aes;
  10272. byte key32[] =
  10273. {
  10274. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10275. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  10276. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10277. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  10278. };
  10279. byte vector[] = /* Now is the time for all w/o trailing 0 */
  10280. {
  10281. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  10282. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  10283. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  10284. };
  10285. const byte a[] =
  10286. {
  10287. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  10288. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  10289. 0xab, 0xad, 0xda, 0xd2
  10290. };
  10291. byte iv[] = "1234567890a";
  10292. byte longIV[] = "1234567890abcdefghij";
  10293. byte enc[sizeof(vector)];
  10294. byte resultT[AES_BLOCK_SIZE];
  10295. byte dec[sizeof(vector)];
  10296. int gcmD = WOLFSSL_FATAL_ERROR;
  10297. int gcmE = WOLFSSL_FATAL_ERROR;
  10298. /* Init stack variables. */
  10299. XMEMSET(enc, 0, sizeof(vector));
  10300. XMEMSET(dec, 0, sizeof(vector));
  10301. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  10302. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  10303. if (ret != 0)
  10304. return ret;
  10305. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  10306. if (ret == 0) {
  10307. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector),
  10308. iv, sizeof(iv)/sizeof(byte), resultT,
  10309. sizeof(resultT), a, sizeof(a));
  10310. }
  10311. if (gcmE == 0) { /* If encrypt fails, no decrypt. */
  10312. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector),
  10313. iv, sizeof(iv)/sizeof(byte), resultT,
  10314. sizeof(resultT), a, sizeof(a));
  10315. if(gcmD == 0 && (XMEMCMP(vector, dec, sizeof(vector)) != 0)) {
  10316. gcmD = WOLFSSL_FATAL_ERROR;
  10317. }
  10318. }
  10319. printf(testingFmt, "wc_AesGcmEncrypt()");
  10320. /*Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  10321. if (gcmE == 0) {
  10322. gcmE = wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector),
  10323. iv, sizeof(iv)/sizeof(byte), resultT, sizeof(resultT),
  10324. a, sizeof(a));
  10325. if (gcmE == BAD_FUNC_ARG) {
  10326. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  10327. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  10328. resultT, sizeof(resultT) + 1, a, sizeof(a));
  10329. }
  10330. if (gcmE == BAD_FUNC_ARG) {
  10331. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  10332. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  10333. resultT, sizeof(resultT) - 5, a, sizeof(a));
  10334. }
  10335. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  10336. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)
  10337. /* FIPS does not check the lower bound of ivSz */
  10338. #else
  10339. if (gcmE == BAD_FUNC_ARG) {
  10340. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  10341. sizeof(vector), iv, 0,
  10342. resultT, sizeof(resultT), a, sizeof(a));
  10343. }
  10344. #endif
  10345. if (gcmE == BAD_FUNC_ARG) {
  10346. gcmE = 0;
  10347. } else {
  10348. gcmE = WOLFSSL_FATAL_ERROR;
  10349. }
  10350. }
  10351. /* This case is now considered good. Long IVs are now allowed.
  10352. * Except for the original FIPS release, it still has an upper
  10353. * bound on the IV length. */
  10354. #if !defined(HAVE_FIPS) || \
  10355. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  10356. if (gcmE == 0) {
  10357. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  10358. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT),
  10359. a, sizeof(a));
  10360. }
  10361. #else
  10362. (void)longIV;
  10363. #endif /* Old FIPS */
  10364. /* END wc_AesGcmEncrypt */
  10365. printf(resultFmt, gcmE == 0 ? passed : failed);
  10366. if (gcmE != 0) {
  10367. wc_AesFree(&aes);
  10368. return gcmE;
  10369. }
  10370. #ifdef HAVE_AES_DECRYPT
  10371. printf(testingFmt, "wc_AesGcmDecrypt()");
  10372. if (gcmD == 0) {
  10373. gcmD = wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte),
  10374. iv, sizeof(iv)/sizeof(byte), resultT,
  10375. sizeof(resultT), a, sizeof(a));
  10376. if (gcmD == BAD_FUNC_ARG) {
  10377. gcmD = wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte),
  10378. iv, sizeof(iv)/sizeof(byte), resultT,
  10379. sizeof(resultT), a, sizeof(a));
  10380. }
  10381. if (gcmD == BAD_FUNC_ARG) {
  10382. gcmD = wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte),
  10383. iv, sizeof(iv)/sizeof(byte), resultT,
  10384. sizeof(resultT), a, sizeof(a));
  10385. }
  10386. if (gcmD == BAD_FUNC_ARG) {
  10387. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  10388. NULL, sizeof(iv)/sizeof(byte), resultT,
  10389. sizeof(resultT), a, sizeof(a));
  10390. }
  10391. if (gcmD == BAD_FUNC_ARG) {
  10392. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  10393. iv, sizeof(iv)/sizeof(byte), NULL,
  10394. sizeof(resultT), a, sizeof(a));
  10395. }
  10396. if (gcmD == BAD_FUNC_ARG) {
  10397. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  10398. iv, sizeof(iv)/sizeof(byte), resultT,
  10399. sizeof(resultT) + 1, a, sizeof(a));
  10400. }
  10401. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  10402. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)
  10403. /* FIPS does not check the lower bound of ivSz */
  10404. #else
  10405. if (gcmD == BAD_FUNC_ARG) {
  10406. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  10407. iv, 0, resultT,
  10408. sizeof(resultT), a, sizeof(a));
  10409. }
  10410. #endif
  10411. if (gcmD == BAD_FUNC_ARG) {
  10412. gcmD = 0;
  10413. } else {
  10414. gcmD = WOLFSSL_FATAL_ERROR;
  10415. }
  10416. } /* END wc_AesGcmDecrypt */
  10417. printf(resultFmt, gcmD == 0 ? passed : failed);
  10418. #endif /* HAVE_AES_DECRYPT */
  10419. wc_AesFree(&aes);
  10420. #endif
  10421. return ret;
  10422. } /* END test_wc_AesGcmEncryptDecrypt */
  10423. /*
  10424. * unit test for wc_GmacSetKey()
  10425. */
  10426. static int test_wc_GmacSetKey (void)
  10427. {
  10428. int ret = 0;
  10429. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  10430. Gmac gmac;
  10431. byte key16[] =
  10432. {
  10433. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10434. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  10435. };
  10436. #ifdef WOLFSSL_AES_192
  10437. byte key24[] =
  10438. {
  10439. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10440. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  10441. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  10442. };
  10443. #endif
  10444. #ifdef WOLFSSL_AES_256
  10445. byte key32[] =
  10446. {
  10447. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10448. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  10449. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10450. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  10451. };
  10452. #endif
  10453. byte badKey16[] =
  10454. {
  10455. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10456. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  10457. };
  10458. byte badKey24[] =
  10459. {
  10460. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  10461. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  10462. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  10463. };
  10464. byte badKey32[] =
  10465. {
  10466. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10467. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  10468. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10469. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  10470. };
  10471. printf(testingFmt, "wc_GmacSetKey()");
  10472. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  10473. if (ret != 0)
  10474. return ret;
  10475. #ifdef WOLFSSL_AES_128
  10476. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte));
  10477. #endif
  10478. #ifdef WOLFSSL_AES_192
  10479. if (ret == 0) {
  10480. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  10481. }
  10482. #endif
  10483. #ifdef WOLFSSL_AES_256
  10484. if (ret == 0) {
  10485. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  10486. }
  10487. #endif
  10488. /* Pass in bad args. */
  10489. if (ret == 0) {
  10490. ret = wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte));
  10491. if (ret == BAD_FUNC_ARG) {
  10492. ret = wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte));
  10493. }
  10494. if (ret == BAD_FUNC_ARG) {
  10495. ret = wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte));
  10496. }
  10497. if (ret == BAD_FUNC_ARG) {
  10498. ret = wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte));
  10499. }
  10500. if (ret == BAD_FUNC_ARG) {
  10501. ret = wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte));
  10502. }
  10503. if (ret == BAD_FUNC_ARG) {
  10504. ret = 0;
  10505. } else {
  10506. ret = WOLFSSL_FATAL_ERROR;
  10507. }
  10508. }
  10509. wc_AesFree(&gmac.aes);
  10510. printf(resultFmt, ret == 0 ? passed : failed);
  10511. #endif
  10512. return ret;
  10513. } /* END test_wc_GmacSetKey */
  10514. /*
  10515. * unit test for wc_GmacUpdate
  10516. */
  10517. static int test_wc_GmacUpdate (void)
  10518. {
  10519. int ret = 0;
  10520. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  10521. Gmac gmac;
  10522. #ifdef WOLFSSL_AES_128
  10523. const byte key16[] =
  10524. {
  10525. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  10526. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  10527. };
  10528. #endif
  10529. #ifdef WOLFSSL_AES_192
  10530. byte key24[] =
  10531. {
  10532. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  10533. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  10534. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  10535. };
  10536. #endif
  10537. #ifdef WOLFSSL_AES_256
  10538. byte key32[] =
  10539. {
  10540. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  10541. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  10542. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  10543. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  10544. };
  10545. #endif
  10546. #ifdef WOLFSSL_AES_128
  10547. const byte authIn[] =
  10548. {
  10549. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  10550. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  10551. };
  10552. #endif
  10553. #ifdef WOLFSSL_AES_192
  10554. const byte authIn2[] =
  10555. {
  10556. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  10557. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  10558. };
  10559. #endif
  10560. const byte authIn3[] =
  10561. {
  10562. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  10563. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  10564. };
  10565. #ifdef WOLFSSL_AES_128
  10566. const byte tag1[] = /* Known. */
  10567. {
  10568. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  10569. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  10570. };
  10571. #endif
  10572. #ifdef WOLFSSL_AES_192
  10573. const byte tag2[] = /* Known */
  10574. {
  10575. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  10576. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  10577. };
  10578. #endif
  10579. const byte tag3[] = /* Known */
  10580. {
  10581. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  10582. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  10583. };
  10584. #ifdef WOLFSSL_AES_128
  10585. const byte iv[] =
  10586. {
  10587. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  10588. 0xe2, 0x8c, 0x8f, 0x16
  10589. };
  10590. #endif
  10591. #ifdef WOLFSSL_AES_192
  10592. const byte iv2[] =
  10593. {
  10594. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  10595. 0x7e, 0x1a, 0x6f, 0xbc
  10596. };
  10597. #endif
  10598. const byte iv3[] =
  10599. {
  10600. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  10601. 0xc3, 0xfb, 0x6c, 0x8a
  10602. };
  10603. byte tagOut[16];
  10604. byte tagOut2[24];
  10605. byte tagOut3[32];
  10606. /* Init stack variables. */
  10607. XMEMSET(tagOut, 0, sizeof(tagOut));
  10608. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  10609. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  10610. printf(testingFmt, "wc_GmacUpdate()");
  10611. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  10612. if (ret != 0)
  10613. return ret;
  10614. #ifdef WOLFSSL_AES_128
  10615. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16));
  10616. if (ret == 0) {
  10617. ret = wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  10618. tagOut, sizeof(tag1));
  10619. if (ret == 0) {
  10620. ret = XMEMCMP(tag1, tagOut, sizeof(tag1));
  10621. }
  10622. }
  10623. #endif
  10624. #ifdef WOLFSSL_AES_192
  10625. if (ret == 0) {
  10626. XMEMSET(&gmac, 0, sizeof(Gmac));
  10627. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  10628. }
  10629. if (ret == 0) {
  10630. ret = wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2,
  10631. sizeof(authIn2), tagOut2, sizeof(tag2));
  10632. }
  10633. if (ret == 0) {
  10634. ret = XMEMCMP(tagOut2, tag2, sizeof(tag2));
  10635. }
  10636. #endif
  10637. #ifdef WOLFSSL_AES_256
  10638. if (ret == 0) {
  10639. XMEMSET(&gmac, 0, sizeof(Gmac));
  10640. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  10641. }
  10642. if (ret == 0) {
  10643. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  10644. sizeof(authIn3), tagOut3, sizeof(tag3));
  10645. }
  10646. if (ret == 0) {
  10647. ret = XMEMCMP(tag3, tagOut3, sizeof(tag3));
  10648. }
  10649. #endif
  10650. /*Pass bad args. */
  10651. if (ret == 0) {
  10652. ret = wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3,
  10653. sizeof(authIn3), tagOut3, sizeof(tag3));
  10654. if (ret == BAD_FUNC_ARG) {
  10655. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  10656. sizeof(authIn3), tagOut3, sizeof(tag3) - 5);
  10657. }
  10658. if (ret == BAD_FUNC_ARG) {
  10659. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  10660. sizeof(authIn3), tagOut3, sizeof(tag3) + 1);
  10661. }
  10662. if (ret == BAD_FUNC_ARG) {
  10663. ret = 0;
  10664. } else {
  10665. ret = WOLFSSL_FATAL_ERROR;
  10666. }
  10667. }
  10668. wc_AesFree(&gmac.aes);
  10669. printf(resultFmt, ret == 0 ? passed : failed);
  10670. #endif
  10671. return ret;
  10672. } /* END test_wc_GmacUpdate */
  10673. /*
  10674. * testing wc_CamelliaSetKey
  10675. */
  10676. static int test_wc_CamelliaSetKey (void)
  10677. {
  10678. int ret = 0;
  10679. #ifdef HAVE_CAMELLIA
  10680. Camellia camellia;
  10681. /*128-bit key*/
  10682. static const byte key16[] =
  10683. {
  10684. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10685. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  10686. };
  10687. /* 192-bit key */
  10688. static const byte key24[] =
  10689. {
  10690. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10691. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  10692. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  10693. };
  10694. /* 256-bit key */
  10695. static const byte key32[] =
  10696. {
  10697. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10698. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  10699. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10700. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  10701. };
  10702. static const byte iv[] =
  10703. {
  10704. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10705. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  10706. };
  10707. printf(testingFmt, "wc_CamelliaSetKey()");
  10708. ret = wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv);
  10709. if (ret == 0) {
  10710. ret = wc_CamelliaSetKey(&camellia, key16,
  10711. (word32)sizeof(key16), NULL);
  10712. if (ret == 0) {
  10713. ret = wc_CamelliaSetKey(&camellia, key24,
  10714. (word32)sizeof(key24), iv);
  10715. }
  10716. if (ret == 0) {
  10717. ret = wc_CamelliaSetKey(&camellia, key24,
  10718. (word32)sizeof(key24), NULL);
  10719. }
  10720. if (ret == 0) {
  10721. ret = wc_CamelliaSetKey(&camellia, key32,
  10722. (word32)sizeof(key32), iv);
  10723. }
  10724. if (ret == 0) {
  10725. ret = wc_CamelliaSetKey(&camellia, key32,
  10726. (word32)sizeof(key32), NULL);
  10727. }
  10728. }
  10729. /* Bad args. */
  10730. if (ret == 0) {
  10731. ret = wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv);
  10732. if (ret != BAD_FUNC_ARG) {
  10733. ret = WOLFSSL_FATAL_ERROR;
  10734. } else {
  10735. ret = 0;
  10736. }
  10737. } /* END bad args. */
  10738. #endif
  10739. return ret;
  10740. } /* END test_wc_CammeliaSetKey */
  10741. /*
  10742. * Testing wc_CamelliaSetIV()
  10743. */
  10744. static int test_wc_CamelliaSetIV (void)
  10745. {
  10746. int ret = 0;
  10747. #ifdef HAVE_CAMELLIA
  10748. Camellia camellia;
  10749. static const byte iv[] =
  10750. {
  10751. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10752. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  10753. };
  10754. printf(testingFmt, "wc_CamelliaSetIV()");
  10755. ret = wc_CamelliaSetIV(&camellia, iv);
  10756. if (ret == 0) {
  10757. ret = wc_CamelliaSetIV(&camellia, NULL);
  10758. }
  10759. /* Bad args. */
  10760. if (ret == 0) {
  10761. ret = wc_CamelliaSetIV(NULL, NULL);
  10762. if (ret != BAD_FUNC_ARG) {
  10763. ret = WOLFSSL_FATAL_ERROR;
  10764. } else {
  10765. ret = 0;
  10766. }
  10767. }
  10768. printf(resultFmt, ret == 0 ? passed : failed);
  10769. #endif
  10770. return ret;
  10771. } /*END test_wc_CamelliaSetIV*/
  10772. /*
  10773. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  10774. */
  10775. static int test_wc_CamelliaEncryptDecryptDirect (void)
  10776. {
  10777. int ret = 0;
  10778. #ifdef HAVE_CAMELLIA
  10779. Camellia camellia;
  10780. static const byte key24[] =
  10781. {
  10782. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10783. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  10784. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  10785. };
  10786. static const byte iv[] =
  10787. {
  10788. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10789. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  10790. };
  10791. static const byte plainT[] =
  10792. {
  10793. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  10794. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  10795. };
  10796. byte enc[sizeof(plainT)];
  10797. byte dec[sizeof(enc)];
  10798. int camE = WOLFSSL_FATAL_ERROR;
  10799. int camD = WOLFSSL_FATAL_ERROR;
  10800. /*Init stack variables.*/
  10801. XMEMSET(enc, 0, 16);
  10802. XMEMSET(enc, 0, 16);
  10803. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv);
  10804. if (ret == 0) {
  10805. ret = wc_CamelliaEncryptDirect(&camellia, enc, plainT);
  10806. if (ret == 0) {
  10807. ret = wc_CamelliaDecryptDirect(&camellia, dec, enc);
  10808. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  10809. ret = WOLFSSL_FATAL_ERROR;
  10810. }
  10811. }
  10812. }
  10813. printf(testingFmt, "wc_CamelliaEncryptDirect()");
  10814. /* Pass bad args. */
  10815. if (ret == 0) {
  10816. camE = wc_CamelliaEncryptDirect(NULL, enc, plainT);
  10817. if (camE == BAD_FUNC_ARG) {
  10818. camE = wc_CamelliaEncryptDirect(&camellia, NULL, plainT);
  10819. }
  10820. if (camE == BAD_FUNC_ARG) {
  10821. camE = wc_CamelliaEncryptDirect(&camellia, enc, NULL);
  10822. }
  10823. if (camE == BAD_FUNC_ARG) {
  10824. camE = 0;
  10825. } else {
  10826. camE = WOLFSSL_FATAL_ERROR;
  10827. }
  10828. }
  10829. printf(resultFmt, camE == 0 ? passed : failed);
  10830. if (camE != 0) {
  10831. return camE;
  10832. }
  10833. printf(testingFmt, "wc_CamelliaDecryptDirect()");
  10834. if (ret == 0) {
  10835. camD = wc_CamelliaDecryptDirect(NULL, dec, enc);
  10836. if (camD == BAD_FUNC_ARG) {
  10837. camD = wc_CamelliaDecryptDirect(&camellia, NULL, enc);
  10838. }
  10839. if (camD == BAD_FUNC_ARG) {
  10840. camD = wc_CamelliaDecryptDirect(&camellia, dec, NULL);
  10841. }
  10842. if (camD == BAD_FUNC_ARG) {
  10843. camD = 0;
  10844. } else {
  10845. camD = WOLFSSL_FATAL_ERROR;
  10846. }
  10847. }
  10848. printf(resultFmt, camD == 0 ? passed : failed);
  10849. if (camD != 0) {
  10850. return camD;
  10851. }
  10852. #endif
  10853. return ret;
  10854. } /* END test-wc_CamelliaEncryptDecryptDirect */
  10855. /*
  10856. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  10857. */
  10858. static int test_wc_CamelliaCbcEncryptDecrypt (void)
  10859. {
  10860. int ret = 0;
  10861. #ifdef HAVE_CAMELLIA
  10862. Camellia camellia;
  10863. static const byte key24[] =
  10864. {
  10865. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10866. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  10867. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  10868. };
  10869. static const byte plainT[] =
  10870. {
  10871. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  10872. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  10873. };
  10874. byte enc[CAMELLIA_BLOCK_SIZE];
  10875. byte dec[CAMELLIA_BLOCK_SIZE];
  10876. int camCbcE = WOLFSSL_FATAL_ERROR;
  10877. int camCbcD = WOLFSSL_FATAL_ERROR;
  10878. /* Init stack variables. */
  10879. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  10880. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  10881. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  10882. if (ret == 0) {
  10883. ret = wc_CamelliaCbcEncrypt(&camellia, enc, plainT, CAMELLIA_BLOCK_SIZE);
  10884. if (ret != 0) {
  10885. ret = WOLFSSL_FATAL_ERROR;
  10886. }
  10887. }
  10888. if (ret == 0) {
  10889. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  10890. if (ret == 0) {
  10891. ret = wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE);
  10892. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  10893. ret = WOLFSSL_FATAL_ERROR;
  10894. }
  10895. }
  10896. }
  10897. printf(testingFmt, "wc_CamelliaCbcEncrypt");
  10898. /* Pass in bad args. */
  10899. if (ret == 0) {
  10900. camCbcE = wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE);
  10901. if (camCbcE == BAD_FUNC_ARG) {
  10902. camCbcE = wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  10903. CAMELLIA_BLOCK_SIZE);
  10904. }
  10905. if (camCbcE == BAD_FUNC_ARG) {
  10906. camCbcE = wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  10907. CAMELLIA_BLOCK_SIZE);
  10908. }
  10909. if (camCbcE == BAD_FUNC_ARG) {
  10910. camCbcE = 0;
  10911. } else {
  10912. camCbcE = WOLFSSL_FATAL_ERROR;
  10913. }
  10914. }
  10915. printf(resultFmt, camCbcE == 0 ? passed : failed);
  10916. if (camCbcE != 0) {
  10917. return camCbcE;
  10918. }
  10919. printf(testingFmt, "wc_CamelliaCbcDecrypt()");
  10920. if (ret == 0) {
  10921. camCbcD = wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE);
  10922. if (camCbcD == BAD_FUNC_ARG) {
  10923. camCbcD = wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  10924. CAMELLIA_BLOCK_SIZE);
  10925. }
  10926. if (camCbcD == BAD_FUNC_ARG) {
  10927. camCbcD = wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  10928. CAMELLIA_BLOCK_SIZE);
  10929. }
  10930. if (camCbcD == BAD_FUNC_ARG) {
  10931. camCbcD = 0;
  10932. } else {
  10933. camCbcD = WOLFSSL_FATAL_ERROR;
  10934. }
  10935. } /* END bad args. */
  10936. printf(resultFmt, camCbcD == 0 ? passed : failed);
  10937. if (camCbcD != 0) {
  10938. return camCbcD;
  10939. }
  10940. #endif
  10941. return ret;
  10942. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  10943. /*
  10944. * Testing wc_RabbitSetKey()
  10945. */
  10946. static int test_wc_RabbitSetKey (void)
  10947. {
  10948. int ret = 0;
  10949. #ifndef NO_RABBIT
  10950. Rabbit rabbit;
  10951. const char* key = "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B"
  10952. "\xFE\x36\x3D\x2E\x29\x13\x28\x91";
  10953. const char* iv = "\x59\x7E\x26\xC1\x75\xF5\x73\xC3";
  10954. printf(testingFmt, "wc_RabbitSetKey()");
  10955. ret = wc_RabbitSetKey(&rabbit, (byte*)key, (byte*)iv);
  10956. /* Test bad args. */
  10957. if (ret == 0) {
  10958. ret = wc_RabbitSetKey(NULL, (byte*)key, (byte*)iv);
  10959. if (ret == BAD_FUNC_ARG) {
  10960. ret = wc_RabbitSetKey(&rabbit, NULL, (byte*)iv);
  10961. }
  10962. if (ret == BAD_FUNC_ARG) {
  10963. ret = wc_RabbitSetKey(&rabbit, (byte*)key, NULL);
  10964. }
  10965. }
  10966. printf(resultFmt, ret == 0 ? passed : failed);
  10967. #endif
  10968. return ret;
  10969. } /* END test_wc_RabbitSetKey */
  10970. /*
  10971. * Test wc_RabbitProcess()
  10972. */
  10973. static int test_wc_RabbitProcess (void)
  10974. {
  10975. int ret = 0;
  10976. #ifndef NO_RABBIT
  10977. Rabbit enc, dec;
  10978. byte cipher[25];
  10979. byte plain[25];
  10980. const char* key = "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B"
  10981. "\xFE\x36\x3D\x2E\x29\x13\x28\x91";
  10982. const char* iv = "\x59\x7E\x26\xC1\x75\xF5\x73\xC3";
  10983. const char* input = "Everyone gets Friday off.";
  10984. unsigned long int inlen = XSTRLEN(input);
  10985. /* Initialize stack variables. */
  10986. XMEMSET(cipher, 0, sizeof(cipher));
  10987. XMEMSET(plain, 0, sizeof(plain));
  10988. printf(testingFmt, "wc_RabbitProcess()");
  10989. ret = wc_RabbitSetKey(&enc, (byte*)key, (byte*)iv);
  10990. if (ret == 0) {
  10991. ret = wc_RabbitSetKey(&dec, (byte*)key, (byte*)iv);
  10992. }
  10993. if (ret == 0) {
  10994. ret = wc_RabbitProcess(&enc, cipher, (byte*)input, (word32)inlen);
  10995. }
  10996. if (ret == 0) {
  10997. ret = wc_RabbitProcess(&dec, plain, cipher, (word32)inlen);
  10998. if (ret != 0 || XMEMCMP(input, plain, inlen)) {
  10999. ret = WOLFSSL_FATAL_ERROR;
  11000. } else {
  11001. ret = 0;
  11002. }
  11003. }
  11004. /* Test bad args. */
  11005. if (ret == 0) {
  11006. ret = wc_RabbitProcess(NULL, plain, cipher, (word32)inlen);
  11007. if (ret == BAD_FUNC_ARG) {
  11008. ret = wc_RabbitProcess(&dec, NULL, cipher, (word32)inlen);
  11009. }
  11010. if (ret == BAD_FUNC_ARG) {
  11011. ret = wc_RabbitProcess(&dec, plain, NULL, (word32)inlen);
  11012. }
  11013. if (ret == BAD_FUNC_ARG) {
  11014. ret = 0;
  11015. } else {
  11016. ret = WOLFSSL_FATAL_ERROR;
  11017. }
  11018. }
  11019. printf(resultFmt, ret == 0 ? passed : failed);
  11020. #endif
  11021. return ret;
  11022. } /* END test_wc_RabbitProcess */
  11023. /*
  11024. * Testing wc_Arc4SetKey()
  11025. */
  11026. static int test_wc_Arc4SetKey (void)
  11027. {
  11028. int ret = 0;
  11029. #ifndef NO_RC4
  11030. Arc4 arc;
  11031. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  11032. int keyLen = 8;
  11033. printf(testingFmt, "wc_Arch4SetKey()");
  11034. ret = wc_Arc4SetKey(&arc, (byte*)key, keyLen);
  11035. /* Test bad args. */
  11036. if (ret == 0) {
  11037. ret = wc_Arc4SetKey(NULL, (byte*)key, keyLen);
  11038. if (ret == BAD_FUNC_ARG)
  11039. ret = wc_Arc4SetKey(&arc, NULL, keyLen); /* NULL key */
  11040. if (ret == BAD_FUNC_ARG)
  11041. ret = wc_Arc4SetKey(&arc, (byte*)key, 0); /* length == 0 */
  11042. if (ret == BAD_FUNC_ARG)
  11043. ret = WOLFSSL_ERROR_NONE;
  11044. else
  11045. ret = WOLFSSL_FATAL_ERROR;
  11046. } /* END test bad args. */
  11047. printf(resultFmt, ret == 0 ? passed : failed);
  11048. #endif
  11049. return ret;
  11050. } /* END test_wc_Arc4SetKey */
  11051. /*
  11052. * Testing wc_Arc4Process for ENC/DEC.
  11053. */
  11054. static int test_wc_Arc4Process (void)
  11055. {
  11056. int ret = 0;
  11057. #ifndef NO_RC4
  11058. Arc4 enc, dec;
  11059. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  11060. int keyLen = 8;
  11061. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  11062. byte cipher[8];
  11063. byte plain[8];
  11064. /* Init stack variables */
  11065. XMEMSET(cipher, 0, sizeof(cipher));
  11066. XMEMSET(plain, 0, sizeof(plain));
  11067. /* Use for async. */
  11068. ret = wc_Arc4Init(&enc, NULL, INVALID_DEVID);
  11069. if (ret == 0) {
  11070. ret = wc_Arc4Init(&dec, NULL, INVALID_DEVID);
  11071. }
  11072. printf(testingFmt, "wc_Arc4Process()");
  11073. if (ret == 0) {
  11074. ret = wc_Arc4SetKey(&enc, (byte*)key, keyLen);
  11075. }
  11076. if (ret == 0) {
  11077. ret = wc_Arc4SetKey(&dec, (byte*)key, keyLen);
  11078. }
  11079. if (ret == 0) {
  11080. ret = wc_Arc4Process(&enc, cipher, (byte*)input, keyLen);
  11081. }
  11082. if (ret == 0) {
  11083. ret = wc_Arc4Process(&dec, plain, cipher, keyLen);
  11084. if (ret != 0 || XMEMCMP(plain, input, keyLen)) {
  11085. ret = WOLFSSL_FATAL_ERROR;
  11086. } else {
  11087. ret = 0;
  11088. }
  11089. }
  11090. /* Bad args. */
  11091. if (ret == 0) {
  11092. ret = wc_Arc4Process(NULL, plain, cipher, keyLen);
  11093. if (ret == BAD_FUNC_ARG) {
  11094. ret = wc_Arc4Process(&dec, NULL, cipher, keyLen);
  11095. }
  11096. if (ret == BAD_FUNC_ARG) {
  11097. ret = wc_Arc4Process(&dec, plain, NULL, keyLen);
  11098. }
  11099. if (ret == BAD_FUNC_ARG) {
  11100. ret = 0;
  11101. } else {
  11102. ret = WOLFSSL_FATAL_ERROR;
  11103. }
  11104. }
  11105. printf(resultFmt, ret == 0 ? passed : failed);
  11106. wc_Arc4Free(&enc);
  11107. wc_Arc4Free(&dec);
  11108. #endif
  11109. return ret;
  11110. }/* END test_wc_Arc4Process */
  11111. /*
  11112. * Testing wc_Init RsaKey()
  11113. */
  11114. static int test_wc_InitRsaKey (void)
  11115. {
  11116. int ret = 0;
  11117. #ifndef NO_RSA
  11118. RsaKey key;
  11119. printf(testingFmt, "wc_InitRsaKey()");
  11120. ret = wc_InitRsaKey(&key, NULL);
  11121. /* Test bad args. */
  11122. if (ret == 0) {
  11123. ret = wc_InitRsaKey(NULL, NULL);
  11124. #ifndef HAVE_USER_RSA
  11125. if (ret == BAD_FUNC_ARG) {
  11126. ret = 0;
  11127. } else {
  11128. #else
  11129. if (ret == USER_CRYPTO_ERROR) {
  11130. ret = 0;
  11131. } else {
  11132. #endif
  11133. ret = WOLFSSL_FATAL_ERROR;
  11134. }
  11135. } /* end if */
  11136. if (wc_FreeRsaKey(&key) || ret != 0) {
  11137. ret = WOLFSSL_FATAL_ERROR;
  11138. }
  11139. printf(resultFmt, ret == 0 ? passed : failed);
  11140. #endif
  11141. return ret;
  11142. } /* END test_wc_InitRsaKey */
  11143. /*
  11144. * Testing wc_RsaPrivateKeyDecode()
  11145. */
  11146. static int test_wc_RsaPrivateKeyDecode (void)
  11147. {
  11148. int ret = 0;
  11149. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  11150. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  11151. RsaKey key;
  11152. byte* tmp;
  11153. word32 idx = 0;
  11154. int bytes = 0;
  11155. printf(testingFmt, "wc_RsaPrivateKeyDecode()");
  11156. tmp = (byte*)XMALLOC(FOURK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11157. if (tmp == NULL) {
  11158. ret = WOLFSSL_FATAL_ERROR;
  11159. }
  11160. if (ret == 0) {
  11161. ret = wc_InitRsaKey(&key, NULL);
  11162. }
  11163. if (ret == 0) {
  11164. #ifdef USE_CERT_BUFFERS_1024
  11165. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  11166. bytes = sizeof_client_key_der_1024;
  11167. #else
  11168. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  11169. bytes = sizeof_client_key_der_2048;
  11170. #endif /* Use cert buffers. */
  11171. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
  11172. }
  11173. #ifndef HAVE_USER_RSA
  11174. /* Test bad args. */
  11175. if (ret == 0) {
  11176. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  11177. if (ret == BAD_FUNC_ARG) {
  11178. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  11179. }
  11180. if (ret == BAD_FUNC_ARG) {
  11181. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  11182. }
  11183. if (ret == BAD_FUNC_ARG) {
  11184. ret = 0;
  11185. } else {
  11186. ret = WOLFSSL_FATAL_ERROR;
  11187. }
  11188. }
  11189. #else
  11190. /* Test bad args. User RSA. */
  11191. if (ret == 0) {
  11192. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  11193. if (ret == USER_CRYPTO_ERROR) {
  11194. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  11195. }
  11196. if (ret == USER_CRYPTO_ERROR) {
  11197. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  11198. }
  11199. if (ret == USER_CRYPTO_ERROR) {
  11200. ret = 0;
  11201. } else {
  11202. ret = WOLFSSL_FATAL_ERROR;
  11203. }
  11204. }
  11205. #endif
  11206. if (tmp != NULL) {
  11207. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11208. }
  11209. if (wc_FreeRsaKey(&key) || ret != 0) {
  11210. ret = WOLFSSL_FATAL_ERROR;
  11211. }
  11212. printf(resultFmt, ret == 0 ? passed : failed);
  11213. #endif
  11214. return ret;
  11215. } /* END test_wc_RsaPrivateKeyDecode */
  11216. /*
  11217. * Testing wc_RsaPublicKeyDecode()
  11218. */
  11219. static int test_wc_RsaPublicKeyDecode (void)
  11220. {
  11221. int ret = 0;
  11222. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  11223. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  11224. RsaKey keyPub;
  11225. byte* tmp;
  11226. word32 idx = 0;
  11227. int bytes = 0;
  11228. word32 keySz = 0;
  11229. word32 tstKeySz = 0;
  11230. tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11231. if (tmp == NULL) {
  11232. ret = WOLFSSL_FATAL_ERROR;
  11233. }
  11234. if (ret == 0) {
  11235. ret = wc_InitRsaKey(&keyPub, NULL);
  11236. }
  11237. if (ret == 0) {
  11238. #ifdef USE_CERT_BUFFERS_1024
  11239. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  11240. bytes = sizeof_client_keypub_der_1024;
  11241. keySz = 1024;
  11242. #else
  11243. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  11244. bytes = sizeof_client_keypub_der_2048;
  11245. keySz = 2048;
  11246. #endif
  11247. printf(testingFmt, "wc_RsaPublicKeyDecode()");
  11248. ret = wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes);
  11249. }
  11250. #ifndef HAVE_USER_RSA
  11251. /* Pass in bad args. */
  11252. if (ret == 0) {
  11253. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  11254. if (ret == BAD_FUNC_ARG) {
  11255. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  11256. }
  11257. if (ret == BAD_FUNC_ARG) {
  11258. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  11259. }
  11260. if (ret == BAD_FUNC_ARG) {
  11261. ret = 0;
  11262. } else {
  11263. ret = WOLFSSL_FATAL_ERROR;
  11264. }
  11265. }
  11266. #else
  11267. /* Pass in bad args. */
  11268. if (ret == 0) {
  11269. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  11270. if (ret == USER_CRYPTO_ERROR) {
  11271. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  11272. }
  11273. if (ret == USER_CRYPTO_ERROR) {
  11274. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  11275. }
  11276. if (ret == USER_CRYPTO_ERROR) {
  11277. ret = 0;
  11278. } else {
  11279. ret = WOLFSSL_FATAL_ERROR;
  11280. }
  11281. }
  11282. #endif
  11283. if (wc_FreeRsaKey(&keyPub) || ret != 0) {
  11284. ret = WOLFSSL_FATAL_ERROR;
  11285. }
  11286. if (ret == 0) {
  11287. /* Test for getting modulus key size */
  11288. idx = 0;
  11289. ret = wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  11290. &tstKeySz, NULL, NULL);
  11291. ret = (ret == 0 && tstKeySz == keySz/8) ? 0 : WOLFSSL_FATAL_ERROR;
  11292. }
  11293. if (tmp != NULL) {
  11294. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11295. }
  11296. printf(resultFmt, ret == 0 ? passed : failed);
  11297. #endif
  11298. return ret;
  11299. } /* END test_wc_RsaPublicKeyDecode */
  11300. /*
  11301. * Testing wc_RsaPublicKeyDecodeRaw()
  11302. */
  11303. static int test_wc_RsaPublicKeyDecodeRaw (void)
  11304. {
  11305. int ret = 0;
  11306. #if !defined(NO_RSA)
  11307. RsaKey key;
  11308. const byte n = 0x23;
  11309. const byte e = 0x03;
  11310. int nSz = sizeof(n);
  11311. int eSz = sizeof(e);
  11312. printf(testingFmt, "wc_RsaPublicKeyDecodeRaw()");
  11313. ret = wc_InitRsaKey(&key, NULL);
  11314. if (ret == 0) {
  11315. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key);
  11316. }
  11317. #ifndef HAVE_USER_RSA
  11318. /* Pass in bad args. */
  11319. if (ret == 0) {
  11320. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  11321. if (ret == BAD_FUNC_ARG) {
  11322. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  11323. }
  11324. if (ret == BAD_FUNC_ARG) {
  11325. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  11326. }
  11327. if (ret == BAD_FUNC_ARG) {
  11328. ret = 0;
  11329. } else {
  11330. ret = WOLFSSL_FATAL_ERROR;
  11331. }
  11332. }
  11333. #else
  11334. /* Pass in bad args. User RSA. */
  11335. if (ret == 0) {
  11336. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  11337. if (ret == USER_CRYPTO_ERROR) {
  11338. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  11339. }
  11340. if (ret == USER_CRYPTO_ERROR) {
  11341. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  11342. }
  11343. if (ret == USER_CRYPTO_ERROR) {
  11344. ret = 0;
  11345. } else {
  11346. ret = WOLFSSL_FATAL_ERROR;
  11347. }
  11348. }
  11349. #endif
  11350. if (wc_FreeRsaKey(&key) || ret != 0) {
  11351. ret = WOLFSSL_FATAL_ERROR;
  11352. }
  11353. printf(resultFmt, ret == 0 ? passed : failed);
  11354. #endif
  11355. return ret;
  11356. } /* END test_wc_RsaPublicKeyDecodeRaw */
  11357. #if (!defined(NO_RSA) || !defined(HAVE_FAST_RSA)) && defined(WOLFSSL_KEY_GEN)
  11358. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  11359. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  11360. * trying until it gets a probable prime. */
  11361. #ifdef HAVE_FIPS
  11362. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  11363. {
  11364. int ret;
  11365. for (;;) {
  11366. ret = wc_MakeRsaKey(key, size, e, rng);
  11367. if (ret != PRIME_GEN_E) break;
  11368. printf("MakeRsaKey couldn't find prime; trying again.\n");
  11369. }
  11370. return ret;
  11371. }
  11372. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  11373. #else
  11374. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  11375. #endif
  11376. #endif
  11377. /*
  11378. * Testing wc_MakeRsaKey()
  11379. */
  11380. static int test_wc_MakeRsaKey (void)
  11381. {
  11382. int ret = 0;
  11383. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  11384. RsaKey genKey;
  11385. WC_RNG rng;
  11386. #ifndef WOLFSSL_SP_MATH
  11387. int bits = 1024;
  11388. #else
  11389. int bits = 2048;
  11390. #endif
  11391. printf(testingFmt, "wc_MakeRsaKey()");
  11392. ret = wc_InitRsaKey(&genKey, NULL);
  11393. if (ret == 0) {
  11394. ret = wc_InitRng(&rng);
  11395. if (ret == 0) {
  11396. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
  11397. if (ret == 0 && wc_FreeRsaKey(&genKey) != 0) {
  11398. ret = WOLFSSL_FATAL_ERROR;
  11399. }
  11400. }
  11401. }
  11402. #ifndef HAVE_USER_RSA
  11403. /* Test bad args. */
  11404. if (ret == 0) {
  11405. ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
  11406. if (ret == BAD_FUNC_ARG) {
  11407. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
  11408. }
  11409. if (ret == BAD_FUNC_ARG) {
  11410. /* e < 3 */
  11411. ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
  11412. }
  11413. if (ret == BAD_FUNC_ARG) {
  11414. /* e & 1 == 0 */
  11415. ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
  11416. }
  11417. if (ret == BAD_FUNC_ARG) {
  11418. ret = 0;
  11419. } else {
  11420. ret = WOLFSSL_FATAL_ERROR;
  11421. }
  11422. }
  11423. #else
  11424. /* Test bad args. */
  11425. if (ret == 0) {
  11426. ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
  11427. if (ret == USER_CRYPTO_ERROR) {
  11428. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
  11429. }
  11430. if (ret == USER_CRYPTO_ERROR) {
  11431. /* e < 3 */
  11432. ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
  11433. }
  11434. if (ret == USER_CRYPTO_ERROR) {
  11435. /* e & 1 == 0 */
  11436. ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
  11437. }
  11438. if (ret == USER_CRYPTO_ERROR) {
  11439. ret = 0;
  11440. } else {
  11441. ret = WOLFSSL_FATAL_ERROR;
  11442. }
  11443. }
  11444. #endif
  11445. if (wc_FreeRng(&rng) || ret != 0) {
  11446. ret = WOLFSSL_FATAL_ERROR;
  11447. }
  11448. printf(resultFmt, ret == 0 ? passed : failed);
  11449. #endif
  11450. return ret;
  11451. } /* END test_wc_MakeRsaKey */
  11452. /*
  11453. * Test the bounds checking on the cipher text versus the key modulus.
  11454. * 1. Make a new RSA key.
  11455. * 2. Set c to 1.
  11456. * 3. Decrypt c into k. (error)
  11457. * 4. Copy the key modulus to c and sub 1 from the copy.
  11458. * 5. Decrypt c into k. (error)
  11459. * Valid bounds test cases are covered by all the other RSA tests.
  11460. */
  11461. static int test_RsaDecryptBoundsCheck(void)
  11462. {
  11463. int ret = 0;
  11464. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  11465. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  11466. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  11467. RsaKey key;
  11468. byte flatC[256];
  11469. word32 flatCSz;
  11470. byte out[256];
  11471. word32 outSz = sizeof(out);
  11472. WC_RNG rng;
  11473. printf(testingFmt, "RSA decrypt bounds check");
  11474. XMEMSET(&rng, 0, sizeof(rng));
  11475. ret = wc_InitRng(&rng);
  11476. if (ret == 0)
  11477. ret = wc_InitRsaKey(&key, NULL);
  11478. if (ret == 0) {
  11479. const byte* derKey;
  11480. word32 derKeySz;
  11481. word32 idx = 0;
  11482. #ifdef USE_CERT_BUFFERS_1024
  11483. derKey = server_key_der_1024;
  11484. derKeySz = (word32)sizeof_server_key_der_1024;
  11485. flatCSz = 128;
  11486. #else
  11487. derKey = server_key_der_2048;
  11488. derKeySz = (word32)sizeof_server_key_der_2048;
  11489. flatCSz = 256;
  11490. #endif
  11491. ret = wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz);
  11492. }
  11493. if (ret == 0) {
  11494. XMEMSET(flatC, 0, flatCSz);
  11495. flatC[flatCSz-1] = 1;
  11496. ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  11497. RSA_PRIVATE_DECRYPT, &rng);
  11498. }
  11499. if (ret == RSA_OUT_OF_RANGE_E) {
  11500. mp_int c;
  11501. mp_init_copy(&c, &key.n);
  11502. mp_sub_d(&c, 1, &c);
  11503. mp_to_unsigned_bin(&c, flatC);
  11504. ret = wc_RsaDirect(flatC, sizeof(flatC), out, &outSz, &key,
  11505. RSA_PRIVATE_DECRYPT, NULL);
  11506. mp_clear(&c);
  11507. }
  11508. if (ret == RSA_OUT_OF_RANGE_E)
  11509. ret = 0;
  11510. if (wc_FreeRsaKey(&key) || wc_FreeRng(&rng) || ret != 0)
  11511. ret = WOLFSSL_FATAL_ERROR;
  11512. printf(resultFmt, ret == 0 ? passed : failed);
  11513. #endif
  11514. return ret;
  11515. } /* END test_wc_RsaDecryptBoundsCheck */
  11516. /*
  11517. * Testing wc_SetKeyUsage()
  11518. */
  11519. static int test_wc_SetKeyUsage (void)
  11520. {
  11521. int ret = 0;
  11522. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  11523. Cert myCert;
  11524. ret = wc_InitCert(&myCert);
  11525. printf(testingFmt, "wc_SetKeyUsage()");
  11526. if (ret == 0) {
  11527. ret = wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement");
  11528. if (ret == 0) {
  11529. ret = wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation");
  11530. }
  11531. if (ret == 0) {
  11532. ret = wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly");
  11533. }
  11534. if (ret == 0) {
  11535. ret = wc_SetKeyUsage(&myCert, "decipherOnly");
  11536. }
  11537. if (ret == 0) {
  11538. ret = wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign");
  11539. }
  11540. }
  11541. /* Test bad args. */
  11542. if (ret == 0) {
  11543. ret = wc_SetKeyUsage(NULL, "decipherOnly");
  11544. if (ret == BAD_FUNC_ARG) {
  11545. ret = wc_SetKeyUsage(&myCert, NULL);
  11546. }
  11547. if (ret == BAD_FUNC_ARG) {
  11548. ret = wc_SetKeyUsage(&myCert, "");
  11549. }
  11550. if (ret == KEYUSAGE_E) {
  11551. ret = wc_SetKeyUsage(&myCert, ",");
  11552. }
  11553. if (ret == KEYUSAGE_E) {
  11554. ret = wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign");
  11555. }
  11556. if (ret == KEYUSAGE_E) {
  11557. ret = 0;
  11558. } else {
  11559. ret = WOLFSSL_FATAL_ERROR;
  11560. }
  11561. }
  11562. printf(resultFmt, ret == 0 ? passed : failed);
  11563. #endif
  11564. return ret;
  11565. } /* END test_wc_SetKeyUsage */
  11566. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  11567. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  11568. {
  11569. (void)flag;
  11570. (void)type;
  11571. (void)file;
  11572. (void)line;
  11573. }
  11574. #endif
  11575. /*
  11576. * Testing wc_LockMutex_ex
  11577. */
  11578. static int test_wc_LockMutex_ex (void)
  11579. {
  11580. int ret = 0;
  11581. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  11582. int flag = CRYPTO_LOCK;
  11583. int type = 0;
  11584. const char* file = "./test-LockMutex_ex.txt";
  11585. int line = 0;
  11586. printf(testingFmt, "wc_LockMutex_ex()");
  11587. /*without SetMutexCb*/
  11588. ret = wc_LockMutex_ex(flag, type, file, line);
  11589. if (ret == BAD_STATE_E) {
  11590. ret = 0;
  11591. }
  11592. /*with SetMutexCb*/
  11593. if (ret == 0) {
  11594. ret = wc_SetMutexCb(sample_mutex_cb);
  11595. if (ret == 0) {
  11596. ret = wc_LockMutex_ex(flag, type, file, line);
  11597. }
  11598. }
  11599. printf(resultFmt, ret == 0 ? passed : failed);
  11600. #endif
  11601. return ret;
  11602. }/*End test_wc_LockMutex_ex*/
  11603. /*
  11604. * Testing wc_SetMutexCb
  11605. */
  11606. static int test_wc_SetMutexCb (void)
  11607. {
  11608. int ret = 0;
  11609. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  11610. printf(testingFmt, "wc_SetMutexCb()");
  11611. ret = wc_SetMutexCb(sample_mutex_cb);
  11612. printf(resultFmt, ret == 0 ? passed : failed);
  11613. #endif
  11614. return ret;
  11615. }/*End test_wc_SetMutexCb*/
  11616. /*
  11617. * Testing wc_RsaKeyToDer()
  11618. */
  11619. static int test_wc_RsaKeyToDer (void)
  11620. {
  11621. int ret = 0;
  11622. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  11623. RsaKey genKey;
  11624. WC_RNG rng;
  11625. byte* der;
  11626. #ifndef WOLFSSL_SP_MATH
  11627. int bits = 1024;
  11628. word32 derSz = 611;
  11629. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  11630. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  11631. #else
  11632. int bits = 2048;
  11633. word32 derSz = 1196;
  11634. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  11635. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  11636. #endif
  11637. XMEMSET(&rng, 0, sizeof(rng));
  11638. XMEMSET(&genKey, 0, sizeof(genKey));
  11639. der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11640. if (der == NULL) {
  11641. ret = WOLFSSL_FATAL_ERROR;
  11642. }
  11643. /* Init structures. */
  11644. if (ret == 0) {
  11645. ret = wc_InitRsaKey(&genKey, NULL);
  11646. }
  11647. if (ret == 0) {
  11648. ret = wc_InitRng(&rng);
  11649. }
  11650. /* Make key. */
  11651. if (ret == 0) {
  11652. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
  11653. if (ret != 0) {
  11654. ret = WOLFSSL_FATAL_ERROR;
  11655. }
  11656. }
  11657. printf(testingFmt, "wc_RsaKeyToDer()");
  11658. if (ret == 0) {
  11659. ret = wc_RsaKeyToDer(&genKey, der, derSz);
  11660. if (ret > 0) {
  11661. ret = 0;
  11662. } else {
  11663. ret = WOLFSSL_FATAL_ERROR;
  11664. }
  11665. }
  11666. #ifndef HAVE_USER_RSA
  11667. /* Pass good/bad args. */
  11668. if (ret == 0) {
  11669. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  11670. if (ret == BAD_FUNC_ARG) {
  11671. /* Get just the output length */
  11672. ret = wc_RsaKeyToDer(&genKey, NULL, 0);
  11673. }
  11674. if (ret > 0) {
  11675. /* Try Public Key. */
  11676. genKey.type = 0;
  11677. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  11678. }
  11679. if (ret == BAD_FUNC_ARG) {
  11680. ret = 0;
  11681. } else {
  11682. ret = WOLFSSL_FATAL_ERROR;
  11683. }
  11684. }
  11685. #else
  11686. /* Pass good/bad args. */
  11687. if (ret == 0) {
  11688. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  11689. if (ret == USER_CRYPTO_ERROR) {
  11690. /* Get just the output length */
  11691. ret = wc_RsaKeyToDer(&genKey, NULL, 0);
  11692. }
  11693. if (ret > 0) {
  11694. /* Try Public Key. */
  11695. genKey.type = 0;
  11696. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  11697. }
  11698. if (ret == USER_CRYPTO_ERROR) {
  11699. ret = 0;
  11700. } else {
  11701. ret = WOLFSSL_FATAL_ERROR;
  11702. }
  11703. }
  11704. #endif
  11705. if (der != NULL) {
  11706. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11707. }
  11708. if (wc_FreeRsaKey(&genKey) || ret != 0) {
  11709. ret = WOLFSSL_FATAL_ERROR;
  11710. }
  11711. if (wc_FreeRng(&rng) || ret != 0) {
  11712. ret = WOLFSSL_FATAL_ERROR;
  11713. }
  11714. printf(resultFmt, ret == 0 ? passed : failed);
  11715. #endif
  11716. return ret;
  11717. } /* END test_wc_RsaKeyToDer */
  11718. /*
  11719. * Testing wc_RsaKeyToPublicDer()
  11720. */
  11721. static int test_wc_RsaKeyToPublicDer (void)
  11722. {
  11723. int ret = 0;
  11724. #if !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) &&\
  11725. (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL))
  11726. RsaKey key;
  11727. WC_RNG rng;
  11728. byte* der;
  11729. #ifndef WOLFSSL_SP_MATH
  11730. int bits = 1024;
  11731. word32 derLen = 162;
  11732. #else
  11733. int bits = 2048;
  11734. word32 derLen = 290;
  11735. #endif
  11736. XMEMSET(&rng, 0, sizeof(rng));
  11737. XMEMSET(&key, 0, sizeof(key));
  11738. der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11739. if (der == NULL) {
  11740. ret = WOLFSSL_FATAL_ERROR;
  11741. }
  11742. if (ret == 0) {
  11743. ret = wc_InitRsaKey(&key, NULL);
  11744. }
  11745. if (ret == 0) {
  11746. ret = wc_InitRng(&rng);
  11747. }
  11748. if (ret == 0) {
  11749. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  11750. }
  11751. printf(testingFmt, "wc_RsaKeyToPublicDer()");
  11752. if (ret == 0) {
  11753. ret = wc_RsaKeyToPublicDer(&key, der, derLen);
  11754. if (ret >= 0) {
  11755. ret = 0;
  11756. } else {
  11757. ret = WOLFSSL_FATAL_ERROR;
  11758. }
  11759. }
  11760. #ifndef HAVE_USER_RSA
  11761. /* Pass in bad args. */
  11762. if (ret == 0) {
  11763. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  11764. if (ret == BAD_FUNC_ARG) {
  11765. ret = wc_RsaKeyToPublicDer(&key, NULL, derLen);
  11766. }
  11767. if (ret == BAD_FUNC_ARG) {
  11768. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  11769. }
  11770. if (ret == BAD_FUNC_ARG) {
  11771. ret = 0;
  11772. } else {
  11773. ret = WOLFSSL_FATAL_ERROR;
  11774. }
  11775. }
  11776. #else
  11777. /* Pass in bad args. */
  11778. if (ret == 0) {
  11779. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  11780. if (ret == USER_CRYPTO_ERROR) {
  11781. ret = wc_RsaKeyToPublicDer(&key, NULL, derLen);
  11782. }
  11783. if (ret == USER_CRYPTO_ERROR) {
  11784. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  11785. }
  11786. if (ret == USER_CRYPTO_ERROR) {
  11787. ret = 0;
  11788. } else {
  11789. ret = WOLFSSL_FATAL_ERROR;
  11790. }
  11791. }
  11792. #endif
  11793. if (der != NULL) {
  11794. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11795. }
  11796. if (wc_FreeRsaKey(&key) || ret != 0) {
  11797. ret = WOLFSSL_FATAL_ERROR;
  11798. }
  11799. if (wc_FreeRng(&rng) || ret != 0) {
  11800. ret = WOLFSSL_FATAL_ERROR;
  11801. }
  11802. printf(resultFmt, ret == 0 ? passed : failed);
  11803. #endif
  11804. return ret;
  11805. } /* END test_wc_RsaKeyToPublicDer */
  11806. /*
  11807. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  11808. */
  11809. static int test_wc_RsaPublicEncryptDecrypt (void)
  11810. {
  11811. int ret = 0;
  11812. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  11813. RsaKey key;
  11814. WC_RNG rng;
  11815. const char* inStr = "Everyone gets Friday off.";
  11816. word32 plainLen = 25;
  11817. word32 inLen = (word32)XSTRLEN(inStr);
  11818. #ifndef WOLFSSL_SP_MATH
  11819. int bits = 1024;
  11820. word32 cipherLen = 128;
  11821. #else
  11822. int bits = 2048;
  11823. word32 cipherLen = 256;
  11824. #endif
  11825. DECLARE_VAR_INIT(in, byte, inLen, inStr, NULL);
  11826. DECLARE_VAR(plain, byte, plainLen, NULL);
  11827. DECLARE_VAR(cipher, byte, cipherLen, NULL);
  11828. ret = wc_InitRsaKey(&key, NULL);
  11829. if (ret == 0) {
  11830. ret = wc_InitRng(&rng);
  11831. }
  11832. if (ret == 0) {
  11833. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  11834. }
  11835. /* Encrypt. */
  11836. printf(testingFmt, "wc_RsaPublicEncrypt()");
  11837. if (ret == 0) {
  11838. ret = wc_RsaPublicEncrypt(in, inLen, cipher, cipherLen, &key, &rng);
  11839. if (ret >= 0) {
  11840. cipherLen = ret;
  11841. ret = 0;
  11842. } else {
  11843. ret = WOLFSSL_FATAL_ERROR;
  11844. }
  11845. }
  11846. /* Pass bad args. */
  11847. /* Tests PsaPublicEncryptEx() which, is tested by another fn. No need dup.*/
  11848. printf(resultFmt, ret == 0 ? passed : failed);
  11849. if (ret != 0) {
  11850. return ret;
  11851. }
  11852. /* Decrypt */
  11853. printf(testingFmt, "wc_RsaPrivateDecrypt()");
  11854. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  11855. /* Bind rng */
  11856. if (ret == 0) {
  11857. ret = wc_RsaSetRNG(&key, &rng);
  11858. }
  11859. #endif
  11860. if (ret == 0) {
  11861. ret = wc_RsaPrivateDecrypt(cipher, cipherLen, plain, plainLen, &key);
  11862. }
  11863. if (ret >= 0) {
  11864. ret = XMEMCMP(plain, inStr, plainLen);
  11865. }
  11866. /* Pass in bad args. */
  11867. /* Tests RsaPrivateDecryptEx() which, is tested by another fn. No need dup.*/
  11868. FREE_VAR(in, NULL);
  11869. FREE_VAR(plain, NULL);
  11870. FREE_VAR(cipher, NULL);
  11871. if (wc_FreeRsaKey(&key) || ret != 0) {
  11872. ret = WOLFSSL_FATAL_ERROR;
  11873. }
  11874. if (wc_FreeRng(&rng) || ret != 0) {
  11875. ret = WOLFSSL_FATAL_ERROR;
  11876. }
  11877. printf(resultFmt, ret == 0 ? passed : failed);
  11878. #endif
  11879. return ret;
  11880. } /* END test_wc_RsaPublicEncryptDecrypt */
  11881. /*
  11882. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  11883. */
  11884. static int test_wc_RsaPublicEncryptDecrypt_ex (void)
  11885. {
  11886. int ret = 0;
  11887. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  11888. && !defined(WC_NO_RSA_OAEP) && !defined(HAVE_USER_RSA)\
  11889. && !defined(NO_SHA)
  11890. RsaKey key;
  11891. WC_RNG rng;
  11892. const char* inStr = "Everyone gets Friday off.";
  11893. word32 inLen = (word32)XSTRLEN(inStr);
  11894. const word32 plainSz = 25;
  11895. byte* res = NULL;
  11896. int idx = 0;
  11897. #ifndef WOLFSSL_SP_MATH
  11898. int bits = 1024;
  11899. const word32 cipherSz = 128;
  11900. #else
  11901. int bits = 2048;
  11902. const word32 cipherSz = 256;
  11903. #endif
  11904. DECLARE_VAR_INIT(in, byte, inLen, inStr, NULL);
  11905. DECLARE_VAR(plain, byte, plainSz, NULL);
  11906. DECLARE_VAR(cipher, byte, cipherSz, NULL);
  11907. /* Initialize stack structures. */
  11908. XMEMSET(&rng, 0, sizeof(rng));
  11909. XMEMSET(&key, 0, sizeof(key));
  11910. ret = wc_InitRsaKey_ex(&key, NULL, INVALID_DEVID);
  11911. if (ret == 0) {
  11912. ret = wc_InitRng(&rng);
  11913. }
  11914. if (ret == 0) {
  11915. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  11916. }
  11917. /* Encrypt */
  11918. printf(testingFmt, "wc_RsaPublicEncrypt_ex()");
  11919. if (ret == 0) {
  11920. ret = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key, &rng,
  11921. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  11922. if (ret >= 0) {
  11923. idx = ret;
  11924. ret = 0;
  11925. } else {
  11926. ret = WOLFSSL_FATAL_ERROR;
  11927. }
  11928. }
  11929. /*Pass bad args.*/
  11930. /* Tests RsaPublicEncryptEx again. No need duplicate. */
  11931. printf(resultFmt, ret == 0 ? passed : failed);
  11932. if (ret != 0) {
  11933. return ret;
  11934. }
  11935. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  11936. /* Decrypt */
  11937. printf(testingFmt, "wc_RsaPrivateDecrypt_ex()");
  11938. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  11939. if (ret == 0) {
  11940. ret = wc_RsaSetRNG(&key, &rng);
  11941. }
  11942. #endif
  11943. if (ret == 0) {
  11944. ret = wc_RsaPrivateDecrypt_ex(cipher, (word32)idx,
  11945. plain, plainSz, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA,
  11946. WC_MGF1SHA1, NULL, 0);
  11947. }
  11948. if (ret >= 0) {
  11949. if (!XMEMCMP(plain, inStr, plainSz)) {
  11950. ret = 0;
  11951. } else {
  11952. ret = WOLFSSL_FATAL_ERROR;
  11953. }
  11954. }
  11955. /*Pass bad args.*/
  11956. /* Tests RsaPrivateDecryptEx() again. No need duplicate. */
  11957. printf(resultFmt, ret == 0 ? passed : failed);
  11958. if (ret != 0) {
  11959. return ret;
  11960. }
  11961. printf(testingFmt, "wc_RsaPrivateDecryptInline_ex()");
  11962. if (ret == 0) {
  11963. ret = wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx,
  11964. &res, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA,
  11965. WC_MGF1SHA1, NULL, 0);
  11966. if (ret >= 0) {
  11967. if (!XMEMCMP(inStr, res, plainSz)) {
  11968. ret = 0;
  11969. } else {
  11970. ret = WOLFSSL_FATAL_ERROR;
  11971. }
  11972. }
  11973. }
  11974. #endif
  11975. FREE_VAR(in, NULL);
  11976. FREE_VAR(plain, NULL);
  11977. FREE_VAR(cipher, NULL);
  11978. if (wc_FreeRsaKey(&key) || ret != 0) {
  11979. ret = WOLFSSL_FATAL_ERROR;
  11980. }
  11981. if (wc_FreeRng(&rng) || ret != 0) {
  11982. ret = WOLFSSL_FATAL_ERROR;
  11983. }
  11984. printf(resultFmt, ret == 0 ? passed : failed);
  11985. #endif
  11986. return ret;
  11987. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  11988. /*
  11989. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  11990. */
  11991. static int test_wc_RsaSSL_SignVerify (void)
  11992. {
  11993. int ret = 0;
  11994. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  11995. RsaKey key;
  11996. WC_RNG rng;
  11997. const char* inStr = "Everyone gets Friday off.";
  11998. const word32 plainSz = 25;
  11999. word32 inLen = (word32)XSTRLEN(inStr);
  12000. word32 idx = 0;
  12001. #ifndef WOLFSSL_SP_MATH
  12002. int bits = 1024;
  12003. const word32 outSz = 128;
  12004. #else
  12005. int bits = 2048;
  12006. const word32 outSz = 256;
  12007. #endif
  12008. DECLARE_VAR_INIT(in, byte, inLen, inStr, NULL);
  12009. DECLARE_VAR(out, byte, outSz, NULL);
  12010. DECLARE_VAR(plain, byte, plainSz, NULL);
  12011. ret = wc_InitRsaKey(&key, NULL);
  12012. if (ret == 0) {
  12013. ret = wc_InitRng(&rng);
  12014. }
  12015. if (ret == 0) {
  12016. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  12017. }
  12018. /* Sign. */
  12019. printf(testingFmt, "wc_RsaSSL_Sign()");
  12020. if (ret == 0) {
  12021. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng);
  12022. if (ret == (int)outSz) {
  12023. idx = ret;
  12024. ret = 0;
  12025. } else {
  12026. ret = WOLFSSL_FATAL_ERROR;
  12027. }
  12028. }
  12029. #ifndef HAVE_USER_RSA
  12030. /* Test bad args. */
  12031. if (ret == 0) {
  12032. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  12033. if (ret == BAD_FUNC_ARG) {
  12034. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  12035. }
  12036. if (ret == BAD_FUNC_ARG) {
  12037. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  12038. }
  12039. if (ret == BAD_FUNC_ARG) {
  12040. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  12041. }
  12042. if (ret == BAD_FUNC_ARG) {
  12043. ret = 0;
  12044. } else {
  12045. ret = WOLFSSL_FATAL_ERROR;
  12046. }
  12047. }
  12048. #else
  12049. /* Test bad args. */
  12050. if (ret == 0) {
  12051. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  12052. if (ret == USER_CRYPTO_ERROR) {
  12053. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  12054. }
  12055. if (ret == USER_CRYPTO_ERROR) {
  12056. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  12057. }
  12058. if (ret == USER_CRYPTO_ERROR) {
  12059. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  12060. }
  12061. if (ret == USER_CRYPTO_ERROR) {
  12062. ret = 0;
  12063. } else {
  12064. ret = WOLFSSL_FATAL_ERROR;
  12065. }
  12066. }
  12067. #endif
  12068. printf(resultFmt, ret == 0 ? passed : failed);
  12069. if (ret != 0) {
  12070. return ret;
  12071. }
  12072. /* Verify. */
  12073. printf(testingFmt, "wc_RsaSSL_Verify()");
  12074. if (ret == 0) {
  12075. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, &key);
  12076. if (ret == (int)inLen) {
  12077. ret = 0;
  12078. } else {
  12079. ret = WOLFSSL_FATAL_ERROR;
  12080. }
  12081. }
  12082. #ifndef HAVE_USER_RSA
  12083. /* Pass bad args. */
  12084. if (ret == 0) {
  12085. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  12086. if (ret == BAD_FUNC_ARG) {
  12087. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  12088. }
  12089. if (ret == BAD_FUNC_ARG) {
  12090. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  12091. }
  12092. if (ret == BAD_FUNC_ARG) {
  12093. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  12094. }
  12095. if (ret == BAD_FUNC_ARG) {
  12096. ret = 0;
  12097. } else {
  12098. ret = WOLFSSL_FATAL_ERROR;
  12099. }
  12100. }
  12101. #else
  12102. /* Pass bad args. */
  12103. if (ret == 0) {
  12104. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  12105. if (ret == USER_CRYPTO_ERROR) {
  12106. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  12107. }
  12108. if (ret == USER_CRYPTO_ERROR) {
  12109. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  12110. }
  12111. if (ret == USER_CRYPTO_ERROR) {
  12112. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  12113. }
  12114. if (ret == USER_CRYPTO_ERROR) {
  12115. ret = 0;
  12116. } else {
  12117. ret = WOLFSSL_FATAL_ERROR;
  12118. }
  12119. }
  12120. #endif
  12121. FREE_VAR(in, NULL);
  12122. FREE_VAR(out, NULL);
  12123. FREE_VAR(plain, NULL);
  12124. if (wc_FreeRsaKey(&key) || ret != 0) {
  12125. ret = WOLFSSL_FATAL_ERROR;
  12126. }
  12127. if (wc_FreeRng(&rng) || ret != 0) {
  12128. ret = WOLFSSL_FATAL_ERROR;
  12129. }
  12130. printf(resultFmt, ret == 0 ? passed : failed);
  12131. #endif
  12132. return ret;
  12133. } /* END test_wc_RsaSSL_SignVerify */
  12134. /*
  12135. * Testing wc_RsaEncryptSize()
  12136. */
  12137. static int test_wc_RsaEncryptSize (void)
  12138. {
  12139. int ret = 0;
  12140. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  12141. RsaKey key;
  12142. WC_RNG rng;
  12143. ret = wc_InitRsaKey(&key, NULL);
  12144. if (ret == 0) {
  12145. ret = wc_InitRng(&rng);
  12146. }
  12147. printf(testingFmt, "wc_RsaEncryptSize()");
  12148. #ifndef WOLFSSL_SP_MATH
  12149. if (ret == 0) {
  12150. ret = MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng);
  12151. if (ret == 0) {
  12152. ret = wc_RsaEncryptSize(&key);
  12153. }
  12154. if (ret == 128) {
  12155. ret = 0;
  12156. } else {
  12157. ret = WOLFSSL_FATAL_ERROR;
  12158. }
  12159. }
  12160. if (wc_FreeRsaKey(&key) || ret != 0) {
  12161. ret = WOLFSSL_FATAL_ERROR;
  12162. } else {
  12163. ret = 0;
  12164. }
  12165. #endif
  12166. if (ret == 0) {
  12167. ret = MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng);
  12168. if (ret == 0) {
  12169. ret = wc_RsaEncryptSize(&key);
  12170. }
  12171. if (ret == 256) {
  12172. ret = 0;
  12173. } else {
  12174. ret = WOLFSSL_FATAL_ERROR;
  12175. }
  12176. }
  12177. /* Pass in bad arg. */
  12178. if (ret == 0) {
  12179. ret = wc_RsaEncryptSize(NULL);
  12180. #ifndef HAVE_USER_RSA
  12181. if (ret == BAD_FUNC_ARG) {
  12182. ret = 0;
  12183. } else {
  12184. ret = WOLFSSL_FATAL_ERROR;
  12185. }
  12186. #endif
  12187. }
  12188. if (wc_FreeRsaKey(&key) || ret != 0) {
  12189. ret = WOLFSSL_FATAL_ERROR;
  12190. }
  12191. if (wc_FreeRng(&rng) || ret != 0) {
  12192. ret = WOLFSSL_FATAL_ERROR;
  12193. }
  12194. printf(resultFmt, ret == 0 ? passed : failed);
  12195. #endif
  12196. return ret;
  12197. } /* END test_wc_RsaEncryptSize*/
  12198. /*
  12199. * Testing wc_RsaFlattenPublicKey()
  12200. */
  12201. static int test_wc_RsaFlattenPublicKey (void)
  12202. {
  12203. int ret = 0;
  12204. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  12205. RsaKey key;
  12206. WC_RNG rng;
  12207. byte e[256];
  12208. byte n[256];
  12209. word32 eSz = sizeof(e);
  12210. word32 nSz = sizeof(n);
  12211. #ifndef WOLFSSL_SP_MATH
  12212. int bits = 1024;
  12213. #else
  12214. int bits = 2048;
  12215. #endif
  12216. ret = wc_InitRsaKey(&key, NULL);
  12217. if (ret == 0) {
  12218. ret = wc_InitRng(&rng);
  12219. }
  12220. if (ret == 0) {
  12221. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  12222. if (ret >= 0) {
  12223. ret = 0;
  12224. } else {
  12225. ret = WOLFSSL_FATAL_ERROR;
  12226. }
  12227. }
  12228. printf(testingFmt, "wc_RsaFlattenPublicKey()");
  12229. if (ret == 0) {
  12230. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz);
  12231. }
  12232. #ifndef HAVE_USER_RSA
  12233. /* Pass bad args. */
  12234. if (ret == 0) {
  12235. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  12236. if (ret == BAD_FUNC_ARG) {
  12237. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  12238. }
  12239. if (ret == BAD_FUNC_ARG) {
  12240. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  12241. }
  12242. if (ret == BAD_FUNC_ARG) {
  12243. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  12244. }
  12245. if (ret == BAD_FUNC_ARG) {
  12246. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  12247. }
  12248. if (ret == BAD_FUNC_ARG) {
  12249. ret = 0;
  12250. } else {
  12251. ret = WOLFSSL_FATAL_ERROR;
  12252. }
  12253. }
  12254. #else
  12255. /* Pass bad args. */
  12256. if (ret == 0) {
  12257. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  12258. if (ret == USER_CRYPTO_ERROR) {
  12259. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  12260. }
  12261. if (ret == USER_CRYPTO_ERROR) {
  12262. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  12263. }
  12264. if (ret == USER_CRYPTO_ERROR) {
  12265. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  12266. }
  12267. if (ret == USER_CRYPTO_ERROR) {
  12268. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  12269. }
  12270. if (ret == USER_CRYPTO_ERROR) {
  12271. ret = 0;
  12272. } else {
  12273. ret = WOLFSSL_FATAL_ERROR;
  12274. }
  12275. }
  12276. #endif
  12277. if (wc_FreeRsaKey(&key) || ret != 0) {
  12278. ret = WOLFSSL_FATAL_ERROR;
  12279. }
  12280. if (wc_FreeRng(&rng) || ret != 0) {
  12281. ret = WOLFSSL_FATAL_ERROR;
  12282. }
  12283. printf(resultFmt, ret == 0 ? passed : failed);
  12284. #endif
  12285. return ret;
  12286. } /* END test_wc_RsaFlattenPublicKey */
  12287. /*
  12288. * unit test for wc_AesCcmSetKey
  12289. */
  12290. static int test_wc_AesCcmSetKey (void)
  12291. {
  12292. int ret = 0;
  12293. #ifdef HAVE_AESCCM
  12294. Aes aes;
  12295. const byte key16[] =
  12296. {
  12297. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  12298. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  12299. };
  12300. const byte key24[] =
  12301. {
  12302. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  12303. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  12304. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  12305. };
  12306. const byte key32[] =
  12307. {
  12308. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  12309. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  12310. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  12311. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  12312. };
  12313. printf(testingFmt, "wc_AesCcmSetKey()");
  12314. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  12315. if (ret != 0)
  12316. return ret;
  12317. #ifdef WOLFSSL_AES_128
  12318. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  12319. #endif
  12320. #ifdef WOLFSSL_AES_192
  12321. if (ret == 0) {
  12322. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24));
  12323. }
  12324. #endif
  12325. #ifdef WOLFSSL_AES_256
  12326. if (ret == 0) {
  12327. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32));
  12328. }
  12329. #endif
  12330. /* Test bad args. */
  12331. if (ret == 0) {
  12332. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1);
  12333. if (ret == BAD_FUNC_ARG) {
  12334. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1);
  12335. }
  12336. if (ret == BAD_FUNC_ARG) {
  12337. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1);
  12338. }
  12339. if (ret != BAD_FUNC_ARG) {
  12340. ret = WOLFSSL_FATAL_ERROR;
  12341. } else {
  12342. ret = 0;
  12343. }
  12344. }
  12345. wc_AesFree(&aes);
  12346. printf(resultFmt, ret == 0 ? passed : failed);
  12347. #endif
  12348. return ret;
  12349. } /* END test_wc_AesCcmSetKey */
  12350. /*
  12351. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  12352. */
  12353. static int test_wc_AesCcmEncryptDecrypt (void)
  12354. {
  12355. int ret = 0;
  12356. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  12357. Aes aes;
  12358. const byte key16[] =
  12359. {
  12360. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  12361. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  12362. };
  12363. /* plaintext */
  12364. const byte plainT[] =
  12365. {
  12366. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  12367. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  12368. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  12369. };
  12370. /* nonce */
  12371. const byte iv[] =
  12372. {
  12373. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  12374. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  12375. };
  12376. const byte c[] = /* cipher text. */
  12377. {
  12378. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  12379. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  12380. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  12381. };
  12382. const byte t[] = /* Auth tag */
  12383. {
  12384. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  12385. };
  12386. const byte authIn[] =
  12387. {
  12388. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  12389. };
  12390. byte cipherOut[sizeof(plainT)];
  12391. byte authTag[sizeof(t)];
  12392. int ccmE = WOLFSSL_FATAL_ERROR;
  12393. #ifdef HAVE_AES_DECRYPT
  12394. int ccmD = WOLFSSL_FATAL_ERROR;
  12395. byte plainOut[sizeof(cipherOut)];
  12396. #endif
  12397. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  12398. if (ret != 0)
  12399. return ret;
  12400. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  12401. if (ret == 0) {
  12402. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  12403. iv, sizeof(iv), authTag, sizeof(authTag),
  12404. authIn , sizeof(authIn));
  12405. if ((XMEMCMP(cipherOut, c, sizeof(c)) && ccmE == 0) ||
  12406. XMEMCMP(t, authTag, sizeof(t))) {
  12407. ccmE = WOLFSSL_FATAL_ERROR;
  12408. ret = WOLFSSL_FATAL_ERROR;
  12409. }
  12410. #ifdef HAVE_AES_DECRYPT
  12411. if (ret == 0) {
  12412. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  12413. sizeof(plainOut), iv, sizeof(iv),
  12414. authTag, sizeof(authTag),
  12415. authIn, sizeof(authIn));
  12416. if (XMEMCMP(plainOut, plainT, sizeof(plainT)) && ccmD == 0) {
  12417. ccmD = WOLFSSL_FATAL_ERROR;
  12418. }
  12419. }
  12420. #endif
  12421. }
  12422. printf(testingFmt, "wc_AesCcmEncrypt()");
  12423. /* Pass in bad args. Encrypt*/
  12424. if (ret == 0 && ccmE == 0) {
  12425. ccmE = wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  12426. iv, sizeof(iv), authTag, sizeof(authTag),
  12427. authIn , sizeof(authIn));
  12428. if (ccmE == BAD_FUNC_ARG) {
  12429. ccmE = wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  12430. iv, sizeof(iv), authTag, sizeof(authTag),
  12431. authIn , sizeof(authIn));
  12432. }
  12433. if (ccmE == BAD_FUNC_ARG) {
  12434. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  12435. iv, sizeof(iv), authTag, sizeof(authTag),
  12436. authIn , sizeof(authIn));
  12437. }
  12438. if (ccmE == BAD_FUNC_ARG) {
  12439. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  12440. NULL, sizeof(iv), authTag, sizeof(authTag),
  12441. authIn , sizeof(authIn));
  12442. }
  12443. if (ccmE == BAD_FUNC_ARG) {
  12444. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  12445. iv, sizeof(iv), NULL, sizeof(authTag),
  12446. authIn , sizeof(authIn));
  12447. }
  12448. if (ccmE == BAD_FUNC_ARG) {
  12449. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  12450. iv, sizeof(iv) + 1, authTag, sizeof(authTag),
  12451. authIn , sizeof(authIn));
  12452. }
  12453. if (ccmE == BAD_FUNC_ARG) {
  12454. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  12455. iv, sizeof(iv) - 7, authTag, sizeof(authTag),
  12456. authIn , sizeof(authIn));
  12457. }
  12458. if (ccmE != BAD_FUNC_ARG) {
  12459. ccmE = WOLFSSL_FATAL_ERROR;
  12460. } else {
  12461. ccmE = 0;
  12462. }
  12463. } /* End Encrypt */
  12464. printf(resultFmt, ccmE == 0 ? passed : failed);
  12465. if (ccmE != 0) {
  12466. wc_AesFree(&aes);
  12467. return ccmE;
  12468. }
  12469. #ifdef HAVE_AES_DECRYPT
  12470. printf(testingFmt, "wc_AesCcmDecrypt()");
  12471. /* Pass in bad args. Decrypt*/
  12472. if (ret == 0 && ccmD == 0) {
  12473. ccmD = wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  12474. iv, sizeof(iv), authTag, sizeof(authTag),
  12475. authIn, sizeof(authIn));
  12476. if (ccmD == BAD_FUNC_ARG) {
  12477. ccmD = wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  12478. iv, sizeof(iv), authTag, sizeof(authTag),
  12479. authIn, sizeof(authIn));
  12480. }
  12481. if (ccmD == BAD_FUNC_ARG) {
  12482. ccmD = wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  12483. iv, sizeof(iv), authTag, sizeof(authTag),
  12484. authIn, sizeof(authIn));
  12485. }
  12486. if (ccmD == BAD_FUNC_ARG) {
  12487. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  12488. sizeof(plainOut), NULL, sizeof(iv),
  12489. authTag, sizeof(authTag),
  12490. authIn, sizeof(authIn));
  12491. }
  12492. if (ccmD == BAD_FUNC_ARG) {
  12493. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  12494. sizeof(plainOut), iv, sizeof(iv), NULL,
  12495. sizeof(authTag), authIn, sizeof(authIn));
  12496. }
  12497. if (ccmD == BAD_FUNC_ARG) {
  12498. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  12499. sizeof(plainOut), iv, sizeof(iv) + 1,
  12500. authTag, sizeof(authTag),
  12501. authIn, sizeof(authIn));
  12502. }
  12503. if (ccmD == BAD_FUNC_ARG) {
  12504. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  12505. sizeof(plainOut), iv, sizeof(iv) - 7,
  12506. authTag, sizeof(authTag),
  12507. authIn, sizeof(authIn));
  12508. }
  12509. if (ccmD != BAD_FUNC_ARG) {
  12510. ccmD = WOLFSSL_FATAL_ERROR;
  12511. } else {
  12512. ccmD = 0;
  12513. }
  12514. } /* END Decrypt */
  12515. printf(resultFmt, ccmD == 0 ? passed : failed);
  12516. if (ccmD != 0) {
  12517. return ccmD;
  12518. }
  12519. #endif
  12520. wc_AesFree(&aes);
  12521. #endif /* HAVE_AESCCM */
  12522. return ret;
  12523. } /* END test_wc_AesCcmEncryptDecrypt */
  12524. /*
  12525. * Test wc_Hc128_SetKey()
  12526. */
  12527. static int test_wc_Hc128_SetKey (void)
  12528. {
  12529. int ret = 0;
  12530. #ifdef HAVE_HC128
  12531. HC128 ctx;
  12532. const char* key = "\x80\x00\x00\x00\x00\x00\x00\x00"
  12533. "\x00\x00\x00\x00\x00\x00\x00\x00";
  12534. const char* iv = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  12535. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  12536. printf(testingFmt, "wc_Hc128_SetKey()");
  12537. ret = wc_Hc128_SetKey(&ctx, (byte*)key, (byte*)iv);
  12538. /* Test bad args. */
  12539. if (ret == 0) {
  12540. ret = wc_Hc128_SetKey(NULL, (byte*)key, (byte*)iv);
  12541. if (ret == BAD_FUNC_ARG) {
  12542. ret = wc_Hc128_SetKey(&ctx, NULL, (byte*)iv);
  12543. }
  12544. if (ret == BAD_FUNC_ARG) {
  12545. ret = wc_Hc128_SetKey(&ctx, (byte*)key, NULL);
  12546. }
  12547. }
  12548. printf(resultFmt, ret == 0 ? passed : failed);
  12549. #endif
  12550. return ret;
  12551. } /* END test_wc_Hc128_SetKey */
  12552. /*
  12553. * Testing wc_Hc128_Process()
  12554. */
  12555. static int test_wc_Hc128_Process (void)
  12556. {
  12557. int ret = 0;
  12558. #ifdef HAVE_HC128
  12559. HC128 enc;
  12560. HC128 dec;
  12561. const char* key = "\x0F\x62\xB5\x08\x5B\xAE\x01\x54"
  12562. "\xA7\xFA\x4D\xA0\xF3\x46\x99\xEC";
  12563. const char* input = "Encrypt Hc128, and then Decrypt.";
  12564. size_t inlen = XSTRLEN(input) + 1; /* Add null terminator */
  12565. byte cipher[inlen];
  12566. byte plain[inlen];
  12567. printf(testingFmt, "wc_Hc128_Process()");
  12568. ret = wc_Hc128_SetKey(&enc, (byte*)key, NULL);
  12569. if (ret == 0) {
  12570. ret = wc_Hc128_SetKey(&dec, (byte*)key, NULL);
  12571. }
  12572. if (ret == 0) {
  12573. ret = wc_Hc128_Process(&enc, cipher, (byte*)input, (word32)inlen);
  12574. if (ret == 0) {
  12575. ret = wc_Hc128_Process(&dec, plain, cipher, (word32)inlen);
  12576. }
  12577. }
  12578. /* Bad args. */
  12579. if (ret == 0) {
  12580. ret = wc_Hc128_Process(NULL, plain, cipher, (word32)inlen);
  12581. if (ret == BAD_FUNC_ARG) {
  12582. ret = wc_Hc128_Process(&dec, NULL, cipher, (word32)inlen);
  12583. }
  12584. if (ret == BAD_FUNC_ARG) {
  12585. ret = wc_Hc128_Process(&dec, plain, NULL, (word32)inlen);
  12586. }
  12587. if (ret == BAD_FUNC_ARG) {
  12588. ret = 0;
  12589. } else {
  12590. ret = WOLFSSL_FATAL_ERROR;
  12591. }
  12592. }
  12593. printf(resultFmt, ret == 0 ? passed : failed);
  12594. #endif
  12595. return ret;
  12596. } /* END test_wc_Hc128_Process */
  12597. /*
  12598. * Testing wc_InitDsaKey()
  12599. */
  12600. static int test_wc_InitDsaKey (void)
  12601. {
  12602. int ret = 0;
  12603. #ifndef NO_DSA
  12604. DsaKey key;
  12605. printf(testingFmt, "wc_InitDsaKey()");
  12606. ret = wc_InitDsaKey(&key);
  12607. /* Pass in bad args. */
  12608. if (ret == 0) {
  12609. ret = wc_InitDsaKey(NULL);
  12610. if (ret == BAD_FUNC_ARG) {
  12611. ret = 0;
  12612. } else {
  12613. ret = WOLFSSL_FATAL_ERROR;
  12614. }
  12615. }
  12616. printf(resultFmt, ret == 0 ? passed : failed);
  12617. wc_FreeDsaKey(&key);
  12618. #endif
  12619. return ret;
  12620. } /* END test_wc_InitDsaKey */
  12621. /*
  12622. * Testing wc_DsaSign() and wc_DsaVerify()
  12623. */
  12624. static int test_wc_DsaSignVerify (void)
  12625. {
  12626. int ret = 0;
  12627. #if !defined(NO_DSA)
  12628. DsaKey key;
  12629. WC_RNG rng;
  12630. wc_Sha sha;
  12631. byte signature[DSA_SIG_SIZE];
  12632. byte hash[WC_SHA_DIGEST_SIZE];
  12633. word32 idx = 0;
  12634. word32 bytes;
  12635. int answer;
  12636. #ifdef USE_CERT_BUFFERS_1024
  12637. byte tmp[ONEK_BUF];
  12638. XMEMSET(tmp, 0, sizeof(tmp));
  12639. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  12640. bytes = sizeof_dsa_key_der_1024;
  12641. #elif defined(USE_CERT_BUFFERS_2048)
  12642. byte tmp[TWOK_BUF];
  12643. XMEMSET(tmp, 0, sizeof(tmp));
  12644. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  12645. bytes = sizeof_dsa_key_der_2048;
  12646. #else
  12647. byte tmp[TWOK_BUF];
  12648. XMEMSET(tmp, 0, sizeof(tmp));
  12649. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  12650. if (fp == XBADFILE) {
  12651. return WOLFSSL_BAD_FILE;
  12652. }
  12653. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  12654. XFCLOSE(fp);
  12655. #endif /* END USE_CERT_BUFFERS_1024 */
  12656. ret = wc_InitSha(&sha);
  12657. if (ret == 0) {
  12658. ret = wc_ShaUpdate(&sha, tmp, bytes);
  12659. if (ret == 0) {
  12660. ret = wc_ShaFinal(&sha, hash);
  12661. }
  12662. if (ret == 0) {
  12663. ret = wc_InitDsaKey(&key);
  12664. }
  12665. if (ret == 0) {
  12666. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  12667. }
  12668. if (ret == 0) {
  12669. ret = wc_InitRng(&rng);
  12670. }
  12671. }
  12672. printf(testingFmt, "wc_DsaSign()");
  12673. /* Sign. */
  12674. if (ret == 0) {
  12675. ret = wc_DsaSign(hash, signature, &key, &rng);
  12676. }
  12677. /* Test bad args. */
  12678. if (ret == 0) {
  12679. ret = wc_DsaSign(NULL, signature, &key, &rng);
  12680. if (ret == BAD_FUNC_ARG) {
  12681. ret = wc_DsaSign(hash, NULL, &key, &rng);
  12682. }
  12683. if (ret == BAD_FUNC_ARG) {
  12684. ret = wc_DsaSign(hash, signature, NULL, &rng);
  12685. }
  12686. if (ret == BAD_FUNC_ARG) {
  12687. ret = wc_DsaSign(hash, signature, &key, NULL);
  12688. }
  12689. if (ret == BAD_FUNC_ARG) {
  12690. ret = 0;
  12691. } else {
  12692. ret = WOLFSSL_FATAL_ERROR;
  12693. }
  12694. }
  12695. printf(resultFmt, ret == 0 ? passed : failed);
  12696. if (ret != 0) {
  12697. return ret;
  12698. }
  12699. /* Verify. */
  12700. printf(testingFmt, "wc_DsaVerify()");
  12701. ret = wc_DsaVerify(hash, signature, &key, &answer);
  12702. if (ret != 0 || answer != 1) {
  12703. ret = WOLFSSL_FATAL_ERROR;
  12704. } else {
  12705. ret = 0;
  12706. }
  12707. /* Pass in bad args. */
  12708. if (ret == 0) {
  12709. ret = wc_DsaVerify(NULL, signature, &key, &answer);
  12710. if (ret == BAD_FUNC_ARG) {
  12711. ret = wc_DsaVerify(hash, NULL, &key, &answer);
  12712. }
  12713. if (ret == BAD_FUNC_ARG) {
  12714. ret = wc_DsaVerify(hash, signature, NULL, &answer);
  12715. }
  12716. if (ret == BAD_FUNC_ARG) {
  12717. ret = wc_DsaVerify(hash, signature, &key, NULL);
  12718. }
  12719. if (ret == BAD_FUNC_ARG) {
  12720. ret = 0;
  12721. } else {
  12722. ret = WOLFSSL_FATAL_ERROR;
  12723. }
  12724. }
  12725. if (wc_FreeRng(&rng) && ret == 0) {
  12726. ret = WOLFSSL_FATAL_ERROR;
  12727. }
  12728. printf(resultFmt, ret == 0 ? passed : failed);
  12729. wc_FreeDsaKey(&key);
  12730. wc_ShaFree(&sha);
  12731. #endif
  12732. return ret;
  12733. } /* END test_wc_DsaSign */
  12734. /*
  12735. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  12736. */
  12737. static int test_wc_DsaPublicPrivateKeyDecode (void)
  12738. {
  12739. int ret = 0;
  12740. #if !defined(NO_DSA)
  12741. DsaKey key;
  12742. word32 bytes;
  12743. word32 idx = 0;
  12744. int priv = WOLFSSL_FATAL_ERROR;
  12745. int pub = WOLFSSL_FATAL_ERROR;
  12746. #ifdef USE_CERT_BUFFERS_1024
  12747. byte tmp[ONEK_BUF];
  12748. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  12749. bytes = sizeof_dsa_key_der_1024;
  12750. #elif defined(USE_CERT_BUFFERS_2048)
  12751. byte tmp[TWOK_BUF];
  12752. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  12753. bytes = sizeof_dsa_key_der_2048;
  12754. #else
  12755. byte tmp[TWOK_BUF];
  12756. XMEMSET(tmp, 0, sizeof(tmp));
  12757. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  12758. if (fp == XBADFILE)
  12759. {
  12760. return WOLFSSL_BAD_FILE;
  12761. }
  12762. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  12763. XFCLOSE(fp);
  12764. #endif /* END USE_CERT_BUFFERS_1024 */
  12765. ret = wc_InitDsaKey(&key);
  12766. printf(testingFmt, "wc_DsaPrivateKeyDecode()");
  12767. if (ret == 0) {
  12768. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  12769. /* Test bad args. */
  12770. if (priv == 0) {
  12771. priv = wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes);
  12772. if (priv == BAD_FUNC_ARG) {
  12773. priv = wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes);
  12774. }
  12775. if (priv == BAD_FUNC_ARG) {
  12776. priv = wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes);
  12777. }
  12778. if (priv == BAD_FUNC_ARG) {
  12779. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  12780. }
  12781. if (priv == ASN_PARSE_E) {
  12782. priv = 0;
  12783. } else {
  12784. priv = WOLFSSL_FATAL_ERROR;
  12785. }
  12786. }
  12787. wc_FreeDsaKey(&key);
  12788. ret = wc_InitDsaKey(&key);
  12789. }
  12790. printf(resultFmt, priv == 0 ? passed : failed);
  12791. printf(testingFmt, "wc_DsaPublicKeyDecode()");
  12792. if (ret == 0) {
  12793. idx = 0; /* Reset */
  12794. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  12795. /* Test bad args. */
  12796. if (pub == 0) {
  12797. pub = wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes);
  12798. if (pub == BAD_FUNC_ARG) {
  12799. pub = wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes);
  12800. }
  12801. if (pub == BAD_FUNC_ARG) {
  12802. pub = wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes);
  12803. }
  12804. if (pub == BAD_FUNC_ARG) {
  12805. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  12806. }
  12807. if (pub == ASN_PARSE_E) {
  12808. pub = 0;
  12809. } else {
  12810. pub = WOLFSSL_FATAL_ERROR;
  12811. }
  12812. }
  12813. } /* END Public Key */
  12814. printf(resultFmt, pub == 0 ? passed : failed);
  12815. wc_FreeDsaKey(&key);
  12816. #endif
  12817. return ret;
  12818. } /* END test_wc_DsaPublicPrivateKeyDecode */
  12819. /*
  12820. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  12821. */
  12822. static int test_wc_MakeDsaKey (void)
  12823. {
  12824. int ret = 0;
  12825. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  12826. DsaKey genKey;
  12827. WC_RNG rng;
  12828. XMEMSET(&rng, 0, sizeof(rng));
  12829. XMEMSET(&genKey, 0, sizeof(genKey));
  12830. ret = wc_InitRng(&rng);
  12831. if (ret == 0) {
  12832. ret = wc_InitDsaKey(&genKey);
  12833. }
  12834. printf(testingFmt, "wc_MakeDsaParameters()");
  12835. if (ret == 0) {
  12836. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  12837. }
  12838. /* Test bad args. */
  12839. if (ret == 0) {
  12840. ret = wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey);
  12841. if (ret == BAD_FUNC_ARG) {
  12842. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, NULL);
  12843. }
  12844. if (ret == BAD_FUNC_ARG) {
  12845. ret = wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey);
  12846. }
  12847. if (ret == BAD_FUNC_ARG) {
  12848. ret = 0;
  12849. } else {
  12850. ret = WOLFSSL_FATAL_ERROR;
  12851. }
  12852. }
  12853. printf(resultFmt, ret == 0 ? passed : failed);
  12854. printf(testingFmt, "wc_MakeDsaKey()");
  12855. if (ret == 0) {
  12856. ret = wc_MakeDsaKey(&rng, &genKey);
  12857. }
  12858. /* Test bad args. */
  12859. if (ret == 0) {
  12860. ret = wc_MakeDsaKey(NULL, &genKey);
  12861. if (ret == BAD_FUNC_ARG) {
  12862. ret = wc_MakeDsaKey(&rng, NULL);
  12863. }
  12864. if (ret == BAD_FUNC_ARG) {
  12865. ret = 0;
  12866. } else {
  12867. ret = WOLFSSL_FATAL_ERROR;
  12868. }
  12869. }
  12870. if (wc_FreeRng(&rng) && ret == 0) {
  12871. ret = WOLFSSL_FAILURE;
  12872. }
  12873. printf(resultFmt, ret == 0 ? passed : failed);
  12874. wc_FreeDsaKey(&genKey);
  12875. #endif
  12876. return ret;
  12877. } /* END test_wc_MakeDsaKey */
  12878. /*
  12879. * Testing wc_DsaKeyToDer()
  12880. */
  12881. static int test_wc_DsaKeyToDer (void)
  12882. {
  12883. int ret = 0;
  12884. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  12885. DsaKey genKey;
  12886. WC_RNG rng;
  12887. word32 bytes;
  12888. word32 idx = 0;
  12889. #ifdef USE_CERT_BUFFERS_1024
  12890. byte tmp[ONEK_BUF];
  12891. byte der[ONEK_BUF];
  12892. XMEMSET(tmp, 0, sizeof(tmp));
  12893. XMEMSET(der, 0, sizeof(der));
  12894. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  12895. bytes = sizeof_dsa_key_der_1024;
  12896. #elif defined(USE_CERT_BUFFERS_2048)
  12897. byte tmp[TWOK_BUF];
  12898. byte der[TWOK_BUF];
  12899. XMEMSET(tmp, 0, sizeof(tmp));
  12900. XMEMSET(der, 0, sizeof(der));
  12901. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  12902. bytes = sizeof_dsa_key_der_2048;
  12903. #else
  12904. byte tmp[TWOK_BUF];
  12905. byte der[TWOK_BUF];
  12906. XMEMSET(tmp, 0, sizeof(tmp));
  12907. XMEMSET(der, 0, sizeof(der));
  12908. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  12909. if (fp == XBADFILE) {
  12910. return WOLFSSL_BAD_FILE;
  12911. }
  12912. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  12913. XFCLOSE(fp);
  12914. #endif /* END USE_CERT_BUFFERS_1024 */
  12915. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  12916. XMEMSET(&rng, 0, sizeof(rng));
  12917. XMEMSET(&genKey, 0, sizeof(genKey));
  12918. #endif
  12919. ret = wc_InitRng(&rng);
  12920. if (ret == 0) {
  12921. ret = wc_InitDsaKey(&genKey);
  12922. }
  12923. if (ret == 0) {
  12924. ret = wc_MakeDsaParameters(&rng, sizeof(tmp), &genKey);
  12925. if (ret == 0) {
  12926. wc_FreeDsaKey(&genKey);
  12927. ret = wc_InitDsaKey(&genKey);
  12928. }
  12929. }
  12930. if (ret == 0) {
  12931. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &genKey, bytes);
  12932. }
  12933. printf(testingFmt, "wc_DsaKeyToDer()");
  12934. if (ret == 0) {
  12935. ret = wc_DsaKeyToDer(&genKey, der, bytes);
  12936. if ( ret >= 0 && ( ret = XMEMCMP(der, tmp, bytes) ) == 0 ) {
  12937. ret = 0;
  12938. }
  12939. }
  12940. /* Test bad args. */
  12941. if (ret == 0) {
  12942. ret = wc_DsaKeyToDer(NULL, der, FOURK_BUF);
  12943. if (ret == BAD_FUNC_ARG) {
  12944. ret = wc_DsaKeyToDer(&genKey, NULL, FOURK_BUF);
  12945. }
  12946. if (ret == BAD_FUNC_ARG) {
  12947. ret = 0;
  12948. } else {
  12949. ret = WOLFSSL_FATAL_ERROR;
  12950. }
  12951. }
  12952. if (wc_FreeRng(&rng) && ret == 0) {
  12953. ret = WOLFSSL_FATAL_ERROR;
  12954. }
  12955. printf(resultFmt, ret == 0 ? passed : failed);
  12956. wc_FreeDsaKey(&genKey);
  12957. #endif
  12958. return ret;
  12959. } /* END test_wc_DsaKeyToDer */
  12960. /*
  12961. * Testing wc_DsaKeyToPublicDer()
  12962. * (indirectly testing setDsaPublicKey())
  12963. */
  12964. static int test_wc_DsaKeyToPublicDer(void)
  12965. {
  12966. int ret = 0;
  12967. #ifndef HAVE_SELFTEST
  12968. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  12969. DsaKey genKey;
  12970. WC_RNG rng;
  12971. byte* der;
  12972. printf(testingFmt, "wc_DsaKeyToPublicDer()");
  12973. der = (byte*)XMALLOC(ONEK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12974. if (der == NULL) {
  12975. ret = WOLFSSL_FATAL_ERROR;
  12976. }
  12977. if (ret == 0) {
  12978. ret = wc_InitDsaKey(&genKey);
  12979. }
  12980. if (ret == 0) {
  12981. ret = wc_InitRng(&rng);
  12982. }
  12983. if (ret == 0) {
  12984. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  12985. }
  12986. if (ret == 0) {
  12987. ret = wc_MakeDsaKey(&rng, &genKey);
  12988. }
  12989. if (ret == 0) {
  12990. ret = wc_DsaKeyToPublicDer(&genKey, der, ONEK_BUF);
  12991. if (ret >= 0) {
  12992. ret = 0;
  12993. } else {
  12994. ret = WOLFSSL_FATAL_ERROR;
  12995. }
  12996. }
  12997. /* Test bad args. */
  12998. if (ret == 0) {
  12999. ret = wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF);
  13000. if (ret == BAD_FUNC_ARG) {
  13001. ret = wc_DsaKeyToPublicDer(&genKey, NULL, FOURK_BUF);
  13002. }
  13003. if (ret == BAD_FUNC_ARG) {
  13004. ret = 0;
  13005. } else {
  13006. ret = WOLFSSL_FATAL_ERROR;
  13007. }
  13008. }
  13009. if (wc_FreeRng(&rng) && ret == 0) {
  13010. ret = WOLFSSL_FATAL_ERROR;
  13011. }
  13012. printf(resultFmt, ret == 0 ? passed : failed);
  13013. XFREE(der,NULL,DYNAMIC_TYPE_TMP_BUFFER);
  13014. wc_FreeDsaKey(&genKey);
  13015. #endif /* !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN) */
  13016. #endif /* HAVE_SELFTEST */
  13017. return ret;
  13018. } /* END test_wc_DsaKeyToPublicDer */
  13019. /*
  13020. * Testing wc_DsaImportParamsRaw()
  13021. */
  13022. static int test_wc_DsaImportParamsRaw (void)
  13023. {
  13024. int ret = 0;
  13025. #if !defined(NO_DSA)
  13026. DsaKey key;
  13027. /* [mod = L=1024, N=160], from CAVP KeyPair */
  13028. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  13029. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  13030. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  13031. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  13032. "47123188f8dc551054ee162b634d60f097f719076640e209"
  13033. "80a0093113a8bd73";
  13034. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  13035. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  13036. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  13037. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  13038. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  13039. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  13040. "76341a7e7d9";
  13041. /* invalid p and q parameters */
  13042. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  13043. const char* invalidQ = "96c5390a";
  13044. printf(testingFmt, "wc_DsaImportParamsRaw()");
  13045. ret = wc_InitDsaKey(&key);
  13046. if (ret == 0) {
  13047. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  13048. }
  13049. /* test bad args */
  13050. if (ret == 0) {
  13051. /* null key struct */
  13052. ret = wc_DsaImportParamsRaw(NULL, p, q, g);
  13053. if (ret == BAD_FUNC_ARG) {
  13054. /* null param pointers */
  13055. ret = wc_DsaImportParamsRaw(&key, NULL, NULL, NULL);
  13056. }
  13057. if (ret == BAD_FUNC_ARG) {
  13058. /* illegal p length */
  13059. ret = wc_DsaImportParamsRaw(&key, invalidP, q, g);
  13060. }
  13061. if (ret == BAD_FUNC_ARG) {
  13062. /* illegal q length */
  13063. ret = wc_DsaImportParamsRaw(&key, p, invalidQ, g);
  13064. if (ret == BAD_FUNC_ARG)
  13065. ret = 0;
  13066. }
  13067. }
  13068. printf(resultFmt, ret == 0 ? passed : failed);
  13069. wc_FreeDsaKey(&key);
  13070. #endif
  13071. return ret;
  13072. } /* END test_wc_DsaImportParamsRaw */
  13073. /*
  13074. * Testing wc_DsaImportParamsRawCheck()
  13075. */
  13076. static int test_wc_DsaImportParamsRawCheck (void)
  13077. {
  13078. int ret = 0;
  13079. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  13080. DsaKey key;
  13081. int trusted = 0;
  13082. /* [mod = L=1024, N=160], from CAVP KeyPair */
  13083. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  13084. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  13085. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  13086. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  13087. "47123188f8dc551054ee162b634d60f097f719076640e209"
  13088. "80a0093113a8bd73";
  13089. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  13090. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  13091. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  13092. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  13093. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  13094. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  13095. "76341a7e7d9";
  13096. /* invalid p and q parameters */
  13097. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  13098. const char* invalidQ = "96c5390a";
  13099. printf(testingFmt, "wc_DsaImportParamsRawCheck()");
  13100. ret = wc_InitDsaKey(&key);
  13101. if (ret == 0) {
  13102. ret = wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL);
  13103. }
  13104. /* test bad args */
  13105. if (ret == 0) {
  13106. /* null key struct */
  13107. ret = wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL);
  13108. if (ret == BAD_FUNC_ARG) {
  13109. /* null param pointers */
  13110. ret = wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted, NULL);
  13111. }
  13112. if (ret == BAD_FUNC_ARG) {
  13113. /* illegal p length */
  13114. ret = wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL);
  13115. }
  13116. if (ret == BAD_FUNC_ARG) {
  13117. /* illegal q length */
  13118. ret = wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL);
  13119. if (ret == BAD_FUNC_ARG)
  13120. ret = 0;
  13121. }
  13122. }
  13123. printf(resultFmt, ret == 0 ? passed : failed);
  13124. wc_FreeDsaKey(&key);
  13125. #endif
  13126. return ret;
  13127. } /* END test_wc_DsaImportParamsRawCheck */
  13128. /*
  13129. * Testing wc_DsaExportParamsRaw()
  13130. */
  13131. static int test_wc_DsaExportParamsRaw (void)
  13132. {
  13133. int ret = 0;
  13134. #if !defined(NO_DSA)
  13135. DsaKey key;
  13136. /* [mod = L=1024, N=160], from CAVP KeyPair */
  13137. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  13138. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  13139. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  13140. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  13141. "47123188f8dc551054ee162b634d60f097f719076640e209"
  13142. "80a0093113a8bd73";
  13143. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  13144. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  13145. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  13146. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  13147. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  13148. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  13149. "76341a7e7d9";
  13150. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  13151. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  13152. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  13153. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  13154. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  13155. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  13156. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  13157. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  13158. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  13159. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  13160. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  13161. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  13162. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  13163. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  13164. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  13165. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  13166. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  13167. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  13168. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  13169. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  13170. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  13171. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  13172. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  13173. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  13174. byte pOut[MAX_DSA_PARAM_SIZE];
  13175. byte qOut[MAX_DSA_PARAM_SIZE];
  13176. byte gOut[MAX_DSA_PARAM_SIZE];
  13177. word32 pOutSz, qOutSz, gOutSz;
  13178. printf(testingFmt, "wc_DsaExportParamsRaw()");
  13179. ret = wc_InitDsaKey(&key);
  13180. if (ret == 0) {
  13181. /* first test using imported raw parameters, for expected */
  13182. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  13183. }
  13184. if (ret == 0) {
  13185. pOutSz = sizeof(pOut);
  13186. qOutSz = sizeof(qOut);
  13187. gOutSz = sizeof(gOut);
  13188. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  13189. gOut, &gOutSz);
  13190. }
  13191. if (ret == 0) {
  13192. /* validate exported parameters are correct */
  13193. if ((XMEMCMP(pOut, pCompare, pOutSz) != 0) ||
  13194. (XMEMCMP(qOut, qCompare, qOutSz) != 0) ||
  13195. (XMEMCMP(gOut, gCompare, gOutSz) != 0) ) {
  13196. ret = -1;
  13197. }
  13198. }
  13199. /* test bad args */
  13200. if (ret == 0) {
  13201. /* null key struct */
  13202. ret = wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz,
  13203. gOut, &gOutSz);
  13204. if (ret == BAD_FUNC_ARG) {
  13205. /* null output pointers */
  13206. ret = wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz,
  13207. NULL, &gOutSz);
  13208. }
  13209. if (ret == LENGTH_ONLY_E) {
  13210. /* null output size pointers */
  13211. ret = wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL,
  13212. gOut, NULL);
  13213. }
  13214. if (ret == BAD_FUNC_ARG) {
  13215. /* p output buffer size too small */
  13216. pOutSz = 1;
  13217. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  13218. gOut, &gOutSz);
  13219. pOutSz = sizeof(pOut);
  13220. }
  13221. if (ret == BUFFER_E) {
  13222. /* q output buffer size too small */
  13223. qOutSz = 1;
  13224. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  13225. gOut, &gOutSz);
  13226. qOutSz = sizeof(qOut);
  13227. }
  13228. if (ret == BUFFER_E) {
  13229. /* g output buffer size too small */
  13230. gOutSz = 1;
  13231. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  13232. gOut, &gOutSz);
  13233. if (ret == BUFFER_E)
  13234. ret = 0;
  13235. }
  13236. }
  13237. printf(resultFmt, ret == 0 ? passed : failed);
  13238. wc_FreeDsaKey(&key);
  13239. #endif
  13240. return ret;
  13241. } /* END test_wc_DsaExportParamsRaw */
  13242. /*
  13243. * Testing wc_DsaExportKeyRaw()
  13244. */
  13245. static int test_wc_DsaExportKeyRaw (void)
  13246. {
  13247. int ret = 0;
  13248. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  13249. DsaKey key;
  13250. WC_RNG rng;
  13251. byte xOut[MAX_DSA_PARAM_SIZE];
  13252. byte yOut[MAX_DSA_PARAM_SIZE];
  13253. word32 xOutSz, yOutSz;
  13254. printf(testingFmt, "wc_DsaExportKeyRaw()");
  13255. XMEMSET(&rng, 0, sizeof(rng));
  13256. XMEMSET(&key, 0, sizeof(key));
  13257. ret = wc_InitRng(&rng);
  13258. if (ret == 0) {
  13259. ret = wc_InitDsaKey(&key);
  13260. }
  13261. if (ret == 0) {
  13262. ret = wc_MakeDsaParameters(&rng, 1024, &key);
  13263. if (ret == 0) {
  13264. ret = wc_MakeDsaKey(&rng, &key);
  13265. }
  13266. }
  13267. /* try successful export */
  13268. if (ret == 0) {
  13269. xOutSz = sizeof(xOut);
  13270. yOutSz = sizeof(yOut);
  13271. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  13272. }
  13273. /* test bad args */
  13274. if (ret == 0) {
  13275. /* null key struct */
  13276. ret = wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz);
  13277. if (ret == BAD_FUNC_ARG) {
  13278. /* null output pointers */
  13279. ret = wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz);
  13280. }
  13281. if (ret == LENGTH_ONLY_E) {
  13282. /* null output size pointers */
  13283. ret = wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL);
  13284. }
  13285. if (ret == BAD_FUNC_ARG) {
  13286. /* x output buffer size too small */
  13287. xOutSz = 1;
  13288. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  13289. xOutSz = sizeof(xOut);
  13290. }
  13291. if (ret == BUFFER_E) {
  13292. /* y output buffer size too small */
  13293. yOutSz = 1;
  13294. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  13295. if (ret == BUFFER_E)
  13296. ret = 0;
  13297. }
  13298. }
  13299. printf(resultFmt, ret == 0 ? passed : failed);
  13300. wc_FreeDsaKey(&key);
  13301. wc_FreeRng(&rng);
  13302. #endif
  13303. return ret;
  13304. } /* END test_wc_DsaExportParamsRaw */
  13305. /*
  13306. * Testing wc_ed25519_make_key().
  13307. */
  13308. static int test_wc_ed25519_make_key (void)
  13309. {
  13310. int ret = 0;
  13311. #if defined(HAVE_ED25519)
  13312. ed25519_key key;
  13313. WC_RNG rng;
  13314. ret = wc_InitRng(&rng);
  13315. if (ret == 0) {
  13316. ret = wc_ed25519_init(&key);
  13317. }
  13318. printf(testingFmt, "wc_ed25519_make_key()");
  13319. if (ret == 0) {
  13320. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  13321. }
  13322. /* Test bad args. */
  13323. if (ret == 0) {
  13324. ret = wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key);
  13325. if (ret == BAD_FUNC_ARG) {
  13326. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL);
  13327. }
  13328. if (ret == BAD_FUNC_ARG) {
  13329. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key);
  13330. }
  13331. if (ret == BAD_FUNC_ARG) {
  13332. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key);
  13333. }
  13334. if (ret == BAD_FUNC_ARG) {
  13335. ret = 0;
  13336. } else if (ret == 0) {
  13337. ret = SSL_FATAL_ERROR;
  13338. }
  13339. }
  13340. printf(resultFmt, ret == 0 ? passed : failed);
  13341. if (wc_FreeRng(&rng) && ret == 0) {
  13342. ret = SSL_FATAL_ERROR;
  13343. }
  13344. wc_ed25519_free(&key);
  13345. #endif
  13346. return ret;
  13347. } /* END test_wc_ed25519_make_key */
  13348. /*
  13349. * Testing wc_ed25519_init()
  13350. */
  13351. static int test_wc_ed25519_init (void)
  13352. {
  13353. int ret = 0;
  13354. #if defined(HAVE_ED25519)
  13355. ed25519_key key;
  13356. printf(testingFmt, "wc_ed25519_init()");
  13357. ret = wc_ed25519_init(&key);
  13358. /* Test bad args. */
  13359. if (ret == 0) {
  13360. ret = wc_ed25519_init(NULL);
  13361. if (ret == BAD_FUNC_ARG) {
  13362. ret = 0;
  13363. } else if (ret == 0) {
  13364. ret = SSL_FATAL_ERROR;
  13365. }
  13366. }
  13367. printf(resultFmt, ret == 0 ? passed : failed);
  13368. wc_ed25519_free(&key);
  13369. #endif
  13370. return ret;
  13371. } /* END test_wc_ed25519_init */
  13372. /*
  13373. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  13374. */
  13375. static int test_wc_ed25519_sign_msg (void)
  13376. {
  13377. int ret = 0;
  13378. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  13379. WC_RNG rng;
  13380. ed25519_key key;
  13381. byte msg[] = "Everybody gets Friday off.\n";
  13382. byte sig[ED25519_SIG_SIZE];
  13383. word32 msglen = sizeof(msg);
  13384. word32 siglen = sizeof(sig);
  13385. word32 badSigLen = sizeof(sig) - 1;
  13386. int verify_ok = 0; /*1 = Verify success.*/
  13387. /* Initialize stack variables. */
  13388. XMEMSET(sig, 0, siglen);
  13389. /* Initialize key. */
  13390. ret = wc_InitRng(&rng);
  13391. if (ret == 0) {
  13392. ret = wc_ed25519_init(&key);
  13393. if (ret == 0) {
  13394. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  13395. }
  13396. }
  13397. printf(testingFmt, "wc_ed25519_sign_msg()");
  13398. if (ret == 0) {
  13399. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key);
  13400. }
  13401. /* Test bad args. */
  13402. if (ret == 0 && siglen == ED25519_SIG_SIZE) {
  13403. ret = wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key);
  13404. if (ret == BAD_FUNC_ARG) {
  13405. ret = wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key);
  13406. }
  13407. if (ret == BAD_FUNC_ARG) {
  13408. ret = wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key);
  13409. }
  13410. if (ret == BAD_FUNC_ARG) {
  13411. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL);
  13412. }
  13413. if (ret == BAD_FUNC_ARG) {
  13414. ret = wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key);
  13415. }
  13416. if (ret == BUFFER_E && badSigLen == ED25519_SIG_SIZE) {
  13417. badSigLen -= 1;
  13418. ret = 0;
  13419. } else if (ret == 0) {
  13420. ret = SSL_FATAL_ERROR;
  13421. }
  13422. } /* END sign */
  13423. printf(resultFmt, ret == 0 ? passed : failed);
  13424. #ifdef HAVE_ED25519_VERIFY
  13425. printf(testingFmt, "wc_ed25519_verify_msg()");
  13426. if (ret == 0) {
  13427. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key);
  13428. if (ret == 0 && verify_ok == 1) {
  13429. ret = 0;
  13430. } else if (ret == 0) {
  13431. ret = SSL_FATAL_ERROR;
  13432. }
  13433. /* Test bad args. */
  13434. if (ret == 0) {
  13435. AssertIntEQ(wc_ed25519_verify_msg(sig, siglen - 1, msg,
  13436. msglen, &verify_ok, &key),
  13437. BAD_FUNC_ARG);
  13438. AssertIntEQ(wc_ed25519_verify_msg(sig, siglen + 1, msg,
  13439. msglen, &verify_ok, &key),
  13440. BAD_FUNC_ARG);
  13441. ret = wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  13442. &key);
  13443. if (ret == BAD_FUNC_ARG) {
  13444. ret = wc_ed25519_verify_msg(sig, siglen, NULL, msglen,
  13445. &verify_ok, &key);
  13446. }
  13447. if (ret == BAD_FUNC_ARG) {
  13448. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  13449. NULL, &key);
  13450. }
  13451. if (ret == BAD_FUNC_ARG) {
  13452. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  13453. &verify_ok, NULL);
  13454. }
  13455. if (ret == BAD_FUNC_ARG) {
  13456. ret = wc_ed25519_verify_msg(sig, badSigLen, msg, msglen,
  13457. &verify_ok, &key);
  13458. }
  13459. if (ret == BAD_FUNC_ARG) {
  13460. ret = 0;
  13461. } else if (ret == 0) {
  13462. ret = SSL_FATAL_ERROR;
  13463. }
  13464. }
  13465. } /* END verify. */
  13466. printf(resultFmt, ret == 0 ? passed : failed);
  13467. #endif /* Verify. */
  13468. if (wc_FreeRng(&rng) && ret == 0) {
  13469. ret = SSL_FATAL_ERROR;
  13470. }
  13471. wc_ed25519_free(&key);
  13472. #endif
  13473. return ret;
  13474. } /* END test_wc_ed25519_sign_msg */
  13475. /*
  13476. * Testing wc_ed25519_import_public()
  13477. */
  13478. static int test_wc_ed25519_import_public (void)
  13479. {
  13480. int ret = 0;
  13481. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  13482. WC_RNG rng;
  13483. ed25519_key pubKey;
  13484. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  13485. word32 inlen = sizeof(in);
  13486. ret = wc_InitRng(&rng);
  13487. if (ret == 0) {
  13488. ret = wc_ed25519_init(&pubKey);
  13489. if (ret == 0) {
  13490. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey);
  13491. }
  13492. }
  13493. printf(testingFmt, "wc_ed25519_import_public()");
  13494. if (ret == 0) {
  13495. ret = wc_ed25519_import_public(in, inlen, &pubKey);
  13496. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  13497. ret = 0;
  13498. } else {
  13499. ret = SSL_FATAL_ERROR;
  13500. }
  13501. /* Test bad args. */
  13502. if (ret == 0) {
  13503. ret = wc_ed25519_import_public(NULL, inlen, &pubKey);
  13504. if (ret == BAD_FUNC_ARG) {
  13505. ret = wc_ed25519_import_public(in, inlen, NULL);
  13506. }
  13507. if (ret == BAD_FUNC_ARG) {
  13508. ret = wc_ed25519_import_public(in, inlen - 1, &pubKey);
  13509. }
  13510. if (ret == BAD_FUNC_ARG) {
  13511. ret = 0;
  13512. } else if (ret == 0) {
  13513. ret = SSL_FATAL_ERROR;
  13514. }
  13515. }
  13516. }
  13517. printf(resultFmt, ret == 0 ? passed : failed);
  13518. if (wc_FreeRng(&rng) && ret == 0) {
  13519. ret = SSL_FATAL_ERROR;
  13520. }
  13521. wc_ed25519_free(&pubKey);
  13522. #endif
  13523. return ret;
  13524. } /* END wc_ed25519_import_public */
  13525. /*
  13526. * Testing wc_ed25519_import_private_key()
  13527. */
  13528. static int test_wc_ed25519_import_private_key (void)
  13529. {
  13530. int ret = 0;
  13531. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  13532. WC_RNG rng;
  13533. ed25519_key key;
  13534. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  13535. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  13536. word32 privKeySz = sizeof(privKey);
  13537. word32 pubKeySz = sizeof(pubKey);
  13538. ret = wc_InitRng(&rng);
  13539. if (ret != 0) {
  13540. return ret;
  13541. }
  13542. ret = wc_ed25519_init(&key);
  13543. if (ret != 0) {
  13544. wc_FreeRng(&rng);
  13545. return ret;
  13546. }
  13547. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  13548. printf(testingFmt, "wc_ed25519_import_private_key()");
  13549. if (ret == 0) {
  13550. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  13551. pubKeySz, &key);
  13552. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
  13553. || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  13554. ret = SSL_FATAL_ERROR;
  13555. }
  13556. }
  13557. /* Test bad args. */
  13558. if (ret == 0) {
  13559. ret = wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  13560. &key);
  13561. if (ret == BAD_FUNC_ARG) {
  13562. ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  13563. pubKeySz, &key);
  13564. }
  13565. if (ret == BAD_FUNC_ARG) {
  13566. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  13567. pubKeySz, NULL);
  13568. }
  13569. if (ret == BAD_FUNC_ARG) {
  13570. ret = wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  13571. pubKeySz, &key);
  13572. }
  13573. if (ret == BAD_FUNC_ARG) {
  13574. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  13575. pubKeySz - 1, &key);
  13576. }
  13577. if (ret == BAD_FUNC_ARG) {
  13578. ret = 0;
  13579. } else if (ret == 0) {
  13580. ret = SSL_FATAL_ERROR;
  13581. }
  13582. }
  13583. printf(resultFmt, ret == 0 ? passed : failed);
  13584. if (wc_FreeRng(&rng) && ret == 0) {
  13585. ret = SSL_FATAL_ERROR;
  13586. }
  13587. wc_ed25519_free(&key);
  13588. #endif
  13589. return ret;
  13590. } /* END test_wc_ed25519_import_private_key */
  13591. /*
  13592. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  13593. */
  13594. static int test_wc_ed25519_export (void)
  13595. {
  13596. int ret = 0;
  13597. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  13598. WC_RNG rng;
  13599. ed25519_key key;
  13600. byte priv[ED25519_PRV_KEY_SIZE];
  13601. byte pub[ED25519_PUB_KEY_SIZE];
  13602. word32 privSz = sizeof(priv);
  13603. word32 pubSz = sizeof(pub);
  13604. ret = wc_InitRng(&rng);
  13605. if (ret != 0) {
  13606. return ret;
  13607. }
  13608. ret = wc_ed25519_init(&key);
  13609. if (ret != 0) {
  13610. wc_FreeRng(&rng);
  13611. return ret;
  13612. }
  13613. if (ret == 0) {
  13614. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  13615. }
  13616. printf(testingFmt, "wc_ed25519_export_public()");
  13617. if (ret == 0) {
  13618. ret = wc_ed25519_export_public(&key, pub, &pubSz);
  13619. if (ret == 0 && (pubSz != ED25519_KEY_SIZE
  13620. || XMEMCMP(key.p, pub, pubSz) != 0)) {
  13621. ret = SSL_FATAL_ERROR;
  13622. }
  13623. if (ret == 0) {
  13624. ret = wc_ed25519_export_public(NULL, pub, &pubSz);
  13625. if (ret == BAD_FUNC_ARG) {
  13626. ret = wc_ed25519_export_public(&key, NULL, &pubSz);
  13627. }
  13628. if (ret == BAD_FUNC_ARG) {
  13629. ret = wc_ed25519_export_public(&key, pub, NULL);
  13630. }
  13631. if (ret == BAD_FUNC_ARG) {
  13632. ret = 0;
  13633. } else if (ret == 0) {
  13634. ret = SSL_FATAL_ERROR;
  13635. }
  13636. }
  13637. }
  13638. printf(resultFmt, ret == 0 ? passed : failed);
  13639. printf(testingFmt, "wc_ed25519_export_private_only()");
  13640. if (ret == 0) {
  13641. ret = wc_ed25519_export_private_only(&key, priv, &privSz);
  13642. if (ret == 0 && (privSz != ED25519_KEY_SIZE
  13643. || XMEMCMP(key.k, priv, privSz) != 0)) {
  13644. ret = SSL_FATAL_ERROR;
  13645. }
  13646. if (ret == 0) {
  13647. ret = wc_ed25519_export_private_only(NULL, priv, &privSz);
  13648. if (ret == BAD_FUNC_ARG) {
  13649. ret = wc_ed25519_export_private_only(&key, NULL, &privSz);
  13650. }
  13651. if (ret == BAD_FUNC_ARG) {
  13652. ret = wc_ed25519_export_private_only(&key, priv, NULL);
  13653. }
  13654. if (ret == BAD_FUNC_ARG) {
  13655. ret = 0;
  13656. } else if (ret == 0) {
  13657. ret = SSL_FATAL_ERROR;
  13658. }
  13659. }
  13660. }
  13661. printf(resultFmt, ret == 0 ? passed : failed);
  13662. if (wc_FreeRng(&rng) && ret == 0) {
  13663. ret = SSL_FATAL_ERROR;
  13664. }
  13665. wc_ed25519_free(&key);
  13666. #endif
  13667. return ret;
  13668. } /* END test_wc_ed25519_export */
  13669. /*
  13670. * Testing wc_ed25519_size()
  13671. */
  13672. static int test_wc_ed25519_size (void)
  13673. {
  13674. int ret = 0;
  13675. #if defined(HAVE_ED25519)
  13676. WC_RNG rng;
  13677. ed25519_key key;
  13678. ret = wc_InitRng(&rng);
  13679. if (ret != 0) {
  13680. return ret;
  13681. }
  13682. ret = wc_ed25519_init(&key);
  13683. if (ret != 0) {
  13684. wc_FreeRng(&rng);
  13685. return ret;
  13686. }
  13687. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  13688. if (ret != 0) {
  13689. wc_FreeRng(&rng);
  13690. wc_ed25519_free(&key);
  13691. return ret;
  13692. }
  13693. printf(testingFmt, "wc_ed25519_size()");
  13694. ret = wc_ed25519_size(&key);
  13695. /* Test bad args. */
  13696. if (ret == ED25519_KEY_SIZE) {
  13697. ret = wc_ed25519_size(NULL);
  13698. if (ret == BAD_FUNC_ARG) {
  13699. ret = 0;
  13700. }
  13701. }
  13702. printf(resultFmt, ret == 0 ? passed : failed);
  13703. if (ret == 0) {
  13704. printf(testingFmt, "wc_ed25519_sig_size()");
  13705. ret = wc_ed25519_sig_size(&key);
  13706. if (ret == ED25519_SIG_SIZE) {
  13707. ret = 0;
  13708. }
  13709. /* Test bad args. */
  13710. if (ret == 0) {
  13711. ret = wc_ed25519_sig_size(NULL);
  13712. if (ret == BAD_FUNC_ARG) {
  13713. ret = 0;
  13714. }
  13715. }
  13716. printf(resultFmt, ret == 0 ? passed : failed);
  13717. } /* END wc_ed25519_sig_size() */
  13718. if (ret == 0) {
  13719. printf(testingFmt, "wc_ed25519_pub_size");
  13720. ret = wc_ed25519_pub_size(&key);
  13721. if (ret == ED25519_PUB_KEY_SIZE) {
  13722. ret = 0;
  13723. }
  13724. if (ret == 0) {
  13725. ret = wc_ed25519_pub_size(NULL);
  13726. if (ret == BAD_FUNC_ARG) {
  13727. ret = 0;
  13728. }
  13729. }
  13730. printf(resultFmt, ret == 0 ? passed : failed);
  13731. } /* END wc_ed25519_pub_size */
  13732. if (ret == 0) {
  13733. printf(testingFmt, "wc_ed25519_priv_size");
  13734. ret = wc_ed25519_priv_size(&key);
  13735. if (ret == ED25519_PRV_KEY_SIZE) {
  13736. ret = 0;
  13737. }
  13738. if (ret == 0) {
  13739. ret = wc_ed25519_priv_size(NULL);
  13740. if (ret == BAD_FUNC_ARG) {
  13741. ret = 0;
  13742. }
  13743. }
  13744. printf(resultFmt, ret == 0 ? passed : failed);
  13745. } /* END wc_ed25519_pub_size */
  13746. if (wc_FreeRng(&rng) && ret == 0) {
  13747. ret = SSL_FATAL_ERROR;
  13748. }
  13749. wc_ed25519_free(&key);
  13750. #endif
  13751. return ret;
  13752. } /* END test_wc_ed25519_size */
  13753. /*
  13754. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  13755. */
  13756. static int test_wc_ed25519_exportKey (void)
  13757. {
  13758. int ret = 0;
  13759. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  13760. WC_RNG rng;
  13761. ed25519_key key;
  13762. byte priv[ED25519_PRV_KEY_SIZE];
  13763. byte pub[ED25519_PUB_KEY_SIZE];
  13764. byte privOnly[ED25519_PRV_KEY_SIZE];
  13765. word32 privSz = sizeof(priv);
  13766. word32 pubSz = sizeof(pub);
  13767. word32 privOnlySz = sizeof(privOnly);
  13768. ret = wc_InitRng(&rng);
  13769. if (ret != 0) {
  13770. return ret;
  13771. }
  13772. ret = wc_ed25519_init(&key);
  13773. if (ret != 0) {
  13774. wc_FreeRng(&rng);
  13775. return ret;
  13776. }
  13777. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  13778. if (ret != 0) {
  13779. wc_FreeRng(&rng);
  13780. wc_ed25519_free(&key);
  13781. return ret;
  13782. }
  13783. printf(testingFmt, "wc_ed25519_export_private()");
  13784. ret = wc_ed25519_export_private(&key, privOnly, &privOnlySz);
  13785. if (ret == 0) {
  13786. ret = wc_ed25519_export_private(NULL, privOnly, &privOnlySz);
  13787. if (ret == BAD_FUNC_ARG) {
  13788. ret = wc_ed25519_export_private(&key, NULL, &privOnlySz);
  13789. }
  13790. if (ret == BAD_FUNC_ARG) {
  13791. ret = wc_ed25519_export_private(&key, privOnly, NULL);
  13792. }
  13793. if (ret == BAD_FUNC_ARG) {
  13794. ret = 0;
  13795. } else if (ret == 0) {
  13796. ret = SSL_FATAL_ERROR;
  13797. }
  13798. }
  13799. printf(resultFmt, ret == 0 ? passed : failed);
  13800. if (ret == 0) {
  13801. printf(testingFmt, "wc_ed25519_export_key()");
  13802. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz);
  13803. if (ret == 0) {
  13804. ret = wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz);
  13805. if (ret == BAD_FUNC_ARG) {
  13806. ret = wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz);
  13807. }
  13808. if (ret == BAD_FUNC_ARG) {
  13809. ret = wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz);
  13810. }
  13811. if (ret == BAD_FUNC_ARG) {
  13812. ret = wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz);
  13813. }
  13814. if (ret == BAD_FUNC_ARG) {
  13815. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, NULL);
  13816. }
  13817. if (ret == BAD_FUNC_ARG) {
  13818. ret = 0;
  13819. } else if (ret == 0) {
  13820. ret = SSL_FATAL_ERROR;
  13821. }
  13822. }
  13823. printf(resultFmt, ret == 0 ? passed : failed);
  13824. } /* END wc_ed25519_export_key() */
  13825. /* Cross check output. */
  13826. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  13827. ret = SSL_FATAL_ERROR;
  13828. }
  13829. if (wc_FreeRng(&rng) && ret == 0) {
  13830. ret = SSL_FATAL_ERROR;
  13831. }
  13832. wc_ed25519_free(&key);
  13833. #endif
  13834. return ret;
  13835. } /* END test_wc_ed25519_exportKey */
  13836. /*
  13837. * Testing wc_Ed25519PublicKeyToDer
  13838. */
  13839. static int test_wc_Ed25519PublicKeyToDer (void)
  13840. {
  13841. int ret = 0;
  13842. #if defined(HAVE_ED25519) && (defined(WOLFSSL_CERT_GEN) || \
  13843. defined(WOLFSSL_KEY_GEN))
  13844. int tmp;
  13845. ed25519_key key;
  13846. byte derBuf[1024];
  13847. printf(testingFmt, "wc_Ed25519PublicKeyToDer()");
  13848. /* Test bad args */
  13849. tmp = wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0);
  13850. if (tmp != BAD_FUNC_ARG) {
  13851. ret = SSL_FATAL_ERROR;
  13852. }
  13853. if (ret == 0) {
  13854. wc_ed25519_init(&key);
  13855. tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0);
  13856. if (tmp != BUFFER_E) {
  13857. ret = SSL_FATAL_ERROR;
  13858. }
  13859. wc_ed25519_free(&key);
  13860. }
  13861. /* Test good args */
  13862. if (ret == 0) {
  13863. WC_RNG rng;
  13864. ret = wc_InitRng(&rng);
  13865. if (ret != 0) {
  13866. return ret;
  13867. }
  13868. ret = wc_ed25519_init(&key);
  13869. if (ret != 0) {
  13870. wc_FreeRng(&rng);
  13871. return ret;
  13872. }
  13873. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  13874. if (ret != 0) {
  13875. wc_FreeRng(&rng);
  13876. wc_ed25519_free(&key);
  13877. return ret;
  13878. }
  13879. tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1);
  13880. if (tmp <= 0) {
  13881. ret = SSL_FATAL_ERROR;
  13882. }
  13883. wc_FreeRng(&rng);
  13884. wc_ed25519_free(&key);
  13885. }
  13886. printf(resultFmt, ret == 0 ? passed : failed);
  13887. #endif
  13888. return ret;
  13889. } /* END testing wc_Ed25519PublicKeyToDer */
  13890. /*
  13891. * Testing wc_curve25519_init and wc_curve25519_free.
  13892. */
  13893. static int test_wc_curve25519_init (void)
  13894. {
  13895. int ret = 0;
  13896. #if defined(HAVE_CURVE25519)
  13897. curve25519_key key;
  13898. printf(testingFmt, "wc_curve25519_init()");
  13899. ret = wc_curve25519_init(&key);
  13900. /* Test bad args for wc_curve25519_init */
  13901. if (ret == 0) {
  13902. ret = wc_curve25519_init(NULL);
  13903. if (ret == BAD_FUNC_ARG) {
  13904. ret = 0;
  13905. } else if (ret == 0) {
  13906. ret = SSL_FATAL_ERROR;
  13907. }
  13908. }
  13909. printf(resultFmt, ret == 0 ? passed : failed);
  13910. /* Test good args for wc_curve_25519_free */
  13911. wc_curve25519_free(&key);
  13912. wc_curve25519_free(NULL);
  13913. #endif
  13914. return ret;
  13915. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  13916. /*
  13917. * Testing test_wc_curve25519_size.
  13918. */
  13919. static int test_wc_curve25519_size (void)
  13920. {
  13921. int ret = 0;
  13922. #if defined(HAVE_CURVE25519)
  13923. curve25519_key key;
  13924. printf(testingFmt, "wc_curve25519_size()");
  13925. ret = wc_curve25519_init(&key);
  13926. /* Test good args for wc_curve25519_size */
  13927. if (ret == 0) {
  13928. ret = wc_curve25519_size(&key);
  13929. }
  13930. /* Test bad args for wc_curve25519_size */
  13931. if (ret != 0) {
  13932. ret = wc_curve25519_size(NULL);
  13933. }
  13934. printf(resultFmt, ret == 0 ? passed : failed);
  13935. wc_curve25519_free(&key);
  13936. #endif
  13937. return ret;
  13938. } /* END test_wc_curve25519_size*/
  13939. /*
  13940. * Testing test_wc_curve25519_export_key_raw().
  13941. */
  13942. static int test_wc_curve25519_export_key_raw (void)
  13943. {
  13944. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  13945. curve25519_key key;
  13946. WC_RNG rng;
  13947. byte privateKey[CURVE25519_KEYSIZE];
  13948. byte publicKey[CURVE25519_KEYSIZE];
  13949. word32 prvkSz;
  13950. word32 pubkSz;
  13951. byte prik[CURVE25519_KEYSIZE];
  13952. byte pubk[CURVE25519_KEYSIZE];
  13953. word32 prksz;
  13954. word32 pbksz;
  13955. printf(testingFmt, "wc_curve25519_export_key_raw()");
  13956. if(0 != wc_InitRng(&rng)){
  13957. printf(testingFmt, "failed due to wc_InitRng");
  13958. fflush( stdout );
  13959. return 1;
  13960. }
  13961. if(0 != wc_curve25519_init(&key)){
  13962. printf(testingFmt, "failed due to wc_curve25519_init");
  13963. fflush( stdout );
  13964. wc_FreeRng(&rng);
  13965. return 1;
  13966. }
  13967. if(0 != wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key)){
  13968. printf(testingFmt, "failed due to wc_curve25519_make_key");
  13969. fflush( stdout );
  13970. wc_curve25519_free(&key);
  13971. wc_FreeRng(&rng);
  13972. return 1;
  13973. }
  13974. /*
  13975. bad-argument-test cases
  13976. target function sould return BAD_FUNC_ARG
  13977. */
  13978. prvkSz = CURVE25519_KEYSIZE;
  13979. pubkSz = CURVE25519_KEYSIZE;
  13980. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  13981. NULL , privateKey, &prvkSz, publicKey, &pubkSz)){
  13982. printf(testingFmt,"failed at bad-arg-case-1.");
  13983. fflush( stdout );
  13984. wc_curve25519_free(&key);
  13985. wc_FreeRng(&rng);
  13986. return 1;
  13987. }
  13988. prvkSz = CURVE25519_KEYSIZE;
  13989. pubkSz = CURVE25519_KEYSIZE;
  13990. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  13991. &key , NULL, &prvkSz, publicKey, &pubkSz)){
  13992. printf(testingFmt,"failed at bad-arg-case-2.");
  13993. fflush( stdout );
  13994. wc_curve25519_free(&key);
  13995. wc_FreeRng(&rng);
  13996. return 1;
  13997. }
  13998. prvkSz = CURVE25519_KEYSIZE;
  13999. pubkSz = CURVE25519_KEYSIZE;
  14000. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  14001. &key , privateKey, NULL, publicKey, &pubkSz)){
  14002. printf(testingFmt,"failed at bad-arg-case-3.");
  14003. fflush( stdout );
  14004. wc_curve25519_free(&key);
  14005. wc_FreeRng(&rng);
  14006. return 1;
  14007. }
  14008. prvkSz = CURVE25519_KEYSIZE;
  14009. pubkSz = CURVE25519_KEYSIZE;
  14010. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  14011. &key , privateKey, &prvkSz, NULL, &pubkSz)){
  14012. printf(testingFmt,"failed at bad-arg-case-4.");
  14013. fflush( stdout );
  14014. wc_curve25519_free(&key);
  14015. wc_FreeRng(&rng);
  14016. return 1;
  14017. }
  14018. prvkSz = CURVE25519_KEYSIZE;
  14019. pubkSz = CURVE25519_KEYSIZE;
  14020. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  14021. &key , privateKey, &prvkSz, publicKey, NULL )){
  14022. printf(testingFmt,"failed at bad-arg-case-5.");
  14023. fflush( stdout );
  14024. wc_curve25519_free(&key);
  14025. wc_FreeRng(&rng);
  14026. return 1;
  14027. }
  14028. /*
  14029. cross-testing
  14030. */
  14031. prksz = CURVE25519_KEYSIZE;
  14032. if( 0 != wc_curve25519_export_private_raw(&key, prik, &prksz)){
  14033. printf(testingFmt,"failed due to wc_curve25519_export_private_raw");
  14034. fflush( stdout );
  14035. wc_curve25519_free(&key);
  14036. wc_FreeRng(&rng);
  14037. return 1;
  14038. }
  14039. pbksz = CURVE25519_KEYSIZE;
  14040. if(0 != wc_curve25519_export_public(&key, pubk, &pbksz)){
  14041. printf(testingFmt,"failed due to wc_curve25519_export_public");
  14042. fflush( stdout );
  14043. wc_curve25519_free(&key);
  14044. wc_FreeRng(&rng);
  14045. return 1;
  14046. }
  14047. prvkSz = CURVE25519_KEYSIZE;
  14048. pubkSz = CURVE25519_KEYSIZE;
  14049. if(0 != wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  14050. publicKey, &pubkSz)){
  14051. printf(testingFmt,"failed due to wc_curve25519_export_key_raw");
  14052. fflush( stdout );
  14053. wc_curve25519_free(&key);
  14054. wc_FreeRng(&rng);
  14055. return 1;
  14056. }
  14057. if((prksz == CURVE25519_KEYSIZE) &&
  14058. (pbksz == CURVE25519_KEYSIZE) &&
  14059. (prvkSz == CURVE25519_KEYSIZE) &&
  14060. (pubkSz == CURVE25519_KEYSIZE)){
  14061. if( 0 == XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE) &&
  14062. 0 == XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE)){
  14063. printf(resultFmt,passed);
  14064. fflush( stdout );
  14065. wc_curve25519_free(&key);
  14066. wc_FreeRng(&rng);
  14067. return 0;
  14068. }
  14069. else{
  14070. printf(testingFmt,"failed due to key-contents-inconsistency.");
  14071. fflush( stdout );
  14072. wc_curve25519_free(&key);
  14073. wc_FreeRng(&rng);
  14074. return 1;
  14075. }
  14076. }
  14077. else{
  14078. printf(testingFmt,"failed due to bad-key-size.");
  14079. fflush( stdout );
  14080. wc_curve25519_free(&key);
  14081. wc_FreeRng(&rng);
  14082. return 1;
  14083. }
  14084. #endif
  14085. fflush( stdout );
  14086. return 0;
  14087. } /* end of test_wc_curve25519_export_key_raw */
  14088. /*
  14089. * Testing test_wc_curve25519_export_key_raw_ex().
  14090. */
  14091. static int test_wc_curve25519_export_key_raw_ex (void)
  14092. {
  14093. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  14094. curve25519_key key;
  14095. WC_RNG rng;
  14096. byte privateKey[CURVE25519_KEYSIZE];
  14097. byte publicKey[CURVE25519_KEYSIZE];
  14098. word32 prvkSz;
  14099. word32 pubkSz;
  14100. byte prik[CURVE25519_KEYSIZE];
  14101. byte pubk[CURVE25519_KEYSIZE];
  14102. word32 prksz;
  14103. word32 pbksz;
  14104. printf(testingFmt, "wc_curve25519_export_key_raw_ex()");
  14105. if(0 != wc_InitRng(&rng)){
  14106. printf(testingFmt, "failed due to wc_InitRng");
  14107. fflush( stdout );
  14108. return 1;
  14109. }
  14110. if(0 != wc_curve25519_init(&key)){
  14111. printf(testingFmt, "failed due to wc_curve25519_init");
  14112. fflush( stdout );
  14113. wc_FreeRng(&rng);
  14114. return 1;
  14115. }
  14116. if(0 != wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key)){
  14117. printf(testingFmt, "failed due to wc_curve25519_make_key");
  14118. fflush( stdout );
  14119. wc_curve25519_free(&key);
  14120. wc_FreeRng(&rng);
  14121. return 1;
  14122. }
  14123. /*
  14124. bad-argument-test cases
  14125. target function sould return BAD_FUNC_ARG
  14126. */
  14127. prvkSz = CURVE25519_KEYSIZE;
  14128. pubkSz = CURVE25519_KEYSIZE;
  14129. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL , privateKey,
  14130. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
  14131. printf(testingFmt,"failed at bad-arg-case-1.");
  14132. fflush( stdout );
  14133. wc_curve25519_free(&key);
  14134. wc_FreeRng(&rng);
  14135. return 1;
  14136. }
  14137. prvkSz = CURVE25519_KEYSIZE;
  14138. pubkSz = CURVE25519_KEYSIZE;
  14139. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key , NULL,
  14140. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
  14141. printf(testingFmt,"failed at bad-arg-case-2.");
  14142. fflush( stdout );
  14143. wc_curve25519_free(&key);
  14144. wc_FreeRng(&rng);
  14145. return 1;
  14146. }
  14147. prvkSz = CURVE25519_KEYSIZE;
  14148. pubkSz = CURVE25519_KEYSIZE;
  14149. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key,privateKey,
  14150. NULL,publicKey, &pubkSz,EC25519_LITTLE_ENDIAN)){
  14151. printf(testingFmt,"failed at bad-arg-case-3.");
  14152. fflush( stdout );
  14153. wc_curve25519_free(&key);
  14154. wc_FreeRng(&rng);
  14155. return 1;
  14156. }
  14157. prvkSz = CURVE25519_KEYSIZE;
  14158. pubkSz = CURVE25519_KEYSIZE;
  14159. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  14160. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN)){
  14161. printf(testingFmt,"failed at bad-arg-case-4.");
  14162. fflush( stdout );
  14163. wc_curve25519_free(&key);
  14164. wc_FreeRng(&rng);
  14165. return 1;
  14166. }
  14167. prvkSz = CURVE25519_KEYSIZE;
  14168. pubkSz = CURVE25519_KEYSIZE;
  14169. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  14170. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN)){
  14171. printf(testingFmt,"failed at bad-arg-case-5.");
  14172. fflush( stdout );
  14173. wc_curve25519_free(&key);
  14174. wc_FreeRng(&rng);
  14175. return 1;
  14176. }
  14177. prvkSz = CURVE25519_KEYSIZE;
  14178. pubkSz = CURVE25519_KEYSIZE;
  14179. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL, privateKey,
  14180. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  14181. printf(testingFmt,"failed at bad-arg-case-6.");
  14182. fflush( stdout );
  14183. wc_curve25519_free(&key);
  14184. wc_FreeRng(&rng);
  14185. return 1;
  14186. }
  14187. prvkSz = CURVE25519_KEYSIZE;
  14188. pubkSz = CURVE25519_KEYSIZE;
  14189. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, NULL, &prvkSz,
  14190. publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  14191. printf(testingFmt,"failed at bad-arg-case-7.");
  14192. fflush( stdout );
  14193. wc_curve25519_free(&key);
  14194. wc_FreeRng(&rng);
  14195. return 1;
  14196. }
  14197. prvkSz = CURVE25519_KEYSIZE;
  14198. pubkSz = CURVE25519_KEYSIZE;
  14199. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  14200. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  14201. printf(testingFmt,"failed at bad-arg-case-8.");
  14202. fflush( stdout );
  14203. wc_curve25519_free(&key);
  14204. wc_FreeRng(&rng);
  14205. return 1;
  14206. }
  14207. prvkSz = CURVE25519_KEYSIZE;
  14208. pubkSz = CURVE25519_KEYSIZE;
  14209. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  14210. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN)){
  14211. printf(testingFmt,"failed at bad-arg-case-9.");
  14212. fflush( stdout );
  14213. wc_curve25519_free(&key);
  14214. wc_FreeRng(&rng);
  14215. return 1;
  14216. }
  14217. prvkSz = CURVE25519_KEYSIZE;
  14218. pubkSz = CURVE25519_KEYSIZE;
  14219. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  14220. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN)){
  14221. printf(testingFmt,"failed at bad-arg-case-10.");
  14222. fflush( stdout );
  14223. wc_curve25519_free(&key);
  14224. wc_FreeRng(&rng);
  14225. return 1;
  14226. }
  14227. /* illegal value for endien */
  14228. prvkSz = CURVE25519_KEYSIZE;
  14229. pubkSz = CURVE25519_KEYSIZE;
  14230. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  14231. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN + 10 )){
  14232. printf(testingFmt,"failed at bad-arg-case-11.");
  14233. fflush( stdout );
  14234. wc_curve25519_free(&key);
  14235. wc_FreeRng(&rng);
  14236. return 1;
  14237. }
  14238. /*
  14239. cross-testing
  14240. */
  14241. prksz = CURVE25519_KEYSIZE;
  14242. if(0 != wc_curve25519_export_private_raw( &key, prik, &prksz )){
  14243. printf(testingFmt,"failed due to wc_curve25519_export_private_raw");
  14244. fflush( stdout );
  14245. wc_curve25519_free(&key);
  14246. wc_FreeRng(&rng);
  14247. return 1;
  14248. }
  14249. pbksz = CURVE25519_KEYSIZE;
  14250. if(0 != wc_curve25519_export_public( &key, pubk, &pbksz )){
  14251. printf(testingFmt,"failed due to wc_curve25519_export_public");
  14252. fflush( stdout );
  14253. wc_curve25519_free(&key);
  14254. wc_FreeRng(&rng);
  14255. return 1;
  14256. }
  14257. prvkSz = CURVE25519_KEYSIZE;
  14258. pubkSz = CURVE25519_KEYSIZE;
  14259. if(0 != wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  14260. publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
  14261. printf(testingFmt,"failed due to wc_curve25519_export_key_raw_ex");
  14262. fflush( stdout );
  14263. wc_curve25519_free(&key);
  14264. wc_FreeRng(&rng);
  14265. return 1;
  14266. }
  14267. if( prksz == CURVE25519_KEYSIZE &&
  14268. pbksz == CURVE25519_KEYSIZE &&
  14269. prvkSz == CURVE25519_KEYSIZE &&
  14270. pubkSz == CURVE25519_KEYSIZE ){
  14271. if( 0 == XMEMCMP( privateKey, prik, CURVE25519_KEYSIZE ) &&
  14272. 0 == XMEMCMP( publicKey, pubk, CURVE25519_KEYSIZE )){
  14273. if( 0 == wc_curve25519_export_key_raw_ex( &key, privateKey,
  14274. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
  14275. if( prvkSz == CURVE25519_KEYSIZE &&
  14276. pubkSz == CURVE25519_KEYSIZE ){
  14277. ; /* proceed to the next test */
  14278. }
  14279. else{
  14280. printf(testingFmt,"failed due to key-size-inconsistency");
  14281. fflush( stdout );
  14282. wc_curve25519_free(&key);
  14283. wc_FreeRng(&rng);
  14284. return 1;
  14285. }
  14286. }
  14287. else{
  14288. printf(testingFmt,
  14289. "failed due to wc_curve25519_export_key_raw_ex");
  14290. fflush( stdout );
  14291. wc_curve25519_free(&key);
  14292. wc_FreeRng(&rng);
  14293. return 1;
  14294. }
  14295. }
  14296. else{
  14297. printf(testingFmt,"failed due to key-contents-inconsistency");
  14298. fflush( stdout );
  14299. wc_curve25519_free(&key);
  14300. wc_FreeRng(&rng);
  14301. return 1;
  14302. }
  14303. }
  14304. else{
  14305. printf(testingFmt,"failed due to bad-key-size");
  14306. fflush( stdout );
  14307. wc_curve25519_free(&key);
  14308. wc_FreeRng(&rng);
  14309. return 1;
  14310. }
  14311. /*
  14312. try once with another endian
  14313. */
  14314. prvkSz = CURVE25519_KEYSIZE;
  14315. pubkSz = CURVE25519_KEYSIZE;
  14316. if( 0 == wc_curve25519_export_key_raw_ex( &key, privateKey,
  14317. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  14318. if( prvkSz == CURVE25519_KEYSIZE &&
  14319. pubkSz == CURVE25519_KEYSIZE ){
  14320. /* no more test*/
  14321. printf(resultFmt, passed );
  14322. fflush( stdout );
  14323. wc_curve25519_free(&key);
  14324. wc_FreeRng(&rng);
  14325. return 0;
  14326. }
  14327. else{
  14328. printf(testingFmt,"failed due to key-size-inconsistency");
  14329. fflush( stdout );
  14330. wc_curve25519_free(&key);
  14331. wc_FreeRng(&rng);
  14332. return 1;
  14333. }
  14334. }
  14335. else{
  14336. printf(testingFmt,
  14337. "failed due to wc_curve25519_export_key_raw_ex(BIGENDIAN)");
  14338. fflush( stdout );
  14339. wc_curve25519_free(&key);
  14340. wc_FreeRng(&rng);
  14341. return 1;
  14342. }
  14343. #endif
  14344. return 0;
  14345. } /* end of test_wc_curve25519_export_key_raw_ex */
  14346. /*
  14347. * Testing wc_curve25519_make_key
  14348. */
  14349. static int test_wc_curve25519_make_key (void)
  14350. {
  14351. int ret = 0;
  14352. #if defined(HAVE_CURVE25519)
  14353. WC_RNG rng;
  14354. curve25519_key key;
  14355. int keysize;
  14356. printf(testingFmt, "wc_curve25519_make_key()");
  14357. ret = wc_curve25519_init(&key);
  14358. if (ret == 0) {
  14359. ret = wc_InitRng(&rng);
  14360. }
  14361. if (ret == 0) {
  14362. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  14363. if (ret == 0) {
  14364. keysize = wc_curve25519_size(&key);
  14365. if (keysize != CURVE25519_KEYSIZE) {
  14366. ret = SSL_FATAL_ERROR;
  14367. }
  14368. }
  14369. if (ret == 0) {
  14370. ret = wc_curve25519_make_key(&rng, keysize, &key);
  14371. }
  14372. }
  14373. /*test bad cases*/
  14374. if (ret == 0) {
  14375. ret = wc_curve25519_make_key(NULL, 0, NULL);
  14376. if (ret == BAD_FUNC_ARG) {
  14377. ret = 0;
  14378. }
  14379. }
  14380. if (ret == 0) {
  14381. ret = wc_curve25519_make_key(&rng, keysize, NULL);
  14382. if (ret == BAD_FUNC_ARG) {
  14383. ret = 0;
  14384. }
  14385. }
  14386. if (ret == 0) {
  14387. ret = wc_curve25519_make_key(NULL, keysize, &key);
  14388. if (ret == BAD_FUNC_ARG) {
  14389. ret = 0;
  14390. }
  14391. }
  14392. if (ret == 0) {
  14393. ret = wc_curve25519_make_key(&rng, 0, &key);
  14394. if (ret == ECC_BAD_ARG_E) {
  14395. ret = 0;
  14396. }
  14397. }
  14398. printf(resultFmt, ret == 0 ? passed : failed);
  14399. wc_curve25519_free(&key);
  14400. wc_FreeRng(&rng);
  14401. #endif
  14402. return ret;
  14403. } /*END test_wc_curve25519_make_key*/
  14404. /*
  14405. * Testing wc_curve25519_shared_secret_ex
  14406. */
  14407. static int test_wc_curve25519_shared_secret_ex (void)
  14408. {
  14409. int ret = 0;
  14410. #if defined(HAVE_CURVE25519)
  14411. WC_RNG rng;
  14412. curve25519_key private_key, public_key;
  14413. byte out[CURVE25519_KEYSIZE];
  14414. word32 outLen = sizeof(out);
  14415. int endian = EC25519_BIG_ENDIAN;
  14416. printf(testingFmt, "wc_curve25519_shared_secret_ex()");
  14417. ret = wc_curve25519_init(&private_key);
  14418. if (ret == 0) {
  14419. ret = wc_InitRng(&rng);
  14420. if (ret == 0) {
  14421. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key);
  14422. }
  14423. }
  14424. if (ret == 0) {
  14425. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key);
  14426. }
  14427. if (ret == 0) {
  14428. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  14429. &outLen, endian);
  14430. }
  14431. /*test bad cases*/
  14432. if (ret == 0) {
  14433. ret = wc_curve25519_shared_secret_ex(NULL, NULL, NULL,
  14434. 0, endian);
  14435. if (ret == BAD_FUNC_ARG) {
  14436. ret = 0;
  14437. }
  14438. }
  14439. if (ret == 0) {
  14440. ret = wc_curve25519_shared_secret_ex(NULL, &public_key, out,
  14441. &outLen, endian);
  14442. if (ret == BAD_FUNC_ARG) {
  14443. ret = 0;
  14444. }
  14445. }
  14446. if (ret == 0) {
  14447. ret = wc_curve25519_shared_secret_ex(&private_key, NULL, out,
  14448. &outLen, endian);
  14449. if (ret == BAD_FUNC_ARG) {
  14450. ret = 0;
  14451. }
  14452. }
  14453. if (ret == 0) {
  14454. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  14455. &outLen, endian);
  14456. if (ret == BAD_FUNC_ARG) {
  14457. ret = 0;
  14458. }
  14459. }
  14460. if (ret == 0) {
  14461. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  14462. NULL, endian);
  14463. if (ret == BAD_FUNC_ARG) {
  14464. ret = 0;
  14465. }
  14466. }
  14467. if (ret == 0) {
  14468. /*curve25519.c is checking for public_key size less than or equal to 0x7f,
  14469. *increasing to 0x8f checks for error being returned*/
  14470. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  14471. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  14472. &outLen, endian);
  14473. if (ret == ECC_BAD_ARG_E) {
  14474. ret = 0;
  14475. }
  14476. }
  14477. outLen = outLen - 2;
  14478. if (ret == 0) {
  14479. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  14480. &outLen, endian);
  14481. if (ret == BAD_FUNC_ARG) {
  14482. ret = 0;
  14483. }
  14484. }
  14485. printf(resultFmt, ret == 0 ? passed : failed);
  14486. wc_curve25519_free(&private_key);
  14487. wc_curve25519_free(&public_key);
  14488. wc_FreeRng(&rng);
  14489. #endif
  14490. return ret;
  14491. } /*END test_wc_curve25519_shared_secret_ex*/
  14492. /*
  14493. * Testing wc_curve25519_make_pub
  14494. */
  14495. static int test_wc_curve25519_make_pub (void)
  14496. {
  14497. int ret = 0;
  14498. #if defined(HAVE_CURVE25519)
  14499. WC_RNG rng;
  14500. curve25519_key key;
  14501. byte out[CURVE25519_KEYSIZE];
  14502. printf(testingFmt, "wc_curve25519_make_pub()");
  14503. ret = wc_curve25519_init(&key);
  14504. if (ret == 0) {
  14505. ret = wc_InitRng(&rng);
  14506. if (ret == 0) {
  14507. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  14508. }
  14509. }
  14510. if (ret == 0) {
  14511. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof key.k.point, key.k.point);
  14512. }
  14513. /*test bad cases*/
  14514. if (ret == 0) {
  14515. ret = wc_curve25519_make_pub((int)sizeof key.k.point - 1, key.k.point, (int)sizeof out, out);
  14516. if (ret == ECC_BAD_ARG_E) {
  14517. ret = 0;
  14518. }
  14519. }
  14520. if (ret == 0) {
  14521. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof key.k.point, NULL);
  14522. if (ret == ECC_BAD_ARG_E) {
  14523. ret = 0;
  14524. }
  14525. }
  14526. if (ret == 0) {
  14527. ret = wc_curve25519_make_pub((int)sizeof out - 1, out, (int)sizeof key.k.point, key.k.point);
  14528. if (ret == ECC_BAD_ARG_E) {
  14529. ret = 0;
  14530. }
  14531. }
  14532. if (ret == 0) {
  14533. ret = wc_curve25519_make_pub((int)sizeof out, NULL, (int)sizeof key.k.point, key.k.point);
  14534. if (ret == ECC_BAD_ARG_E) {
  14535. ret = 0;
  14536. }
  14537. }
  14538. if (ret == 0) {
  14539. /* verify clamping test */
  14540. key.k.point[0] |= ~248;
  14541. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof key.k.point, key.k.point);
  14542. if (ret == ECC_BAD_ARG_E) {
  14543. ret = 0;
  14544. }
  14545. key.k.point[0] &= 248;
  14546. }
  14547. /* repeat the expected-to-succeed test. */
  14548. if (ret == 0) {
  14549. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof key.k.point, key.k.point);
  14550. }
  14551. printf(resultFmt, ret == 0 ? passed : failed);
  14552. wc_curve25519_free(&key);
  14553. wc_FreeRng(&rng);
  14554. #endif
  14555. return ret;
  14556. } /*END test_wc_curve25519_make_pub */
  14557. /*
  14558. * Testing test_wc_curve25519_export_public_ex
  14559. */
  14560. static int test_wc_curve25519_export_public_ex (void)
  14561. {
  14562. int ret = 0;
  14563. #if defined(HAVE_CURVE25519)
  14564. WC_RNG rng;
  14565. curve25519_key key;
  14566. byte out[CURVE25519_KEYSIZE];
  14567. word32 outLen = sizeof(out);
  14568. int endian = EC25519_BIG_ENDIAN;
  14569. printf(testingFmt, "wc_curve25519_export_public_ex()");
  14570. ret = wc_curve25519_init(&key);
  14571. if (ret == 0) {
  14572. ret = wc_InitRng(&rng);
  14573. }
  14574. if (ret == 0) {
  14575. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  14576. if (ret == 0) {
  14577. ret = wc_curve25519_export_public(&key, out, &outLen);
  14578. }
  14579. if (ret == 0) {
  14580. ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
  14581. }
  14582. }
  14583. /*test bad cases*/
  14584. if (ret == 0) {
  14585. ret = wc_curve25519_export_public_ex(NULL, NULL, NULL, endian);
  14586. if (ret == BAD_FUNC_ARG) {
  14587. ret = 0;
  14588. }
  14589. }
  14590. if (ret == 0) {
  14591. ret = wc_curve25519_export_public_ex(NULL, out, &outLen, endian);
  14592. if (ret == BAD_FUNC_ARG) {
  14593. ret = 0;
  14594. }
  14595. }
  14596. if (ret == 0) {
  14597. ret = wc_curve25519_export_public_ex(&key, NULL, &outLen, endian);
  14598. if (ret == BAD_FUNC_ARG) {
  14599. ret = 0;
  14600. }
  14601. }
  14602. if (ret == 0) {
  14603. ret = wc_curve25519_export_public_ex(&key, out, NULL, endian);
  14604. if (ret == BAD_FUNC_ARG) {
  14605. ret = 0;
  14606. }
  14607. }
  14608. outLen = outLen - 2;
  14609. if (ret == 0) {
  14610. ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
  14611. if (ret == ECC_BAD_ARG_E) {
  14612. ret = 0;
  14613. }
  14614. }
  14615. printf(resultFmt, ret == 0 ? passed : failed);
  14616. wc_curve25519_free(&key);
  14617. wc_FreeRng(&rng);
  14618. #endif
  14619. return ret;
  14620. } /*END test_wc_curve25519_export_public_ex*/
  14621. /*
  14622. * Testing test_wc_curve25519_import_private_raw_ex
  14623. */
  14624. static int test_wc_curve25519_import_private_raw_ex (void)
  14625. {
  14626. int ret = 0;
  14627. #if defined(HAVE_CURVE25519)
  14628. WC_RNG rng;
  14629. curve25519_key key;
  14630. byte priv[CURVE25519_KEYSIZE];
  14631. byte pub[CURVE25519_KEYSIZE];
  14632. word32 privSz = sizeof(priv);
  14633. word32 pubSz = sizeof(pub);
  14634. int endian = EC25519_BIG_ENDIAN;
  14635. printf(testingFmt, "wc_curve25519_import_private_raw_ex()");
  14636. ret = wc_curve25519_init(&key);
  14637. if (ret == 0) {
  14638. ret = wc_InitRng(&rng);
  14639. }
  14640. if (ret == 0) {
  14641. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  14642. if (ret == 0) {
  14643. ret = wc_curve25519_export_private_raw_ex(&key, priv, &privSz, endian);
  14644. }
  14645. if (ret == 0) {
  14646. ret = wc_curve25519_export_public(&key, pub, &pubSz);
  14647. }
  14648. if (ret == 0) {
  14649. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  14650. &key, endian);
  14651. }
  14652. }
  14653. /*test bad cases*/
  14654. if (ret == 0) {
  14655. ret = wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  14656. endian);
  14657. if (ret == BAD_FUNC_ARG) {
  14658. ret = 0;
  14659. }
  14660. }
  14661. if (ret == 0) {
  14662. ret = wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  14663. &key, endian);
  14664. if (ret == BAD_FUNC_ARG) {
  14665. ret = 0;
  14666. }
  14667. }
  14668. if (ret == 0) {
  14669. ret = wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  14670. &key, endian);
  14671. if (ret == BAD_FUNC_ARG) {
  14672. ret = 0;
  14673. }
  14674. }
  14675. if (ret == 0) {
  14676. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  14677. NULL, endian);
  14678. if (ret == BAD_FUNC_ARG) {
  14679. ret = 0;
  14680. }
  14681. }
  14682. if (ret == 0) {
  14683. ret = wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  14684. &key, endian);
  14685. if (ret == ECC_BAD_ARG_E) {
  14686. ret = 0;
  14687. }
  14688. }
  14689. if (ret == 0) {
  14690. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  14691. &key, endian);
  14692. if (ret == ECC_BAD_ARG_E) {
  14693. ret = 0;
  14694. }
  14695. }
  14696. if (ret == 0) {
  14697. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  14698. &key, EC25519_LITTLE_ENDIAN);
  14699. }
  14700. printf(resultFmt, ret == 0 ? passed : failed);
  14701. wc_curve25519_free(&key);
  14702. wc_FreeRng(&rng);
  14703. #endif
  14704. return ret;
  14705. } /*END test_wc_curve25519_import_private_raw_ex*/
  14706. /*
  14707. * Testing test_wc_curve25519_import_private
  14708. */
  14709. static int test_wc_curve25519_import_private (void)
  14710. {
  14711. int ret = 0;
  14712. #if defined(HAVE_CURVE25519)
  14713. curve25519_key key;
  14714. WC_RNG rng;
  14715. byte priv[CURVE25519_KEYSIZE];
  14716. word32 privSz = sizeof(priv);
  14717. printf(testingFmt, "wc_curve25519_import_private()");
  14718. ret = wc_curve25519_init(&key);
  14719. if (ret == 0) {
  14720. ret = wc_InitRng(&rng);
  14721. }
  14722. if (ret == 0) {
  14723. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  14724. if (ret == 0) {
  14725. ret = wc_curve25519_export_private_raw(&key, priv, &privSz);
  14726. }
  14727. }
  14728. if (ret == 0) {
  14729. ret = wc_curve25519_import_private(priv, privSz, &key);
  14730. }
  14731. printf(resultFmt, ret == 0 ? passed : failed);
  14732. wc_curve25519_free(&key);
  14733. wc_FreeRng(&rng);
  14734. #endif
  14735. return ret;
  14736. } /*END test_wc_curve25519_import*/
  14737. /*
  14738. * Testing test_wc_curve25519_export_private_raw_ex
  14739. */
  14740. static int test_wc_curve25519_export_private_raw_ex (void)
  14741. {
  14742. int ret = 0;
  14743. #if defined(HAVE_CURVE25519)
  14744. WC_RNG rng;
  14745. curve25519_key key;
  14746. byte out[CURVE25519_KEYSIZE];
  14747. word32 outLen = sizeof(out);
  14748. int endian = EC25519_BIG_ENDIAN;
  14749. printf(testingFmt, "wc_curve25519_export_private_raw_ex()");
  14750. ret = wc_curve25519_init(&key);
  14751. if (ret == 0) {
  14752. ret = wc_InitRng(&rng);
  14753. }
  14754. if (ret == 0) {
  14755. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
  14756. }
  14757. /*test bad cases*/
  14758. if (ret == 0) {
  14759. ret = wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian);
  14760. if (ret == BAD_FUNC_ARG) {
  14761. ret = 0;
  14762. }
  14763. }
  14764. if (ret == 0) {
  14765. ret = wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian);
  14766. if (ret == BAD_FUNC_ARG) {
  14767. ret = 0;
  14768. }
  14769. }
  14770. if (ret == 0) {
  14771. ret = wc_curve25519_export_private_raw_ex(&key, NULL, &outLen, endian);
  14772. if (ret == BAD_FUNC_ARG) {
  14773. ret = 0;
  14774. }
  14775. }
  14776. if (ret == 0) {
  14777. ret = wc_curve25519_export_private_raw_ex(&key, out, NULL, endian);
  14778. if (ret == BAD_FUNC_ARG) {
  14779. ret = 0;
  14780. }
  14781. }
  14782. if (ret == 0) {
  14783. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  14784. EC25519_LITTLE_ENDIAN);
  14785. }
  14786. outLen = outLen - 2;
  14787. if (ret == 0) {
  14788. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
  14789. if (ret == ECC_BAD_ARG_E) {
  14790. ret = 0;
  14791. }
  14792. }
  14793. printf(resultFmt, ret == 0 ? passed : failed);
  14794. wc_curve25519_free(&key);
  14795. wc_FreeRng(&rng);
  14796. #endif
  14797. return ret;
  14798. }/*END test_wc_curve25519_export_private_raw_ex*/
  14799. /*
  14800. * Testing wc_ed448_make_key().
  14801. */
  14802. static int test_wc_ed448_make_key (void)
  14803. {
  14804. int ret = 0;
  14805. #if defined(HAVE_ED448)
  14806. ed448_key key;
  14807. WC_RNG rng;
  14808. ret = wc_InitRng(&rng);
  14809. if (ret == 0) {
  14810. ret = wc_ed448_init(&key);
  14811. }
  14812. printf(testingFmt, "wc_ed448_make_key()");
  14813. if (ret == 0) {
  14814. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  14815. }
  14816. /* Test bad args. */
  14817. if (ret == 0) {
  14818. ret = wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key);
  14819. if (ret == BAD_FUNC_ARG) {
  14820. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL);
  14821. }
  14822. if (ret == BAD_FUNC_ARG) {
  14823. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key);
  14824. }
  14825. if (ret == BAD_FUNC_ARG) {
  14826. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key);
  14827. }
  14828. if (ret == BAD_FUNC_ARG) {
  14829. ret = 0;
  14830. } else if (ret == 0) {
  14831. ret = SSL_FATAL_ERROR;
  14832. }
  14833. }
  14834. printf(resultFmt, ret == 0 ? passed : failed);
  14835. if (wc_FreeRng(&rng) && ret == 0) {
  14836. ret = SSL_FATAL_ERROR;
  14837. }
  14838. wc_ed448_free(&key);
  14839. #endif
  14840. return ret;
  14841. } /* END test_wc_ed448_make_key */
  14842. /*
  14843. * Testing wc_ed448_init()
  14844. */
  14845. static int test_wc_ed448_init (void)
  14846. {
  14847. int ret = 0;
  14848. #if defined(HAVE_ED448)
  14849. ed448_key key;
  14850. printf(testingFmt, "wc_ed448_init()");
  14851. ret = wc_ed448_init(&key);
  14852. /* Test bad args. */
  14853. if (ret == 0) {
  14854. ret = wc_ed448_init(NULL);
  14855. if (ret == BAD_FUNC_ARG) {
  14856. ret = 0;
  14857. } else if (ret == 0) {
  14858. ret = SSL_FATAL_ERROR;
  14859. }
  14860. }
  14861. printf(resultFmt, ret == 0 ? passed : failed);
  14862. wc_ed448_free(&key);
  14863. #endif
  14864. return ret;
  14865. } /* END test_wc_ed448_init */
  14866. /*
  14867. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  14868. */
  14869. static int test_wc_ed448_sign_msg (void)
  14870. {
  14871. int ret = 0;
  14872. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  14873. WC_RNG rng;
  14874. ed448_key key;
  14875. byte msg[] = "Everybody gets Friday off.\n";
  14876. byte sig[ED448_SIG_SIZE];
  14877. word32 msglen = sizeof(msg);
  14878. word32 siglen = sizeof(sig);
  14879. word32 badSigLen = sizeof(sig) - 1;
  14880. int verify_ok = 0; /*1 = Verify success.*/
  14881. /* Initialize stack variables. */
  14882. XMEMSET(sig, 0, siglen);
  14883. /* Initialize key. */
  14884. ret = wc_InitRng(&rng);
  14885. if (ret == 0) {
  14886. ret = wc_ed448_init(&key);
  14887. if (ret == 0) {
  14888. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  14889. }
  14890. }
  14891. printf(testingFmt, "wc_ed448_sign_msg()");
  14892. if (ret == 0) {
  14893. ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0);
  14894. }
  14895. /* Test bad args. */
  14896. if (ret == 0 && siglen == ED448_SIG_SIZE) {
  14897. ret = wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0);
  14898. if (ret == BAD_FUNC_ARG) {
  14899. ret = wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0);
  14900. }
  14901. if (ret == BAD_FUNC_ARG) {
  14902. ret = wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0);
  14903. }
  14904. if (ret == BAD_FUNC_ARG) {
  14905. ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0);
  14906. }
  14907. if (ret == BAD_FUNC_ARG) {
  14908. ret = wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key,
  14909. NULL, 0);
  14910. }
  14911. if (ret == BUFFER_E && badSigLen == ED448_SIG_SIZE) {
  14912. badSigLen -= 1;
  14913. ret = 0;
  14914. } else if (ret == 0) {
  14915. ret = SSL_FATAL_ERROR;
  14916. }
  14917. } /* END sign */
  14918. printf(resultFmt, ret == 0 ? passed : failed);
  14919. #ifdef HAVE_ED448_VERIFY
  14920. printf(testingFmt, "wc_ed448_verify_msg()");
  14921. if (ret == 0) {
  14922. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  14923. &key, NULL, 0);
  14924. if (ret == 0 && verify_ok == 1) {
  14925. ret = 0;
  14926. } else if (ret == 0) {
  14927. ret = SSL_FATAL_ERROR;
  14928. }
  14929. /* Test bad args. */
  14930. if (ret == 0) {
  14931. AssertIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg,
  14932. msglen, &verify_ok, &key,
  14933. NULL, 0),
  14934. BAD_FUNC_ARG);
  14935. AssertIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg,
  14936. msglen, &verify_ok, &key,
  14937. NULL, 0),
  14938. BAD_FUNC_ARG);
  14939. ret = wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  14940. &key, NULL, 0);
  14941. if (ret == BAD_FUNC_ARG) {
  14942. ret = wc_ed448_verify_msg(sig, siglen, NULL, msglen,
  14943. &verify_ok, &key, NULL, 0);
  14944. }
  14945. if (ret == BAD_FUNC_ARG) {
  14946. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
  14947. NULL, &key, NULL, 0);
  14948. }
  14949. if (ret == BAD_FUNC_ARG) {
  14950. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
  14951. &verify_ok, NULL, NULL, 0);
  14952. }
  14953. if (ret == BAD_FUNC_ARG) {
  14954. ret = wc_ed448_verify_msg(sig, badSigLen, msg, msglen,
  14955. &verify_ok, &key, NULL, 0);
  14956. }
  14957. if (ret == BAD_FUNC_ARG) {
  14958. ret = 0;
  14959. } else if (ret == 0) {
  14960. ret = SSL_FATAL_ERROR;
  14961. }
  14962. }
  14963. } /* END verify. */
  14964. printf(resultFmt, ret == 0 ? passed : failed);
  14965. #endif /* Verify. */
  14966. if (wc_FreeRng(&rng) && ret == 0) {
  14967. ret = SSL_FATAL_ERROR;
  14968. }
  14969. wc_ed448_free(&key);
  14970. #endif
  14971. return ret;
  14972. } /* END test_wc_ed448_sign_msg */
  14973. /*
  14974. * Testing wc_ed448_import_public()
  14975. */
  14976. static int test_wc_ed448_import_public (void)
  14977. {
  14978. int ret = 0;
  14979. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  14980. WC_RNG rng;
  14981. ed448_key pubKey;
  14982. const byte in[] =
  14983. "Ed448PublicKeyUnitTest.................................\n";
  14984. word32 inlen = sizeof(in);
  14985. ret = wc_InitRng(&rng);
  14986. if (ret == 0) {
  14987. ret = wc_ed448_init(&pubKey);
  14988. if (ret == 0) {
  14989. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey);
  14990. }
  14991. }
  14992. printf(testingFmt, "wc_ed448_import_public()");
  14993. if (ret == 0) {
  14994. ret = wc_ed448_import_public(in, inlen, &pubKey);
  14995. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  14996. ret = 0;
  14997. } else {
  14998. ret = SSL_FATAL_ERROR;
  14999. }
  15000. /* Test bad args. */
  15001. if (ret == 0) {
  15002. ret = wc_ed448_import_public(NULL, inlen, &pubKey);
  15003. if (ret == BAD_FUNC_ARG) {
  15004. ret = wc_ed448_import_public(in, inlen, NULL);
  15005. }
  15006. if (ret == BAD_FUNC_ARG) {
  15007. ret = wc_ed448_import_public(in, inlen - 1, &pubKey);
  15008. }
  15009. if (ret == BAD_FUNC_ARG) {
  15010. ret = 0;
  15011. } else if (ret == 0) {
  15012. ret = SSL_FATAL_ERROR;
  15013. }
  15014. }
  15015. }
  15016. printf(resultFmt, ret == 0 ? passed : failed);
  15017. if (wc_FreeRng(&rng) && ret == 0) {
  15018. ret = SSL_FATAL_ERROR;
  15019. }
  15020. wc_ed448_free(&pubKey);
  15021. #endif
  15022. return ret;
  15023. } /* END wc_ed448_import_public */
  15024. /*
  15025. * Testing wc_ed448_import_private_key()
  15026. */
  15027. static int test_wc_ed448_import_private_key (void)
  15028. {
  15029. int ret = 0;
  15030. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  15031. WC_RNG rng;
  15032. ed448_key key;
  15033. const byte privKey[] =
  15034. "Ed448PrivateKeyUnitTest................................\n";
  15035. const byte pubKey[] =
  15036. "Ed448PublicKeyUnitTest.................................\n";
  15037. word32 privKeySz = sizeof(privKey);
  15038. word32 pubKeySz = sizeof(pubKey);
  15039. ret = wc_InitRng(&rng);
  15040. if (ret != 0) {
  15041. return ret;
  15042. }
  15043. ret = wc_ed448_init(&key);
  15044. if (ret != 0) {
  15045. wc_FreeRng(&rng);
  15046. return ret;
  15047. }
  15048. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  15049. printf(testingFmt, "wc_ed448_import_private_key()");
  15050. if (ret == 0) {
  15051. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey, pubKeySz,
  15052. &key);
  15053. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
  15054. XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  15055. ret = SSL_FATAL_ERROR;
  15056. }
  15057. }
  15058. /* Test bad args. */
  15059. if (ret == 0) {
  15060. ret = wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  15061. &key);
  15062. if (ret == BAD_FUNC_ARG) {
  15063. ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
  15064. pubKeySz, &key);
  15065. }
  15066. if (ret == BAD_FUNC_ARG) {
  15067. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  15068. pubKeySz, NULL);
  15069. }
  15070. if (ret == BAD_FUNC_ARG) {
  15071. ret = wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  15072. pubKeySz, &key);
  15073. }
  15074. if (ret == BAD_FUNC_ARG) {
  15075. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  15076. pubKeySz - 1, &key);
  15077. }
  15078. if (ret == BAD_FUNC_ARG) {
  15079. ret = 0;
  15080. } else if (ret == 0) {
  15081. ret = SSL_FATAL_ERROR;
  15082. }
  15083. }
  15084. printf(resultFmt, ret == 0 ? passed : failed);
  15085. if (wc_FreeRng(&rng) && ret == 0) {
  15086. ret = SSL_FATAL_ERROR;
  15087. }
  15088. wc_ed448_free(&key);
  15089. #endif
  15090. return ret;
  15091. } /* END test_wc_ed448_import_private_key */
  15092. /*
  15093. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  15094. */
  15095. static int test_wc_ed448_export (void)
  15096. {
  15097. int ret = 0;
  15098. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  15099. WC_RNG rng;
  15100. ed448_key key;
  15101. byte priv[ED448_PRV_KEY_SIZE];
  15102. byte pub[ED448_PUB_KEY_SIZE];
  15103. word32 privSz = sizeof(priv);
  15104. word32 pubSz = sizeof(pub);
  15105. ret = wc_InitRng(&rng);
  15106. if (ret != 0) {
  15107. return ret;
  15108. }
  15109. ret = wc_ed448_init(&key);
  15110. if (ret != 0) {
  15111. wc_FreeRng(&rng);
  15112. return ret;
  15113. }
  15114. if (ret == 0) {
  15115. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  15116. }
  15117. printf(testingFmt, "wc_ed448_export_public()");
  15118. if (ret == 0) {
  15119. ret = wc_ed448_export_public(&key, pub, &pubSz);
  15120. if (ret == 0 && (pubSz != ED448_KEY_SIZE ||
  15121. XMEMCMP(key.p, pub, pubSz) != 0)) {
  15122. ret = SSL_FATAL_ERROR;
  15123. }
  15124. if (ret == 0) {
  15125. ret = wc_ed448_export_public(NULL, pub, &pubSz);
  15126. if (ret == BAD_FUNC_ARG) {
  15127. ret = wc_ed448_export_public(&key, NULL, &pubSz);
  15128. }
  15129. if (ret == BAD_FUNC_ARG) {
  15130. ret = wc_ed448_export_public(&key, pub, NULL);
  15131. }
  15132. if (ret == BAD_FUNC_ARG) {
  15133. ret = 0;
  15134. } else if (ret == 0) {
  15135. ret = SSL_FATAL_ERROR;
  15136. }
  15137. }
  15138. }
  15139. printf(resultFmt, ret == 0 ? passed : failed);
  15140. printf(testingFmt, "wc_ed448_export_private_only()");
  15141. if (ret == 0) {
  15142. ret = wc_ed448_export_private_only(&key, priv, &privSz);
  15143. if (ret == 0 && (privSz != ED448_KEY_SIZE ||
  15144. XMEMCMP(key.k, priv, privSz) != 0)) {
  15145. ret = SSL_FATAL_ERROR;
  15146. }
  15147. if (ret == 0) {
  15148. ret = wc_ed448_export_private_only(NULL, priv, &privSz);
  15149. if (ret == BAD_FUNC_ARG) {
  15150. ret = wc_ed448_export_private_only(&key, NULL, &privSz);
  15151. }
  15152. if (ret == BAD_FUNC_ARG) {
  15153. ret = wc_ed448_export_private_only(&key, priv, NULL);
  15154. }
  15155. if (ret == BAD_FUNC_ARG) {
  15156. ret = 0;
  15157. } else if (ret == 0) {
  15158. ret = SSL_FATAL_ERROR;
  15159. }
  15160. }
  15161. }
  15162. printf(resultFmt, ret == 0 ? passed : failed);
  15163. if (wc_FreeRng(&rng) && ret == 0) {
  15164. ret = SSL_FATAL_ERROR;
  15165. }
  15166. wc_ed448_free(&key);
  15167. #endif
  15168. return ret;
  15169. } /* END test_wc_ed448_export */
  15170. /*
  15171. * Testing wc_ed448_size()
  15172. */
  15173. static int test_wc_ed448_size (void)
  15174. {
  15175. int ret = 0;
  15176. #if defined(HAVE_ED448)
  15177. WC_RNG rng;
  15178. ed448_key key;
  15179. ret = wc_InitRng(&rng);
  15180. if (ret != 0) {
  15181. return ret;
  15182. }
  15183. ret = wc_ed448_init(&key);
  15184. if (ret != 0) {
  15185. wc_FreeRng(&rng);
  15186. return ret;
  15187. }
  15188. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  15189. if (ret != 0) {
  15190. wc_FreeRng(&rng);
  15191. wc_ed448_free(&key);
  15192. return ret;
  15193. }
  15194. printf(testingFmt, "wc_ed448_size()");
  15195. ret = wc_ed448_size(&key);
  15196. /* Test bad args. */
  15197. if (ret == ED448_KEY_SIZE) {
  15198. ret = wc_ed448_size(NULL);
  15199. if (ret == BAD_FUNC_ARG) {
  15200. ret = 0;
  15201. }
  15202. }
  15203. printf(resultFmt, ret == 0 ? passed : failed);
  15204. if (ret == 0) {
  15205. printf(testingFmt, "wc_ed448_sig_size()");
  15206. ret = wc_ed448_sig_size(&key);
  15207. if (ret == ED448_SIG_SIZE) {
  15208. ret = 0;
  15209. }
  15210. /* Test bad args. */
  15211. if (ret == 0) {
  15212. ret = wc_ed448_sig_size(NULL);
  15213. if (ret == BAD_FUNC_ARG) {
  15214. ret = 0;
  15215. }
  15216. }
  15217. printf(resultFmt, ret == 0 ? passed : failed);
  15218. } /* END wc_ed448_sig_size() */
  15219. if (ret == 0) {
  15220. printf(testingFmt, "wc_ed448_pub_size");
  15221. ret = wc_ed448_pub_size(&key);
  15222. if (ret == ED448_PUB_KEY_SIZE) {
  15223. ret = 0;
  15224. }
  15225. if (ret == 0) {
  15226. ret = wc_ed448_pub_size(NULL);
  15227. if (ret == BAD_FUNC_ARG) {
  15228. ret = 0;
  15229. }
  15230. }
  15231. printf(resultFmt, ret == 0 ? passed : failed);
  15232. } /* END wc_ed448_pub_size */
  15233. if (ret == 0) {
  15234. printf(testingFmt, "wc_ed448_priv_size");
  15235. ret = wc_ed448_priv_size(&key);
  15236. if (ret == ED448_PRV_KEY_SIZE) {
  15237. ret = 0;
  15238. }
  15239. if (ret == 0) {
  15240. ret = wc_ed448_priv_size(NULL);
  15241. if (ret == BAD_FUNC_ARG) {
  15242. ret = 0;
  15243. }
  15244. }
  15245. printf(resultFmt, ret == 0 ? passed : failed);
  15246. } /* END wc_ed448_pub_size */
  15247. if (wc_FreeRng(&rng) && ret == 0) {
  15248. ret = SSL_FATAL_ERROR;
  15249. }
  15250. wc_ed448_free(&key);
  15251. #endif
  15252. return ret;
  15253. } /* END test_wc_ed448_size */
  15254. /*
  15255. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  15256. */
  15257. static int test_wc_ed448_exportKey (void)
  15258. {
  15259. int ret = 0;
  15260. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  15261. WC_RNG rng;
  15262. ed448_key key;
  15263. byte priv[ED448_PRV_KEY_SIZE];
  15264. byte pub[ED448_PUB_KEY_SIZE];
  15265. byte privOnly[ED448_PRV_KEY_SIZE];
  15266. word32 privSz = sizeof(priv);
  15267. word32 pubSz = sizeof(pub);
  15268. word32 privOnlySz = sizeof(privOnly);
  15269. ret = wc_InitRng(&rng);
  15270. if (ret != 0) {
  15271. return ret;
  15272. }
  15273. ret = wc_ed448_init(&key);
  15274. if (ret != 0) {
  15275. wc_FreeRng(&rng);
  15276. return ret;
  15277. }
  15278. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  15279. if (ret != 0) {
  15280. wc_FreeRng(&rng);
  15281. wc_ed448_free(&key);
  15282. return ret;
  15283. }
  15284. printf(testingFmt, "wc_ed448_export_private()");
  15285. ret = wc_ed448_export_private(&key, privOnly, &privOnlySz);
  15286. if (ret == 0) {
  15287. ret = wc_ed448_export_private(NULL, privOnly, &privOnlySz);
  15288. if (ret == BAD_FUNC_ARG) {
  15289. ret = wc_ed448_export_private(&key, NULL, &privOnlySz);
  15290. }
  15291. if (ret == BAD_FUNC_ARG) {
  15292. ret = wc_ed448_export_private(&key, privOnly, NULL);
  15293. }
  15294. if (ret == BAD_FUNC_ARG) {
  15295. ret = 0;
  15296. } else if (ret == 0) {
  15297. ret = SSL_FATAL_ERROR;
  15298. }
  15299. }
  15300. printf(resultFmt, ret == 0 ? passed : failed);
  15301. if (ret == 0) {
  15302. printf(testingFmt, "wc_ed448_export_key()");
  15303. ret = wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz);
  15304. if (ret == 0) {
  15305. ret = wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz);
  15306. if (ret == BAD_FUNC_ARG) {
  15307. ret = wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz);
  15308. }
  15309. if (ret == BAD_FUNC_ARG) {
  15310. ret = wc_ed448_export_key(&key, priv, NULL, pub, &pubSz);
  15311. }
  15312. if (ret == BAD_FUNC_ARG) {
  15313. ret = wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz);
  15314. }
  15315. if (ret == BAD_FUNC_ARG) {
  15316. ret = wc_ed448_export_key(&key, priv, &privSz, pub, NULL);
  15317. }
  15318. if (ret == BAD_FUNC_ARG) {
  15319. ret = 0;
  15320. } else if (ret == 0) {
  15321. ret = SSL_FATAL_ERROR;
  15322. }
  15323. }
  15324. printf(resultFmt, ret == 0 ? passed : failed);
  15325. } /* END wc_ed448_export_key() */
  15326. /* Cross check output. */
  15327. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  15328. ret = SSL_FATAL_ERROR;
  15329. }
  15330. if (wc_FreeRng(&rng) && ret == 0) {
  15331. ret = SSL_FATAL_ERROR;
  15332. }
  15333. wc_ed448_free(&key);
  15334. #endif
  15335. return ret;
  15336. } /* END test_wc_ed448_exportKey */
  15337. /*
  15338. * Testing wc_Ed448PublicKeyToDer
  15339. */
  15340. static int test_wc_Ed448PublicKeyToDer (void)
  15341. {
  15342. int ret = 0;
  15343. #if defined(HAVE_ED448) && (defined(WOLFSSL_CERT_GEN) || \
  15344. defined(WOLFSSL_KEY_GEN))
  15345. int tmp;
  15346. ed448_key key;
  15347. byte derBuf[1024];
  15348. printf(testingFmt, "wc_Ed448PublicKeyToDer()");
  15349. /* Test bad args */
  15350. tmp = wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0);
  15351. if (tmp != BAD_FUNC_ARG) {
  15352. ret = SSL_FATAL_ERROR;
  15353. }
  15354. if (ret == 0) {
  15355. wc_ed448_init(&key);
  15356. tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0);
  15357. if (tmp != BUFFER_E) {
  15358. ret = SSL_FATAL_ERROR;
  15359. }
  15360. wc_ed448_free(&key);
  15361. }
  15362. /* Test good args */
  15363. if (ret == 0) {
  15364. WC_RNG rng;
  15365. ret = wc_InitRng(&rng);
  15366. if (ret != 0) {
  15367. return ret;
  15368. }
  15369. ret = wc_ed448_init(&key);
  15370. if (ret != 0) {
  15371. wc_FreeRng(&rng);
  15372. return ret;
  15373. }
  15374. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  15375. if (ret != 0) {
  15376. wc_FreeRng(&rng);
  15377. wc_ed448_free(&key);
  15378. return ret;
  15379. }
  15380. tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1);
  15381. if (tmp <= 0) {
  15382. ret = SSL_FATAL_ERROR;
  15383. }
  15384. wc_FreeRng(&rng);
  15385. wc_ed448_free(&key);
  15386. }
  15387. printf(resultFmt, ret == 0 ? passed : failed);
  15388. #endif
  15389. return ret;
  15390. } /* END testing wc_Ed448PublicKeyToDer */
  15391. /*
  15392. * Testing wc_curve448_init and wc_curve448_free.
  15393. */
  15394. static int test_wc_curve448_init (void)
  15395. {
  15396. int ret = 0;
  15397. #if defined(HAVE_CURVE448)
  15398. curve448_key key;
  15399. printf(testingFmt, "wc_curve448_init()");
  15400. ret = wc_curve448_init(&key);
  15401. /* Test bad args for wc_curve448_init */
  15402. if (ret == 0) {
  15403. ret = wc_curve448_init(NULL);
  15404. if (ret == BAD_FUNC_ARG) {
  15405. ret = 0;
  15406. } else if (ret == 0) {
  15407. ret = SSL_FATAL_ERROR;
  15408. }
  15409. }
  15410. printf(resultFmt, ret == 0 ? passed : failed);
  15411. /* Test good args for wc_curve_448_free */
  15412. wc_curve448_free(&key);
  15413. wc_curve448_free(NULL);
  15414. #endif
  15415. return ret;
  15416. } /* END test_wc_curve448_init and wc_curve_448_free*/
  15417. /*
  15418. * Testing wc_curve448_make_key
  15419. */
  15420. static int test_wc_curve448_make_key (void)
  15421. {
  15422. int ret = 0;
  15423. #if defined(HAVE_CURVE448)
  15424. WC_RNG rng;
  15425. curve448_key key;
  15426. int keysize;
  15427. printf(testingFmt, "wc_curve448_make_key()");
  15428. ret = wc_curve448_init(&key);
  15429. if (ret == 0) {
  15430. ret = wc_InitRng(&rng);
  15431. }
  15432. if (ret == 0) {
  15433. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  15434. if (ret == 0) {
  15435. keysize = wc_curve448_size(&key);
  15436. if (keysize != CURVE448_KEY_SIZE) {
  15437. ret = SSL_FATAL_ERROR;
  15438. }
  15439. }
  15440. if (ret == 0) {
  15441. ret = wc_curve448_make_key(&rng, keysize, &key);
  15442. }
  15443. }
  15444. /*test bad cases*/
  15445. if (ret == 0) {
  15446. ret = wc_curve448_make_key(NULL, 0, NULL);
  15447. if (ret == BAD_FUNC_ARG) {
  15448. ret = 0;
  15449. }
  15450. }
  15451. if (ret == 0) {
  15452. ret = wc_curve448_make_key(&rng, keysize, NULL);
  15453. if (ret == BAD_FUNC_ARG) {
  15454. ret = 0;
  15455. }
  15456. }
  15457. if (ret == 0) {
  15458. ret = wc_curve448_make_key(NULL, keysize, &key);
  15459. if (ret == BAD_FUNC_ARG) {
  15460. ret = 0;
  15461. }
  15462. }
  15463. if (ret == 0) {
  15464. ret = wc_curve448_make_key(&rng, 0, &key);
  15465. if (ret == ECC_BAD_ARG_E) {
  15466. ret = 0;
  15467. }
  15468. }
  15469. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  15470. ret = WOLFSSL_FATAL_ERROR;
  15471. }
  15472. printf(resultFmt, ret == 0 ? passed : failed);
  15473. wc_curve448_free(&key);
  15474. #endif
  15475. return ret;
  15476. } /*END test_wc_curve448_make_key*/
  15477. /*
  15478. * Testing test_wc_curve448_shared_secret_ex
  15479. */
  15480. static int test_wc_curve448_shared_secret_ex (void)
  15481. {
  15482. int ret = 0;
  15483. #if defined(HAVE_CURVE448)
  15484. WC_RNG rng;
  15485. curve448_key private_key, public_key;
  15486. byte out[CURVE448_KEY_SIZE];
  15487. word32 outLen = sizeof(out);
  15488. int endian = EC448_BIG_ENDIAN;
  15489. printf(testingFmt, "wc_curve448_shared_secret_ex()");
  15490. ret = wc_curve448_init(&private_key);
  15491. if (ret == 0) {
  15492. ret = wc_InitRng(&rng);
  15493. if (ret == 0) {
  15494. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key);
  15495. }
  15496. }
  15497. if (ret == 0) {
  15498. ret = wc_curve448_init(&public_key);
  15499. }
  15500. if (ret == 0) {
  15501. if (ret == 0) {
  15502. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key);
  15503. }
  15504. }
  15505. if (ret == 0) {
  15506. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  15507. &outLen, endian);
  15508. }
  15509. /*test bad cases*/
  15510. if (ret == 0) {
  15511. ret = wc_curve448_shared_secret_ex(NULL, NULL, NULL,
  15512. 0, endian);
  15513. if (ret == BAD_FUNC_ARG) {
  15514. ret = 0;
  15515. }
  15516. }
  15517. if (ret == 0) {
  15518. ret = wc_curve448_shared_secret_ex(NULL, &public_key, out,
  15519. &outLen, endian);
  15520. if (ret == BAD_FUNC_ARG) {
  15521. ret = 0;
  15522. }
  15523. }
  15524. if (ret == 0) {
  15525. ret = wc_curve448_shared_secret_ex(&private_key, NULL, out,
  15526. &outLen, endian);
  15527. if (ret == BAD_FUNC_ARG) {
  15528. ret = 0;
  15529. }
  15530. }
  15531. if (ret == 0) {
  15532. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  15533. &outLen, endian);
  15534. if (ret == BAD_FUNC_ARG) {
  15535. ret = 0;
  15536. }
  15537. }
  15538. if (ret == 0) {
  15539. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  15540. NULL, endian);
  15541. if (ret == BAD_FUNC_ARG) {
  15542. ret = 0;
  15543. }
  15544. }
  15545. outLen = outLen - 2;
  15546. if (ret == 0) {
  15547. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  15548. &outLen, endian);
  15549. if (ret == BAD_FUNC_ARG) {
  15550. ret = 0;
  15551. }
  15552. }
  15553. printf(resultFmt, ret == 0 ? passed : failed);
  15554. wc_curve448_free(&private_key);
  15555. wc_curve448_free(&public_key);
  15556. wc_FreeRng(&rng);
  15557. #endif
  15558. return ret;
  15559. } /*END test_wc_curve448_shared_secret_ex*/
  15560. /*
  15561. * Testing test_wc_curve448_export_public_ex
  15562. */
  15563. static int test_wc_curve448_export_public_ex (void)
  15564. {
  15565. int ret = 0;
  15566. #if defined(HAVE_CURVE448)
  15567. WC_RNG rng;
  15568. curve448_key key;
  15569. byte out[CURVE448_KEY_SIZE];
  15570. word32 outLen = sizeof(out);
  15571. int endian = EC448_BIG_ENDIAN;
  15572. printf(testingFmt, "wc_curve448_export_public_ex()");
  15573. ret = wc_curve448_init(&key);
  15574. if (ret == 0) {
  15575. ret = wc_InitRng(&rng);
  15576. }
  15577. if (ret == 0) {
  15578. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  15579. if (ret == 0){
  15580. ret = wc_curve448_export_public(&key, out, &outLen);
  15581. }
  15582. if (ret == 0) {
  15583. ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
  15584. }
  15585. }
  15586. /*test bad cases*/
  15587. if (ret == 0) {
  15588. ret = wc_curve448_export_public_ex(NULL, NULL, NULL, endian);
  15589. if (ret == BAD_FUNC_ARG) {
  15590. ret = 0;
  15591. }
  15592. }
  15593. if (ret == 0) {
  15594. ret = wc_curve448_export_public_ex(NULL, out, &outLen, endian);
  15595. if (ret == BAD_FUNC_ARG) {
  15596. ret = 0;
  15597. }
  15598. }
  15599. if (ret == 0) {
  15600. ret = wc_curve448_export_public_ex(&key, NULL, &outLen, endian);
  15601. if (ret == BAD_FUNC_ARG) {
  15602. ret = 0;
  15603. }
  15604. }
  15605. if (ret == 0) {
  15606. ret = wc_curve448_export_public_ex(&key, out, NULL, endian);
  15607. if (ret == BAD_FUNC_ARG) {
  15608. ret = 0;
  15609. }
  15610. }
  15611. outLen = outLen - 2;
  15612. if (ret == 0) {
  15613. ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
  15614. if (ret == ECC_BAD_ARG_E) {
  15615. ret = 0;
  15616. }
  15617. }
  15618. printf(resultFmt, ret == 0 ? passed : failed);
  15619. wc_curve448_free(&key);
  15620. wc_FreeRng(&rng);
  15621. #endif
  15622. return ret;
  15623. } /*END test_wc_curve448_export_public_ex*/
  15624. /*
  15625. * Testing test_wc_curve448_export_private_raw_ex
  15626. */
  15627. static int test_wc_curve448_export_private_raw_ex (void)
  15628. {
  15629. int ret = 0;
  15630. #if defined(HAVE_CURVE448)
  15631. WC_RNG rng;
  15632. curve448_key key;
  15633. byte out[CURVE448_KEY_SIZE];
  15634. word32 outLen = sizeof(out);
  15635. int endian = EC448_BIG_ENDIAN;
  15636. printf(testingFmt, "wc_curve448_export_private_raw_ex()");
  15637. ret = wc_curve448_init(&key);
  15638. if (ret == 0) {
  15639. ret = wc_InitRng(&rng);
  15640. }
  15641. if (ret == 0) {
  15642. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
  15643. }
  15644. /*test bad cases*/
  15645. if (ret == 0) {
  15646. ret = wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian);
  15647. if (ret == BAD_FUNC_ARG) {
  15648. ret = 0;
  15649. }
  15650. }
  15651. if (ret == 0) {
  15652. ret = wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian);
  15653. if (ret == BAD_FUNC_ARG) {
  15654. ret = 0;
  15655. }
  15656. }
  15657. if (ret == 0) {
  15658. ret = wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian);
  15659. if (ret == BAD_FUNC_ARG) {
  15660. ret = 0;
  15661. }
  15662. }
  15663. if (ret == 0) {
  15664. ret = wc_curve448_export_private_raw_ex(&key, out, NULL, endian);
  15665. if (ret == BAD_FUNC_ARG) {
  15666. ret = 0;
  15667. }
  15668. }
  15669. if (ret == 0) {
  15670. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen,
  15671. EC448_LITTLE_ENDIAN);
  15672. }
  15673. outLen = outLen - 2;
  15674. if (ret == 0) {
  15675. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
  15676. if (ret == ECC_BAD_ARG_E) {
  15677. ret = 0;
  15678. }
  15679. }
  15680. printf(resultFmt, ret == 0 ? passed : failed);
  15681. wc_curve448_free(&key);
  15682. wc_FreeRng(&rng);
  15683. #endif
  15684. return ret;
  15685. }/*END test_wc_curve448_export_private_raw_ex*/
  15686. /*
  15687. * Testing test_wc_curve448_import_private_raw_ex
  15688. */
  15689. static int test_wc_curve448_import_private_raw_ex (void)
  15690. {
  15691. int ret = 0;
  15692. #if defined(HAVE_CURVE448)
  15693. WC_RNG rng;
  15694. curve448_key key;
  15695. byte priv[CURVE448_KEY_SIZE];
  15696. byte pub[CURVE448_KEY_SIZE];
  15697. word32 privSz = sizeof(priv);
  15698. word32 pubSz = sizeof(pub);
  15699. int endian = EC448_BIG_ENDIAN;
  15700. printf(testingFmt, "wc_curve448_import_private_raw_ex()");
  15701. ret = wc_curve448_init(&key);
  15702. if (ret == 0) {
  15703. ret = wc_InitRng(&rng);
  15704. }
  15705. if (ret == 0) {
  15706. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  15707. if (ret == 0){
  15708. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  15709. }
  15710. if (ret == 0){
  15711. ret = wc_curve448_export_public(&key, pub, &pubSz);
  15712. }
  15713. if (ret == 0) {
  15714. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  15715. &key, endian);
  15716. }
  15717. }
  15718. /*test bad cases*/
  15719. if (ret == 0) {
  15720. ret = wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0);
  15721. if (ret == BAD_FUNC_ARG) {
  15722. ret = 0;
  15723. }
  15724. }
  15725. if (ret == 0) {
  15726. ret = wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  15727. &key, endian);
  15728. if (ret == BAD_FUNC_ARG) {
  15729. ret = 0;
  15730. }
  15731. }
  15732. if (ret == 0) {
  15733. ret = wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  15734. &key, endian);
  15735. if (ret == BAD_FUNC_ARG) {
  15736. ret = 0;
  15737. }
  15738. }
  15739. if (ret == 0) {
  15740. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  15741. NULL, endian);
  15742. if (ret == BAD_FUNC_ARG) {
  15743. ret = 0;
  15744. }
  15745. }
  15746. if (ret == 0) {
  15747. ret = wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  15748. &key, endian);
  15749. if (ret == ECC_BAD_ARG_E) {
  15750. ret = 0;
  15751. }
  15752. }
  15753. if (ret == 0) {
  15754. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  15755. &key, endian);
  15756. if (ret == ECC_BAD_ARG_E) {
  15757. ret = 0;
  15758. }
  15759. }
  15760. if (ret == 0) {
  15761. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  15762. &key, EC448_LITTLE_ENDIAN);
  15763. }
  15764. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  15765. ret = WOLFSSL_FATAL_ERROR;
  15766. }
  15767. printf(resultFmt, ret == 0 ? passed : failed);
  15768. wc_curve448_free(&key);
  15769. #endif
  15770. return ret;
  15771. } /*END test_wc_curve448_import_private_raw_ex*/
  15772. /*
  15773. * Testing test_curve448_export_key_raw
  15774. */
  15775. static int test_wc_curve448_export_key_raw (void)
  15776. {
  15777. int ret = 0;
  15778. #if defined(HAVE_CURVE448)
  15779. WC_RNG rng;
  15780. curve448_key key;
  15781. byte priv[CURVE448_KEY_SIZE];
  15782. byte pub[CURVE448_KEY_SIZE];
  15783. word32 privSz = sizeof(priv);
  15784. word32 pubSz = sizeof(pub);
  15785. printf(testingFmt, "wc_curve448_export_key_raw()");
  15786. ret = wc_curve448_init(&key);
  15787. if (ret == 0) {
  15788. ret = wc_InitRng(&rng);
  15789. }
  15790. if (ret == 0) {
  15791. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  15792. if (ret == 0) {
  15793. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  15794. }
  15795. if (ret == 0) {
  15796. ret = wc_curve448_export_public(&key, pub, &pubSz);
  15797. }
  15798. if (ret == 0) {
  15799. ret = wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz);
  15800. }
  15801. }
  15802. printf(resultFmt, ret == 0 ? passed : failed);
  15803. wc_curve448_free(&key);
  15804. wc_FreeRng(&rng);
  15805. #endif
  15806. return ret;
  15807. }/*END test_wc_curve448_import_private_raw_ex*/
  15808. /*
  15809. * Testing test_wc_curve448_import_private
  15810. */
  15811. static int test_wc_curve448_import_private (void)
  15812. {
  15813. int ret = 0;
  15814. #if defined(HAVE_CURVE448)
  15815. curve448_key key;
  15816. WC_RNG rng;
  15817. byte priv[CURVE448_KEY_SIZE];
  15818. word32 privSz = sizeof(priv);
  15819. printf(testingFmt, "wc_curve448_import_private()");
  15820. ret = wc_curve448_init(&key);
  15821. if (ret == 0) {
  15822. ret = wc_InitRng(&rng);
  15823. }
  15824. if (ret == 0) {
  15825. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  15826. if (ret == 0) {
  15827. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  15828. }
  15829. }
  15830. if (ret == 0) {
  15831. ret = wc_curve448_import_private(priv, privSz, &key);
  15832. }
  15833. printf(resultFmt, ret == 0 ? passed : failed);
  15834. wc_curve448_free(&key);
  15835. wc_FreeRng(&rng);
  15836. #endif
  15837. return ret;
  15838. } /*END test_wc_curve448_import*/
  15839. /*
  15840. * Testing test_wc_curve448_size.
  15841. */
  15842. static int test_wc_curve448_size (void)
  15843. {
  15844. int ret = 0;
  15845. #if defined(HAVE_CURVE448)
  15846. curve448_key key;
  15847. printf(testingFmt, "wc_curve448_size()");
  15848. ret = wc_curve448_init(&key);
  15849. /* Test good args for wc_curve448_size */
  15850. if (ret == 0) {
  15851. ret = wc_curve448_size(&key);
  15852. }
  15853. /* Test bad args for wc_curve448_size */
  15854. if (ret != 0) {
  15855. ret = wc_curve448_size(NULL);
  15856. }
  15857. printf(resultFmt, ret == 0 ? passed : failed);
  15858. wc_curve448_free(&key);
  15859. #endif
  15860. return ret;
  15861. } /* END test_wc_curve448_size*/
  15862. /*
  15863. * Testing wc_ecc_make_key.
  15864. */
  15865. static int test_wc_ecc_make_key (void)
  15866. {
  15867. int ret = 0;
  15868. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  15869. WC_RNG rng;
  15870. ecc_key key;
  15871. printf(testingFmt, "wc_ecc_make_key()");
  15872. ret = wc_InitRng(&rng);
  15873. if (ret != 0)
  15874. return ret;
  15875. ret = wc_ecc_init(&key);
  15876. if (ret == 0) {
  15877. ret = wc_ecc_make_key(&rng, KEY14, &key);
  15878. /* Pass in bad args. */
  15879. if (ret == 0) {
  15880. ret = wc_ecc_make_key(NULL, KEY14, &key);
  15881. if (ret == BAD_FUNC_ARG) {
  15882. ret = wc_ecc_make_key(&rng, KEY14, NULL);
  15883. }
  15884. if (ret == BAD_FUNC_ARG) {
  15885. ret = 0;
  15886. } else if (ret == 0) {
  15887. ret = WOLFSSL_FATAL_ERROR;
  15888. }
  15889. }
  15890. wc_ecc_free(&key);
  15891. }
  15892. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  15893. ret = WOLFSSL_FATAL_ERROR;
  15894. }
  15895. #ifdef FP_ECC
  15896. wc_ecc_fp_free();
  15897. #endif
  15898. printf(resultFmt, ret == 0 ? passed : failed);
  15899. #endif
  15900. return ret;
  15901. } /* END test_wc_ecc_make_key */
  15902. /*
  15903. * Testing wc_ecc_init()
  15904. */
  15905. static int test_wc_ecc_init (void)
  15906. {
  15907. int ret = 0;
  15908. #ifdef HAVE_ECC
  15909. ecc_key key;
  15910. printf(testingFmt, "wc_ecc_init()");
  15911. ret = wc_ecc_init(&key);
  15912. /* Pass in bad args. */
  15913. if (ret == 0) {
  15914. ret = wc_ecc_init(NULL);
  15915. if (ret == BAD_FUNC_ARG) {
  15916. ret = 0;
  15917. } else if (ret == 0) {
  15918. ret = WOLFSSL_FATAL_ERROR;
  15919. }
  15920. }
  15921. printf(resultFmt, ret == 0 ? passed : failed);
  15922. wc_ecc_free(&key);
  15923. #endif
  15924. return ret;
  15925. } /* END test_wc_ecc_init */
  15926. /*
  15927. * Testing wc_ecc_check_key()
  15928. */
  15929. static int test_wc_ecc_check_key (void)
  15930. {
  15931. int ret = 0;
  15932. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  15933. WC_RNG rng;
  15934. ecc_key key;
  15935. XMEMSET(&rng, 0, sizeof(rng));
  15936. XMEMSET(&key, 0, sizeof(key));
  15937. ret = wc_InitRng(&rng);
  15938. if (ret == 0) {
  15939. ret = wc_ecc_init(&key);
  15940. if (ret == 0) {
  15941. ret = wc_ecc_make_key(&rng, KEY14, &key);
  15942. }
  15943. }
  15944. printf(testingFmt, "wc_ecc_check_key()");
  15945. if (ret == 0) {
  15946. ret = wc_ecc_check_key(&key);
  15947. }
  15948. /* Pass in bad args. */
  15949. if (ret == 0) {
  15950. ret = wc_ecc_check_key(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. if (wc_FreeRng(&rng) && ret == 0) {
  15959. ret = WOLFSSL_FATAL_ERROR;
  15960. }
  15961. wc_ecc_free(&key);
  15962. #ifdef FP_ECC
  15963. wc_ecc_fp_free();
  15964. #endif
  15965. #endif
  15966. return ret;
  15967. } /* END test_wc_ecc_check_key */
  15968. /*
  15969. * Testing wc_ecc_get_generator()
  15970. */
  15971. static int test_wc_ecc_get_generator(void)
  15972. {
  15973. int ret = 0;
  15974. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  15975. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  15976. ecc_point* pt;
  15977. printf(testingFmt, "wc_ecc_new_point()");
  15978. pt = wc_ecc_new_point();
  15979. if (!pt) {
  15980. ret = WOLFSSL_FATAL_ERROR;
  15981. }
  15982. printf(testingFmt, "wc_ecc_get_generator()");
  15983. if (ret == 0) {
  15984. ret = wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1));
  15985. }
  15986. /* Test bad args. */
  15987. if (ret == MP_OKAY) {
  15988. /* Returns Zero for bad arg. */
  15989. ret = wc_ecc_get_generator(pt, -1);
  15990. if (ret != MP_OKAY)
  15991. wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1));
  15992. if (ret != MP_OKAY)
  15993. wc_ecc_get_generator(pt, 1000); /* If we ever get to 1000 curves
  15994. * increase this number */
  15995. if (ret != MP_OKAY)
  15996. wc_ecc_get_generator(NULL, -1);
  15997. ret = ret == MP_OKAY ? WOLFSSL_FATAL_ERROR : 0;
  15998. }
  15999. printf(resultFmt, ret == 0 ? passed : failed);
  16000. wc_ecc_del_point(pt);
  16001. #endif
  16002. return ret;
  16003. } /* END test_wc_ecc_get_generator */
  16004. /*
  16005. * Testing wc_ecc_size()
  16006. */
  16007. static int test_wc_ecc_size (void)
  16008. {
  16009. int ret = 0;
  16010. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  16011. WC_RNG rng;
  16012. ecc_key key;
  16013. XMEMSET(&rng, 0, sizeof(rng));
  16014. XMEMSET(&key, 0, sizeof(key));
  16015. ret = wc_InitRng(&rng);
  16016. if (ret == 0) {
  16017. ret = wc_ecc_init(&key);
  16018. if (ret == 0) {
  16019. ret = wc_ecc_make_key(&rng, KEY14, &key);
  16020. }
  16021. }
  16022. printf(testingFmt, "wc_ecc_size()");
  16023. if (ret == 0) {
  16024. ret = wc_ecc_size(&key);
  16025. if (ret == KEY14) {
  16026. ret = 0;
  16027. } else if (ret == 0){
  16028. ret = WOLFSSL_FATAL_ERROR;
  16029. }
  16030. }
  16031. /* Test bad args. */
  16032. if (ret == 0) {
  16033. /* Returns Zero for bad arg. */
  16034. ret = wc_ecc_size(NULL);
  16035. }
  16036. printf(resultFmt, ret == 0 ? passed : failed);
  16037. if (wc_FreeRng(&rng) && ret == 0) {
  16038. ret = WOLFSSL_FATAL_ERROR;
  16039. }
  16040. wc_ecc_free(&key);
  16041. #endif
  16042. return ret;
  16043. } /* END test_wc_ecc_size */
  16044. static void test_wc_ecc_params(void)
  16045. {
  16046. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  16047. It was added after certifications */
  16048. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  16049. const ecc_set_type* ecc_set;
  16050. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  16051. /* Test for SECP256R1 curve */
  16052. int curve_id = ECC_SECP256R1;
  16053. int curve_idx = wc_ecc_get_curve_idx(curve_id);
  16054. AssertIntNE(curve_idx, ECC_CURVE_INVALID);
  16055. ecc_set = wc_ecc_get_curve_params(curve_idx);
  16056. AssertNotNull(ecc_set);
  16057. AssertIntEQ(ecc_set->id, curve_id);
  16058. #endif
  16059. /* Test case when SECP256R1 is not enabled */
  16060. /* Test that we get curve params for index 0 */
  16061. ecc_set = wc_ecc_get_curve_params(0);
  16062. AssertNotNull(ecc_set);
  16063. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  16064. }
  16065. /*
  16066. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  16067. */
  16068. static int test_wc_ecc_signVerify_hash (void)
  16069. {
  16070. int ret = 0;
  16071. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  16072. WC_RNG rng;
  16073. ecc_key key;
  16074. int signH = WOLFSSL_FATAL_ERROR;
  16075. #ifdef HAVE_ECC_VERIFY
  16076. int verifyH = WOLFSSL_FATAL_ERROR;
  16077. int verify = 0;
  16078. #endif
  16079. word32 siglen = ECC_BUFSIZE;
  16080. byte sig[ECC_BUFSIZE];
  16081. byte digest[] = "Everyone gets Friday off.";
  16082. word32 digestlen = (word32)XSTRLEN((char*)digest);
  16083. /* Init stack var */
  16084. XMEMSET(sig, 0, siglen);
  16085. XMEMSET(&key, 0, sizeof(key));
  16086. /* Init structs. */
  16087. ret = wc_InitRng(&rng);
  16088. if (ret == 0) {
  16089. ret = wc_ecc_init(&key);
  16090. if (ret == 0) {
  16091. ret = wc_ecc_make_key(&rng, KEY14, &key);
  16092. }
  16093. }
  16094. printf(testingFmt, "wc_ecc_sign_hash()");
  16095. if (ret == 0) {
  16096. ret = wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key);
  16097. }
  16098. /* Check bad args. */
  16099. if (ret == 0) {
  16100. signH = wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key);
  16101. if (signH == ECC_BAD_ARG_E) {
  16102. signH = wc_ecc_sign_hash(digest, digestlen, NULL, &siglen,
  16103. &rng, &key);
  16104. }
  16105. if (signH == ECC_BAD_ARG_E) {
  16106. signH = wc_ecc_sign_hash(digest, digestlen, sig, NULL,
  16107. &rng, &key);
  16108. }
  16109. if (signH == ECC_BAD_ARG_E) {
  16110. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  16111. NULL, &key);
  16112. }
  16113. if (signH == ECC_BAD_ARG_E) {
  16114. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  16115. &rng, NULL);
  16116. }
  16117. if (signH == ECC_BAD_ARG_E) {
  16118. signH = 0;
  16119. } else if (ret == 0) {
  16120. signH = WOLFSSL_FATAL_ERROR;
  16121. }
  16122. }
  16123. printf(resultFmt, signH == 0 ? passed : failed);
  16124. #ifdef HAVE_ECC_VERIFY
  16125. printf(testingFmt, "wc_ecc_verify_hash()");
  16126. ret = wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify, &key);
  16127. if (verify != 1 && ret == 0) {
  16128. ret = WOLFSSL_FATAL_ERROR;
  16129. }
  16130. /* Test bad args. */
  16131. if (ret == 0) {
  16132. verifyH = wc_ecc_verify_hash(NULL, siglen, digest, digestlen,
  16133. &verify, &key);
  16134. if (verifyH == ECC_BAD_ARG_E) {
  16135. verifyH = wc_ecc_verify_hash(sig, siglen, NULL, digestlen,
  16136. &verify, &key);
  16137. }
  16138. if (verifyH == ECC_BAD_ARG_E) {
  16139. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  16140. NULL, &key);
  16141. }
  16142. if (verifyH == ECC_BAD_ARG_E) {
  16143. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  16144. &verify, NULL);
  16145. }
  16146. if (verifyH == ECC_BAD_ARG_E) {
  16147. verifyH = 0;
  16148. } else if (ret == 0) {
  16149. verifyH = WOLFSSL_FATAL_ERROR;
  16150. }
  16151. }
  16152. printf(resultFmt, verifyH == 0 ? passed : failed);
  16153. #endif /* HAVE_ECC_VERIFY */
  16154. if (wc_FreeRng(&rng) && ret == 0) {
  16155. ret = WOLFSSL_FATAL_ERROR;
  16156. }
  16157. wc_ecc_free(&key);
  16158. #ifdef FP_ECC
  16159. wc_ecc_fp_free();
  16160. #endif
  16161. #endif
  16162. return ret;
  16163. } /* END test_wc_ecc_sign_hash */
  16164. /*
  16165. * Testing wc_ecc_shared_secret()
  16166. */
  16167. static int test_wc_ecc_shared_secret (void)
  16168. {
  16169. int ret = 0;
  16170. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  16171. ecc_key key, pubKey;
  16172. WC_RNG rng;
  16173. int keySz = KEY16;
  16174. byte out[KEY16];
  16175. word32 outlen = (word32)sizeof(out);
  16176. /* Initialize variables. */
  16177. XMEMSET(out, 0, keySz);
  16178. XMEMSET(&rng, 0, sizeof(rng));
  16179. XMEMSET(&key, 0, sizeof(key));
  16180. XMEMSET(&pubKey, 0, sizeof(pubKey));
  16181. ret = wc_InitRng(&rng);
  16182. if (ret == 0) {
  16183. ret = wc_ecc_init(&key);
  16184. if (ret == 0) {
  16185. ret = wc_ecc_init(&pubKey);
  16186. }
  16187. }
  16188. if (ret == 0) {
  16189. ret = wc_ecc_make_key(&rng, keySz, &key);
  16190. }
  16191. if (ret == 0) {
  16192. ret = wc_ecc_make_key(&rng, keySz, &pubKey);
  16193. }
  16194. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  16195. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))) && \
  16196. !defined(HAVE_SELFTEST)
  16197. if (ret == 0) {
  16198. ret = wc_ecc_set_rng(&key, &rng);
  16199. }
  16200. #endif
  16201. printf(testingFmt, "wc_ecc_shared_secret()");
  16202. if (ret == 0) {
  16203. ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
  16204. /* Test bad args. */
  16205. if (ret == 0) {
  16206. ret = wc_ecc_shared_secret(NULL, &pubKey, out, &outlen);
  16207. if (ret == BAD_FUNC_ARG) {
  16208. ret = wc_ecc_shared_secret(&key, NULL, out, &outlen);
  16209. }
  16210. if (ret == BAD_FUNC_ARG) {
  16211. ret = wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen);
  16212. }
  16213. if (ret == BAD_FUNC_ARG) {
  16214. ret = wc_ecc_shared_secret(&key, &pubKey, out, NULL);
  16215. }
  16216. if (ret == BAD_FUNC_ARG) {
  16217. ret = 0;
  16218. } else if (ret == 0) {
  16219. ret = WOLFSSL_FATAL_ERROR;
  16220. }
  16221. }
  16222. }
  16223. printf(resultFmt, ret == 0 ? passed : failed);
  16224. if (wc_FreeRng(&rng) && ret == 0) {
  16225. ret = WOLFSSL_FATAL_ERROR;
  16226. }
  16227. wc_ecc_free(&key);
  16228. wc_ecc_free(&pubKey);
  16229. #ifdef FP_ECC
  16230. wc_ecc_fp_free();
  16231. #endif
  16232. #endif
  16233. return ret;
  16234. } /* END tests_wc_ecc_shared_secret */
  16235. /*
  16236. * testint wc_ecc_export_x963()
  16237. */
  16238. static int test_wc_ecc_export_x963 (void)
  16239. {
  16240. int ret = 0;
  16241. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  16242. ecc_key key;
  16243. WC_RNG rng;
  16244. byte out[ECC_ASN963_MAX_BUF_SZ];
  16245. word32 outlen = sizeof(out);
  16246. /* Initialize variables. */
  16247. XMEMSET(out, 0, outlen);
  16248. XMEMSET(&rng, 0, sizeof(rng));
  16249. XMEMSET(&key, 0, sizeof(key));
  16250. ret = wc_InitRng(&rng);
  16251. if (ret == 0) {
  16252. ret = wc_ecc_init(&key);
  16253. if (ret == 0) {
  16254. ret = wc_ecc_make_key(&rng, KEY20, &key);
  16255. }
  16256. }
  16257. printf(testingFmt, "wc_ecc_export_x963()");
  16258. if (ret == 0) {
  16259. ret = wc_ecc_export_x963(&key, out, &outlen);
  16260. }
  16261. /* Test bad args. */
  16262. if (ret == 0) {
  16263. ret = wc_ecc_export_x963(NULL, out, &outlen);
  16264. if (ret == ECC_BAD_ARG_E) {
  16265. ret = wc_ecc_export_x963(&key, NULL, &outlen);
  16266. }
  16267. if (ret == LENGTH_ONLY_E) {
  16268. ret = wc_ecc_export_x963(&key, out, NULL);
  16269. }
  16270. if (ret == ECC_BAD_ARG_E) {
  16271. key.idx = -4;
  16272. ret = wc_ecc_export_x963(&key, out, &outlen);
  16273. }
  16274. if (ret == ECC_BAD_ARG_E) {
  16275. ret = 0;
  16276. } else {
  16277. ret = WOLFSSL_FATAL_ERROR;
  16278. }
  16279. }
  16280. printf(resultFmt, ret == 0 ? passed : failed);
  16281. if (wc_FreeRng(&rng) && ret == 0) {
  16282. ret = WOLFSSL_FATAL_ERROR;
  16283. }
  16284. wc_ecc_free(&key);
  16285. #ifdef FP_ECC
  16286. wc_ecc_fp_free();
  16287. #endif
  16288. #endif
  16289. return ret;
  16290. } /* END test_wc_ecc_export_x963 */
  16291. /*
  16292. * Testing wc_ecc_export_x963_ex()
  16293. * compile with --enable-compkey will use compression.
  16294. */
  16295. static int test_wc_ecc_export_x963_ex (void)
  16296. {
  16297. int ret = 0;
  16298. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  16299. ecc_key key;
  16300. WC_RNG rng;
  16301. byte out[ECC_ASN963_MAX_BUF_SZ];
  16302. word32 outlen = sizeof(out);
  16303. #ifdef HAVE_COMP_KEY
  16304. word32 badOutLen = 5;
  16305. #endif
  16306. /* Init stack variables. */
  16307. XMEMSET(out, 0, outlen);
  16308. XMEMSET(&rng, 0, sizeof(rng));
  16309. XMEMSET(&key, 0, sizeof(key));
  16310. ret = wc_InitRng(&rng);
  16311. if (ret == 0) {
  16312. ret = wc_ecc_init(&key);
  16313. if (ret == 0) {
  16314. ret = wc_ecc_make_key(&rng, KEY64, &key);
  16315. }
  16316. }
  16317. printf(testingFmt, "wc_ecc_export_x963_ex()");
  16318. #ifdef HAVE_COMP_KEY
  16319. if (ret == 0) {
  16320. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  16321. }
  16322. #else
  16323. if (ret == 0) {
  16324. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  16325. }
  16326. #endif
  16327. /* Test bad args. */
  16328. #ifdef HAVE_COMP_KEY
  16329. if (ret == 0) {
  16330. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, COMP);
  16331. if (ret == BAD_FUNC_ARG) {
  16332. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP);
  16333. }
  16334. if (ret == BAD_FUNC_ARG) {
  16335. ret = wc_ecc_export_x963_ex(&key, out, NULL, COMP);
  16336. }
  16337. if (ret == BAD_FUNC_ARG) {
  16338. ret = wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP);
  16339. }
  16340. if (ret == BUFFER_E) {
  16341. key.idx = -4;
  16342. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  16343. }
  16344. if (ret == ECC_BAD_ARG_E) {
  16345. ret = 0;
  16346. } else {
  16347. ret = WOLFSSL_FATAL_ERROR;
  16348. }
  16349. }
  16350. #else
  16351. if (ret == 0) {
  16352. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP);
  16353. if (ret == BAD_FUNC_ARG) {
  16354. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP);
  16355. }
  16356. if (ret == BAD_FUNC_ARG) {
  16357. ret = wc_ecc_export_x963_ex(&key, out, &outlen, 1);
  16358. }
  16359. if (ret == NOT_COMPILED_IN) {
  16360. ret = wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP);
  16361. }
  16362. if (ret == BAD_FUNC_ARG) {
  16363. key.idx = -4;
  16364. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  16365. }
  16366. if (ret == ECC_BAD_ARG_E) {
  16367. ret = 0;
  16368. } else if (ret == 0) {
  16369. ret = WOLFSSL_FATAL_ERROR;
  16370. }
  16371. }
  16372. #endif
  16373. printf(resultFmt, ret == 0 ? passed : failed);
  16374. if (wc_FreeRng(&rng) && ret == 0) {
  16375. ret = WOLFSSL_FATAL_ERROR;
  16376. }
  16377. wc_ecc_free(&key);
  16378. #ifdef FP_ECC
  16379. wc_ecc_fp_free();
  16380. #endif
  16381. #endif
  16382. return ret;
  16383. } /* END test_wc_ecc_export_x963_ex */
  16384. /*
  16385. * testing wc_ecc_import_x963()
  16386. */
  16387. static int test_wc_ecc_import_x963 (void)
  16388. {
  16389. int ret = 0;
  16390. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  16391. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  16392. ecc_key pubKey, key;
  16393. WC_RNG rng;
  16394. byte x963[ECC_ASN963_MAX_BUF_SZ];
  16395. word32 x963Len = (word32)sizeof(x963);
  16396. /* Init stack variables. */
  16397. XMEMSET(x963, 0, x963Len);
  16398. XMEMSET(&rng, 0, sizeof(rng));
  16399. XMEMSET(&key, 0, sizeof(key));
  16400. XMEMSET(&pubKey, 0, sizeof(pubKey));
  16401. ret = wc_InitRng(&rng);
  16402. if (ret == 0) {
  16403. ret = wc_ecc_init(&pubKey);
  16404. if (ret == 0) {
  16405. ret = wc_ecc_init(&key);
  16406. }
  16407. if (ret == 0) {
  16408. ret = wc_ecc_make_key(&rng, KEY24, &key);
  16409. }
  16410. if (ret == 0) {
  16411. ret = wc_ecc_export_x963(&key, x963, &x963Len);
  16412. }
  16413. }
  16414. printf(testingFmt, "wc_ecc_import_x963()");
  16415. if (ret == 0) {
  16416. ret = wc_ecc_import_x963(x963, x963Len, &pubKey);
  16417. }
  16418. /* Test bad args. */
  16419. if (ret == 0) {
  16420. ret = wc_ecc_import_x963(NULL, x963Len, &pubKey);
  16421. if (ret == BAD_FUNC_ARG) {
  16422. ret = wc_ecc_import_x963(x963, x963Len, NULL);
  16423. }
  16424. if (ret == BAD_FUNC_ARG) {
  16425. ret = wc_ecc_import_x963(x963, x963Len + 1, &pubKey);
  16426. }
  16427. if (ret == ECC_BAD_ARG_E) {
  16428. ret = 0;
  16429. } else if (ret == 0) {
  16430. ret = WOLFSSL_FATAL_ERROR;
  16431. }
  16432. }
  16433. printf(resultFmt, ret == 0 ? passed : failed);
  16434. if (wc_FreeRng(&rng) && ret == 0) {
  16435. ret = WOLFSSL_FATAL_ERROR;
  16436. }
  16437. wc_ecc_free(&key);
  16438. wc_ecc_free(&pubKey);
  16439. #ifdef FP_ECC
  16440. wc_ecc_fp_free();
  16441. #endif
  16442. #endif
  16443. return ret;
  16444. } /* END wc_ecc_import_x963 */
  16445. /*
  16446. * testing wc_ecc_import_private_key()
  16447. */
  16448. static int ecc_import_private_key (void)
  16449. {
  16450. int ret = 0;
  16451. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  16452. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  16453. ecc_key key, keyImp;
  16454. WC_RNG rng;
  16455. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  16456. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  16457. word32 privKeySz = (word32)sizeof(privKey);
  16458. word32 x963KeySz = (word32)sizeof(x963Key);
  16459. /* Init stack variables. */
  16460. XMEMSET(privKey, 0, privKeySz);
  16461. XMEMSET(x963Key, 0, x963KeySz);
  16462. XMEMSET(&rng, 0, sizeof(rng));
  16463. XMEMSET(&key, 0, sizeof(key));
  16464. XMEMSET(&keyImp, 0, sizeof(keyImp));
  16465. ret = wc_InitRng(&rng);
  16466. if (ret == 0) {
  16467. ret = wc_ecc_init(&key);
  16468. if (ret == 0) {
  16469. ret = wc_ecc_init(&keyImp);
  16470. }
  16471. if (ret == 0) {
  16472. ret = wc_ecc_make_key(&rng, KEY48, &key);
  16473. }
  16474. if (ret == 0) {
  16475. ret = wc_ecc_export_x963(&key, x963Key, &x963KeySz);
  16476. }
  16477. if (ret == 0) {
  16478. ret = wc_ecc_export_private_only(&key, privKey, &privKeySz);
  16479. }
  16480. }
  16481. printf(testingFmt, "wc_ecc_import_private_key()");
  16482. if (ret == 0) {
  16483. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  16484. x963KeySz, &keyImp);
  16485. }
  16486. /* Pass in bad args. */
  16487. if (ret == 0) {
  16488. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  16489. x963KeySz, NULL);
  16490. if (ret == BAD_FUNC_ARG) {
  16491. ret = wc_ecc_import_private_key(NULL, privKeySz, x963Key,
  16492. x963KeySz, &keyImp);
  16493. }
  16494. if (ret == BAD_FUNC_ARG) {
  16495. ret = 0;
  16496. } else if (ret == 0) {
  16497. ret = WOLFSSL_FATAL_ERROR;
  16498. }
  16499. }
  16500. printf(resultFmt, ret == 0 ? passed : failed);
  16501. if (wc_FreeRng(&rng) && ret == 0) {
  16502. ret = WOLFSSL_FATAL_ERROR;
  16503. }
  16504. wc_ecc_free(&key);
  16505. wc_ecc_free(&keyImp);
  16506. #ifdef FP_ECC
  16507. wc_ecc_fp_free();
  16508. #endif
  16509. #endif
  16510. return ret;
  16511. } /* END wc_ecc_import_private_key */
  16512. /*
  16513. * Testing wc_ecc_export_private_only()
  16514. */
  16515. static int test_wc_ecc_export_private_only (void)
  16516. {
  16517. int ret = 0;
  16518. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  16519. ecc_key key;
  16520. WC_RNG rng;
  16521. byte out[ECC_PRIV_KEY_BUF];
  16522. word32 outlen = sizeof(out);
  16523. /* Init stack variables. */
  16524. XMEMSET(out, 0, outlen);
  16525. XMEMSET(&rng, 0, sizeof(rng));
  16526. XMEMSET(&key, 0, sizeof(key));
  16527. ret = wc_InitRng(&rng);
  16528. if (ret == 0) {
  16529. ret = wc_ecc_init(&key);
  16530. if (ret == 0) {
  16531. ret = wc_ecc_make_key(&rng, KEY32, &key);
  16532. }
  16533. }
  16534. printf(testingFmt, "wc_ecc_export_private_only()");
  16535. if (ret == 0) {
  16536. ret = wc_ecc_export_private_only(&key, out, &outlen);
  16537. }
  16538. /* Pass in bad args. */
  16539. if (ret == 0) {
  16540. ret = wc_ecc_export_private_only(NULL, out, &outlen);
  16541. if (ret == BAD_FUNC_ARG) {
  16542. ret = wc_ecc_export_private_only(&key, NULL, &outlen);
  16543. }
  16544. if (ret == BAD_FUNC_ARG) {
  16545. ret = wc_ecc_export_private_only(&key, out, NULL);
  16546. }
  16547. if (ret == BAD_FUNC_ARG) {
  16548. ret = 0;
  16549. } else if (ret == 0) {
  16550. ret = WOLFSSL_FATAL_ERROR;
  16551. }
  16552. }
  16553. printf(resultFmt, ret == 0 ? passed : failed);
  16554. if (wc_FreeRng(&rng) && ret == 0) {
  16555. ret = WOLFSSL_FATAL_ERROR;
  16556. }
  16557. wc_ecc_free(&key);
  16558. #ifdef FP_ECC
  16559. wc_ecc_fp_free();
  16560. #endif
  16561. #endif
  16562. return ret;
  16563. } /* END test_wc_ecc_export_private_only */
  16564. /*
  16565. * Testing wc_ecc_rs_to_sig()
  16566. */
  16567. static int test_wc_ecc_rs_to_sig (void)
  16568. {
  16569. int ret = 0;
  16570. #if defined(HAVE_ECC) && !defined(NO_ASN)
  16571. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  16572. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  16573. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  16574. byte sig[ECC_MAX_SIG_SIZE];
  16575. word32 siglen = (word32)sizeof(sig);
  16576. /*R and S max size is the order of curve. 2^192.*/
  16577. int keySz = KEY24;
  16578. byte r[KEY24];
  16579. byte s[KEY24];
  16580. word32 rlen = (word32)sizeof(r);
  16581. word32 slen = (word32)sizeof(s);
  16582. /* Init stack variables. */
  16583. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  16584. XMEMSET(r, 0, keySz);
  16585. XMEMSET(s, 0, keySz);
  16586. printf(testingFmt, "wc_ecc_rs_to_sig()");
  16587. ret = wc_ecc_rs_to_sig(R, S, sig, &siglen);
  16588. /* Test bad args. */
  16589. if (ret == 0) {
  16590. ret = wc_ecc_rs_to_sig(NULL, S, sig, &siglen);
  16591. if (ret == ECC_BAD_ARG_E) {
  16592. ret = wc_ecc_rs_to_sig(R, NULL, sig, &siglen);
  16593. }
  16594. if (ret == ECC_BAD_ARG_E) {
  16595. ret = wc_ecc_rs_to_sig(R, S, sig, NULL);
  16596. }
  16597. if (ret == ECC_BAD_ARG_E) {
  16598. ret = wc_ecc_rs_to_sig(R, S, NULL, &siglen);
  16599. }
  16600. if (ret == ECC_BAD_ARG_E) {
  16601. ret = 0;
  16602. } else {
  16603. ret = WOLFSSL_FATAL_ERROR;
  16604. }
  16605. }
  16606. printf(resultFmt, ret == 0 ? passed : failed);
  16607. printf(testingFmt, "wc_ecc_sig_to_rs()");
  16608. if (ret == 0) {
  16609. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen);
  16610. }
  16611. /* Test bad args. */
  16612. if (ret == 0) {
  16613. ret = wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen);
  16614. if (ret == ECC_BAD_ARG_E) {
  16615. ret = wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen);
  16616. }
  16617. if (ret == ECC_BAD_ARG_E) {
  16618. ret = wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen);
  16619. }
  16620. if (ret == ECC_BAD_ARG_E) {
  16621. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen);
  16622. }
  16623. if (ret == ECC_BAD_ARG_E) {
  16624. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL);
  16625. }
  16626. if (ret == ECC_BAD_ARG_E) {
  16627. ret = 0;
  16628. } else if (ret == 0) {
  16629. ret = WOLFSSL_FATAL_ERROR;
  16630. }
  16631. }
  16632. printf(resultFmt, ret == 0 ? passed : failed);
  16633. #endif
  16634. return ret;
  16635. } /* END test_wc_ecc_rs_to_sig */
  16636. static int test_wc_ecc_import_raw(void)
  16637. {
  16638. int ret = 0;
  16639. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  16640. ecc_key key;
  16641. const char* qx =
  16642. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  16643. const char* qy =
  16644. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  16645. const char* d =
  16646. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  16647. const char* curveName = "SECP256R1";
  16648. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  16649. const char* kNullStr = "";
  16650. #endif
  16651. ret = wc_ecc_init(&key);
  16652. printf(testingFmt, "wc_ecc_import_raw()");
  16653. if (ret == 0) {
  16654. ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
  16655. }
  16656. /* Test bad args. */
  16657. if (ret == 0) {
  16658. ret = wc_ecc_import_raw(NULL, qx, qy, d, curveName);
  16659. if (ret == BAD_FUNC_ARG) {
  16660. ret = wc_ecc_import_raw(&key, NULL, qy, d, curveName);
  16661. }
  16662. if (ret == BAD_FUNC_ARG) {
  16663. ret = wc_ecc_import_raw(&key, qx, NULL, d, curveName);
  16664. }
  16665. if (ret == BAD_FUNC_ARG) {
  16666. ret = wc_ecc_import_raw(&key, qx, qy, d, NULL);
  16667. }
  16668. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  16669. if (ret == BAD_FUNC_ARG) {
  16670. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  16671. wc_ecc_free(&key);
  16672. #endif
  16673. ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr, curveName);
  16674. if (ret == ECC_INF_E)
  16675. ret = BAD_FUNC_ARG; /* This is expected by other tests */
  16676. }
  16677. #endif
  16678. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  16679. if (ret == BAD_FUNC_ARG) {
  16680. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  16681. wc_ecc_free(&key);
  16682. #endif
  16683. ret = wc_ecc_import_raw(&key, "0", qy, d, curveName);
  16684. }
  16685. if (ret == BAD_FUNC_ARG) {
  16686. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  16687. wc_ecc_free(&key);
  16688. #endif
  16689. ret = wc_ecc_import_raw(&key, qx, "0", d, curveName);
  16690. }
  16691. #endif
  16692. if (ret == BAD_FUNC_ARG) {
  16693. ret = 0;
  16694. }
  16695. }
  16696. printf(resultFmt, ret == 0 ? passed : failed);
  16697. wc_ecc_free(&key);
  16698. #endif
  16699. return ret;
  16700. } /* END test_wc_ecc_import_raw */
  16701. static int test_wc_ecc_import_unsigned(void)
  16702. {
  16703. int ret = 0;
  16704. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  16705. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  16706. ecc_key key;
  16707. const byte qx[] = {
  16708. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  16709. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  16710. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  16711. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  16712. };
  16713. const byte qy[] = {
  16714. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  16715. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  16716. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  16717. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  16718. };
  16719. const byte d[] = {
  16720. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  16721. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  16722. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  16723. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  16724. };
  16725. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  16726. const byte nullBytes[32] = {0};
  16727. #endif
  16728. int curveId = ECC_SECP256R1;
  16729. ret = wc_ecc_init(&key);
  16730. printf(testingFmt, "wc_ecc_import_unsigned()");
  16731. if (ret == 0) {
  16732. ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  16733. curveId);
  16734. }
  16735. /* Test bad args. */
  16736. if (ret == 0) {
  16737. ret = wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  16738. curveId);
  16739. if (ret == BAD_FUNC_ARG) {
  16740. ret = wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  16741. curveId);
  16742. }
  16743. if (ret == BAD_FUNC_ARG) {
  16744. ret = wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  16745. curveId);
  16746. }
  16747. if (ret == BAD_FUNC_ARG) {
  16748. ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  16749. ECC_CURVE_INVALID);
  16750. }
  16751. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  16752. if (ret == BAD_FUNC_ARG) {
  16753. ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  16754. (byte*)nullBytes, (byte*)nullBytes, curveId);
  16755. }
  16756. #endif
  16757. if (ret == BAD_FUNC_ARG || ret == ECC_INF_E) {
  16758. ret = 0;
  16759. }
  16760. }
  16761. printf(resultFmt, ret == 0 ? passed : failed);
  16762. wc_ecc_free(&key);
  16763. #endif
  16764. return ret;
  16765. } /* END test_wc_ecc_import_unsigned */
  16766. /*
  16767. * Testing wc_ecc_sig_size()
  16768. */
  16769. static int test_wc_ecc_sig_size (void)
  16770. {
  16771. int ret = 0;
  16772. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  16773. ecc_key key;
  16774. WC_RNG rng;
  16775. int keySz = KEY16;
  16776. XMEMSET(&rng, 0, sizeof(rng));
  16777. XMEMSET(&key, 0, sizeof(key));
  16778. ret = wc_InitRng(&rng);
  16779. if (ret == 0) {
  16780. ret = wc_ecc_init(&key);
  16781. if (ret == 0) {
  16782. ret = wc_ecc_make_key(&rng, keySz, &key);
  16783. }
  16784. }
  16785. printf(testingFmt, "wc_ecc_sig_size()");
  16786. if (ret == 0) {
  16787. ret = wc_ecc_sig_size(&key);
  16788. if (ret <= (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ)) {
  16789. ret = 0;
  16790. }
  16791. }
  16792. printf(resultFmt, ret == 0 ? passed : failed);
  16793. if (wc_FreeRng(&rng) && ret == 0) {
  16794. ret = WOLFSSL_FATAL_ERROR;
  16795. }
  16796. wc_ecc_free(&key);
  16797. #endif
  16798. return ret;
  16799. } /* END test_wc_ecc_sig_size */
  16800. /*
  16801. * Testing wc_ecc_ctx_new()
  16802. */
  16803. static int test_wc_ecc_ctx_new (void)
  16804. {
  16805. int ret = 0;
  16806. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  16807. WC_RNG rng;
  16808. ecEncCtx* cli = NULL;
  16809. ecEncCtx* srv = NULL;
  16810. ret = wc_InitRng(&rng);
  16811. printf(testingFmt, "wc_ecc_ctx_new()");
  16812. if (ret == 0) {
  16813. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
  16814. srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng);
  16815. }
  16816. if (ret == 0 && (cli == NULL || srv == NULL)) {
  16817. ret = WOLFSSL_FATAL_ERROR;
  16818. }
  16819. wc_ecc_ctx_free(cli);
  16820. wc_ecc_ctx_free(srv);
  16821. /* Test bad args. */
  16822. if (ret == 0) {
  16823. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  16824. cli = wc_ecc_ctx_new(0, &rng);
  16825. if (cli != NULL) {
  16826. ret = WOLFSSL_FATAL_ERROR;
  16827. }
  16828. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL);
  16829. if (cli != NULL) {
  16830. ret = WOLFSSL_FATAL_ERROR;
  16831. }
  16832. }
  16833. printf(resultFmt, ret == 0 ? passed : failed);
  16834. if (wc_FreeRng(&rng) && ret == 0) {
  16835. ret = WOLFSSL_FATAL_ERROR;
  16836. }
  16837. wc_ecc_ctx_free(cli);
  16838. #endif
  16839. return ret;
  16840. } /* END test_wc_ecc_ctx_new */
  16841. /*
  16842. * Tesing wc_ecc_reset()
  16843. */
  16844. static int test_wc_ecc_ctx_reset (void)
  16845. {
  16846. int ret = 0;
  16847. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  16848. ecEncCtx* ctx = NULL;
  16849. WC_RNG rng;
  16850. ret = wc_InitRng(&rng);
  16851. if (ret == 0) {
  16852. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) {
  16853. ret = WOLFSSL_FATAL_ERROR;
  16854. }
  16855. }
  16856. printf(testingFmt, "wc_ecc_ctx_reset()");
  16857. if (ret == 0) {
  16858. ret = wc_ecc_ctx_reset(ctx, &rng);
  16859. }
  16860. /* Pass in bad args. */
  16861. if (ret == 0) {
  16862. ret = wc_ecc_ctx_reset(NULL, &rng);
  16863. if (ret == BAD_FUNC_ARG) {
  16864. ret = wc_ecc_ctx_reset(ctx, NULL);
  16865. }
  16866. if (ret == BAD_FUNC_ARG) {
  16867. ret = 0;
  16868. } else if (ret == 0) {
  16869. ret = WOLFSSL_FATAL_ERROR;
  16870. }
  16871. }
  16872. printf(resultFmt, ret == 0 ? passed : failed);
  16873. if (wc_FreeRng(&rng) && ret == 0) {
  16874. ret = WOLFSSL_FATAL_ERROR;
  16875. }
  16876. wc_ecc_ctx_free(ctx);
  16877. #endif
  16878. return ret;
  16879. } /* END test_wc_ecc_ctx_reset */
  16880. /*
  16881. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  16882. */
  16883. static int test_wc_ecc_ctx_set_peer_salt (void)
  16884. {
  16885. int ret = 0;
  16886. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  16887. WC_RNG rng;
  16888. ecEncCtx* cliCtx = NULL;
  16889. ecEncCtx* servCtx = NULL;
  16890. const byte* cliSalt = NULL;
  16891. const byte* servSalt = NULL;
  16892. ret = wc_InitRng(&rng);
  16893. if (ret == 0) {
  16894. if ( ( (cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) ||
  16895. ( (servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng)) == NULL) ) {
  16896. ret = WOLFSSL_FATAL_ERROR;
  16897. }
  16898. }
  16899. printf(testingFmt, "wc_ecc_ctx_get_own_salt()");
  16900. /* Test bad args. */
  16901. if (ret == 0) {
  16902. cliSalt = wc_ecc_ctx_get_own_salt(NULL);
  16903. if (cliSalt != NULL) {
  16904. ret = WOLFSSL_FATAL_ERROR;
  16905. }
  16906. }
  16907. if (ret == 0) {
  16908. cliSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  16909. servSalt = wc_ecc_ctx_get_own_salt(servCtx);
  16910. if (cliSalt == NULL || servSalt == NULL) {
  16911. ret = WOLFSSL_FATAL_ERROR;
  16912. }
  16913. }
  16914. printf(resultFmt, ret == 0 ? passed : failed);
  16915. printf(testingFmt, "wc_ecc_ctx_set_peer_salt()");
  16916. if (ret == 0) {
  16917. ret = wc_ecc_ctx_set_peer_salt(cliCtx, servSalt);
  16918. }
  16919. /* Test bad args. */
  16920. if (ret == 0) {
  16921. ret = wc_ecc_ctx_set_peer_salt(NULL, servSalt);
  16922. if (ret == BAD_FUNC_ARG) {
  16923. ret = wc_ecc_ctx_set_peer_salt(cliCtx, NULL);
  16924. }
  16925. if (ret == BAD_FUNC_ARG) {
  16926. ret = 0;
  16927. } else if (ret == 0) {
  16928. ret = WOLFSSL_FATAL_ERROR;
  16929. }
  16930. }
  16931. printf(resultFmt, ret == 0 ? passed : failed);
  16932. if (wc_FreeRng(&rng) && ret == 0) {
  16933. ret = WOLFSSL_FATAL_ERROR;
  16934. }
  16935. wc_ecc_ctx_free(cliCtx);
  16936. wc_ecc_ctx_free(servCtx);
  16937. #endif
  16938. return ret;
  16939. } /* END test_wc_ecc_ctx_set_peer_salt */
  16940. /*
  16941. * Testing wc_ecc_ctx_set_info()
  16942. */
  16943. static int test_wc_ecc_ctx_set_info (void)
  16944. {
  16945. int ret = 0;
  16946. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  16947. ecEncCtx* ctx = NULL;
  16948. WC_RNG rng;
  16949. const char* optInfo = "Optional Test Info.";
  16950. int optInfoSz = (int)XSTRLEN(optInfo);
  16951. const char* badOptInfo = NULL;
  16952. ret = wc_InitRng(&rng);
  16953. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL || ret != 0 ) {
  16954. ret = WOLFSSL_FATAL_ERROR;
  16955. }
  16956. printf(testingFmt, "wc_ecc_ctx_set_info()");
  16957. if (ret == 0) {
  16958. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz);
  16959. }
  16960. /* Test bad args. */
  16961. if (ret == 0) {
  16962. ret = wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz);
  16963. if (ret == BAD_FUNC_ARG) {
  16964. ret = wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz);
  16965. }
  16966. if (ret == BAD_FUNC_ARG) {
  16967. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1);
  16968. }
  16969. if (ret == BAD_FUNC_ARG) {
  16970. ret = 0;
  16971. } else if (ret == 0) {
  16972. ret = WOLFSSL_FATAL_ERROR;
  16973. }
  16974. }
  16975. printf(resultFmt, ret == 0 ? passed : failed);
  16976. if (wc_FreeRng(&rng) && ret == 0) {
  16977. ret = WOLFSSL_FATAL_ERROR;
  16978. }
  16979. wc_ecc_ctx_free(ctx);
  16980. #endif
  16981. return ret;
  16982. } /* END test_wc_ecc_ctx_set_info */
  16983. /*
  16984. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  16985. */
  16986. static int test_wc_ecc_encryptDecrypt (void)
  16987. {
  16988. int ret = 0;
  16989. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && defined(WOLFSSL_AES_128) \
  16990. && !defined(WC_NO_RNG)
  16991. ecc_key srvKey, cliKey;
  16992. WC_RNG rng;
  16993. const char* msg = "EccBlock Size 16";
  16994. word32 msgSz = (word32)XSTRLEN(msg);
  16995. byte out[XSTRLEN(msg) + WC_SHA256_DIGEST_SIZE];
  16996. word32 outSz = (word32)sizeof(out);
  16997. byte plain[XSTRLEN(msg) + 1];
  16998. word32 plainSz = (word32)sizeof(plain);
  16999. int keySz = KEY20;
  17000. /* Init stack variables. */
  17001. XMEMSET(out, 0, outSz);
  17002. XMEMSET(plain, 0, plainSz);
  17003. XMEMSET(&rng, 0, sizeof(rng));
  17004. XMEMSET(&srvKey, 0, sizeof(srvKey));
  17005. XMEMSET(&cliKey, 0, sizeof(cliKey));
  17006. ret = wc_InitRng(&rng);
  17007. if (ret == 0) {
  17008. ret = wc_ecc_init(&cliKey);
  17009. if (ret == 0) {
  17010. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  17011. }
  17012. if (ret == 0) {
  17013. ret = wc_ecc_init(&srvKey);
  17014. }
  17015. if (ret == 0) {
  17016. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  17017. }
  17018. }
  17019. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  17020. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))) && \
  17021. !defined(HAVE_SELFTEST)
  17022. if (ret == 0) {
  17023. ret = wc_ecc_set_rng(&srvKey, &rng);
  17024. }
  17025. if (ret == 0) {
  17026. ret = wc_ecc_set_rng(&cliKey, &rng);
  17027. }
  17028. #endif
  17029. printf(testingFmt, "wc_ecc_encrypt()");
  17030. if (ret == 0) {
  17031. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  17032. &outSz, NULL);
  17033. }
  17034. if (ret == 0) {
  17035. ret = wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out,
  17036. &outSz, NULL);
  17037. if (ret == BAD_FUNC_ARG) {
  17038. ret = wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out,
  17039. &outSz, NULL);
  17040. }
  17041. if (ret == BAD_FUNC_ARG) {
  17042. ret = wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out,
  17043. &outSz, NULL);
  17044. }
  17045. if (ret == BAD_FUNC_ARG) {
  17046. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  17047. &outSz, NULL);
  17048. }
  17049. if (ret == BAD_FUNC_ARG) {
  17050. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  17051. NULL, NULL);
  17052. }
  17053. if (ret == BAD_FUNC_ARG) {
  17054. ret = 0;
  17055. } else if (ret == 0) {
  17056. ret = WOLFSSL_FATAL_ERROR;
  17057. }
  17058. }
  17059. printf(resultFmt, ret == 0 ? passed : failed);
  17060. printf(testingFmt, "wc_ecc_decrypt()");
  17061. if (ret == 0) {
  17062. ret = wc_ecc_decrypt(&srvKey, &cliKey, out, outSz, plain,
  17063. &plainSz, NULL);
  17064. }
  17065. if (ret == 0) {
  17066. ret = wc_ecc_decrypt(NULL, &cliKey, out, outSz, plain,
  17067. &plainSz, NULL);
  17068. if (ret == BAD_FUNC_ARG) {
  17069. ret = wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain,
  17070. &plainSz, NULL);
  17071. }
  17072. if (ret == BAD_FUNC_ARG) {
  17073. ret = wc_ecc_decrypt(&srvKey, &cliKey, NULL, outSz, plain,
  17074. &plainSz, NULL);
  17075. }
  17076. if (ret == BAD_FUNC_ARG) {
  17077. ret = wc_ecc_decrypt(&srvKey, &cliKey, out, outSz, NULL,
  17078. &plainSz, NULL);
  17079. }
  17080. if (ret == BAD_FUNC_ARG) {
  17081. ret = wc_ecc_decrypt(&srvKey, &cliKey, out, outSz,
  17082. plain, NULL, NULL);
  17083. }
  17084. if (ret == BAD_FUNC_ARG) {
  17085. ret = 0;
  17086. } else if (ret == 0) {
  17087. ret = WOLFSSL_FATAL_ERROR;
  17088. }
  17089. }
  17090. if (XMEMCMP(msg, plain, msgSz) != 0) {
  17091. ret = WOLFSSL_FATAL_ERROR;
  17092. }
  17093. printf(resultFmt, ret == 0 ? passed : failed);
  17094. if (wc_FreeRng(&rng) && ret == 0) {
  17095. ret = WOLFSSL_FATAL_ERROR;
  17096. }
  17097. wc_ecc_free(&cliKey);
  17098. wc_ecc_free(&srvKey);
  17099. #endif
  17100. return ret;
  17101. } /* END test_wc_ecc_encryptDecrypt */
  17102. /*
  17103. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  17104. */
  17105. static int test_wc_ecc_del_point (void)
  17106. {
  17107. int ret = 0;
  17108. #if defined(HAVE_ECC)
  17109. ecc_point* pt;
  17110. printf(testingFmt, "wc_ecc_new_point()");
  17111. pt = wc_ecc_new_point();
  17112. if (!pt) {
  17113. ret = WOLFSSL_FATAL_ERROR;
  17114. }
  17115. printf(resultFmt, ret == 0 ? passed : failed);
  17116. wc_ecc_del_point(pt);
  17117. #endif
  17118. return ret;
  17119. } /* END test_wc_ecc_del_point */
  17120. /*
  17121. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  17122. * wc_ecc_import_point_der(), wc_ecc_copy_point(), and wc_ecc_cmp_point()
  17123. */
  17124. static int test_wc_ecc_pointFns (void)
  17125. {
  17126. int ret = 0;
  17127. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  17128. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  17129. !defined(WOLFSSL_ATECC608A)
  17130. ecc_key key;
  17131. WC_RNG rng;
  17132. ecc_point* point = NULL;
  17133. ecc_point* cpypt = NULL;
  17134. int idx = 0;
  17135. int keySz = KEY32;
  17136. byte der[DER_SZ(KEY32)];
  17137. word32 derlenChk = 0;
  17138. word32 derSz = DER_SZ(KEY32);
  17139. /* Init stack variables. */
  17140. XMEMSET(der, 0, derSz);
  17141. XMEMSET(&rng, 0, sizeof(rng));
  17142. XMEMSET(&key, 0, sizeof(key));
  17143. ret = wc_InitRng(&rng);
  17144. if (ret == 0) {
  17145. ret = wc_ecc_init(&key);
  17146. if (ret == 0) {
  17147. ret = wc_ecc_make_key(&rng, keySz, &key);
  17148. }
  17149. }
  17150. if (ret == 0) {
  17151. point = wc_ecc_new_point();
  17152. if (!point) {
  17153. ret = WOLFSSL_FATAL_ERROR;
  17154. }
  17155. }
  17156. if (ret == 0) {
  17157. cpypt = wc_ecc_new_point();
  17158. if (!cpypt) {
  17159. ret = WOLFSSL_FATAL_ERROR;
  17160. }
  17161. }
  17162. /* Export */
  17163. printf(testingFmt, "wc_ecc_export_point_der()");
  17164. if (ret == 0) {
  17165. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  17166. NULL, &derlenChk);
  17167. /* Check length value. */
  17168. if (derSz == derlenChk && ret == LENGTH_ONLY_E) {
  17169. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  17170. der, &derSz);
  17171. }
  17172. }
  17173. /* Test bad args. */
  17174. if (ret == 0) {
  17175. ret = wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz);
  17176. if (ret == ECC_BAD_ARG_E) {
  17177. ret = wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz);
  17178. }
  17179. if (ret == ECC_BAD_ARG_E) {
  17180. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  17181. der, NULL);
  17182. }
  17183. if (ret == ECC_BAD_ARG_E) {
  17184. ret = 0;
  17185. } else if (ret == 0) {
  17186. ret = WOLFSSL_FATAL_ERROR;
  17187. }
  17188. }
  17189. printf(resultFmt, ret == 0 ? passed : failed);
  17190. /* Import */
  17191. printf(testingFmt, "wc_ecc_import_point_der()");
  17192. if (ret == 0) {
  17193. ret = wc_ecc_import_point_der(der, derSz, idx, point);
  17194. /* Condition double checks wc_ecc_cmp_point(). */
  17195. if (ret == 0 && XMEMCMP(&key.pubkey, point, sizeof(key.pubkey))) {
  17196. ret = wc_ecc_cmp_point(&key.pubkey, point);
  17197. }
  17198. }
  17199. /* Test bad args. */
  17200. if (ret == 0) {
  17201. ret = wc_ecc_import_point_der(NULL, derSz, idx, point);
  17202. if (ret == ECC_BAD_ARG_E) {
  17203. ret = wc_ecc_import_point_der(der, derSz, idx, NULL);
  17204. }
  17205. if (ret == ECC_BAD_ARG_E) {
  17206. ret = wc_ecc_import_point_der(der, derSz, -1, point);
  17207. }
  17208. if (ret == ECC_BAD_ARG_E) {
  17209. ret = wc_ecc_import_point_der(der, derSz + 1, idx, point);
  17210. }
  17211. if (ret == ECC_BAD_ARG_E) {
  17212. ret = 0;
  17213. } else if (ret == 0) {
  17214. ret = WOLFSSL_FATAL_ERROR;
  17215. }
  17216. }
  17217. printf(resultFmt, ret == 0 ? passed : failed);
  17218. /* Copy */
  17219. printf(testingFmt, "wc_ecc_copy_point()");
  17220. if (ret == 0) {
  17221. ret = wc_ecc_copy_point(point, cpypt);
  17222. }
  17223. /* Test bad args. */
  17224. if (ret == 0) {
  17225. ret = wc_ecc_copy_point(NULL, cpypt);
  17226. if (ret == ECC_BAD_ARG_E) {
  17227. ret = wc_ecc_copy_point(point, NULL);
  17228. }
  17229. if (ret == ECC_BAD_ARG_E) {
  17230. ret = 0;
  17231. } else if (ret == 0) {
  17232. ret = WOLFSSL_FATAL_ERROR;
  17233. }
  17234. }
  17235. printf(resultFmt, ret == 0 ? passed : failed);
  17236. printf(testingFmt, "wc_ecc_cmp_point()");
  17237. /* Compare point */
  17238. if (ret == 0) {
  17239. ret = wc_ecc_cmp_point(point, cpypt);
  17240. }
  17241. /* Test bad args. */
  17242. if (ret == 0) {
  17243. ret = wc_ecc_cmp_point(NULL, cpypt);
  17244. if (ret == BAD_FUNC_ARG) {
  17245. ret = wc_ecc_cmp_point(point, NULL);
  17246. }
  17247. if (ret == BAD_FUNC_ARG) {
  17248. ret = 0;
  17249. } else if (ret == 0) {
  17250. ret = WOLFSSL_FATAL_ERROR;
  17251. }
  17252. }
  17253. printf(resultFmt, ret == 0 ? passed : failed);
  17254. printf(testingFmt, "wc_ecc_point_is_at_infinity()");
  17255. /* At infinity if return == 1, otherwise return == 0. */
  17256. if (ret == 0) {
  17257. ret = wc_ecc_point_is_at_infinity(point);
  17258. }
  17259. /* Test bad args. */
  17260. if (ret == 0) {
  17261. ret = wc_ecc_point_is_at_infinity(NULL);
  17262. if (ret == BAD_FUNC_ARG) {
  17263. ret = 0;
  17264. } else if (ret == 0) {
  17265. ret = WOLFSSL_FATAL_ERROR;
  17266. }
  17267. }
  17268. printf(resultFmt, ret == 0 ? passed : failed);
  17269. /* Free */
  17270. wc_ecc_del_point(point);
  17271. wc_ecc_del_point(cpypt);
  17272. wc_ecc_free(&key);
  17273. if (wc_FreeRng(&rng) && ret == 0) {
  17274. ret = WOLFSSL_FATAL_ERROR;
  17275. }
  17276. #endif
  17277. return ret;
  17278. } /* END test_wc_ecc_pointFns */
  17279. /*
  17280. * Testing wc_ecc_sahred_secret_ssh()
  17281. */
  17282. static int test_wc_ecc_shared_secret_ssh (void)
  17283. {
  17284. int ret = 0;
  17285. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  17286. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  17287. !defined(WOLFSSL_ATECC608A)
  17288. ecc_key key, key2;
  17289. WC_RNG rng;
  17290. int keySz = KEY32;
  17291. int key2Sz = KEY24;
  17292. byte secret[KEY32];
  17293. word32 secretLen = keySz;
  17294. /* Init stack variables. */
  17295. XMEMSET(secret, 0, secretLen);
  17296. XMEMSET(&rng, 0, sizeof(rng));
  17297. XMEMSET(&key, 0, sizeof(key));
  17298. XMEMSET(&key2, 0, sizeof(key2));
  17299. /* Make keys */
  17300. ret = wc_InitRng(&rng);
  17301. if (ret == 0) {
  17302. ret = wc_ecc_init(&key);
  17303. if (ret == 0) {
  17304. ret = wc_ecc_make_key(&rng, keySz, &key);
  17305. }
  17306. if (wc_FreeRng(&rng) && ret == 0) {
  17307. ret = WOLFSSL_FATAL_ERROR;
  17308. }
  17309. }
  17310. if (ret == 0) {
  17311. ret = wc_InitRng(&rng);
  17312. if (ret == 0) {
  17313. ret = wc_ecc_init(&key2);
  17314. }
  17315. if (ret == 0) {
  17316. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  17317. }
  17318. }
  17319. printf(testingFmt, "ecc_shared_secret_ssh()");
  17320. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  17321. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))) && \
  17322. !defined(HAVE_SELFTEST)
  17323. if (ret == 0) {
  17324. ret = wc_ecc_set_rng(&key, &rng);
  17325. }
  17326. #endif
  17327. if (ret == 0) {
  17328. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  17329. }
  17330. /* Pass in bad args. */
  17331. if (ret == 0) {
  17332. ret = wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret, &secretLen);
  17333. if (ret == BAD_FUNC_ARG) {
  17334. ret = wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen);
  17335. }
  17336. if (ret == BAD_FUNC_ARG) {
  17337. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen);
  17338. }
  17339. if (ret == BAD_FUNC_ARG) {
  17340. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL);
  17341. }
  17342. if (ret == BAD_FUNC_ARG) {
  17343. key.type = ECC_PUBLICKEY;
  17344. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  17345. if (ret == ECC_BAD_ARG_E) {
  17346. ret = 0;
  17347. } else if (ret == 0) {
  17348. ret = WOLFSSL_FATAL_ERROR;
  17349. }
  17350. } else if (ret == 0) {
  17351. ret = WOLFSSL_FATAL_ERROR;
  17352. }
  17353. }
  17354. printf(resultFmt, ret == 0 ? passed : failed);
  17355. if (wc_FreeRng(&rng) && ret == 0) {
  17356. ret = WOLFSSL_FATAL_ERROR;
  17357. }
  17358. wc_ecc_free(&key);
  17359. wc_ecc_free(&key2);
  17360. #ifdef FP_ECC
  17361. wc_ecc_fp_free();
  17362. #endif
  17363. #endif
  17364. return ret;
  17365. } /* END test_wc_ecc_shared_secret_ssh */
  17366. /*
  17367. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  17368. */
  17369. static int test_wc_ecc_verify_hash_ex (void)
  17370. {
  17371. int ret = 0;
  17372. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  17373. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  17374. !defined(WOLFSSL_ATECC608A)
  17375. ecc_key key;
  17376. WC_RNG rng;
  17377. mp_int r;
  17378. mp_int s;
  17379. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  17380. unsigned char iHash[] = "Everyone gets Friday off.......";
  17381. unsigned char shortHash[] = "Everyone gets Friday off.";
  17382. word32 hashlen = sizeof(hash);
  17383. word32 iHashLen = sizeof(iHash);
  17384. word32 shortHashLen = sizeof(shortHash);
  17385. int keySz = KEY32;
  17386. int sig = WOLFSSL_FATAL_ERROR;
  17387. int ver = WOLFSSL_FATAL_ERROR;
  17388. int verify_ok = 0;
  17389. /* Initialize r and s. */
  17390. ret = mp_init_multi(&r, &s, NULL, NULL, NULL, NULL);
  17391. if (ret != MP_OKAY) {
  17392. return MP_INIT_E;
  17393. }
  17394. ret = wc_InitRng(&rng);
  17395. if (ret == 0) {
  17396. ret = wc_ecc_init(&key);
  17397. if (ret == 0) {
  17398. ret = wc_ecc_make_key(&rng, keySz, &key);
  17399. }
  17400. }
  17401. if (ret == 0) {
  17402. ret = wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s);
  17403. if (ret == 0) {
  17404. /* verify_ok should be 1. */
  17405. ret = wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key);
  17406. if (verify_ok != 1 && ret == 0) {
  17407. ret = WOLFSSL_FATAL_ERROR;
  17408. }
  17409. }
  17410. if (ret == 0) {
  17411. /* verify_ok should be 0 */
  17412. ret = wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen,
  17413. &verify_ok, &key);
  17414. if (verify_ok != 0 && ret == 0) {
  17415. ret = WOLFSSL_FATAL_ERROR;
  17416. }
  17417. }
  17418. if (ret == 0) {
  17419. /* verify_ok should be 0. */
  17420. ret = wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  17421. &verify_ok, &key);
  17422. if (verify_ok != 0 && ret == 0) {
  17423. ret = WOLFSSL_FATAL_ERROR;
  17424. }
  17425. }
  17426. }
  17427. printf(testingFmt, "wc_ecc_sign_hash_ex()");
  17428. /* Test bad args. */
  17429. if (ret == 0) {
  17430. if (wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s)
  17431. == ECC_BAD_ARG_E) {
  17432. sig = 0;
  17433. }
  17434. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s)
  17435. != ECC_BAD_ARG_E) {
  17436. sig = WOLFSSL_FATAL_ERROR;
  17437. }
  17438. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s)
  17439. != ECC_BAD_ARG_E) {
  17440. sig = WOLFSSL_FATAL_ERROR;
  17441. }
  17442. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s)
  17443. != ECC_BAD_ARG_E) {
  17444. sig = WOLFSSL_FATAL_ERROR;
  17445. }
  17446. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL)
  17447. != ECC_BAD_ARG_E) {
  17448. sig = WOLFSSL_FATAL_ERROR;
  17449. }
  17450. }
  17451. printf(resultFmt, sig == 0 ? passed : failed);
  17452. printf(testingFmt, "wc_ecc_verify_hash_ex()");
  17453. /* Test bad args. */
  17454. if (ret == 0) {
  17455. if (wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen, &verify_ok, &key)
  17456. == ECC_BAD_ARG_E) {
  17457. ver = 0;
  17458. }
  17459. if (ver == 0 && wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  17460. &verify_ok, &key) != ECC_BAD_ARG_E) {
  17461. ver = WOLFSSL_FATAL_ERROR;
  17462. }
  17463. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  17464. &key) != ECC_BAD_ARG_E) {
  17465. ver = WOLFSSL_FATAL_ERROR;
  17466. }
  17467. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  17468. NULL, &key) != ECC_BAD_ARG_E) {
  17469. ver = WOLFSSL_FATAL_ERROR;
  17470. }
  17471. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  17472. &verify_ok, NULL) != ECC_BAD_ARG_E) {
  17473. ver = WOLFSSL_FATAL_ERROR;
  17474. }
  17475. }
  17476. printf(resultFmt, ver == 0 ? passed : failed);
  17477. wc_ecc_free(&key);
  17478. mp_free(&r);
  17479. mp_free(&s);
  17480. if (wc_FreeRng(&rng)) {
  17481. return WOLFSSL_FATAL_ERROR;
  17482. }
  17483. if (ret == 0 && (sig != 0 || ver != 0)) {
  17484. ret = WOLFSSL_FATAL_ERROR;
  17485. }
  17486. #endif
  17487. return ret;
  17488. } /* END test_wc_ecc_verify_hash_ex */
  17489. /*
  17490. * Testing wc_ecc_mulmod()
  17491. */
  17492. static int test_wc_ecc_mulmod (void)
  17493. {
  17494. int ret = 0;
  17495. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  17496. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  17497. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  17498. ecc_key key1, key2, key3;
  17499. WC_RNG rng;
  17500. ret = wc_InitRng(&rng);
  17501. if (ret == 0) {
  17502. ret = wc_ecc_init(&key1);
  17503. if (ret == 0) {
  17504. ret = wc_ecc_init(&key2);
  17505. }
  17506. if (ret == 0) {
  17507. ret = wc_ecc_init(&key3);
  17508. }
  17509. if (ret == 0) {
  17510. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  17511. }
  17512. wc_FreeRng(&rng);
  17513. }
  17514. if (ret == 0) {
  17515. ret = wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy, key1.dp->Af,
  17516. ECC_SECP256R1);
  17517. if (ret == 0) {
  17518. ret = wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  17519. key1.dp->prime, ECC_SECP256R1);
  17520. }
  17521. }
  17522. printf(testingFmt, "wc_ecc_mulmod()");
  17523. if (ret == 0) {
  17524. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey, &key2.k,
  17525. &key3.k, 1);
  17526. }
  17527. /* Test bad args. */
  17528. if (ret == 0) {
  17529. ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey, &key2.k,
  17530. &key3.k, 1);
  17531. if (ret == ECC_BAD_ARG_E) {
  17532. ret = wc_ecc_mulmod(&key1.k, NULL, &key3.pubkey, &key2.k,
  17533. &key3.k, 1);
  17534. }
  17535. if (ret == ECC_BAD_ARG_E) {
  17536. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, NULL, &key2.k,
  17537. &key3.k, 1);
  17538. }
  17539. if (ret == ECC_BAD_ARG_E) {
  17540. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey,
  17541. &key2.k, NULL, 1);
  17542. }
  17543. if (ret == ECC_BAD_ARG_E) {
  17544. ret = 0;
  17545. } else if (ret == 0) {
  17546. ret = WOLFSSL_FATAL_ERROR;
  17547. }
  17548. }
  17549. printf(resultFmt, ret == 0 ? passed : failed);
  17550. wc_ecc_free(&key1);
  17551. wc_ecc_free(&key2);
  17552. wc_ecc_free(&key3);
  17553. #ifdef FP_ECC
  17554. wc_ecc_fp_free();
  17555. #endif
  17556. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  17557. return ret;
  17558. } /* END test_wc_ecc_mulmod */
  17559. /*
  17560. * Testing wc_ecc_is_valid_idx()
  17561. */
  17562. static int test_wc_ecc_is_valid_idx (void)
  17563. {
  17564. int ret = 0;
  17565. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  17566. ecc_key key;
  17567. WC_RNG rng;
  17568. int iVal = -2;
  17569. int iVal2 = 3000;
  17570. XMEMSET(&rng, 0, sizeof(rng));
  17571. XMEMSET(&key, 0, sizeof(key));
  17572. ret = wc_InitRng(&rng);
  17573. if (ret == 0) {
  17574. ret = wc_ecc_init(&key);
  17575. if (ret == 0) {
  17576. ret = wc_ecc_make_key(&rng, 32, &key);
  17577. }
  17578. }
  17579. printf(testingFmt, "wc_ecc_is_valid_idx()");
  17580. if (ret == 0) {
  17581. ret = wc_ecc_is_valid_idx(key.idx);
  17582. if (ret == 1) {
  17583. ret = 0;
  17584. } else {
  17585. ret = WOLFSSL_FATAL_ERROR;
  17586. }
  17587. }
  17588. /* Test bad args. */
  17589. if (ret == 0) {
  17590. ret = wc_ecc_is_valid_idx(iVal); /* should return 0 */
  17591. if (ret == 0) {
  17592. ret = wc_ecc_is_valid_idx(iVal2);
  17593. }
  17594. if (ret != 0) {
  17595. ret = WOLFSSL_FATAL_ERROR;
  17596. }
  17597. }
  17598. printf(resultFmt, ret == 0 ? passed : failed);
  17599. if (wc_FreeRng(&rng) && ret == 0) {
  17600. ret = WOLFSSL_FATAL_ERROR;
  17601. }
  17602. wc_ecc_free(&key);
  17603. #ifdef FP_ECC
  17604. wc_ecc_fp_free();
  17605. #endif
  17606. #endif
  17607. return ret;
  17608. } /* END test_wc_ecc_is_valid_idx */
  17609. /*
  17610. * Testing ToTraditional
  17611. */
  17612. static int test_ToTraditional (void)
  17613. {
  17614. int ret = 0;
  17615. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  17616. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  17617. defined(OPENSSL_EXTRA_X509_SMALL))
  17618. XFILE f;
  17619. byte input[TWOK_BUF];
  17620. word32 sz;
  17621. printf(testingFmt, "ToTraditional()");
  17622. f = XFOPEN("./certs/server-keyPkcs8.der", "rb");
  17623. AssertTrue((f != XBADFILE));
  17624. sz = (word32)XFREAD(input, 1, sizeof(input), f);
  17625. XFCLOSE(f);
  17626. /* Good case */
  17627. ret = ToTraditional(input, sz);
  17628. if (ret > 0) {
  17629. ret = 0;
  17630. }
  17631. /* Bad cases */
  17632. if (ret == 0) {
  17633. ret = ToTraditional(NULL, 0);
  17634. if (ret == BAD_FUNC_ARG) {
  17635. ret = 0;
  17636. }
  17637. }
  17638. if (ret == 0) {
  17639. ret = ToTraditional(NULL, sz);
  17640. if (ret == BAD_FUNC_ARG) {
  17641. ret = 0;
  17642. }
  17643. }
  17644. if (ret == 0) {
  17645. ret = ToTraditional(input, 0);
  17646. if (ret == ASN_PARSE_E) {
  17647. ret = 0;
  17648. }
  17649. }
  17650. printf(resultFmt, ret == 0 ? passed : failed);
  17651. #endif
  17652. return ret;
  17653. }/* End test_ToTraditional*/
  17654. /*
  17655. * Testing wc_EccPrivateKeyToDer
  17656. */
  17657. static int test_wc_EccPrivateKeyToDer (void)
  17658. {
  17659. int ret = 0;
  17660. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  17661. byte output[ONEK_BUF];
  17662. ecc_key eccKey;
  17663. WC_RNG rng;
  17664. word32 inLen;
  17665. printf(testingFmt, "wc_EccPrivateKeyToDer()");
  17666. ret = wc_InitRng(&rng);
  17667. if (ret == 0) {
  17668. ret = wc_ecc_init(&eccKey);
  17669. if (ret == 0) {
  17670. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  17671. }
  17672. inLen = (word32)sizeof(output);
  17673. /* Bad Cases */
  17674. if (ret == 0) {
  17675. ret = wc_EccPrivateKeyToDer(NULL, NULL, 0);
  17676. if (ret == BAD_FUNC_ARG) {
  17677. ret = 0;
  17678. }
  17679. }
  17680. if (ret == 0) {
  17681. ret = wc_EccPrivateKeyToDer(NULL, output, inLen);
  17682. if (ret == BAD_FUNC_ARG) {
  17683. ret = 0;
  17684. }
  17685. }
  17686. if (ret == 0) {
  17687. ret = wc_EccPrivateKeyToDer(&eccKey, NULL, inLen);
  17688. if (ret == BAD_FUNC_ARG) {
  17689. ret = 0;
  17690. }
  17691. }
  17692. if (ret == 0) {
  17693. ret = wc_EccPrivateKeyToDer(&eccKey, output, 0);
  17694. if (ret == BAD_FUNC_ARG) {
  17695. ret = 0;
  17696. }
  17697. }
  17698. /*Good Case */
  17699. if (ret == 0) {
  17700. ret = wc_EccPrivateKeyToDer(&eccKey, output, inLen);
  17701. if (ret > 0) {
  17702. ret = 0;
  17703. }
  17704. }
  17705. wc_ecc_free(&eccKey);
  17706. }
  17707. wc_FreeRng(&rng);
  17708. printf(resultFmt, ret == 0 ? passed : failed);
  17709. #endif
  17710. return ret;
  17711. }/* End test_wc_EccPrivateKeyToDer*/
  17712. /*
  17713. * Testing wc_Ed25519KeyToDer
  17714. */
  17715. static int test_wc_Ed25519KeyToDer (void)
  17716. {
  17717. int ret = 0;
  17718. #if defined(HAVE_ED25519) && (defined(WOLFSSL_CERT_GEN) || \
  17719. defined(WOLFSSL_KEY_GEN))
  17720. byte output[ONEK_BUF];
  17721. ed25519_key ed25519Key;
  17722. WC_RNG rng;
  17723. word32 inLen;
  17724. printf(testingFmt, "wc_Ed25519KeyToDer()");
  17725. ret = wc_InitRng(&rng);
  17726. if (ret == 0) {
  17727. ret = wc_ed25519_init(&ed25519Key);
  17728. if (ret == 0) {
  17729. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  17730. }
  17731. inLen = (word32)sizeof(output);
  17732. /* Bad Cases */
  17733. if (ret == 0) {
  17734. ret = wc_Ed25519KeyToDer(NULL, NULL, 0);
  17735. if (ret == BAD_FUNC_ARG) {
  17736. ret = 0;
  17737. }
  17738. }
  17739. if (ret == 0) {
  17740. ret = wc_Ed25519KeyToDer(NULL, output, inLen);
  17741. if (ret == BAD_FUNC_ARG) {
  17742. ret = 0;
  17743. }
  17744. }
  17745. if (ret == 0) {
  17746. ret = wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen);
  17747. if (ret == BAD_FUNC_ARG) {
  17748. ret = 0;
  17749. }
  17750. }
  17751. if (ret == 0) {
  17752. ret = wc_Ed25519KeyToDer(&ed25519Key, output, 0);
  17753. if (ret == BAD_FUNC_ARG) {
  17754. ret = 0;
  17755. }
  17756. }
  17757. /* Good Case */
  17758. if (ret == 0) {
  17759. ret = wc_Ed25519KeyToDer(&ed25519Key, output, inLen);
  17760. if (ret > 0) {
  17761. ret = 0;
  17762. }
  17763. }
  17764. wc_ed25519_free(&ed25519Key);
  17765. }
  17766. wc_FreeRng(&rng);
  17767. printf(resultFmt, ret == 0 ? passed : failed);
  17768. #endif
  17769. return ret;
  17770. }/* End test_wc_Ed25519KeyToDer*/
  17771. /*
  17772. * Testing wc_Ed25519PrivateKeyToDer
  17773. */
  17774. static int test_wc_Ed25519PrivateKeyToDer (void)
  17775. {
  17776. int ret = 0;
  17777. #if defined(HAVE_ED25519) && (defined(WOLFSSL_CERT_GEN) || \
  17778. defined(WOLFSSL_KEY_GEN))
  17779. byte output[ONEK_BUF];
  17780. ed25519_key ed25519PrivKey;
  17781. WC_RNG rng;
  17782. word32 inLen;
  17783. printf(testingFmt, "wc_Ed25519PrivateKeyToDer()");
  17784. ret = wc_InitRng(&rng);
  17785. if (ret == 0) {
  17786. ret = wc_ed25519_init(&ed25519PrivKey);
  17787. if (ret == 0) {
  17788. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey);
  17789. }
  17790. inLen = (word32)sizeof(output);
  17791. /* Bad Cases */
  17792. if (ret == 0) {
  17793. ret = wc_Ed25519PrivateKeyToDer(NULL, NULL, 0);
  17794. if (ret == BAD_FUNC_ARG) {
  17795. ret = 0;
  17796. }
  17797. }
  17798. if (ret == 0) {
  17799. ret = wc_Ed25519PrivateKeyToDer(NULL, output, inLen);
  17800. if (ret == BAD_FUNC_ARG) {
  17801. ret = 0;
  17802. }
  17803. }
  17804. if (ret == 0) {
  17805. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen);
  17806. if (ret == BAD_FUNC_ARG) {
  17807. ret = 0;
  17808. }
  17809. }
  17810. if (ret == 0) {
  17811. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0);
  17812. if (ret == BAD_FUNC_ARG) {
  17813. ret = 0;
  17814. }
  17815. }
  17816. /* Good Case */
  17817. if (ret == 0) {
  17818. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen);
  17819. if (ret > 0) {
  17820. ret = 0;
  17821. }
  17822. }
  17823. wc_ed25519_free(&ed25519PrivKey);
  17824. }
  17825. wc_FreeRng(&rng);
  17826. printf(resultFmt, ret == 0 ? passed : failed);
  17827. #endif
  17828. return ret;
  17829. }/* End test_wc_Ed25519PrivateKeyToDer*/
  17830. /*
  17831. * Testing wc_Ed448KeyToDer
  17832. */
  17833. static int test_wc_Ed448KeyToDer (void)
  17834. {
  17835. int ret = 0;
  17836. #if defined(HAVE_ED448) && (defined(WOLFSSL_CERT_GEN) || \
  17837. defined(WOLFSSL_KEY_GEN))
  17838. byte output[ONEK_BUF];
  17839. ed448_key ed448Key;
  17840. WC_RNG rng;
  17841. word32 inLen;
  17842. printf(testingFmt, "wc_Ed448KeyToDer()");
  17843. ret = wc_InitRng(&rng);
  17844. if (ret == 0) {
  17845. ret = wc_ed448_init(&ed448Key);
  17846. if (ret == 0) {
  17847. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  17848. }
  17849. inLen = sizeof(output);
  17850. /* Bad Cases */
  17851. if (ret == 0) {
  17852. ret = wc_Ed448KeyToDer(NULL, NULL, 0);
  17853. if (ret == BAD_FUNC_ARG) {
  17854. ret = 0;
  17855. }
  17856. }
  17857. if (ret == 0) {
  17858. ret = wc_Ed448KeyToDer(NULL, output, inLen);
  17859. if (ret == BAD_FUNC_ARG) {
  17860. ret = 0;
  17861. }
  17862. }
  17863. if (ret == 0) {
  17864. ret = wc_Ed448KeyToDer(&ed448Key, NULL, inLen);
  17865. if (ret == BAD_FUNC_ARG) {
  17866. ret = 0;
  17867. }
  17868. }
  17869. if (ret == 0) {
  17870. ret = wc_Ed448KeyToDer(&ed448Key, output, 0);
  17871. if (ret == BAD_FUNC_ARG) {
  17872. ret = 0;
  17873. }
  17874. }
  17875. /* Good Case */
  17876. if (ret == 0) {
  17877. ret = wc_Ed448KeyToDer(&ed448Key, output, inLen);
  17878. if (ret > 0) {
  17879. ret = 0;
  17880. }
  17881. }
  17882. wc_ed448_free(&ed448Key);
  17883. }
  17884. wc_FreeRng(&rng);
  17885. printf(resultFmt, ret == 0 ? passed : failed);
  17886. #endif
  17887. return ret;
  17888. }/* End test_wc_Ed448KeyToDer*/
  17889. /*
  17890. * Testing wc_Ed448PrivateKeyToDer
  17891. */
  17892. static int test_wc_Ed448PrivateKeyToDer (void)
  17893. {
  17894. int ret = 0;
  17895. #if defined(HAVE_ED448) && (defined(WOLFSSL_CERT_GEN) || \
  17896. defined(WOLFSSL_KEY_GEN))
  17897. byte output[ONEK_BUF];
  17898. ed448_key ed448PrivKey;
  17899. WC_RNG rng;
  17900. word32 inLen;
  17901. printf(testingFmt, "wc_Ed448PrivateKeyToDer()");
  17902. ret = wc_InitRng(&rng);
  17903. if (ret == 0) {
  17904. ret = wc_ed448_init(&ed448PrivKey);
  17905. if (ret == 0) {
  17906. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey);
  17907. }
  17908. inLen = sizeof(output);
  17909. /* Bad Cases */
  17910. if (ret == 0) {
  17911. ret = wc_Ed448PrivateKeyToDer(NULL, NULL, 0);
  17912. if (ret == BAD_FUNC_ARG) {
  17913. ret = 0;
  17914. }
  17915. }
  17916. if (ret == 0) {
  17917. ret = wc_Ed448PrivateKeyToDer(NULL, output, inLen);
  17918. if (ret == BAD_FUNC_ARG) {
  17919. ret = 0;
  17920. }
  17921. }
  17922. if (ret == 0) {
  17923. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen);
  17924. if (ret == BAD_FUNC_ARG) {
  17925. ret = 0;
  17926. }
  17927. }
  17928. if (ret == 0) {
  17929. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0);
  17930. if (ret == BAD_FUNC_ARG) {
  17931. ret = 0;
  17932. }
  17933. }
  17934. /* Good case */
  17935. if (ret == 0) {
  17936. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen);
  17937. if (ret > 0) {
  17938. ret = 0;
  17939. }
  17940. }
  17941. wc_ed448_free(&ed448PrivKey);
  17942. }
  17943. wc_FreeRng(&rng);
  17944. printf(resultFmt, ret == 0 ? passed : failed);
  17945. #endif
  17946. return ret;
  17947. }/* End test_wc_Ed448PrivateKeyToDer*/
  17948. /*
  17949. * Testing wc_SetSubjectBuffer
  17950. */
  17951. static int test_wc_SetSubjectBuffer (void)
  17952. {
  17953. int ret = 0;
  17954. #if defined(WOLFSSL_CERT_GEN)
  17955. Cert cert;
  17956. FILE* file;
  17957. byte* der;
  17958. word32 derSz;
  17959. printf(testingFmt, "wc_SetSubjectBuffer()");
  17960. derSz = FOURK_BUF;
  17961. der = XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17962. if (der == NULL) {
  17963. ret = -1;
  17964. }
  17965. if (ret == 0) {
  17966. file = XFOPEN("./certs/ca-cert.der", "rb");
  17967. if (file != NULL) {
  17968. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  17969. XFCLOSE(file);
  17970. }
  17971. }
  17972. if (ret == 0) {
  17973. ret = wc_InitCert(&cert);
  17974. }
  17975. if (ret == 0) {
  17976. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  17977. }
  17978. if (ret == 0) {
  17979. ret = wc_SetSubjectBuffer(NULL, der, derSz);
  17980. if (ret == BAD_FUNC_ARG) {
  17981. ret = 0;
  17982. }
  17983. }
  17984. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17985. printf(resultFmt, ret == 0 ? passed : failed);
  17986. #endif
  17987. return ret;
  17988. }/* End test_wc_SetSubjectBuffer*/
  17989. /*
  17990. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  17991. */
  17992. static int test_wc_SetSubjectKeyIdFromPublicKey_ex (void)
  17993. {
  17994. int ret = 0;
  17995. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  17996. WC_RNG rng;
  17997. Cert cert;
  17998. #if defined(HAVE_ED25519)
  17999. ed25519_key ed25519Key;
  18000. #endif
  18001. #if !defined(NO_RSA) && defined(HAVE_RSA)
  18002. RsaKey rsaKey;
  18003. int bits = 2048;
  18004. #endif
  18005. ecc_key eccKey;
  18006. #if defined(HAVE_ED448)
  18007. ed448_key ed448Key;
  18008. #endif
  18009. printf(testingFmt, "wc_SetSubjectKeyIdFromPublicKey_ex()");
  18010. #ifndef HAVE_FIPS
  18011. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  18012. #else
  18013. ret = wc_InitRng(&rng);
  18014. #endif
  18015. wc_InitCert(&cert);
  18016. #if defined(HAVE_ED25519)
  18017. if (ret == 0) { /*ED25519*/
  18018. ret = wc_ed25519_init(&ed25519Key);
  18019. if (ret == 0) {
  18020. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  18021. }
  18022. if (ret == 0) {
  18023. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  18024. &ed25519Key);
  18025. }
  18026. wc_ed25519_free(&ed25519Key);
  18027. }
  18028. #endif
  18029. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  18030. if (ret == 0) { /*RSA*/
  18031. ret = wc_InitRsaKey(&rsaKey, NULL);
  18032. if (ret == 0) {
  18033. MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
  18034. }
  18035. if (ret == 0) {
  18036. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
  18037. }
  18038. wc_FreeRsaKey(&rsaKey);
  18039. }
  18040. #endif
  18041. if (ret == 0) { /*ECC*/
  18042. ret = wc_ecc_init(&eccKey);
  18043. if (ret == 0) {
  18044. wc_ecc_make_key(&rng, KEY14, &eccKey);
  18045. }
  18046. if (ret == 0) {
  18047. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
  18048. }
  18049. wc_ecc_free(&eccKey);
  18050. }
  18051. #if defined(HAVE_ED448) && (defined(WOLFSSL_CERT_GEN) || \
  18052. defined(WOLFSSL_KEY_GEN))
  18053. if (ret == 0) { /*ED448*/
  18054. ret = wc_ed448_init(&ed448Key);
  18055. if (ret == 0) {
  18056. wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  18057. }
  18058. if (ret == 0) {
  18059. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  18060. &ed448Key);
  18061. }
  18062. wc_ed448_free(&ed448Key);
  18063. }
  18064. #endif
  18065. printf(resultFmt, ret == 0 ? passed : failed);
  18066. wc_FreeRng(&rng);
  18067. #endif
  18068. return ret;
  18069. }/* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  18070. /*
  18071. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  18072. */
  18073. static int test_wc_SetAuthKeyIdFromPublicKey_ex (void)
  18074. {
  18075. int ret = 0;
  18076. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  18077. WC_RNG rng;
  18078. Cert cert;
  18079. #if defined(HAVE_ED25519)
  18080. ed25519_key ed25519Key;
  18081. #endif
  18082. #if !defined(NO_RSA) && defined(HAVE_RSA)
  18083. RsaKey rsaKey;
  18084. int bits = 2048;
  18085. #endif
  18086. ecc_key eccKey;
  18087. #if defined(HAVE_ED448)
  18088. ed448_key ed448Key;
  18089. #endif
  18090. printf(testingFmt, "wc_SetAuthKeyIdFromPublicKey_ex()");
  18091. #ifndef HAVE_FIPS
  18092. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  18093. #else
  18094. ret = wc_InitRng(&rng);
  18095. #endif
  18096. wc_InitCert(&cert);
  18097. #if defined(HAVE_ED25519)
  18098. if (ret == 0) { /*ED25519*/
  18099. ret = wc_ed25519_init(&ed25519Key);
  18100. if (ret == 0) {
  18101. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  18102. }
  18103. if (ret == 0) {
  18104. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  18105. &ed25519Key);
  18106. }
  18107. wc_ed25519_free(&ed25519Key);
  18108. }
  18109. #endif
  18110. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  18111. if (ret == 0) { /*RSA*/
  18112. ret = wc_InitRsaKey(&rsaKey, NULL);
  18113. if (ret == 0) {
  18114. MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
  18115. }
  18116. if (ret == 0) {
  18117. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
  18118. }
  18119. wc_FreeRsaKey(&rsaKey);
  18120. }
  18121. #endif
  18122. if (ret == 0) { /*ECC*/
  18123. ret = wc_ecc_init(&eccKey);
  18124. if (ret == 0) {
  18125. wc_ecc_make_key(&rng, KEY14, &eccKey);
  18126. }
  18127. if (ret == 0) {
  18128. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
  18129. }
  18130. wc_ecc_free(&eccKey);
  18131. }
  18132. #if defined(HAVE_ED448) && (defined(WOLFSSL_CERT_GEN) || \
  18133. defined(WOLFSSL_KEY_GEN))
  18134. if (ret == 0) { /*ED448*/
  18135. ret = wc_ed448_init(&ed448Key);
  18136. if (ret == 0) {
  18137. wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  18138. }
  18139. if (ret == 0) {
  18140. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  18141. &ed448Key);
  18142. }
  18143. wc_ed448_free(&ed448Key);
  18144. }
  18145. #endif
  18146. printf(resultFmt, ret == 0 ? passed : failed);
  18147. wc_FreeRng(&rng);
  18148. #endif /*defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  18149. return ret;
  18150. }/* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  18151. /*
  18152. * Testing wc_PKCS7_New()
  18153. */
  18154. static void test_wc_PKCS7_New (void)
  18155. {
  18156. #if defined(HAVE_PKCS7)
  18157. PKCS7* pkcs7;
  18158. void* heap = NULL;
  18159. printf(testingFmt, "wc_PKCS7_New()");
  18160. pkcs7 = wc_PKCS7_New(heap, devId);
  18161. AssertNotNull(pkcs7);
  18162. printf(resultFmt, passed);
  18163. wc_PKCS7_Free(pkcs7);
  18164. #endif
  18165. } /* END test-wc_PKCS7_New */
  18166. /*
  18167. * Testing wc_PKCS7_Init()
  18168. */
  18169. static void test_wc_PKCS7_Init (void)
  18170. {
  18171. #if defined(HAVE_PKCS7)
  18172. PKCS7* pkcs7;
  18173. void* heap = NULL;
  18174. printf(testingFmt, "wc_PKCS7_Init()");
  18175. pkcs7 = wc_PKCS7_New(heap, devId);
  18176. AssertNotNull(pkcs7);
  18177. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, devId), 0);
  18178. /* Pass in bad args. */
  18179. AssertIntEQ(wc_PKCS7_Init(NULL, heap, devId), BAD_FUNC_ARG);
  18180. printf(resultFmt, passed);
  18181. wc_PKCS7_Free(pkcs7);
  18182. #endif
  18183. } /* END test-wc_PKCS7_Init */
  18184. /*
  18185. * Testing wc_PKCS7_InitWithCert()
  18186. */
  18187. static void test_wc_PKCS7_InitWithCert (void)
  18188. {
  18189. #if defined(HAVE_PKCS7)
  18190. PKCS7* pkcs7;
  18191. #ifndef NO_RSA
  18192. #if defined(USE_CERT_BUFFERS_2048)
  18193. unsigned char cert[sizeof_client_cert_der_2048];
  18194. int certSz = (int)sizeof(cert);
  18195. XMEMSET(cert, 0, certSz);
  18196. XMEMCPY(cert, client_cert_der_2048, sizeof_client_cert_der_2048);
  18197. #elif defined(USE_CERT_BUFFERS_1024)
  18198. unsigned char cert[sizeof_client_cert_der_1024];
  18199. int certSz = (int)sizeof(cert);
  18200. XMEMSET(cert, 0, certSz);
  18201. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  18202. #else
  18203. unsigned char cert[ONEK_BUF];
  18204. XFILE fp;
  18205. int certSz;
  18206. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  18207. AssertTrue(fp != XBADFILE);
  18208. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  18209. XFCLOSE(fp);
  18210. #endif
  18211. #elif defined(HAVE_ECC)
  18212. #if defined(USE_CERT_BUFFERS_256)
  18213. unsigned char cert[sizeof_cliecc_cert_der_256];
  18214. int certSz = (int)sizeof(cert);
  18215. XMEMSET(cert, 0, certSz);
  18216. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  18217. #else
  18218. unsigned char cert[ONEK_BUF];
  18219. XFILE fp;
  18220. int certSz;
  18221. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  18222. AssertTrue(fp != XBADFILE);
  18223. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  18224. XFCLOSE(fp);
  18225. #endif
  18226. #else
  18227. #error PKCS7 requires ECC or RSA
  18228. #endif
  18229. printf(testingFmt, "wc_PKCS7_InitWithCert()");
  18230. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  18231. /* If initialization is not successful, it's free'd in init func. */
  18232. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  18233. wc_PKCS7_Free(pkcs7);
  18234. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  18235. /* Valid initialization usage. */
  18236. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  18237. /* Pass in bad args. No need free for null checks, free at end.*/
  18238. AssertIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  18239. BAD_FUNC_ARG);
  18240. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  18241. BAD_FUNC_ARG);
  18242. printf(resultFmt, passed);
  18243. wc_PKCS7_Free(pkcs7);
  18244. #endif
  18245. } /* END test_wc_PKCS7_InitWithCert */
  18246. /*
  18247. * Testing wc_PKCS7_EncodeData()
  18248. */
  18249. static void test_wc_PKCS7_EncodeData (void)
  18250. {
  18251. #if defined(HAVE_PKCS7)
  18252. PKCS7* pkcs7;
  18253. byte output[FOURK_BUF];
  18254. byte data[] = "My encoded DER cert.";
  18255. #ifndef NO_RSA
  18256. #if defined(USE_CERT_BUFFERS_2048)
  18257. unsigned char cert[sizeof_client_cert_der_2048];
  18258. unsigned char key[sizeof_client_key_der_2048];
  18259. int certSz = (int)sizeof(cert);
  18260. int keySz = (int)sizeof(key);
  18261. XMEMSET(cert, 0, certSz);
  18262. XMEMSET(key, 0, keySz);
  18263. XMEMCPY(cert, client_cert_der_2048, certSz);
  18264. XMEMCPY(key, client_key_der_2048, keySz);
  18265. #elif defined(USE_CERT_BUFFERS_1024)
  18266. unsigned char cert[sizeof_client_cert_der_1024];
  18267. unsigned char key[sizeof_client_key_der_1024];
  18268. int certSz = (int)sizeof(cert);
  18269. int keySz = (int)sizeof(key);
  18270. XMEMSET(cert, 0, certSz);
  18271. XMEMSET(key, 0, keySz);
  18272. XMEMCPY(cert, client_cert_der_1024, certSz);
  18273. XMEMCPY(key, client_key_der_1024, keySz);
  18274. #else
  18275. unsigned char cert[ONEK_BUF];
  18276. unsigned char key[ONEK_BUF];
  18277. XFILE fp;
  18278. int certSz;
  18279. int keySz;
  18280. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  18281. AssertTrue(fp != XBADFILE);
  18282. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  18283. XFCLOSE(fp);
  18284. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  18285. AssertTrue(fp != XBADFILE);
  18286. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  18287. XFCLOSE(fp);
  18288. #endif
  18289. #elif defined(HAVE_ECC)
  18290. #if defined(USE_CERT_BUFFERS_256)
  18291. unsigned char cert[sizeof_cliecc_cert_der_256];
  18292. unsigned char key[sizeof_ecc_clikey_der_256];
  18293. int certSz = (int)sizeof(cert);
  18294. int keySz = (int)sizeof(key);
  18295. XMEMSET(cert, 0, certSz);
  18296. XMEMSET(key, 0, keySz);
  18297. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  18298. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  18299. #else
  18300. unsigned char cert[ONEK_BUF];
  18301. unsigned char key[ONEK_BUF];
  18302. XFILE fp;
  18303. int certSz, keySz;
  18304. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  18305. AssertTrue(fp != XBADFILE);
  18306. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  18307. XFCLOSE(fp);
  18308. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  18309. AssertTrue(fp != XBADFILE);
  18310. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  18311. XFCLOSE(fp);
  18312. #endif
  18313. #endif
  18314. XMEMSET(output, 0, sizeof(output));
  18315. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  18316. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  18317. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, certSz), 0);
  18318. printf(testingFmt, "wc_PKCS7_EncodeData()");
  18319. pkcs7->content = data;
  18320. pkcs7->contentSz = sizeof(data);
  18321. pkcs7->privateKey = key;
  18322. pkcs7->privateKeySz = keySz;
  18323. AssertIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  18324. /* Test bad args. */
  18325. AssertIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  18326. BAD_FUNC_ARG);
  18327. AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  18328. BAD_FUNC_ARG);
  18329. AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  18330. printf(resultFmt, passed);
  18331. wc_PKCS7_Free(pkcs7);
  18332. #endif
  18333. } /* END test_wc_PKCS7_EncodeData */
  18334. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  18335. !defined(NO_RSA) && !defined(NO_SHA256)
  18336. /* RSA sign raw digest callback */
  18337. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  18338. byte* out, word32 outSz, byte* privateKey,
  18339. word32 privateKeySz, int devid, int hashOID)
  18340. {
  18341. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  18342. byte digInfoEncoding[] = {
  18343. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  18344. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  18345. 0x00, 0x04, 0x20
  18346. };
  18347. int ret;
  18348. byte digestInfo[ONEK_BUF];
  18349. byte sig[FOURK_BUF];
  18350. word32 digestInfoSz = 0;
  18351. word32 idx = 0;
  18352. RsaKey rsa;
  18353. /* SHA-256 required only for this example callback due to above
  18354. * digInfoEncoding[] */
  18355. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  18356. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  18357. (hashOID != SHA256h)) {
  18358. return -1;
  18359. }
  18360. /* build DigestInfo */
  18361. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  18362. digestInfoSz += sizeof(digInfoEncoding);
  18363. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  18364. digestInfoSz += digestSz;
  18365. /* set up RSA key */
  18366. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  18367. if (ret != 0) {
  18368. return ret;
  18369. }
  18370. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  18371. /* sign DigestInfo */
  18372. if (ret == 0) {
  18373. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  18374. &rsa, pkcs7->rng);
  18375. if (ret > 0) {
  18376. if (ret > (int)outSz) {
  18377. /* output buffer too small */
  18378. ret = -1;
  18379. } else {
  18380. /* success, ret holds sig size */
  18381. XMEMCPY(out, sig, ret);
  18382. }
  18383. }
  18384. }
  18385. wc_FreeRsaKey(&rsa);
  18386. return ret;
  18387. }
  18388. #endif
  18389. /*
  18390. * Testing wc_PKCS7_EncodeSignedData()
  18391. */
  18392. static void test_wc_PKCS7_EncodeSignedData(void)
  18393. {
  18394. #if defined(HAVE_PKCS7)
  18395. PKCS7* pkcs7;
  18396. WC_RNG rng;
  18397. byte output[FOURK_BUF];
  18398. byte badOut[0];
  18399. word32 outputSz = (word32)sizeof(output);
  18400. word32 badOutSz = (word32)sizeof(badOut);
  18401. byte data[] = "Test data to encode.";
  18402. #ifndef NO_RSA
  18403. #if defined(USE_CERT_BUFFERS_2048)
  18404. byte key[sizeof_client_key_der_2048];
  18405. byte cert[sizeof_client_cert_der_2048];
  18406. word32 keySz = (word32)sizeof(key);
  18407. word32 certSz = (word32)sizeof(cert);
  18408. XMEMSET(key, 0, keySz);
  18409. XMEMSET(cert, 0, certSz);
  18410. XMEMCPY(key, client_key_der_2048, keySz);
  18411. XMEMCPY(cert, client_cert_der_2048, certSz);
  18412. #elif defined(USE_CERT_BUFFERS_1024)
  18413. byte key[sizeof_client_key_der_1024];
  18414. byte cert[sizeof_client_cert_der_1024];
  18415. word32 keySz = (word32)sizeof(key);
  18416. word32 certSz = (word32)sizeof(cert);
  18417. XMEMSET(key, 0, keySz);
  18418. XMEMSET(cert, 0, certSz);
  18419. XMEMCPY(key, client_key_der_1024, keySz);
  18420. XMEMCPY(cert, client_cert_der_1024, certSz);
  18421. #else
  18422. unsigned char cert[ONEK_BUF];
  18423. unsigned char key[ONEK_BUF];
  18424. XFILE fp;
  18425. int certSz;
  18426. int keySz;
  18427. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  18428. AssertTrue(fp != XBADFILE);
  18429. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  18430. XFCLOSE(fp);
  18431. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  18432. AssertTrue(fp != XBADFILE);
  18433. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  18434. XFCLOSE(fp);
  18435. #endif
  18436. #elif defined(HAVE_ECC)
  18437. #if defined(USE_CERT_BUFFERS_256)
  18438. unsigned char cert[sizeof_cliecc_cert_der_256];
  18439. unsigned char key[sizeof_ecc_clikey_der_256];
  18440. int certSz = (int)sizeof(cert);
  18441. int keySz = (int)sizeof(key);
  18442. XMEMSET(cert, 0, certSz);
  18443. XMEMSET(key, 0, keySz);
  18444. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  18445. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  18446. #else
  18447. unsigned char cert[ONEK_BUF];
  18448. unsigned char key[ONEK_BUF];
  18449. XFILE fp;
  18450. int certSz, keySz;
  18451. fp = XOPEN("./certs/client-ecc-cert.der", "rb");
  18452. AssertTrue(fp != XBADFILE);
  18453. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  18454. XFCLOSE(fp);
  18455. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  18456. AssertTrue(fp != XBADFILE);
  18457. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  18458. XFCLOSE(fp);
  18459. #endif
  18460. #endif
  18461. XMEMSET(output, 0, outputSz);
  18462. AssertIntEQ(wc_InitRng(&rng), 0);
  18463. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  18464. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  18465. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  18466. printf(testingFmt, "wc_PKCS7_EncodeSignedData()");
  18467. pkcs7->content = data;
  18468. pkcs7->contentSz = (word32)sizeof(data);
  18469. pkcs7->privateKey = key;
  18470. pkcs7->privateKeySz = (word32)sizeof(key);
  18471. pkcs7->encryptOID = RSAk;
  18472. pkcs7->hashOID = SHAh;
  18473. pkcs7->rng = &rng;
  18474. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  18475. wc_PKCS7_Free(pkcs7);
  18476. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  18477. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  18478. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  18479. /* Pass in bad args. */
  18480. AssertIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz), BAD_FUNC_ARG);
  18481. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  18482. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  18483. badOutSz), BAD_FUNC_ARG);
  18484. pkcs7->hashOID = 0; /* bad hashOID */
  18485. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), BAD_FUNC_ARG);
  18486. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  18487. !defined(NO_RSA) && !defined(NO_SHA256)
  18488. /* test RSA sign raw digest callback, if using RSA and compiled in.
  18489. * Example callback assumes SHA-256, so only run test if compiled in. */
  18490. wc_PKCS7_Free(pkcs7);
  18491. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  18492. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  18493. pkcs7->content = data;
  18494. pkcs7->contentSz = (word32)sizeof(data);
  18495. pkcs7->privateKey = key;
  18496. pkcs7->privateKeySz = (word32)sizeof(key);
  18497. pkcs7->encryptOID = RSAk;
  18498. pkcs7->hashOID = SHA256h;
  18499. pkcs7->rng = &rng;
  18500. AssertIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  18501. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  18502. #endif
  18503. printf(resultFmt, passed);
  18504. wc_PKCS7_Free(pkcs7);
  18505. wc_FreeRng(&rng);
  18506. #endif
  18507. } /* END test_wc_PKCS7_EncodeSignedData */
  18508. /*
  18509. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  18510. */
  18511. static void test_wc_PKCS7_EncodeSignedData_ex(void)
  18512. {
  18513. #if defined(HAVE_PKCS7)
  18514. int ret, i;
  18515. PKCS7* pkcs7;
  18516. WC_RNG rng;
  18517. byte outputHead[FOURK_BUF/2];
  18518. byte outputFoot[FOURK_BUF/2];
  18519. word32 outputHeadSz = (word32)sizeof(outputHead);
  18520. word32 outputFootSz = (word32)sizeof(outputFoot);
  18521. byte data[FOURK_BUF];
  18522. wc_HashAlg hash;
  18523. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  18524. byte hashBuf[WC_MAX_DIGEST_SIZE];
  18525. word32 hashSz = wc_HashGetDigestSize(hashType);
  18526. #ifndef NO_RSA
  18527. #if defined(USE_CERT_BUFFERS_2048)
  18528. byte key[sizeof_client_key_der_2048];
  18529. byte cert[sizeof_client_cert_der_2048];
  18530. word32 keySz = (word32)sizeof(key);
  18531. word32 certSz = (word32)sizeof(cert);
  18532. XMEMSET(key, 0, keySz);
  18533. XMEMSET(cert, 0, certSz);
  18534. XMEMCPY(key, client_key_der_2048, keySz);
  18535. XMEMCPY(cert, client_cert_der_2048, certSz);
  18536. #elif defined(USE_CERT_BUFFERS_1024)
  18537. byte key[sizeof_client_key_der_1024];
  18538. byte cert[sizeof_client_cert_der_1024];
  18539. word32 keySz = (word32)sizeof(key);
  18540. word32 certSz = (word32)sizeof(cert);
  18541. XMEMSET(key, 0, keySz);
  18542. XMEMSET(cert, 0, certSz);
  18543. XMEMCPY(key, client_key_der_1024, keySz);
  18544. XMEMCPY(cert, client_cert_der_1024, certSz);
  18545. #else
  18546. unsigned char cert[ONEK_BUF];
  18547. unsigned char key[ONEK_BUF];
  18548. XFILE fp;
  18549. int certSz;
  18550. int keySz;
  18551. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  18552. AssertTrue((fp != XBADFILE));
  18553. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  18554. XFCLOSE(fp);
  18555. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  18556. AssertTrue(fp != XBADFILE);
  18557. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  18558. XFCLOSE(fp);
  18559. #endif
  18560. #elif defined(HAVE_ECC)
  18561. #if defined(USE_CERT_BUFFERS_256)
  18562. unsigned char cert[sizeof_cliecc_cert_der_256];
  18563. unsigned char key[sizeof_ecc_clikey_der_256];
  18564. int certSz = (int)sizeof(cert);
  18565. int keySz = (int)sizeof(key);
  18566. XMEMSET(cert, 0, certSz);
  18567. XMEMSET(key, 0, keySz);
  18568. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  18569. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  18570. #else
  18571. unsigned char cert[ONEK_BUF];
  18572. unsigned char key[ONEK_BUF];
  18573. XFILE fp;
  18574. int certSz, keySz;
  18575. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  18576. AssertTrue(fp != XBADFILE);
  18577. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  18578. XFCLOSE(fp);
  18579. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  18580. AssertTrue(fp != XBADFILE);
  18581. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  18582. XFCLOSE(fp);
  18583. #endif
  18584. #endif
  18585. /* initialize large data with sequence */
  18586. for (i=0; i<(int)sizeof(data); i++)
  18587. data[i] = i & 0xff;
  18588. XMEMSET(outputHead, 0, outputHeadSz);
  18589. XMEMSET(outputFoot, 0, outputFootSz);
  18590. AssertIntEQ(wc_InitRng(&rng), 0);
  18591. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  18592. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  18593. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  18594. printf(testingFmt, "wc_PKCS7_EncodeSignedData()");
  18595. pkcs7->content = NULL; /* not used for ex */
  18596. pkcs7->contentSz = (word32)sizeof(data);
  18597. pkcs7->privateKey = key;
  18598. pkcs7->privateKeySz = (word32)sizeof(key);
  18599. pkcs7->encryptOID = RSAk;
  18600. pkcs7->hashOID = SHAh;
  18601. pkcs7->rng = &rng;
  18602. /* calculate hash for content */
  18603. ret = wc_HashInit(&hash, hashType);
  18604. if (ret == 0) {
  18605. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  18606. if (ret == 0) {
  18607. ret = wc_HashFinal(&hash, hashType, hashBuf);
  18608. }
  18609. wc_HashFree(&hash, hashType);
  18610. }
  18611. AssertIntEQ(ret, 0);
  18612. /* Perform PKCS7 sign using hash directly */
  18613. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  18614. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  18615. AssertIntGT(outputHeadSz, 0);
  18616. AssertIntGT(outputFootSz, 0);
  18617. wc_PKCS7_Free(pkcs7);
  18618. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  18619. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  18620. /* required parameter even on verify when using _ex */
  18621. pkcs7->contentSz = (word32)sizeof(data);
  18622. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  18623. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  18624. wc_PKCS7_Free(pkcs7);
  18625. /* assembly complete PKCS7 sign and use normal verify */
  18626. {
  18627. byte* output = (byte*)XMALLOC(outputHeadSz + sizeof(data) + outputFootSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18628. word32 outputSz = 0;
  18629. AssertNotNull(output);
  18630. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  18631. outputSz += outputHeadSz;
  18632. XMEMCPY(&output[outputSz], data, sizeof(data));
  18633. outputSz += sizeof(data);
  18634. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  18635. outputSz += outputFootSz;
  18636. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  18637. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  18638. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  18639. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18640. }
  18641. /* Pass in bad args. */
  18642. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  18643. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  18644. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  18645. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  18646. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  18647. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  18648. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  18649. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  18650. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  18651. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  18652. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  18653. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  18654. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  18655. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  18656. pkcs7->hashOID = 0; /* bad hashOID */
  18657. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  18658. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  18659. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  18660. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  18661. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  18662. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  18663. #ifndef NO_PKCS7_STREAM
  18664. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  18665. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  18666. #else
  18667. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  18668. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  18669. #endif
  18670. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  18671. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  18672. #ifndef NO_PKCS7_STREAM
  18673. /* can pass in 0 buffer length with streaming API */
  18674. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  18675. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  18676. #else
  18677. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  18678. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  18679. #endif
  18680. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  18681. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  18682. #ifndef NO_PKCS7_STREAM
  18683. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  18684. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  18685. #else
  18686. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  18687. outputHead, outputHeadSz, outputFoot, 0), ASN_PARSE_E);
  18688. #endif
  18689. printf(resultFmt, passed);
  18690. wc_PKCS7_Free(pkcs7);
  18691. wc_FreeRng(&rng);
  18692. #endif
  18693. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  18694. #if defined(HAVE_PKCS7)
  18695. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  18696. byte* data, word32 dataSz,
  18697. int withAttribs, int detachedSig)
  18698. {
  18699. PKCS7* pkcs7;
  18700. WC_RNG rng;
  18701. static byte messageTypeOid[] =
  18702. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  18703. 0x09, 0x02 };
  18704. static byte messageType[] = { 0x13, 2, '1', '9' };
  18705. PKCS7Attrib attribs[] =
  18706. {
  18707. { messageTypeOid, sizeof(messageTypeOid), messageType,
  18708. sizeof(messageType) }
  18709. };
  18710. #ifndef NO_RSA
  18711. #if defined(USE_CERT_BUFFERS_2048)
  18712. byte key[sizeof_client_key_der_2048];
  18713. byte cert[sizeof_client_cert_der_2048];
  18714. word32 keySz = (word32)sizeof(key);
  18715. word32 certSz = (word32)sizeof(cert);
  18716. XMEMSET(key, 0, keySz);
  18717. XMEMSET(cert, 0, certSz);
  18718. XMEMCPY(key, client_key_der_2048, keySz);
  18719. XMEMCPY(cert, client_cert_der_2048, certSz);
  18720. #elif defined(USE_CERT_BUFFERS_1024)
  18721. byte key[sizeof_client_key_der_1024];
  18722. byte cert[sizeof_client_cert_der_1024];
  18723. word32 keySz = (word32)sizeof(key);
  18724. word32 certSz = (word32)sizeof(cert);
  18725. XMEMSET(key, 0, keySz);
  18726. XMEMSET(cert, 0, certSz);
  18727. XMEMCPY(key, client_key_der_1024, keySz);
  18728. XMEMCPY(cert, client_cert_der_1024, certSz);
  18729. #else
  18730. unsigned char cert[ONEK_BUF];
  18731. unsigned char key[ONEK_BUF];
  18732. FILE* fp;
  18733. int certSz;
  18734. int keySz;
  18735. fp = fopen("./certs/1024/client-cert.der", "rb");
  18736. AssertNotNull(fp);
  18737. certSz = fread(cert, 1, sizeof_client_cert_der_1024, fp);
  18738. fclose(fp);
  18739. fp = fopen("./certs/1024/client-key.der", "rb");
  18740. AssertNotNull(fp);
  18741. keySz = fread(key, 1, sizeof_client_key_der_1024, fp);
  18742. fclose(fp);
  18743. #endif
  18744. #elif defined(HAVE_ECC)
  18745. #if defined(USE_CERT_BUFFERS_256)
  18746. unsigned char cert[sizeof_cliecc_cert_der_256];
  18747. unsigned char key[sizeof_ecc_clikey_der_256];
  18748. int certSz = (int)sizeof(cert);
  18749. int keySz = (int)sizeof(key);
  18750. XMEMSET(cert, 0, certSz);
  18751. XMEMSET(key, 0, keySz);
  18752. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  18753. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  18754. #else
  18755. unsigned char cert[ONEK_BUF];
  18756. unsigned char key[ONEK_BUF];
  18757. FILE* fp;
  18758. int certSz, keySz;
  18759. fp = fopen("./certs/client-ecc-cert.der", "rb");
  18760. AssertNotNull(fp);
  18761. certSz = fread(cert, 1, sizeof_cliecc_cert_der_256, fp);
  18762. fclose(fp);
  18763. fp = fopen("./certs/client-ecc-key.der", "rb");
  18764. AssertNotNull(fp);
  18765. keySz = fread(key, 1, sizeof_ecc_clikey_der_256, fp);
  18766. fclose(fp);
  18767. #endif
  18768. #endif
  18769. XMEMSET(output, 0, outputSz);
  18770. AssertIntEQ(wc_InitRng(&rng), 0);
  18771. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  18772. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  18773. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  18774. printf(testingFmt, "wc_PKCS7_VerifySignedData()");
  18775. pkcs7->content = data;
  18776. pkcs7->contentSz = dataSz;
  18777. pkcs7->privateKey = key;
  18778. pkcs7->privateKeySz = (word32)sizeof(key);
  18779. pkcs7->encryptOID = RSAk;
  18780. pkcs7->hashOID = SHAh;
  18781. pkcs7->rng = &rng;
  18782. if (withAttribs) {
  18783. /* include a signed attribute */
  18784. pkcs7->signedAttribs = attribs;
  18785. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  18786. }
  18787. if (detachedSig) {
  18788. AssertIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  18789. }
  18790. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  18791. wc_PKCS7_Free(pkcs7);
  18792. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  18793. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  18794. if (detachedSig) {
  18795. pkcs7->content = data;
  18796. pkcs7->contentSz = dataSz;
  18797. }
  18798. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  18799. wc_PKCS7_Free(pkcs7);
  18800. wc_FreeRng(&rng);
  18801. return outputSz;
  18802. }
  18803. #endif
  18804. /*
  18805. * Testing wc_PKCS_VerifySignedData()
  18806. */
  18807. static void test_wc_PKCS7_VerifySignedData(void)
  18808. {
  18809. #if defined(HAVE_PKCS7)
  18810. PKCS7* pkcs7;
  18811. byte output[FOURK_BUF];
  18812. word32 outputSz = sizeof(output);
  18813. byte data[] = "Test data to encode.";
  18814. byte badOut[0];
  18815. word32 badOutSz = (word32)sizeof(badOut);
  18816. byte badContent[] = "This is different content than was signed";
  18817. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  18818. (word32)sizeof(data),
  18819. 0, 0)), 0);
  18820. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  18821. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  18822. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  18823. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  18824. /* Test bad args. */
  18825. AssertIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz), BAD_FUNC_ARG);
  18826. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  18827. #ifndef NO_PKCS7_STREAM
  18828. /* can pass in 0 buffer length with streaming API */
  18829. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  18830. badOutSz), WC_PKCS7_WANT_READ_E);
  18831. #else
  18832. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  18833. badOutSz), BAD_FUNC_ARG);
  18834. #endif
  18835. wc_PKCS7_Free(pkcs7);
  18836. /* Invalid content should error, use detached signature so we can
  18837. * easily change content */
  18838. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  18839. (word32)sizeof(data),
  18840. 1, 1)), 0);
  18841. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  18842. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  18843. pkcs7->content = badContent;
  18844. pkcs7->contentSz = sizeof(badContent);
  18845. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), SIG_VERIFY_E);
  18846. wc_PKCS7_Free(pkcs7);
  18847. /* Test success case with detached signature and valid content */
  18848. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  18849. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  18850. pkcs7->content = data;
  18851. pkcs7->contentSz = sizeof(data);
  18852. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  18853. wc_PKCS7_Free(pkcs7);
  18854. printf(resultFmt, passed);
  18855. #endif
  18856. } /* END test_wc_PKCS7_VerifySignedData() */
  18857. #if defined(HAVE_PKCS7) && !defined(NO_AES) && !defined(NO_AES_256)
  18858. static const byte defKey[] = {
  18859. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  18860. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  18861. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  18862. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  18863. };
  18864. static byte aesHandle[32]; /* simulated hardware key handle */
  18865. /* return 0 on success */
  18866. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  18867. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  18868. byte* in, int inSz, byte* out, void* usrCtx)
  18869. {
  18870. int ret;
  18871. Aes aes;
  18872. if (usrCtx == NULL) {
  18873. /* no simulated handle passed in */
  18874. return -1;
  18875. }
  18876. switch (encryptOID) {
  18877. case AES256CBCb:
  18878. if (ivSz != AES_BLOCK_SIZE)
  18879. return BAD_FUNC_ARG;
  18880. break;
  18881. default:
  18882. WOLFSSL_MSG("Unsupported content cipher type for test");
  18883. return ALGO_ID_E;
  18884. };
  18885. /* simulate using handle to get key */
  18886. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  18887. if (ret == 0) {
  18888. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  18889. if (ret == 0)
  18890. ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
  18891. wc_AesFree(&aes);
  18892. }
  18893. (void)aad;
  18894. (void)aadSz;
  18895. (void)authTag;
  18896. (void)authTagSz;
  18897. (void)pkcs7;
  18898. return ret;
  18899. }
  18900. /* returns key size on success */
  18901. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  18902. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  18903. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  18904. {
  18905. int ret = -1;
  18906. if (out == NULL)
  18907. return BAD_FUNC_ARG;
  18908. if (keyId[0] != 0x00) {
  18909. return -1;
  18910. }
  18911. if (type != (int)PKCS7_KEKRI) {
  18912. return -1;
  18913. }
  18914. switch (keyWrapAlgo) {
  18915. case AES256_WRAP:
  18916. /* simulate setting a handle for later decryption but use key
  18917. * as handle in the test case here */
  18918. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  18919. aesHandle, sizeof(aesHandle), NULL);
  18920. if (ret < 0)
  18921. return ret;
  18922. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  18923. if (ret < 0)
  18924. return ret;
  18925. /* return key size on success */
  18926. return sizeof(defKey);
  18927. default:
  18928. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  18929. return BAD_KEYWRAP_ALG_E;
  18930. };
  18931. (void)cekSz;
  18932. (void)cek;
  18933. (void)outSz;
  18934. (void)keyIdSz;
  18935. (void)direction;
  18936. (void)orginKey; /* used with KAKRI */
  18937. (void)orginKeySz;
  18938. return ret;
  18939. }
  18940. #endif /* HAVE_PKCS7 && !NO_AES && !NO_AES_256 */
  18941. /*
  18942. * Testing wc_PKCS7_EncodeEnvelopedData()
  18943. */
  18944. static void test_wc_PKCS7_EncodeDecodeEnvelopedData (void)
  18945. {
  18946. #if defined(HAVE_PKCS7)
  18947. PKCS7* pkcs7;
  18948. WC_RNG rng;
  18949. word32 tempWrd32 = 0;
  18950. byte* tmpBytePtr = NULL;
  18951. const char input[] = "Test data to encode.";
  18952. int i;
  18953. int testSz = 0;
  18954. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  18955. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  18956. byte* rsaCert = NULL;
  18957. byte* rsaPrivKey = NULL;
  18958. word32 rsaCertSz;
  18959. word32 rsaPrivKeySz;
  18960. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  18961. !defined(USE_CERT_BUFFERS_2048) )
  18962. static const char* rsaClientCert = "./certs/client-cert.der";
  18963. static const char* rsaClientKey = "./certs/client-key.der";
  18964. rsaCertSz = (word32)sizeof(rsaClientCert);
  18965. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  18966. #endif
  18967. #endif
  18968. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  18969. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  18970. byte* eccCert = NULL;
  18971. byte* eccPrivKey = NULL;
  18972. word32 eccCertSz;
  18973. word32 eccPrivKeySz;
  18974. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  18975. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  18976. static const char* eccClientKey = "./certs/ecc-client-key.der";
  18977. #endif
  18978. #endif
  18979. /* Generic buffer size. */
  18980. byte output[ONEK_BUF];
  18981. byte decoded[sizeof(input)/sizeof(char)];
  18982. int decodedSz = 0;
  18983. #ifndef NO_FILESYSTEM
  18984. XFILE certFile;
  18985. XFILE keyFile;
  18986. #endif
  18987. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  18988. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  18989. /* RSA certs and keys. */
  18990. #if defined(USE_CERT_BUFFERS_1024)
  18991. /* Allocate buffer space. */
  18992. AssertNotNull(rsaCert =
  18993. (byte*)XMALLOC(ONEK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  18994. /* Init buffer. */
  18995. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  18996. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  18997. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(ONEK_BUF, HEAP_HINT,
  18998. DYNAMIC_TYPE_TMP_BUFFER));
  18999. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  19000. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  19001. #elif defined(USE_CERT_BUFFERS_2048)
  19002. /* Allocate buffer */
  19003. AssertNotNull(rsaCert =
  19004. (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  19005. /* Init buffer. */
  19006. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  19007. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  19008. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  19009. DYNAMIC_TYPE_TMP_BUFFER));
  19010. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  19011. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  19012. #else
  19013. /* File system. */
  19014. certFile = XFOPEN(rsaClientCert, "rb");
  19015. AssertTrue(certFile != XBADFILE);
  19016. rsaCertSz = (word32)FOURK_BUF;
  19017. AssertNotNull(rsaCert =
  19018. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  19019. rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz, certFile);
  19020. XFCLOSE(certFile);
  19021. keyFile = XFOPEN(rsaClientKey, "rb");
  19022. AssertTrue(keyFile != XBADFILE);
  19023. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  19024. DYNAMIC_TYPE_TMP_BUFFER));
  19025. rsaPrivKeySz = (word32)FOURK_BUF;
  19026. rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz, keyFile);
  19027. XFCLOSE(keyFile);
  19028. #endif /* USE_CERT_BUFFERS */
  19029. #endif /* NO_RSA */
  19030. /* ECC */
  19031. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  19032. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  19033. #ifdef USE_CERT_BUFFERS_256
  19034. AssertNotNull(eccCert =
  19035. (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  19036. /* Init buffer. */
  19037. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  19038. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  19039. AssertNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  19040. DYNAMIC_TYPE_TMP_BUFFER));
  19041. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  19042. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  19043. #else /* File system. */
  19044. certFile = XFOPEN(eccClientCert, "rb");
  19045. AssertTrue(certFile != XBADFILE);
  19046. eccCertSz = (word32)FOURK_BUF;
  19047. AssertNotNull(eccCert =
  19048. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  19049. eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz, certFile);
  19050. XFCLOSE(certFile);
  19051. keyFile = XFOPEN(eccClientKey, "rb");
  19052. AssertTrue(keyFile != XBADFILE);
  19053. eccPrivKeySz = (word32)FOURK_BUF;
  19054. AssertNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  19055. DYNAMIC_TYPE_TMP_BUFFER));
  19056. eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz, keyFile);
  19057. XFCLOSE(keyFile);
  19058. #endif /* USE_CERT_BUFFERS_256 */
  19059. #endif /* END HAVE_ECC */
  19060. /* Silence. */
  19061. (void)keyFile;
  19062. (void)certFile;
  19063. const pkcs7EnvelopedVector testVectors[] = {
  19064. /* DATA is a global variable defined in the makefile. */
  19065. #if !defined(NO_RSA)
  19066. #ifndef NO_DES3
  19067. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  19068. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  19069. #endif /* NO_DES3 */
  19070. #ifndef NO_AES
  19071. #ifndef NO_AES_128
  19072. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  19073. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  19074. #endif
  19075. #ifndef NO_AES_192
  19076. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  19077. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  19078. #endif
  19079. #ifndef NO_AES_256
  19080. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  19081. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  19082. #endif
  19083. #endif /* NO_AES */
  19084. #endif /* NO_RSA */
  19085. #if defined(HAVE_ECC)
  19086. #ifndef NO_AES
  19087. #if !defined(NO_SHA) && !defined(NO_AES_128)
  19088. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  19089. AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme, eccCert,
  19090. eccCertSz, eccPrivKey, eccPrivKeySz},
  19091. #endif
  19092. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  19093. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  19094. AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme, eccCert,
  19095. eccCertSz, eccPrivKey, eccPrivKeySz},
  19096. #endif
  19097. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  19098. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  19099. AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme, eccCert,
  19100. eccCertSz, eccPrivKey, eccPrivKeySz},
  19101. #endif
  19102. #endif /* NO_AES */
  19103. #endif /* END HAVE_ECC */
  19104. }; /* END pkcs7EnvelopedVector */
  19105. #ifdef ECC_TIMING_RESISTANT
  19106. AssertIntEQ(wc_InitRng(&rng), 0);
  19107. #endif
  19108. printf(testingFmt, "wc_PKCS7_EncodeEnvelopedData()");
  19109. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  19110. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, devId), 0);
  19111. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  19112. for (i = 0; i < testSz; i++) {
  19113. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  19114. (word32)(testVectors + i)->certSz), 0);
  19115. #ifdef ECC_TIMING_RESISTANT
  19116. pkcs7->rng = &rng;
  19117. #endif
  19118. pkcs7->content = (byte*)(testVectors + i)->content;
  19119. pkcs7->contentSz = (testVectors + i)->contentSz;
  19120. pkcs7->contentOID = (testVectors + i)->contentOID;
  19121. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  19122. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  19123. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  19124. pkcs7->privateKey = (testVectors + i)->privateKey;
  19125. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  19126. AssertIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  19127. (word32)sizeof(output)), 0);
  19128. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  19129. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  19130. AssertIntGE(decodedSz, 0);
  19131. /* Verify the size of each buffer. */
  19132. AssertIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  19133. /* Don't free the last time through the loop. */
  19134. if (i < testSz - 1 ){
  19135. wc_PKCS7_Free(pkcs7);
  19136. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  19137. }
  19138. } /* END test loop. */
  19139. /* Test bad args. */
  19140. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  19141. (word32)sizeof(output)), BAD_FUNC_ARG);
  19142. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  19143. (word32)sizeof(output)), BAD_FUNC_ARG);
  19144. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  19145. printf(resultFmt, passed);
  19146. /* Decode. */
  19147. printf(testingFmt, "wc_PKCS7_DecodeEnvelopedData()");
  19148. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  19149. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  19150. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  19151. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  19152. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  19153. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  19154. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  19155. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  19156. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  19157. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  19158. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  19159. #if defined(HAVE_ECC) && !defined(NO_AES)
  19160. /* only a failure for KARI test cases */
  19161. tempWrd32 = pkcs7->singleCertSz;
  19162. pkcs7->singleCertSz = 0;
  19163. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  19164. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  19165. pkcs7->singleCertSz = tempWrd32;
  19166. tmpBytePtr = pkcs7->singleCert;
  19167. pkcs7->singleCert = NULL;
  19168. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  19169. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  19170. pkcs7->singleCert = tmpBytePtr;
  19171. #endif
  19172. tempWrd32 = pkcs7->privateKeySz;
  19173. pkcs7->privateKeySz = 0;
  19174. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  19175. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  19176. pkcs7->privateKeySz = tempWrd32;
  19177. tmpBytePtr = pkcs7->privateKey;
  19178. pkcs7->privateKey = NULL;
  19179. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  19180. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  19181. pkcs7->privateKey = tmpBytePtr;
  19182. wc_PKCS7_Free(pkcs7);
  19183. #if !defined(NO_AES) && !defined(NO_AES_256)
  19184. /* test of decrypt callback with KEKRI enveloped data */
  19185. {
  19186. int envelopedSz;
  19187. const byte keyId[] = { 0x00 };
  19188. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  19189. pkcs7->content = (byte*)input;
  19190. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  19191. pkcs7->contentOID = DATA;
  19192. pkcs7->encryptOID = AES256CBCb;
  19193. AssertIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  19194. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  19195. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  19196. AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  19197. AssertIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  19198. (word32)sizeof(output))), 0);
  19199. wc_PKCS7_Free(pkcs7);
  19200. /* decode envelopedData */
  19201. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  19202. AssertIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  19203. AssertIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  19204. AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  19205. envelopedSz, decoded, sizeof(decoded))), 0);
  19206. wc_PKCS7_Free(pkcs7);
  19207. }
  19208. #endif /* !NO_AES && !NO_AES_256 */
  19209. printf(resultFmt, passed);
  19210. #ifndef NO_RSA
  19211. if (rsaCert) {
  19212. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19213. }
  19214. if (rsaPrivKey) {
  19215. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19216. }
  19217. #endif /*NO_RSA */
  19218. #ifdef HAVE_ECC
  19219. if (eccCert) {
  19220. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19221. }
  19222. if (eccPrivKey) {
  19223. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19224. }
  19225. #endif /* HAVE_ECC */
  19226. #ifdef ECC_TIMING_RESISTANT
  19227. wc_FreeRng(&rng);
  19228. #endif
  19229. #endif /* HAVE_PKCS7 */
  19230. } /* END test_wc_PKCS7_EncodeEnvelopedData() */
  19231. /*
  19232. * Testing wc_PKCS7_EncodeEncryptedData()
  19233. */
  19234. static void test_wc_PKCS7_EncodeEncryptedData (void)
  19235. {
  19236. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  19237. PKCS7* pkcs7;
  19238. byte* tmpBytePtr = NULL;
  19239. byte encrypted[TWOK_BUF];
  19240. byte decoded[TWOK_BUF];
  19241. word32 tmpWrd32 = 0;
  19242. int tmpInt = 0;
  19243. int decodedSz;
  19244. int encryptedSz;
  19245. int testSz;
  19246. int i;
  19247. const byte data[] = { /* Hello World */
  19248. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  19249. 0x72,0x6c,0x64
  19250. };
  19251. #ifndef NO_DES3
  19252. byte desKey[] = {
  19253. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  19254. };
  19255. byte des3Key[] = {
  19256. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  19257. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  19258. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  19259. };
  19260. #endif
  19261. #ifndef NO_AES
  19262. #ifndef NO_AES_128
  19263. byte aes128Key[] = {
  19264. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  19265. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  19266. };
  19267. #endif
  19268. #ifndef NO_AES_192
  19269. byte aes192Key[] = {
  19270. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  19271. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  19272. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  19273. };
  19274. #endif
  19275. #ifndef NO_AES_256
  19276. byte aes256Key[] = {
  19277. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  19278. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  19279. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  19280. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  19281. };
  19282. #endif
  19283. #endif
  19284. const pkcs7EncryptedVector testVectors[] =
  19285. {
  19286. #ifndef NO_DES3
  19287. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  19288. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  19289. #endif /* NO_DES3 */
  19290. #ifndef NO_AES
  19291. #ifndef NO_AES_128
  19292. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  19293. sizeof(aes128Key)},
  19294. #endif
  19295. #ifndef NO_AES_192
  19296. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  19297. sizeof(aes192Key)},
  19298. #endif
  19299. #ifndef NO_AES_256
  19300. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  19301. sizeof(aes256Key)},
  19302. #endif
  19303. #endif /* NO_AES */
  19304. };
  19305. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  19306. for (i = 0; i < testSz; i++) {
  19307. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  19308. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, devId), 0);
  19309. pkcs7->content = (byte*)testVectors[i].content;
  19310. pkcs7->contentSz = testVectors[i].contentSz;
  19311. pkcs7->contentOID = testVectors[i].contentOID;
  19312. pkcs7->encryptOID = testVectors[i].encryptOID;
  19313. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  19314. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  19315. pkcs7->heap = HEAP_HINT;
  19316. /* encode encryptedData */
  19317. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  19318. sizeof(encrypted));
  19319. AssertIntGT(encryptedSz, 0);
  19320. /* Decode encryptedData */
  19321. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  19322. decoded, sizeof(decoded));
  19323. AssertIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  19324. /* Keep values for last itr. */
  19325. if (i < testSz - 1) {
  19326. wc_PKCS7_Free(pkcs7);
  19327. }
  19328. }
  19329. printf(testingFmt, "wc_PKCS7_EncodeEncryptedData()");
  19330. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  19331. sizeof(encrypted)),BAD_FUNC_ARG);
  19332. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  19333. sizeof(encrypted)), BAD_FUNC_ARG);
  19334. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  19335. 0), BAD_FUNC_ARG);
  19336. /* Testing the struct. */
  19337. tmpBytePtr = pkcs7->content;
  19338. pkcs7->content = NULL;
  19339. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  19340. sizeof(encrypted)), BAD_FUNC_ARG);
  19341. pkcs7->content = tmpBytePtr;
  19342. tmpWrd32 = pkcs7->contentSz;
  19343. pkcs7->contentSz = 0;
  19344. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  19345. sizeof(encrypted)), BAD_FUNC_ARG);
  19346. pkcs7->contentSz = tmpWrd32;
  19347. tmpInt = pkcs7->encryptOID;
  19348. pkcs7->encryptOID = 0;
  19349. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  19350. sizeof(encrypted)), BAD_FUNC_ARG);
  19351. pkcs7->encryptOID = tmpInt;
  19352. tmpBytePtr = pkcs7->encryptionKey;
  19353. pkcs7->encryptionKey = NULL;
  19354. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  19355. sizeof(encrypted)), BAD_FUNC_ARG);
  19356. pkcs7->encryptionKey = tmpBytePtr;
  19357. tmpWrd32 = pkcs7->encryptionKeySz;
  19358. pkcs7->encryptionKeySz = 0;
  19359. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  19360. sizeof(encrypted)), BAD_FUNC_ARG);
  19361. pkcs7->encryptionKeySz = tmpWrd32;
  19362. printf(resultFmt, passed);
  19363. printf(testingFmt, "wc_PKCS7_EncodeEncryptedData()");
  19364. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  19365. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  19366. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
  19367. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  19368. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  19369. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  19370. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  19371. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  19372. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  19373. decoded, 0), BAD_FUNC_ARG);
  19374. /* Test struct fields */
  19375. tmpBytePtr = pkcs7->encryptionKey;
  19376. pkcs7->encryptionKey = NULL;
  19377. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  19378. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  19379. pkcs7->encryptionKey = tmpBytePtr;
  19380. pkcs7->encryptionKeySz = 0;
  19381. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  19382. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  19383. printf(resultFmt, passed);
  19384. wc_PKCS7_Free(pkcs7);
  19385. #endif
  19386. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  19387. /*
  19388. * Testing wc_PKCS7_Degenerate()
  19389. */
  19390. static void test_wc_PKCS7_Degenerate(void)
  19391. {
  19392. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  19393. PKCS7* pkcs7;
  19394. char fName[] = "./certs/test-degenerate.p7b";
  19395. XFILE f;
  19396. byte der[4096];
  19397. word32 derSz;
  19398. int ret;
  19399. printf(testingFmt, "wc_PKCS7_Degenerate()");
  19400. AssertNotNull(f = XFOPEN(fName, "rb"));
  19401. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  19402. derSz = (word32)ret;
  19403. XFCLOSE(f);
  19404. /* test degenerate success */
  19405. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  19406. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  19407. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  19408. #ifndef NO_RSA
  19409. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  19410. #else
  19411. AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  19412. #endif
  19413. wc_PKCS7_Free(pkcs7);
  19414. /* test with turning off degenerate cases */
  19415. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  19416. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  19417. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  19418. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  19419. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), PKCS7_NO_SIGNER_E);
  19420. wc_PKCS7_Free(pkcs7);
  19421. printf(resultFmt, passed);
  19422. #endif
  19423. } /* END test_wc_PKCS7_Degenerate() */
  19424. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  19425. defined(ASN_BER_TO_DER) && !defined(NO_DES3)
  19426. static byte berContent[] = {
  19427. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  19428. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  19429. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  19430. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  19431. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  19432. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  19433. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  19434. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  19435. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  19436. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  19437. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  19438. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  19439. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  19440. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  19441. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  19442. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  19443. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  19444. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  19445. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  19446. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  19447. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  19448. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  19449. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  19450. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  19451. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  19452. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  19453. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  19454. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  19455. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  19456. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  19457. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  19458. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  19459. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  19460. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  19461. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  19462. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  19463. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  19464. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  19465. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  19466. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  19467. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  19468. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  19469. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  19470. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  19471. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  19472. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  19473. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  19474. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  19475. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  19476. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  19477. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  19478. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  19479. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  19480. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  19481. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  19482. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  19483. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  19484. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  19485. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  19486. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  19487. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  19488. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  19489. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  19490. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  19491. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  19492. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  19493. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  19494. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  19495. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  19496. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  19497. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  19498. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  19499. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  19500. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  19501. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  19502. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  19503. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  19504. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  19505. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  19506. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  19507. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  19508. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  19509. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  19510. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  19511. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  19512. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  19513. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  19514. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  19515. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  19516. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  19517. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  19518. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  19519. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  19520. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  19521. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  19522. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  19523. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  19524. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  19525. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  19526. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  19527. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  19528. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  19529. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  19530. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  19531. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  19532. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  19533. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  19534. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  19535. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  19536. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  19537. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  19538. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  19539. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  19540. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  19541. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  19542. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  19543. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  19544. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  19545. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  19546. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  19547. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  19548. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  19549. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  19550. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  19551. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  19552. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  19553. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  19554. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  19555. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  19556. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  19557. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  19558. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  19559. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  19560. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  19561. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  19562. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  19563. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  19564. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  19565. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  19566. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  19567. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  19568. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  19569. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  19570. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  19571. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  19572. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  19573. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  19574. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  19575. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  19576. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  19577. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  19578. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  19579. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  19580. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  19581. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  19582. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  19583. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  19584. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  19585. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  19586. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  19587. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  19588. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  19589. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  19590. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  19591. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  19592. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  19593. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  19594. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  19595. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  19596. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  19597. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  19598. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  19599. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  19600. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  19601. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  19602. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  19603. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  19604. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  19605. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  19606. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  19607. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  19608. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  19609. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  19610. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  19611. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  19612. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  19613. 0x00, 0x00, 0x00, 0x00, 0x00
  19614. };
  19615. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER && !NO_DES3 */
  19616. /*
  19617. * Testing wc_PKCS7_BER()
  19618. */
  19619. static void test_wc_PKCS7_BER(void)
  19620. {
  19621. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  19622. defined(ASN_BER_TO_DER)
  19623. PKCS7* pkcs7;
  19624. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  19625. XFILE f;
  19626. byte der[4096];
  19627. #ifndef NO_DES3
  19628. byte decoded[2048];
  19629. #endif
  19630. word32 derSz;
  19631. int ret;
  19632. printf(testingFmt, "wc_PKCS7_BER()");
  19633. AssertNotNull(f = XFOPEN(fName, "rb"));
  19634. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  19635. derSz = (word32)ret;
  19636. XFCLOSE(f);
  19637. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  19638. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  19639. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  19640. #ifndef NO_RSA
  19641. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  19642. #else
  19643. AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  19644. #endif
  19645. wc_PKCS7_Free(pkcs7);
  19646. #ifndef NO_DES3
  19647. /* decode BER content */
  19648. AssertNotNull(f = XFOPEN("./certs/1024/client-cert.der", "rb"));
  19649. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  19650. derSz = (word32)ret;
  19651. XFCLOSE(f);
  19652. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  19653. #ifndef NO_RSA
  19654. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  19655. #else
  19656. AssertIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  19657. #endif
  19658. AssertNotNull(f = XFOPEN("./certs/1024/client-key.der", "rb"));
  19659. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  19660. derSz = (word32)ret;
  19661. XFCLOSE(f);
  19662. pkcs7->privateKey = der;
  19663. pkcs7->privateKeySz = derSz;
  19664. #ifndef NO_RSA
  19665. AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  19666. sizeof(berContent), decoded, sizeof(decoded)), 0);
  19667. #else
  19668. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  19669. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  19670. #endif
  19671. wc_PKCS7_Free(pkcs7);
  19672. #endif /* !NO_DES3 */
  19673. printf(resultFmt, passed);
  19674. #endif
  19675. } /* END test_wc_PKCS7_BER() */
  19676. static void test_PKCS7_signed_enveloped(void)
  19677. {
  19678. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  19679. && !defined(NO_AES)
  19680. XFILE f;
  19681. PKCS7* pkcs7;
  19682. PKCS7* inner;
  19683. void* pt;
  19684. WC_RNG rng;
  19685. unsigned char key[FOURK_BUF/2];
  19686. unsigned char cert[FOURK_BUF/2];
  19687. unsigned char env[FOURK_BUF];
  19688. int envSz = FOURK_BUF;
  19689. int keySz;
  19690. int certSz;
  19691. unsigned char sig[FOURK_BUF * 2];
  19692. int sigSz = FOURK_BUF * 2;
  19693. unsigned char decoded[FOURK_BUF];
  19694. int decodedSz = FOURK_BUF;
  19695. printf(testingFmt, "PKCS7_signed_enveloped");
  19696. /* load cert */
  19697. AssertNotNull(f = XFOPEN(cliCertDerFile, "rb"));
  19698. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  19699. XFCLOSE(f);
  19700. /* load key */
  19701. AssertNotNull(f = XFOPEN(cliKeyFile, "rb"));
  19702. AssertIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  19703. XFCLOSE(f);
  19704. keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL);
  19705. /* sign cert for envelope */
  19706. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  19707. AssertIntEQ(wc_InitRng(&rng), 0);
  19708. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  19709. pkcs7->content = cert;
  19710. pkcs7->contentSz = certSz;
  19711. pkcs7->contentOID = DATA;
  19712. pkcs7->privateKey = key;
  19713. pkcs7->privateKeySz = keySz;
  19714. pkcs7->encryptOID = RSAk;
  19715. pkcs7->hashOID = SHA256h;
  19716. pkcs7->rng = &rng;
  19717. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  19718. wc_PKCS7_Free(pkcs7);
  19719. wc_FreeRng(&rng);
  19720. /* create envelope */
  19721. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  19722. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  19723. pkcs7->content = sig;
  19724. pkcs7->contentSz = sigSz;
  19725. pkcs7->contentOID = DATA;
  19726. pkcs7->encryptOID = AES256CBCb;
  19727. pkcs7->privateKey = key;
  19728. pkcs7->privateKeySz = keySz;
  19729. AssertIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
  19730. wc_PKCS7_Free(pkcs7);
  19731. /* create bad signed enveloped data */
  19732. sigSz = FOURK_BUF * 2;
  19733. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  19734. AssertIntEQ(wc_InitRng(&rng), 0);
  19735. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  19736. pkcs7->content = env;
  19737. pkcs7->contentSz = envSz;
  19738. pkcs7->contentOID = DATA;
  19739. pkcs7->privateKey = key;
  19740. pkcs7->privateKeySz = keySz;
  19741. pkcs7->encryptOID = RSAk;
  19742. pkcs7->hashOID = SHA256h;
  19743. pkcs7->rng = &rng;
  19744. /* Set no certs in bundle for this test. Hang on to the pointer though to
  19745. * free it later. */
  19746. pt = (void*)pkcs7->certList;
  19747. pkcs7->certList = NULL; /* no certs in bundle */
  19748. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  19749. pkcs7->certList = (Pkcs7Cert*)pt; /* restore pointer for PKCS7 free call */
  19750. wc_PKCS7_Free(pkcs7);
  19751. /* check verify fails */
  19752. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  19753. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  19754. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
  19755. PKCS7_SIGNEEDS_CHECK);
  19756. /* try verifying the signature manually */
  19757. {
  19758. RsaKey rKey;
  19759. word32 idx = 0;
  19760. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  19761. WC_MAX_DIGEST_SIZE];
  19762. int digestSz;
  19763. AssertIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  19764. AssertIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
  19765. digestSz = wc_RsaSSL_Verify(pkcs7->signature, pkcs7->signatureSz,
  19766. digest, sizeof(digest), &rKey);
  19767. AssertIntGT(digestSz, 0);
  19768. AssertIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  19769. AssertIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  19770. AssertIntEQ(wc_FreeRsaKey(&rKey), 0);
  19771. /* verify was success */
  19772. }
  19773. wc_PKCS7_Free(pkcs7);
  19774. /* initializing the PKCS7 struct with the signing certificate should pass */
  19775. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  19776. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  19777. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  19778. wc_PKCS7_Free(pkcs7);
  19779. /* create valid degenerate bundle */
  19780. sigSz = FOURK_BUF * 2;
  19781. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  19782. pkcs7->content = env;
  19783. pkcs7->contentSz = envSz;
  19784. pkcs7->contentOID = DATA;
  19785. pkcs7->privateKey = key;
  19786. pkcs7->privateKeySz = keySz;
  19787. pkcs7->encryptOID = RSAk;
  19788. pkcs7->hashOID = SHA256h;
  19789. pkcs7->rng = &rng;
  19790. AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  19791. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  19792. wc_PKCS7_Free(pkcs7);
  19793. wc_FreeRng(&rng);
  19794. /* check verify */
  19795. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  19796. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, devId), 0);
  19797. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  19798. AssertNotNull(pkcs7->content);
  19799. /* check decode */
  19800. AssertNotNull(inner = wc_PKCS7_New(NULL, 0));
  19801. AssertIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
  19802. inner->privateKey = key;
  19803. inner->privateKeySz = keySz;
  19804. AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  19805. pkcs7->contentSz, decoded, decodedSz)), 0);
  19806. wc_PKCS7_Free(inner);
  19807. wc_PKCS7_Free(pkcs7);
  19808. /* check cert set */
  19809. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  19810. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  19811. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
  19812. AssertNotNull(pkcs7->singleCert);
  19813. AssertIntNE(pkcs7->singleCertSz, 0);
  19814. wc_PKCS7_Free(pkcs7);
  19815. printf(resultFmt, passed);
  19816. #endif
  19817. }
  19818. static void test_wc_i2d_PKCS12(void)
  19819. {
  19820. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  19821. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  19822. && !defined(NO_AES) && !defined(NO_DES3) && !defined(NO_SHA)
  19823. WC_PKCS12* pkcs12 = NULL;
  19824. unsigned char der[FOURK_BUF * 2];
  19825. unsigned char* pt;
  19826. int derSz;
  19827. unsigned char out[FOURK_BUF * 2];
  19828. int outSz = FOURK_BUF * 2;
  19829. const char p12_f[] = "./certs/test-servercert.p12";
  19830. XFILE f;
  19831. printf(testingFmt, "wc_i2d_PKCS12");
  19832. f = XFOPEN(p12_f, "rb");
  19833. AssertNotNull(f);
  19834. derSz = (int)XFREAD(der, 1, sizeof(der), f);
  19835. AssertIntGT(derSz, 0);
  19836. XFCLOSE(f);
  19837. AssertNotNull(pkcs12 = wc_PKCS12_new());
  19838. AssertIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
  19839. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  19840. AssertIntEQ(outSz, derSz);
  19841. outSz = derSz - 1;
  19842. pt = out;
  19843. AssertIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  19844. outSz = derSz;
  19845. AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  19846. AssertIntEQ((pt == out), 0);
  19847. pt = NULL;
  19848. AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  19849. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  19850. wc_PKCS12_free(pkcs12);
  19851. printf(resultFmt, passed);
  19852. #endif
  19853. }
  19854. /* Testing wc_SignatureGetSize() for signature type ECC */
  19855. static int test_wc_SignatureGetSize_ecc(void)
  19856. {
  19857. int ret = 0;
  19858. #ifndef NO_SIG_WRAPPER
  19859. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  19860. enum wc_SignatureType sig_type;
  19861. word32 key_len;
  19862. /* Initialize ECC Key */
  19863. ecc_key ecc;
  19864. const char* qx =
  19865. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  19866. const char* qy =
  19867. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  19868. const char* d =
  19869. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  19870. ret = wc_ecc_init(&ecc);
  19871. if (ret == 0) {
  19872. ret = wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1");
  19873. }
  19874. printf(testingFmt, "wc_SigntureGetSize_ecc()");
  19875. if (ret == 0) {
  19876. /* Input for signature type ECC */
  19877. sig_type = WC_SIGNATURE_TYPE_ECC;
  19878. key_len = sizeof(ecc_key);
  19879. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  19880. /* Test bad args */
  19881. if (ret > 0) {
  19882. sig_type = (enum wc_SignatureType) 100;
  19883. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  19884. if (ret == BAD_FUNC_ARG) {
  19885. sig_type = WC_SIGNATURE_TYPE_ECC;
  19886. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  19887. }
  19888. if (ret >= 0) {
  19889. key_len = (word32) 0;
  19890. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  19891. }
  19892. if (ret == BAD_FUNC_ARG) {
  19893. ret = SIG_TYPE_E;
  19894. }
  19895. }
  19896. } else {
  19897. ret = WOLFSSL_FATAL_ERROR;
  19898. }
  19899. wc_ecc_free(&ecc);
  19900. #else
  19901. ret = SIG_TYPE_E;
  19902. #endif
  19903. if (ret == SIG_TYPE_E) {
  19904. ret = 0;
  19905. }
  19906. else {
  19907. ret = WOLFSSL_FATAL_ERROR;
  19908. }
  19909. printf(resultFmt, ret == 0 ? passed : failed);
  19910. #endif /* NO_SIG_WRAPPER */
  19911. return ret;
  19912. }/* END test_wc_SignatureGetSize_ecc() */
  19913. /* Testing wc_SignatureGetSize() for signature type rsa */
  19914. static int test_wc_SignatureGetSize_rsa(void)
  19915. {
  19916. int ret = 0;
  19917. #ifndef NO_SIG_WRAPPER
  19918. #ifndef NO_RSA
  19919. enum wc_SignatureType sig_type;
  19920. word32 key_len;
  19921. word32 idx = 0;
  19922. /* Initialize RSA Key */
  19923. RsaKey rsa_key;
  19924. byte* tmp = NULL;
  19925. size_t bytes;
  19926. #ifdef USE_CERT_BUFFERS_1024
  19927. bytes = (size_t)sizeof_client_key_der_1024;
  19928. if (bytes < (size_t)sizeof_client_key_der_1024)
  19929. bytes = (size_t)sizeof_client_cert_der_1024;
  19930. #elif defined(USE_CERT_BUFFERS_2048)
  19931. bytes = (size_t)sizeof_client_key_der_2048;
  19932. if (bytes < (size_t)sizeof_client_cert_der_2048)
  19933. bytes = (size_t)sizeof_client_cert_der_2048;
  19934. #else
  19935. bytes = FOURK_BUF;
  19936. #endif
  19937. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19938. if (tmp != NULL) {
  19939. #ifdef USE_CERT_BUFFERS_1024
  19940. XMEMCPY(tmp, client_key_der_1024,
  19941. (size_t)sizeof_client_key_der_1024);
  19942. #elif defined(USE_CERT_BUFFERS_2048)
  19943. XMEMCPY(tmp, client_key_der_2048,
  19944. (size_t)sizeof_client_key_der_2048);
  19945. #elif !defined(NO_FILESYSTEM)
  19946. file = XFOPEN(clientKey, "rb");
  19947. if (file != XBADFILE) {
  19948. bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file);
  19949. XFCLOSE(file);
  19950. }
  19951. else {
  19952. ret = WOLFSSL_FATAL_ERROR;
  19953. }
  19954. #else
  19955. ret = WOLFSSL_FATAL_ERROR;
  19956. #endif
  19957. } else {
  19958. ret = WOLFSSL_FATAL_ERROR;
  19959. }
  19960. if (ret == 0) {
  19961. ret = wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, devId);
  19962. }
  19963. if (ret == 0) {
  19964. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes);
  19965. }
  19966. printf(testingFmt, "wc_SigntureGetSize_rsa()");
  19967. if (ret == 0) {
  19968. /* Input for signature type RSA */
  19969. sig_type = WC_SIGNATURE_TYPE_RSA;
  19970. key_len = sizeof(RsaKey);
  19971. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  19972. /* Test bad args */
  19973. if (ret > 0) {
  19974. sig_type = (enum wc_SignatureType) 100;
  19975. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  19976. if (ret == BAD_FUNC_ARG) {
  19977. sig_type = WC_SIGNATURE_TYPE_RSA;
  19978. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  19979. }
  19980. #ifndef HAVE_USER_RSA
  19981. if (ret == BAD_FUNC_ARG) {
  19982. #else
  19983. if (ret == 0) {
  19984. #endif
  19985. key_len = (word32)0;
  19986. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  19987. }
  19988. if (ret == BAD_FUNC_ARG) {
  19989. ret = SIG_TYPE_E;
  19990. }
  19991. }
  19992. } else {
  19993. ret = WOLFSSL_FATAL_ERROR;
  19994. }
  19995. wc_FreeRsaKey(&rsa_key);
  19996. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19997. #else
  19998. ret = SIG_TYPE_E;
  19999. #endif
  20000. if (ret == SIG_TYPE_E) {
  20001. ret = 0;
  20002. }else {
  20003. ret = WOLFSSL_FATAL_ERROR;
  20004. }
  20005. printf(resultFmt, ret == 0 ? passed : failed);
  20006. #endif /* NO_SIG_WRAPPER */
  20007. return ret;
  20008. }/* END test_wc_SignatureGetSize_rsa(void) */
  20009. /*----------------------------------------------------------------------------*
  20010. | hash.h Tests
  20011. *----------------------------------------------------------------------------*/
  20012. static int test_wc_HashInit(void)
  20013. {
  20014. int ret = 0, i; /* 0 indicates tests passed, 1 indicates failure */
  20015. wc_HashAlg hash;
  20016. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  20017. enum wc_HashType enumArray[] = {
  20018. #ifndef NO_MD5
  20019. WC_HASH_TYPE_MD5,
  20020. #endif
  20021. #ifndef NO_SHA
  20022. WC_HASH_TYPE_SHA,
  20023. #endif
  20024. #ifndef WOLFSSL_SHA224
  20025. WC_HASH_TYPE_SHA224,
  20026. #endif
  20027. #ifndef NO_SHA256
  20028. WC_HASH_TYPE_SHA256,
  20029. #endif
  20030. #ifndef WOLFSSL_SHA384
  20031. WC_HASH_TYPE_SHA384,
  20032. #endif
  20033. #ifndef WOLFSSL_SHA512
  20034. WC_HASH_TYPE_SHA512,
  20035. #endif
  20036. };
  20037. /* dynamically finds the length */
  20038. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  20039. /* For loop to test various arguments... */
  20040. for (i = 0; i < enumlen; i++) {
  20041. /* check for bad args */
  20042. if (wc_HashInit(&hash, enumArray[i]) == BAD_FUNC_ARG) {
  20043. ret = 1;
  20044. break;
  20045. }
  20046. wc_HashFree(&hash, enumArray[i]);
  20047. /* check for null ptr */
  20048. if (wc_HashInit(NULL, enumArray[i]) != BAD_FUNC_ARG) {
  20049. ret = 1;
  20050. break;
  20051. }
  20052. } /* end of for loop */
  20053. printf(testingFmt, "wc_HashInit()");
  20054. if (ret==0) { /* all tests have passed */
  20055. printf(resultFmt, passed);
  20056. }
  20057. else { /* a test has failed */
  20058. printf(resultFmt, failed);
  20059. }
  20060. return ret;
  20061. } /* end of test_wc_HashInit */
  20062. /*
  20063. * Unit test function for wc_HashSetFlags()
  20064. */
  20065. static int test_wc_HashSetFlags(void)
  20066. {
  20067. int ret = 0;
  20068. #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
  20069. wc_HashAlg hash;
  20070. word32 flags = 0;
  20071. int i, j;
  20072. printf(testingFmt, "wc_HashSetFlags()");
  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. #ifdef WOLFSSL_SHA224
  20082. WC_HASH_TYPE_SHA224,
  20083. #endif
  20084. #ifndef NO_SHA256
  20085. WC_HASH_TYPE_SHA256,
  20086. #endif
  20087. #ifdef WOLFSSL_SHA384
  20088. WC_HASH_TYPE_SHA384,
  20089. #endif
  20090. #ifdef WOLFSSL_SHA512
  20091. WC_HASH_TYPE_SHA512,
  20092. #endif
  20093. #ifdef WOLFSSL_SHA3
  20094. WC_HASH_TYPE_SHA3_224,
  20095. #endif
  20096. };
  20097. enum wc_HashType notSupported[] = {
  20098. WC_HASH_TYPE_MD5_SHA,
  20099. WC_HASH_TYPE_MD2,
  20100. WC_HASH_TYPE_MD4,
  20101. WC_HASH_TYPE_BLAKE2B,
  20102. WC_HASH_TYPE_BLAKE2S,
  20103. WC_HASH_TYPE_NONE,
  20104. };
  20105. /* dynamically finds the length */
  20106. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  20107. /* For loop to test various arguments... */
  20108. for (i = 0; i < enumlen; i++) {
  20109. ret = wc_HashInit(&hash, enumArray[i]);
  20110. if (ret == 0) {
  20111. ret = wc_HashSetFlags(&hash, enumArray[i], flags);
  20112. }
  20113. if (ret == 0) {
  20114. if (flags & WC_HASH_FLAG_ISCOPY) {
  20115. ret = 0;
  20116. }
  20117. }
  20118. if (ret == 0) {
  20119. ret = wc_HashSetFlags(NULL, enumArray[i], flags);
  20120. if (ret == BAD_FUNC_ARG) {
  20121. ret = 0;
  20122. }
  20123. }
  20124. wc_HashFree(&hash, enumArray[i]);
  20125. }
  20126. /* For loop to test not supported cases */
  20127. int notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  20128. for (j = 0; j < notSupportedLen; j++){
  20129. if (ret == 0) {
  20130. ret = wc_HashInit(&hash, notSupported[j]);
  20131. if (ret == BAD_FUNC_ARG){
  20132. ret = 0;
  20133. if (ret == 0){
  20134. ret = wc_HashSetFlags(&hash, notSupported[j], flags);
  20135. if (ret == BAD_FUNC_ARG) {
  20136. ret = 0;
  20137. }
  20138. }
  20139. }
  20140. }
  20141. if (ret == 0) {
  20142. ret = wc_HashFree(&hash, notSupported[j]);
  20143. if (ret == BAD_FUNC_ARG) {
  20144. ret = 0;
  20145. }
  20146. }
  20147. }
  20148. printf(resultFmt, ret == 0 ? passed : failed);
  20149. #endif
  20150. return ret;
  20151. } /* END test_wc_HashSetFlags */
  20152. /*
  20153. * Unit test function for wc_HashGetFlags()
  20154. */
  20155. static int test_wc_HashGetFlags(void)
  20156. {
  20157. int ret = 0;
  20158. #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
  20159. wc_HashAlg hash;
  20160. word32 flags = 0;
  20161. int i, j;
  20162. printf(testingFmt, "wc_HashGetFlags()");
  20163. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  20164. enum wc_HashType enumArray[] = {
  20165. #ifndef NO_MD5
  20166. WC_HASH_TYPE_MD5,
  20167. #endif
  20168. #ifndef NO_SHA
  20169. WC_HASH_TYPE_SHA,
  20170. #endif
  20171. #ifdef WOLFSSL_SHA224
  20172. WC_HASH_TYPE_SHA224,
  20173. #endif
  20174. #ifndef NO_SHA256
  20175. WC_HASH_TYPE_SHA256,
  20176. #endif
  20177. #ifdef WOLFSSL_SHA384
  20178. WC_HASH_TYPE_SHA384,
  20179. #endif
  20180. #ifdef WOLFSSL_SHA512
  20181. WC_HASH_TYPE_SHA512,
  20182. #endif
  20183. #ifdef WOLFSSL_SHA3
  20184. WC_HASH_TYPE_SHA3_224,
  20185. #endif
  20186. };
  20187. enum wc_HashType notSupported[] = {
  20188. WC_HASH_TYPE_MD5_SHA,
  20189. WC_HASH_TYPE_MD2,
  20190. WC_HASH_TYPE_MD4,
  20191. WC_HASH_TYPE_BLAKE2B,
  20192. WC_HASH_TYPE_BLAKE2S,
  20193. WC_HASH_TYPE_NONE,
  20194. };
  20195. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  20196. /* For loop to test various arguments... */
  20197. for (i = 0; i < enumlen; i++) {
  20198. ret = wc_HashInit(&hash, enumArray[i]);
  20199. if (ret == 0) {
  20200. ret = wc_HashGetFlags(&hash, enumArray[i], &flags);
  20201. }
  20202. if (ret == 0) {
  20203. if (flags & WC_HASH_FLAG_ISCOPY) {
  20204. ret = 0;
  20205. }
  20206. }
  20207. if (ret == 0) {
  20208. ret = wc_HashGetFlags(NULL, enumArray[i], &flags);
  20209. if (ret == BAD_FUNC_ARG) {
  20210. ret = 0;
  20211. }
  20212. }
  20213. wc_HashFree(&hash, enumArray[i]);
  20214. if (ret != 0) {
  20215. break;
  20216. }
  20217. }
  20218. /* For loop to test not supported cases */
  20219. int notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  20220. for (j = 0; j < notSupportedLen; j++){
  20221. if (ret == 0) {
  20222. ret = wc_HashInit(&hash, notSupported[j]);
  20223. if (ret == BAD_FUNC_ARG){
  20224. ret = 0;
  20225. if (ret == 0){
  20226. ret = wc_HashGetFlags(&hash, notSupported[j], &flags);
  20227. if (ret == BAD_FUNC_ARG) {
  20228. ret = 0;
  20229. }
  20230. }
  20231. }
  20232. }
  20233. if (ret == 0) {
  20234. ret = wc_HashFree(&hash, notSupported[j]);
  20235. if (ret == BAD_FUNC_ARG) {
  20236. ret = 0;
  20237. }
  20238. }
  20239. }
  20240. printf(resultFmt, ret == 0 ? passed : failed);
  20241. #endif
  20242. return ret;
  20243. } /* END test_wc_HashGetFlags */
  20244. /*----------------------------------------------------------------------------*
  20245. | Compatibility Tests
  20246. *----------------------------------------------------------------------------*/
  20247. static void test_wolfSSL_X509_NAME(void)
  20248. {
  20249. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  20250. && !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  20251. X509* x509;
  20252. const unsigned char* c;
  20253. unsigned char buf[4096];
  20254. int bytes;
  20255. XFILE f;
  20256. const X509_NAME* a;
  20257. const X509_NAME* b;
  20258. int sz;
  20259. unsigned char* tmp;
  20260. char file[] = "./certs/ca-cert.der";
  20261. printf(testingFmt, "wolfSSL_X509_NAME()");
  20262. /* test compile of deprecated function, returns 0 */
  20263. AssertIntEQ(CRYPTO_thread_id(), 0);
  20264. AssertNotNull(a = X509_NAME_new());
  20265. X509_NAME_free((X509_NAME*)a);
  20266. f = XFOPEN(file, "rb");
  20267. AssertTrue(f != XBADFILE);
  20268. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  20269. XFCLOSE(f);
  20270. c = buf;
  20271. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(c, bytes,
  20272. SSL_FILETYPE_ASN1));
  20273. /* test cmp function */
  20274. AssertNotNull(a = X509_get_issuer_name(x509));
  20275. AssertNotNull(b = X509_get_subject_name(x509));
  20276. AssertIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  20277. tmp = buf;
  20278. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  20279. if (sz > 0 && tmp == buf) {
  20280. printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__); \
  20281. printf(" Expected pointer to be incremented\n");
  20282. abort();
  20283. }
  20284. /* retry but with the function creating a buffer */
  20285. tmp = NULL;
  20286. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  20287. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  20288. AssertNotNull(b = X509_NAME_dup((X509_NAME*)a));
  20289. AssertIntEQ(X509_NAME_cmp(a, b), 0);
  20290. X509_NAME_free((X509_NAME*)b);
  20291. X509_free(x509);
  20292. printf(resultFmt, passed);
  20293. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  20294. }
  20295. static void test_wolfSSL_X509_INFO(void)
  20296. {
  20297. #if defined(OPENSSL_ALL)
  20298. STACK_OF(X509_INFO) *info_stack;
  20299. X509_INFO *info;
  20300. BIO *cert;
  20301. int i;
  20302. printf(testingFmt, "wolfSSL_X509_INFO");
  20303. AssertNotNull(cert = BIO_new_file(cliCertFileExt, "r"));
  20304. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  20305. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  20306. AssertNotNull(info = sk_X509_INFO_value(info_stack, i));
  20307. AssertNotNull(info->x509);
  20308. AssertNull(info->crl);
  20309. }
  20310. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  20311. BIO_free(cert);
  20312. AssertNotNull(cert = BIO_new_file(cliCertFileExt, "r"));
  20313. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  20314. sk_X509_INFO_free(info_stack);
  20315. BIO_free(cert);
  20316. printf(resultFmt, passed);
  20317. #endif
  20318. }
  20319. static void test_wolfSSL_X509_subject_name_hash(void)
  20320. {
  20321. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  20322. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  20323. X509* x509;
  20324. X509_NAME* subjectName = NULL;
  20325. unsigned long ret = 0;
  20326. printf(testingFmt, "wolfSSL_X509_subject_name_hash()");
  20327. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  20328. SSL_FILETYPE_PEM));
  20329. AssertNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  20330. ret = X509_subject_name_hash(x509);
  20331. AssertIntNE(ret, 0);
  20332. X509_free(x509);
  20333. printf(resultFmt, passed);
  20334. #endif
  20335. }
  20336. static void test_wolfSSL_X509_issuer_name_hash(void)
  20337. {
  20338. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  20339. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  20340. X509* x509;
  20341. X509_NAME* issuertName = NULL;
  20342. unsigned long ret = 0;
  20343. printf(testingFmt, "wolfSSL_X509_issuer_name_hash()");
  20344. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  20345. SSL_FILETYPE_PEM));
  20346. AssertNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  20347. ret = X509_issuer_name_hash(x509);
  20348. AssertIntNE(ret, 0);
  20349. X509_free(x509);
  20350. printf(resultFmt, passed);
  20351. #endif
  20352. }
  20353. static void test_wolfSSL_DES(void)
  20354. {
  20355. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  20356. const_DES_cblock myDes;
  20357. DES_cblock iv;
  20358. DES_key_schedule key;
  20359. word32 i;
  20360. DES_LONG dl;
  20361. unsigned char msg[] = "hello wolfssl";
  20362. printf(testingFmt, "wolfSSL_DES()");
  20363. DES_check_key(1);
  20364. DES_set_key(&myDes, &key);
  20365. /* check, check of odd parity */
  20366. XMEMSET(myDes, 4, sizeof(const_DES_cblock)); myDes[0] = 6; /*set even parity*/
  20367. XMEMSET(key, 5, sizeof(DES_key_schedule));
  20368. AssertIntEQ(DES_set_key_checked(&myDes, &key), -1);
  20369. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  20370. /* set odd parity for success case */
  20371. DES_set_odd_parity(&myDes);
  20372. AssertIntEQ(DES_check_key_parity(&myDes), 1);
  20373. printf("%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2], myDes[3]);
  20374. AssertIntEQ(DES_set_key_checked(&myDes, &key), 0);
  20375. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  20376. AssertIntEQ(key[i], myDes[i]);
  20377. }
  20378. AssertIntEQ(DES_is_weak_key(&myDes), 0);
  20379. /* check weak key */
  20380. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  20381. XMEMSET(key, 5, sizeof(DES_key_schedule));
  20382. AssertIntEQ(DES_set_key_checked(&myDes, &key), -2);
  20383. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  20384. /* now do unchecked copy of a weak key over */
  20385. DES_set_key_unchecked(&myDes, &key);
  20386. /* compare arrays, should be the same */
  20387. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  20388. AssertIntEQ(key[i], myDes[i]);
  20389. }
  20390. AssertIntEQ(DES_is_weak_key(&myDes), 1);
  20391. /* check DES_key_sched API */
  20392. XMEMSET(key, 1, sizeof(DES_key_schedule));
  20393. AssertIntEQ(DES_key_sched(&myDes, NULL), 0);
  20394. AssertIntEQ(DES_key_sched(NULL, &key), 0);
  20395. AssertIntEQ(DES_key_sched(&myDes, &key), 0);
  20396. /* compare arrays, should be the same */
  20397. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  20398. AssertIntEQ(key[i], myDes[i]);
  20399. }
  20400. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  20401. * DES_cbc_encrypt on the input */
  20402. XMEMSET(iv, 0, sizeof(DES_cblock));
  20403. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  20404. AssertIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  20405. AssertIntEQ(dl, 480052723);
  20406. printf(resultFmt, passed);
  20407. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  20408. }
  20409. static void test_wc_PemToDer(void)
  20410. {
  20411. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER)
  20412. int ret;
  20413. DerBuffer* pDer = NULL;
  20414. const char* ca_cert = "./certs/server-cert.pem";
  20415. byte* cert_buf = NULL;
  20416. size_t cert_sz = 0;
  20417. int eccKey = 0;
  20418. EncryptedInfo info;
  20419. printf(testingFmt, "wc_PemToDer()");
  20420. XMEMSET(&info, 0, sizeof(info));
  20421. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  20422. if (ret == 0) {
  20423. ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE,
  20424. &pDer, NULL, &info, &eccKey);
  20425. AssertIntEQ(ret, 0);
  20426. wc_FreeDer(&pDer);
  20427. }
  20428. if (cert_buf)
  20429. free(cert_buf);
  20430. #ifdef HAVE_ECC
  20431. {
  20432. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  20433. byte key_buf[256] = {0};
  20434. /* Test fail of loading a key with cert type */
  20435. AssertIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  20436. key_buf[0] = '\n';
  20437. XMEMCPY(key_buf + 1, cert_buf, cert_sz);
  20438. AssertIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  20439. &pDer, NULL, &info, &eccKey)), 0);
  20440. #ifdef OPENSSL_EXTRA
  20441. AssertIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  20442. &pDer, NULL, &info, &eccKey)), 0);
  20443. #endif
  20444. wc_FreeDer(&pDer);
  20445. if (cert_buf)
  20446. free(cert_buf);
  20447. }
  20448. #endif
  20449. printf(resultFmt, passed);
  20450. #endif
  20451. }
  20452. static void test_wc_AllocDer(void)
  20453. {
  20454. #if !defined(NO_CERTS)
  20455. int ret;
  20456. DerBuffer* pDer = NULL;
  20457. word32 testSize = 1024;
  20458. printf(testingFmt, "wc_AllocDer()");
  20459. ret = wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT);
  20460. AssertIntEQ(ret, 0);
  20461. AssertNotNull(pDer);
  20462. wc_FreeDer(&pDer);
  20463. printf(resultFmt, passed);
  20464. #endif
  20465. }
  20466. static void test_wc_CertPemToDer(void)
  20467. {
  20468. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER)
  20469. int ret;
  20470. const char* ca_cert = "./certs/ca-cert.pem";
  20471. byte* cert_buf = NULL;
  20472. size_t cert_sz = 0, cert_dersz = 0;
  20473. byte* cert_der = NULL;
  20474. printf(testingFmt, "wc_CertPemToDer()");
  20475. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  20476. if (ret == 0) {
  20477. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  20478. cert_der = (byte*)malloc(cert_dersz);
  20479. if (cert_der) {
  20480. ret = wc_CertPemToDer(cert_buf, (int)cert_sz,
  20481. cert_der, (int)cert_dersz, CERT_TYPE);
  20482. AssertIntGE(ret, 0);
  20483. }
  20484. }
  20485. if (cert_der)
  20486. free(cert_der);
  20487. if (cert_buf)
  20488. free(cert_buf);
  20489. #endif
  20490. }
  20491. static void test_wc_PubKeyPemToDer(void)
  20492. {
  20493. #ifdef WOLFSSL_PEM_TO_DER
  20494. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER)
  20495. int ret;
  20496. const char* key = "./certs/ecc-client-keyPub.pem";
  20497. byte* cert_buf = NULL;
  20498. size_t cert_sz = 0, cert_dersz = 0;
  20499. byte* cert_der = NULL;
  20500. printf(testingFmt, "wc_PubKeyPemToDer()");
  20501. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  20502. cert_der, (int)cert_dersz);
  20503. AssertIntGE(ret, BAD_FUNC_ARG);
  20504. ret = load_file(key, &cert_buf, &cert_sz);
  20505. if (ret == 0) {
  20506. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  20507. cert_der = (byte*)malloc(cert_dersz);
  20508. if (cert_der) {
  20509. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  20510. cert_der, (int)cert_dersz);
  20511. AssertIntGE(ret, 0);
  20512. }
  20513. }
  20514. if (cert_der)
  20515. free(cert_der);
  20516. if (cert_buf)
  20517. free(cert_buf);
  20518. #endif
  20519. #endif
  20520. }
  20521. static void test_wc_PemPubKeyToDer(void)
  20522. {
  20523. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER)
  20524. int ret;
  20525. const char* key = "./certs/ecc-client-keyPub.pem";
  20526. size_t cert_dersz = 1024;
  20527. byte* cert_der = (byte*)malloc(cert_dersz);
  20528. printf(testingFmt, "wc_PemPubKeyToDer()");
  20529. ret = wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz);
  20530. AssertIntGE(ret, BAD_FUNC_ARG);
  20531. if (cert_der) {
  20532. ret = wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz);
  20533. AssertIntGE(ret, 0);
  20534. free(cert_der);
  20535. }
  20536. #endif
  20537. }
  20538. static void test_wolfSSL_certs(void)
  20539. {
  20540. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  20541. !defined(NO_RSA)
  20542. X509* x509;
  20543. WOLFSSL* ssl;
  20544. WOLFSSL_CTX* ctx;
  20545. STACK_OF(ASN1_OBJECT)* sk;
  20546. ASN1_BIT_STRING* bit_str;
  20547. int crit;
  20548. printf(testingFmt, "wolfSSL_certs()");
  20549. #ifndef NO_WOLFSSL_SERVER
  20550. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  20551. #else
  20552. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  20553. #endif
  20554. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  20555. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  20556. #ifndef HAVE_USER_RSA
  20557. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  20558. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  20559. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  20560. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  20561. #endif
  20562. AssertNotNull(ssl = SSL_new(ctx));
  20563. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  20564. #ifdef HAVE_PK_CALLBACKS
  20565. AssertIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  20566. #endif /* HAVE_PK_CALLBACKS */
  20567. /* create and use x509 */
  20568. x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt, WOLFSSL_FILETYPE_PEM);
  20569. AssertNotNull(x509);
  20570. AssertIntEQ(SSL_use_certificate(ssl, x509), WOLFSSL_SUCCESS);
  20571. #ifndef HAVE_USER_RSA
  20572. /* with loading in a new cert the check on private key should now fail */
  20573. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  20574. #endif
  20575. #if defined(USE_CERT_BUFFERS_2048)
  20576. AssertIntEQ(SSL_use_certificate_ASN1(ssl,
  20577. (unsigned char*)server_cert_der_2048,
  20578. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  20579. #endif
  20580. #if !defined(NO_SHA) && !defined(NO_SHA256)
  20581. /************* Get Digest of Certificate ******************/
  20582. {
  20583. byte digest[64]; /* max digest size */
  20584. word32 digestSz;
  20585. XMEMSET(digest, 0, sizeof(digest));
  20586. AssertIntEQ(X509_digest(x509, wolfSSL_EVP_sha1(), digest, &digestSz),
  20587. WOLFSSL_SUCCESS);
  20588. AssertIntEQ(X509_digest(x509, wolfSSL_EVP_sha256(), digest, &digestSz),
  20589. WOLFSSL_SUCCESS);
  20590. AssertIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  20591. WOLFSSL_FAILURE);
  20592. }
  20593. #endif /* !NO_SHA && !NO_SHA256*/
  20594. /* test and checkout X509 extensions */
  20595. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_basic_constraints,
  20596. &crit, NULL);
  20597. AssertNotNull(sk);
  20598. AssertIntEQ(crit, 0);
  20599. sk_ASN1_OBJECT_free(sk);
  20600. bit_str = (ASN1_BIT_STRING*)X509_get_ext_d2i(x509, NID_key_usage, &crit, NULL);
  20601. AssertNotNull(bit_str);
  20602. AssertIntEQ(crit, 1);
  20603. AssertIntEQ(bit_str->type, NID_key_usage);
  20604. ASN1_BIT_STRING_free(bit_str);
  20605. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_ext_key_usage,
  20606. &crit, NULL);
  20607. /* AssertNotNull(sk); no extension set */
  20608. sk_ASN1_OBJECT_free(sk);
  20609. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  20610. NID_authority_key_identifier, &crit, NULL);
  20611. AssertNotNull(sk);
  20612. sk_ASN1_OBJECT_free(sk);
  20613. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  20614. NID_private_key_usage_period, &crit, NULL);
  20615. /* AssertNotNull(sk); NID not yet supported */
  20616. AssertIntEQ(crit, -1);
  20617. sk_ASN1_OBJECT_free(sk);
  20618. sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509, NID_subject_alt_name,
  20619. &crit, NULL);
  20620. /* AssertNotNull(sk); no alt names set */
  20621. sk_GENERAL_NAME_free(sk);
  20622. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_issuer_alt_name,
  20623. &crit, NULL);
  20624. /* AssertNotNull(sk); NID not yet supported */
  20625. AssertIntEQ(crit, -1);
  20626. sk_ASN1_OBJECT_free(sk);
  20627. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_info_access, &crit,
  20628. NULL);
  20629. /* AssertNotNull(sk); no auth info set */
  20630. sk_ASN1_OBJECT_free(sk);
  20631. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_sinfo_access,
  20632. &crit, NULL);
  20633. /* AssertNotNull(sk); NID not yet supported */
  20634. AssertIntEQ(crit, -1);
  20635. sk_ASN1_OBJECT_free(sk);
  20636. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_name_constraints,
  20637. &crit, NULL);
  20638. /* AssertNotNull(sk); NID not yet supported */
  20639. AssertIntEQ(crit, -1);
  20640. sk_ASN1_OBJECT_free(sk);
  20641. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  20642. NID_certificate_policies, &crit, NULL);
  20643. #if !defined(WOLFSSL_SEP) && !defined(WOLFSSL_CERT_EXT)
  20644. AssertNull(sk);
  20645. #else
  20646. /* AssertNotNull(sk); no cert policy set */
  20647. #endif
  20648. sk_ASN1_OBJECT_free(sk);
  20649. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_policy_mappings,
  20650. &crit, NULL);
  20651. /* AssertNotNull(sk); NID not yet supported */
  20652. AssertIntEQ(crit, -1);
  20653. sk_ASN1_OBJECT_free(sk);
  20654. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_policy_constraints,
  20655. &crit, NULL);
  20656. /* AssertNotNull(sk); NID not yet supported */
  20657. AssertIntEQ(crit, -1);
  20658. sk_ASN1_OBJECT_free(sk);
  20659. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_inhibit_any_policy,
  20660. &crit, NULL);
  20661. /* AssertNotNull(sk); NID not yet supported */
  20662. AssertIntEQ(crit, -1);
  20663. sk_ASN1_OBJECT_free(sk);
  20664. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_tlsfeature, &crit,
  20665. NULL);
  20666. /* AssertNotNull(sk); NID not yet supported */
  20667. AssertIntEQ(crit, -1);
  20668. sk_ASN1_OBJECT_free(sk);
  20669. /* test invalid cases */
  20670. crit = 0;
  20671. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, -1, &crit, NULL);
  20672. AssertNull(sk);
  20673. AssertIntEQ(crit, -1);
  20674. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL, NID_tlsfeature,
  20675. NULL, NULL);
  20676. AssertNull(sk);
  20677. AssertIntEQ(SSL_get_hit(ssl), 0);
  20678. X509_free(x509);
  20679. SSL_free(ssl);
  20680. SSL_CTX_free(ctx);
  20681. printf(resultFmt, passed);
  20682. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  20683. }
  20684. static void test_wolfSSL_ASN1_TIME_print(void)
  20685. {
  20686. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) \
  20687. && (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  20688. defined(WOLFSSL_HAPROXY)) && defined(USE_CERT_BUFFERS_2048)
  20689. BIO* bio;
  20690. X509* x509;
  20691. const unsigned char* der = client_cert_der_2048;
  20692. ASN1_TIME* t;
  20693. unsigned char buf[25];
  20694. printf(testingFmt, "wolfSSL_ASN1_TIME_print()");
  20695. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  20696. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  20697. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  20698. AssertIntEQ(ASN1_TIME_print(bio, X509_get_notBefore(x509)), 1);
  20699. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  20700. AssertIntEQ(XMEMCMP(buf, "May 7 07:39:03 2020 GMT", sizeof(buf) - 1), 0);
  20701. /* create a bad time and test results */
  20702. AssertNotNull(t = X509_get_notAfter(x509));
  20703. AssertIntEQ(ASN1_TIME_check(t), WOLFSSL_SUCCESS);
  20704. t->data[8] = 0;
  20705. t->data[3] = 0;
  20706. AssertIntNE(ASN1_TIME_print(bio, t), 1);
  20707. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  20708. AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  20709. AssertIntEQ(ASN1_TIME_check(t), WOLFSSL_FAILURE);
  20710. BIO_free(bio);
  20711. X509_free(x509);
  20712. printf(resultFmt, passed);
  20713. #endif
  20714. }
  20715. static void test_wolfSSL_ASN1_UTCTIME_print(void)
  20716. {
  20717. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  20718. BIO* bio;
  20719. ASN1_UTCTIME* utc = NULL;
  20720. unsigned char buf[25];
  20721. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  20722. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  20723. printf(testingFmt, "ASN1_UTCTIME_print()");
  20724. /* NULL parameter check */
  20725. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  20726. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  20727. BIO_free(bio);
  20728. /* Valid date */
  20729. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  20730. AssertNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  20731. DYNAMIC_TYPE_ASN1));
  20732. utc->type = ASN_UTC_TIME;
  20733. utc->length = ASN_UTC_TIME_SIZE;
  20734. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  20735. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  20736. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  20737. AssertIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  20738. XMEMSET(buf, 0, sizeof(buf));
  20739. BIO_free(bio);
  20740. /* Invalid format */
  20741. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  20742. utc->type = ASN_UTC_TIME;
  20743. utc->length = ASN_UTC_TIME_SIZE;
  20744. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  20745. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  20746. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  20747. AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  20748. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  20749. BIO_free(bio);
  20750. printf(resultFmt, passed);
  20751. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME */
  20752. }
  20753. static void test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  20754. {
  20755. #if defined(OPENSSL_EXTRA)
  20756. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime;
  20757. unsigned char nullstr[32];
  20758. XMEMSET(nullstr, 0, 32);
  20759. asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  20760. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL,
  20761. DYNAMIC_TYPE_TMP_BUFFER);
  20762. if (asn1_gtime) {
  20763. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  20764. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  20765. AssertIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  20766. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  20767. }
  20768. #endif /* OPENSSL_EXTRA */
  20769. }
  20770. static void test_wolfSSL_private_keys(void)
  20771. {
  20772. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  20773. !defined(NO_FILESYSTEM)
  20774. WOLFSSL* ssl;
  20775. WOLFSSL_CTX* ctx;
  20776. EVP_PKEY* pkey = NULL;
  20777. printf(testingFmt, "wolfSSL_private_keys()");
  20778. OpenSSL_add_all_digests();
  20779. OpenSSL_add_all_algorithms();
  20780. #ifndef NO_RSA
  20781. #ifndef NO_WOLFSSL_SERVER
  20782. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  20783. #else
  20784. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  20785. #endif
  20786. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  20787. /* Have to load a cert before you can check the private key against that
  20788. * certificates public key! */
  20789. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  20790. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  20791. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  20792. AssertNotNull(ssl = SSL_new(ctx));
  20793. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  20794. #ifdef USE_CERT_BUFFERS_2048
  20795. {
  20796. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  20797. unsigned char buf[FOURK_BUF];
  20798. word32 bufSz;
  20799. AssertIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  20800. (unsigned char*)client_key_der_2048,
  20801. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  20802. #ifndef HAVE_USER_RSA
  20803. /* Should mismatch now that a different private key loaded */
  20804. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  20805. #endif
  20806. AssertIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  20807. (unsigned char*)server_key,
  20808. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  20809. /* After loading back in DER format of original key, should match */
  20810. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  20811. /* test loading private key to the WOLFSSL_CTX */
  20812. AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  20813. (unsigned char*)client_key_der_2048,
  20814. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  20815. #ifndef NO_CHECK_PRIVATE_KEY
  20816. #ifndef HAVE_USER_RSA
  20817. /* Should mismatch now that a different private key loaded */
  20818. AssertIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  20819. #endif
  20820. AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  20821. (unsigned char*)server_key,
  20822. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  20823. /* After loading back in DER format of original key, should match */
  20824. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  20825. #endif /* !NO_CHECK_PRIVATE_KEY */
  20826. /* pkey not set yet, expecting to fail */
  20827. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  20828. /* set PKEY and test again */
  20829. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  20830. &server_key, (long)sizeof_server_key_der_2048));
  20831. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  20832. /* reuse PKEY structure and test
  20833. * this should be checked with a memory management sanity checker */
  20834. AssertFalse(server_key == (const unsigned char*)server_key_der_2048);
  20835. server_key = (const unsigned char*)server_key_der_2048;
  20836. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  20837. &server_key, (long)sizeof_server_key_der_2048));
  20838. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  20839. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  20840. bufSz = FOURK_BUF;
  20841. AssertIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  20842. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  20843. RSAk, NULL, 0)), 0);
  20844. server_key = (const unsigned char*)buf;
  20845. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  20846. (long)bufSz));
  20847. }
  20848. #endif
  20849. EVP_PKEY_free(pkey);
  20850. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  20851. SSL_CTX_free(ctx);
  20852. #endif /* end of RSA private key match tests */
  20853. #ifdef HAVE_ECC
  20854. #ifndef NO_WOLFSSL_SERVER
  20855. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  20856. #else
  20857. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  20858. #endif
  20859. AssertTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  20860. WOLFSSL_FILETYPE_PEM));
  20861. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  20862. WOLFSSL_FILETYPE_PEM));
  20863. AssertNotNull(ssl = SSL_new(ctx));
  20864. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  20865. SSL_free(ssl);
  20866. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  20867. WOLFSSL_FILETYPE_PEM));
  20868. AssertNotNull(ssl = SSL_new(ctx));
  20869. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  20870. SSL_free(ssl);
  20871. SSL_CTX_free(ctx);
  20872. #endif /* end of ECC private key match tests */
  20873. #ifdef HAVE_ED25519
  20874. #ifndef NO_WOLFSSL_SERVER
  20875. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  20876. #else
  20877. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  20878. #endif
  20879. AssertTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  20880. WOLFSSL_FILETYPE_PEM));
  20881. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  20882. WOLFSSL_FILETYPE_PEM));
  20883. AssertNotNull(ssl = SSL_new(ctx));
  20884. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  20885. SSL_free(ssl);
  20886. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  20887. WOLFSSL_FILETYPE_PEM));
  20888. AssertNotNull(ssl = SSL_new(ctx));
  20889. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  20890. SSL_free(ssl);
  20891. SSL_CTX_free(ctx);
  20892. #endif /* end of Ed25519 private key match tests */
  20893. #ifdef HAVE_ED448
  20894. #ifndef NO_WOLFSSL_SERVER
  20895. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  20896. #else
  20897. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  20898. #endif
  20899. AssertTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  20900. WOLFSSL_FILETYPE_PEM));
  20901. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  20902. WOLFSSL_FILETYPE_PEM));
  20903. AssertNotNull(ssl = SSL_new(ctx));
  20904. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  20905. SSL_free(ssl);
  20906. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  20907. WOLFSSL_FILETYPE_PEM));
  20908. AssertNotNull(ssl = SSL_new(ctx));
  20909. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  20910. SSL_free(ssl);
  20911. SSL_CTX_free(ctx);
  20912. #endif /* end of Ed448 private key match tests */
  20913. EVP_cleanup();
  20914. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  20915. CONF_modules_free();
  20916. ENGINE_cleanup();
  20917. CONF_modules_unload();
  20918. (void)ssl;
  20919. (void)ctx;
  20920. (void)pkey;
  20921. printf(resultFmt, passed);
  20922. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  20923. }
  20924. static void test_wolfSSL_PEM_PrivateKey(void)
  20925. {
  20926. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  20927. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  20928. BIO* bio = NULL;
  20929. EVP_PKEY* pkey = NULL;
  20930. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  20931. /* test creating new EVP_PKEY with bad arg */
  20932. AssertNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  20933. /* test loading RSA key using BIO */
  20934. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  20935. {
  20936. XFILE file;
  20937. const char* fname = "./certs/server-key.pem";
  20938. size_t sz;
  20939. byte* buf;
  20940. file = XFOPEN(fname, "rb");
  20941. AssertTrue((file != XBADFILE));
  20942. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  20943. sz = XFTELL(file);
  20944. XREWIND(file);
  20945. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  20946. if (buf) {
  20947. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  20948. }
  20949. XFCLOSE(file);
  20950. /* Test using BIO new mem and loading PEM private key */
  20951. bio = BIO_new_mem_buf(buf, (int)sz);
  20952. AssertNotNull(bio);
  20953. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  20954. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  20955. BIO_free(bio);
  20956. bio = NULL;
  20957. EVP_PKEY_free(pkey);
  20958. pkey = NULL;
  20959. }
  20960. #endif
  20961. /* test loading ECC key using BIO */
  20962. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  20963. {
  20964. XFILE file;
  20965. const char* fname = "./certs/ecc-key.pem";
  20966. size_t sz;
  20967. byte* buf;
  20968. file = XFOPEN(fname, "rb");
  20969. AssertTrue((file != XBADFILE));
  20970. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  20971. sz = XFTELL(file);
  20972. XREWIND(file);
  20973. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  20974. if (buf)
  20975. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  20976. XFCLOSE(file);
  20977. /* Test using BIO new mem and loading PEM private key */
  20978. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  20979. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  20980. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  20981. BIO_free(bio);
  20982. bio = NULL;
  20983. EVP_PKEY_free(pkey);
  20984. pkey = NULL;
  20985. }
  20986. #endif
  20987. #if !defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN))
  20988. {
  20989. #define BIO_PEM_TEST_CHAR 'a'
  20990. EVP_PKEY* pkey2 = NULL;
  20991. unsigned char extra[10];
  20992. int i;
  20993. printf(testingFmt, "wolfSSL_PEM_PrivateKey()");
  20994. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  20995. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  20996. AssertIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  20997. AssertNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
  20998. &server_key, (long)sizeof_server_key_der_2048));
  20999. AssertNull(pkey);
  21000. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  21001. &server_key, (long)sizeof_server_key_der_2048));
  21002. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  21003. WOLFSSL_SUCCESS);
  21004. /* test creating new EVP_PKEY with good args */
  21005. AssertNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  21006. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr)
  21007. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz), 0);
  21008. /* test of reuse of EVP_PKEY */
  21009. AssertNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  21010. AssertIntEQ(BIO_pending(bio), 0);
  21011. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  21012. SSL_SUCCESS);
  21013. AssertIntEQ(BIO_write(bio, extra, 10), 10); /* add 10 extra bytes after PEM */
  21014. AssertNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  21015. AssertNotNull(pkey);
  21016. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  21017. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
  21018. }
  21019. AssertIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
  21020. AssertIntEQ(BIO_read(bio, extra, 10), 10);
  21021. for (i = 0; i < 10; i++) {
  21022. AssertIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  21023. }
  21024. BIO_free(bio);
  21025. bio = NULL;
  21026. EVP_PKEY_free(pkey);
  21027. pkey = NULL;
  21028. EVP_PKEY_free(pkey2);
  21029. }
  21030. #endif
  21031. /* key is DES encrypted */
  21032. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  21033. !defined(NO_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_MD5)
  21034. {
  21035. XFILE f;
  21036. pem_password_cb* passwd_cb;
  21037. void* passwd_cb_userdata;
  21038. SSL_CTX* ctx;
  21039. char passwd[] = "bad password";
  21040. #ifndef WOLFSSL_NO_TLS12
  21041. #ifndef NO_WOLFSSL_SERVER
  21042. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  21043. #else
  21044. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  21045. #endif
  21046. #else
  21047. #ifndef NO_WOLFSSL_SERVER
  21048. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  21049. #else
  21050. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  21051. #endif
  21052. #endif
  21053. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  21054. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  21055. AssertNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  21056. AssertNull(passwd_cb_userdata =
  21057. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  21058. /* fail case with password call back */
  21059. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  21060. (void*)passwd));
  21061. BIO_free(bio);
  21062. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  21063. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  21064. (void*)passwd));
  21065. BIO_free(bio);
  21066. f = XFOPEN("./certs/server-keyEnc.pem", "rb");
  21067. AssertNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  21068. /* use callback that works */
  21069. AssertNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  21070. (void*)"yassl123"));
  21071. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  21072. EVP_PKEY_free(pkey);
  21073. pkey = NULL;
  21074. BIO_free(bio);
  21075. bio = NULL;
  21076. SSL_CTX_free(ctx);
  21077. }
  21078. #endif /* !defined(NO_DES3) */
  21079. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  21080. {
  21081. unsigned char buf[2048];
  21082. size_t bytes;
  21083. XFILE f;
  21084. SSL_CTX* ctx;
  21085. #ifndef WOLFSSL_NO_TLS12
  21086. #ifndef NO_WOLFSSL_SERVER
  21087. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  21088. #else
  21089. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  21090. #endif
  21091. #else
  21092. #ifndef NO_WOLFSSL_SERVER
  21093. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  21094. #else
  21095. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  21096. #endif
  21097. #endif
  21098. f = XFOPEN("./certs/ecc-key.der", "rb");
  21099. AssertTrue((f != XBADFILE));
  21100. bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f);
  21101. XFCLOSE(f);
  21102. server_key = buf;
  21103. pkey = NULL;
  21104. AssertNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  21105. AssertNull(pkey);
  21106. AssertNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  21107. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  21108. EVP_PKEY_free(pkey);
  21109. pkey = NULL;
  21110. SSL_CTX_free(ctx);
  21111. }
  21112. #endif
  21113. printf(resultFmt, passed);
  21114. (void)bio;
  21115. (void)pkey;
  21116. (void)server_key;
  21117. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  21118. }
  21119. static void test_wolfSSL_PEM_bio_RSAKey(void)
  21120. {
  21121. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  21122. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  21123. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  21124. RSA* rsa = NULL;
  21125. BIO* bio = NULL;
  21126. printf(testingFmt, "wolfSSL_PEM_bio_RSAKey");
  21127. /* PrivateKey */
  21128. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  21129. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  21130. AssertNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  21131. AssertIntEQ(RSA_size(rsa), 256);
  21132. AssertIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  21133. NULL), WOLFSSL_FAILURE);
  21134. BIO_free(bio);
  21135. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  21136. AssertIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  21137. NULL), WOLFSSL_SUCCESS);
  21138. BIO_free(bio);
  21139. RSA_free(rsa);
  21140. /* PUBKEY */
  21141. AssertNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  21142. AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  21143. AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  21144. AssertIntEQ(RSA_size(rsa), 256);
  21145. AssertIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  21146. BIO_free(bio);
  21147. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  21148. AssertIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  21149. BIO_free(bio);
  21150. RSA_free(rsa);
  21151. #ifdef HAVE_ECC
  21152. /* ensure that non-rsa keys do not work */
  21153. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  21154. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  21155. AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  21156. BIO_free(bio);
  21157. RSA_free(rsa);
  21158. #endif /* HAVE_ECC */
  21159. printf(resultFmt, passed);
  21160. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  21161. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  21162. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  21163. }
  21164. static void test_wolfSSL_PEM_RSAPrivateKey(void)
  21165. {
  21166. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  21167. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  21168. RSA* rsa = NULL;
  21169. RSA* rsa_dup = NULL;
  21170. BIO* bio = NULL;
  21171. printf(testingFmt, "wolfSSL_PEM_RSAPrivateKey()");
  21172. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  21173. AssertNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  21174. AssertIntEQ(RSA_size(rsa), 256);
  21175. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  21176. AssertNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  21177. AssertPtrNE(rsa_dup, rsa);
  21178. #endif
  21179. /* test if valgrind complains about unreleased memory */
  21180. RSA_up_ref(rsa);
  21181. RSA_free(rsa);
  21182. BIO_free(bio);
  21183. RSA_free(rsa);
  21184. RSA_free(rsa_dup);
  21185. #ifdef HAVE_ECC
  21186. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  21187. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  21188. BIO_free(bio);
  21189. #endif /* HAVE_ECC */
  21190. printf(resultFmt, passed);
  21191. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  21192. }
  21193. static void test_wolfSSL_PEM_bio_DSAKey(void)
  21194. {
  21195. #ifndef HAVE_SELFTEST
  21196. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  21197. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  21198. DSA* dsa = NULL;
  21199. BIO* bio = NULL;
  21200. printf(testingFmt, "wolfSSL_PEM_bio_DSAKey");
  21201. /* PrivateKey */
  21202. AssertNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  21203. AssertNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  21204. AssertNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  21205. AssertIntEQ(BN_num_bytes(dsa->g), 128);
  21206. AssertIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, NULL),
  21207. WOLFSSL_FAILURE);
  21208. BIO_free(bio);
  21209. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  21210. AssertIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL, NULL),
  21211. WOLFSSL_SUCCESS);
  21212. BIO_free(bio);
  21213. DSA_free(dsa);
  21214. /* PUBKEY */
  21215. AssertNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  21216. AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  21217. AssertNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  21218. AssertIntEQ(BN_num_bytes(dsa->g), 128);
  21219. AssertIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  21220. BIO_free(bio);
  21221. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  21222. AssertIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  21223. BIO_free(bio);
  21224. DSA_free(dsa);
  21225. #ifdef HAVE_ECC
  21226. /* ensure that non-dsa keys do not work */
  21227. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  21228. AssertNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  21229. AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  21230. BIO_free(bio);
  21231. DSA_free(dsa);
  21232. #endif /* HAVE_ECC */
  21233. printf(resultFmt, passed);
  21234. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  21235. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  21236. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  21237. #endif /* HAVE_SELFTEST */
  21238. }
  21239. static void test_wolfSSL_PEM_bio_ECKey(void)
  21240. {
  21241. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  21242. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  21243. EC_KEY* ec = NULL;
  21244. BIO* bio = NULL;
  21245. printf(testingFmt, "wolfSSL_PEM_bio_ECKey");
  21246. /* PrivateKey */
  21247. AssertNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  21248. AssertNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  21249. AssertNotNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  21250. AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  21251. AssertIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  21252. NULL),WOLFSSL_FAILURE);
  21253. BIO_free(bio);
  21254. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  21255. AssertIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  21256. NULL), WOLFSSL_SUCCESS);
  21257. BIO_free(bio);
  21258. EC_KEY_free(ec);
  21259. /* PUBKEY */
  21260. AssertNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  21261. AssertNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  21262. AssertNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  21263. AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  21264. AssertIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  21265. BIO_free(bio);
  21266. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  21267. AssertIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  21268. BIO_free(bio);
  21269. EC_KEY_free(ec);
  21270. #ifndef NO_RSA
  21271. /* ensure that non-ec keys do not work */
  21272. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  21273. AssertNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  21274. AssertNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  21275. BIO_free(bio);
  21276. EC_KEY_free(ec);
  21277. #endif /* HAVE_ECC */
  21278. printf(resultFmt, passed);
  21279. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  21280. }
  21281. static void test_wolfSSL_PEM_PUBKEY(void)
  21282. {
  21283. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  21284. BIO* bio = NULL;
  21285. EVP_PKEY* pkey = NULL;
  21286. /* test creating new EVP_PKEY with bad arg */
  21287. AssertNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  21288. /* test loading ECC key using BIO */
  21289. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  21290. {
  21291. XFILE file;
  21292. const char* fname = "./certs/ecc-client-keyPub.pem";
  21293. size_t sz;
  21294. byte* buf;
  21295. file = XFOPEN(fname, "rb");
  21296. AssertTrue((file != XBADFILE));
  21297. XFSEEK(file, 0, XSEEK_END);
  21298. sz = XFTELL(file);
  21299. XREWIND(file);
  21300. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  21301. if (buf)
  21302. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  21303. XFCLOSE(file);
  21304. /* Test using BIO new mem and loading PEM private key */
  21305. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  21306. AssertNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  21307. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  21308. BIO_free(bio);
  21309. bio = NULL;
  21310. EVP_PKEY_free(pkey);
  21311. pkey = NULL;
  21312. }
  21313. #endif
  21314. (void)bio;
  21315. (void)pkey;
  21316. #endif
  21317. }
  21318. static void test_DSA_do_sign_verify(void)
  21319. {
  21320. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  21321. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  21322. !defined(NO_DSA)
  21323. unsigned char digest[WC_SHA_DIGEST_SIZE];
  21324. DSA_SIG* sig;
  21325. DSA* dsa;
  21326. word32 bytes;
  21327. byte sigBin[DSA_SIG_SIZE];
  21328. int dsacheck;
  21329. #ifdef USE_CERT_BUFFERS_1024
  21330. byte tmp[ONEK_BUF];
  21331. XMEMSET(tmp, 0, sizeof(tmp));
  21332. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  21333. bytes = sizeof_dsa_key_der_1024;
  21334. #elif defined(USE_CERT_BUFFERS_2048)
  21335. byte tmp[TWOK_BUF];
  21336. XMEMSET(tmp, 0, sizeof(tmp));
  21337. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  21338. bytes = sizeof_dsa_key_der_2048;
  21339. #else
  21340. byte tmp[TWOK_BUF];
  21341. XMEMSET(tmp, 0, sizeof(tmp));
  21342. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  21343. if (fp == XBADFILE) {
  21344. return WOLFSSL_BAD_FILE;
  21345. }
  21346. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  21347. XFCLOSE(fp);
  21348. #endif /* END USE_CERT_BUFFERS_1024 */
  21349. printf(testingFmt, "DSA_do_sign_verify()");
  21350. XMEMSET(digest, 202, sizeof(digest));
  21351. AssertNotNull(dsa = DSA_new());
  21352. AssertIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
  21353. AssertIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  21354. AssertIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  21355. AssertNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  21356. AssertIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  21357. DSA_SIG_free(sig);
  21358. DSA_free(dsa);
  21359. #endif
  21360. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  21361. }
  21362. static void test_wolfSSL_tmp_dh(void)
  21363. {
  21364. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  21365. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH)
  21366. byte buffer[6000];
  21367. char file[] = "./certs/dsaparams.pem";
  21368. XFILE f;
  21369. int bytes;
  21370. DSA* dsa;
  21371. DH* dh;
  21372. BIO* bio;
  21373. SSL* ssl;
  21374. SSL_CTX* ctx;
  21375. printf(testingFmt, "wolfSSL_tmp_dh()");
  21376. #ifndef NO_WOLFSSL_SERVER
  21377. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  21378. #else
  21379. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  21380. #endif
  21381. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  21382. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  21383. AssertNotNull(ssl = SSL_new(ctx));
  21384. f = XFOPEN(file, "rb");
  21385. AssertTrue((f != XBADFILE));
  21386. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  21387. XFCLOSE(f);
  21388. bio = BIO_new_mem_buf((void*)buffer, bytes);
  21389. AssertNotNull(bio);
  21390. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  21391. AssertNotNull(dsa);
  21392. dh = wolfSSL_DSA_dup_DH(dsa);
  21393. AssertNotNull(dh);
  21394. AssertIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  21395. #ifndef NO_WOLFSSL_SERVER
  21396. AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  21397. #else
  21398. AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  21399. #endif
  21400. BIO_free(bio);
  21401. DSA_free(dsa);
  21402. DH_free(dh);
  21403. SSL_free(ssl);
  21404. SSL_CTX_free(ctx);
  21405. printf(resultFmt, passed);
  21406. #endif
  21407. }
  21408. static void test_wolfSSL_ctrl(void)
  21409. {
  21410. #if defined (OPENSSL_EXTRA)
  21411. byte buff[6000];
  21412. BIO* bio;
  21413. int bytes;
  21414. BUF_MEM* ptr = NULL;
  21415. printf(testingFmt, "wolfSSL_crtl()");
  21416. bytes = sizeof(buff);
  21417. bio = BIO_new_mem_buf((void*)buff, bytes);
  21418. AssertNotNull(bio);
  21419. AssertNotNull(BIO_s_socket());
  21420. AssertIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  21421. /* needs tested after stubs filled out @TODO
  21422. SSL_ctrl
  21423. SSL_CTX_ctrl
  21424. */
  21425. BIO_free(bio);
  21426. printf(resultFmt, passed);
  21427. #endif /* defined(OPENSSL_EXTRA) */
  21428. }
  21429. static void test_wolfSSL_EVP_PKEY_new_mac_key(void)
  21430. {
  21431. #ifdef OPENSSL_EXTRA
  21432. static const unsigned char pw[] = "password";
  21433. static const int pwSz = sizeof(pw) - 1;
  21434. size_t checkPwSz = 0;
  21435. const unsigned char* checkPw = NULL;
  21436. WOLFSSL_EVP_PKEY* key = NULL;
  21437. printf(testingFmt, "wolfSSL_EVP_PKEY_new_mac_key()");
  21438. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  21439. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  21440. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, pwSz));
  21441. if (key) {
  21442. AssertIntEQ(key->type, EVP_PKEY_HMAC);
  21443. AssertIntEQ(key->save_type, EVP_PKEY_HMAC);
  21444. AssertIntEQ(key->pkey_sz, pwSz);
  21445. AssertIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  21446. }
  21447. AssertNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  21448. AssertIntEQ((int)checkPwSz, pwSz);
  21449. if (checkPw) {
  21450. AssertIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  21451. }
  21452. wolfSSL_EVP_PKEY_free(key);
  21453. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, 0));
  21454. if (key) {
  21455. AssertIntEQ(key->pkey_sz, 0);
  21456. }
  21457. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  21458. (void)checkPw;
  21459. AssertIntEQ((int)checkPwSz, 0);
  21460. wolfSSL_EVP_PKEY_free(key);
  21461. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL, 0));
  21462. if (key) {
  21463. AssertIntEQ(key->pkey_sz, 0);
  21464. }
  21465. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  21466. (void)checkPw;
  21467. AssertIntEQ((int)checkPwSz, 0);
  21468. wolfSSL_EVP_PKEY_free(key);
  21469. printf(resultFmt, passed);
  21470. #endif /* OPENSSL_EXTRA */
  21471. }
  21472. static void test_wolfSSL_EVP_Digest(void)
  21473. {
  21474. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  21475. const char* in = "abc";
  21476. int inLen = (int)XSTRLEN(in);
  21477. byte out[WC_SHA256_DIGEST_SIZE];
  21478. unsigned int outLen;
  21479. const char* expOut = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  21480. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  21481. "\x15\xAD";
  21482. printf(testingFmt, "wolfSSL_EVP_Digest()");
  21483. AssertIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen, "SHA256", NULL), 1);
  21484. AssertIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  21485. AssertIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  21486. printf(resultFmt, passed);
  21487. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  21488. }
  21489. static void test_wolfSSL_EVP_MD_size(void)
  21490. {
  21491. #ifdef OPENSSL_EXTRA
  21492. WOLFSSL_EVP_MD_CTX mdCtx;
  21493. printf(testingFmt, "wolfSSL_EVP_MD_size()");
  21494. #ifndef NO_SHA256
  21495. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21496. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  21497. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA256_DIGEST_SIZE);
  21498. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  21499. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21500. #endif
  21501. #ifndef NO_MD5
  21502. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21503. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  21504. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  21505. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  21506. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21507. #endif
  21508. #ifdef WOLFSSL_SHA224
  21509. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21510. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  21511. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  21512. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  21513. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21514. #endif
  21515. #ifdef WOLFSSL_SHA384
  21516. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21517. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  21518. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  21519. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  21520. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21521. #endif
  21522. #ifdef WOLFSSL_SHA512
  21523. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21524. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  21525. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  21526. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  21527. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21528. #endif
  21529. #ifndef NO_SHA
  21530. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21531. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  21532. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  21533. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  21534. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21535. #endif
  21536. /* error case */
  21537. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21538. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  21539. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), BAD_FUNC_ARG);
  21540. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  21541. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 0);
  21542. printf(resultFmt, passed);
  21543. #endif /* OPENSSL_EXTRA */
  21544. }
  21545. static void test_wolfSSL_EVP_MD_hmac_signing(void)
  21546. {
  21547. #ifdef OPENSSL_EXTRA
  21548. const unsigned char testKey[] =
  21549. {
  21550. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  21551. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  21552. 0x0b, 0x0b, 0x0b, 0x0b
  21553. };
  21554. const char testData[] = "Hi There";
  21555. const unsigned char testResult[] =
  21556. {
  21557. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  21558. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  21559. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  21560. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  21561. };
  21562. unsigned char check[sizeof(testResult)];
  21563. size_t checkSz = -1;
  21564. WOLFSSL_EVP_PKEY* key;
  21565. WOLFSSL_EVP_MD_CTX mdCtx;
  21566. printf(testingFmt, "wolfSSL_EVP_MD_hmac_signing()");
  21567. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  21568. testKey, (int)sizeof(testKey)));
  21569. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21570. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  21571. NULL, key), 1);
  21572. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  21573. (unsigned int)XSTRLEN(testData)), 1);
  21574. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  21575. AssertIntEQ((int)checkSz, sizeof(testResult));
  21576. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  21577. AssertIntEQ((int)checkSz,(int)sizeof(testResult));
  21578. AssertIntEQ(XMEMCMP(testResult, check, sizeof(testResult)), 0);
  21579. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21580. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  21581. NULL, key), 1);
  21582. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  21583. (unsigned int)XSTRLEN(testData)),
  21584. 1);
  21585. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  21586. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21587. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21588. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  21589. NULL, key), 1);
  21590. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  21591. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  21592. AssertIntEQ((int)checkSz, sizeof(testResult));
  21593. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  21594. AssertIntEQ((int)checkSz,(int)sizeof(testResult));
  21595. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  21596. (unsigned int)XSTRLEN(testData) - 4), 1);
  21597. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  21598. AssertIntEQ((int)checkSz,(int)sizeof(testResult));
  21599. AssertIntEQ(XMEMCMP(testResult, check, sizeof(testResult)), 0);
  21600. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21601. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  21602. NULL, key), 1);
  21603. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  21604. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  21605. (unsigned int)XSTRLEN(testData) - 4),
  21606. 1);
  21607. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  21608. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21609. wolfSSL_EVP_PKEY_free(key);
  21610. printf(resultFmt, passed);
  21611. #endif /* OPENSSL_EXTRA */
  21612. }
  21613. static void test_wolfSSL_EVP_MD_rsa_signing(void)
  21614. {
  21615. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  21616. defined(USE_CERT_BUFFERS_2048)
  21617. WOLFSSL_EVP_PKEY* privKey;
  21618. WOLFSSL_EVP_PKEY* pubKey;
  21619. const char testData[] = "Hi There";
  21620. WOLFSSL_EVP_MD_CTX mdCtx;
  21621. size_t checkSz = -1;
  21622. int sz = 2048 / 8;
  21623. const unsigned char* cp;
  21624. const unsigned char* p;
  21625. unsigned char check[2048/8];
  21626. printf(testingFmt, "wolfSSL_EVP_MD_rsa_signing()");
  21627. cp = client_key_der_2048;
  21628. AssertNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  21629. sizeof_client_key_der_2048)));
  21630. p = client_keypub_der_2048;
  21631. AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  21632. sizeof_client_keypub_der_2048)));
  21633. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21634. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  21635. NULL, privKey), 1);
  21636. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  21637. (unsigned int)XSTRLEN(testData)), 1);
  21638. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  21639. AssertIntEQ((int)checkSz, sz);
  21640. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  21641. AssertIntEQ((int)checkSz,sz);
  21642. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21643. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21644. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  21645. NULL, pubKey), 1);
  21646. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  21647. (unsigned int)XSTRLEN(testData)),
  21648. 1);
  21649. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  21650. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21651. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21652. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  21653. NULL, privKey), 1);
  21654. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  21655. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  21656. AssertIntEQ((int)checkSz, sz);
  21657. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  21658. AssertIntEQ((int)checkSz, sz);
  21659. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  21660. (unsigned int)XSTRLEN(testData) - 4), 1);
  21661. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  21662. AssertIntEQ((int)checkSz, sz);
  21663. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21664. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21665. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  21666. NULL, pubKey), 1);
  21667. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  21668. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  21669. (unsigned int)XSTRLEN(testData) - 4),
  21670. 1);
  21671. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  21672. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21673. wolfSSL_EVP_PKEY_free(pubKey);
  21674. wolfSSL_EVP_PKEY_free(privKey);
  21675. printf(resultFmt, passed);
  21676. #endif
  21677. }
  21678. static void test_wolfSSL_EVP_MD_ecc_signing(void)
  21679. {
  21680. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  21681. WOLFSSL_EVP_PKEY* privKey;
  21682. WOLFSSL_EVP_PKEY* pubKey;
  21683. const char testData[] = "Hi There";
  21684. WOLFSSL_EVP_MD_CTX mdCtx;
  21685. size_t checkSz = -1;
  21686. const unsigned char* cp;
  21687. const unsigned char* p;
  21688. unsigned char check[2048/8];
  21689. printf(testingFmt, "wolfSSL_EVP_MD_ecc_signing()");
  21690. cp = ecc_clikey_der_256;
  21691. privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  21692. sizeof_ecc_clikey_der_256);
  21693. AssertNotNull(privKey);
  21694. p = ecc_clikeypub_der_256;
  21695. AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  21696. sizeof_ecc_clikeypub_der_256)));
  21697. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21698. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  21699. NULL, privKey), 1);
  21700. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  21701. (unsigned int)XSTRLEN(testData)), 1);
  21702. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  21703. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  21704. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21705. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21706. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  21707. NULL, pubKey), 1);
  21708. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  21709. (unsigned int)XSTRLEN(testData)),
  21710. 1);
  21711. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  21712. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21713. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21714. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  21715. NULL, privKey), 1);
  21716. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  21717. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  21718. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  21719. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  21720. (unsigned int)XSTRLEN(testData) - 4), 1);
  21721. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  21722. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21723. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  21724. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  21725. NULL, pubKey), 1);
  21726. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  21727. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  21728. (unsigned int)XSTRLEN(testData) - 4),
  21729. 1);
  21730. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  21731. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  21732. wolfSSL_EVP_PKEY_free(pubKey);
  21733. wolfSSL_EVP_PKEY_free(privKey);
  21734. printf(resultFmt, passed);
  21735. #endif
  21736. }
  21737. static void test_wolfSSL_CTX_add_extra_chain_cert(void)
  21738. {
  21739. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  21740. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  21741. char caFile[] = "./certs/client-ca.pem";
  21742. char clientFile[] = "./certs/client-cert.pem";
  21743. SSL_CTX* ctx;
  21744. X509* x509;
  21745. printf(testingFmt, "wolfSSL_CTX_add_extra_chain_cert()");
  21746. #ifndef NO_WOLFSSL_SERVER
  21747. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  21748. #else
  21749. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  21750. #endif
  21751. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  21752. AssertNotNull(x509);
  21753. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  21754. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  21755. AssertNotNull(x509);
  21756. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  21757. /* additional test of getting EVP_PKEY key size from X509
  21758. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  21759. * allowed with user RSA */
  21760. {
  21761. EVP_PKEY* pkey;
  21762. #if defined(HAVE_ECC)
  21763. X509* ecX509;
  21764. #endif /* HAVE_ECC */
  21765. AssertNotNull(pkey = X509_get_pubkey(x509));
  21766. /* current RSA key is 2048 bit (256 bytes) */
  21767. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  21768. EVP_PKEY_free(pkey);
  21769. #if defined(HAVE_ECC)
  21770. #if defined(USE_CERT_BUFFERS_256)
  21771. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  21772. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  21773. SSL_FILETYPE_ASN1));
  21774. #else
  21775. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(cliEccCertFile,
  21776. SSL_FILETYPE_PEM));
  21777. #endif
  21778. pkey = X509_get_pubkey(ecX509);
  21779. AssertNotNull(pkey);
  21780. /* current ECC key is 256 bit (32 bytes) */
  21781. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  21782. X509_free(ecX509);
  21783. EVP_PKEY_free(pkey);
  21784. #endif /* HAVE_ECC */
  21785. }
  21786. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  21787. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  21788. #ifdef WOLFSSL_ENCRYPTED_KEYS
  21789. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  21790. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  21791. #endif
  21792. SSL_CTX_free(ctx);
  21793. printf(resultFmt, passed);
  21794. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  21795. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  21796. }
  21797. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  21798. static void test_wolfSSL_ERR_peek_last_error_line(void)
  21799. {
  21800. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  21801. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  21802. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  21803. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  21804. tcp_ready ready;
  21805. func_args client_args;
  21806. func_args server_args;
  21807. #ifndef SINGLE_THREADED
  21808. THREAD_TYPE serverThread;
  21809. #endif
  21810. callback_functions client_cb;
  21811. callback_functions server_cb;
  21812. int line = 0;
  21813. int flag = ERR_TXT_STRING;
  21814. const char* file = NULL;
  21815. const char* data = NULL;
  21816. printf(testingFmt, "wolfSSL_ERR_peek_last_error_line()");
  21817. /* create a failed connection and inspect the error */
  21818. #ifdef WOLFSSL_TIRTOS
  21819. fdOpenSession(Task_self());
  21820. #endif
  21821. XMEMSET(&client_args, 0, sizeof(func_args));
  21822. XMEMSET(&server_args, 0, sizeof(func_args));
  21823. StartTCP();
  21824. InitTcpReady(&ready);
  21825. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  21826. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  21827. client_cb.method = wolfTLSv1_1_client_method;
  21828. server_cb.method = wolfTLSv1_2_server_method;
  21829. server_args.signal = &ready;
  21830. server_args.callbacks = &server_cb;
  21831. client_args.signal = &ready;
  21832. client_args.callbacks = &client_cb;
  21833. #ifndef SINGLE_THREADED
  21834. start_thread(test_server_nofail, &server_args, &serverThread);
  21835. wait_tcp_ready(&server_args);
  21836. test_client_nofail(&client_args, NULL);
  21837. join_thread(serverThread);
  21838. #endif
  21839. FreeTcpReady(&ready);
  21840. AssertIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  21841. AssertNotNull(data);
  21842. /* check clearing error state */
  21843. ERR_remove_state(0);
  21844. AssertIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  21845. ERR_peek_last_error_line(NULL, &line);
  21846. AssertIntEQ(line, 0);
  21847. ERR_peek_last_error_line(&file, NULL);
  21848. AssertNull(file);
  21849. /* retry connection to fill error queue */
  21850. XMEMSET(&client_args, 0, sizeof(func_args));
  21851. XMEMSET(&server_args, 0, sizeof(func_args));
  21852. StartTCP();
  21853. InitTcpReady(&ready);
  21854. client_cb.method = wolfTLSv1_1_client_method;
  21855. server_cb.method = wolfTLSv1_2_server_method;
  21856. server_args.signal = &ready;
  21857. server_args.callbacks = &server_cb;
  21858. client_args.signal = &ready;
  21859. client_args.callbacks = &client_cb;
  21860. start_thread(test_server_nofail, &server_args, &serverThread);
  21861. wait_tcp_ready(&server_args);
  21862. test_client_nofail(&client_args, NULL);
  21863. join_thread(serverThread);
  21864. FreeTcpReady(&ready);
  21865. /* check that error code was stored */
  21866. AssertIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  21867. ERR_peek_last_error_line(NULL, &line);
  21868. AssertIntNE(line, 0);
  21869. ERR_peek_last_error_line(&file, NULL);
  21870. AssertNotNull(file);
  21871. #ifdef WOLFSSL_TIRTOS
  21872. fdOpenSession(Task_self());
  21873. #endif
  21874. printf(resultFmt, passed);
  21875. printf("\nTesting error print out\n");
  21876. ERR_print_errors_fp(stdout);
  21877. printf("Done testing print out\n\n");
  21878. fflush(stdout);
  21879. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  21880. !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  21881. }
  21882. #endif
  21883. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  21884. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  21885. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  21886. {
  21887. (void) ok;
  21888. (void) ctx;
  21889. printf("ENTER verify_cb\n");
  21890. return SSL_SUCCESS;
  21891. }
  21892. #endif
  21893. static void test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  21894. {
  21895. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  21896. #ifdef WOLFSSL_SIGNER_DER_CERT
  21897. int cmp;
  21898. #endif
  21899. X509_STORE_CTX* ctx;
  21900. X509_STORE* str;
  21901. X509* x509Ca;
  21902. X509* x509Svr;
  21903. X509* issuer;
  21904. X509_NAME* caName;
  21905. X509_NAME* issuerName;
  21906. printf(testingFmt, "wolfSSL_X509_STORE_CTX_get0_current_issuer()");
  21907. AssertNotNull(ctx = X509_STORE_CTX_new());
  21908. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  21909. AssertNotNull((x509Ca =
  21910. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  21911. AssertIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  21912. AssertNotNull((x509Svr =
  21913. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  21914. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  21915. AssertNull(X509_STORE_CTX_get0_current_issuer(NULL));
  21916. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  21917. AssertNotNull(issuer);
  21918. caName = X509_get_subject_name(x509Ca);
  21919. AssertNotNull(caName);
  21920. issuerName = X509_get_subject_name(issuer);
  21921. #ifdef WOLFSSL_SIGNER_DER_CERT
  21922. AssertNotNull(issuerName);
  21923. cmp = X509_NAME_cmp(caName, issuerName);
  21924. AssertIntEQ(cmp, 0);
  21925. #else
  21926. /* X509_STORE_CTX_get0_current_issuer() returns empty issuer */
  21927. AssertNull(issuerName);
  21928. #endif
  21929. X509_free(issuer);
  21930. X509_STORE_CTX_free(ctx);
  21931. X509_free(x509Svr);
  21932. X509_STORE_free(str);
  21933. X509_free(x509Ca);
  21934. printf(resultFmt, passed);
  21935. #endif
  21936. }
  21937. static void test_wolfSSL_X509_STORE_CTX(void)
  21938. {
  21939. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  21940. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  21941. X509_STORE_CTX* ctx;
  21942. X509_STORE* str;
  21943. X509* x509;
  21944. #ifdef OPENSSL_ALL
  21945. X509* x5092;
  21946. STACK_OF(X509) *sk, *sk2, *sk3;
  21947. #endif
  21948. printf(testingFmt, "wolfSSL_X509_STORE_CTX()");
  21949. AssertNotNull(ctx = X509_STORE_CTX_new());
  21950. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  21951. AssertNotNull((x509 =
  21952. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  21953. AssertIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  21954. #ifdef OPENSSL_ALL
  21955. /* sk_X509_new only in OPENSSL_ALL */
  21956. sk = sk_X509_new();
  21957. AssertNotNull(sk);
  21958. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  21959. #else
  21960. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  21961. #endif
  21962. AssertIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  21963. X509_STORE_CTX_set_error(ctx, -5);
  21964. X509_STORE_CTX_set_error(NULL, -5);
  21965. X509_STORE_CTX_free(ctx);
  21966. X509_STORE_free(str);
  21967. X509_free(x509);
  21968. AssertNotNull(ctx = X509_STORE_CTX_new());
  21969. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  21970. X509_STORE_CTX_free(ctx);
  21971. #ifdef OPENSSL_ALL
  21972. /* test X509_STORE_CTX_get(1)_chain */
  21973. AssertNotNull((x509 = X509_load_certificate_file(svrCertFile,
  21974. SSL_FILETYPE_PEM)));
  21975. AssertNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  21976. SSL_FILETYPE_PEM)));
  21977. AssertNotNull((sk = sk_X509_new()));
  21978. AssertIntEQ(sk_X509_push(sk, x509), 1);
  21979. AssertNotNull((str = X509_STORE_new()));
  21980. AssertNotNull((ctx = X509_STORE_CTX_new()));
  21981. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  21982. AssertNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  21983. AssertNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  21984. AssertIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  21985. AssertNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  21986. AssertNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  21987. AssertIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  21988. X509_STORE_CTX_free(ctx);
  21989. X509_STORE_free(str);
  21990. /* CTX certs not freed yet */
  21991. X509_free(x5092);
  21992. /* sk2 freed as part of X509_STORE_CTX_free(), sk3 is dup so free here */
  21993. sk_X509_free(sk3);
  21994. #endif
  21995. /* test X509_STORE_CTX_get/set_ex_data */
  21996. {
  21997. int i = 0, tmpData = 5;
  21998. void* tmpDataRet;
  21999. AssertNotNull(ctx = X509_STORE_CTX_new());
  22000. #if defined(HAVE_EX_DATA) || defined(FORTRESS)
  22001. for (i = 0; i < MAX_EX_DATA; i++) {
  22002. AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  22003. WOLFSSL_SUCCESS);
  22004. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  22005. AssertNotNull(tmpDataRet);
  22006. AssertIntEQ(tmpData, *(int*)tmpDataRet);
  22007. }
  22008. #else
  22009. AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  22010. WOLFSSL_FAILURE);
  22011. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  22012. AssertNull(tmpDataRet);
  22013. #endif
  22014. X509_STORE_CTX_free(ctx);
  22015. }
  22016. printf(resultFmt, passed);
  22017. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  22018. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  22019. }
  22020. static void test_wolfSSL_X509_STORE_set_flags(void)
  22021. {
  22022. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  22023. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  22024. X509_STORE* store;
  22025. X509* x509;
  22026. printf(testingFmt, "wolfSSL_X509_STORE_set_flags()");
  22027. AssertNotNull((store = wolfSSL_X509_STORE_new()));
  22028. AssertNotNull((x509 =
  22029. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM)));
  22030. AssertIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  22031. #ifdef HAVE_CRL
  22032. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL), WOLFSSL_SUCCESS);
  22033. #else
  22034. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  22035. NOT_COMPILED_IN);
  22036. #endif
  22037. wolfSSL_X509_free(x509);
  22038. wolfSSL_X509_STORE_free(store);
  22039. printf(resultFmt, passed);
  22040. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  22041. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  22042. }
  22043. static void test_wolfSSL_X509_LOOKUP_load_file(void)
  22044. {
  22045. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  22046. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  22047. WOLFSSL_X509_STORE* store;
  22048. WOLFSSL_X509_LOOKUP* lookup;
  22049. printf(testingFmt, "wolfSSL_X509_LOOKUP_load_file()");
  22050. AssertNotNull(store = wolfSSL_X509_STORE_new());
  22051. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  22052. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  22053. X509_FILETYPE_PEM), 1);
  22054. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  22055. X509_FILETYPE_PEM), 1);
  22056. if (store) {
  22057. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  22058. WOLFSSL_FILETYPE_PEM), 1);
  22059. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  22060. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  22061. }
  22062. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  22063. X509_FILETYPE_PEM), 1);
  22064. if (store) {
  22065. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  22066. WOLFSSL_FILETYPE_PEM), 1);
  22067. }
  22068. wolfSSL_X509_STORE_free(store);
  22069. printf(resultFmt, passed);
  22070. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  22071. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  22072. }
  22073. static void test_wolfSSL_X509_STORE_CTX_set_time(void)
  22074. {
  22075. #if defined(OPENSSL_EXTRA)
  22076. WOLFSSL_X509_STORE_CTX* ctx;
  22077. time_t c_time;
  22078. printf(testingFmt, "wolfSSL_X509_set_time()");
  22079. AssertNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  22080. c_time = 365*24*60*60;
  22081. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  22082. AssertTrue(
  22083. (ctx->param->flags & WOLFSSL_USE_CHECK_TIME) == WOLFSSL_USE_CHECK_TIME);
  22084. AssertTrue(ctx->param->check_time == c_time);
  22085. wolfSSL_X509_STORE_CTX_free(ctx);
  22086. printf(resultFmt, passed);
  22087. #endif /* OPENSSL_EXTRA */
  22088. }
  22089. static void test_wolfSSL_get0_param(void)
  22090. {
  22091. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  22092. SSL_CTX* ctx;
  22093. SSL* ssl;
  22094. WOLFSSL_X509_VERIFY_PARAM* pParam;
  22095. printf(testingFmt, "wolfSSL_get0_param()");
  22096. #ifndef NO_WOLFSSL_SERVER
  22097. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  22098. #else
  22099. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  22100. #endif
  22101. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  22102. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  22103. AssertNotNull(ssl = SSL_new(ctx));
  22104. pParam = SSL_get0_param(ssl);
  22105. (void)pParam;
  22106. SSL_free(ssl);
  22107. SSL_CTX_free(ctx);
  22108. printf(resultFmt, passed);
  22109. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  22110. }
  22111. static void test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  22112. {
  22113. #if defined(OPENSSL_EXTRA)
  22114. const char host[] = "www.example.com";
  22115. WOLFSSL_X509_VERIFY_PARAM* pParam;
  22116. printf(testingFmt, "wolfSSL_X509_VERIFY_PARAM_set1_host()");
  22117. AssertNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  22118. sizeof(WOLFSSL_X509_VERIFY_PARAM),
  22119. HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  22120. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  22121. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  22122. AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  22123. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  22124. AssertIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  22125. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  22126. printf(resultFmt, passed);
  22127. #endif /* OPENSSL_EXTRA */
  22128. }
  22129. static void test_wolfSSL_X509_STORE_CTX_get0_store(void)
  22130. {
  22131. #if defined(OPENSSL_EXTRA)
  22132. X509_STORE* store;
  22133. X509_STORE_CTX* ctx;
  22134. X509_STORE_CTX* ctx_no_init;
  22135. printf(testingFmt, "wolfSSL_X509_STORE_CTX_get0_store()");
  22136. AssertNotNull((store = X509_STORE_new()));
  22137. AssertNotNull(ctx = X509_STORE_CTX_new());
  22138. AssertNotNull(ctx_no_init = X509_STORE_CTX_new());
  22139. AssertIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  22140. AssertNull(X509_STORE_CTX_get0_store(NULL));
  22141. /* should return NULL if ctx has not bee initialized */
  22142. AssertNull(X509_STORE_CTX_get0_store(ctx_no_init));
  22143. AssertNotNull(X509_STORE_CTX_get0_store(ctx));
  22144. wolfSSL_X509_STORE_CTX_free(ctx);
  22145. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  22146. X509_STORE_free(store);
  22147. printf(resultFmt, passed);
  22148. #endif /* OPENSSL_EXTRA */
  22149. }
  22150. static void test_wolfSSL_CTX_set_client_CA_list(void)
  22151. {
  22152. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  22153. !defined(NO_WOLFSSL_CLIENT)
  22154. WOLFSSL_CTX* ctx;
  22155. X509_NAME* name = NULL;
  22156. STACK_OF(X509_NAME)* names = NULL;
  22157. STACK_OF(X509_NAME)* ca_list = NULL;
  22158. int i, names_len;
  22159. printf(testingFmt, "wolfSSL_CTX_set_client_CA_list()");
  22160. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  22161. names = SSL_load_client_CA_file(cliCertFile);
  22162. AssertNotNull(names);
  22163. SSL_CTX_set_client_CA_list(ctx,names);
  22164. AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  22165. AssertIntGT((names_len = sk_X509_NAME_num(names)), 0);
  22166. for (i=0; i<names_len; i++) {
  22167. AssertNotNull(name = sk_X509_NAME_value(names, i));
  22168. AssertIntEQ(sk_X509_NAME_find(names, name), i);
  22169. }
  22170. wolfSSL_CTX_free(ctx);
  22171. printf(resultFmt, passed);
  22172. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  22173. }
  22174. static void test_wolfSSL_CTX_add_client_CA(void)
  22175. {
  22176. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  22177. !defined(NO_WOLFSSL_CLIENT)
  22178. WOLFSSL_CTX* ctx;
  22179. WOLFSSL_X509* x509;
  22180. WOLFSSL_X509* x509_a;
  22181. STACK_OF(X509_NAME)* ca_list;
  22182. int ret = 0;
  22183. printf(testingFmt, "wolfSSL_CTX_add_client_CA()");
  22184. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  22185. /* Add client cert */
  22186. x509 = X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
  22187. AssertNotNull(x509);
  22188. ret = SSL_CTX_add_client_CA(ctx, x509);
  22189. AssertIntEQ(ret, SSL_SUCCESS);
  22190. AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  22191. /* Add another client cert */
  22192. AssertNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  22193. SSL_FILETYPE_PEM));
  22194. AssertIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  22195. X509_free(x509);
  22196. X509_free(x509_a);
  22197. SSL_CTX_free(ctx);
  22198. printf(resultFmt, passed);
  22199. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  22200. }
  22201. static void test_wolfSSL_X509_NID(void)
  22202. {
  22203. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  22204. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  22205. int sigType;
  22206. int nameSz;
  22207. X509* cert;
  22208. EVP_PKEY* pubKeyTmp;
  22209. X509_NAME* name;
  22210. char commonName[80];
  22211. char countryName[80];
  22212. char localityName[80];
  22213. char stateName[80];
  22214. char orgName[80];
  22215. char orgUnit[80];
  22216. printf(testingFmt, "wolfSSL_X509_NID()");
  22217. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  22218. /* convert cert from DER to internal WOLFSSL_X509 struct */
  22219. AssertNotNull(cert = wolfSSL_X509_d2i(&cert, client_cert_der_2048,
  22220. sizeof_client_cert_der_2048));
  22221. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  22222. /* extract PUBLIC KEY from cert */
  22223. AssertNotNull(pubKeyTmp = X509_get_pubkey(cert));
  22224. /* extract signatureType */
  22225. AssertIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  22226. /* extract subjectName info */
  22227. AssertNotNull(name = X509_get_subject_name(cert));
  22228. AssertIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  22229. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  22230. NULL, 0)), 0);
  22231. AssertIntEQ(nameSz, 15);
  22232. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  22233. commonName, sizeof(commonName))), 0);
  22234. AssertIntEQ(nameSz, 15);
  22235. AssertIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  22236. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  22237. commonName, 9)), 0);
  22238. AssertIntEQ(nameSz, 8);
  22239. AssertIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  22240. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  22241. countryName, sizeof(countryName))), 0);
  22242. AssertIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  22243. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  22244. localityName, sizeof(localityName))), 0);
  22245. AssertIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  22246. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_stateOrProvinceName,
  22247. stateName, sizeof(stateName))), 0);
  22248. AssertIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  22249. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  22250. orgName, sizeof(orgName))), 0);
  22251. AssertIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  22252. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationalUnitName,
  22253. orgUnit, sizeof(orgUnit))), 0);
  22254. AssertIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  22255. EVP_PKEY_free(pubKeyTmp);
  22256. X509_free(cert);
  22257. printf(resultFmt, passed);
  22258. #endif
  22259. }
  22260. static void test_wolfSSL_CTX_set_srp_username(void)
  22261. {
  22262. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  22263. && !defined(NO_SHA256) && !defined(WC_NO_RNG)
  22264. WOLFSSL_CTX* ctx;
  22265. const char *username = "TESTUSER";
  22266. const char *password = "TESTPASSWORD";
  22267. int r;
  22268. printf(testingFmt, "wolfSSL_CTX_set_srp_username()");
  22269. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  22270. AssertNotNull(ctx);
  22271. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  22272. AssertIntEQ(r,SSL_SUCCESS);
  22273. wolfSSL_CTX_free(ctx);
  22274. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  22275. AssertNotNull(ctx);
  22276. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  22277. AssertIntEQ(r,SSL_SUCCESS);
  22278. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  22279. AssertIntEQ(r,SSL_SUCCESS);
  22280. wolfSSL_CTX_free(ctx);
  22281. printf(resultFmt, passed);
  22282. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  22283. /* && !NO_SHA256 && !WC_NO_RNG */
  22284. }
  22285. static void test_wolfSSL_CTX_set_srp_password(void)
  22286. {
  22287. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  22288. && !defined(NO_SHA256) && !defined(WC_NO_RNG)
  22289. WOLFSSL_CTX* ctx;
  22290. const char *username = "TESTUSER";
  22291. const char *password = "TESTPASSWORD";
  22292. int r;
  22293. printf(testingFmt, "wolfSSL_CTX_set_srp_password()");
  22294. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  22295. AssertNotNull(ctx);
  22296. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  22297. AssertIntEQ(r,SSL_SUCCESS);
  22298. wolfSSL_CTX_free(ctx);
  22299. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  22300. AssertNotNull(ctx);
  22301. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  22302. AssertIntEQ(r,SSL_SUCCESS);
  22303. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  22304. AssertIntEQ(r,SSL_SUCCESS);
  22305. wolfSSL_CTX_free(ctx);
  22306. printf(resultFmt, passed);
  22307. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  22308. /* && !NO_SHA256 && !WC_NO_RNG */
  22309. }
  22310. static void test_wolfSSL_X509_STORE(void)
  22311. {
  22312. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  22313. X509_STORE *store;
  22314. #ifdef HAVE_CRL
  22315. X509_STORE_CTX *storeCtx;
  22316. X509_CRL *crl;
  22317. X509 *ca, *cert;
  22318. const char crlPem[] = "./certs/crl/crl.revoked";
  22319. const char srvCert[] = "./certs/server-revoked-cert.pem";
  22320. const char caCert[] = "./certs/ca-cert.pem";
  22321. XFILE fp;
  22322. printf(testingFmt, "test_wolfSSL_X509_STORE");
  22323. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  22324. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  22325. SSL_FILETYPE_PEM)));
  22326. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  22327. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  22328. SSL_FILETYPE_PEM)));
  22329. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  22330. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  22331. AssertIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  22332. X509_STORE_free(store);
  22333. X509_STORE_CTX_free(storeCtx);
  22334. X509_free(cert);
  22335. X509_free(ca);
  22336. /* should fail to verify now after adding in CRL */
  22337. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  22338. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  22339. SSL_FILETYPE_PEM)));
  22340. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  22341. fp = XFOPEN(crlPem, "rb");
  22342. AssertTrue((fp != XBADFILE));
  22343. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  22344. NULL, NULL));
  22345. XFCLOSE(fp);
  22346. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  22347. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  22348. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  22349. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  22350. SSL_FILETYPE_PEM)));
  22351. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  22352. AssertIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  22353. AssertIntEQ(X509_STORE_CTX_get_error(storeCtx), CRL_CERT_REVOKED);
  22354. X509_CRL_free(crl);
  22355. X509_STORE_free(store);
  22356. X509_STORE_CTX_free(storeCtx);
  22357. X509_free(cert);
  22358. X509_free(ca);
  22359. #endif /* HAVE_CRL */
  22360. #ifndef WOLFCRYPT_ONLY
  22361. {
  22362. SSL_CTX* ctx;
  22363. #ifndef NO_WOLFSSL_SERVER
  22364. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  22365. #else
  22366. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  22367. #endif
  22368. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  22369. SSL_CTX_set_cert_store(ctx, store);
  22370. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  22371. SSL_CTX_set_cert_store(ctx, store);
  22372. SSL_CTX_free(ctx);
  22373. }
  22374. #endif
  22375. printf(resultFmt, passed);
  22376. #endif
  22377. return;
  22378. }
  22379. static void test_wolfSSL_X509_STORE_load_locations(void)
  22380. {
  22381. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_FILESYSTEM)
  22382. SSL_CTX *ctx;
  22383. X509_STORE *store;
  22384. const char ca_file[] = "./certs/ca-cert.pem";
  22385. const char client_pem_file[] = "./certs/client-cert.pem";
  22386. const char client_der_file[] = "./certs/client-cert.der";
  22387. const char ecc_file[] = "./certs/ecc-key.pem";
  22388. const char certs_path[] = "./certs/";
  22389. const char bad_path[] = "./bad-path/";
  22390. #ifdef HAVE_CRL
  22391. const char crl_path[] = "./certs/crl/";
  22392. const char crl_file[] = "./certs/crl/crl.pem";
  22393. #endif
  22394. printf(testingFmt, "wolfSSL_X509_STORE_load_locations");
  22395. #ifndef NO_WOLFSSL_SERVER
  22396. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  22397. #else
  22398. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  22399. #endif
  22400. AssertNotNull(store = SSL_CTX_get_cert_store(ctx));
  22401. AssertIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL), WOLFSSL_SUCCESS);
  22402. /* Test bad arguments */
  22403. AssertIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL), WOLFSSL_FAILURE);
  22404. AssertIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  22405. AssertIntEQ(X509_STORE_load_locations(store, client_der_file, NULL), WOLFSSL_FAILURE);
  22406. AssertIntEQ(X509_STORE_load_locations(store, ecc_file, NULL), WOLFSSL_FAILURE);
  22407. AssertIntEQ(X509_STORE_load_locations(store, NULL, bad_path), WOLFSSL_FAILURE);
  22408. #ifdef HAVE_CRL
  22409. /* Test with CRL */
  22410. AssertIntEQ(X509_STORE_load_locations(store, crl_file, NULL), WOLFSSL_SUCCESS);
  22411. AssertIntEQ(X509_STORE_load_locations(store, NULL, crl_path), WOLFSSL_SUCCESS);
  22412. #endif
  22413. /* Test with CA */
  22414. AssertIntEQ(X509_STORE_load_locations(store, ca_file, NULL), WOLFSSL_SUCCESS);
  22415. /* Test with client_cert and certs path */
  22416. AssertIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL), WOLFSSL_SUCCESS);
  22417. AssertIntEQ(X509_STORE_load_locations(store, NULL, certs_path), WOLFSSL_SUCCESS);
  22418. SSL_CTX_free(ctx);
  22419. printf(resultFmt, passed);
  22420. #endif
  22421. }
  22422. static void test_wolfSSL_BN(void)
  22423. {
  22424. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  22425. BIGNUM* a;
  22426. BIGNUM* b;
  22427. BIGNUM* c;
  22428. BIGNUM* d;
  22429. ASN1_INTEGER* ai;
  22430. unsigned char value[1];
  22431. printf(testingFmt, "wolfSSL_BN()");
  22432. AssertNotNull(b = BN_new());
  22433. AssertNotNull(c = BN_new());
  22434. AssertNotNull(d = BN_new());
  22435. value[0] = 0x03;
  22436. ai = ASN1_INTEGER_new();
  22437. AssertNotNull(ai);
  22438. /* at the moment hard setting since no set function */
  22439. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  22440. ai->data[1] = 0x01; /* length of integer */
  22441. ai->data[2] = value[0];
  22442. AssertNotNull(a = ASN1_INTEGER_to_BN(ai, NULL));
  22443. ASN1_INTEGER_free(ai);
  22444. value[0] = 0x02;
  22445. AssertNotNull(BN_bin2bn(value, sizeof(value), b));
  22446. value[0] = 0x05;
  22447. AssertNotNull(BN_bin2bn(value, sizeof(value), c));
  22448. /* a^b mod c = */
  22449. AssertIntEQ(BN_mod_exp(d, NULL, b, c, NULL), WOLFSSL_FAILURE);
  22450. AssertIntEQ(BN_mod_exp(d, a, b, c, NULL), WOLFSSL_SUCCESS);
  22451. /* check result 3^2 mod 5 */
  22452. value[0] = 0;
  22453. AssertIntEQ(BN_bn2bin(d, value), sizeof(value));
  22454. AssertIntEQ((int)(value[0]), 4);
  22455. /* a*b mod c = */
  22456. AssertIntEQ(BN_mod_mul(d, NULL, b, c, NULL), SSL_FAILURE);
  22457. AssertIntEQ(BN_mod_mul(d, a, b, c, NULL), SSL_SUCCESS);
  22458. /* check result 3*2 mod 5 */
  22459. value[0] = 0;
  22460. AssertIntEQ(BN_bn2bin(d, value), sizeof(value));
  22461. AssertIntEQ((int)(value[0]), 1);
  22462. /* BN_mod_inverse test */
  22463. value[0] = 0;
  22464. BIGNUM *r = BN_new();
  22465. BIGNUM *val = BN_mod_inverse(r,b,c,NULL);
  22466. AssertIntEQ(BN_bn2bin(r, value), 1);
  22467. AssertIntEQ((int)(value[0] & 0x03), 3);
  22468. BN_free(val);
  22469. AssertIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  22470. AssertIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  22471. AssertIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  22472. AssertIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  22473. AssertIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  22474. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  22475. {
  22476. char* ret;
  22477. AssertNotNull(ret = BN_bn2dec(c));
  22478. AssertIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  22479. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  22480. }
  22481. #endif
  22482. AssertIntEQ(BN_get_word(c), 4);
  22483. BN_free(a);
  22484. BN_free(b);
  22485. BN_free(c);
  22486. BN_clear_free(d);
  22487. /* check that converting NULL and the null string returns an error */
  22488. a = NULL;
  22489. AssertIntLE(BN_hex2bn(&a, NULL), 0);
  22490. AssertIntLE(BN_hex2bn(&a, ""), 0);
  22491. AssertNull(a);
  22492. /* check that getting a string and a bin of the same number are equal,
  22493. * and that the comparison works EQ, LT and GT */
  22494. AssertIntGT(BN_hex2bn(&a, "03"), 0);
  22495. value[0] = 0x03;
  22496. AssertNotNull(b = BN_new());
  22497. AssertNotNull(BN_bin2bn(value, sizeof(value), b));
  22498. value[0] = 0x04;
  22499. AssertNotNull(c = BN_new());
  22500. AssertNotNull(BN_bin2bn(value, sizeof(value), c));
  22501. AssertIntEQ(BN_cmp(a, b), 0);
  22502. AssertIntLT(BN_cmp(a, c), 0);
  22503. AssertIntGT(BN_cmp(c, b), 0);
  22504. AssertIntEQ(BN_set_word(a, 0), 1);
  22505. AssertIntEQ(BN_is_zero(a), 1);
  22506. AssertIntEQ(BN_set_bit(a, 0x45), 1);
  22507. AssertIntEQ(BN_is_zero(a), 0);
  22508. AssertIntEQ(BN_is_bit_set(a, 0x45), 1);
  22509. AssertIntEQ(BN_clear_bit(a, 0x45), 1);
  22510. AssertIntEQ(BN_is_bit_set(a, 0x45), 0);
  22511. AssertIntEQ(BN_is_zero(a), 1);
  22512. BN_free(a);
  22513. BN_free(b);
  22514. BN_free(c);
  22515. #if defined(USE_FAST_MATH) && !defined(HAVE_WOLF_BIGINT)
  22516. {
  22517. BIGNUM *ap;
  22518. BIGNUM bv;
  22519. BIGNUM cv;
  22520. BIGNUM dv;
  22521. AssertNotNull(ap = BN_new());
  22522. BN_init(&bv);
  22523. BN_init(&cv);
  22524. BN_init(&dv);
  22525. value[0] = 0x3;
  22526. AssertNotNull(BN_bin2bn(value, sizeof(value), ap));
  22527. value[0] = 0x02;
  22528. AssertNotNull(BN_bin2bn(value, sizeof(value), &bv));
  22529. value[0] = 0x05;
  22530. AssertNotNull(BN_bin2bn(value, sizeof(value), &cv));
  22531. /* a^b mod c = */
  22532. AssertIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  22533. AssertIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  22534. /* check result 3^2 mod 5 */
  22535. value[0] = 0;
  22536. AssertIntEQ(BN_bn2bin(&dv, value), sizeof(value));
  22537. AssertIntEQ((int)(value[0]), 4);
  22538. /* a*b mod c = */
  22539. AssertIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  22540. AssertIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  22541. /* check result 3*2 mod 5 */
  22542. value[0] = 0;
  22543. AssertIntEQ(BN_bn2bin(&dv, value), sizeof(value));
  22544. AssertIntEQ((int)(value[0]), 1);
  22545. BN_free(ap);
  22546. }
  22547. #endif
  22548. printf(resultFmt, passed);
  22549. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  22550. }
  22551. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  22552. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  22553. #define TEST_ARG 0x1234
  22554. static void msg_cb(int write_p, int version, int content_type,
  22555. const void *buf, size_t len, SSL *ssl, void *arg)
  22556. {
  22557. (void)write_p;
  22558. (void)version;
  22559. (void)content_type;
  22560. (void)buf;
  22561. (void)len;
  22562. (void)ssl;
  22563. AssertTrue(arg == (void*)TEST_ARG);
  22564. }
  22565. #endif
  22566. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  22567. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  22568. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
  22569. !defined(NO_WOLFSSL_SERVER)
  22570. #ifndef SINGLE_THREADED
  22571. #if defined(SESSION_CERTS)
  22572. #include "wolfssl/internal.h"
  22573. #endif
  22574. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  22575. {
  22576. (void) ctx;
  22577. (void) ssl;
  22578. #ifdef WOLFSSL_QT
  22579. STACK_OF(X509)* sk;
  22580. X509* x509;
  22581. int i, num;
  22582. BIO* bio;
  22583. #endif
  22584. printf("\n===== msgcb called ====\n");
  22585. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  22586. AssertTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  22587. AssertIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 1);
  22588. #endif
  22589. #ifdef WOLFSSL_QT
  22590. bio = BIO_new(BIO_s_file());
  22591. BIO_set_fp(bio, stdout, BIO_NOCLOSE);
  22592. sk = SSL_get_peer_cert_chain(ssl);
  22593. AssertNotNull(sk);
  22594. if (!sk) {
  22595. BIO_free(bio);
  22596. return SSL_FAILURE;
  22597. }
  22598. num = sk_X509_num(sk);
  22599. AssertTrue(num > 0);
  22600. for (i = 0; i < num; i++) {
  22601. x509 = sk_X509_value(sk,i);
  22602. AssertNotNull(x509);
  22603. if (!x509)
  22604. break;
  22605. printf("Certificate at index [%d] = :\n",i);
  22606. X509_print(bio,x509);
  22607. printf("\n\n");
  22608. }
  22609. BIO_free(bio);
  22610. #endif
  22611. return SSL_SUCCESS;
  22612. }
  22613. #endif
  22614. #endif
  22615. static void test_wolfSSL_msgCb(void)
  22616. {
  22617. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  22618. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  22619. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
  22620. !defined(NO_WOLFSSL_SERVER)
  22621. tcp_ready ready;
  22622. func_args client_args;
  22623. func_args server_args;
  22624. #ifndef SINGLE_THREADED
  22625. THREAD_TYPE serverThread;
  22626. #endif
  22627. callback_functions client_cb;
  22628. callback_functions server_cb;
  22629. printf(testingFmt, "test_wolfSSL_msgCb");
  22630. /* create a failed connection and inspect the error */
  22631. #ifdef WOLFSSL_TIRTOS
  22632. fdOpenSession(Task_self());
  22633. #endif
  22634. XMEMSET(&client_args, 0, sizeof(func_args));
  22635. XMEMSET(&server_args, 0, sizeof(func_args));
  22636. StartTCP();
  22637. InitTcpReady(&ready);
  22638. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  22639. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  22640. #ifndef WOLFSSL_NO_TLS12
  22641. client_cb.method = wolfTLSv1_2_client_method;
  22642. server_cb.method = wolfTLSv1_2_server_method;
  22643. #else
  22644. client_cb.method = wolfTLSv1_3_client_method;
  22645. server_cb.method = wolfTLSv1_3_server_method;
  22646. #endif
  22647. server_args.signal = &ready;
  22648. server_args.callbacks = &server_cb;
  22649. client_args.signal = &ready;
  22650. client_args.callbacks = &client_cb;
  22651. client_args.return_code = TEST_FAIL;
  22652. #ifndef SINGLE_THREADED
  22653. start_thread(test_server_nofail, &server_args, &serverThread);
  22654. wait_tcp_ready(&server_args);
  22655. test_client_nofail(&client_args, (void *)msgCb);
  22656. join_thread(serverThread);
  22657. #endif
  22658. FreeTcpReady(&ready);
  22659. #ifndef SINGLE_THREADED
  22660. AssertTrue(client_args.return_code);
  22661. AssertTrue(server_args.return_code);
  22662. #endif
  22663. #ifdef WOLFSSL_TIRTOS
  22664. fdOpenSession(Task_self());
  22665. #endif
  22666. printf(resultFmt, passed);
  22667. #endif
  22668. }
  22669. static void test_wolfSSL_either_side(void)
  22670. {
  22671. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  22672. !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  22673. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  22674. tcp_ready ready;
  22675. func_args client_args;
  22676. func_args server_args;
  22677. #ifndef SINGLE_THREADED
  22678. THREAD_TYPE serverThread;
  22679. #endif
  22680. callback_functions client_cb;
  22681. callback_functions server_cb;
  22682. printf(testingFmt, "test_wolfSSL_either_side");
  22683. /* create a failed connection and inspect the error */
  22684. #ifdef WOLFSSL_TIRTOS
  22685. fdOpenSession(Task_self());
  22686. #endif
  22687. XMEMSET(&client_args, 0, sizeof(func_args));
  22688. XMEMSET(&server_args, 0, sizeof(func_args));
  22689. StartTCP();
  22690. InitTcpReady(&ready);
  22691. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  22692. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  22693. /* Use same CTX for both client and server */
  22694. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  22695. AssertNotNull(client_cb.ctx);
  22696. server_cb.ctx = client_cb.ctx;
  22697. /* we are responsible for free'ing WOLFSSL_CTX */
  22698. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  22699. server_args.signal = &ready;
  22700. server_args.callbacks = &server_cb;
  22701. client_args.signal = &ready;
  22702. client_args.callbacks = &client_cb;
  22703. client_args.return_code = TEST_FAIL;
  22704. #ifndef SINGLE_THREADED
  22705. start_thread(test_server_nofail, &server_args, &serverThread);
  22706. wait_tcp_ready(&server_args);
  22707. test_client_nofail(&client_args, NULL);
  22708. join_thread(serverThread);
  22709. #endif
  22710. wolfSSL_CTX_free(client_cb.ctx);
  22711. FreeTcpReady(&ready);
  22712. #ifndef SINGLE_THREADED
  22713. AssertTrue(client_args.return_code);
  22714. AssertTrue(server_args.return_code);
  22715. #endif
  22716. #ifdef WOLFSSL_TIRTOS
  22717. fdOpenSession(Task_self());
  22718. #endif
  22719. printf(resultFmt, passed);
  22720. #endif
  22721. }
  22722. static void test_wolfSSL_DTLS_either_side(void)
  22723. {
  22724. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  22725. !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  22726. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  22727. defined(WOLFSSL_DTLS)
  22728. tcp_ready ready;
  22729. func_args client_args;
  22730. func_args server_args;
  22731. #ifndef SINGLE_THREADED
  22732. THREAD_TYPE serverThread;
  22733. #endif
  22734. callback_functions client_cb;
  22735. callback_functions server_cb;
  22736. printf(testingFmt, "test_wolfSSL_DTLS_either_side");
  22737. /* create a failed connection and inspect the error */
  22738. #ifdef WOLFSSL_TIRTOS
  22739. fdOpenSession(Task_self());
  22740. #endif
  22741. XMEMSET(&client_args, 0, sizeof(func_args));
  22742. XMEMSET(&server_args, 0, sizeof(func_args));
  22743. StartTCP();
  22744. InitTcpReady(&ready);
  22745. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  22746. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  22747. /* Use same CTX for both client and server */
  22748. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  22749. AssertNotNull(client_cb.ctx);
  22750. server_cb.ctx = client_cb.ctx;
  22751. /* we are responsible for free'ing WOLFSSL_CTX */
  22752. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  22753. server_args.signal = &ready;
  22754. server_args.callbacks = &server_cb;
  22755. client_args.signal = &ready;
  22756. client_args.callbacks = &client_cb;
  22757. client_args.return_code = TEST_FAIL;
  22758. #ifndef SINGLE_THREADED
  22759. start_thread(test_server_nofail, &server_args, &serverThread);
  22760. wait_tcp_ready(&server_args);
  22761. test_client_nofail(&client_args, NULL);
  22762. join_thread(serverThread);
  22763. #endif
  22764. wolfSSL_CTX_free(client_cb.ctx);
  22765. FreeTcpReady(&ready);
  22766. #ifndef SINGLE_THREADED
  22767. AssertTrue(client_args.return_code);
  22768. AssertTrue(server_args.return_code);
  22769. #endif
  22770. #ifdef WOLFSSL_TIRTOS
  22771. fdOpenSession(Task_self());
  22772. #endif
  22773. printf(resultFmt, passed);
  22774. #endif
  22775. }
  22776. static void test_generate_cookie(void)
  22777. {
  22778. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA)
  22779. SSL_CTX* ctx;
  22780. SSL* ssl;
  22781. byte buf[FOURK_BUF] = {0};
  22782. printf(testingFmt, "test_generate_cookie");
  22783. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  22784. AssertNotNull(ssl = SSL_new(ctx));
  22785. /* Test unconnected */
  22786. AssertIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  22787. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  22788. wolfSSL_SetCookieCtx(ssl, ctx);
  22789. AssertNotNull(wolfSSL_GetCookieCtx(ssl));
  22790. AssertNull(wolfSSL_GetCookieCtx(NULL));
  22791. SSL_free(ssl);
  22792. SSL_CTX_free(ctx);
  22793. printf(resultFmt, passed);
  22794. #endif
  22795. }
  22796. static void test_wolfSSL_set_options(void)
  22797. {
  22798. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  22799. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  22800. SSL* ssl;
  22801. SSL_CTX* ctx;
  22802. char appData[] = "extra msg";
  22803. unsigned char protos[] = {
  22804. 7, 't', 'l', 's', '/', '1', '.', '2',
  22805. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  22806. };
  22807. unsigned int len = sizeof(protos);
  22808. void *arg = (void *)TEST_ARG;
  22809. printf(testingFmt, "wolfSSL_set_options()");
  22810. #ifndef NO_WOLFSSL_SERVER
  22811. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  22812. #else
  22813. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  22814. #endif
  22815. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  22816. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  22817. AssertTrue(SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1) == SSL_OP_NO_TLSv1);
  22818. AssertTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  22819. AssertIntGT((int)SSL_CTX_set_options(ctx, (SSL_OP_COOKIE_EXCHANGE |
  22820. SSL_OP_NO_SSLv2)), 0);
  22821. AssertTrue((SSL_CTX_set_options(ctx, SSL_OP_COOKIE_EXCHANGE) &
  22822. SSL_OP_COOKIE_EXCHANGE) == SSL_OP_COOKIE_EXCHANGE);
  22823. AssertTrue((SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_2) &
  22824. SSL_OP_NO_TLSv1_2) == SSL_OP_NO_TLSv1_2);
  22825. AssertTrue((SSL_CTX_set_options(ctx, SSL_OP_NO_COMPRESSION) &
  22826. SSL_OP_NO_COMPRESSION) == SSL_OP_NO_COMPRESSION);
  22827. AssertNull((SSL_CTX_clear_options(ctx, SSL_OP_NO_COMPRESSION) &
  22828. SSL_OP_NO_COMPRESSION));
  22829. SSL_CTX_free(ctx);
  22830. #ifndef NO_WOLFSSL_SERVER
  22831. ctx = SSL_CTX_new(wolfSSLv23_server_method());
  22832. AssertNotNull(ctx);
  22833. #else
  22834. ctx = SSL_CTX_new(wolfSSLv23_client_method());
  22835. AssertNotNull(ctx);
  22836. #endif
  22837. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  22838. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  22839. AssertNotNull(ssl = SSL_new(ctx));
  22840. #if defined(HAVE_EX_DATA) || defined(FORTRESS)
  22841. AssertIntEQ(SSL_set_app_data(ssl, (void*)appData), SSL_SUCCESS);
  22842. AssertNotNull(SSL_get_app_data((const WOLFSSL*)ssl));
  22843. if (ssl) {
  22844. AssertIntEQ(XMEMCMP(SSL_get_app_data((const WOLFSSL*)ssl),
  22845. appData, sizeof(appData)), 0);
  22846. }
  22847. #else
  22848. AssertIntEQ(SSL_set_app_data(ssl, (void*)appData), SSL_FAILURE);
  22849. AssertNull(SSL_get_app_data((const WOLFSSL*)ssl));
  22850. #endif
  22851. AssertTrue(SSL_set_options(ssl, SSL_OP_NO_TLSv1) == SSL_OP_NO_TLSv1);
  22852. AssertTrue(SSL_get_options(ssl) == SSL_OP_NO_TLSv1);
  22853. AssertIntGT((int)SSL_set_options(ssl, (SSL_OP_COOKIE_EXCHANGE |
  22854. WOLFSSL_OP_NO_SSLv2)), 0);
  22855. AssertTrue((SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE) &
  22856. SSL_OP_COOKIE_EXCHANGE) == SSL_OP_COOKIE_EXCHANGE);
  22857. AssertTrue((SSL_set_options(ssl, SSL_OP_NO_TLSv1_2) &
  22858. SSL_OP_NO_TLSv1_2) == SSL_OP_NO_TLSv1_2);
  22859. AssertTrue((SSL_set_options(ssl, SSL_OP_NO_COMPRESSION) &
  22860. SSL_OP_NO_COMPRESSION) == SSL_OP_NO_COMPRESSION);
  22861. AssertNull((SSL_clear_options(ssl, SSL_OP_NO_COMPRESSION) &
  22862. SSL_OP_NO_COMPRESSION));
  22863. AssertTrue(SSL_set_msg_callback(ssl, msg_cb) == SSL_SUCCESS);
  22864. SSL_set_msg_callback_arg(ssl, arg);
  22865. AssertTrue(SSL_CTX_set_alpn_protos(ctx, protos, len) == SSL_SUCCESS);
  22866. SSL_free(ssl);
  22867. SSL_CTX_free(ctx);
  22868. printf(resultFmt, passed);
  22869. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  22870. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  22871. }
  22872. static void test_wolfSSL_sk_SSL_CIPHER(void)
  22873. {
  22874. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  22875. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  22876. SSL* ssl;
  22877. SSL_CTX* ctx;
  22878. STACK_OF(SSL_CIPHER) *sk, *dup;
  22879. printf(testingFmt, "wolfSSL_sk_SSL_CIPHER_*()");
  22880. #ifndef NO_WOLFSSL_SERVER
  22881. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  22882. #else
  22883. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  22884. #endif
  22885. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  22886. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  22887. AssertNotNull(ssl = SSL_new(ctx));
  22888. AssertNotNull(sk = SSL_get_ciphers(ssl));
  22889. AssertNotNull(dup = sk_SSL_CIPHER_dup(sk));
  22890. AssertIntGT(sk_SSL_CIPHER_num(sk), 0);
  22891. AssertIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dup));
  22892. /* error case because connection has not been established yet */
  22893. AssertIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  22894. sk_SSL_CIPHER_free(dup);
  22895. /* sk is pointer to internal struct that should be free'd in SSL_free */
  22896. SSL_free(ssl);
  22897. SSL_CTX_free(ctx);
  22898. printf(resultFmt, passed);
  22899. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  22900. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  22901. }
  22902. /* Testing wolfSSL_set_tlsext_status_type function.
  22903. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  22904. */
  22905. static void test_wolfSSL_set_tlsext_status_type(void){
  22906. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  22907. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  22908. SSL* ssl;
  22909. SSL_CTX* ctx;
  22910. printf(testingFmt, "wolfSSL_set_tlsext_status_type()");
  22911. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  22912. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  22913. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  22914. AssertNotNull(ssl = SSL_new(ctx));
  22915. AssertTrue(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp)
  22916. == SSL_SUCCESS);
  22917. SSL_free(ssl);
  22918. SSL_CTX_free(ctx);
  22919. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  22920. }
  22921. static void test_wolfSSL_PEM_read_bio(void)
  22922. {
  22923. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  22924. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  22925. byte buff[6000];
  22926. XFILE f;
  22927. int bytes;
  22928. X509* x509;
  22929. BIO* bio = NULL;
  22930. BUF_MEM* buf;
  22931. printf(testingFmt, "wolfSSL_PEM_read_bio()");
  22932. f = XFOPEN(cliCertFile, "rb");
  22933. AssertTrue((f != XBADFILE));
  22934. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  22935. XFCLOSE(f);
  22936. AssertNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  22937. AssertNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  22938. AssertNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  22939. AssertIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  22940. AssertIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  22941. AssertIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  22942. AssertIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  22943. BIO_free(bio);
  22944. BUF_MEM_free(buf);
  22945. X509_free(x509);
  22946. printf(resultFmt, passed);
  22947. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  22948. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  22949. }
  22950. #if defined(OPENSSL_EXTRA)
  22951. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  22952. long argl, long ret)
  22953. {
  22954. (void)bio;
  22955. (void)cmd;
  22956. (void)argp;
  22957. (void)argi;
  22958. (void)argl;
  22959. return ret;
  22960. }
  22961. #endif
  22962. static void test_wolfSSL_BIO(void)
  22963. {
  22964. #if defined(OPENSSL_EXTRA)
  22965. const unsigned char* p;
  22966. byte buff[20];
  22967. BIO* bio1;
  22968. BIO* bio2;
  22969. BIO* bio3;
  22970. char* bufPt;
  22971. int i;
  22972. printf(testingFmt, "wolfSSL_BIO()");
  22973. for (i = 0; i < 20; i++) {
  22974. buff[i] = i;
  22975. }
  22976. /* Creating and testing type BIO_s_bio */
  22977. AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
  22978. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  22979. AssertNotNull(bio3 = BIO_new(BIO_s_bio()));
  22980. /* read/write before set up */
  22981. AssertIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  22982. AssertIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  22983. AssertIntEQ(BIO_set_nbio(bio1, 1), 1);
  22984. AssertIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  22985. AssertIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  22986. AssertIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  22987. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  22988. XMEMCPY(bufPt, buff, 10);
  22989. AssertIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  22990. /* write buffer full */
  22991. AssertIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  22992. AssertIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  22993. AssertIntEQ((int)BIO_ctrl_pending(bio1), 0);
  22994. /* write the other direction with pair */
  22995. AssertIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  22996. XMEMCPY(bufPt, buff, 8);
  22997. AssertIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  22998. /* try read */
  22999. AssertIntEQ((int)BIO_ctrl_pending(bio1), 8);
  23000. AssertIntEQ((int)BIO_ctrl_pending(bio2), 20);
  23001. /* try read using ctrl function */
  23002. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  23003. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  23004. AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  23005. AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  23006. AssertIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  23007. for (i = 0; i < 20; i++) {
  23008. AssertIntEQ((int)bufPt[i], i);
  23009. }
  23010. AssertIntEQ(BIO_nread(bio2, &bufPt, 1), WOLFSSL_BIO_ERROR);
  23011. AssertIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  23012. for (i = 0; i < 8; i++) {
  23013. AssertIntEQ((int)bufPt[i], i);
  23014. }
  23015. AssertIntEQ(BIO_nread(bio1, &bufPt, 1), WOLFSSL_BIO_ERROR);
  23016. AssertIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  23017. /* new pair */
  23018. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  23019. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  23020. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  23021. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  23022. AssertIntEQ(BIO_nread(bio3, &bufPt, 10), WOLFSSL_BIO_ERROR);
  23023. /* test wrap around... */
  23024. AssertIntEQ(BIO_reset(bio1), 0);
  23025. AssertIntEQ(BIO_reset(bio3), 0);
  23026. /* fill write buffer, read only small amount then write again */
  23027. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  23028. XMEMCPY(bufPt, buff, 20);
  23029. AssertIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  23030. for (i = 0; i < 4; i++) {
  23031. AssertIntEQ(bufPt[i], i);
  23032. }
  23033. /* try writing over read index */
  23034. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  23035. XMEMSET(bufPt, 0, 4);
  23036. AssertIntEQ((int)BIO_ctrl_pending(bio3), 20);
  23037. /* read and write 0 bytes */
  23038. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  23039. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  23040. /* should read only to end of write buffer then need to read again */
  23041. AssertIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  23042. for (i = 0; i < 16; i++) {
  23043. AssertIntEQ(bufPt[i], buff[4 + i]);
  23044. }
  23045. AssertIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  23046. AssertIntEQ(BIO_nread0(bio3, &bufPt), 4);
  23047. for (i = 0; i < 4; i++) {
  23048. AssertIntEQ(bufPt[i], 0);
  23049. }
  23050. /* read index should not have advanced with nread0 */
  23051. AssertIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  23052. for (i = 0; i < 4; i++) {
  23053. AssertIntEQ(bufPt[i], 0);
  23054. }
  23055. /* write and fill up buffer checking reset of index state */
  23056. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  23057. XMEMCPY(bufPt, buff, 20);
  23058. /* test reset on data in bio1 write buffer */
  23059. AssertIntEQ(BIO_reset(bio1), 0);
  23060. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  23061. AssertIntEQ(BIO_nread(bio3, &bufPt, 3), WOLFSSL_BIO_ERROR);
  23062. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  23063. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  23064. AssertNotNull(p);
  23065. XMEMCPY(bufPt, buff, 20);
  23066. AssertIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  23067. for (i = 0; i < 6; i++) {
  23068. AssertIntEQ(bufPt[i], i);
  23069. }
  23070. /* test case of writing twice with offset read index */
  23071. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  23072. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  23073. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  23074. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  23075. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  23076. AssertIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  23077. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  23078. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  23079. BIO_free(bio1);
  23080. BIO_free(bio3);
  23081. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  23082. {
  23083. BIO* bioA = NULL;
  23084. BIO* bioB = NULL;
  23085. AssertIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  23086. AssertIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  23087. BIO_free(bioA);
  23088. bioA = NULL;
  23089. BIO_free(bioB);
  23090. bioB = NULL;
  23091. }
  23092. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  23093. /* BIOs with file pointers */
  23094. #if !defined(NO_FILESYSTEM)
  23095. {
  23096. XFILE f1;
  23097. XFILE f2;
  23098. BIO* f_bio1;
  23099. BIO* f_bio2;
  23100. unsigned char cert[300];
  23101. char testFile[] = "tests/bio_write_test.txt";
  23102. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  23103. AssertNotNull(f_bio1 = BIO_new(BIO_s_file()));
  23104. AssertNotNull(f_bio2 = BIO_new(BIO_s_file()));
  23105. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  23106. AssertIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  23107. f1 = XFOPEN(svrCertFile, "rwb");
  23108. AssertTrue((f1 != XBADFILE));
  23109. AssertIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  23110. AssertIntEQ(BIO_write_filename(f_bio2, testFile),
  23111. WOLFSSL_SUCCESS);
  23112. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  23113. AssertIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  23114. AssertIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  23115. AssertIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  23116. AssertIntEQ(BIO_reset(f_bio2), 0);
  23117. AssertIntEQ(BIO_seek(f_bio2, 4), 0);
  23118. BIO_free(f_bio1);
  23119. BIO_free(f_bio2);
  23120. AssertNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  23121. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  23122. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  23123. BIO_free(f_bio1);
  23124. }
  23125. #endif /* !defined(NO_FILESYSTEM) */
  23126. /* BIO info callback */
  23127. {
  23128. const char* testArg = "test";
  23129. BIO* cb_bio;
  23130. AssertNotNull(cb_bio = BIO_new(BIO_s_mem()));
  23131. BIO_set_callback(cb_bio, bioCallback);
  23132. AssertNotNull(BIO_get_callback(cb_bio));
  23133. BIO_set_callback(cb_bio, NULL);
  23134. AssertNull(BIO_get_callback(cb_bio));
  23135. BIO_set_callback_arg(cb_bio, (char*)testArg);
  23136. AssertStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  23137. AssertNull(BIO_get_callback_arg(NULL));
  23138. BIO_free(cb_bio);
  23139. }
  23140. /* BIO_vfree */
  23141. AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
  23142. BIO_vfree(NULL);
  23143. BIO_vfree(bio1);
  23144. printf(resultFmt, passed);
  23145. #endif
  23146. }
  23147. static void test_wolfSSL_ASN1_STRING(void)
  23148. {
  23149. #if defined(OPENSSL_EXTRA)
  23150. ASN1_STRING* str = NULL;
  23151. const char data[] = "hello wolfSSL";
  23152. printf(testingFmt, "wolfSSL_ASN1_STRING()");
  23153. AssertNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  23154. AssertIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  23155. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  23156. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  23157. AssertIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  23158. ASN1_STRING_free(str);
  23159. printf(resultFmt, passed);
  23160. #endif
  23161. }
  23162. static void test_wolfSSL_ASN1_BIT_STRING(void)
  23163. {
  23164. #ifdef OPENSSL_ALL
  23165. ASN1_BIT_STRING* str;
  23166. printf(testingFmt, "test_wolfSSL_ASN1_BIT_STRING()");
  23167. AssertNotNull(str = ASN1_BIT_STRING_new());
  23168. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  23169. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  23170. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  23171. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  23172. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  23173. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  23174. ASN1_BIT_STRING_free(str);
  23175. printf(resultFmt, passed);
  23176. #endif
  23177. }
  23178. static void test_wolfSSL_DES_ecb_encrypt(void)
  23179. {
  23180. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  23181. WOLFSSL_DES_cblock input1,input2,output1,output2,back1,back2;
  23182. WOLFSSL_DES_key_schedule key;
  23183. printf(testingFmt, "wolfSSL_DES_ecb_encrypt()");
  23184. XMEMCPY(key,"12345678",sizeof(WOLFSSL_DES_key_schedule));
  23185. XMEMCPY(input1, "Iamhuman",sizeof(WOLFSSL_DES_cblock));
  23186. XMEMCPY(input2, "Whoisit?",sizeof(WOLFSSL_DES_cblock));
  23187. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  23188. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  23189. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  23190. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  23191. /* Encrypt messages */
  23192. wolfSSL_DES_ecb_encrypt(&input1,&output1,&key,DES_ENCRYPT);
  23193. wolfSSL_DES_ecb_encrypt(&input2,&output2,&key,DES_ENCRYPT);
  23194. /* Decrypt messages */
  23195. int ret1 = 0;
  23196. int ret2 = 0;
  23197. wolfSSL_DES_ecb_encrypt(&output1,&back1,&key,DES_DECRYPT);
  23198. ret1 = XMEMCMP((unsigned char *) back1,(unsigned char *) input1,sizeof(WOLFSSL_DES_cblock));
  23199. AssertIntEQ(ret1,0);
  23200. wolfSSL_DES_ecb_encrypt(&output2,&back2,&key,DES_DECRYPT);
  23201. ret2 = XMEMCMP((unsigned char *) back2,(unsigned char *) input2,sizeof(WOLFSSL_DES_cblock));
  23202. AssertIntEQ(ret2,0);
  23203. printf(resultFmt, passed);
  23204. #endif
  23205. }
  23206. static void test_wolfSSL_ASN1_TIME_adj(void)
  23207. {
  23208. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  23209. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  23210. const int year = 365*24*60*60;
  23211. const int day = 24*60*60;
  23212. const int hour = 60*60;
  23213. const int mini = 60;
  23214. const byte asn_utc_time = ASN_UTC_TIME;
  23215. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  23216. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  23217. #endif
  23218. WOLFSSL_ASN1_TIME *asn_time, *s;
  23219. int offset_day;
  23220. long offset_sec;
  23221. char date_str[CTC_DATE_SIZE + 1];
  23222. time_t t;
  23223. printf(testingFmt, "wolfSSL_ASN1_TIME_adj()");
  23224. s = wolfSSL_ASN1_TIME_new();
  23225. /* UTC notation test */
  23226. /* 2000/2/15 20:30:00 */
  23227. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  23228. offset_day = 7;
  23229. offset_sec = 45 * mini;
  23230. /* offset_sec = -45 * min;*/
  23231. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  23232. AssertTrue(asn_time->type == asn_utc_time);
  23233. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  23234. date_str[CTC_DATE_SIZE] = '\0';
  23235. AssertIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  23236. /* negative offset */
  23237. offset_sec = -45 * mini;
  23238. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  23239. AssertTrue(asn_time->type == asn_utc_time);
  23240. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  23241. date_str[CTC_DATE_SIZE] = '\0';
  23242. AssertIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  23243. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  23244. XMEMSET(date_str, 0, sizeof(date_str));
  23245. /* Generalized time will overflow time_t if not long */
  23246. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  23247. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  23248. DYNAMIC_TYPE_OPENSSL);
  23249. /* GeneralizedTime notation test */
  23250. /* 2055/03/01 09:00:00 */
  23251. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  23252. offset_day = 12;
  23253. offset_sec = 10 * mini;
  23254. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  23255. AssertTrue(asn_time->type == asn_gen_time);
  23256. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  23257. date_str[CTC_DATE_SIZE] = '\0';
  23258. AssertIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  23259. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  23260. XMEMSET(date_str, 0, sizeof(date_str));
  23261. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  23262. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  23263. s = NULL;
  23264. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  23265. offset_day = 7;
  23266. offset_sec = 45 * mini;
  23267. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  23268. AssertTrue(asn_time->type == asn_utc_time);
  23269. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  23270. date_str[CTC_DATE_SIZE] = '\0';
  23271. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  23272. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  23273. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, offset_sec);
  23274. AssertTrue(asn_time->type == asn_utc_time);
  23275. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  23276. date_str[CTC_DATE_SIZE] = '\0';
  23277. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  23278. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  23279. printf(resultFmt, passed);
  23280. #endif
  23281. }
  23282. static void test_wolfSSL_X509_cmp_time(void)
  23283. {
  23284. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  23285. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  23286. WOLFSSL_ASN1_TIME asn_time;
  23287. time_t t;
  23288. printf(testingFmt, "wolfSSL_X509_cmp_time()");
  23289. AssertIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  23290. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  23291. AssertIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  23292. asn_time.type = ASN_UTC_TIME;
  23293. asn_time.length = ASN_UTC_TIME_SIZE;
  23294. XMEMCPY(&asn_time.data, "000222211515Z", 13);
  23295. AssertIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  23296. printf(resultFmt, passed);
  23297. #endif
  23298. }
  23299. static void test_wolfSSL_X509_time_adj(void)
  23300. {
  23301. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  23302. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  23303. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  23304. !defined(NO_ASN_TIME)
  23305. X509* x509;
  23306. time_t t, not_before, not_after;
  23307. printf(testingFmt, "wolfSSL_X509_time_adj()");
  23308. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  23309. client_cert_der_2048, sizeof_client_cert_der_2048,
  23310. WOLFSSL_FILETYPE_ASN1));
  23311. t = 0;
  23312. not_before = XTIME(0);
  23313. not_after = XTIME(0) + (60 * 24 * 30); /* 30 days after */
  23314. AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  23315. AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  23316. X509_free(x509);
  23317. printf(resultFmt, passed);
  23318. #endif
  23319. }
  23320. static void test_wolfSSL_X509(void)
  23321. {
  23322. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)\
  23323. && !defined(NO_RSA)
  23324. X509* x509;
  23325. BIO* bio;
  23326. X509_STORE_CTX* ctx;
  23327. X509_STORE* store;
  23328. char der[] = "certs/ca-cert.der";
  23329. XFILE fp;
  23330. printf(testingFmt, "wolfSSL_X509()");
  23331. AssertNotNull(x509 = X509_new());
  23332. X509_free(x509);
  23333. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
  23334. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  23335. AssertIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  23336. AssertNotNull(ctx = X509_STORE_CTX_new());
  23337. AssertIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  23338. AssertNotNull(store = X509_STORE_new());
  23339. AssertIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  23340. AssertIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  23341. AssertIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  23342. X509_STORE_CTX_free(ctx);
  23343. X509_STORE_free(store);
  23344. X509_free(x509);
  23345. BIO_free(bio);
  23346. /** d2i_X509_fp test **/
  23347. fp = XFOPEN(der, "rb");
  23348. AssertTrue((fp != XBADFILE));
  23349. AssertNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  23350. AssertNotNull(x509);
  23351. X509_free(x509);
  23352. XFCLOSE(fp);
  23353. fp = XFOPEN(der, "rb");
  23354. AssertTrue((fp != XBADFILE));
  23355. AssertNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  23356. AssertNotNull(x509);
  23357. X509_free(x509);
  23358. XFCLOSE(fp);
  23359. /* X509_up_ref test */
  23360. AssertIntEQ(X509_up_ref(NULL), 0);
  23361. AssertNotNull(x509 = X509_new()); /* refCount = 1 */
  23362. AssertIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  23363. AssertIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  23364. X509_free(x509); /* refCount = 2 */
  23365. X509_free(x509); /* refCount = 1 */
  23366. X509_free(x509); /* refCount = 0, free */
  23367. printf(resultFmt, passed);
  23368. #endif
  23369. }
  23370. static void test_wolfSSL_X509_get_ext_count(void)
  23371. {
  23372. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  23373. int ret = 0;
  23374. WOLFSSL_X509* x509;
  23375. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  23376. FILE* f;
  23377. printf(testingFmt, "wolfSSL_X509_get_ext_count()");
  23378. /* NULL parameter check */
  23379. AssertIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  23380. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  23381. SSL_FILETYPE_PEM));
  23382. AssertIntEQ(X509_get_ext_count(x509), 4);
  23383. wolfSSL_X509_free(x509);
  23384. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  23385. SSL_FILETYPE_PEM));
  23386. AssertIntEQ(X509_get_ext_count(x509), 5);
  23387. wolfSSL_X509_free(x509);
  23388. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  23389. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  23390. fclose(f);
  23391. printf(testingFmt, "wolfSSL_X509_get_ext_count() valid input");
  23392. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 4);
  23393. printf(resultFmt, ret == 4 ? passed : failed);
  23394. printf(testingFmt, "wolfSSL_X509_get_ext_count() NULL argument");
  23395. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  23396. printf(resultFmt, ret == WOLFSSL_FAILURE ? passed : failed);
  23397. wolfSSL_X509_free(x509);
  23398. printf(resultFmt, passed);
  23399. #endif
  23400. }
  23401. static void test_wolfSSL_X509_sign(void)
  23402. {
  23403. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  23404. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  23405. int ret;
  23406. X509_NAME *name;
  23407. X509 *x509;
  23408. EVP_PKEY *pub;
  23409. EVP_PKEY *priv;
  23410. #if defined(USE_CERT_BUFFERS_1024)
  23411. const unsigned char* rsaPriv = client_key_der_1024;
  23412. const unsigned char* rsaPub = client_keypub_der_1024;
  23413. long clientKeySz = (long)sizeof_client_key_der_1024;
  23414. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  23415. #elif defined(USE_CERT_BUFFERS_2048)
  23416. const unsigned char* rsaPriv = client_key_der_2048;
  23417. const unsigned char* rsaPub = client_keypub_der_2048;
  23418. long clientKeySz = (long)sizeof_client_key_der_2048;
  23419. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  23420. #endif
  23421. byte sn[16];
  23422. int snSz = sizeof(sn);
  23423. printf(testingFmt, "wolfSSL_X509_sign\n");
  23424. /* Set X509_NAME fields */
  23425. AssertNotNull(name = X509_NAME_new());
  23426. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  23427. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  23428. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  23429. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  23430. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  23431. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  23432. /* Get private and public keys */
  23433. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  23434. clientKeySz));
  23435. AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  23436. AssertNotNull(x509 = X509_new());
  23437. /* Set version 3 */
  23438. AssertIntNE(X509_set_version(x509, 2L), 0);
  23439. /* Set subject name, add pubkey, and sign certificate */
  23440. AssertIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  23441. AssertIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  23442. #ifdef WOLFSSL_ALT_NAMES
  23443. /* Add some subject alt names */
  23444. AssertIntNE(wolfSSL_X509_add_altname(NULL,
  23445. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  23446. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  23447. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  23448. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  23449. "sphygmomanometer",
  23450. ASN_DNS_TYPE), SSL_SUCCESS);
  23451. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  23452. "supercalifragilisticexpialidocious",
  23453. ASN_DNS_TYPE), SSL_SUCCESS);
  23454. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  23455. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  23456. ASN_DNS_TYPE), SSL_SUCCESS);
  23457. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  23458. {
  23459. unsigned char ip_type[] = {127,0,0,1};
  23460. AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip_type,
  23461. sizeof(ip_type), ASN_IP_TYPE), SSL_SUCCESS);
  23462. }
  23463. #endif
  23464. #endif /* WOLFSSL_ALT_NAMES */
  23465. /* Test invalid parameters */
  23466. AssertIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  23467. AssertIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  23468. AssertIntEQ(X509_sign(x509, priv, NULL), 0);
  23469. ret = X509_sign(x509, priv, EVP_sha256());
  23470. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  23471. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  23472. #endif
  23473. #if 0
  23474. /* example for writing to file */
  23475. XFILE tmpFile = XFOPEN("./signed.der", "wb");
  23476. if (tmpFile) {
  23477. int derSz = 0;
  23478. const byte* der = wolfSSL_X509_get_der(x509, &derSz);
  23479. XFWRITE(der, 1, derSz, tmpFile);
  23480. }
  23481. XFCLOSE(tmpFile);
  23482. #endif
  23483. /* test invalid version number */
  23484. #if defined(OPENSSL_ALL)
  23485. #ifdef WOLFSSL_ALT_NAMES
  23486. AssertIntEQ(X509_get_ext_count(x509), 1);
  23487. #endif
  23488. AssertIntNE(X509_set_version(x509, 6L), 0);
  23489. AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  23490. /* uses ParseCert which fails on bad version number */
  23491. AssertIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  23492. #endif
  23493. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  23494. WOLFSSL_SUCCESS);
  23495. #ifndef WOLFSSL_ALT_NAMES
  23496. /* Valid case - size should be 798 with 16 byte serial number */
  23497. AssertIntEQ(ret, 782 + snSz);
  23498. #else /* WOLFSSL_ALT_NAMES */
  23499. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  23500. /* Valid case - size should be 936 with 16 byte serial number */
  23501. AssertIntEQ(ret, 920 + snSz);
  23502. #else
  23503. /* Valid case - size should be 927 with 16 byte serial number */
  23504. AssertIntEQ(ret, 911 + snSz);
  23505. #endif
  23506. #endif /* WOLFSSL_ALT_NAMES */
  23507. X509_NAME_free(name);
  23508. EVP_PKEY_free(priv);
  23509. EVP_PKEY_free(pub);
  23510. X509_free(x509);
  23511. printf(resultFmt, passed);
  23512. #endif
  23513. }
  23514. static void test_wolfSSL_X509_get0_tbs_sigalg(void)
  23515. {
  23516. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  23517. X509* x509 = NULL;
  23518. const X509_ALGOR* alg;
  23519. printf(testingFmt, "wolfSSL_X509_get0_tbs_sigalg");
  23520. AssertNotNull(x509 = X509_new());
  23521. AssertNull(alg = X509_get0_tbs_sigalg(NULL));
  23522. AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
  23523. X509_free(x509);
  23524. printf(resultFmt, passed);
  23525. #endif
  23526. }
  23527. static void test_wolfSSL_X509_ALGOR_get0(void)
  23528. {
  23529. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_SHA256)
  23530. X509* x509 = NULL;
  23531. const ASN1_OBJECT* obj = NULL;
  23532. const X509_ALGOR* alg;
  23533. int pptype = 0;
  23534. const void *ppval = NULL;
  23535. printf(testingFmt, "wolfSSL_X509_ALGOR_get0");
  23536. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  23537. SSL_FILETYPE_PEM));
  23538. AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
  23539. /* Invalid case */
  23540. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  23541. AssertNull(obj);
  23542. /* Valid case */
  23543. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  23544. AssertNotNull(obj);
  23545. AssertNotNull(ppval);
  23546. AssertIntNE(pptype, 0);
  23547. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  23548. AssertIntEQ(OBJ_obj2nid(obj), CTC_SHA256wRSA);
  23549. X509_free(x509);
  23550. printf(resultFmt, passed);
  23551. #endif
  23552. }
  23553. static void test_wolfSSL_X509_VERIFY_PARAM(void)
  23554. {
  23555. #if defined(OPENSSL_EXTRA)
  23556. WOLFSSL_X509_VERIFY_PARAM *param;
  23557. int ret;
  23558. char testIPv4[] = "127.0.0.1";
  23559. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  23560. printf(testingFmt, "wolfSSL_X509()");
  23561. /* Initializer function is not ported */
  23562. /* param = wolfSSL_X509_VERIFY_PARAM_new(); */
  23563. param = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  23564. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL);
  23565. AssertNotNull(param);
  23566. XMEMSET(param, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM ));
  23567. wolfSSL_X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  23568. wolfSSL_X509_VERIFY_PARAM_set_hostflags(param, 0x01);
  23569. AssertIntEQ(0x01, param->hostFlags);
  23570. ret = wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4);
  23571. AssertIntEQ(0, ret);
  23572. ret = wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(param, testIPv4);
  23573. AssertIntEQ(1, ret);
  23574. AssertIntEQ(0, XSTRNCMP(param->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  23575. ret = wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(param, NULL);
  23576. AssertIntEQ(1, ret);
  23577. ret = wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(param, testIPv6);
  23578. AssertIntEQ(1, ret);
  23579. AssertIntEQ(0, XSTRNCMP(param->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  23580. XFREE(param, NULL, DYNAMIC_TYPE_OPENSSL);
  23581. printf(resultFmt, passed);
  23582. #endif
  23583. }
  23584. static void test_wolfSSL_X509_get_X509_PUBKEY(void)
  23585. {
  23586. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  23587. X509* x509 = NULL;
  23588. X509_PUBKEY* pubKey;
  23589. printf(testingFmt, "wolfSSL_X509_get_X509_PUBKEY");
  23590. AssertNotNull(x509 = X509_new());
  23591. AssertNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  23592. AssertNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  23593. X509_free(x509);
  23594. printf(resultFmt, passed);
  23595. #endif
  23596. }
  23597. static void test_wolfSSL_X509_PUBKEY(void)
  23598. {
  23599. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_SHA256)
  23600. X509* x509 = NULL;
  23601. ASN1_OBJECT* obj = NULL;
  23602. X509_PUBKEY* pubKey;
  23603. X509_PUBKEY* pubKey2;
  23604. EVP_PKEY* evpKey;
  23605. const unsigned char *pk;
  23606. int ppklen;
  23607. WOLFSSL_X509_ALGOR *pa;
  23608. printf(testingFmt, "wolfSSL_X509_get_X509_PUBKEY");
  23609. AssertNotNull(x509 = X509_load_certificate_file(cliCertFile,
  23610. SSL_FILETYPE_PEM));
  23611. AssertNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  23612. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  23613. AssertNotNull(pk);
  23614. AssertNotNull(pa);
  23615. AssertNotNull(pubKey);
  23616. AssertIntGT(ppklen, 0);
  23617. AssertIntEQ(OBJ_obj2nid(obj), RSAk);
  23618. AssertNotNull(evpKey = X509_PUBKEY_get(pubKey));
  23619. AssertNotNull(pubKey2 = X509_PUBKEY_new());
  23620. AssertIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  23621. X509_PUBKEY_free(pubKey2);
  23622. X509_free(x509);
  23623. printf(resultFmt, passed);
  23624. #endif
  23625. }
  23626. static void test_wolfSSL_RAND(void)
  23627. {
  23628. #if defined(OPENSSL_EXTRA)
  23629. byte seed[16];
  23630. printf(testingFmt, "wolfSSL_RAND()");
  23631. RAND_seed(seed, sizeof(seed));
  23632. AssertIntEQ(RAND_poll(), 1);
  23633. RAND_cleanup();
  23634. AssertIntEQ(RAND_egd(NULL), -1);
  23635. #ifndef NO_FILESYSTEM
  23636. {
  23637. char fname[100];
  23638. AssertNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  23639. AssertIntEQ(RAND_write_file(NULL), 0);
  23640. }
  23641. #endif
  23642. printf(resultFmt, passed);
  23643. #endif
  23644. }
  23645. static void test_wolfSSL_BUF(void)
  23646. {
  23647. #if defined(OPENSSL_EXTRA)
  23648. BUF_MEM* buf;
  23649. AssertNotNull(buf = BUF_MEM_new());
  23650. AssertIntEQ(BUF_MEM_grow(buf, 10), 10);
  23651. AssertIntEQ(BUF_MEM_grow(buf, -1), 0);
  23652. BUF_MEM_free(buf);
  23653. #endif /* OPENSSL_EXTRA */
  23654. }
  23655. static void test_wolfSSL_pseudo_rand(void)
  23656. {
  23657. #if defined(OPENSSL_EXTRA)
  23658. BIGNUM* bn;
  23659. unsigned char bin[8];
  23660. int i;
  23661. printf(testingFmt, "wolfSSL_pseudo_rand()");
  23662. /* BN_pseudo_rand returns 1 on success 0 on failure
  23663. * int BN_pseudo_rand(BIGNUM* bn, int bits, int top, int bottom) */
  23664. for (i = 0; i < 10; i++) {
  23665. AssertNotNull(bn = BN_new());
  23666. AssertIntEQ(BN_pseudo_rand(bn, 8, 0, 0), SSL_SUCCESS);
  23667. AssertIntGT(BN_bn2bin(bn, bin),0);
  23668. AssertIntEQ((bin[0] & 0x80), 0x80); /* top bit should be set */
  23669. BN_free(bn);
  23670. }
  23671. for (i = 0; i < 10; i++) {
  23672. AssertNotNull(bn = BN_new());
  23673. AssertIntEQ(BN_pseudo_rand(bn, 8, 1, 1), SSL_SUCCESS);
  23674. AssertIntGT(BN_bn2bin(bn, bin),0);
  23675. AssertIntEQ((bin[0] & 0xc1), 0xc1); /* top bit should be set */
  23676. BN_free(bn);
  23677. }
  23678. printf(resultFmt, passed);
  23679. #endif
  23680. }
  23681. static void test_wolfSSL_PKCS8_Compat(void)
  23682. {
  23683. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  23684. PKCS8_PRIV_KEY_INFO* pt;
  23685. BIO* bio;
  23686. XFILE f;
  23687. int bytes;
  23688. char pkcs8_buffer[512];
  23689. printf(testingFmt, "wolfSSL_pkcs8()");
  23690. /* file from wolfssl/certs/ directory */
  23691. f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb");
  23692. AssertTrue(f != XBADFILE);
  23693. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)), 0);
  23694. XFCLOSE(f);
  23695. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  23696. AssertNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  23697. BIO_free(bio);
  23698. PKCS8_PRIV_KEY_INFO_free(pt);
  23699. printf(resultFmt, passed);
  23700. #endif
  23701. }
  23702. static void test_wolfSSL_PKCS8_d2i(void)
  23703. {
  23704. #ifndef HAVE_FIPS
  23705. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  23706. * requires a 12 byte password in FIPS mode. This test ends up
  23707. * trying to use an 8 byte password. */
  23708. #ifdef OPENSSL_ALL
  23709. WOLFSSL_EVP_PKEY* pkey = NULL;
  23710. #ifndef NO_FILESYSTEM
  23711. unsigned char pkcs8_buffer[2048];
  23712. const unsigned char* p;
  23713. int bytes;
  23714. XFILE file;
  23715. BIO* bio;
  23716. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  23717. #endif
  23718. #ifndef NO_RSA
  23719. #ifndef NO_FILESYSTEM
  23720. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  23721. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  23722. #ifndef NO_DES3
  23723. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  23724. #endif
  23725. #endif
  23726. #ifdef USE_CERT_BUFFERS_1024
  23727. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  23728. int rsaSz = sizeof_server_key_der_1024;
  23729. #else
  23730. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  23731. int rsaSz = sizeof_server_key_der_2048;
  23732. #endif
  23733. #endif
  23734. #ifdef HAVE_ECC
  23735. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  23736. int ecSz = sizeof_ecc_key_der_256;
  23737. #ifndef NO_FILESYSTEM
  23738. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  23739. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  23740. #ifndef NO_DES3
  23741. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  23742. #endif
  23743. #endif
  23744. #endif
  23745. #ifndef NO_FILESYSTEM
  23746. (void)pkcs8_buffer;
  23747. (void)p;
  23748. (void)bytes;
  23749. (void)file;
  23750. (void)bio;
  23751. #endif
  23752. #ifndef NO_RSA
  23753. /* Try to auto-detect normal RSA private key */
  23754. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  23755. wolfSSL_EVP_PKEY_free(pkey);
  23756. #endif
  23757. #ifdef HAVE_ECC
  23758. /* Try to auto-detect normal EC private key */
  23759. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  23760. wolfSSL_EVP_PKEY_free(pkey);
  23761. #endif
  23762. #ifndef NO_FILESYSTEM
  23763. #ifndef NO_RSA
  23764. /* Get DER encoded RSA PKCS#8 data. */
  23765. file = XFOPEN(rsaDerPkcs8File, "rb");
  23766. AssertTrue(file != XBADFILE);
  23767. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  23768. file)), 0);
  23769. XFCLOSE(file);
  23770. p = pkcs8_buffer;
  23771. /* Try to decode - auto-detect key type. */
  23772. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  23773. /* Get PEM encoded RSA PKCS#8 data. */
  23774. file = XFOPEN(rsaPemPkcs8File, "rb");
  23775. AssertTrue(file != XBADFILE);
  23776. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  23777. file)), 0);
  23778. XFCLOSE(file);
  23779. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  23780. /* Write PKCS#8 PEM to BIO. */
  23781. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  23782. NULL), bytes);
  23783. /* Compare file and written data */
  23784. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &p), bytes);
  23785. AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  23786. BIO_free(bio);
  23787. #ifndef NO_DES3
  23788. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  23789. /* Write Encrypted PKCS#8 PEM to BIO. */
  23790. bytes = 1834;
  23791. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  23792. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  23793. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  23794. (void*)"yassl123"));
  23795. wolfSSL_EVP_PKEY_free(evpPkey);
  23796. BIO_free(bio);
  23797. #endif
  23798. wolfSSL_EVP_PKEY_free(pkey);
  23799. /* PKCS#8 encrypted RSA key */
  23800. #ifndef NO_DES3
  23801. file = XFOPEN(rsaDerPkcs8EncFile, "rb");
  23802. AssertTrue(file != XBADFILE);
  23803. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  23804. file)), 0);
  23805. XFCLOSE(file);
  23806. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  23807. AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  23808. (void*)"yassl123"));
  23809. wolfSSL_EVP_PKEY_free(pkey);
  23810. BIO_free(bio);
  23811. #endif
  23812. #endif
  23813. #ifdef HAVE_ECC
  23814. /* PKCS#8 encode EC key */
  23815. file = XFOPEN(ecDerPkcs8File, "rb");
  23816. AssertTrue(file != XBADFILE);
  23817. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  23818. file)), 0);
  23819. XFCLOSE(file);
  23820. p = pkcs8_buffer;
  23821. /* Try to decode - auto-detect key type. */
  23822. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  23823. /* Get PEM encoded RSA PKCS#8 data. */
  23824. file = XFOPEN(ecPemPkcs8File, "rb");
  23825. AssertTrue(file != XBADFILE);
  23826. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  23827. file)), 0);
  23828. XFCLOSE(file);
  23829. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  23830. /* Write PKCS#8 PEM to BIO. */
  23831. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  23832. NULL), bytes);
  23833. /* Compare file and written data */
  23834. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &p), bytes);
  23835. AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  23836. BIO_free(bio);
  23837. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  23838. /* Write Encrypted PKCS#8 PEM to BIO. */
  23839. bytes = 379;
  23840. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  23841. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  23842. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  23843. (void*)"yassl123"));
  23844. wolfSSL_EVP_PKEY_free(evpPkey);
  23845. BIO_free(bio);
  23846. wolfSSL_EVP_PKEY_free(pkey);
  23847. /* PKCS#8 encrypted EC key */
  23848. #ifndef NO_DES3
  23849. file = XFOPEN(ecDerPkcs8EncFile, "rb");
  23850. AssertTrue(file != XBADFILE);
  23851. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  23852. file)), 0);
  23853. XFCLOSE(file);
  23854. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  23855. AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  23856. (void*)"yassl123"));
  23857. wolfSSL_EVP_PKEY_free(pkey);
  23858. BIO_free(bio);
  23859. #endif
  23860. #endif
  23861. #endif
  23862. printf(resultFmt, passed);
  23863. #endif
  23864. #endif /* HAVE_FIPS */
  23865. }
  23866. static void test_wolfSSL_ERR_put_error(void)
  23867. {
  23868. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  23869. defined(DEBUG_WOLFSSL)
  23870. const char* file;
  23871. int line;
  23872. printf(testingFmt, "wolfSSL_ERR_put_error()");
  23873. ERR_clear_error(); /* clear out any error nodes */
  23874. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  23875. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  23876. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  23877. AssertIntEQ(ERR_get_error_line(&file, &line), 1);
  23878. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  23879. AssertIntEQ(ERR_get_error_line(&file, &line), 2);
  23880. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  23881. AssertIntEQ(ERR_get_error_line(&file, &line), 3);
  23882. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  23883. AssertIntEQ(ERR_get_error_line(&file, &line), 4);
  23884. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  23885. AssertIntEQ(ERR_get_error_line(&file, &line), 5);
  23886. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  23887. AssertIntEQ(ERR_get_error_line(&file, &line), 6);
  23888. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  23889. AssertIntEQ(ERR_get_error_line(&file, &line), 7);
  23890. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  23891. AssertIntEQ(ERR_get_error_line(&file, &line), 8);
  23892. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  23893. AssertIntEQ(ERR_get_error_line(&file, &line), 9);
  23894. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  23895. AssertIntEQ(ERR_get_error_line(&file, &line), 10);
  23896. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  23897. AssertIntEQ(ERR_get_error_line(&file, &line), 11);
  23898. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  23899. AssertIntEQ(ERR_get_error_line(&file, &line), 12);
  23900. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  23901. AssertIntEQ(ERR_get_error_line(&file, &line), 13);
  23902. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  23903. AssertIntEQ(ERR_get_error_line(&file, &line), 14);
  23904. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  23905. AssertIntEQ(ERR_get_error_line(&file, &line), 15);
  23906. /* try reading past end of error queue */
  23907. file = NULL;
  23908. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  23909. AssertNull(file);
  23910. AssertIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  23911. PEMerr(4,4);
  23912. AssertIntEQ(ERR_get_error(), 4);
  23913. /* Empty and free up all error nodes */
  23914. ERR_clear_error();
  23915. /* Verify all nodes are cleared */
  23916. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  23917. ERR_clear_error();
  23918. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  23919. printf(resultFmt, passed);
  23920. #endif
  23921. }
  23922. static void test_wolfSSL_ERR_print_errors(void)
  23923. {
  23924. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  23925. defined(DEBUG_WOLFSSL)
  23926. BIO* bio;
  23927. char buf[1024];
  23928. printf(testingFmt, "wolfSSL_ERR_print_errors()");
  23929. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  23930. ERR_clear_error(); /* clear out any error nodes */
  23931. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  23932. /* Choosing -295 as an unused errno between MIN_CODE_E < x < WC_LAST_E. */
  23933. ERR_put_error(0,SYS_F_BIND, -295, "asn.c", 100);
  23934. ERR_print_errors(bio);
  23935. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  23936. AssertIntEQ(XSTRNCMP("error:173:wolfSSL library:Bad function argument:ssl.c:0",
  23937. buf, 55), 0);
  23938. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  23939. AssertIntEQ(XSTRNCMP("error:295:wolfSSL library:unknown error number:asn.c:100",
  23940. buf, 56), 0);
  23941. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  23942. AssertIntEQ(ERR_get_error_line(NULL, NULL), 0);
  23943. BIO_free(bio);
  23944. printf(resultFmt, passed);
  23945. #endif
  23946. }
  23947. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  23948. defined(DEBUG_WOLFSSL)
  23949. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  23950. {
  23951. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  23952. return 0;
  23953. }
  23954. #endif
  23955. static void test_wolfSSL_ERR_print_errors_cb(void)
  23956. {
  23957. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  23958. defined(DEBUG_WOLFSSL)
  23959. BIO* bio;
  23960. char buf[1024];
  23961. printf(testingFmt, "wolfSSL_ERR_print_errors_cb()");
  23962. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  23963. ERR_clear_error(); /* clear out any error nodes */
  23964. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  23965. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  23966. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  23967. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  23968. AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  23969. buf, 53), 0);
  23970. AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 275 line:100 file:asn.c",
  23971. buf + 53, 55), 0);
  23972. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  23973. BIO_free(bio);
  23974. printf(resultFmt, passed);
  23975. #endif
  23976. }
  23977. /*
  23978. * Testing WOLFSSL_ERROR_MSG
  23979. */
  23980. static int test_WOLFSSL_ERROR_MSG (void)
  23981. {
  23982. int ret = 0;
  23983. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  23984. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  23985. const char* msg = "Everyone gets Friday off.";
  23986. printf(testingFmt, "WOLFSSL_ERROR_MSG()");
  23987. WOLFSSL_ERROR_MSG(msg);
  23988. printf(resultFmt, ret == 0 ? passed : failed);
  23989. #endif
  23990. return ret;
  23991. }/*End test_WOLFSSL_ERROR_MSG*/
  23992. /*
  23993. * Testing wc_ERR_remove_state
  23994. */
  23995. static int test_wc_ERR_remove_state (void)
  23996. {
  23997. int ret = 0;
  23998. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  23999. printf(testingFmt, "wc_ERR_remove_state()");
  24000. wc_ERR_remove_state();
  24001. printf(resultFmt, ret == 0 ? passed : failed);
  24002. #endif
  24003. return ret;
  24004. }/*End test_wc_ERR_remove_state*/
  24005. /*
  24006. * Testing wc_ERR_print_errors_fp
  24007. */
  24008. static int test_wc_ERR_print_errors_fp (void)
  24009. {
  24010. int ret = 0;
  24011. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  24012. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  24013. long sz;
  24014. printf(testingFmt, "wc_ERR_print_errors_fp()");
  24015. WOLFSSL_ERROR(BAD_FUNC_ARG);
  24016. XFILE fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar");
  24017. wc_ERR_print_errors_fp(fp);
  24018. #if defined(DEBUG_WOLFSSL)
  24019. AssertTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  24020. sz = XFTELL(fp);
  24021. if (sz == 0) {
  24022. ret = BAD_FUNC_ARG;
  24023. }
  24024. #endif
  24025. printf(resultFmt, ret == 0 ? passed : failed);
  24026. XFCLOSE(fp);
  24027. (void)sz;
  24028. #endif
  24029. return ret;
  24030. }/*End test_wc_ERR_print_errors_fp*/
  24031. #ifdef DEBUG_WOLFSSL
  24032. static void Logging_cb(const int logLevel, const char *const logMessage)
  24033. {
  24034. (void)logLevel;
  24035. (void)logMessage;
  24036. }
  24037. #endif
  24038. /*
  24039. * Testing wolfSSL_GetLoggingCb
  24040. */
  24041. static int test_wolfSSL_GetLoggingCb (void)
  24042. {
  24043. int ret = 0;
  24044. #ifdef DEBUG_WOLFSSL
  24045. printf(testingFmt, "wolfSSL_GetLoggingCb()");
  24046. /*Testing without wolfSSL_SetLoggingCb()*/
  24047. if (ret == 0) {
  24048. if(wolfSSL_GetLoggingCb() == NULL){ /*Should be true*/
  24049. ret = 0;
  24050. }
  24051. if(wolfSSL_GetLoggingCb() != NULL){ /*Should not be true*/
  24052. ret = -1;
  24053. }
  24054. }
  24055. /*Testing with wolfSSL_SetLoggingCb()*/
  24056. if (ret == 0) {
  24057. ret = wolfSSL_SetLoggingCb(Logging_cb);
  24058. if (ret == 0){
  24059. if(wolfSSL_GetLoggingCb() == NULL){ /*Should not be true*/
  24060. ret = -1;
  24061. }
  24062. if (ret == 0) {
  24063. if(wolfSSL_GetLoggingCb() == Logging_cb){ /*Should be true*/
  24064. ret = 0;
  24065. }
  24066. }
  24067. }
  24068. }
  24069. printf(resultFmt, ret == 0 ? passed : failed);
  24070. #endif
  24071. return ret;
  24072. }/*End test_wolfSSL_GetLoggingCb*/
  24073. static void test_wolfSSL_HMAC(void)
  24074. {
  24075. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  24076. HMAC_CTX* hmac;
  24077. ENGINE* e = NULL;
  24078. const unsigned char key[] = "simple test key";
  24079. unsigned char hash[WC_MAX_DIGEST_SIZE];
  24080. unsigned int len;
  24081. printf(testingFmt, "wolfSSL_HMAC()");
  24082. AssertNotNull(hmac = HMAC_CTX_new());
  24083. HMAC_CTX_init(hmac);
  24084. AssertIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key),
  24085. EVP_sha256(), e), SSL_SUCCESS);
  24086. /* re-using test key as data to hash */
  24087. AssertIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), SSL_SUCCESS);
  24088. AssertIntEQ(HMAC_Update(hmac, NULL, 0), SSL_SUCCESS);
  24089. AssertIntEQ(HMAC_Final(hmac, hash, &len), SSL_SUCCESS);
  24090. AssertIntEQ(len, (int)WC_SHA256_DIGEST_SIZE);
  24091. AssertIntEQ(HMAC_size(hmac), WC_SHA256_DIGEST_SIZE);
  24092. HMAC_cleanup(hmac);
  24093. HMAC_CTX_free(hmac);
  24094. #endif
  24095. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  24096. len = 0;
  24097. AssertNotNull(HMAC(EVP_sha256(), key, (int)sizeof(key), NULL, 0, hash, &len));
  24098. AssertIntEQ(len, (int)WC_SHA256_DIGEST_SIZE);
  24099. #endif
  24100. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224)
  24101. len = 0;
  24102. AssertNotNull(HMAC(EVP_sha224(), key, (int)sizeof(key), NULL, 0, hash, &len));
  24103. AssertIntEQ(len, (int)WC_SHA224_DIGEST_SIZE);
  24104. #endif
  24105. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA384)
  24106. len = 0;
  24107. AssertNotNull(HMAC(EVP_sha384(), key, (int)sizeof(key), NULL, 0, hash, &len));
  24108. AssertIntEQ(len, (int)WC_SHA384_DIGEST_SIZE);
  24109. #endif
  24110. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  24111. len = 0;
  24112. AssertNotNull(HMAC(EVP_sha512(), key, (int)sizeof(key), NULL, 0, hash, &len));
  24113. AssertIntEQ(len, (int)WC_SHA512_DIGEST_SIZE);
  24114. #endif
  24115. printf(resultFmt, passed);
  24116. }
  24117. static void test_wolfSSL_OBJ(void)
  24118. {
  24119. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  24120. * mode
  24121. */
  24122. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  24123. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  24124. defined(WOLFSSL_CERT_GEN)
  24125. ASN1_OBJECT *obj = NULL;
  24126. char buf[50];
  24127. XFILE fp;
  24128. X509 *x509 = NULL;
  24129. X509_NAME *x509Name;
  24130. X509_NAME_ENTRY *x509NameEntry;
  24131. ASN1_OBJECT *asn1Name = NULL;
  24132. int numNames;
  24133. BIO *bio = NULL;
  24134. int nid;
  24135. int i, j;
  24136. const char *f[] = {
  24137. #ifndef NO_RSA
  24138. "./certs/ca-cert.der",
  24139. #endif
  24140. #ifdef HAVE_ECC
  24141. "./certs/ca-ecc-cert.der",
  24142. "./certs/ca-ecc384-cert.der",
  24143. #endif
  24144. NULL};
  24145. ASN1_OBJECT *field_name_obj = NULL;
  24146. int lastpos = -1;
  24147. int tmp = -1;
  24148. ASN1_STRING *asn1 = NULL;
  24149. unsigned char *buf_dyn = NULL;
  24150. PKCS12 *p12;
  24151. int boolRet;
  24152. EVP_PKEY *pkey = NULL;
  24153. const char *p12_f[] = {
  24154. #if !defined(NO_DES3) && !defined(NO_RSA)
  24155. "./certs/test-servercert.p12",
  24156. #endif
  24157. NULL};
  24158. printf(testingFmt, "wolfSSL_OBJ()");
  24159. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  24160. AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
  24161. AssertIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  24162. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  24163. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  24164. ASN1_OBJECT_free(obj);
  24165. AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
  24166. AssertIntEQ(OBJ_obj2nid(obj), NID_sha256);
  24167. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  24168. #ifdef WOLFSSL_CERT_EXT
  24169. AssertIntEQ(OBJ_txt2nid(buf), NID_sha256);
  24170. #endif
  24171. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  24172. ASN1_OBJECT_free(obj);
  24173. for (i = 0; f[i] != NULL; i++)
  24174. {
  24175. AssertTrue((fp = XFOPEN(f[i], "r")) != XBADFILE);
  24176. AssertNotNull(x509 = d2i_X509_fp(fp, NULL));
  24177. XFCLOSE(fp);
  24178. AssertNotNull(x509Name = X509_get_issuer_name(x509));
  24179. AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  24180. /* Get the Common Name by using OBJ_txt2obj */
  24181. AssertNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  24182. do
  24183. {
  24184. lastpos = tmp;
  24185. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  24186. } while (tmp > -1);
  24187. AssertIntNE(lastpos, -1);
  24188. ASN1_OBJECT_free(field_name_obj);
  24189. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  24190. AssertNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  24191. AssertIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  24192. /*
  24193. * All Common Names should be www.wolfssl.com
  24194. * This makes testing easier as we can test for the expected value.
  24195. */
  24196. AssertStrEQ((char*)buf_dyn, "www.wolfssl.com");
  24197. OPENSSL_free(buf_dyn);
  24198. bio = BIO_new(BIO_s_mem());
  24199. AssertTrue(bio != NULL);
  24200. for (j = 0; j < numNames; j++)
  24201. {
  24202. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  24203. AssertNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  24204. AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  24205. }
  24206. BIO_free(bio);
  24207. ASN1_OBJECT_free(asn1Name);
  24208. X509_free(x509);
  24209. }
  24210. for (i = 0; p12_f[i] != NULL; i++)
  24211. {
  24212. AssertTrue((fp = XFOPEN(p12_f[i], "r")) != XBADFILE);
  24213. AssertNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  24214. XFCLOSE(fp);
  24215. AssertTrue((boolRet = PKCS12_parse(p12, "wolfSSL test", &pkey, &x509, NULL)) > 0);
  24216. wc_PKCS12_free(p12);
  24217. EVP_PKEY_free(pkey);
  24218. x509Name = X509_get_issuer_name(x509);
  24219. AssertNotNull(x509Name);
  24220. AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  24221. AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  24222. for (j = 0; j < numNames; j++)
  24223. {
  24224. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  24225. AssertNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  24226. AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  24227. }
  24228. BIO_free(bio);
  24229. ASN1_OBJECT_free(asn1Name);
  24230. X509_free(x509);
  24231. }
  24232. printf(resultFmt, passed);
  24233. #endif
  24234. }
  24235. static void test_wolfSSL_i2a_ASN1_OBJECT(void)
  24236. {
  24237. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  24238. ASN1_OBJECT *obj = NULL;
  24239. BIO *bio = NULL;
  24240. AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
  24241. AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  24242. AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  24243. AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  24244. AssertIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  24245. BIO_free(bio);
  24246. ASN1_OBJECT_free(obj);
  24247. #endif
  24248. }
  24249. static void test_wolfSSL_OBJ_cmp(void)
  24250. {
  24251. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  24252. ASN1_OBJECT *obj = NULL;
  24253. ASN1_OBJECT *obj2 = NULL;
  24254. printf(testingFmt, "wolfSSL_OBJ_cmp()");
  24255. AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
  24256. AssertNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  24257. AssertIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  24258. AssertIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  24259. AssertIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  24260. AssertIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  24261. AssertIntEQ(OBJ_cmp(obj, obj), 0);
  24262. AssertIntEQ(OBJ_cmp(obj2, obj2), 0);
  24263. ASN1_OBJECT_free(obj);
  24264. ASN1_OBJECT_free(obj2);
  24265. printf(resultFmt, passed);
  24266. #endif
  24267. }
  24268. static void test_wolfSSL_OBJ_txt2nid(void)
  24269. {
  24270. #if !defined(NO_WOLFSSL_STUB) && defined(WOLFSSL_APACHE_HTTPD)
  24271. int i;
  24272. static const struct {
  24273. const char* sn;
  24274. const char* ln;
  24275. const char* oid;
  24276. int nid;
  24277. } testVals[] = {
  24278. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  24279. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  24280. NID_id_on_dnsSRV },
  24281. { "msUPN", "Microsoft User Principal Name",
  24282. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  24283. { NULL, NULL, NULL, NID_undef }
  24284. };
  24285. printf(testingFmt, "wolfSSL_OBJ_txt2nid()");
  24286. /* Invalid cases */
  24287. AssertIntEQ(OBJ_txt2nid(NULL), NID_undef);
  24288. AssertIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  24289. /* Valid cases */
  24290. for (i = 0; testVals[i].sn != NULL; i++) {
  24291. AssertIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  24292. AssertIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  24293. AssertIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  24294. }
  24295. printf(resultFmt, passed);
  24296. #endif
  24297. }
  24298. static void test_wolfSSL_OBJ_txt2obj(void)
  24299. {
  24300. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  24301. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  24302. int i;
  24303. char buf[50];
  24304. ASN1_OBJECT* obj;
  24305. static const struct {
  24306. const char* oidStr;
  24307. const char* sn;
  24308. const char* ln;
  24309. } objs_list[] = {
  24310. #if defined(WOLFSSL_APACHE_HTTPD)
  24311. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  24312. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  24313. #endif
  24314. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  24315. { NULL, NULL, NULL }
  24316. };
  24317. printf(testingFmt, "wolfSSL_OBJ_txt2obj()");
  24318. AssertNull(obj = OBJ_txt2obj("Bad name", 0));
  24319. AssertNull(obj = OBJ_txt2obj(NULL, 0));
  24320. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  24321. /* Test numerical value of oid (oidStr) */
  24322. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  24323. /* Convert object back to text to confirm oid is correct */
  24324. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  24325. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  24326. ASN1_OBJECT_free(obj);
  24327. XMEMSET(buf, 0, sizeof(buf));
  24328. /* Test short name (sn) */
  24329. AssertNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  24330. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  24331. /* Convert object back to text to confirm oid is correct */
  24332. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  24333. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  24334. ASN1_OBJECT_free(obj);
  24335. XMEMSET(buf, 0, sizeof(buf));
  24336. /* Test long name (ln) - should fail when no_name = 1 */
  24337. AssertNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  24338. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  24339. /* Convert object back to text to confirm oid is correct */
  24340. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  24341. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  24342. ASN1_OBJECT_free(obj);
  24343. XMEMSET(buf, 0, sizeof(buf));
  24344. }
  24345. printf(resultFmt, passed);
  24346. #endif
  24347. }
  24348. static void test_wolfSSL_X509_NAME_ENTRY(void)
  24349. {
  24350. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  24351. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  24352. X509* x509;
  24353. BIO* bio;
  24354. X509_NAME* nm;
  24355. X509_NAME_ENTRY* entry;
  24356. unsigned char cn[] = "another name to add";
  24357. printf(testingFmt, "wolfSSL_X509_NAME_ENTRY()");
  24358. AssertNotNull(x509 =
  24359. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  24360. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  24361. AssertIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  24362. #ifdef WOLFSSL_CERT_REQ
  24363. {
  24364. X509_REQ* req;
  24365. BIO* bReq;
  24366. AssertNotNull(req =
  24367. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  24368. AssertNotNull(bReq = BIO_new(BIO_s_mem()));
  24369. AssertIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  24370. BIO_free(bReq);
  24371. X509_free(req);
  24372. }
  24373. #endif
  24374. AssertNotNull(nm = X509_get_subject_name(x509));
  24375. /* Test add entry */
  24376. AssertNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  24377. 0x0c, cn, (int)sizeof(cn)));
  24378. AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  24379. #ifdef WOLFSSL_CERT_EXT
  24380. AssertIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  24381. (byte*)"support@wolfssl.com", 19, -1,
  24382. 1), WOLFSSL_SUCCESS);
  24383. #endif
  24384. X509_NAME_ENTRY_free(entry);
  24385. /* Test add entry by text */
  24386. AssertNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  24387. 0x0c, cn, (int)sizeof(cn)));
  24388. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  24389. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  24390. AssertNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  24391. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  24392. #endif
  24393. AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  24394. X509_NAME_ENTRY_free(entry);
  24395. /* Test add entry by NID */
  24396. AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  24397. cn, -1, -1, 0), WOLFSSL_SUCCESS);
  24398. BIO_free(bio);
  24399. X509_free(x509); /* free's nm */
  24400. printf(resultFmt, passed);
  24401. #endif
  24402. }
  24403. static void test_wolfSSL_X509_set_name(void)
  24404. {
  24405. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  24406. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  24407. X509* x509;
  24408. X509_NAME* name;
  24409. printf(testingFmt, "wolfSSL_X509_set_name()");
  24410. AssertNotNull(name = X509_NAME_new());
  24411. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  24412. (byte*)"wolfssl.com", 11, 0, 1),
  24413. WOLFSSL_SUCCESS);
  24414. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  24415. (byte*)"support@wolfssl.com", 19, -1,
  24416. 1), WOLFSSL_SUCCESS);
  24417. AssertNotNull(x509 = X509_new());
  24418. AssertIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  24419. AssertIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  24420. AssertIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  24421. AssertIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  24422. AssertIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  24423. AssertIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  24424. AssertIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  24425. AssertIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  24426. X509_free(x509);
  24427. X509_NAME_free(name);
  24428. printf(resultFmt, passed);
  24429. #endif /* OPENSSL_ALL && !NO_CERTS */
  24430. }
  24431. static void test_wolfSSL_X509_set_notAfter(void)
  24432. {
  24433. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  24434. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  24435. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  24436. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  24437. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  24438. /* Generalized time will overflow time_t if not long */
  24439. X509* x;
  24440. BIO* bio;
  24441. ASN1_TIME *asn_time, *time_check;
  24442. const int year = 365*24*60*60;
  24443. const int day = 24*60*60;
  24444. const int hour = 60*60;
  24445. const int mini = 60;
  24446. int offset_day;
  24447. unsigned char buf[25];
  24448. time_t t;
  24449. printf(testingFmt, "wolfSSL_X509_set_notAfter()");
  24450. /*
  24451. * Setup asn_time. APACHE HTTPD uses time(NULL)
  24452. */
  24453. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  24454. offset_day = 7;
  24455. /*
  24456. * Free these.
  24457. */
  24458. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  24459. AssertNotNull(asn_time);
  24460. AssertNotNull(x = X509_new());
  24461. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  24462. /*
  24463. * Tests
  24464. */
  24465. AssertTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  24466. /* time_check is simply (ANS1_TIME*)x->notAfter */
  24467. AssertNotNull(time_check = X509_get_notAfter(x));
  24468. /* ANS1_TIME_check validates by checking if argument can be parsed */
  24469. AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  24470. /* Convert to human readable format and compare to intended date */
  24471. AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
  24472. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  24473. AssertIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  24474. /*
  24475. * Cleanup
  24476. */
  24477. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  24478. X509_free(x);
  24479. BIO_free(bio);
  24480. printf(resultFmt, passed);
  24481. #endif
  24482. }
  24483. static void test_wolfSSL_X509_set_notBefore(void)
  24484. {
  24485. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  24486. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  24487. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  24488. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  24489. X509* x;
  24490. BIO* bio;
  24491. ASN1_TIME *asn_time, *time_check;
  24492. const int year = 365*24*60*60;
  24493. const int day = 24*60*60;
  24494. const int hour = 60*60;
  24495. const int mini = 60;
  24496. int offset_day;
  24497. unsigned char buf[25];
  24498. time_t t;
  24499. printf(testingFmt, "wolfSSL_X509_set_notBefore()");
  24500. /*
  24501. * Setup asn_time. APACHE HTTPD uses time(NULL)
  24502. */
  24503. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  24504. offset_day = 7;
  24505. /*
  24506. * Free these.
  24507. */
  24508. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  24509. AssertNotNull(asn_time);
  24510. AssertNotNull(x = X509_new());
  24511. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  24512. AssertIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  24513. /*
  24514. * Main Tests
  24515. */
  24516. AssertTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  24517. /* time_check == (ANS1_TIME*)x->notBefore */
  24518. AssertNotNull(time_check = X509_get_notBefore(x));
  24519. /* ANS1_TIME_check validates by checking if argument can be parsed */
  24520. AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  24521. /* Convert to human readable format and compare to intended date */
  24522. AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
  24523. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  24524. AssertIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  24525. /*
  24526. * Cleanup
  24527. */
  24528. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  24529. X509_free(x);
  24530. BIO_free(bio);
  24531. printf(resultFmt, passed);
  24532. #endif
  24533. }
  24534. static void test_wolfSSL_X509_set_version(void)
  24535. {
  24536. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  24537. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  24538. X509* x509;
  24539. long v = 2L;
  24540. long max = INT_MAX;
  24541. AssertNotNull(x509 = X509_new());
  24542. /* These should pass. */
  24543. AssertTrue(wolfSSL_X509_set_version(x509, v));
  24544. AssertIntEQ(v, wolfSSL_X509_get_version(x509));
  24545. /* Fail Case: When v(long) is greater than x509->version(int). */
  24546. v = max+1;
  24547. AssertFalse(wolfSSL_X509_set_version(x509, v));
  24548. /* Cleanup */
  24549. X509_free(x509);
  24550. printf(resultFmt, passed);
  24551. #endif
  24552. }
  24553. static void test_wolfSSL_BIO_gets(void)
  24554. {
  24555. #if defined(OPENSSL_EXTRA)
  24556. BIO* bio;
  24557. BIO* bio2;
  24558. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  24559. char emp[] = "";
  24560. char bio_buffer[20];
  24561. int bufferSz = 20;
  24562. printf(testingFmt, "wolfSSL_BIO_gets()");
  24563. /* try with bad args */
  24564. AssertNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  24565. /* try with real msg */
  24566. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  24567. XMEMSET(bio_buffer, 0, bufferSz);
  24568. AssertNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  24569. AssertNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  24570. AssertNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  24571. AssertFalse(bio2 != BIO_next(bio));
  24572. /* make buffer filled with no terminating characters */
  24573. XMEMSET(bio_buffer, 1, bufferSz);
  24574. /* BIO_gets reads a line of data */
  24575. AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  24576. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  24577. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  24578. AssertStrEQ(bio_buffer, "hello wolfSSL\n");
  24579. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  24580. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  24581. AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  24582. /* check not null terminated string */
  24583. BIO_free(bio);
  24584. msg[0] = 0x33;
  24585. msg[1] = 0x33;
  24586. msg[2] = 0x33;
  24587. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  24588. AssertIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  24589. AssertIntEQ(bio_buffer[0], msg[0]);
  24590. AssertIntEQ(bio_buffer[1], msg[1]);
  24591. AssertIntNE(bio_buffer[2], msg[2]);
  24592. BIO_free(bio);
  24593. msg[3] = 0x33;
  24594. bio_buffer[3] = 0x33;
  24595. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  24596. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  24597. AssertIntEQ(bio_buffer[0], msg[0]);
  24598. AssertIntEQ(bio_buffer[1], msg[1]);
  24599. AssertIntEQ(bio_buffer[2], msg[2]);
  24600. AssertIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  24601. /* check reading an empty string */
  24602. BIO_free(bio);
  24603. AssertNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  24604. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  24605. AssertStrEQ(emp, bio_buffer);
  24606. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  24607. /* check error cases */
  24608. BIO_free(bio);
  24609. AssertIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  24610. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  24611. AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  24612. #if !defined(NO_FILESYSTEM)
  24613. {
  24614. BIO* f_bio;
  24615. XFILE f;
  24616. AssertNotNull(f_bio = BIO_new(BIO_s_file()));
  24617. AssertIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  24618. f = XFOPEN(svrCertFile, "rb");
  24619. AssertTrue((f != XBADFILE));
  24620. AssertIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  24621. AssertIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  24622. BIO_free(f_bio);
  24623. }
  24624. #endif /* NO_FILESYSTEM */
  24625. BIO_free(bio);
  24626. BIO_free(bio2);
  24627. /* try with type BIO */
  24628. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  24629. sizeof(msg));
  24630. AssertNotNull(bio = BIO_new(BIO_s_bio()));
  24631. AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  24632. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  24633. AssertIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  24634. AssertIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  24635. AssertIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  24636. AssertIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  24637. AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  24638. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  24639. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  24640. AssertStrEQ(bio_buffer, "hello wolfSSL\n");
  24641. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  24642. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  24643. AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  24644. BIO_free(bio);
  24645. BIO_free(bio2);
  24646. /* check reading an empty string */
  24647. AssertNotNull(bio = BIO_new(BIO_s_bio()));
  24648. AssertIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  24649. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  24650. AssertStrEQ(emp, bio_buffer);
  24651. BIO_free(bio);
  24652. printf(resultFmt, passed);
  24653. #endif
  24654. }
  24655. static void test_wolfSSL_BIO_puts(void)
  24656. {
  24657. #if defined(OPENSSL_EXTRA)
  24658. BIO* bio;
  24659. char input[] = "hello\0world\n.....ok\n\0";
  24660. char output[128];
  24661. printf(testingFmt, "wolfSSL_BIO_puts()");
  24662. XMEMSET(output, 0, sizeof(output));
  24663. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  24664. AssertIntEQ(BIO_puts(bio, input), 5);
  24665. AssertIntEQ(BIO_pending(bio), 5);
  24666. AssertIntEQ(BIO_puts(bio, input + 6), 14);
  24667. AssertIntEQ(BIO_pending(bio), 19);
  24668. AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  24669. AssertStrEQ(output, "helloworld\n");
  24670. AssertIntEQ(BIO_pending(bio), 8);
  24671. AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  24672. AssertStrEQ(output, ".....ok\n");
  24673. AssertIntEQ(BIO_pending(bio), 0);
  24674. AssertIntEQ(BIO_puts(bio, ""), -1);
  24675. BIO_free(bio);
  24676. printf(resultFmt, passed);
  24677. #endif
  24678. }
  24679. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  24680. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  24681. defined(HAVE_IO_TESTS_DEPENDENCIES)
  24682. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  24683. {
  24684. (void)ssl;
  24685. (void)buf;
  24686. (void)sz;
  24687. (void)ctx;
  24688. return WOLFSSL_CBIO_ERR_WANT_READ;
  24689. }
  24690. #endif
  24691. static void test_wolfSSL_BIO_should_retry(void)
  24692. {
  24693. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  24694. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  24695. defined(HAVE_IO_TESTS_DEPENDENCIES)
  24696. tcp_ready ready;
  24697. func_args server_args;
  24698. THREAD_TYPE serverThread;
  24699. SOCKET_T sockfd = 0;
  24700. WOLFSSL_CTX* ctx;
  24701. WOLFSSL* ssl;
  24702. char msg[64] = "hello wolfssl!";
  24703. char reply[1024];
  24704. int msgSz = (int)XSTRLEN(msg);
  24705. int ret;
  24706. BIO* bio;
  24707. printf(testingFmt, "wolfSSL_BIO_should_retry()");
  24708. XMEMSET(&server_args, 0, sizeof(func_args));
  24709. #ifdef WOLFSSL_TIRTOS
  24710. fdOpenSession(Task_self());
  24711. #endif
  24712. StartTCP();
  24713. InitTcpReady(&ready);
  24714. #if defined(USE_WINDOWS_API)
  24715. /* use RNG to get random port if using windows */
  24716. ready.port = GetRandomPort();
  24717. #endif
  24718. server_args.signal = &ready;
  24719. start_thread(test_server_nofail, &server_args, &serverThread);
  24720. wait_tcp_ready(&server_args);
  24721. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  24722. AssertIntEQ(WOLFSSL_SUCCESS,
  24723. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  24724. AssertIntEQ(WOLFSSL_SUCCESS,
  24725. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  24726. AssertIntEQ(WOLFSSL_SUCCESS,
  24727. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  24728. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  24729. /* force retry */
  24730. ssl = wolfSSL_new(ctx);
  24731. AssertNotNull(ssl);
  24732. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  24733. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  24734. AssertNotNull(bio = BIO_new(BIO_f_ssl()));
  24735. BIO_set_ssl(bio, ssl, BIO_CLOSE);
  24736. AssertIntLE(BIO_write(bio, msg, msgSz), 0);
  24737. AssertIntNE(BIO_should_retry(bio), 0);
  24738. /* now perform successful connection */
  24739. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  24740. AssertIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  24741. BIO_read(bio, reply, sizeof(reply));
  24742. ret = wolfSSL_get_error(ssl, -1);
  24743. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  24744. AssertIntNE(BIO_should_retry(bio), 0);
  24745. }
  24746. else {
  24747. AssertIntEQ(BIO_should_retry(bio), 0);
  24748. }
  24749. AssertIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  24750. XSTRLEN("I hear you fa shizzle!")), 0);
  24751. BIO_free(bio);
  24752. wolfSSL_CTX_free(ctx);
  24753. join_thread(serverThread);
  24754. FreeTcpReady(&ready);
  24755. #ifdef WOLFSSL_TIRTOS
  24756. fdOpenSession(Task_self());
  24757. #endif
  24758. printf(resultFmt, passed);
  24759. #endif
  24760. }
  24761. static void test_wolfSSL_BIO_write(void)
  24762. {
  24763. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  24764. BIO* bio;
  24765. BIO* bio64;
  24766. BIO* ptr;
  24767. int sz;
  24768. char msg[] = "conversion test";
  24769. char out[40];
  24770. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  24771. BUF_MEM* buf = NULL;
  24772. printf(testingFmt, "wolfSSL_BIO_write()");
  24773. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  24774. AssertNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  24775. /* now should convert to base64 then write to memory */
  24776. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  24777. BIO_flush(bio);
  24778. /* test BIO chain */
  24779. AssertIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  24780. AssertNotNull(buf);
  24781. AssertIntEQ(buf->length, 25);
  24782. AssertNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  24783. sz = sizeof(out);
  24784. XMEMSET(out, 0, sz);
  24785. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  24786. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  24787. /* write then read should return the same message */
  24788. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  24789. sz = sizeof(out);
  24790. XMEMSET(out, 0, sz);
  24791. AssertIntEQ(BIO_read(bio, out, sz), 16);
  24792. AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  24793. /* now try encoding with no line ending */
  24794. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  24795. #ifdef HAVE_EX_DATA
  24796. BIO_set_ex_data(bio64, 0, (void*) "data");
  24797. AssertIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  24798. #endif
  24799. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  24800. BIO_flush(bio);
  24801. sz = sizeof(out);
  24802. XMEMSET(out, 0, sz);
  24803. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  24804. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  24805. BIO_free_all(bio); /* frees bio64 also */
  24806. /* test with more than one bio64 in list */
  24807. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  24808. AssertNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  24809. AssertNotNull(BIO_push(bio64, BIO_new(BIO_s_mem())));
  24810. /* now should convert to base64 when stored and then decode with read */
  24811. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  24812. BIO_flush(bio);
  24813. sz = sizeof(out);
  24814. XMEMSET(out, 0, sz);
  24815. AssertIntEQ((sz = BIO_read(bio, out, sz)), 16);
  24816. AssertIntEQ(XMEMCMP(out, msg, sz), 0);
  24817. BIO_clear_flags(bio64, ~0);
  24818. BIO_set_retry_read(bio);
  24819. BIO_free_all(bio); /* frees bio64s also */
  24820. printf(resultFmt, passed);
  24821. #endif
  24822. }
  24823. static void test_wolfSSL_BIO_printf(void)
  24824. {
  24825. #if defined(OPENSSL_ALL)
  24826. BIO* bio;
  24827. int sz = 7;
  24828. char msg[] = "TLS 1.3 for the world";
  24829. char out[60];
  24830. char expected[] = "TLS 1.3 for the world : sz = 7";
  24831. printf(testingFmt, "wolfSSL_BIO_printf()");
  24832. XMEMSET(out, 0, sizeof(out));
  24833. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  24834. AssertIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  24835. AssertIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  24836. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  24837. AssertIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  24838. BIO_free(bio);
  24839. printf(resultFmt, passed);
  24840. #endif
  24841. }
  24842. static void test_wolfSSL_BIO_f_md(void)
  24843. {
  24844. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  24845. BIO *bio, *mem;
  24846. char msg[] = "message to hash";
  24847. char out[60];
  24848. EVP_MD_CTX* ctx;
  24849. const unsigned char testKey[] =
  24850. {
  24851. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  24852. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  24853. 0x0b, 0x0b, 0x0b, 0x0b
  24854. };
  24855. const char testData[] = "Hi There";
  24856. const unsigned char testResult[] =
  24857. {
  24858. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  24859. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  24860. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  24861. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  24862. };
  24863. const unsigned char expectedHash[] =
  24864. {
  24865. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  24866. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  24867. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  24868. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  24869. };
  24870. const unsigned char emptyHash[] =
  24871. {
  24872. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  24873. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  24874. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  24875. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  24876. };
  24877. unsigned char check[sizeof(testResult) + 1];
  24878. size_t checkSz = -1;
  24879. EVP_PKEY* key;
  24880. printf(testingFmt, "wolfSSL_BIO_f_md()");
  24881. XMEMSET(out, 0, sizeof(out));
  24882. AssertNotNull(bio = BIO_new(BIO_f_md()));
  24883. AssertNotNull(mem = BIO_new(BIO_s_mem()));
  24884. AssertIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  24885. AssertIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  24886. /* should not be able to write/read yet since just digest wrapper and no
  24887. * data is passing through the bio */
  24888. AssertIntEQ(BIO_write(bio, msg, 0), 0);
  24889. AssertIntEQ(BIO_pending(bio), 0);
  24890. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  24891. AssertIntEQ(BIO_gets(bio, out, 3), 0);
  24892. AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  24893. AssertIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  24894. BIO_reset(bio);
  24895. /* append BIO mem to bio in order to read/write */
  24896. AssertNotNull(bio = BIO_push(bio, mem));
  24897. XMEMSET(out, 0, sizeof(out));
  24898. AssertIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  24899. AssertIntEQ(BIO_pending(bio), 16);
  24900. /* this just reads the message and does not hash it (gets calls final) */
  24901. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  24902. AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  24903. /* create a message digest using BIO */
  24904. XMEMSET(out, 0, sizeof(out));
  24905. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  24906. AssertIntEQ(BIO_pending(mem), 16);
  24907. AssertIntEQ(BIO_pending(bio), 16);
  24908. AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  24909. AssertIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  24910. BIO_free(bio);
  24911. BIO_free(mem);
  24912. /* test with HMAC */
  24913. XMEMSET(out, 0, sizeof(out));
  24914. AssertNotNull(bio = BIO_new(BIO_f_md()));
  24915. AssertNotNull(mem = BIO_new(BIO_s_mem()));
  24916. BIO_get_md_ctx(bio, &ctx);
  24917. AssertNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  24918. testKey, (int)sizeof(testKey)));
  24919. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  24920. AssertNotNull(bio = BIO_push(bio, mem));
  24921. BIO_write(bio, testData, (int)strlen(testData));
  24922. EVP_DigestSignFinal(ctx, NULL, &checkSz);
  24923. EVP_DigestSignFinal(ctx, check, &checkSz);
  24924. AssertIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  24925. EVP_PKEY_free(key);
  24926. BIO_free(bio);
  24927. BIO_free(mem);
  24928. printf(resultFmt, passed);
  24929. #endif
  24930. }
  24931. static void test_wolfSSL_SESSION(void)
  24932. {
  24933. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  24934. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  24935. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_SESSION_CACHE)
  24936. WOLFSSL* ssl;
  24937. WOLFSSL_CTX* ctx;
  24938. WOLFSSL_SESSION* sess;
  24939. WOLFSSL_SESSION* sess_copy;
  24940. unsigned char* sessDer = NULL;
  24941. unsigned char* ptr = NULL;
  24942. #ifdef OPENSSL_EXTRA
  24943. const unsigned char context[] = "user app context";
  24944. unsigned int contextSz = (unsigned int)sizeof(context);
  24945. #endif
  24946. int ret, err, sockfd, sz;
  24947. tcp_ready ready;
  24948. func_args server_args;
  24949. THREAD_TYPE serverThread;
  24950. char msg[80];
  24951. printf(testingFmt, "wolfSSL_SESSION()");
  24952. /* TLS v1.3 requires session tickets */
  24953. /* CHACHA and POLY1305 required for myTicketEncCb */
  24954. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  24955. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  24956. defined(HAVE_POLY1305)))
  24957. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  24958. #else
  24959. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  24960. #endif
  24961. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  24962. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  24963. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  24964. #ifdef WOLFSSL_ENCRYPTED_KEYS
  24965. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  24966. #endif
  24967. XMEMSET(&server_args, 0, sizeof(func_args));
  24968. #ifdef WOLFSSL_TIRTOS
  24969. fdOpenSession(Task_self());
  24970. #endif
  24971. StartTCP();
  24972. InitTcpReady(&ready);
  24973. #if defined(USE_WINDOWS_API)
  24974. /* use RNG to get random port if using windows */
  24975. ready.port = GetRandomPort();
  24976. #endif
  24977. server_args.signal = &ready;
  24978. start_thread(test_server_nofail, &server_args, &serverThread);
  24979. wait_tcp_ready(&server_args);
  24980. /* client connection */
  24981. ssl = wolfSSL_new(ctx);
  24982. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  24983. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), SSL_SUCCESS);
  24984. err = 0; /* Reset error */
  24985. do {
  24986. #ifdef WOLFSSL_ASYNC_CRYPT
  24987. if (err == WC_PENDING_E) {
  24988. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  24989. if (ret < 0) { break; } else if (ret == 0) { continue; }
  24990. }
  24991. #endif
  24992. ret = wolfSSL_connect(ssl);
  24993. if (ret != SSL_SUCCESS) {
  24994. err = wolfSSL_get_error(ssl, 0);
  24995. }
  24996. } while (ret != SSL_SUCCESS && err == WC_PENDING_E);
  24997. AssertIntEQ(ret, SSL_SUCCESS);
  24998. AssertIntEQ(wolfSSL_write(ssl, "GET", 3), 3);
  24999. AssertIntEQ(wolfSSL_read(ssl, msg, sizeof(msg)), 23);
  25000. sess = wolfSSL_get_session(ssl);
  25001. wolfSSL_shutdown(ssl);
  25002. wolfSSL_free(ssl);
  25003. join_thread(serverThread);
  25004. FreeTcpReady(&ready);
  25005. #ifdef WOLFSSL_TIRTOS
  25006. fdOpenSession(Task_self());
  25007. #endif
  25008. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  25009. {
  25010. X509 *x509;
  25011. char buf[30];
  25012. int bufSz;
  25013. AssertNotNull(x509 = SSL_SESSION_get0_peer(sess));
  25014. AssertIntGT((bufSz = X509_NAME_get_text_by_NID(
  25015. X509_get_subject_name(x509), NID_organizationalUnitName,
  25016. buf, sizeof(buf))), 0);
  25017. AssertIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  25018. if (bufSz == 7) {
  25019. AssertIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  25020. }
  25021. if (bufSz == 16) {
  25022. AssertIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  25023. }
  25024. }
  25025. #endif
  25026. AssertNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  25027. wolfSSL_SESSION_free(sess_copy);
  25028. /* get session from DER and update the timeout */
  25029. AssertIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  25030. AssertIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  25031. wolfSSL_SESSION_free(sess);
  25032. ptr = sessDer;
  25033. AssertNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  25034. AssertNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  25035. (const unsigned char**)&ptr, sz));
  25036. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  25037. AssertIntGT(wolfSSL_SESSION_get_time(sess), 0);
  25038. AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  25039. /* successful set session test */
  25040. AssertNotNull(ssl = wolfSSL_new(ctx));
  25041. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_SUCCESS);
  25042. #ifdef HAVE_SESSION_TICKET
  25043. /* Test set/get session ticket */
  25044. {
  25045. const char* ticket = "This is a session ticket";
  25046. char buf[64] = {0};
  25047. word32 bufSz = (word32)sizeof(buf);
  25048. AssertIntEQ(SSL_SUCCESS,
  25049. wolfSSL_set_SessionTicket(ssl, (byte *)ticket, (word32)XSTRLEN(ticket)));
  25050. AssertIntEQ(SSL_SUCCESS,
  25051. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  25052. AssertStrEQ(ticket, buf);
  25053. }
  25054. #endif
  25055. #ifdef OPENSSL_EXTRA
  25056. /* fail case with miss match session context IDs (use compatibility API) */
  25057. AssertIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  25058. SSL_SUCCESS);
  25059. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  25060. wolfSSL_free(ssl);
  25061. AssertIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  25062. SSL_FAILURE);
  25063. AssertIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  25064. SSL_SUCCESS);
  25065. AssertNotNull(ssl = wolfSSL_new(ctx));
  25066. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  25067. #endif
  25068. wolfSSL_free(ssl);
  25069. SSL_SESSION_free(sess);
  25070. wolfSSL_CTX_free(ctx);
  25071. printf(resultFmt, passed);
  25072. #endif
  25073. }
  25074. static void test_wolfSSL_d2i_PUBKEY(void)
  25075. {
  25076. #if defined(OPENSSL_EXTRA)
  25077. BIO* bio;
  25078. EVP_PKEY* pkey;
  25079. printf(testingFmt, "wolfSSL_d2i_PUBKEY()");
  25080. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  25081. AssertNull(d2i_PUBKEY_bio(NULL, NULL));
  25082. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  25083. /* RSA PUBKEY test */
  25084. AssertIntGT(BIO_write(bio, client_keypub_der_2048,
  25085. sizeof_client_keypub_der_2048), 0);
  25086. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  25087. EVP_PKEY_free(pkey);
  25088. #endif
  25089. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  25090. /* ECC PUBKEY test */
  25091. AssertIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  25092. sizeof_ecc_clikeypub_der_256), 0);
  25093. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  25094. EVP_PKEY_free(pkey);
  25095. #endif
  25096. BIO_free(bio);
  25097. (void)pkey;
  25098. printf(resultFmt, passed);
  25099. #endif
  25100. }
  25101. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  25102. static void test_wolfSSL_d2i_PrivateKeys_bio(void)
  25103. {
  25104. BIO* bio = NULL;
  25105. EVP_PKEY* pkey = NULL;
  25106. #ifndef NO_RSA
  25107. #endif
  25108. WOLFSSL_CTX* ctx;
  25109. #if defined(WOLFSSL_KEY_GEN)
  25110. unsigned char buff[4096];
  25111. unsigned char* bufPtr = buff;
  25112. #endif
  25113. printf(testingFmt, "wolfSSL_d2i_PrivateKeys_bio()");
  25114. /* test creating new EVP_PKEY with bad arg */
  25115. AssertNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  25116. /* test loading RSA key using BIO */
  25117. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  25118. {
  25119. XFILE file;
  25120. const char* fname = "./certs/server-key.der";
  25121. size_t sz;
  25122. byte* buf;
  25123. file = XFOPEN(fname, "rb");
  25124. AssertTrue((file != XBADFILE));
  25125. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  25126. sz = XFTELL(file);
  25127. XREWIND(file);
  25128. AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  25129. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  25130. XFCLOSE(file);
  25131. /* Test using BIO new mem and loading DER private key */
  25132. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  25133. AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  25134. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  25135. BIO_free(bio);
  25136. bio = NULL;
  25137. EVP_PKEY_free(pkey);
  25138. pkey = NULL;
  25139. }
  25140. #endif
  25141. /* test loading ECC key using BIO */
  25142. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  25143. {
  25144. XFILE file;
  25145. const char* fname = "./certs/ecc-key.der";
  25146. size_t sz;
  25147. byte* buf;
  25148. file = XFOPEN(fname, "rb");
  25149. AssertTrue((file != XBADFILE));
  25150. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  25151. sz = XFTELL(file);
  25152. XREWIND(file);
  25153. AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  25154. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  25155. XFCLOSE(file);
  25156. /* Test using BIO new mem and loading DER private key */
  25157. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  25158. AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  25159. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  25160. BIO_free(bio);
  25161. bio = NULL;
  25162. EVP_PKEY_free(pkey);
  25163. pkey = NULL;
  25164. }
  25165. #endif
  25166. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  25167. #ifndef NO_WOLFSSL_SERVER
  25168. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  25169. #else
  25170. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  25171. #endif
  25172. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  25173. !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  25174. {
  25175. RSA* rsa = NULL;
  25176. /* Tests bad parameters */
  25177. AssertNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  25178. /* RSA not set yet, expecting to fail*/
  25179. AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  25180. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  25181. /* set RSA using bio*/
  25182. AssertIntGT(BIO_write(bio, client_key_der_2048,
  25183. sizeof_client_key_der_2048), 0);
  25184. AssertNotNull(rsa = d2i_RSAPrivateKey_bio(bio, NULL));
  25185. AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  25186. /*i2d RSAprivate key tests */
  25187. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  25188. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  25189. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  25190. sizeof_client_key_der_2048);
  25191. bufPtr = NULL;
  25192. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  25193. sizeof_client_key_der_2048);
  25194. AssertNotNull(bufPtr);
  25195. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  25196. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  25197. RSA_free(rsa);
  25198. }
  25199. #endif /* !HAVE_FAST_RSA && WOLFSSL_KEY_GEN && !NO_RSA && !HAVE_USER_RSA*/
  25200. SSL_CTX_free(ctx);
  25201. ctx = NULL;
  25202. BIO_free(bio);
  25203. bio = NULL;
  25204. printf(resultFmt, passed);
  25205. }
  25206. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  25207. static void test_wolfSSL_sk_GENERAL_NAME(void)
  25208. {
  25209. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  25210. !defined(NO_RSA)
  25211. X509* x509;
  25212. GENERAL_NAME* gn;
  25213. unsigned char buf[4096];
  25214. const unsigned char* bufPt;
  25215. int bytes, i;
  25216. XFILE f;
  25217. STACK_OF(GENERAL_NAME)* sk;
  25218. printf(testingFmt, "wolfSSL_sk_GENERAL_NAME()");
  25219. f = XFOPEN(cliCertDerFileExt, "rb");
  25220. AssertTrue((f != XBADFILE));
  25221. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  25222. XFCLOSE(f);
  25223. bufPt = buf;
  25224. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  25225. AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  25226. NID_subject_alt_name, NULL, NULL));
  25227. AssertIntEQ(sk_GENERAL_NAME_num(sk), 1);
  25228. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  25229. AssertNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  25230. switch (gn->type) {
  25231. case GEN_DNS:
  25232. printf("found type GEN_DNS\n");
  25233. break;
  25234. case GEN_EMAIL:
  25235. printf("found type GEN_EMAIL\n");
  25236. break;
  25237. case GEN_URI:
  25238. printf("found type GEN_URI\n");
  25239. break;
  25240. }
  25241. }
  25242. X509_free(x509);
  25243. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  25244. printf(resultFmt, passed);
  25245. #endif
  25246. }
  25247. static void test_wolfSSL_MD4(void)
  25248. {
  25249. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  25250. MD4_CTX md4;
  25251. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  25252. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  25253. "789012345678901234567890";
  25254. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  25255. "\xcc\x05\x36";
  25256. int msgSz = (int)XSTRLEN(msg);
  25257. printf(testingFmt, "wolfSSL_MD4()");
  25258. XMEMSET(out, 0, sizeof(out));
  25259. MD4_Init(&md4);
  25260. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  25261. MD4_Final(out, &md4);
  25262. AssertIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  25263. printf(resultFmt, passed);
  25264. #endif
  25265. }
  25266. static void test_wolfSSL_RSA(void)
  25267. {
  25268. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  25269. defined(WOLFSSL_KEY_GEN)
  25270. RSA* rsa;
  25271. const BIGNUM *n;
  25272. const BIGNUM *e;
  25273. const BIGNUM *d;
  25274. printf(testingFmt, "wolfSSL_RSA()");
  25275. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  25276. AssertIntEQ(RSA_size(rsa), 256);
  25277. RSA_get0_key(rsa, &n, &e, &d);
  25278. AssertPtrEq(rsa->n, n);
  25279. AssertPtrEq(rsa->e, e);
  25280. AssertPtrEq(rsa->d, d);
  25281. AssertNotNull(n = BN_new());
  25282. AssertNotNull(e = BN_new());
  25283. AssertNotNull(d = BN_new());
  25284. AssertIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  25285. AssertPtrEq(rsa->n, n);
  25286. AssertPtrEq(rsa->e, e);
  25287. AssertPtrEq(rsa->d, d);
  25288. RSA_free(rsa);
  25289. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  25290. AssertNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  25291. AssertIntEQ(RSA_size(rsa), 384);
  25292. RSA_free(rsa);
  25293. #endif
  25294. /* remove for now with odd key size until adjusting rsa key size check with
  25295. wc_MakeRsaKey()
  25296. AssertNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  25297. RSA_free(rsa);
  25298. */
  25299. AssertNull(RSA_generate_key(-1, 3, NULL, NULL));
  25300. AssertNull(RSA_generate_key(511, 3, NULL, NULL)); /* RSA_MIN_SIZE - 1 */
  25301. AssertNull(RSA_generate_key(4097, 3, NULL, NULL)); /* RSA_MAX_SIZE + 1 */
  25302. AssertNull(RSA_generate_key(2048, 0, NULL, NULL));
  25303. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  25304. {
  25305. byte buff[FOURK_BUF];
  25306. byte der[FOURK_BUF];
  25307. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  25308. XFILE f;
  25309. int bytes;
  25310. /* test loading encrypted RSA private pem w/o password */
  25311. f = XFOPEN(PrivKeyPemFile, "rb");
  25312. AssertTrue((f != XBADFILE));
  25313. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  25314. XFCLOSE(f);
  25315. XMEMSET(der, 0, sizeof(der));
  25316. /* test that error value is returned with no password */
  25317. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""), 0);
  25318. }
  25319. #endif
  25320. printf(resultFmt, passed);
  25321. #endif
  25322. }
  25323. static void test_wolfSSL_RSA_DER(void)
  25324. {
  25325. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  25326. !defined(NO_RSA) && !defined(HAVE_USER_RSA) && defined(OPENSSL_EXTRA)
  25327. RSA *rsa;
  25328. int i;
  25329. const unsigned char *buff = NULL;
  25330. struct tbl_s
  25331. {
  25332. const unsigned char *der;
  25333. int sz;
  25334. } tbl[] = {
  25335. #ifdef USE_CERT_BUFFERS_1024
  25336. {client_key_der_1024, sizeof_client_key_der_1024},
  25337. {server_key_der_1024, sizeof_server_key_der_1024},
  25338. #endif
  25339. #ifdef USE_CERT_BUFFERS_2048
  25340. {client_key_der_2048, sizeof_client_key_der_2048},
  25341. {server_key_der_2048, sizeof_server_key_der_2048},
  25342. #endif
  25343. {NULL, 0}
  25344. };
  25345. /* Public Key DER */
  25346. struct tbl_s pub[] = {
  25347. #ifdef USE_CERT_BUFFERS_1024
  25348. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  25349. #endif
  25350. #ifdef USE_CERT_BUFFERS_2048
  25351. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  25352. #endif
  25353. {NULL, 0}
  25354. };
  25355. printf(testingFmt, "test_wolfSSL_RSA_DER()");
  25356. for (i = 0; tbl[i].der != NULL; i++)
  25357. {
  25358. AssertNotNull(d2i_RSAPublicKey(&rsa, &tbl[i].der, tbl[i].sz));
  25359. AssertNotNull(rsa);
  25360. RSA_free(rsa);
  25361. }
  25362. for (i = 0; tbl[i].der != NULL; i++)
  25363. {
  25364. AssertNotNull(d2i_RSAPrivateKey(&rsa, &tbl[i].der, tbl[i].sz));
  25365. AssertNotNull(rsa);
  25366. RSA_free(rsa);
  25367. }
  25368. for (i = 0; pub[i].der != NULL; i++)
  25369. {
  25370. AssertNotNull(d2i_RSAPublicKey(&rsa, &pub[i].der, pub[i].sz));
  25371. AssertNotNull(rsa);
  25372. AssertIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  25373. buff = NULL;
  25374. AssertIntEQ(i2d_RSAPublicKey(rsa, &buff), pub[i].sz);
  25375. AssertNotNull(buff);
  25376. AssertIntEQ(0, memcmp((void *)buff, (void *)pub[i].der, pub[i].sz));
  25377. XFREE((void *)buff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  25378. RSA_free(rsa);
  25379. }
  25380. printf(resultFmt, passed);
  25381. #endif
  25382. }
  25383. static void test_wolfSSL_RSA_get0_key(void)
  25384. {
  25385. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  25386. RSA *rsa = NULL;
  25387. const BIGNUM* n = NULL;
  25388. const BIGNUM* e = NULL;
  25389. const BIGNUM* d = NULL;
  25390. const unsigned char* der;
  25391. int derSz;
  25392. #ifdef USE_CERT_BUFFERS_1024
  25393. der = client_key_der_1024;
  25394. derSz = sizeof_client_key_der_1024;
  25395. #elif defined(USE_CERT_BUFFERS_2048)
  25396. der = client_key_der_2048;
  25397. derSz = sizeof_client_key_der_2048;
  25398. #else
  25399. der = NULL;
  25400. derSz = 0;
  25401. #endif
  25402. printf(testingFmt, "test_wolfSSL_RSA_get0_key()");
  25403. if (der != NULL) {
  25404. RSA_get0_key(NULL, NULL, NULL, NULL);
  25405. RSA_get0_key(rsa, NULL, NULL, NULL);
  25406. RSA_get0_key(NULL, &n, &e, &d);
  25407. AssertNull(n);
  25408. AssertNull(e);
  25409. AssertNull(d);
  25410. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  25411. AssertNotNull(rsa);
  25412. RSA_get0_key(rsa, NULL, NULL, NULL);
  25413. RSA_get0_key(rsa, &n, NULL, NULL);
  25414. AssertNotNull(n);
  25415. RSA_get0_key(rsa, NULL, &e, NULL);
  25416. AssertNotNull(e);
  25417. RSA_get0_key(rsa, NULL, NULL, &d);
  25418. AssertNotNull(d);
  25419. RSA_get0_key(rsa, &n, &e, &d);
  25420. AssertNotNull(n);
  25421. AssertNotNull(e);
  25422. AssertNotNull(d);
  25423. RSA_free(rsa);
  25424. }
  25425. printf(resultFmt, passed);
  25426. #endif
  25427. }
  25428. static void test_wolfSSL_RSA_meth(void)
  25429. {
  25430. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  25431. RSA *rsa;
  25432. RSA_METHOD *rsa_meth;
  25433. printf(testingFmt, "test_wolfSSL_RSA_meth");
  25434. #ifdef WOLFSSL_KEY_GEN
  25435. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  25436. RSA_free(rsa);
  25437. #else
  25438. AssertNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  25439. #endif
  25440. AssertNotNull(rsa_meth =
  25441. RSA_meth_new("placeholder RSA method", RSA_METHOD_FLAG_NO_CHECK));
  25442. #ifndef NO_WOLFSSL_STUB
  25443. AssertIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  25444. AssertIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  25445. AssertIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  25446. AssertIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  25447. AssertIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  25448. AssertIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  25449. AssertIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  25450. #endif
  25451. AssertNotNull(rsa = RSA_new());
  25452. AssertIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  25453. AssertPtrEq(RSA_get_method(rsa), rsa_meth);
  25454. AssertIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  25455. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  25456. AssertIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  25457. /* rsa_meth is freed here */
  25458. RSA_free(rsa);
  25459. printf(resultFmt, passed);
  25460. #endif
  25461. }
  25462. static void test_wolfSSL_verify_depth(void)
  25463. {
  25464. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  25465. WOLFSSL* ssl;
  25466. WOLFSSL_CTX* ctx;
  25467. long depth;
  25468. printf(testingFmt, "test_wolfSSL_verify_depth()");
  25469. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  25470. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  25471. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  25472. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  25473. AssertIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  25474. AssertNotNull(ssl = SSL_new(ctx));
  25475. AssertIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  25476. SSL_free(ssl);
  25477. SSL_CTX_set_verify_depth(ctx, -1);
  25478. AssertIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  25479. SSL_CTX_set_verify_depth(ctx, 2);
  25480. AssertIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  25481. AssertNotNull(ssl = SSL_new(ctx));
  25482. AssertIntEQ(2, SSL_get_verify_depth(ssl));
  25483. SSL_free(ssl);
  25484. SSL_CTX_free(ctx);
  25485. printf(resultFmt, passed);
  25486. #endif
  25487. }
  25488. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  25489. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  25490. * buffer of 64 bytes.
  25491. *
  25492. * returns the size of the digest buffer on success and a negative value on
  25493. * failure.
  25494. */
  25495. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest)
  25496. {
  25497. HMAC_CTX ctx1;
  25498. HMAC_CTX ctx2;
  25499. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  25500. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  25501. unsigned char long_key[] =
  25502. "0123456789012345678901234567890123456789"
  25503. "0123456789012345678901234567890123456789"
  25504. "0123456789012345678901234567890123456789"
  25505. "0123456789012345678901234567890123456789";
  25506. unsigned char msg[] = "message to hash";
  25507. unsigned int digestSz = 64;
  25508. int keySz = sizeof(key);
  25509. int long_keySz = sizeof(long_key);
  25510. int msgSz = sizeof(msg);
  25511. unsigned char digest2[64];
  25512. unsigned int digestSz2 = 64;
  25513. HMAC_CTX_init(&ctx1);
  25514. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  25515. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  25516. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  25517. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  25518. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  25519. HMAC_CTX_cleanup(&ctx1);
  25520. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  25521. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  25522. HMAC_CTX_cleanup(&ctx2);
  25523. AssertIntEQ(digestSz, digestSz2);
  25524. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  25525. /* test HMAC_Init with NULL key */
  25526. /* init after copy */
  25527. printf("test HMAC_Init with NULL key (0)\n");
  25528. HMAC_CTX_init(&ctx1);
  25529. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  25530. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  25531. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  25532. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  25533. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  25534. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  25535. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  25536. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  25537. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  25538. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  25539. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  25540. HMAC_CTX_cleanup(&ctx2);
  25541. AssertIntEQ(digestSz, digestSz2);
  25542. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  25543. /* long key */
  25544. printf("test HMAC_Init with NULL key (1)\n");
  25545. HMAC_CTX_init(&ctx1);
  25546. AssertIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type), SSL_SUCCESS);
  25547. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  25548. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  25549. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  25550. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  25551. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  25552. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  25553. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  25554. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  25555. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  25556. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  25557. HMAC_CTX_cleanup(&ctx2);
  25558. AssertIntEQ(digestSz, digestSz2);
  25559. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  25560. /* init before copy */
  25561. printf("test HMAC_Init with NULL key (2)\n");
  25562. HMAC_CTX_init(&ctx1);
  25563. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  25564. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  25565. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  25566. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  25567. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  25568. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  25569. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  25570. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  25571. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  25572. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  25573. HMAC_CTX_cleanup(&ctx1);
  25574. HMAC_CTX_cleanup(&ctx2);
  25575. AssertIntEQ(digestSz, digestSz2);
  25576. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  25577. return digestSz;
  25578. }
  25579. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  25580. static void test_wolfSSL_HMAC_CTX(void)
  25581. {
  25582. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  25583. unsigned char digest[64];
  25584. int digestSz;
  25585. printf(testingFmt, "wolfSSL_HMAC_CTX()");
  25586. #ifndef NO_SHA
  25587. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha1(), digest)), 20);
  25588. AssertIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  25589. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  25590. #endif /* !NO_SHA */
  25591. #ifdef WOLFSSL_SHA224
  25592. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha224(), digest)), 28);
  25593. AssertIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  25594. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  25595. "\x02\x0E", digest, digestSz), 0);
  25596. #endif /* WOLFSSL_SHA224 */
  25597. #ifndef NO_SHA256
  25598. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha256(), digest)), 32);
  25599. AssertIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  25600. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  25601. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  25602. #endif /* !NO_SHA256 */
  25603. #ifdef WOLFSSL_SHA384
  25604. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha384(), digest)), 48);
  25605. AssertIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  25606. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  25607. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  25608. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  25609. digest, digestSz), 0);
  25610. #endif /* WOLFSSL_SHA384 */
  25611. #ifdef WOLFSSL_SHA512
  25612. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha512(), digest)), 64);
  25613. AssertIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  25614. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  25615. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  25616. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  25617. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  25618. digest, digestSz), 0);
  25619. #endif /* WOLFSSL_SHA512 */
  25620. #ifndef NO_MD5
  25621. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_md5(), digest)), 16);
  25622. AssertIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  25623. "\xE4\x98\xDD", digest, digestSz), 0);
  25624. #endif /* !NO_MD5 */
  25625. printf(resultFmt, passed);
  25626. #endif
  25627. }
  25628. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  25629. static void sslMsgCb(int w, int version, int type, const void* buf,
  25630. size_t sz, SSL* ssl, void* arg)
  25631. {
  25632. int i;
  25633. unsigned char* pt = (unsigned char*)buf;
  25634. printf("%s %d bytes of version %d , type %d : ", (w)?"Writing":"Reading",
  25635. (int)sz, version, type);
  25636. for (i = 0; i < (int)sz; i++) printf("%02X", pt[i]);
  25637. printf("\n");
  25638. (void)ssl;
  25639. (void)arg;
  25640. }
  25641. #endif /* OPENSSL_EXTRA */
  25642. static void test_wolfSSL_msg_callback(void)
  25643. {
  25644. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  25645. WOLFSSL* ssl;
  25646. WOLFSSL_CTX* ctx;
  25647. printf(testingFmt, "wolfSSL_msg_callback()");
  25648. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  25649. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  25650. SSL_FILETYPE_PEM));
  25651. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  25652. SSL_FILETYPE_PEM));
  25653. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  25654. SSL_SUCCESS);
  25655. AssertNotNull(ssl = SSL_new(ctx));
  25656. AssertIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  25657. AssertIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  25658. AssertIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  25659. SSL_free(ssl);
  25660. SSL_CTX_free(ctx);
  25661. printf(resultFmt, passed);
  25662. #endif
  25663. }
  25664. static void test_wolfSSL_SHA(void)
  25665. {
  25666. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  25667. printf(testingFmt, "wolfSSL_SHA()");
  25668. #if !defined(NO_SHA)
  25669. {
  25670. const unsigned char in[] = "abc";
  25671. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  25672. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  25673. unsigned char out[WC_SHA_DIGEST_SIZE];
  25674. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  25675. AssertNotNull(SHA1(in, XSTRLEN((char*)in), out));
  25676. AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  25677. }
  25678. #endif
  25679. #if !defined(NO_SHA256)
  25680. {
  25681. const unsigned char in[] = "abc";
  25682. unsigned char expected[] = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  25683. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  25684. "\x15\xAD";
  25685. unsigned char out[WC_SHA256_DIGEST_SIZE];
  25686. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  25687. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  25688. AssertNotNull(SHA256(in, XSTRLEN((char*)in), out));
  25689. #else
  25690. AssertNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  25691. #endif
  25692. AssertIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  25693. }
  25694. #endif
  25695. #if defined(WOLFSSL_SHA384)
  25696. {
  25697. const unsigned char in[] = "abc";
  25698. unsigned char expected[] = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  25699. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  25700. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  25701. "\xc8\x25\xa7";
  25702. unsigned char out[WC_SHA384_DIGEST_SIZE];
  25703. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  25704. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  25705. AssertNotNull(SHA384(in, XSTRLEN((char*)in), out));
  25706. #else
  25707. AssertNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  25708. #endif
  25709. AssertIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  25710. }
  25711. #endif
  25712. #if defined(WOLFSSL_SHA512)
  25713. {
  25714. const unsigned char in[] = "abc";
  25715. unsigned char expected[] = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  25716. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  25717. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  25718. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  25719. "\xa5\x4c\xa4\x9f";
  25720. unsigned char out[WC_SHA512_DIGEST_SIZE];
  25721. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  25722. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  25723. AssertNotNull(SHA512(in, XSTRLEN((char*)in), out));
  25724. #else
  25725. AssertNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  25726. #endif
  25727. AssertIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  25728. }
  25729. #endif
  25730. printf(resultFmt, passed);
  25731. #endif
  25732. }
  25733. static void test_wolfSSL_DH_1536_prime(void)
  25734. {
  25735. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  25736. BIGNUM* bn;
  25737. unsigned char bits[200];
  25738. int sz = 192; /* known binary size */
  25739. const byte expected[] = {
  25740. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  25741. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  25742. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  25743. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  25744. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  25745. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  25746. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  25747. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  25748. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  25749. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  25750. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  25751. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  25752. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  25753. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  25754. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  25755. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  25756. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  25757. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  25758. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  25759. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  25760. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  25761. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  25762. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  25763. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  25764. };
  25765. printf(testingFmt, "wolfSSL_DH_1536_prime()");
  25766. bn = get_rfc3526_prime_1536(NULL);
  25767. AssertNotNull(bn);
  25768. AssertIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  25769. AssertIntEQ(0, XMEMCMP(expected, bits, sz));
  25770. BN_free(bn);
  25771. printf(resultFmt, passed);
  25772. #endif
  25773. }
  25774. static void test_wolfSSL_PEM_write_DHparams(void)
  25775. {
  25776. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  25777. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && !defined(NO_FILESYSTEM)
  25778. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  25779. DH* dh;
  25780. BIO* bio;
  25781. XFILE fp;
  25782. byte pem[2048];
  25783. int pemSz;
  25784. const char expected[] =
  25785. "-----BEGIN DH PARAMETERS-----\n\
  25786. MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n\
  25787. v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n\
  25788. nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n\
  25789. joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n\
  25790. wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n\
  25791. tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n\
  25792. -----END DH PARAMETERS-----\n";
  25793. printf(testingFmt, "wolfSSL_PEM_write_DHparams()");
  25794. AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
  25795. AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  25796. XFCLOSE(fp);
  25797. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  25798. AssertIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  25799. AssertNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  25800. BIO_free(bio);
  25801. AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  25802. AssertIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  25803. AssertIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  25804. XFCLOSE(fp);
  25805. DH_free(dh);
  25806. /* check results */
  25807. XMEMSET(pem, 0, sizeof(pem));
  25808. AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "rb"));
  25809. AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  25810. AssertIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  25811. XFCLOSE(fp);
  25812. printf(resultFmt, passed);
  25813. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  25814. #endif /* OPENSSL_ALL || OPENSSL_QT */
  25815. #endif
  25816. }
  25817. static void test_wolfSSL_AES_ecb_encrypt(void)
  25818. {
  25819. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB)
  25820. AES_KEY aes;
  25821. const byte msg[] =
  25822. {
  25823. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  25824. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  25825. };
  25826. const byte verify[] =
  25827. {
  25828. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  25829. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  25830. };
  25831. const byte key[] =
  25832. {
  25833. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  25834. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  25835. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  25836. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  25837. };
  25838. byte out[AES_BLOCK_SIZE];
  25839. printf(testingFmt, "wolfSSL_AES_ecb_encrypt()");
  25840. AssertIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  25841. XMEMSET(out, 0, AES_BLOCK_SIZE);
  25842. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  25843. AssertIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  25844. #ifdef HAVE_AES_DECRYPT
  25845. AssertIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  25846. XMEMSET(out, 0, AES_BLOCK_SIZE);
  25847. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  25848. AssertIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  25849. #endif
  25850. /* test bad arguments */
  25851. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  25852. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  25853. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  25854. printf(resultFmt, passed);
  25855. #endif
  25856. }
  25857. static void test_wolfSSL_SHA256(void)
  25858. {
  25859. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  25860. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  25861. unsigned char input[] =
  25862. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  25863. unsigned char output[] =
  25864. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  25865. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  25866. "\x06\xC1";
  25867. size_t inLen;
  25868. byte hash[WC_SHA256_DIGEST_SIZE];
  25869. printf(testingFmt, "wolfSSL_SHA256()");
  25870. inLen = XSTRLEN((char*)input);
  25871. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  25872. AssertNotNull(SHA256(input, inLen, hash));
  25873. AssertIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  25874. printf(resultFmt, passed);
  25875. #endif
  25876. }
  25877. static void test_wolfSSL_X509_get_serialNumber(void)
  25878. {
  25879. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  25880. ASN1_INTEGER* a;
  25881. BIGNUM* bn;
  25882. X509* x509;
  25883. char *serialHex;
  25884. printf(testingFmt, "wolfSSL_X509_get_serialNumber()");
  25885. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  25886. SSL_FILETYPE_PEM));
  25887. AssertNotNull(a = X509_get_serialNumber(x509));
  25888. /* check on value of ASN1 Integer */
  25889. AssertNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  25890. X509_free(x509); /* free's a */
  25891. AssertNotNull(serialHex = BN_bn2hex(bn));
  25892. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  25893. AssertStrEQ(serialHex, "01");
  25894. #else
  25895. AssertStrEQ(serialHex, "1");
  25896. #endif
  25897. OPENSSL_free(serialHex);
  25898. AssertIntEQ(BN_get_word(bn), 1);
  25899. BN_free(bn);
  25900. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  25901. a = ASN1_INTEGER_new();
  25902. if (a) {
  25903. AssertNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  25904. DYNAMIC_TYPE_OPENSSL));
  25905. a->isDynamic = 1;
  25906. ASN1_INTEGER_free(a);
  25907. }
  25908. printf(resultFmt, passed);
  25909. #endif
  25910. }
  25911. static void test_wolfSSL_OpenSSL_add_all_algorithms(void){
  25912. #if defined(OPENSSL_EXTRA)
  25913. printf(testingFmt, "wolfSSL_OpenSSL_add_all_algorithms()");
  25914. AssertIntEQ(wolfSSL_add_all_algorithms(),WOLFSSL_SUCCESS);
  25915. wolfSSL_Cleanup();
  25916. AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(),WOLFSSL_SUCCESS);
  25917. wolfSSL_Cleanup();
  25918. AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(),WOLFSSL_SUCCESS);
  25919. wolfSSL_Cleanup();
  25920. printf(resultFmt, passed);
  25921. #endif
  25922. }
  25923. static void test_wolfSSL_ASN1_STRING_print_ex(void){
  25924. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  25925. ASN1_STRING* asn_str;
  25926. const char data[] = "Hello wolfSSL!";
  25927. ASN1_STRING* esc_str;
  25928. const char esc_data[] = "a+;<>";
  25929. BIO *bio;
  25930. unsigned long flags;
  25931. int p_len;
  25932. unsigned char rbuf[255];
  25933. printf(testingFmt, "wolfSSL_ASN1_STRING_print_ex()");
  25934. /* setup */
  25935. XMEMSET(rbuf, 0, 255);
  25936. bio = BIO_new(BIO_s_mem());
  25937. BIO_set_write_buf_size(bio,255);
  25938. asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  25939. ASN1_STRING_set(asn_str, (const void*)data, sizeof(data));
  25940. esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  25941. ASN1_STRING_set(esc_str, (const void*)esc_data, sizeof(esc_data));
  25942. /* no flags */
  25943. XMEMSET(rbuf, 0, 255);
  25944. flags = 0;
  25945. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  25946. AssertIntEQ(p_len, 15);
  25947. BIO_read(bio, (void*)rbuf, 15);
  25948. AssertStrEQ((char*)rbuf, "Hello wolfSSL!");
  25949. /* RFC2253 Escape */
  25950. XMEMSET(rbuf, 0, 255);
  25951. flags = ASN1_STRFLGS_ESC_2253;
  25952. p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags);
  25953. AssertIntEQ(p_len, 9);
  25954. BIO_read(bio, (void*)rbuf, 9);
  25955. AssertStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  25956. /* Show type */
  25957. XMEMSET(rbuf, 0, 255);
  25958. flags = ASN1_STRFLGS_SHOW_TYPE;
  25959. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  25960. AssertIntEQ(p_len, 28);
  25961. BIO_read(bio, (void*)rbuf, 28);
  25962. AssertStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  25963. /* Dump All */
  25964. XMEMSET(rbuf, 0, 255);
  25965. flags = ASN1_STRFLGS_DUMP_ALL;
  25966. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  25967. AssertIntEQ(p_len, 31);
  25968. BIO_read(bio, (void*)rbuf, 31);
  25969. AssertStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  25970. /* Dump Der */
  25971. XMEMSET(rbuf, 0, 255);
  25972. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  25973. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  25974. AssertIntEQ(p_len, 35);
  25975. BIO_read(bio, (void*)rbuf, 35);
  25976. AssertStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  25977. /* Dump All + Show type */
  25978. XMEMSET(rbuf, 0, 255);
  25979. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  25980. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  25981. AssertIntEQ(p_len, 44);
  25982. BIO_read(bio, (void*)rbuf, 44);
  25983. AssertStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  25984. BIO_free(bio);
  25985. ASN1_STRING_free(asn_str);
  25986. ASN1_STRING_free(esc_str);
  25987. printf(resultFmt, passed);
  25988. #endif
  25989. }
  25990. static void test_wolfSSL_ASN1_TIME_to_generalizedtime(void){
  25991. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  25992. WOLFSSL_ASN1_TIME *t;
  25993. WOLFSSL_ASN1_TIME *out;
  25994. WOLFSSL_ASN1_TIME *gtime;
  25995. int tlen = 0;
  25996. unsigned char *data;
  25997. printf(testingFmt, "wolfSSL_ASN1_TIME_to_generalizedtime()");
  25998. /* UTC Time test */
  25999. AssertNotNull(t = wolfSSL_ASN1_TIME_new());
  26000. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  26001. AssertNotNull(out = wolfSSL_ASN1_TIME_new());
  26002. t->type = ASN_UTC_TIME;
  26003. t->length = ASN_UTC_TIME_SIZE;
  26004. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  26005. tlen = wolfSSL_ASN1_TIME_get_length(t);
  26006. AssertIntEQ(tlen, ASN_UTC_TIME_SIZE);
  26007. data = wolfSSL_ASN1_TIME_get_data(t);
  26008. AssertStrEQ((char*)data, "050727123456Z");
  26009. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  26010. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  26011. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  26012. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  26013. /* Generalized Time test */
  26014. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  26015. XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE);
  26016. XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE);
  26017. t->type = ASN_GENERALIZED_TIME;
  26018. t->length = ASN_GENERALIZED_TIME_SIZE;
  26019. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  26020. tlen = wolfSSL_ASN1_TIME_get_length(t);
  26021. AssertIntEQ(tlen, ASN_GENERALIZED_TIME_SIZE);
  26022. data = wolfSSL_ASN1_TIME_get_data(t);
  26023. AssertStrEQ((char*)data, "20050727123456Z");
  26024. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  26025. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  26026. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  26027. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  26028. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26029. /* Null parameter test */
  26030. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  26031. gtime = NULL;
  26032. out = NULL;
  26033. t->type = ASN_UTC_TIME;
  26034. t->length = ASN_UTC_TIME_SIZE;
  26035. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  26036. AssertNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  26037. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  26038. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  26039. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  26040. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26041. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26042. printf(resultFmt, passed);
  26043. #endif
  26044. }
  26045. static void test_wolfSSL_X509_CA_num(void){
  26046. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  26047. defined(HAVE_ECC) && !defined(NO_RSA)
  26048. WOLFSSL_X509_STORE *store;
  26049. WOLFSSL_X509 *x509_1, *x509_2;
  26050. int ca_num = 0;
  26051. printf(testingFmt, "wolfSSL_X509_CA_num()");
  26052. store = wolfSSL_X509_STORE_new();
  26053. x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  26054. wolfSSL_X509_STORE_add_cert(store, x509_1);
  26055. ca_num = wolfSSL_X509_CA_num(store);
  26056. AssertIntEQ(ca_num, 1);
  26057. x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile, WOLFSSL_FILETYPE_PEM);
  26058. wolfSSL_X509_STORE_add_cert(store, x509_2);
  26059. ca_num = wolfSSL_X509_CA_num(store);
  26060. AssertIntEQ(ca_num, 2);
  26061. wolfSSL_X509_free(x509_1);
  26062. wolfSSL_X509_free(x509_2);
  26063. wolfSSL_X509_STORE_free(store);
  26064. printf(resultFmt, passed);
  26065. #endif
  26066. }
  26067. static void test_wolfSSL_X509_check_ca(void){
  26068. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  26069. WOLFSSL_X509 *x509;
  26070. printf(testingFmt, "wolfSSL_X509_check_ca()");
  26071. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  26072. AssertIntEQ(wolfSSL_X509_check_ca(x509), 1);
  26073. wolfSSL_X509_free(x509);
  26074. x509 = wolfSSL_X509_load_certificate_file(ntruCertFile, WOLFSSL_FILETYPE_PEM);
  26075. AssertIntEQ(wolfSSL_X509_check_ca(x509), 0);
  26076. wolfSSL_X509_free(x509);
  26077. printf(resultFmt, passed);
  26078. #endif
  26079. }
  26080. static void test_wolfSSL_X509_check_ip_asc(void){
  26081. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  26082. WOLFSSL_X509 *x509;
  26083. printf(testingFmt, "wolfSSL_X509_check_ip_asc()");
  26084. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  26085. #if 0
  26086. /* TODO: add cert gen for testing positive case */
  26087. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  26088. #endif
  26089. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  26090. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  26091. wolfSSL_X509_free(x509);
  26092. printf(resultFmt, passed);
  26093. #endif
  26094. }
  26095. static void test_wolfSSL_DC_cert(void)
  26096. {
  26097. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  26098. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_KEY_GEN) && \
  26099. defined(WOLFSSL_CERT_EXT)
  26100. Cert cert;
  26101. RsaKey key;
  26102. WC_RNG rng;
  26103. byte der[FOURK_BUF];
  26104. int certSz;
  26105. int ret, idx;
  26106. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  26107. const unsigned char* pt;
  26108. X509* x509;
  26109. X509_NAME* x509name;
  26110. X509_NAME_ENTRY* entry;
  26111. ASN1_STRING* entryValue;
  26112. CertName name;
  26113. printf(testingFmt, "wolfSSL Certs with DC");
  26114. XMEMSET(&name, 0, sizeof(CertName));
  26115. /* set up cert name */
  26116. XMEMCPY(name.country, "US", sizeof("US"));
  26117. name.countryEnc = CTC_PRINTABLE;
  26118. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  26119. name.stateEnc = CTC_UTF8;
  26120. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  26121. name.localityEnc = CTC_UTF8;
  26122. XMEMCPY(name.sur, "Test", sizeof("Test"));
  26123. name.surEnc = CTC_UTF8;
  26124. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  26125. name.orgEnc = CTC_UTF8;
  26126. XMEMCPY(name.unit, "Development", sizeof("Development"));
  26127. name.unitEnc = CTC_UTF8;
  26128. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  26129. name.commonNameEnc = CTC_UTF8;
  26130. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  26131. name.serialDevEnc = CTC_PRINTABLE;
  26132. #ifdef WOLFSSL_MULTI_ATTRIB
  26133. #if CTC_MAX_ATTRIB > 2
  26134. {
  26135. NameAttrib* n;
  26136. n = &name.name[0];
  26137. n->id = ASN_DOMAIN_COMPONENT;
  26138. n->type = CTC_UTF8;
  26139. n->sz = sizeof("com");
  26140. XMEMCPY(n->value, "com", sizeof("com"));
  26141. n = &name.name[1];
  26142. n->id = ASN_DOMAIN_COMPONENT;
  26143. n->type = CTC_UTF8;
  26144. n->sz = sizeof("wolfssl");
  26145. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  26146. }
  26147. #endif
  26148. #endif /* WOLFSSL_MULTI_ATTRIB */
  26149. AssertIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  26150. #ifndef HAVE_FIPS
  26151. AssertIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, devId), 0);
  26152. #else
  26153. AssertIntEQ(wc_InitRng(&rng), 0);
  26154. #endif
  26155. AssertIntEQ(wc_MakeRsaKey(&key, 1024, 3, &rng), 0);
  26156. XMEMSET(&cert, 0 , sizeof(Cert));
  26157. AssertIntEQ(wc_InitCert(&cert), 0);
  26158. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  26159. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  26160. cert.serialSz = (int)sizeof(mySerial);
  26161. cert.isCA = 1;
  26162. #ifndef NO_SHA256
  26163. cert.sigType = CTC_SHA256wRSA;
  26164. #else
  26165. cert.sigType = CTC_SHAwRSA;
  26166. #endif
  26167. /* add SKID from the Public Key */
  26168. AssertIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  26169. /* add AKID from the Public Key */
  26170. AssertIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  26171. ret = 0;
  26172. do {
  26173. #if defined(WOLFSSL_ASYNC_CRYPT)
  26174. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26175. #endif
  26176. if (ret >= 0) {
  26177. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  26178. }
  26179. } while (ret == WC_PENDING_E);
  26180. AssertIntGT(ret, 0);
  26181. certSz = ret;
  26182. /* der holds a certificate with DC's now check X509 parsing of it */
  26183. pt = der;
  26184. AssertNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  26185. AssertNotNull(x509name = X509_get_subject_name(x509));
  26186. #ifdef WOLFSSL_MULTI_ATTRIB
  26187. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  26188. -1)), 5);
  26189. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  26190. idx)), 6);
  26191. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  26192. idx)), -1);
  26193. #endif /* WOLFSSL_MULTI_ATTRIB */
  26194. /* compare DN at index 0 */
  26195. AssertNotNull(entry = X509_NAME_get_entry(x509name, 0));
  26196. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  26197. AssertIntEQ(ASN1_STRING_length(entryValue), 2);
  26198. AssertStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  26199. #ifdef WOLFSSL_MULTI_ATTRIB
  26200. /* get first and second DC and compare result */
  26201. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  26202. -1)), 5);
  26203. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  26204. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  26205. AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  26206. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  26207. idx)), 6);
  26208. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  26209. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  26210. AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  26211. #endif /* WOLFSSL_MULTI_ATTRIB */
  26212. /* try invalid index locations for regression test and sanity check */
  26213. AssertNull(entry = X509_NAME_get_entry(x509name, 11));
  26214. AssertNull(entry = X509_NAME_get_entry(x509name, 20));
  26215. (void)idx;
  26216. X509_free(x509);
  26217. wc_FreeRsaKey(&key);
  26218. wc_FreeRng(&rng);
  26219. printf(resultFmt, passed);
  26220. #endif
  26221. }
  26222. static void test_wolfSSL_X509_get_version(void){
  26223. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  26224. WOLFSSL_X509 *x509;
  26225. printf(testingFmt, "wolfSSL_X509_get_version()");
  26226. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  26227. AssertNotNull(x509);
  26228. AssertIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  26229. wolfSSL_X509_free(x509);
  26230. printf(resultFmt, passed);
  26231. #endif
  26232. }
  26233. static void test_wolfSSL_DES_ncbc(void){
  26234. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  26235. const_DES_cblock myDes;
  26236. DES_cblock iv = {1};
  26237. DES_key_schedule key = {0};
  26238. unsigned char msg[] = "hello wolfssl";
  26239. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  26240. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  26241. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  26242. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  26243. printf(testingFmt, "wolfSSL_DES_ncbc()");
  26244. /* partial block test */
  26245. DES_set_key(&key, &myDes);
  26246. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  26247. AssertIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  26248. AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  26249. DES_set_key(&key, &myDes);
  26250. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  26251. *((byte*)&iv) = 1;
  26252. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  26253. AssertIntEQ(XMEMCMP(msg, pln, 3), 0);
  26254. AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  26255. /* full block test */
  26256. DES_set_key(&key, &myDes);
  26257. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  26258. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  26259. *((byte*)&iv) = 1;
  26260. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  26261. AssertIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  26262. AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  26263. DES_set_key(&key, &myDes);
  26264. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  26265. *((byte*)&iv) = 1;
  26266. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  26267. AssertIntEQ(XMEMCMP(msg, pln, 8), 0);
  26268. AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  26269. printf(resultFmt, passed);
  26270. #endif
  26271. }
  26272. static void test_wolfSSL_AES_cbc_encrypt()
  26273. {
  26274. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA)
  26275. AES_KEY aes;
  26276. AES_KEY* aesN = NULL;
  26277. size_t len = 0;
  26278. size_t lenB = 0;
  26279. int keySz0 = 0;
  26280. int keySzN = -1;
  26281. byte out[AES_BLOCK_SIZE] = {0};
  26282. byte* outN = NULL;
  26283. const int enc1 = AES_ENCRYPT;
  26284. const int enc2 = AES_DECRYPT;
  26285. /* Test vectors retrieved from:
  26286. * <begin URL>
  26287. * https://csrc.nist.gov/
  26288. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  26289. * documents/aes/KAT_AES.zip
  26290. * </end URL>
  26291. */
  26292. const byte* pt128N = NULL;
  26293. byte* key128N = NULL;
  26294. byte* iv128N = NULL;
  26295. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  26296. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26297. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  26298. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  26299. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  26300. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26301. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  26302. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  26303. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  26304. len = sizeof(pt128);
  26305. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  26306. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  26307. AssertIntNE(XMEMCMP(b, g, h), i)
  26308. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  26309. printf(testingFmt, "Stressing wolfSSL_AES_cbc_encrypt()");
  26310. STRESS_T(pt128N, out, len, &aes, iv128tmp, enc1, ct128, AES_BLOCK_SIZE, 0);
  26311. STRESS_T(pt128, out, len, &aes, iv128N, enc1, ct128, AES_BLOCK_SIZE, 0);
  26312. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, enc1);
  26313. AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  26314. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, enc1);
  26315. AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  26316. STRESS_T(pt128, out, lenB, &aes, iv128tmp, enc1, ct128, AES_BLOCK_SIZE, 0);
  26317. printf(resultFmt, "Stress Tests: passed");
  26318. printf(testingFmt, "Stressing wolfSSL_AES_set_encrypt_key");
  26319. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  26320. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  26321. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  26322. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  26323. printf(resultFmt, "Stress Tests: passed");
  26324. printf(testingFmt, "Stressing wolfSSL_AES_set_decrypt_key");
  26325. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  26326. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  26327. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  26328. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  26329. printf(resultFmt, "Stress Tests: passed");
  26330. #ifdef WOLFSSL_AES_128
  26331. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 128-bit");
  26332. XMEMSET(out, 0, AES_BLOCK_SIZE);
  26333. RESET_IV(iv128tmp, iv128);
  26334. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  26335. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, enc1);
  26336. AssertIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  26337. printf(resultFmt, "passed");
  26338. #ifdef HAVE_AES_DECRYPT
  26339. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode");
  26340. XMEMSET(out, 0, AES_BLOCK_SIZE);
  26341. RESET_IV(iv128tmp, iv128);
  26342. len = sizeof(ct128);
  26343. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  26344. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, enc2);
  26345. AssertIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  26346. printf(resultFmt, "passed");
  26347. #endif
  26348. #endif /* WOLFSSL_AES_128 */
  26349. #ifdef WOLFSSL_AES_192
  26350. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  26351. * Appendix F.2.3 */
  26352. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  26353. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  26354. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  26355. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  26356. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  26357. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  26358. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  26359. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  26360. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  26361. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  26362. len = sizeof(pt192);
  26363. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 192-bit");
  26364. XMEMSET(out, 0, AES_BLOCK_SIZE);
  26365. RESET_IV(iv192tmp, iv192);
  26366. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  26367. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, enc1);
  26368. AssertIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  26369. printf(resultFmt, "passed");
  26370. #ifdef HAVE_AES_DECRYPT
  26371. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode");
  26372. len = sizeof(ct192);
  26373. RESET_IV(iv192tmp, iv192);
  26374. XMEMSET(out, 0, AES_BLOCK_SIZE);
  26375. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  26376. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, enc2);
  26377. AssertIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  26378. printf(resultFmt, "passed");
  26379. #endif
  26380. #endif /* WOLFSSL_AES_192 */
  26381. #ifdef WOLFSSL_AES_256
  26382. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  26383. * Appendix F.2.5 */
  26384. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  26385. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  26386. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  26387. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  26388. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  26389. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  26390. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  26391. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  26392. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  26393. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  26394. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  26395. len = sizeof(pt256);
  26396. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 256-bit");
  26397. XMEMSET(out, 0, AES_BLOCK_SIZE);
  26398. RESET_IV(iv256tmp, iv256);
  26399. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  26400. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, enc1);
  26401. AssertIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  26402. printf(resultFmt, "passed");
  26403. #ifdef HAVE_AES_DECRYPT
  26404. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode");
  26405. len = sizeof(ct256);
  26406. RESET_IV(iv256tmp, iv256);
  26407. XMEMSET(out, 0, AES_BLOCK_SIZE);
  26408. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  26409. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, enc2);
  26410. AssertIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  26411. printf(resultFmt, "passed");
  26412. #endif
  26413. #endif /* WOLFSSL_AES_256 */
  26414. #endif
  26415. }
  26416. #if defined(OPENSSL_ALL)
  26417. #if !defined(NO_ASN)
  26418. static void test_wolfSSL_ASN1_STRING_to_UTF8(void)
  26419. {
  26420. WOLFSSL_X509* x509;
  26421. WOLFSSL_X509_NAME* subject;
  26422. WOLFSSL_X509_NAME_ENTRY* e;
  26423. WOLFSSL_ASN1_STRING* a;
  26424. FILE* file;
  26425. int idx = 0;
  26426. char targetOutput[16] = "www.wolfssl.com";
  26427. unsigned char* actual_output;
  26428. int len = 0;
  26429. int result = 0;
  26430. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  26431. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  26432. fclose(file);
  26433. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName");
  26434. AssertNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  26435. AssertIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  26436. NID_commonName, -1)), 5);
  26437. AssertNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  26438. AssertNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  26439. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  26440. result = strncmp((const char*)actual_output, targetOutput, len);
  26441. AssertIntEQ(result, 0);
  26442. printf(resultFmt, result == 0 ? passed : failed);
  26443. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(NULL, valid): ");
  26444. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)),
  26445. WOLFSSL_FATAL_ERROR);
  26446. printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
  26447. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(valid, NULL): ");
  26448. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)),
  26449. WOLFSSL_FATAL_ERROR);
  26450. printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
  26451. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL): ");
  26452. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)),
  26453. WOLFSSL_FATAL_ERROR);
  26454. printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
  26455. wolfSSL_X509_free(x509);
  26456. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26457. }
  26458. #endif /* !defined(NO_ASN) */
  26459. static void test_wolfSSL_sk_CIPHER_description(void)
  26460. {
  26461. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  26462. int i,j,k;
  26463. int numCiphers = 0;
  26464. const SSL_METHOD *method = NULL;
  26465. const SSL_CIPHER *cipher = NULL;
  26466. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  26467. SSL_CTX *ctx = NULL;
  26468. SSL *ssl = NULL;
  26469. char buf[256];
  26470. char test_str[9] = "0000000";
  26471. const char badStr[] = "unknown";
  26472. const char certPath[] = "./certs/client-cert.pem";
  26473. XMEMSET(buf, 0, sizeof(buf));
  26474. printf(testingFmt, "wolfSSL_sk_CIPHER_description");
  26475. AssertNotNull(method = TLSv1_2_client_method());
  26476. AssertNotNull(ctx = SSL_CTX_new(method));
  26477. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  26478. SSL_CTX_set_verify_depth(ctx, 4);
  26479. SSL_CTX_set_options(ctx, flags);
  26480. AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  26481. WOLFSSL_SUCCESS);
  26482. AssertNotNull(ssl = SSL_new(ctx));
  26483. /* SSL_get_ciphers returns a stack of all configured ciphers
  26484. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  26485. */
  26486. AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  26487. /* loop through the amount of supportedCiphers */
  26488. numCiphers = sk_num(supportedCiphers);
  26489. for (i = 0; i < numCiphers; ++i) {
  26490. /* sk_value increments "sk->data.cipher->cipherOffset".
  26491. * wolfSSL_sk_CIPHER_description sets the description for
  26492. * the cipher based on the provided offset.
  26493. */
  26494. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  26495. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  26496. }
  26497. /* Search cipher description string for "unknown" descriptor */
  26498. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  26499. k = 0;
  26500. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  26501. test_str[k] = badStr[k];
  26502. j++;
  26503. k++;
  26504. }
  26505. }
  26506. /* Fail if test_str == badStr == "unknown" */
  26507. AssertStrNE(test_str,badStr);
  26508. }
  26509. SSL_free(ssl);
  26510. SSL_CTX_free(ctx);
  26511. printf(resultFmt, passed);
  26512. }
  26513. static void test_wolfSSL_get_ciphers_compat(void)
  26514. {
  26515. const SSL_METHOD *method = NULL;
  26516. const char certPath[] = "./certs/client-cert.pem";
  26517. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  26518. SSL_CTX *ctx = NULL;
  26519. WOLFSSL *ssl = NULL;
  26520. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  26521. printf(testingFmt, "wolfSSL_get_ciphers_compat");
  26522. method = SSLv23_client_method();
  26523. AssertNotNull(method);
  26524. ctx = SSL_CTX_new(method);
  26525. AssertNotNull(ctx);
  26526. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  26527. SSL_CTX_set_verify_depth(ctx, 4);
  26528. SSL_CTX_set_options(ctx, flags);
  26529. AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  26530. WOLFSSL_SUCCESS);
  26531. AssertNotNull(ssl = SSL_new(ctx));
  26532. /* Test Bad NULL input */
  26533. AssertNull(supportedCiphers = SSL_get_ciphers(NULL));
  26534. /* Test for Good input */
  26535. AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  26536. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  26537. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  26538. SSL_free(ssl);
  26539. SSL_CTX_free(ctx);
  26540. printf(resultFmt, passed);
  26541. }
  26542. static void test_wolfSSL_X509_PUBKEY_get(void)
  26543. {
  26544. WOLFSSL_X509_PUBKEY pubkey;
  26545. WOLFSSL_X509_PUBKEY* key;
  26546. WOLFSSL_EVP_PKEY evpkey;
  26547. WOLFSSL_EVP_PKEY* evpPkey;
  26548. WOLFSSL_EVP_PKEY* retEvpPkey;
  26549. key = &pubkey;
  26550. evpPkey = &evpkey;
  26551. evpPkey->type = WOLFSSL_SUCCESS;
  26552. key->pkey = evpPkey;
  26553. printf(testingFmt, "wolfSSL_X509_PUBKEY_get()");
  26554. AssertNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  26555. AssertIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  26556. AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  26557. key->pkey = NULL;
  26558. AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  26559. printf(resultFmt,retEvpPkey == NULL ? passed : failed);
  26560. }
  26561. static void test_wolfSSL_d2i_DHparams()
  26562. {
  26563. #if !defined(NO_DH)
  26564. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  26565. FILE* f = NULL;
  26566. unsigned char buf[4096];
  26567. const unsigned char* pt = buf;
  26568. const char* params1 = "./certs/dh2048.der";
  26569. const char* params2 = "./certs/dh3072.der";
  26570. long len = 0;
  26571. WOLFSSL_DH* dh = NULL;
  26572. XMEMSET(buf, 0, sizeof(buf));
  26573. /* Test 2048 bit parameters */
  26574. printf(testingFmt, "wolfSSL_d2i_DHparams() 2048-bit");
  26575. f = XFOPEN(params1, "rb");
  26576. AssertTrue(f != XBADFILE);
  26577. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  26578. XFCLOSE(f);
  26579. /* Valid case */
  26580. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  26581. AssertNotNull(dh->p);
  26582. AssertNotNull(dh->g);
  26583. AssertTrue(pt != buf);
  26584. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  26585. /* Invalid cases */
  26586. AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  26587. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  26588. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  26589. DH_free(dh);
  26590. printf(resultFmt, passed);
  26591. *buf = 0;
  26592. pt = buf;
  26593. /* Test 3072 bit parameters */
  26594. printf(testingFmt, "wolfSSL_d2i_DHparams() 3072-bit");
  26595. f = XFOPEN(params2, "rb");
  26596. AssertTrue(f != XBADFILE);
  26597. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  26598. XFCLOSE(f);
  26599. /* Valid case */
  26600. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  26601. AssertNotNull(dh->p);
  26602. AssertNotNull(dh->g);
  26603. AssertTrue(pt != buf);
  26604. AssertIntEQ(DH_generate_key(dh), 1);
  26605. /* Invalid cases */
  26606. AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  26607. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  26608. DH_free(dh);
  26609. printf(resultFmt, passed);
  26610. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  26611. #endif /* !NO_DH */
  26612. }
  26613. static void test_wolfSSL_i2d_DHparams()
  26614. {
  26615. #if !defined(NO_DH)
  26616. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  26617. FILE* f;
  26618. unsigned char buf[4096];
  26619. const unsigned char* pt = buf;
  26620. unsigned char* pt2 = buf;
  26621. const char* params1 = "./certs/dh2048.der";
  26622. const char* params2 = "./certs/dh3072.der";
  26623. long len;
  26624. WOLFSSL_DH* dh;
  26625. /* Test 2048 bit parameters */
  26626. printf(testingFmt, "wolfSSL_i2d_DHparams() 2048-bit");
  26627. f = XFOPEN(params1, "rb");
  26628. AssertTrue(f != XBADFILE);
  26629. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  26630. XFCLOSE(f);
  26631. /* Valid case */
  26632. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  26633. AssertTrue(pt != buf);
  26634. AssertIntEQ(DH_generate_key(dh), 1);
  26635. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  26636. /* Invalid cases */
  26637. AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  26638. AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 264);
  26639. DH_free(dh);
  26640. printf(resultFmt, passed);
  26641. *buf = 0;
  26642. pt = buf;
  26643. pt2 = buf;
  26644. /* Test 3072 bit parameters */
  26645. printf(testingFmt, "wolfSSL_i2d_DHparams() 3072-bit");
  26646. f = XFOPEN(params2, "rb");
  26647. AssertTrue(f != XBADFILE);
  26648. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  26649. XFCLOSE(f);
  26650. /* Valid case */
  26651. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  26652. AssertTrue(pt != buf);
  26653. AssertIntEQ(DH_generate_key(dh), 1);
  26654. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  26655. /* Invalid cases */
  26656. AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  26657. AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 392);
  26658. DH_free(dh);
  26659. printf(resultFmt, passed);
  26660. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  26661. #endif
  26662. }
  26663. static void test_wolfSSL_EC_KEY_dup(void)
  26664. {
  26665. #if defined(HAVE_ECC) && (defined(OPENSSL_EXTRA) || \
  26666. defined(OPENSSL_EXTRA_X509_SMALL))
  26667. WOLFSSL_EC_KEY* ecKey;
  26668. WOLFSSL_EC_KEY* dupKey;
  26669. ecc_key* srcKey;
  26670. ecc_key* destKey;
  26671. printf(testingFmt, "wolfSSL_EC_KEY_dup()");
  26672. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  26673. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  26674. /* Valid cases */
  26675. AssertNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  26676. AssertIntEQ(wc_ecc_check_key((ecc_key*)dupKey->internal), 0);
  26677. /* Compare pubkey */
  26678. srcKey = (ecc_key*)ecKey->internal;
  26679. destKey = (ecc_key*)dupKey->internal;
  26680. AssertIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  26681. /* compare EC_GROUP */
  26682. AssertIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  26683. /* compare EC_POINT */
  26684. AssertIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  26685. dupKey->pub_key, NULL), MP_EQ);
  26686. /* compare BIGNUM */
  26687. AssertIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  26688. wolfSSL_EC_KEY_free(dupKey);
  26689. /* Invalid cases */
  26690. /* NULL key */
  26691. AssertNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  26692. /* NULL ecc_key */
  26693. wc_ecc_free((ecc_key*)ecKey->internal);
  26694. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  26695. ecKey->internal = NULL; /* Set ecc_key to NULL */
  26696. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  26697. wolfSSL_EC_KEY_free(ecKey);
  26698. wolfSSL_EC_KEY_free(dupKey);
  26699. /* NULL Group */
  26700. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  26701. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  26702. wolfSSL_EC_GROUP_free(ecKey->group);
  26703. ecKey->group = NULL; /* Set group to NULL */
  26704. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  26705. wolfSSL_EC_KEY_free(ecKey);
  26706. wolfSSL_EC_KEY_free(dupKey);
  26707. /* NULL public key */
  26708. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  26709. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  26710. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  26711. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  26712. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  26713. wolfSSL_EC_POINT_free(ecKey->pub_key);
  26714. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  26715. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  26716. wolfSSL_EC_KEY_free(ecKey);
  26717. wolfSSL_EC_KEY_free(dupKey);
  26718. /* NULL private key */
  26719. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  26720. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  26721. #if defined(WOLFSSL_PUBLIC_MP)
  26722. mp_int* mp = (mp_int*)ecKey->priv_key->internal;
  26723. mp_forcezero(mp);
  26724. mp_free(mp);
  26725. ecKey->priv_key->internal = NULL; /* Set internal key to NULL */
  26726. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  26727. #endif
  26728. wolfSSL_BN_free(ecKey->priv_key);
  26729. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  26730. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  26731. wolfSSL_EC_KEY_free(ecKey);
  26732. wolfSSL_EC_KEY_free(dupKey);
  26733. printf(resultFmt, passed);
  26734. #endif
  26735. }
  26736. static void test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  26737. {
  26738. #if !defined(NO_DSA)
  26739. DSA *dsa = NULL;
  26740. DSA *setDsa = NULL;
  26741. EVP_PKEY *pkey = NULL;
  26742. EVP_PKEY *set1Pkey = NULL;
  26743. SHA_CTX sha;
  26744. byte signature[DSA_SIG_SIZE];
  26745. byte hash[WC_SHA_DIGEST_SIZE];
  26746. word32 bytes;
  26747. int answer;
  26748. #ifdef USE_CERT_BUFFERS_1024
  26749. const unsigned char* dsaKeyDer = dsa_key_der1024;
  26750. int dsaKeySz = sizeof_dsa_key_der_1024;
  26751. byte tmp[ONEK_BUF];
  26752. XMEMSET(tmp, 0, sizeof(tmp));
  26753. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  26754. bytes = dsa_key_der_sz;
  26755. #elif defined(USE_CERT_BUFFERS_2048)
  26756. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  26757. int dsaKeySz = sizeof_dsa_key_der_2048;
  26758. byte tmp[TWOK_BUF];
  26759. XMEMSET(tmp, 0, sizeof(tmp));
  26760. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  26761. bytes = dsaKeySz;
  26762. #else
  26763. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  26764. int dsaKeySz = sizeof_dsa_key_der_2048;
  26765. byte tmp[TWOK_BUF];
  26766. XMEMSET(tmp, 0, sizeof(tmp));
  26767. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  26768. XFILE fp = XOPEN("./certs/dsa2048.der", "rb");
  26769. if (fp == XBADFILE) {
  26770. return WOLFSSL_BAD_FILE;
  26771. }
  26772. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  26773. XFCLOSE(fp);
  26774. #endif /* END USE_CERT_BUFFERS_1024 */
  26775. printf(testingFmt,
  26776. "wolfSSL_EVP_PKEY_set1_DSA and wolfSSL_EVP_PKEY_get1_DSA");
  26777. /* Create hash to later Sign and Verify */
  26778. AssertIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  26779. AssertIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  26780. AssertIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  26781. /* Initialize pkey with der format dsa key */
  26782. AssertNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey,
  26783. &dsaKeyDer ,(long)dsaKeySz));
  26784. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  26785. /* Should Fail: NULL argument */
  26786. AssertNull(dsa = EVP_PKEY_get0_DSA(NULL));
  26787. AssertNull(dsa = EVP_PKEY_get1_DSA(NULL));
  26788. /* Should Pass: Initialized pkey argument */
  26789. AssertNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  26790. AssertNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  26791. AssertIntEQ(DSA_bits(dsa), 2048);
  26792. /* Sign */
  26793. AssertIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  26794. /* Verify. */
  26795. AssertIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  26796. WOLFSSL_SUCCESS);
  26797. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  26798. /* Should Fail: set1Pkey not initialized */
  26799. AssertIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  26800. /* Initialize set1Pkey */
  26801. set1Pkey = EVP_PKEY_new();
  26802. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  26803. AssertIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  26804. WOLFSSL_SUCCESS);
  26805. /* Should Pass: set dsa into set1Pkey */
  26806. AssertIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  26807. printf(resultFmt, passed);
  26808. DSA_free(dsa);
  26809. DSA_free(setDsa);
  26810. EVP_PKEY_free(pkey);
  26811. EVP_PKEY_free(set1Pkey);
  26812. #endif /* NO_DSA */
  26813. } /* END test_EVP_PKEY_set1_get1_DSA */
  26814. static void test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  26815. {
  26816. #ifdef HAVE_ECC
  26817. WOLFSSL_EC_KEY *ecKey = NULL;
  26818. WOLFSSL_EC_KEY *ecGet1 = NULL;
  26819. EVP_PKEY *pkey = NULL;
  26820. printf(testingFmt,
  26821. "wolfSSL_EVP_PKEY_set1_EC_KEY and wolfSSL_EVP_PKEY_get1_EC_KEY");
  26822. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  26823. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  26824. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  26825. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  26826. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  26827. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  26828. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  26829. AssertNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  26830. AssertNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  26831. wolfSSL_EC_KEY_free(ecKey);
  26832. wolfSSL_EC_KEY_free(ecGet1);
  26833. EVP_PKEY_free(pkey);
  26834. /* PASSED */
  26835. printf(resultFmt, passed);
  26836. #endif /* HAVE_ECC */
  26837. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  26838. static void test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  26839. {
  26840. #if !defined(NO_DH)
  26841. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  26842. DH *dh = NULL;
  26843. DH *setDh = NULL;
  26844. EVP_PKEY *pkey = NULL;
  26845. FILE* f = NULL;
  26846. unsigned char buf[4096];
  26847. const unsigned char* pt = buf;
  26848. const char* dh2048 = "./certs/dh2048.der";
  26849. long len = 0;
  26850. int code = -1;
  26851. printf(testingFmt,"wolfSSL_EVP_PKEY_set1_DH and wolfSSL_EVP_PKEY_get1_DH");
  26852. XMEMSET(buf, 0, sizeof(buf));
  26853. f = XFOPEN(dh2048, "rb");
  26854. AssertTrue(f != XBADFILE);
  26855. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  26856. XFCLOSE(f);
  26857. /* Load dh2048.der into DH with internal format */
  26858. AssertNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  26859. AssertIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  26860. AssertIntEQ(code, 0);
  26861. code = -1;
  26862. pkey = wolfSSL_EVP_PKEY_new();
  26863. /* Set DH into PKEY */
  26864. AssertIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  26865. /* Get DH from PKEY */
  26866. AssertNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  26867. AssertIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  26868. AssertIntEQ(code, 0);
  26869. EVP_PKEY_free(pkey);
  26870. DH_free(setDh);
  26871. DH_free(dh);
  26872. printf(resultFmt, passed);
  26873. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  26874. #endif /* NO_DH */
  26875. } /* END test_EVP_PKEY_set1_get1_DH */
  26876. static void test_wolfSSL_CTX_ctrl(void)
  26877. {
  26878. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  26879. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  26880. char caFile[] = "./certs/client-ca.pem";
  26881. char clientFile[] = "./certs/client-cert.pem";
  26882. SSL_CTX* ctx;
  26883. X509* x509 = NULL;
  26884. #if !defined(NO_DH) && !defined(NO_DSA)
  26885. byte buf[6000];
  26886. char file[] = "./certs/dsaparams.pem";
  26887. XFILE f;
  26888. int bytes;
  26889. BIO* bio;
  26890. DSA* dsa;
  26891. DH* dh;
  26892. #endif
  26893. #ifdef HAVE_ECC
  26894. WOLFSSL_EC_KEY* ecKey;
  26895. #endif
  26896. printf(testingFmt, "wolfSSL_CTX_ctrl");
  26897. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  26898. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  26899. AssertNotNull(x509);
  26900. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  26901. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  26902. AssertNotNull(x509);
  26903. #if !defined(NO_DH) && !defined(NO_DSA)
  26904. /* Initialize DH */
  26905. f = XFOPEN(file, "rb");
  26906. AssertTrue((f != XBADFILE));
  26907. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  26908. XFCLOSE(f);
  26909. bio = BIO_new_mem_buf((void*)buf, bytes);
  26910. AssertNotNull(bio);
  26911. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  26912. AssertNotNull(dsa);
  26913. dh = wolfSSL_DSA_dup_DH(dsa);
  26914. AssertNotNull(dh);
  26915. #endif
  26916. #ifdef HAVE_ECC
  26917. /* Initialize WOLFSSL_EC_KEY */
  26918. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  26919. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey),1);
  26920. #endif
  26921. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  26922. /* additional test of getting EVP_PKEY key size from X509
  26923. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  26924. * allowed with user RSA */
  26925. {
  26926. EVP_PKEY* pkey;
  26927. #if defined(HAVE_ECC)
  26928. X509* ecX509;
  26929. #endif /* HAVE_ECC */
  26930. AssertNotNull(pkey = X509_get_pubkey(x509));
  26931. /* current RSA key is 2048 bit (256 bytes) */
  26932. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  26933. EVP_PKEY_free(pkey);
  26934. #if defined(HAVE_ECC)
  26935. #if defined(USE_CERT_BUFFERS_256)
  26936. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  26937. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  26938. SSL_FILETYPE_ASN1));
  26939. #else
  26940. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  26941. cliEccCertFile, SSL_FILETYPE_PEM));
  26942. #endif
  26943. AssertNotNull(pkey = X509_get_pubkey(ecX509));
  26944. /* current ECC key is 256 bit (32 bytes) */
  26945. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  26946. X509_free(ecX509);
  26947. EVP_PKEY_free(pkey);
  26948. #endif /* HAVE_ECC */
  26949. }
  26950. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  26951. /* Tests should fail with passed in NULL pointer */
  26952. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,NULL),
  26953. SSL_FAILURE);
  26954. #if !defined(NO_DH) && !defined(NO_DSA)
  26955. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,NULL),
  26956. SSL_FAILURE);
  26957. #endif
  26958. #ifdef HAVE_ECC
  26959. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,NULL),
  26960. SSL_FAILURE);
  26961. #endif
  26962. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  26963. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  26964. */
  26965. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,x509),
  26966. SSL_SUCCESS);
  26967. /* Test with SSL_CTRL_OPTIONS
  26968. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  26969. */
  26970. AssertTrue(wolfSSL_CTX_ctrl(ctx,SSL_CTRL_OPTIONS,SSL_OP_NO_TLSv1,NULL)
  26971. == SSL_OP_NO_TLSv1);
  26972. AssertTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  26973. /* Test with SSL_CTRL_SET_TMP_DH
  26974. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  26975. */
  26976. #if !defined(NO_DH) && !defined(NO_DSA)
  26977. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,dh),
  26978. SSL_SUCCESS);
  26979. #endif
  26980. /* Test with SSL_CTRL_SET_TMP_ECDH
  26981. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  26982. */
  26983. #ifdef HAVE_ECC
  26984. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,ecKey),
  26985. SSL_SUCCESS);
  26986. #endif
  26987. #ifdef WOLFSSL_ENCRYPTED_KEYS
  26988. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  26989. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  26990. #endif
  26991. /* Cleanup and Pass */
  26992. #if !defined(NO_DH) && !defined(NO_DSA)
  26993. BIO_free(bio);
  26994. DSA_free(dsa);
  26995. DH_free(dh);
  26996. #endif
  26997. #ifdef HAVE_ECC
  26998. wolfSSL_EC_KEY_free(ecKey);
  26999. #endif
  27000. SSL_CTX_free(ctx);
  27001. printf(resultFmt, passed);
  27002. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  27003. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  27004. }
  27005. static void test_wolfSSL_DH_check(void)
  27006. {
  27007. #if !defined(NO_DH) && !defined(NO_DSA)
  27008. byte buf[6000];
  27009. char file[] = "./certs/dsaparams.pem";
  27010. XFILE f;
  27011. int bytes;
  27012. BIO* bio;
  27013. DSA* dsa;
  27014. DH* dh = NULL;
  27015. WOLFSSL_BIGNUM* pTmp = NULL;
  27016. WOLFSSL_BIGNUM* gTmp = NULL;
  27017. int codes = -1;
  27018. printf(testingFmt, "wolfSSL_DH_check");
  27019. /* Initialize DH */
  27020. f = XFOPEN(file, "rb");
  27021. AssertTrue((f != XBADFILE));
  27022. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  27023. XFCLOSE(f);
  27024. bio = BIO_new_mem_buf((void*)buf, bytes);
  27025. AssertNotNull(bio);
  27026. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  27027. AssertNotNull(dsa);
  27028. dh = wolfSSL_DSA_dup_DH(dsa);
  27029. AssertNotNull(dh);
  27030. /* Test assumed to be valid dh.
  27031. * Should return WOLFSSL_SUCCESS
  27032. * codes should be 0
  27033. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  27034. */
  27035. AssertIntEQ(wolfSSL_DH_check(dh, &codes), WOLFSSL_SUCCESS);
  27036. AssertIntEQ(codes, 0);
  27037. /* Test NULL dh: expected BAD_FUNC_ARG */
  27038. AssertIntEQ(wolfSSL_DH_check(NULL, &codes), WOLFSSL_FAILURE);
  27039. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  27040. pTmp = dh->p;
  27041. dh->p = NULL;
  27042. AssertIntEQ(wolfSSL_DH_check(dh, &codes), WOLFSSL_FAILURE);
  27043. AssertIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  27044. /* set dh->p back to normal so it wont fail on next tests */
  27045. dh->p = pTmp;
  27046. pTmp = NULL;
  27047. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  27048. gTmp = dh->g;
  27049. dh->g = NULL;
  27050. AssertIntEQ(wolfSSL_DH_check(dh, &codes), WOLFSSL_FAILURE);
  27051. AssertIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  27052. dh->g = gTmp;
  27053. gTmp = NULL;
  27054. /* Cleanup and Pass Test */
  27055. BIO_free(bio);
  27056. DSA_free(dsa);
  27057. DH_free(dh);
  27058. printf(resultFmt, passed);
  27059. #endif /* !NO_DH && !NO_DSA */
  27060. }
  27061. static void test_wolfSSL_EVP_PKEY_assign(void)
  27062. {
  27063. #if defined(OPENSSL_ALL)
  27064. int type;
  27065. WOLFSSL_EVP_PKEY* pkey;
  27066. #ifndef NO_RSA
  27067. WOLFSSL_RSA* rsa;
  27068. #endif
  27069. #ifndef NO_DSA
  27070. WOLFSSL_DSA* dsa;
  27071. #endif
  27072. #ifdef HAVE_ECC
  27073. WOLFSSL_EC_KEY* ecKey;
  27074. #endif
  27075. printf(testingFmt, "wolfSSL_EVP_PKEY_assign");
  27076. #ifndef NO_RSA
  27077. type = EVP_PKEY_RSA;
  27078. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  27079. AssertNotNull(rsa = wolfSSL_RSA_new());
  27080. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,rsa), WOLFSSL_FAILURE);
  27081. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  27082. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,rsa), WOLFSSL_FAILURE);
  27083. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,rsa), WOLFSSL_SUCCESS);
  27084. wolfSSL_EVP_PKEY_free(pkey);
  27085. #endif /* NO_RSA */
  27086. #ifndef NO_DSA
  27087. type = EVP_PKEY_DSA;
  27088. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  27089. AssertNotNull(dsa = wolfSSL_DSA_new());
  27090. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,dsa), WOLFSSL_FAILURE);
  27091. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  27092. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,dsa), WOLFSSL_FAILURE);
  27093. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,dsa), WOLFSSL_SUCCESS);
  27094. wolfSSL_EVP_PKEY_free(pkey);
  27095. #endif /* NO_DSA */
  27096. #ifdef HAVE_ECC
  27097. type = EVP_PKEY_EC;
  27098. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  27099. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  27100. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,ecKey), WOLFSSL_FAILURE);
  27101. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  27102. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,ecKey), WOLFSSL_FAILURE);
  27103. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_SUCCESS);
  27104. wolfSSL_EVP_PKEY_free(pkey);
  27105. #endif /* HAVE_ECC */
  27106. printf(resultFmt, passed);
  27107. #endif /* OPENSSL_ALL */
  27108. }
  27109. static void test_wolfSSL_OBJ_ln(void)
  27110. {
  27111. const int nid_set[] = {
  27112. NID_commonName,
  27113. NID_serialNumber,
  27114. NID_countryName,
  27115. NID_localityName,
  27116. NID_stateOrProvinceName,
  27117. NID_organizationName,
  27118. NID_organizationalUnitName,
  27119. NID_domainComponent,
  27120. NID_businessCategory,
  27121. NID_jurisdictionCountryName,
  27122. NID_jurisdictionStateOrProvinceName,
  27123. NID_emailAddress
  27124. };
  27125. const char* ln_set[] = {
  27126. "commonName",
  27127. "serialNumber",
  27128. "countryName",
  27129. "localityName",
  27130. "stateOrProvinceName",
  27131. "organizationName",
  27132. "organizationalUnitName",
  27133. "domainComponent",
  27134. "businessCategory",
  27135. "jurisdictionCountryName",
  27136. "jurisdictionStateOrProvinceName",
  27137. "emailAddress",
  27138. };
  27139. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  27140. printf(testingFmt, "wolfSSL_OBJ_ln");
  27141. AssertIntEQ(OBJ_ln2nid(NULL), NID_undef);
  27142. #ifdef HAVE_ECC
  27143. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  27144. {
  27145. size_t nCurves = 27;
  27146. EC_builtin_curve r[nCurves];
  27147. nCurves = EC_get_builtin_curves(r,nCurves);
  27148. for (i = 0; i < nCurves; i++) {
  27149. AssertIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  27150. AssertStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  27151. }
  27152. }
  27153. #endif
  27154. #endif
  27155. for (i = 0; i < maxIdx; i++) {
  27156. AssertIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  27157. AssertStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  27158. }
  27159. printf(resultFmt, passed);
  27160. }
  27161. static void test_wolfSSL_OBJ_sn(void)
  27162. {
  27163. int i = 0, maxIdx = 7;
  27164. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  27165. NID_stateOrProvinceName,NID_organizationName,
  27166. NID_organizationalUnitName,NID_emailAddress};
  27167. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  27168. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  27169. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  27170. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  27171. WOLFSSL_EMAIL_ADDR};
  27172. printf(testingFmt, "wolfSSL_OBJ_sn");
  27173. AssertIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  27174. for (i = 0; i < maxIdx; i++) {
  27175. AssertIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  27176. AssertStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  27177. }
  27178. printf(resultFmt, passed);
  27179. }
  27180. #endif /* OPENSSL_ALL */
  27181. static void test_wolfSSL_X509V3_EXT_get(void) {
  27182. #if !defined(NO_FILESYSTEM) && defined (OPENSSL_ALL)
  27183. FILE* f;
  27184. int numOfExt =0;
  27185. int extNid = 0;
  27186. int i = 0;
  27187. WOLFSSL_X509* x509;
  27188. WOLFSSL_X509_EXTENSION* ext;
  27189. const WOLFSSL_v3_ext_method* method;
  27190. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  27191. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  27192. fclose(f);
  27193. printf(testingFmt, "wolfSSL_X509V3_EXT_get() return struct and nid test");
  27194. AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 4);
  27195. for (i = 0; i < numOfExt; i++) {
  27196. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  27197. AssertNotNull(extNid = ext->obj->nid);
  27198. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  27199. AssertIntEQ(method->ext_nid, extNid);
  27200. }
  27201. printf(resultFmt, "passed");
  27202. printf(testingFmt, "wolfSSL_X509V3_EXT_get() NULL argument test");
  27203. AssertNull(method = wolfSSL_X509V3_EXT_get(NULL));
  27204. printf(resultFmt, "passed");
  27205. wolfSSL_X509_free(x509);
  27206. #endif
  27207. }
  27208. static void test_wolfSSL_X509V3_EXT_d2i(void) {
  27209. #if !defined(NO_FILESYSTEM) && defined (OPENSSL_ALL)
  27210. FILE* f;
  27211. int numOfExt = 0, nid = 0, i = 0, expected, actual;
  27212. char* str;
  27213. unsigned char* data;
  27214. const WOLFSSL_v3_ext_method* method;
  27215. WOLFSSL_X509* x509;
  27216. WOLFSSL_X509_EXTENSION* ext;
  27217. WOLFSSL_ASN1_OBJECT *obj, *adObj;
  27218. WOLFSSL_ASN1_STRING* asn1str;
  27219. WOLFSSL_AUTHORITY_KEYID* aKeyId;
  27220. WOLFSSL_AUTHORITY_INFO_ACCESS* aia;
  27221. WOLFSSL_BASIC_CONSTRAINTS* bc;
  27222. WOLFSSL_ACCESS_DESCRIPTION* ad;
  27223. WOLFSSL_GENERAL_NAME* gn;
  27224. printf(testingFmt, "wolfSSL_X509V3_EXT_d2i()");
  27225. /* Check NULL argument */
  27226. AssertNull(wolfSSL_X509V3_EXT_d2i(NULL));
  27227. /* Using OCSP cert with X509V3 extensions */
  27228. AssertNotNull(f = fopen("./certs/ocsp/root-ca-cert.pem", "rb"));
  27229. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  27230. fclose(f);
  27231. AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  27232. /* Basic Constraints */
  27233. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  27234. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  27235. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  27236. AssertNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  27237. AssertIntEQ(bc->ca, 1);
  27238. AssertNull(bc->pathlen);
  27239. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  27240. i++;
  27241. /* Subject Key Identifier */
  27242. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  27243. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  27244. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  27245. AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  27246. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  27247. AssertNotNull(method->i2s);
  27248. AssertNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  27249. wolfSSL_ASN1_STRING_free(asn1str);
  27250. actual = strcmp(str,
  27251. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  27252. AssertIntEQ(actual, 0);
  27253. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27254. i++;
  27255. /* Authority Key Identifier */
  27256. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  27257. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  27258. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  27259. AssertNotNull(aKeyId =
  27260. (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(ext));
  27261. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  27262. AssertNotNull(asn1str = aKeyId->keyid);
  27263. AssertNotNull(str =
  27264. wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method, asn1str));
  27265. actual = strcmp(str,
  27266. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  27267. AssertIntEQ(actual, 0);
  27268. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27269. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  27270. i++;
  27271. /* Key Usage */
  27272. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  27273. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  27274. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  27275. AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  27276. AssertNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  27277. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  27278. #ifdef BIG_ENDIAN_ORDER
  27279. actual = data[1];
  27280. #else
  27281. actual = data[0];
  27282. #endif
  27283. AssertIntEQ(actual, expected);
  27284. wolfSSL_ASN1_STRING_free(asn1str);
  27285. #if 0
  27286. i++;
  27287. /* Authority Info Access */
  27288. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  27289. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  27290. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  27291. AssertNotNull(aia = wolfSSL_X509V3_EXT_d2i(ext));
  27292. AssertIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  27293. /* URI entry is an ACCESS_DESCRIPTION type */
  27294. AssertNotNull(ad = wolfSSL_sk_value(aia, 0));
  27295. AssertNotNull(adObj = ad->method);
  27296. /* Make sure nid is OCSP */
  27297. AssertIntEQ(wolfSSL_OBJ_obj2nid(adObj), AIA_OCSP_OID);
  27298. /* GENERAL_NAME stores URI as an ASN1_STRING */
  27299. AssertNotNull(gn = ad->location);
  27300. AssertIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  27301. AssertNotNull(asn1str = gn->d.uniformResourceIdentifier);
  27302. AssertIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  27303. str = (char*)wolfSSL_ASN1_STRING_data(asn1str);
  27304. actual = strcmp(str, "http://127.0.0.1:22220");
  27305. AssertIntEQ(actual, 0);
  27306. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  27307. XFREE(ad, NULL, DYNAMIC_TYPE_X509_EXT);
  27308. #else
  27309. (void) aia; (void) ad; (void) adObj; (void) gn;
  27310. #endif
  27311. wolfSSL_X509_free(x509);
  27312. printf(resultFmt, "passed");
  27313. #endif
  27314. }
  27315. static void test_wolfSSL_X509_get_ext(void){
  27316. #if !defined(NO_FILESYSTEM) && defined (OPENSSL_ALL)
  27317. int ret = 0;
  27318. FILE* f;
  27319. WOLFSSL_X509* x509;
  27320. WOLFSSL_X509_EXTENSION* foundExtension;
  27321. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  27322. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  27323. fclose(f);
  27324. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 4);
  27325. printf(testingFmt, "wolfSSL_X509_get_ext() valid input");
  27326. AssertNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  27327. printf(resultFmt, "passed");
  27328. printf(testingFmt, "wolfSSL_X509_get_ext() valid x509, idx out of bounds");
  27329. AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  27330. AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  27331. printf(resultFmt, "passed");
  27332. printf(testingFmt, "wolfSSL_X509_get_ext() NULL x509, idx out of bounds");
  27333. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  27334. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  27335. printf(resultFmt, "passed");
  27336. printf(testingFmt, "wolfSSL_X509_get_ext() NULL x509, valid idx");
  27337. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  27338. printf(resultFmt, "passed");
  27339. wolfSSL_X509_free(x509);
  27340. #endif
  27341. }
  27342. static void test_wolfSSL_X509_get_ext_by_NID(void)
  27343. {
  27344. #if defined(OPENSSL_ALL)
  27345. int rc;
  27346. FILE* f;
  27347. WOLFSSL_X509* x509;
  27348. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  27349. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  27350. fclose(f);
  27351. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
  27352. AssertIntGE(rc, 0);
  27353. /* Start search from last location (should fail) */
  27354. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, rc);
  27355. AssertIntGE(rc, -1);
  27356. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -2);
  27357. AssertIntGE(rc, -1);
  27358. rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints, -1);
  27359. AssertIntEQ(rc, -1);
  27360. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1);
  27361. AssertIntEQ(rc, -1);
  27362. wolfSSL_X509_free(x509);
  27363. #endif
  27364. }
  27365. static void test_wolfSSL_X509_EXTENSION_new(void)
  27366. {
  27367. #if defined (OPENSSL_ALL)
  27368. WOLFSSL_X509_EXTENSION* ext;
  27369. AssertNotNull(ext = wolfSSL_X509_EXTENSION_new());
  27370. AssertNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  27371. ext->obj->nid = WOLFSSL_SUCCESS;
  27372. AssertIntEQ(WOLFSSL_SUCCESS, ext->obj->nid);
  27373. wolfSSL_X509_EXTENSION_free(ext);
  27374. #endif
  27375. }
  27376. static void test_wolfSSL_X509_EXTENSION_get_object(void)
  27377. {
  27378. #if !defined(NO_FILESYSTEM) && defined (OPENSSL_ALL)
  27379. WOLFSSL_X509* x509;
  27380. WOLFSSL_X509_EXTENSION* ext;
  27381. WOLFSSL_ASN1_OBJECT* o;
  27382. FILE* file;
  27383. int nid = 0;
  27384. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  27385. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  27386. fclose(file);
  27387. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_object() testing ext idx 0");
  27388. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  27389. AssertNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  27390. AssertIntEQ(o->nid, 128);
  27391. nid = o->nid;
  27392. printf(resultFmt, nid == 128 ? passed : failed);
  27393. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_object() NULL argument");
  27394. AssertNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  27395. printf(resultFmt, passed);
  27396. wolfSSL_X509_free(x509);
  27397. #endif
  27398. }
  27399. static void test_wolfSSL_X509_EXTENSION_get_data(void)
  27400. {
  27401. #if !defined(NO_FILESYSTEM) && defined (OPENSSL_ALL)
  27402. WOLFSSL_X509* x509;
  27403. WOLFSSL_X509_EXTENSION* ext;
  27404. WOLFSSL_ASN1_STRING* str;
  27405. FILE* file;
  27406. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_data");
  27407. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  27408. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  27409. fclose(file);
  27410. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  27411. AssertNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  27412. printf(resultFmt, passed);
  27413. wolfSSL_X509_free(x509);
  27414. #endif
  27415. }
  27416. static void test_wolfSSL_X509_EXTENSION_get_critical(void)
  27417. {
  27418. #if !defined(NO_FILESYSTEM) && defined (OPENSSL_ALL)
  27419. WOLFSSL_X509* x509;
  27420. WOLFSSL_X509_EXTENSION* ext;
  27421. FILE* file;
  27422. int crit;
  27423. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_critical");
  27424. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  27425. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  27426. fclose(file);
  27427. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  27428. crit = wolfSSL_X509_EXTENSION_get_critical(ext);
  27429. AssertIntEQ(crit, 0);
  27430. printf(resultFmt, passed);
  27431. wolfSSL_X509_free(x509);
  27432. #endif
  27433. }
  27434. static void test_wolfSSL_X509V3_EXT_print(void)
  27435. {
  27436. #if !defined(NO_FILESYSTEM) && defined (OPENSSL_ALL)
  27437. printf(testingFmt, "wolfSSL_X509V3_EXT_print");
  27438. {
  27439. FILE* f;
  27440. WOLFSSL_X509* x509;
  27441. X509_EXTENSION * ext = NULL;
  27442. int loc;
  27443. BIO *bio = NULL;
  27444. AssertNotNull(f = fopen(svrCertFile, "rb"));
  27445. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  27446. fclose(f);
  27447. AssertNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  27448. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
  27449. AssertIntGT(loc, -1);
  27450. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  27451. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  27452. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_subject_key_identifier, -1);
  27453. AssertIntGT(loc, -1);
  27454. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  27455. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  27456. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_authority_key_identifier, -1);
  27457. AssertIntGT(loc, -1);
  27458. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  27459. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  27460. wolfSSL_BIO_free(bio);
  27461. wolfSSL_X509_free(x509);
  27462. }
  27463. {
  27464. X509 *x509;
  27465. BIO *bio;
  27466. X509_EXTENSION *ext;
  27467. unsigned int i;
  27468. unsigned int idx;
  27469. /* Some NIDs to test with */
  27470. int nids[] = {
  27471. /* NID_key_usage, currently X509_get_ext returns this as a bit
  27472. * string, which messes up X509V3_EXT_print */
  27473. /* NID_ext_key_usage, */
  27474. NID_subject_alt_name,
  27475. };
  27476. int* n;
  27477. printf(testingFmt, "wolfSSL_X509V3_EXT_print");
  27478. AssertNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  27479. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  27480. WOLFSSL_FILETYPE_PEM));
  27481. printf("\nPrinting extension values:\n");
  27482. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  27483. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  27484. * update the index */
  27485. AssertIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  27486. AssertNotNull(ext = X509_get_ext(x509, idx));
  27487. AssertIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  27488. printf("\n");
  27489. }
  27490. BIO_free(bio);
  27491. X509_free(x509);
  27492. }
  27493. printf(resultFmt, passed);
  27494. #endif
  27495. }
  27496. static void test_wolfSSL_X509_cmp(void)
  27497. {
  27498. #if defined(OPENSSL_ALL)
  27499. FILE* file1;
  27500. FILE* file2;
  27501. WOLFSSL_X509* cert1;
  27502. WOLFSSL_X509* cert2;
  27503. int ret = 0;
  27504. AssertNotNull(file1=fopen("./certs/server-cert.pem", "rb"));
  27505. AssertNotNull(file2=fopen("./certs/3072/client-cert.pem", "rb"));
  27506. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  27507. AssertNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  27508. fclose(file1);
  27509. fclose(file2);
  27510. printf(testingFmt, "wolfSSL_X509_cmp() testing matching certs");
  27511. ret = wolfSSL_X509_cmp(cert1, cert1);
  27512. AssertIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  27513. printf(resultFmt, ret == 0 ? passed : failed);
  27514. printf(testingFmt, "wolfSSL_X509_cmp() testing mismatched certs");
  27515. ret = wolfSSL_X509_cmp(cert1, cert2);
  27516. AssertIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  27517. printf(resultFmt, ret == -1 ? passed : failed);
  27518. printf(testingFmt, "wolfSSL_X509_cmp() testing NULL, valid args");
  27519. ret = wolfSSL_X509_cmp(NULL, cert2);
  27520. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  27521. printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
  27522. printf(testingFmt, "wolfSSL_X509_cmp() testing valid, NULL args");
  27523. ret = wolfSSL_X509_cmp(cert1, NULL);
  27524. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  27525. printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
  27526. printf(testingFmt, "wolfSSL_X509_cmp() testing NULL, NULL args");
  27527. ret = wolfSSL_X509_cmp(NULL, NULL);
  27528. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  27529. printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
  27530. wolfSSL_X509_free(cert1);
  27531. wolfSSL_X509_free(cert2);
  27532. #endif
  27533. }
  27534. static void test_wolfSSL_PKEY_up_ref()
  27535. {
  27536. #if defined(OPENSSL_ALL)
  27537. EVP_PKEY* pkey;
  27538. printf(testingFmt, "wolfSSL_PKEY_up_ref()");
  27539. pkey = EVP_PKEY_new();
  27540. AssertIntEQ(EVP_PKEY_up_ref(NULL), 0);
  27541. AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
  27542. EVP_PKEY_free(pkey);
  27543. AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
  27544. EVP_PKEY_free(pkey);
  27545. EVP_PKEY_free(pkey);
  27546. printf(resultFmt, "passed");
  27547. #endif
  27548. }
  27549. static void test_wolfSSL_i2d_PrivateKey()
  27550. {
  27551. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA)
  27552. printf(testingFmt, "wolfSSL_i2d_PrivateKey()");
  27553. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  27554. {
  27555. EVP_PKEY* pkey;
  27556. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  27557. unsigned char buf[FOURK_BUF];
  27558. unsigned char* pt;
  27559. int bufSz;
  27560. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  27561. (long)sizeof_server_key_der_2048));
  27562. AssertIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  27563. pt = buf;
  27564. AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  27565. AssertIntNE((pt - buf), 0);
  27566. AssertIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  27567. EVP_PKEY_free(pkey);
  27568. }
  27569. #endif
  27570. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  27571. {
  27572. EVP_PKEY* pkey;
  27573. const unsigned char* client_key =
  27574. (const unsigned char*)ecc_clikey_der_256;
  27575. unsigned char buf[FOURK_BUF];
  27576. unsigned char* pt;
  27577. int bufSz;
  27578. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  27579. sizeof_ecc_clikey_der_256)));
  27580. AssertIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  27581. pt = buf;
  27582. AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  27583. AssertIntNE((pt - buf), 0);
  27584. AssertIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  27585. EVP_PKEY_free(pkey);
  27586. }
  27587. #endif
  27588. printf(resultFmt, "passed");
  27589. #endif
  27590. }
  27591. static void test_wolfSSL_OCSP_get0_info()
  27592. {
  27593. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP) && !defined(NO_FILESYSTEM)
  27594. X509* cert;
  27595. X509* issuer;
  27596. OCSP_CERTID* id;
  27597. ASN1_STRING* name = NULL;
  27598. ASN1_OBJECT* pmd = NULL;
  27599. ASN1_STRING* keyHash = NULL;
  27600. ASN1_INTEGER* serial = NULL;
  27601. ASN1_INTEGER* x509Int;
  27602. printf(testingFmt, "wolfSSL_OCSP_get0_info()");
  27603. AssertNotNull(cert =
  27604. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM));
  27605. AssertNotNull(issuer =
  27606. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
  27607. id = OCSP_cert_to_id(NULL, cert, issuer);
  27608. AssertNotNull(id);
  27609. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  27610. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  27611. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  27612. AssertIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  27613. AssertIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  27614. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  27615. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  27616. AssertNotNull(serial);
  27617. /* compare serial number to one in cert, should be equal */
  27618. x509Int = X509_get_serialNumber(cert);
  27619. AssertNotNull(x509Int);
  27620. AssertIntEQ(x509Int->dataMax, serial->dataMax);
  27621. AssertIntEQ(XMEMCMP(x509Int->data, serial->data, serial->dataMax), 0);
  27622. OCSP_CERTID_free(id);
  27623. X509_free(cert); /* free's x509Int */
  27624. X509_free(issuer);
  27625. printf(resultFmt, "passed");
  27626. #endif /* OPENSSL_EXTRA & HAVE_OCSP */
  27627. }
  27628. static void test_wolfSSL_EVP_PKEY_derive(void)
  27629. {
  27630. #ifdef OPENSSL_ALL
  27631. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  27632. EVP_PKEY_CTX *ctx;
  27633. unsigned char *skey;
  27634. size_t skeylen;
  27635. EVP_PKEY *pkey, *peerkey;
  27636. const unsigned char* key;
  27637. /* DH */
  27638. key = dh_key_der_2048;
  27639. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  27640. sizeof_dh_key_der_2048)));
  27641. AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  27642. key = dh_key_der_2048;
  27643. AssertNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  27644. sizeof_dh_key_der_2048)));
  27645. AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  27646. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  27647. AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
  27648. AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  27649. AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  27650. AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
  27651. AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  27652. EVP_PKEY_CTX_free(ctx);
  27653. EVP_PKEY_free(peerkey);
  27654. EVP_PKEY_free(pkey);
  27655. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  27656. #ifdef HAVE_ECC
  27657. /* ECDH */
  27658. key = ecc_clikey_der_256;
  27659. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  27660. sizeof_ecc_clikey_der_256)));
  27661. key = ecc_clikeypub_der_256;
  27662. AssertNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  27663. sizeof_ecc_clikeypub_der_256)));
  27664. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  27665. AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
  27666. AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  27667. AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  27668. AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
  27669. AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  27670. EVP_PKEY_CTX_free(ctx);
  27671. EVP_PKEY_free(peerkey);
  27672. EVP_PKEY_free(pkey);
  27673. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  27674. #endif /* HAVE_ECC */
  27675. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  27676. #endif /* OPENSSL_ALL */
  27677. }
  27678. static void test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  27679. {
  27680. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  27681. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  27682. RSA *rsa;
  27683. const unsigned char *derBuf = client_key_der_2048;
  27684. unsigned char em[256] = {0}; /* len = 2048/8 */
  27685. /* Random data simulating a hash */
  27686. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  27687. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  27688. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  27689. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  27690. };
  27691. AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  27692. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -1), 1);
  27693. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -1), 1);
  27694. RSA_free(rsa);
  27695. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  27696. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  27697. }
  27698. static void test_wolfSSL_EC_get_builtin_curves(void)
  27699. {
  27700. #if defined(HAVE_ECC) && (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL))
  27701. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  27702. EC_builtin_curve* curves = NULL;
  27703. size_t crv_len = 0;
  27704. size_t i = 0;
  27705. printf(testingFmt, "wolfSSL_EC_get_builtin_curves");
  27706. AssertIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  27707. AssertNotNull(curves = (EC_builtin_curve*)
  27708. XMALLOC(sizeof(EC_builtin_curve)*crv_len, NULL,
  27709. DYNAMIC_TYPE_TMP_BUFFER));
  27710. AssertIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  27711. for (i = 0; i < crv_len; i++)
  27712. {
  27713. if (curves[i].comment != NULL)
  27714. AssertStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  27715. }
  27716. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27717. printf(resultFmt, passed);
  27718. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  27719. #endif /* defined(HAVE_ECC) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  27720. }
  27721. static void test_no_op_functions(void)
  27722. {
  27723. #if defined(OPENSSL_EXTRA)
  27724. printf(testingFmt, "no_op_functions()");
  27725. /* this makes sure wolfSSL can compile and run these no-op functions */
  27726. SSL_load_error_strings();
  27727. ENGINE_load_builtin_engines();
  27728. OpenSSL_add_all_ciphers();
  27729. AssertIntEQ(CRYPTO_malloc_init(), 0);
  27730. printf(resultFmt, passed);
  27731. #endif
  27732. }
  27733. static void test_wolfSSL_CRYPTO_memcmp(void)
  27734. {
  27735. #ifdef OPENSSL_EXTRA
  27736. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  27737. "implementation of TLS/SSL for embedded devices to the cloud.";
  27738. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  27739. "implementation of TLS/SSL for embedded devices to the cloud.";
  27740. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  27741. "implementation of TLS/SSL for embedded devices to the cloud!";
  27742. AssertIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  27743. AssertIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  27744. #endif
  27745. }
  27746. /*----------------------------------------------------------------------------*
  27747. | wolfCrypt ASN
  27748. *----------------------------------------------------------------------------*/
  27749. static void test_wc_GetPkcs8TraditionalOffset(void)
  27750. {
  27751. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  27752. int length, derSz;
  27753. word32 inOutIdx;
  27754. const char* path = "./certs/server-keyPkcs8.der";
  27755. XFILE file;
  27756. byte der[2048];
  27757. printf(testingFmt, "wc_GetPkcs8TraditionalOffset");
  27758. file = XFOPEN(path, "rb");
  27759. AssertTrue(file != XBADFILE);
  27760. derSz = (int)XFREAD(der, 1, sizeof(der), file);
  27761. XFCLOSE(file);
  27762. /* valid case */
  27763. inOutIdx = 0;
  27764. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  27765. AssertIntGT(length, 0);
  27766. /* inOutIdx > sz */
  27767. inOutIdx = 4000;
  27768. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  27769. AssertIntEQ(length, BAD_FUNC_ARG);
  27770. /* null input */
  27771. inOutIdx = 0;
  27772. length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0);
  27773. AssertIntEQ(length, BAD_FUNC_ARG);
  27774. /* invalid input, fill buffer with 1's */
  27775. XMEMSET(der, 1, sizeof(der));
  27776. inOutIdx = 0;
  27777. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  27778. AssertIntEQ(length, ASN_PARSE_E);
  27779. printf(resultFmt, passed);
  27780. #endif /* NO_ASN */
  27781. }
  27782. static void test_wc_SetSubjectRaw(void)
  27783. {
  27784. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  27785. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  27786. const char* joiCertFile = "./certs/test/cert-ext-joi.pem";
  27787. WOLFSSL_X509* x509;
  27788. int peerCertSz;
  27789. const byte* peerCertBuf;
  27790. Cert forgedCert;
  27791. printf(testingFmt, "test_wc_SetSubjectRaw()");
  27792. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_PEM));
  27793. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  27794. AssertIntEQ(0, wc_InitCert(&forgedCert));
  27795. AssertIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  27796. wolfSSL_FreeX509(x509);
  27797. printf(resultFmt, passed);
  27798. #endif
  27799. }
  27800. static void test_wc_GetSubjectRaw(void)
  27801. {
  27802. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  27803. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  27804. Cert cert;
  27805. byte *subjectRaw;
  27806. printf(testingFmt, "test_wc_GetSubjectRaw()");
  27807. AssertIntEQ(0, wc_InitCert(&cert));
  27808. AssertIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  27809. printf(resultFmt, passed);
  27810. #endif
  27811. }
  27812. static void test_wc_SetIssuerRaw(void)
  27813. {
  27814. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  27815. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  27816. const char* joiCertFile = "./certs/test/cert-ext-joi.pem";
  27817. WOLFSSL_X509* x509;
  27818. int peerCertSz;
  27819. const byte* peerCertBuf;
  27820. Cert forgedCert;
  27821. printf(testingFmt, "test_wc_SetIssuerRaw()");
  27822. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_PEM));
  27823. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  27824. AssertIntEQ(0, wc_InitCert(&forgedCert));
  27825. AssertIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  27826. wolfSSL_FreeX509(x509);
  27827. printf(resultFmt, passed);
  27828. #endif
  27829. }
  27830. static void test_wc_SetIssueBuffer(void)
  27831. {
  27832. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  27833. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  27834. const char* joiCertFile = "./certs/test/cert-ext-joi.pem";
  27835. WOLFSSL_X509* x509;
  27836. int peerCertSz;
  27837. const byte* peerCertBuf;
  27838. Cert forgedCert;
  27839. printf(testingFmt, "test_wc_SetIssuerBuffer()");
  27840. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_PEM));
  27841. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  27842. AssertIntEQ(0, wc_InitCert(&forgedCert));
  27843. AssertIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  27844. wolfSSL_FreeX509(x509);
  27845. printf(resultFmt, passed);
  27846. #endif
  27847. }
  27848. /*
  27849. * Testing wc_SetSubjectKeyId
  27850. */
  27851. static void test_wc_SetSubjectKeyId(void)
  27852. {
  27853. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  27854. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  27855. Cert cert;
  27856. const char* file = "certs/ecc-client-keyPub.pem";
  27857. printf(testingFmt, "wc_SetSubjectKeyId()");
  27858. AssertIntEQ(0, wc_InitCert(&cert));
  27859. AssertIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  27860. AssertIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  27861. AssertIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  27862. printf(resultFmt, passed);
  27863. #endif
  27864. } /* END test_wc_SetSubjectKeyId */
  27865. /*
  27866. * Testing wc_SetSubject
  27867. */
  27868. static void test_wc_SetSubject(void)
  27869. {
  27870. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  27871. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  27872. Cert cert;
  27873. const char* file = "./certs/ca-ecc-cert.pem";
  27874. printf(testingFmt, "wc_SetSubject()");
  27875. AssertIntEQ(0, wc_InitCert(&cert));
  27876. AssertIntEQ(0, wc_SetSubject(&cert, file));
  27877. AssertIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  27878. AssertIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  27879. printf(resultFmt, passed);
  27880. #endif
  27881. } /* END test_wc_SetSubject */
  27882. static void test_CheckCertSignature(void)
  27883. {
  27884. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  27885. WOLFSSL_CERT_MANAGER* cm = NULL;
  27886. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  27887. FILE* fp;
  27888. byte cert[4096];
  27889. int certSz;
  27890. #endif
  27891. AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
  27892. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  27893. AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
  27894. #ifndef NO_RSA
  27895. #ifdef USE_CERT_BUFFERS_1024
  27896. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
  27897. sizeof_server_cert_der_1024, NULL, cm));
  27898. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  27899. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  27900. WOLFSSL_FILETYPE_ASN1));
  27901. AssertIntEQ(0, CheckCertSignature(server_cert_der_1024,
  27902. sizeof_server_cert_der_1024, NULL, cm));
  27903. #elif defined(USE_CERT_BUFFERS_2048)
  27904. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
  27905. sizeof_server_cert_der_2048, NULL, cm));
  27906. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  27907. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  27908. WOLFSSL_FILETYPE_ASN1));
  27909. AssertIntEQ(0, CheckCertSignature(server_cert_der_2048,
  27910. sizeof_server_cert_der_2048, NULL, cm));
  27911. #endif
  27912. #endif
  27913. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  27914. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
  27915. sizeof_serv_ecc_der_256, NULL, cm));
  27916. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  27917. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  27918. WOLFSSL_FILETYPE_ASN1));
  27919. AssertIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  27920. NULL, cm));
  27921. #endif
  27922. #if !defined(NO_FILESYSTEM)
  27923. wolfSSL_CertManagerFree(cm);
  27924. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  27925. #ifndef NO_RSA
  27926. AssertNotNull(fp = XFOPEN("./certs/server-cert.der", "rb"));
  27927. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  27928. XFCLOSE(fp);
  27929. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  27930. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  27931. "./certs/ca-cert.pem", NULL));
  27932. AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  27933. #endif
  27934. #ifdef HAVE_ECC
  27935. AssertNotNull(fp = XFOPEN("./certs/server-ecc.der", "rb"));
  27936. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  27937. XFCLOSE(fp);
  27938. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  27939. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  27940. "./certs/ca-ecc-cert.pem", NULL));
  27941. AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  27942. #endif
  27943. #endif
  27944. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  27945. (void)fp;
  27946. (void)cert;
  27947. (void)certSz;
  27948. #endif
  27949. wolfSSL_CertManagerFree(cm);
  27950. #endif
  27951. }
  27952. /*----------------------------------------------------------------------------*
  27953. | wolfCrypt ECC
  27954. *----------------------------------------------------------------------------*/
  27955. static void test_wc_ecc_get_curve_size_from_name(void)
  27956. {
  27957. #ifdef HAVE_ECC
  27958. int ret;
  27959. printf(testingFmt, "wc_ecc_get_curve_size_from_name");
  27960. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  27961. ret = wc_ecc_get_curve_size_from_name("SECP256R1");
  27962. AssertIntEQ(ret, 32);
  27963. #endif
  27964. /* invalid case */
  27965. ret = wc_ecc_get_curve_size_from_name("BADCURVE");
  27966. AssertIntEQ(ret, -1);
  27967. /* NULL input */
  27968. ret = wc_ecc_get_curve_size_from_name(NULL);
  27969. AssertIntEQ(ret, BAD_FUNC_ARG);
  27970. printf(resultFmt, passed);
  27971. #endif /* HAVE_ECC */
  27972. }
  27973. static void test_wc_ecc_get_curve_id_from_name(void)
  27974. {
  27975. #ifdef HAVE_ECC
  27976. int id;
  27977. printf(testingFmt, "wc_ecc_get_curve_id_from_name");
  27978. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  27979. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  27980. AssertIntEQ(id, ECC_SECP256R1);
  27981. #endif
  27982. /* invalid case */
  27983. id = wc_ecc_get_curve_id_from_name("BADCURVE");
  27984. AssertIntEQ(id, -1);
  27985. /* NULL input */
  27986. id = wc_ecc_get_curve_id_from_name(NULL);
  27987. AssertIntEQ(id, BAD_FUNC_ARG);
  27988. printf(resultFmt, passed);
  27989. #endif /* HAVE_ECC */
  27990. }
  27991. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  27992. !defined(HAVE_SELFTEST) && \
  27993. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  27994. static void test_wc_ecc_get_curve_id_from_dp_params(void)
  27995. {
  27996. int id;
  27997. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  27998. int curve_id;
  27999. ecc_key* key;
  28000. const ecc_set_type* params;
  28001. int ret;
  28002. #endif
  28003. WOLFSSL_EC_KEY *ecKey = NULL;
  28004. printf(testingFmt, "wc_ecc_get_curve_id_from_dp_params");
  28005. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  28006. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  28007. AssertIntEQ(id, ECC_SECP256R1);
  28008. ecKey = wolfSSL_EC_KEY_new_by_curve_name(id);
  28009. AssertNotNull(ecKey);
  28010. ret = wolfSSL_EC_KEY_generate_key(ecKey);
  28011. if (ret == 0) {
  28012. /* normal test */
  28013. key = (ecc_key*)ecKey->internal;
  28014. params = key->dp;
  28015. curve_id = wc_ecc_get_curve_id_from_dp_params(params);
  28016. AssertIntEQ(curve_id, id);
  28017. }
  28018. #endif
  28019. /* invalid case, NULL input*/
  28020. id = wc_ecc_get_curve_id_from_dp_params(NULL);
  28021. AssertIntEQ(id, BAD_FUNC_ARG);
  28022. wolfSSL_EC_KEY_free(ecKey);
  28023. printf(resultFmt, passed);
  28024. }
  28025. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  28026. static void test_wc_ecc_get_curve_id_from_params(void)
  28027. {
  28028. #ifdef HAVE_ECC
  28029. int id;
  28030. const byte prime[] =
  28031. {
  28032. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  28033. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28034. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  28035. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  28036. };
  28037. const byte primeInvalid[] =
  28038. {
  28039. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  28040. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28041. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  28042. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  28043. };
  28044. const byte Af[] =
  28045. {
  28046. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  28047. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28048. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  28049. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  28050. };
  28051. const byte Bf[] =
  28052. {
  28053. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  28054. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  28055. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  28056. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  28057. };
  28058. const byte order[] =
  28059. {
  28060. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  28061. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  28062. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  28063. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  28064. };
  28065. const byte Gx[] =
  28066. {
  28067. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  28068. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  28069. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  28070. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  28071. };
  28072. const byte Gy[] =
  28073. {
  28074. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  28075. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  28076. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  28077. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  28078. };
  28079. int cofactor = 1;
  28080. int fieldSize = 256;
  28081. printf(testingFmt, "wc_ecc_get_curve_id_from_params");
  28082. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  28083. id = wc_ecc_get_curve_id_from_params(fieldSize, prime, sizeof(prime),
  28084. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  28085. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  28086. AssertIntEQ(id, ECC_SECP256R1);
  28087. #endif
  28088. /* invalid case, fieldSize = 0 */
  28089. id = wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  28090. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  28091. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  28092. AssertIntEQ(id, ECC_CURVE_INVALID);
  28093. /* invalid case, NULL prime */
  28094. id = wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  28095. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  28096. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  28097. AssertIntEQ(id, BAD_FUNC_ARG);
  28098. /* invalid case, invalid prime */
  28099. id = wc_ecc_get_curve_id_from_params(fieldSize,
  28100. primeInvalid, sizeof(primeInvalid),
  28101. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  28102. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  28103. AssertIntEQ(id, ECC_CURVE_INVALID);
  28104. printf(resultFmt, passed);
  28105. #endif
  28106. }
  28107. static void test_wolfSSL_EVP_PKEY_encrypt(void)
  28108. {
  28109. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  28110. !defined(HAVE_FAST_RSA)
  28111. WOLFSSL_RSA* rsa = NULL;
  28112. WOLFSSL_EVP_PKEY* pkey = NULL;
  28113. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  28114. const char* in = "What is easy to do is easy not to do.";
  28115. size_t inlen = XSTRLEN(in);
  28116. size_t outEncLen = 0;
  28117. byte* outEnc = NULL;
  28118. byte* outDec = NULL;
  28119. size_t outDecLen = 0;
  28120. size_t rsaKeySz = 2048/8; /* Bytes */
  28121. #ifdef WC_RSA_NO_PADDING
  28122. byte* inTmp = NULL;
  28123. byte* outEncTmp = NULL;
  28124. byte* outDecTmp = NULL;
  28125. #endif
  28126. printf(testingFmt, "wolfSSL_EVP_PKEY_encrypt()");
  28127. AssertNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  28128. XMEMSET(outEnc, 0, rsaKeySz);
  28129. AssertNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  28130. XMEMSET(outDec, 0, rsaKeySz);
  28131. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  28132. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  28133. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  28134. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  28135. AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  28136. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  28137. WOLFSSL_SUCCESS);
  28138. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  28139. since ctx uses it.*/
  28140. AssertIntEQ(pkey->references, 2);
  28141. EVP_PKEY_free(pkey);
  28142. AssertIntEQ(pkey->references, 1);
  28143. /* Encrypt data */
  28144. AssertIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  28145. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  28146. /* Decrypt data */
  28147. AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  28148. AssertIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  28149. WOLFSSL_SUCCESS);
  28150. AssertIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  28151. #ifdef WC_RSA_NO_PADDING
  28152. /* The input length must be the same size as the RSA key.*/
  28153. AssertNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  28154. XMEMSET(inTmp, 9, rsaKeySz);
  28155. AssertNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  28156. XMEMSET(outEncTmp, 0, rsaKeySz);
  28157. AssertNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  28158. XMEMSET(outDecTmp, 0, rsaKeySz);
  28159. AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  28160. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  28161. WOLFSSL_SUCCESS);
  28162. AssertIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  28163. WOLFSSL_SUCCESS);
  28164. AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  28165. AssertIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp, outEncLen),
  28166. WOLFSSL_SUCCESS);
  28167. AssertIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  28168. #endif
  28169. EVP_PKEY_CTX_free(ctx);
  28170. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28171. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28172. #ifdef WC_RSA_NO_PADDING
  28173. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28174. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28175. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28176. #endif
  28177. printf(resultFmt, passed);
  28178. #endif
  28179. }
  28180. static void test_wolfSSL_EVP_PKEY_sign(void)
  28181. {
  28182. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  28183. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  28184. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  28185. WOLFSSL_RSA* rsa = NULL;
  28186. WOLFSSL_EVP_PKEY* pkey = NULL;
  28187. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  28188. const char* in = "What is easy to do is easy not to do.";
  28189. size_t inlen = XSTRLEN(in);
  28190. byte hash[SHA256_DIGEST_LENGTH] = {0};
  28191. SHA256_CTX c;
  28192. byte* sig = NULL;
  28193. byte* sigVerify = NULL;
  28194. size_t siglen = 0;
  28195. size_t rsaKeySz = 2048/8; /* Bytes */
  28196. printf(testingFmt, "wolfSSL_EVP_PKEY_sign()");
  28197. sig = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28198. AssertNotNull(sig);
  28199. XMEMSET(sig, 0, rsaKeySz);
  28200. AssertNotNull(sigVerify = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  28201. XMEMSET(sigVerify, 0, rsaKeySz);
  28202. /* Generate hash */
  28203. SHA256_Init(&c);
  28204. SHA256_Update(&c, in, inlen);
  28205. SHA256_Final(hash, &c);
  28206. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  28207. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  28208. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  28209. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  28210. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  28211. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  28212. WOLFSSL_SUCCESS);
  28213. /* Sign data */
  28214. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash, SHA256_DIGEST_LENGTH),
  28215. WOLFSSL_SUCCESS);
  28216. /* Verify signature.
  28217. EVP_PKEY_verify() doesn't exist yet, so use RSA_public_decrypt(). */
  28218. AssertIntEQ(RSA_public_decrypt((int)siglen, sig, sigVerify,
  28219. rsa, RSA_PKCS1_PADDING), SHA256_DIGEST_LENGTH);
  28220. AssertIntEQ(XMEMCMP(hash, sigVerify, SHA256_DIGEST_LENGTH), 0);
  28221. /* error cases */
  28222. AssertIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  28223. ctx->pkey->type = EVP_PKEY_RSA2;
  28224. AssertIntNE(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  28225. AssertIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  28226. WOLFSSL_SUCCESS);
  28227. AssertIntNE(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  28228. WOLFSSL_SUCCESS);
  28229. EVP_PKEY_free(pkey);
  28230. EVP_PKEY_CTX_free(ctx);
  28231. wolfSSL_RSA_free(rsa);
  28232. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28233. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28234. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  28235. printf(resultFmt, passed);
  28236. #endif
  28237. }
  28238. static void test_EVP_PKEY_rsa(void)
  28239. {
  28240. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  28241. WOLFSSL_RSA* rsa;
  28242. WOLFSSL_EVP_PKEY* pkey;
  28243. AssertNotNull(rsa = wolfSSL_RSA_new());
  28244. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  28245. AssertIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  28246. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  28247. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  28248. AssertPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  28249. wolfSSL_EVP_PKEY_free(pkey);
  28250. printf(resultFmt, passed);
  28251. #endif
  28252. }
  28253. static void test_EVP_PKEY_ec(void)
  28254. {
  28255. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  28256. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  28257. WOLFSSL_EC_KEY* ecKey;
  28258. WOLFSSL_EVP_PKEY* pkey;
  28259. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  28260. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  28261. AssertIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  28262. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  28263. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  28264. wolfSSL_EVP_PKEY_free(pkey);
  28265. printf(resultFmt, passed);
  28266. #endif
  28267. #endif
  28268. }
  28269. static void test_EVP_PKEY_cmp(void)
  28270. {
  28271. #if defined(OPENSSL_EXTRA)
  28272. EVP_PKEY *a, *b;
  28273. const unsigned char *in;
  28274. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  28275. in = client_key_der_2048;
  28276. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  28277. &in, (long)sizeof_client_key_der_2048));
  28278. in = client_key_der_2048;
  28279. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  28280. &in, (long)sizeof_client_key_der_2048));
  28281. /* Test success case RSA */
  28282. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  28283. EVP_PKEY_free(b);
  28284. EVP_PKEY_free(a);
  28285. #endif
  28286. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  28287. in = ecc_clikey_der_256;
  28288. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  28289. &in, (long)sizeof_ecc_clikey_der_256));
  28290. in = ecc_clikey_der_256;
  28291. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  28292. &in, (long)sizeof_ecc_clikey_der_256));
  28293. /* Test success case ECC */
  28294. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  28295. EVP_PKEY_free(b);
  28296. EVP_PKEY_free(a);
  28297. #endif
  28298. /* Test failure cases */
  28299. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  28300. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  28301. in = client_key_der_2048;
  28302. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  28303. &in, (long)sizeof_client_key_der_2048));
  28304. in = ecc_clikey_der_256;
  28305. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  28306. &in, (long)sizeof_ecc_clikey_der_256));
  28307. AssertIntNE(EVP_PKEY_cmp(a, b), 0);
  28308. EVP_PKEY_free(b);
  28309. EVP_PKEY_free(a);
  28310. #endif
  28311. /* invalid or empty failure cases */
  28312. a = EVP_PKEY_new();
  28313. b = EVP_PKEY_new();
  28314. AssertIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  28315. AssertIntNE(EVP_PKEY_cmp(a, NULL), 0);
  28316. AssertIntNE(EVP_PKEY_cmp(NULL, b), 0);
  28317. AssertIntNE(EVP_PKEY_cmp(a, b), 0);
  28318. EVP_PKEY_free(b);
  28319. EVP_PKEY_free(a);
  28320. (void)in;
  28321. printf(resultFmt, passed);
  28322. #endif
  28323. }
  28324. static void test_ERR_load_crypto_strings(void)
  28325. {
  28326. #if defined(OPENSSL_ALL)
  28327. ERR_load_crypto_strings();
  28328. printf(resultFmt, passed);
  28329. #endif
  28330. }
  28331. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  28332. static void free_x509(X509* x)
  28333. {
  28334. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  28335. }
  28336. #endif
  28337. static void test_sk_X509(void)
  28338. {
  28339. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  28340. STACK_OF(X509)* s;
  28341. AssertNotNull(s = sk_X509_new());
  28342. AssertIntEQ(sk_X509_num(s), 0);
  28343. sk_X509_free(s);
  28344. AssertNotNull(s = sk_X509_new_null());
  28345. AssertIntEQ(sk_X509_num(s), 0);
  28346. sk_X509_free(s);
  28347. AssertNotNull(s = sk_X509_new());
  28348. sk_X509_push(s, (X509*)1);
  28349. AssertIntEQ(sk_X509_num(s), 1);
  28350. AssertIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  28351. sk_X509_push(s, (X509*)2);
  28352. AssertIntEQ(sk_X509_num(s), 2);
  28353. AssertIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  28354. AssertIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  28355. sk_X509_push(s, (X509*)2);
  28356. sk_X509_pop_free(s, free_x509);
  28357. printf(resultFmt, passed);
  28358. #endif
  28359. }
  28360. static void test_X509_get_signature_nid(void)
  28361. {
  28362. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  28363. X509* x509;
  28364. AssertIntEQ(X509_get_signature_nid(NULL), 0);
  28365. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  28366. SSL_FILETYPE_PEM));
  28367. AssertIntEQ(X509_get_signature_nid(x509), CTC_SHA256wRSA);
  28368. X509_free(x509);
  28369. printf(resultFmt, passed);
  28370. #endif
  28371. }
  28372. static void test_X509_REQ(void)
  28373. {
  28374. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  28375. defined(WOLFSSL_CERT_REQ)
  28376. X509_NAME* name;
  28377. #if !defined(NO_RSA) || defined(HAVE_ECC)
  28378. X509_REQ* req;
  28379. EVP_PKEY* priv;
  28380. EVP_PKEY* pub;
  28381. unsigned char* der = NULL;
  28382. #endif
  28383. #ifndef NO_RSA
  28384. #ifdef USE_CERT_BUFFERS_1024
  28385. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  28386. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  28387. #elif defined(USE_CERT_BUFFERS_2048)
  28388. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  28389. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  28390. #endif
  28391. #endif
  28392. #ifdef HAVE_ECC
  28393. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  28394. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  28395. int len;
  28396. #endif
  28397. AssertNotNull(name = X509_NAME_new());
  28398. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  28399. (byte*)"wolfssl.com", 11, 0, 1),
  28400. WOLFSSL_SUCCESS);
  28401. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  28402. (byte*)"support@wolfssl.com", 19, -1,
  28403. 1), WOLFSSL_SUCCESS);
  28404. #ifndef NO_RSA
  28405. AssertNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  28406. (long)sizeof_client_key_der_2048));
  28407. AssertNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  28408. (long)sizeof_client_keypub_der_2048));
  28409. AssertNotNull(req = X509_REQ_new());
  28410. AssertIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  28411. AssertIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  28412. AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  28413. AssertIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  28414. AssertIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  28415. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  28416. AssertIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  28417. AssertIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  28418. AssertIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  28419. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  28420. AssertIntEQ(i2d_X509_REQ(req, &der), 643);
  28421. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  28422. der = NULL;
  28423. X509_REQ_free(NULL);
  28424. X509_REQ_free(req);
  28425. EVP_PKEY_free(pub);
  28426. EVP_PKEY_free(priv);
  28427. #endif
  28428. #ifdef HAVE_ECC
  28429. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  28430. sizeof_ecc_clikey_der_256));
  28431. AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  28432. sizeof_ecc_clikeypub_der_256));
  28433. AssertNotNull(req = X509_REQ_new());
  28434. AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  28435. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  28436. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  28437. /* Signature is random and may be shorter or longer. */
  28438. AssertIntGE((len = i2d_X509_REQ(req, &der)), 245);
  28439. AssertIntLE(len, 253);
  28440. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  28441. X509_REQ_free(req);
  28442. EVP_PKEY_free(pub);
  28443. EVP_PKEY_free(priv);
  28444. #ifdef FP_ECC
  28445. wc_ecc_fp_free();
  28446. #endif
  28447. #endif /* HAVE_ECC */
  28448. X509_NAME_free(name);
  28449. printf(resultFmt, passed);
  28450. #endif
  28451. }
  28452. static void test_wolfssl_PKCS7(void)
  28453. {
  28454. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  28455. PKCS7* pkcs7;
  28456. byte data[FOURK_BUF];
  28457. word32 len = sizeof(data);
  28458. const byte* p = data;
  28459. byte content[] = "Test data to encode.";
  28460. AssertIntGT((len = CreatePKCS7SignedData(data, len, content,
  28461. (word32)sizeof(content),
  28462. 0, 0)), 0);
  28463. AssertNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
  28464. AssertNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  28465. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  28466. AssertIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  28467. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  28468. PKCS7_free(pkcs7);
  28469. /* fail case, without PKCS7_NOVERIFY */
  28470. p = data;
  28471. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  28472. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  28473. 0), WOLFSSL_FAILURE);
  28474. PKCS7_free(pkcs7);
  28475. /* success case, with PKCS7_NOVERIFY */
  28476. p = data;
  28477. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  28478. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  28479. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  28480. PKCS7_free(NULL);
  28481. PKCS7_free(pkcs7);
  28482. printf(resultFmt, passed);
  28483. #endif
  28484. }
  28485. static void test_wolfSSL_PKCS7_SIGNED_new(void)
  28486. {
  28487. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  28488. PKCS7_SIGNED* pkcs7;
  28489. printf(testingFmt, "wolfSSL_PKCS7_SIGNED_new()");
  28490. pkcs7 = PKCS7_SIGNED_new();
  28491. AssertNotNull(pkcs7);
  28492. AssertIntEQ(pkcs7->contentOID, SIGNED_DATA);
  28493. PKCS7_SIGNED_free(pkcs7);
  28494. printf(resultFmt, passed);
  28495. #endif
  28496. }
  28497. static void test_wolfSSL_PEM_write_bio_PKCS7(void)
  28498. {
  28499. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  28500. PKCS7* pkcs7 = NULL;
  28501. BIO* bio = NULL;
  28502. const byte* cert_buf = NULL;
  28503. int ret = 0;
  28504. WC_RNG rng;
  28505. const byte data[] = { /* Hello World */
  28506. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  28507. 0x72,0x6c,0x64
  28508. };
  28509. #ifndef NO_RSA
  28510. #if defined(USE_CERT_BUFFERS_2048)
  28511. byte key[sizeof_client_key_der_2048];
  28512. byte cert[sizeof_client_cert_der_2048];
  28513. word32 keySz = (word32)sizeof(key);
  28514. word32 certSz = (word32)sizeof(cert);
  28515. XMEMSET(key, 0, keySz);
  28516. XMEMSET(cert, 0, certSz);
  28517. XMEMCPY(key, client_key_der_2048, keySz);
  28518. XMEMCPY(cert, client_cert_der_2048, certSz);
  28519. #elif defined(USE_CERT_BUFFERS_1024)
  28520. byte key[sizeof_client_key_der_1024];
  28521. byte cert[sizeof_client_cert_der_1024];
  28522. word32 keySz = (word32)sizeof(key);
  28523. word32 certSz = (word32)sizeof(cert);
  28524. XMEMSET(key, 0, keySz);
  28525. XMEMSET(cert, 0, certSz);
  28526. XMEMCPY(key, client_key_der_1024, keySz);
  28527. XMEMCPY(cert, client_cert_der_1024, certSz);
  28528. #else
  28529. unsigned char cert[ONEK_BUF];
  28530. unsigned char key[ONEK_BUF];
  28531. XFILE fp;
  28532. int certSz;
  28533. int keySz;
  28534. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  28535. AssertTrue((fp != XBADFILE));
  28536. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  28537. XFCLOSE(fp);
  28538. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  28539. AssertTrue(fp != XBADFILE);
  28540. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  28541. XFCLOSE(fp);
  28542. #endif
  28543. #elif defined(HAVE_ECC)
  28544. #if defined(USE_CERT_BUFFERS_256)
  28545. unsigned char cert[sizeof_cliecc_cert_der_256];
  28546. unsigned char key[sizeof_ecc_clikey_der_256];
  28547. int certSz = (int)sizeof(cert);
  28548. int keySz = (int)sizeof(key);
  28549. XMEMSET(cert, 0, certSz);
  28550. XMEMSET(key, 0, keySz);
  28551. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  28552. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  28553. #else
  28554. unsigned char cert[ONEK_BUF];
  28555. unsigned char key[ONEK_BUF];
  28556. XFILE fp;
  28557. int certSz, keySz;
  28558. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  28559. AssertTrue(fp != XBADFILE);
  28560. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  28561. XFCLOSE(fp);
  28562. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  28563. AssertTrue(fp != XBADFILE);
  28564. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  28565. XFCLOSE(fp);
  28566. #endif
  28567. #else
  28568. #error PKCS7 requires ECC or RSA
  28569. #endif
  28570. printf(testingFmt, "wolfSSL_PEM_write_bio_PKCS7()");
  28571. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  28572. /* initialize with DER encoded cert */
  28573. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  28574. /* init rng */
  28575. AssertIntEQ(wc_InitRng(&rng), 0);
  28576. pkcs7->rng = &rng;
  28577. pkcs7->content = (byte*)data; /* not used for ex */
  28578. pkcs7->contentSz = (word32)sizeof(data);
  28579. pkcs7->contentOID = SIGNED_DATA;
  28580. pkcs7->privateKey = key;
  28581. pkcs7->privateKeySz = (word32)sizeof(key);
  28582. pkcs7->encryptOID = RSAk;
  28583. pkcs7->hashOID = SHAh;
  28584. pkcs7->signedAttribs = NULL;
  28585. pkcs7->signedAttribsSz = 0;
  28586. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  28587. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  28588. AssertIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  28589. /* Read PKCS#7 PEM from BIO */
  28590. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  28591. AssertIntGE(ret, 0);
  28592. BIO_free(bio);
  28593. wc_PKCS7_Free(pkcs7);
  28594. wc_FreeRng(&rng);
  28595. printf(resultFmt, passed);
  28596. #endif
  28597. }
  28598. /*----------------------------------------------------------------------------*
  28599. | Certificate Failure Checks
  28600. *----------------------------------------------------------------------------*/
  28601. #ifndef NO_CERTS
  28602. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  28603. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  28604. int type)
  28605. {
  28606. int ret;
  28607. WOLFSSL_CERT_MANAGER* cm = NULL;
  28608. switch (type) {
  28609. case TESTING_RSA:
  28610. #ifdef NO_RSA
  28611. printf("RSA disabled, skipping test\n");
  28612. return ASN_SIG_CONFIRM_E;
  28613. #else
  28614. break;
  28615. #endif
  28616. case TESTING_ECC:
  28617. #ifndef HAVE_ECC
  28618. printf("ECC disabled, skipping test\n");
  28619. return ASN_SIG_CONFIRM_E;
  28620. #else
  28621. break;
  28622. #endif
  28623. default:
  28624. printf("Bad function argument\n");
  28625. return BAD_FUNC_ARG;
  28626. }
  28627. cm = wolfSSL_CertManagerNew();
  28628. if (cm == NULL) {
  28629. printf("wolfSSL_CertManagerNew failed\n");
  28630. return -1;
  28631. }
  28632. #ifndef NO_FILESYSTEM
  28633. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  28634. if (ret != WOLFSSL_SUCCESS) {
  28635. printf("wolfSSL_CertManagerLoadCA failed\n");
  28636. wolfSSL_CertManagerFree(cm);
  28637. return ret;
  28638. }
  28639. #else
  28640. (void)ca;
  28641. #endif
  28642. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz, WOLFSSL_FILETYPE_ASN1);
  28643. /* Let AssertIntEQ handle return code */
  28644. wolfSSL_CertManagerFree(cm);
  28645. return ret;
  28646. }
  28647. static int test_RsaSigFailure_cm(void)
  28648. {
  28649. int ret = 0;
  28650. const char* ca_cert = "./certs/ca-cert.pem";
  28651. const char* server_cert = "./certs/server-cert.der";
  28652. byte* cert_buf = NULL;
  28653. size_t cert_sz = 0;
  28654. ret = load_file(server_cert, &cert_buf, &cert_sz);
  28655. if (ret == 0) {
  28656. /* corrupt DER - invert last byte, which is signature */
  28657. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  28658. /* test bad cert */
  28659. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA);
  28660. }
  28661. printf("Signature failure test: RSA: Ret %d\n", ret);
  28662. if (cert_buf)
  28663. free(cert_buf);
  28664. return ret;
  28665. }
  28666. static int test_EccSigFailure_cm(void)
  28667. {
  28668. int ret = 0;
  28669. /* self-signed ECC cert, so use server cert as CA */
  28670. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  28671. const char* server_cert = "./certs/server-ecc.der";
  28672. byte* cert_buf = NULL;
  28673. size_t cert_sz = 0;
  28674. ret = load_file(server_cert, &cert_buf, &cert_sz);
  28675. if (ret == 0) {
  28676. /* corrupt DER - invert last byte, which is signature */
  28677. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  28678. /* test bad cert */
  28679. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC);
  28680. }
  28681. printf("Signature failure test: ECC: Ret %d\n", ret);
  28682. if (cert_buf)
  28683. free(cert_buf);
  28684. #ifdef FP_ECC
  28685. wc_ecc_fp_free();
  28686. #endif
  28687. return ret;
  28688. }
  28689. #endif /* NO_CERTS */
  28690. #ifdef WOLFSSL_TLS13
  28691. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  28692. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  28693. #endif
  28694. #ifdef WOLFSSL_EARLY_DATA
  28695. static const char earlyData[] = "Early Data";
  28696. static char earlyDataBuffer[1];
  28697. #endif
  28698. static int test_tls13_apis(void)
  28699. {
  28700. int ret = 0;
  28701. #ifndef WOLFSSL_NO_TLS12
  28702. #ifndef NO_WOLFSSL_CLIENT
  28703. WOLFSSL_CTX* clientTls12Ctx;
  28704. WOLFSSL* clientTls12Ssl;
  28705. #endif
  28706. #ifndef NO_WOLFSSL_SERVER
  28707. WOLFSSL_CTX* serverTls12Ctx;
  28708. WOLFSSL* serverTls12Ssl;
  28709. #endif
  28710. #endif
  28711. #ifndef NO_WOLFSSL_CLIENT
  28712. WOLFSSL_CTX* clientCtx;
  28713. WOLFSSL* clientSsl;
  28714. #endif
  28715. #ifndef NO_WOLFSSL_SERVER
  28716. WOLFSSL_CTX* serverCtx;
  28717. WOLFSSL* serverSsl;
  28718. #ifndef NO_CERTS
  28719. const char* ourCert = svrCertFile;
  28720. const char* ourKey = svrKeyFile;
  28721. #endif
  28722. #endif
  28723. #ifdef WOLFSSL_EARLY_DATA
  28724. int outSz;
  28725. #endif
  28726. int groups[2] = { WOLFSSL_ECC_X25519, WOLFSSL_ECC_X448 };
  28727. int numGroups = 2;
  28728. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  28729. char groupList[] = "P-521:P-384:P-256";
  28730. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  28731. #ifndef WOLFSSL_NO_TLS12
  28732. #ifndef NO_WOLFSSL_CLIENT
  28733. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  28734. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  28735. #endif
  28736. #ifndef NO_WOLFSSL_SERVER
  28737. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  28738. #ifndef NO_CERTS
  28739. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  28740. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey, WOLFSSL_FILETYPE_PEM);
  28741. #endif
  28742. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  28743. #endif
  28744. #endif
  28745. #ifndef NO_WOLFSSL_CLIENT
  28746. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  28747. clientSsl = wolfSSL_new(clientCtx);
  28748. #endif
  28749. #ifndef NO_WOLFSSL_SERVER
  28750. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  28751. #ifndef NO_CERTS
  28752. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  28753. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  28754. #endif
  28755. serverSsl = wolfSSL_new(serverCtx);
  28756. #endif
  28757. #ifdef WOLFSSL_SEND_HRR_COOKIE
  28758. AssertIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  28759. #ifndef NO_WOLFSSL_CLIENT
  28760. AssertIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  28761. #endif
  28762. #ifndef NO_WOLFSSL_SERVER
  28763. #ifndef WOLFSSL_NO_TLS12
  28764. AssertIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0), BAD_FUNC_ARG);
  28765. #endif
  28766. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  28767. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  28768. WOLFSSL_SUCCESS);
  28769. #endif
  28770. #endif
  28771. #ifdef HAVE_ECC
  28772. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  28773. #ifndef NO_WOLFSSL_SERVER
  28774. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1),
  28775. WOLFSSL_SUCCESS);
  28776. #endif
  28777. #ifndef NO_WOLFSSL_CLIENT
  28778. #ifndef WOLFSSL_NO_TLS12
  28779. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  28780. WOLFSSL_SUCCESS);
  28781. #endif
  28782. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  28783. WOLFSSL_SUCCESS);
  28784. #endif
  28785. #elif defined(HAVE_CURVE25519)
  28786. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  28787. #ifndef NO_WOLFSSL_SERVER
  28788. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  28789. WOLFSSL_SUCCESS);
  28790. #endif
  28791. #ifndef NO_WOLFSSL_CLIENT
  28792. #ifndef WOLFSSL_NO_TLS12
  28793. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  28794. WOLFSSL_SUCCESS);
  28795. #endif
  28796. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  28797. WOLFSSL_SUCCESS);
  28798. #endif
  28799. #elif defined(HAVE_CURVE448)
  28800. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  28801. #ifndef NO_WOLFSSL_SERVER
  28802. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  28803. WOLFSSL_SUCCESS);
  28804. #endif
  28805. #ifndef NO_WOLFSSL_CLIENT
  28806. #ifndef WOLFSSL_NO_TLS12
  28807. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  28808. WOLFSSL_SUCCESS);
  28809. #endif
  28810. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  28811. WOLFSSL_SUCCESS);
  28812. #endif
  28813. #else
  28814. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  28815. #ifndef NO_WOLFSSL_CLIENT
  28816. #ifndef WOLFSSL_NO_TLS12
  28817. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  28818. NOT_COMPILED_IN);
  28819. #endif
  28820. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  28821. NOT_COMPILED_IN);
  28822. #endif
  28823. #endif
  28824. AssertIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  28825. #ifndef NO_WOLFSSL_SERVER
  28826. AssertIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  28827. #endif
  28828. #ifndef NO_WOLFSSL_CLIENT
  28829. #ifndef WOLFSSL_NO_TLS12
  28830. AssertIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  28831. #endif
  28832. AssertIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  28833. #endif
  28834. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  28835. #ifndef NO_WOLFSSL_CLIENT
  28836. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  28837. #endif
  28838. #ifndef NO_WOLFSSL_SERVER
  28839. #ifndef WOLFSSL_NO_TLS12
  28840. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  28841. #endif
  28842. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  28843. #endif
  28844. AssertIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  28845. #ifndef NO_WOLFSSL_CLIENT
  28846. AssertIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  28847. #endif
  28848. #ifndef NO_WOLFSSL_SERVER
  28849. #ifndef WOLFSSL_NO_TLS12
  28850. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  28851. #endif
  28852. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  28853. #endif
  28854. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  28855. #ifndef NO_WOLFSSL_CLIENT
  28856. #ifndef WOLFSSL_NO_TLS12
  28857. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  28858. #endif
  28859. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  28860. #endif
  28861. #ifndef NO_WOLFSSL_SERVER
  28862. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  28863. #endif
  28864. AssertIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  28865. #ifndef NO_WOLFSSL_CLIENT
  28866. #ifndef WOLFSSL_NO_TLS12
  28867. AssertIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  28868. #endif
  28869. AssertIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  28870. #endif
  28871. #ifndef NO_WOLFSSL_SERVER
  28872. AssertIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  28873. #endif
  28874. AssertIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  28875. #ifndef NO_WOLFSSL_CLIENT
  28876. #ifndef WOLFSSL_NO_TLS12
  28877. AssertIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  28878. #endif
  28879. AssertIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  28880. #endif
  28881. #ifndef NO_WOLFSSL_SERVER
  28882. AssertIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  28883. #endif
  28884. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  28885. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  28886. #ifndef NO_WOLFSSL_SERVER
  28887. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  28888. #endif
  28889. #ifndef NO_WOLFSSL_CLIENT
  28890. #ifndef WOLFSSL_NO_TLS12
  28891. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  28892. BAD_FUNC_ARG);
  28893. #endif
  28894. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  28895. #endif
  28896. AssertIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  28897. #ifndef NO_WOLFSSL_SERVER
  28898. AssertIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  28899. #endif
  28900. #ifndef NO_WOLFSSL_CLIENT
  28901. #ifndef WOLFSSL_NO_TLS12
  28902. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  28903. BAD_FUNC_ARG);
  28904. #endif
  28905. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  28906. #endif
  28907. AssertIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  28908. #ifndef NO_WOLFSSL_CLIENT
  28909. AssertIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  28910. #endif
  28911. #ifndef NO_WOLFSSL_SERVER
  28912. #ifndef WOLFSSL_NO_TLS12
  28913. AssertIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  28914. BAD_FUNC_ARG);
  28915. #endif
  28916. AssertIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  28917. #endif
  28918. #endif
  28919. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  28920. AssertIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  28921. #ifndef NO_WOLFSSL_SERVER
  28922. AssertIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  28923. #endif
  28924. #ifndef NO_WOLFSSL_CLIENT
  28925. #ifndef WOLFSSL_NO_TLS12
  28926. AssertIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  28927. #endif
  28928. AssertIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  28929. #endif
  28930. #endif
  28931. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  28932. #ifndef NO_WOLFSSL_CLIENT
  28933. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  28934. #endif
  28935. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  28936. #ifndef NO_WOLFSSL_CLIENT
  28937. #ifndef WOLFSSL_NO_TLS12
  28938. AssertIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  28939. BAD_FUNC_ARG);
  28940. #endif
  28941. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  28942. WOLFSSL_MAX_GROUP_COUNT + 1),
  28943. BAD_FUNC_ARG);
  28944. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  28945. WOLFSSL_SUCCESS);
  28946. #endif
  28947. #ifndef NO_WOLFSSL_SERVER
  28948. AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  28949. WOLFSSL_SUCCESS);
  28950. #endif
  28951. AssertIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  28952. #ifndef NO_WOLFSSL_CLIENT
  28953. AssertIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  28954. #endif
  28955. AssertIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  28956. #ifndef NO_WOLFSSL_CLIENT
  28957. #ifndef WOLFSSL_NO_TLS12
  28958. AssertIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  28959. BAD_FUNC_ARG);
  28960. #endif
  28961. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups,
  28962. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  28963. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  28964. WOLFSSL_SUCCESS);
  28965. #endif
  28966. #ifndef NO_WOLFSSL_SERVER
  28967. AssertIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  28968. WOLFSSL_SUCCESS);
  28969. #endif
  28970. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  28971. AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  28972. #ifndef NO_WOLFSSL_CLIENT
  28973. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL), WOLFSSL_FAILURE);
  28974. #endif
  28975. AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  28976. #ifndef NO_WOLFSSL_CLIENT
  28977. #ifndef WOLFSSL_NO_TLS12
  28978. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  28979. WOLFSSL_FAILURE);
  28980. #endif
  28981. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  28982. WOLFSSL_SUCCESS);
  28983. #endif
  28984. #ifndef NO_WOLFSSL_SERVER
  28985. AssertIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  28986. WOLFSSL_SUCCESS);
  28987. #endif
  28988. AssertIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  28989. #ifndef NO_WOLFSSL_CLIENT
  28990. AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  28991. #endif
  28992. AssertIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  28993. #ifndef NO_WOLFSSL_CLIENT
  28994. #ifndef WOLFSSL_NO_TLS12
  28995. AssertIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  28996. WOLFSSL_FAILURE);
  28997. #endif
  28998. AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  28999. WOLFSSL_SUCCESS);
  29000. #endif
  29001. #ifndef NO_WOLFSSL_SERVER
  29002. AssertIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  29003. WOLFSSL_SUCCESS);
  29004. #endif
  29005. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  29006. #ifdef WOLFSSL_EARLY_DATA
  29007. AssertIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  29008. #ifndef NO_WOLFSSL_CLIENT
  29009. AssertIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  29010. #endif
  29011. #ifndef NO_WOLFSSL_SERVER
  29012. #ifndef WOLFSSL_NO_TLS12
  29013. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  29014. BAD_FUNC_ARG);
  29015. #endif
  29016. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 0), 0);
  29017. #endif
  29018. AssertIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  29019. #ifndef NO_WOLFSSL_CLIENT
  29020. AssertIntEQ(wolfSSL_set_max_early_data(clientSsl, 0), SIDE_ERROR);
  29021. #endif
  29022. #ifndef NO_WOLFSSL_SERVER
  29023. #ifndef WOLFSSL_NO_TLS12
  29024. AssertIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  29025. #endif
  29026. AssertIntEQ(wolfSSL_set_max_early_data(serverSsl, 0), 0);
  29027. #endif
  29028. AssertIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  29029. &outSz), BAD_FUNC_ARG);
  29030. #ifndef NO_WOLFSSL_CLIENT
  29031. AssertIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  29032. &outSz), BAD_FUNC_ARG);
  29033. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  29034. BAD_FUNC_ARG);
  29035. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  29036. sizeof(earlyData), NULL),
  29037. BAD_FUNC_ARG);
  29038. #endif
  29039. #ifndef NO_WOLFSSL_SERVER
  29040. AssertIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  29041. sizeof(earlyData), &outSz),
  29042. SIDE_ERROR);
  29043. #endif
  29044. #ifndef NO_WOLFSSL_CLIENT
  29045. #ifndef WOLFSSL_NO_TLS12
  29046. AssertIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  29047. sizeof(earlyData), &outSz),
  29048. BAD_FUNC_ARG);
  29049. #endif
  29050. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  29051. sizeof(earlyData), &outSz),
  29052. WOLFSSL_FATAL_ERROR);
  29053. #endif
  29054. AssertIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  29055. sizeof(earlyDataBuffer), &outSz),
  29056. BAD_FUNC_ARG);
  29057. #ifndef NO_WOLFSSL_SERVER
  29058. AssertIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  29059. sizeof(earlyDataBuffer), &outSz),
  29060. BAD_FUNC_ARG);
  29061. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1, &outSz),
  29062. BAD_FUNC_ARG);
  29063. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  29064. sizeof(earlyDataBuffer), NULL),
  29065. BAD_FUNC_ARG);
  29066. #endif
  29067. #ifndef NO_WOLFSSL_CLIENT
  29068. AssertIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  29069. sizeof(earlyDataBuffer), &outSz),
  29070. SIDE_ERROR);
  29071. #endif
  29072. #ifndef NO_WOLFSSL_SERVER
  29073. #ifndef WOLFSSL_NO_TLS12
  29074. AssertIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  29075. sizeof(earlyDataBuffer), &outSz),
  29076. BAD_FUNC_ARG);
  29077. #endif
  29078. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  29079. sizeof(earlyDataBuffer), &outSz),
  29080. WOLFSSL_FATAL_ERROR);
  29081. #endif
  29082. #endif
  29083. #ifndef NO_WOLFSSL_SERVER
  29084. wolfSSL_free(serverSsl);
  29085. wolfSSL_CTX_free(serverCtx);
  29086. #endif
  29087. #ifndef NO_WOLFSSL_CLIENT
  29088. wolfSSL_free(clientSsl);
  29089. wolfSSL_CTX_free(clientCtx);
  29090. #endif
  29091. #ifndef WOLFSSL_NO_TLS12
  29092. #ifndef NO_WOLFSSL_SERVER
  29093. wolfSSL_free(serverTls12Ssl);
  29094. wolfSSL_CTX_free(serverTls12Ctx);
  29095. #endif
  29096. #ifndef NO_WOLFSSL_CLIENT
  29097. wolfSSL_free(clientTls12Ssl);
  29098. wolfSSL_CTX_free(clientTls12Ctx);
  29099. #endif
  29100. #endif
  29101. return ret;
  29102. }
  29103. #endif
  29104. #ifdef HAVE_PK_CALLBACKS
  29105. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  29106. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  29107. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  29108. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  29109. const unsigned char* priv, unsigned int privSz,
  29110. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  29111. unsigned char* out, unsigned int* outlen,
  29112. void* ctx)
  29113. {
  29114. /* Test fail when context associated with WOLFSSL is NULL */
  29115. if (ctx == NULL) {
  29116. return -1;
  29117. }
  29118. (void)ssl;
  29119. /* return 0 on success */
  29120. return wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  29121. };
  29122. static void test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  29123. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  29124. #ifdef WOLFSSL_AES_128
  29125. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  29126. WOLFSSL_SUCCESS);
  29127. #endif
  29128. #ifdef WOLFSSL_AES_256
  29129. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  29130. WOLFSSL_SUCCESS);
  29131. #endif
  29132. }
  29133. static void test_dh_ssl_setup(WOLFSSL* ssl)
  29134. {
  29135. static int dh_test_ctx = 1;
  29136. int ret;
  29137. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  29138. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  29139. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  29140. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  29141. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  29142. }
  29143. }
  29144. static void test_dh_ssl_setup_fail(WOLFSSL* ssl)
  29145. {
  29146. int ret;
  29147. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  29148. AssertNull(wolfSSL_GetDhAgreeCtx(ssl));
  29149. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  29150. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  29151. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  29152. }
  29153. }
  29154. #endif
  29155. static void test_DhCallbacks(void)
  29156. {
  29157. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  29158. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  29159. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  29160. WOLFSSL_CTX *ctx;
  29161. WOLFSSL *ssl;
  29162. tcp_ready ready;
  29163. func_args server_args;
  29164. func_args client_args;
  29165. THREAD_TYPE serverThread;
  29166. callback_functions func_cb_client;
  29167. callback_functions func_cb_server;
  29168. int test;
  29169. printf(testingFmt, "test_DhCallbacks");
  29170. #ifndef NO_WOLFSSL_CLIENT
  29171. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  29172. #else
  29173. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  29174. #endif
  29175. AssertIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  29176. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  29177. /* load client ca cert */
  29178. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  29179. WOLFSSL_SUCCESS);
  29180. /* test with NULL arguments */
  29181. wolfSSL_SetDhAgreeCtx(NULL, &test);
  29182. AssertNull(wolfSSL_GetDhAgreeCtx(NULL));
  29183. /* test success case */
  29184. test = 1;
  29185. AssertNotNull(ssl = wolfSSL_new(ctx));
  29186. wolfSSL_SetDhAgreeCtx(ssl, &test);
  29187. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  29188. wolfSSL_free(ssl);
  29189. wolfSSL_CTX_free(ctx);
  29190. /* test a connection where callback is used */
  29191. #ifdef WOLFSSL_TIRTOS
  29192. fdOpenSession(Task_self());
  29193. #endif
  29194. XMEMSET(&server_args, 0, sizeof(func_args));
  29195. XMEMSET(&client_args, 0, sizeof(func_args));
  29196. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  29197. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  29198. StartTCP();
  29199. InitTcpReady(&ready);
  29200. #if defined(USE_WINDOWS_API)
  29201. /* use RNG to get random port if using windows */
  29202. ready.port = GetRandomPort();
  29203. #endif
  29204. server_args.signal = &ready;
  29205. client_args.signal = &ready;
  29206. server_args.return_code = TEST_FAIL;
  29207. client_args.return_code = TEST_FAIL;
  29208. /* set callbacks to use DH functions */
  29209. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  29210. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  29211. #ifndef WOLFSSL_NO_TLS12
  29212. func_cb_client.method = wolfTLSv1_2_client_method;
  29213. #else
  29214. func_cb_client.method = wolfTLSv1_3_client_method;
  29215. #endif
  29216. client_args.callbacks = &func_cb_client;
  29217. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  29218. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  29219. #ifndef WOLFSSL_NO_TLS12
  29220. func_cb_server.method = wolfTLSv1_2_server_method;
  29221. #else
  29222. func_cb_server.method = wolfTLSv1_3_server_method;
  29223. #endif
  29224. server_args.callbacks = &func_cb_server;
  29225. start_thread(test_server_nofail, &server_args, &serverThread);
  29226. wait_tcp_ready(&server_args);
  29227. test_client_nofail(&client_args, NULL);
  29228. join_thread(serverThread);
  29229. AssertTrue(client_args.return_code);
  29230. AssertTrue(server_args.return_code);
  29231. FreeTcpReady(&ready);
  29232. #ifdef WOLFSSL_TIRTOS
  29233. fdOpenSession(Task_self());
  29234. #endif
  29235. /* now set user ctx to not be 1 so that the callback returns fail case */
  29236. #ifdef WOLFSSL_TIRTOS
  29237. fdOpenSession(Task_self());
  29238. #endif
  29239. XMEMSET(&server_args, 0, sizeof(func_args));
  29240. XMEMSET(&client_args, 0, sizeof(func_args));
  29241. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  29242. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  29243. StartTCP();
  29244. InitTcpReady(&ready);
  29245. #if defined(USE_WINDOWS_API)
  29246. /* use RNG to get random port if using windows */
  29247. ready.port = GetRandomPort();
  29248. #endif
  29249. server_args.signal = &ready;
  29250. client_args.signal = &ready;
  29251. server_args.return_code = TEST_FAIL;
  29252. client_args.return_code = TEST_FAIL;
  29253. /* set callbacks to use DH functions */
  29254. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  29255. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  29256. #ifndef WOLFSSL_NO_TLS12
  29257. func_cb_client.method = wolfTLSv1_2_client_method;
  29258. #else
  29259. func_cb_client.method = wolfTLSv1_3_client_method;
  29260. #endif
  29261. client_args.callbacks = &func_cb_client;
  29262. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  29263. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  29264. #ifndef WOLFSSL_NO_TLS12
  29265. func_cb_server.method = wolfTLSv1_2_server_method;
  29266. #else
  29267. func_cb_server.method = wolfTLSv1_3_server_method;
  29268. #endif
  29269. server_args.callbacks = &func_cb_server;
  29270. start_thread(test_server_nofail, &server_args, &serverThread);
  29271. wait_tcp_ready(&server_args);
  29272. test_client_nofail(&client_args, NULL);
  29273. join_thread(serverThread);
  29274. AssertIntEQ(client_args.return_code, TEST_FAIL);
  29275. AssertIntEQ(server_args.return_code, TEST_FAIL);
  29276. FreeTcpReady(&ready);
  29277. #ifdef WOLFSSL_TIRTOS
  29278. fdOpenSession(Task_self());
  29279. #endif
  29280. printf(resultFmt, passed);
  29281. #endif
  29282. }
  29283. #endif /* HAVE_PK_CALLBACKS */
  29284. #ifdef HAVE_HASHDRBG
  29285. #ifdef TEST_RESEED_INTERVAL
  29286. static int test_wc_RNG_GenerateBlock_Reseed(void)
  29287. {
  29288. int i, ret;
  29289. WC_RNG rng;
  29290. byte key[32];
  29291. ret = wc_InitRng(&rng);
  29292. if (ret == 0) {
  29293. for(i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  29294. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  29295. if (ret != 0) {
  29296. break;
  29297. }
  29298. }
  29299. }
  29300. wc_FreeRng(&rng);
  29301. return ret;
  29302. }
  29303. #endif /* TEST_RESEED_INTERVAL */
  29304. static int test_wc_RNG_GenerateBlock(void)
  29305. {
  29306. int i, ret;
  29307. WC_RNG rng;
  29308. byte key[32];
  29309. ret = wc_InitRng(&rng);
  29310. if (ret == 0) {
  29311. for(i = 0; i < 10; i++) {
  29312. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  29313. if (ret != 0) {
  29314. break;
  29315. }
  29316. }
  29317. }
  29318. wc_FreeRng(&rng);
  29319. (void)rng; /* for WC_NO_RNG case */
  29320. (void)key;
  29321. return ret;
  29322. }
  29323. #endif
  29324. /*
  29325. * Testing get_rand_digit
  29326. */
  29327. static int test_get_rand_digit (void)
  29328. {
  29329. int ret = 0;
  29330. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  29331. WC_RNG rng;
  29332. mp_digit d;
  29333. printf(testingFmt, "get_rand_digit()");
  29334. ret = wc_InitRng(&rng);
  29335. if (ret == 0) {
  29336. ret = get_rand_digit(&rng, &d);
  29337. }
  29338. if (ret == 0) {
  29339. ret = get_rand_digit(NULL, NULL);
  29340. if (ret == BAD_FUNC_ARG) {
  29341. ret = 0;
  29342. }
  29343. }
  29344. if (ret == 0) {
  29345. ret = get_rand_digit(NULL, &d);
  29346. if (ret == BAD_FUNC_ARG) {
  29347. ret = 0;
  29348. }
  29349. }
  29350. if (ret == 0) {
  29351. ret = get_rand_digit(&rng, NULL);
  29352. if (ret == BAD_FUNC_ARG) {
  29353. ret = 0;
  29354. }
  29355. }
  29356. if (ret == 0) {
  29357. ret = wc_FreeRng(&rng);
  29358. }
  29359. printf(resultFmt, ret == 0 ? passed : failed);
  29360. #endif
  29361. return ret;
  29362. }/* End test_get_rand_digit*/
  29363. /*
  29364. * Testing get_digit_count
  29365. */
  29366. static int test_get_digit_count (void)
  29367. {
  29368. int ret = 0;
  29369. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  29370. mp_int a;
  29371. printf(testingFmt, "get_digit_count()");
  29372. if (mp_init(&a) != MP_OKAY) {
  29373. ret = -1;
  29374. }
  29375. if (ret == 0) {
  29376. ret = get_digit_count(NULL);
  29377. }
  29378. if (ret == 0) {
  29379. ret = get_digit_count(&a);
  29380. }
  29381. printf(resultFmt, ret == 0 ? passed : failed);
  29382. mp_clear(&a);
  29383. #endif
  29384. return ret;
  29385. }/* End test_get_digit_count*/
  29386. /*
  29387. * Testing mp_cond_copy
  29388. */
  29389. static int test_mp_cond_copy (void)
  29390. {
  29391. int ret = 0;
  29392. #if defined(WOLFSSL_PUBLIC_MP)
  29393. mp_int a;
  29394. mp_int b;
  29395. int copy = 0;
  29396. printf(testingFmt, "mp_cond_copy()");
  29397. if (mp_init(&a) != MP_OKAY) {
  29398. ret = -1;
  29399. }
  29400. if (ret == 0) {
  29401. if (mp_init(&b) != MP_OKAY) {
  29402. ret = -1;
  29403. }
  29404. }
  29405. if (ret == 0) {
  29406. ret = mp_cond_copy(NULL, copy, NULL);
  29407. if (ret == BAD_FUNC_ARG) {
  29408. ret = 0;
  29409. }
  29410. }
  29411. if (ret == 0) {
  29412. ret = mp_cond_copy(NULL, copy, &b);
  29413. if (ret == BAD_FUNC_ARG) {
  29414. ret = 0;
  29415. }
  29416. }
  29417. if (ret == 0) {
  29418. ret = mp_cond_copy(&a, copy, NULL);
  29419. if (ret == BAD_FUNC_ARG) {
  29420. ret = 0;
  29421. }
  29422. }
  29423. if (ret == 0) {
  29424. ret = mp_cond_copy(&a, copy, &b);
  29425. }
  29426. printf(resultFmt, ret == 0 ? passed : failed);
  29427. mp_clear(&a);
  29428. mp_clear(&b);
  29429. #endif
  29430. return ret;
  29431. }/* End test_mp_cond_copy*/
  29432. /*
  29433. * Testing mp_rand
  29434. */
  29435. static int test_mp_rand (void)
  29436. {
  29437. int ret = 0;
  29438. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  29439. mp_int a;
  29440. int digits = 1;
  29441. WC_RNG rng;
  29442. printf(testingFmt, "mp_rand()");
  29443. if (mp_init(&a) != MP_OKAY) {
  29444. ret = -1;
  29445. }
  29446. if (ret == 0) {
  29447. ret = wc_InitRng(&rng);
  29448. }
  29449. if (ret == 0) {
  29450. ret = mp_rand(&a, digits, NULL);
  29451. if (ret == MISSING_RNG_E) {
  29452. ret = 0;
  29453. }
  29454. }
  29455. if (ret == 0) {
  29456. ret = mp_rand(NULL, digits, &rng);
  29457. if (ret == BAD_FUNC_ARG) {
  29458. ret = 0;
  29459. }
  29460. }
  29461. if (ret == 0) {
  29462. ret = mp_rand(&a, 0, &rng);
  29463. if (ret == BAD_FUNC_ARG) {
  29464. ret = 0;
  29465. }
  29466. }
  29467. if (ret == 0) {
  29468. ret = mp_rand(&a, digits, &rng);
  29469. }
  29470. printf(resultFmt, ret == 0 ? passed : failed);
  29471. mp_clear(&a);
  29472. wc_FreeRng(&rng);
  29473. #endif
  29474. return ret;
  29475. }/* End test_mp_rand*/
  29476. /*
  29477. * Testing get_digit
  29478. */
  29479. static int test_get_digit (void)
  29480. {
  29481. int ret = 0;
  29482. #if defined(WOLFSSL_PUBLIC_MP)
  29483. mp_int a;
  29484. int n = 0;
  29485. printf(testingFmt, "get_digit()");
  29486. if (mp_init(&a) != MP_OKAY) {
  29487. ret = -1;
  29488. }
  29489. if (ret == 0) {
  29490. if (get_digit(NULL, n) != 0) { /* Should not hit this */
  29491. ret = -1;
  29492. }
  29493. }
  29494. if (ret == 0) {
  29495. if (get_digit(NULL, n) == 0) { /* Should hit this */
  29496. ret = 0;
  29497. }
  29498. }
  29499. if (ret == 0) {
  29500. if (get_digit(&a, n) != 0) { /* Should not hit this */
  29501. ret = -1;
  29502. }
  29503. }
  29504. if (ret == 0) {
  29505. if (get_digit(&a, n) == 0) { /* Should hit this */
  29506. ret = 0;
  29507. }
  29508. }
  29509. printf(resultFmt, ret == 0 ? passed : failed);
  29510. mp_clear(&a);
  29511. #endif
  29512. return ret;
  29513. }/* End test_get_digit*/
  29514. /*
  29515. * Testing wc_export_int
  29516. */
  29517. static int test_wc_export_int (void)
  29518. {
  29519. int ret = 0;
  29520. #if defined(WOLFSSL_PUBLIC_MP)
  29521. mp_int mp;
  29522. byte buf[256];
  29523. word32 keySz = (word32)sizeof(buf);
  29524. word32 len = (word32)sizeof(buf);
  29525. int encType = WC_TYPE_UNSIGNED_BIN;
  29526. printf(testingFmt, "wc_export_int()");
  29527. if (mp_init(&mp) != MP_OKAY) {
  29528. ret = -1;
  29529. }
  29530. if (ret == 0) {
  29531. ret = wc_export_int(NULL, buf, &len, keySz, encType);
  29532. if (ret == BAD_FUNC_ARG) {
  29533. ret = 0;
  29534. }
  29535. }
  29536. len = sizeof(buf)-1;
  29537. if (ret == 0) {
  29538. ret = wc_export_int(&mp, buf, &len, keySz, encType);
  29539. if (ret == BUFFER_E) {
  29540. ret = 0;
  29541. }
  29542. }
  29543. len = sizeof(buf);
  29544. if (ret == 0) {
  29545. ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_HEX_STR);
  29546. }
  29547. if (ret == 0) {
  29548. ret = wc_export_int(&mp, buf, &len, keySz, encType);
  29549. }
  29550. printf(resultFmt, ret == 0 ? passed : failed);
  29551. mp_clear(&mp);
  29552. #endif
  29553. return ret;
  29554. }/* End test_wc_export_int*/
  29555. static int test_wc_InitRngNonce(void)
  29556. {
  29557. int ret=0;
  29558. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  29559. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  29560. WC_RNG rng;
  29561. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  29562. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  29563. word32 nonceSz = sizeof(nonce);
  29564. printf(testingFmt, "wc_InitRngNonce()");
  29565. if (ret == 0){
  29566. ret = wc_InitRngNonce(&rng, nonce, nonceSz);
  29567. }
  29568. wc_FreeRng(&rng);
  29569. printf(resultFmt, ret == 0 ? passed : failed);
  29570. #endif
  29571. return ret;
  29572. }/* End test_wc_InitRngNonce*/
  29573. /*
  29574. * Testing wc_InitRngNonce_ex
  29575. */
  29576. static int test_wc_InitRngNonce_ex(void)
  29577. {
  29578. int ret=0;
  29579. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  29580. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  29581. WC_RNG rng;
  29582. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  29583. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  29584. word32 nonceSz = sizeof(nonce);
  29585. printf(testingFmt, "wc_InitRngNonce_ex()");
  29586. if (ret == 0){
  29587. ret = wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, devId);
  29588. }
  29589. wc_FreeRng(&rng);
  29590. printf(resultFmt, ret == 0 ? passed : failed);
  29591. #endif
  29592. return ret;
  29593. }/*End test_wc_InitRngNonce_ex*/
  29594. static void test_wolfSSL_X509_CRL(void)
  29595. {
  29596. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  29597. X509_CRL *crl;
  29598. char pem[][100] = {
  29599. "./certs/crl/crl.pem",
  29600. "./certs/crl/crl2.pem",
  29601. "./certs/crl/caEccCrl.pem",
  29602. "./certs/crl/eccCliCRL.pem",
  29603. "./certs/crl/eccSrvCRL.pem",
  29604. ""
  29605. };
  29606. BIO *bio;
  29607. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  29608. char der[][100] = {
  29609. "./certs/crl/crl.der",
  29610. "./certs/crl/crl2.der",
  29611. ""};
  29612. #endif
  29613. XFILE fp;
  29614. int i;
  29615. printf(testingFmt, "test_wolfSSL_X509_CRL");
  29616. for (i = 0; pem[i][0] != '\0'; i++)
  29617. {
  29618. fp = XFOPEN(pem[i], "rb");
  29619. AssertTrue((fp != XBADFILE));
  29620. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
  29621. AssertNotNull(crl);
  29622. X509_CRL_free(crl);
  29623. XFCLOSE(fp);
  29624. fp = XFOPEN(pem[i], "rb");
  29625. AssertTrue((fp != XBADFILE));
  29626. AssertNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL, NULL));
  29627. AssertNotNull(crl);
  29628. X509_CRL_free(crl);
  29629. XFCLOSE(fp);
  29630. }
  29631. for (i = 0; pem[i][0] != '\0'; i++)
  29632. {
  29633. AssertNotNull(bio = BIO_new_file(pem[i], "r"));
  29634. AssertNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  29635. X509_CRL_free(crl);
  29636. BIO_free(bio);
  29637. }
  29638. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  29639. for(i = 0; der[i][0] != '\0'; i++){
  29640. fp = XFOPEN(der[i], "rb");
  29641. AssertTrue((fp != XBADFILE));
  29642. AssertNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  29643. AssertNotNull(crl);
  29644. X509_CRL_free(crl);
  29645. XFCLOSE(fp);
  29646. fp = XFOPEN(der[i], "rb");
  29647. AssertTrue((fp != XBADFILE));
  29648. AssertNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  29649. AssertNotNull(crl);
  29650. X509_CRL_free(crl);
  29651. XFCLOSE(fp);
  29652. }
  29653. #endif
  29654. printf(resultFmt, passed);
  29655. #endif
  29656. return;
  29657. }
  29658. static void test_wolfSSL_PEM_read_X509(void)
  29659. {
  29660. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  29661. !defined(NO_RSA)
  29662. X509 *x509 = NULL;
  29663. XFILE fp;
  29664. printf(testingFmt, "wolfSSL_PEM_read_X509");
  29665. fp = XFOPEN(svrCertFile, "rb");
  29666. AssertTrue((fp != XBADFILE));
  29667. AssertNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  29668. X509_free(x509);
  29669. XFCLOSE(fp);
  29670. printf(resultFmt, passed);
  29671. #endif
  29672. }
  29673. static void test_wolfSSL_PEM_read(void)
  29674. {
  29675. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  29676. const char* filename = "./certs/server-keyEnc.pem";
  29677. XFILE fp;
  29678. char* name = NULL;
  29679. char* header = NULL;
  29680. byte* data = NULL;
  29681. long len;
  29682. EVP_CIPHER_INFO cipher;
  29683. WOLFSSL_BIO* bio;
  29684. byte* fileData;
  29685. size_t fileDataSz;
  29686. byte* out;
  29687. printf(testingFmt, "wolfSSL_PEM_read");
  29688. fp = XFOPEN(filename, "rb");
  29689. AssertTrue((fp != XBADFILE));
  29690. /* Fail cases. */
  29691. AssertIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  29692. AssertIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  29693. AssertIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  29694. AssertIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  29695. AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  29696. AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  29697. AssertIntGT(XSTRLEN(header), 0);
  29698. AssertIntGT(len, 0);
  29699. AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  29700. AssertIntGT((fileDataSz = XFTELL(fp)), 0);
  29701. AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  29702. AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  29703. DYNAMIC_TYPE_TMP_BUFFER));
  29704. AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  29705. XFCLOSE(fp);
  29706. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29707. /* Fail cases. */
  29708. AssertIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  29709. AssertIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  29710. AssertIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  29711. AssertIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  29712. AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  29713. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  29714. AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  29715. /* Fail cases. */
  29716. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  29717. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  29718. AssertIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  29719. #ifndef NO_DES3
  29720. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  29721. #endif
  29722. /* Fail cases. */
  29723. AssertIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  29724. (void*)"yassl123"), WOLFSSL_FAILURE);
  29725. AssertIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  29726. (void*)"yassl123"), WOLFSSL_FAILURE);
  29727. AssertIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  29728. (void*)"yassl123"), WOLFSSL_FAILURE);
  29729. #if !defined(NO_DES3) && !defined(NO_MD5)
  29730. AssertIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  29731. (void*)"yassl123"), WOLFSSL_SUCCESS);
  29732. #endif
  29733. BIO_free(bio);
  29734. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29735. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29736. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29737. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29738. name = NULL;
  29739. header = NULL;
  29740. data = NULL;
  29741. fp = XFOPEN(svrKeyFile, "rb");
  29742. AssertTrue((fp != XBADFILE));
  29743. AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  29744. AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  29745. AssertIntEQ(XSTRLEN(header), 0);
  29746. AssertIntGT(len, 0);
  29747. AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  29748. AssertIntGT((fileDataSz = XFTELL(fp)), 0);
  29749. AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  29750. AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  29751. DYNAMIC_TYPE_TMP_BUFFER));
  29752. AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  29753. XFCLOSE(fp);
  29754. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29755. AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  29756. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  29757. AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  29758. BIO_free(bio);
  29759. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29760. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29761. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29762. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29763. printf(resultFmt, passed);
  29764. #endif
  29765. }
  29766. static void test_wolfssl_EVP_aes_gcm(void)
  29767. {
  29768. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  29769. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  29770. /* A 256 bit key, AES_128 will use the first 128 bit*/
  29771. byte *key = (byte*)"01234567890123456789012345678901";
  29772. /* A 128 bit IV */
  29773. byte *iv = (byte*)"0123456789012345";
  29774. int ivSz = AES_BLOCK_SIZE;
  29775. /* Message to be encrypted */
  29776. byte *plaintxt = (byte*)"for things to change you have to change";
  29777. /* Additional non-confidential data */
  29778. byte *aad = (byte*)"Don't spend major time on minor things.";
  29779. unsigned char tag[AES_BLOCK_SIZE] = {0};
  29780. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  29781. int aadSz = (int)XSTRLEN((char*)aad);
  29782. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  29783. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  29784. int ciphertxtSz = 0;
  29785. int decryptedtxtSz = 0;
  29786. int len = 0;
  29787. int i = 0;
  29788. EVP_CIPHER_CTX en[2];
  29789. EVP_CIPHER_CTX de[2];
  29790. printf(testingFmt, "wolfssl_EVP_aes_gcm");
  29791. for (i = 0; i < 2; i++) {
  29792. EVP_CIPHER_CTX_init(&en[i]);
  29793. if (i == 0) {
  29794. /* Default uses 96-bits IV length */
  29795. #ifdef WOLFSSL_AES_128
  29796. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, key, iv));
  29797. #elif defined(WOLFSSL_AES_192)
  29798. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, key, iv));
  29799. #elif defined(WOLFSSL_AES_256)
  29800. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, key, iv));
  29801. #endif
  29802. }
  29803. else {
  29804. #ifdef WOLFSSL_AES_128
  29805. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
  29806. #elif defined(WOLFSSL_AES_192)
  29807. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
  29808. #elif defined(WOLFSSL_AES_256)
  29809. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
  29810. #endif
  29811. /* non-default must to set the IV length first */
  29812. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  29813. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  29814. }
  29815. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  29816. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  29817. ciphertxtSz = len;
  29818. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  29819. ciphertxtSz += len;
  29820. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, AES_BLOCK_SIZE, tag));
  29821. EVP_CIPHER_CTX_init(&de[i]);
  29822. if (i == 0) {
  29823. /* Default uses 96-bits IV length */
  29824. #ifdef WOLFSSL_AES_128
  29825. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, key, iv));
  29826. #elif defined(WOLFSSL_AES_192)
  29827. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, key, iv));
  29828. #elif defined(WOLFSSL_AES_256)
  29829. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, key, iv));
  29830. #endif
  29831. }
  29832. else {
  29833. #ifdef WOLFSSL_AES_128
  29834. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
  29835. #elif defined(WOLFSSL_AES_192)
  29836. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
  29837. #elif defined(WOLFSSL_AES_256)
  29838. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
  29839. #endif
  29840. /* non-default must to set the IV length first */
  29841. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  29842. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  29843. }
  29844. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  29845. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  29846. decryptedtxtSz = len;
  29847. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
  29848. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  29849. decryptedtxtSz += len;
  29850. AssertIntEQ(ciphertxtSz, decryptedtxtSz);
  29851. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  29852. /* modify tag*/
  29853. tag[AES_BLOCK_SIZE-1]+=0xBB;
  29854. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  29855. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
  29856. /* fail due to wrong tag */
  29857. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  29858. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  29859. AssertIntEQ(0, len);
  29860. }
  29861. printf(resultFmt, passed);
  29862. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  29863. }
  29864. static void test_wolfSSL_PEM_X509_INFO_read_bio(void)
  29865. {
  29866. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM)
  29867. BIO* bio;
  29868. X509_INFO* info;
  29869. STACK_OF(X509_INFO)* sk;
  29870. char* subject;
  29871. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  29872. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  29873. printf(testingFmt, "wolfSSL_PEM_X509_INFO_read_bio");
  29874. AssertNotNull(bio = BIO_new(BIO_s_file()));
  29875. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  29876. AssertNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  29877. AssertIntEQ(sk_X509_INFO_num(sk), 2);
  29878. /* using dereference to maintain testing for Apache port*/
  29879. AssertNotNull(info = sk_X509_INFO_pop(sk));
  29880. AssertNotNull(info->x_pkey);
  29881. AssertNotNull(info->x_pkey->dec_pkey);
  29882. AssertIntEQ(EVP_PKEY_bits(info->x_pkey->dec_pkey), 2048);
  29883. AssertNotNull(subject =
  29884. X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
  29885. AssertIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  29886. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  29887. X509_INFO_free(info);
  29888. AssertNotNull(info = sk_X509_INFO_pop(sk));
  29889. AssertNotNull(subject =
  29890. X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
  29891. AssertIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  29892. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  29893. X509_INFO_free(info);
  29894. AssertNull(info = sk_X509_INFO_pop(sk));
  29895. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  29896. BIO_free(bio);
  29897. printf(resultFmt, passed);
  29898. #endif
  29899. }
  29900. static void test_wolfSSL_X509_NAME_ENTRY_get_object()
  29901. {
  29902. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29903. X509 *x509;
  29904. X509_NAME* name;
  29905. int idx = 0;
  29906. X509_NAME_ENTRY *ne;
  29907. ASN1_OBJECT *object = NULL;
  29908. printf(testingFmt, "wolfSSL_X509_NAME_ENTRY_get_object");
  29909. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  29910. AssertNotNull(x509);
  29911. name = X509_get_subject_name(x509);
  29912. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  29913. AssertIntGE(idx, 0);
  29914. ne = X509_NAME_get_entry(name, idx);
  29915. AssertNotNull(ne);
  29916. AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
  29917. X509_free(x509);
  29918. printf(resultFmt, passed);
  29919. #endif
  29920. }
  29921. static void test_wolfSSL_ASN1_INTEGER_set()
  29922. {
  29923. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  29924. ASN1_INTEGER *a;
  29925. long val;
  29926. int ret;
  29927. printf(testingFmt, "wolfSSL_ASN1_INTEGER_set");
  29928. a = wolfSSL_ASN1_INTEGER_new();
  29929. val = 0;
  29930. ret = ASN1_INTEGER_set(NULL, val);
  29931. AssertIntEQ(ret, 0);
  29932. wolfSSL_ASN1_INTEGER_free(a);
  29933. /* 0 */
  29934. a = wolfSSL_ASN1_INTEGER_new();
  29935. val = 0;
  29936. ret = ASN1_INTEGER_set(a, val);
  29937. AssertIntEQ(ret, 1);
  29938. wolfSSL_ASN1_INTEGER_free(a);
  29939. /* 40 */
  29940. a = wolfSSL_ASN1_INTEGER_new();
  29941. val = 40;
  29942. ret = ASN1_INTEGER_set(a, val);
  29943. AssertIntEQ(ret, 1);
  29944. wolfSSL_ASN1_INTEGER_free(a);
  29945. /* -40 */
  29946. a = wolfSSL_ASN1_INTEGER_new();
  29947. val = -40;
  29948. ret = ASN1_INTEGER_set(a, val);
  29949. AssertIntEQ(ret, 1);
  29950. AssertIntEQ(a->negative, 1);
  29951. wolfSSL_ASN1_INTEGER_free(a);
  29952. /* 128 */
  29953. a = wolfSSL_ASN1_INTEGER_new();
  29954. val = 128;
  29955. ret = ASN1_INTEGER_set(a, val);
  29956. AssertIntEQ(ret, 1);
  29957. wolfSSL_ASN1_INTEGER_free(a);
  29958. /* -128 */
  29959. a = wolfSSL_ASN1_INTEGER_new();
  29960. val = -128;
  29961. ret = ASN1_INTEGER_set(a, val);
  29962. AssertIntEQ(ret, 1);
  29963. AssertIntEQ(a->negative, 1);
  29964. wolfSSL_ASN1_INTEGER_free(a);
  29965. /* 200 */
  29966. a = wolfSSL_ASN1_INTEGER_new();
  29967. val = 200;
  29968. ret = ASN1_INTEGER_set(a, val);
  29969. AssertIntEQ(ret, 1);
  29970. wolfSSL_ASN1_INTEGER_free(a);
  29971. #ifndef TIME_T_NOT_64BIT
  29972. /* int max (2147483647) */
  29973. a = wolfSSL_ASN1_INTEGER_new();
  29974. val = 2147483647;
  29975. ret = ASN1_INTEGER_set(a, val);
  29976. AssertIntEQ(ret, 1);
  29977. wolfSSL_ASN1_INTEGER_free(a);
  29978. /* int min (-2147483648) */
  29979. a = wolfSSL_ASN1_INTEGER_new();
  29980. val = -2147483647 - 1;
  29981. ret = ASN1_INTEGER_set(a, val);
  29982. AssertIntEQ(a->negative, 1);
  29983. AssertIntEQ(ret, 1);
  29984. wolfSSL_ASN1_INTEGER_free(a);
  29985. #endif
  29986. printf(resultFmt, passed);
  29987. #endif
  29988. }
  29989. /* Testing code used in dpp.c in hostap */
  29990. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  29991. typedef struct {
  29992. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  29993. * as an OID identifying the curve */
  29994. X509_ALGOR *alg;
  29995. /* Compressed format public key per ANSI X9.63 */
  29996. ASN1_BIT_STRING *pub_key;
  29997. } DPP_BOOTSTRAPPING_KEY;
  29998. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  29999. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  30000. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  30001. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY);
  30002. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY);
  30003. #endif
  30004. static void test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS()
  30005. {
  30006. /* Testing code used in dpp.c in hostap */
  30007. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  30008. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  30009. EC_KEY *eckey;
  30010. EVP_PKEY *key;
  30011. size_t len;
  30012. unsigned char *der = NULL;
  30013. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  30014. const unsigned char *in = ecc_clikey_der_256;
  30015. const EC_GROUP *group;
  30016. const EC_POINT *point;
  30017. int nid;
  30018. AssertNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  30019. AssertNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  30020. (long)sizeof_ecc_clikey_der_256));
  30021. AssertNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  30022. AssertNotNull(group = EC_KEY_get0_group(eckey));
  30023. AssertNotNull(point = EC_KEY_get0_public_key(eckey));
  30024. nid = EC_GROUP_get_curve_name(group);
  30025. AssertIntEQ(X509_ALGOR_set0(bootstrap->alg, OBJ_nid2obj(EVP_PKEY_EC),
  30026. V_ASN1_OBJECT, OBJ_nid2obj(nid)), 1);
  30027. #ifdef HAVE_COMP_KEY
  30028. AssertIntGT((len = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  30029. NULL, 0, NULL)), 0);
  30030. #else
  30031. AssertIntGT((len = EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  30032. NULL, 0, NULL)), 0);
  30033. #endif
  30034. AssertNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  30035. #ifdef HAVE_COMP_KEY
  30036. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  30037. der, len, NULL), len);
  30038. #else
  30039. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  30040. der, len, NULL), len);
  30041. #endif
  30042. bootstrap->pub_key->data = der;
  30043. bootstrap->pub_key->length = (int)len;
  30044. /* Not actually used */
  30045. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  30046. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  30047. der = NULL;
  30048. AssertIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  30049. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  30050. EVP_PKEY_free(key);
  30051. EC_KEY_free(eckey);
  30052. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  30053. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  30054. #endif /* WOLFSSL_WPAS && HAVE_ECC && USE_CERT_BUFFERS_256 */
  30055. }
  30056. static void test_wolfSSL_i2c_ASN1_INTEGER()
  30057. {
  30058. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  30059. ASN1_INTEGER *a;
  30060. unsigned char *pp,*tpp;
  30061. int ret;
  30062. printf(testingFmt, "wolfSSL_i2c_ASN1_INTEGER");
  30063. a = wolfSSL_ASN1_INTEGER_new();
  30064. /* 40 */
  30065. a->intData[0] = ASN_INTEGER;
  30066. a->intData[1] = 1;
  30067. a->intData[2] = 40;
  30068. ret = i2c_ASN1_INTEGER(a, NULL);
  30069. AssertIntEQ(ret, 1);
  30070. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  30071. DYNAMIC_TYPE_TMP_BUFFER));
  30072. tpp = pp;
  30073. XMEMSET(pp, 0, ret + 1);
  30074. i2c_ASN1_INTEGER(a, &pp);
  30075. pp--;
  30076. AssertIntEQ(*pp, 40);
  30077. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  30078. /* 128 */
  30079. a->intData[0] = ASN_INTEGER;
  30080. a->intData[1] = 1;
  30081. a->intData[2] = 128;
  30082. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  30083. AssertIntEQ(ret, 2);
  30084. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  30085. DYNAMIC_TYPE_TMP_BUFFER));
  30086. tpp = pp;
  30087. XMEMSET(pp, 0, ret + 1);
  30088. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  30089. pp--;
  30090. AssertIntEQ(*(pp--), 128);
  30091. AssertIntEQ(*pp, 0);
  30092. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  30093. /* -40 */
  30094. a->intData[0] = ASN_INTEGER;
  30095. a->intData[1] = 1;
  30096. a->intData[2] = 40;
  30097. a->negative = 1;
  30098. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  30099. AssertIntEQ(ret, 1);
  30100. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  30101. DYNAMIC_TYPE_TMP_BUFFER));
  30102. tpp = pp;
  30103. XMEMSET(pp, 0, ret + 1);
  30104. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  30105. pp--;
  30106. AssertIntEQ(*pp, 216);
  30107. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  30108. /* -128 */
  30109. a->intData[0] = ASN_INTEGER;
  30110. a->intData[1] = 1;
  30111. a->intData[2] = 128;
  30112. a->negative = 1;
  30113. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  30114. AssertIntEQ(ret, 1);
  30115. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  30116. DYNAMIC_TYPE_TMP_BUFFER));
  30117. tpp = pp;
  30118. XMEMSET(pp, 0, ret + 1);
  30119. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  30120. pp--;
  30121. AssertIntEQ(*pp, 128);
  30122. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  30123. /* -200 */
  30124. a->intData[0] = ASN_INTEGER;
  30125. a->intData[1] = 1;
  30126. a->intData[2] = 200;
  30127. a->negative = 1;
  30128. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  30129. AssertIntEQ(ret, 2);
  30130. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  30131. DYNAMIC_TYPE_TMP_BUFFER));
  30132. tpp = pp;
  30133. XMEMSET(pp, 0, ret + 1);
  30134. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  30135. pp--;
  30136. AssertIntEQ(*(pp--), 56);
  30137. AssertIntEQ(*pp, 255);
  30138. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  30139. wolfSSL_ASN1_INTEGER_free(a);
  30140. printf(resultFmt, passed);
  30141. #endif /* OPENSSL_EXTRA && !NO_ASN */
  30142. }
  30143. #ifndef NO_INLINE
  30144. #define WOLFSSL_MISC_INCLUDED
  30145. #include <wolfcrypt/src/misc.c>
  30146. #else
  30147. #include <wolfssl/wolfcrypt/misc.h>
  30148. #endif
  30149. static int test_ForceZero(void)
  30150. {
  30151. unsigned char data[32];
  30152. unsigned int i, j, len;
  30153. /* Test case with 0 length */
  30154. ForceZero(data, 0);
  30155. /* Test ForceZero */
  30156. for (i = 0; i < sizeof(data); i++) {
  30157. for (len = 1; len < sizeof(data) - i; len++) {
  30158. for (j = 0; j < sizeof(data); j++)
  30159. data[j] = j + 1;
  30160. ForceZero(data + i, len);
  30161. for (j = 0; j < sizeof(data); j++) {
  30162. if (j < i || j >= i + len) {
  30163. if (data[j] == 0x00)
  30164. return -10200;
  30165. }
  30166. else if (data[j] != 0x00)
  30167. return -10201;
  30168. }
  30169. }
  30170. }
  30171. return 0;
  30172. }
  30173. static void test_wolfSSL_X509_print()
  30174. {
  30175. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  30176. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(XSNPRINTF)
  30177. X509 *x509;
  30178. BIO *bio;
  30179. printf(testingFmt, "wolfSSL_X509_print");
  30180. x509 = X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  30181. AssertNotNull(x509);
  30182. /* print to memory */
  30183. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  30184. AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  30185. #if defined(WOLFSSL_QT)
  30186. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3113);
  30187. #else
  30188. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3103);
  30189. #endif
  30190. BIO_free(bio);
  30191. /* print to stdout */
  30192. AssertNotNull(bio = BIO_new(BIO_s_file()));
  30193. wolfSSL_BIO_set_fp(bio, stdout, BIO_NOCLOSE);
  30194. AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  30195. BIO_free(bio);
  30196. X509_free(x509);
  30197. printf(resultFmt, passed);
  30198. #endif
  30199. }
  30200. static void test_wolfSSL_RSA_print()
  30201. {
  30202. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  30203. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  30204. !defined(HAVE_FAST_RSA)
  30205. BIO *bio;
  30206. WOLFSSL_RSA* rsa = NULL;
  30207. printf(testingFmt, "wolfSSL_RSA_print");
  30208. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  30209. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file()));
  30210. wolfSSL_BIO_set_fp(bio, stdout, BIO_NOCLOSE);
  30211. AssertIntEQ(RSA_print(bio, rsa, 0), SSL_SUCCESS);
  30212. BIO_free(bio);
  30213. wolfSSL_RSA_free(rsa);
  30214. printf(resultFmt, passed);
  30215. #endif
  30216. }
  30217. static void test_wolfSSL_BIO_get_len()
  30218. {
  30219. #if defined(OPENSSL_EXTRA)
  30220. BIO *bio;
  30221. const char txt[] = "Some example text to push to the BIO.";
  30222. printf(testingFmt, "wolfSSL_BIO_get_len");
  30223. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30224. AssertIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  30225. AssertIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  30226. BIO_free(bio);
  30227. printf(resultFmt, passed);
  30228. #endif
  30229. }
  30230. static void test_wolfSSL_ASN1_STRING_print(void){
  30231. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS)
  30232. ASN1_STRING* asnStr = NULL;
  30233. const char HELLO_DATA[]= \
  30234. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  30235. const unsigned int MAX_UNPRINTABLE_CHAR = 32;
  30236. const unsigned int MAX_BUF = 255;
  30237. const int LF = 10, CR = 13;
  30238. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  30239. unsigned char expected[sizeof(unprintableData)+1];
  30240. unsigned char rbuf[MAX_BUF];
  30241. BIO *bio;
  30242. int p_len, i;
  30243. printf(testingFmt, "wolfSSL_ASN1_STRING_print()");
  30244. /* setup */
  30245. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  30246. unprintableData[i] = HELLO_DATA[i];
  30247. expected[i] = HELLO_DATA[i];
  30248. }
  30249. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  30250. unprintableData[sizeof(HELLO_DATA)+i] = i;
  30251. if (i == LF || i == CR)
  30252. expected[sizeof(HELLO_DATA)+i] = i;
  30253. else
  30254. expected[sizeof(HELLO_DATA)+i] = '.';
  30255. }
  30256. unprintableData[sizeof(unprintableData)-1] = '\0';
  30257. expected[sizeof(expected)-1] = '\0';
  30258. XMEMSET(rbuf, 0, MAX_BUF);
  30259. bio = BIO_new(BIO_s_mem());
  30260. BIO_set_write_buf_size(bio, MAX_BUF);
  30261. asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  30262. ASN1_STRING_set(asnStr,(const void*)unprintableData,
  30263. (int)sizeof(unprintableData));
  30264. /* test */
  30265. p_len = wolfSSL_ASN1_STRING_print(bio, asnStr);
  30266. AssertIntEQ(p_len, 46);
  30267. BIO_read(bio, (void*)rbuf, 46);
  30268. AssertStrEQ((char*)rbuf, (const char*)expected);
  30269. BIO_free(bio);
  30270. ASN1_STRING_free(asnStr);
  30271. printf(resultFmt, passed);
  30272. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS */
  30273. }
  30274. static void test_wolfSSL_RSA_verify()
  30275. {
  30276. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && \
  30277. !defined(NO_FILESYSTEM) && defined(HAVE_CRL)
  30278. XFILE fp;
  30279. RSA *pKey, *pubKey;
  30280. X509 *cert;
  30281. const char *text = "Hello wolfSSL !";
  30282. unsigned char hash[SHA256_DIGEST_LENGTH];
  30283. unsigned char signature[2048/8];
  30284. unsigned int signatureLength;
  30285. byte *buf;
  30286. BIO *bio;
  30287. SHA256_CTX c;
  30288. EVP_PKEY *evpPkey, *evpPubkey;
  30289. size_t sz;
  30290. printf(testingFmt, "wolfSSL_RSA_verify");
  30291. /* generate hash */
  30292. SHA256_Init(&c);
  30293. SHA256_Update(&c, text, strlen(text));
  30294. SHA256_Final(hash, &c);
  30295. /* read privete key file */
  30296. fp = XFOPEN(svrKeyFile, "r");
  30297. AssertTrue((fp != XBADFILE));
  30298. XFSEEK(fp, 0, XSEEK_END);
  30299. sz = XFTELL(fp);
  30300. XREWIND(fp);
  30301. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30302. AssertIntEQ(XFREAD(buf, 1, sz, fp), sz);
  30303. XFCLOSE(fp);
  30304. /* read private key and sign hash data */
  30305. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30306. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  30307. AssertNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  30308. AssertIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  30309. signature, &signatureLength, pKey), SSL_SUCCESS);
  30310. /* read public key and verify signed data */
  30311. fp = XFOPEN(svrCertFile,"r");
  30312. AssertTrue((fp != XBADFILE));
  30313. cert = PEM_read_X509(fp, 0, 0, 0 );
  30314. XFCLOSE(fp);
  30315. evpPubkey = X509_get_pubkey(cert);
  30316. pubKey = EVP_PKEY_get1_RSA(evpPubkey);
  30317. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  30318. signatureLength, pubKey), SSL_SUCCESS);
  30319. RSA_free(pKey);
  30320. EVP_PKEY_free(evpPkey);
  30321. RSA_free(pubKey);
  30322. EVP_PKEY_free(evpPubkey);
  30323. X509_free(cert);
  30324. BIO_free(bio);
  30325. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30326. printf(resultFmt, passed);
  30327. #endif
  30328. }
  30329. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30330. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  30331. static void test_openssl_make_self_signed_certificate(EVP_PKEY* pkey)
  30332. {
  30333. X509* x509 = NULL;
  30334. BIGNUM* serial_number = NULL;
  30335. X509_NAME* name = NULL;
  30336. time_t epoch_off = 0;
  30337. ASN1_INTEGER* asn1_serial_number;
  30338. long not_before, not_after;
  30339. AssertNotNull(x509 = X509_new());
  30340. AssertIntNE(X509_set_pubkey(x509, pkey), 0);
  30341. AssertNotNull(serial_number = BN_new());
  30342. AssertIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  30343. AssertNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  30344. AssertNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  30345. /* version 3 */
  30346. AssertIntNE(X509_set_version(x509, 2L), 0);
  30347. AssertNotNull(name = X509_NAME_new());
  30348. AssertIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  30349. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  30350. AssertIntNE(X509_set_subject_name(x509, name), 0);
  30351. AssertIntNE(X509_set_issuer_name(x509, name), 0);
  30352. not_before = (long)XTIME(NULL);
  30353. not_after = not_before + (365 * 24 * 60 * 60);
  30354. AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &epoch_off));
  30355. AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &epoch_off));
  30356. AssertIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  30357. BN_free(serial_number);
  30358. X509_NAME_free(name);
  30359. X509_free(x509);
  30360. }
  30361. #endif
  30362. static void test_openssl_generate_key_and_cert(void)
  30363. {
  30364. #if defined(OPENSSL_EXTRA)
  30365. #if !defined(NO_RSA)
  30366. {
  30367. EVP_PKEY* pkey = EVP_PKEY_new();
  30368. int key_length = 2048;
  30369. BIGNUM* exponent = BN_new();
  30370. RSA* rsa = RSA_new();
  30371. AssertNotNull(pkey);
  30372. AssertNotNull(exponent);
  30373. AssertNotNull(rsa);
  30374. AssertIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  30375. #ifndef WOLFSSL_KEY_GEN
  30376. AssertIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), WOLFSSL_FAILURE);
  30377. #if defined(USE_CERT_BUFFERS_1024)
  30378. AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  30379. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  30380. key_length = 1024;
  30381. #elif defined(USE_CERT_BUFFERS_2048)
  30382. AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  30383. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  30384. #else
  30385. RSA_free(rsa);
  30386. rsa = NULL;
  30387. #endif
  30388. #else
  30389. AssertIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  30390. #endif
  30391. if (rsa) {
  30392. AssertIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  30393. BN_free(exponent);
  30394. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  30395. test_openssl_make_self_signed_certificate(pkey);
  30396. #endif
  30397. }
  30398. EVP_PKEY_free(pkey);
  30399. }
  30400. #endif /* !NO_RSA */
  30401. #ifdef HAVE_ECC
  30402. {
  30403. EVP_PKEY* pkey = EVP_PKEY_new();
  30404. EC_KEY* ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  30405. AssertNotNull(pkey);
  30406. AssertNotNull(ec_key);
  30407. #ifndef NO_WOLFSSL_STUB
  30408. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  30409. #endif
  30410. AssertIntNE(EC_KEY_generate_key(ec_key), 0);
  30411. AssertIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  30412. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  30413. test_openssl_make_self_signed_certificate(pkey);
  30414. #endif
  30415. EVP_PKEY_free(pkey);
  30416. }
  30417. #endif /* HAVE_ECC */
  30418. #endif /* OPENSSL_EXTRA */
  30419. }
  30420. static void test_stubs_are_stubs()
  30421. {
  30422. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB)
  30423. WOLFSSL_CTX* ctx = NULL;
  30424. WOLFSSL_CTX* ctxN = NULL;
  30425. #ifndef NO_WOLFSSL_CLIENT
  30426. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  30427. AssertNotNull(ctx);
  30428. #elif !defined(NO_WOLFSSL_SERVER)
  30429. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  30430. AssertNotNull(ctx);
  30431. #else
  30432. return;
  30433. #endif
  30434. #define CHECKZERO_RET(x, y, z) AssertIntEQ((int) x(y), 0); \
  30435. AssertIntEQ((int) x(z), 0)
  30436. /* test logic, all stubs return same result regardless of ctx being NULL
  30437. * as there are no sanity checks, it's just a stub! If at some
  30438. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  30439. * if invalid inputs are supplied. Test calling both
  30440. * with and without valid inputs, if a stub functionality remains unchanged.
  30441. */
  30442. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  30443. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  30444. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  30445. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  30446. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  30447. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  30448. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  30449. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  30450. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  30451. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  30452. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  30453. wolfSSL_CTX_free(ctx);
  30454. ctx = NULL;
  30455. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB */
  30456. }
  30457. static void test_wolfSSL_CTX_LoadCRL()
  30458. {
  30459. #ifdef HAVE_CRL
  30460. WOLFSSL_CTX* ctx = NULL;
  30461. const char* badPath = "dummypath";
  30462. const char* validPath = "./certs/crl";
  30463. int derType = WOLFSSL_FILETYPE_ASN1;
  30464. int rawType = WOLFSSL_FILETYPE_RAW;
  30465. int pemType = WOLFSSL_FILETYPE_PEM;
  30466. int monitor = WOLFSSL_CRL_MONITOR;
  30467. #define FAIL_T1(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
  30468. BAD_FUNC_ARG)
  30469. #define SUCC_T(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
  30470. WOLFSSL_SUCCESS)
  30471. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  30472. #ifndef NO_WOLFSSL_CLIENT
  30473. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  30474. #elif !defined(NO_WOLFSSL_SERVER)
  30475. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  30476. #else
  30477. return;
  30478. #endif
  30479. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  30480. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  30481. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  30482. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, rawType, monitor);
  30483. wolfSSL_CTX_free(ctx);
  30484. ctx = NULL;
  30485. #endif
  30486. }
  30487. static void test_SetTmpEC_DHE_Sz(void)
  30488. {
  30489. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  30490. WOLFSSL_CTX *ctx;
  30491. WOLFSSL *ssl;
  30492. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  30493. AssertNotNull(ctx);
  30494. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  30495. ssl = wolfSSL_new(ctx);
  30496. AssertNotNull(ssl);
  30497. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  30498. wolfSSL_free(ssl);
  30499. wolfSSL_CTX_free(ctx);
  30500. #endif
  30501. }
  30502. static void test_wolfSSL_dtls_set_mtu(void)
  30503. {
  30504. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  30505. defined(WOLFSSL_DTLS)
  30506. WOLFSSL_CTX* ctx = NULL;
  30507. WOLFSSL* ssl = NULL;
  30508. const char* testCertFile;
  30509. const char* testKeyFile;
  30510. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  30511. #ifndef NO_RSA
  30512. testCertFile = svrCertFile;
  30513. testKeyFile = svrKeyFile;
  30514. #elif defined(HAVE_ECC)
  30515. testCertFile = eccCertFile;
  30516. testKeyFile = eccKeyFile;
  30517. #endif
  30518. if (testCertFile != NULL && testKeyFile != NULL) {
  30519. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  30520. WOLFSSL_FILETYPE_PEM));
  30521. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  30522. WOLFSSL_FILETYPE_PEM));
  30523. }
  30524. AssertNotNull(ssl = wolfSSL_new(ctx));
  30525. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  30526. AssertIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  30527. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  30528. AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  30529. AssertIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  30530. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  30531. AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  30532. wolfSSL_free(ssl);
  30533. wolfSSL_CTX_free(ctx);
  30534. printf(testingFmt, "wolfSSL_dtls_set_mtu()");
  30535. printf(resultFmt, passed);
  30536. #endif
  30537. }
  30538. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  30539. !defined(NO_CERTS)
  30540. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  30541. {
  30542. int ret;
  30543. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  30544. printf("loading cert %s failed\n", certA);
  30545. printf("Error: (%d): %s\n", ret, wolfSSL_ERR_reason_error_string(ret));
  30546. return -1;
  30547. }
  30548. return 0;
  30549. }
  30550. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  30551. {
  30552. int ret;
  30553. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  30554. != WOLFSSL_SUCCESS) {
  30555. printf("could not verify the cert: %s\n", certA);
  30556. printf("Error: (%d): %s\n", ret, wolfSSL_ERR_reason_error_string(ret));
  30557. return -1;
  30558. } else {
  30559. printf("successfully verified: %s\n", certA);
  30560. }
  30561. return 0;
  30562. }
  30563. #define LOAD_ONE_CA(a, b, c, d) \
  30564. do { \
  30565. a = load_ca_into_cm(c, d); \
  30566. if (a != 0) \
  30567. return b; \
  30568. else \
  30569. b--; \
  30570. } while(0)
  30571. #define VERIFY_ONE_CERT(a, b, c, d) \
  30572. do { \
  30573. a = verify_cert_with_cm(c, d); \
  30574. if (a != 0) \
  30575. return b; \
  30576. else \
  30577. b--; \
  30578. } while(0)
  30579. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  30580. {
  30581. int ret;
  30582. int i = -1;
  30583. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  30584. char chainGArr[9][50] = {"certs/ca-cert.pem",
  30585. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  30586. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  30587. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  30588. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  30589. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  30590. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  30591. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  30592. "certs/test-pathlen/chainG-entity.pem"};
  30593. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  30594. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  30595. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  30596. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  30597. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  30598. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  30599. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  30600. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  30601. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  30602. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  30603. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  30604. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  30605. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  30606. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  30607. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  30608. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  30609. /* test validating the entity twice, should have no effect on pathLen since
  30610. * entity/leaf cert */
  30611. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  30612. return ret;
  30613. }
  30614. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  30615. {
  30616. int ret;
  30617. int i = -1;
  30618. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  30619. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  30620. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  30621. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  30622. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  30623. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  30624. */
  30625. char chainHArr[6][50] = {"certs/ca-cert.pem",
  30626. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  30627. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  30628. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  30629. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  30630. "certs/test-pathlen/chainH-entity.pem"};
  30631. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  30632. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  30633. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  30634. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  30635. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  30636. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  30637. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  30638. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  30639. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  30640. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  30641. return ret;
  30642. }
  30643. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  30644. {
  30645. int ret;
  30646. int i = -1;
  30647. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  30648. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  30649. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  30650. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  30651. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  30652. */
  30653. char chainIArr[5][50] = {"certs/ca-cert.pem",
  30654. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  30655. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  30656. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  30657. "certs/test-pathlen/chainI-entity.pem"};
  30658. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  30659. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  30660. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  30661. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  30662. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  30663. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  30664. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  30665. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  30666. return ret;
  30667. }
  30668. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  30669. {
  30670. int ret;
  30671. int i = -1;
  30672. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  30673. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  30674. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  30675. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  30676. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  30677. */
  30678. char chainJArr[6][50] = {"certs/ca-cert.pem",
  30679. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  30680. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  30681. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  30682. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  30683. "certs/test-pathlen/chainJ-entity.pem"};
  30684. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  30685. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  30686. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  30687. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  30688. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  30689. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  30690. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  30691. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  30692. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  30693. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  30694. return ret;
  30695. }
  30696. static int test_various_pathlen_chains(void)
  30697. {
  30698. int ret;
  30699. WOLFSSL_CERT_MANAGER* cm;
  30700. /* Test chain G (large chain with varying pathLens) */
  30701. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  30702. printf("cert manager new failed\n");
  30703. return -1;
  30704. }
  30705. AssertIntEQ(test_chainG(cm), 0);
  30706. ret = wolfSSL_CertManagerUnloadCAs(cm);
  30707. if (ret != WOLFSSL_SUCCESS)
  30708. return -1;
  30709. wolfSSL_CertManagerFree(cm);
  30710. /* end test chain G */
  30711. /* Test chain H (5 chain with same pathLens) */
  30712. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  30713. printf("cert manager new failed\n");
  30714. return -1;
  30715. }
  30716. AssertIntLT(test_chainH(cm), 0);
  30717. wolfSSL_CertManagerUnloadCAs(cm);
  30718. wolfSSL_CertManagerFree(cm);
  30719. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  30720. printf("cert manager new failed\n");
  30721. return -1;
  30722. }
  30723. ret = wolfSSL_CertManagerUnloadCAs(cm);
  30724. if (ret != WOLFSSL_SUCCESS)
  30725. return -1;
  30726. wolfSSL_CertManagerFree(cm);
  30727. /* end test chain H */
  30728. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  30729. * followed by 2 ICA's, should pass) */
  30730. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  30731. printf("cert manager new failed\n");
  30732. return -1;
  30733. }
  30734. AssertIntEQ(test_chainI(cm), 0);
  30735. wolfSSL_CertManagerUnloadCAs(cm);
  30736. wolfSSL_CertManagerFree(cm);
  30737. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  30738. printf("cert manager new failed\n");
  30739. return -1;
  30740. }
  30741. ret = wolfSSL_CertManagerUnloadCAs(cm);
  30742. if (ret != WOLFSSL_SUCCESS)
  30743. return -1;
  30744. wolfSSL_CertManagerFree(cm);
  30745. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  30746. * this time followed by 3 ICA's, should fail */
  30747. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  30748. printf("cert manager new failed\n");
  30749. return -1;
  30750. }
  30751. AssertIntLT(test_chainJ(cm), 0);
  30752. wolfSSL_CertManagerUnloadCAs(cm);
  30753. wolfSSL_CertManagerFree(cm);
  30754. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  30755. printf("cert manager new failed\n");
  30756. return -1;
  30757. }
  30758. ret = wolfSSL_CertManagerUnloadCAs(cm);
  30759. wolfSSL_CertManagerFree(cm);
  30760. return ret;
  30761. }
  30762. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  30763. /*----------------------------------------------------------------------------*
  30764. | Main
  30765. *----------------------------------------------------------------------------*/
  30766. void ApiTest(void)
  30767. {
  30768. printf("\n-----------------Porting tests------------------\n");
  30769. AssertTrue(test_fileAccess());
  30770. printf(" Begin API Tests\n");
  30771. AssertIntEQ(test_wolfSSL_Init(), WOLFSSL_SUCCESS);
  30772. /* wolfcrypt initialization tests */
  30773. test_wolfSSL_Method_Allocators();
  30774. #ifndef NO_WOLFSSL_SERVER
  30775. test_wolfSSL_CTX_new(wolfSSLv23_server_method());
  30776. #endif
  30777. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  30778. (!defined(NO_RSA) || defined(HAVE_ECC))
  30779. test_for_double_Free();
  30780. #endif
  30781. test_wolfSSL_CTX_use_certificate_file();
  30782. AssertIntEQ(test_wolfSSL_CTX_use_certificate_buffer(), WOLFSSL_SUCCESS);
  30783. test_wolfSSL_CTX_use_PrivateKey_file();
  30784. test_wolfSSL_CTX_load_verify_locations();
  30785. test_wolfSSL_CertManagerLoadCABuffer();
  30786. test_wolfSSL_CertManagerGetCerts();
  30787. test_wolfSSL_CertManagerSetVerify();
  30788. test_wolfSSL_CertManagerNameConstraint();
  30789. test_wolfSSL_CertManagerCRL();
  30790. test_wolfSSL_CTX_load_verify_locations_ex();
  30791. test_wolfSSL_CTX_load_verify_buffer_ex();
  30792. test_wolfSSL_CTX_load_verify_chain_buffer_format();
  30793. test_wolfSSL_CTX_use_certificate_chain_file_format();
  30794. test_wolfSSL_CTX_trust_peer_cert();
  30795. test_wolfSSL_CTX_SetTmpDH_file();
  30796. test_wolfSSL_CTX_SetTmpDH_buffer();
  30797. test_wolfSSL_CTX_SetMinMaxDhKey_Sz();
  30798. test_wolfSSL_CTX_der_load_verify_locations();
  30799. test_wolfSSL_CTX_enable_disable();
  30800. test_server_wolfSSL_new();
  30801. test_client_wolfSSL_new();
  30802. test_wolfSSL_SetTmpDH_file();
  30803. test_wolfSSL_SetTmpDH_buffer();
  30804. test_wolfSSL_SetMinMaxDhKey_Sz();
  30805. test_SetTmpEC_DHE_Sz();
  30806. test_wolfSSL_dtls_set_mtu();
  30807. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  30808. defined(HAVE_IO_TESTS_DEPENDENCIES)
  30809. test_wolfSSL_read_write();
  30810. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  30811. test_wolfSSL_reuse_WOLFSSLobj();
  30812. #endif
  30813. test_wolfSSL_dtls_export();
  30814. #endif
  30815. AssertIntEQ(test_wolfSSL_SetMinVersion(), WOLFSSL_SUCCESS);
  30816. AssertIntEQ(test_wolfSSL_CTX_SetMinVersion(), WOLFSSL_SUCCESS);
  30817. /* TLS extensions tests */
  30818. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  30819. test_wolfSSL_UseSNI();
  30820. #endif
  30821. test_wolfSSL_UseTrustedCA();
  30822. test_wolfSSL_UseMaxFragment();
  30823. test_wolfSSL_UseTruncatedHMAC();
  30824. test_wolfSSL_UseSupportedCurve();
  30825. test_wolfSSL_UseALPN();
  30826. test_wolfSSL_DisableExtendedMasterSecret();
  30827. test_wolfSSL_wolfSSL_UseSecureRenegotiation();
  30828. /* X509 tests */
  30829. test_wolfSSL_X509_NAME_get_entry();
  30830. test_wolfSSL_PKCS12();
  30831. test_wolfSSL_no_password_cb();
  30832. test_wolfSSL_PKCS8();
  30833. test_wolfSSL_PKCS8_ED25519();
  30834. test_wolfSSL_PKCS8_ED448();
  30835. test_wolfSSL_PKCS5();
  30836. test_wolfSSL_URI();
  30837. test_wolfSSL_TBS();
  30838. test_wolfSSL_X509_verify();
  30839. test_wc_PemToDer();
  30840. test_wc_AllocDer();
  30841. test_wc_CertPemToDer();
  30842. test_wc_PubKeyPemToDer();
  30843. test_wc_PemPubKeyToDer();
  30844. /*OCSP Stapling. */
  30845. AssertIntEQ(test_wolfSSL_UseOCSPStapling(), WOLFSSL_SUCCESS);
  30846. AssertIntEQ(test_wolfSSL_UseOCSPStaplingV2(), WOLFSSL_SUCCESS);
  30847. /* Multicast */
  30848. test_wolfSSL_mcast();
  30849. /* compatibility tests */
  30850. test_wolfSSL_X509_NAME();
  30851. test_wolfSSL_X509_INFO();
  30852. test_wolfSSL_X509_subject_name_hash();
  30853. test_wolfSSL_X509_issuer_name_hash();
  30854. test_wolfSSL_DES();
  30855. test_wolfSSL_certs();
  30856. test_wolfSSL_ASN1_TIME_print();
  30857. test_wolfSSL_ASN1_UTCTIME_print();
  30858. test_wolfSSL_ASN1_GENERALIZEDTIME_free();
  30859. test_wolfSSL_private_keys();
  30860. test_wolfSSL_PEM_PrivateKey();
  30861. test_wolfSSL_PEM_bio_RSAKey();
  30862. test_wolfSSL_PEM_bio_DSAKey();
  30863. test_wolfSSL_PEM_bio_ECKey();
  30864. test_wolfSSL_PEM_RSAPrivateKey();
  30865. test_wolfSSL_PEM_PUBKEY();
  30866. test_DSA_do_sign_verify();
  30867. test_wolfSSL_tmp_dh();
  30868. test_wolfSSL_ctrl();
  30869. test_wolfSSL_EVP_MD_size();
  30870. test_wolfSSL_EVP_Digest();
  30871. test_wolfSSL_EVP_PKEY_new_mac_key();
  30872. test_wolfSSL_EVP_MD_hmac_signing();
  30873. test_wolfSSL_EVP_MD_rsa_signing();
  30874. test_wolfSSL_EVP_MD_ecc_signing();
  30875. test_wolfSSL_CTX_add_extra_chain_cert();
  30876. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  30877. test_wolfSSL_ERR_peek_last_error_line();
  30878. #endif
  30879. test_wolfSSL_ERR_print_errors_cb();
  30880. AssertFalse(test_wolfSSL_GetLoggingCb());
  30881. AssertFalse(test_WOLFSSL_ERROR_MSG());
  30882. AssertFalse(test_wc_ERR_remove_state());
  30883. AssertFalse(test_wc_ERR_print_errors_fp());
  30884. test_wolfSSL_set_options();
  30885. test_wolfSSL_sk_SSL_CIPHER();
  30886. test_wolfSSL_X509_STORE_CTX();
  30887. test_wolfSSL_X509_STORE_CTX_get0_current_issuer();
  30888. test_wolfSSL_msgCb();
  30889. test_wolfSSL_either_side();
  30890. test_wolfSSL_DTLS_either_side();
  30891. test_generate_cookie();
  30892. test_wolfSSL_X509_STORE_set_flags();
  30893. test_wolfSSL_X509_LOOKUP_load_file();
  30894. test_wolfSSL_X509_NID();
  30895. test_wolfSSL_X509_STORE_CTX_set_time();
  30896. test_wolfSSL_get0_param();
  30897. test_wolfSSL_X509_VERIFY_PARAM_set1_host();
  30898. test_wolfSSL_X509_STORE_CTX_get0_store();
  30899. test_wolfSSL_X509_STORE();
  30900. test_wolfSSL_X509_STORE_load_locations();
  30901. test_wolfSSL_BN();
  30902. test_wolfSSL_PEM_read_bio();
  30903. test_wolfSSL_BIO();
  30904. test_wolfSSL_ASN1_STRING();
  30905. test_wolfSSL_ASN1_BIT_STRING();
  30906. test_wolfSSL_X509();
  30907. test_wolfSSL_X509_VERIFY_PARAM();
  30908. test_wolfSSL_X509_sign();
  30909. test_wolfSSL_X509_get0_tbs_sigalg();
  30910. test_wolfSSL_X509_ALGOR_get0();
  30911. test_wolfSSL_X509_get_X509_PUBKEY();
  30912. test_wolfSSL_X509_PUBKEY();
  30913. test_wolfSSL_RAND();
  30914. test_wolfSSL_BUF();
  30915. test_wolfSSL_set_tlsext_status_type();
  30916. test_wolfSSL_ASN1_TIME_adj();
  30917. test_wolfSSL_X509_cmp_time();
  30918. test_wolfSSL_X509_time_adj();
  30919. test_wolfSSL_CTX_set_client_CA_list();
  30920. test_wolfSSL_CTX_add_client_CA();
  30921. test_wolfSSL_CTX_set_srp_username();
  30922. test_wolfSSL_CTX_set_srp_password();
  30923. test_wolfSSL_pseudo_rand();
  30924. test_wolfSSL_PKCS8_Compat();
  30925. test_wolfSSL_PKCS8_d2i();
  30926. test_wolfSSL_ERR_put_error();
  30927. test_wolfSSL_ERR_print_errors();
  30928. test_wolfSSL_HMAC();
  30929. test_wolfSSL_OBJ();
  30930. test_wolfSSL_i2a_ASN1_OBJECT();
  30931. test_wolfSSL_OBJ_cmp();
  30932. test_wolfSSL_OBJ_txt2nid();
  30933. test_wolfSSL_OBJ_txt2obj();
  30934. test_wolfSSL_X509_NAME_ENTRY();
  30935. test_wolfSSL_X509_set_name();
  30936. test_wolfSSL_X509_set_notAfter();
  30937. test_wolfSSL_X509_set_notBefore();
  30938. test_wolfSSL_X509_set_version();
  30939. test_wolfSSL_BIO_gets();
  30940. test_wolfSSL_BIO_puts();
  30941. test_wolfSSL_BIO_should_retry();
  30942. test_wolfSSL_d2i_PUBKEY();
  30943. test_wolfSSL_BIO_write();
  30944. test_wolfSSL_BIO_printf();
  30945. test_wolfSSL_BIO_f_md();
  30946. test_wolfSSL_SESSION();
  30947. test_wolfSSL_DES_ecb_encrypt();
  30948. test_wolfSSL_sk_GENERAL_NAME();
  30949. test_wolfSSL_MD4();
  30950. test_wolfSSL_RSA();
  30951. test_wolfSSL_RSA_DER();
  30952. test_wolfSSL_RSA_get0_key();
  30953. test_wolfSSL_RSA_meth();
  30954. test_wolfSSL_verify_depth();
  30955. test_wolfSSL_HMAC_CTX();
  30956. test_wolfSSL_msg_callback();
  30957. test_wolfSSL_SHA();
  30958. test_wolfSSL_DH_1536_prime();
  30959. test_wolfSSL_PEM_write_DHparams();
  30960. test_wolfSSL_AES_ecb_encrypt();
  30961. test_wolfSSL_SHA256();
  30962. test_wolfSSL_X509_get_serialNumber();
  30963. test_wolfSSL_X509_CRL();
  30964. test_wolfSSL_PEM_read_X509();
  30965. test_wolfSSL_PEM_read();
  30966. test_wolfSSL_PEM_X509_INFO_read_bio();
  30967. test_wolfSSL_PEM_read_bio_ECPKParameters();
  30968. test_wolfSSL_X509_NAME_ENTRY_get_object();
  30969. test_wolfSSL_OpenSSL_add_all_algorithms();
  30970. test_wolfSSL_ASN1_STRING_print_ex();
  30971. test_wolfSSL_ASN1_TIME_to_generalizedtime();
  30972. test_wolfSSL_ASN1_INTEGER_set();
  30973. test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS();
  30974. test_wolfSSL_i2c_ASN1_INTEGER();
  30975. test_wolfSSL_X509_check_ca();
  30976. test_wolfSSL_X509_check_ip_asc();
  30977. test_wolfSSL_DC_cert();
  30978. test_wolfSSL_DES_ncbc();
  30979. test_wolfSSL_AES_cbc_encrypt();
  30980. test_wolfssl_EVP_aes_gcm();
  30981. test_wolfSSL_PKEY_up_ref();
  30982. test_wolfSSL_i2d_PrivateKey();
  30983. test_wolfSSL_OCSP_get0_info();
  30984. test_wolfSSL_EVP_PKEY_derive();
  30985. test_wolfSSL_RSA_padding_add_PKCS1_PSS();
  30986. #if defined(OPENSSL_ALL)
  30987. test_wolfSSL_X509_PUBKEY_get();
  30988. test_wolfSSL_sk_CIPHER_description();
  30989. test_wolfSSL_get_ciphers_compat();
  30990. test_wolfSSL_d2i_DHparams();
  30991. test_wolfSSL_i2d_DHparams();
  30992. test_wolfSSL_ASN1_STRING_to_UTF8();
  30993. test_wolfSSL_EC_KEY_dup();
  30994. test_wolfSSL_EVP_PKEY_set1_get1_DSA();
  30995. test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY();
  30996. test_wolfSSL_EVP_PKEY_set1_get1_DH();
  30997. test_wolfSSL_CTX_ctrl();
  30998. test_wolfSSL_DH_check();
  30999. test_wolfSSL_EVP_PKEY_assign();
  31000. test_wolfSSL_OBJ_ln();
  31001. test_wolfSSL_OBJ_sn();
  31002. #endif /* OPENSSL_ALL */
  31003. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  31004. AssertIntEQ(test_wolfSSL_CTX_use_certificate_ASN1(), WOLFSSL_SUCCESS);
  31005. test_wolfSSL_d2i_PrivateKeys_bio();
  31006. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  31007. test_wolfSSL_X509_CA_num();
  31008. test_wolfSSL_X509_get_version();
  31009. test_wolfSSL_X509_print();
  31010. test_wolfSSL_BIO_get_len();
  31011. test_wolfSSL_RSA_verify();
  31012. test_wolfSSL_X509V3_EXT_get();
  31013. test_wolfSSL_X509V3_EXT_d2i();
  31014. test_wolfSSL_X509_get_ext();
  31015. test_wolfSSL_X509_get_ext_by_NID();
  31016. test_wolfSSL_X509_get_ext_count();
  31017. test_wolfSSL_X509_EXTENSION_new();
  31018. test_wolfSSL_X509_EXTENSION_get_object();
  31019. test_wolfSSL_X509_EXTENSION_get_data();
  31020. test_wolfSSL_X509_EXTENSION_get_critical();
  31021. test_wolfSSL_X509V3_EXT_print();
  31022. test_wolfSSL_X509_cmp();
  31023. test_wolfSSL_RSA_print();
  31024. test_wolfSSL_ASN1_STRING_print();
  31025. test_openssl_generate_key_and_cert();
  31026. test_wolfSSL_EC_get_builtin_curves();
  31027. test_wolfSSL_CRYPTO_memcmp();
  31028. /* test the no op functions for compatibility */
  31029. test_no_op_functions();
  31030. /* OpenSSL EVP_PKEY API tests */
  31031. test_EVP_PKEY_rsa();
  31032. test_wolfSSL_EVP_PKEY_encrypt();
  31033. test_wolfSSL_EVP_PKEY_sign();
  31034. test_EVP_PKEY_ec();
  31035. test_EVP_PKEY_cmp();
  31036. /* OpenSSL error API tests */
  31037. test_ERR_load_crypto_strings();
  31038. /* OpenSSL sk_X509 API test */
  31039. test_sk_X509();
  31040. /* OpenSSL X509 API test */
  31041. test_X509_get_signature_nid();
  31042. /* OpenSSL X509 REQ API test */
  31043. test_X509_REQ();
  31044. /* OpenSSL PKCS7 API test */
  31045. test_wolfssl_PKCS7();
  31046. test_wolfSSL_PKCS7_SIGNED_new();
  31047. test_wolfSSL_PEM_write_bio_PKCS7();
  31048. /* wolfCrypt ASN tests */
  31049. test_wc_GetPkcs8TraditionalOffset();
  31050. test_wc_SetSubjectRaw();
  31051. test_wc_GetSubjectRaw();
  31052. test_wc_SetIssuerRaw();
  31053. test_wc_SetIssueBuffer();
  31054. test_wc_SetSubjectKeyId();
  31055. test_wc_SetSubject();
  31056. test_CheckCertSignature();
  31057. /* wolfCrypt ECC tests */
  31058. test_wc_ecc_get_curve_size_from_name();
  31059. test_wc_ecc_get_curve_id_from_name();
  31060. test_wc_ecc_get_curve_id_from_params();
  31061. #ifdef WOLFSSL_TLS13
  31062. /* TLS v1.3 API tests */
  31063. test_tls13_apis();
  31064. #endif
  31065. #ifndef NO_CERTS
  31066. /* Bad certificate signature tests */
  31067. AssertIntEQ(test_EccSigFailure_cm(), ASN_SIG_CONFIRM_E);
  31068. AssertIntEQ(test_RsaSigFailure_cm(), ASN_SIG_CONFIRM_E);
  31069. #endif /* NO_CERTS */
  31070. #ifdef HAVE_PK_CALLBACKS
  31071. /* public key callback tests */
  31072. test_DhCallbacks();
  31073. #endif
  31074. /*wolfcrypt */
  31075. printf("\n-----------------wolfcrypt unit tests------------------\n");
  31076. AssertFalse(test_wolfCrypt_Init());
  31077. AssertFalse(test_wc_InitMd5());
  31078. AssertFalse(test_wc_Md5Update());
  31079. AssertFalse(test_wc_Md5Final());
  31080. AssertFalse(test_wc_InitSha());
  31081. AssertFalse(test_wc_ShaUpdate());
  31082. AssertFalse(test_wc_ShaFinal());
  31083. AssertFalse(test_wc_InitSha256());
  31084. AssertFalse(test_wc_Sha256Update());
  31085. AssertFalse(test_wc_Sha256Final());
  31086. AssertFalse(test_wc_Sha256FinalRaw());
  31087. AssertFalse(test_wc_Sha256GetFlags());
  31088. AssertFalse(test_wc_Sha256Free());
  31089. AssertFalse(test_wc_Sha256GetHash());
  31090. AssertFalse(test_wc_Sha256Copy());
  31091. AssertFalse(test_wc_InitSha512());
  31092. AssertFalse(test_wc_Sha512Update());
  31093. AssertFalse(test_wc_Sha512Final());
  31094. AssertFalse(test_wc_Sha512GetFlags());
  31095. AssertFalse(test_wc_Sha512FinalRaw());
  31096. AssertFalse(test_wc_Sha512Free());
  31097. AssertFalse(test_wc_Sha512GetHash());
  31098. AssertFalse(test_wc_Sha512Copy());
  31099. AssertFalse(test_wc_InitSha384());
  31100. AssertFalse(test_wc_Sha384Update());
  31101. AssertFalse(test_wc_Sha384Final());
  31102. AssertFalse(test_wc_Sha384GetFlags());
  31103. AssertFalse(test_wc_Sha384FinalRaw());
  31104. AssertFalse(test_wc_Sha384Free());
  31105. AssertFalse(test_wc_Sha384GetHash());
  31106. AssertFalse(test_wc_Sha384Copy());
  31107. AssertFalse(test_wc_InitSha224());
  31108. AssertFalse(test_wc_Sha224Update());
  31109. AssertFalse(test_wc_Sha224Final());
  31110. AssertFalse(test_wc_Sha224SetFlags());
  31111. AssertFalse(test_wc_Sha224GetFlags());
  31112. AssertFalse(test_wc_Sha224Free());
  31113. AssertFalse(test_wc_Sha224GetHash());
  31114. AssertFalse(test_wc_Sha224Copy());
  31115. AssertFalse(test_wc_InitBlake2b());
  31116. AssertFalse(test_wc_InitRipeMd());
  31117. AssertFalse(test_wc_RipeMdUpdate());
  31118. AssertFalse(test_wc_RipeMdFinal());
  31119. AssertIntEQ(test_wc_InitSha3(), 0);
  31120. AssertIntEQ(testing_wc_Sha3_Update(), 0);
  31121. AssertIntEQ(test_wc_Sha3_224_Final(), 0);
  31122. AssertIntEQ(test_wc_Sha3_256_Final(), 0);
  31123. AssertIntEQ(test_wc_Sha3_384_Final(), 0);
  31124. AssertIntEQ(test_wc_Sha3_512_Final(), 0);
  31125. AssertIntEQ(test_wc_Sha3_224_Copy(), 0);
  31126. AssertIntEQ(test_wc_Sha3_256_Copy(), 0);
  31127. AssertIntEQ(test_wc_Sha3_384_Copy(), 0);
  31128. AssertIntEQ(test_wc_Sha3_512_Copy(), 0);
  31129. AssertIntEQ(test_wc_Sha3_GetFlags(), 0);
  31130. AssertIntEQ(test_wc_InitShake256(), 0);
  31131. AssertIntEQ(testing_wc_Shake256_Update(), 0);
  31132. AssertIntEQ(test_wc_Shake256_Final(), 0);
  31133. AssertIntEQ(test_wc_Shake256_Copy(), 0);
  31134. AssertIntEQ(test_wc_Shake256Hash(), 0);
  31135. AssertFalse(test_wc_Md5HmacSetKey());
  31136. AssertFalse(test_wc_Md5HmacUpdate());
  31137. AssertFalse(test_wc_Md5HmacFinal());
  31138. AssertFalse(test_wc_ShaHmacSetKey());
  31139. AssertFalse(test_wc_ShaHmacUpdate());
  31140. AssertFalse(test_wc_ShaHmacFinal());
  31141. AssertFalse(test_wc_Sha224HmacSetKey());
  31142. AssertFalse(test_wc_Sha224HmacUpdate());
  31143. AssertFalse(test_wc_Sha224HmacFinal());
  31144. AssertFalse(test_wc_Sha256HmacSetKey());
  31145. AssertFalse(test_wc_Sha256HmacUpdate());
  31146. AssertFalse(test_wc_Sha256HmacFinal());
  31147. AssertFalse(test_wc_Sha384HmacSetKey());
  31148. AssertFalse(test_wc_Sha384HmacUpdate());
  31149. AssertFalse(test_wc_Sha384HmacFinal());
  31150. AssertIntEQ(test_wc_HashInit(), 0);
  31151. AssertIntEQ(test_wc_HashSetFlags(), 0);
  31152. AssertIntEQ(test_wc_HashGetFlags(), 0);
  31153. AssertIntEQ(test_wc_InitCmac(), 0);
  31154. AssertIntEQ(test_wc_CmacUpdate(), 0);
  31155. AssertIntEQ(test_wc_CmacFinal(), 0);
  31156. AssertIntEQ(test_wc_AesCmacGenerate(), 0);
  31157. AssertIntEQ(test_wc_Des3_SetIV(), 0);
  31158. AssertIntEQ(test_wc_Des3_SetKey(), 0);
  31159. AssertIntEQ(test_wc_Des3_CbcEncryptDecrypt(), 0);
  31160. AssertIntEQ(test_wc_Des3_CbcEncryptDecryptWithKey(), 0);
  31161. AssertIntEQ(test_wc_IdeaSetKey(), 0);
  31162. AssertIntEQ(test_wc_IdeaSetIV(), 0);
  31163. AssertIntEQ(test_wc_IdeaCipher(), 0);
  31164. AssertIntEQ(test_wc_IdeaCbcEncyptDecrypt(), 0);
  31165. AssertIntEQ(test_wc_Chacha_SetKey(), 0);
  31166. AssertIntEQ(test_wc_Chacha_Process(), 0);
  31167. AssertIntEQ(test_wc_ChaCha20Poly1305_aead(), 0);
  31168. AssertIntEQ(test_wc_Poly1305SetKey(), 0);
  31169. AssertIntEQ(test_wc_CamelliaSetKey(), 0);
  31170. AssertIntEQ(test_wc_CamelliaSetIV(), 0);
  31171. AssertIntEQ(test_wc_CamelliaEncryptDecryptDirect(), 0);
  31172. AssertIntEQ(test_wc_CamelliaCbcEncryptDecrypt(), 0);
  31173. AssertIntEQ(test_wc_RabbitSetKey(), 0);
  31174. AssertIntEQ(test_wc_RabbitProcess(), 0);
  31175. AssertIntEQ(test_wc_Arc4SetKey(), 0);
  31176. AssertIntEQ(test_wc_Arc4Process(), 0);
  31177. AssertIntEQ(test_wc_AesSetKey(), 0);
  31178. AssertIntEQ(test_wc_AesSetIV(), 0);
  31179. AssertIntEQ(test_wc_AesCbcEncryptDecrypt(), 0);
  31180. AssertIntEQ(test_wc_AesCtrEncryptDecrypt(), 0);
  31181. AssertIntEQ(test_wc_AesGcmSetKey(), 0);
  31182. AssertIntEQ(test_wc_AesGcmEncryptDecrypt(), 0);
  31183. AssertIntEQ(test_wc_GmacSetKey(), 0);
  31184. AssertIntEQ(test_wc_GmacUpdate(), 0);
  31185. AssertIntEQ(test_wc_InitRsaKey(), 0);
  31186. AssertIntEQ(test_wc_RsaPrivateKeyDecode(), 0);
  31187. AssertIntEQ(test_wc_RsaPublicKeyDecode(), 0);
  31188. AssertIntEQ(test_wc_RsaPublicKeyDecodeRaw(), 0);
  31189. AssertIntEQ(test_wc_MakeRsaKey(), 0);
  31190. AssertIntEQ(test_wc_SetKeyUsage (), 0);
  31191. AssertIntEQ(test_wc_SetMutexCb(), 0);
  31192. AssertIntEQ(test_wc_LockMutex_ex(), 0);
  31193. AssertIntEQ(test_wc_RsaKeyToDer(), 0);
  31194. AssertIntEQ(test_wc_RsaKeyToPublicDer(), 0);
  31195. AssertIntEQ(test_wc_RsaPublicEncryptDecrypt(), 0);
  31196. AssertIntEQ(test_wc_RsaPublicEncryptDecrypt_ex(), 0);
  31197. AssertIntEQ(test_wc_RsaEncryptSize(), 0);
  31198. AssertIntEQ(test_wc_RsaSSL_SignVerify(), 0);
  31199. AssertIntEQ(test_wc_RsaFlattenPublicKey(), 0);
  31200. AssertIntEQ(test_RsaDecryptBoundsCheck(), 0);
  31201. AssertIntEQ(test_wc_AesCcmSetKey(), 0);
  31202. AssertIntEQ(test_wc_AesCcmEncryptDecrypt(), 0);
  31203. AssertIntEQ(test_wc_Hc128_SetKey(), 0);
  31204. AssertIntEQ(test_wc_Hc128_Process(), 0);
  31205. AssertIntEQ(test_wc_InitDsaKey(), 0);
  31206. AssertIntEQ(test_wc_DsaSignVerify(), 0);
  31207. AssertIntEQ(test_wc_DsaPublicPrivateKeyDecode(), 0);
  31208. AssertIntEQ(test_wc_MakeDsaKey(), 0);
  31209. AssertIntEQ(test_wc_DsaKeyToDer(), 0);
  31210. AssertIntEQ(test_wc_DsaKeyToPublicDer(), 0);
  31211. AssertIntEQ(test_wc_DsaImportParamsRaw(), 0);
  31212. AssertIntEQ(test_wc_DsaImportParamsRawCheck(), 0);
  31213. AssertIntEQ(test_wc_DsaExportParamsRaw(), 0);
  31214. AssertIntEQ(test_wc_DsaExportKeyRaw(), 0);
  31215. AssertIntEQ(test_wc_SignatureGetSize_ecc(), 0);
  31216. AssertIntEQ(test_wc_SignatureGetSize_rsa(), 0);
  31217. wolfCrypt_Cleanup();
  31218. #ifdef OPENSSL_EXTRA
  31219. /*wolfSSL_EVP_get_cipherbynid test*/
  31220. test_wolfSSL_EVP_get_cipherbynid();
  31221. test_wolfSSL_EVP_CIPHER_CTX();
  31222. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  31223. test_wolfSSL_EC();
  31224. #endif
  31225. test_wolfSSL_ECDSA_SIG();
  31226. test_ECDSA_size_sign();
  31227. test_ED25519();
  31228. test_ED448();
  31229. #endif
  31230. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  31231. !defined(HAVE_SELFTEST) && \
  31232. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  31233. test_wc_ecc_get_curve_id_from_dp_params();
  31234. #endif
  31235. #ifdef HAVE_HASHDRBG
  31236. #ifdef TEST_RESEED_INTERVAL
  31237. AssertIntEQ(test_wc_RNG_GenerateBlock_Reseed(), 0);
  31238. #endif
  31239. AssertIntEQ(test_wc_RNG_GenerateBlock(), 0);
  31240. #endif
  31241. AssertIntEQ(test_get_rand_digit(), 0);
  31242. AssertIntEQ(test_get_digit_count(), 0);
  31243. AssertIntEQ(test_mp_cond_copy(), 0);
  31244. AssertIntEQ(test_mp_rand(), 0);
  31245. AssertIntEQ(test_get_digit(), 0);
  31246. AssertIntEQ(test_wc_export_int(), 0);
  31247. AssertIntEQ(test_wc_InitRngNonce(), 0);
  31248. AssertIntEQ(test_wc_InitRngNonce_ex(), 0);
  31249. AssertIntEQ(test_wc_ed25519_make_key(), 0);
  31250. AssertIntEQ(test_wc_ed25519_init(), 0);
  31251. AssertIntEQ(test_wc_ed25519_sign_msg(), 0);
  31252. AssertIntEQ(test_wc_ed25519_import_public(), 0);
  31253. AssertIntEQ(test_wc_ed25519_import_private_key(), 0);
  31254. AssertIntEQ(test_wc_ed25519_export(), 0);
  31255. AssertIntEQ(test_wc_ed25519_size(), 0);
  31256. AssertIntEQ(test_wc_ed25519_exportKey(), 0);
  31257. AssertIntEQ(test_wc_Ed25519PublicKeyToDer(), 0);
  31258. AssertIntEQ(test_wc_curve25519_init(), 0);
  31259. AssertIntEQ(test_wc_curve25519_size(), 0);
  31260. AssertIntEQ(test_wc_curve25519_export_key_raw(), 0);
  31261. AssertIntEQ(test_wc_curve25519_export_key_raw_ex(), 0);
  31262. AssertIntEQ(test_wc_curve25519_size (), 0);
  31263. AssertIntEQ(test_wc_curve25519_make_key (), 0);
  31264. AssertIntEQ(test_wc_curve25519_shared_secret_ex (), 0);
  31265. AssertIntEQ(test_wc_curve25519_make_pub (), 0);
  31266. AssertIntEQ(test_wc_curve25519_export_public_ex (), 0);
  31267. AssertIntEQ(test_wc_curve25519_export_private_raw_ex (), 0);
  31268. AssertIntEQ(test_wc_curve25519_import_private_raw_ex (), 0);
  31269. AssertIntEQ(test_wc_curve25519_import_private (), 0);
  31270. AssertIntEQ(test_wc_ed448_make_key(), 0);
  31271. AssertIntEQ(test_wc_ed448_init(), 0);
  31272. AssertIntEQ(test_wc_ed448_sign_msg(), 0);
  31273. AssertIntEQ(test_wc_ed448_import_public(), 0);
  31274. AssertIntEQ(test_wc_ed448_import_private_key(), 0);
  31275. AssertIntEQ(test_wc_ed448_export(), 0);
  31276. AssertIntEQ(test_wc_ed448_size(), 0);
  31277. AssertIntEQ(test_wc_ed448_exportKey(), 0);
  31278. AssertIntEQ(test_wc_Ed448PublicKeyToDer(), 0);
  31279. AssertIntEQ(test_wc_curve448_make_key (), 0);
  31280. AssertIntEQ(test_wc_curve448_shared_secret_ex (), 0);
  31281. AssertIntEQ(test_wc_curve448_export_public_ex (), 0);
  31282. AssertIntEQ(test_wc_curve448_export_private_raw_ex (), 0);
  31283. AssertIntEQ(test_wc_curve448_export_key_raw (), 0);
  31284. AssertIntEQ(test_wc_curve448_import_private_raw_ex (), 0);
  31285. AssertIntEQ(test_wc_curve448_import_private (), 0);
  31286. AssertIntEQ(test_wc_curve448_init(), 0);
  31287. AssertIntEQ(test_wc_curve448_size (), 0);
  31288. AssertIntEQ(test_wc_ecc_make_key(), 0);
  31289. AssertIntEQ(test_wc_ecc_init(), 0);
  31290. AssertIntEQ(test_wc_ecc_check_key(), 0);
  31291. AssertIntEQ(test_wc_ecc_get_generator(), 0);
  31292. AssertIntEQ(test_wc_ecc_size(), 0);
  31293. test_wc_ecc_params();
  31294. AssertIntEQ(test_wc_ecc_signVerify_hash(), 0);
  31295. AssertIntEQ(test_wc_ecc_shared_secret(), 0);
  31296. AssertIntEQ(test_wc_ecc_export_x963(), 0);
  31297. AssertIntEQ(test_wc_ecc_export_x963_ex(), 0);
  31298. AssertIntEQ(test_wc_ecc_import_x963(), 0);
  31299. AssertIntEQ(ecc_import_private_key(), 0);
  31300. AssertIntEQ(test_wc_ecc_export_private_only(), 0);
  31301. AssertIntEQ(test_wc_ecc_rs_to_sig(), 0);
  31302. AssertIntEQ(test_wc_ecc_import_raw(), 0);
  31303. AssertIntEQ(test_wc_ecc_import_unsigned(), 0);
  31304. AssertIntEQ(test_wc_ecc_sig_size(), 0);
  31305. AssertIntEQ(test_wc_ecc_ctx_new(), 0);
  31306. AssertIntEQ(test_wc_ecc_ctx_reset(), 0);
  31307. AssertIntEQ(test_wc_ecc_ctx_set_peer_salt(), 0);
  31308. AssertIntEQ(test_wc_ecc_ctx_set_info(), 0);
  31309. AssertIntEQ(test_wc_ecc_encryptDecrypt(), 0);
  31310. AssertIntEQ(test_wc_ecc_del_point(), 0);
  31311. AssertIntEQ(test_wc_ecc_pointFns(), 0);
  31312. AssertIntEQ(test_wc_ecc_shared_secret_ssh(), 0);
  31313. AssertIntEQ(test_wc_ecc_verify_hash_ex(), 0);
  31314. AssertIntEQ(test_wc_ecc_mulmod(), 0);
  31315. AssertIntEQ(test_wc_ecc_is_valid_idx(), 0);
  31316. AssertIntEQ(test_ToTraditional(), 0);
  31317. AssertIntEQ(test_wc_EccPrivateKeyToDer(), 0);
  31318. AssertIntEQ(test_wc_Ed25519KeyToDer(), 0);
  31319. AssertIntEQ(test_wc_Ed25519PrivateKeyToDer(), 0);
  31320. AssertIntEQ(test_wc_Ed448KeyToDer(), 0);
  31321. AssertIntEQ(test_wc_Ed448PrivateKeyToDer(), 0);
  31322. AssertIntEQ(test_wc_SetAuthKeyIdFromPublicKey_ex(), 0);
  31323. AssertIntEQ(test_wc_SetSubjectBuffer(), 0);
  31324. AssertIntEQ(test_wc_SetSubjectKeyIdFromPublicKey_ex(), 0);
  31325. test_wc_PKCS7_New();
  31326. test_wc_PKCS7_Init();
  31327. test_wc_PKCS7_InitWithCert();
  31328. test_wc_PKCS7_EncodeData();
  31329. test_wc_PKCS7_EncodeSignedData();
  31330. test_wc_PKCS7_EncodeSignedData_ex();
  31331. test_wc_PKCS7_VerifySignedData();
  31332. test_wc_PKCS7_EncodeDecodeEnvelopedData();
  31333. test_wc_PKCS7_EncodeEncryptedData();
  31334. test_wc_PKCS7_Degenerate();
  31335. test_wc_PKCS7_BER();
  31336. test_PKCS7_signed_enveloped();
  31337. test_wc_i2d_PKCS12();
  31338. test_wolfSSL_CTX_LoadCRL();
  31339. AssertIntEQ(test_ForceZero(), 0);
  31340. AssertIntEQ(test_wolfSSL_Cleanup(), WOLFSSL_SUCCESS);
  31341. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  31342. !defined(NO_CERTS)
  31343. AssertIntEQ(test_various_pathlen_chains(), WOLFSSL_SUCCESS);
  31344. #endif
  31345. /* If at some point a stub get implemented this test should fail indicating
  31346. * a need to implement a new test case
  31347. */
  31348. test_stubs_are_stubs();
  31349. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  31350. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  31351. wc_ecc_fp_free(); /* free per thread cache */
  31352. #endif
  31353. wolfSSL_Cleanup();
  31354. (void)devId;
  31355. printf(" End API Tests\n");
  31356. }